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