blob: abbf14e9bd725fd5ee975a9b59b680f31a08df34 [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003 * fs/f2fs/data.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09007 */
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 Yu8f46dca2015-07-14 18:56:10 +080014#include <linux/pagevec.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090015#include <linux/blkdev.h>
16#include <linux/bio.h>
Jaegeuk Kim4969c062019-07-01 19:15:29 -070017#include <linux/swap.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010018#include <linux/prefetch.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080019#include <linux/uio.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070020#include <linux/cleancache.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010021#include <linux/sched/signal.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090022
23#include "f2fs.h"
24#include "node.h"
25#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080026#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090027#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090028
Eric Biggers6dbb1792018-04-18 11:09:48 -070029#define NUM_PREALLOC_POST_READ_CTXS 128
30
31static struct kmem_cache *bio_post_read_ctx_cache;
32static mempool_t *bio_post_read_ctx_pool;
33
Chao Yu36951b32016-11-16 10:41:20 +080034static bool __is_cp_guaranteed(struct page *page)
35{
36 struct address_space *mapping = page->mapping;
37 struct inode *inode;
38 struct f2fs_sb_info *sbi;
39
40 if (!mapping)
41 return false;
42
43 inode = mapping->host;
44 sbi = F2FS_I_SB(inode);
45
46 if (inode->i_ino == F2FS_META_INO(sbi) ||
47 inode->i_ino == F2FS_NODE_INO(sbi) ||
48 S_ISDIR(inode->i_mode) ||
Chao Yue7a4feb2018-05-08 14:06:03 +080049 (S_ISREG(inode->i_mode) &&
Chao Yuaf033b22018-09-20 20:05:00 +080050 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
Chao Yu36951b32016-11-16 10:41:20 +080051 is_cold_data(page))
52 return true;
53 return false;
54}
55
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -070056static enum count_type __read_io_type(struct page *page)
57{
Jaegeuk Kim4969c062019-07-01 19:15:29 -070058 struct address_space *mapping = page_file_mapping(page);
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -070059
60 if (mapping) {
61 struct inode *inode = mapping->host;
62 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
63
64 if (inode->i_ino == F2FS_META_INO(sbi))
65 return F2FS_RD_META;
66
67 if (inode->i_ino == F2FS_NODE_INO(sbi))
68 return F2FS_RD_NODE;
69 }
70 return F2FS_RD_DATA;
71}
72
Eric Biggers6dbb1792018-04-18 11:09:48 -070073/* postprocessing steps for read bios */
74enum bio_post_read_step {
75 STEP_INITIAL = 0,
76 STEP_DECRYPT,
77};
78
79struct bio_post_read_ctx {
80 struct bio *bio;
81 struct work_struct work;
82 unsigned int cur_step;
83 unsigned int enabled_steps;
84};
85
86static void __read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090087{
Eric Biggers6dbb1792018-04-18 11:09:48 -070088 struct page *page;
89 struct bio_vec *bv;
Ming Lei6dc4f102019-02-15 19:13:19 +080090 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090091
Christoph Hellwig2b070cf2019-04-25 09:03:00 +020092 bio_for_each_segment_all(bv, bio, iter_all) {
Eric Biggers6dbb1792018-04-18 11:09:48 -070093 page = bv->bv_page;
94
95 /* PG_error was set if any post_read step failed */
96 if (bio->bi_status || PageError(page)) {
97 ClearPageUptodate(page);
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -070098 /* will re-read again later */
99 ClearPageError(page);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700100 } else {
101 SetPageUptodate(page);
102 }
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700103 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
Eric Biggers6dbb1792018-04-18 11:09:48 -0700104 unlock_page(page);
105 }
106 if (bio->bi_private)
107 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
108 bio_put(bio);
109}
110
111static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
112
113static void decrypt_work(struct work_struct *work)
114{
115 struct bio_post_read_ctx *ctx =
116 container_of(work, struct bio_post_read_ctx, work);
117
118 fscrypt_decrypt_bio(ctx->bio);
119
120 bio_post_read_processing(ctx);
121}
122
123static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
124{
125 switch (++ctx->cur_step) {
126 case STEP_DECRYPT:
127 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
128 INIT_WORK(&ctx->work, decrypt_work);
129 fscrypt_enqueue_decrypt_work(&ctx->work);
130 return;
131 }
132 ctx->cur_step++;
133 /* fall-through */
134 default:
135 __read_end_io(ctx->bio);
136 }
137}
138
139static bool f2fs_bio_post_read_required(struct bio *bio)
140{
141 return bio->bi_private && !bio->bi_status;
142}
143
144static void f2fs_read_end_io(struct bio *bio)
145{
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800146 if (time_to_inject(F2FS_P_SB(bio_first_page_all(bio)),
147 FAULT_READ_IO)) {
148 f2fs_show_injection_info(FAULT_READ_IO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200149 bio->bi_status = BLK_STS_IOERR;
Chao Yu55523512017-02-25 11:08:28 +0800150 }
Chao Yu8b038c72016-09-18 23:30:07 +0800151
Eric Biggers6dbb1792018-04-18 11:09:48 -0700152 if (f2fs_bio_post_read_required(bio)) {
153 struct bio_post_read_ctx *ctx = bio->bi_private;
154
155 ctx->cur_step = STEP_INITIAL;
156 bio_post_read_processing(ctx);
157 return;
Chao Yu12377022015-05-25 18:03:38 +0800158 }
159
Eric Biggers6dbb1792018-04-18 11:09:48 -0700160 __read_end_io(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900161}
162
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200163static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900164{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +0900165 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -0800166 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800167 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900168
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800169 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
170 f2fs_show_injection_info(FAULT_WRITE_IO);
171 bio->bi_status = BLK_STS_IOERR;
172 }
173
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200174 bio_for_each_segment_all(bvec, bio, iter_all) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900175 struct page *page = bvec->bv_page;
Chao Yu36951b32016-11-16 10:41:20 +0800176 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900177
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800178 if (IS_DUMMY_WRITTEN_PAGE(page)) {
179 set_page_private(page, (unsigned long)NULL);
180 ClearPagePrivate(page);
181 unlock_page(page);
182 mempool_free(page, sbi->write_io_dummy);
183
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200184 if (unlikely(bio->bi_status))
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800185 f2fs_stop_checkpoint(sbi, true);
186 continue;
187 }
188
Eric Biggersd2d07272019-05-20 09:29:39 -0700189 fscrypt_finalize_bounce_page(&page);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700190
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200191 if (unlikely(bio->bi_status)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700192 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kimb1ca3212017-12-31 16:26:38 -0800193 if (type == F2FS_WB_CP_DATA)
194 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900195 }
Yunlei He7dff55d2018-01-11 14:19:32 +0800196
197 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
198 page->index != nid_of_node(page));
199
Chao Yu36951b32016-11-16 10:41:20 +0800200 dec_page_count(sbi, type);
Chao Yu50fa53e2018-08-02 23:03:19 +0800201 if (f2fs_in_warm_node_list(sbi, page))
202 f2fs_del_fsync_node_entry(sbi, page);
Chao Yu36951b32016-11-16 10:41:20 +0800203 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900204 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800205 }
Chao Yu36951b32016-11-16 10:41:20 +0800206 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700207 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900208 wake_up(&sbi->cp_wait);
209
210 bio_put(bio);
211}
212
Gu Zheng940a6d32013-12-20 17:39:59 +0800213/*
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700214 * Return true, if pre_bio's bdev is same as its target device.
215 */
216struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
217 block_t blk_addr, struct bio *bio)
218{
219 struct block_device *bdev = sbi->sb->s_bdev;
220 int i;
221
Damien Le Moal09168782019-03-16 09:13:06 +0900222 if (f2fs_is_multi_device(sbi)) {
223 for (i = 0; i < sbi->s_ndevs; i++) {
224 if (FDEV(i).start_blk <= blk_addr &&
225 FDEV(i).end_blk >= blk_addr) {
226 blk_addr -= FDEV(i).start_blk;
227 bdev = FDEV(i).bdev;
228 break;
229 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700230 }
231 }
232 if (bio) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200233 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700234 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
235 }
236 return bdev;
237}
238
239int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
240{
241 int i;
242
Damien Le Moal09168782019-03-16 09:13:06 +0900243 if (!f2fs_is_multi_device(sbi))
244 return 0;
245
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700246 for (i = 0; i < sbi->s_ndevs; i++)
247 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
248 return i;
249 return 0;
250}
251
252static bool __same_bdev(struct f2fs_sb_info *sbi,
253 block_t blk_addr, struct bio *bio)
254{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200255 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
256 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700257}
258
259/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800260 * Low-level block read/write IO operations.
261 */
262static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
Yufen Yu578c6472018-01-09 19:33:39 +0800263 struct writeback_control *wbc,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900264 int npages, bool is_read,
265 enum page_type type, enum temp_type temp)
Gu Zheng940a6d32013-12-20 17:39:59 +0800266{
267 struct bio *bio;
268
Chao Yud62fe972017-10-28 16:52:31 +0800269 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800270
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700271 f2fs_target_device(sbi, blk_addr, bio);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900272 if (is_read) {
273 bio->bi_end_io = f2fs_read_end_io;
274 bio->bi_private = NULL;
275 } else {
276 bio->bi_end_io = f2fs_write_end_io;
277 bio->bi_private = sbi;
Chao Yu4d57b862018-05-30 00:20:41 +0800278 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, type, temp);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900279 }
Yufen Yu578c6472018-01-09 19:33:39 +0800280 if (wbc)
281 wbc_init_bio(wbc, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800282
283 return bio;
284}
285
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700286static inline void __submit_bio(struct f2fs_sb_info *sbi,
287 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700288{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700289 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800290 unsigned int start;
291
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800292 if (type != DATA && type != NODE)
293 goto submit_io;
294
Yunlong Song66415ce2018-07-12 23:09:28 +0800295 if (test_opt(sbi, LFS) && current->plug)
Tiezhu Yang3bb09a02018-02-06 08:21:45 +0800296 blk_finish_plug(current->plug);
297
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800298 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
299 start %= F2FS_IO_SIZE(sbi);
300
301 if (start == 0)
302 goto submit_io;
303
304 /* fill dummy pages */
305 for (; start < F2FS_IO_SIZE(sbi); start++) {
306 struct page *page =
307 mempool_alloc(sbi->write_io_dummy,
Gao Xiangbc73a4b2019-02-19 10:31:52 +0800308 GFP_NOIO | __GFP_NOFAIL);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800309 f2fs_bug_on(sbi, !page);
310
Gao Xiangbc73a4b2019-02-19 10:31:52 +0800311 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800312 SetPagePrivate(page);
313 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
314 lock_page(page);
315 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
316 f2fs_bug_on(sbi, 1);
317 }
318 /*
319 * In the NODE case, we lose next block address chain. So, we
320 * need to do checkpoint in f2fs_sync_file.
321 */
322 if (type == NODE)
323 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700324 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800325submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800326 if (is_read_io(bio_op(bio)))
327 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
328 else
329 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500330 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700331}
332
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900333static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900334{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900335 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900336
337 if (!io->bio)
338 return;
339
Mike Christie04d328d2016-06-05 14:31:55 -0500340 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
341
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800342 if (is_read_io(fio->op))
343 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
344 else
345 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
346
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700347 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900348 io->bio = NULL;
349}
350
Chao Yu8648de22019-02-19 16:15:29 +0800351static bool __has_merged_page(struct bio *bio, struct inode *inode,
Chao Yubab475c2018-09-27 23:41:16 +0800352 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800353{
Chao Yu0fd785e2016-01-18 18:24:59 +0800354 struct bio_vec *bvec;
355 struct page *target;
Ming Lei6dc4f102019-02-15 19:13:19 +0800356 struct bvec_iter_all iter_all;
Chao Yu0fd785e2016-01-18 18:24:59 +0800357
Chao Yu8648de22019-02-19 16:15:29 +0800358 if (!bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800359 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800360
Chao Yubab475c2018-09-27 23:41:16 +0800361 if (!inode && !page && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800362 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800363
Chao Yu8648de22019-02-19 16:15:29 +0800364 bio_for_each_segment_all(bvec, bio, iter_all) {
Chao Yu0fd785e2016-01-18 18:24:59 +0800365
Eric Biggersd2d07272019-05-20 09:29:39 -0700366 target = bvec->bv_page;
367 if (fscrypt_is_bounce_page(target))
368 target = fscrypt_pagecache_page(target);
Chao Yu0fd785e2016-01-18 18:24:59 +0800369
Chao Yu0c3a5792016-01-18 18:28:11 +0800370 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800371 return true;
Chao Yubab475c2018-09-27 23:41:16 +0800372 if (page && page == target)
373 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800374 if (ino && ino == ino_of_node(target))
375 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800376 }
377
Chao Yu0fd785e2016-01-18 18:24:59 +0800378 return false;
379}
380
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700381static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700382 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900383{
384 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700385 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900386
Chao Yudf0f8dc2014-03-22 14:57:23 +0800387 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900388
389 /* change META to META_FLUSH in the checkpoint procedure */
390 if (type >= META_FLUSH) {
391 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500392 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200393 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600394 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800395 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900396 }
397 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800398 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900399}
400
Jaegeuk Kima912b542017-05-10 11:18:25 -0700401static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800402 struct inode *inode, struct page *page,
403 nid_t ino, enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800404{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700405 enum temp_type temp;
Yunlong Song1e771e82018-11-13 11:57:32 +0800406 bool ret = true;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700407
408 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
Yunlong Song1e771e82018-11-13 11:57:32 +0800409 if (!force) {
410 enum page_type btype = PAGE_TYPE_OF_BIO(type);
411 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700412
Yunlong Song1e771e82018-11-13 11:57:32 +0800413 down_read(&io->io_rwsem);
Chao Yu8648de22019-02-19 16:15:29 +0800414 ret = __has_merged_page(io->bio, inode, page, ino);
Yunlong Song1e771e82018-11-13 11:57:32 +0800415 up_read(&io->io_rwsem);
416 }
417 if (ret)
418 __f2fs_submit_merged_write(sbi, type, temp);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700419
420 /* TODO: use HOT temp only for meta pages now. */
421 if (type >= META)
422 break;
423 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800424}
425
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700426void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800427{
Hariprasad Kelamadcc00f2019-04-06 16:29:36 +0530428 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900429}
430
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700431void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800432 struct inode *inode, struct page *page,
433 nid_t ino, enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900434{
Chao Yubab475c2018-09-27 23:41:16 +0800435 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900436}
437
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700438void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800439{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700440 f2fs_submit_merged_write(sbi, DATA);
441 f2fs_submit_merged_write(sbi, NODE);
442 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800443}
444
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900445/*
446 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300447 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900448 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700449int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900450{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900451 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700452 struct page *page = fio->encrypted_page ?
453 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900454
Chao Yuc9b60782018-08-01 19:13:44 +0800455 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800456 fio->is_por ? META_POR : (__is_meta_io(fio) ?
457 META_GENERIC : DATA_GENERIC_ENHANCE)))
Chao Yu10f966b2019-06-20 11:36:14 +0800458 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +0800459
Chao Yu2ace38e2014-12-24 16:08:14 +0800460 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700461 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900462
463 /* Allocate a new bio */
Yufen Yu578c6472018-01-09 19:33:39 +0800464 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900465 1, is_read_io(fio->op), fio->type, fio->temp);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900466
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300467 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900468 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900469 return -EFAULT;
470 }
Chao Yu78efac52018-10-22 23:24:28 +0800471
472 if (fio->io_wbc && !is_read_io(fio->op))
Tejun Heo34e51a52019-06-27 13:39:49 -0700473 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu78efac52018-10-22 23:24:28 +0800474
Mike Christie04d328d2016-06-05 14:31:55 -0500475 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900476
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700477 inc_page_count(fio->sbi, is_read_io(fio->op) ?
478 __read_io_type(page): WB_DATA_TYPE(fio->page));
Chao Yu4c58ed02018-10-22 09:12:51 +0800479
480 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900481 return 0;
482}
483
Chao Yu8648de22019-02-19 16:15:29 +0800484int f2fs_merge_page_bio(struct f2fs_io_info *fio)
485{
486 struct bio *bio = *fio->bio;
487 struct page *page = fio->encrypted_page ?
488 fio->encrypted_page : fio->page;
489
490 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
491 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
Chao Yu10f966b2019-06-20 11:36:14 +0800492 return -EFSCORRUPTED;
Chao Yu8648de22019-02-19 16:15:29 +0800493
494 trace_f2fs_submit_page_bio(page, fio);
495 f2fs_trace_ios(fio, 0);
496
497 if (bio && (*fio->last_block + 1 != fio->new_blkaddr ||
498 !__same_bdev(fio->sbi, fio->new_blkaddr, bio))) {
499 __submit_bio(fio->sbi, bio, fio->type);
500 bio = NULL;
501 }
502alloc_new:
503 if (!bio) {
504 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
505 BIO_MAX_PAGES, false, fio->type, fio->temp);
506 bio_set_op_attrs(bio, fio->op, fio->op_flags);
507 }
508
509 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
510 __submit_bio(fio->sbi, bio, fio->type);
511 bio = NULL;
512 goto alloc_new;
513 }
514
515 if (fio->io_wbc)
Linus Torvalds9637d512019-07-15 21:20:52 -0700516 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu8648de22019-02-19 16:15:29 +0800517
518 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
519
520 *fio->last_block = fio->new_blkaddr;
521 *fio->bio = bio;
522
523 return 0;
524}
525
526static void f2fs_submit_ipu_bio(struct f2fs_sb_info *sbi, struct bio **bio,
527 struct page *page)
528{
529 if (!bio)
530 return;
531
532 if (!__has_merged_page(*bio, NULL, page, 0))
533 return;
534
535 __submit_bio(sbi, *bio, DATA);
536 *bio = NULL;
537}
538
Chao Yufe16efe2018-05-28 23:47:18 +0800539void f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900540{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700541 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900542 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700543 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700544 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900545
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700546 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900547
Chao Yufb830fc2017-05-19 23:37:01 +0800548 down_write(&io->io_rwsem);
549next:
550 if (fio->in_list) {
551 spin_lock(&io->io_lock);
552 if (list_empty(&io->io_list)) {
553 spin_unlock(&io->io_lock);
Chao Yufe16efe2018-05-28 23:47:18 +0800554 goto out;
Chao Yufb830fc2017-05-19 23:37:01 +0800555 }
556 fio = list_first_entry(&io->io_list,
557 struct f2fs_io_info, list);
558 list_del(&fio->list);
559 spin_unlock(&io->io_lock);
560 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900561
Chao Yu93770ab2019-04-15 15:26:32 +0800562 verify_fio_blkaddr(fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900563
Chao Yu36951b32016-11-16 10:41:20 +0800564 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
565
Thomas Meyerebf7c522017-10-07 16:02:21 +0200566 /* set submitted = true as a return value */
567 fio->submitted = true;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800568
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700569 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900570
Chao Yu7a9d7542016-02-22 18:36:38 +0800571 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700572 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
573 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900574 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900575alloc_new:
576 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800577 if ((fio->type == DATA || fio->type == NODE) &&
578 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700579 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Chao Yufe16efe2018-05-28 23:47:18 +0800580 fio->retry = true;
581 goto skip;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800582 }
Yufen Yu578c6472018-01-09 19:33:39 +0800583 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900584 BIO_MAX_PAGES, false,
585 fio->type, fio->temp);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900586 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900587 }
588
Jaegeuk Kima912b542017-05-10 11:18:25 -0700589 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900590 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900591 goto alloc_new;
592 }
593
Yufen Yu578c6472018-01-09 19:33:39 +0800594 if (fio->io_wbc)
Tejun Heo34e51a52019-06-27 13:39:49 -0700595 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
Yufen Yu578c6472018-01-09 19:33:39 +0800596
Chao Yu7a9d7542016-02-22 18:36:38 +0800597 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700598 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800599
600 trace_f2fs_submit_page_write(fio->page, fio);
Chao Yufe16efe2018-05-28 23:47:18 +0800601skip:
Chao Yufb830fc2017-05-19 23:37:01 +0800602 if (fio->in_list)
603 goto next;
Chao Yufe16efe2018-05-28 23:47:18 +0800604out:
Daniel Rosenberg43549942018-08-20 19:21:43 -0700605 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
606 f2fs_is_checkpoint_ready(sbi))
Jaegeuk Kim5ce80582018-09-06 11:40:12 -0700607 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800608 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900609}
610
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700611static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
Jaegeuk Kime2e59412018-06-21 11:29:43 -0700612 unsigned nr_pages, unsigned op_flag)
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700613{
614 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700615 struct bio *bio;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700616 struct bio_post_read_ctx *ctx;
617 unsigned int post_read_steps = 0;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700618
Eric Biggers6dbb1792018-04-18 11:09:48 -0700619 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
620 if (!bio)
621 return ERR_PTR(-ENOMEM);
622 f2fs_target_device(sbi, blkaddr, bio);
623 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kime2e59412018-06-21 11:29:43 -0700624 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700625
626 if (f2fs_encrypted_file(inode))
627 post_read_steps |= 1 << STEP_DECRYPT;
628 if (post_read_steps) {
629 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
630 if (!ctx) {
631 bio_put(bio);
632 return ERR_PTR(-ENOMEM);
633 }
634 ctx->bio = bio;
635 ctx->enabled_steps = post_read_steps;
636 bio->bi_private = ctx;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700637 }
638
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700639 return bio;
640}
641
642/* This can handle encryption stuffs */
643static int f2fs_submit_page_read(struct inode *inode, struct page *page,
644 block_t blkaddr)
645{
Chao Yu93770ab2019-04-15 15:26:32 +0800646 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
647 struct bio *bio;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700648
Chao Yu93770ab2019-04-15 15:26:32 +0800649 bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700650 if (IS_ERR(bio))
651 return PTR_ERR(bio);
652
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -0700653 /* wait for GCed page writeback via META_MAPPING */
654 f2fs_wait_on_block_writeback(inode, blkaddr);
655
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700656 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
657 bio_put(bio);
658 return -EFAULT;
659 }
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -0700660 ClearPageError(page);
Chao Yu93770ab2019-04-15 15:26:32 +0800661 inc_page_count(sbi, F2FS_RD_DATA);
662 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700663 return 0;
664}
665
Chao Yu46008c62016-05-09 19:56:30 +0800666static void __set_data_blkaddr(struct dnode_of_data *dn)
667{
668 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
669 __le32 *addr_array;
Chao Yu7a2af762017-07-19 00:19:06 +0800670 int base = 0;
671
672 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
673 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800674
675 /* Get physical address of data block */
676 addr_array = blkaddr_in_node(rn);
Chao Yu7a2af762017-07-19 00:19:06 +0800677 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800678}
679
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900680/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900681 * Lock ordering for the change of data block address:
682 * ->data_page
683 * ->node_page
684 * update block addresses in the node page
685 */
Chao Yu4d57b862018-05-30 00:20:41 +0800686void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900687{
Chao Yubae0ee72018-12-25 17:43:42 +0800688 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +0800689 __set_data_blkaddr(dn);
690 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800691 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900692}
693
Chao Yuf28b3432016-02-24 17:16:47 +0800694void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
695{
696 dn->data_blkaddr = blkaddr;
Chao Yu4d57b862018-05-30 00:20:41 +0800697 f2fs_set_data_blkaddr(dn);
Chao Yuf28b3432016-02-24 17:16:47 +0800698 f2fs_update_extent_cache(dn);
699}
700
Chao Yu46008c62016-05-09 19:56:30 +0800701/* dn->ofs_in_node will be returned with up-to-date last block pointer */
Chao Yu4d57b862018-05-30 00:20:41 +0800702int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900703{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700704 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800705 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900706
Chao Yu46008c62016-05-09 19:56:30 +0800707 if (!count)
708 return 0;
709
Jaegeuk Kim91942322016-05-20 10:13:22 -0700710 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900711 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +0800712 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
713 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900714
Chao Yu46008c62016-05-09 19:56:30 +0800715 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
716 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900717
Chao Yubae0ee72018-12-25 17:43:42 +0800718 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +0800719
720 for (; count > 0; dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800721 block_t blkaddr = datablock_addr(dn->inode,
722 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800723 if (blkaddr == NULL_ADDR) {
724 dn->data_blkaddr = NEW_ADDR;
725 __set_data_blkaddr(dn);
726 count--;
727 }
728 }
729
730 if (set_page_dirty(dn->node_page))
731 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900732 return 0;
733}
734
Chao Yu46008c62016-05-09 19:56:30 +0800735/* Should keep dn->ofs_in_node unchanged */
Chao Yu4d57b862018-05-30 00:20:41 +0800736int f2fs_reserve_new_block(struct dnode_of_data *dn)
Chao Yu46008c62016-05-09 19:56:30 +0800737{
738 unsigned int ofs_in_node = dn->ofs_in_node;
739 int ret;
740
Chao Yu4d57b862018-05-30 00:20:41 +0800741 ret = f2fs_reserve_new_blocks(dn, 1);
Chao Yu46008c62016-05-09 19:56:30 +0800742 dn->ofs_in_node = ofs_in_node;
743 return ret;
744}
745
Huajun Lib6009652013-11-10 23:13:18 +0800746int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
747{
748 bool need_put = dn->inode_page ? false : true;
749 int err;
750
Chao Yu4d57b862018-05-30 00:20:41 +0800751 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
Huajun Lib6009652013-11-10 23:13:18 +0800752 if (err)
753 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900754
Huajun Lib6009652013-11-10 23:13:18 +0800755 if (dn->data_blkaddr == NULL_ADDR)
Chao Yu4d57b862018-05-30 00:20:41 +0800756 err = f2fs_reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900757 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800758 f2fs_put_dnode(dn);
759 return err;
760}
761
Fan Li759af1c2015-08-05 15:52:16 +0800762int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900763{
Hou Pengyange15882b2017-02-23 09:18:05 +0000764 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800765 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800766
Fan Li759af1c2015-08-05 15:52:16 +0800767 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
768 dn->data_blkaddr = ei.blk + index - ei.fofs;
769 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800770 }
771
Fan Li759af1c2015-08-05 15:52:16 +0800772 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900773}
774
Chao Yu4d57b862018-05-30 00:20:41 +0800775struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500776 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900777{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900778 struct address_space *mapping = inode->i_mapping;
779 struct dnode_of_data dn;
780 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +0000781 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900782 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700783
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700784 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900785 if (!page)
786 return ERR_PTR(-ENOMEM);
787
Chao Yucb3bc9e2015-02-05 18:03:40 +0800788 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
789 dn.data_blkaddr = ei.blk + index - ei.fofs;
Chao Yu93770ab2019-04-15 15:26:32 +0800790 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
791 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +0800792 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +0800793 goto put_err;
794 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800795 goto got_it;
796 }
797
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900798 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800799 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700800 if (err)
801 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900802 f2fs_put_dnode(&dn);
803
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900804 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700805 err = -ENOENT;
806 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900807 }
Chao Yu93770ab2019-04-15 15:26:32 +0800808 if (dn.data_blkaddr != NEW_ADDR &&
809 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
810 dn.data_blkaddr,
811 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +0800812 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +0800813 goto put_err;
814 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800815got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700816 if (PageUptodate(page)) {
817 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900818 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700819 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900820
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900821 /*
822 * A new dentry page is allocated but not able to be written, since its
823 * new inode page couldn't be allocated due to -ENOSPC.
824 * In such the case, its blkaddr can be remained as NEW_ADDR.
Chao Yu4d57b862018-05-30 00:20:41 +0800825 * see, f2fs_add_link -> f2fs_get_new_data_page ->
826 * f2fs_init_inode_metadata.
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900827 */
828 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300829 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700830 if (!PageUptodate(page))
831 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700832 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900833 return page;
834 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900835
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700836 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900837 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700838 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700839 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700840
841put_err:
842 f2fs_put_page(page, 1);
843 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700844}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900845
Chao Yu4d57b862018-05-30 00:20:41 +0800846struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700847{
848 struct address_space *mapping = inode->i_mapping;
849 struct page *page;
850
851 page = find_get_page(mapping, index);
852 if (page && PageUptodate(page))
853 return page;
854 f2fs_put_page(page, 0);
855
Chao Yu4d57b862018-05-30 00:20:41 +0800856 page = f2fs_get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700857 if (IS_ERR(page))
858 return page;
859
860 if (PageUptodate(page))
861 return page;
862
863 wait_on_page_locked(page);
864 if (unlikely(!PageUptodate(page))) {
865 f2fs_put_page(page, 0);
866 return ERR_PTR(-EIO);
867 }
868 return page;
869}
870
871/*
872 * If it tries to access a hole, return an error.
873 * Because, the callers, functions in dir.c and GC, should be able to know
874 * whether this page exists or not.
875 */
Chao Yu4d57b862018-05-30 00:20:41 +0800876struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700877 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700878{
879 struct address_space *mapping = inode->i_mapping;
880 struct page *page;
881repeat:
Chao Yu4d57b862018-05-30 00:20:41 +0800882 page = f2fs_get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700883 if (IS_ERR(page))
884 return page;
885
886 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900887 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900888 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900889 f2fs_put_page(page, 1);
890 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900891 }
Chao Yu1563ac72016-07-03 22:05:12 +0800892 if (unlikely(!PageUptodate(page))) {
893 f2fs_put_page(page, 1);
894 return ERR_PTR(-EIO);
895 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900896 return page;
897}
898
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900899/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900900 * Caller ensures that this data page is never allocated.
901 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900902 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800903 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
904 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800905 * Note that, ipage is set only by make_empty_dir, and if any error occur,
906 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900907 */
Chao Yu4d57b862018-05-30 00:20:41 +0800908struct page *f2fs_get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900909 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900910{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900911 struct address_space *mapping = inode->i_mapping;
912 struct page *page;
913 struct dnode_of_data dn;
914 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800915
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700916 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800917 if (!page) {
918 /*
919 * before exiting, we should make sure ipage will be released
920 * if any error occur.
921 */
922 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700923 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800924 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900925
Jaegeuk Kima8865372013-12-27 17:04:17 +0900926 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800927 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700928 if (err) {
929 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900930 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900931 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700932 if (!ipage)
933 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900934
935 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700936 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900937
938 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300939 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700940 if (!PageUptodate(page))
941 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900942 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700943 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900944
Jaegeuk Kim76121182016-01-01 22:03:47 -0800945 /* if ipage exists, blkaddr should be NEW_ADDR */
946 f2fs_bug_on(F2FS_I_SB(inode), ipage);
Chao Yu4d57b862018-05-30 00:20:41 +0800947 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700948 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800949 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900950 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700951got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800952 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700953 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700954 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900955 return page;
956}
957
Hyunchul Leed5097be2017-11-28 09:23:00 +0900958static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900959{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700960 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900961 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900962 struct node_info ni;
Chao Yu6aa58d82018-08-14 22:37:25 +0800963 block_t old_blkaddr;
Chao Yu46008c62016-05-09 19:56:30 +0800964 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +0800965 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900966
Jaegeuk Kim91942322016-05-20 10:13:22 -0700967 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900968 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800969
Chao Yu77357302018-07-17 00:02:17 +0800970 err = f2fs_get_node_info(sbi, dn->nid, &ni);
971 if (err)
972 return err;
973
Chao Yu7a2af762017-07-19 00:19:06 +0800974 dn->data_blkaddr = datablock_addr(dn->inode,
975 dn->node_page, dn->ofs_in_node);
Chao Yuf847c692018-09-27 18:34:52 +0800976 if (dn->data_blkaddr != NULL_ADDR)
Chao Yudf6136e2015-03-23 10:33:37 +0800977 goto alloc;
978
Chao Yu0abd6752017-07-09 00:13:07 +0800979 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
980 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900981
Chao Yudf6136e2015-03-23 10:33:37 +0800982alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900983 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Chao Yu6aa58d82018-08-14 22:37:25 +0800984 old_blkaddr = dn->data_blkaddr;
985 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
Hyunchul Leed5097be2017-11-28 09:23:00 +0900986 &sum, seg_type, NULL, false);
Chao Yu6aa58d82018-08-14 22:37:25 +0800987 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
988 invalidate_mapping_pages(META_MAPPING(sbi),
989 old_blkaddr, old_blkaddr);
Chao Yu4d57b862018-05-30 00:20:41 +0800990 f2fs_set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900991
Jaegeuk Kim0a4daae2018-09-19 15:28:40 -0700992 /*
993 * i_size will be updated by direct_IO. Otherwise, we'll get stale
994 * data from unwritten block via dio_read.
995 */
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900996 return 0;
997}
998
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800999int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001000{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001001 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +08001002 struct f2fs_map_blocks map;
Chao Yud6d478a12018-01-03 17:30:19 +08001003 int flag;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001004 int err = 0;
Chao Yud6d478a12018-01-03 17:30:19 +08001005 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001006
Weichao Guo71ad6822017-09-29 22:43:23 +08001007 /* convert inline data for Direct I/O*/
Chao Yud6d478a12018-01-03 17:30:19 +08001008 if (direct_io) {
Weichao Guo71ad6822017-09-29 22:43:23 +08001009 err = f2fs_convert_inline_inode(inode);
1010 if (err)
1011 return err;
1012 }
1013
Chao Yuf9d6d052018-11-13 14:33:45 +08001014 if (direct_io && allow_outplace_dio(inode, iocb, from))
1015 return 0;
1016
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08001017 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
1018 return 0;
1019
Jaegeuk Kim0080c502016-05-07 08:52:57 -07001020 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +08001021 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1022 if (map.m_len > map.m_lblk)
1023 map.m_len -= map.m_lblk;
1024 else
1025 map.m_len = 0;
1026
Chao Yuda859852016-01-26 15:42:58 +08001027 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08001028 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001029 map.m_seg_type = NO_CHECK_TYPE;
Chao Yuf9d6d052018-11-13 14:33:45 +08001030 map.m_may_create = true;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001031
Chao Yud6d478a12018-01-03 17:30:19 +08001032 if (direct_io) {
Chao Yu4d57b862018-05-30 00:20:41 +08001033 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
Chao Yuf847c692018-09-27 18:34:52 +08001034 flag = f2fs_force_buffered_io(inode, iocb, from) ?
Chao Yud6d478a12018-01-03 17:30:19 +08001035 F2FS_GET_BLOCK_PRE_AIO :
1036 F2FS_GET_BLOCK_PRE_DIO;
1037 goto map_blocks;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001038 }
Chao Yuf2470372017-07-19 00:19:05 +08001039 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001040 err = f2fs_convert_inline_inode(inode);
1041 if (err)
1042 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001043 }
Chao Yud6d478a12018-01-03 17:30:19 +08001044 if (f2fs_has_inline_data(inode))
Sheng Yong250066452017-11-22 18:23:39 +08001045 return err;
Chao Yud6d478a12018-01-03 17:30:19 +08001046
1047 flag = F2FS_GET_BLOCK_PRE_AIO;
1048
1049map_blocks:
1050 err = f2fs_map_blocks(inode, &map, 1, flag);
1051 if (map.m_len > 0 && err == -ENOSPC) {
1052 if (!direct_io)
1053 set_inode_flag(inode, FI_NO_PREALLOC);
1054 err = 0;
Sheng Yong250066452017-11-22 18:23:39 +08001055 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001056 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001057}
1058
Chao Yu39a86952018-09-27 18:33:18 +08001059void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
Yunlei He59c90812017-03-13 20:22:18 +08001060{
1061 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1062 if (lock)
1063 down_read(&sbi->node_change);
1064 else
1065 up_read(&sbi->node_change);
1066 } else {
1067 if (lock)
1068 f2fs_lock_op(sbi);
1069 else
1070 f2fs_unlock_op(sbi);
1071 }
1072}
1073
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001074/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001075 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1076 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +08001077 * If original data blocks are allocated, then give them to blockdev.
1078 * Otherwise,
1079 * a. preallocate requested block addresses
1080 * b. do not use extent cache for better performance
1081 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001082 */
Chao Yud323d002015-10-27 09:53:45 +08001083int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +08001084 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001085{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001086 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001087 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +08001088 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf9d6d052018-11-13 14:33:45 +08001089 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +08001090 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001091 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +08001092 unsigned int ofs_in_node, last_ofs_in_node;
1093 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +00001094 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +08001095 block_t blkaddr;
Chao Yuc4020b22018-01-11 14:42:30 +08001096 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001097
Chao Yudfd02e42016-08-20 15:12:01 +08001098 if (!maxblocks)
1099 return 0;
1100
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001101 map->m_len = 0;
1102 map->m_flags = 0;
1103
1104 /* it only supports block size == page size */
1105 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +08001106 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001107
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001108 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jia Zhuf4f0b672018-11-20 04:29:35 +08001109 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1110 map->m_may_create)
1111 goto next_dnode;
1112
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001113 map->m_pblk = ei.blk + pgofs - ei.fofs;
1114 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1115 map->m_flags = F2FS_MAP_MAPPED;
Chao Yuc4020b22018-01-11 14:42:30 +08001116 if (map->m_next_extent)
1117 *map->m_next_extent = pgofs + map->m_len;
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301118
1119 /* for hardware encryption, but to avoid potential issue in future */
1120 if (flag == F2FS_GET_BLOCK_DIO)
1121 f2fs_wait_on_block_writeback_range(inode,
1122 map->m_pblk, map->m_len);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001123 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001124 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001125
Chao Yu4fe71e82016-01-26 15:37:38 +08001126next_dnode:
Chao Yuf9d6d052018-11-13 14:33:45 +08001127 if (map->m_may_create)
Yunlei He59c90812017-03-13 20:22:18 +08001128 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001129
1130 /* When reading holes, we need its node page */
1131 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001132 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001133 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +08001134 if (flag == F2FS_GET_BLOCK_BMAP)
1135 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +08001136 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001137 err = 0;
Chao Yuda859852016-01-26 15:42:58 +08001138 if (map->m_next_pgofs)
1139 *map->m_next_pgofs =
Chao Yu4d57b862018-05-30 00:20:41 +08001140 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuc4020b22018-01-11 14:42:30 +08001141 if (map->m_next_extent)
1142 *map->m_next_extent =
Chao Yu4d57b862018-05-30 00:20:41 +08001143 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +08001144 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001145 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001146 }
Chao Yu973163f2015-09-18 16:51:51 +08001147
Chao Yuc4020b22018-01-11 14:42:30 +08001148 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +08001149 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +01001150 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +08001151 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001152
Chao Yu4fe71e82016-01-26 15:37:38 +08001153next_block:
Chao Yu7a2af762017-07-19 00:19:06 +08001154 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +08001155
Chao Yuc9b60782018-08-01 19:13:44 +08001156 if (__is_valid_data_blkaddr(blkaddr) &&
Chao Yu93770ab2019-04-15 15:26:32 +08001157 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001158 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08001159 goto sync_out;
1160 }
1161
Chao Yu93770ab2019-04-15 15:26:32 +08001162 if (__is_valid_data_blkaddr(blkaddr)) {
Chao Yuf847c692018-09-27 18:34:52 +08001163 /* use out-place-update for driect IO under LFS mode */
Chao Yuf9d6d052018-11-13 14:33:45 +08001164 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1165 map->m_may_create) {
Chao Yuf847c692018-09-27 18:34:52 +08001166 err = __allocate_data_block(&dn, map->m_seg_type);
Jia Zhu73c0a922018-11-27 02:32:32 +08001167 if (!err) {
1168 blkaddr = dn.data_blkaddr;
Chao Yuf847c692018-09-27 18:34:52 +08001169 set_inode_flag(inode, FI_APPEND_WRITE);
Jia Zhu73c0a922018-11-27 02:32:32 +08001170 }
Chao Yuf847c692018-09-27 18:34:52 +08001171 }
1172 } else {
Fan Li7df3a432015-12-17 13:20:59 +08001173 if (create) {
1174 if (unlikely(f2fs_cp_error(sbi))) {
1175 err = -EIO;
1176 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +08001177 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001178 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +08001179 if (blkaddr == NULL_ADDR) {
1180 prealloc++;
1181 last_ofs_in_node = dn.ofs_in_node;
1182 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001183 } else {
Jaegeuk Kim0a4daae2018-09-19 15:28:40 -07001184 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1185 flag != F2FS_GET_BLOCK_DIO);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001186 err = __allocate_data_block(&dn,
1187 map->m_seg_type);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001188 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001189 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001190 }
Fan Li7df3a432015-12-17 13:20:59 +08001191 if (err)
1192 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +08001193 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001194 blkaddr = dn.data_blkaddr;
1195 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001196 if (flag == F2FS_GET_BLOCK_BMAP) {
1197 map->m_pblk = 0;
1198 goto sync_out;
1199 }
Chao Yuc4020b22018-01-11 14:42:30 +08001200 if (flag == F2FS_GET_BLOCK_PRECACHE)
1201 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001202 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1203 blkaddr == NULL_ADDR) {
1204 if (map->m_next_pgofs)
1205 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001206 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001207 }
Chao Yuf3d98e72018-01-10 18:18:52 +08001208 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1209 /* for defragment case */
1210 if (map->m_next_pgofs)
1211 *map->m_next_pgofs = pgofs + 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001212 goto sync_out;
Chao Yuf3d98e72018-01-10 18:18:52 +08001213 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001214 }
1215 }
Fan Li7df3a432015-12-17 13:20:59 +08001216
Chao Yu46008c62016-05-09 19:56:30 +08001217 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1218 goto skip;
1219
Chao Yu4fe71e82016-01-26 15:37:38 +08001220 if (map->m_len == 0) {
1221 /* preallocated unwritten block should be mapped for fiemap. */
1222 if (blkaddr == NEW_ADDR)
1223 map->m_flags |= F2FS_MAP_UNWRITTEN;
1224 map->m_flags |= F2FS_MAP_MAPPED;
1225
1226 map->m_pblk = blkaddr;
1227 map->m_len = 1;
1228 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001229 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001230 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001231 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001232 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001233 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001234 } else {
1235 goto sync_out;
1236 }
1237
Chao Yu46008c62016-05-09 19:56:30 +08001238skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001239 dn.ofs_in_node++;
1240 pgofs++;
1241
Chao Yu46008c62016-05-09 19:56:30 +08001242 /* preallocate blocks in batch for one dnode page */
1243 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1244 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001245
Chao Yu46008c62016-05-09 19:56:30 +08001246 dn.ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001247 err = f2fs_reserve_new_blocks(&dn, prealloc);
Chao Yu46008c62016-05-09 19:56:30 +08001248 if (err)
1249 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001250
Chao Yu46008c62016-05-09 19:56:30 +08001251 map->m_len += dn.ofs_in_node - ofs_in_node;
1252 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1253 err = -ENOSPC;
1254 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001255 }
Chao Yu46008c62016-05-09 19:56:30 +08001256 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001257 }
1258
Chao Yu46008c62016-05-09 19:56:30 +08001259 if (pgofs >= end)
1260 goto sync_out;
1261 else if (dn.ofs_in_node < end_offset)
1262 goto next_block;
1263
Chao Yuc4020b22018-01-11 14:42:30 +08001264 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1265 if (map->m_flags & F2FS_MAP_MAPPED) {
1266 unsigned int ofs = start_pgofs - map->m_lblk;
1267
1268 f2fs_update_extent_cache_range(&dn,
1269 start_pgofs, map->m_pblk + ofs,
1270 map->m_len - ofs);
1271 }
1272 }
1273
Chao Yu46008c62016-05-09 19:56:30 +08001274 f2fs_put_dnode(&dn);
1275
Chao Yuf9d6d052018-11-13 14:33:45 +08001276 if (map->m_may_create) {
Yunlei He59c90812017-03-13 20:22:18 +08001277 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001278 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001279 }
Chao Yu46008c62016-05-09 19:56:30 +08001280 goto next_dnode;
1281
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001282sync_out:
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301283
1284 /* for hardware encryption, but to avoid potential issue in future */
1285 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1286 f2fs_wait_on_block_writeback_range(inode,
1287 map->m_pblk, map->m_len);
1288
Chao Yuc4020b22018-01-11 14:42:30 +08001289 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1290 if (map->m_flags & F2FS_MAP_MAPPED) {
1291 unsigned int ofs = start_pgofs - map->m_lblk;
1292
1293 f2fs_update_extent_cache_range(&dn,
1294 start_pgofs, map->m_pblk + ofs,
1295 map->m_len - ofs);
1296 }
1297 if (map->m_next_extent)
1298 *map->m_next_extent = pgofs + 1;
1299 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001300 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001301unlock_out:
Chao Yuf9d6d052018-11-13 14:33:45 +08001302 if (map->m_may_create) {
Yunlei He59c90812017-03-13 20:22:18 +08001303 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001304 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001305 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001306out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001307 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001308 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001309}
1310
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001311bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1312{
1313 struct f2fs_map_blocks map;
1314 block_t last_lblk;
1315 int err;
1316
1317 if (pos + len > i_size_read(inode))
1318 return false;
1319
1320 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1321 map.m_next_pgofs = NULL;
1322 map.m_next_extent = NULL;
1323 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08001324 map.m_may_create = false;
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001325 last_lblk = F2FS_BLK_ALIGN(pos + len);
1326
1327 while (map.m_lblk < last_lblk) {
1328 map.m_len = last_lblk - map.m_lblk;
1329 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1330 if (err || map.m_len == 0)
1331 return false;
1332 map.m_lblk += map.m_len;
1333 }
1334 return true;
1335}
1336
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001337static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001338 struct buffer_head *bh, int create, int flag,
Chao Yuf9d6d052018-11-13 14:33:45 +08001339 pgoff_t *next_pgofs, int seg_type, bool may_write)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001340{
1341 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001342 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001343
1344 map.m_lblk = iblock;
1345 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001346 map.m_next_pgofs = next_pgofs;
Chao Yuc4020b22018-01-11 14:42:30 +08001347 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001348 map.m_seg_type = seg_type;
Chao Yuf9d6d052018-11-13 14:33:45 +08001349 map.m_may_create = may_write;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001350
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001351 err = f2fs_map_blocks(inode, &map, create, flag);
1352 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001353 map_bh(bh, inode->i_sb, map.m_pblk);
1354 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001355 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001356 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001357 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001358}
1359
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001360static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001361 struct buffer_head *bh_result, int create, int flag,
1362 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001363{
Chao Yuda859852016-01-26 15:42:58 +08001364 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001365 flag, next_pgofs,
Chao Yuf9d6d052018-11-13 14:33:45 +08001366 NO_CHECK_TYPE, create);
1367}
1368
1369static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1370 struct buffer_head *bh_result, int create)
1371{
1372 return __get_data_block(inode, iblock, bh_result, create,
1373 F2FS_GET_BLOCK_DIO, NULL,
1374 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1375 true);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001376}
1377
Chao Yue2b4e2b2015-08-19 19:11:19 +08001378static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001379 struct buffer_head *bh_result, int create)
1380{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001381 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuf9d6d052018-11-13 14:33:45 +08001382 F2FS_GET_BLOCK_DIO, NULL,
1383 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1384 false);
Chao Yue2b4e2b2015-08-19 19:11:19 +08001385}
1386
1387static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1388 struct buffer_head *bh_result, int create)
1389{
Yunlei He179448b2015-12-28 21:48:32 +08001390 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001391 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001392 return -EFBIG;
1393
Chao Yue2b4e2b2015-08-19 19:11:19 +08001394 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001395 F2FS_GET_BLOCK_BMAP, NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001396 NO_CHECK_TYPE, create);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001397}
1398
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001399static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1400{
1401 return (offset >> inode->i_blkbits);
1402}
1403
1404static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1405{
1406 return (blk << inode->i_blkbits);
1407}
1408
Chao Yu442a9db2018-01-11 14:39:57 +08001409static int f2fs_xattr_fiemap(struct inode *inode,
1410 struct fiemap_extent_info *fieinfo)
1411{
1412 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1413 struct page *page;
1414 struct node_info ni;
1415 __u64 phys = 0, len;
1416 __u32 flags;
1417 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1418 int err = 0;
1419
1420 if (f2fs_has_inline_xattr(inode)) {
1421 int offset;
1422
1423 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1424 inode->i_ino, false);
1425 if (!page)
1426 return -ENOMEM;
1427
Chao Yu77357302018-07-17 00:02:17 +08001428 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1429 if (err) {
1430 f2fs_put_page(page, 1);
1431 return err;
1432 }
Chao Yu442a9db2018-01-11 14:39:57 +08001433
1434 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1435 offset = offsetof(struct f2fs_inode, i_addr) +
1436 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yub323fd22018-01-17 16:31:36 +08001437 get_inline_xattr_addrs(inode));
Chao Yu442a9db2018-01-11 14:39:57 +08001438
1439 phys += offset;
1440 len = inline_xattr_size(inode);
1441
1442 f2fs_put_page(page, 1);
1443
1444 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1445
1446 if (!xnid)
1447 flags |= FIEMAP_EXTENT_LAST;
1448
1449 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1450 if (err || err == 1)
1451 return err;
1452 }
1453
1454 if (xnid) {
1455 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1456 if (!page)
1457 return -ENOMEM;
1458
Chao Yu77357302018-07-17 00:02:17 +08001459 err = f2fs_get_node_info(sbi, xnid, &ni);
1460 if (err) {
1461 f2fs_put_page(page, 1);
1462 return err;
1463 }
Chao Yu442a9db2018-01-11 14:39:57 +08001464
1465 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1466 len = inode->i_sb->s_blocksize;
1467
1468 f2fs_put_page(page, 1);
1469
1470 flags = FIEMAP_EXTENT_LAST;
1471 }
1472
1473 if (phys)
1474 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1475
1476 return (err < 0 ? err : 0);
1477}
1478
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001479int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1480 u64 start, u64 len)
1481{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001482 struct buffer_head map_bh;
1483 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001484 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001485 u64 logical = 0, phys = 0, size = 0;
1486 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001487 int ret = 0;
1488
Chao Yuc4020b22018-01-11 14:42:30 +08001489 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1490 ret = f2fs_precache_extents(inode);
1491 if (ret)
1492 return ret;
1493 }
1494
Chao Yu442a9db2018-01-11 14:39:57 +08001495 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001496 if (ret)
1497 return ret;
1498
Chao Yuf1b43d42018-01-11 14:37:35 +08001499 inode_lock(inode);
1500
Chao Yu442a9db2018-01-11 14:39:57 +08001501 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1502 ret = f2fs_xattr_fiemap(inode, fieinfo);
1503 goto out;
1504 }
1505
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001506 if (f2fs_has_inline_data(inode)) {
1507 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1508 if (ret != -EAGAIN)
Chao Yuf1b43d42018-01-11 14:37:35 +08001509 goto out;
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001510 }
1511
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001512 if (logical_to_blk(inode, len) == 0)
1513 len = blk_to_logical(inode, 1);
1514
1515 start_blk = logical_to_blk(inode, start);
1516 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001517
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001518next:
1519 memset(&map_bh, 0, sizeof(struct buffer_head));
1520 map_bh.b_size = len;
1521
Chao Yue2b4e2b2015-08-19 19:11:19 +08001522 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001523 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001524 if (ret)
1525 goto out;
1526
1527 /* HOLE */
1528 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001529 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001530
1531 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1532 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001533 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001534
Fan Li9a950d52015-12-26 18:07:41 +08001535 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001536 }
Fan Li9a950d52015-12-26 18:07:41 +08001537
Chao Yuda5af122016-01-08 20:19:27 +08001538 if (size) {
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301539 if (IS_ENCRYPTED(inode))
Chao Yuda5af122016-01-08 20:19:27 +08001540 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1541
Fan Li9a950d52015-12-26 18:07:41 +08001542 ret = fiemap_fill_next_extent(fieinfo, logical,
1543 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001544 }
Fan Li9a950d52015-12-26 18:07:41 +08001545
1546 if (start_blk > last_blk || ret)
1547 goto out;
1548
1549 logical = blk_to_logical(inode, start_blk);
1550 phys = blk_to_logical(inode, map_bh.b_blocknr);
1551 size = map_bh.b_size;
1552 flags = 0;
1553 if (buffer_unwritten(&map_bh))
1554 flags = FIEMAP_EXTENT_UNWRITTEN;
1555
1556 start_blk += logical_to_blk(inode, size);
1557
1558prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001559 cond_resched();
1560 if (fatal_signal_pending(current))
1561 ret = -EINTR;
1562 else
1563 goto next;
1564out:
1565 if (ret == 1)
1566 ret = 0;
1567
Al Viro59551022016-01-22 15:40:57 -05001568 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001569 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001570}
1571
Chao Yu2df0ab02019-03-25 21:07:30 +08001572static int f2fs_read_single_page(struct inode *inode, struct page *page,
1573 unsigned nr_pages,
1574 struct f2fs_map_blocks *map,
1575 struct bio **bio_ret,
1576 sector_t *last_block_in_bio,
1577 bool is_readahead)
1578{
1579 struct bio *bio = *bio_ret;
1580 const unsigned blkbits = inode->i_blkbits;
1581 const unsigned blocksize = 1 << blkbits;
1582 sector_t block_in_file;
1583 sector_t last_block;
1584 sector_t last_block_in_file;
1585 sector_t block_nr;
1586 int ret = 0;
1587
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001588 block_in_file = (sector_t)page_index(page);
Chao Yu2df0ab02019-03-25 21:07:30 +08001589 last_block = block_in_file + nr_pages;
1590 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1591 blkbits;
1592 if (last_block > last_block_in_file)
1593 last_block = last_block_in_file;
1594
1595 /* just zeroing out page which is beyond EOF */
1596 if (block_in_file >= last_block)
1597 goto zero_out;
1598 /*
1599 * Map blocks using the previous result first.
1600 */
1601 if ((map->m_flags & F2FS_MAP_MAPPED) &&
1602 block_in_file > map->m_lblk &&
1603 block_in_file < (map->m_lblk + map->m_len))
1604 goto got_it;
1605
1606 /*
1607 * Then do more f2fs_map_blocks() calls until we are
1608 * done with this page.
1609 */
1610 map->m_lblk = block_in_file;
1611 map->m_len = last_block - block_in_file;
1612
1613 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
1614 if (ret)
1615 goto out;
1616got_it:
1617 if ((map->m_flags & F2FS_MAP_MAPPED)) {
1618 block_nr = map->m_pblk + block_in_file - map->m_lblk;
1619 SetPageMappedToDisk(page);
1620
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001621 if (!PageUptodate(page) && (!PageSwapCache(page) &&
1622 !cleancache_get_page(page))) {
Chao Yu2df0ab02019-03-25 21:07:30 +08001623 SetPageUptodate(page);
1624 goto confused;
1625 }
1626
1627 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
Chao Yu93770ab2019-04-15 15:26:32 +08001628 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001629 ret = -EFSCORRUPTED;
Chao Yu2df0ab02019-03-25 21:07:30 +08001630 goto out;
1631 }
1632 } else {
1633zero_out:
1634 zero_user_segment(page, 0, PAGE_SIZE);
1635 if (!PageUptodate(page))
1636 SetPageUptodate(page);
1637 unlock_page(page);
1638 goto out;
1639 }
1640
1641 /*
1642 * This page will go to BIO. Do we need to send this
1643 * BIO off first?
1644 */
1645 if (bio && (*last_block_in_bio != block_nr - 1 ||
1646 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
1647submit_and_realloc:
1648 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1649 bio = NULL;
1650 }
1651 if (bio == NULL) {
1652 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
1653 is_readahead ? REQ_RAHEAD : 0);
1654 if (IS_ERR(bio)) {
1655 ret = PTR_ERR(bio);
1656 bio = NULL;
1657 goto out;
1658 }
1659 }
1660
1661 /*
1662 * If the page is under writeback, we need to wait for
1663 * its completion to see the correct decrypted data.
1664 */
1665 f2fs_wait_on_block_writeback(inode, block_nr);
1666
1667 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1668 goto submit_and_realloc;
1669
1670 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
1671 ClearPageError(page);
1672 *last_block_in_bio = block_nr;
1673 goto out;
1674confused:
1675 if (bio) {
1676 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1677 bio = NULL;
1678 }
1679 unlock_page(page);
1680out:
1681 *bio_ret = bio;
1682 return ret;
1683}
1684
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001685/*
1686 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1687 * Major change was from block_size == page_size in f2fs by default.
Jaegeuk Kime2e59412018-06-21 11:29:43 -07001688 *
1689 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1690 * this function ever deviates from doing just read-ahead, it should either
1691 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1692 * from read-ahead.
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001693 */
1694static int f2fs_mpage_readpages(struct address_space *mapping,
1695 struct list_head *pages, struct page *page,
Jaegeuk Kime2e59412018-06-21 11:29:43 -07001696 unsigned nr_pages, bool is_readahead)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001697{
1698 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001699 sector_t last_block_in_bio = 0;
1700 struct inode *inode = mapping->host;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001701 struct f2fs_map_blocks map;
Chao Yu2df0ab02019-03-25 21:07:30 +08001702 int ret = 0;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001703
1704 map.m_pblk = 0;
1705 map.m_lblk = 0;
1706 map.m_len = 0;
1707 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001708 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08001709 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001710 map.m_seg_type = NO_CHECK_TYPE;
Chao Yuf9d6d052018-11-13 14:33:45 +08001711 map.m_may_create = false;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001712
LiFan736c0a72017-11-25 11:46:18 +08001713 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001714 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001715 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08001716
1717 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001718 list_del(&page->lru);
1719 if (add_to_page_cache_lru(page, mapping,
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001720 page_index(page),
Michal Hocko8a5c7432016-07-26 15:24:53 -07001721 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001722 goto next_page;
1723 }
1724
Chao Yu2df0ab02019-03-25 21:07:30 +08001725 ret = f2fs_read_single_page(inode, page, nr_pages, &map, &bio,
1726 &last_block_in_bio, is_readahead);
1727 if (ret) {
1728 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001729 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001730 unlock_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001731 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001732next_page:
1733 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001734 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001735 }
1736 BUG_ON(pages && !list_empty(pages));
1737 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001738 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Chao Yu2df0ab02019-03-25 21:07:30 +08001739 return pages ? 0 : ret;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001740}
1741
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001742static int f2fs_read_data_page(struct file *file, struct page *page)
1743{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001744 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001745 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001746
Chao Yuc20e89c2014-05-06 16:53:08 +08001747 trace_f2fs_readpage(page, DATA);
1748
arter97e1c42042014-08-06 23:22:50 +09001749 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001750 if (f2fs_has_inline_data(inode))
1751 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001752 if (ret == -EAGAIN)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001753 ret = f2fs_mpage_readpages(page_file_mapping(page),
1754 NULL, page, 1, false);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001755 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001756}
1757
1758static int f2fs_read_data_pages(struct file *file,
1759 struct address_space *mapping,
1760 struct list_head *pages, unsigned nr_pages)
1761{
Hsiang Kao71cb4af2017-09-24 02:45:42 +08001762 struct inode *inode = mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001763 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001764
1765 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001766
1767 /* If the file has inline data, skip readpages */
1768 if (f2fs_has_inline_data(inode))
1769 return 0;
1770
Jaegeuk Kime2e59412018-06-21 11:29:43 -07001771 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001772}
1773
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001774static int encrypt_one_page(struct f2fs_io_info *fio)
1775{
1776 struct inode *inode = fio->page->mapping->host;
Chao Yu6aa58d82018-08-14 22:37:25 +08001777 struct page *mpage;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001778 gfp_t gfp_flags = GFP_NOFS;
1779
Jaegeuk Kim19585932017-09-05 16:54:24 -07001780 if (!f2fs_encrypted_file(inode))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001781 return 0;
1782
Eric Biggers6dbb1792018-04-18 11:09:48 -07001783 /* wait for GCed page writeback via META_MAPPING */
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07001784 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001785
1786retry_encrypt:
Eric Biggers53bc1d852019-05-20 09:29:44 -07001787 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(fio->page,
1788 PAGE_SIZE, 0,
1789 gfp_flags);
Chao Yu6aa58d82018-08-14 22:37:25 +08001790 if (IS_ERR(fio->encrypted_page)) {
1791 /* flush pending IOs and wait for a while in the ENOMEM case */
1792 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
1793 f2fs_flush_merged_writes(fio->sbi);
1794 congestion_wait(BLK_RW_ASYNC, HZ/50);
1795 gfp_flags |= __GFP_NOFAIL;
1796 goto retry_encrypt;
1797 }
1798 return PTR_ERR(fio->encrypted_page);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001799 }
Chao Yu6aa58d82018-08-14 22:37:25 +08001800
1801 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
1802 if (mpage) {
1803 if (PageUptodate(mpage))
1804 memcpy(page_address(mpage),
1805 page_address(fio->encrypted_page), PAGE_SIZE);
1806 f2fs_put_page(mpage, 1);
1807 }
1808 return 0;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001809}
1810
Chao Yubb9e3bb2018-01-17 16:31:38 +08001811static inline bool check_inplace_update_policy(struct inode *inode,
1812 struct f2fs_io_info *fio)
1813{
1814 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1815 unsigned int policy = SM_I(sbi)->ipu_policy;
1816
1817 if (policy & (0x1 << F2FS_IPU_FORCE))
1818 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08001819 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
Chao Yubb9e3bb2018-01-17 16:31:38 +08001820 return true;
1821 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1822 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1823 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08001824 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
Chao Yubb9e3bb2018-01-17 16:31:38 +08001825 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1826 return true;
1827
1828 /*
1829 * IPU for rewrite async pages
1830 */
1831 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1832 fio && fio->op == REQ_OP_WRITE &&
1833 !(fio->op_flags & REQ_SYNC) &&
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301834 !IS_ENCRYPTED(inode))
Chao Yubb9e3bb2018-01-17 16:31:38 +08001835 return true;
1836
1837 /* this is only set during fdatasync */
1838 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1839 is_inode_flag_set(inode, FI_NEED_IPU))
1840 return true;
1841
Daniel Rosenberg43549942018-08-20 19:21:43 -07001842 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1843 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1844 return true;
1845
Chao Yubb9e3bb2018-01-17 16:31:38 +08001846 return false;
1847}
1848
Chao Yu4d57b862018-05-30 00:20:41 +08001849bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb2018-01-17 16:31:38 +08001850{
1851 if (f2fs_is_pinned_file(inode))
1852 return true;
1853
1854 /* if this is cold file, we should overwrite to avoid fragmentation */
1855 if (file_is_cold(inode))
1856 return true;
1857
1858 return check_inplace_update_policy(inode, fio);
1859}
1860
Chao Yu4d57b862018-05-30 00:20:41 +08001861bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb2018-01-17 16:31:38 +08001862{
1863 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1864
1865 if (test_opt(sbi, LFS))
1866 return true;
1867 if (S_ISDIR(inode->i_mode))
1868 return true;
Chao Yuaf033b22018-09-20 20:05:00 +08001869 if (IS_NOQUOTA(inode))
1870 return true;
Chao Yubb9e3bb2018-01-17 16:31:38 +08001871 if (f2fs_is_atomic_file(inode))
1872 return true;
1873 if (fio) {
1874 if (is_cold_data(fio->page))
1875 return true;
1876 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1877 return true;
Daniel Rosenberg43549942018-08-20 19:21:43 -07001878 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1879 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1880 return true;
Chao Yubb9e3bb2018-01-17 16:31:38 +08001881 }
1882 return false;
1883}
1884
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001885static inline bool need_inplace_update(struct f2fs_io_info *fio)
1886{
1887 struct inode *inode = fio->page->mapping->host;
1888
Chao Yu4d57b862018-05-30 00:20:41 +08001889 if (f2fs_should_update_outplace(inode, fio))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001890 return false;
1891
Chao Yu4d57b862018-05-30 00:20:41 +08001892 return f2fs_should_update_inplace(inode, fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001893}
1894
Chao Yu4d57b862018-05-30 00:20:41 +08001895int f2fs_do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001896{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001897 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001898 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001899 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001900 struct extent_info ei = {0,0,0};
Chao Yu77357302018-07-17 00:02:17 +08001901 struct node_info ni;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001902 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001903 int err = 0;
1904
1905 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00001906 if (need_inplace_update(fio) &&
1907 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1908 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07001909
Chao Yuc9b60782018-08-01 19:13:44 +08001910 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08001911 DATA_GENERIC_ENHANCE))
Chao Yu10f966b2019-06-20 11:36:14 +08001912 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08001913
1914 ipu_force = true;
1915 fio->need_lock = LOCK_DONE;
1916 goto got_it;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001917 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001918
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001919 /* Deadlock due to between page->lock and f2fs_lock_op */
1920 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1921 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001922
Chao Yu4d57b862018-05-30 00:20:41 +08001923 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001924 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001925 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001926
Chao Yu28bc1062016-02-06 14:40:34 +08001927 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001928
1929 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001930 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001931 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +08001932 clear_cold_data(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001933 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001934 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00001935got_it:
Chao Yuc9b60782018-08-01 19:13:44 +08001936 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
1937 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08001938 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001939 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08001940 goto out_writepage;
1941 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001942 /*
1943 * If current allocation needs SSR,
1944 * it had better in-place writes for updated data.
1945 */
Chao Yu93770ab2019-04-15 15:26:32 +08001946 if (ipu_force ||
1947 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
Chao Yu7b525dd2018-05-23 22:25:08 +08001948 need_inplace_update(fio))) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001949 err = encrypt_one_page(fio);
1950 if (err)
1951 goto out_writepage;
1952
1953 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07001954 ClearPageError(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001955 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001956 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001957 f2fs_unlock_op(fio->sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001958 err = f2fs_inplace_write_data(fio);
Chao Yu6492a332019-02-21 20:37:14 +08001959 if (err) {
1960 if (f2fs_encrypted_file(inode))
Eric Biggersd2d07272019-05-20 09:29:39 -07001961 fscrypt_finalize_bounce_page(&fio->encrypted_page);
Chao Yu6492a332019-02-21 20:37:14 +08001962 if (PageWriteback(page))
1963 end_page_writeback(page);
Chao Yucd23ffa92019-04-15 15:30:53 +08001964 } else {
1965 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu6492a332019-02-21 20:37:14 +08001966 }
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001967 trace_f2fs_do_write_data_page(fio->page, IPU);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001968 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001969 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001970
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001971 if (fio->need_lock == LOCK_RETRY) {
1972 if (!f2fs_trylock_op(fio->sbi)) {
1973 err = -EAGAIN;
1974 goto out_writepage;
1975 }
1976 fio->need_lock = LOCK_REQ;
1977 }
1978
Chao Yu77357302018-07-17 00:02:17 +08001979 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
1980 if (err)
1981 goto out_writepage;
1982
1983 fio->version = ni.version;
1984
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001985 err = encrypt_one_page(fio);
1986 if (err)
1987 goto out_writepage;
1988
1989 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07001990 ClearPageError(page);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001991
Hou Pengyang279d6df2017-04-27 00:17:21 +08001992 /* LFS mode write path */
Chao Yu4d57b862018-05-30 00:20:41 +08001993 f2fs_outplace_write_data(&dn, fio);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001994 trace_f2fs_do_write_data_page(page, OPU);
1995 set_inode_flag(inode, FI_APPEND_WRITE);
1996 if (page->index == 0)
1997 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001998out_writepage:
1999 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002000out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002001 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002002 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002003 return err;
2004}
2005
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002006static int __write_data_page(struct page *page, bool *submitted,
Chao Yu8648de22019-02-19 16:15:29 +08002007 struct bio **bio,
2008 sector_t *last_block,
Chao Yub0af6d42017-08-02 23:21:48 +08002009 struct writeback_control *wbc,
2010 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002011{
2012 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002013 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002014 loff_t i_size = i_size_read(inode);
2015 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002016 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07002017 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002018 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09002019 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002020 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002021 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002022 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08002023 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002024 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05002025 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06002026 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00002027 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002028 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002029 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002030 .submitted = false,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002031 .need_lock = LOCK_RETRY,
Chao Yub0af6d42017-08-02 23:21:48 +08002032 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08002033 .io_wbc = wbc,
Chao Yu8648de22019-02-19 16:15:29 +08002034 .bio = bio,
2035 .last_block = last_block,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002036 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002037
Chao Yuecda0de2014-05-06 16:48:26 +08002038 trace_f2fs_writepage(page, DATA);
2039
Chao Yudb198ae2018-01-18 17:29:10 +08002040 /* we should bypass data pages to proceed the kworkder jobs */
2041 if (unlikely(f2fs_cp_error(sbi))) {
2042 mapping_set_error(page->mapping, -EIO);
Chao Yu1174abf2018-05-28 16:59:26 +08002043 /*
2044 * don't drop any dirty dentry pages for keeping lastest
2045 * directory structure.
2046 */
2047 if (S_ISDIR(inode->i_mode))
2048 goto redirty_out;
Chao Yudb198ae2018-01-18 17:29:10 +08002049 goto out;
2050 }
2051
Chao Yu0771fcc2017-06-29 23:20:45 +08002052 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2053 goto redirty_out;
2054
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002055 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09002056 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002057
2058 /*
2059 * If the offset is out-of-range of file size,
2060 * this page does not have to be written to disk.
2061 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002062 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002063 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09002064 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002065
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002066 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09002067write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002068 if (f2fs_is_drop_cache(inode))
2069 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07002070 /* we should not write 0'th page having journal header */
2071 if (f2fs_is_volatile_file(inode) && (!page->index ||
2072 (!wbc->for_reclaim &&
Chao Yu4d57b862018-05-30 00:20:41 +08002073 f2fs_available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002074 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002075
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002076 /* Dentry blocks are controlled by checkpoint */
2077 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002078 fio.need_lock = LOCK_DONE;
Chao Yu4d57b862018-05-30 00:20:41 +08002079 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002080 goto done;
2081 }
2082
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002083 if (!wbc->for_reclaim)
2084 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002085 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09002086 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002087 else
2088 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002089
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002090 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08002091 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002092 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08002093 if (!err)
2094 goto out;
2095 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002096
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002097 if (err == -EAGAIN) {
Chao Yu4d57b862018-05-30 00:20:41 +08002098 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002099 if (err == -EAGAIN) {
2100 fio.need_lock = LOCK_REQ;
Chao Yu4d57b862018-05-30 00:20:41 +08002101 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002102 }
2103 }
Chao Yua0d00fa2017-10-09 17:55:19 +08002104
Chao Yueb449792018-01-17 16:31:37 +08002105 if (err) {
2106 file_set_keep_isize(inode);
2107 } else {
2108 down_write(&F2FS_I(inode)->i_sem);
2109 if (F2FS_I(inode)->last_disk_size < psize)
2110 F2FS_I(inode)->last_disk_size = psize;
2111 up_write(&F2FS_I(inode)->i_sem);
2112 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002113
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002114done:
2115 if (err && err != -ENOENT)
2116 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002117
Jaegeuk Kim39936832012-11-22 16:21:29 +09002118out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002119 inode_dec_dirty_pages(inode);
Chao Yu2baf0782018-07-27 18:15:16 +08002120 if (err) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002121 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +08002122 clear_cold_data(page);
2123 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002124
2125 if (wbc->for_reclaim) {
Chao Yubab475c2018-09-27 23:41:16 +08002126 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002127 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu4d57b862018-05-30 00:20:41 +08002128 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002129 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08002130 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002131
2132 unlock_page(page);
Chao Yu186857c2019-04-02 18:52:19 +08002133 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
Chao Yu8648de22019-02-19 16:15:29 +08002134 !F2FS_I(inode)->cp_task) {
2135 f2fs_submit_ipu_bio(sbi, bio, page);
Jaegeuk Kima7881892017-04-20 13:51:57 -07002136 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu8648de22019-02-19 16:15:29 +08002137 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002138
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002139 if (unlikely(f2fs_cp_error(sbi))) {
Chao Yu8648de22019-02-19 16:15:29 +08002140 f2fs_submit_ipu_bio(sbi, bio, page);
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002141 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002142 submitted = NULL;
2143 }
2144
2145 if (submitted)
2146 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08002147
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002148 return 0;
2149
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002150redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002151 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim5b19d282018-05-03 23:26:02 -07002152 /*
2153 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2154 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2155 * file_write_and_wait_range() will see EIO error, which is critical
2156 * to return value of fsync() followed by atomic_write failure to user.
2157 */
2158 if (!err || wbc->for_reclaim)
Chao Yu0002b612016-11-28 19:13:43 -08002159 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002160 unlock_page(page);
2161 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09002162}
2163
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002164static int f2fs_write_data_page(struct page *page,
2165 struct writeback_control *wbc)
2166{
Chao Yu8648de22019-02-19 16:15:29 +08002167 return __write_data_page(page, NULL, NULL, NULL, wbc, FS_DATA_IO);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002168}
2169
Chao Yu8f46dca2015-07-14 18:56:10 +08002170/*
2171 * This function was copied from write_cche_pages from mm/page-writeback.c.
2172 * The major change is making write step of cold data page separately from
2173 * warm/hot data page.
2174 */
2175static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08002176 struct writeback_control *wbc,
2177 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08002178{
2179 int ret = 0;
2180 int done = 0;
2181 struct pagevec pvec;
Chao Yuc29fd0c2018-06-04 23:20:36 +08002182 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Chao Yu8648de22019-02-19 16:15:29 +08002183 struct bio *bio = NULL;
2184 sector_t last_block;
Chao Yu8f46dca2015-07-14 18:56:10 +08002185 int nr_pages;
2186 pgoff_t uninitialized_var(writeback_index);
2187 pgoff_t index;
2188 pgoff_t end; /* Inclusive */
2189 pgoff_t done_index;
2190 int cycled;
2191 int range_whole = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05002192 xa_mark_t tag;
Chao Yubab475c2018-09-27 23:41:16 +08002193 int nwritten = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08002194
Mel Gorman86679822017-11-15 17:37:52 -08002195 pagevec_init(&pvec);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07002196
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002197 if (get_dirty_pages(mapping->host) <=
2198 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2199 set_inode_flag(mapping->host, FI_HOT_DATA);
2200 else
2201 clear_inode_flag(mapping->host, FI_HOT_DATA);
2202
Chao Yu8f46dca2015-07-14 18:56:10 +08002203 if (wbc->range_cyclic) {
2204 writeback_index = mapping->writeback_index; /* prev offset */
2205 index = writeback_index;
2206 if (index == 0)
2207 cycled = 1;
2208 else
2209 cycled = 0;
2210 end = -1;
2211 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002212 index = wbc->range_start >> PAGE_SHIFT;
2213 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08002214 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2215 range_whole = 1;
2216 cycled = 1; /* ignore range_cyclic tests */
2217 }
2218 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2219 tag = PAGECACHE_TAG_TOWRITE;
2220 else
2221 tag = PAGECACHE_TAG_DIRTY;
2222retry:
2223 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2224 tag_pages_for_writeback(mapping, index, end);
2225 done_index = index;
2226 while (!done && (index <= end)) {
2227 int i;
2228
Jan Kara69c4f352017-11-15 17:34:48 -08002229 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08002230 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08002231 if (nr_pages == 0)
2232 break;
2233
2234 for (i = 0; i < nr_pages; i++) {
2235 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002236 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08002237
Chao Yuf8de4332018-05-23 22:25:09 +08002238 /* give a priority to WB_SYNC threads */
Chao Yuc29fd0c2018-06-04 23:20:36 +08002239 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
Chao Yuf8de4332018-05-23 22:25:09 +08002240 wbc->sync_mode == WB_SYNC_NONE) {
2241 done = 1;
2242 break;
2243 }
2244
Chao Yu8f46dca2015-07-14 18:56:10 +08002245 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002246retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08002247 lock_page(page);
2248
2249 if (unlikely(page->mapping != mapping)) {
2250continue_unlock:
2251 unlock_page(page);
2252 continue;
2253 }
2254
2255 if (!PageDirty(page)) {
2256 /* someone wrote it for us */
2257 goto continue_unlock;
2258 }
2259
Chao Yu8f46dca2015-07-14 18:56:10 +08002260 if (PageWriteback(page)) {
Chao Yu8648de22019-02-19 16:15:29 +08002261 if (wbc->sync_mode != WB_SYNC_NONE) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002262 f2fs_wait_on_page_writeback(page,
Chao Yubae0ee72018-12-25 17:43:42 +08002263 DATA, true, true);
Chao Yu8648de22019-02-19 16:15:29 +08002264 f2fs_submit_ipu_bio(sbi, &bio, page);
2265 } else {
Chao Yu8f46dca2015-07-14 18:56:10 +08002266 goto continue_unlock;
Chao Yu8648de22019-02-19 16:15:29 +08002267 }
Chao Yu8f46dca2015-07-14 18:56:10 +08002268 }
2269
Chao Yu8f46dca2015-07-14 18:56:10 +08002270 if (!clear_page_dirty_for_io(page))
2271 goto continue_unlock;
2272
Chao Yu8648de22019-02-19 16:15:29 +08002273 ret = __write_data_page(page, &submitted, &bio,
2274 &last_block, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08002275 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08002276 /*
2277 * keep nr_to_write, since vfs uses this to
2278 * get # of written pages.
2279 */
2280 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2281 unlock_page(page);
2282 ret = 0;
2283 continue;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002284 } else if (ret == -EAGAIN) {
2285 ret = 0;
2286 if (wbc->sync_mode == WB_SYNC_ALL) {
2287 cond_resched();
2288 congestion_wait(BLK_RW_ASYNC,
2289 HZ/50);
2290 goto retry_write;
2291 }
2292 continue;
Chao Yu0002b612016-11-28 19:13:43 -08002293 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002294 done_index = page->index + 1;
2295 done = 1;
2296 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002297 } else if (submitted) {
Chao Yubab475c2018-09-27 23:41:16 +08002298 nwritten++;
Chao Yu8f46dca2015-07-14 18:56:10 +08002299 }
2300
Chao Yuf8de4332018-05-23 22:25:09 +08002301 if (--wbc->nr_to_write <= 0 &&
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002302 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08002303 done = 1;
2304 break;
2305 }
2306 }
2307 pagevec_release(&pvec);
2308 cond_resched();
2309 }
2310
Chao Yu8f46dca2015-07-14 18:56:10 +08002311 if (!cycled && !done) {
2312 cycled = 1;
2313 index = 0;
2314 end = writeback_index - 1;
2315 goto retry;
2316 }
2317 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2318 mapping->writeback_index = done_index;
2319
Chao Yubab475c2018-09-27 23:41:16 +08002320 if (nwritten)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002321 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
Chao Yubab475c2018-09-27 23:41:16 +08002322 NULL, 0, DATA);
Chao Yu8648de22019-02-19 16:15:29 +08002323 /* submit cached bio of IPU write */
2324 if (bio)
2325 __submit_bio(sbi, bio, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08002326
Chao Yu8f46dca2015-07-14 18:56:10 +08002327 return ret;
2328}
2329
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002330static inline bool __should_serialize_io(struct inode *inode,
2331 struct writeback_control *wbc)
2332{
2333 if (!S_ISREG(inode->i_mode))
2334 return false;
Chao Yuaf033b22018-09-20 20:05:00 +08002335 if (IS_NOQUOTA(inode))
2336 return false;
Chao Yu040d2bb2019-05-20 17:36:59 +08002337 /* to avoid deadlock in path of data flush */
2338 if (F2FS_I(inode)->cp_task)
2339 return false;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002340 if (wbc->sync_mode != WB_SYNC_ALL)
2341 return true;
2342 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
2343 return true;
2344 return false;
2345}
2346
Chao Yufc99fe22018-05-30 00:20:39 +08002347static int __f2fs_write_data_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08002348 struct writeback_control *wbc,
2349 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002350{
2351 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002352 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002353 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002354 int ret;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002355 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002356
P J Pcfb185a2013-04-03 11:38:00 +09002357 /* deal with chardevs and other special file */
2358 if (!mapping->a_ops->writepage)
2359 return 0;
2360
Chao Yu6a290542015-07-17 18:02:39 +08002361 /* skip writing if there is no dirty page in this inode */
2362 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2363 return 0;
2364
Chao Yu0771fcc2017-06-29 23:20:45 +08002365 /* during POR, we don't need to trigger writepage at all. */
2366 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2367 goto skip_write;
2368
Chao Yuaf033b22018-09-20 20:05:00 +08002369 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
2370 wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima1257022015-10-08 10:40:07 -07002371 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Chao Yu4d57b862018-05-30 00:20:41 +08002372 f2fs_available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kima1257022015-10-08 10:40:07 -07002373 goto skip_write;
2374
Chao Yud323d002015-10-27 09:53:45 +08002375 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07002376 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08002377 goto skip_write;
2378
Yunlei Hed31c7c32016-02-04 16:14:00 +08002379 trace_f2fs_writepages(mapping->host, wbc, DATA);
2380
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002381 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2382 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08002383 atomic_inc(&sbi->wb_sync_req[DATA]);
2384 else if (atomic_read(&sbi->wb_sync_req[DATA]))
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002385 goto skip_write;
2386
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002387 if (__should_serialize_io(inode, wbc)) {
2388 mutex_lock(&sbi->writepages);
2389 locked = true;
2390 }
2391
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002392 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08002393 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002394 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002395
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002396 if (locked)
2397 mutex_unlock(&sbi->writepages);
2398
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002399 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08002400 atomic_dec(&sbi->wb_sync_req[DATA]);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07002401 /*
2402 * if some pages were truncated, we cannot guarantee its mapping->host
2403 * to detect pending bios.
2404 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002405
Chao Yu4d57b862018-05-30 00:20:41 +08002406 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002407 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002408
2409skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002410 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08002411 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002412 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002413}
2414
Chao Yub0af6d42017-08-02 23:21:48 +08002415static int f2fs_write_data_pages(struct address_space *mapping,
2416 struct writeback_control *wbc)
2417{
2418 struct inode *inode = mapping->host;
2419
2420 return __f2fs_write_data_pages(mapping, wbc,
2421 F2FS_I(inode)->cp_task == current ?
2422 FS_CP_DATA_IO : FS_DATA_IO);
2423}
2424
Chao Yu3aab8f82014-07-02 13:25:04 +08002425static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2426{
2427 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002428 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002429
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002430 if (to > i_size) {
Chao Yua33c1502018-08-05 23:04:25 +08002431 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002432 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08002433
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002434 truncate_pagecache(inode, i_size);
Chao Yuc42d28c2019-02-02 17:33:01 +08002435 if (!IS_NOQUOTA(inode))
2436 f2fs_truncate_blocks(inode, i_size, true);
Chao Yua33c1502018-08-05 23:04:25 +08002437
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08002438 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002439 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu3aab8f82014-07-02 13:25:04 +08002440 }
2441}
2442
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002443static int prepare_write_begin(struct f2fs_sb_info *sbi,
2444 struct page *page, loff_t pos, unsigned len,
2445 block_t *blk_addr, bool *node_changed)
2446{
2447 struct inode *inode = page->mapping->host;
2448 pgoff_t index = page->index;
2449 struct dnode_of_data dn;
2450 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002451 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00002452 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002453 int err = 0;
Sheng Yong2866fb12018-11-14 19:34:28 +08002454 int flag;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002455
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002456 /*
2457 * we already allocated all the blocks, so we don't need to get
2458 * the block addresses when there is no need to fill the page.
2459 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002460 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2461 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002462 return 0;
2463
Sheng Yong2866fb12018-11-14 19:34:28 +08002464 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
2465 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
2466 flag = F2FS_GET_BLOCK_DEFAULT;
2467 else
2468 flag = F2FS_GET_BLOCK_PRE_AIO;
2469
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002470 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002471 (pos & PAGE_MASK) >= i_size_read(inode)) {
Sheng Yong2866fb12018-11-14 19:34:28 +08002472 __do_map_lock(sbi, flag, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002473 locked = true;
2474 }
2475restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002476 /* check inline_data */
Chao Yu4d57b862018-05-30 00:20:41 +08002477 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002478 if (IS_ERR(ipage)) {
2479 err = PTR_ERR(ipage);
2480 goto unlock_out;
2481 }
2482
2483 set_new_dnode(&dn, inode, ipage, ipage, 0);
2484
2485 if (f2fs_has_inline_data(inode)) {
Chao Yuf2470372017-07-19 00:19:05 +08002486 if (pos + len <= MAX_INLINE_DATA(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002487 f2fs_do_read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002488 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08002489 if (inode->i_nlink)
2490 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002491 } else {
2492 err = f2fs_convert_inline_page(&dn, page);
2493 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002494 goto out;
2495 if (dn.data_blkaddr == NULL_ADDR)
2496 err = f2fs_get_block(&dn, index);
2497 }
2498 } else if (locked) {
2499 err = f2fs_get_block(&dn, index);
2500 } else {
2501 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2502 dn.data_blkaddr = ei.blk + index - ei.fofs;
2503 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002504 /* hole case */
Chao Yu4d57b862018-05-30 00:20:41 +08002505 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07002506 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002507 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08002508 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2509 true);
Sheng Yong2866fb12018-11-14 19:34:28 +08002510 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002511 locked = true;
2512 goto restart;
2513 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002514 }
2515 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002516
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002517 /* convert_inline_page can make node_changed */
2518 *blk_addr = dn.data_blkaddr;
2519 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002520out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002521 f2fs_put_dnode(&dn);
2522unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002523 if (locked)
Sheng Yong2866fb12018-11-14 19:34:28 +08002524 __do_map_lock(sbi, flag, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002525 return err;
2526}
2527
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002528static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2529 loff_t pos, unsigned len, unsigned flags,
2530 struct page **pagep, void **fsdata)
2531{
2532 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002533 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002534 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002535 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yua2e2e762018-01-15 17:16:46 +08002536 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002537 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002538 int err = 0;
2539
Chao Yu62aed042014-05-06 16:46:04 +08002540 trace_f2fs_write_begin(inode, pos, len, flags);
2541
Daniel Rosenberg43549942018-08-20 19:21:43 -07002542 err = f2fs_is_checkpoint_ready(sbi);
2543 if (err)
2544 goto fail;
2545
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002546 if ((f2fs_is_atomic_file(inode) &&
2547 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
2548 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002549 err = -ENOMEM;
Chao Yua2e2e762018-01-15 17:16:46 +08002550 drop_atomic = true;
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002551 goto fail;
2552 }
2553
Jaegeuk Kim5f727392014-11-25 10:59:45 -08002554 /*
2555 * We should check this at this moment to avoid deadlock on inode page
2556 * and #0 page. The locking rule for inline_data conversion should be:
2557 * lock_page(page #0) -> lock_page(inode_page)
2558 */
2559 if (index != 0) {
2560 err = f2fs_convert_inline_inode(inode);
2561 if (err)
2562 goto fail;
2563 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002564repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002565 /*
2566 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2567 * wait_for_stable_page. Will wait that below with our IO control.
2568 */
Chao Yu01eccef2017-10-28 16:52:30 +08002569 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002570 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08002571 if (!page) {
2572 err = -ENOMEM;
2573 goto fail;
2574 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09002575
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002576 *pagep = page;
2577
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002578 err = prepare_write_begin(sbi, page, pos, len,
2579 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002580 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002581 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002582
Chao Yuaf033b22018-09-20 20:05:00 +08002583 if (need_balance && !IS_NOQUOTA(inode) &&
2584 has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002585 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002586 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002587 lock_page(page);
2588 if (page->mapping != mapping) {
2589 /* The page got truncated from under us */
2590 f2fs_put_page(page, 1);
2591 goto repeat;
2592 }
2593 }
2594
Chao Yubae0ee72018-12-25 17:43:42 +08002595 f2fs_wait_on_page_writeback(page, DATA, false, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002596
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002597 if (len == PAGE_SIZE || PageUptodate(page))
2598 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002599
Yunlei He746e2402016-12-20 11:11:35 +08002600 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2601 zero_user_segment(page, len, PAGE_SIZE);
2602 return 0;
2603 }
2604
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002605 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002606 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002607 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002608 } else {
Chao Yu93770ab2019-04-15 15:26:32 +08002609 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
2610 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08002611 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08002612 goto fail;
2613 }
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07002614 err = f2fs_submit_page_read(inode, page, blkaddr);
2615 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08002616 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08002617
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002618 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002619 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002620 f2fs_put_page(page, 1);
2621 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002622 }
Chao Yu1563ac72016-07-03 22:05:12 +08002623 if (unlikely(!PageUptodate(page))) {
2624 err = -EIO;
2625 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002626 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002627 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002628 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002629
Chao Yu3aab8f82014-07-02 13:25:04 +08002630fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002631 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002632 f2fs_write_failed(mapping, pos + len);
Chao Yua2e2e762018-01-15 17:16:46 +08002633 if (drop_atomic)
Chao Yu4d57b862018-05-30 00:20:41 +08002634 f2fs_drop_inmem_pages_all(sbi, false);
Chao Yu3aab8f82014-07-02 13:25:04 +08002635 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002636}
2637
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002638static int f2fs_write_end(struct file *file,
2639 struct address_space *mapping,
2640 loff_t pos, unsigned len, unsigned copied,
2641 struct page *page, void *fsdata)
2642{
2643 struct inode *inode = page->mapping->host;
2644
Chao Yudfb2bf32014-05-06 16:47:23 +08002645 trace_f2fs_write_end(inode, pos, len, copied);
2646
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002647 /*
2648 * This should be come from len == PAGE_SIZE, and we expect copied
2649 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2650 * let generic_perform_write() try to copy data again through copied=0.
2651 */
2652 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08002653 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002654 copied = 0;
2655 else
2656 SetPageUptodate(page);
2657 }
2658 if (!copied)
2659 goto unlock_out;
2660
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002661 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002662
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002663 if (pos + copied > i_size_read(inode))
2664 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002665unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002666 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002667 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002668 return copied;
2669}
2670
Omar Sandoval6f673762015-03-16 04:33:52 -07002671static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2672 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002673{
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07002674 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2675 unsigned blkbits = i_blkbits;
2676 unsigned blocksize_mask = (1 << blkbits) - 1;
2677 unsigned long align = offset | iov_iter_alignment(iter);
2678 struct block_device *bdev = inode->i_sb->s_bdev;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002679
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07002680 if (align & blocksize_mask) {
2681 if (bdev)
2682 blkbits = blksize_bits(bdev_logical_block_size(bdev));
2683 blocksize_mask = (1 << blkbits) - 1;
2684 if (align & blocksize_mask)
2685 return -EINVAL;
2686 return 1;
2687 }
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002688 return 0;
2689}
2690
Chao Yu02b16d02018-11-12 00:46:46 +08002691static void f2fs_dio_end_io(struct bio *bio)
2692{
2693 struct f2fs_private_dio *dio = bio->bi_private;
2694
2695 dec_page_count(F2FS_I_SB(dio->inode),
2696 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2697
2698 bio->bi_private = dio->orig_private;
2699 bio->bi_end_io = dio->orig_end_io;
2700
Jaegeuk Kim52225952018-12-13 18:38:33 -08002701 kvfree(dio);
Chao Yu02b16d02018-11-12 00:46:46 +08002702
2703 bio_endio(bio);
2704}
2705
2706static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
2707 loff_t file_offset)
2708{
2709 struct f2fs_private_dio *dio;
2710 bool write = (bio_op(bio) == REQ_OP_WRITE);
Chao Yu02b16d02018-11-12 00:46:46 +08002711
2712 dio = f2fs_kzalloc(F2FS_I_SB(inode),
2713 sizeof(struct f2fs_private_dio), GFP_NOFS);
YueHaibing8e114032019-01-04 01:38:29 +00002714 if (!dio)
Chao Yu02b16d02018-11-12 00:46:46 +08002715 goto out;
Chao Yu02b16d02018-11-12 00:46:46 +08002716
2717 dio->inode = inode;
2718 dio->orig_end_io = bio->bi_end_io;
2719 dio->orig_private = bio->bi_private;
2720 dio->write = write;
2721
2722 bio->bi_end_io = f2fs_dio_end_io;
2723 bio->bi_private = dio;
2724
2725 inc_page_count(F2FS_I_SB(inode),
2726 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2727
2728 submit_bio(bio);
2729 return;
2730out:
2731 bio->bi_status = BLK_STS_IOERR;
2732 bio_endio(bio);
2733}
2734
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002735static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002736{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002737 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002738 struct inode *inode = mapping->host;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002739 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf847c692018-09-27 18:34:52 +08002740 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002741 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002742 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002743 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002744 int err;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002745 enum rw_hint hint = iocb->ki_hint;
Chao Yu63189b72018-03-08 14:22:56 +08002746 int whint_mode = F2FS_OPTION(sbi).whint_mode;
Chao Yuf847c692018-09-27 18:34:52 +08002747 bool do_opu;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002748
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002749 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002750 if (err)
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07002751 return err < 0 ? err : 0;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002752
Chao Yuf847c692018-09-27 18:34:52 +08002753 if (f2fs_force_buffered_io(inode, iocb, iter))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002754 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002755
Chao Yuf847c692018-09-27 18:34:52 +08002756 do_opu = allow_outplace_dio(inode, iocb, iter);
2757
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002758 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002759
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002760 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2761 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2762
Chao Yuf847c692018-09-27 18:34:52 +08002763 if (iocb->ki_flags & IOCB_NOWAIT) {
2764 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002765 iocb->ki_hint = hint;
2766 err = -EAGAIN;
2767 goto out;
2768 }
Chao Yuf847c692018-09-27 18:34:52 +08002769 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
2770 up_read(&fi->i_gc_rwsem[rw]);
2771 iocb->ki_hint = hint;
2772 err = -EAGAIN;
2773 goto out;
2774 }
2775 } else {
2776 down_read(&fi->i_gc_rwsem[rw]);
2777 if (do_opu)
2778 down_read(&fi->i_gc_rwsem[READ]);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002779 }
2780
Chao Yu02b16d02018-11-12 00:46:46 +08002781 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
Chao Yuf9d6d052018-11-13 14:33:45 +08002782 iter, rw == WRITE ? get_data_block_dio_write :
2783 get_data_block_dio, NULL, f2fs_dio_submit_bio,
Chao Yu02b16d02018-11-12 00:46:46 +08002784 DIO_LOCKING | DIO_SKIP_HOLES);
Chao Yuf847c692018-09-27 18:34:52 +08002785
2786 if (do_opu)
2787 up_read(&fi->i_gc_rwsem[READ]);
2788
2789 up_read(&fi->i_gc_rwsem[rw]);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002790
2791 if (rw == WRITE) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002792 if (whint_mode == WHINT_MODE_OFF)
2793 iocb->ki_hint = hint;
Chao Yub0af6d42017-08-02 23:21:48 +08002794 if (err > 0) {
2795 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2796 err);
Chao Yuf847c692018-09-27 18:34:52 +08002797 if (!do_opu)
2798 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yub0af6d42017-08-02 23:21:48 +08002799 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002800 f2fs_write_failed(mapping, offset + count);
Chao Yub0af6d42017-08-02 23:21:48 +08002801 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002802 }
Chao Yu70407fa2014-07-31 21:11:22 +08002803
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002804out:
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002805 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002806
Chao Yu3aab8f82014-07-02 13:25:04 +08002807 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002808}
2809
Chao Yu487261f2015-02-05 17:44:29 +08002810void f2fs_invalidate_page(struct page *page, unsigned int offset,
2811 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002812{
2813 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002814 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002815
Chao Yu487261f2015-02-05 17:44:29 +08002816 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002817 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002818 return;
2819
Chao Yu487261f2015-02-05 17:44:29 +08002820 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08002821 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002822 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08002823 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002824 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08002825 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002826 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +08002827 f2fs_remove_dirty_inode(inode);
Chao Yu933439c2016-10-11 22:57:01 +08002828 }
Chao Yu487261f2015-02-05 17:44:29 +08002829 }
Chao Yudecd36b2015-08-07 18:42:09 +08002830
Chao Yu2baf0782018-07-27 18:15:16 +08002831 clear_cold_data(page);
2832
Chao Yudecd36b2015-08-07 18:42:09 +08002833 if (IS_ATOMIC_WRITTEN_PAGE(page))
Chao Yu4d57b862018-05-30 00:20:41 +08002834 return f2fs_drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002835
Chao Yu240a5912019-03-06 17:30:59 +08002836 f2fs_clear_page_private(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002837}
2838
Chao Yu487261f2015-02-05 17:44:29 +08002839int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002840{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002841 /* If this is dirty page, keep PagePrivate */
2842 if (PageDirty(page))
2843 return 0;
2844
Chao Yudecd36b2015-08-07 18:42:09 +08002845 /* This is atomic written page, keep Private */
2846 if (IS_ATOMIC_WRITTEN_PAGE(page))
2847 return 0;
2848
Chao Yu2baf0782018-07-27 18:15:16 +08002849 clear_cold_data(page);
Chao Yu240a5912019-03-06 17:30:59 +08002850 f2fs_clear_page_private(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002851 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002852}
2853
2854static int f2fs_set_data_page_dirty(struct page *page)
2855{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002856 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002857
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002858 trace_f2fs_set_page_dirty(page, DATA);
2859
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002860 if (!PageUptodate(page))
2861 SetPageUptodate(page);
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002862 if (PageSwapCache(page))
2863 return __set_page_dirty_nobuffers(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002864
Chao Yu5fe45742017-01-07 18:50:26 +08002865 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002866 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002867 f2fs_register_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002868 return 1;
2869 }
2870 /*
2871 * Previously, this page has been registered, we just
2872 * return here.
2873 */
2874 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002875 }
2876
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002877 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07002878 __set_page_dirty_nobuffers(page);
Chao Yu4d57b862018-05-30 00:20:41 +08002879 f2fs_update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002880 return 1;
2881 }
2882 return 0;
2883}
2884
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002885static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2886{
Chao Yu454ae7e2014-04-22 13:34:01 +08002887 struct inode *inode = mapping->host;
2888
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002889 if (f2fs_has_inline_data(inode))
2890 return 0;
2891
2892 /* make sure allocating whole blocks */
2893 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2894 filemap_write_and_wait(mapping);
2895
Chao Yue2b4e2b2015-08-19 19:11:19 +08002896 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002897}
2898
Weichao Guo5b7a4872016-09-20 05:03:27 +08002899#ifdef CONFIG_MIGRATION
2900#include <linux/migrate.h>
2901
2902int f2fs_migrate_page(struct address_space *mapping,
2903 struct page *newpage, struct page *page, enum migrate_mode mode)
2904{
2905 int rc, extra_count;
2906 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2907 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2908
2909 BUG_ON(PageWriteback(page));
2910
2911 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07002912 if (atomic_written) {
2913 if (mode != MIGRATE_SYNC)
2914 return -EBUSY;
2915 if (!mutex_trylock(&fi->inmem_lock))
2916 return -EAGAIN;
2917 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002918
Chao Yu240a5912019-03-06 17:30:59 +08002919 /* one extra reference was held for atomic_write page */
2920 extra_count = atomic_written ? 1 : 0;
Weichao Guo5b7a4872016-09-20 05:03:27 +08002921 rc = migrate_page_move_mapping(mapping, newpage,
Keith Busch37109692019-07-18 15:58:46 -07002922 page, extra_count);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002923 if (rc != MIGRATEPAGE_SUCCESS) {
2924 if (atomic_written)
2925 mutex_unlock(&fi->inmem_lock);
2926 return rc;
2927 }
2928
2929 if (atomic_written) {
2930 struct inmem_pages *cur;
2931 list_for_each_entry(cur, &fi->inmem_pages, list)
2932 if (cur->page == page) {
2933 cur->page = newpage;
2934 break;
2935 }
2936 mutex_unlock(&fi->inmem_lock);
2937 put_page(page);
2938 get_page(newpage);
2939 }
2940
Chao Yu240a5912019-03-06 17:30:59 +08002941 if (PagePrivate(page)) {
2942 f2fs_set_page_private(newpage, page_private(page));
2943 f2fs_clear_page_private(page);
2944 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002945
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07002946 if (mode != MIGRATE_SYNC_NO_COPY)
2947 migrate_page_copy(newpage, page);
2948 else
2949 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002950
2951 return MIGRATEPAGE_SUCCESS;
2952}
2953#endif
2954
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002955#ifdef CONFIG_SWAP
2956/* Copied from generic_swapfile_activate() to check any holes */
2957static int check_swap_activate(struct file *swap_file, unsigned int max)
2958{
2959 struct address_space *mapping = swap_file->f_mapping;
2960 struct inode *inode = mapping->host;
2961 unsigned blocks_per_page;
2962 unsigned long page_no;
2963 unsigned blkbits;
2964 sector_t probe_block;
2965 sector_t last_block;
2966 sector_t lowest_block = -1;
2967 sector_t highest_block = 0;
2968
2969 blkbits = inode->i_blkbits;
2970 blocks_per_page = PAGE_SIZE >> blkbits;
2971
2972 /*
2973 * Map all the blocks into the extent list. This code doesn't try
2974 * to be very smart.
2975 */
2976 probe_block = 0;
2977 page_no = 0;
2978 last_block = i_size_read(inode) >> blkbits;
2979 while ((probe_block + blocks_per_page) <= last_block && page_no < max) {
2980 unsigned block_in_page;
2981 sector_t first_block;
2982
2983 cond_resched();
2984
2985 first_block = bmap(inode, probe_block);
2986 if (first_block == 0)
2987 goto bad_bmap;
2988
2989 /*
2990 * It must be PAGE_SIZE aligned on-disk
2991 */
2992 if (first_block & (blocks_per_page - 1)) {
2993 probe_block++;
2994 goto reprobe;
2995 }
2996
2997 for (block_in_page = 1; block_in_page < blocks_per_page;
2998 block_in_page++) {
2999 sector_t block;
3000
3001 block = bmap(inode, probe_block + block_in_page);
3002 if (block == 0)
3003 goto bad_bmap;
3004 if (block != first_block + block_in_page) {
3005 /* Discontiguity */
3006 probe_block++;
3007 goto reprobe;
3008 }
3009 }
3010
3011 first_block >>= (PAGE_SHIFT - blkbits);
3012 if (page_no) { /* exclude the header page */
3013 if (first_block < lowest_block)
3014 lowest_block = first_block;
3015 if (first_block > highest_block)
3016 highest_block = first_block;
3017 }
3018
3019 page_no++;
3020 probe_block += blocks_per_page;
3021reprobe:
3022 continue;
3023 }
3024 return 0;
3025
3026bad_bmap:
3027 pr_err("swapon: swapfile has holes\n");
3028 return -EINVAL;
3029}
3030
3031static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3032 sector_t *span)
3033{
3034 struct inode *inode = file_inode(file);
3035 int ret;
3036
3037 if (!S_ISREG(inode->i_mode))
3038 return -EINVAL;
3039
3040 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3041 return -EROFS;
3042
3043 ret = f2fs_convert_inline_inode(inode);
3044 if (ret)
3045 return ret;
3046
3047 ret = check_swap_activate(file, sis->max);
3048 if (ret)
3049 return ret;
3050
3051 set_inode_flag(inode, FI_PIN_FILE);
3052 f2fs_precache_extents(inode);
3053 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3054 return 0;
3055}
3056
3057static void f2fs_swap_deactivate(struct file *file)
3058{
3059 struct inode *inode = file_inode(file);
3060
3061 clear_inode_flag(inode, FI_PIN_FILE);
3062}
3063#else
3064static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3065 sector_t *span)
3066{
3067 return -EOPNOTSUPP;
3068}
3069
3070static void f2fs_swap_deactivate(struct file *file)
3071{
3072}
3073#endif
3074
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003075const struct address_space_operations f2fs_dblock_aops = {
3076 .readpage = f2fs_read_data_page,
3077 .readpages = f2fs_read_data_pages,
3078 .writepage = f2fs_write_data_page,
3079 .writepages = f2fs_write_data_pages,
3080 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003081 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003082 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08003083 .invalidatepage = f2fs_invalidate_page,
3084 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003085 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09003086 .bmap = f2fs_bmap,
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003087 .swap_activate = f2fs_swap_activate,
3088 .swap_deactivate = f2fs_swap_deactivate,
Weichao Guo5b7a4872016-09-20 05:03:27 +08003089#ifdef CONFIG_MIGRATION
3090 .migratepage = f2fs_migrate_page,
3091#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003092};
Eric Biggers6dbb1792018-04-18 11:09:48 -07003093
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05003094void f2fs_clear_page_cache_dirty_tag(struct page *page)
Chao Yuaec2f722018-05-26 18:03:35 +08003095{
3096 struct address_space *mapping = page_mapping(page);
3097 unsigned long flags;
3098
3099 xa_lock_irqsave(&mapping->i_pages, flags);
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05003100 __xa_clear_mark(&mapping->i_pages, page_index(page),
Chao Yuaec2f722018-05-26 18:03:35 +08003101 PAGECACHE_TAG_DIRTY);
3102 xa_unlock_irqrestore(&mapping->i_pages, flags);
3103}
3104
Eric Biggers6dbb1792018-04-18 11:09:48 -07003105int __init f2fs_init_post_read_processing(void)
3106{
3107 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0);
3108 if (!bio_post_read_ctx_cache)
3109 goto fail;
3110 bio_post_read_ctx_pool =
3111 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
3112 bio_post_read_ctx_cache);
3113 if (!bio_post_read_ctx_pool)
3114 goto fail_free_cache;
3115 return 0;
3116
3117fail_free_cache:
3118 kmem_cache_destroy(bio_post_read_ctx_cache);
3119fail:
3120 return -ENOMEM;
3121}
3122
3123void __exit f2fs_destroy_post_read_processing(void)
3124{
3125 mempool_destroy(bio_post_read_ctx_pool);
3126 kmem_cache_destroy(bio_post_read_ctx_cache);
3127}