blob: 5755e897a5f03671a3147a6dbc5cc8bf190ea5f1 [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,
Eric Biggers95ae2512019-07-22 09:26:24 -070077 STEP_VERITY,
Eric Biggers6dbb1792018-04-18 11:09:48 -070078};
79
80struct bio_post_read_ctx {
81 struct bio *bio;
82 struct work_struct work;
83 unsigned int cur_step;
84 unsigned int enabled_steps;
85};
86
87static void __read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090088{
Eric Biggers6dbb1792018-04-18 11:09:48 -070089 struct page *page;
90 struct bio_vec *bv;
Ming Lei6dc4f102019-02-15 19:13:19 +080091 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090092
Christoph Hellwig2b070cf2019-04-25 09:03:00 +020093 bio_for_each_segment_all(bv, bio, iter_all) {
Eric Biggers6dbb1792018-04-18 11:09:48 -070094 page = bv->bv_page;
95
96 /* PG_error was set if any post_read step failed */
97 if (bio->bi_status || PageError(page)) {
98 ClearPageUptodate(page);
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -070099 /* will re-read again later */
100 ClearPageError(page);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700101 } else {
102 SetPageUptodate(page);
103 }
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700104 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
Eric Biggers6dbb1792018-04-18 11:09:48 -0700105 unlock_page(page);
106 }
107 if (bio->bi_private)
108 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
109 bio_put(bio);
110}
111
112static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
113
114static void decrypt_work(struct work_struct *work)
115{
116 struct bio_post_read_ctx *ctx =
117 container_of(work, struct bio_post_read_ctx, work);
118
119 fscrypt_decrypt_bio(ctx->bio);
120
121 bio_post_read_processing(ctx);
122}
123
Eric Biggers95ae2512019-07-22 09:26:24 -0700124static void verity_work(struct work_struct *work)
125{
126 struct bio_post_read_ctx *ctx =
127 container_of(work, struct bio_post_read_ctx, work);
128
129 fsverity_verify_bio(ctx->bio);
130
131 bio_post_read_processing(ctx);
132}
133
Eric Biggers6dbb1792018-04-18 11:09:48 -0700134static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
135{
Eric Biggers95ae2512019-07-22 09:26:24 -0700136 /*
137 * We use different work queues for decryption and for verity because
138 * verity may require reading metadata pages that need decryption, and
139 * we shouldn't recurse to the same workqueue.
140 */
Eric Biggers6dbb1792018-04-18 11:09:48 -0700141 switch (++ctx->cur_step) {
142 case STEP_DECRYPT:
143 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
144 INIT_WORK(&ctx->work, decrypt_work);
145 fscrypt_enqueue_decrypt_work(&ctx->work);
146 return;
147 }
148 ctx->cur_step++;
149 /* fall-through */
Eric Biggers95ae2512019-07-22 09:26:24 -0700150 case STEP_VERITY:
151 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
152 INIT_WORK(&ctx->work, verity_work);
153 fsverity_enqueue_verify_work(&ctx->work);
154 return;
155 }
156 ctx->cur_step++;
157 /* fall-through */
Eric Biggers6dbb1792018-04-18 11:09:48 -0700158 default:
159 __read_end_io(ctx->bio);
160 }
161}
162
163static bool f2fs_bio_post_read_required(struct bio *bio)
164{
165 return bio->bi_private && !bio->bi_status;
166}
167
168static void f2fs_read_end_io(struct bio *bio)
169{
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800170 if (time_to_inject(F2FS_P_SB(bio_first_page_all(bio)),
171 FAULT_READ_IO)) {
172 f2fs_show_injection_info(FAULT_READ_IO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200173 bio->bi_status = BLK_STS_IOERR;
Chao Yu55523512017-02-25 11:08:28 +0800174 }
Chao Yu8b038c72016-09-18 23:30:07 +0800175
Eric Biggers6dbb1792018-04-18 11:09:48 -0700176 if (f2fs_bio_post_read_required(bio)) {
177 struct bio_post_read_ctx *ctx = bio->bi_private;
178
179 ctx->cur_step = STEP_INITIAL;
180 bio_post_read_processing(ctx);
181 return;
Chao Yu12377022015-05-25 18:03:38 +0800182 }
183
Eric Biggers6dbb1792018-04-18 11:09:48 -0700184 __read_end_io(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900185}
186
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200187static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900188{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +0900189 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -0800190 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800191 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900192
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800193 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
194 f2fs_show_injection_info(FAULT_WRITE_IO);
195 bio->bi_status = BLK_STS_IOERR;
196 }
197
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200198 bio_for_each_segment_all(bvec, bio, iter_all) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900199 struct page *page = bvec->bv_page;
Chao Yu36951b32016-11-16 10:41:20 +0800200 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900201
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800202 if (IS_DUMMY_WRITTEN_PAGE(page)) {
203 set_page_private(page, (unsigned long)NULL);
204 ClearPagePrivate(page);
205 unlock_page(page);
206 mempool_free(page, sbi->write_io_dummy);
207
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200208 if (unlikely(bio->bi_status))
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800209 f2fs_stop_checkpoint(sbi, true);
210 continue;
211 }
212
Eric Biggersd2d07272019-05-20 09:29:39 -0700213 fscrypt_finalize_bounce_page(&page);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700214
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200215 if (unlikely(bio->bi_status)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700216 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kimb1ca3212017-12-31 16:26:38 -0800217 if (type == F2FS_WB_CP_DATA)
218 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900219 }
Yunlei He7dff55d2018-01-11 14:19:32 +0800220
221 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
222 page->index != nid_of_node(page));
223
Chao Yu36951b32016-11-16 10:41:20 +0800224 dec_page_count(sbi, type);
Chao Yu50fa53e2018-08-02 23:03:19 +0800225 if (f2fs_in_warm_node_list(sbi, page))
226 f2fs_del_fsync_node_entry(sbi, page);
Chao Yu36951b32016-11-16 10:41:20 +0800227 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900228 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800229 }
Chao Yu36951b32016-11-16 10:41:20 +0800230 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700231 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900232 wake_up(&sbi->cp_wait);
233
234 bio_put(bio);
235}
236
Gu Zheng940a6d32013-12-20 17:39:59 +0800237/*
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700238 * Return true, if pre_bio's bdev is same as its target device.
239 */
240struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
241 block_t blk_addr, struct bio *bio)
242{
243 struct block_device *bdev = sbi->sb->s_bdev;
244 int i;
245
Damien Le Moal09168782019-03-16 09:13:06 +0900246 if (f2fs_is_multi_device(sbi)) {
247 for (i = 0; i < sbi->s_ndevs; i++) {
248 if (FDEV(i).start_blk <= blk_addr &&
249 FDEV(i).end_blk >= blk_addr) {
250 blk_addr -= FDEV(i).start_blk;
251 bdev = FDEV(i).bdev;
252 break;
253 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700254 }
255 }
256 if (bio) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200257 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700258 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
259 }
260 return bdev;
261}
262
263int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
264{
265 int i;
266
Damien Le Moal09168782019-03-16 09:13:06 +0900267 if (!f2fs_is_multi_device(sbi))
268 return 0;
269
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700270 for (i = 0; i < sbi->s_ndevs; i++)
271 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
272 return i;
273 return 0;
274}
275
276static bool __same_bdev(struct f2fs_sb_info *sbi,
277 block_t blk_addr, struct bio *bio)
278{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200279 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
280 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700281}
282
283/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800284 * Low-level block read/write IO operations.
285 */
Chao Yub757f6e2019-08-23 17:58:35 +0800286static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
Gu Zheng940a6d32013-12-20 17:39:59 +0800287{
Chao Yub757f6e2019-08-23 17:58:35 +0800288 struct f2fs_sb_info *sbi = fio->sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800289 struct bio *bio;
290
Chao Yud62fe972017-10-28 16:52:31 +0800291 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800292
Chao Yub757f6e2019-08-23 17:58:35 +0800293 f2fs_target_device(sbi, fio->new_blkaddr, bio);
294 if (is_read_io(fio->op)) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900295 bio->bi_end_io = f2fs_read_end_io;
296 bio->bi_private = NULL;
297 } else {
298 bio->bi_end_io = f2fs_write_end_io;
299 bio->bi_private = sbi;
Chao Yub757f6e2019-08-23 17:58:35 +0800300 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
301 fio->type, fio->temp);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900302 }
Chao Yub757f6e2019-08-23 17:58:35 +0800303 if (fio->io_wbc)
304 wbc_init_bio(fio->io_wbc, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800305
306 return bio;
307}
308
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700309static inline void __submit_bio(struct f2fs_sb_info *sbi,
310 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700311{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700312 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800313 unsigned int start;
314
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800315 if (type != DATA && type != NODE)
316 goto submit_io;
317
Yunlong Song66415ce2018-07-12 23:09:28 +0800318 if (test_opt(sbi, LFS) && current->plug)
Tiezhu Yang3bb09a02018-02-06 08:21:45 +0800319 blk_finish_plug(current->plug);
320
Chao Yu8223ecc2019-08-28 17:33:38 +0800321 if (F2FS_IO_ALIGNED(sbi))
322 goto submit_io;
323
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800324 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
325 start %= F2FS_IO_SIZE(sbi);
326
327 if (start == 0)
328 goto submit_io;
329
330 /* fill dummy pages */
331 for (; start < F2FS_IO_SIZE(sbi); start++) {
332 struct page *page =
333 mempool_alloc(sbi->write_io_dummy,
Gao Xiangbc73a4b2019-02-19 10:31:52 +0800334 GFP_NOIO | __GFP_NOFAIL);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800335 f2fs_bug_on(sbi, !page);
336
Gao Xiangbc73a4b2019-02-19 10:31:52 +0800337 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800338 SetPagePrivate(page);
339 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
340 lock_page(page);
341 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
342 f2fs_bug_on(sbi, 1);
343 }
344 /*
345 * In the NODE case, we lose next block address chain. So, we
346 * need to do checkpoint in f2fs_sync_file.
347 */
348 if (type == NODE)
349 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700350 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800351submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800352 if (is_read_io(bio_op(bio)))
353 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
354 else
355 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500356 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700357}
358
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900359static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900360{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900361 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900362
363 if (!io->bio)
364 return;
365
Mike Christie04d328d2016-06-05 14:31:55 -0500366 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
367
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800368 if (is_read_io(fio->op))
369 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
370 else
371 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
372
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700373 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900374 io->bio = NULL;
375}
376
Chao Yu8648de22019-02-19 16:15:29 +0800377static bool __has_merged_page(struct bio *bio, struct inode *inode,
Chao Yubab475c2018-09-27 23:41:16 +0800378 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800379{
Chao Yu0fd785e2016-01-18 18:24:59 +0800380 struct bio_vec *bvec;
381 struct page *target;
Ming Lei6dc4f102019-02-15 19:13:19 +0800382 struct bvec_iter_all iter_all;
Chao Yu0fd785e2016-01-18 18:24:59 +0800383
Chao Yu8648de22019-02-19 16:15:29 +0800384 if (!bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800385 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800386
Chao Yubab475c2018-09-27 23:41:16 +0800387 if (!inode && !page && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800388 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800389
Chao Yu8648de22019-02-19 16:15:29 +0800390 bio_for_each_segment_all(bvec, bio, iter_all) {
Chao Yu0fd785e2016-01-18 18:24:59 +0800391
Eric Biggersd2d07272019-05-20 09:29:39 -0700392 target = bvec->bv_page;
393 if (fscrypt_is_bounce_page(target))
394 target = fscrypt_pagecache_page(target);
Chao Yu0fd785e2016-01-18 18:24:59 +0800395
Chao Yu0c3a5792016-01-18 18:28:11 +0800396 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800397 return true;
Chao Yubab475c2018-09-27 23:41:16 +0800398 if (page && page == target)
399 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800400 if (ino && ino == ino_of_node(target))
401 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800402 }
403
Chao Yu0fd785e2016-01-18 18:24:59 +0800404 return false;
405}
406
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700407static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700408 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900409{
410 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700411 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900412
Chao Yudf0f8dc2014-03-22 14:57:23 +0800413 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900414
415 /* change META to META_FLUSH in the checkpoint procedure */
416 if (type >= META_FLUSH) {
417 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500418 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200419 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600420 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800421 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900422 }
423 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800424 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900425}
426
Jaegeuk Kima912b542017-05-10 11:18:25 -0700427static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800428 struct inode *inode, struct page *page,
429 nid_t ino, enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800430{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700431 enum temp_type temp;
Yunlong Song1e771e82018-11-13 11:57:32 +0800432 bool ret = true;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700433
434 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
Yunlong Song1e771e82018-11-13 11:57:32 +0800435 if (!force) {
436 enum page_type btype = PAGE_TYPE_OF_BIO(type);
437 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700438
Yunlong Song1e771e82018-11-13 11:57:32 +0800439 down_read(&io->io_rwsem);
Chao Yu8648de22019-02-19 16:15:29 +0800440 ret = __has_merged_page(io->bio, inode, page, ino);
Yunlong Song1e771e82018-11-13 11:57:32 +0800441 up_read(&io->io_rwsem);
442 }
443 if (ret)
444 __f2fs_submit_merged_write(sbi, type, temp);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700445
446 /* TODO: use HOT temp only for meta pages now. */
447 if (type >= META)
448 break;
449 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800450}
451
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700452void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800453{
Hariprasad Kelamadcc00f2019-04-06 16:29:36 +0530454 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900455}
456
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700457void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800458 struct inode *inode, struct page *page,
459 nid_t ino, enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900460{
Chao Yubab475c2018-09-27 23:41:16 +0800461 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900462}
463
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700464void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800465{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700466 f2fs_submit_merged_write(sbi, DATA);
467 f2fs_submit_merged_write(sbi, NODE);
468 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800469}
470
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900471/*
472 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300473 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900474 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700475int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900476{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900477 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700478 struct page *page = fio->encrypted_page ?
479 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900480
Chao Yuc9b60782018-08-01 19:13:44 +0800481 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800482 fio->is_por ? META_POR : (__is_meta_io(fio) ?
483 META_GENERIC : DATA_GENERIC_ENHANCE)))
Chao Yu10f966b2019-06-20 11:36:14 +0800484 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +0800485
Chao Yu2ace38e2014-12-24 16:08:14 +0800486 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700487 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900488
489 /* Allocate a new bio */
Chao Yub757f6e2019-08-23 17:58:35 +0800490 bio = __bio_alloc(fio, 1);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900491
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300492 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900493 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900494 return -EFAULT;
495 }
Chao Yu78efac52018-10-22 23:24:28 +0800496
497 if (fio->io_wbc && !is_read_io(fio->op))
Tejun Heo34e51a52019-06-27 13:39:49 -0700498 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu78efac52018-10-22 23:24:28 +0800499
Mike Christie04d328d2016-06-05 14:31:55 -0500500 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900501
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700502 inc_page_count(fio->sbi, is_read_io(fio->op) ?
503 __read_io_type(page): WB_DATA_TYPE(fio->page));
Chao Yu4c58ed02018-10-22 09:12:51 +0800504
505 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900506 return 0;
507}
508
Chao Yu8896cbd2019-07-12 16:55:41 +0800509static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
510 block_t last_blkaddr, block_t cur_blkaddr)
511{
512 if (last_blkaddr + 1 != cur_blkaddr)
513 return false;
514 return __same_bdev(sbi, cur_blkaddr, bio);
515}
516
517static bool io_type_is_mergeable(struct f2fs_bio_info *io,
518 struct f2fs_io_info *fio)
519{
520 if (io->fio.op != fio->op)
521 return false;
522 return io->fio.op_flags == fio->op_flags;
523}
524
525static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
526 struct f2fs_bio_info *io,
527 struct f2fs_io_info *fio,
528 block_t last_blkaddr,
529 block_t cur_blkaddr)
530{
Chao Yuc72db712019-07-12 16:55:42 +0800531 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
532 unsigned int filled_blocks =
533 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
534 unsigned int io_size = F2FS_IO_SIZE(sbi);
535 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
536
537 /* IOs in bio is aligned and left space of vectors is not enough */
538 if (!(filled_blocks % io_size) && left_vecs < io_size)
539 return false;
540 }
Chao Yu8896cbd2019-07-12 16:55:41 +0800541 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
542 return false;
543 return io_type_is_mergeable(io, fio);
544}
545
Chao Yu8648de22019-02-19 16:15:29 +0800546int f2fs_merge_page_bio(struct f2fs_io_info *fio)
547{
548 struct bio *bio = *fio->bio;
549 struct page *page = fio->encrypted_page ?
550 fio->encrypted_page : fio->page;
551
552 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
553 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
Chao Yu10f966b2019-06-20 11:36:14 +0800554 return -EFSCORRUPTED;
Chao Yu8648de22019-02-19 16:15:29 +0800555
556 trace_f2fs_submit_page_bio(page, fio);
557 f2fs_trace_ios(fio, 0);
558
Chao Yu8896cbd2019-07-12 16:55:41 +0800559 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
560 fio->new_blkaddr)) {
Chao Yu8648de22019-02-19 16:15:29 +0800561 __submit_bio(fio->sbi, bio, fio->type);
562 bio = NULL;
563 }
564alloc_new:
565 if (!bio) {
Chao Yub757f6e2019-08-23 17:58:35 +0800566 bio = __bio_alloc(fio, BIO_MAX_PAGES);
Chao Yu8648de22019-02-19 16:15:29 +0800567 bio_set_op_attrs(bio, fio->op, fio->op_flags);
568 }
569
570 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
571 __submit_bio(fio->sbi, bio, fio->type);
572 bio = NULL;
573 goto alloc_new;
574 }
575
576 if (fio->io_wbc)
Linus Torvalds9637d512019-07-15 21:20:52 -0700577 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu8648de22019-02-19 16:15:29 +0800578
579 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
580
581 *fio->last_block = fio->new_blkaddr;
582 *fio->bio = bio;
583
584 return 0;
585}
586
587static void f2fs_submit_ipu_bio(struct f2fs_sb_info *sbi, struct bio **bio,
588 struct page *page)
589{
590 if (!bio)
591 return;
592
593 if (!__has_merged_page(*bio, NULL, page, 0))
594 return;
595
596 __submit_bio(sbi, *bio, DATA);
597 *bio = NULL;
598}
599
Chao Yufe16efe2018-05-28 23:47:18 +0800600void f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900601{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700602 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900603 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700604 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700605 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900606
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700607 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900608
Chao Yufb830fc2017-05-19 23:37:01 +0800609 down_write(&io->io_rwsem);
610next:
611 if (fio->in_list) {
612 spin_lock(&io->io_lock);
613 if (list_empty(&io->io_list)) {
614 spin_unlock(&io->io_lock);
Chao Yufe16efe2018-05-28 23:47:18 +0800615 goto out;
Chao Yufb830fc2017-05-19 23:37:01 +0800616 }
617 fio = list_first_entry(&io->io_list,
618 struct f2fs_io_info, list);
619 list_del(&fio->list);
620 spin_unlock(&io->io_lock);
621 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900622
Chao Yu93770ab2019-04-15 15:26:32 +0800623 verify_fio_blkaddr(fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900624
Chao Yu36951b32016-11-16 10:41:20 +0800625 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
626
Thomas Meyerebf7c522017-10-07 16:02:21 +0200627 /* set submitted = true as a return value */
628 fio->submitted = true;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800629
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700630 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900631
Chao Yu8896cbd2019-07-12 16:55:41 +0800632 if (io->bio && !io_is_mergeable(sbi, io->bio, io, fio,
633 io->last_block_in_bio, fio->new_blkaddr))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900634 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900635alloc_new:
636 if (io->bio == NULL) {
Chao Yu8223ecc2019-08-28 17:33:38 +0800637 if (F2FS_IO_ALIGNED(sbi) &&
638 (fio->type == DATA || fio->type == NODE) &&
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800639 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700640 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Chao Yufe16efe2018-05-28 23:47:18 +0800641 fio->retry = true;
642 goto skip;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800643 }
Chao Yub757f6e2019-08-23 17:58:35 +0800644 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900645 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900646 }
647
Jaegeuk Kima912b542017-05-10 11:18:25 -0700648 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900649 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900650 goto alloc_new;
651 }
652
Yufen Yu578c6472018-01-09 19:33:39 +0800653 if (fio->io_wbc)
Tejun Heo34e51a52019-06-27 13:39:49 -0700654 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
Yufen Yu578c6472018-01-09 19:33:39 +0800655
Chao Yu7a9d7542016-02-22 18:36:38 +0800656 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700657 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800658
659 trace_f2fs_submit_page_write(fio->page, fio);
Chao Yufe16efe2018-05-28 23:47:18 +0800660skip:
Chao Yufb830fc2017-05-19 23:37:01 +0800661 if (fio->in_list)
662 goto next;
Chao Yufe16efe2018-05-28 23:47:18 +0800663out:
Daniel Rosenberg43549942018-08-20 19:21:43 -0700664 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
Chao Yu00e09c02019-08-23 17:58:36 +0800665 !f2fs_is_checkpoint_ready(sbi))
Jaegeuk Kim5ce80582018-09-06 11:40:12 -0700666 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800667 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900668}
669
Eric Biggers95ae2512019-07-22 09:26:24 -0700670static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
671{
672 return fsverity_active(inode) &&
673 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
674}
675
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700676static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
Eric Biggers95ae2512019-07-22 09:26:24 -0700677 unsigned nr_pages, unsigned op_flag,
678 pgoff_t first_idx)
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700679{
680 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700681 struct bio *bio;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700682 struct bio_post_read_ctx *ctx;
683 unsigned int post_read_steps = 0;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700684
Eric Biggers6dbb1792018-04-18 11:09:48 -0700685 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
686 if (!bio)
687 return ERR_PTR(-ENOMEM);
688 f2fs_target_device(sbi, blkaddr, bio);
689 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kime2e59412018-06-21 11:29:43 -0700690 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700691
692 if (f2fs_encrypted_file(inode))
693 post_read_steps |= 1 << STEP_DECRYPT;
Eric Biggers95ae2512019-07-22 09:26:24 -0700694
695 if (f2fs_need_verity(inode, first_idx))
696 post_read_steps |= 1 << STEP_VERITY;
697
Eric Biggers6dbb1792018-04-18 11:09:48 -0700698 if (post_read_steps) {
699 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
700 if (!ctx) {
701 bio_put(bio);
702 return ERR_PTR(-ENOMEM);
703 }
704 ctx->bio = bio;
705 ctx->enabled_steps = post_read_steps;
706 bio->bi_private = ctx;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700707 }
708
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700709 return bio;
710}
711
712/* This can handle encryption stuffs */
713static int f2fs_submit_page_read(struct inode *inode, struct page *page,
714 block_t blkaddr)
715{
Chao Yu93770ab2019-04-15 15:26:32 +0800716 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
717 struct bio *bio;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700718
Eric Biggers95ae2512019-07-22 09:26:24 -0700719 bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0, page->index);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700720 if (IS_ERR(bio))
721 return PTR_ERR(bio);
722
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -0700723 /* wait for GCed page writeback via META_MAPPING */
724 f2fs_wait_on_block_writeback(inode, blkaddr);
725
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700726 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
727 bio_put(bio);
728 return -EFAULT;
729 }
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -0700730 ClearPageError(page);
Chao Yu93770ab2019-04-15 15:26:32 +0800731 inc_page_count(sbi, F2FS_RD_DATA);
732 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700733 return 0;
734}
735
Chao Yu46008c62016-05-09 19:56:30 +0800736static void __set_data_blkaddr(struct dnode_of_data *dn)
737{
738 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
739 __le32 *addr_array;
Chao Yu7a2af762017-07-19 00:19:06 +0800740 int base = 0;
741
742 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
743 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800744
745 /* Get physical address of data block */
746 addr_array = blkaddr_in_node(rn);
Chao Yu7a2af762017-07-19 00:19:06 +0800747 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800748}
749
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900750/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900751 * Lock ordering for the change of data block address:
752 * ->data_page
753 * ->node_page
754 * update block addresses in the node page
755 */
Chao Yu4d57b862018-05-30 00:20:41 +0800756void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900757{
Chao Yubae0ee72018-12-25 17:43:42 +0800758 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +0800759 __set_data_blkaddr(dn);
760 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800761 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900762}
763
Chao Yuf28b3432016-02-24 17:16:47 +0800764void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
765{
766 dn->data_blkaddr = blkaddr;
Chao Yu4d57b862018-05-30 00:20:41 +0800767 f2fs_set_data_blkaddr(dn);
Chao Yuf28b3432016-02-24 17:16:47 +0800768 f2fs_update_extent_cache(dn);
769}
770
Chao Yu46008c62016-05-09 19:56:30 +0800771/* dn->ofs_in_node will be returned with up-to-date last block pointer */
Chao Yu4d57b862018-05-30 00:20:41 +0800772int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900773{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700774 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800775 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900776
Chao Yu46008c62016-05-09 19:56:30 +0800777 if (!count)
778 return 0;
779
Jaegeuk Kim91942322016-05-20 10:13:22 -0700780 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900781 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +0800782 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
783 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900784
Chao Yu46008c62016-05-09 19:56:30 +0800785 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
786 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900787
Chao Yubae0ee72018-12-25 17:43:42 +0800788 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +0800789
790 for (; count > 0; dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800791 block_t blkaddr = datablock_addr(dn->inode,
792 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800793 if (blkaddr == NULL_ADDR) {
794 dn->data_blkaddr = NEW_ADDR;
795 __set_data_blkaddr(dn);
796 count--;
797 }
798 }
799
800 if (set_page_dirty(dn->node_page))
801 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900802 return 0;
803}
804
Chao Yu46008c62016-05-09 19:56:30 +0800805/* Should keep dn->ofs_in_node unchanged */
Chao Yu4d57b862018-05-30 00:20:41 +0800806int f2fs_reserve_new_block(struct dnode_of_data *dn)
Chao Yu46008c62016-05-09 19:56:30 +0800807{
808 unsigned int ofs_in_node = dn->ofs_in_node;
809 int ret;
810
Chao Yu4d57b862018-05-30 00:20:41 +0800811 ret = f2fs_reserve_new_blocks(dn, 1);
Chao Yu46008c62016-05-09 19:56:30 +0800812 dn->ofs_in_node = ofs_in_node;
813 return ret;
814}
815
Huajun Lib6009652013-11-10 23:13:18 +0800816int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
817{
818 bool need_put = dn->inode_page ? false : true;
819 int err;
820
Chao Yu4d57b862018-05-30 00:20:41 +0800821 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
Huajun Lib6009652013-11-10 23:13:18 +0800822 if (err)
823 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900824
Huajun Lib6009652013-11-10 23:13:18 +0800825 if (dn->data_blkaddr == NULL_ADDR)
Chao Yu4d57b862018-05-30 00:20:41 +0800826 err = f2fs_reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900827 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800828 f2fs_put_dnode(dn);
829 return err;
830}
831
Fan Li759af1c2015-08-05 15:52:16 +0800832int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900833{
Hou Pengyange15882b2017-02-23 09:18:05 +0000834 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800835 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800836
Fan Li759af1c2015-08-05 15:52:16 +0800837 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
838 dn->data_blkaddr = ei.blk + index - ei.fofs;
839 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800840 }
841
Fan Li759af1c2015-08-05 15:52:16 +0800842 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900843}
844
Chao Yu4d57b862018-05-30 00:20:41 +0800845struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500846 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900847{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900848 struct address_space *mapping = inode->i_mapping;
849 struct dnode_of_data dn;
850 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +0000851 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900852 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700853
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700854 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900855 if (!page)
856 return ERR_PTR(-ENOMEM);
857
Chao Yucb3bc9e2015-02-05 18:03:40 +0800858 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
859 dn.data_blkaddr = ei.blk + index - ei.fofs;
Chao Yu93770ab2019-04-15 15:26:32 +0800860 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
861 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +0800862 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +0800863 goto put_err;
864 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800865 goto got_it;
866 }
867
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900868 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800869 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700870 if (err)
871 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900872 f2fs_put_dnode(&dn);
873
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900874 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700875 err = -ENOENT;
876 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900877 }
Chao Yu93770ab2019-04-15 15:26:32 +0800878 if (dn.data_blkaddr != NEW_ADDR &&
879 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
880 dn.data_blkaddr,
881 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +0800882 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +0800883 goto put_err;
884 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800885got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700886 if (PageUptodate(page)) {
887 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900888 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700889 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900890
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900891 /*
892 * A new dentry page is allocated but not able to be written, since its
893 * new inode page couldn't be allocated due to -ENOSPC.
894 * In such the case, its blkaddr can be remained as NEW_ADDR.
Chao Yu4d57b862018-05-30 00:20:41 +0800895 * see, f2fs_add_link -> f2fs_get_new_data_page ->
896 * f2fs_init_inode_metadata.
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900897 */
898 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300899 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700900 if (!PageUptodate(page))
901 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700902 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900903 return page;
904 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900905
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700906 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900907 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700908 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700909 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700910
911put_err:
912 f2fs_put_page(page, 1);
913 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700914}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900915
Chao Yu4d57b862018-05-30 00:20:41 +0800916struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700917{
918 struct address_space *mapping = inode->i_mapping;
919 struct page *page;
920
921 page = find_get_page(mapping, index);
922 if (page && PageUptodate(page))
923 return page;
924 f2fs_put_page(page, 0);
925
Chao Yu4d57b862018-05-30 00:20:41 +0800926 page = f2fs_get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700927 if (IS_ERR(page))
928 return page;
929
930 if (PageUptodate(page))
931 return page;
932
933 wait_on_page_locked(page);
934 if (unlikely(!PageUptodate(page))) {
935 f2fs_put_page(page, 0);
936 return ERR_PTR(-EIO);
937 }
938 return page;
939}
940
941/*
942 * If it tries to access a hole, return an error.
943 * Because, the callers, functions in dir.c and GC, should be able to know
944 * whether this page exists or not.
945 */
Chao Yu4d57b862018-05-30 00:20:41 +0800946struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700947 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700948{
949 struct address_space *mapping = inode->i_mapping;
950 struct page *page;
951repeat:
Chao Yu4d57b862018-05-30 00:20:41 +0800952 page = f2fs_get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700953 if (IS_ERR(page))
954 return page;
955
956 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900957 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900958 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900959 f2fs_put_page(page, 1);
960 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900961 }
Chao Yu1563ac72016-07-03 22:05:12 +0800962 if (unlikely(!PageUptodate(page))) {
963 f2fs_put_page(page, 1);
964 return ERR_PTR(-EIO);
965 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900966 return page;
967}
968
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900969/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900970 * Caller ensures that this data page is never allocated.
971 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900972 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800973 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
974 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800975 * Note that, ipage is set only by make_empty_dir, and if any error occur,
976 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900977 */
Chao Yu4d57b862018-05-30 00:20:41 +0800978struct page *f2fs_get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900979 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900980{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900981 struct address_space *mapping = inode->i_mapping;
982 struct page *page;
983 struct dnode_of_data dn;
984 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800985
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700986 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800987 if (!page) {
988 /*
989 * before exiting, we should make sure ipage will be released
990 * if any error occur.
991 */
992 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700993 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800994 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900995
Jaegeuk Kima8865372013-12-27 17:04:17 +0900996 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800997 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700998 if (err) {
999 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001000 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001001 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001002 if (!ipage)
1003 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001004
1005 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001006 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001007
1008 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001009 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001010 if (!PageUptodate(page))
1011 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001012 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001013 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001014
Jaegeuk Kim76121182016-01-01 22:03:47 -08001015 /* if ipage exists, blkaddr should be NEW_ADDR */
1016 f2fs_bug_on(F2FS_I_SB(inode), ipage);
Chao Yu4d57b862018-05-30 00:20:41 +08001017 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001018 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -08001019 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001020 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001021got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +08001022 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001023 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001024 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001025 return page;
1026}
1027
Hyunchul Leed5097be2017-11-28 09:23:00 +09001028static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001029{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001030 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001031 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001032 struct node_info ni;
Chao Yu6aa58d82018-08-14 22:37:25 +08001033 block_t old_blkaddr;
Chao Yu46008c62016-05-09 19:56:30 +08001034 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +08001035 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001036
Jaegeuk Kim91942322016-05-20 10:13:22 -07001037 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001038 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +08001039
Chao Yu77357302018-07-17 00:02:17 +08001040 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1041 if (err)
1042 return err;
1043
Chao Yu7a2af762017-07-19 00:19:06 +08001044 dn->data_blkaddr = datablock_addr(dn->inode,
1045 dn->node_page, dn->ofs_in_node);
Chao Yuf847c692018-09-27 18:34:52 +08001046 if (dn->data_blkaddr != NULL_ADDR)
Chao Yudf6136e2015-03-23 10:33:37 +08001047 goto alloc;
1048
Chao Yu0abd6752017-07-09 00:13:07 +08001049 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1050 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001051
Chao Yudf6136e2015-03-23 10:33:37 +08001052alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001053 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Chao Yu6aa58d82018-08-14 22:37:25 +08001054 old_blkaddr = dn->data_blkaddr;
1055 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001056 &sum, seg_type, NULL, false);
Chao Yu6aa58d82018-08-14 22:37:25 +08001057 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1058 invalidate_mapping_pages(META_MAPPING(sbi),
1059 old_blkaddr, old_blkaddr);
Chao Yu86f35dc2019-08-28 17:33:35 +08001060 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001061
Jaegeuk Kim0a4daae2018-09-19 15:28:40 -07001062 /*
1063 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1064 * data from unwritten block via dio_read.
1065 */
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001066 return 0;
1067}
1068
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001069int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001070{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001071 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +08001072 struct f2fs_map_blocks map;
Chao Yud6d478a12018-01-03 17:30:19 +08001073 int flag;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001074 int err = 0;
Chao Yud6d478a12018-01-03 17:30:19 +08001075 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001076
Weichao Guo71ad6822017-09-29 22:43:23 +08001077 /* convert inline data for Direct I/O*/
Chao Yud6d478a12018-01-03 17:30:19 +08001078 if (direct_io) {
Weichao Guo71ad6822017-09-29 22:43:23 +08001079 err = f2fs_convert_inline_inode(inode);
1080 if (err)
1081 return err;
1082 }
1083
Chao Yuf9d6d052018-11-13 14:33:45 +08001084 if (direct_io && allow_outplace_dio(inode, iocb, from))
1085 return 0;
1086
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08001087 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
1088 return 0;
1089
Jaegeuk Kim0080c502016-05-07 08:52:57 -07001090 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +08001091 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1092 if (map.m_len > map.m_lblk)
1093 map.m_len -= map.m_lblk;
1094 else
1095 map.m_len = 0;
1096
Chao Yuda859852016-01-26 15:42:58 +08001097 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08001098 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001099 map.m_seg_type = NO_CHECK_TYPE;
Chao Yuf9d6d052018-11-13 14:33:45 +08001100 map.m_may_create = true;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001101
Chao Yud6d478a12018-01-03 17:30:19 +08001102 if (direct_io) {
Chao Yu4d57b862018-05-30 00:20:41 +08001103 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
Chao Yuf847c692018-09-27 18:34:52 +08001104 flag = f2fs_force_buffered_io(inode, iocb, from) ?
Chao Yud6d478a12018-01-03 17:30:19 +08001105 F2FS_GET_BLOCK_PRE_AIO :
1106 F2FS_GET_BLOCK_PRE_DIO;
1107 goto map_blocks;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001108 }
Chao Yuf2470372017-07-19 00:19:05 +08001109 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001110 err = f2fs_convert_inline_inode(inode);
1111 if (err)
1112 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001113 }
Chao Yud6d478a12018-01-03 17:30:19 +08001114 if (f2fs_has_inline_data(inode))
Sheng Yong250066452017-11-22 18:23:39 +08001115 return err;
Chao Yud6d478a12018-01-03 17:30:19 +08001116
1117 flag = F2FS_GET_BLOCK_PRE_AIO;
1118
1119map_blocks:
1120 err = f2fs_map_blocks(inode, &map, 1, flag);
1121 if (map.m_len > 0 && err == -ENOSPC) {
1122 if (!direct_io)
1123 set_inode_flag(inode, FI_NO_PREALLOC);
1124 err = 0;
Sheng Yong250066452017-11-22 18:23:39 +08001125 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001126 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001127}
1128
Chao Yu39a86952018-09-27 18:33:18 +08001129void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
Yunlei He59c90812017-03-13 20:22:18 +08001130{
1131 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1132 if (lock)
1133 down_read(&sbi->node_change);
1134 else
1135 up_read(&sbi->node_change);
1136 } else {
1137 if (lock)
1138 f2fs_lock_op(sbi);
1139 else
1140 f2fs_unlock_op(sbi);
1141 }
1142}
1143
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001144/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001145 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1146 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +08001147 * If original data blocks are allocated, then give them to blockdev.
1148 * Otherwise,
1149 * a. preallocate requested block addresses
1150 * b. do not use extent cache for better performance
1151 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001152 */
Chao Yud323d002015-10-27 09:53:45 +08001153int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +08001154 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001155{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001156 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001157 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +08001158 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf9d6d052018-11-13 14:33:45 +08001159 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +08001160 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001161 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +08001162 unsigned int ofs_in_node, last_ofs_in_node;
1163 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +00001164 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +08001165 block_t blkaddr;
Chao Yuc4020b22018-01-11 14:42:30 +08001166 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001167
Chao Yudfd02e42016-08-20 15:12:01 +08001168 if (!maxblocks)
1169 return 0;
1170
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001171 map->m_len = 0;
1172 map->m_flags = 0;
1173
1174 /* it only supports block size == page size */
1175 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +08001176 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001177
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001178 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jia Zhuf4f0b672018-11-20 04:29:35 +08001179 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1180 map->m_may_create)
1181 goto next_dnode;
1182
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001183 map->m_pblk = ei.blk + pgofs - ei.fofs;
1184 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1185 map->m_flags = F2FS_MAP_MAPPED;
Chao Yuc4020b22018-01-11 14:42:30 +08001186 if (map->m_next_extent)
1187 *map->m_next_extent = pgofs + map->m_len;
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301188
1189 /* for hardware encryption, but to avoid potential issue in future */
1190 if (flag == F2FS_GET_BLOCK_DIO)
1191 f2fs_wait_on_block_writeback_range(inode,
1192 map->m_pblk, map->m_len);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001193 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001194 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001195
Chao Yu4fe71e82016-01-26 15:37:38 +08001196next_dnode:
Chao Yuf9d6d052018-11-13 14:33:45 +08001197 if (map->m_may_create)
Yunlei He59c90812017-03-13 20:22:18 +08001198 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001199
1200 /* When reading holes, we need its node page */
1201 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001202 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001203 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +08001204 if (flag == F2FS_GET_BLOCK_BMAP)
1205 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +08001206 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001207 err = 0;
Chao Yuda859852016-01-26 15:42:58 +08001208 if (map->m_next_pgofs)
1209 *map->m_next_pgofs =
Chao Yu4d57b862018-05-30 00:20:41 +08001210 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuc4020b22018-01-11 14:42:30 +08001211 if (map->m_next_extent)
1212 *map->m_next_extent =
Chao Yu4d57b862018-05-30 00:20:41 +08001213 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +08001214 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001215 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001216 }
Chao Yu973163f2015-09-18 16:51:51 +08001217
Chao Yuc4020b22018-01-11 14:42:30 +08001218 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +08001219 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +01001220 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +08001221 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001222
Chao Yu4fe71e82016-01-26 15:37:38 +08001223next_block:
Chao Yu7a2af762017-07-19 00:19:06 +08001224 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +08001225
Chao Yuc9b60782018-08-01 19:13:44 +08001226 if (__is_valid_data_blkaddr(blkaddr) &&
Chao Yu93770ab2019-04-15 15:26:32 +08001227 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001228 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08001229 goto sync_out;
1230 }
1231
Chao Yu93770ab2019-04-15 15:26:32 +08001232 if (__is_valid_data_blkaddr(blkaddr)) {
Chao Yuf847c692018-09-27 18:34:52 +08001233 /* use out-place-update for driect IO under LFS mode */
Chao Yuf9d6d052018-11-13 14:33:45 +08001234 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1235 map->m_may_create) {
Chao Yuf847c692018-09-27 18:34:52 +08001236 err = __allocate_data_block(&dn, map->m_seg_type);
Chao Yu05e3600612019-08-28 17:33:36 +08001237 if (err)
1238 goto sync_out;
1239 blkaddr = dn.data_blkaddr;
1240 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yuf847c692018-09-27 18:34:52 +08001241 }
1242 } else {
Fan Li7df3a432015-12-17 13:20:59 +08001243 if (create) {
1244 if (unlikely(f2fs_cp_error(sbi))) {
1245 err = -EIO;
1246 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +08001247 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001248 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +08001249 if (blkaddr == NULL_ADDR) {
1250 prealloc++;
1251 last_ofs_in_node = dn.ofs_in_node;
1252 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001253 } else {
Jaegeuk Kim0a4daae2018-09-19 15:28:40 -07001254 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1255 flag != F2FS_GET_BLOCK_DIO);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001256 err = __allocate_data_block(&dn,
1257 map->m_seg_type);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001258 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001259 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001260 }
Fan Li7df3a432015-12-17 13:20:59 +08001261 if (err)
1262 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +08001263 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001264 blkaddr = dn.data_blkaddr;
1265 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001266 if (flag == F2FS_GET_BLOCK_BMAP) {
1267 map->m_pblk = 0;
1268 goto sync_out;
1269 }
Chao Yuc4020b22018-01-11 14:42:30 +08001270 if (flag == F2FS_GET_BLOCK_PRECACHE)
1271 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001272 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1273 blkaddr == NULL_ADDR) {
1274 if (map->m_next_pgofs)
1275 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001276 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001277 }
Chao Yuf3d98e72018-01-10 18:18:52 +08001278 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1279 /* for defragment case */
1280 if (map->m_next_pgofs)
1281 *map->m_next_pgofs = pgofs + 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001282 goto sync_out;
Chao Yuf3d98e72018-01-10 18:18:52 +08001283 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001284 }
1285 }
Fan Li7df3a432015-12-17 13:20:59 +08001286
Chao Yu46008c62016-05-09 19:56:30 +08001287 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1288 goto skip;
1289
Chao Yu4fe71e82016-01-26 15:37:38 +08001290 if (map->m_len == 0) {
1291 /* preallocated unwritten block should be mapped for fiemap. */
1292 if (blkaddr == NEW_ADDR)
1293 map->m_flags |= F2FS_MAP_UNWRITTEN;
1294 map->m_flags |= F2FS_MAP_MAPPED;
1295
1296 map->m_pblk = blkaddr;
1297 map->m_len = 1;
1298 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001299 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001300 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001301 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001302 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001303 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001304 } else {
1305 goto sync_out;
1306 }
1307
Chao Yu46008c62016-05-09 19:56:30 +08001308skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001309 dn.ofs_in_node++;
1310 pgofs++;
1311
Chao Yu46008c62016-05-09 19:56:30 +08001312 /* preallocate blocks in batch for one dnode page */
1313 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1314 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001315
Chao Yu46008c62016-05-09 19:56:30 +08001316 dn.ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001317 err = f2fs_reserve_new_blocks(&dn, prealloc);
Chao Yu46008c62016-05-09 19:56:30 +08001318 if (err)
1319 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001320
Chao Yu46008c62016-05-09 19:56:30 +08001321 map->m_len += dn.ofs_in_node - ofs_in_node;
1322 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1323 err = -ENOSPC;
1324 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001325 }
Chao Yu46008c62016-05-09 19:56:30 +08001326 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001327 }
1328
Chao Yu46008c62016-05-09 19:56:30 +08001329 if (pgofs >= end)
1330 goto sync_out;
1331 else if (dn.ofs_in_node < end_offset)
1332 goto next_block;
1333
Chao Yuc4020b22018-01-11 14:42:30 +08001334 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1335 if (map->m_flags & F2FS_MAP_MAPPED) {
1336 unsigned int ofs = start_pgofs - map->m_lblk;
1337
1338 f2fs_update_extent_cache_range(&dn,
1339 start_pgofs, map->m_pblk + ofs,
1340 map->m_len - ofs);
1341 }
1342 }
1343
Chao Yu46008c62016-05-09 19:56:30 +08001344 f2fs_put_dnode(&dn);
1345
Chao Yuf9d6d052018-11-13 14:33:45 +08001346 if (map->m_may_create) {
Yunlei He59c90812017-03-13 20:22:18 +08001347 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001348 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001349 }
Chao Yu46008c62016-05-09 19:56:30 +08001350 goto next_dnode;
1351
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001352sync_out:
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301353
1354 /* for hardware encryption, but to avoid potential issue in future */
1355 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1356 f2fs_wait_on_block_writeback_range(inode,
1357 map->m_pblk, map->m_len);
1358
Chao Yuc4020b22018-01-11 14:42:30 +08001359 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1360 if (map->m_flags & F2FS_MAP_MAPPED) {
1361 unsigned int ofs = start_pgofs - map->m_lblk;
1362
1363 f2fs_update_extent_cache_range(&dn,
1364 start_pgofs, map->m_pblk + ofs,
1365 map->m_len - ofs);
1366 }
1367 if (map->m_next_extent)
1368 *map->m_next_extent = pgofs + 1;
1369 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001370 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001371unlock_out:
Chao Yuf9d6d052018-11-13 14:33:45 +08001372 if (map->m_may_create) {
Yunlei He59c90812017-03-13 20:22:18 +08001373 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001374 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001375 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001376out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001377 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001378 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001379}
1380
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001381bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1382{
1383 struct f2fs_map_blocks map;
1384 block_t last_lblk;
1385 int err;
1386
1387 if (pos + len > i_size_read(inode))
1388 return false;
1389
1390 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1391 map.m_next_pgofs = NULL;
1392 map.m_next_extent = NULL;
1393 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08001394 map.m_may_create = false;
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001395 last_lblk = F2FS_BLK_ALIGN(pos + len);
1396
1397 while (map.m_lblk < last_lblk) {
1398 map.m_len = last_lblk - map.m_lblk;
1399 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1400 if (err || map.m_len == 0)
1401 return false;
1402 map.m_lblk += map.m_len;
1403 }
1404 return true;
1405}
1406
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001407static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001408 struct buffer_head *bh, int create, int flag,
Chao Yuf9d6d052018-11-13 14:33:45 +08001409 pgoff_t *next_pgofs, int seg_type, bool may_write)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001410{
1411 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001412 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001413
1414 map.m_lblk = iblock;
1415 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001416 map.m_next_pgofs = next_pgofs;
Chao Yuc4020b22018-01-11 14:42:30 +08001417 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001418 map.m_seg_type = seg_type;
Chao Yuf9d6d052018-11-13 14:33:45 +08001419 map.m_may_create = may_write;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001420
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001421 err = f2fs_map_blocks(inode, &map, create, flag);
1422 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001423 map_bh(bh, inode->i_sb, map.m_pblk);
1424 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001425 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001426 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001427 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001428}
1429
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001430static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001431 struct buffer_head *bh_result, int create, int flag,
1432 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001433{
Chao Yuda859852016-01-26 15:42:58 +08001434 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001435 flag, next_pgofs,
Chao Yuf9d6d052018-11-13 14:33:45 +08001436 NO_CHECK_TYPE, create);
1437}
1438
1439static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1440 struct buffer_head *bh_result, int create)
1441{
1442 return __get_data_block(inode, iblock, bh_result, create,
1443 F2FS_GET_BLOCK_DIO, NULL,
1444 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
Jaegeuk Kim75a037f2019-07-31 13:27:05 -07001445 IS_SWAPFILE(inode) ? false : true);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001446}
1447
Chao Yue2b4e2b2015-08-19 19:11:19 +08001448static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001449 struct buffer_head *bh_result, int create)
1450{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001451 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuf9d6d052018-11-13 14:33:45 +08001452 F2FS_GET_BLOCK_DIO, NULL,
1453 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1454 false);
Chao Yue2b4e2b2015-08-19 19:11:19 +08001455}
1456
1457static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1458 struct buffer_head *bh_result, int create)
1459{
Yunlei He179448b2015-12-28 21:48:32 +08001460 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001461 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001462 return -EFBIG;
1463
Chao Yue2b4e2b2015-08-19 19:11:19 +08001464 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001465 F2FS_GET_BLOCK_BMAP, NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001466 NO_CHECK_TYPE, create);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001467}
1468
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001469static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1470{
1471 return (offset >> inode->i_blkbits);
1472}
1473
1474static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1475{
1476 return (blk << inode->i_blkbits);
1477}
1478
Chao Yu442a9db2018-01-11 14:39:57 +08001479static int f2fs_xattr_fiemap(struct inode *inode,
1480 struct fiemap_extent_info *fieinfo)
1481{
1482 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1483 struct page *page;
1484 struct node_info ni;
1485 __u64 phys = 0, len;
1486 __u32 flags;
1487 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1488 int err = 0;
1489
1490 if (f2fs_has_inline_xattr(inode)) {
1491 int offset;
1492
1493 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1494 inode->i_ino, false);
1495 if (!page)
1496 return -ENOMEM;
1497
Chao Yu77357302018-07-17 00:02:17 +08001498 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1499 if (err) {
1500 f2fs_put_page(page, 1);
1501 return err;
1502 }
Chao Yu442a9db2018-01-11 14:39:57 +08001503
1504 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1505 offset = offsetof(struct f2fs_inode, i_addr) +
1506 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yub323fd22018-01-17 16:31:36 +08001507 get_inline_xattr_addrs(inode));
Chao Yu442a9db2018-01-11 14:39:57 +08001508
1509 phys += offset;
1510 len = inline_xattr_size(inode);
1511
1512 f2fs_put_page(page, 1);
1513
1514 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1515
1516 if (!xnid)
1517 flags |= FIEMAP_EXTENT_LAST;
1518
1519 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1520 if (err || err == 1)
1521 return err;
1522 }
1523
1524 if (xnid) {
1525 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1526 if (!page)
1527 return -ENOMEM;
1528
Chao Yu77357302018-07-17 00:02:17 +08001529 err = f2fs_get_node_info(sbi, xnid, &ni);
1530 if (err) {
1531 f2fs_put_page(page, 1);
1532 return err;
1533 }
Chao Yu442a9db2018-01-11 14:39:57 +08001534
1535 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1536 len = inode->i_sb->s_blocksize;
1537
1538 f2fs_put_page(page, 1);
1539
1540 flags = FIEMAP_EXTENT_LAST;
1541 }
1542
1543 if (phys)
1544 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1545
1546 return (err < 0 ? err : 0);
1547}
1548
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001549int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1550 u64 start, u64 len)
1551{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001552 struct buffer_head map_bh;
1553 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001554 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001555 u64 logical = 0, phys = 0, size = 0;
1556 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001557 int ret = 0;
1558
Chao Yuc4020b22018-01-11 14:42:30 +08001559 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1560 ret = f2fs_precache_extents(inode);
1561 if (ret)
1562 return ret;
1563 }
1564
Chao Yu442a9db2018-01-11 14:39:57 +08001565 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001566 if (ret)
1567 return ret;
1568
Chao Yuf1b43d42018-01-11 14:37:35 +08001569 inode_lock(inode);
1570
Chao Yu442a9db2018-01-11 14:39:57 +08001571 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1572 ret = f2fs_xattr_fiemap(inode, fieinfo);
1573 goto out;
1574 }
1575
Chao Yu7975f342019-07-22 18:03:50 +08001576 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001577 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1578 if (ret != -EAGAIN)
Chao Yuf1b43d42018-01-11 14:37:35 +08001579 goto out;
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001580 }
1581
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001582 if (logical_to_blk(inode, len) == 0)
1583 len = blk_to_logical(inode, 1);
1584
1585 start_blk = logical_to_blk(inode, start);
1586 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001587
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001588next:
1589 memset(&map_bh, 0, sizeof(struct buffer_head));
1590 map_bh.b_size = len;
1591
Chao Yue2b4e2b2015-08-19 19:11:19 +08001592 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001593 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001594 if (ret)
1595 goto out;
1596
1597 /* HOLE */
1598 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001599 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001600
1601 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1602 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001603 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001604
Fan Li9a950d52015-12-26 18:07:41 +08001605 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001606 }
Fan Li9a950d52015-12-26 18:07:41 +08001607
Chao Yuda5af122016-01-08 20:19:27 +08001608 if (size) {
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301609 if (IS_ENCRYPTED(inode))
Chao Yuda5af122016-01-08 20:19:27 +08001610 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1611
Fan Li9a950d52015-12-26 18:07:41 +08001612 ret = fiemap_fill_next_extent(fieinfo, logical,
1613 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001614 }
Fan Li9a950d52015-12-26 18:07:41 +08001615
1616 if (start_blk > last_blk || ret)
1617 goto out;
1618
1619 logical = blk_to_logical(inode, start_blk);
1620 phys = blk_to_logical(inode, map_bh.b_blocknr);
1621 size = map_bh.b_size;
1622 flags = 0;
1623 if (buffer_unwritten(&map_bh))
1624 flags = FIEMAP_EXTENT_UNWRITTEN;
1625
1626 start_blk += logical_to_blk(inode, size);
1627
1628prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001629 cond_resched();
1630 if (fatal_signal_pending(current))
1631 ret = -EINTR;
1632 else
1633 goto next;
1634out:
1635 if (ret == 1)
1636 ret = 0;
1637
Al Viro59551022016-01-22 15:40:57 -05001638 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001639 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001640}
1641
Eric Biggers95ae2512019-07-22 09:26:24 -07001642static inline loff_t f2fs_readpage_limit(struct inode *inode)
1643{
1644 if (IS_ENABLED(CONFIG_FS_VERITY) &&
1645 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1646 return inode->i_sb->s_maxbytes;
1647
1648 return i_size_read(inode);
1649}
1650
Chao Yu2df0ab02019-03-25 21:07:30 +08001651static int f2fs_read_single_page(struct inode *inode, struct page *page,
1652 unsigned nr_pages,
1653 struct f2fs_map_blocks *map,
1654 struct bio **bio_ret,
1655 sector_t *last_block_in_bio,
1656 bool is_readahead)
1657{
1658 struct bio *bio = *bio_ret;
1659 const unsigned blkbits = inode->i_blkbits;
1660 const unsigned blocksize = 1 << blkbits;
1661 sector_t block_in_file;
1662 sector_t last_block;
1663 sector_t last_block_in_file;
1664 sector_t block_nr;
1665 int ret = 0;
1666
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001667 block_in_file = (sector_t)page_index(page);
Chao Yu2df0ab02019-03-25 21:07:30 +08001668 last_block = block_in_file + nr_pages;
Eric Biggers95ae2512019-07-22 09:26:24 -07001669 last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
Chao Yu2df0ab02019-03-25 21:07:30 +08001670 blkbits;
1671 if (last_block > last_block_in_file)
1672 last_block = last_block_in_file;
1673
1674 /* just zeroing out page which is beyond EOF */
1675 if (block_in_file >= last_block)
1676 goto zero_out;
1677 /*
1678 * Map blocks using the previous result first.
1679 */
1680 if ((map->m_flags & F2FS_MAP_MAPPED) &&
1681 block_in_file > map->m_lblk &&
1682 block_in_file < (map->m_lblk + map->m_len))
1683 goto got_it;
1684
1685 /*
1686 * Then do more f2fs_map_blocks() calls until we are
1687 * done with this page.
1688 */
1689 map->m_lblk = block_in_file;
1690 map->m_len = last_block - block_in_file;
1691
1692 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
1693 if (ret)
1694 goto out;
1695got_it:
1696 if ((map->m_flags & F2FS_MAP_MAPPED)) {
1697 block_nr = map->m_pblk + block_in_file - map->m_lblk;
1698 SetPageMappedToDisk(page);
1699
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001700 if (!PageUptodate(page) && (!PageSwapCache(page) &&
1701 !cleancache_get_page(page))) {
Chao Yu2df0ab02019-03-25 21:07:30 +08001702 SetPageUptodate(page);
1703 goto confused;
1704 }
1705
1706 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
Chao Yu93770ab2019-04-15 15:26:32 +08001707 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001708 ret = -EFSCORRUPTED;
Chao Yu2df0ab02019-03-25 21:07:30 +08001709 goto out;
1710 }
1711 } else {
1712zero_out:
1713 zero_user_segment(page, 0, PAGE_SIZE);
Eric Biggers95ae2512019-07-22 09:26:24 -07001714 if (f2fs_need_verity(inode, page->index) &&
1715 !fsverity_verify_page(page)) {
1716 ret = -EIO;
1717 goto out;
1718 }
Chao Yu2df0ab02019-03-25 21:07:30 +08001719 if (!PageUptodate(page))
1720 SetPageUptodate(page);
1721 unlock_page(page);
1722 goto out;
1723 }
1724
1725 /*
1726 * This page will go to BIO. Do we need to send this
1727 * BIO off first?
1728 */
Chao Yu8896cbd2019-07-12 16:55:41 +08001729 if (bio && !page_is_mergeable(F2FS_I_SB(inode), bio,
1730 *last_block_in_bio, block_nr)) {
Chao Yu2df0ab02019-03-25 21:07:30 +08001731submit_and_realloc:
1732 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1733 bio = NULL;
1734 }
1735 if (bio == NULL) {
1736 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
Eric Biggers95ae2512019-07-22 09:26:24 -07001737 is_readahead ? REQ_RAHEAD : 0, page->index);
Chao Yu2df0ab02019-03-25 21:07:30 +08001738 if (IS_ERR(bio)) {
1739 ret = PTR_ERR(bio);
1740 bio = NULL;
1741 goto out;
1742 }
1743 }
1744
1745 /*
1746 * If the page is under writeback, we need to wait for
1747 * its completion to see the correct decrypted data.
1748 */
1749 f2fs_wait_on_block_writeback(inode, block_nr);
1750
1751 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1752 goto submit_and_realloc;
1753
1754 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
1755 ClearPageError(page);
1756 *last_block_in_bio = block_nr;
1757 goto out;
1758confused:
1759 if (bio) {
1760 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1761 bio = NULL;
1762 }
1763 unlock_page(page);
1764out:
1765 *bio_ret = bio;
1766 return ret;
1767}
1768
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001769/*
1770 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1771 * Major change was from block_size == page_size in f2fs by default.
Jaegeuk Kime2e59412018-06-21 11:29:43 -07001772 *
1773 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1774 * this function ever deviates from doing just read-ahead, it should either
1775 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1776 * from read-ahead.
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001777 */
1778static int f2fs_mpage_readpages(struct address_space *mapping,
1779 struct list_head *pages, struct page *page,
Jaegeuk Kime2e59412018-06-21 11:29:43 -07001780 unsigned nr_pages, bool is_readahead)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001781{
1782 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001783 sector_t last_block_in_bio = 0;
1784 struct inode *inode = mapping->host;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001785 struct f2fs_map_blocks map;
Chao Yu2df0ab02019-03-25 21:07:30 +08001786 int ret = 0;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001787
1788 map.m_pblk = 0;
1789 map.m_lblk = 0;
1790 map.m_len = 0;
1791 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001792 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08001793 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001794 map.m_seg_type = NO_CHECK_TYPE;
Chao Yuf9d6d052018-11-13 14:33:45 +08001795 map.m_may_create = false;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001796
LiFan736c0a72017-11-25 11:46:18 +08001797 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001798 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001799 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08001800
1801 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001802 list_del(&page->lru);
1803 if (add_to_page_cache_lru(page, mapping,
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001804 page_index(page),
Michal Hocko8a5c7432016-07-26 15:24:53 -07001805 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001806 goto next_page;
1807 }
1808
Chao Yu2df0ab02019-03-25 21:07:30 +08001809 ret = f2fs_read_single_page(inode, page, nr_pages, &map, &bio,
1810 &last_block_in_bio, is_readahead);
1811 if (ret) {
1812 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001813 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001814 unlock_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001815 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001816next_page:
1817 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001818 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001819 }
1820 BUG_ON(pages && !list_empty(pages));
1821 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001822 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Chao Yu2df0ab02019-03-25 21:07:30 +08001823 return pages ? 0 : ret;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001824}
1825
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001826static int f2fs_read_data_page(struct file *file, struct page *page)
1827{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001828 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001829 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001830
Chao Yuc20e89c2014-05-06 16:53:08 +08001831 trace_f2fs_readpage(page, DATA);
1832
arter97e1c42042014-08-06 23:22:50 +09001833 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001834 if (f2fs_has_inline_data(inode))
1835 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001836 if (ret == -EAGAIN)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001837 ret = f2fs_mpage_readpages(page_file_mapping(page),
1838 NULL, page, 1, false);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001839 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001840}
1841
1842static int f2fs_read_data_pages(struct file *file,
1843 struct address_space *mapping,
1844 struct list_head *pages, unsigned nr_pages)
1845{
Hsiang Kao71cb4af2017-09-24 02:45:42 +08001846 struct inode *inode = mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001847 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001848
1849 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001850
1851 /* If the file has inline data, skip readpages */
1852 if (f2fs_has_inline_data(inode))
1853 return 0;
1854
Jaegeuk Kime2e59412018-06-21 11:29:43 -07001855 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001856}
1857
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001858static int encrypt_one_page(struct f2fs_io_info *fio)
1859{
1860 struct inode *inode = fio->page->mapping->host;
Chao Yu6aa58d82018-08-14 22:37:25 +08001861 struct page *mpage;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001862 gfp_t gfp_flags = GFP_NOFS;
1863
Jaegeuk Kim19585932017-09-05 16:54:24 -07001864 if (!f2fs_encrypted_file(inode))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001865 return 0;
1866
Eric Biggers6dbb1792018-04-18 11:09:48 -07001867 /* wait for GCed page writeback via META_MAPPING */
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07001868 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001869
1870retry_encrypt:
Eric Biggers53bc1d852019-05-20 09:29:44 -07001871 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(fio->page,
1872 PAGE_SIZE, 0,
1873 gfp_flags);
Chao Yu6aa58d82018-08-14 22:37:25 +08001874 if (IS_ERR(fio->encrypted_page)) {
1875 /* flush pending IOs and wait for a while in the ENOMEM case */
1876 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
1877 f2fs_flush_merged_writes(fio->sbi);
1878 congestion_wait(BLK_RW_ASYNC, HZ/50);
1879 gfp_flags |= __GFP_NOFAIL;
1880 goto retry_encrypt;
1881 }
1882 return PTR_ERR(fio->encrypted_page);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001883 }
Chao Yu6aa58d82018-08-14 22:37:25 +08001884
1885 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
1886 if (mpage) {
1887 if (PageUptodate(mpage))
1888 memcpy(page_address(mpage),
1889 page_address(fio->encrypted_page), PAGE_SIZE);
1890 f2fs_put_page(mpage, 1);
1891 }
1892 return 0;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001893}
1894
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001895static inline bool check_inplace_update_policy(struct inode *inode,
1896 struct f2fs_io_info *fio)
1897{
1898 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1899 unsigned int policy = SM_I(sbi)->ipu_policy;
1900
1901 if (policy & (0x1 << F2FS_IPU_FORCE))
1902 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08001903 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001904 return true;
1905 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1906 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1907 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08001908 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001909 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1910 return true;
1911
1912 /*
1913 * IPU for rewrite async pages
1914 */
1915 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1916 fio && fio->op == REQ_OP_WRITE &&
1917 !(fio->op_flags & REQ_SYNC) &&
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301918 !IS_ENCRYPTED(inode))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001919 return true;
1920
1921 /* this is only set during fdatasync */
1922 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1923 is_inode_flag_set(inode, FI_NEED_IPU))
1924 return true;
1925
Daniel Rosenberg43549942018-08-20 19:21:43 -07001926 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1927 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1928 return true;
1929
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001930 return false;
1931}
1932
Chao Yu4d57b862018-05-30 00:20:41 +08001933bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001934{
1935 if (f2fs_is_pinned_file(inode))
1936 return true;
1937
1938 /* if this is cold file, we should overwrite to avoid fragmentation */
1939 if (file_is_cold(inode))
1940 return true;
1941
1942 return check_inplace_update_policy(inode, fio);
1943}
1944
Chao Yu4d57b862018-05-30 00:20:41 +08001945bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001946{
1947 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1948
1949 if (test_opt(sbi, LFS))
1950 return true;
1951 if (S_ISDIR(inode->i_mode))
1952 return true;
Chao Yuaf033b22018-09-20 20:05:00 +08001953 if (IS_NOQUOTA(inode))
1954 return true;
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001955 if (f2fs_is_atomic_file(inode))
1956 return true;
1957 if (fio) {
1958 if (is_cold_data(fio->page))
1959 return true;
1960 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1961 return true;
Daniel Rosenberg43549942018-08-20 19:21:43 -07001962 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1963 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1964 return true;
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001965 }
1966 return false;
1967}
1968
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001969static inline bool need_inplace_update(struct f2fs_io_info *fio)
1970{
1971 struct inode *inode = fio->page->mapping->host;
1972
Chao Yu4d57b862018-05-30 00:20:41 +08001973 if (f2fs_should_update_outplace(inode, fio))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001974 return false;
1975
Chao Yu4d57b862018-05-30 00:20:41 +08001976 return f2fs_should_update_inplace(inode, fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001977}
1978
Chao Yu4d57b862018-05-30 00:20:41 +08001979int f2fs_do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001980{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001981 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001982 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001983 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001984 struct extent_info ei = {0,0,0};
Chao Yu77357302018-07-17 00:02:17 +08001985 struct node_info ni;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001986 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001987 int err = 0;
1988
1989 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00001990 if (need_inplace_update(fio) &&
1991 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1992 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07001993
Chao Yuc9b60782018-08-01 19:13:44 +08001994 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08001995 DATA_GENERIC_ENHANCE))
Chao Yu10f966b2019-06-20 11:36:14 +08001996 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08001997
1998 ipu_force = true;
1999 fio->need_lock = LOCK_DONE;
2000 goto got_it;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002001 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002002
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002003 /* Deadlock due to between page->lock and f2fs_lock_op */
2004 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2005 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08002006
Chao Yu4d57b862018-05-30 00:20:41 +08002007 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002008 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002009 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002010
Chao Yu28bc1062016-02-06 14:40:34 +08002011 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002012
2013 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08002014 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002015 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +08002016 clear_cold_data(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002017 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002018 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00002019got_it:
Chao Yuc9b60782018-08-01 19:13:44 +08002020 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2021 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08002022 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08002023 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08002024 goto out_writepage;
2025 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002026 /*
2027 * If current allocation needs SSR,
2028 * it had better in-place writes for updated data.
2029 */
Chao Yu93770ab2019-04-15 15:26:32 +08002030 if (ipu_force ||
2031 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
Chao Yu7b525dd2018-05-23 22:25:08 +08002032 need_inplace_update(fio))) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002033 err = encrypt_one_page(fio);
2034 if (err)
2035 goto out_writepage;
2036
2037 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07002038 ClearPageError(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002039 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002040 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002041 f2fs_unlock_op(fio->sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08002042 err = f2fs_inplace_write_data(fio);
Chao Yu6492a332019-02-21 20:37:14 +08002043 if (err) {
2044 if (f2fs_encrypted_file(inode))
Eric Biggersd2d07272019-05-20 09:29:39 -07002045 fscrypt_finalize_bounce_page(&fio->encrypted_page);
Chao Yu6492a332019-02-21 20:37:14 +08002046 if (PageWriteback(page))
2047 end_page_writeback(page);
Chao Yucd23ffa92019-04-15 15:30:53 +08002048 } else {
2049 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu6492a332019-02-21 20:37:14 +08002050 }
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002051 trace_f2fs_do_write_data_page(fio->page, IPU);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002052 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002053 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002054
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002055 if (fio->need_lock == LOCK_RETRY) {
2056 if (!f2fs_trylock_op(fio->sbi)) {
2057 err = -EAGAIN;
2058 goto out_writepage;
2059 }
2060 fio->need_lock = LOCK_REQ;
2061 }
2062
Chao Yu77357302018-07-17 00:02:17 +08002063 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2064 if (err)
2065 goto out_writepage;
2066
2067 fio->version = ni.version;
2068
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002069 err = encrypt_one_page(fio);
2070 if (err)
2071 goto out_writepage;
2072
2073 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07002074 ClearPageError(page);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002075
Hou Pengyang279d6df2017-04-27 00:17:21 +08002076 /* LFS mode write path */
Chao Yu4d57b862018-05-30 00:20:41 +08002077 f2fs_outplace_write_data(&dn, fio);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002078 trace_f2fs_do_write_data_page(page, OPU);
2079 set_inode_flag(inode, FI_APPEND_WRITE);
2080 if (page->index == 0)
2081 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002082out_writepage:
2083 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002084out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002085 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002086 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002087 return err;
2088}
2089
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002090static int __write_data_page(struct page *page, bool *submitted,
Chao Yu8648de22019-02-19 16:15:29 +08002091 struct bio **bio,
2092 sector_t *last_block,
Chao Yub0af6d42017-08-02 23:21:48 +08002093 struct writeback_control *wbc,
2094 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002095{
2096 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002097 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002098 loff_t i_size = i_size_read(inode);
2099 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002100 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07002101 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002102 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09002103 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002104 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002105 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002106 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08002107 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002108 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05002109 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06002110 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00002111 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002112 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002113 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002114 .submitted = false,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002115 .need_lock = LOCK_RETRY,
Chao Yub0af6d42017-08-02 23:21:48 +08002116 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08002117 .io_wbc = wbc,
Chao Yu8648de22019-02-19 16:15:29 +08002118 .bio = bio,
2119 .last_block = last_block,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002120 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002121
Chao Yuecda0de2014-05-06 16:48:26 +08002122 trace_f2fs_writepage(page, DATA);
2123
Chao Yudb198ae2018-01-18 17:29:10 +08002124 /* we should bypass data pages to proceed the kworkder jobs */
2125 if (unlikely(f2fs_cp_error(sbi))) {
2126 mapping_set_error(page->mapping, -EIO);
Chao Yu1174abf2018-05-28 16:59:26 +08002127 /*
2128 * don't drop any dirty dentry pages for keeping lastest
2129 * directory structure.
2130 */
2131 if (S_ISDIR(inode->i_mode))
2132 goto redirty_out;
Chao Yudb198ae2018-01-18 17:29:10 +08002133 goto out;
2134 }
2135
Chao Yu0771fcc2017-06-29 23:20:45 +08002136 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2137 goto redirty_out;
2138
Eric Biggers95ae2512019-07-22 09:26:24 -07002139 if (page->index < end_index || f2fs_verity_in_progress(inode))
Jaegeuk Kim39936832012-11-22 16:21:29 +09002140 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002141
2142 /*
2143 * If the offset is out-of-range of file size,
2144 * this page does not have to be written to disk.
2145 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002146 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002147 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09002148 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002149
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002150 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09002151write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002152 if (f2fs_is_drop_cache(inode))
2153 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07002154 /* we should not write 0'th page having journal header */
2155 if (f2fs_is_volatile_file(inode) && (!page->index ||
2156 (!wbc->for_reclaim &&
Chao Yu4d57b862018-05-30 00:20:41 +08002157 f2fs_available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002158 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002159
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002160 /* Dentry blocks are controlled by checkpoint */
2161 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002162 fio.need_lock = LOCK_DONE;
Chao Yu4d57b862018-05-30 00:20:41 +08002163 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002164 goto done;
2165 }
2166
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002167 if (!wbc->for_reclaim)
2168 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002169 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09002170 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002171 else
2172 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002173
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002174 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08002175 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002176 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08002177 if (!err)
2178 goto out;
2179 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002180
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002181 if (err == -EAGAIN) {
Chao Yu4d57b862018-05-30 00:20:41 +08002182 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002183 if (err == -EAGAIN) {
2184 fio.need_lock = LOCK_REQ;
Chao Yu4d57b862018-05-30 00:20:41 +08002185 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002186 }
2187 }
Chao Yua0d00fa2017-10-09 17:55:19 +08002188
Chao Yueb449792018-01-17 16:31:37 +08002189 if (err) {
2190 file_set_keep_isize(inode);
2191 } else {
2192 down_write(&F2FS_I(inode)->i_sem);
2193 if (F2FS_I(inode)->last_disk_size < psize)
2194 F2FS_I(inode)->last_disk_size = psize;
2195 up_write(&F2FS_I(inode)->i_sem);
2196 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002197
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002198done:
2199 if (err && err != -ENOENT)
2200 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002201
Jaegeuk Kim39936832012-11-22 16:21:29 +09002202out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002203 inode_dec_dirty_pages(inode);
Chao Yu2baf0782018-07-27 18:15:16 +08002204 if (err) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002205 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +08002206 clear_cold_data(page);
2207 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002208
2209 if (wbc->for_reclaim) {
Chao Yubab475c2018-09-27 23:41:16 +08002210 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002211 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu4d57b862018-05-30 00:20:41 +08002212 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002213 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08002214 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002215
2216 unlock_page(page);
Chao Yu186857c2019-04-02 18:52:19 +08002217 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
Chao Yu8648de22019-02-19 16:15:29 +08002218 !F2FS_I(inode)->cp_task) {
2219 f2fs_submit_ipu_bio(sbi, bio, page);
Jaegeuk Kima7881892017-04-20 13:51:57 -07002220 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu8648de22019-02-19 16:15:29 +08002221 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002222
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002223 if (unlikely(f2fs_cp_error(sbi))) {
Chao Yu8648de22019-02-19 16:15:29 +08002224 f2fs_submit_ipu_bio(sbi, bio, page);
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002225 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002226 submitted = NULL;
2227 }
2228
2229 if (submitted)
2230 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08002231
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002232 return 0;
2233
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002234redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002235 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim5b19d282018-05-03 23:26:02 -07002236 /*
2237 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2238 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2239 * file_write_and_wait_range() will see EIO error, which is critical
2240 * to return value of fsync() followed by atomic_write failure to user.
2241 */
2242 if (!err || wbc->for_reclaim)
Chao Yu0002b612016-11-28 19:13:43 -08002243 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002244 unlock_page(page);
2245 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09002246}
2247
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002248static int f2fs_write_data_page(struct page *page,
2249 struct writeback_control *wbc)
2250{
Chao Yu8648de22019-02-19 16:15:29 +08002251 return __write_data_page(page, NULL, NULL, NULL, wbc, FS_DATA_IO);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002252}
2253
Chao Yu8f46dca2015-07-14 18:56:10 +08002254/*
2255 * This function was copied from write_cche_pages from mm/page-writeback.c.
2256 * The major change is making write step of cold data page separately from
2257 * warm/hot data page.
2258 */
2259static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08002260 struct writeback_control *wbc,
2261 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08002262{
2263 int ret = 0;
2264 int done = 0;
2265 struct pagevec pvec;
Chao Yuc29fd0c2018-06-04 23:20:36 +08002266 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Chao Yu8648de22019-02-19 16:15:29 +08002267 struct bio *bio = NULL;
2268 sector_t last_block;
Chao Yu8f46dca2015-07-14 18:56:10 +08002269 int nr_pages;
2270 pgoff_t uninitialized_var(writeback_index);
2271 pgoff_t index;
2272 pgoff_t end; /* Inclusive */
2273 pgoff_t done_index;
2274 int cycled;
2275 int range_whole = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05002276 xa_mark_t tag;
Chao Yubab475c2018-09-27 23:41:16 +08002277 int nwritten = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08002278
Mel Gorman86679822017-11-15 17:37:52 -08002279 pagevec_init(&pvec);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07002280
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002281 if (get_dirty_pages(mapping->host) <=
2282 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2283 set_inode_flag(mapping->host, FI_HOT_DATA);
2284 else
2285 clear_inode_flag(mapping->host, FI_HOT_DATA);
2286
Chao Yu8f46dca2015-07-14 18:56:10 +08002287 if (wbc->range_cyclic) {
2288 writeback_index = mapping->writeback_index; /* prev offset */
2289 index = writeback_index;
2290 if (index == 0)
2291 cycled = 1;
2292 else
2293 cycled = 0;
2294 end = -1;
2295 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002296 index = wbc->range_start >> PAGE_SHIFT;
2297 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08002298 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2299 range_whole = 1;
2300 cycled = 1; /* ignore range_cyclic tests */
2301 }
2302 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2303 tag = PAGECACHE_TAG_TOWRITE;
2304 else
2305 tag = PAGECACHE_TAG_DIRTY;
2306retry:
2307 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2308 tag_pages_for_writeback(mapping, index, end);
2309 done_index = index;
2310 while (!done && (index <= end)) {
2311 int i;
2312
Jan Kara69c4f352017-11-15 17:34:48 -08002313 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08002314 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08002315 if (nr_pages == 0)
2316 break;
2317
2318 for (i = 0; i < nr_pages; i++) {
2319 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002320 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08002321
Chao Yuf8de4332018-05-23 22:25:09 +08002322 /* give a priority to WB_SYNC threads */
Chao Yuc29fd0c2018-06-04 23:20:36 +08002323 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
Chao Yuf8de4332018-05-23 22:25:09 +08002324 wbc->sync_mode == WB_SYNC_NONE) {
2325 done = 1;
2326 break;
2327 }
2328
Chao Yu8f46dca2015-07-14 18:56:10 +08002329 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002330retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08002331 lock_page(page);
2332
2333 if (unlikely(page->mapping != mapping)) {
2334continue_unlock:
2335 unlock_page(page);
2336 continue;
2337 }
2338
2339 if (!PageDirty(page)) {
2340 /* someone wrote it for us */
2341 goto continue_unlock;
2342 }
2343
Chao Yu8f46dca2015-07-14 18:56:10 +08002344 if (PageWriteback(page)) {
Chao Yu8648de22019-02-19 16:15:29 +08002345 if (wbc->sync_mode != WB_SYNC_NONE) {
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002346 f2fs_wait_on_page_writeback(page,
Chao Yubae0ee72018-12-25 17:43:42 +08002347 DATA, true, true);
Chao Yu8648de22019-02-19 16:15:29 +08002348 f2fs_submit_ipu_bio(sbi, &bio, page);
2349 } else {
Chao Yu8f46dca2015-07-14 18:56:10 +08002350 goto continue_unlock;
Chao Yu8648de22019-02-19 16:15:29 +08002351 }
Chao Yu8f46dca2015-07-14 18:56:10 +08002352 }
2353
Chao Yu8f46dca2015-07-14 18:56:10 +08002354 if (!clear_page_dirty_for_io(page))
2355 goto continue_unlock;
2356
Chao Yu8648de22019-02-19 16:15:29 +08002357 ret = __write_data_page(page, &submitted, &bio,
2358 &last_block, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08002359 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08002360 /*
2361 * keep nr_to_write, since vfs uses this to
2362 * get # of written pages.
2363 */
2364 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2365 unlock_page(page);
2366 ret = 0;
2367 continue;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002368 } else if (ret == -EAGAIN) {
2369 ret = 0;
2370 if (wbc->sync_mode == WB_SYNC_ALL) {
2371 cond_resched();
2372 congestion_wait(BLK_RW_ASYNC,
2373 HZ/50);
2374 goto retry_write;
2375 }
2376 continue;
Chao Yu0002b612016-11-28 19:13:43 -08002377 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002378 done_index = page->index + 1;
2379 done = 1;
2380 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002381 } else if (submitted) {
Chao Yubab475c2018-09-27 23:41:16 +08002382 nwritten++;
Chao Yu8f46dca2015-07-14 18:56:10 +08002383 }
2384
Chao Yuf8de4332018-05-23 22:25:09 +08002385 if (--wbc->nr_to_write <= 0 &&
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002386 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08002387 done = 1;
2388 break;
2389 }
2390 }
2391 pagevec_release(&pvec);
2392 cond_resched();
2393 }
2394
Chao Yu8f46dca2015-07-14 18:56:10 +08002395 if (!cycled && !done) {
2396 cycled = 1;
2397 index = 0;
2398 end = writeback_index - 1;
2399 goto retry;
2400 }
2401 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2402 mapping->writeback_index = done_index;
2403
Chao Yubab475c2018-09-27 23:41:16 +08002404 if (nwritten)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002405 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
Chao Yubab475c2018-09-27 23:41:16 +08002406 NULL, 0, DATA);
Chao Yu8648de22019-02-19 16:15:29 +08002407 /* submit cached bio of IPU write */
2408 if (bio)
2409 __submit_bio(sbi, bio, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08002410
Chao Yu8f46dca2015-07-14 18:56:10 +08002411 return ret;
2412}
2413
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002414static inline bool __should_serialize_io(struct inode *inode,
2415 struct writeback_control *wbc)
2416{
2417 if (!S_ISREG(inode->i_mode))
2418 return false;
Chao Yuaf033b22018-09-20 20:05:00 +08002419 if (IS_NOQUOTA(inode))
2420 return false;
Chao Yu040d2bb2019-05-20 17:36:59 +08002421 /* to avoid deadlock in path of data flush */
2422 if (F2FS_I(inode)->cp_task)
2423 return false;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002424 if (wbc->sync_mode != WB_SYNC_ALL)
2425 return true;
2426 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
2427 return true;
2428 return false;
2429}
2430
Chao Yufc99fe22018-05-30 00:20:39 +08002431static int __f2fs_write_data_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08002432 struct writeback_control *wbc,
2433 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002434{
2435 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002436 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002437 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002438 int ret;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002439 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002440
P J Pcfb185a2013-04-03 11:38:00 +09002441 /* deal with chardevs and other special file */
2442 if (!mapping->a_ops->writepage)
2443 return 0;
2444
Chao Yu6a290542015-07-17 18:02:39 +08002445 /* skip writing if there is no dirty page in this inode */
2446 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2447 return 0;
2448
Chao Yu0771fcc2017-06-29 23:20:45 +08002449 /* during POR, we don't need to trigger writepage at all. */
2450 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2451 goto skip_write;
2452
Chao Yuaf033b22018-09-20 20:05:00 +08002453 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
2454 wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima1257022015-10-08 10:40:07 -07002455 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Chao Yu4d57b862018-05-30 00:20:41 +08002456 f2fs_available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kima1257022015-10-08 10:40:07 -07002457 goto skip_write;
2458
Chao Yud323d002015-10-27 09:53:45 +08002459 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07002460 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08002461 goto skip_write;
2462
Yunlei Hed31c7c32016-02-04 16:14:00 +08002463 trace_f2fs_writepages(mapping->host, wbc, DATA);
2464
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002465 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2466 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08002467 atomic_inc(&sbi->wb_sync_req[DATA]);
2468 else if (atomic_read(&sbi->wb_sync_req[DATA]))
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002469 goto skip_write;
2470
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002471 if (__should_serialize_io(inode, wbc)) {
2472 mutex_lock(&sbi->writepages);
2473 locked = true;
2474 }
2475
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002476 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08002477 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002478 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002479
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002480 if (locked)
2481 mutex_unlock(&sbi->writepages);
2482
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002483 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08002484 atomic_dec(&sbi->wb_sync_req[DATA]);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07002485 /*
2486 * if some pages were truncated, we cannot guarantee its mapping->host
2487 * to detect pending bios.
2488 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002489
Chao Yu4d57b862018-05-30 00:20:41 +08002490 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002491 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002492
2493skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002494 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08002495 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002496 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002497}
2498
Chao Yub0af6d42017-08-02 23:21:48 +08002499static int f2fs_write_data_pages(struct address_space *mapping,
2500 struct writeback_control *wbc)
2501{
2502 struct inode *inode = mapping->host;
2503
2504 return __f2fs_write_data_pages(mapping, wbc,
2505 F2FS_I(inode)->cp_task == current ?
2506 FS_CP_DATA_IO : FS_DATA_IO);
2507}
2508
Chao Yu3aab8f82014-07-02 13:25:04 +08002509static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2510{
2511 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002512 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002513
Eric Biggers95ae2512019-07-22 09:26:24 -07002514 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
2515 if (to > i_size && !f2fs_verity_in_progress(inode)) {
Chao Yua33c1502018-08-05 23:04:25 +08002516 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002517 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08002518
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002519 truncate_pagecache(inode, i_size);
Chao Yuc42d28c2019-02-02 17:33:01 +08002520 if (!IS_NOQUOTA(inode))
2521 f2fs_truncate_blocks(inode, i_size, true);
Chao Yua33c1502018-08-05 23:04:25 +08002522
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08002523 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09002524 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu3aab8f82014-07-02 13:25:04 +08002525 }
2526}
2527
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002528static int prepare_write_begin(struct f2fs_sb_info *sbi,
2529 struct page *page, loff_t pos, unsigned len,
2530 block_t *blk_addr, bool *node_changed)
2531{
2532 struct inode *inode = page->mapping->host;
2533 pgoff_t index = page->index;
2534 struct dnode_of_data dn;
2535 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002536 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00002537 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002538 int err = 0;
Sheng Yong2866fb12018-11-14 19:34:28 +08002539 int flag;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002540
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002541 /*
2542 * we already allocated all the blocks, so we don't need to get
2543 * the block addresses when there is no need to fill the page.
2544 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002545 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
Eric Biggers95ae2512019-07-22 09:26:24 -07002546 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
2547 !f2fs_verity_in_progress(inode))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002548 return 0;
2549
Sheng Yong2866fb12018-11-14 19:34:28 +08002550 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
2551 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
2552 flag = F2FS_GET_BLOCK_DEFAULT;
2553 else
2554 flag = F2FS_GET_BLOCK_PRE_AIO;
2555
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002556 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002557 (pos & PAGE_MASK) >= i_size_read(inode)) {
Sheng Yong2866fb12018-11-14 19:34:28 +08002558 __do_map_lock(sbi, flag, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002559 locked = true;
2560 }
2561restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002562 /* check inline_data */
Chao Yu4d57b862018-05-30 00:20:41 +08002563 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002564 if (IS_ERR(ipage)) {
2565 err = PTR_ERR(ipage);
2566 goto unlock_out;
2567 }
2568
2569 set_new_dnode(&dn, inode, ipage, ipage, 0);
2570
2571 if (f2fs_has_inline_data(inode)) {
Chao Yuf2470372017-07-19 00:19:05 +08002572 if (pos + len <= MAX_INLINE_DATA(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002573 f2fs_do_read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002574 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08002575 if (inode->i_nlink)
2576 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002577 } else {
2578 err = f2fs_convert_inline_page(&dn, page);
2579 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002580 goto out;
2581 if (dn.data_blkaddr == NULL_ADDR)
2582 err = f2fs_get_block(&dn, index);
2583 }
2584 } else if (locked) {
2585 err = f2fs_get_block(&dn, index);
2586 } else {
2587 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2588 dn.data_blkaddr = ei.blk + index - ei.fofs;
2589 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002590 /* hole case */
Chao Yu4d57b862018-05-30 00:20:41 +08002591 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07002592 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002593 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08002594 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2595 true);
Sheng Yong2866fb12018-11-14 19:34:28 +08002596 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002597 locked = true;
2598 goto restart;
2599 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002600 }
2601 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002602
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002603 /* convert_inline_page can make node_changed */
2604 *blk_addr = dn.data_blkaddr;
2605 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002606out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002607 f2fs_put_dnode(&dn);
2608unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002609 if (locked)
Sheng Yong2866fb12018-11-14 19:34:28 +08002610 __do_map_lock(sbi, flag, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002611 return err;
2612}
2613
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002614static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2615 loff_t pos, unsigned len, unsigned flags,
2616 struct page **pagep, void **fsdata)
2617{
2618 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002619 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002620 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002621 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yua2e2e762018-01-15 17:16:46 +08002622 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002623 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002624 int err = 0;
2625
Chao Yu62aed042014-05-06 16:46:04 +08002626 trace_f2fs_write_begin(inode, pos, len, flags);
2627
Chao Yu00e09c02019-08-23 17:58:36 +08002628 if (!f2fs_is_checkpoint_ready(sbi)) {
2629 err = -ENOSPC;
Daniel Rosenberg43549942018-08-20 19:21:43 -07002630 goto fail;
Chao Yu00e09c02019-08-23 17:58:36 +08002631 }
Daniel Rosenberg43549942018-08-20 19:21:43 -07002632
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09002633 if ((f2fs_is_atomic_file(inode) &&
2634 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
2635 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002636 err = -ENOMEM;
Chao Yua2e2e762018-01-15 17:16:46 +08002637 drop_atomic = true;
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002638 goto fail;
2639 }
2640
Jaegeuk Kim5f727392014-11-25 10:59:45 -08002641 /*
2642 * We should check this at this moment to avoid deadlock on inode page
2643 * and #0 page. The locking rule for inline_data conversion should be:
2644 * lock_page(page #0) -> lock_page(inode_page)
2645 */
2646 if (index != 0) {
2647 err = f2fs_convert_inline_inode(inode);
2648 if (err)
2649 goto fail;
2650 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002651repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002652 /*
2653 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2654 * wait_for_stable_page. Will wait that below with our IO control.
2655 */
Chao Yu01eccef2017-10-28 16:52:30 +08002656 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002657 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08002658 if (!page) {
2659 err = -ENOMEM;
2660 goto fail;
2661 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09002662
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002663 *pagep = page;
2664
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002665 err = prepare_write_begin(sbi, page, pos, len,
2666 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002667 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002668 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002669
Chao Yuaf033b22018-09-20 20:05:00 +08002670 if (need_balance && !IS_NOQUOTA(inode) &&
2671 has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002672 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002673 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002674 lock_page(page);
2675 if (page->mapping != mapping) {
2676 /* The page got truncated from under us */
2677 f2fs_put_page(page, 1);
2678 goto repeat;
2679 }
2680 }
2681
Chao Yubae0ee72018-12-25 17:43:42 +08002682 f2fs_wait_on_page_writeback(page, DATA, false, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002683
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002684 if (len == PAGE_SIZE || PageUptodate(page))
2685 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002686
Eric Biggers95ae2512019-07-22 09:26:24 -07002687 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
2688 !f2fs_verity_in_progress(inode)) {
Yunlei He746e2402016-12-20 11:11:35 +08002689 zero_user_segment(page, len, PAGE_SIZE);
2690 return 0;
2691 }
2692
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002693 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002694 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002695 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002696 } else {
Chao Yu93770ab2019-04-15 15:26:32 +08002697 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
2698 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08002699 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08002700 goto fail;
2701 }
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07002702 err = f2fs_submit_page_read(inode, page, blkaddr);
2703 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08002704 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08002705
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002706 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002707 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002708 f2fs_put_page(page, 1);
2709 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002710 }
Chao Yu1563ac72016-07-03 22:05:12 +08002711 if (unlikely(!PageUptodate(page))) {
2712 err = -EIO;
2713 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002714 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002715 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002716 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002717
Chao Yu3aab8f82014-07-02 13:25:04 +08002718fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002719 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002720 f2fs_write_failed(mapping, pos + len);
Chao Yua2e2e762018-01-15 17:16:46 +08002721 if (drop_atomic)
Chao Yu4d57b862018-05-30 00:20:41 +08002722 f2fs_drop_inmem_pages_all(sbi, false);
Chao Yu3aab8f82014-07-02 13:25:04 +08002723 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002724}
2725
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002726static int f2fs_write_end(struct file *file,
2727 struct address_space *mapping,
2728 loff_t pos, unsigned len, unsigned copied,
2729 struct page *page, void *fsdata)
2730{
2731 struct inode *inode = page->mapping->host;
2732
Chao Yudfb2bf32014-05-06 16:47:23 +08002733 trace_f2fs_write_end(inode, pos, len, copied);
2734
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002735 /*
2736 * This should be come from len == PAGE_SIZE, and we expect copied
2737 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2738 * let generic_perform_write() try to copy data again through copied=0.
2739 */
2740 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08002741 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002742 copied = 0;
2743 else
2744 SetPageUptodate(page);
2745 }
2746 if (!copied)
2747 goto unlock_out;
2748
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002749 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002750
Eric Biggers95ae2512019-07-22 09:26:24 -07002751 if (pos + copied > i_size_read(inode) &&
2752 !f2fs_verity_in_progress(inode))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002753 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002754unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002755 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002756 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002757 return copied;
2758}
2759
Omar Sandoval6f673762015-03-16 04:33:52 -07002760static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2761 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002762{
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07002763 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2764 unsigned blkbits = i_blkbits;
2765 unsigned blocksize_mask = (1 << blkbits) - 1;
2766 unsigned long align = offset | iov_iter_alignment(iter);
2767 struct block_device *bdev = inode->i_sb->s_bdev;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002768
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07002769 if (align & blocksize_mask) {
2770 if (bdev)
2771 blkbits = blksize_bits(bdev_logical_block_size(bdev));
2772 blocksize_mask = (1 << blkbits) - 1;
2773 if (align & blocksize_mask)
2774 return -EINVAL;
2775 return 1;
2776 }
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002777 return 0;
2778}
2779
Chao Yu02b16d02018-11-12 00:46:46 +08002780static void f2fs_dio_end_io(struct bio *bio)
2781{
2782 struct f2fs_private_dio *dio = bio->bi_private;
2783
2784 dec_page_count(F2FS_I_SB(dio->inode),
2785 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2786
2787 bio->bi_private = dio->orig_private;
2788 bio->bi_end_io = dio->orig_end_io;
2789
Jaegeuk Kim52225952018-12-13 18:38:33 -08002790 kvfree(dio);
Chao Yu02b16d02018-11-12 00:46:46 +08002791
2792 bio_endio(bio);
2793}
2794
2795static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
2796 loff_t file_offset)
2797{
2798 struct f2fs_private_dio *dio;
2799 bool write = (bio_op(bio) == REQ_OP_WRITE);
Chao Yu02b16d02018-11-12 00:46:46 +08002800
2801 dio = f2fs_kzalloc(F2FS_I_SB(inode),
2802 sizeof(struct f2fs_private_dio), GFP_NOFS);
YueHaibing8e114032019-01-04 01:38:29 +00002803 if (!dio)
Chao Yu02b16d02018-11-12 00:46:46 +08002804 goto out;
Chao Yu02b16d02018-11-12 00:46:46 +08002805
2806 dio->inode = inode;
2807 dio->orig_end_io = bio->bi_end_io;
2808 dio->orig_private = bio->bi_private;
2809 dio->write = write;
2810
2811 bio->bi_end_io = f2fs_dio_end_io;
2812 bio->bi_private = dio;
2813
2814 inc_page_count(F2FS_I_SB(inode),
2815 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2816
2817 submit_bio(bio);
2818 return;
2819out:
2820 bio->bi_status = BLK_STS_IOERR;
2821 bio_endio(bio);
2822}
2823
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002824static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002825{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002826 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002827 struct inode *inode = mapping->host;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002828 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf847c692018-09-27 18:34:52 +08002829 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002830 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002831 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002832 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002833 int err;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002834 enum rw_hint hint = iocb->ki_hint;
Chao Yu63189b72018-03-08 14:22:56 +08002835 int whint_mode = F2FS_OPTION(sbi).whint_mode;
Chao Yuf847c692018-09-27 18:34:52 +08002836 bool do_opu;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002837
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002838 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002839 if (err)
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07002840 return err < 0 ? err : 0;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002841
Chao Yuf847c692018-09-27 18:34:52 +08002842 if (f2fs_force_buffered_io(inode, iocb, iter))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002843 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002844
Chao Yuf847c692018-09-27 18:34:52 +08002845 do_opu = allow_outplace_dio(inode, iocb, iter);
2846
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002847 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002848
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002849 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2850 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2851
Chao Yuf847c692018-09-27 18:34:52 +08002852 if (iocb->ki_flags & IOCB_NOWAIT) {
2853 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002854 iocb->ki_hint = hint;
2855 err = -EAGAIN;
2856 goto out;
2857 }
Chao Yuf847c692018-09-27 18:34:52 +08002858 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
2859 up_read(&fi->i_gc_rwsem[rw]);
2860 iocb->ki_hint = hint;
2861 err = -EAGAIN;
2862 goto out;
2863 }
2864 } else {
2865 down_read(&fi->i_gc_rwsem[rw]);
2866 if (do_opu)
2867 down_read(&fi->i_gc_rwsem[READ]);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002868 }
2869
Chao Yu02b16d02018-11-12 00:46:46 +08002870 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
Chao Yuf9d6d052018-11-13 14:33:45 +08002871 iter, rw == WRITE ? get_data_block_dio_write :
2872 get_data_block_dio, NULL, f2fs_dio_submit_bio,
Chao Yu02b16d02018-11-12 00:46:46 +08002873 DIO_LOCKING | DIO_SKIP_HOLES);
Chao Yuf847c692018-09-27 18:34:52 +08002874
2875 if (do_opu)
2876 up_read(&fi->i_gc_rwsem[READ]);
2877
2878 up_read(&fi->i_gc_rwsem[rw]);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002879
2880 if (rw == WRITE) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002881 if (whint_mode == WHINT_MODE_OFF)
2882 iocb->ki_hint = hint;
Chao Yub0af6d42017-08-02 23:21:48 +08002883 if (err > 0) {
2884 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2885 err);
Chao Yuf847c692018-09-27 18:34:52 +08002886 if (!do_opu)
2887 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yub0af6d42017-08-02 23:21:48 +08002888 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002889 f2fs_write_failed(mapping, offset + count);
Chao Yub0af6d42017-08-02 23:21:48 +08002890 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002891 }
Chao Yu70407fa2014-07-31 21:11:22 +08002892
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002893out:
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002894 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002895
Chao Yu3aab8f82014-07-02 13:25:04 +08002896 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002897}
2898
Chao Yu487261f2015-02-05 17:44:29 +08002899void f2fs_invalidate_page(struct page *page, unsigned int offset,
2900 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002901{
2902 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002903 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002904
Chao Yu487261f2015-02-05 17:44:29 +08002905 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002906 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002907 return;
2908
Chao Yu487261f2015-02-05 17:44:29 +08002909 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08002910 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002911 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08002912 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002913 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08002914 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002915 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +08002916 f2fs_remove_dirty_inode(inode);
Chao Yu933439c2016-10-11 22:57:01 +08002917 }
Chao Yu487261f2015-02-05 17:44:29 +08002918 }
Chao Yudecd36b2015-08-07 18:42:09 +08002919
Chao Yu2baf0782018-07-27 18:15:16 +08002920 clear_cold_data(page);
2921
Chao Yudecd36b2015-08-07 18:42:09 +08002922 if (IS_ATOMIC_WRITTEN_PAGE(page))
Chao Yu4d57b862018-05-30 00:20:41 +08002923 return f2fs_drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002924
Chao Yu240a5912019-03-06 17:30:59 +08002925 f2fs_clear_page_private(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002926}
2927
Chao Yu487261f2015-02-05 17:44:29 +08002928int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002929{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002930 /* If this is dirty page, keep PagePrivate */
2931 if (PageDirty(page))
2932 return 0;
2933
Chao Yudecd36b2015-08-07 18:42:09 +08002934 /* This is atomic written page, keep Private */
2935 if (IS_ATOMIC_WRITTEN_PAGE(page))
2936 return 0;
2937
Chao Yu2baf0782018-07-27 18:15:16 +08002938 clear_cold_data(page);
Chao Yu240a5912019-03-06 17:30:59 +08002939 f2fs_clear_page_private(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002940 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002941}
2942
2943static int f2fs_set_data_page_dirty(struct page *page)
2944{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002945 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002946
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002947 trace_f2fs_set_page_dirty(page, DATA);
2948
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002949 if (!PageUptodate(page))
2950 SetPageUptodate(page);
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002951 if (PageSwapCache(page))
2952 return __set_page_dirty_nobuffers(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002953
Chao Yu5fe45742017-01-07 18:50:26 +08002954 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002955 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002956 f2fs_register_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002957 return 1;
2958 }
2959 /*
2960 * Previously, this page has been registered, we just
2961 * return here.
2962 */
2963 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002964 }
2965
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002966 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07002967 __set_page_dirty_nobuffers(page);
Chao Yu4d57b862018-05-30 00:20:41 +08002968 f2fs_update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002969 return 1;
2970 }
2971 return 0;
2972}
2973
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002974static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2975{
Chao Yu454ae7e2014-04-22 13:34:01 +08002976 struct inode *inode = mapping->host;
2977
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002978 if (f2fs_has_inline_data(inode))
2979 return 0;
2980
2981 /* make sure allocating whole blocks */
2982 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2983 filemap_write_and_wait(mapping);
2984
Chao Yue2b4e2b2015-08-19 19:11:19 +08002985 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002986}
2987
Weichao Guo5b7a4872016-09-20 05:03:27 +08002988#ifdef CONFIG_MIGRATION
2989#include <linux/migrate.h>
2990
2991int f2fs_migrate_page(struct address_space *mapping,
2992 struct page *newpage, struct page *page, enum migrate_mode mode)
2993{
2994 int rc, extra_count;
2995 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2996 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2997
2998 BUG_ON(PageWriteback(page));
2999
3000 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07003001 if (atomic_written) {
3002 if (mode != MIGRATE_SYNC)
3003 return -EBUSY;
3004 if (!mutex_trylock(&fi->inmem_lock))
3005 return -EAGAIN;
3006 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08003007
Chao Yu240a5912019-03-06 17:30:59 +08003008 /* one extra reference was held for atomic_write page */
3009 extra_count = atomic_written ? 1 : 0;
Weichao Guo5b7a4872016-09-20 05:03:27 +08003010 rc = migrate_page_move_mapping(mapping, newpage,
Keith Busch37109692019-07-18 15:58:46 -07003011 page, extra_count);
Weichao Guo5b7a4872016-09-20 05:03:27 +08003012 if (rc != MIGRATEPAGE_SUCCESS) {
3013 if (atomic_written)
3014 mutex_unlock(&fi->inmem_lock);
3015 return rc;
3016 }
3017
3018 if (atomic_written) {
3019 struct inmem_pages *cur;
3020 list_for_each_entry(cur, &fi->inmem_pages, list)
3021 if (cur->page == page) {
3022 cur->page = newpage;
3023 break;
3024 }
3025 mutex_unlock(&fi->inmem_lock);
3026 put_page(page);
3027 get_page(newpage);
3028 }
3029
Chao Yu240a5912019-03-06 17:30:59 +08003030 if (PagePrivate(page)) {
3031 f2fs_set_page_private(newpage, page_private(page));
3032 f2fs_clear_page_private(page);
3033 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08003034
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07003035 if (mode != MIGRATE_SYNC_NO_COPY)
3036 migrate_page_copy(newpage, page);
3037 else
3038 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08003039
3040 return MIGRATEPAGE_SUCCESS;
3041}
3042#endif
3043
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003044#ifdef CONFIG_SWAP
3045/* Copied from generic_swapfile_activate() to check any holes */
3046static int check_swap_activate(struct file *swap_file, unsigned int max)
3047{
3048 struct address_space *mapping = swap_file->f_mapping;
3049 struct inode *inode = mapping->host;
3050 unsigned blocks_per_page;
3051 unsigned long page_no;
3052 unsigned blkbits;
3053 sector_t probe_block;
3054 sector_t last_block;
3055 sector_t lowest_block = -1;
3056 sector_t highest_block = 0;
3057
3058 blkbits = inode->i_blkbits;
3059 blocks_per_page = PAGE_SIZE >> blkbits;
3060
3061 /*
3062 * Map all the blocks into the extent list. This code doesn't try
3063 * to be very smart.
3064 */
3065 probe_block = 0;
3066 page_no = 0;
3067 last_block = i_size_read(inode) >> blkbits;
3068 while ((probe_block + blocks_per_page) <= last_block && page_no < max) {
3069 unsigned block_in_page;
3070 sector_t first_block;
3071
3072 cond_resched();
3073
3074 first_block = bmap(inode, probe_block);
3075 if (first_block == 0)
3076 goto bad_bmap;
3077
3078 /*
3079 * It must be PAGE_SIZE aligned on-disk
3080 */
3081 if (first_block & (blocks_per_page - 1)) {
3082 probe_block++;
3083 goto reprobe;
3084 }
3085
3086 for (block_in_page = 1; block_in_page < blocks_per_page;
3087 block_in_page++) {
3088 sector_t block;
3089
3090 block = bmap(inode, probe_block + block_in_page);
3091 if (block == 0)
3092 goto bad_bmap;
3093 if (block != first_block + block_in_page) {
3094 /* Discontiguity */
3095 probe_block++;
3096 goto reprobe;
3097 }
3098 }
3099
3100 first_block >>= (PAGE_SHIFT - blkbits);
3101 if (page_no) { /* exclude the header page */
3102 if (first_block < lowest_block)
3103 lowest_block = first_block;
3104 if (first_block > highest_block)
3105 highest_block = first_block;
3106 }
3107
3108 page_no++;
3109 probe_block += blocks_per_page;
3110reprobe:
3111 continue;
3112 }
3113 return 0;
3114
3115bad_bmap:
3116 pr_err("swapon: swapfile has holes\n");
3117 return -EINVAL;
3118}
3119
3120static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3121 sector_t *span)
3122{
3123 struct inode *inode = file_inode(file);
3124 int ret;
3125
3126 if (!S_ISREG(inode->i_mode))
3127 return -EINVAL;
3128
3129 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3130 return -EROFS;
3131
3132 ret = f2fs_convert_inline_inode(inode);
3133 if (ret)
3134 return ret;
3135
3136 ret = check_swap_activate(file, sis->max);
3137 if (ret)
3138 return ret;
3139
3140 set_inode_flag(inode, FI_PIN_FILE);
3141 f2fs_precache_extents(inode);
3142 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3143 return 0;
3144}
3145
3146static void f2fs_swap_deactivate(struct file *file)
3147{
3148 struct inode *inode = file_inode(file);
3149
3150 clear_inode_flag(inode, FI_PIN_FILE);
3151}
3152#else
3153static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3154 sector_t *span)
3155{
3156 return -EOPNOTSUPP;
3157}
3158
3159static void f2fs_swap_deactivate(struct file *file)
3160{
3161}
3162#endif
3163
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003164const struct address_space_operations f2fs_dblock_aops = {
3165 .readpage = f2fs_read_data_page,
3166 .readpages = f2fs_read_data_pages,
3167 .writepage = f2fs_write_data_page,
3168 .writepages = f2fs_write_data_pages,
3169 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003170 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003171 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08003172 .invalidatepage = f2fs_invalidate_page,
3173 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003174 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09003175 .bmap = f2fs_bmap,
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003176 .swap_activate = f2fs_swap_activate,
3177 .swap_deactivate = f2fs_swap_deactivate,
Weichao Guo5b7a4872016-09-20 05:03:27 +08003178#ifdef CONFIG_MIGRATION
3179 .migratepage = f2fs_migrate_page,
3180#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003181};
Eric Biggers6dbb1792018-04-18 11:09:48 -07003182
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05003183void f2fs_clear_page_cache_dirty_tag(struct page *page)
Chao Yuaec2f722018-05-26 18:03:35 +08003184{
3185 struct address_space *mapping = page_mapping(page);
3186 unsigned long flags;
3187
3188 xa_lock_irqsave(&mapping->i_pages, flags);
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05003189 __xa_clear_mark(&mapping->i_pages, page_index(page),
Chao Yuaec2f722018-05-26 18:03:35 +08003190 PAGECACHE_TAG_DIRTY);
3191 xa_unlock_irqrestore(&mapping->i_pages, flags);
3192}
3193
Eric Biggers6dbb1792018-04-18 11:09:48 -07003194int __init f2fs_init_post_read_processing(void)
3195{
Eric Biggers95ae2512019-07-22 09:26:24 -07003196 bio_post_read_ctx_cache =
3197 kmem_cache_create("f2fs_bio_post_read_ctx",
3198 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
Eric Biggers6dbb1792018-04-18 11:09:48 -07003199 if (!bio_post_read_ctx_cache)
3200 goto fail;
3201 bio_post_read_ctx_pool =
3202 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
3203 bio_post_read_ctx_cache);
3204 if (!bio_post_read_ctx_pool)
3205 goto fail_free_cache;
3206 return 0;
3207
3208fail_free_cache:
3209 kmem_cache_destroy(bio_post_read_ctx_cache);
3210fail:
3211 return -ENOMEM;
3212}
3213
3214void __exit f2fs_destroy_post_read_processing(void)
3215{
3216 mempool_destroy(bio_post_read_ctx_pool);
3217 kmem_cache_destroy(bio_post_read_ctx_cache);
3218}