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