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