Chao Yu | 6d1a832 | 2018-09-12 09:16:07 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 3 | * fs/f2fs/data.c |
| 4 | * |
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 6 | * http://www.samsung.com/ |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/f2fs_fs.h> |
| 10 | #include <linux/buffer_head.h> |
| 11 | #include <linux/mpage.h> |
| 12 | #include <linux/writeback.h> |
| 13 | #include <linux/backing-dev.h> |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 14 | #include <linux/pagevec.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 15 | #include <linux/blkdev.h> |
| 16 | #include <linux/bio.h> |
Geert Uytterhoeven | 690e4a3 | 2012-12-19 22:19:30 +0100 | [diff] [blame] | 17 | #include <linux/prefetch.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 18 | #include <linux/uio.h> |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 19 | #include <linux/cleancache.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 20 | |
| 21 | #include "f2fs.h" |
| 22 | #include "node.h" |
| 23 | #include "segment.h" |
Jaegeuk Kim | db9f7c1 | 2014-12-17 20:04:08 -0800 | [diff] [blame] | 24 | #include "trace.h" |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 25 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 26 | |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 27 | #define NUM_PREALLOC_POST_READ_CTXS 128 |
| 28 | |
| 29 | static struct kmem_cache *bio_post_read_ctx_cache; |
| 30 | static mempool_t *bio_post_read_ctx_pool; |
| 31 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 32 | static bool __is_cp_guaranteed(struct page *page) |
| 33 | { |
| 34 | struct address_space *mapping = page->mapping; |
| 35 | struct inode *inode; |
| 36 | struct f2fs_sb_info *sbi; |
| 37 | |
| 38 | if (!mapping) |
| 39 | return false; |
| 40 | |
| 41 | inode = mapping->host; |
| 42 | sbi = F2FS_I_SB(inode); |
| 43 | |
| 44 | if (inode->i_ino == F2FS_META_INO(sbi) || |
| 45 | inode->i_ino == F2FS_NODE_INO(sbi) || |
| 46 | S_ISDIR(inode->i_mode) || |
Chao Yu | a301e76 | 2018-05-08 14:06:03 +0800 | [diff] [blame] | 47 | (S_ISREG(inode->i_mode) && |
Chao Yu | d831272 | 2018-09-20 20:05:00 +0800 | [diff] [blame] | 48 | (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) || |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 49 | is_cold_data(page)) |
| 50 | return true; |
| 51 | return false; |
| 52 | } |
| 53 | |
Jaegeuk Kim | 8025d72 | 2018-10-16 10:20:53 -0700 | [diff] [blame] | 54 | static enum count_type __read_io_type(struct page *page) |
| 55 | { |
| 56 | struct address_space *mapping = page->mapping; |
| 57 | |
| 58 | if (mapping) { |
| 59 | struct inode *inode = mapping->host; |
| 60 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 61 | |
| 62 | if (inode->i_ino == F2FS_META_INO(sbi)) |
| 63 | return F2FS_RD_META; |
| 64 | |
| 65 | if (inode->i_ino == F2FS_NODE_INO(sbi)) |
| 66 | return F2FS_RD_NODE; |
| 67 | } |
| 68 | return F2FS_RD_DATA; |
| 69 | } |
| 70 | |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 71 | /* postprocessing steps for read bios */ |
| 72 | enum bio_post_read_step { |
| 73 | STEP_INITIAL = 0, |
| 74 | STEP_DECRYPT, |
| 75 | }; |
| 76 | |
| 77 | struct bio_post_read_ctx { |
| 78 | struct bio *bio; |
| 79 | struct work_struct work; |
| 80 | unsigned int cur_step; |
| 81 | unsigned int enabled_steps; |
| 82 | }; |
| 83 | |
| 84 | static void __read_end_io(struct bio *bio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 85 | { |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 86 | struct page *page; |
| 87 | struct bio_vec *bv; |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 88 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 89 | |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 90 | bio_for_each_segment_all(bv, bio, i) { |
| 91 | page = bv->bv_page; |
| 92 | |
| 93 | /* PG_error was set if any post_read step failed */ |
| 94 | if (bio->bi_error || PageError(page)) { |
| 95 | ClearPageUptodate(page); |
Jaegeuk Kim | 4edba23 | 2018-09-25 13:54:33 -0700 | [diff] [blame] | 96 | /* will re-read again later */ |
| 97 | ClearPageError(page); |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 98 | } else { |
| 99 | SetPageUptodate(page); |
| 100 | } |
Jaegeuk Kim | 8025d72 | 2018-10-16 10:20:53 -0700 | [diff] [blame] | 101 | dec_page_count(F2FS_P_SB(page), __read_io_type(page)); |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 102 | unlock_page(page); |
| 103 | } |
| 104 | if (bio->bi_private) |
| 105 | mempool_free(bio->bi_private, bio_post_read_ctx_pool); |
| 106 | bio_put(bio); |
| 107 | } |
| 108 | |
| 109 | static void bio_post_read_processing(struct bio_post_read_ctx *ctx); |
| 110 | |
| 111 | static void decrypt_work(struct work_struct *work) |
| 112 | { |
| 113 | struct bio_post_read_ctx *ctx = |
| 114 | container_of(work, struct bio_post_read_ctx, work); |
| 115 | |
| 116 | fscrypt_decrypt_bio(ctx->bio); |
| 117 | |
| 118 | bio_post_read_processing(ctx); |
| 119 | } |
| 120 | |
| 121 | static void bio_post_read_processing(struct bio_post_read_ctx *ctx) |
| 122 | { |
| 123 | switch (++ctx->cur_step) { |
| 124 | case STEP_DECRYPT: |
| 125 | if (ctx->enabled_steps & (1 << STEP_DECRYPT)) { |
| 126 | INIT_WORK(&ctx->work, decrypt_work); |
| 127 | fscrypt_enqueue_decrypt_work(&ctx->work); |
| 128 | return; |
| 129 | } |
| 130 | ctx->cur_step++; |
| 131 | /* fall-through */ |
| 132 | default: |
| 133 | __read_end_io(ctx->bio); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static bool f2fs_bio_post_read_required(struct bio *bio) |
| 138 | { |
| 139 | return bio->bi_private && !bio->bi_error; |
| 140 | } |
| 141 | |
| 142 | static void f2fs_read_end_io(struct bio *bio) |
| 143 | { |
Chao Yu | 40292b9 | 2018-09-12 09:22:29 +0800 | [diff] [blame] | 144 | if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_READ_IO)) { |
| 145 | f2fs_show_injection_info(FAULT_READ_IO); |
Chao Yu | 8b038c7 | 2016-09-18 23:30:07 +0800 | [diff] [blame] | 146 | bio->bi_error = -EIO; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 147 | } |
Chao Yu | 8b038c7 | 2016-09-18 23:30:07 +0800 | [diff] [blame] | 148 | |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 149 | if (f2fs_bio_post_read_required(bio)) { |
| 150 | struct bio_post_read_ctx *ctx = bio->bi_private; |
| 151 | |
| 152 | ctx->cur_step = STEP_INITIAL; |
| 153 | bio_post_read_processing(ctx); |
| 154 | return; |
Chao Yu | 1237702 | 2015-05-25 18:03:38 +0800 | [diff] [blame] | 155 | } |
| 156 | |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 157 | __read_end_io(bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 158 | } |
| 159 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 160 | static void f2fs_write_end_io(struct bio *bio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 161 | { |
Jaegeuk Kim | 1b1f559 | 2014-02-03 10:50:22 +0900 | [diff] [blame] | 162 | struct f2fs_sb_info *sbi = bio->bi_private; |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 163 | struct bio_vec *bvec; |
| 164 | int i; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 165 | |
Chao Yu | 40292b9 | 2018-09-12 09:22:29 +0800 | [diff] [blame] | 166 | if (time_to_inject(sbi, FAULT_WRITE_IO)) { |
| 167 | f2fs_show_injection_info(FAULT_WRITE_IO); |
| 168 | bio->bi_error = -EIO; |
| 169 | } |
| 170 | |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 171 | bio_for_each_segment_all(bvec, bio, i) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 172 | struct page *page = bvec->bv_page; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 173 | enum count_type type = WB_DATA_TYPE(page); |
| 174 | |
| 175 | if (IS_DUMMY_WRITTEN_PAGE(page)) { |
| 176 | set_page_private(page, (unsigned long)NULL); |
| 177 | ClearPagePrivate(page); |
| 178 | unlock_page(page); |
| 179 | mempool_free(page, sbi->write_io_dummy); |
| 180 | |
| 181 | if (unlikely(bio->bi_error)) |
| 182 | f2fs_stop_checkpoint(sbi, true); |
| 183 | continue; |
| 184 | } |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 185 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 186 | fscrypt_pullback_bio_page(&page, true); |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 187 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 188 | if (unlikely(bio->bi_error)) { |
Michal Hocko | 5114a97 | 2016-10-11 13:56:01 -0700 | [diff] [blame] | 189 | mapping_set_error(page->mapping, -EIO); |
Jaegeuk Kim | e270976 | 2017-12-31 16:26:38 -0800 | [diff] [blame] | 190 | if (type == F2FS_WB_CP_DATA) |
| 191 | f2fs_stop_checkpoint(sbi, true); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 192 | } |
Yunlei He | 92b8f9c | 2018-01-11 14:19:32 +0800 | [diff] [blame] | 193 | |
| 194 | f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) && |
| 195 | page->index != nid_of_node(page)); |
| 196 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 197 | dec_page_count(sbi, type); |
Chao Yu | 3d5c54a | 2018-08-02 23:03:19 +0800 | [diff] [blame] | 198 | if (f2fs_in_warm_node_list(sbi, page)) |
| 199 | f2fs_del_fsync_node_entry(sbi, page); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 200 | clear_cold_data(page); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 201 | end_page_writeback(page); |
Linus Torvalds | f568849 | 2014-01-30 11:19:05 -0800 | [diff] [blame] | 202 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 203 | if (!get_pages(sbi, F2FS_WB_CP_DATA) && |
Jaegeuk Kim | f573018 | 2016-05-17 16:23:36 -0700 | [diff] [blame] | 204 | wq_has_sleeper(&sbi->cp_wait)) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 205 | wake_up(&sbi->cp_wait); |
| 206 | |
| 207 | bio_put(bio); |
| 208 | } |
| 209 | |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 210 | /* |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 211 | * Return true, if pre_bio's bdev is same as its target device. |
| 212 | */ |
| 213 | struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi, |
| 214 | block_t blk_addr, struct bio *bio) |
| 215 | { |
| 216 | struct block_device *bdev = sbi->sb->s_bdev; |
| 217 | int i; |
| 218 | |
| 219 | for (i = 0; i < sbi->s_ndevs; i++) { |
| 220 | if (FDEV(i).start_blk <= blk_addr && |
| 221 | FDEV(i).end_blk >= blk_addr) { |
| 222 | blk_addr -= FDEV(i).start_blk; |
| 223 | bdev = FDEV(i).bdev; |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | if (bio) { |
| 228 | bio->bi_bdev = bdev; |
| 229 | bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr); |
| 230 | } |
| 231 | return bdev; |
| 232 | } |
| 233 | |
| 234 | int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr) |
| 235 | { |
| 236 | int i; |
| 237 | |
| 238 | for (i = 0; i < sbi->s_ndevs; i++) |
| 239 | if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr) |
| 240 | return i; |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static bool __same_bdev(struct f2fs_sb_info *sbi, |
| 245 | block_t blk_addr, struct bio *bio) |
| 246 | { |
| 247 | return f2fs_target_device(sbi, blk_addr, NULL) == bio->bi_bdev; |
| 248 | } |
| 249 | |
| 250 | /* |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 251 | * Low-level block read/write IO operations. |
| 252 | */ |
| 253 | static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr, |
Yufen Yu | cbd5e5a | 2018-01-09 19:33:39 +0800 | [diff] [blame] | 254 | struct writeback_control *wbc, |
Hyunchul Lee | bcdc571 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 255 | int npages, bool is_read, |
| 256 | enum page_type type, enum temp_type temp) |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 257 | { |
| 258 | struct bio *bio; |
| 259 | |
Chao Yu | b5db2de | 2017-10-28 16:52:31 +0800 | [diff] [blame] | 260 | bio = f2fs_bio_alloc(sbi, npages, true); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 261 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 262 | f2fs_target_device(sbi, blk_addr, bio); |
Hyunchul Lee | bcdc571 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 263 | if (is_read) { |
| 264 | bio->bi_end_io = f2fs_read_end_io; |
| 265 | bio->bi_private = NULL; |
| 266 | } else { |
| 267 | bio->bi_end_io = f2fs_write_end_io; |
| 268 | bio->bi_private = sbi; |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 269 | bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, type, temp); |
Hyunchul Lee | bcdc571 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 270 | } |
Yufen Yu | cbd5e5a | 2018-01-09 19:33:39 +0800 | [diff] [blame] | 271 | if (wbc) |
| 272 | wbc_init_bio(wbc, bio); |
Gu Zheng | 940a6d3 | 2013-12-20 17:39:59 +0800 | [diff] [blame] | 273 | |
| 274 | return bio; |
| 275 | } |
| 276 | |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 277 | static inline void __submit_bio(struct f2fs_sb_info *sbi, |
| 278 | struct bio *bio, enum page_type type) |
Jaegeuk Kim | f573018 | 2016-05-17 16:23:36 -0700 | [diff] [blame] | 279 | { |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 280 | if (!is_read_io(bio_op(bio))) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 281 | unsigned int start; |
| 282 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 283 | if (type != DATA && type != NODE) |
| 284 | goto submit_io; |
| 285 | |
Yunlong Song | 94e1c6a | 2018-07-12 23:09:28 +0800 | [diff] [blame] | 286 | if (test_opt(sbi, LFS) && current->plug) |
Tiezhu Yang | 4d8e4a8 | 2018-02-06 08:21:45 +0800 | [diff] [blame] | 287 | blk_finish_plug(current->plug); |
| 288 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 289 | start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS; |
| 290 | start %= F2FS_IO_SIZE(sbi); |
| 291 | |
| 292 | if (start == 0) |
| 293 | goto submit_io; |
| 294 | |
| 295 | /* fill dummy pages */ |
| 296 | for (; start < F2FS_IO_SIZE(sbi); start++) { |
| 297 | struct page *page = |
| 298 | mempool_alloc(sbi->write_io_dummy, |
Gao Xiang | 07894d2 | 2019-02-19 10:31:52 +0800 | [diff] [blame] | 299 | GFP_NOIO | __GFP_NOFAIL); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 300 | f2fs_bug_on(sbi, !page); |
| 301 | |
Gao Xiang | 07894d2 | 2019-02-19 10:31:52 +0800 | [diff] [blame] | 302 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 303 | SetPagePrivate(page); |
| 304 | set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE); |
| 305 | lock_page(page); |
| 306 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) |
| 307 | f2fs_bug_on(sbi, 1); |
| 308 | } |
| 309 | /* |
| 310 | * In the NODE case, we lose next block address chain. So, we |
| 311 | * need to do checkpoint in f2fs_sync_file. |
| 312 | */ |
| 313 | if (type == NODE) |
| 314 | set_sbi_flag(sbi, SBI_NEED_CP); |
Jaegeuk Kim | 19a5f5e | 2016-06-04 14:25:24 -0700 | [diff] [blame] | 315 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 316 | submit_io: |
| 317 | if (is_read_io(bio_op(bio))) |
| 318 | trace_f2fs_submit_read_bio(sbi->sb, type, bio); |
| 319 | else |
| 320 | trace_f2fs_submit_write_bio(sbi->sb, type, bio); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 321 | submit_bio(bio); |
Jaegeuk Kim | f573018 | 2016-05-17 16:23:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 324 | static void __submit_merged_bio(struct f2fs_bio_info *io) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 325 | { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 326 | struct f2fs_io_info *fio = &io->fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 327 | |
| 328 | if (!io->bio) |
| 329 | return; |
| 330 | |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 331 | bio_set_op_attrs(io->bio, fio->op, fio->op_flags); |
| 332 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 333 | if (is_read_io(fio->op)) |
| 334 | trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio); |
| 335 | else |
| 336 | trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio); |
| 337 | |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 338 | __submit_bio(io->sbi, io->bio, fio->type); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 339 | io->bio = NULL; |
| 340 | } |
| 341 | |
Chao Yu | 39373df | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 342 | static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode, |
| 343 | struct page *page, nid_t ino) |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 344 | { |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 345 | struct bio_vec *bvec; |
| 346 | struct page *target; |
| 347 | int i; |
| 348 | |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 349 | if (!io->bio) |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 350 | return false; |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 351 | |
Chao Yu | 39373df | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 352 | if (!inode && !page && !ino) |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 353 | return true; |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 354 | |
| 355 | bio_for_each_segment_all(bvec, io->bio, i) { |
| 356 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 357 | if (bvec->bv_page->mapping) |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 358 | target = bvec->bv_page; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 359 | else |
| 360 | target = fscrypt_control_page(bvec->bv_page); |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 361 | |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 362 | if (inode && inode == target->mapping->host) |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 363 | return true; |
Chao Yu | 39373df | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 364 | if (page && page == target) |
| 365 | return true; |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 366 | if (ino && ino == ino_of_node(target)) |
| 367 | return true; |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 368 | } |
| 369 | |
Chao Yu | 0fd785e | 2016-01-18 18:24:59 +0800 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | |
Jaegeuk Kim | 9bc576a | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 373 | static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi, |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 374 | enum page_type type, enum temp_type temp) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 375 | { |
| 376 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 377 | struct f2fs_bio_info *io = sbi->write_io[btype] + temp; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 378 | |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 379 | down_write(&io->io_rwsem); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 380 | |
| 381 | /* change META to META_FLUSH in the checkpoint procedure */ |
| 382 | if (type >= META_FLUSH) { |
| 383 | io->fio.type = META_FLUSH; |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 384 | io->fio.op = REQ_OP_WRITE; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 385 | io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC; |
| 386 | if (!test_opt(sbi, NOBARRIER)) |
| 387 | io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 388 | } |
| 389 | __submit_merged_bio(io); |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 390 | up_write(&io->io_rwsem); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 391 | } |
| 392 | |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 393 | static void __submit_merged_write_cond(struct f2fs_sb_info *sbi, |
Chao Yu | 39373df | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 394 | struct inode *inode, struct page *page, |
| 395 | nid_t ino, enum page_type type, bool force) |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 396 | { |
| 397 | enum temp_type temp; |
Yunlong Song | a151f36 | 2018-11-13 11:57:32 +0800 | [diff] [blame] | 398 | bool ret = true; |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 399 | |
| 400 | for (temp = HOT; temp < NR_TEMP_TYPE; temp++) { |
Yunlong Song | a151f36 | 2018-11-13 11:57:32 +0800 | [diff] [blame] | 401 | if (!force) { |
| 402 | enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 403 | struct f2fs_bio_info *io = sbi->write_io[btype] + temp; |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 404 | |
Yunlong Song | a151f36 | 2018-11-13 11:57:32 +0800 | [diff] [blame] | 405 | down_read(&io->io_rwsem); |
| 406 | ret = __has_merged_page(io, inode, page, ino); |
| 407 | up_read(&io->io_rwsem); |
| 408 | } |
| 409 | if (ret) |
| 410 | __f2fs_submit_merged_write(sbi, type, temp); |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 411 | |
| 412 | /* TODO: use HOT temp only for meta pages now. */ |
| 413 | if (type >= META) |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | |
Jaegeuk Kim | 9bc576a | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 418 | void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type) |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 419 | { |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 420 | __submit_merged_write_cond(sbi, NULL, 0, 0, type, true); |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 421 | } |
| 422 | |
Jaegeuk Kim | 9bc576a | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 423 | void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi, |
Chao Yu | 39373df | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 424 | struct inode *inode, struct page *page, |
| 425 | nid_t ino, enum page_type type) |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 426 | { |
Chao Yu | 39373df | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 427 | __submit_merged_write_cond(sbi, inode, page, ino, type, false); |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 428 | } |
| 429 | |
Jaegeuk Kim | 9bc576a | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 430 | void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi) |
Chao Yu | 406657d | 2016-02-24 17:17:55 +0800 | [diff] [blame] | 431 | { |
Jaegeuk Kim | 9bc576a | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 432 | f2fs_submit_merged_write(sbi, DATA); |
| 433 | f2fs_submit_merged_write(sbi, NODE); |
| 434 | f2fs_submit_merged_write(sbi, META); |
Chao Yu | 406657d | 2016-02-24 17:17:55 +0800 | [diff] [blame] | 435 | } |
| 436 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 437 | /* |
| 438 | * Fill the locked page with data located in the block address. |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 439 | * A caller needs to unlock the page on failure. |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 440 | */ |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 441 | int f2fs_submit_page_bio(struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 442 | { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 443 | struct bio *bio; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 444 | struct page *page = fio->encrypted_page ? |
| 445 | fio->encrypted_page : fio->page; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 446 | |
Chao Yu | b3c869a | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 447 | if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, |
| 448 | __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC)) |
| 449 | return -EFAULT; |
| 450 | |
Chao Yu | 2ace38e | 2014-12-24 16:08:14 +0800 | [diff] [blame] | 451 | trace_f2fs_submit_page_bio(page, fio); |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 452 | f2fs_trace_ios(fio, 0); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 453 | |
| 454 | /* Allocate a new bio */ |
Yufen Yu | cbd5e5a | 2018-01-09 19:33:39 +0800 | [diff] [blame] | 455 | bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc, |
Hyunchul Lee | bcdc571 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 456 | 1, is_read_io(fio->op), fio->type, fio->temp); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 457 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 458 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 459 | bio_put(bio); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 460 | return -EFAULT; |
| 461 | } |
Chao Yu | b8cc7a9 | 2018-10-22 23:24:28 +0800 | [diff] [blame] | 462 | |
| 463 | if (fio->io_wbc && !is_read_io(fio->op)) |
| 464 | wbc_account_io(fio->io_wbc, page, PAGE_SIZE); |
| 465 | |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 466 | bio_set_op_attrs(bio, fio->op, fio->op_flags); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 467 | |
Jaegeuk Kim | 8025d72 | 2018-10-16 10:20:53 -0700 | [diff] [blame] | 468 | inc_page_count(fio->sbi, is_read_io(fio->op) ? |
| 469 | __read_io_type(page): WB_DATA_TYPE(fio->page)); |
Chao Yu | f44ba51 | 2018-10-22 09:12:51 +0800 | [diff] [blame] | 470 | |
| 471 | __submit_bio(fio->sbi, bio, fio->type); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 472 | return 0; |
| 473 | } |
| 474 | |
Chao Yu | 34880e0 | 2018-05-28 23:47:18 +0800 | [diff] [blame] | 475 | void f2fs_submit_page_write(struct f2fs_io_info *fio) |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 476 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 477 | struct f2fs_sb_info *sbi = fio->sbi; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 478 | enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 479 | struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 480 | struct page *bio_page; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 481 | |
Jaegeuk Kim | 9bc576a | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 482 | f2fs_bug_on(sbi, is_read_io(fio->op)); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 483 | |
Chao Yu | c52dc0f | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 484 | down_write(&io->io_rwsem); |
| 485 | next: |
| 486 | if (fio->in_list) { |
| 487 | spin_lock(&io->io_lock); |
| 488 | if (list_empty(&io->io_list)) { |
| 489 | spin_unlock(&io->io_lock); |
Chao Yu | 34880e0 | 2018-05-28 23:47:18 +0800 | [diff] [blame] | 490 | goto out; |
Chao Yu | c52dc0f | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 491 | } |
| 492 | fio = list_first_entry(&io->io_list, |
| 493 | struct f2fs_io_info, list); |
| 494 | list_del(&fio->list); |
| 495 | spin_unlock(&io->io_lock); |
| 496 | } |
| 497 | |
Chao Yu | d05e871 | 2018-06-05 17:44:11 +0800 | [diff] [blame] | 498 | if (__is_valid_data_blkaddr(fio->old_blkaddr)) |
Yunlei He | a3f8ec8 | 2018-03-08 16:29:13 +0800 | [diff] [blame] | 499 | verify_block_addr(fio, fio->old_blkaddr); |
| 500 | verify_block_addr(fio, fio->new_blkaddr); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 501 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 502 | bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page; |
| 503 | |
Thomas Meyer | 5950467 | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 504 | /* set submitted = true as a return value */ |
| 505 | fio->submitted = true; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 506 | |
Jaegeuk Kim | 9bc576a | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 507 | inc_page_count(sbi, WB_DATA_TYPE(bio_page)); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 508 | |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 509 | if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 || |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 510 | (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) || |
| 511 | !__same_bdev(sbi, fio->new_blkaddr, io->bio))) |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 512 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 513 | alloc_new: |
| 514 | if (io->bio == NULL) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 515 | if ((fio->type == DATA || fio->type == NODE) && |
| 516 | fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) { |
Jaegeuk Kim | 9bc576a | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 517 | dec_page_count(sbi, WB_DATA_TYPE(bio_page)); |
Chao Yu | 34880e0 | 2018-05-28 23:47:18 +0800 | [diff] [blame] | 518 | fio->retry = true; |
| 519 | goto skip; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 520 | } |
Yufen Yu | cbd5e5a | 2018-01-09 19:33:39 +0800 | [diff] [blame] | 521 | io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc, |
Hyunchul Lee | bcdc571 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 522 | BIO_MAX_PAGES, false, |
| 523 | fio->type, fio->temp); |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 524 | io->fio = *fio; |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 525 | } |
| 526 | |
Jaegeuk Kim | c412726 | 2017-05-10 11:18:25 -0700 | [diff] [blame] | 527 | if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) { |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 528 | __submit_merged_bio(io); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 529 | goto alloc_new; |
| 530 | } |
| 531 | |
Yufen Yu | cbd5e5a | 2018-01-09 19:33:39 +0800 | [diff] [blame] | 532 | if (fio->io_wbc) |
| 533 | wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE); |
| 534 | |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 535 | io->last_block_in_bio = fio->new_blkaddr; |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 536 | f2fs_trace_ios(fio, 0); |
Chao Yu | c52dc0f | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 537 | |
| 538 | trace_f2fs_submit_page_write(fio->page, fio); |
Chao Yu | 34880e0 | 2018-05-28 23:47:18 +0800 | [diff] [blame] | 539 | skip: |
Chao Yu | c52dc0f | 2017-05-19 23:37:01 +0800 | [diff] [blame] | 540 | if (fio->in_list) |
| 541 | goto next; |
Chao Yu | 34880e0 | 2018-05-28 23:47:18 +0800 | [diff] [blame] | 542 | out: |
Daniel Rosenberg | f22f93a | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 543 | if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || |
| 544 | f2fs_is_checkpoint_ready(sbi)) |
Jaegeuk Kim | 60208e2 | 2018-09-06 11:40:12 -0700 | [diff] [blame] | 545 | __submit_merged_bio(io); |
Chao Yu | df0f8dc | 2014-03-22 14:57:23 +0800 | [diff] [blame] | 546 | up_write(&io->io_rwsem); |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 547 | } |
| 548 | |
Jaegeuk Kim | 5a69067 | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 549 | static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr, |
Jaegeuk Kim | f9d75d6 | 2018-06-21 11:29:43 -0700 | [diff] [blame] | 550 | unsigned nr_pages, unsigned op_flag) |
Jaegeuk Kim | 5a69067 | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 551 | { |
| 552 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 5a69067 | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 553 | struct bio *bio; |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 554 | struct bio_post_read_ctx *ctx; |
| 555 | unsigned int post_read_steps = 0; |
Jaegeuk Kim | 5a69067 | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 556 | |
Chao Yu | 10d6703 | 2018-07-10 23:01:45 +0800 | [diff] [blame] | 557 | if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) |
| 558 | return ERR_PTR(-EFAULT); |
| 559 | |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 560 | bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false); |
| 561 | if (!bio) |
| 562 | return ERR_PTR(-ENOMEM); |
| 563 | f2fs_target_device(sbi, blkaddr, bio); |
| 564 | bio->bi_end_io = f2fs_read_end_io; |
Jaegeuk Kim | f9d75d6 | 2018-06-21 11:29:43 -0700 | [diff] [blame] | 565 | bio_set_op_attrs(bio, REQ_OP_READ, op_flag); |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 566 | |
| 567 | if (f2fs_encrypted_file(inode)) |
| 568 | post_read_steps |= 1 << STEP_DECRYPT; |
| 569 | if (post_read_steps) { |
| 570 | ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS); |
| 571 | if (!ctx) { |
| 572 | bio_put(bio); |
| 573 | return ERR_PTR(-ENOMEM); |
| 574 | } |
| 575 | ctx->bio = bio; |
| 576 | ctx->enabled_steps = post_read_steps; |
| 577 | bio->bi_private = ctx; |
Jaegeuk Kim | 5a69067 | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Jaegeuk Kim | 5a69067 | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 580 | return bio; |
| 581 | } |
| 582 | |
| 583 | /* This can handle encryption stuffs */ |
| 584 | static int f2fs_submit_page_read(struct inode *inode, struct page *page, |
| 585 | block_t blkaddr) |
| 586 | { |
Jaegeuk Kim | f9d75d6 | 2018-06-21 11:29:43 -0700 | [diff] [blame] | 587 | struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0); |
Jaegeuk Kim | 5a69067 | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 588 | |
| 589 | if (IS_ERR(bio)) |
| 590 | return PTR_ERR(bio); |
| 591 | |
Jaegeuk Kim | 0d51cea | 2018-08-22 21:18:00 -0700 | [diff] [blame] | 592 | /* wait for GCed page writeback via META_MAPPING */ |
| 593 | f2fs_wait_on_block_writeback(inode, blkaddr); |
| 594 | |
Jaegeuk Kim | 5a69067 | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 595 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { |
| 596 | bio_put(bio); |
| 597 | return -EFAULT; |
| 598 | } |
Jaegeuk Kim | 4edba23 | 2018-09-25 13:54:33 -0700 | [diff] [blame] | 599 | ClearPageError(page); |
Jaegeuk Kim | 8025d72 | 2018-10-16 10:20:53 -0700 | [diff] [blame] | 600 | inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA); |
Jaegeuk Kim | 5a69067 | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 601 | __submit_bio(F2FS_I_SB(inode), bio, DATA); |
| 602 | return 0; |
| 603 | } |
| 604 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 605 | static void __set_data_blkaddr(struct dnode_of_data *dn) |
| 606 | { |
| 607 | struct f2fs_node *rn = F2FS_NODE(dn->node_page); |
| 608 | __le32 *addr_array; |
Chao Yu | fbcf931 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 609 | int base = 0; |
| 610 | |
| 611 | if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode)) |
| 612 | base = get_extra_isize(dn->inode); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 613 | |
| 614 | /* Get physical address of data block */ |
| 615 | addr_array = blkaddr_in_node(rn); |
Chao Yu | fbcf931 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 616 | addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 617 | } |
| 618 | |
Jaegeuk Kim | 93dfe2a | 2013-11-30 12:51:14 +0900 | [diff] [blame] | 619 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 620 | * Lock ordering for the change of data block address: |
| 621 | * ->data_page |
| 622 | * ->node_page |
| 623 | * update block addresses in the node page |
| 624 | */ |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 625 | void f2fs_set_data_blkaddr(struct dnode_of_data *dn) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 626 | { |
Chao Yu | d9b3df5 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 627 | f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 628 | __set_data_blkaddr(dn); |
| 629 | if (set_page_dirty(dn->node_page)) |
Jaegeuk Kim | 12719ae | 2016-01-07 13:23:12 -0800 | [diff] [blame] | 630 | dn->node_changed = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 631 | } |
| 632 | |
Chao Yu | f28b343 | 2016-02-24 17:16:47 +0800 | [diff] [blame] | 633 | void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) |
| 634 | { |
| 635 | dn->data_blkaddr = blkaddr; |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 636 | f2fs_set_data_blkaddr(dn); |
Chao Yu | f28b343 | 2016-02-24 17:16:47 +0800 | [diff] [blame] | 637 | f2fs_update_extent_cache(dn); |
| 638 | } |
| 639 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 640 | /* dn->ofs_in_node will be returned with up-to-date last block pointer */ |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 641 | int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 642 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 643 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Chao Yu | 09c3a72 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 644 | int err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 645 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 646 | if (!count) |
| 647 | return 0; |
| 648 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 649 | if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 650 | return -EPERM; |
Chao Yu | 09c3a72 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 651 | if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count)))) |
| 652 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 653 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 654 | trace_f2fs_reserve_new_blocks(dn->inode, dn->nid, |
| 655 | dn->ofs_in_node, count); |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 656 | |
Chao Yu | d9b3df5 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 657 | f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 658 | |
| 659 | for (; count > 0; dn->ofs_in_node++) { |
Chao Yu | fbcf931 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 660 | block_t blkaddr = datablock_addr(dn->inode, |
| 661 | dn->node_page, dn->ofs_in_node); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 662 | if (blkaddr == NULL_ADDR) { |
| 663 | dn->data_blkaddr = NEW_ADDR; |
| 664 | __set_data_blkaddr(dn); |
| 665 | count--; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | if (set_page_dirty(dn->node_page)) |
| 670 | dn->node_changed = true; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 671 | return 0; |
| 672 | } |
| 673 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 674 | /* Should keep dn->ofs_in_node unchanged */ |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 675 | int f2fs_reserve_new_block(struct dnode_of_data *dn) |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 676 | { |
| 677 | unsigned int ofs_in_node = dn->ofs_in_node; |
| 678 | int ret; |
| 679 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 680 | ret = f2fs_reserve_new_blocks(dn, 1); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 681 | dn->ofs_in_node = ofs_in_node; |
| 682 | return ret; |
| 683 | } |
| 684 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 685 | int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
| 686 | { |
| 687 | bool need_put = dn->inode_page ? false : true; |
| 688 | int err; |
| 689 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 690 | err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 691 | if (err) |
| 692 | return err; |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 693 | |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 694 | if (dn->data_blkaddr == NULL_ADDR) |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 695 | err = f2fs_reserve_new_block(dn); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 696 | if (err || need_put) |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 697 | f2fs_put_dnode(dn); |
| 698 | return err; |
| 699 | } |
| 700 | |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 701 | int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 702 | { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 703 | struct extent_info ei = {0,0,0}; |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 704 | struct inode *inode = dn->inode; |
Chao Yu | 028a41e | 2015-03-19 19:26:02 +0800 | [diff] [blame] | 705 | |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 706 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 707 | dn->data_blkaddr = ei.blk + index - ei.fofs; |
| 708 | return 0; |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 709 | } |
| 710 | |
Fan Li | 759af1c | 2015-08-05 15:52:16 +0800 | [diff] [blame] | 711 | return f2fs_reserve_block(dn, index); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 712 | } |
| 713 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 714 | struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 715 | int op_flags, bool for_write) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 716 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 717 | struct address_space *mapping = inode->i_mapping; |
| 718 | struct dnode_of_data dn; |
| 719 | struct page *page; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 720 | struct extent_info ei = {0,0,0}; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 721 | int err; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 722 | |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 723 | page = f2fs_grab_cache_page(mapping, index, for_write); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 724 | if (!page) |
| 725 | return ERR_PTR(-ENOMEM); |
| 726 | |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 727 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 728 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 729 | goto got_it; |
| 730 | } |
| 731 | |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 732 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 733 | err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 734 | if (err) |
| 735 | goto put_err; |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 736 | f2fs_put_dnode(&dn); |
| 737 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 738 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 739 | err = -ENOENT; |
| 740 | goto put_err; |
Jaegeuk Kim | 650495d | 2013-05-13 08:38:35 +0900 | [diff] [blame] | 741 | } |
Chao Yu | cb3bc9e | 2015-02-05 18:03:40 +0800 | [diff] [blame] | 742 | got_it: |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 743 | if (PageUptodate(page)) { |
| 744 | unlock_page(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 745 | return page; |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 746 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 747 | |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 748 | /* |
| 749 | * A new dentry page is allocated but not able to be written, since its |
| 750 | * new inode page couldn't be allocated due to -ENOSPC. |
| 751 | * In such the case, its blkaddr can be remained as NEW_ADDR. |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 752 | * see, f2fs_add_link -> f2fs_get_new_data_page -> |
| 753 | * f2fs_init_inode_metadata. |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 754 | */ |
| 755 | if (dn.data_blkaddr == NEW_ADDR) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 756 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 757 | if (!PageUptodate(page)) |
| 758 | SetPageUptodate(page); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 759 | unlock_page(page); |
Jaegeuk Kim | d59ff4d | 2013-08-20 19:13:07 +0900 | [diff] [blame] | 760 | return page; |
| 761 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 762 | |
Jaegeuk Kim | 5a69067 | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 763 | err = f2fs_submit_page_read(inode, page, dn.data_blkaddr); |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 764 | if (err) |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 765 | goto put_err; |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 766 | return page; |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 767 | |
| 768 | put_err: |
| 769 | f2fs_put_page(page, 1); |
| 770 | return ERR_PTR(err); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 771 | } |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 772 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 773 | struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index) |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 774 | { |
| 775 | struct address_space *mapping = inode->i_mapping; |
| 776 | struct page *page; |
| 777 | |
| 778 | page = find_get_page(mapping, index); |
| 779 | if (page && PageUptodate(page)) |
| 780 | return page; |
| 781 | f2fs_put_page(page, 0); |
| 782 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 783 | page = f2fs_get_read_data_page(inode, index, 0, false); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 784 | if (IS_ERR(page)) |
| 785 | return page; |
| 786 | |
| 787 | if (PageUptodate(page)) |
| 788 | return page; |
| 789 | |
| 790 | wait_on_page_locked(page); |
| 791 | if (unlikely(!PageUptodate(page))) { |
| 792 | f2fs_put_page(page, 0); |
| 793 | return ERR_PTR(-EIO); |
| 794 | } |
| 795 | return page; |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * If it tries to access a hole, return an error. |
| 800 | * Because, the callers, functions in dir.c and GC, should be able to know |
| 801 | * whether this page exists or not. |
| 802 | */ |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 803 | struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index, |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 804 | bool for_write) |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 805 | { |
| 806 | struct address_space *mapping = inode->i_mapping; |
| 807 | struct page *page; |
| 808 | repeat: |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 809 | page = f2fs_get_read_data_page(inode, index, 0, for_write); |
Jaegeuk Kim | 43f3eae | 2015-04-30 17:00:33 -0700 | [diff] [blame] | 810 | if (IS_ERR(page)) |
| 811 | return page; |
| 812 | |
| 813 | /* wait for read completion */ |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 814 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 815 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 816 | f2fs_put_page(page, 1); |
| 817 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 818 | } |
Chao Yu | 1563ac7 | 2016-07-03 22:05:12 +0800 | [diff] [blame] | 819 | if (unlikely(!PageUptodate(page))) { |
| 820 | f2fs_put_page(page, 1); |
| 821 | return ERR_PTR(-EIO); |
| 822 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 823 | return page; |
| 824 | } |
| 825 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 826 | /* |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 827 | * Caller ensures that this data page is never allocated. |
| 828 | * A new zero-filled data page is allocated in the page cache. |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 829 | * |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 830 | * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 831 | * f2fs_unlock_op(). |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 832 | * Note that, ipage is set only by make_empty_dir, and if any error occur, |
| 833 | * ipage should be released by this function. |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 834 | */ |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 835 | struct page *f2fs_get_new_data_page(struct inode *inode, |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 836 | struct page *ipage, pgoff_t index, bool new_i_size) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 837 | { |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 838 | struct address_space *mapping = inode->i_mapping; |
| 839 | struct page *page; |
| 840 | struct dnode_of_data dn; |
| 841 | int err; |
Jaegeuk Kim | 7612118 | 2016-01-01 22:03:47 -0800 | [diff] [blame] | 842 | |
Jaegeuk Kim | a56c7c6 | 2015-10-09 15:11:38 -0700 | [diff] [blame] | 843 | page = f2fs_grab_cache_page(mapping, index, true); |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 844 | if (!page) { |
| 845 | /* |
| 846 | * before exiting, we should make sure ipage will be released |
| 847 | * if any error occur. |
| 848 | */ |
| 849 | f2fs_put_page(ipage, 1); |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 850 | return ERR_PTR(-ENOMEM); |
Chao Yu | 470f00e | 2015-07-14 18:14:06 +0800 | [diff] [blame] | 851 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 852 | |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 853 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 854 | err = f2fs_reserve_block(&dn, index); |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 855 | if (err) { |
| 856 | f2fs_put_page(page, 1); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 857 | return ERR_PTR(err); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 858 | } |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 859 | if (!ipage) |
| 860 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 861 | |
| 862 | if (PageUptodate(page)) |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 863 | goto got_it; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 864 | |
| 865 | if (dn.data_blkaddr == NEW_ADDR) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 866 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 867 | if (!PageUptodate(page)) |
| 868 | SetPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 869 | } else { |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 870 | f2fs_put_page(page, 1); |
Jaegeuk Kim | a886537 | 2013-12-27 17:04:17 +0900 | [diff] [blame] | 871 | |
Jaegeuk Kim | 7612118 | 2016-01-01 22:03:47 -0800 | [diff] [blame] | 872 | /* if ipage exists, blkaddr should be NEW_ADDR */ |
| 873 | f2fs_bug_on(F2FS_I_SB(inode), ipage); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 874 | page = f2fs_get_lock_data_page(inode, index, true); |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 875 | if (IS_ERR(page)) |
Jaegeuk Kim | 7612118 | 2016-01-01 22:03:47 -0800 | [diff] [blame] | 876 | return page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 877 | } |
Jaegeuk Kim | 01f2861 | 2015-04-29 11:18:42 -0700 | [diff] [blame] | 878 | got_it: |
Chao Yu | 9edcdab | 2015-09-11 14:43:52 +0800 | [diff] [blame] | 879 | if (new_i_size && i_size_read(inode) < |
Jaegeuk Kim | ee6d182 | 2016-05-20 16:32:49 -0700 | [diff] [blame] | 880 | ((loff_t)(index + 1) << PAGE_SHIFT)) |
Jaegeuk Kim | fc9581c | 2016-05-20 09:22:03 -0700 | [diff] [blame] | 881 | f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT)); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 882 | return page; |
| 883 | } |
| 884 | |
Hyunchul Lee | 1a9d6a9 | 2017-11-28 09:23:00 +0900 | [diff] [blame] | 885 | static int __allocate_data_block(struct dnode_of_data *dn, int seg_type) |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 886 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 887 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 888 | struct f2fs_summary sum; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 889 | struct node_info ni; |
Chao Yu | 08386a6 | 2018-08-14 22:37:25 +0800 | [diff] [blame] | 890 | block_t old_blkaddr; |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 891 | blkcnt_t count = 1; |
Chao Yu | 09c3a72 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 892 | int err; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 893 | |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 894 | if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 895 | return -EPERM; |
Chao Yu | df6136e | 2015-03-23 10:33:37 +0800 | [diff] [blame] | 896 | |
Chao Yu | 3c74326 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 897 | err = f2fs_get_node_info(sbi, dn->nid, &ni); |
| 898 | if (err) |
| 899 | return err; |
| 900 | |
Chao Yu | fbcf931 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 901 | dn->data_blkaddr = datablock_addr(dn->inode, |
| 902 | dn->node_page, dn->ofs_in_node); |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 903 | if (dn->data_blkaddr != NULL_ADDR) |
Chao Yu | df6136e | 2015-03-23 10:33:37 +0800 | [diff] [blame] | 904 | goto alloc; |
| 905 | |
Chao Yu | 09c3a72 | 2017-07-09 00:13:07 +0800 | [diff] [blame] | 906 | if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count)))) |
| 907 | return err; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 908 | |
Chao Yu | df6136e | 2015-03-23 10:33:37 +0800 | [diff] [blame] | 909 | alloc: |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 910 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
Chao Yu | 08386a6 | 2018-08-14 22:37:25 +0800 | [diff] [blame] | 911 | old_blkaddr = dn->data_blkaddr; |
| 912 | f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr, |
Hyunchul Lee | 1a9d6a9 | 2017-11-28 09:23:00 +0900 | [diff] [blame] | 913 | &sum, seg_type, NULL, false); |
Chao Yu | 08386a6 | 2018-08-14 22:37:25 +0800 | [diff] [blame] | 914 | if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) |
| 915 | invalidate_mapping_pages(META_MAPPING(sbi), |
| 916 | old_blkaddr, old_blkaddr); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 917 | f2fs_set_data_blkaddr(dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 918 | |
Jaegeuk Kim | 0328f88 | 2018-09-19 15:28:40 -0700 | [diff] [blame] | 919 | /* |
| 920 | * i_size will be updated by direct_IO. Otherwise, we'll get stale |
| 921 | * data from unwritten block via dio_read. |
| 922 | */ |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 923 | return 0; |
| 924 | } |
| 925 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 926 | int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from) |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 927 | { |
Jaegeuk Kim | b439b10 | 2016-02-03 13:09:09 -0800 | [diff] [blame] | 928 | struct inode *inode = file_inode(iocb->ki_filp); |
Chao Yu | 5b8db7f | 2016-01-26 15:38:29 +0800 | [diff] [blame] | 929 | struct f2fs_map_blocks map; |
Chao Yu | 8fc7446 | 2018-01-03 17:30:19 +0800 | [diff] [blame] | 930 | int flag; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 931 | int err = 0; |
Chao Yu | 8fc7446 | 2018-01-03 17:30:19 +0800 | [diff] [blame] | 932 | bool direct_io = iocb->ki_flags & IOCB_DIRECT; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 933 | |
Weichao Guo | 8456d34 | 2017-09-29 22:43:23 +0800 | [diff] [blame] | 934 | /* convert inline data for Direct I/O*/ |
Chao Yu | 8fc7446 | 2018-01-03 17:30:19 +0800 | [diff] [blame] | 935 | if (direct_io) { |
Weichao Guo | 8456d34 | 2017-09-29 22:43:23 +0800 | [diff] [blame] | 936 | err = f2fs_convert_inline_inode(inode); |
| 937 | if (err) |
| 938 | return err; |
| 939 | } |
| 940 | |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 941 | if (direct_io && allow_outplace_dio(inode, iocb, from)) |
| 942 | return 0; |
| 943 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 944 | if (is_inode_flag_set(inode, FI_NO_PREALLOC)) |
| 945 | return 0; |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 946 | |
Jaegeuk Kim | 0080c50 | 2016-05-07 08:52:57 -0700 | [diff] [blame] | 947 | map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos); |
Chao Yu | dfd02e4 | 2016-08-20 15:12:01 +0800 | [diff] [blame] | 948 | map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from)); |
| 949 | if (map.m_len > map.m_lblk) |
| 950 | map.m_len -= map.m_lblk; |
| 951 | else |
| 952 | map.m_len = 0; |
| 953 | |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 954 | map.m_next_pgofs = NULL; |
Chao Yu | 6bce963 | 2018-01-11 14:42:30 +0800 | [diff] [blame] | 955 | map.m_next_extent = NULL; |
Hyunchul Lee | 1a9d6a9 | 2017-11-28 09:23:00 +0900 | [diff] [blame] | 956 | map.m_seg_type = NO_CHECK_TYPE; |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 957 | map.m_may_create = true; |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 958 | |
Chao Yu | 8fc7446 | 2018-01-03 17:30:19 +0800 | [diff] [blame] | 959 | if (direct_io) { |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 960 | map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint); |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 961 | flag = f2fs_force_buffered_io(inode, iocb, from) ? |
Chao Yu | 8fc7446 | 2018-01-03 17:30:19 +0800 | [diff] [blame] | 962 | F2FS_GET_BLOCK_PRE_AIO : |
| 963 | F2FS_GET_BLOCK_PRE_DIO; |
| 964 | goto map_blocks; |
Hyunchul Lee | 1a9d6a9 | 2017-11-28 09:23:00 +0900 | [diff] [blame] | 965 | } |
Chao Yu | e84f88e | 2017-07-19 00:19:05 +0800 | [diff] [blame] | 966 | if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 967 | err = f2fs_convert_inline_inode(inode); |
| 968 | if (err) |
| 969 | return err; |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 970 | } |
Chao Yu | 8fc7446 | 2018-01-03 17:30:19 +0800 | [diff] [blame] | 971 | if (f2fs_has_inline_data(inode)) |
Sheng Yong | 76b6e8e | 2017-11-22 18:23:39 +0800 | [diff] [blame] | 972 | return err; |
Chao Yu | 8fc7446 | 2018-01-03 17:30:19 +0800 | [diff] [blame] | 973 | |
| 974 | flag = F2FS_GET_BLOCK_PRE_AIO; |
| 975 | |
| 976 | map_blocks: |
| 977 | err = f2fs_map_blocks(inode, &map, 1, flag); |
| 978 | if (map.m_len > 0 && err == -ENOSPC) { |
| 979 | if (!direct_io) |
| 980 | set_inode_flag(inode, FI_NO_PREALLOC); |
| 981 | err = 0; |
Sheng Yong | 76b6e8e | 2017-11-22 18:23:39 +0800 | [diff] [blame] | 982 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 983 | return err; |
| 984 | } |
| 985 | |
Chao Yu | 6923cd1 | 2018-09-27 18:33:18 +0800 | [diff] [blame] | 986 | void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 987 | { |
| 988 | if (flag == F2FS_GET_BLOCK_PRE_AIO) { |
| 989 | if (lock) |
| 990 | down_read(&sbi->node_change); |
| 991 | else |
| 992 | up_read(&sbi->node_change); |
| 993 | } else { |
| 994 | if (lock) |
| 995 | f2fs_lock_op(sbi); |
| 996 | else |
| 997 | f2fs_unlock_op(sbi); |
| 998 | } |
Jaegeuk Kim | 59b802e | 2015-02-09 12:09:53 -0800 | [diff] [blame] | 999 | } |
| 1000 | |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1001 | /* |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1002 | * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with |
| 1003 | * f2fs_map_blocks structure. |
Chao Yu | 4f4124d | 2013-12-21 18:02:14 +0800 | [diff] [blame] | 1004 | * If original data blocks are allocated, then give them to blockdev. |
| 1005 | * Otherwise, |
| 1006 | * a. preallocate requested block addresses |
| 1007 | * b. do not use extent cache for better performance |
| 1008 | * c. give the block addresses to blockdev |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1009 | */ |
Chao Yu | d323d00 | 2015-10-27 09:53:45 +0800 | [diff] [blame] | 1010 | int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1011 | int create, int flag) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1012 | { |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1013 | unsigned int maxblocks = map->m_len; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1014 | struct dnode_of_data dn; |
Chao Yu | f981170 | 2015-09-21 20:17:52 +0800 | [diff] [blame] | 1015 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 1016 | int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE; |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1017 | pgoff_t pgofs, end_offset, end; |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1018 | int err = 0, ofs = 1; |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1019 | unsigned int ofs_in_node, last_ofs_in_node; |
| 1020 | blkcnt_t prealloc; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1021 | struct extent_info ei = {0,0,0}; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1022 | block_t blkaddr; |
Chao Yu | 6bce963 | 2018-01-11 14:42:30 +0800 | [diff] [blame] | 1023 | unsigned int start_pgofs; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1024 | |
Chao Yu | dfd02e4 | 2016-08-20 15:12:01 +0800 | [diff] [blame] | 1025 | if (!maxblocks) |
| 1026 | return 0; |
| 1027 | |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1028 | map->m_len = 0; |
| 1029 | map->m_flags = 0; |
| 1030 | |
| 1031 | /* it only supports block size == page size */ |
| 1032 | pgofs = (pgoff_t)map->m_lblk; |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1033 | end = pgofs + maxblocks; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1034 | |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 1035 | if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) { |
Jia Zhu | e3faef1 | 2018-11-20 04:29:35 +0800 | [diff] [blame] | 1036 | if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO && |
| 1037 | map->m_may_create) |
| 1038 | goto next_dnode; |
| 1039 | |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1040 | map->m_pblk = ei.blk + pgofs - ei.fofs; |
| 1041 | map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs); |
| 1042 | map->m_flags = F2FS_MAP_MAPPED; |
Chao Yu | 6bce963 | 2018-01-11 14:42:30 +0800 | [diff] [blame] | 1043 | if (map->m_next_extent) |
| 1044 | *map->m_next_extent = pgofs + map->m_len; |
Sahitya Tummala | 3d57147 | 2018-10-10 10:56:22 +0530 | [diff] [blame] | 1045 | |
| 1046 | /* for hardware encryption, but to avoid potential issue in future */ |
| 1047 | if (flag == F2FS_GET_BLOCK_DIO) |
| 1048 | f2fs_wait_on_block_writeback_range(inode, |
| 1049 | map->m_pblk, map->m_len); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1050 | goto out; |
Chao Yu | a2e7d1b | 2015-02-05 17:50:30 +0800 | [diff] [blame] | 1051 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1052 | |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1053 | next_dnode: |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 1054 | if (map->m_may_create) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1055 | __do_map_lock(sbi, flag, true); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1056 | |
| 1057 | /* When reading holes, we need its node page */ |
| 1058 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1059 | err = f2fs_get_dnode_of_data(&dn, pgofs, mode); |
Jaegeuk Kim | 1ec7908 | 2013-12-26 16:55:22 +0900 | [diff] [blame] | 1060 | if (err) { |
Chao Yu | 43473f9 | 2016-05-05 19:13:02 +0800 | [diff] [blame] | 1061 | if (flag == F2FS_GET_BLOCK_BMAP) |
| 1062 | map->m_pblk = 0; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1063 | if (err == -ENOENT) { |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1064 | err = 0; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1065 | if (map->m_next_pgofs) |
| 1066 | *map->m_next_pgofs = |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1067 | f2fs_get_next_page_offset(&dn, pgofs); |
Chao Yu | 6bce963 | 2018-01-11 14:42:30 +0800 | [diff] [blame] | 1068 | if (map->m_next_extent) |
| 1069 | *map->m_next_extent = |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1070 | f2fs_get_next_page_offset(&dn, pgofs); |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1071 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1072 | goto unlock_out; |
Namjae Jeon | 848753a | 2013-04-23 16:38:02 +0900 | [diff] [blame] | 1073 | } |
Chao Yu | 973163f | 2015-09-18 16:51:51 +0800 | [diff] [blame] | 1074 | |
Chao Yu | 6bce963 | 2018-01-11 14:42:30 +0800 | [diff] [blame] | 1075 | start_pgofs = pgofs; |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1076 | prealloc = 0; |
Arnd Bergmann | a299abd | 2016-11-02 14:52:15 +0100 | [diff] [blame] | 1077 | last_ofs_in_node = ofs_in_node = dn.ofs_in_node; |
Chao Yu | 81ca735 | 2016-01-26 15:39:35 +0800 | [diff] [blame] | 1078 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1079 | |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1080 | next_block: |
Chao Yu | fbcf931 | 2017-07-19 00:19:06 +0800 | [diff] [blame] | 1081 | blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node); |
Chao Yu | 973163f | 2015-09-18 16:51:51 +0800 | [diff] [blame] | 1082 | |
Chao Yu | b3c869a | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 1083 | if (__is_valid_data_blkaddr(blkaddr) && |
| 1084 | !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) { |
| 1085 | err = -EFAULT; |
| 1086 | goto sync_out; |
| 1087 | } |
| 1088 | |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 1089 | if (is_valid_data_blkaddr(sbi, blkaddr)) { |
| 1090 | /* use out-place-update for driect IO under LFS mode */ |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 1091 | if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO && |
| 1092 | map->m_may_create) { |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 1093 | err = __allocate_data_block(&dn, map->m_seg_type); |
Jia Zhu | 35304bb | 2018-11-27 02:32:32 +0800 | [diff] [blame] | 1094 | if (!err) { |
| 1095 | blkaddr = dn.data_blkaddr; |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 1096 | set_inode_flag(inode, FI_APPEND_WRITE); |
Jia Zhu | 35304bb | 2018-11-27 02:32:32 +0800 | [diff] [blame] | 1097 | } |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 1098 | } |
| 1099 | } else { |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1100 | if (create) { |
| 1101 | if (unlikely(f2fs_cp_error(sbi))) { |
| 1102 | err = -EIO; |
| 1103 | goto sync_out; |
Chao Yu | 973163f | 2015-09-18 16:51:51 +0800 | [diff] [blame] | 1104 | } |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 1105 | if (flag == F2FS_GET_BLOCK_PRE_AIO) { |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1106 | if (blkaddr == NULL_ADDR) { |
| 1107 | prealloc++; |
| 1108 | last_ofs_in_node = dn.ofs_in_node; |
| 1109 | } |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 1110 | } else { |
Jaegeuk Kim | 0328f88 | 2018-09-19 15:28:40 -0700 | [diff] [blame] | 1111 | WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO && |
| 1112 | flag != F2FS_GET_BLOCK_DIO); |
Hyunchul Lee | 1a9d6a9 | 2017-11-28 09:23:00 +0900 | [diff] [blame] | 1113 | err = __allocate_data_block(&dn, |
| 1114 | map->m_seg_type); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1115 | if (!err) |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 1116 | set_inode_flag(inode, FI_APPEND_WRITE); |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 1117 | } |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1118 | if (err) |
| 1119 | goto sync_out; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1120 | map->m_flags |= F2FS_MAP_NEW; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1121 | blkaddr = dn.data_blkaddr; |
| 1122 | } else { |
Chao Yu | 43473f9 | 2016-05-05 19:13:02 +0800 | [diff] [blame] | 1123 | if (flag == F2FS_GET_BLOCK_BMAP) { |
| 1124 | map->m_pblk = 0; |
| 1125 | goto sync_out; |
| 1126 | } |
Chao Yu | 6bce963 | 2018-01-11 14:42:30 +0800 | [diff] [blame] | 1127 | if (flag == F2FS_GET_BLOCK_PRECACHE) |
| 1128 | goto sync_out; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1129 | if (flag == F2FS_GET_BLOCK_FIEMAP && |
| 1130 | blkaddr == NULL_ADDR) { |
| 1131 | if (map->m_next_pgofs) |
| 1132 | *map->m_next_pgofs = pgofs + 1; |
Chao Yu | 987892c | 2017-11-30 19:28:22 +0800 | [diff] [blame] | 1133 | goto sync_out; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1134 | } |
Chao Yu | fd21442 | 2018-01-10 18:18:52 +0800 | [diff] [blame] | 1135 | if (flag != F2FS_GET_BLOCK_FIEMAP) { |
| 1136 | /* for defragment case */ |
| 1137 | if (map->m_next_pgofs) |
| 1138 | *map->m_next_pgofs = pgofs + 1; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1139 | goto sync_out; |
Chao Yu | fd21442 | 2018-01-10 18:18:52 +0800 | [diff] [blame] | 1140 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1141 | } |
| 1142 | } |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1143 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1144 | if (flag == F2FS_GET_BLOCK_PRE_AIO) |
| 1145 | goto skip; |
| 1146 | |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1147 | if (map->m_len == 0) { |
| 1148 | /* preallocated unwritten block should be mapped for fiemap. */ |
| 1149 | if (blkaddr == NEW_ADDR) |
| 1150 | map->m_flags |= F2FS_MAP_UNWRITTEN; |
| 1151 | map->m_flags |= F2FS_MAP_MAPPED; |
| 1152 | |
| 1153 | map->m_pblk = blkaddr; |
| 1154 | map->m_len = 1; |
| 1155 | } else if ((map->m_pblk != NEW_ADDR && |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1156 | blkaddr == (map->m_pblk + ofs)) || |
Jaegeuk Kim | b439b10 | 2016-02-03 13:09:09 -0800 | [diff] [blame] | 1157 | (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) || |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1158 | flag == F2FS_GET_BLOCK_PRE_DIO) { |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1159 | ofs++; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1160 | map->m_len++; |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1161 | } else { |
| 1162 | goto sync_out; |
| 1163 | } |
| 1164 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1165 | skip: |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1166 | dn.ofs_in_node++; |
| 1167 | pgofs++; |
| 1168 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1169 | /* preallocate blocks in batch for one dnode page */ |
| 1170 | if (flag == F2FS_GET_BLOCK_PRE_AIO && |
| 1171 | (pgofs == end || dn.ofs_in_node == end_offset)) { |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1172 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1173 | dn.ofs_in_node = ofs_in_node; |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1174 | err = f2fs_reserve_new_blocks(&dn, prealloc); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1175 | if (err) |
| 1176 | goto sync_out; |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1177 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1178 | map->m_len += dn.ofs_in_node - ofs_in_node; |
| 1179 | if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) { |
| 1180 | err = -ENOSPC; |
| 1181 | goto sync_out; |
Chao Yu | 4fe71e8 | 2016-01-26 15:37:38 +0800 | [diff] [blame] | 1182 | } |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1183 | dn.ofs_in_node = end_offset; |
Fan Li | 7df3a43 | 2015-12-17 13:20:59 +0800 | [diff] [blame] | 1184 | } |
| 1185 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1186 | if (pgofs >= end) |
| 1187 | goto sync_out; |
| 1188 | else if (dn.ofs_in_node < end_offset) |
| 1189 | goto next_block; |
| 1190 | |
Chao Yu | 6bce963 | 2018-01-11 14:42:30 +0800 | [diff] [blame] | 1191 | if (flag == F2FS_GET_BLOCK_PRECACHE) { |
| 1192 | if (map->m_flags & F2FS_MAP_MAPPED) { |
| 1193 | unsigned int ofs = start_pgofs - map->m_lblk; |
| 1194 | |
| 1195 | f2fs_update_extent_cache_range(&dn, |
| 1196 | start_pgofs, map->m_pblk + ofs, |
| 1197 | map->m_len - ofs); |
| 1198 | } |
| 1199 | } |
| 1200 | |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1201 | f2fs_put_dnode(&dn); |
| 1202 | |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 1203 | if (map->m_may_create) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1204 | __do_map_lock(sbi, flag, false); |
| 1205 | f2fs_balance_fs(sbi, dn.node_changed); |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1206 | } |
Chao Yu | 46008c6 | 2016-05-09 19:56:30 +0800 | [diff] [blame] | 1207 | goto next_dnode; |
| 1208 | |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1209 | sync_out: |
Sahitya Tummala | 3d57147 | 2018-10-10 10:56:22 +0530 | [diff] [blame] | 1210 | |
| 1211 | /* for hardware encryption, but to avoid potential issue in future */ |
| 1212 | if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) |
| 1213 | f2fs_wait_on_block_writeback_range(inode, |
| 1214 | map->m_pblk, map->m_len); |
| 1215 | |
Chao Yu | 6bce963 | 2018-01-11 14:42:30 +0800 | [diff] [blame] | 1216 | if (flag == F2FS_GET_BLOCK_PRECACHE) { |
| 1217 | if (map->m_flags & F2FS_MAP_MAPPED) { |
| 1218 | unsigned int ofs = start_pgofs - map->m_lblk; |
| 1219 | |
| 1220 | f2fs_update_extent_cache_range(&dn, |
| 1221 | start_pgofs, map->m_pblk + ofs, |
| 1222 | map->m_len - ofs); |
| 1223 | } |
| 1224 | if (map->m_next_extent) |
| 1225 | *map->m_next_extent = pgofs + 1; |
| 1226 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1227 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1228 | unlock_out: |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 1229 | if (map->m_may_create) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1230 | __do_map_lock(sbi, flag, false); |
| 1231 | f2fs_balance_fs(sbi, dn.node_changed); |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 1232 | } |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1233 | out: |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1234 | trace_f2fs_map_blocks(inode, map, err); |
Jaegeuk Kim | bfad7c2 | 2013-12-16 19:04:05 +0900 | [diff] [blame] | 1235 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1236 | } |
| 1237 | |
Hyunchul Lee | 355d234 | 2018-03-08 19:34:38 +0900 | [diff] [blame] | 1238 | bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len) |
| 1239 | { |
| 1240 | struct f2fs_map_blocks map; |
| 1241 | block_t last_lblk; |
| 1242 | int err; |
| 1243 | |
| 1244 | if (pos + len > i_size_read(inode)) |
| 1245 | return false; |
| 1246 | |
| 1247 | map.m_lblk = F2FS_BYTES_TO_BLK(pos); |
| 1248 | map.m_next_pgofs = NULL; |
| 1249 | map.m_next_extent = NULL; |
| 1250 | map.m_seg_type = NO_CHECK_TYPE; |
Jia Zhu | e3faef1 | 2018-11-20 04:29:35 +0800 | [diff] [blame] | 1251 | map.m_may_create = false; |
Hyunchul Lee | 355d234 | 2018-03-08 19:34:38 +0900 | [diff] [blame] | 1252 | last_lblk = F2FS_BLK_ALIGN(pos + len); |
| 1253 | |
| 1254 | while (map.m_lblk < last_lblk) { |
| 1255 | map.m_len = last_lblk - map.m_lblk; |
| 1256 | err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT); |
| 1257 | if (err || map.m_len == 0) |
| 1258 | return false; |
| 1259 | map.m_lblk += map.m_len; |
| 1260 | } |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1264 | static int __get_data_block(struct inode *inode, sector_t iblock, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1265 | struct buffer_head *bh, int create, int flag, |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 1266 | pgoff_t *next_pgofs, int seg_type, bool may_write) |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1267 | { |
| 1268 | struct f2fs_map_blocks map; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1269 | int err; |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1270 | |
| 1271 | map.m_lblk = iblock; |
| 1272 | map.m_len = bh->b_size >> inode->i_blkbits; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1273 | map.m_next_pgofs = next_pgofs; |
Chao Yu | 6bce963 | 2018-01-11 14:42:30 +0800 | [diff] [blame] | 1274 | map.m_next_extent = NULL; |
Hyunchul Lee | 1a9d6a9 | 2017-11-28 09:23:00 +0900 | [diff] [blame] | 1275 | map.m_seg_type = seg_type; |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 1276 | map.m_may_create = may_write; |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1277 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1278 | err = f2fs_map_blocks(inode, &map, create, flag); |
| 1279 | if (!err) { |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1280 | map_bh(bh, inode->i_sb, map.m_pblk); |
| 1281 | bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1282 | bh->b_size = (u64)map.m_len << inode->i_blkbits; |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1283 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1284 | return err; |
Jaegeuk Kim | 003a3e1 | 2015-04-06 19:55:34 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1287 | static int get_data_block(struct inode *inode, sector_t iblock, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1288 | struct buffer_head *bh_result, int create, int flag, |
| 1289 | pgoff_t *next_pgofs) |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1290 | { |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1291 | return __get_data_block(inode, iblock, bh_result, create, |
Hyunchul Lee | 1a9d6a9 | 2017-11-28 09:23:00 +0900 | [diff] [blame] | 1292 | flag, next_pgofs, |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 1293 | NO_CHECK_TYPE, create); |
| 1294 | } |
| 1295 | |
| 1296 | static int get_data_block_dio_write(struct inode *inode, sector_t iblock, |
| 1297 | struct buffer_head *bh_result, int create) |
| 1298 | { |
| 1299 | return __get_data_block(inode, iblock, bh_result, create, |
| 1300 | F2FS_GET_BLOCK_DIO, NULL, |
| 1301 | f2fs_rw_hint_to_seg_type(inode->i_write_hint), |
| 1302 | true); |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1303 | } |
| 1304 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1305 | static int get_data_block_dio(struct inode *inode, sector_t iblock, |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1306 | struct buffer_head *bh_result, int create) |
| 1307 | { |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1308 | return __get_data_block(inode, iblock, bh_result, create, |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 1309 | F2FS_GET_BLOCK_DIO, NULL, |
| 1310 | f2fs_rw_hint_to_seg_type(inode->i_write_hint), |
| 1311 | false); |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | static int get_data_block_bmap(struct inode *inode, sector_t iblock, |
| 1315 | struct buffer_head *bh_result, int create) |
| 1316 | { |
Yunlei He | 179448b | 2015-12-28 21:48:32 +0800 | [diff] [blame] | 1317 | /* Block number less than F2FS MAX BLOCKS */ |
Chao Yu | e0afc4d | 2015-12-31 14:35:37 +0800 | [diff] [blame] | 1318 | if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks)) |
Yunlei He | 179448b | 2015-12-28 21:48:32 +0800 | [diff] [blame] | 1319 | return -EFBIG; |
| 1320 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1321 | return __get_data_block(inode, iblock, bh_result, create, |
Hyunchul Lee | 1a9d6a9 | 2017-11-28 09:23:00 +0900 | [diff] [blame] | 1322 | F2FS_GET_BLOCK_BMAP, NULL, |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 1323 | NO_CHECK_TYPE, create); |
Jaegeuk Kim | ccfb300 | 2014-06-13 13:02:11 +0900 | [diff] [blame] | 1324 | } |
| 1325 | |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1326 | static inline sector_t logical_to_blk(struct inode *inode, loff_t offset) |
| 1327 | { |
| 1328 | return (offset >> inode->i_blkbits); |
| 1329 | } |
| 1330 | |
| 1331 | static inline loff_t blk_to_logical(struct inode *inode, sector_t blk) |
| 1332 | { |
| 1333 | return (blk << inode->i_blkbits); |
| 1334 | } |
| 1335 | |
Chao Yu | 08ddb19 | 2018-01-11 14:39:57 +0800 | [diff] [blame] | 1336 | static int f2fs_xattr_fiemap(struct inode *inode, |
| 1337 | struct fiemap_extent_info *fieinfo) |
| 1338 | { |
| 1339 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1340 | struct page *page; |
| 1341 | struct node_info ni; |
| 1342 | __u64 phys = 0, len; |
| 1343 | __u32 flags; |
| 1344 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
| 1345 | int err = 0; |
| 1346 | |
| 1347 | if (f2fs_has_inline_xattr(inode)) { |
| 1348 | int offset; |
| 1349 | |
| 1350 | page = f2fs_grab_cache_page(NODE_MAPPING(sbi), |
| 1351 | inode->i_ino, false); |
| 1352 | if (!page) |
| 1353 | return -ENOMEM; |
| 1354 | |
Chao Yu | 3c74326 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1355 | err = f2fs_get_node_info(sbi, inode->i_ino, &ni); |
| 1356 | if (err) { |
| 1357 | f2fs_put_page(page, 1); |
| 1358 | return err; |
| 1359 | } |
Chao Yu | 08ddb19 | 2018-01-11 14:39:57 +0800 | [diff] [blame] | 1360 | |
| 1361 | phys = (__u64)blk_to_logical(inode, ni.blk_addr); |
| 1362 | offset = offsetof(struct f2fs_inode, i_addr) + |
| 1363 | sizeof(__le32) * (DEF_ADDRS_PER_INODE - |
Chao Yu | 3aa46e2 | 2018-01-17 16:31:36 +0800 | [diff] [blame] | 1364 | get_inline_xattr_addrs(inode)); |
Chao Yu | 08ddb19 | 2018-01-11 14:39:57 +0800 | [diff] [blame] | 1365 | |
| 1366 | phys += offset; |
| 1367 | len = inline_xattr_size(inode); |
| 1368 | |
| 1369 | f2fs_put_page(page, 1); |
| 1370 | |
| 1371 | flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED; |
| 1372 | |
| 1373 | if (!xnid) |
| 1374 | flags |= FIEMAP_EXTENT_LAST; |
| 1375 | |
| 1376 | err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); |
| 1377 | if (err || err == 1) |
| 1378 | return err; |
| 1379 | } |
| 1380 | |
| 1381 | if (xnid) { |
| 1382 | page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false); |
| 1383 | if (!page) |
| 1384 | return -ENOMEM; |
| 1385 | |
Chao Yu | 3c74326 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1386 | err = f2fs_get_node_info(sbi, xnid, &ni); |
| 1387 | if (err) { |
| 1388 | f2fs_put_page(page, 1); |
| 1389 | return err; |
| 1390 | } |
Chao Yu | 08ddb19 | 2018-01-11 14:39:57 +0800 | [diff] [blame] | 1391 | |
| 1392 | phys = (__u64)blk_to_logical(inode, ni.blk_addr); |
| 1393 | len = inode->i_sb->s_blocksize; |
| 1394 | |
| 1395 | f2fs_put_page(page, 1); |
| 1396 | |
| 1397 | flags = FIEMAP_EXTENT_LAST; |
| 1398 | } |
| 1399 | |
| 1400 | if (phys) |
| 1401 | err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); |
| 1402 | |
| 1403 | return (err < 0 ? err : 0); |
| 1404 | } |
| 1405 | |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 1406 | int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 1407 | u64 start, u64 len) |
| 1408 | { |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1409 | struct buffer_head map_bh; |
| 1410 | sector_t start_blk, last_blk; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1411 | pgoff_t next_pgofs; |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1412 | u64 logical = 0, phys = 0, size = 0; |
| 1413 | u32 flags = 0; |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1414 | int ret = 0; |
| 1415 | |
Chao Yu | 6bce963 | 2018-01-11 14:42:30 +0800 | [diff] [blame] | 1416 | if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { |
| 1417 | ret = f2fs_precache_extents(inode); |
| 1418 | if (ret) |
| 1419 | return ret; |
| 1420 | } |
| 1421 | |
Chao Yu | 08ddb19 | 2018-01-11 14:39:57 +0800 | [diff] [blame] | 1422 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR); |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1423 | if (ret) |
| 1424 | return ret; |
| 1425 | |
Chao Yu | aa9c1c1 | 2018-01-11 14:37:35 +0800 | [diff] [blame] | 1426 | inode_lock(inode); |
| 1427 | |
Chao Yu | 08ddb19 | 2018-01-11 14:39:57 +0800 | [diff] [blame] | 1428 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
| 1429 | ret = f2fs_xattr_fiemap(inode, fieinfo); |
| 1430 | goto out; |
| 1431 | } |
| 1432 | |
Jaegeuk Kim | 67f8cf3c | 2015-10-15 11:34:49 -0700 | [diff] [blame] | 1433 | if (f2fs_has_inline_data(inode)) { |
| 1434 | ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len); |
| 1435 | if (ret != -EAGAIN) |
Chao Yu | aa9c1c1 | 2018-01-11 14:37:35 +0800 | [diff] [blame] | 1436 | goto out; |
Jaegeuk Kim | 67f8cf3c | 2015-10-15 11:34:49 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1439 | if (logical_to_blk(inode, len) == 0) |
| 1440 | len = blk_to_logical(inode, 1); |
| 1441 | |
| 1442 | start_blk = logical_to_blk(inode, start); |
| 1443 | last_blk = logical_to_blk(inode, start + len - 1); |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1444 | |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1445 | next: |
| 1446 | memset(&map_bh, 0, sizeof(struct buffer_head)); |
| 1447 | map_bh.b_size = len; |
| 1448 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 1449 | ret = get_data_block(inode, start_blk, &map_bh, 0, |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1450 | F2FS_GET_BLOCK_FIEMAP, &next_pgofs); |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1451 | if (ret) |
| 1452 | goto out; |
| 1453 | |
| 1454 | /* HOLE */ |
| 1455 | if (!buffer_mapped(&map_bh)) { |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1456 | start_blk = next_pgofs; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1457 | |
| 1458 | if (blk_to_logical(inode, start_blk) < blk_to_logical(inode, |
| 1459 | F2FS_I_SB(inode)->max_file_blocks)) |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1460 | goto prep_next; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1461 | |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1462 | flags |= FIEMAP_EXTENT_LAST; |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1463 | } |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1464 | |
Chao Yu | da5af12 | 2016-01-08 20:19:27 +0800 | [diff] [blame] | 1465 | if (size) { |
| 1466 | if (f2fs_encrypted_inode(inode)) |
| 1467 | flags |= FIEMAP_EXTENT_DATA_ENCRYPTED; |
| 1468 | |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1469 | ret = fiemap_fill_next_extent(fieinfo, logical, |
| 1470 | phys, size, flags); |
Chao Yu | da5af12 | 2016-01-08 20:19:27 +0800 | [diff] [blame] | 1471 | } |
Fan Li | 9a950d5 | 2015-12-26 18:07:41 +0800 | [diff] [blame] | 1472 | |
| 1473 | if (start_blk > last_blk || ret) |
| 1474 | goto out; |
| 1475 | |
| 1476 | logical = blk_to_logical(inode, start_blk); |
| 1477 | phys = blk_to_logical(inode, map_bh.b_blocknr); |
| 1478 | size = map_bh.b_size; |
| 1479 | flags = 0; |
| 1480 | if (buffer_unwritten(&map_bh)) |
| 1481 | flags = FIEMAP_EXTENT_UNWRITTEN; |
| 1482 | |
| 1483 | start_blk += logical_to_blk(inode, size); |
| 1484 | |
| 1485 | prep_next: |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1486 | cond_resched(); |
| 1487 | if (fatal_signal_pending(current)) |
| 1488 | ret = -EINTR; |
| 1489 | else |
| 1490 | goto next; |
| 1491 | out: |
| 1492 | if (ret == 1) |
| 1493 | ret = 0; |
| 1494 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 1495 | inode_unlock(inode); |
Jaegeuk Kim | 7f63eb7 | 2015-05-08 19:30:32 -0700 | [diff] [blame] | 1496 | return ret; |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 1497 | } |
| 1498 | |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1499 | /* |
| 1500 | * This function was originally taken from fs/mpage.c, and customized for f2fs. |
| 1501 | * Major change was from block_size == page_size in f2fs by default. |
Jaegeuk Kim | f9d75d6 | 2018-06-21 11:29:43 -0700 | [diff] [blame] | 1502 | * |
| 1503 | * Note that the aops->readpages() function is ONLY used for read-ahead. If |
| 1504 | * this function ever deviates from doing just read-ahead, it should either |
| 1505 | * use ->readpage() or do the necessary surgery to decouple ->readpages() |
| 1506 | * from read-ahead. |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1507 | */ |
| 1508 | static int f2fs_mpage_readpages(struct address_space *mapping, |
| 1509 | struct list_head *pages, struct page *page, |
Jaegeuk Kim | f9d75d6 | 2018-06-21 11:29:43 -0700 | [diff] [blame] | 1510 | unsigned nr_pages, bool is_readahead) |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1511 | { |
| 1512 | struct bio *bio = NULL; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1513 | sector_t last_block_in_bio = 0; |
| 1514 | struct inode *inode = mapping->host; |
| 1515 | const unsigned blkbits = inode->i_blkbits; |
| 1516 | const unsigned blocksize = 1 << blkbits; |
| 1517 | sector_t block_in_file; |
| 1518 | sector_t last_block; |
| 1519 | sector_t last_block_in_file; |
| 1520 | sector_t block_nr; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1521 | struct f2fs_map_blocks map; |
| 1522 | |
| 1523 | map.m_pblk = 0; |
| 1524 | map.m_lblk = 0; |
| 1525 | map.m_len = 0; |
| 1526 | map.m_flags = 0; |
Chao Yu | da85985 | 2016-01-26 15:42:58 +0800 | [diff] [blame] | 1527 | map.m_next_pgofs = NULL; |
Chao Yu | 6bce963 | 2018-01-11 14:42:30 +0800 | [diff] [blame] | 1528 | map.m_next_extent = NULL; |
Hyunchul Lee | 1a9d6a9 | 2017-11-28 09:23:00 +0900 | [diff] [blame] | 1529 | map.m_seg_type = NO_CHECK_TYPE; |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 1530 | map.m_may_create = false; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1531 | |
LiFan | ba4556c | 2017-11-25 11:46:18 +0800 | [diff] [blame] | 1532 | for (; nr_pages; nr_pages--) { |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1533 | if (pages) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1534 | page = list_last_entry(pages, struct page, lru); |
| 1535 | |
| 1536 | prefetchw(&page->flags); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1537 | list_del(&page->lru); |
| 1538 | if (add_to_page_cache_lru(page, mapping, |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 1539 | page->index, |
| 1540 | readahead_gfp_mask(mapping))) |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1541 | goto next_page; |
| 1542 | } |
| 1543 | |
| 1544 | block_in_file = (sector_t)page->index; |
| 1545 | last_block = block_in_file + nr_pages; |
| 1546 | last_block_in_file = (i_size_read(inode) + blocksize - 1) >> |
| 1547 | blkbits; |
| 1548 | if (last_block > last_block_in_file) |
| 1549 | last_block = last_block_in_file; |
| 1550 | |
Chao Yu | b50924a | 2019-03-07 17:31:30 +0800 | [diff] [blame] | 1551 | /* just zeroing out page which is beyond EOF */ |
| 1552 | if (block_in_file >= last_block) |
| 1553 | goto zero_out; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1554 | /* |
| 1555 | * Map blocks using the previous result first. |
| 1556 | */ |
| 1557 | if ((map.m_flags & F2FS_MAP_MAPPED) && |
| 1558 | block_in_file > map.m_lblk && |
| 1559 | block_in_file < (map.m_lblk + map.m_len)) |
| 1560 | goto got_it; |
| 1561 | |
| 1562 | /* |
| 1563 | * Then do more f2fs_map_blocks() calls until we are |
| 1564 | * done with this page. |
| 1565 | */ |
Chao Yu | b50924a | 2019-03-07 17:31:30 +0800 | [diff] [blame] | 1566 | map.m_lblk = block_in_file; |
| 1567 | map.m_len = last_block - block_in_file; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1568 | |
Chao Yu | b50924a | 2019-03-07 17:31:30 +0800 | [diff] [blame] | 1569 | if (f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT)) |
| 1570 | goto set_error_page; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1571 | got_it: |
| 1572 | if ((map.m_flags & F2FS_MAP_MAPPED)) { |
| 1573 | block_nr = map.m_pblk + block_in_file - map.m_lblk; |
| 1574 | SetPageMappedToDisk(page); |
| 1575 | |
| 1576 | if (!PageUptodate(page) && !cleancache_get_page(page)) { |
| 1577 | SetPageUptodate(page); |
| 1578 | goto confused; |
| 1579 | } |
Chao Yu | b3c869a | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 1580 | |
| 1581 | if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr, |
| 1582 | DATA_GENERIC)) |
| 1583 | goto set_error_page; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1584 | } else { |
Chao Yu | b50924a | 2019-03-07 17:31:30 +0800 | [diff] [blame] | 1585 | zero_out: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1586 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 1587 | if (!PageUptodate(page)) |
| 1588 | SetPageUptodate(page); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1589 | unlock_page(page); |
| 1590 | goto next_page; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | * This page will go to BIO. Do we need to send this |
| 1595 | * BIO off first? |
| 1596 | */ |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1597 | if (bio && (last_block_in_bio != block_nr - 1 || |
| 1598 | !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) { |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1599 | submit_and_realloc: |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 1600 | __submit_bio(F2FS_I_SB(inode), bio, DATA); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1601 | bio = NULL; |
| 1602 | } |
| 1603 | if (bio == NULL) { |
Jaegeuk Kim | f9d75d6 | 2018-06-21 11:29:43 -0700 | [diff] [blame] | 1604 | bio = f2fs_grab_read_bio(inode, block_nr, nr_pages, |
| 1605 | is_readahead ? REQ_RAHEAD : 0); |
Jaegeuk Kim | 1d353eb | 2016-07-12 09:38:48 -0700 | [diff] [blame] | 1606 | if (IS_ERR(bio)) { |
| 1607 | bio = NULL; |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1608 | goto set_error_page; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1609 | } |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
Jaegeuk Kim | 0d51cea | 2018-08-22 21:18:00 -0700 | [diff] [blame] | 1612 | /* |
| 1613 | * If the page is under writeback, we need to wait for |
| 1614 | * its completion to see the correct decrypted data. |
| 1615 | */ |
| 1616 | f2fs_wait_on_block_writeback(inode, block_nr); |
| 1617 | |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1618 | if (bio_add_page(bio, page, blocksize, 0) < blocksize) |
| 1619 | goto submit_and_realloc; |
| 1620 | |
Jaegeuk Kim | 8025d72 | 2018-10-16 10:20:53 -0700 | [diff] [blame] | 1621 | inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA); |
Jaegeuk Kim | 4edba23 | 2018-09-25 13:54:33 -0700 | [diff] [blame] | 1622 | ClearPageError(page); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1623 | last_block_in_bio = block_nr; |
| 1624 | goto next_page; |
| 1625 | set_error_page: |
| 1626 | SetPageError(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1627 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1628 | unlock_page(page); |
| 1629 | goto next_page; |
| 1630 | confused: |
| 1631 | if (bio) { |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 1632 | __submit_bio(F2FS_I_SB(inode), bio, DATA); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1633 | bio = NULL; |
| 1634 | } |
| 1635 | unlock_page(page); |
| 1636 | next_page: |
| 1637 | if (pages) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1638 | put_page(page); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1639 | } |
| 1640 | BUG_ON(pages && !list_empty(pages)); |
| 1641 | if (bio) |
Linus Torvalds | 4fc29c1 | 2016-07-27 10:36:31 -0700 | [diff] [blame] | 1642 | __submit_bio(F2FS_I_SB(inode), bio, DATA); |
Jaegeuk Kim | f1e8866 | 2015-04-09 11:20:42 -0700 | [diff] [blame] | 1643 | return 0; |
| 1644 | } |
| 1645 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1646 | static int f2fs_read_data_page(struct file *file, struct page *page) |
| 1647 | { |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1648 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1649 | int ret = -EAGAIN; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1650 | |
Chao Yu | c20e89c | 2014-05-06 16:53:08 +0800 | [diff] [blame] | 1651 | trace_f2fs_readpage(page, DATA); |
| 1652 | |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 1653 | /* If the file has inline data, try to read it directly */ |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1654 | if (f2fs_has_inline_data(inode)) |
| 1655 | ret = f2fs_read_inline_data(inode, page); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1656 | if (ret == -EAGAIN) |
Jaegeuk Kim | f9d75d6 | 2018-06-21 11:29:43 -0700 | [diff] [blame] | 1657 | ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1, false); |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1658 | return ret; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | static int f2fs_read_data_pages(struct file *file, |
| 1662 | struct address_space *mapping, |
| 1663 | struct list_head *pages, unsigned nr_pages) |
| 1664 | { |
Hsiang Kao | 3f01af7 | 2017-09-24 02:45:42 +0800 | [diff] [blame] | 1665 | struct inode *inode = mapping->host; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1666 | struct page *page = list_last_entry(pages, struct page, lru); |
Chao Yu | b8c2940 | 2015-10-12 17:02:26 +0800 | [diff] [blame] | 1667 | |
| 1668 | trace_f2fs_readpages(inode, page, nr_pages); |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1669 | |
| 1670 | /* If the file has inline data, skip readpages */ |
| 1671 | if (f2fs_has_inline_data(inode)) |
| 1672 | return 0; |
| 1673 | |
Jaegeuk Kim | f9d75d6 | 2018-06-21 11:29:43 -0700 | [diff] [blame] | 1674 | return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1675 | } |
| 1676 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1677 | static int encrypt_one_page(struct f2fs_io_info *fio) |
| 1678 | { |
| 1679 | struct inode *inode = fio->page->mapping->host; |
Chao Yu | 08386a6 | 2018-08-14 22:37:25 +0800 | [diff] [blame] | 1680 | struct page *mpage; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1681 | gfp_t gfp_flags = GFP_NOFS; |
| 1682 | |
Jaegeuk Kim | a4fb189 | 2017-09-05 16:54:24 -0700 | [diff] [blame] | 1683 | if (!f2fs_encrypted_file(inode)) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1684 | return 0; |
| 1685 | |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 1686 | /* wait for GCed page writeback via META_MAPPING */ |
Jaegeuk Kim | 0d51cea | 2018-08-22 21:18:00 -0700 | [diff] [blame] | 1687 | f2fs_wait_on_block_writeback(inode, fio->old_blkaddr); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1688 | |
| 1689 | retry_encrypt: |
| 1690 | fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page, |
| 1691 | PAGE_SIZE, 0, fio->page->index, gfp_flags); |
Chao Yu | 08386a6 | 2018-08-14 22:37:25 +0800 | [diff] [blame] | 1692 | if (IS_ERR(fio->encrypted_page)) { |
| 1693 | /* flush pending IOs and wait for a while in the ENOMEM case */ |
| 1694 | if (PTR_ERR(fio->encrypted_page) == -ENOMEM) { |
| 1695 | f2fs_flush_merged_writes(fio->sbi); |
| 1696 | congestion_wait(BLK_RW_ASYNC, HZ/50); |
| 1697 | gfp_flags |= __GFP_NOFAIL; |
| 1698 | goto retry_encrypt; |
| 1699 | } |
| 1700 | return PTR_ERR(fio->encrypted_page); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1701 | } |
Chao Yu | 08386a6 | 2018-08-14 22:37:25 +0800 | [diff] [blame] | 1702 | |
| 1703 | mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr); |
| 1704 | if (mpage) { |
| 1705 | if (PageUptodate(mpage)) |
| 1706 | memcpy(page_address(mpage), |
| 1707 | page_address(fio->encrypted_page), PAGE_SIZE); |
| 1708 | f2fs_put_page(mpage, 1); |
| 1709 | } |
| 1710 | return 0; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
Chao Yu | 66356ee | 2018-01-17 16:31:38 +0800 | [diff] [blame] | 1713 | static inline bool check_inplace_update_policy(struct inode *inode, |
| 1714 | struct f2fs_io_info *fio) |
| 1715 | { |
| 1716 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1717 | unsigned int policy = SM_I(sbi)->ipu_policy; |
| 1718 | |
| 1719 | if (policy & (0x1 << F2FS_IPU_FORCE)) |
| 1720 | return true; |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1721 | if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi)) |
Chao Yu | 66356ee | 2018-01-17 16:31:38 +0800 | [diff] [blame] | 1722 | return true; |
| 1723 | if (policy & (0x1 << F2FS_IPU_UTIL) && |
| 1724 | utilization(sbi) > SM_I(sbi)->min_ipu_util) |
| 1725 | return true; |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1726 | if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) && |
Chao Yu | 66356ee | 2018-01-17 16:31:38 +0800 | [diff] [blame] | 1727 | utilization(sbi) > SM_I(sbi)->min_ipu_util) |
| 1728 | return true; |
| 1729 | |
| 1730 | /* |
| 1731 | * IPU for rewrite async pages |
| 1732 | */ |
| 1733 | if (policy & (0x1 << F2FS_IPU_ASYNC) && |
| 1734 | fio && fio->op == REQ_OP_WRITE && |
| 1735 | !(fio->op_flags & REQ_SYNC) && |
| 1736 | !f2fs_encrypted_inode(inode)) |
| 1737 | return true; |
| 1738 | |
| 1739 | /* this is only set during fdatasync */ |
| 1740 | if (policy & (0x1 << F2FS_IPU_FSYNC) && |
| 1741 | is_inode_flag_set(inode, FI_NEED_IPU)) |
| 1742 | return true; |
| 1743 | |
Daniel Rosenberg | f22f93a | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 1744 | if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) && |
| 1745 | !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) |
| 1746 | return true; |
| 1747 | |
Chao Yu | 66356ee | 2018-01-17 16:31:38 +0800 | [diff] [blame] | 1748 | return false; |
| 1749 | } |
| 1750 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1751 | bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio) |
Chao Yu | 66356ee | 2018-01-17 16:31:38 +0800 | [diff] [blame] | 1752 | { |
| 1753 | if (f2fs_is_pinned_file(inode)) |
| 1754 | return true; |
| 1755 | |
| 1756 | /* if this is cold file, we should overwrite to avoid fragmentation */ |
| 1757 | if (file_is_cold(inode)) |
| 1758 | return true; |
| 1759 | |
| 1760 | return check_inplace_update_policy(inode, fio); |
| 1761 | } |
| 1762 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1763 | bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio) |
Chao Yu | 66356ee | 2018-01-17 16:31:38 +0800 | [diff] [blame] | 1764 | { |
| 1765 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1766 | |
| 1767 | if (test_opt(sbi, LFS)) |
| 1768 | return true; |
| 1769 | if (S_ISDIR(inode->i_mode)) |
| 1770 | return true; |
Chao Yu | d831272 | 2018-09-20 20:05:00 +0800 | [diff] [blame] | 1771 | if (IS_NOQUOTA(inode)) |
| 1772 | return true; |
Chao Yu | 66356ee | 2018-01-17 16:31:38 +0800 | [diff] [blame] | 1773 | if (f2fs_is_atomic_file(inode)) |
| 1774 | return true; |
| 1775 | if (fio) { |
| 1776 | if (is_cold_data(fio->page)) |
| 1777 | return true; |
| 1778 | if (IS_ATOMIC_WRITTEN_PAGE(fio->page)) |
| 1779 | return true; |
Daniel Rosenberg | f22f93a | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 1780 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) && |
| 1781 | f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) |
| 1782 | return true; |
Chao Yu | 66356ee | 2018-01-17 16:31:38 +0800 | [diff] [blame] | 1783 | } |
| 1784 | return false; |
| 1785 | } |
| 1786 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1787 | static inline bool need_inplace_update(struct f2fs_io_info *fio) |
| 1788 | { |
| 1789 | struct inode *inode = fio->page->mapping->host; |
| 1790 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1791 | if (f2fs_should_update_outplace(inode, fio)) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1792 | return false; |
| 1793 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1794 | return f2fs_should_update_inplace(inode, fio); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1795 | } |
| 1796 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1797 | int f2fs_do_write_data_page(struct f2fs_io_info *fio) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1798 | { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1799 | struct page *page = fio->page; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1800 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1801 | struct dnode_of_data dn; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1802 | struct extent_info ei = {0,0,0}; |
Chao Yu | 3c74326 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1803 | struct node_info ni; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1804 | bool ipu_force = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1805 | int err = 0; |
| 1806 | |
| 1807 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1808 | if (need_inplace_update(fio) && |
| 1809 | f2fs_lookup_extent_cache(inode, page->index, &ei)) { |
| 1810 | fio->old_blkaddr = ei.blk + page->index - ei.fofs; |
| 1811 | |
Chao Yu | b3c869a | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 1812 | if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, |
| 1813 | DATA_GENERIC)) |
| 1814 | return -EFAULT; |
| 1815 | |
| 1816 | ipu_force = true; |
| 1817 | fio->need_lock = LOCK_DONE; |
| 1818 | goto got_it; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Jaegeuk Kim | 0f4dc16 | 2017-06-21 17:52:39 -0700 | [diff] [blame] | 1821 | /* Deadlock due to between page->lock and f2fs_lock_op */ |
| 1822 | if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi)) |
| 1823 | return -EAGAIN; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1824 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1825 | err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1826 | if (err) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1827 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1828 | |
Chao Yu | 28bc106 | 2016-02-06 14:40:34 +0800 | [diff] [blame] | 1829 | fio->old_blkaddr = dn.data_blkaddr; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1830 | |
| 1831 | /* This page is already truncated */ |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 1832 | if (fio->old_blkaddr == NULL_ADDR) { |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1833 | ClearPageUptodate(page); |
Chao Yu | 408bca0 | 2018-07-27 18:15:16 +0800 | [diff] [blame] | 1834 | clear_cold_data(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1835 | goto out_writepage; |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 1836 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1837 | got_it: |
Chao Yu | b3c869a | 2018-08-01 19:13:44 +0800 | [diff] [blame] | 1838 | if (__is_valid_data_blkaddr(fio->old_blkaddr) && |
| 1839 | !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, |
| 1840 | DATA_GENERIC)) { |
| 1841 | err = -EFAULT; |
| 1842 | goto out_writepage; |
| 1843 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1844 | /* |
| 1845 | * If current allocation needs SSR, |
| 1846 | * it had better in-place writes for updated data. |
| 1847 | */ |
Chao Yu | d05e871 | 2018-06-05 17:44:11 +0800 | [diff] [blame] | 1848 | if (ipu_force || (is_valid_data_blkaddr(fio->sbi, fio->old_blkaddr) && |
Chao Yu | 158d9bb | 2018-05-23 22:25:08 +0800 | [diff] [blame] | 1849 | need_inplace_update(fio))) { |
Jaegeuk Kim | 1b48ed6 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1850 | err = encrypt_one_page(fio); |
| 1851 | if (err) |
| 1852 | goto out_writepage; |
| 1853 | |
| 1854 | set_page_writeback(page); |
Jaegeuk Kim | 926b370 | 2018-04-11 23:09:04 -0700 | [diff] [blame] | 1855 | ClearPageError(page); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1856 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | 1b48ed6 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1857 | if (fio->need_lock == LOCK_REQ) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1858 | f2fs_unlock_op(fio->sbi); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1859 | err = f2fs_inplace_write_data(fio); |
Chao Yu | b4b3453 | 2019-02-21 20:37:14 +0800 | [diff] [blame] | 1860 | if (err) { |
| 1861 | if (f2fs_encrypted_file(inode)) |
| 1862 | fscrypt_pullback_bio_page(&fio->encrypted_page, |
| 1863 | true); |
| 1864 | if (PageWriteback(page)) |
| 1865 | end_page_writeback(page); |
| 1866 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1867 | trace_f2fs_do_write_data_page(fio->page, IPU); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 1868 | set_inode_flag(inode, FI_UPDATE_WRITE); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1869 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1870 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1871 | |
Jaegeuk Kim | 1b48ed6 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1872 | if (fio->need_lock == LOCK_RETRY) { |
| 1873 | if (!f2fs_trylock_op(fio->sbi)) { |
| 1874 | err = -EAGAIN; |
| 1875 | goto out_writepage; |
| 1876 | } |
| 1877 | fio->need_lock = LOCK_REQ; |
| 1878 | } |
| 1879 | |
Chao Yu | 3c74326 | 2018-07-17 00:02:17 +0800 | [diff] [blame] | 1880 | err = f2fs_get_node_info(fio->sbi, dn.nid, &ni); |
| 1881 | if (err) |
| 1882 | goto out_writepage; |
| 1883 | |
| 1884 | fio->version = ni.version; |
| 1885 | |
Jaegeuk Kim | 1b48ed6 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1886 | err = encrypt_one_page(fio); |
| 1887 | if (err) |
| 1888 | goto out_writepage; |
| 1889 | |
| 1890 | set_page_writeback(page); |
Jaegeuk Kim | 926b370 | 2018-04-11 23:09:04 -0700 | [diff] [blame] | 1891 | ClearPageError(page); |
Jaegeuk Kim | 1b48ed6 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1892 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1893 | /* LFS mode write path */ |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1894 | f2fs_outplace_write_data(&dn, fio); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1895 | trace_f2fs_do_write_data_page(page, OPU); |
| 1896 | set_inode_flag(inode, FI_APPEND_WRITE); |
| 1897 | if (page->index == 0) |
| 1898 | set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1899 | out_writepage: |
| 1900 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1901 | out: |
Jaegeuk Kim | 1b48ed6 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1902 | if (fio->need_lock == LOCK_REQ) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1903 | f2fs_unlock_op(fio->sbi); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1904 | return err; |
| 1905 | } |
| 1906 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1907 | static int __write_data_page(struct page *page, bool *submitted, |
Chao Yu | c0fe488 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1908 | struct writeback_control *wbc, |
| 1909 | enum iostat_type io_type) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1910 | { |
| 1911 | struct inode *inode = page->mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 1912 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1913 | loff_t i_size = i_size_read(inode); |
| 1914 | const pgoff_t end_index = ((unsigned long long) i_size) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1915 | >> PAGE_SHIFT; |
Jaegeuk Kim | 26de9b1 | 2016-05-20 20:42:37 -0700 | [diff] [blame] | 1916 | loff_t psize = (page->index + 1) << PAGE_SHIFT; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 1917 | unsigned offset = 0; |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1918 | bool need_balance_fs = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1919 | int err = 0; |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1920 | struct f2fs_io_info fio = { |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1921 | .sbi = sbi, |
Chao Yu | f014be8 | 2017-09-29 13:59:38 +0800 | [diff] [blame] | 1922 | .ino = inode->i_ino, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1923 | .type = DATA, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 1924 | .op = REQ_OP_WRITE, |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1925 | .op_flags = wbc_to_write_flags(wbc), |
| 1926 | .old_blkaddr = NULL_ADDR, |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 1927 | .page = page, |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 1928 | .encrypted_page = NULL, |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1929 | .submitted = false, |
Jaegeuk Kim | 1b48ed6 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1930 | .need_lock = LOCK_RETRY, |
Chao Yu | c0fe488 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 1931 | .io_type = io_type, |
Yufen Yu | cbd5e5a | 2018-01-09 19:33:39 +0800 | [diff] [blame] | 1932 | .io_wbc = wbc, |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 1933 | }; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1934 | |
Chao Yu | ecda0de | 2014-05-06 16:48:26 +0800 | [diff] [blame] | 1935 | trace_f2fs_writepage(page, DATA); |
| 1936 | |
Chao Yu | 86f78c1 | 2018-01-18 17:29:10 +0800 | [diff] [blame] | 1937 | /* we should bypass data pages to proceed the kworkder jobs */ |
| 1938 | if (unlikely(f2fs_cp_error(sbi))) { |
| 1939 | mapping_set_error(page->mapping, -EIO); |
Chao Yu | 9780d68 | 2018-05-28 16:59:26 +0800 | [diff] [blame] | 1940 | /* |
| 1941 | * don't drop any dirty dentry pages for keeping lastest |
| 1942 | * directory structure. |
| 1943 | */ |
| 1944 | if (S_ISDIR(inode->i_mode)) |
| 1945 | goto redirty_out; |
Chao Yu | 86f78c1 | 2018-01-18 17:29:10 +0800 | [diff] [blame] | 1946 | goto out; |
| 1947 | } |
| 1948 | |
Chao Yu | 00cc7ed | 2017-06-29 23:20:45 +0800 | [diff] [blame] | 1949 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 1950 | goto redirty_out; |
| 1951 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1952 | if (page->index < end_index) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1953 | goto write; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1954 | |
| 1955 | /* |
| 1956 | * If the offset is out-of-range of file size, |
| 1957 | * this page does not have to be written to disk. |
| 1958 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1959 | offset = i_size & (PAGE_SIZE - 1); |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 1960 | if ((page->index >= end_index + 1) || !offset) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1961 | goto out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1962 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1963 | zero_user_segment(page, offset, PAGE_SIZE); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1964 | write: |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1965 | if (f2fs_is_drop_cache(inode)) |
| 1966 | goto out; |
Jaegeuk Kim | e6e5f56 | 2016-04-14 16:48:52 -0700 | [diff] [blame] | 1967 | /* we should not write 0'th page having journal header */ |
| 1968 | if (f2fs_is_volatile_file(inode) && (!page->index || |
| 1969 | (!wbc->for_reclaim && |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1970 | f2fs_available_free_memory(sbi, BASE_CHECK)))) |
Jaegeuk Kim | 1e84371 | 2014-12-09 06:08:59 -0800 | [diff] [blame] | 1971 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1972 | |
Jaegeuk Kim | b230e6c | 2016-05-29 21:18:23 -0700 | [diff] [blame] | 1973 | /* Dentry blocks are controlled by checkpoint */ |
| 1974 | if (S_ISDIR(inode->i_mode)) { |
Jaegeuk Kim | 1b48ed6 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1975 | fio.need_lock = LOCK_DONE; |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1976 | err = f2fs_do_write_data_page(&fio); |
Jaegeuk Kim | b230e6c | 2016-05-29 21:18:23 -0700 | [diff] [blame] | 1977 | goto done; |
| 1978 | } |
| 1979 | |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 1980 | if (!wbc->for_reclaim) |
| 1981 | need_balance_fs = true; |
Jaegeuk Kim | 7f3037a | 2016-09-01 12:02:51 -0700 | [diff] [blame] | 1982 | else if (has_not_enough_free_secs(sbi, 0, 0)) |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 1983 | goto redirty_out; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1984 | else |
| 1985 | set_inode_flag(inode, FI_HOT_DATA); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 1986 | |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1987 | err = -EAGAIN; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1988 | if (f2fs_has_inline_data(inode)) { |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 1989 | err = f2fs_write_inline_data(inode, page); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 1990 | if (!err) |
| 1991 | goto out; |
| 1992 | } |
| 1993 | |
Jaegeuk Kim | 1b48ed6 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1994 | if (err == -EAGAIN) { |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1995 | err = f2fs_do_write_data_page(&fio); |
Jaegeuk Kim | 1b48ed6 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1996 | if (err == -EAGAIN) { |
| 1997 | fio.need_lock = LOCK_REQ; |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 1998 | err = f2fs_do_write_data_page(&fio); |
Jaegeuk Kim | 1b48ed6 | 2017-05-12 13:51:34 -0700 | [diff] [blame] | 1999 | } |
| 2000 | } |
Chao Yu | 33fdebb | 2017-10-09 17:55:19 +0800 | [diff] [blame] | 2001 | |
Chao Yu | 5912fba | 2018-01-17 16:31:37 +0800 | [diff] [blame] | 2002 | if (err) { |
| 2003 | file_set_keep_isize(inode); |
| 2004 | } else { |
| 2005 | down_write(&F2FS_I(inode)->i_sem); |
| 2006 | if (F2FS_I(inode)->last_disk_size < psize) |
| 2007 | F2FS_I(inode)->last_disk_size = psize; |
| 2008 | up_write(&F2FS_I(inode)->i_sem); |
| 2009 | } |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2010 | |
Jaegeuk Kim | 8618b88 | 2014-02-17 19:29:27 +0900 | [diff] [blame] | 2011 | done: |
| 2012 | if (err && err != -ENOENT) |
| 2013 | goto redirty_out; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2014 | |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 2015 | out: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 2016 | inode_dec_dirty_pages(inode); |
Chao Yu | 408bca0 | 2018-07-27 18:15:16 +0800 | [diff] [blame] | 2017 | if (err) { |
Jaegeuk Kim | 2bca1e2 | 2015-02-25 19:25:01 -0800 | [diff] [blame] | 2018 | ClearPageUptodate(page); |
Chao Yu | 408bca0 | 2018-07-27 18:15:16 +0800 | [diff] [blame] | 2019 | clear_cold_data(page); |
| 2020 | } |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 2021 | |
| 2022 | if (wbc->for_reclaim) { |
Chao Yu | 39373df | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 2023 | f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2024 | clear_inode_flag(inode, FI_HOT_DATA); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2025 | f2fs_remove_dirty_inode(inode); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2026 | submitted = NULL; |
Chao Yu | eb7e813 | 2015-11-10 18:45:07 +0800 | [diff] [blame] | 2027 | } |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 2028 | |
| 2029 | unlock_page(page); |
Chao Yu | d831272 | 2018-09-20 20:05:00 +0800 | [diff] [blame] | 2030 | if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode)) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2031 | f2fs_balance_fs(sbi, need_balance_fs); |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 2032 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2033 | if (unlikely(f2fs_cp_error(sbi))) { |
Jaegeuk Kim | 9bc576a | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 2034 | f2fs_submit_merged_write(sbi, DATA); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2035 | submitted = NULL; |
| 2036 | } |
| 2037 | |
| 2038 | if (submitted) |
| 2039 | *submitted = fio.submitted; |
Chao Yu | 0c3a579 | 2016-01-18 18:28:11 +0800 | [diff] [blame] | 2040 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2041 | return 0; |
| 2042 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2043 | redirty_out: |
Jaegeuk Kim | 76f6026 | 2014-04-15 16:04:15 +0900 | [diff] [blame] | 2044 | redirty_page_for_writepage(wbc, page); |
Jaegeuk Kim | 5b37ebb | 2018-05-03 23:26:02 -0700 | [diff] [blame] | 2045 | /* |
| 2046 | * pageout() in MM traslates EAGAIN, so calls handle_write_error() |
| 2047 | * -> mapping_set_error() -> set_bit(AS_EIO, ...). |
| 2048 | * file_write_and_wait_range() will see EIO error, which is critical |
| 2049 | * to return value of fsync() followed by atomic_write failure to user. |
| 2050 | */ |
| 2051 | if (!err || wbc->for_reclaim) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2052 | return AOP_WRITEPAGE_ACTIVATE; |
Jaegeuk Kim | b230e6c | 2016-05-29 21:18:23 -0700 | [diff] [blame] | 2053 | unlock_page(page); |
| 2054 | return err; |
Namjae Jeon | fa9150a | 2013-01-15 16:45:24 +0900 | [diff] [blame] | 2055 | } |
| 2056 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2057 | static int f2fs_write_data_page(struct page *page, |
| 2058 | struct writeback_control *wbc) |
| 2059 | { |
Chao Yu | c0fe488 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2060 | return __write_data_page(page, NULL, wbc, FS_DATA_IO); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2061 | } |
| 2062 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2063 | /* |
| 2064 | * This function was copied from write_cche_pages from mm/page-writeback.c. |
| 2065 | * The major change is making write step of cold data page separately from |
| 2066 | * warm/hot data page. |
| 2067 | */ |
| 2068 | static int f2fs_write_cache_pages(struct address_space *mapping, |
Chao Yu | c0fe488 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2069 | struct writeback_control *wbc, |
| 2070 | enum iostat_type io_type) |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2071 | { |
| 2072 | int ret = 0; |
| 2073 | int done = 0; |
| 2074 | struct pagevec pvec; |
Chao Yu | deb78d4 | 2018-06-04 23:20:36 +0800 | [diff] [blame] | 2075 | struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2076 | int nr_pages; |
| 2077 | pgoff_t uninitialized_var(writeback_index); |
| 2078 | pgoff_t index; |
| 2079 | pgoff_t end; /* Inclusive */ |
| 2080 | pgoff_t done_index; |
| 2081 | int cycled; |
| 2082 | int range_whole = 0; |
| 2083 | int tag; |
Chao Yu | 39373df | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 2084 | int nwritten = 0; |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2085 | |
| 2086 | pagevec_init(&pvec, 0); |
Jaegeuk Kim | 46ae957 | 2016-05-25 20:57:16 -0700 | [diff] [blame] | 2087 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2088 | if (get_dirty_pages(mapping->host) <= |
| 2089 | SM_I(F2FS_M_SB(mapping))->min_hot_blocks) |
| 2090 | set_inode_flag(mapping->host, FI_HOT_DATA); |
| 2091 | else |
| 2092 | clear_inode_flag(mapping->host, FI_HOT_DATA); |
| 2093 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2094 | if (wbc->range_cyclic) { |
| 2095 | writeback_index = mapping->writeback_index; /* prev offset */ |
| 2096 | index = writeback_index; |
| 2097 | if (index == 0) |
| 2098 | cycled = 1; |
| 2099 | else |
| 2100 | cycled = 0; |
| 2101 | end = -1; |
| 2102 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2103 | index = wbc->range_start >> PAGE_SHIFT; |
| 2104 | end = wbc->range_end >> PAGE_SHIFT; |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2105 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 2106 | range_whole = 1; |
| 2107 | cycled = 1; /* ignore range_cyclic tests */ |
| 2108 | } |
| 2109 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 2110 | tag = PAGECACHE_TAG_TOWRITE; |
| 2111 | else |
| 2112 | tag = PAGECACHE_TAG_DIRTY; |
| 2113 | retry: |
| 2114 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 2115 | tag_pages_for_writeback(mapping, index, end); |
| 2116 | done_index = index; |
| 2117 | while (!done && (index <= end)) { |
| 2118 | int i; |
| 2119 | |
Jan Kara | e067365 | 2017-11-15 17:34:48 -0800 | [diff] [blame] | 2120 | nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end, |
Jan Kara | 4d93a43 | 2017-11-15 17:35:19 -0800 | [diff] [blame] | 2121 | tag); |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2122 | if (nr_pages == 0) |
| 2123 | break; |
| 2124 | |
| 2125 | for (i = 0; i < nr_pages; i++) { |
| 2126 | struct page *page = pvec.pages[i]; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2127 | bool submitted = false; |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2128 | |
Chao Yu | 458e47f | 2018-05-23 22:25:09 +0800 | [diff] [blame] | 2129 | /* give a priority to WB_SYNC threads */ |
Chao Yu | deb78d4 | 2018-06-04 23:20:36 +0800 | [diff] [blame] | 2130 | if (atomic_read(&sbi->wb_sync_req[DATA]) && |
Chao Yu | 458e47f | 2018-05-23 22:25:09 +0800 | [diff] [blame] | 2131 | wbc->sync_mode == WB_SYNC_NONE) { |
| 2132 | done = 1; |
| 2133 | break; |
| 2134 | } |
| 2135 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2136 | done_index = page->index; |
Jaegeuk Kim | 0f4dc16 | 2017-06-21 17:52:39 -0700 | [diff] [blame] | 2137 | retry_write: |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2138 | lock_page(page); |
| 2139 | |
| 2140 | if (unlikely(page->mapping != mapping)) { |
| 2141 | continue_unlock: |
| 2142 | unlock_page(page); |
| 2143 | continue; |
| 2144 | } |
| 2145 | |
| 2146 | if (!PageDirty(page)) { |
| 2147 | /* someone wrote it for us */ |
| 2148 | goto continue_unlock; |
| 2149 | } |
| 2150 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2151 | if (PageWriteback(page)) { |
| 2152 | if (wbc->sync_mode != WB_SYNC_NONE) |
Jaegeuk Kim | fec1d65 | 2016-01-20 23:43:51 +0800 | [diff] [blame] | 2153 | f2fs_wait_on_page_writeback(page, |
Chao Yu | d9b3df5 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 2154 | DATA, true, true); |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2155 | else |
| 2156 | goto continue_unlock; |
| 2157 | } |
| 2158 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2159 | if (!clear_page_dirty_for_io(page)) |
| 2160 | goto continue_unlock; |
| 2161 | |
Chao Yu | c0fe488 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2162 | ret = __write_data_page(page, &submitted, wbc, io_type); |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2163 | if (unlikely(ret)) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2164 | /* |
| 2165 | * keep nr_to_write, since vfs uses this to |
| 2166 | * get # of written pages. |
| 2167 | */ |
| 2168 | if (ret == AOP_WRITEPAGE_ACTIVATE) { |
| 2169 | unlock_page(page); |
| 2170 | ret = 0; |
| 2171 | continue; |
Jaegeuk Kim | 0f4dc16 | 2017-06-21 17:52:39 -0700 | [diff] [blame] | 2172 | } else if (ret == -EAGAIN) { |
| 2173 | ret = 0; |
| 2174 | if (wbc->sync_mode == WB_SYNC_ALL) { |
| 2175 | cond_resched(); |
| 2176 | congestion_wait(BLK_RW_ASYNC, |
| 2177 | HZ/50); |
| 2178 | goto retry_write; |
| 2179 | } |
| 2180 | continue; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2181 | } |
Jaegeuk Kim | b230e6c | 2016-05-29 21:18:23 -0700 | [diff] [blame] | 2182 | done_index = page->index + 1; |
| 2183 | done = 1; |
| 2184 | break; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2185 | } else if (submitted) { |
Chao Yu | 39373df | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 2186 | nwritten++; |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2187 | } |
| 2188 | |
Chao Yu | 458e47f | 2018-05-23 22:25:09 +0800 | [diff] [blame] | 2189 | if (--wbc->nr_to_write <= 0 && |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2190 | wbc->sync_mode == WB_SYNC_NONE) { |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2191 | done = 1; |
| 2192 | break; |
| 2193 | } |
| 2194 | } |
| 2195 | pagevec_release(&pvec); |
| 2196 | cond_resched(); |
| 2197 | } |
| 2198 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2199 | if (!cycled && !done) { |
| 2200 | cycled = 1; |
| 2201 | index = 0; |
| 2202 | end = writeback_index - 1; |
| 2203 | goto retry; |
| 2204 | } |
| 2205 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 2206 | mapping->writeback_index = done_index; |
| 2207 | |
Chao Yu | 39373df | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 2208 | if (nwritten) |
Jaegeuk Kim | 9bc576a | 2017-05-10 11:28:38 -0700 | [diff] [blame] | 2209 | f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host, |
Chao Yu | 39373df | 2018-09-27 23:41:16 +0800 | [diff] [blame] | 2210 | NULL, 0, DATA); |
Chao Yu | 6ca56ca | 2016-09-29 18:50:11 +0800 | [diff] [blame] | 2211 | |
Chao Yu | 8f46dca | 2015-07-14 18:56:10 +0800 | [diff] [blame] | 2212 | return ret; |
| 2213 | } |
| 2214 | |
Jaegeuk Kim | 6d59845 | 2018-08-09 17:53:34 -0700 | [diff] [blame] | 2215 | static inline bool __should_serialize_io(struct inode *inode, |
| 2216 | struct writeback_control *wbc) |
| 2217 | { |
| 2218 | if (!S_ISREG(inode->i_mode)) |
| 2219 | return false; |
Chao Yu | d831272 | 2018-09-20 20:05:00 +0800 | [diff] [blame] | 2220 | if (IS_NOQUOTA(inode)) |
| 2221 | return false; |
Jaegeuk Kim | 6d59845 | 2018-08-09 17:53:34 -0700 | [diff] [blame] | 2222 | if (wbc->sync_mode != WB_SYNC_ALL) |
| 2223 | return true; |
| 2224 | if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks) |
| 2225 | return true; |
| 2226 | return false; |
| 2227 | } |
| 2228 | |
Chao Yu | fdd569a | 2018-05-30 00:20:39 +0800 | [diff] [blame] | 2229 | static int __f2fs_write_data_pages(struct address_space *mapping, |
Chao Yu | c0fe488 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2230 | struct writeback_control *wbc, |
| 2231 | enum iostat_type io_type) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2232 | { |
| 2233 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 2234 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 9dfa1ba | 2016-07-13 19:33:19 -0700 | [diff] [blame] | 2235 | struct blk_plug plug; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2236 | int ret; |
Jaegeuk Kim | 6d59845 | 2018-08-09 17:53:34 -0700 | [diff] [blame] | 2237 | bool locked = false; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2238 | |
P J P | cfb185a | 2013-04-03 11:38:00 +0900 | [diff] [blame] | 2239 | /* deal with chardevs and other special file */ |
| 2240 | if (!mapping->a_ops->writepage) |
| 2241 | return 0; |
| 2242 | |
Chao Yu | 6a29054 | 2015-07-17 18:02:39 +0800 | [diff] [blame] | 2243 | /* skip writing if there is no dirty page in this inode */ |
| 2244 | if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) |
| 2245 | return 0; |
| 2246 | |
Chao Yu | 00cc7ed | 2017-06-29 23:20:45 +0800 | [diff] [blame] | 2247 | /* during POR, we don't need to trigger writepage at all. */ |
| 2248 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 2249 | goto skip_write; |
| 2250 | |
Chao Yu | d831272 | 2018-09-20 20:05:00 +0800 | [diff] [blame] | 2251 | if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) && |
| 2252 | wbc->sync_mode == WB_SYNC_NONE && |
Jaegeuk Kim | a125702 | 2015-10-08 10:40:07 -0700 | [diff] [blame] | 2253 | get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2254 | f2fs_available_free_memory(sbi, DIRTY_DENTS)) |
Jaegeuk Kim | a125702 | 2015-10-08 10:40:07 -0700 | [diff] [blame] | 2255 | goto skip_write; |
| 2256 | |
Chao Yu | d323d00 | 2015-10-27 09:53:45 +0800 | [diff] [blame] | 2257 | /* skip writing during file defragment */ |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 2258 | if (is_inode_flag_set(inode, FI_DO_DEFRAG)) |
Chao Yu | d323d00 | 2015-10-27 09:53:45 +0800 | [diff] [blame] | 2259 | goto skip_write; |
| 2260 | |
Yunlei He | d31c7c3 | 2016-02-04 16:14:00 +0800 | [diff] [blame] | 2261 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 2262 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2263 | /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */ |
| 2264 | if (wbc->sync_mode == WB_SYNC_ALL) |
Chao Yu | deb78d4 | 2018-06-04 23:20:36 +0800 | [diff] [blame] | 2265 | atomic_inc(&sbi->wb_sync_req[DATA]); |
| 2266 | else if (atomic_read(&sbi->wb_sync_req[DATA])) |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2267 | goto skip_write; |
| 2268 | |
Jaegeuk Kim | 6d59845 | 2018-08-09 17:53:34 -0700 | [diff] [blame] | 2269 | if (__should_serialize_io(inode, wbc)) { |
| 2270 | mutex_lock(&sbi->writepages); |
| 2271 | locked = true; |
| 2272 | } |
| 2273 | |
Jaegeuk Kim | 9dfa1ba | 2016-07-13 19:33:19 -0700 | [diff] [blame] | 2274 | blk_start_plug(&plug); |
Chao Yu | c0fe488 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2275 | ret = f2fs_write_cache_pages(mapping, wbc, io_type); |
Jaegeuk Kim | 9dfa1ba | 2016-07-13 19:33:19 -0700 | [diff] [blame] | 2276 | blk_finish_plug(&plug); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2277 | |
Jaegeuk Kim | 6d59845 | 2018-08-09 17:53:34 -0700 | [diff] [blame] | 2278 | if (locked) |
| 2279 | mutex_unlock(&sbi->writepages); |
| 2280 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2281 | if (wbc->sync_mode == WB_SYNC_ALL) |
Chao Yu | deb78d4 | 2018-06-04 23:20:36 +0800 | [diff] [blame] | 2282 | atomic_dec(&sbi->wb_sync_req[DATA]); |
Jaegeuk Kim | 28ea616 | 2016-05-25 17:17:56 -0700 | [diff] [blame] | 2283 | /* |
| 2284 | * if some pages were truncated, we cannot guarantee its mapping->host |
| 2285 | * to detect pending bios. |
| 2286 | */ |
Jaegeuk Kim | 458e619 | 2013-12-11 13:54:01 +0900 | [diff] [blame] | 2287 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2288 | f2fs_remove_dirty_inode(inode); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2289 | return ret; |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 2290 | |
| 2291 | skip_write: |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 2292 | wbc->pages_skipped += get_dirty_pages(inode); |
Yunlei He | d31c7c3 | 2016-02-04 16:14:00 +0800 | [diff] [blame] | 2293 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
Jaegeuk Kim | d3baf95 | 2014-03-18 13:43:05 +0900 | [diff] [blame] | 2294 | return 0; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2295 | } |
| 2296 | |
Chao Yu | c0fe488 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2297 | static int f2fs_write_data_pages(struct address_space *mapping, |
| 2298 | struct writeback_control *wbc) |
| 2299 | { |
| 2300 | struct inode *inode = mapping->host; |
| 2301 | |
| 2302 | return __f2fs_write_data_pages(mapping, wbc, |
| 2303 | F2FS_I(inode)->cp_task == current ? |
| 2304 | FS_CP_DATA_IO : FS_DATA_IO); |
| 2305 | } |
| 2306 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2307 | static void f2fs_write_failed(struct address_space *mapping, loff_t to) |
| 2308 | { |
| 2309 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 819d915 | 2015-12-28 13:48:11 -0800 | [diff] [blame] | 2310 | loff_t i_size = i_size_read(inode); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2311 | |
Jaegeuk Kim | 819d915 | 2015-12-28 13:48:11 -0800 | [diff] [blame] | 2312 | if (to > i_size) { |
Chao Yu | 1d8aac3 | 2018-08-05 23:04:25 +0800 | [diff] [blame] | 2313 | down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
Jaegeuk Kim | f64b4d7 | 2018-07-25 12:11:56 +0900 | [diff] [blame] | 2314 | down_write(&F2FS_I(inode)->i_mmap_sem); |
Chao Yu | 1d8aac3 | 2018-08-05 23:04:25 +0800 | [diff] [blame] | 2315 | |
Jaegeuk Kim | 819d915 | 2015-12-28 13:48:11 -0800 | [diff] [blame] | 2316 | truncate_pagecache(inode, i_size); |
Chao Yu | c6f7823 | 2019-02-02 17:33:01 +0800 | [diff] [blame] | 2317 | if (!IS_NOQUOTA(inode)) |
| 2318 | f2fs_truncate_blocks(inode, i_size, true); |
Chao Yu | 1d8aac3 | 2018-08-05 23:04:25 +0800 | [diff] [blame] | 2319 | |
Qiuyang Sun | 7b23ea1 | 2017-05-18 11:06:45 +0800 | [diff] [blame] | 2320 | up_write(&F2FS_I(inode)->i_mmap_sem); |
Jaegeuk Kim | f64b4d7 | 2018-07-25 12:11:56 +0900 | [diff] [blame] | 2321 | up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2322 | } |
| 2323 | } |
| 2324 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2325 | static int prepare_write_begin(struct f2fs_sb_info *sbi, |
| 2326 | struct page *page, loff_t pos, unsigned len, |
| 2327 | block_t *blk_addr, bool *node_changed) |
| 2328 | { |
| 2329 | struct inode *inode = page->mapping->host; |
| 2330 | pgoff_t index = page->index; |
| 2331 | struct dnode_of_data dn; |
| 2332 | struct page *ipage; |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 2333 | bool locked = false; |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2334 | struct extent_info ei = {0,0,0}; |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2335 | int err = 0; |
Sheng Yong | bcabd03 | 2018-11-14 19:34:28 +0800 | [diff] [blame] | 2336 | int flag; |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2337 | |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 2338 | /* |
| 2339 | * we already allocated all the blocks, so we don't need to get |
| 2340 | * the block addresses when there is no need to fill the page. |
| 2341 | */ |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2342 | if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE && |
| 2343 | !is_inode_flag_set(inode, FI_NO_PREALLOC)) |
Jaegeuk Kim | 24b8491 | 2016-02-03 13:49:44 -0800 | [diff] [blame] | 2344 | return 0; |
| 2345 | |
Sheng Yong | bcabd03 | 2018-11-14 19:34:28 +0800 | [diff] [blame] | 2346 | /* f2fs_lock_op avoids race between write CP and convert_inline_page */ |
| 2347 | if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode)) |
| 2348 | flag = F2FS_GET_BLOCK_DEFAULT; |
| 2349 | else |
| 2350 | flag = F2FS_GET_BLOCK_PRE_AIO; |
| 2351 | |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 2352 | if (f2fs_has_inline_data(inode) || |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2353 | (pos & PAGE_MASK) >= i_size_read(inode)) { |
Sheng Yong | bcabd03 | 2018-11-14 19:34:28 +0800 | [diff] [blame] | 2354 | __do_map_lock(sbi, flag, true); |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 2355 | locked = true; |
| 2356 | } |
| 2357 | restart: |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2358 | /* check inline_data */ |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2359 | ipage = f2fs_get_node_page(sbi, inode->i_ino); |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2360 | if (IS_ERR(ipage)) { |
| 2361 | err = PTR_ERR(ipage); |
| 2362 | goto unlock_out; |
| 2363 | } |
| 2364 | |
| 2365 | set_new_dnode(&dn, inode, ipage, ipage, 0); |
| 2366 | |
| 2367 | if (f2fs_has_inline_data(inode)) { |
Chao Yu | e84f88e | 2017-07-19 00:19:05 +0800 | [diff] [blame] | 2368 | if (pos + len <= MAX_INLINE_DATA(inode)) { |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2369 | f2fs_do_read_inline_data(page, ipage); |
Jaegeuk Kim | 9194232 | 2016-05-20 10:13:22 -0700 | [diff] [blame] | 2370 | set_inode_flag(inode, FI_DATA_EXIST); |
Chao Yu | ab47036 | 2016-05-11 19:48:44 +0800 | [diff] [blame] | 2371 | if (inode->i_nlink) |
| 2372 | set_inline_node(ipage); |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2373 | } else { |
| 2374 | err = f2fs_convert_inline_page(&dn, page); |
| 2375 | if (err) |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 2376 | goto out; |
| 2377 | if (dn.data_blkaddr == NULL_ADDR) |
| 2378 | err = f2fs_get_block(&dn, index); |
| 2379 | } |
| 2380 | } else if (locked) { |
| 2381 | err = f2fs_get_block(&dn, index); |
| 2382 | } else { |
| 2383 | if (f2fs_lookup_extent_cache(inode, index, &ei)) { |
| 2384 | dn.data_blkaddr = ei.blk + index - ei.fofs; |
| 2385 | } else { |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 2386 | /* hole case */ |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2387 | err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | 4da7bf5 | 2016-04-06 11:27:03 -0700 | [diff] [blame] | 2388 | if (err || dn.data_blkaddr == NULL_ADDR) { |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 2389 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2390 | __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, |
| 2391 | true); |
Sheng Yong | bcabd03 | 2018-11-14 19:34:28 +0800 | [diff] [blame] | 2392 | WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO); |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 2393 | locked = true; |
| 2394 | goto restart; |
| 2395 | } |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2396 | } |
| 2397 | } |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 2398 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2399 | /* convert_inline_page can make node_changed */ |
| 2400 | *blk_addr = dn.data_blkaddr; |
| 2401 | *node_changed = dn.node_changed; |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 2402 | out: |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2403 | f2fs_put_dnode(&dn); |
| 2404 | unlock_out: |
Jaegeuk Kim | b4d07a3 | 2015-12-23 13:48:58 -0800 | [diff] [blame] | 2405 | if (locked) |
Sheng Yong | bcabd03 | 2018-11-14 19:34:28 +0800 | [diff] [blame] | 2406 | __do_map_lock(sbi, flag, false); |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2407 | return err; |
| 2408 | } |
| 2409 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2410 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
| 2411 | loff_t pos, unsigned len, unsigned flags, |
| 2412 | struct page **pagep, void **fsdata) |
| 2413 | { |
| 2414 | struct inode *inode = mapping->host; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 2415 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 2416 | struct page *page = NULL; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2417 | pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT; |
Chao Yu | 2a6489c | 2018-01-15 17:16:46 +0800 | [diff] [blame] | 2418 | bool need_balance = false, drop_atomic = false; |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2419 | block_t blkaddr = NULL_ADDR; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2420 | int err = 0; |
| 2421 | |
Chao Yu | 62aed04 | 2014-05-06 16:46:04 +0800 | [diff] [blame] | 2422 | trace_f2fs_write_begin(inode, pos, len, flags); |
| 2423 | |
Daniel Rosenberg | f22f93a | 2018-08-20 19:21:43 -0700 | [diff] [blame] | 2424 | err = f2fs_is_checkpoint_ready(sbi); |
| 2425 | if (err) |
| 2426 | goto fail; |
| 2427 | |
Jaegeuk Kim | 77d1332 | 2018-07-27 18:15:11 +0900 | [diff] [blame] | 2428 | if ((f2fs_is_atomic_file(inode) && |
| 2429 | !f2fs_available_free_memory(sbi, INMEM_PAGES)) || |
| 2430 | is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) { |
Jaegeuk Kim | 35e13ca | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 2431 | err = -ENOMEM; |
Chao Yu | 2a6489c | 2018-01-15 17:16:46 +0800 | [diff] [blame] | 2432 | drop_atomic = true; |
Jaegeuk Kim | 35e13ca | 2017-10-18 19:05:57 -0700 | [diff] [blame] | 2433 | goto fail; |
| 2434 | } |
| 2435 | |
Jaegeuk Kim | 5f72739 | 2014-11-25 10:59:45 -0800 | [diff] [blame] | 2436 | /* |
| 2437 | * We should check this at this moment to avoid deadlock on inode page |
| 2438 | * and #0 page. The locking rule for inline_data conversion should be: |
| 2439 | * lock_page(page #0) -> lock_page(inode_page) |
| 2440 | */ |
| 2441 | if (index != 0) { |
| 2442 | err = f2fs_convert_inline_inode(inode); |
| 2443 | if (err) |
| 2444 | goto fail; |
| 2445 | } |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 2446 | repeat: |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2447 | /* |
| 2448 | * Do not use grab_cache_page_write_begin() to avoid deadlock due to |
| 2449 | * wait_for_stable_page. Will wait that below with our IO control. |
| 2450 | */ |
Chao Yu | 58ddec8 | 2017-10-28 16:52:30 +0800 | [diff] [blame] | 2451 | page = f2fs_pagecache_get_page(mapping, index, |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2452 | FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2453 | if (!page) { |
| 2454 | err = -ENOMEM; |
| 2455 | goto fail; |
| 2456 | } |
Jaegeuk Kim | d5f6699 | 2014-04-30 09:22:45 +0900 | [diff] [blame] | 2457 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2458 | *pagep = page; |
| 2459 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2460 | err = prepare_write_begin(sbi, page, pos, len, |
| 2461 | &blkaddr, &need_balance); |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 2462 | if (err) |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2463 | goto fail; |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 2464 | |
Chao Yu | d831272 | 2018-09-20 20:05:00 +0800 | [diff] [blame] | 2465 | if (need_balance && !IS_NOQUOTA(inode) && |
| 2466 | has_not_enough_free_secs(sbi, 0, 0)) { |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 2467 | unlock_page(page); |
Jaegeuk Kim | 2c4db1a | 2016-01-07 14:15:04 -0800 | [diff] [blame] | 2468 | f2fs_balance_fs(sbi, true); |
Jaegeuk Kim | 2a34076 | 2015-12-22 13:23:35 -0800 | [diff] [blame] | 2469 | lock_page(page); |
| 2470 | if (page->mapping != mapping) { |
| 2471 | /* The page got truncated from under us */ |
| 2472 | f2fs_put_page(page, 1); |
| 2473 | goto repeat; |
| 2474 | } |
| 2475 | } |
| 2476 | |
Chao Yu | d9b3df5 | 2018-12-25 17:43:42 +0800 | [diff] [blame] | 2477 | f2fs_wait_on_page_writeback(page, DATA, false, true); |
Jaegeuk Kim | b3d208f | 2014-10-23 19:48:09 -0700 | [diff] [blame] | 2478 | |
Jaegeuk Kim | 649d7df | 2016-09-06 11:02:03 -0700 | [diff] [blame] | 2479 | if (len == PAGE_SIZE || PageUptodate(page)) |
| 2480 | return 0; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2481 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2482 | if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) { |
| 2483 | zero_user_segment(page, len, PAGE_SIZE); |
| 2484 | return 0; |
| 2485 | } |
| 2486 | |
Jaegeuk Kim | 2aadac0 | 2015-12-23 11:55:18 -0800 | [diff] [blame] | 2487 | if (blkaddr == NEW_ADDR) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2488 | zero_user_segment(page, 0, PAGE_SIZE); |
Jaegeuk Kim | 649d7df | 2016-09-06 11:02:03 -0700 | [diff] [blame] | 2489 | SetPageUptodate(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2490 | } else { |
Jaegeuk Kim | 5a69067 | 2017-09-06 21:04:44 -0700 | [diff] [blame] | 2491 | err = f2fs_submit_page_read(inode, page, blkaddr); |
| 2492 | if (err) |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2493 | goto fail; |
Chao Yu | d54c795 | 2014-03-29 15:30:40 +0800 | [diff] [blame] | 2494 | |
Jaegeuk Kim | 393ff91 | 2013-03-08 21:29:23 +0900 | [diff] [blame] | 2495 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 2496 | if (unlikely(page->mapping != mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 2497 | f2fs_put_page(page, 1); |
| 2498 | goto repeat; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2499 | } |
Chao Yu | 1563ac7 | 2016-07-03 22:05:12 +0800 | [diff] [blame] | 2500 | if (unlikely(!PageUptodate(page))) { |
| 2501 | err = -EIO; |
| 2502 | goto fail; |
Jaegeuk Kim | 4375a33 | 2015-04-23 12:04:33 -0700 | [diff] [blame] | 2503 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2504 | } |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2505 | return 0; |
Jaegeuk Kim | 9ba69cf | 2014-10-17 20:33:55 -0700 | [diff] [blame] | 2506 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2507 | fail: |
Jaegeuk Kim | 86531d6 | 2015-07-15 13:08:21 -0700 | [diff] [blame] | 2508 | f2fs_put_page(page, 1); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2509 | f2fs_write_failed(mapping, pos + len); |
Chao Yu | 2a6489c | 2018-01-15 17:16:46 +0800 | [diff] [blame] | 2510 | if (drop_atomic) |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2511 | f2fs_drop_inmem_pages_all(sbi, false); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2512 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2513 | } |
| 2514 | |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 2515 | static int f2fs_write_end(struct file *file, |
| 2516 | struct address_space *mapping, |
| 2517 | loff_t pos, unsigned len, unsigned copied, |
| 2518 | struct page *page, void *fsdata) |
| 2519 | { |
| 2520 | struct inode *inode = page->mapping->host; |
| 2521 | |
Chao Yu | dfb2bf3 | 2014-05-06 16:47:23 +0800 | [diff] [blame] | 2522 | trace_f2fs_write_end(inode, pos, len, copied); |
| 2523 | |
Jaegeuk Kim | 649d7df | 2016-09-06 11:02:03 -0700 | [diff] [blame] | 2524 | /* |
| 2525 | * This should be come from len == PAGE_SIZE, and we expect copied |
| 2526 | * should be PAGE_SIZE. Otherwise, we treat it with zero copied and |
| 2527 | * let generic_perform_write() try to copy data again through copied=0. |
| 2528 | */ |
| 2529 | if (!PageUptodate(page)) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2530 | if (unlikely(copied != len)) |
Jaegeuk Kim | 649d7df | 2016-09-06 11:02:03 -0700 | [diff] [blame] | 2531 | copied = 0; |
| 2532 | else |
| 2533 | SetPageUptodate(page); |
| 2534 | } |
| 2535 | if (!copied) |
| 2536 | goto unlock_out; |
| 2537 | |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 2538 | set_page_dirty(page); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 2539 | |
Jaegeuk Kim | fc9581c | 2016-05-20 09:22:03 -0700 | [diff] [blame] | 2540 | if (pos + copied > i_size_read(inode)) |
| 2541 | f2fs_i_size_write(inode, pos + copied); |
Jaegeuk Kim | 649d7df | 2016-09-06 11:02:03 -0700 | [diff] [blame] | 2542 | unlock_out: |
Chao Yu | 3024c9a | 2016-08-06 21:09:41 +0800 | [diff] [blame] | 2543 | f2fs_put_page(page, 1); |
Jaegeuk Kim | d0239e1 | 2016-01-08 16:57:48 -0800 | [diff] [blame] | 2544 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 2545 | return copied; |
| 2546 | } |
| 2547 | |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 2548 | static int check_direct_IO(struct inode *inode, struct iov_iter *iter, |
| 2549 | loff_t offset) |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 2550 | { |
Jaegeuk Kim | 2847525 | 2018-06-29 18:55:12 -0700 | [diff] [blame] | 2551 | unsigned i_blkbits = READ_ONCE(inode->i_blkbits); |
| 2552 | unsigned blkbits = i_blkbits; |
| 2553 | unsigned blocksize_mask = (1 << blkbits) - 1; |
| 2554 | unsigned long align = offset | iov_iter_alignment(iter); |
| 2555 | struct block_device *bdev = inode->i_sb->s_bdev; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 2556 | |
Jaegeuk Kim | 2847525 | 2018-06-29 18:55:12 -0700 | [diff] [blame] | 2557 | if (align & blocksize_mask) { |
| 2558 | if (bdev) |
| 2559 | blkbits = blksize_bits(bdev_logical_block_size(bdev)); |
| 2560 | blocksize_mask = (1 << blkbits) - 1; |
| 2561 | if (align & blocksize_mask) |
| 2562 | return -EINVAL; |
| 2563 | return 1; |
| 2564 | } |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 2565 | return 0; |
| 2566 | } |
| 2567 | |
Chao Yu | cc32cf3 | 2018-11-12 00:46:46 +0800 | [diff] [blame] | 2568 | static void f2fs_dio_end_io(struct bio *bio) |
| 2569 | { |
| 2570 | struct f2fs_private_dio *dio = bio->bi_private; |
| 2571 | |
| 2572 | dec_page_count(F2FS_I_SB(dio->inode), |
| 2573 | dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ); |
| 2574 | |
| 2575 | bio->bi_private = dio->orig_private; |
| 2576 | bio->bi_end_io = dio->orig_end_io; |
| 2577 | |
Jaegeuk Kim | 9248dba | 2018-12-13 18:38:33 -0800 | [diff] [blame] | 2578 | kvfree(dio); |
Chao Yu | cc32cf3 | 2018-11-12 00:46:46 +0800 | [diff] [blame] | 2579 | |
| 2580 | bio_endio(bio); |
| 2581 | } |
| 2582 | |
| 2583 | static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode, |
| 2584 | loff_t file_offset) |
| 2585 | { |
| 2586 | struct f2fs_private_dio *dio; |
| 2587 | bool write = (bio_op(bio) == REQ_OP_WRITE); |
Chao Yu | cc32cf3 | 2018-11-12 00:46:46 +0800 | [diff] [blame] | 2588 | |
| 2589 | dio = f2fs_kzalloc(F2FS_I_SB(inode), |
| 2590 | sizeof(struct f2fs_private_dio), GFP_NOFS); |
YueHaibing | c476cbd | 2019-01-04 01:38:29 +0000 | [diff] [blame] | 2591 | if (!dio) |
Chao Yu | cc32cf3 | 2018-11-12 00:46:46 +0800 | [diff] [blame] | 2592 | goto out; |
Chao Yu | cc32cf3 | 2018-11-12 00:46:46 +0800 | [diff] [blame] | 2593 | |
| 2594 | dio->inode = inode; |
| 2595 | dio->orig_end_io = bio->bi_end_io; |
| 2596 | dio->orig_private = bio->bi_private; |
| 2597 | dio->write = write; |
| 2598 | |
| 2599 | bio->bi_end_io = f2fs_dio_end_io; |
| 2600 | bio->bi_private = dio; |
| 2601 | |
| 2602 | inc_page_count(F2FS_I_SB(inode), |
| 2603 | write ? F2FS_DIO_WRITE : F2FS_DIO_READ); |
| 2604 | |
| 2605 | submit_bio(bio); |
| 2606 | return; |
| 2607 | out: |
| 2608 | bio->bi_error = -EIO; |
| 2609 | bio_endio(bio); |
| 2610 | } |
| 2611 | |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 2612 | static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2613 | { |
Jaegeuk Kim | b439b10 | 2016-02-03 13:09:09 -0800 | [diff] [blame] | 2614 | struct address_space *mapping = iocb->ki_filp->f_mapping; |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2615 | struct inode *inode = mapping->host; |
Hyunchul Lee | bcdc571 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2616 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 2617 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2618 | size_t count = iov_iter_count(iter); |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 2619 | loff_t offset = iocb->ki_pos; |
Chao Yu | 82e0a5a | 2016-07-13 09:18:29 +0800 | [diff] [blame] | 2620 | int rw = iov_iter_rw(iter); |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2621 | int err; |
Hyunchul Lee | bcdc571 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2622 | enum rw_hint hint = iocb->ki_hint; |
Chao Yu | e9a50e6 | 2018-03-08 14:22:56 +0800 | [diff] [blame] | 2623 | int whint_mode = F2FS_OPTION(sbi).whint_mode; |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 2624 | bool do_opu; |
Jaegeuk Kim | 944fcfc | 2013-12-26 20:15:09 +0900 | [diff] [blame] | 2625 | |
Jaegeuk Kim | b439b10 | 2016-02-03 13:09:09 -0800 | [diff] [blame] | 2626 | err = check_direct_IO(inode, iter, offset); |
Jaegeuk Kim | b9d777b | 2015-12-22 11:09:35 -0800 | [diff] [blame] | 2627 | if (err) |
Jaegeuk Kim | 2847525 | 2018-06-29 18:55:12 -0700 | [diff] [blame] | 2628 | return err < 0 ? err : 0; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 2629 | |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 2630 | if (f2fs_force_buffered_io(inode, iocb, iter)) |
Jaegeuk Kim | 36abef4 | 2016-06-03 19:29:38 -0700 | [diff] [blame] | 2631 | return 0; |
Jaegeuk Kim | fcc85a4 | 2015-04-21 20:39:58 -0700 | [diff] [blame] | 2632 | |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 2633 | do_opu = allow_outplace_dio(inode, iocb, iter); |
| 2634 | |
Jaegeuk Kim | 5302fb0 | 2016-07-22 15:25:47 -0700 | [diff] [blame] | 2635 | trace_f2fs_direct_IO_enter(inode, offset, count, rw); |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 2636 | |
Hyunchul Lee | bcdc571 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2637 | if (rw == WRITE && whint_mode == WHINT_MODE_OFF) |
| 2638 | iocb->ki_hint = WRITE_LIFE_NOT_SET; |
| 2639 | |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 2640 | if (iocb->ki_flags & IOCB_NOWAIT) { |
| 2641 | if (!down_read_trylock(&fi->i_gc_rwsem[rw])) { |
Hyunchul Lee | 355d234 | 2018-03-08 19:34:38 +0900 | [diff] [blame] | 2642 | iocb->ki_hint = hint; |
| 2643 | err = -EAGAIN; |
| 2644 | goto out; |
| 2645 | } |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 2646 | if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) { |
| 2647 | up_read(&fi->i_gc_rwsem[rw]); |
| 2648 | iocb->ki_hint = hint; |
| 2649 | err = -EAGAIN; |
| 2650 | goto out; |
| 2651 | } |
| 2652 | } else { |
| 2653 | down_read(&fi->i_gc_rwsem[rw]); |
| 2654 | if (do_opu) |
| 2655 | down_read(&fi->i_gc_rwsem[READ]); |
Hyunchul Lee | 355d234 | 2018-03-08 19:34:38 +0900 | [diff] [blame] | 2656 | } |
| 2657 | |
Chao Yu | cc32cf3 | 2018-11-12 00:46:46 +0800 | [diff] [blame] | 2658 | err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, |
Chao Yu | 5b5162d | 2018-11-13 14:33:45 +0800 | [diff] [blame] | 2659 | iter, rw == WRITE ? get_data_block_dio_write : |
| 2660 | get_data_block_dio, NULL, f2fs_dio_submit_bio, |
Chao Yu | cc32cf3 | 2018-11-12 00:46:46 +0800 | [diff] [blame] | 2661 | DIO_LOCKING | DIO_SKIP_HOLES); |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 2662 | |
| 2663 | if (do_opu) |
| 2664 | up_read(&fi->i_gc_rwsem[READ]); |
| 2665 | |
| 2666 | up_read(&fi->i_gc_rwsem[rw]); |
Chao Yu | 82e0a5a | 2016-07-13 09:18:29 +0800 | [diff] [blame] | 2667 | |
| 2668 | if (rw == WRITE) { |
Hyunchul Lee | bcdc571 | 2018-01-31 11:36:57 +0900 | [diff] [blame] | 2669 | if (whint_mode == WHINT_MODE_OFF) |
| 2670 | iocb->ki_hint = hint; |
Chao Yu | c0fe488 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2671 | if (err > 0) { |
| 2672 | f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO, |
| 2673 | err); |
Chao Yu | b84f9a7 | 2018-09-27 18:34:52 +0800 | [diff] [blame] | 2674 | if (!do_opu) |
| 2675 | set_inode_flag(inode, FI_UPDATE_WRITE); |
Chao Yu | c0fe488 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2676 | } else if (err < 0) { |
Jaegeuk Kim | 6bfc491 | 2016-04-18 17:07:44 -0400 | [diff] [blame] | 2677 | f2fs_write_failed(mapping, offset + count); |
Chao Yu | c0fe488 | 2017-08-02 23:21:48 +0800 | [diff] [blame] | 2678 | } |
Jaegeuk Kim | 6bfc491 | 2016-04-18 17:07:44 -0400 | [diff] [blame] | 2679 | } |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 2680 | |
Hyunchul Lee | 355d234 | 2018-03-08 19:34:38 +0900 | [diff] [blame] | 2681 | out: |
Jaegeuk Kim | 5302fb0 | 2016-07-22 15:25:47 -0700 | [diff] [blame] | 2682 | trace_f2fs_direct_IO_exit(inode, offset, count, rw, err); |
Chao Yu | 70407fa | 2014-07-31 21:11:22 +0800 | [diff] [blame] | 2683 | |
Chao Yu | 3aab8f8 | 2014-07-02 13:25:04 +0800 | [diff] [blame] | 2684 | return err; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2685 | } |
| 2686 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2687 | void f2fs_invalidate_page(struct page *page, unsigned int offset, |
| 2688 | unsigned int length) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2689 | { |
| 2690 | struct inode *inode = page->mapping->host; |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2691 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 2692 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2693 | if (inode->i_ino >= F2FS_ROOT_INO(sbi) && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2694 | (offset % PAGE_SIZE || length != PAGE_SIZE)) |
Jaegeuk Kim | a7ffdbe | 2014-09-12 15:53:45 -0700 | [diff] [blame] | 2695 | return; |
| 2696 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2697 | if (PageDirty(page)) { |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2698 | if (inode->i_ino == F2FS_META_INO(sbi)) { |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2699 | dec_page_count(sbi, F2FS_DIRTY_META); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2700 | } else if (inode->i_ino == F2FS_NODE_INO(sbi)) { |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2701 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2702 | } else { |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2703 | inode_dec_dirty_pages(inode); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2704 | f2fs_remove_dirty_inode(inode); |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2705 | } |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2706 | } |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 2707 | |
Chao Yu | 408bca0 | 2018-07-27 18:15:16 +0800 | [diff] [blame] | 2708 | clear_cold_data(page); |
| 2709 | |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 2710 | if (IS_ATOMIC_WRITTEN_PAGE(page)) |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2711 | return f2fs_drop_inmem_page(inode, page); |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 2712 | |
Chao Yu | c4e0760 | 2019-03-06 17:30:59 +0800 | [diff] [blame] | 2713 | f2fs_clear_page_private(page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2714 | } |
| 2715 | |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2716 | int f2fs_release_page(struct page *page, gfp_t wait) |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2717 | { |
Jaegeuk Kim | f68daee | 2015-01-30 11:39:08 -0800 | [diff] [blame] | 2718 | /* If this is dirty page, keep PagePrivate */ |
| 2719 | if (PageDirty(page)) |
| 2720 | return 0; |
| 2721 | |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 2722 | /* This is atomic written page, keep Private */ |
| 2723 | if (IS_ATOMIC_WRITTEN_PAGE(page)) |
| 2724 | return 0; |
| 2725 | |
Chao Yu | 408bca0 | 2018-07-27 18:15:16 +0800 | [diff] [blame] | 2726 | clear_cold_data(page); |
Chao Yu | c4e0760 | 2019-03-06 17:30:59 +0800 | [diff] [blame] | 2727 | f2fs_clear_page_private(page); |
Jaegeuk Kim | c3850aa | 2013-03-14 09:24:32 +0900 | [diff] [blame] | 2728 | return 1; |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | static int f2fs_set_data_page_dirty(struct page *page) |
| 2732 | { |
| 2733 | struct address_space *mapping = page->mapping; |
| 2734 | struct inode *inode = mapping->host; |
| 2735 | |
Jaegeuk Kim | 26c6b88 | 2013-10-24 17:53:29 +0900 | [diff] [blame] | 2736 | trace_f2fs_set_page_dirty(page, DATA); |
| 2737 | |
Jaegeuk Kim | 237c079 | 2016-06-30 18:49:15 -0700 | [diff] [blame] | 2738 | if (!PageUptodate(page)) |
| 2739 | SetPageUptodate(page); |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 2740 | |
Jaegeuk Kim | e6b120d | 2017-07-10 12:55:09 -0700 | [diff] [blame] | 2741 | if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) { |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 2742 | if (!IS_ATOMIC_WRITTEN_PAGE(page)) { |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2743 | f2fs_register_inmem_page(inode, page); |
Chao Yu | decd36b | 2015-08-07 18:42:09 +0800 | [diff] [blame] | 2744 | return 1; |
| 2745 | } |
| 2746 | /* |
| 2747 | * Previously, this page has been registered, we just |
| 2748 | * return here. |
| 2749 | */ |
| 2750 | return 0; |
Jaegeuk Kim | 34ba94b | 2014-10-09 13:19:53 -0700 | [diff] [blame] | 2751 | } |
| 2752 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2753 | if (!PageDirty(page)) { |
Jaegeuk Kim | 95459e9 | 2018-04-20 19:29:52 -0700 | [diff] [blame] | 2754 | __set_page_dirty_nobuffers(page); |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2755 | f2fs_update_dirty_page(inode, page); |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2756 | return 1; |
| 2757 | } |
| 2758 | return 0; |
| 2759 | } |
| 2760 | |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 2761 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 2762 | { |
Chao Yu | 454ae7e | 2014-04-22 13:34:01 +0800 | [diff] [blame] | 2763 | struct inode *inode = mapping->host; |
| 2764 | |
Jaegeuk Kim | 1d373a0 | 2015-10-19 10:29:51 -0700 | [diff] [blame] | 2765 | if (f2fs_has_inline_data(inode)) |
| 2766 | return 0; |
| 2767 | |
| 2768 | /* make sure allocating whole blocks */ |
| 2769 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
| 2770 | filemap_write_and_wait(mapping); |
| 2771 | |
Chao Yu | e2b4e2b | 2015-08-19 19:11:19 +0800 | [diff] [blame] | 2772 | return generic_block_bmap(mapping, block, get_data_block_bmap); |
Chao Yu | 429511c | 2015-02-05 17:54:31 +0800 | [diff] [blame] | 2773 | } |
| 2774 | |
Weichao Guo | 5b7a487 | 2016-09-20 05:03:27 +0800 | [diff] [blame] | 2775 | #ifdef CONFIG_MIGRATION |
| 2776 | #include <linux/migrate.h> |
| 2777 | |
| 2778 | int f2fs_migrate_page(struct address_space *mapping, |
| 2779 | struct page *newpage, struct page *page, enum migrate_mode mode) |
| 2780 | { |
| 2781 | int rc, extra_count; |
| 2782 | struct f2fs_inode_info *fi = F2FS_I(mapping->host); |
| 2783 | bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page); |
| 2784 | |
| 2785 | BUG_ON(PageWriteback(page)); |
| 2786 | |
| 2787 | /* migrating an atomic written page is safe with the inmem_lock hold */ |
Jaegeuk Kim | 025258e | 2017-07-06 14:46:01 -0700 | [diff] [blame] | 2788 | if (atomic_written) { |
| 2789 | if (mode != MIGRATE_SYNC) |
| 2790 | return -EBUSY; |
| 2791 | if (!mutex_trylock(&fi->inmem_lock)) |
| 2792 | return -EAGAIN; |
| 2793 | } |
Weichao Guo | 5b7a487 | 2016-09-20 05:03:27 +0800 | [diff] [blame] | 2794 | |
Chao Yu | c4e0760 | 2019-03-06 17:30:59 +0800 | [diff] [blame] | 2795 | /* one extra reference was held for atomic_write page */ |
| 2796 | extra_count = atomic_written ? 1 : 0; |
Weichao Guo | 5b7a487 | 2016-09-20 05:03:27 +0800 | [diff] [blame] | 2797 | rc = migrate_page_move_mapping(mapping, newpage, |
| 2798 | page, NULL, mode, extra_count); |
| 2799 | if (rc != MIGRATEPAGE_SUCCESS) { |
| 2800 | if (atomic_written) |
| 2801 | mutex_unlock(&fi->inmem_lock); |
| 2802 | return rc; |
| 2803 | } |
| 2804 | |
| 2805 | if (atomic_written) { |
| 2806 | struct inmem_pages *cur; |
| 2807 | list_for_each_entry(cur, &fi->inmem_pages, list) |
| 2808 | if (cur->page == page) { |
| 2809 | cur->page = newpage; |
| 2810 | break; |
| 2811 | } |
| 2812 | mutex_unlock(&fi->inmem_lock); |
| 2813 | put_page(page); |
| 2814 | get_page(newpage); |
| 2815 | } |
| 2816 | |
Chao Yu | c4e0760 | 2019-03-06 17:30:59 +0800 | [diff] [blame] | 2817 | if (PagePrivate(page)) { |
| 2818 | f2fs_set_page_private(newpage, page_private(page)); |
| 2819 | f2fs_clear_page_private(page); |
| 2820 | } |
Weichao Guo | 5b7a487 | 2016-09-20 05:03:27 +0800 | [diff] [blame] | 2821 | |
| 2822 | migrate_page_copy(newpage, page); |
| 2823 | |
| 2824 | return MIGRATEPAGE_SUCCESS; |
| 2825 | } |
| 2826 | #endif |
| 2827 | |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2828 | const struct address_space_operations f2fs_dblock_aops = { |
| 2829 | .readpage = f2fs_read_data_page, |
| 2830 | .readpages = f2fs_read_data_pages, |
| 2831 | .writepage = f2fs_write_data_page, |
| 2832 | .writepages = f2fs_write_data_pages, |
| 2833 | .write_begin = f2fs_write_begin, |
Jaegeuk Kim | a1dd3c1 | 2013-06-27 13:04:08 +0900 | [diff] [blame] | 2834 | .write_end = f2fs_write_end, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2835 | .set_page_dirty = f2fs_set_data_page_dirty, |
Chao Yu | 487261f | 2015-02-05 17:44:29 +0800 | [diff] [blame] | 2836 | .invalidatepage = f2fs_invalidate_page, |
| 2837 | .releasepage = f2fs_release_page, |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2838 | .direct_IO = f2fs_direct_IO, |
Jaegeuk Kim | c01e54b | 2013-01-17 20:30:23 +0900 | [diff] [blame] | 2839 | .bmap = f2fs_bmap, |
Weichao Guo | 5b7a487 | 2016-09-20 05:03:27 +0800 | [diff] [blame] | 2840 | #ifdef CONFIG_MIGRATION |
| 2841 | .migratepage = f2fs_migrate_page, |
| 2842 | #endif |
Jaegeuk Kim | eb47b80 | 2012-11-02 17:10:12 +0900 | [diff] [blame] | 2843 | }; |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 2844 | |
Chao Yu | 6b4d6a8 | 2018-05-30 00:20:41 +0800 | [diff] [blame] | 2845 | void f2fs_clear_radix_tree_dirty_tag(struct page *page) |
Chao Yu | bcbcda4 | 2018-05-26 18:03:35 +0800 | [diff] [blame] | 2846 | { |
| 2847 | struct address_space *mapping = page_mapping(page); |
| 2848 | unsigned long flags; |
| 2849 | |
| 2850 | spin_lock_irqsave(&mapping->tree_lock, flags); |
| 2851 | radix_tree_tag_clear(&mapping->page_tree, page_index(page), |
| 2852 | PAGECACHE_TAG_DIRTY); |
| 2853 | spin_unlock_irqrestore(&mapping->tree_lock, flags); |
| 2854 | } |
| 2855 | |
Eric Biggers | f69e814 | 2018-04-18 11:09:48 -0700 | [diff] [blame] | 2856 | int __init f2fs_init_post_read_processing(void) |
| 2857 | { |
| 2858 | bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0); |
| 2859 | if (!bio_post_read_ctx_cache) |
| 2860 | goto fail; |
| 2861 | bio_post_read_ctx_pool = |
| 2862 | mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS, |
| 2863 | bio_post_read_ctx_cache); |
| 2864 | if (!bio_post_read_ctx_pool) |
| 2865 | goto fail_free_cache; |
| 2866 | return 0; |
| 2867 | |
| 2868 | fail_free_cache: |
| 2869 | kmem_cache_destroy(bio_post_read_ctx_cache); |
| 2870 | fail: |
| 2871 | return -ENOMEM; |
| 2872 | } |
| 2873 | |
| 2874 | void __exit f2fs_destroy_post_read_processing(void) |
| 2875 | { |
| 2876 | mempool_destroy(bio_post_read_ctx_pool); |
| 2877 | kmem_cache_destroy(bio_post_read_ctx_cache); |
| 2878 | } |