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