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