Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2 | * fs/f2fs/data.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/f2fs_fs.h> |
| 13 | #include <linux/buffer_head.h> |
| 14 | #include <linux/mpage.h> |
| 15 | #include <linux/writeback.h> |
| 16 | #include <linux/backing-dev.h> |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 17 | #include <linux/pagevec.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 18 | #include <linux/blkdev.h> |
| 19 | #include <linux/bio.h> |
Geert Uytterhoeven | 690e4a3 | 2012-12-19 22:19:30 +0100 | [diff] [blame] | 20 | #include <linux/prefetch.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 21 | #include <linux/uio.h> |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 22 | #include <linux/cleancache.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 23 | |
| 24 | #include "f2fs.h" |
| 25 | #include "node.h" |
| 26 | #include "segment.h" |
Jaegeuk Kim | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 27 | #include "trace.h" |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 28 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 29 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 30 | static void f2fs_read_end_io(struct bio *bio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 31 | { |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 32 | struct bio_vec *bvec; |
| 33 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 34 | |
Chao Yu | 1237702 | 2015-05-25 18:03:38 +0800 | [diff] [blame] | 35 | if (f2fs_bio_encrypted(bio)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 36 | if (bio->bi_error) { |
Chao Yu | 1237702 | 2015-05-25 18:03:38 +0800 | [diff] [blame] | 37 | f2fs_release_crypto_ctx(bio->bi_private); |
| 38 | } else { |
| 39 | f2fs_end_io_crypto_work(bio->bi_private, bio); |
| 40 | return; |
| 41 | } |
| 42 | } |
| 43 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 44 | bio_for_each_segment_all(bvec, bio, i) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 45 | struct page *page = bvec->bv_page; |
| 46 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 47 | if (!bio->bi_error) { |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 48 | SetPageUptodate(page); |
| 49 | } else { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 50 | ClearPageUptodate(page); |
| 51 | SetPageError(page); |
| 52 | } |
| 53 | unlock_page(page); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 54 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 55 | bio_put(bio); |
| 56 | } |
| 57 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 58 | static void f2fs_write_end_io(struct bio *bio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 59 | { |
Jaegeuk Kim | 1b1f559 | 2014-02-03 10:50:22 +0900 | [diff] [blame] | 60 | struct f2fs_sb_info *sbi = bio->bi_private; |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 61 | struct bio_vec *bvec; |
| 62 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 63 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 64 | bio_for_each_segment_all(bvec, bio, i) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 65 | struct page *page = bvec->bv_page; |
| 66 | |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 67 | f2fs_restore_and_release_control_page(&page); |
| 68 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 69 | if (unlikely(bio->bi_error)) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 70 | set_bit(AS_EIO, &page->mapping->flags); |
Jaegeuk Kim | 744602c | 2014-01-24 09:42:16 +0900 | [diff] [blame] | 71 | f2fs_stop_checkpoint(sbi); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 72 | } |
| 73 | end_page_writeback(page); |
| 74 | dec_page_count(sbi, F2FS_WRITEBACK); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 75 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 76 | |
Jaegeuk Kim | 7c50689 | 2016-01-26 11:55:35 -0800 | [diff] [blame] | 77 | if (!get_pages(sbi, F2FS_WRITEBACK) && wq_has_sleeper(&sbi->cp_wait)) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 78 | wake_up(&sbi->cp_wait); |
| 79 | |
| 80 | bio_put(bio); |
| 81 | } |
| 82 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 83 | /* |
| 84 | * Low-level block read/write IO operations. |
| 85 | */ |
| 86 | static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr, |
| 87 | int npages, bool is_read) |
| 88 | { |
| 89 | struct bio *bio; |
| 90 | |
Jaegeuk Kim | 740432f | 2015-08-14 11:43:56 -0700 | [diff] [blame] | 91 | bio = f2fs_bio_alloc(npages); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 92 | |
| 93 | bio->bi_bdev = sbi->sb->s_bdev; |
Chao Yu | 55cf9cb | 2014-09-15 18:01:10 +0800 | [diff] [blame] | 94 | bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 95 | bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io; |
Chao Yu | 1237702 | 2015-05-25 18:03:38 +0800 | [diff] [blame] | 96 | bio->bi_private = is_read ? NULL : sbi; |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 97 | |
| 98 | return bio; |
| 99 | } |
| 100 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 101 | static void __submit_merged_bio(struct f2fs_bio_info *io) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 102 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 103 | struct f2fs_io_info *fio = &io->fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 104 | |
| 105 | if (!io->bio) |
| 106 | return; |
| 107 | |
Jaegeuk Kim | 6a8f8ca | 2014-10-29 14:37:22 -0700 | [diff] [blame] | 108 | if (is_read_io(fio->rw)) |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 109 | trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio); |
Jaegeuk Kim | 6a8f8ca | 2014-10-29 14:37:22 -0700 | [diff] [blame] | 110 | else |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 111 | trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 112 | |
Jaegeuk Kim | 6a8f8ca | 2014-10-29 14:37:22 -0700 | [diff] [blame] | 113 | submit_bio(fio->rw, io->bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 114 | io->bio = NULL; |
| 115 | } |
| 116 | |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 117 | bool is_merged_page(struct f2fs_sb_info *sbi, struct page *page, |
| 118 | enum page_type type) |
| 119 | { |
| 120 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 121 | struct f2fs_bio_info *io = &sbi->write_io[btype]; |
| 122 | struct bio_vec *bvec; |
| 123 | struct page *target; |
| 124 | int i; |
| 125 | |
| 126 | down_read(&io->io_rwsem); |
| 127 | if (!io->bio) { |
| 128 | up_read(&io->io_rwsem); |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | bio_for_each_segment_all(bvec, io->bio, i) { |
| 133 | |
| 134 | if (bvec->bv_page->mapping) { |
| 135 | target = bvec->bv_page; |
| 136 | } else { |
| 137 | struct f2fs_crypto_ctx *ctx; |
| 138 | |
| 139 | /* encrypted page */ |
| 140 | ctx = (struct f2fs_crypto_ctx *)page_private( |
| 141 | bvec->bv_page); |
| 142 | target = ctx->w.control_page; |
| 143 | } |
| 144 | |
| 145 | if (page == target) { |
| 146 | up_read(&io->io_rwsem); |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | up_read(&io->io_rwsem); |
| 152 | return false; |
| 153 | } |
| 154 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 155 | void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 156 | enum page_type type, int rw) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 157 | { |
| 158 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 159 | struct f2fs_bio_info *io; |
| 160 | |
| 161 | io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype]; |
| 162 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 163 | down_write(&io->io_rwsem); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 164 | |
| 165 | /* change META to META_FLUSH in the checkpoint procedure */ |
| 166 | if (type >= META_FLUSH) { |
| 167 | io->fio.type = META_FLUSH; |
Jaegeuk Kim | 0f7b2ab | 2014-07-23 09:57:31 -0700 | [diff] [blame] | 168 | if (test_opt(sbi, NOBARRIER)) |
| 169 | io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO; |
| 170 | else |
| 171 | io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 172 | } |
| 173 | __submit_merged_bio(io); |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 174 | up_write(&io->io_rwsem); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Fill the locked page with data located in the block address. |
| 179 | * Return unlocked page. |
| 180 | */ |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 181 | int f2fs_submit_page_bio(struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 182 | { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 183 | struct bio *bio; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 184 | struct page *page = fio->encrypted_page ? fio->encrypted_page : fio->page; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 185 | |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 186 | trace_f2fs_submit_page_bio(page, fio); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 187 | f2fs_trace_ios(fio, 0); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 188 | |
| 189 | /* Allocate a new bio */ |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 190 | bio = __bio_alloc(fio->sbi, fio->blk_addr, 1, is_read_io(fio->rw)); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 191 | |
| 192 | if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) { |
| 193 | bio_put(bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 194 | return -EFAULT; |
| 195 | } |
| 196 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 197 | submit_bio(fio->rw, bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 201 | void f2fs_submit_page_mbio(struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 202 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 203 | struct f2fs_sb_info *sbi = fio->sbi; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 204 | enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 205 | struct f2fs_bio_info *io; |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 206 | bool is_read = is_read_io(fio->rw); |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 207 | struct page *bio_page; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 208 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 209 | io = is_read ? &sbi->read_io : &sbi->write_io[btype]; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 210 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 211 | verify_block_addr(sbi, fio->blk_addr); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 212 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 213 | down_write(&io->io_rwsem); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 214 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 215 | if (!is_read) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 216 | inc_page_count(sbi, F2FS_WRITEBACK); |
| 217 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 218 | if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 || |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 219 | io->fio.rw != fio->rw)) |
| 220 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 221 | alloc_new: |
| 222 | if (io->bio == NULL) { |
Jaegeuk Kim | 90a893c | 2014-09-22 16:21:07 -0700 | [diff] [blame] | 223 | int bio_blocks = MAX_BIO_BLOCKS(sbi); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 224 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 225 | io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 226 | io->fio = *fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 227 | } |
| 228 | |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 229 | bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page; |
| 230 | |
| 231 | if (bio_add_page(io->bio, bio_page, PAGE_CACHE_SIZE, 0) < |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 232 | PAGE_CACHE_SIZE) { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 233 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 234 | goto alloc_new; |
| 235 | } |
| 236 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 237 | io->last_block_in_bio = fio->blk_addr; |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 238 | f2fs_trace_ios(fio, 0); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 239 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 240 | up_write(&io->io_rwsem); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 241 | trace_f2fs_submit_page_mbio(fio->page, fio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 245 | * Lock ordering for the change of data block address: |
| 246 | * ->data_page |
| 247 | * ->node_page |
| 248 | * update block addresses in the node page |
| 249 | */ |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 250 | void set_data_blkaddr(struct dnode_of_data *dn) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 251 | { |
| 252 | struct f2fs_node *rn; |
| 253 | __le32 *addr_array; |
| 254 | struct page *node_page = dn->node_page; |
| 255 | unsigned int ofs_in_node = dn->ofs_in_node; |
| 256 | |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 257 | f2fs_wait_on_page_writeback(node_page, NODE, true); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 258 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 259 | rn = F2FS_NODE(node_page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 260 | |
| 261 | /* Get physical address of data block */ |
| 262 | addr_array = blkaddr_in_node(rn); |
Jaegeuk Kim | e1509cf | 2014-12-30 22:57:55 -0800 | [diff] [blame] | 263 | addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr); |
Jaegeuk Kim | 12719ae | 2016-01-07 13:23:12 -0800 | [diff] [blame] | 264 | if (set_page_dirty(node_page)) |
| 265 | dn->node_changed = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int reserve_new_block(struct dnode_of_data *dn) |
| 269 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 270 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 271 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 272 | if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 273 | return -EPERM; |
Chao Yu | cfb271d | 2013-12-05 17:15:22 +0800 | [diff] [blame] | 274 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 275 | return -ENOSPC; |
| 276 | |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 277 | trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node); |
| 278 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 279 | dn->data_blkaddr = NEW_ADDR; |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 280 | set_data_blkaddr(dn); |
Jaegeuk Kim | a18ff06 | 2014-01-21 13:32:12 +0900 | [diff] [blame] | 281 | mark_inode_dirty(dn->inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 282 | sync_inode_page(dn); |
| 283 | return 0; |
| 284 | } |
| 285 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 286 | int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
| 287 | { |
| 288 | bool need_put = dn->inode_page ? false : true; |
| 289 | int err; |
| 290 | |
| 291 | err = get_dnode_of_data(dn, index, ALLOC_NODE); |
| 292 | if (err) |
| 293 | return err; |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 294 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 295 | if (dn->data_blkaddr == NULL_ADDR) |
| 296 | err = reserve_new_block(dn); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 297 | if (err || need_put) |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 298 | f2fs_put_dnode(dn); |
| 299 | return err; |
| 300 | } |
| 301 | |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 302 | int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 303 | { |
Chao Yu | 028a41e | 2015-03-19 19:26:02 +0800 | [diff] [blame] | 304 | struct extent_info ei; |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 305 | struct inode *inode = dn->inode; |
Chao Yu | 028a41e | 2015-03-19 19:26:02 +0800 | [diff] [blame] | 306 | |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 307 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 308 | dn->data_blkaddr = ei.blk + index - ei.fofs; |
| 309 | return 0; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 310 | } |
| 311 | |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 312 | return f2fs_reserve_block(dn, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 313 | } |
| 314 | |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 315 | struct page *get_read_data_page(struct inode *inode, pgoff_t index, |
| 316 | int rw, bool for_write) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 317 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 318 | struct address_space *mapping = inode->i_mapping; |
| 319 | struct dnode_of_data dn; |
| 320 | struct page *page; |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 321 | struct extent_info ei; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 322 | int err; |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 323 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 324 | .sbi = F2FS_I_SB(inode), |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 325 | .type = DATA, |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 326 | .rw = rw, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 327 | .encrypted_page = NULL, |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 328 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 329 | |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 330 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) |
| 331 | return read_mapping_page(mapping, index, NULL); |
| 332 | |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 333 | page = f2fs_grab_cache_page(mapping, index, for_write); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 334 | if (!page) |
| 335 | return ERR_PTR(-ENOMEM); |
| 336 | |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 337 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 338 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 339 | goto got_it; |
| 340 | } |
| 341 | |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 342 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 343 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 344 | if (err) |
| 345 | goto put_err; |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 346 | f2fs_put_dnode(&dn); |
| 347 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 348 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 349 | err = -ENOENT; |
| 350 | goto put_err; |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 351 | } |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 352 | got_it: |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 353 | if (PageUptodate(page)) { |
| 354 | unlock_page(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 355 | return page; |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 356 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 357 | |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 358 | /* |
| 359 | * A new dentry page is allocated but not able to be written, since its |
| 360 | * new inode page couldn't be allocated due to -ENOSPC. |
| 361 | * In such the case, its blkaddr can be remained as NEW_ADDR. |
| 362 | * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata. |
| 363 | */ |
| 364 | if (dn.data_blkaddr == NEW_ADDR) { |
| 365 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 366 | SetPageUptodate(page); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 367 | unlock_page(page); |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 368 | return page; |
| 369 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 370 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 371 | fio.blk_addr = dn.data_blkaddr; |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 372 | fio.page = page; |
| 373 | err = f2fs_submit_page_bio(&fio); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 374 | if (err) |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 375 | goto put_err; |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 376 | return page; |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 377 | |
| 378 | put_err: |
| 379 | f2fs_put_page(page, 1); |
| 380 | return ERR_PTR(err); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 381 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 382 | |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 383 | struct page *find_data_page(struct inode *inode, pgoff_t index) |
| 384 | { |
| 385 | struct address_space *mapping = inode->i_mapping; |
| 386 | struct page *page; |
| 387 | |
| 388 | page = find_get_page(mapping, index); |
| 389 | if (page && PageUptodate(page)) |
| 390 | return page; |
| 391 | f2fs_put_page(page, 0); |
| 392 | |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 393 | page = get_read_data_page(inode, index, READ_SYNC, false); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 394 | if (IS_ERR(page)) |
| 395 | return page; |
| 396 | |
| 397 | if (PageUptodate(page)) |
| 398 | return page; |
| 399 | |
| 400 | wait_on_page_locked(page); |
| 401 | if (unlikely(!PageUptodate(page))) { |
| 402 | f2fs_put_page(page, 0); |
| 403 | return ERR_PTR(-EIO); |
| 404 | } |
| 405 | return page; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * If it tries to access a hole, return an error. |
| 410 | * Because, the callers, functions in dir.c and GC, should be able to know |
| 411 | * whether this page exists or not. |
| 412 | */ |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 413 | struct page *get_lock_data_page(struct inode *inode, pgoff_t index, |
| 414 | bool for_write) |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 415 | { |
| 416 | struct address_space *mapping = inode->i_mapping; |
| 417 | struct page *page; |
| 418 | repeat: |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 419 | page = get_read_data_page(inode, index, READ_SYNC, for_write); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 420 | if (IS_ERR(page)) |
| 421 | return page; |
| 422 | |
| 423 | /* wait for read completion */ |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 424 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 425 | if (unlikely(!PageUptodate(page))) { |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 426 | f2fs_put_page(page, 1); |
| 427 | return ERR_PTR(-EIO); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 428 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 429 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 430 | f2fs_put_page(page, 1); |
| 431 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 432 | } |
| 433 | return page; |
| 434 | } |
| 435 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 436 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 437 | * Caller ensures that this data page is never allocated. |
| 438 | * A new zero-filled data page is allocated in the page cache. |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 439 | * |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 440 | * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 441 | * f2fs_unlock_op(). |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 442 | * Note that, ipage is set only by make_empty_dir, and if any error occur, |
| 443 | * ipage should be released by this function. |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 444 | */ |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 445 | struct page *get_new_data_page(struct inode *inode, |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 446 | struct page *ipage, pgoff_t index, bool new_i_size) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 447 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 448 | struct address_space *mapping = inode->i_mapping; |
| 449 | struct page *page; |
| 450 | struct dnode_of_data dn; |
| 451 | int err; |
Jaegeuk Kim | 7612118 | 2016-01-01 22:03:47 -0800 | [diff] [blame] | 452 | |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 453 | page = f2fs_grab_cache_page(mapping, index, true); |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 454 | if (!page) { |
| 455 | /* |
| 456 | * before exiting, we should make sure ipage will be released |
| 457 | * if any error occur. |
| 458 | */ |
| 459 | f2fs_put_page(ipage, 1); |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 460 | return ERR_PTR(-ENOMEM); |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 461 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 462 | |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 463 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 464 | err = f2fs_reserve_block(&dn, index); |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 465 | if (err) { |
| 466 | f2fs_put_page(page, 1); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 467 | return ERR_PTR(err); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 468 | } |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 469 | if (!ipage) |
| 470 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 471 | |
| 472 | if (PageUptodate(page)) |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 473 | goto got_it; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 474 | |
| 475 | if (dn.data_blkaddr == NEW_ADDR) { |
| 476 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 477 | SetPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 478 | } else { |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 479 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 480 | |
Jaegeuk Kim | 7612118 | 2016-01-01 22:03:47 -0800 | [diff] [blame] | 481 | /* if ipage exists, blkaddr should be NEW_ADDR */ |
| 482 | f2fs_bug_on(F2FS_I_SB(inode), ipage); |
| 483 | page = get_lock_data_page(inode, index, true); |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 484 | if (IS_ERR(page)) |
Jaegeuk Kim | 7612118 | 2016-01-01 22:03:47 -0800 | [diff] [blame] | 485 | return page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 486 | } |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 487 | got_it: |
Chao Yu | 9edcdab | 2015-09-11 14:43:52 +0800 | [diff] [blame] | 488 | if (new_i_size && i_size_read(inode) < |
| 489 | ((loff_t)(index + 1) << PAGE_CACHE_SHIFT)) { |
| 490 | i_size_write(inode, ((loff_t)(index + 1) << PAGE_CACHE_SHIFT)); |
Jaegeuk Kim | 699489b | 2013-06-07 22:08:23 +0900 | [diff] [blame] | 491 | /* Only the directory inode sets new_i_size */ |
| 492 | set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 493 | } |
| 494 | return page; |
| 495 | } |
| 496 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 497 | static int __allocate_data_block(struct dnode_of_data *dn) |
| 498 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 499 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 500 | struct f2fs_summary sum; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 501 | struct node_info ni; |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 502 | int seg = CURSEG_WARM_DATA; |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 503 | pgoff_t fofs; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 504 | |
| 505 | if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))) |
| 506 | return -EPERM; |
Chao Yu | df6136e | 2015-03-23 10:33:37 +0800 | [diff] [blame] | 507 | |
| 508 | dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node); |
| 509 | if (dn->data_blkaddr == NEW_ADDR) |
| 510 | goto alloc; |
| 511 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 512 | if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1))) |
| 513 | return -ENOSPC; |
| 514 | |
Chao Yu | df6136e | 2015-03-23 10:33:37 +0800 | [diff] [blame] | 515 | alloc: |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 516 | get_node_info(sbi, dn->nid, &ni); |
| 517 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
| 518 | |
Jaegeuk Kim | 38aa088 | 2015-01-05 16:02:20 -0800 | [diff] [blame] | 519 | if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page) |
| 520 | seg = CURSEG_DIRECT_IO; |
| 521 | |
Chao Yu | df6136e | 2015-03-23 10:33:37 +0800 | [diff] [blame] | 522 | allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr, |
| 523 | &sum, seg); |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 524 | set_data_blkaddr(dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 525 | |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 526 | /* update i_size */ |
Chao Yu | 81ca735 | 2016-01-26 15:39:35 +0800 | [diff] [blame] | 527 | fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) + |
Jaegeuk Kim | 976e4c5 | 2014-09-15 19:32:16 -0700 | [diff] [blame] | 528 | dn->ofs_in_node; |
Chao Yu | 9edcdab | 2015-09-11 14:43:52 +0800 | [diff] [blame] | 529 | if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT)) |
| 530 | i_size_write(dn->inode, |
| 531 | ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT)); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 532 | return 0; |
| 533 | } |
| 534 | |
Chao Yu | 6d5a149 | 2015-12-24 18:04:56 +0800 | [diff] [blame] | 535 | static int __allocate_data_blocks(struct inode *inode, loff_t offset, |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 536 | size_t count) |
| 537 | { |
Chao Yu | 5b8db7f | 2016-01-26 15:38:29 +0800 | [diff] [blame] | 538 | struct f2fs_map_blocks map; |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 539 | |
Chao Yu | 5b8db7f | 2016-01-26 15:38:29 +0800 | [diff] [blame] | 540 | map.m_lblk = F2FS_BYTES_TO_BLK(offset); |
| 541 | map.m_len = F2FS_BYTES_TO_BLK(count); |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 542 | map.m_next_pgofs = NULL; |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 543 | |
Chao Yu | 5b8db7f | 2016-01-26 15:38:29 +0800 | [diff] [blame] | 544 | return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_DIO); |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 545 | } |
| 546 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 547 | /* |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 548 | * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with |
| 549 | * f2fs_map_blocks structure. |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 550 | * If original data blocks are allocated, then give them to blockdev. |
| 551 | * Otherwise, |
| 552 | * a. preallocate requested block addresses |
| 553 | * b. do not use extent cache for better performance |
| 554 | * c. give the block addresses to blockdev |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 555 | */ |
Chao Yu | d323d00 | 2015-10-27 09:53:45 +0800 | [diff] [blame] | 556 | int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 557 | int create, int flag) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 558 | { |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 559 | unsigned int maxblocks = map->m_len; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 560 | struct dnode_of_data dn; |
Chao Yu | f981170 | 2015-09-21 20:17:52 +0800 | [diff] [blame] | 561 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 562 | int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA; |
| 563 | pgoff_t pgofs, end_offset; |
| 564 | int err = 0, ofs = 1; |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 565 | struct extent_info ei; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 566 | bool allocated = false; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 567 | block_t blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 568 | |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 569 | map->m_len = 0; |
| 570 | map->m_flags = 0; |
| 571 | |
| 572 | /* it only supports block size == page size */ |
| 573 | pgofs = (pgoff_t)map->m_lblk; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 574 | |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 575 | if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) { |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 576 | map->m_pblk = ei.blk + pgofs - ei.fofs; |
| 577 | map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs); |
| 578 | map->m_flags = F2FS_MAP_MAPPED; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 579 | goto out; |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 580 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 581 | |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 582 | next_dnode: |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 583 | if (create) |
Chao Yu | 3104af35 | 2015-12-23 17:11:43 +0800 | [diff] [blame] | 584 | f2fs_lock_op(sbi); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 585 | |
| 586 | /* When reading holes, we need its node page */ |
| 587 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 588 | err = get_dnode_of_data(&dn, pgofs, mode); |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 589 | if (err) { |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 590 | if (err == -ENOENT) { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 591 | err = 0; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 592 | if (map->m_next_pgofs) |
| 593 | *map->m_next_pgofs = |
| 594 | get_next_page_offset(&dn, pgofs); |
| 595 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 596 | goto unlock_out; |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 597 | } |
Chao Yu | 973163f | 2015-09-18 16:51:51 +0800 | [diff] [blame] | 598 | |
Chao Yu | 81ca735 | 2016-01-26 15:39:35 +0800 | [diff] [blame] | 599 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 600 | |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 601 | next_block: |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 602 | blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
Chao Yu | 973163f | 2015-09-18 16:51:51 +0800 | [diff] [blame] | 603 | |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 604 | if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) { |
| 605 | if (create) { |
| 606 | if (unlikely(f2fs_cp_error(sbi))) { |
| 607 | err = -EIO; |
| 608 | goto sync_out; |
Chao Yu | 973163f | 2015-09-18 16:51:51 +0800 | [diff] [blame] | 609 | } |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 610 | err = __allocate_data_block(&dn); |
| 611 | if (err) |
| 612 | goto sync_out; |
| 613 | allocated = true; |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 614 | map->m_flags = F2FS_MAP_NEW; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 615 | blkaddr = dn.data_blkaddr; |
| 616 | } else { |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 617 | if (flag == F2FS_GET_BLOCK_FIEMAP && |
| 618 | blkaddr == NULL_ADDR) { |
| 619 | if (map->m_next_pgofs) |
| 620 | *map->m_next_pgofs = pgofs + 1; |
| 621 | } |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 622 | if (flag != F2FS_GET_BLOCK_FIEMAP || |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 623 | blkaddr != NEW_ADDR) { |
| 624 | if (flag == F2FS_GET_BLOCK_BMAP) |
| 625 | err = -ENOENT; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 626 | goto sync_out; |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 627 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 628 | } |
| 629 | } |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 630 | |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 631 | if (map->m_len == 0) { |
| 632 | /* preallocated unwritten block should be mapped for fiemap. */ |
| 633 | if (blkaddr == NEW_ADDR) |
| 634 | map->m_flags |= F2FS_MAP_UNWRITTEN; |
| 635 | map->m_flags |= F2FS_MAP_MAPPED; |
| 636 | |
| 637 | map->m_pblk = blkaddr; |
| 638 | map->m_len = 1; |
| 639 | } else if ((map->m_pblk != NEW_ADDR && |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 640 | blkaddr == (map->m_pblk + ofs)) || |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 641 | (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR)) { |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 642 | ofs++; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 643 | map->m_len++; |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 644 | } else { |
| 645 | goto sync_out; |
| 646 | } |
| 647 | |
| 648 | dn.ofs_in_node++; |
| 649 | pgofs++; |
| 650 | |
| 651 | if (map->m_len < maxblocks) { |
| 652 | if (dn.ofs_in_node < end_offset) |
| 653 | goto next_block; |
| 654 | |
| 655 | if (allocated) |
| 656 | sync_inode_page(&dn); |
| 657 | f2fs_put_dnode(&dn); |
| 658 | |
| 659 | if (create) { |
| 660 | f2fs_unlock_op(sbi); |
| 661 | f2fs_balance_fs(sbi, allocated); |
| 662 | } |
| 663 | allocated = false; |
| 664 | goto next_dnode; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 665 | } |
| 666 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 667 | sync_out: |
| 668 | if (allocated) |
| 669 | sync_inode_page(&dn); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 670 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 671 | unlock_out: |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 672 | if (create) { |
Chao Yu | 3104af35 | 2015-12-23 17:11:43 +0800 | [diff] [blame] | 673 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 3c082b7 | 2016-01-23 13:35:18 -0800 | [diff] [blame] | 674 | f2fs_balance_fs(sbi, allocated); |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 675 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 676 | out: |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 677 | trace_f2fs_map_blocks(inode, map, err); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 678 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 679 | } |
| 680 | |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 681 | static int __get_data_block(struct inode *inode, sector_t iblock, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 682 | struct buffer_head *bh, int create, int flag, |
| 683 | pgoff_t *next_pgofs) |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 684 | { |
| 685 | struct f2fs_map_blocks map; |
| 686 | int ret; |
| 687 | |
| 688 | map.m_lblk = iblock; |
| 689 | map.m_len = bh->b_size >> inode->i_blkbits; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 690 | map.m_next_pgofs = next_pgofs; |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 691 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 692 | ret = f2fs_map_blocks(inode, &map, create, flag); |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 693 | if (!ret) { |
| 694 | map_bh(bh, inode->i_sb, map.m_pblk); |
| 695 | bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags; |
| 696 | bh->b_size = map.m_len << inode->i_blkbits; |
| 697 | } |
| 698 | return ret; |
| 699 | } |
| 700 | |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 701 | static int get_data_block(struct inode *inode, sector_t iblock, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 702 | struct buffer_head *bh_result, int create, int flag, |
| 703 | pgoff_t *next_pgofs) |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 704 | { |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 705 | return __get_data_block(inode, iblock, bh_result, create, |
| 706 | flag, next_pgofs); |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 707 | } |
| 708 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 709 | static int get_data_block_dio(struct inode *inode, sector_t iblock, |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 710 | struct buffer_head *bh_result, int create) |
| 711 | { |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 712 | return __get_data_block(inode, iblock, bh_result, create, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 713 | F2FS_GET_BLOCK_DIO, NULL); |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | static int get_data_block_bmap(struct inode *inode, sector_t iblock, |
| 717 | struct buffer_head *bh_result, int create) |
| 718 | { |
Yunlei He | 179448b | 2015-12-28 21:48:32 +0800 | [diff] [blame] | 719 | /* Block number less than F2FS MAX BLOCKS */ |
Chao Yu | e0afc4d | 2015-12-31 14:35:37 +0800 | [diff] [blame] | 720 | if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks)) |
Yunlei He | 179448b | 2015-12-28 21:48:32 +0800 | [diff] [blame] | 721 | return -EFBIG; |
| 722 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 723 | return __get_data_block(inode, iblock, bh_result, create, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 724 | F2FS_GET_BLOCK_BMAP, NULL); |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 725 | } |
| 726 | |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 727 | static inline sector_t logical_to_blk(struct inode *inode, loff_t offset) |
| 728 | { |
| 729 | return (offset >> inode->i_blkbits); |
| 730 | } |
| 731 | |
| 732 | static inline loff_t blk_to_logical(struct inode *inode, sector_t blk) |
| 733 | { |
| 734 | return (blk << inode->i_blkbits); |
| 735 | } |
| 736 | |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 737 | int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 738 | u64 start, u64 len) |
| 739 | { |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 740 | struct buffer_head map_bh; |
| 741 | sector_t start_blk, last_blk; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 742 | pgoff_t next_pgofs; |
Fan Li | de1475c | 2016-01-04 15:56:50 +0800 | [diff] [blame] | 743 | loff_t isize; |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 744 | u64 logical = 0, phys = 0, size = 0; |
| 745 | u32 flags = 0; |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 746 | int ret = 0; |
| 747 | |
| 748 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); |
| 749 | if (ret) |
| 750 | return ret; |
| 751 | |
Jaegeuk Kim | 67f8cf3 | 2015-10-15 11:34:49 -0700 | [diff] [blame] | 752 | if (f2fs_has_inline_data(inode)) { |
| 753 | ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len); |
| 754 | if (ret != -EAGAIN) |
| 755 | return ret; |
| 756 | } |
| 757 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 758 | inode_lock(inode); |
Fan Li | de1475c | 2016-01-04 15:56:50 +0800 | [diff] [blame] | 759 | |
| 760 | isize = i_size_read(inode); |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 761 | if (start >= isize) |
| 762 | goto out; |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 763 | |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 764 | if (start + len > isize) |
| 765 | len = isize - start; |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 766 | |
| 767 | if (logical_to_blk(inode, len) == 0) |
| 768 | len = blk_to_logical(inode, 1); |
| 769 | |
| 770 | start_blk = logical_to_blk(inode, start); |
| 771 | last_blk = logical_to_blk(inode, start + len - 1); |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 772 | |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 773 | next: |
| 774 | memset(&map_bh, 0, sizeof(struct buffer_head)); |
| 775 | map_bh.b_size = len; |
| 776 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 777 | ret = get_data_block(inode, start_blk, &map_bh, 0, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 778 | F2FS_GET_BLOCK_FIEMAP, &next_pgofs); |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 779 | if (ret) |
| 780 | goto out; |
| 781 | |
| 782 | /* HOLE */ |
| 783 | if (!buffer_mapped(&map_bh)) { |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 784 | start_blk = next_pgofs; |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 785 | /* Go through holes util pass the EOF */ |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 786 | if (blk_to_logical(inode, start_blk) < isize) |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 787 | goto prep_next; |
| 788 | /* Found a hole beyond isize means no more extents. |
| 789 | * Note that the premise is that filesystems don't |
| 790 | * punch holes beyond isize and keep size unchanged. |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 791 | */ |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 792 | flags |= FIEMAP_EXTENT_LAST; |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 793 | } |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 794 | |
Chao Yu | da5af12 | 2016-01-08 20:19:27 +0800 | [diff] [blame] | 795 | if (size) { |
| 796 | if (f2fs_encrypted_inode(inode)) |
| 797 | flags |= FIEMAP_EXTENT_DATA_ENCRYPTED; |
| 798 | |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 799 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 800 | phys, size, flags); |
Chao Yu | da5af12 | 2016-01-08 20:19:27 +0800 | [diff] [blame] | 801 | } |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 802 | |
| 803 | if (start_blk > last_blk || ret) |
| 804 | goto out; |
| 805 | |
| 806 | logical = blk_to_logical(inode, start_blk); |
| 807 | phys = blk_to_logical(inode, map_bh.b_blocknr); |
| 808 | size = map_bh.b_size; |
| 809 | flags = 0; |
| 810 | if (buffer_unwritten(&map_bh)) |
| 811 | flags = FIEMAP_EXTENT_UNWRITTEN; |
| 812 | |
| 813 | start_blk += logical_to_blk(inode, size); |
| 814 | |
| 815 | prep_next: |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 816 | cond_resched(); |
| 817 | if (fatal_signal_pending(current)) |
| 818 | ret = -EINTR; |
| 819 | else |
| 820 | goto next; |
| 821 | out: |
| 822 | if (ret == 1) |
| 823 | ret = 0; |
| 824 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 825 | inode_unlock(inode); |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 826 | return ret; |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 827 | } |
| 828 | |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 829 | /* |
| 830 | * This function was originally taken from fs/mpage.c, and customized for f2fs. |
| 831 | * Major change was from block_size == page_size in f2fs by default. |
| 832 | */ |
| 833 | static int f2fs_mpage_readpages(struct address_space *mapping, |
| 834 | struct list_head *pages, struct page *page, |
| 835 | unsigned nr_pages) |
| 836 | { |
| 837 | struct bio *bio = NULL; |
| 838 | unsigned page_idx; |
| 839 | sector_t last_block_in_bio = 0; |
| 840 | struct inode *inode = mapping->host; |
| 841 | const unsigned blkbits = inode->i_blkbits; |
| 842 | const unsigned blocksize = 1 << blkbits; |
| 843 | sector_t block_in_file; |
| 844 | sector_t last_block; |
| 845 | sector_t last_block_in_file; |
| 846 | sector_t block_nr; |
| 847 | struct block_device *bdev = inode->i_sb->s_bdev; |
| 848 | struct f2fs_map_blocks map; |
| 849 | |
| 850 | map.m_pblk = 0; |
| 851 | map.m_lblk = 0; |
| 852 | map.m_len = 0; |
| 853 | map.m_flags = 0; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 854 | map.m_next_pgofs = NULL; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 855 | |
| 856 | for (page_idx = 0; nr_pages; page_idx++, nr_pages--) { |
| 857 | |
| 858 | prefetchw(&page->flags); |
| 859 | if (pages) { |
| 860 | page = list_entry(pages->prev, struct page, lru); |
| 861 | list_del(&page->lru); |
| 862 | if (add_to_page_cache_lru(page, mapping, |
| 863 | page->index, GFP_KERNEL)) |
| 864 | goto next_page; |
| 865 | } |
| 866 | |
| 867 | block_in_file = (sector_t)page->index; |
| 868 | last_block = block_in_file + nr_pages; |
| 869 | last_block_in_file = (i_size_read(inode) + blocksize - 1) >> |
| 870 | blkbits; |
| 871 | if (last_block > last_block_in_file) |
| 872 | last_block = last_block_in_file; |
| 873 | |
| 874 | /* |
| 875 | * Map blocks using the previous result first. |
| 876 | */ |
| 877 | if ((map.m_flags & F2FS_MAP_MAPPED) && |
| 878 | block_in_file > map.m_lblk && |
| 879 | block_in_file < (map.m_lblk + map.m_len)) |
| 880 | goto got_it; |
| 881 | |
| 882 | /* |
| 883 | * Then do more f2fs_map_blocks() calls until we are |
| 884 | * done with this page. |
| 885 | */ |
| 886 | map.m_flags = 0; |
| 887 | |
| 888 | if (block_in_file < last_block) { |
| 889 | map.m_lblk = block_in_file; |
| 890 | map.m_len = last_block - block_in_file; |
| 891 | |
Chao Yu | 46c9e14 | 2015-09-18 16:54:16 +0800 | [diff] [blame] | 892 | if (f2fs_map_blocks(inode, &map, 0, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame^] | 893 | F2FS_GET_BLOCK_READ)) |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 894 | goto set_error_page; |
| 895 | } |
| 896 | got_it: |
| 897 | if ((map.m_flags & F2FS_MAP_MAPPED)) { |
| 898 | block_nr = map.m_pblk + block_in_file - map.m_lblk; |
| 899 | SetPageMappedToDisk(page); |
| 900 | |
| 901 | if (!PageUptodate(page) && !cleancache_get_page(page)) { |
| 902 | SetPageUptodate(page); |
| 903 | goto confused; |
| 904 | } |
| 905 | } else { |
| 906 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 907 | SetPageUptodate(page); |
| 908 | unlock_page(page); |
| 909 | goto next_page; |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * This page will go to BIO. Do we need to send this |
| 914 | * BIO off first? |
| 915 | */ |
| 916 | if (bio && (last_block_in_bio != block_nr - 1)) { |
| 917 | submit_and_realloc: |
| 918 | submit_bio(READ, bio); |
| 919 | bio = NULL; |
| 920 | } |
| 921 | if (bio == NULL) { |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 922 | struct f2fs_crypto_ctx *ctx = NULL; |
| 923 | |
| 924 | if (f2fs_encrypted_inode(inode) && |
| 925 | S_ISREG(inode->i_mode)) { |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 926 | |
| 927 | ctx = f2fs_get_crypto_ctx(inode); |
| 928 | if (IS_ERR(ctx)) |
| 929 | goto set_error_page; |
| 930 | |
| 931 | /* wait the page to be moved by cleaning */ |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 932 | f2fs_wait_on_encrypted_page_writeback( |
| 933 | F2FS_I_SB(inode), block_nr); |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 936 | bio = bio_alloc(GFP_KERNEL, |
Kent Overstreet | b54ffb7 | 2015-05-19 14:31:01 +0200 | [diff] [blame] | 937 | min_t(int, nr_pages, BIO_MAX_PAGES)); |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 938 | if (!bio) { |
| 939 | if (ctx) |
| 940 | f2fs_release_crypto_ctx(ctx); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 941 | goto set_error_page; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 942 | } |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 943 | bio->bi_bdev = bdev; |
| 944 | bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr); |
Chao Yu | 1237702 | 2015-05-25 18:03:38 +0800 | [diff] [blame] | 945 | bio->bi_end_io = f2fs_read_end_io; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 946 | bio->bi_private = ctx; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | if (bio_add_page(bio, page, blocksize, 0) < blocksize) |
| 950 | goto submit_and_realloc; |
| 951 | |
| 952 | last_block_in_bio = block_nr; |
| 953 | goto next_page; |
| 954 | set_error_page: |
| 955 | SetPageError(page); |
| 956 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 957 | unlock_page(page); |
| 958 | goto next_page; |
| 959 | confused: |
| 960 | if (bio) { |
| 961 | submit_bio(READ, bio); |
| 962 | bio = NULL; |
| 963 | } |
| 964 | unlock_page(page); |
| 965 | next_page: |
| 966 | if (pages) |
| 967 | page_cache_release(page); |
| 968 | } |
| 969 | BUG_ON(pages && !list_empty(pages)); |
| 970 | if (bio) |
| 971 | submit_bio(READ, bio); |
| 972 | return 0; |
| 973 | } |
| 974 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 975 | static int f2fs_read_data_page(struct file *file, struct page *page) |
| 976 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 977 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 978 | int ret = -EAGAIN; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 979 | |
Chao Yu | c20e89c | 2014-05-06 16:53:08 +0800 | [diff] [blame] | 980 | trace_f2fs_readpage(page, DATA); |
| 981 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 982 | /* If the file has inline data, try to read it directly */ |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 983 | if (f2fs_has_inline_data(inode)) |
| 984 | ret = f2fs_read_inline_data(inode, page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 985 | if (ret == -EAGAIN) |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 986 | ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1); |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 987 | return ret; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | static int f2fs_read_data_pages(struct file *file, |
| 991 | struct address_space *mapping, |
| 992 | struct list_head *pages, unsigned nr_pages) |
| 993 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 994 | struct inode *inode = file->f_mapping->host; |
Chao Yu | b8c2940 | 2015-10-12 17:02:26 +0800 | [diff] [blame] | 995 | struct page *page = list_entry(pages->prev, struct page, lru); |
| 996 | |
| 997 | trace_f2fs_readpages(inode, page, nr_pages); |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 998 | |
| 999 | /* If the file has inline data, skip readpages */ |
| 1000 | if (f2fs_has_inline_data(inode)) |
| 1001 | return 0; |
| 1002 | |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1003 | return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1004 | } |
| 1005 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1006 | int do_write_data_page(struct f2fs_io_info *fio) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1007 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1008 | struct page *page = fio->page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1009 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1010 | struct dnode_of_data dn; |
| 1011 | int err = 0; |
| 1012 | |
| 1013 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 1014 | err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1015 | if (err) |
| 1016 | return err; |
| 1017 | |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1018 | fio->blk_addr = dn.data_blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1019 | |
| 1020 | /* This page is already truncated */ |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1021 | if (fio->blk_addr == NULL_ADDR) { |
| 1022 | ClearPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1023 | goto out_writepage; |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1024 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1025 | |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1026 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) { |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 1027 | |
| 1028 | /* wait for GCed encrypted page writeback */ |
| 1029 | f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode), |
| 1030 | fio->blk_addr); |
| 1031 | |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1032 | fio->encrypted_page = f2fs_encrypt(inode, fio->page); |
| 1033 | if (IS_ERR(fio->encrypted_page)) { |
| 1034 | err = PTR_ERR(fio->encrypted_page); |
| 1035 | goto out_writepage; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1039 | set_page_writeback(page); |
| 1040 | |
| 1041 | /* |
| 1042 | * If current allocation needs SSR, |
| 1043 | * it had better in-place writes for updated data. |
| 1044 | */ |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1045 | if (unlikely(fio->blk_addr != NEW_ADDR && |
Haicheng Li | b25958b | 2013-06-13 16:59:29 +0800 | [diff] [blame] | 1046 | !is_cold_data(page) && |
Chao Yu | 2da3e027 | 2015-10-28 17:56:14 +0800 | [diff] [blame] | 1047 | !IS_ATOMIC_WRITTEN_PAGE(page) && |
Haicheng Li | b25958b | 2013-06-13 16:59:29 +0800 | [diff] [blame] | 1048 | need_inplace_update(inode))) { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1049 | rewrite_data_page(fio); |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 1050 | set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE); |
Jaegeuk Kim | 8ce67cb | 2015-03-17 17:58:08 -0700 | [diff] [blame] | 1051 | trace_f2fs_do_write_data_page(page, IPU); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1052 | } else { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1053 | write_data_page(&dn, fio); |
Chao Yu | 216a620 | 2015-03-19 19:23:32 +0800 | [diff] [blame] | 1054 | set_data_blkaddr(&dn); |
Chao Yu | 7e4dde7 | 2015-02-05 17:51:34 +0800 | [diff] [blame] | 1055 | f2fs_update_extent_cache(&dn); |
Jaegeuk Kim | 8ce67cb | 2015-03-17 17:58:08 -0700 | [diff] [blame] | 1056 | trace_f2fs_do_write_data_page(page, OPU); |
Jaegeuk Kim | fff04f9 | 2014-07-25 07:40:59 -0700 | [diff] [blame] | 1057 | set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE); |
Jaegeuk Kim | 3c6c2be | 2015-03-17 17:16:35 -0700 | [diff] [blame] | 1058 | if (page->index == 0) |
| 1059 | set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1060 | } |
| 1061 | out_writepage: |
| 1062 | f2fs_put_dnode(&dn); |
| 1063 | return err; |
| 1064 | } |
| 1065 | |
| 1066 | static int f2fs_write_data_page(struct page *page, |
| 1067 | struct writeback_control *wbc) |
| 1068 | { |
| 1069 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1070 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1071 | loff_t i_size = i_size_read(inode); |
| 1072 | const pgoff_t end_index = ((unsigned long long) i_size) |
| 1073 | >> PAGE_CACHE_SHIFT; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1074 | unsigned offset = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1075 | bool need_balance_fs = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1076 | int err = 0; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1077 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1078 | .sbi = sbi, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1079 | .type = DATA, |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 1080 | .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE, |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1081 | .page = page, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1082 | .encrypted_page = NULL, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1083 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1084 | |
Chao Yu | ecda0de | 2014-05-06 16:48:26 +0800 | [diff] [blame] | 1085 | trace_f2fs_writepage(page, DATA); |
| 1086 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1087 | if (page->index < end_index) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1088 | goto write; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1089 | |
| 1090 | /* |
| 1091 | * If the offset is out-of-range of file size, |
| 1092 | * this page does not have to be written to disk. |
| 1093 | */ |
| 1094 | offset = i_size & (PAGE_CACHE_SIZE - 1); |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 1095 | if ((page->index >= end_index + 1) || !offset) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1096 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1097 | |
| 1098 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1099 | write: |
Chao Yu | caf0047 | 2015-01-28 17:48:42 +0800 | [diff] [blame] | 1100 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1101 | goto redirty_out; |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1102 | if (f2fs_is_drop_cache(inode)) |
| 1103 | goto out; |
| 1104 | if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim && |
| 1105 | available_free_memory(sbi, BASE_CHECK)) |
| 1106 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1107 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1108 | /* Dentry blocks are controlled by checkpoint */ |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1109 | if (S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1110 | if (unlikely(f2fs_cp_error(sbi))) |
| 1111 | goto redirty_out; |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1112 | err = do_write_data_page(&fio); |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1113 | goto done; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1114 | } |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1115 | |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1116 | /* we should bypass data pages to proceed the kworkder jobs */ |
| 1117 | if (unlikely(f2fs_cp_error(sbi))) { |
| 1118 | SetPageError(page); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1119 | goto out; |
Jaegeuk Kim | cf779ca | 2014-08-11 18:37:46 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1122 | if (!wbc->for_reclaim) |
| 1123 | need_balance_fs = true; |
| 1124 | else if (has_not_enough_free_secs(sbi, 0)) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1125 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1126 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1127 | err = -EAGAIN; |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1128 | f2fs_lock_op(sbi); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1129 | if (f2fs_has_inline_data(inode)) |
| 1130 | err = f2fs_write_inline_data(inode, page); |
| 1131 | if (err == -EAGAIN) |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1132 | err = do_write_data_page(&fio); |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1133 | f2fs_unlock_op(sbi); |
| 1134 | done: |
| 1135 | if (err && err != -ENOENT) |
| 1136 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1137 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1138 | clear_cold_data(page); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1139 | out: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1140 | inode_dec_dirty_pages(inode); |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1141 | if (err) |
| 1142 | ClearPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1143 | unlock_page(page); |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 1144 | f2fs_balance_fs(sbi, need_balance_fs); |
Jaegeuk Kim | 8d4ea29 | 2015-12-31 13:08:02 -0800 | [diff] [blame] | 1145 | if (wbc->for_reclaim || unlikely(f2fs_cp_error(sbi))) { |
Jaegeuk Kim | 2aea39e | 2014-04-24 09:49:52 +0900 | [diff] [blame] | 1146 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Chao Yu | c227f91 | 2015-12-16 13:09:20 +0800 | [diff] [blame] | 1147 | remove_dirty_inode(inode); |
Chao Yu | eb7e813 | 2015-11-10 18:45:07 +0800 | [diff] [blame] | 1148 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1149 | return 0; |
| 1150 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1151 | redirty_out: |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 1152 | redirty_page_for_writepage(wbc, page); |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1153 | return AOP_WRITEPAGE_ACTIVATE; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1154 | } |
| 1155 | |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 1156 | static int __f2fs_writepage(struct page *page, struct writeback_control *wbc, |
| 1157 | void *data) |
| 1158 | { |
| 1159 | struct address_space *mapping = data; |
| 1160 | int ret = mapping->a_ops->writepage(page, wbc); |
| 1161 | mapping_set_error(mapping, ret); |
| 1162 | return ret; |
| 1163 | } |
| 1164 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1165 | /* |
| 1166 | * This function was copied from write_cche_pages from mm/page-writeback.c. |
| 1167 | * The major change is making write step of cold data page separately from |
| 1168 | * warm/hot data page. |
| 1169 | */ |
| 1170 | static int f2fs_write_cache_pages(struct address_space *mapping, |
| 1171 | struct writeback_control *wbc, writepage_t writepage, |
| 1172 | void *data) |
| 1173 | { |
| 1174 | int ret = 0; |
| 1175 | int done = 0; |
| 1176 | struct pagevec pvec; |
| 1177 | int nr_pages; |
| 1178 | pgoff_t uninitialized_var(writeback_index); |
| 1179 | pgoff_t index; |
| 1180 | pgoff_t end; /* Inclusive */ |
| 1181 | pgoff_t done_index; |
| 1182 | int cycled; |
| 1183 | int range_whole = 0; |
| 1184 | int tag; |
| 1185 | int step = 0; |
| 1186 | |
| 1187 | pagevec_init(&pvec, 0); |
| 1188 | next: |
| 1189 | if (wbc->range_cyclic) { |
| 1190 | writeback_index = mapping->writeback_index; /* prev offset */ |
| 1191 | index = writeback_index; |
| 1192 | if (index == 0) |
| 1193 | cycled = 1; |
| 1194 | else |
| 1195 | cycled = 0; |
| 1196 | end = -1; |
| 1197 | } else { |
| 1198 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 1199 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 1200 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 1201 | range_whole = 1; |
| 1202 | cycled = 1; /* ignore range_cyclic tests */ |
| 1203 | } |
| 1204 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 1205 | tag = PAGECACHE_TAG_TOWRITE; |
| 1206 | else |
| 1207 | tag = PAGECACHE_TAG_DIRTY; |
| 1208 | retry: |
| 1209 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 1210 | tag_pages_for_writeback(mapping, index, end); |
| 1211 | done_index = index; |
| 1212 | while (!done && (index <= end)) { |
| 1213 | int i; |
| 1214 | |
| 1215 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
| 1216 | min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1); |
| 1217 | if (nr_pages == 0) |
| 1218 | break; |
| 1219 | |
| 1220 | for (i = 0; i < nr_pages; i++) { |
| 1221 | struct page *page = pvec.pages[i]; |
| 1222 | |
| 1223 | if (page->index > end) { |
| 1224 | done = 1; |
| 1225 | break; |
| 1226 | } |
| 1227 | |
| 1228 | done_index = page->index; |
| 1229 | |
| 1230 | lock_page(page); |
| 1231 | |
| 1232 | if (unlikely(page->mapping != mapping)) { |
| 1233 | continue_unlock: |
| 1234 | unlock_page(page); |
| 1235 | continue; |
| 1236 | } |
| 1237 | |
| 1238 | if (!PageDirty(page)) { |
| 1239 | /* someone wrote it for us */ |
| 1240 | goto continue_unlock; |
| 1241 | } |
| 1242 | |
Tiezhu Yang | 737f189 | 2015-07-17 12:56:00 +0800 | [diff] [blame] | 1243 | if (step == is_cold_data(page)) |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1244 | goto continue_unlock; |
| 1245 | |
| 1246 | if (PageWriteback(page)) { |
| 1247 | if (wbc->sync_mode != WB_SYNC_NONE) |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 1248 | f2fs_wait_on_page_writeback(page, |
| 1249 | DATA, true); |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1250 | else |
| 1251 | goto continue_unlock; |
| 1252 | } |
| 1253 | |
| 1254 | BUG_ON(PageWriteback(page)); |
| 1255 | if (!clear_page_dirty_for_io(page)) |
| 1256 | goto continue_unlock; |
| 1257 | |
| 1258 | ret = (*writepage)(page, wbc, data); |
| 1259 | if (unlikely(ret)) { |
| 1260 | if (ret == AOP_WRITEPAGE_ACTIVATE) { |
| 1261 | unlock_page(page); |
| 1262 | ret = 0; |
| 1263 | } else { |
| 1264 | done_index = page->index + 1; |
| 1265 | done = 1; |
| 1266 | break; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | if (--wbc->nr_to_write <= 0 && |
| 1271 | wbc->sync_mode == WB_SYNC_NONE) { |
| 1272 | done = 1; |
| 1273 | break; |
| 1274 | } |
| 1275 | } |
| 1276 | pagevec_release(&pvec); |
| 1277 | cond_resched(); |
| 1278 | } |
| 1279 | |
| 1280 | if (step < 1) { |
| 1281 | step++; |
| 1282 | goto next; |
| 1283 | } |
| 1284 | |
| 1285 | if (!cycled && !done) { |
| 1286 | cycled = 1; |
| 1287 | index = 0; |
| 1288 | end = writeback_index - 1; |
| 1289 | goto retry; |
| 1290 | } |
| 1291 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 1292 | mapping->writeback_index = done_index; |
| 1293 | |
| 1294 | return ret; |
| 1295 | } |
| 1296 | |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 1297 | static int f2fs_write_data_pages(struct address_space *mapping, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1298 | struct writeback_control *wbc) |
| 1299 | { |
| 1300 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1301 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 5463e7c | 2015-04-21 10:40:54 -0700 | [diff] [blame] | 1302 | bool locked = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1303 | int ret; |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1304 | long diff; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1305 | |
Chao Yu | e574843 | 2014-05-06 16:51:24 +0800 | [diff] [blame] | 1306 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 1307 | |
P J P | cfb185a | 2013-04-03 11:38:00 +0900 | [diff] [blame] | 1308 | /* deal with chardevs and other special file */ |
| 1309 | if (!mapping->a_ops->writepage) |
| 1310 | return 0; |
| 1311 | |
Chao Yu | 6a29054 | 2015-07-17 18:02:39 +0800 | [diff] [blame] | 1312 | /* skip writing if there is no dirty page in this inode */ |
| 1313 | if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) |
| 1314 | return 0; |
| 1315 | |
Jaegeuk Kim | a125702 | 2015-10-08 10:40:07 -0700 | [diff] [blame] | 1316 | if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE && |
| 1317 | get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && |
| 1318 | available_free_memory(sbi, DIRTY_DENTS)) |
| 1319 | goto skip_write; |
| 1320 | |
Chao Yu | d323d00 | 2015-10-27 09:53:45 +0800 | [diff] [blame] | 1321 | /* skip writing during file defragment */ |
| 1322 | if (is_inode_flag_set(F2FS_I(inode), FI_DO_DEFRAG)) |
| 1323 | goto skip_write; |
| 1324 | |
Jaegeuk Kim | d5669f7 | 2015-02-27 13:37:39 -0800 | [diff] [blame] | 1325 | /* during POR, we don't need to trigger writepage at all. */ |
| 1326 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 1327 | goto skip_write; |
| 1328 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1329 | diff = nr_pages_to_write(sbi, DATA, wbc); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1330 | |
Jaegeuk Kim | 25c1355 | 2016-01-20 23:46:05 +0800 | [diff] [blame] | 1331 | if (!S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_ALL) { |
Jaegeuk Kim | 5463e7c | 2015-04-21 10:40:54 -0700 | [diff] [blame] | 1332 | mutex_lock(&sbi->writepages); |
| 1333 | locked = true; |
| 1334 | } |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 1335 | ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping); |
Chao Yu | bb96a8d | 2015-07-06 20:31:49 +0800 | [diff] [blame] | 1336 | f2fs_submit_merged_bio(sbi, DATA, WRITE); |
Jaegeuk Kim | 5463e7c | 2015-04-21 10:40:54 -0700 | [diff] [blame] | 1337 | if (locked) |
| 1338 | mutex_unlock(&sbi->writepages); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1339 | |
Chao Yu | c227f91 | 2015-12-16 13:09:20 +0800 | [diff] [blame] | 1340 | remove_dirty_inode(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1341 | |
Jaegeuk Kim | 50c8cdb | 2014-03-18 13:47:11 +0900 | [diff] [blame] | 1342 | wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1343 | return ret; |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 1344 | |
| 1345 | skip_write: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1346 | wbc->pages_skipped += get_dirty_pages(inode); |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 1347 | return 0; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1348 | } |
| 1349 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1350 | static void f2fs_write_failed(struct address_space *mapping, loff_t to) |
| 1351 | { |
| 1352 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 819d915 | 2015-12-28 13:48:11 -0800 | [diff] [blame] | 1353 | loff_t i_size = i_size_read(inode); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1354 | |
Jaegeuk Kim | 819d915 | 2015-12-28 13:48:11 -0800 | [diff] [blame] | 1355 | if (to > i_size) { |
| 1356 | truncate_pagecache(inode, i_size); |
| 1357 | truncate_blocks(inode, i_size, true); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1358 | } |
| 1359 | } |
| 1360 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1361 | static int prepare_write_begin(struct f2fs_sb_info *sbi, |
| 1362 | struct page *page, loff_t pos, unsigned len, |
| 1363 | block_t *blk_addr, bool *node_changed) |
| 1364 | { |
| 1365 | struct inode *inode = page->mapping->host; |
| 1366 | pgoff_t index = page->index; |
| 1367 | struct dnode_of_data dn; |
| 1368 | struct page *ipage; |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1369 | bool locked = false; |
| 1370 | struct extent_info ei; |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1371 | int err = 0; |
| 1372 | |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1373 | if (f2fs_has_inline_data(inode) || |
| 1374 | (pos & PAGE_CACHE_MASK) >= i_size_read(inode)) { |
| 1375 | f2fs_lock_op(sbi); |
| 1376 | locked = true; |
| 1377 | } |
| 1378 | restart: |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1379 | /* check inline_data */ |
| 1380 | ipage = get_node_page(sbi, inode->i_ino); |
| 1381 | if (IS_ERR(ipage)) { |
| 1382 | err = PTR_ERR(ipage); |
| 1383 | goto unlock_out; |
| 1384 | } |
| 1385 | |
| 1386 | set_new_dnode(&dn, inode, ipage, ipage, 0); |
| 1387 | |
| 1388 | if (f2fs_has_inline_data(inode)) { |
| 1389 | if (pos + len <= MAX_INLINE_DATA) { |
| 1390 | read_inline_data(page, ipage); |
| 1391 | set_inode_flag(F2FS_I(inode), FI_DATA_EXIST); |
Jaegeuk Kim | 2049d4f | 2016-01-25 05:57:05 -0800 | [diff] [blame] | 1392 | set_inline_node(ipage); |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1393 | } else { |
| 1394 | err = f2fs_convert_inline_page(&dn, page); |
| 1395 | if (err) |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1396 | goto out; |
| 1397 | if (dn.data_blkaddr == NULL_ADDR) |
| 1398 | err = f2fs_get_block(&dn, index); |
| 1399 | } |
| 1400 | } else if (locked) { |
| 1401 | err = f2fs_get_block(&dn, index); |
| 1402 | } else { |
| 1403 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 1404 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 1405 | } else { |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1406 | /* hole case */ |
| 1407 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | b483fad | 2016-01-20 08:27:37 +0800 | [diff] [blame] | 1408 | if (err || (!err && dn.data_blkaddr == NULL_ADDR)) { |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1409 | f2fs_put_dnode(&dn); |
| 1410 | f2fs_lock_op(sbi); |
| 1411 | locked = true; |
| 1412 | goto restart; |
| 1413 | } |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1414 | } |
| 1415 | } |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1416 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1417 | /* convert_inline_page can make node_changed */ |
| 1418 | *blk_addr = dn.data_blkaddr; |
| 1419 | *node_changed = dn.node_changed; |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1420 | out: |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1421 | f2fs_put_dnode(&dn); |
| 1422 | unlock_out: |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 1423 | if (locked) |
| 1424 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1425 | return err; |
| 1426 | } |
| 1427 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1428 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
| 1429 | loff_t pos, unsigned len, unsigned flags, |
| 1430 | struct page **pagep, void **fsdata) |
| 1431 | { |
| 1432 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1433 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 1434 | struct page *page = NULL; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1435 | pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT; |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1436 | bool need_balance = false; |
| 1437 | block_t blkaddr = NULL_ADDR; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1438 | int err = 0; |
| 1439 | |
Chao Yu | 62aed04 | 2014-05-06 16:46:04 +0800 | [diff] [blame] | 1440 | trace_f2fs_write_begin(inode, pos, len, flags); |
| 1441 | |
Jaegeuk Kim | 5f72739 | 2014-11-25 10:59:45 -0800 | [diff] [blame] | 1442 | /* |
| 1443 | * We should check this at this moment to avoid deadlock on inode page |
| 1444 | * and #0 page. The locking rule for inline_data conversion should be: |
| 1445 | * lock_page(page #0) -> lock_page(inode_page) |
| 1446 | */ |
| 1447 | if (index != 0) { |
| 1448 | err = f2fs_convert_inline_inode(inode); |
| 1449 | if (err) |
| 1450 | goto fail; |
| 1451 | } |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1452 | repeat: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1453 | page = grab_cache_page_write_begin(mapping, index, flags); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1454 | if (!page) { |
| 1455 | err = -ENOMEM; |
| 1456 | goto fail; |
| 1457 | } |
Jaegeuk Kim | d5f6699 | 2014-04-30 09:22:45 +0900 | [diff] [blame] | 1458 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1459 | *pagep = page; |
| 1460 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1461 | err = prepare_write_begin(sbi, page, pos, len, |
| 1462 | &blkaddr, &need_balance); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1463 | if (err) |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1464 | goto fail; |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1465 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1466 | if (need_balance && has_not_enough_free_secs(sbi, 0)) { |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 1467 | unlock_page(page); |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 1468 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 1469 | lock_page(page); |
| 1470 | if (page->mapping != mapping) { |
| 1471 | /* The page got truncated from under us */ |
| 1472 | f2fs_put_page(page, 1); |
| 1473 | goto repeat; |
| 1474 | } |
| 1475 | } |
| 1476 | |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 1477 | f2fs_wait_on_page_writeback(page, DATA, false); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1478 | |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 1479 | /* wait for GCed encrypted page writeback */ |
| 1480 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1481 | f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr); |
Chao Yu | 08b39fb | 2015-10-08 13:27:34 +0800 | [diff] [blame] | 1482 | |
Chao Yu | 90d4388 | 2015-07-08 18:24:38 +0800 | [diff] [blame] | 1483 | if (len == PAGE_CACHE_SIZE) |
| 1484 | goto out_update; |
| 1485 | if (PageUptodate(page)) |
| 1486 | goto out_clear; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1487 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1488 | if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) { |
| 1489 | unsigned start = pos & (PAGE_CACHE_SIZE - 1); |
| 1490 | unsigned end = start + len; |
| 1491 | |
| 1492 | /* Reading beyond i_size is simple: memset to zero */ |
| 1493 | zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE); |
Chao Yu | 90d4388 | 2015-07-08 18:24:38 +0800 | [diff] [blame] | 1494 | goto out_update; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1495 | } |
| 1496 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1497 | if (blkaddr == NEW_ADDR) { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1498 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 1499 | } else { |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1500 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1501 | .sbi = sbi, |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1502 | .type = DATA, |
| 1503 | .rw = READ_SYNC, |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 1504 | .blk_addr = blkaddr, |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1505 | .page = page, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1506 | .encrypted_page = NULL, |
Jaegeuk Kim | cf04e8e | 2014-12-17 19:33:13 -0800 | [diff] [blame] | 1507 | }; |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1508 | err = f2fs_submit_page_bio(&fio); |
Jan Kara | 9234f31 | 2014-10-22 15:21:47 +0200 | [diff] [blame] | 1509 | if (err) |
| 1510 | goto fail; |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 1511 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 1512 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1513 | if (unlikely(!PageUptodate(page))) { |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1514 | err = -EIO; |
| 1515 | goto fail; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1516 | } |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 1517 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 1518 | f2fs_put_page(page, 1); |
| 1519 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1520 | } |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1521 | |
| 1522 | /* avoid symlink page */ |
| 1523 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) { |
| 1524 | err = f2fs_decrypt_one(inode, page); |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 1525 | if (err) |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1526 | goto fail; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1527 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1528 | } |
Chao Yu | 90d4388 | 2015-07-08 18:24:38 +0800 | [diff] [blame] | 1529 | out_update: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1530 | SetPageUptodate(page); |
Chao Yu | 90d4388 | 2015-07-08 18:24:38 +0800 | [diff] [blame] | 1531 | out_clear: |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1532 | clear_cold_data(page); |
| 1533 | return 0; |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 1534 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1535 | fail: |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 1536 | f2fs_put_page(page, 1); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1537 | f2fs_write_failed(mapping, pos + len); |
| 1538 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1539 | } |
| 1540 | |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1541 | static int f2fs_write_end(struct file *file, |
| 1542 | struct address_space *mapping, |
| 1543 | loff_t pos, unsigned len, unsigned copied, |
| 1544 | struct page *page, void *fsdata) |
| 1545 | { |
| 1546 | struct inode *inode = page->mapping->host; |
| 1547 | |
Chao Yu | dfb2bf3 | 2014-05-06 16:47:23 +0800 | [diff] [blame] | 1548 | trace_f2fs_write_end(inode, pos, len, copied); |
| 1549 | |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1550 | set_page_dirty(page); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1551 | |
| 1552 | if (pos + copied > i_size_read(inode)) { |
| 1553 | i_size_write(inode, pos + copied); |
| 1554 | mark_inode_dirty(inode); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1555 | } |
| 1556 | |
Chao Yu | 75c3c8b | 2013-11-16 14:15:59 +0800 | [diff] [blame] | 1557 | f2fs_put_page(page, 1); |
Jaegeuk Kim | d0239e1 | 2016-01-08 16:57:48 -0800 | [diff] [blame] | 1558 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1559 | return copied; |
| 1560 | } |
| 1561 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 1562 | static int check_direct_IO(struct inode *inode, struct iov_iter *iter, |
| 1563 | loff_t offset) |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1564 | { |
| 1565 | unsigned blocksize_mask = inode->i_sb->s_blocksize - 1; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1566 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1567 | if (offset & blocksize_mask) |
| 1568 | return -EINVAL; |
| 1569 | |
Al Viro | 5b46f25 | 2014-03-16 18:07:34 -0400 | [diff] [blame] | 1570 | if (iov_iter_alignment(iter) & blocksize_mask) |
| 1571 | return -EINVAL; |
| 1572 | |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1573 | return 0; |
| 1574 | } |
| 1575 | |
Omar Sandoval | 22c6186 | 2015-03-16 04:33:53 -0700 | [diff] [blame] | 1576 | static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter, |
| 1577 | loff_t offset) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1578 | { |
| 1579 | struct file *file = iocb->ki_filp; |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1580 | struct address_space *mapping = file->f_mapping; |
| 1581 | struct inode *inode = mapping->host; |
| 1582 | size_t count = iov_iter_count(iter); |
| 1583 | int err; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1584 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1585 | /* we don't need to use inline_data strictly */ |
Jaegeuk Kim | b9d777b | 2015-12-22 11:09:35 -0800 | [diff] [blame] | 1586 | err = f2fs_convert_inline_inode(inode); |
| 1587 | if (err) |
| 1588 | return err; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1589 | |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 1590 | if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) |
| 1591 | return 0; |
| 1592 | |
Chao Yu | c15e859 | 2015-08-07 18:39:32 +0800 | [diff] [blame] | 1593 | err = check_direct_IO(inode, iter, offset); |
| 1594 | if (err) |
| 1595 | return err; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 1596 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 1597 | trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter)); |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 1598 | |
Chao Yu | f981170 | 2015-09-21 20:17:52 +0800 | [diff] [blame] | 1599 | if (iov_iter_rw(iter) == WRITE) { |
Chao Yu | 6d5a149 | 2015-12-24 18:04:56 +0800 | [diff] [blame] | 1600 | err = __allocate_data_blocks(inode, offset, count); |
| 1601 | if (err) |
Chao Yu | f981170 | 2015-09-21 20:17:52 +0800 | [diff] [blame] | 1602 | goto out; |
Chao Yu | f981170 | 2015-09-21 20:17:52 +0800 | [diff] [blame] | 1603 | } |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 1604 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1605 | err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block_dio); |
Chao Yu | f981170 | 2015-09-21 20:17:52 +0800 | [diff] [blame] | 1606 | out: |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 1607 | if (err < 0 && iov_iter_rw(iter) == WRITE) |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1608 | f2fs_write_failed(mapping, offset + count); |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 1609 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 1610 | trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err); |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 1611 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 1612 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1613 | } |
| 1614 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1615 | void f2fs_invalidate_page(struct page *page, unsigned int offset, |
| 1616 | unsigned int length) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1617 | { |
| 1618 | struct inode *inode = page->mapping->host; |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1619 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1620 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1621 | if (inode->i_ino >= F2FS_ROOT_INO(sbi) && |
| 1622 | (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE)) |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1623 | return; |
| 1624 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1625 | if (PageDirty(page)) { |
| 1626 | if (inode->i_ino == F2FS_META_INO(sbi)) |
| 1627 | dec_page_count(sbi, F2FS_DIRTY_META); |
| 1628 | else if (inode->i_ino == F2FS_NODE_INO(sbi)) |
| 1629 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
| 1630 | else |
| 1631 | inode_dec_dirty_pages(inode); |
| 1632 | } |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 1633 | |
| 1634 | /* This is atomic written page, keep Private */ |
| 1635 | if (IS_ATOMIC_WRITTEN_PAGE(page)) |
| 1636 | return; |
| 1637 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1638 | ClearPagePrivate(page); |
| 1639 | } |
| 1640 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1641 | int f2fs_release_page(struct page *page, gfp_t wait) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1642 | { |
Jaegeuk Kim | f68daee | 2015-01-30 11:39:08 -0800 | [diff] [blame] | 1643 | /* If this is dirty page, keep PagePrivate */ |
| 1644 | if (PageDirty(page)) |
| 1645 | return 0; |
| 1646 | |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 1647 | /* This is atomic written page, keep Private */ |
| 1648 | if (IS_ATOMIC_WRITTEN_PAGE(page)) |
| 1649 | return 0; |
| 1650 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1651 | ClearPagePrivate(page); |
Jaegeuk Kim | c3850aa | 2013-03-14 09:24:32 +0900 | [diff] [blame] | 1652 | return 1; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | static int f2fs_set_data_page_dirty(struct page *page) |
| 1656 | { |
| 1657 | struct address_space *mapping = page->mapping; |
| 1658 | struct inode *inode = mapping->host; |
| 1659 | |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 1660 | trace_f2fs_set_page_dirty(page, DATA); |
| 1661 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1662 | SetPageUptodate(page); |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1663 | |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1664 | if (f2fs_is_atomic_file(inode)) { |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 1665 | if (!IS_ATOMIC_WRITTEN_PAGE(page)) { |
| 1666 | register_inmem_page(inode, page); |
| 1667 | return 1; |
| 1668 | } |
| 1669 | /* |
| 1670 | * Previously, this page has been registered, we just |
| 1671 | * return here. |
| 1672 | */ |
| 1673 | return 0; |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1676 | if (!PageDirty(page)) { |
| 1677 | __set_page_dirty_nobuffers(page); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 1678 | update_dirty_page(inode, page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1679 | return 1; |
| 1680 | } |
| 1681 | return 0; |
| 1682 | } |
| 1683 | |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1684 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 1685 | { |
Chao Yu | 454ae7e | 2014-04-22 13:34:01 +0800 | [diff] [blame] | 1686 | struct inode *inode = mapping->host; |
| 1687 | |
Jaegeuk Kim | 1d373a0 | 2015-10-19 10:29:51 -0700 | [diff] [blame] | 1688 | if (f2fs_has_inline_data(inode)) |
| 1689 | return 0; |
| 1690 | |
| 1691 | /* make sure allocating whole blocks */ |
| 1692 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
| 1693 | filemap_write_and_wait(mapping); |
| 1694 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1695 | return generic_block_bmap(mapping, block, get_data_block_bmap); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 1696 | } |
| 1697 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1698 | const struct address_space_operations f2fs_dblock_aops = { |
| 1699 | .readpage = f2fs_read_data_page, |
| 1700 | .readpages = f2fs_read_data_pages, |
| 1701 | .writepage = f2fs_write_data_page, |
| 1702 | .writepages = f2fs_write_data_pages, |
| 1703 | .write_begin = f2fs_write_begin, |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 1704 | .write_end = f2fs_write_end, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1705 | .set_page_dirty = f2fs_set_data_page_dirty, |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 1706 | .invalidatepage = f2fs_invalidate_page, |
| 1707 | .releasepage = f2fs_release_page, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1708 | .direct_IO = f2fs_direct_IO, |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 1709 | .bmap = f2fs_bmap, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1710 | }; |