blob: 20f31ff47e05ffb4a21e031f686790cc17b32f61 [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>
Christoph Hellwig10c5db22020-05-23 09:30:11 +020022#include <linux/fiemap.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090023
24#include "f2fs.h"
25#include "node.h"
26#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080027#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090028#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090029
Eric Biggers6dbb1792018-04-18 11:09:48 -070030#define NUM_PREALLOC_POST_READ_CTXS 128
31
32static struct kmem_cache *bio_post_read_ctx_cache;
Chao Yu0b20fce2019-09-30 18:53:25 +080033static struct kmem_cache *bio_entry_slab;
Eric Biggers6dbb1792018-04-18 11:09:48 -070034static mempool_t *bio_post_read_ctx_pool;
Chao Yuf5438052019-12-04 09:52:58 +080035static struct bio_set f2fs_bioset;
36
37#define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
38
39int __init f2fs_init_bioset(void)
40{
41 if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
42 0, BIOSET_NEED_BVECS))
43 return -ENOMEM;
44 return 0;
45}
46
47void f2fs_destroy_bioset(void)
48{
49 bioset_exit(&f2fs_bioset);
50}
51
52static inline struct bio *__f2fs_bio_alloc(gfp_t gfp_mask,
53 unsigned int nr_iovecs)
54{
55 return bio_alloc_bioset(gfp_mask, nr_iovecs, &f2fs_bioset);
56}
57
Chao Yuca9e9682020-02-18 18:21:34 +080058struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool noio)
Chao Yuf5438052019-12-04 09:52:58 +080059{
Chao Yuca9e9682020-02-18 18:21:34 +080060 if (noio) {
Chao Yuf5438052019-12-04 09:52:58 +080061 /* No failure on bio allocation */
Chao Yuca9e9682020-02-18 18:21:34 +080062 return __f2fs_bio_alloc(GFP_NOIO, npages);
Chao Yuf5438052019-12-04 09:52:58 +080063 }
Chao Yuca9e9682020-02-18 18:21:34 +080064
Chao Yuf5438052019-12-04 09:52:58 +080065 if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
66 f2fs_show_injection_info(sbi, FAULT_ALLOC_BIO);
67 return NULL;
68 }
69
70 return __f2fs_bio_alloc(GFP_KERNEL, npages);
71}
Eric Biggers6dbb1792018-04-18 11:09:48 -070072
Chao Yu36951b32016-11-16 10:41:20 +080073static bool __is_cp_guaranteed(struct page *page)
74{
75 struct address_space *mapping = page->mapping;
76 struct inode *inode;
77 struct f2fs_sb_info *sbi;
78
79 if (!mapping)
80 return false;
81
Chao Yu4c8ff702019-11-01 18:07:14 +080082 if (f2fs_is_compressed_page(page))
83 return false;
84
Chao Yu36951b32016-11-16 10:41:20 +080085 inode = mapping->host;
86 sbi = F2FS_I_SB(inode);
87
88 if (inode->i_ino == F2FS_META_INO(sbi) ||
89 inode->i_ino == F2FS_NODE_INO(sbi) ||
90 S_ISDIR(inode->i_mode) ||
Chao Yue7a4feb2018-05-08 14:06:03 +080091 (S_ISREG(inode->i_mode) &&
Chao Yuaf033b22018-09-20 20:05:00 +080092 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
Chao Yu36951b32016-11-16 10:41:20 +080093 is_cold_data(page))
94 return true;
95 return false;
96}
97
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -070098static enum count_type __read_io_type(struct page *page)
99{
Jaegeuk Kim4969c062019-07-01 19:15:29 -0700100 struct address_space *mapping = page_file_mapping(page);
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700101
102 if (mapping) {
103 struct inode *inode = mapping->host;
104 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
105
106 if (inode->i_ino == F2FS_META_INO(sbi))
107 return F2FS_RD_META;
108
109 if (inode->i_ino == F2FS_NODE_INO(sbi))
110 return F2FS_RD_NODE;
111 }
112 return F2FS_RD_DATA;
113}
114
Eric Biggers6dbb1792018-04-18 11:09:48 -0700115/* postprocessing steps for read bios */
116enum bio_post_read_step {
Eric Biggers6dbb1792018-04-18 11:09:48 -0700117 STEP_DECRYPT,
Chao Yu03382f12020-04-21 19:36:21 +0800118 STEP_DECOMPRESS_NOWQ, /* handle normal cluster data inplace */
119 STEP_DECOMPRESS, /* handle compressed cluster data in workqueue */
Eric Biggers95ae2512019-07-22 09:26:24 -0700120 STEP_VERITY,
Eric Biggers6dbb1792018-04-18 11:09:48 -0700121};
122
123struct bio_post_read_ctx {
124 struct bio *bio;
Chao Yu4c8ff702019-11-01 18:07:14 +0800125 struct f2fs_sb_info *sbi;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700126 struct work_struct work;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700127 unsigned int enabled_steps;
128};
129
Chao Yu4c8ff702019-11-01 18:07:14 +0800130static void __read_end_io(struct bio *bio, bool compr, bool verity)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900131{
Eric Biggers6dbb1792018-04-18 11:09:48 -0700132 struct page *page;
133 struct bio_vec *bv;
Ming Lei6dc4f102019-02-15 19:13:19 +0800134 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900135
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200136 bio_for_each_segment_all(bv, bio, iter_all) {
Eric Biggers6dbb1792018-04-18 11:09:48 -0700137 page = bv->bv_page;
138
Chao Yu4c8ff702019-11-01 18:07:14 +0800139#ifdef CONFIG_F2FS_FS_COMPRESSION
140 if (compr && f2fs_is_compressed_page(page)) {
141 f2fs_decompress_pages(bio, page, verity);
142 continue;
143 }
Chao Yu748785652020-03-24 14:20:57 +0800144 if (verity)
145 continue;
Chao Yu4c8ff702019-11-01 18:07:14 +0800146#endif
147
Eric Biggers6dbb1792018-04-18 11:09:48 -0700148 /* PG_error was set if any post_read step failed */
149 if (bio->bi_status || PageError(page)) {
150 ClearPageUptodate(page);
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -0700151 /* will re-read again later */
152 ClearPageError(page);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700153 } else {
154 SetPageUptodate(page);
155 }
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700156 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
Eric Biggers6dbb1792018-04-18 11:09:48 -0700157 unlock_page(page);
158 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800159}
160
161static void f2fs_release_read_bio(struct bio *bio);
162static void __f2fs_read_end_io(struct bio *bio, bool compr, bool verity)
163{
164 if (!compr)
165 __read_end_io(bio, false, verity);
166 f2fs_release_read_bio(bio);
167}
168
169static void f2fs_decompress_bio(struct bio *bio, bool verity)
170{
171 __read_end_io(bio, true, verity);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700172}
173
174static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
175
Chao Yu4c8ff702019-11-01 18:07:14 +0800176static void f2fs_decrypt_work(struct bio_post_read_ctx *ctx)
Eric Biggers6dbb1792018-04-18 11:09:48 -0700177{
Eric Biggers6dbb1792018-04-18 11:09:48 -0700178 fscrypt_decrypt_bio(ctx->bio);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700179}
180
Chao Yu4c8ff702019-11-01 18:07:14 +0800181static void f2fs_decompress_work(struct bio_post_read_ctx *ctx)
182{
183 f2fs_decompress_bio(ctx->bio, ctx->enabled_steps & (1 << STEP_VERITY));
184}
185
186#ifdef CONFIG_F2FS_FS_COMPRESSION
187static void f2fs_verify_pages(struct page **rpages, unsigned int cluster_size)
188{
189 f2fs_decompress_end_io(rpages, cluster_size, false, true);
190}
191
192static void f2fs_verify_bio(struct bio *bio)
193{
Chao Yu79bbefb2020-03-23 17:43:04 +0800194 struct bio_vec *bv;
195 struct bvec_iter_all iter_all;
Chao Yu4c8ff702019-11-01 18:07:14 +0800196
Chao Yu79bbefb2020-03-23 17:43:04 +0800197 bio_for_each_segment_all(bv, bio, iter_all) {
198 struct page *page = bv->bv_page;
199 struct decompress_io_ctx *dic;
200
201 dic = (struct decompress_io_ctx *)page_private(page);
202
203 if (dic) {
204 if (refcount_dec_not_one(&dic->ref))
205 continue;
206 f2fs_verify_pages(dic->rpages,
207 dic->cluster_size);
208 f2fs_free_dic(dic);
209 continue;
210 }
211
212 if (bio->bi_status || PageError(page))
213 goto clear_uptodate;
214
215 if (fsverity_verify_page(page)) {
216 SetPageUptodate(page);
217 goto unlock;
218 }
219clear_uptodate:
220 ClearPageUptodate(page);
221 ClearPageError(page);
222unlock:
Chao Yu748785652020-03-24 14:20:57 +0800223 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
Chao Yu79bbefb2020-03-23 17:43:04 +0800224 unlock_page(page);
225 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800226}
227#endif
228
229static void f2fs_verity_work(struct work_struct *work)
Eric Biggers95ae2512019-07-22 09:26:24 -0700230{
231 struct bio_post_read_ctx *ctx =
232 container_of(work, struct bio_post_read_ctx, work);
Eric Biggers644c8c92019-12-31 12:14:16 -0600233 struct bio *bio = ctx->bio;
234#ifdef CONFIG_F2FS_FS_COMPRESSION
235 unsigned int enabled_steps = ctx->enabled_steps;
236#endif
237
238 /*
239 * fsverity_verify_bio() may call readpages() again, and while verity
240 * will be disabled for this, decryption may still be needed, resulting
241 * in another bio_post_read_ctx being allocated. So to prevent
242 * deadlocks we need to release the current ctx to the mempool first.
243 * This assumes that verity is the last post-read step.
244 */
245 mempool_free(ctx, bio_post_read_ctx_pool);
246 bio->bi_private = NULL;
Eric Biggers95ae2512019-07-22 09:26:24 -0700247
Chao Yu4c8ff702019-11-01 18:07:14 +0800248#ifdef CONFIG_F2FS_FS_COMPRESSION
249 /* previous step is decompression */
Eric Biggers644c8c92019-12-31 12:14:16 -0600250 if (enabled_steps & (1 << STEP_DECOMPRESS)) {
251 f2fs_verify_bio(bio);
252 f2fs_release_read_bio(bio);
Chao Yu4c8ff702019-11-01 18:07:14 +0800253 return;
254 }
255#endif
256
Eric Biggers644c8c92019-12-31 12:14:16 -0600257 fsverity_verify_bio(bio);
258 __f2fs_read_end_io(bio, false, false);
Chao Yu4c8ff702019-11-01 18:07:14 +0800259}
260
261static void f2fs_post_read_work(struct work_struct *work)
262{
263 struct bio_post_read_ctx *ctx =
264 container_of(work, struct bio_post_read_ctx, work);
265
266 if (ctx->enabled_steps & (1 << STEP_DECRYPT))
267 f2fs_decrypt_work(ctx);
268
269 if (ctx->enabled_steps & (1 << STEP_DECOMPRESS))
270 f2fs_decompress_work(ctx);
271
272 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
273 INIT_WORK(&ctx->work, f2fs_verity_work);
274 fsverity_enqueue_verify_work(&ctx->work);
275 return;
276 }
277
278 __f2fs_read_end_io(ctx->bio,
279 ctx->enabled_steps & (1 << STEP_DECOMPRESS), false);
280}
281
282static void f2fs_enqueue_post_read_work(struct f2fs_sb_info *sbi,
283 struct work_struct *work)
284{
285 queue_work(sbi->post_read_wq, work);
Eric Biggers95ae2512019-07-22 09:26:24 -0700286}
287
Eric Biggers6dbb1792018-04-18 11:09:48 -0700288static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
289{
Eric Biggers95ae2512019-07-22 09:26:24 -0700290 /*
291 * We use different work queues for decryption and for verity because
292 * verity may require reading metadata pages that need decryption, and
293 * we shouldn't recurse to the same workqueue.
294 */
Chao Yu4c8ff702019-11-01 18:07:14 +0800295
296 if (ctx->enabled_steps & (1 << STEP_DECRYPT) ||
297 ctx->enabled_steps & (1 << STEP_DECOMPRESS)) {
298 INIT_WORK(&ctx->work, f2fs_post_read_work);
299 f2fs_enqueue_post_read_work(ctx->sbi, &ctx->work);
300 return;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700301 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800302
303 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
304 INIT_WORK(&ctx->work, f2fs_verity_work);
305 fsverity_enqueue_verify_work(&ctx->work);
306 return;
307 }
308
309 __f2fs_read_end_io(ctx->bio, false, false);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700310}
311
312static bool f2fs_bio_post_read_required(struct bio *bio)
313{
Chao Yu4c8ff702019-11-01 18:07:14 +0800314 return bio->bi_private;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700315}
316
317static void f2fs_read_end_io(struct bio *bio)
318{
Chao Yuc45d6002019-11-01 17:53:23 +0800319 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
320
321 if (time_to_inject(sbi, FAULT_READ_IO)) {
322 f2fs_show_injection_info(sbi, FAULT_READ_IO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200323 bio->bi_status = BLK_STS_IOERR;
Chao Yu55523512017-02-25 11:08:28 +0800324 }
Chao Yu8b038c72016-09-18 23:30:07 +0800325
Eric Biggers6dbb1792018-04-18 11:09:48 -0700326 if (f2fs_bio_post_read_required(bio)) {
327 struct bio_post_read_ctx *ctx = bio->bi_private;
328
Eric Biggers6dbb1792018-04-18 11:09:48 -0700329 bio_post_read_processing(ctx);
330 return;
Chao Yu12377022015-05-25 18:03:38 +0800331 }
332
Chao Yu4c8ff702019-11-01 18:07:14 +0800333 __f2fs_read_end_io(bio, false, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900334}
335
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200336static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900337{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +0900338 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -0800339 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800340 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900341
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800342 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800343 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800344 bio->bi_status = BLK_STS_IOERR;
345 }
346
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200347 bio_for_each_segment_all(bvec, bio, iter_all) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900348 struct page *page = bvec->bv_page;
Chao Yu36951b32016-11-16 10:41:20 +0800349 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900350
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800351 if (IS_DUMMY_WRITTEN_PAGE(page)) {
352 set_page_private(page, (unsigned long)NULL);
353 ClearPagePrivate(page);
354 unlock_page(page);
355 mempool_free(page, sbi->write_io_dummy);
356
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200357 if (unlikely(bio->bi_status))
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800358 f2fs_stop_checkpoint(sbi, true);
359 continue;
360 }
361
Eric Biggersd2d07272019-05-20 09:29:39 -0700362 fscrypt_finalize_bounce_page(&page);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700363
Chao Yu4c8ff702019-11-01 18:07:14 +0800364#ifdef CONFIG_F2FS_FS_COMPRESSION
365 if (f2fs_is_compressed_page(page)) {
366 f2fs_compress_write_end_io(bio, page);
367 continue;
368 }
369#endif
370
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200371 if (unlikely(bio->bi_status)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700372 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kimb1ca3212017-12-31 16:26:38 -0800373 if (type == F2FS_WB_CP_DATA)
374 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900375 }
Yunlei He7dff55d2018-01-11 14:19:32 +0800376
377 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
378 page->index != nid_of_node(page));
379
Chao Yu36951b32016-11-16 10:41:20 +0800380 dec_page_count(sbi, type);
Chao Yu50fa53e2018-08-02 23:03:19 +0800381 if (f2fs_in_warm_node_list(sbi, page))
382 f2fs_del_fsync_node_entry(sbi, page);
Chao Yu36951b32016-11-16 10:41:20 +0800383 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900384 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800385 }
Chao Yu36951b32016-11-16 10:41:20 +0800386 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700387 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900388 wake_up(&sbi->cp_wait);
389
390 bio_put(bio);
391}
392
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700393struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
394 block_t blk_addr, struct bio *bio)
395{
396 struct block_device *bdev = sbi->sb->s_bdev;
397 int i;
398
Damien Le Moal09168782019-03-16 09:13:06 +0900399 if (f2fs_is_multi_device(sbi)) {
400 for (i = 0; i < sbi->s_ndevs; i++) {
401 if (FDEV(i).start_blk <= blk_addr &&
402 FDEV(i).end_blk >= blk_addr) {
403 blk_addr -= FDEV(i).start_blk;
404 bdev = FDEV(i).bdev;
405 break;
406 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700407 }
408 }
409 if (bio) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200410 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700411 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
412 }
413 return bdev;
414}
415
416int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
417{
418 int i;
419
Damien Le Moal09168782019-03-16 09:13:06 +0900420 if (!f2fs_is_multi_device(sbi))
421 return 0;
422
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700423 for (i = 0; i < sbi->s_ndevs; i++)
424 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
425 return i;
426 return 0;
427}
428
Chao Yu7a88ddb2020-02-27 19:30:05 +0800429/*
430 * Return true, if pre_bio's bdev is same as its target device.
431 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700432static bool __same_bdev(struct f2fs_sb_info *sbi,
433 block_t blk_addr, struct bio *bio)
434{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200435 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
436 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700437}
438
Chao Yub757f6e2019-08-23 17:58:35 +0800439static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
Gu Zheng940a6d32013-12-20 17:39:59 +0800440{
Chao Yub757f6e2019-08-23 17:58:35 +0800441 struct f2fs_sb_info *sbi = fio->sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800442 struct bio *bio;
443
Chao Yud62fe972017-10-28 16:52:31 +0800444 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800445
Chao Yub757f6e2019-08-23 17:58:35 +0800446 f2fs_target_device(sbi, fio->new_blkaddr, bio);
447 if (is_read_io(fio->op)) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900448 bio->bi_end_io = f2fs_read_end_io;
449 bio->bi_private = NULL;
450 } else {
451 bio->bi_end_io = f2fs_write_end_io;
452 bio->bi_private = sbi;
Chao Yub757f6e2019-08-23 17:58:35 +0800453 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
454 fio->type, fio->temp);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900455 }
Chao Yub757f6e2019-08-23 17:58:35 +0800456 if (fio->io_wbc)
457 wbc_init_bio(fio->io_wbc, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800458
459 return bio;
460}
461
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700462static inline void __submit_bio(struct f2fs_sb_info *sbi,
463 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700464{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700465 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800466 unsigned int start;
467
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800468 if (type != DATA && type != NODE)
469 goto submit_io;
470
Chao Yub0332a02020-02-14 17:44:12 +0800471 if (f2fs_lfs_mode(sbi) && current->plug)
Tiezhu Yang3bb09a02018-02-06 08:21:45 +0800472 blk_finish_plug(current->plug);
473
Chao Yu8223ecc2019-08-28 17:33:38 +0800474 if (F2FS_IO_ALIGNED(sbi))
475 goto submit_io;
476
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800477 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
478 start %= F2FS_IO_SIZE(sbi);
479
480 if (start == 0)
481 goto submit_io;
482
483 /* fill dummy pages */
484 for (; start < F2FS_IO_SIZE(sbi); start++) {
485 struct page *page =
486 mempool_alloc(sbi->write_io_dummy,
Gao Xiangbc73a4b2019-02-19 10:31:52 +0800487 GFP_NOIO | __GFP_NOFAIL);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800488 f2fs_bug_on(sbi, !page);
489
Gao Xiangbc73a4b2019-02-19 10:31:52 +0800490 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800491 SetPagePrivate(page);
492 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
493 lock_page(page);
494 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
495 f2fs_bug_on(sbi, 1);
496 }
497 /*
498 * In the NODE case, we lose next block address chain. So, we
499 * need to do checkpoint in f2fs_sync_file.
500 */
501 if (type == NODE)
502 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700503 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800504submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800505 if (is_read_io(bio_op(bio)))
506 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
507 else
508 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500509 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700510}
511
Chao Yu4c8ff702019-11-01 18:07:14 +0800512void f2fs_submit_bio(struct f2fs_sb_info *sbi,
513 struct bio *bio, enum page_type type)
514{
515 __submit_bio(sbi, bio, type);
516}
517
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700518static void __attach_io_flag(struct f2fs_io_info *fio)
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700519{
520 struct f2fs_sb_info *sbi = fio->sbi;
521 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700522 unsigned int io_flag, fua_flag, meta_flag;
523
524 if (fio->type == DATA)
525 io_flag = sbi->data_io_flag;
526 else if (fio->type == NODE)
527 io_flag = sbi->node_io_flag;
528 else
529 return;
530
531 fua_flag = io_flag & temp_mask;
532 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
533
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700534 /*
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700535 * data/node io flag bits per temp:
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700536 * REQ_META | REQ_FUA |
537 * 5 | 4 | 3 | 2 | 1 | 0 |
538 * Cold | Warm | Hot | Cold | Warm | Hot |
539 */
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700540 if ((1 << fio->temp) & meta_flag)
541 fio->op_flags |= REQ_META;
542 if ((1 << fio->temp) & fua_flag)
543 fio->op_flags |= REQ_FUA;
544}
545
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900546static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900547{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900548 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900549
550 if (!io->bio)
551 return;
552
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700553 __attach_io_flag(fio);
Mike Christie04d328d2016-06-05 14:31:55 -0500554 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
555
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800556 if (is_read_io(fio->op))
557 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
558 else
559 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
560
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700561 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900562 io->bio = NULL;
563}
564
Chao Yu8648de22019-02-19 16:15:29 +0800565static bool __has_merged_page(struct bio *bio, struct inode *inode,
Chao Yubab475c2018-09-27 23:41:16 +0800566 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800567{
Chao Yu0fd785e2016-01-18 18:24:59 +0800568 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800569 struct bvec_iter_all iter_all;
Chao Yu0fd785e2016-01-18 18:24:59 +0800570
Chao Yu8648de22019-02-19 16:15:29 +0800571 if (!bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800572 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800573
Chao Yubab475c2018-09-27 23:41:16 +0800574 if (!inode && !page && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800575 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800576
Chao Yu8648de22019-02-19 16:15:29 +0800577 bio_for_each_segment_all(bvec, bio, iter_all) {
Chao Yu4c8ff702019-11-01 18:07:14 +0800578 struct page *target = bvec->bv_page;
Chao Yu0fd785e2016-01-18 18:24:59 +0800579
Chao Yu4c8ff702019-11-01 18:07:14 +0800580 if (fscrypt_is_bounce_page(target)) {
Eric Biggersd2d07272019-05-20 09:29:39 -0700581 target = fscrypt_pagecache_page(target);
Chao Yu4c8ff702019-11-01 18:07:14 +0800582 if (IS_ERR(target))
583 continue;
584 }
585 if (f2fs_is_compressed_page(target)) {
586 target = f2fs_compress_control_page(target);
587 if (IS_ERR(target))
588 continue;
589 }
Chao Yu0fd785e2016-01-18 18:24:59 +0800590
Chao Yu0c3a5792016-01-18 18:28:11 +0800591 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800592 return true;
Chao Yubab475c2018-09-27 23:41:16 +0800593 if (page && page == target)
594 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800595 if (ino && ino == ino_of_node(target))
596 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800597 }
598
Chao Yu0fd785e2016-01-18 18:24:59 +0800599 return false;
600}
601
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700602static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700603 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900604{
605 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700606 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900607
Chao Yudf0f8dc2014-03-22 14:57:23 +0800608 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900609
610 /* change META to META_FLUSH in the checkpoint procedure */
611 if (type >= META_FLUSH) {
612 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500613 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200614 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600615 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800616 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900617 }
618 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800619 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900620}
621
Jaegeuk Kima912b542017-05-10 11:18:25 -0700622static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800623 struct inode *inode, struct page *page,
624 nid_t ino, enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800625{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700626 enum temp_type temp;
Yunlong Song1e771e82018-11-13 11:57:32 +0800627 bool ret = true;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700628
629 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
Yunlong Song1e771e82018-11-13 11:57:32 +0800630 if (!force) {
631 enum page_type btype = PAGE_TYPE_OF_BIO(type);
632 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700633
Yunlong Song1e771e82018-11-13 11:57:32 +0800634 down_read(&io->io_rwsem);
Chao Yu8648de22019-02-19 16:15:29 +0800635 ret = __has_merged_page(io->bio, inode, page, ino);
Yunlong Song1e771e82018-11-13 11:57:32 +0800636 up_read(&io->io_rwsem);
637 }
638 if (ret)
639 __f2fs_submit_merged_write(sbi, type, temp);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700640
641 /* TODO: use HOT temp only for meta pages now. */
642 if (type >= META)
643 break;
644 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800645}
646
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700647void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800648{
Hariprasad Kelamadcc00f2019-04-06 16:29:36 +0530649 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900650}
651
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700652void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800653 struct inode *inode, struct page *page,
654 nid_t ino, enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900655{
Chao Yubab475c2018-09-27 23:41:16 +0800656 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900657}
658
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700659void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800660{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700661 f2fs_submit_merged_write(sbi, DATA);
662 f2fs_submit_merged_write(sbi, NODE);
663 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800664}
665
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900666/*
667 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300668 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900669 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700670int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900671{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900672 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700673 struct page *page = fio->encrypted_page ?
674 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900675
Chao Yuc9b60782018-08-01 19:13:44 +0800676 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800677 fio->is_por ? META_POR : (__is_meta_io(fio) ?
678 META_GENERIC : DATA_GENERIC_ENHANCE)))
Chao Yu10f966b2019-06-20 11:36:14 +0800679 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +0800680
Chao Yu2ace38e2014-12-24 16:08:14 +0800681 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700682 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900683
684 /* Allocate a new bio */
Chao Yub757f6e2019-08-23 17:58:35 +0800685 bio = __bio_alloc(fio, 1);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900686
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300687 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900688 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900689 return -EFAULT;
690 }
Chao Yu78efac52018-10-22 23:24:28 +0800691
692 if (fio->io_wbc && !is_read_io(fio->op))
Tejun Heo34e51a52019-06-27 13:39:49 -0700693 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu78efac52018-10-22 23:24:28 +0800694
Jaegeuk Kimb7b911d2020-06-04 16:45:30 -0700695 __attach_io_flag(fio);
Mike Christie04d328d2016-06-05 14:31:55 -0500696 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900697
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700698 inc_page_count(fio->sbi, is_read_io(fio->op) ?
699 __read_io_type(page): WB_DATA_TYPE(fio->page));
Chao Yu4c58ed02018-10-22 09:12:51 +0800700
701 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900702 return 0;
703}
704
Chao Yu8896cbd2019-07-12 16:55:41 +0800705static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
706 block_t last_blkaddr, block_t cur_blkaddr)
707{
708 if (last_blkaddr + 1 != cur_blkaddr)
709 return false;
710 return __same_bdev(sbi, cur_blkaddr, bio);
711}
712
713static bool io_type_is_mergeable(struct f2fs_bio_info *io,
714 struct f2fs_io_info *fio)
715{
716 if (io->fio.op != fio->op)
717 return false;
718 return io->fio.op_flags == fio->op_flags;
719}
720
721static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
722 struct f2fs_bio_info *io,
723 struct f2fs_io_info *fio,
724 block_t last_blkaddr,
725 block_t cur_blkaddr)
726{
Chao Yuc72db712019-07-12 16:55:42 +0800727 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
728 unsigned int filled_blocks =
729 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
730 unsigned int io_size = F2FS_IO_SIZE(sbi);
731 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
732
733 /* IOs in bio is aligned and left space of vectors is not enough */
734 if (!(filled_blocks % io_size) && left_vecs < io_size)
735 return false;
736 }
Chao Yu8896cbd2019-07-12 16:55:41 +0800737 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
738 return false;
739 return io_type_is_mergeable(io, fio);
740}
741
Chao Yu0b20fce2019-09-30 18:53:25 +0800742static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
743 struct page *page, enum temp_type temp)
744{
745 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
746 struct bio_entry *be;
747
748 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
749 be->bio = bio;
750 bio_get(bio);
751
752 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
753 f2fs_bug_on(sbi, 1);
754
755 down_write(&io->bio_list_lock);
756 list_add_tail(&be->list, &io->bio_list);
757 up_write(&io->bio_list_lock);
758}
759
760static void del_bio_entry(struct bio_entry *be)
761{
762 list_del(&be->list);
763 kmem_cache_free(bio_entry_slab, be);
764}
765
766static int add_ipu_page(struct f2fs_sb_info *sbi, struct bio **bio,
767 struct page *page)
768{
769 enum temp_type temp;
770 bool found = false;
771 int ret = -EAGAIN;
772
773 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
774 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
775 struct list_head *head = &io->bio_list;
776 struct bio_entry *be;
777
778 down_write(&io->bio_list_lock);
779 list_for_each_entry(be, head, list) {
780 if (be->bio != *bio)
781 continue;
782
783 found = true;
784
Chao Yu4c8ff702019-11-01 18:07:14 +0800785 if (bio_add_page(*bio, page, PAGE_SIZE, 0) ==
786 PAGE_SIZE) {
Chao Yu0b20fce2019-09-30 18:53:25 +0800787 ret = 0;
788 break;
789 }
790
791 /* bio is full */
792 del_bio_entry(be);
793 __submit_bio(sbi, *bio, DATA);
794 break;
795 }
796 up_write(&io->bio_list_lock);
797 }
798
799 if (ret) {
800 bio_put(*bio);
801 *bio = NULL;
802 }
803
804 return ret;
805}
806
807void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
808 struct bio **bio, struct page *page)
809{
810 enum temp_type temp;
811 bool found = false;
812 struct bio *target = bio ? *bio : NULL;
813
814 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
815 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
816 struct list_head *head = &io->bio_list;
817 struct bio_entry *be;
818
819 if (list_empty(head))
820 continue;
821
822 down_read(&io->bio_list_lock);
823 list_for_each_entry(be, head, list) {
824 if (target)
825 found = (target == be->bio);
826 else
827 found = __has_merged_page(be->bio, NULL,
828 page, 0);
829 if (found)
830 break;
831 }
832 up_read(&io->bio_list_lock);
833
834 if (!found)
835 continue;
836
837 found = false;
838
839 down_write(&io->bio_list_lock);
840 list_for_each_entry(be, head, list) {
841 if (target)
842 found = (target == be->bio);
843 else
844 found = __has_merged_page(be->bio, NULL,
845 page, 0);
846 if (found) {
847 target = be->bio;
848 del_bio_entry(be);
849 break;
850 }
851 }
852 up_write(&io->bio_list_lock);
853 }
854
855 if (found)
856 __submit_bio(sbi, target, DATA);
857 if (bio && *bio) {
858 bio_put(*bio);
859 *bio = NULL;
860 }
861}
862
Chao Yu8648de22019-02-19 16:15:29 +0800863int f2fs_merge_page_bio(struct f2fs_io_info *fio)
864{
865 struct bio *bio = *fio->bio;
866 struct page *page = fio->encrypted_page ?
867 fio->encrypted_page : fio->page;
868
869 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
870 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
Chao Yu10f966b2019-06-20 11:36:14 +0800871 return -EFSCORRUPTED;
Chao Yu8648de22019-02-19 16:15:29 +0800872
873 trace_f2fs_submit_page_bio(page, fio);
874 f2fs_trace_ios(fio, 0);
875
Chao Yu8896cbd2019-07-12 16:55:41 +0800876 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
Chao Yu0b20fce2019-09-30 18:53:25 +0800877 fio->new_blkaddr))
878 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
Chao Yu8648de22019-02-19 16:15:29 +0800879alloc_new:
880 if (!bio) {
Chao Yub757f6e2019-08-23 17:58:35 +0800881 bio = __bio_alloc(fio, BIO_MAX_PAGES);
Jaegeuk Kimb7b911d2020-06-04 16:45:30 -0700882 __attach_io_flag(fio);
Chao Yu8648de22019-02-19 16:15:29 +0800883 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Chao Yu8648de22019-02-19 16:15:29 +0800884
Chao Yu0b20fce2019-09-30 18:53:25 +0800885 add_bio_entry(fio->sbi, bio, page, fio->temp);
886 } else {
887 if (add_ipu_page(fio->sbi, &bio, page))
888 goto alloc_new;
Chao Yu8648de22019-02-19 16:15:29 +0800889 }
890
891 if (fio->io_wbc)
Linus Torvalds9637d512019-07-15 21:20:52 -0700892 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu8648de22019-02-19 16:15:29 +0800893
894 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
895
896 *fio->last_block = fio->new_blkaddr;
897 *fio->bio = bio;
898
899 return 0;
900}
901
Chao Yufe16efe2018-05-28 23:47:18 +0800902void f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900903{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700904 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900905 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700906 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700907 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900908
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700909 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900910
Chao Yufb830fc2017-05-19 23:37:01 +0800911 down_write(&io->io_rwsem);
912next:
913 if (fio->in_list) {
914 spin_lock(&io->io_lock);
915 if (list_empty(&io->io_list)) {
916 spin_unlock(&io->io_lock);
Chao Yufe16efe2018-05-28 23:47:18 +0800917 goto out;
Chao Yufb830fc2017-05-19 23:37:01 +0800918 }
919 fio = list_first_entry(&io->io_list,
920 struct f2fs_io_info, list);
921 list_del(&fio->list);
922 spin_unlock(&io->io_lock);
923 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900924
Chao Yu93770ab2019-04-15 15:26:32 +0800925 verify_fio_blkaddr(fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900926
Chao Yu4c8ff702019-11-01 18:07:14 +0800927 if (fio->encrypted_page)
928 bio_page = fio->encrypted_page;
929 else if (fio->compressed_page)
930 bio_page = fio->compressed_page;
931 else
932 bio_page = fio->page;
Chao Yu36951b32016-11-16 10:41:20 +0800933
Thomas Meyerebf7c522017-10-07 16:02:21 +0200934 /* set submitted = true as a return value */
935 fio->submitted = true;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800936
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700937 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900938
Chao Yu8896cbd2019-07-12 16:55:41 +0800939 if (io->bio && !io_is_mergeable(sbi, io->bio, io, fio,
940 io->last_block_in_bio, fio->new_blkaddr))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900941 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900942alloc_new:
943 if (io->bio == NULL) {
Chao Yu8223ecc2019-08-28 17:33:38 +0800944 if (F2FS_IO_ALIGNED(sbi) &&
945 (fio->type == DATA || fio->type == NODE) &&
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800946 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700947 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Chao Yufe16efe2018-05-28 23:47:18 +0800948 fio->retry = true;
949 goto skip;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800950 }
Chao Yub757f6e2019-08-23 17:58:35 +0800951 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900952 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900953 }
954
Jaegeuk Kima912b542017-05-10 11:18:25 -0700955 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900956 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900957 goto alloc_new;
958 }
959
Yufen Yu578c6472018-01-09 19:33:39 +0800960 if (fio->io_wbc)
Tejun Heo34e51a52019-06-27 13:39:49 -0700961 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
Yufen Yu578c6472018-01-09 19:33:39 +0800962
Chao Yu7a9d7542016-02-22 18:36:38 +0800963 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700964 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800965
966 trace_f2fs_submit_page_write(fio->page, fio);
Chao Yufe16efe2018-05-28 23:47:18 +0800967skip:
Chao Yufb830fc2017-05-19 23:37:01 +0800968 if (fio->in_list)
969 goto next;
Chao Yufe16efe2018-05-28 23:47:18 +0800970out:
Daniel Rosenberg43549942018-08-20 19:21:43 -0700971 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
Chao Yu00e09c02019-08-23 17:58:36 +0800972 !f2fs_is_checkpoint_ready(sbi))
Jaegeuk Kim5ce80582018-09-06 11:40:12 -0700973 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800974 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900975}
976
Eric Biggers95ae2512019-07-22 09:26:24 -0700977static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
978{
979 return fsverity_active(inode) &&
980 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
981}
982
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700983static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
Eric Biggers95ae2512019-07-22 09:26:24 -0700984 unsigned nr_pages, unsigned op_flag,
Chao Yu06837282020-02-18 18:21:35 +0800985 pgoff_t first_idx, bool for_write)
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700986{
987 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700988 struct bio *bio;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700989 struct bio_post_read_ctx *ctx;
990 unsigned int post_read_steps = 0;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700991
Chao Yu06837282020-02-18 18:21:35 +0800992 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES),
993 for_write);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700994 if (!bio)
995 return ERR_PTR(-ENOMEM);
996 f2fs_target_device(sbi, blkaddr, bio);
997 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kime2e59412018-06-21 11:29:43 -0700998 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700999
1000 if (f2fs_encrypted_file(inode))
1001 post_read_steps |= 1 << STEP_DECRYPT;
Chao Yu4c8ff702019-11-01 18:07:14 +08001002 if (f2fs_compressed_file(inode))
Chao Yu03382f12020-04-21 19:36:21 +08001003 post_read_steps |= 1 << STEP_DECOMPRESS_NOWQ;
Eric Biggers95ae2512019-07-22 09:26:24 -07001004 if (f2fs_need_verity(inode, first_idx))
1005 post_read_steps |= 1 << STEP_VERITY;
1006
Eric Biggers6dbb1792018-04-18 11:09:48 -07001007 if (post_read_steps) {
Eric Biggerse8ce5742019-12-31 12:14:56 -06001008 /* Due to the mempool, this never fails. */
Eric Biggers6dbb1792018-04-18 11:09:48 -07001009 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
Eric Biggers6dbb1792018-04-18 11:09:48 -07001010 ctx->bio = bio;
Chao Yu4c8ff702019-11-01 18:07:14 +08001011 ctx->sbi = sbi;
Eric Biggers6dbb1792018-04-18 11:09:48 -07001012 ctx->enabled_steps = post_read_steps;
1013 bio->bi_private = ctx;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001014 }
1015
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001016 return bio;
1017}
1018
Chao Yu4c8ff702019-11-01 18:07:14 +08001019static void f2fs_release_read_bio(struct bio *bio)
1020{
1021 if (bio->bi_private)
1022 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
1023 bio_put(bio);
1024}
1025
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001026/* This can handle encryption stuffs */
1027static int f2fs_submit_page_read(struct inode *inode, struct page *page,
Jia Yangb7973092020-07-01 10:27:40 +08001028 block_t blkaddr, int op_flags, bool for_write)
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001029{
Chao Yu93770ab2019-04-15 15:26:32 +08001030 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1031 struct bio *bio;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001032
Jia Yangb7973092020-07-01 10:27:40 +08001033 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1034 page->index, for_write);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001035 if (IS_ERR(bio))
1036 return PTR_ERR(bio);
1037
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07001038 /* wait for GCed page writeback via META_MAPPING */
1039 f2fs_wait_on_block_writeback(inode, blkaddr);
1040
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001041 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1042 bio_put(bio);
1043 return -EFAULT;
1044 }
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -07001045 ClearPageError(page);
Chao Yu93770ab2019-04-15 15:26:32 +08001046 inc_page_count(sbi, F2FS_RD_DATA);
Chao Yu8b83ac82020-04-16 18:16:56 +08001047 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
Chao Yu93770ab2019-04-15 15:26:32 +08001048 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001049 return 0;
1050}
1051
Chao Yu46008c62016-05-09 19:56:30 +08001052static void __set_data_blkaddr(struct dnode_of_data *dn)
1053{
1054 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1055 __le32 *addr_array;
Chao Yu7a2af762017-07-19 00:19:06 +08001056 int base = 0;
1057
1058 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1059 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +08001060
1061 /* Get physical address of data block */
1062 addr_array = blkaddr_in_node(rn);
Chao Yu7a2af762017-07-19 00:19:06 +08001063 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +08001064}
1065
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001066/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001067 * Lock ordering for the change of data block address:
1068 * ->data_page
1069 * ->node_page
1070 * update block addresses in the node page
1071 */
Chao Yu4d57b862018-05-30 00:20:41 +08001072void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001073{
Chao Yubae0ee72018-12-25 17:43:42 +08001074 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +08001075 __set_data_blkaddr(dn);
1076 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001077 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001078}
1079
Chao Yuf28b3432016-02-24 17:16:47 +08001080void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1081{
1082 dn->data_blkaddr = blkaddr;
Chao Yu4d57b862018-05-30 00:20:41 +08001083 f2fs_set_data_blkaddr(dn);
Chao Yuf28b3432016-02-24 17:16:47 +08001084 f2fs_update_extent_cache(dn);
1085}
1086
Chao Yu46008c62016-05-09 19:56:30 +08001087/* dn->ofs_in_node will be returned with up-to-date last block pointer */
Chao Yu4d57b862018-05-30 00:20:41 +08001088int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001089{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001090 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +08001091 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001092
Chao Yu46008c62016-05-09 19:56:30 +08001093 if (!count)
1094 return 0;
1095
Jaegeuk Kim91942322016-05-20 10:13:22 -07001096 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001097 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +08001098 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1099 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001100
Chao Yu46008c62016-05-09 19:56:30 +08001101 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1102 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +09001103
Chao Yubae0ee72018-12-25 17:43:42 +08001104 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +08001105
1106 for (; count > 0; dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001107 block_t blkaddr = f2fs_data_blkaddr(dn);
Chao Yu46008c62016-05-09 19:56:30 +08001108 if (blkaddr == NULL_ADDR) {
1109 dn->data_blkaddr = NEW_ADDR;
1110 __set_data_blkaddr(dn);
1111 count--;
1112 }
1113 }
1114
1115 if (set_page_dirty(dn->node_page))
1116 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001117 return 0;
1118}
1119
Chao Yu46008c62016-05-09 19:56:30 +08001120/* Should keep dn->ofs_in_node unchanged */
Chao Yu4d57b862018-05-30 00:20:41 +08001121int f2fs_reserve_new_block(struct dnode_of_data *dn)
Chao Yu46008c62016-05-09 19:56:30 +08001122{
1123 unsigned int ofs_in_node = dn->ofs_in_node;
1124 int ret;
1125
Chao Yu4d57b862018-05-30 00:20:41 +08001126 ret = f2fs_reserve_new_blocks(dn, 1);
Chao Yu46008c62016-05-09 19:56:30 +08001127 dn->ofs_in_node = ofs_in_node;
1128 return ret;
1129}
1130
Huajun Lib6009652013-11-10 23:13:18 +08001131int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1132{
1133 bool need_put = dn->inode_page ? false : true;
1134 int err;
1135
Chao Yu4d57b862018-05-30 00:20:41 +08001136 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
Huajun Lib6009652013-11-10 23:13:18 +08001137 if (err)
1138 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +09001139
Huajun Lib6009652013-11-10 23:13:18 +08001140 if (dn->data_blkaddr == NULL_ADDR)
Chao Yu4d57b862018-05-30 00:20:41 +08001141 err = f2fs_reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001142 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +08001143 f2fs_put_dnode(dn);
1144 return err;
1145}
1146
Fan Li759af1c2015-08-05 15:52:16 +08001147int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001148{
Hou Pengyange15882b2017-02-23 09:18:05 +00001149 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +08001150 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +08001151
Fan Li759af1c2015-08-05 15:52:16 +08001152 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1153 dn->data_blkaddr = ei.blk + index - ei.fofs;
1154 return 0;
Chao Yu429511c2015-02-05 17:54:31 +08001155 }
1156
Fan Li759af1c2015-08-05 15:52:16 +08001157 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001158}
1159
Chao Yu4d57b862018-05-30 00:20:41 +08001160struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -05001161 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001162{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001163 struct address_space *mapping = inode->i_mapping;
1164 struct dnode_of_data dn;
1165 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +00001166 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001167 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001168
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001169 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001170 if (!page)
1171 return ERR_PTR(-ENOMEM);
1172
Chao Yucb3bc9e2015-02-05 18:03:40 +08001173 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1174 dn.data_blkaddr = ei.blk + index - ei.fofs;
Chao Yu93770ab2019-04-15 15:26:32 +08001175 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1176 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001177 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001178 goto put_err;
1179 }
Chao Yucb3bc9e2015-02-05 18:03:40 +08001180 goto got_it;
1181 }
1182
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001183 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001184 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001185 if (err)
1186 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001187 f2fs_put_dnode(&dn);
1188
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001189 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001190 err = -ENOENT;
1191 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001192 }
Chao Yu93770ab2019-04-15 15:26:32 +08001193 if (dn.data_blkaddr != NEW_ADDR &&
1194 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1195 dn.data_blkaddr,
1196 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001197 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001198 goto put_err;
1199 }
Chao Yucb3bc9e2015-02-05 18:03:40 +08001200got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001201 if (PageUptodate(page)) {
1202 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001203 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001204 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001205
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001206 /*
1207 * A new dentry page is allocated but not able to be written, since its
1208 * new inode page couldn't be allocated due to -ENOSPC.
1209 * In such the case, its blkaddr can be remained as NEW_ADDR.
Chao Yu4d57b862018-05-30 00:20:41 +08001210 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1211 * f2fs_init_inode_metadata.
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001212 */
1213 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001214 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001215 if (!PageUptodate(page))
1216 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001217 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001218 return page;
1219 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001220
Jia Yangb7973092020-07-01 10:27:40 +08001221 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1222 op_flags, for_write);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001223 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001224 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001225 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001226
1227put_err:
1228 f2fs_put_page(page, 1);
1229 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001230}
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001231
Chao Yu4d57b862018-05-30 00:20:41 +08001232struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001233{
1234 struct address_space *mapping = inode->i_mapping;
1235 struct page *page;
1236
1237 page = find_get_page(mapping, index);
1238 if (page && PageUptodate(page))
1239 return page;
1240 f2fs_put_page(page, 0);
1241
Chao Yu4d57b862018-05-30 00:20:41 +08001242 page = f2fs_get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001243 if (IS_ERR(page))
1244 return page;
1245
1246 if (PageUptodate(page))
1247 return page;
1248
1249 wait_on_page_locked(page);
1250 if (unlikely(!PageUptodate(page))) {
1251 f2fs_put_page(page, 0);
1252 return ERR_PTR(-EIO);
1253 }
1254 return page;
1255}
1256
1257/*
1258 * If it tries to access a hole, return an error.
1259 * Because, the callers, functions in dir.c and GC, should be able to know
1260 * whether this page exists or not.
1261 */
Chao Yu4d57b862018-05-30 00:20:41 +08001262struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001263 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001264{
1265 struct address_space *mapping = inode->i_mapping;
1266 struct page *page;
1267repeat:
Chao Yu4d57b862018-05-30 00:20:41 +08001268 page = f2fs_get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001269 if (IS_ERR(page))
1270 return page;
1271
1272 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001273 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001274 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001275 f2fs_put_page(page, 1);
1276 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001277 }
Chao Yu1563ac72016-07-03 22:05:12 +08001278 if (unlikely(!PageUptodate(page))) {
1279 f2fs_put_page(page, 1);
1280 return ERR_PTR(-EIO);
1281 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001282 return page;
1283}
1284
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001285/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001286 * Caller ensures that this data page is never allocated.
1287 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +09001288 *
Chao Yu4f4124d2013-12-21 18:02:14 +08001289 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1290 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +08001291 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1292 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001293 */
Chao Yu4d57b862018-05-30 00:20:41 +08001294struct page *f2fs_get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +09001295 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001296{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001297 struct address_space *mapping = inode->i_mapping;
1298 struct page *page;
1299 struct dnode_of_data dn;
1300 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -08001301
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001302 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +08001303 if (!page) {
1304 /*
1305 * before exiting, we should make sure ipage will be released
1306 * if any error occur.
1307 */
1308 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001309 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +08001310 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001311
Jaegeuk Kima8865372013-12-27 17:04:17 +09001312 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +08001313 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001314 if (err) {
1315 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001316 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001317 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001318 if (!ipage)
1319 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001320
1321 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001322 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001323
1324 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001325 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001326 if (!PageUptodate(page))
1327 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001328 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001329 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001330
Jaegeuk Kim76121182016-01-01 22:03:47 -08001331 /* if ipage exists, blkaddr should be NEW_ADDR */
1332 f2fs_bug_on(F2FS_I_SB(inode), ipage);
Chao Yu4d57b862018-05-30 00:20:41 +08001333 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001334 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -08001335 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001336 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001337got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +08001338 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001339 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001340 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001341 return page;
1342}
1343
Hyunchul Leed5097be2017-11-28 09:23:00 +09001344static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001345{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001346 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001347 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001348 struct node_info ni;
Chao Yu6aa58d82018-08-14 22:37:25 +08001349 block_t old_blkaddr;
Chao Yu46008c62016-05-09 19:56:30 +08001350 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +08001351 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001352
Jaegeuk Kim91942322016-05-20 10:13:22 -07001353 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001354 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +08001355
Chao Yu77357302018-07-17 00:02:17 +08001356 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1357 if (err)
1358 return err;
1359
Chao Yua2ced1c2020-02-14 17:44:10 +08001360 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Chao Yuf847c692018-09-27 18:34:52 +08001361 if (dn->data_blkaddr != NULL_ADDR)
Chao Yudf6136e2015-03-23 10:33:37 +08001362 goto alloc;
1363
Chao Yu0abd6752017-07-09 00:13:07 +08001364 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1365 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001366
Chao Yudf6136e2015-03-23 10:33:37 +08001367alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001368 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Chao Yu6aa58d82018-08-14 22:37:25 +08001369 old_blkaddr = dn->data_blkaddr;
1370 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
Chao Yuf608c382020-06-18 14:36:24 +08001371 &sum, seg_type, NULL);
Chao Yu6aa58d82018-08-14 22:37:25 +08001372 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1373 invalidate_mapping_pages(META_MAPPING(sbi),
1374 old_blkaddr, old_blkaddr);
Chao Yu86f35dc2019-08-28 17:33:35 +08001375 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001376
Jaegeuk Kim0a4daae2018-09-19 15:28:40 -07001377 /*
1378 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1379 * data from unwritten block via dio_read.
1380 */
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001381 return 0;
1382}
1383
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001384int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001385{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001386 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +08001387 struct f2fs_map_blocks map;
Chao Yud6d478a12018-01-03 17:30:19 +08001388 int flag;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001389 int err = 0;
Chao Yud6d478a12018-01-03 17:30:19 +08001390 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001391
Jaegeuk Kim0080c502016-05-07 08:52:57 -07001392 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +08001393 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1394 if (map.m_len > map.m_lblk)
1395 map.m_len -= map.m_lblk;
1396 else
1397 map.m_len = 0;
1398
Chao Yuda859852016-01-26 15:42:58 +08001399 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08001400 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001401 map.m_seg_type = NO_CHECK_TYPE;
Chao Yuf9d6d052018-11-13 14:33:45 +08001402 map.m_may_create = true;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001403
Chao Yud6d478a12018-01-03 17:30:19 +08001404 if (direct_io) {
Chao Yu4d57b862018-05-30 00:20:41 +08001405 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
Chao Yuf847c692018-09-27 18:34:52 +08001406 flag = f2fs_force_buffered_io(inode, iocb, from) ?
Chao Yud6d478a12018-01-03 17:30:19 +08001407 F2FS_GET_BLOCK_PRE_AIO :
1408 F2FS_GET_BLOCK_PRE_DIO;
1409 goto map_blocks;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001410 }
Chao Yuf2470372017-07-19 00:19:05 +08001411 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001412 err = f2fs_convert_inline_inode(inode);
1413 if (err)
1414 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001415 }
Chao Yud6d478a12018-01-03 17:30:19 +08001416 if (f2fs_has_inline_data(inode))
Sheng Yong250066452017-11-22 18:23:39 +08001417 return err;
Chao Yud6d478a12018-01-03 17:30:19 +08001418
1419 flag = F2FS_GET_BLOCK_PRE_AIO;
1420
1421map_blocks:
1422 err = f2fs_map_blocks(inode, &map, 1, flag);
1423 if (map.m_len > 0 && err == -ENOSPC) {
1424 if (!direct_io)
1425 set_inode_flag(inode, FI_NO_PREALLOC);
1426 err = 0;
Sheng Yong250066452017-11-22 18:23:39 +08001427 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001428 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001429}
1430
Chao Yu0ef81832020-06-18 14:36:22 +08001431void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
Yunlei He59c90812017-03-13 20:22:18 +08001432{
1433 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1434 if (lock)
1435 down_read(&sbi->node_change);
1436 else
1437 up_read(&sbi->node_change);
1438 } else {
1439 if (lock)
1440 f2fs_lock_op(sbi);
1441 else
1442 f2fs_unlock_op(sbi);
1443 }
1444}
1445
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001446/*
Chao Yu7a88ddb2020-02-27 19:30:05 +08001447 * f2fs_map_blocks() tries to find or build mapping relationship which
1448 * maps continuous logical blocks to physical blocks, and return such
1449 * info via f2fs_map_blocks structure.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001450 */
Chao Yud323d002015-10-27 09:53:45 +08001451int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +08001452 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001453{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001454 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001455 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +08001456 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf9d6d052018-11-13 14:33:45 +08001457 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +08001458 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001459 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +08001460 unsigned int ofs_in_node, last_ofs_in_node;
1461 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +00001462 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +08001463 block_t blkaddr;
Chao Yuc4020b22018-01-11 14:42:30 +08001464 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001465
Chao Yudfd02e42016-08-20 15:12:01 +08001466 if (!maxblocks)
1467 return 0;
1468
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001469 map->m_len = 0;
1470 map->m_flags = 0;
1471
1472 /* it only supports block size == page size */
1473 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +08001474 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001475
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001476 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Chao Yub0332a02020-02-14 17:44:12 +08001477 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
Jia Zhuf4f0b672018-11-20 04:29:35 +08001478 map->m_may_create)
1479 goto next_dnode;
1480
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001481 map->m_pblk = ei.blk + pgofs - ei.fofs;
1482 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1483 map->m_flags = F2FS_MAP_MAPPED;
Chao Yuc4020b22018-01-11 14:42:30 +08001484 if (map->m_next_extent)
1485 *map->m_next_extent = pgofs + map->m_len;
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301486
1487 /* for hardware encryption, but to avoid potential issue in future */
1488 if (flag == F2FS_GET_BLOCK_DIO)
1489 f2fs_wait_on_block_writeback_range(inode,
1490 map->m_pblk, map->m_len);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001491 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001492 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001493
Chao Yu4fe71e82016-01-26 15:37:38 +08001494next_dnode:
Chao Yuf9d6d052018-11-13 14:33:45 +08001495 if (map->m_may_create)
Chao Yu0ef81832020-06-18 14:36:22 +08001496 f2fs_do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001497
1498 /* When reading holes, we need its node page */
1499 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001500 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001501 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +08001502 if (flag == F2FS_GET_BLOCK_BMAP)
1503 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +08001504 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001505 err = 0;
Chao Yuda859852016-01-26 15:42:58 +08001506 if (map->m_next_pgofs)
1507 *map->m_next_pgofs =
Chao Yu4d57b862018-05-30 00:20:41 +08001508 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuc4020b22018-01-11 14:42:30 +08001509 if (map->m_next_extent)
1510 *map->m_next_extent =
Chao Yu4d57b862018-05-30 00:20:41 +08001511 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +08001512 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001513 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001514 }
Chao Yu973163f2015-09-18 16:51:51 +08001515
Chao Yuc4020b22018-01-11 14:42:30 +08001516 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +08001517 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +01001518 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +08001519 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001520
Chao Yu4fe71e82016-01-26 15:37:38 +08001521next_block:
Chao Yua2ced1c2020-02-14 17:44:10 +08001522 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu973163f2015-09-18 16:51:51 +08001523
Chao Yuc9b60782018-08-01 19:13:44 +08001524 if (__is_valid_data_blkaddr(blkaddr) &&
Chao Yu93770ab2019-04-15 15:26:32 +08001525 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001526 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08001527 goto sync_out;
1528 }
1529
Chao Yu93770ab2019-04-15 15:26:32 +08001530 if (__is_valid_data_blkaddr(blkaddr)) {
Chao Yuf847c692018-09-27 18:34:52 +08001531 /* use out-place-update for driect IO under LFS mode */
Chao Yub0332a02020-02-14 17:44:12 +08001532 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
Chao Yuf9d6d052018-11-13 14:33:45 +08001533 map->m_may_create) {
Chao Yuf847c692018-09-27 18:34:52 +08001534 err = __allocate_data_block(&dn, map->m_seg_type);
Chao Yu05e3600612019-08-28 17:33:36 +08001535 if (err)
1536 goto sync_out;
1537 blkaddr = dn.data_blkaddr;
1538 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yuf847c692018-09-27 18:34:52 +08001539 }
1540 } else {
Fan Li7df3a432015-12-17 13:20:59 +08001541 if (create) {
1542 if (unlikely(f2fs_cp_error(sbi))) {
1543 err = -EIO;
1544 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +08001545 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001546 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +08001547 if (blkaddr == NULL_ADDR) {
1548 prealloc++;
1549 last_ofs_in_node = dn.ofs_in_node;
1550 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001551 } else {
Jaegeuk Kim0a4daae2018-09-19 15:28:40 -07001552 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1553 flag != F2FS_GET_BLOCK_DIO);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001554 err = __allocate_data_block(&dn,
1555 map->m_seg_type);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001556 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001557 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001558 }
Fan Li7df3a432015-12-17 13:20:59 +08001559 if (err)
1560 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +08001561 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001562 blkaddr = dn.data_blkaddr;
1563 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001564 if (flag == F2FS_GET_BLOCK_BMAP) {
1565 map->m_pblk = 0;
1566 goto sync_out;
1567 }
Chao Yuc4020b22018-01-11 14:42:30 +08001568 if (flag == F2FS_GET_BLOCK_PRECACHE)
1569 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001570 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1571 blkaddr == NULL_ADDR) {
1572 if (map->m_next_pgofs)
1573 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001574 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001575 }
Chao Yuf3d98e72018-01-10 18:18:52 +08001576 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1577 /* for defragment case */
1578 if (map->m_next_pgofs)
1579 *map->m_next_pgofs = pgofs + 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001580 goto sync_out;
Chao Yuf3d98e72018-01-10 18:18:52 +08001581 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001582 }
1583 }
Fan Li7df3a432015-12-17 13:20:59 +08001584
Chao Yu46008c62016-05-09 19:56:30 +08001585 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1586 goto skip;
1587
Chao Yu4fe71e82016-01-26 15:37:38 +08001588 if (map->m_len == 0) {
1589 /* preallocated unwritten block should be mapped for fiemap. */
1590 if (blkaddr == NEW_ADDR)
1591 map->m_flags |= F2FS_MAP_UNWRITTEN;
1592 map->m_flags |= F2FS_MAP_MAPPED;
1593
1594 map->m_pblk = blkaddr;
1595 map->m_len = 1;
1596 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001597 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001598 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001599 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001600 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001601 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001602 } else {
1603 goto sync_out;
1604 }
1605
Chao Yu46008c62016-05-09 19:56:30 +08001606skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001607 dn.ofs_in_node++;
1608 pgofs++;
1609
Chao Yu46008c62016-05-09 19:56:30 +08001610 /* preallocate blocks in batch for one dnode page */
1611 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1612 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001613
Chao Yu46008c62016-05-09 19:56:30 +08001614 dn.ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001615 err = f2fs_reserve_new_blocks(&dn, prealloc);
Chao Yu46008c62016-05-09 19:56:30 +08001616 if (err)
1617 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001618
Chao Yu46008c62016-05-09 19:56:30 +08001619 map->m_len += dn.ofs_in_node - ofs_in_node;
1620 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1621 err = -ENOSPC;
1622 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001623 }
Chao Yu46008c62016-05-09 19:56:30 +08001624 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001625 }
1626
Chao Yu46008c62016-05-09 19:56:30 +08001627 if (pgofs >= end)
1628 goto sync_out;
1629 else if (dn.ofs_in_node < end_offset)
1630 goto next_block;
1631
Chao Yuc4020b22018-01-11 14:42:30 +08001632 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1633 if (map->m_flags & F2FS_MAP_MAPPED) {
1634 unsigned int ofs = start_pgofs - map->m_lblk;
1635
1636 f2fs_update_extent_cache_range(&dn,
1637 start_pgofs, map->m_pblk + ofs,
1638 map->m_len - ofs);
1639 }
1640 }
1641
Chao Yu46008c62016-05-09 19:56:30 +08001642 f2fs_put_dnode(&dn);
1643
Chao Yuf9d6d052018-11-13 14:33:45 +08001644 if (map->m_may_create) {
Chao Yu0ef81832020-06-18 14:36:22 +08001645 f2fs_do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001646 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001647 }
Chao Yu46008c62016-05-09 19:56:30 +08001648 goto next_dnode;
1649
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001650sync_out:
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301651
1652 /* for hardware encryption, but to avoid potential issue in future */
1653 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1654 f2fs_wait_on_block_writeback_range(inode,
1655 map->m_pblk, map->m_len);
1656
Chao Yuc4020b22018-01-11 14:42:30 +08001657 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1658 if (map->m_flags & F2FS_MAP_MAPPED) {
1659 unsigned int ofs = start_pgofs - map->m_lblk;
1660
1661 f2fs_update_extent_cache_range(&dn,
1662 start_pgofs, map->m_pblk + ofs,
1663 map->m_len - ofs);
1664 }
1665 if (map->m_next_extent)
1666 *map->m_next_extent = pgofs + 1;
1667 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001668 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001669unlock_out:
Chao Yuf9d6d052018-11-13 14:33:45 +08001670 if (map->m_may_create) {
Chao Yu0ef81832020-06-18 14:36:22 +08001671 f2fs_do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001672 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001673 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001674out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001675 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001676 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001677}
1678
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001679bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1680{
1681 struct f2fs_map_blocks map;
1682 block_t last_lblk;
1683 int err;
1684
1685 if (pos + len > i_size_read(inode))
1686 return false;
1687
1688 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1689 map.m_next_pgofs = NULL;
1690 map.m_next_extent = NULL;
1691 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08001692 map.m_may_create = false;
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001693 last_lblk = F2FS_BLK_ALIGN(pos + len);
1694
1695 while (map.m_lblk < last_lblk) {
1696 map.m_len = last_lblk - map.m_lblk;
1697 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1698 if (err || map.m_len == 0)
1699 return false;
1700 map.m_lblk += map.m_len;
1701 }
1702 return true;
1703}
1704
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001705static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001706 struct buffer_head *bh, int create, int flag,
Chao Yuf9d6d052018-11-13 14:33:45 +08001707 pgoff_t *next_pgofs, int seg_type, bool may_write)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001708{
1709 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001710 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001711
1712 map.m_lblk = iblock;
1713 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001714 map.m_next_pgofs = next_pgofs;
Chao Yuc4020b22018-01-11 14:42:30 +08001715 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001716 map.m_seg_type = seg_type;
Chao Yuf9d6d052018-11-13 14:33:45 +08001717 map.m_may_create = may_write;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001718
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001719 err = f2fs_map_blocks(inode, &map, create, flag);
1720 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001721 map_bh(bh, inode->i_sb, map.m_pblk);
1722 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001723 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001724 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001725 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001726}
1727
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001728static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001729 struct buffer_head *bh_result, int create, int flag,
1730 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001731{
Chao Yuda859852016-01-26 15:42:58 +08001732 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001733 flag, next_pgofs,
Chao Yuf9d6d052018-11-13 14:33:45 +08001734 NO_CHECK_TYPE, create);
1735}
1736
1737static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1738 struct buffer_head *bh_result, int create)
1739{
1740 return __get_data_block(inode, iblock, bh_result, create,
1741 F2FS_GET_BLOCK_DIO, NULL,
1742 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
Jaegeuk Kim75a037f2019-07-31 13:27:05 -07001743 IS_SWAPFILE(inode) ? false : true);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001744}
1745
Chao Yue2b4e2b2015-08-19 19:11:19 +08001746static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001747 struct buffer_head *bh_result, int create)
1748{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001749 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuf9d6d052018-11-13 14:33:45 +08001750 F2FS_GET_BLOCK_DIO, NULL,
1751 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1752 false);
Chao Yue2b4e2b2015-08-19 19:11:19 +08001753}
1754
1755static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1756 struct buffer_head *bh_result, int create)
1757{
Yunlei He179448b2015-12-28 21:48:32 +08001758 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001759 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001760 return -EFBIG;
1761
Chao Yue2b4e2b2015-08-19 19:11:19 +08001762 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001763 F2FS_GET_BLOCK_BMAP, NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001764 NO_CHECK_TYPE, create);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001765}
1766
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001767static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1768{
1769 return (offset >> inode->i_blkbits);
1770}
1771
1772static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1773{
1774 return (blk << inode->i_blkbits);
1775}
1776
Chao Yu442a9db2018-01-11 14:39:57 +08001777static int f2fs_xattr_fiemap(struct inode *inode,
1778 struct fiemap_extent_info *fieinfo)
1779{
1780 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1781 struct page *page;
1782 struct node_info ni;
1783 __u64 phys = 0, len;
1784 __u32 flags;
1785 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1786 int err = 0;
1787
1788 if (f2fs_has_inline_xattr(inode)) {
1789 int offset;
1790
1791 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1792 inode->i_ino, false);
1793 if (!page)
1794 return -ENOMEM;
1795
Chao Yu77357302018-07-17 00:02:17 +08001796 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1797 if (err) {
1798 f2fs_put_page(page, 1);
1799 return err;
1800 }
Chao Yu442a9db2018-01-11 14:39:57 +08001801
1802 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1803 offset = offsetof(struct f2fs_inode, i_addr) +
1804 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yub323fd22018-01-17 16:31:36 +08001805 get_inline_xattr_addrs(inode));
Chao Yu442a9db2018-01-11 14:39:57 +08001806
1807 phys += offset;
1808 len = inline_xattr_size(inode);
1809
1810 f2fs_put_page(page, 1);
1811
1812 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1813
1814 if (!xnid)
1815 flags |= FIEMAP_EXTENT_LAST;
1816
1817 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
Chao Yudd5a09b2020-06-29 20:13:13 +08001818 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
Chao Yu442a9db2018-01-11 14:39:57 +08001819 if (err || err == 1)
1820 return err;
1821 }
1822
1823 if (xnid) {
1824 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1825 if (!page)
1826 return -ENOMEM;
1827
Chao Yu77357302018-07-17 00:02:17 +08001828 err = f2fs_get_node_info(sbi, xnid, &ni);
1829 if (err) {
1830 f2fs_put_page(page, 1);
1831 return err;
1832 }
Chao Yu442a9db2018-01-11 14:39:57 +08001833
1834 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1835 len = inode->i_sb->s_blocksize;
1836
1837 f2fs_put_page(page, 1);
1838
1839 flags = FIEMAP_EXTENT_LAST;
1840 }
1841
Chao Yudd5a09b2020-06-29 20:13:13 +08001842 if (phys) {
Chao Yu442a9db2018-01-11 14:39:57 +08001843 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
Chao Yudd5a09b2020-06-29 20:13:13 +08001844 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1845 }
Chao Yu442a9db2018-01-11 14:39:57 +08001846
1847 return (err < 0 ? err : 0);
1848}
1849
Chao Yubf38fba2020-03-28 17:40:40 +08001850static loff_t max_inode_blocks(struct inode *inode)
1851{
1852 loff_t result = ADDRS_PER_INODE(inode);
1853 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1854
1855 /* two direct node blocks */
1856 result += (leaf_count * 2);
1857
1858 /* two indirect node blocks */
1859 leaf_count *= NIDS_PER_BLOCK;
1860 result += (leaf_count * 2);
1861
1862 /* one double indirect node block */
1863 leaf_count *= NIDS_PER_BLOCK;
1864 result += leaf_count;
1865
1866 return result;
1867}
1868
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001869int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1870 u64 start, u64 len)
1871{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001872 struct buffer_head map_bh;
1873 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001874 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001875 u64 logical = 0, phys = 0, size = 0;
1876 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001877 int ret = 0;
Chao Yubf38fba2020-03-28 17:40:40 +08001878 bool compr_cluster = false;
1879 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001880
Chao Yuc4020b22018-01-11 14:42:30 +08001881 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1882 ret = f2fs_precache_extents(inode);
1883 if (ret)
1884 return ret;
1885 }
1886
Christoph Hellwig45dd0522020-05-23 09:30:14 +02001887 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001888 if (ret)
1889 return ret;
1890
Chao Yuf1b43d42018-01-11 14:37:35 +08001891 inode_lock(inode);
1892
Chao Yu442a9db2018-01-11 14:39:57 +08001893 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1894 ret = f2fs_xattr_fiemap(inode, fieinfo);
1895 goto out;
1896 }
1897
Chao Yu7975f342019-07-22 18:03:50 +08001898 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001899 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1900 if (ret != -EAGAIN)
Chao Yuf1b43d42018-01-11 14:37:35 +08001901 goto out;
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001902 }
1903
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001904 if (logical_to_blk(inode, len) == 0)
1905 len = blk_to_logical(inode, 1);
1906
1907 start_blk = logical_to_blk(inode, start);
1908 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001909
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001910next:
1911 memset(&map_bh, 0, sizeof(struct buffer_head));
1912 map_bh.b_size = len;
1913
Chao Yubf38fba2020-03-28 17:40:40 +08001914 if (compr_cluster)
1915 map_bh.b_size = blk_to_logical(inode, cluster_size - 1);
1916
Chao Yue2b4e2b2015-08-19 19:11:19 +08001917 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001918 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001919 if (ret)
1920 goto out;
1921
1922 /* HOLE */
1923 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001924 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001925
1926 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
Chao Yubf38fba2020-03-28 17:40:40 +08001927 max_inode_blocks(inode)))
Fan Li9a950d52015-12-26 18:07:41 +08001928 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001929
Fan Li9a950d52015-12-26 18:07:41 +08001930 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001931 }
Fan Li9a950d52015-12-26 18:07:41 +08001932
Chao Yuda5af122016-01-08 20:19:27 +08001933 if (size) {
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301934 if (IS_ENCRYPTED(inode))
Chao Yuda5af122016-01-08 20:19:27 +08001935 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1936
Fan Li9a950d52015-12-26 18:07:41 +08001937 ret = fiemap_fill_next_extent(fieinfo, logical,
1938 phys, size, flags);
Chao Yudd5a09b2020-06-29 20:13:13 +08001939 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
Chao Yubf38fba2020-03-28 17:40:40 +08001940 if (ret)
1941 goto out;
1942 size = 0;
Chao Yuda5af122016-01-08 20:19:27 +08001943 }
Fan Li9a950d52015-12-26 18:07:41 +08001944
Chao Yubf38fba2020-03-28 17:40:40 +08001945 if (start_blk > last_blk)
Fan Li9a950d52015-12-26 18:07:41 +08001946 goto out;
1947
Chao Yubf38fba2020-03-28 17:40:40 +08001948 if (compr_cluster) {
1949 compr_cluster = false;
1950
1951
1952 logical = blk_to_logical(inode, start_blk - 1);
1953 phys = blk_to_logical(inode, map_bh.b_blocknr);
1954 size = blk_to_logical(inode, cluster_size);
1955
1956 flags |= FIEMAP_EXTENT_ENCODED;
1957
1958 start_blk += cluster_size - 1;
1959
1960 if (start_blk > last_blk)
1961 goto out;
1962
1963 goto prep_next;
1964 }
1965
1966 if (map_bh.b_blocknr == COMPRESS_ADDR) {
1967 compr_cluster = true;
1968 start_blk++;
1969 goto prep_next;
1970 }
1971
Fan Li9a950d52015-12-26 18:07:41 +08001972 logical = blk_to_logical(inode, start_blk);
1973 phys = blk_to_logical(inode, map_bh.b_blocknr);
1974 size = map_bh.b_size;
1975 flags = 0;
1976 if (buffer_unwritten(&map_bh))
1977 flags = FIEMAP_EXTENT_UNWRITTEN;
1978
1979 start_blk += logical_to_blk(inode, size);
1980
1981prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001982 cond_resched();
1983 if (fatal_signal_pending(current))
1984 ret = -EINTR;
1985 else
1986 goto next;
1987out:
1988 if (ret == 1)
1989 ret = 0;
1990
Al Viro59551022016-01-22 15:40:57 -05001991 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001992 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001993}
1994
Eric Biggers95ae2512019-07-22 09:26:24 -07001995static inline loff_t f2fs_readpage_limit(struct inode *inode)
1996{
1997 if (IS_ENABLED(CONFIG_FS_VERITY) &&
1998 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1999 return inode->i_sb->s_maxbytes;
2000
2001 return i_size_read(inode);
2002}
2003
Chao Yu2df0ab02019-03-25 21:07:30 +08002004static int f2fs_read_single_page(struct inode *inode, struct page *page,
2005 unsigned nr_pages,
2006 struct f2fs_map_blocks *map,
2007 struct bio **bio_ret,
2008 sector_t *last_block_in_bio,
2009 bool is_readahead)
2010{
2011 struct bio *bio = *bio_ret;
2012 const unsigned blkbits = inode->i_blkbits;
2013 const unsigned blocksize = 1 << blkbits;
2014 sector_t block_in_file;
2015 sector_t last_block;
2016 sector_t last_block_in_file;
2017 sector_t block_nr;
2018 int ret = 0;
2019
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002020 block_in_file = (sector_t)page_index(page);
Chao Yu2df0ab02019-03-25 21:07:30 +08002021 last_block = block_in_file + nr_pages;
Eric Biggers95ae2512019-07-22 09:26:24 -07002022 last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
Chao Yu2df0ab02019-03-25 21:07:30 +08002023 blkbits;
2024 if (last_block > last_block_in_file)
2025 last_block = last_block_in_file;
2026
2027 /* just zeroing out page which is beyond EOF */
2028 if (block_in_file >= last_block)
2029 goto zero_out;
2030 /*
2031 * Map blocks using the previous result first.
2032 */
2033 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2034 block_in_file > map->m_lblk &&
2035 block_in_file < (map->m_lblk + map->m_len))
2036 goto got_it;
2037
2038 /*
2039 * Then do more f2fs_map_blocks() calls until we are
2040 * done with this page.
2041 */
2042 map->m_lblk = block_in_file;
2043 map->m_len = last_block - block_in_file;
2044
2045 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2046 if (ret)
2047 goto out;
2048got_it:
2049 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2050 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2051 SetPageMappedToDisk(page);
2052
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002053 if (!PageUptodate(page) && (!PageSwapCache(page) &&
2054 !cleancache_get_page(page))) {
Chao Yu2df0ab02019-03-25 21:07:30 +08002055 SetPageUptodate(page);
2056 goto confused;
2057 }
2058
2059 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
Chao Yu93770ab2019-04-15 15:26:32 +08002060 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08002061 ret = -EFSCORRUPTED;
Chao Yu2df0ab02019-03-25 21:07:30 +08002062 goto out;
2063 }
2064 } else {
2065zero_out:
2066 zero_user_segment(page, 0, PAGE_SIZE);
Eric Biggers95ae2512019-07-22 09:26:24 -07002067 if (f2fs_need_verity(inode, page->index) &&
2068 !fsverity_verify_page(page)) {
2069 ret = -EIO;
2070 goto out;
2071 }
Chao Yu2df0ab02019-03-25 21:07:30 +08002072 if (!PageUptodate(page))
2073 SetPageUptodate(page);
2074 unlock_page(page);
2075 goto out;
2076 }
2077
2078 /*
2079 * This page will go to BIO. Do we need to send this
2080 * BIO off first?
2081 */
Chao Yu8896cbd2019-07-12 16:55:41 +08002082 if (bio && !page_is_mergeable(F2FS_I_SB(inode), bio,
2083 *last_block_in_bio, block_nr)) {
Chao Yu2df0ab02019-03-25 21:07:30 +08002084submit_and_realloc:
2085 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2086 bio = NULL;
2087 }
2088 if (bio == NULL) {
2089 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
Chao Yu06837282020-02-18 18:21:35 +08002090 is_readahead ? REQ_RAHEAD : 0, page->index,
2091 false);
Chao Yu2df0ab02019-03-25 21:07:30 +08002092 if (IS_ERR(bio)) {
2093 ret = PTR_ERR(bio);
2094 bio = NULL;
2095 goto out;
2096 }
2097 }
2098
2099 /*
2100 * If the page is under writeback, we need to wait for
2101 * its completion to see the correct decrypted data.
2102 */
2103 f2fs_wait_on_block_writeback(inode, block_nr);
2104
2105 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2106 goto submit_and_realloc;
2107
2108 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
Chao Yu8b83ac82020-04-16 18:16:56 +08002109 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
Chao Yu2df0ab02019-03-25 21:07:30 +08002110 ClearPageError(page);
2111 *last_block_in_bio = block_nr;
2112 goto out;
2113confused:
2114 if (bio) {
2115 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2116 bio = NULL;
2117 }
2118 unlock_page(page);
2119out:
2120 *bio_ret = bio;
2121 return ret;
2122}
2123
Chao Yu4c8ff702019-11-01 18:07:14 +08002124#ifdef CONFIG_F2FS_FS_COMPRESSION
2125int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2126 unsigned nr_pages, sector_t *last_block_in_bio,
Chao Yu06837282020-02-18 18:21:35 +08002127 bool is_readahead, bool for_write)
Chao Yu4c8ff702019-11-01 18:07:14 +08002128{
2129 struct dnode_of_data dn;
2130 struct inode *inode = cc->inode;
2131 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2132 struct bio *bio = *bio_ret;
2133 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2134 sector_t last_block_in_file;
2135 const unsigned blkbits = inode->i_blkbits;
2136 const unsigned blocksize = 1 << blkbits;
2137 struct decompress_io_ctx *dic = NULL;
2138 int i;
2139 int ret = 0;
2140
2141 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2142
Chao Yu7496aff2020-03-30 18:03:15 +08002143 last_block_in_file = (f2fs_readpage_limit(inode) +
2144 blocksize - 1) >> blkbits;
Chao Yu4c8ff702019-11-01 18:07:14 +08002145
2146 /* get rid of pages beyond EOF */
2147 for (i = 0; i < cc->cluster_size; i++) {
2148 struct page *page = cc->rpages[i];
2149
2150 if (!page)
2151 continue;
2152 if ((sector_t)page->index >= last_block_in_file) {
2153 zero_user_segment(page, 0, PAGE_SIZE);
2154 if (!PageUptodate(page))
2155 SetPageUptodate(page);
2156 } else if (!PageUptodate(page)) {
2157 continue;
2158 }
2159 unlock_page(page);
2160 cc->rpages[i] = NULL;
2161 cc->nr_rpages--;
2162 }
2163
2164 /* we are done since all pages are beyond EOF */
2165 if (f2fs_cluster_is_empty(cc))
2166 goto out;
2167
2168 set_new_dnode(&dn, inode, NULL, NULL, 0);
2169 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2170 if (ret)
2171 goto out;
2172
2173 /* cluster was overwritten as normal cluster */
2174 if (dn.data_blkaddr != COMPRESS_ADDR)
2175 goto out;
2176
2177 for (i = 1; i < cc->cluster_size; i++) {
2178 block_t blkaddr;
2179
Chao Yua2ced1c2020-02-14 17:44:10 +08002180 blkaddr = data_blkaddr(dn.inode, dn.node_page,
Chao Yu4c8ff702019-11-01 18:07:14 +08002181 dn.ofs_in_node + i);
2182
2183 if (!__is_valid_data_blkaddr(blkaddr))
2184 break;
2185
2186 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2187 ret = -EFAULT;
2188 goto out_put_dnode;
2189 }
2190 cc->nr_cpages++;
2191 }
2192
2193 /* nothing to decompress */
2194 if (cc->nr_cpages == 0) {
2195 ret = 0;
2196 goto out_put_dnode;
2197 }
2198
2199 dic = f2fs_alloc_dic(cc);
2200 if (IS_ERR(dic)) {
2201 ret = PTR_ERR(dic);
2202 goto out_put_dnode;
2203 }
2204
2205 for (i = 0; i < dic->nr_cpages; i++) {
2206 struct page *page = dic->cpages[i];
2207 block_t blkaddr;
Chao Yu03382f12020-04-21 19:36:21 +08002208 struct bio_post_read_ctx *ctx;
Chao Yu4c8ff702019-11-01 18:07:14 +08002209
Chao Yua2ced1c2020-02-14 17:44:10 +08002210 blkaddr = data_blkaddr(dn.inode, dn.node_page,
Chao Yu4c8ff702019-11-01 18:07:14 +08002211 dn.ofs_in_node + i + 1);
2212
2213 if (bio && !page_is_mergeable(sbi, bio,
2214 *last_block_in_bio, blkaddr)) {
2215submit_and_realloc:
2216 __submit_bio(sbi, bio, DATA);
2217 bio = NULL;
2218 }
2219
2220 if (!bio) {
2221 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2222 is_readahead ? REQ_RAHEAD : 0,
Chao Yu06837282020-02-18 18:21:35 +08002223 page->index, for_write);
Chao Yu4c8ff702019-11-01 18:07:14 +08002224 if (IS_ERR(bio)) {
2225 ret = PTR_ERR(bio);
Chao Yu4c8ff702019-11-01 18:07:14 +08002226 dic->failed = true;
2227 if (refcount_sub_and_test(dic->nr_cpages - i,
Chao Yuf3494342020-04-23 17:57:33 +08002228 &dic->ref)) {
Chao Yu4c8ff702019-11-01 18:07:14 +08002229 f2fs_decompress_end_io(dic->rpages,
2230 cc->cluster_size, true,
2231 false);
Chao Yuf3494342020-04-23 17:57:33 +08002232 f2fs_free_dic(dic);
2233 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002234 f2fs_put_dnode(&dn);
Chao Yuf3494342020-04-23 17:57:33 +08002235 *bio_ret = NULL;
Chao Yu4c8ff702019-11-01 18:07:14 +08002236 return ret;
2237 }
2238 }
2239
2240 f2fs_wait_on_block_writeback(inode, blkaddr);
2241
2242 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2243 goto submit_and_realloc;
2244
Chao Yu03382f12020-04-21 19:36:21 +08002245 /* tag STEP_DECOMPRESS to handle IO in wq */
2246 ctx = bio->bi_private;
2247 if (!(ctx->enabled_steps & (1 << STEP_DECOMPRESS)))
2248 ctx->enabled_steps |= 1 << STEP_DECOMPRESS;
2249
Chao Yu4c8ff702019-11-01 18:07:14 +08002250 inc_page_count(sbi, F2FS_RD_DATA);
Chao Yu8b83ac82020-04-16 18:16:56 +08002251 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
Chao Yu9c122382020-04-23 18:03:06 +08002252 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
Chao Yu4c8ff702019-11-01 18:07:14 +08002253 ClearPageError(page);
2254 *last_block_in_bio = blkaddr;
2255 }
2256
2257 f2fs_put_dnode(&dn);
2258
2259 *bio_ret = bio;
2260 return 0;
2261
2262out_put_dnode:
2263 f2fs_put_dnode(&dn);
2264out:
2265 f2fs_decompress_end_io(cc->rpages, cc->cluster_size, true, false);
2266 *bio_ret = bio;
2267 return ret;
2268}
2269#endif
2270
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002271/*
2272 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2273 * Major change was from block_size == page_size in f2fs by default.
Jaegeuk Kime2e59412018-06-21 11:29:43 -07002274 *
2275 * Note that the aops->readpages() function is ONLY used for read-ahead. If
2276 * this function ever deviates from doing just read-ahead, it should either
2277 * use ->readpage() or do the necessary surgery to decouple ->readpages()
2278 * from read-ahead.
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002279 */
Matthew Wilcox (Oracle)e20a7692020-06-01 21:47:27 -07002280static int f2fs_mpage_readpages(struct inode *inode,
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002281 struct readahead_control *rac, struct page *page)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002282{
2283 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002284 sector_t last_block_in_bio = 0;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002285 struct f2fs_map_blocks map;
Chao Yu4c8ff702019-11-01 18:07:14 +08002286#ifdef CONFIG_F2FS_FS_COMPRESSION
2287 struct compress_ctx cc = {
2288 .inode = inode,
2289 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2290 .cluster_size = F2FS_I(inode)->i_cluster_size,
2291 .cluster_idx = NULL_CLUSTER,
2292 .rpages = NULL,
2293 .cpages = NULL,
2294 .nr_rpages = 0,
2295 .nr_cpages = 0,
2296 };
2297#endif
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002298 unsigned nr_pages = rac ? readahead_count(rac) : 1;
Chao Yu4c8ff702019-11-01 18:07:14 +08002299 unsigned max_nr_pages = nr_pages;
Chao Yu2df0ab02019-03-25 21:07:30 +08002300 int ret = 0;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002301
2302 map.m_pblk = 0;
2303 map.m_lblk = 0;
2304 map.m_len = 0;
2305 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08002306 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08002307 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09002308 map.m_seg_type = NO_CHECK_TYPE;
Chao Yuf9d6d052018-11-13 14:33:45 +08002309 map.m_may_create = false;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002310
LiFan736c0a72017-11-25 11:46:18 +08002311 for (; nr_pages; nr_pages--) {
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002312 if (rac) {
2313 page = readahead_page(rac);
Kinglong Meea83d50b2017-03-13 16:35:13 +08002314 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002315 }
2316
Chao Yu4c8ff702019-11-01 18:07:14 +08002317#ifdef CONFIG_F2FS_FS_COMPRESSION
2318 if (f2fs_compressed_file(inode)) {
2319 /* there are remained comressed pages, submit them */
2320 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2321 ret = f2fs_read_multi_pages(&cc, &bio,
2322 max_nr_pages,
2323 &last_block_in_bio,
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002324 rac != NULL, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08002325 f2fs_destroy_compress_ctx(&cc);
2326 if (ret)
2327 goto set_error_page;
2328 }
2329 ret = f2fs_is_compressed_cluster(inode, page->index);
2330 if (ret < 0)
2331 goto set_error_page;
2332 else if (!ret)
2333 goto read_single_page;
2334
2335 ret = f2fs_init_compress_ctx(&cc);
2336 if (ret)
2337 goto set_error_page;
2338
2339 f2fs_compress_ctx_add_page(&cc, page);
2340
2341 goto next_page;
2342 }
2343read_single_page:
2344#endif
2345
2346 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002347 &bio, &last_block_in_bio, rac);
Chao Yu2df0ab02019-03-25 21:07:30 +08002348 if (ret) {
Chao Yu4c8ff702019-11-01 18:07:14 +08002349#ifdef CONFIG_F2FS_FS_COMPRESSION
2350set_error_page:
2351#endif
Chao Yu2df0ab02019-03-25 21:07:30 +08002352 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002353 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002354 unlock_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002355 }
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002356#ifdef CONFIG_F2FS_FS_COMPRESSION
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002357next_page:
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002358#endif
2359 if (rac)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002360 put_page(page);
Chao Yu4c8ff702019-11-01 18:07:14 +08002361
2362#ifdef CONFIG_F2FS_FS_COMPRESSION
2363 if (f2fs_compressed_file(inode)) {
2364 /* last page */
2365 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2366 ret = f2fs_read_multi_pages(&cc, &bio,
2367 max_nr_pages,
2368 &last_block_in_bio,
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002369 rac != NULL, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08002370 f2fs_destroy_compress_ctx(&cc);
2371 }
2372 }
2373#endif
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002374 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002375 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07002376 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002377 return ret;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002378}
2379
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002380static int f2fs_read_data_page(struct file *file, struct page *page)
2381{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002382 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002383 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002384
Chao Yuc20e89c2014-05-06 16:53:08 +08002385 trace_f2fs_readpage(page, DATA);
2386
Chao Yu4c8ff702019-11-01 18:07:14 +08002387 if (!f2fs_is_compress_backend_ready(inode)) {
2388 unlock_page(page);
2389 return -EOPNOTSUPP;
2390 }
2391
arter97e1c42042014-08-06 23:22:50 +09002392 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002393 if (f2fs_has_inline_data(inode))
2394 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002395 if (ret == -EAGAIN)
Matthew Wilcox (Oracle)e20a7692020-06-01 21:47:27 -07002396 ret = f2fs_mpage_readpages(inode, NULL, page);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002397 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002398}
2399
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002400static void f2fs_readahead(struct readahead_control *rac)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002401{
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002402 struct inode *inode = rac->mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08002403
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002404 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002405
Chao Yu4c8ff702019-11-01 18:07:14 +08002406 if (!f2fs_is_compress_backend_ready(inode))
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002407 return;
Chao Yu4c8ff702019-11-01 18:07:14 +08002408
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002409 /* If the file has inline data, skip readpages */
2410 if (f2fs_has_inline_data(inode))
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002411 return;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002412
Matthew Wilcox (Oracle)e20a7692020-06-01 21:47:27 -07002413 f2fs_mpage_readpages(inode, rac, NULL);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002414}
2415
Chao Yu4c8ff702019-11-01 18:07:14 +08002416int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002417{
2418 struct inode *inode = fio->page->mapping->host;
Chao Yu4c8ff702019-11-01 18:07:14 +08002419 struct page *mpage, *page;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002420 gfp_t gfp_flags = GFP_NOFS;
2421
Jaegeuk Kim19585932017-09-05 16:54:24 -07002422 if (!f2fs_encrypted_file(inode))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002423 return 0;
2424
Chao Yu4c8ff702019-11-01 18:07:14 +08002425 page = fio->compressed_page ? fio->compressed_page : fio->page;
2426
Eric Biggers6dbb1792018-04-18 11:09:48 -07002427 /* wait for GCed page writeback via META_MAPPING */
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07002428 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002429
2430retry_encrypt:
Chao Yu4c8ff702019-11-01 18:07:14 +08002431 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2432 PAGE_SIZE, 0, gfp_flags);
Chao Yu6aa58d82018-08-14 22:37:25 +08002433 if (IS_ERR(fio->encrypted_page)) {
2434 /* flush pending IOs and wait for a while in the ENOMEM case */
2435 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2436 f2fs_flush_merged_writes(fio->sbi);
Chao Yu5df7731f2020-02-17 17:45:44 +08002437 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
Chao Yu6aa58d82018-08-14 22:37:25 +08002438 gfp_flags |= __GFP_NOFAIL;
2439 goto retry_encrypt;
2440 }
2441 return PTR_ERR(fio->encrypted_page);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002442 }
Chao Yu6aa58d82018-08-14 22:37:25 +08002443
2444 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2445 if (mpage) {
2446 if (PageUptodate(mpage))
2447 memcpy(page_address(mpage),
2448 page_address(fio->encrypted_page), PAGE_SIZE);
2449 f2fs_put_page(mpage, 1);
2450 }
2451 return 0;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002452}
2453
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002454static inline bool check_inplace_update_policy(struct inode *inode,
2455 struct f2fs_io_info *fio)
2456{
2457 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2458 unsigned int policy = SM_I(sbi)->ipu_policy;
2459
2460 if (policy & (0x1 << F2FS_IPU_FORCE))
2461 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08002462 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002463 return true;
2464 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2465 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2466 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08002467 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002468 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2469 return true;
2470
2471 /*
2472 * IPU for rewrite async pages
2473 */
2474 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2475 fio && fio->op == REQ_OP_WRITE &&
2476 !(fio->op_flags & REQ_SYNC) &&
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302477 !IS_ENCRYPTED(inode))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002478 return true;
2479
2480 /* this is only set during fdatasync */
2481 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2482 is_inode_flag_set(inode, FI_NEED_IPU))
2483 return true;
2484
Daniel Rosenberg43549942018-08-20 19:21:43 -07002485 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2486 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2487 return true;
2488
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002489 return false;
2490}
2491
Chao Yu4d57b862018-05-30 00:20:41 +08002492bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002493{
2494 if (f2fs_is_pinned_file(inode))
2495 return true;
2496
2497 /* if this is cold file, we should overwrite to avoid fragmentation */
2498 if (file_is_cold(inode))
2499 return true;
2500
2501 return check_inplace_update_policy(inode, fio);
2502}
2503
Chao Yu4d57b862018-05-30 00:20:41 +08002504bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002505{
2506 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2507
Chao Yub0332a02020-02-14 17:44:12 +08002508 if (f2fs_lfs_mode(sbi))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002509 return true;
2510 if (S_ISDIR(inode->i_mode))
2511 return true;
Chao Yuaf033b22018-09-20 20:05:00 +08002512 if (IS_NOQUOTA(inode))
2513 return true;
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002514 if (f2fs_is_atomic_file(inode))
2515 return true;
2516 if (fio) {
2517 if (is_cold_data(fio->page))
2518 return true;
2519 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2520 return true;
Daniel Rosenberg43549942018-08-20 19:21:43 -07002521 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2522 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2523 return true;
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002524 }
2525 return false;
2526}
2527
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002528static inline bool need_inplace_update(struct f2fs_io_info *fio)
2529{
2530 struct inode *inode = fio->page->mapping->host;
2531
Chao Yu4d57b862018-05-30 00:20:41 +08002532 if (f2fs_should_update_outplace(inode, fio))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002533 return false;
2534
Chao Yu4d57b862018-05-30 00:20:41 +08002535 return f2fs_should_update_inplace(inode, fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002536}
2537
Chao Yu4d57b862018-05-30 00:20:41 +08002538int f2fs_do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002539{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002540 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002541 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002542 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002543 struct extent_info ei = {0,0,0};
Chao Yu77357302018-07-17 00:02:17 +08002544 struct node_info ni;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002545 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002546 int err = 0;
2547
2548 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00002549 if (need_inplace_update(fio) &&
2550 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2551 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07002552
Chao Yuc9b60782018-08-01 19:13:44 +08002553 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08002554 DATA_GENERIC_ENHANCE))
Chao Yu10f966b2019-06-20 11:36:14 +08002555 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08002556
2557 ipu_force = true;
2558 fio->need_lock = LOCK_DONE;
2559 goto got_it;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002560 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002561
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002562 /* Deadlock due to between page->lock and f2fs_lock_op */
2563 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2564 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08002565
Chao Yu4d57b862018-05-30 00:20:41 +08002566 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002567 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002568 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002569
Chao Yu28bc1062016-02-06 14:40:34 +08002570 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002571
2572 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08002573 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002574 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +08002575 clear_cold_data(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002576 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002577 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00002578got_it:
Chao Yuc9b60782018-08-01 19:13:44 +08002579 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2580 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08002581 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08002582 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08002583 goto out_writepage;
2584 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002585 /*
2586 * If current allocation needs SSR,
2587 * it had better in-place writes for updated data.
2588 */
Chao Yu93770ab2019-04-15 15:26:32 +08002589 if (ipu_force ||
2590 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
Chao Yu7b525dd2018-05-23 22:25:08 +08002591 need_inplace_update(fio))) {
Chao Yu4c8ff702019-11-01 18:07:14 +08002592 err = f2fs_encrypt_one_page(fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002593 if (err)
2594 goto out_writepage;
2595
2596 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07002597 ClearPageError(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002598 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002599 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002600 f2fs_unlock_op(fio->sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08002601 err = f2fs_inplace_write_data(fio);
Chao Yu6492a332019-02-21 20:37:14 +08002602 if (err) {
2603 if (f2fs_encrypted_file(inode))
Eric Biggersd2d07272019-05-20 09:29:39 -07002604 fscrypt_finalize_bounce_page(&fio->encrypted_page);
Chao Yu6492a332019-02-21 20:37:14 +08002605 if (PageWriteback(page))
2606 end_page_writeback(page);
Chao Yucd23ffa92019-04-15 15:30:53 +08002607 } else {
2608 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu6492a332019-02-21 20:37:14 +08002609 }
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002610 trace_f2fs_do_write_data_page(fio->page, IPU);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002611 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002612 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002613
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002614 if (fio->need_lock == LOCK_RETRY) {
2615 if (!f2fs_trylock_op(fio->sbi)) {
2616 err = -EAGAIN;
2617 goto out_writepage;
2618 }
2619 fio->need_lock = LOCK_REQ;
2620 }
2621
Chao Yu77357302018-07-17 00:02:17 +08002622 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2623 if (err)
2624 goto out_writepage;
2625
2626 fio->version = ni.version;
2627
Chao Yu4c8ff702019-11-01 18:07:14 +08002628 err = f2fs_encrypt_one_page(fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002629 if (err)
2630 goto out_writepage;
2631
2632 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07002633 ClearPageError(page);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002634
Chao Yu4c8ff702019-11-01 18:07:14 +08002635 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2636 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2637
Hou Pengyang279d6df2017-04-27 00:17:21 +08002638 /* LFS mode write path */
Chao Yu4d57b862018-05-30 00:20:41 +08002639 f2fs_outplace_write_data(&dn, fio);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002640 trace_f2fs_do_write_data_page(page, OPU);
2641 set_inode_flag(inode, FI_APPEND_WRITE);
2642 if (page->index == 0)
2643 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002644out_writepage:
2645 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002646out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002647 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002648 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002649 return err;
2650}
2651
Chao Yu4c8ff702019-11-01 18:07:14 +08002652int f2fs_write_single_data_page(struct page *page, int *submitted,
Chao Yu8648de22019-02-19 16:15:29 +08002653 struct bio **bio,
2654 sector_t *last_block,
Chao Yub0af6d42017-08-02 23:21:48 +08002655 struct writeback_control *wbc,
Chao Yu4c8ff702019-11-01 18:07:14 +08002656 enum iostat_type io_type,
2657 int compr_blocks)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002658{
2659 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002660 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002661 loff_t i_size = i_size_read(inode);
Chao Yu4c8ff702019-11-01 18:07:14 +08002662 const pgoff_t end_index = ((unsigned long long)i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002663 >> PAGE_SHIFT;
Chao Yu1f0d5c92019-11-07 17:29:00 +08002664 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002665 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09002666 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002667 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002668 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002669 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08002670 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002671 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05002672 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06002673 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00002674 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002675 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002676 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002677 .submitted = false,
Chao Yu4c8ff702019-11-01 18:07:14 +08002678 .compr_blocks = compr_blocks,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002679 .need_lock = LOCK_RETRY,
Chao Yub0af6d42017-08-02 23:21:48 +08002680 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08002681 .io_wbc = wbc,
Chao Yu8648de22019-02-19 16:15:29 +08002682 .bio = bio,
2683 .last_block = last_block,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002684 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002685
Chao Yuecda0de2014-05-06 16:48:26 +08002686 trace_f2fs_writepage(page, DATA);
2687
Chao Yudb198ae2018-01-18 17:29:10 +08002688 /* we should bypass data pages to proceed the kworkder jobs */
2689 if (unlikely(f2fs_cp_error(sbi))) {
2690 mapping_set_error(page->mapping, -EIO);
Chao Yu1174abf2018-05-28 16:59:26 +08002691 /*
2692 * don't drop any dirty dentry pages for keeping lastest
2693 * directory structure.
2694 */
2695 if (S_ISDIR(inode->i_mode))
2696 goto redirty_out;
Chao Yudb198ae2018-01-18 17:29:10 +08002697 goto out;
2698 }
2699
Chao Yu0771fcc2017-06-29 23:20:45 +08002700 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2701 goto redirty_out;
2702
Chao Yu4c8ff702019-11-01 18:07:14 +08002703 if (page->index < end_index ||
2704 f2fs_verity_in_progress(inode) ||
2705 compr_blocks)
Jaegeuk Kim39936832012-11-22 16:21:29 +09002706 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002707
2708 /*
2709 * If the offset is out-of-range of file size,
2710 * this page does not have to be written to disk.
2711 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002712 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002713 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09002714 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002715
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002716 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09002717write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002718 if (f2fs_is_drop_cache(inode))
2719 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07002720 /* we should not write 0'th page having journal header */
2721 if (f2fs_is_volatile_file(inode) && (!page->index ||
2722 (!wbc->for_reclaim &&
Chao Yu4d57b862018-05-30 00:20:41 +08002723 f2fs_available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002724 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002725
Jaegeuk Kim435cbab2020-04-09 10:25:21 -07002726 /* Dentry/quota blocks are controlled by checkpoint */
2727 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
Chao Yu79963d92020-06-18 14:36:23 +08002728 /*
2729 * We need to wait for node_write to avoid block allocation during
2730 * checkpoint. This can only happen to quota writes which can cause
2731 * the below discard race condition.
2732 */
2733 if (IS_NOQUOTA(inode))
2734 down_read(&sbi->node_write);
2735
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002736 fio.need_lock = LOCK_DONE;
Chao Yu4d57b862018-05-30 00:20:41 +08002737 err = f2fs_do_write_data_page(&fio);
Chao Yu79963d92020-06-18 14:36:23 +08002738
2739 if (IS_NOQUOTA(inode))
2740 up_read(&sbi->node_write);
2741
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002742 goto done;
2743 }
2744
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002745 if (!wbc->for_reclaim)
2746 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002747 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09002748 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002749 else
2750 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002751
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002752 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08002753 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002754 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08002755 if (!err)
2756 goto out;
2757 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002758
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002759 if (err == -EAGAIN) {
Chao Yu4d57b862018-05-30 00:20:41 +08002760 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002761 if (err == -EAGAIN) {
2762 fio.need_lock = LOCK_REQ;
Chao Yu4d57b862018-05-30 00:20:41 +08002763 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002764 }
2765 }
Chao Yua0d00fa2017-10-09 17:55:19 +08002766
Chao Yueb449792018-01-17 16:31:37 +08002767 if (err) {
2768 file_set_keep_isize(inode);
2769 } else {
Chao Yuc10c9822020-02-27 19:30:03 +08002770 spin_lock(&F2FS_I(inode)->i_size_lock);
Chao Yueb449792018-01-17 16:31:37 +08002771 if (F2FS_I(inode)->last_disk_size < psize)
2772 F2FS_I(inode)->last_disk_size = psize;
Chao Yuc10c9822020-02-27 19:30:03 +08002773 spin_unlock(&F2FS_I(inode)->i_size_lock);
Chao Yueb449792018-01-17 16:31:37 +08002774 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002775
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002776done:
2777 if (err && err != -ENOENT)
2778 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002779
Jaegeuk Kim39936832012-11-22 16:21:29 +09002780out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002781 inode_dec_dirty_pages(inode);
Chao Yu2baf0782018-07-27 18:15:16 +08002782 if (err) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002783 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +08002784 clear_cold_data(page);
2785 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002786
2787 if (wbc->for_reclaim) {
Chao Yubab475c2018-09-27 23:41:16 +08002788 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002789 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu4d57b862018-05-30 00:20:41 +08002790 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002791 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08002792 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002793 unlock_page(page);
Chao Yu186857c2019-04-02 18:52:19 +08002794 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
Chao Yu0b20fce2019-09-30 18:53:25 +08002795 !F2FS_I(inode)->cp_task)
Jaegeuk Kima7881892017-04-20 13:51:57 -07002796 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08002797
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002798 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002799 f2fs_submit_merged_write(sbi, DATA);
Chao Yu0b20fce2019-09-30 18:53:25 +08002800 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002801 submitted = NULL;
2802 }
2803
2804 if (submitted)
Chao Yu4c8ff702019-11-01 18:07:14 +08002805 *submitted = fio.submitted ? 1 : 0;
Chao Yu0c3a5792016-01-18 18:28:11 +08002806
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002807 return 0;
2808
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002809redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002810 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim5b19d282018-05-03 23:26:02 -07002811 /*
2812 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2813 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2814 * file_write_and_wait_range() will see EIO error, which is critical
2815 * to return value of fsync() followed by atomic_write failure to user.
2816 */
2817 if (!err || wbc->for_reclaim)
Chao Yu0002b612016-11-28 19:13:43 -08002818 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002819 unlock_page(page);
2820 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09002821}
2822
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002823static int f2fs_write_data_page(struct page *page,
2824 struct writeback_control *wbc)
2825{
Chao Yu4c8ff702019-11-01 18:07:14 +08002826#ifdef CONFIG_F2FS_FS_COMPRESSION
2827 struct inode *inode = page->mapping->host;
2828
2829 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2830 goto out;
2831
2832 if (f2fs_compressed_file(inode)) {
2833 if (f2fs_is_compressed_cluster(inode, page->index)) {
2834 redirty_page_for_writepage(wbc, page);
2835 return AOP_WRITEPAGE_ACTIVATE;
2836 }
2837 }
2838out:
2839#endif
2840
2841 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2842 wbc, FS_DATA_IO, 0);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002843}
2844
Chao Yu8f46dca2015-07-14 18:56:10 +08002845/*
2846 * This function was copied from write_cche_pages from mm/page-writeback.c.
2847 * The major change is making write step of cold data page separately from
2848 * warm/hot data page.
2849 */
2850static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08002851 struct writeback_control *wbc,
2852 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08002853{
2854 int ret = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08002855 int done = 0, retry = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08002856 struct pagevec pvec;
Chao Yuc29fd0c2018-06-04 23:20:36 +08002857 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Chao Yu8648de22019-02-19 16:15:29 +08002858 struct bio *bio = NULL;
2859 sector_t last_block;
Chao Yu4c8ff702019-11-01 18:07:14 +08002860#ifdef CONFIG_F2FS_FS_COMPRESSION
2861 struct inode *inode = mapping->host;
2862 struct compress_ctx cc = {
2863 .inode = inode,
2864 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2865 .cluster_size = F2FS_I(inode)->i_cluster_size,
2866 .cluster_idx = NULL_CLUSTER,
2867 .rpages = NULL,
2868 .nr_rpages = 0,
2869 .cpages = NULL,
2870 .rbuf = NULL,
2871 .cbuf = NULL,
2872 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2873 .private = NULL,
2874 };
2875#endif
Chao Yu8f46dca2015-07-14 18:56:10 +08002876 int nr_pages;
2877 pgoff_t uninitialized_var(writeback_index);
2878 pgoff_t index;
2879 pgoff_t end; /* Inclusive */
2880 pgoff_t done_index;
Chao Yu8f46dca2015-07-14 18:56:10 +08002881 int range_whole = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05002882 xa_mark_t tag;
Chao Yubab475c2018-09-27 23:41:16 +08002883 int nwritten = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08002884 int submitted = 0;
2885 int i;
Chao Yu8f46dca2015-07-14 18:56:10 +08002886
Mel Gorman86679822017-11-15 17:37:52 -08002887 pagevec_init(&pvec);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07002888
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002889 if (get_dirty_pages(mapping->host) <=
2890 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2891 set_inode_flag(mapping->host, FI_HOT_DATA);
2892 else
2893 clear_inode_flag(mapping->host, FI_HOT_DATA);
2894
Chao Yu8f46dca2015-07-14 18:56:10 +08002895 if (wbc->range_cyclic) {
2896 writeback_index = mapping->writeback_index; /* prev offset */
2897 index = writeback_index;
Chao Yu8f46dca2015-07-14 18:56:10 +08002898 end = -1;
2899 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002900 index = wbc->range_start >> PAGE_SHIFT;
2901 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08002902 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2903 range_whole = 1;
Chao Yu8f46dca2015-07-14 18:56:10 +08002904 }
2905 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2906 tag = PAGECACHE_TAG_TOWRITE;
2907 else
2908 tag = PAGECACHE_TAG_DIRTY;
2909retry:
Chao Yu4c8ff702019-11-01 18:07:14 +08002910 retry = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08002911 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2912 tag_pages_for_writeback(mapping, index, end);
2913 done_index = index;
Chao Yu4c8ff702019-11-01 18:07:14 +08002914 while (!done && !retry && (index <= end)) {
Jan Kara69c4f352017-11-15 17:34:48 -08002915 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08002916 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08002917 if (nr_pages == 0)
2918 break;
2919
2920 for (i = 0; i < nr_pages; i++) {
2921 struct page *page = pvec.pages[i];
Chao Yu4c8ff702019-11-01 18:07:14 +08002922 bool need_readd;
2923readd:
2924 need_readd = false;
2925#ifdef CONFIG_F2FS_FS_COMPRESSION
2926 if (f2fs_compressed_file(inode)) {
2927 ret = f2fs_init_compress_ctx(&cc);
2928 if (ret) {
2929 done = 1;
2930 break;
2931 }
Chao Yu8f46dca2015-07-14 18:56:10 +08002932
Chao Yu4c8ff702019-11-01 18:07:14 +08002933 if (!f2fs_cluster_can_merge_page(&cc,
2934 page->index)) {
2935 ret = f2fs_write_multi_pages(&cc,
2936 &submitted, wbc, io_type);
2937 if (!ret)
2938 need_readd = true;
2939 goto result;
2940 }
2941
2942 if (unlikely(f2fs_cp_error(sbi)))
2943 goto lock_page;
2944
2945 if (f2fs_cluster_is_empty(&cc)) {
2946 void *fsdata = NULL;
2947 struct page *pagep;
2948 int ret2;
2949
2950 ret2 = f2fs_prepare_compress_overwrite(
2951 inode, &pagep,
2952 page->index, &fsdata);
2953 if (ret2 < 0) {
2954 ret = ret2;
2955 done = 1;
2956 break;
2957 } else if (ret2 &&
2958 !f2fs_compress_write_end(inode,
2959 fsdata, page->index,
2960 1)) {
2961 retry = 1;
2962 break;
2963 }
2964 } else {
2965 goto lock_page;
2966 }
2967 }
2968#endif
Chao Yuf8de4332018-05-23 22:25:09 +08002969 /* give a priority to WB_SYNC threads */
Chao Yuc29fd0c2018-06-04 23:20:36 +08002970 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
Chao Yuf8de4332018-05-23 22:25:09 +08002971 wbc->sync_mode == WB_SYNC_NONE) {
2972 done = 1;
2973 break;
2974 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002975#ifdef CONFIG_F2FS_FS_COMPRESSION
2976lock_page:
2977#endif
Chao Yu8f46dca2015-07-14 18:56:10 +08002978 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002979retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08002980 lock_page(page);
2981
2982 if (unlikely(page->mapping != mapping)) {
2983continue_unlock:
2984 unlock_page(page);
2985 continue;
2986 }
2987
2988 if (!PageDirty(page)) {
2989 /* someone wrote it for us */
2990 goto continue_unlock;
2991 }
2992
Chao Yu8f46dca2015-07-14 18:56:10 +08002993 if (PageWriteback(page)) {
Chao Yu0b20fce2019-09-30 18:53:25 +08002994 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002995 f2fs_wait_on_page_writeback(page,
Chao Yubae0ee72018-12-25 17:43:42 +08002996 DATA, true, true);
Chao Yu0b20fce2019-09-30 18:53:25 +08002997 else
Chao Yu8f46dca2015-07-14 18:56:10 +08002998 goto continue_unlock;
2999 }
3000
Chao Yu8f46dca2015-07-14 18:56:10 +08003001 if (!clear_page_dirty_for_io(page))
3002 goto continue_unlock;
3003
Chao Yu4c8ff702019-11-01 18:07:14 +08003004#ifdef CONFIG_F2FS_FS_COMPRESSION
3005 if (f2fs_compressed_file(inode)) {
3006 get_page(page);
3007 f2fs_compress_ctx_add_page(&cc, page);
3008 continue;
3009 }
3010#endif
3011 ret = f2fs_write_single_data_page(page, &submitted,
3012 &bio, &last_block, wbc, io_type, 0);
3013 if (ret == AOP_WRITEPAGE_ACTIVATE)
3014 unlock_page(page);
3015#ifdef CONFIG_F2FS_FS_COMPRESSION
3016result:
3017#endif
3018 nwritten += submitted;
3019 wbc->nr_to_write -= submitted;
3020
Chao Yu8f46dca2015-07-14 18:56:10 +08003021 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08003022 /*
3023 * keep nr_to_write, since vfs uses this to
3024 * get # of written pages.
3025 */
3026 if (ret == AOP_WRITEPAGE_ACTIVATE) {
Chao Yu0002b612016-11-28 19:13:43 -08003027 ret = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08003028 goto next;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07003029 } else if (ret == -EAGAIN) {
3030 ret = 0;
3031 if (wbc->sync_mode == WB_SYNC_ALL) {
3032 cond_resched();
3033 congestion_wait(BLK_RW_ASYNC,
Chao Yu5df7731f2020-02-17 17:45:44 +08003034 DEFAULT_IO_TIMEOUT);
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07003035 goto retry_write;
3036 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003037 goto next;
Chao Yu0002b612016-11-28 19:13:43 -08003038 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07003039 done_index = page->index + 1;
3040 done = 1;
3041 break;
Chao Yu8f46dca2015-07-14 18:56:10 +08003042 }
3043
Chao Yu4c8ff702019-11-01 18:07:14 +08003044 if (wbc->nr_to_write <= 0 &&
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003045 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08003046 done = 1;
3047 break;
3048 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003049next:
3050 if (need_readd)
3051 goto readd;
Chao Yu8f46dca2015-07-14 18:56:10 +08003052 }
3053 pagevec_release(&pvec);
3054 cond_resched();
3055 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003056#ifdef CONFIG_F2FS_FS_COMPRESSION
3057 /* flush remained pages in compress cluster */
3058 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3059 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3060 nwritten += submitted;
3061 wbc->nr_to_write -= submitted;
3062 if (ret) {
3063 done = 1;
3064 retry = 0;
3065 }
3066 }
3067#endif
Sahitya Tummalae78790f2020-06-02 18:11:47 +05303068 if (retry) {
Chao Yu8f46dca2015-07-14 18:56:10 +08003069 index = 0;
Sahitya Tummalae78790f2020-06-02 18:11:47 +05303070 end = -1;
Chao Yu8f46dca2015-07-14 18:56:10 +08003071 goto retry;
3072 }
Sahitya Tummalae78790f2020-06-02 18:11:47 +05303073 if (wbc->range_cyclic && !done)
3074 done_index = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08003075 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3076 mapping->writeback_index = done_index;
3077
Chao Yubab475c2018-09-27 23:41:16 +08003078 if (nwritten)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07003079 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
Chao Yubab475c2018-09-27 23:41:16 +08003080 NULL, 0, DATA);
Chao Yu8648de22019-02-19 16:15:29 +08003081 /* submit cached bio of IPU write */
3082 if (bio)
Chao Yu0b20fce2019-09-30 18:53:25 +08003083 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
Chao Yu6ca56ca2016-09-29 18:50:11 +08003084
Chao Yu8f46dca2015-07-14 18:56:10 +08003085 return ret;
3086}
3087
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003088static inline bool __should_serialize_io(struct inode *inode,
3089 struct writeback_control *wbc)
3090{
Chao Yu040d2bb2019-05-20 17:36:59 +08003091 /* to avoid deadlock in path of data flush */
3092 if (F2FS_I(inode)->cp_task)
3093 return false;
Chao Yub13f67ff2020-03-19 19:57:57 +08003094
3095 if (!S_ISREG(inode->i_mode))
3096 return false;
3097 if (IS_NOQUOTA(inode))
3098 return false;
3099
3100 if (f2fs_compressed_file(inode))
3101 return true;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003102 if (wbc->sync_mode != WB_SYNC_ALL)
3103 return true;
3104 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3105 return true;
3106 return false;
3107}
3108
Chao Yufc99fe22018-05-30 00:20:39 +08003109static int __f2fs_write_data_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08003110 struct writeback_control *wbc,
3111 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003112{
3113 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07003114 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003115 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003116 int ret;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003117 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003118
P J Pcfb185a2013-04-03 11:38:00 +09003119 /* deal with chardevs and other special file */
3120 if (!mapping->a_ops->writepage)
3121 return 0;
3122
Chao Yu6a290542015-07-17 18:02:39 +08003123 /* skip writing if there is no dirty page in this inode */
3124 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3125 return 0;
3126
Chao Yu0771fcc2017-06-29 23:20:45 +08003127 /* during POR, we don't need to trigger writepage at all. */
3128 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3129 goto skip_write;
3130
Chao Yuaf033b22018-09-20 20:05:00 +08003131 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3132 wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima1257022015-10-08 10:40:07 -07003133 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Chao Yu4d57b862018-05-30 00:20:41 +08003134 f2fs_available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kima1257022015-10-08 10:40:07 -07003135 goto skip_write;
3136
Chao Yud323d002015-10-27 09:53:45 +08003137 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07003138 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08003139 goto skip_write;
3140
Yunlei Hed31c7c32016-02-04 16:14:00 +08003141 trace_f2fs_writepages(mapping->host, wbc, DATA);
3142
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003143 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3144 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08003145 atomic_inc(&sbi->wb_sync_req[DATA]);
3146 else if (atomic_read(&sbi->wb_sync_req[DATA]))
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003147 goto skip_write;
3148
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003149 if (__should_serialize_io(inode, wbc)) {
3150 mutex_lock(&sbi->writepages);
3151 locked = true;
3152 }
3153
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003154 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08003155 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003156 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003157
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003158 if (locked)
3159 mutex_unlock(&sbi->writepages);
3160
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003161 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08003162 atomic_dec(&sbi->wb_sync_req[DATA]);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07003163 /*
3164 * if some pages were truncated, we cannot guarantee its mapping->host
3165 * to detect pending bios.
3166 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003167
Chao Yu4d57b862018-05-30 00:20:41 +08003168 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003169 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09003170
3171skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003172 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08003173 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09003174 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003175}
3176
Chao Yub0af6d42017-08-02 23:21:48 +08003177static int f2fs_write_data_pages(struct address_space *mapping,
3178 struct writeback_control *wbc)
3179{
3180 struct inode *inode = mapping->host;
3181
3182 return __f2fs_write_data_pages(mapping, wbc,
3183 F2FS_I(inode)->cp_task == current ?
3184 FS_CP_DATA_IO : FS_DATA_IO);
3185}
3186
Chao Yu3aab8f82014-07-02 13:25:04 +08003187static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3188{
3189 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08003190 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08003191
Jaegeuk Kim3f188c22019-12-03 18:54:29 -08003192 if (IS_NOQUOTA(inode))
3193 return;
3194
Eric Biggers95ae2512019-07-22 09:26:24 -07003195 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3196 if (to > i_size && !f2fs_verity_in_progress(inode)) {
Chao Yua33c1502018-08-05 23:04:25 +08003197 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09003198 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08003199
Jaegeuk Kim819d9152015-12-28 13:48:11 -08003200 truncate_pagecache(inode, i_size);
Jaegeuk Kim3f188c22019-12-03 18:54:29 -08003201 f2fs_truncate_blocks(inode, i_size, true);
Chao Yua33c1502018-08-05 23:04:25 +08003202
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08003203 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09003204 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu3aab8f82014-07-02 13:25:04 +08003205 }
3206}
3207
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003208static int prepare_write_begin(struct f2fs_sb_info *sbi,
3209 struct page *page, loff_t pos, unsigned len,
3210 block_t *blk_addr, bool *node_changed)
3211{
3212 struct inode *inode = page->mapping->host;
3213 pgoff_t index = page->index;
3214 struct dnode_of_data dn;
3215 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003216 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00003217 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003218 int err = 0;
Sheng Yong2866fb12018-11-14 19:34:28 +08003219 int flag;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003220
Jaegeuk Kim24b84912016-02-03 13:49:44 -08003221 /*
3222 * we already allocated all the blocks, so we don't need to get
3223 * the block addresses when there is no need to fill the page.
3224 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003225 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
Eric Biggers95ae2512019-07-22 09:26:24 -07003226 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3227 !f2fs_verity_in_progress(inode))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08003228 return 0;
3229
Sheng Yong2866fb12018-11-14 19:34:28 +08003230 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3231 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3232 flag = F2FS_GET_BLOCK_DEFAULT;
3233 else
3234 flag = F2FS_GET_BLOCK_PRE_AIO;
3235
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003236 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003237 (pos & PAGE_MASK) >= i_size_read(inode)) {
Chao Yu0ef81832020-06-18 14:36:22 +08003238 f2fs_do_map_lock(sbi, flag, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003239 locked = true;
3240 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003241
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003242restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003243 /* check inline_data */
Chao Yu4d57b862018-05-30 00:20:41 +08003244 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003245 if (IS_ERR(ipage)) {
3246 err = PTR_ERR(ipage);
3247 goto unlock_out;
3248 }
3249
3250 set_new_dnode(&dn, inode, ipage, ipage, 0);
3251
3252 if (f2fs_has_inline_data(inode)) {
Chao Yuf2470372017-07-19 00:19:05 +08003253 if (pos + len <= MAX_INLINE_DATA(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003254 f2fs_do_read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07003255 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08003256 if (inode->i_nlink)
3257 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003258 } else {
3259 err = f2fs_convert_inline_page(&dn, page);
3260 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003261 goto out;
3262 if (dn.data_blkaddr == NULL_ADDR)
3263 err = f2fs_get_block(&dn, index);
3264 }
3265 } else if (locked) {
3266 err = f2fs_get_block(&dn, index);
3267 } else {
3268 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3269 dn.data_blkaddr = ei.blk + index - ei.fofs;
3270 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003271 /* hole case */
Chao Yu4d57b862018-05-30 00:20:41 +08003272 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07003273 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003274 f2fs_put_dnode(&dn);
Chao Yu0ef81832020-06-18 14:36:22 +08003275 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
Yunlei He59c90812017-03-13 20:22:18 +08003276 true);
Sheng Yong2866fb12018-11-14 19:34:28 +08003277 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003278 locked = true;
3279 goto restart;
3280 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003281 }
3282 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003283
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003284 /* convert_inline_page can make node_changed */
3285 *blk_addr = dn.data_blkaddr;
3286 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003287out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003288 f2fs_put_dnode(&dn);
3289unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003290 if (locked)
Chao Yu0ef81832020-06-18 14:36:22 +08003291 f2fs_do_map_lock(sbi, flag, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003292 return err;
3293}
3294
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003295static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3296 loff_t pos, unsigned len, unsigned flags,
3297 struct page **pagep, void **fsdata)
3298{
3299 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07003300 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07003301 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003302 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yua2e2e762018-01-15 17:16:46 +08003303 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003304 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003305 int err = 0;
3306
Chao Yu62aed042014-05-06 16:46:04 +08003307 trace_f2fs_write_begin(inode, pos, len, flags);
3308
Chao Yu00e09c02019-08-23 17:58:36 +08003309 if (!f2fs_is_checkpoint_ready(sbi)) {
3310 err = -ENOSPC;
Daniel Rosenberg43549942018-08-20 19:21:43 -07003311 goto fail;
Chao Yu00e09c02019-08-23 17:58:36 +08003312 }
Daniel Rosenberg43549942018-08-20 19:21:43 -07003313
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09003314 if ((f2fs_is_atomic_file(inode) &&
3315 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3316 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07003317 err = -ENOMEM;
Chao Yua2e2e762018-01-15 17:16:46 +08003318 drop_atomic = true;
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07003319 goto fail;
3320 }
3321
Jaegeuk Kim5f727392014-11-25 10:59:45 -08003322 /*
3323 * We should check this at this moment to avoid deadlock on inode page
3324 * and #0 page. The locking rule for inline_data conversion should be:
3325 * lock_page(page #0) -> lock_page(inode_page)
3326 */
3327 if (index != 0) {
3328 err = f2fs_convert_inline_inode(inode);
3329 if (err)
3330 goto fail;
3331 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003332
3333#ifdef CONFIG_F2FS_FS_COMPRESSION
3334 if (f2fs_compressed_file(inode)) {
3335 int ret;
3336
3337 *fsdata = NULL;
3338
3339 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3340 index, fsdata);
3341 if (ret < 0) {
3342 err = ret;
3343 goto fail;
3344 } else if (ret) {
3345 return 0;
3346 }
3347 }
3348#endif
3349
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09003350repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08003351 /*
3352 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3353 * wait_for_stable_page. Will wait that below with our IO control.
3354 */
Chao Yu01eccef2017-10-28 16:52:30 +08003355 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kim86d54792017-02-17 09:55:55 -08003356 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08003357 if (!page) {
3358 err = -ENOMEM;
3359 goto fail;
3360 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09003361
Chao Yu4c8ff702019-11-01 18:07:14 +08003362 /* TODO: cluster can be compressed due to race with .writepage */
3363
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003364 *pagep = page;
3365
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003366 err = prepare_write_begin(sbi, page, pos, len,
3367 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003368 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003369 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003370
Chao Yuaf033b22018-09-20 20:05:00 +08003371 if (need_balance && !IS_NOQUOTA(inode) &&
3372 has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08003373 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08003374 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08003375 lock_page(page);
3376 if (page->mapping != mapping) {
3377 /* The page got truncated from under us */
3378 f2fs_put_page(page, 1);
3379 goto repeat;
3380 }
3381 }
3382
Chao Yubae0ee72018-12-25 17:43:42 +08003383 f2fs_wait_on_page_writeback(page, DATA, false, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07003384
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003385 if (len == PAGE_SIZE || PageUptodate(page))
3386 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003387
Eric Biggers95ae2512019-07-22 09:26:24 -07003388 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3389 !f2fs_verity_in_progress(inode)) {
Yunlei He746e2402016-12-20 11:11:35 +08003390 zero_user_segment(page, len, PAGE_SIZE);
3391 return 0;
3392 }
3393
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003394 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003395 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003396 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003397 } else {
Chao Yu93770ab2019-04-15 15:26:32 +08003398 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3399 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08003400 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08003401 goto fail;
3402 }
Jia Yangb7973092020-07-01 10:27:40 +08003403 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07003404 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08003405 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08003406
Jaegeuk Kim393ff912013-03-08 21:29:23 +09003407 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09003408 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09003409 f2fs_put_page(page, 1);
3410 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003411 }
Chao Yu1563ac72016-07-03 22:05:12 +08003412 if (unlikely(!PageUptodate(page))) {
3413 err = -EIO;
3414 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07003415 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003416 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003417 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003418
Chao Yu3aab8f82014-07-02 13:25:04 +08003419fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07003420 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08003421 f2fs_write_failed(mapping, pos + len);
Chao Yua2e2e762018-01-15 17:16:46 +08003422 if (drop_atomic)
Chao Yu4d57b862018-05-30 00:20:41 +08003423 f2fs_drop_inmem_pages_all(sbi, false);
Chao Yu3aab8f82014-07-02 13:25:04 +08003424 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003425}
3426
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003427static int f2fs_write_end(struct file *file,
3428 struct address_space *mapping,
3429 loff_t pos, unsigned len, unsigned copied,
3430 struct page *page, void *fsdata)
3431{
3432 struct inode *inode = page->mapping->host;
3433
Chao Yudfb2bf32014-05-06 16:47:23 +08003434 trace_f2fs_write_end(inode, pos, len, copied);
3435
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003436 /*
3437 * This should be come from len == PAGE_SIZE, and we expect copied
3438 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3439 * let generic_perform_write() try to copy data again through copied=0.
3440 */
3441 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08003442 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003443 copied = 0;
3444 else
3445 SetPageUptodate(page);
3446 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003447
3448#ifdef CONFIG_F2FS_FS_COMPRESSION
3449 /* overwrite compressed file */
3450 if (f2fs_compressed_file(inode) && fsdata) {
3451 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3452 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3453 return copied;
3454 }
3455#endif
3456
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003457 if (!copied)
3458 goto unlock_out;
3459
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003460 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003461
Eric Biggers95ae2512019-07-22 09:26:24 -07003462 if (pos + copied > i_size_read(inode) &&
3463 !f2fs_verity_in_progress(inode))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07003464 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003465unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08003466 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08003467 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003468 return copied;
3469}
3470
Omar Sandoval6f673762015-03-16 04:33:52 -07003471static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3472 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003473{
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07003474 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3475 unsigned blkbits = i_blkbits;
3476 unsigned blocksize_mask = (1 << blkbits) - 1;
3477 unsigned long align = offset | iov_iter_alignment(iter);
3478 struct block_device *bdev = inode->i_sb->s_bdev;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003479
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07003480 if (align & blocksize_mask) {
3481 if (bdev)
3482 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3483 blocksize_mask = (1 << blkbits) - 1;
3484 if (align & blocksize_mask)
3485 return -EINVAL;
3486 return 1;
3487 }
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003488 return 0;
3489}
3490
Chao Yu02b16d02018-11-12 00:46:46 +08003491static void f2fs_dio_end_io(struct bio *bio)
3492{
3493 struct f2fs_private_dio *dio = bio->bi_private;
3494
3495 dec_page_count(F2FS_I_SB(dio->inode),
3496 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3497
3498 bio->bi_private = dio->orig_private;
3499 bio->bi_end_io = dio->orig_end_io;
3500
Jaegeuk Kim52225952018-12-13 18:38:33 -08003501 kvfree(dio);
Chao Yu02b16d02018-11-12 00:46:46 +08003502
3503 bio_endio(bio);
3504}
3505
3506static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3507 loff_t file_offset)
3508{
3509 struct f2fs_private_dio *dio;
3510 bool write = (bio_op(bio) == REQ_OP_WRITE);
Chao Yu02b16d02018-11-12 00:46:46 +08003511
3512 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3513 sizeof(struct f2fs_private_dio), GFP_NOFS);
YueHaibing8e114032019-01-04 01:38:29 +00003514 if (!dio)
Chao Yu02b16d02018-11-12 00:46:46 +08003515 goto out;
Chao Yu02b16d02018-11-12 00:46:46 +08003516
3517 dio->inode = inode;
3518 dio->orig_end_io = bio->bi_end_io;
3519 dio->orig_private = bio->bi_private;
3520 dio->write = write;
3521
3522 bio->bi_end_io = f2fs_dio_end_io;
3523 bio->bi_private = dio;
3524
3525 inc_page_count(F2FS_I_SB(inode),
3526 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3527
3528 submit_bio(bio);
3529 return;
3530out:
3531 bio->bi_status = BLK_STS_IOERR;
3532 bio_endio(bio);
3533}
3534
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003535static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003536{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003537 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08003538 struct inode *inode = mapping->host;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003539 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf847c692018-09-27 18:34:52 +08003540 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08003541 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003542 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08003543 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08003544 int err;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003545 enum rw_hint hint = iocb->ki_hint;
Chao Yu63189b72018-03-08 14:22:56 +08003546 int whint_mode = F2FS_OPTION(sbi).whint_mode;
Chao Yuf847c692018-09-27 18:34:52 +08003547 bool do_opu;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003548
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003549 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08003550 if (err)
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07003551 return err < 0 ? err : 0;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08003552
Chao Yuf847c692018-09-27 18:34:52 +08003553 if (f2fs_force_buffered_io(inode, iocb, iter))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07003554 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003555
Chao Yuf847c692018-09-27 18:34:52 +08003556 do_opu = allow_outplace_dio(inode, iocb, iter);
3557
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07003558 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08003559
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003560 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3561 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3562
Chao Yuf847c692018-09-27 18:34:52 +08003563 if (iocb->ki_flags & IOCB_NOWAIT) {
3564 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003565 iocb->ki_hint = hint;
3566 err = -EAGAIN;
3567 goto out;
3568 }
Chao Yuf847c692018-09-27 18:34:52 +08003569 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3570 up_read(&fi->i_gc_rwsem[rw]);
3571 iocb->ki_hint = hint;
3572 err = -EAGAIN;
3573 goto out;
3574 }
3575 } else {
3576 down_read(&fi->i_gc_rwsem[rw]);
3577 if (do_opu)
3578 down_read(&fi->i_gc_rwsem[READ]);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003579 }
3580
Chao Yu02b16d02018-11-12 00:46:46 +08003581 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
Chao Yuf9d6d052018-11-13 14:33:45 +08003582 iter, rw == WRITE ? get_data_block_dio_write :
3583 get_data_block_dio, NULL, f2fs_dio_submit_bio,
DongDongJuad8d6a02020-03-20 15:01:32 +09003584 rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3585 DIO_SKIP_HOLES);
Chao Yuf847c692018-09-27 18:34:52 +08003586
3587 if (do_opu)
3588 up_read(&fi->i_gc_rwsem[READ]);
3589
3590 up_read(&fi->i_gc_rwsem[rw]);
Chao Yu82e0a5a2016-07-13 09:18:29 +08003591
3592 if (rw == WRITE) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003593 if (whint_mode == WHINT_MODE_OFF)
3594 iocb->ki_hint = hint;
Chao Yub0af6d42017-08-02 23:21:48 +08003595 if (err > 0) {
3596 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3597 err);
Chao Yuf847c692018-09-27 18:34:52 +08003598 if (!do_opu)
3599 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yub0af6d42017-08-02 23:21:48 +08003600 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04003601 f2fs_write_failed(mapping, offset + count);
Chao Yub0af6d42017-08-02 23:21:48 +08003602 }
Chao Yu8b83ac82020-04-16 18:16:56 +08003603 } else {
3604 if (err > 0)
3605 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04003606 }
Chao Yu70407fa2014-07-31 21:11:22 +08003607
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003608out:
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07003609 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08003610
Chao Yu3aab8f82014-07-02 13:25:04 +08003611 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003612}
3613
Chao Yu487261f2015-02-05 17:44:29 +08003614void f2fs_invalidate_page(struct page *page, unsigned int offset,
3615 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003616{
3617 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08003618 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003619
Chao Yu487261f2015-02-05 17:44:29 +08003620 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003621 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003622 return;
3623
Chao Yu487261f2015-02-05 17:44:29 +08003624 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08003625 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08003626 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08003627 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08003628 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08003629 } else {
Chao Yu487261f2015-02-05 17:44:29 +08003630 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +08003631 f2fs_remove_dirty_inode(inode);
Chao Yu933439c2016-10-11 22:57:01 +08003632 }
Chao Yu487261f2015-02-05 17:44:29 +08003633 }
Chao Yudecd36b2015-08-07 18:42:09 +08003634
Chao Yu2baf0782018-07-27 18:15:16 +08003635 clear_cold_data(page);
3636
Chao Yudecd36b2015-08-07 18:42:09 +08003637 if (IS_ATOMIC_WRITTEN_PAGE(page))
Chao Yu4d57b862018-05-30 00:20:41 +08003638 return f2fs_drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08003639
Chao Yu240a5912019-03-06 17:30:59 +08003640 f2fs_clear_page_private(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003641}
3642
Chao Yu487261f2015-02-05 17:44:29 +08003643int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003644{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08003645 /* If this is dirty page, keep PagePrivate */
3646 if (PageDirty(page))
3647 return 0;
3648
Chao Yudecd36b2015-08-07 18:42:09 +08003649 /* This is atomic written page, keep Private */
3650 if (IS_ATOMIC_WRITTEN_PAGE(page))
3651 return 0;
3652
Chao Yu2baf0782018-07-27 18:15:16 +08003653 clear_cold_data(page);
Chao Yu240a5912019-03-06 17:30:59 +08003654 f2fs_clear_page_private(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09003655 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003656}
3657
3658static int f2fs_set_data_page_dirty(struct page *page)
3659{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003660 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003661
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09003662 trace_f2fs_set_page_dirty(page, DATA);
3663
Jaegeuk Kim237c0792016-06-30 18:49:15 -07003664 if (!PageUptodate(page))
3665 SetPageUptodate(page);
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003666 if (PageSwapCache(page))
3667 return __set_page_dirty_nobuffers(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003668
Chao Yu5fe45742017-01-07 18:50:26 +08003669 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08003670 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003671 f2fs_register_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08003672 return 1;
3673 }
3674 /*
3675 * Previously, this page has been registered, we just
3676 * return here.
3677 */
3678 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003679 }
3680
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003681 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07003682 __set_page_dirty_nobuffers(page);
Chao Yu4d57b862018-05-30 00:20:41 +08003683 f2fs_update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003684 return 1;
3685 }
3686 return 0;
3687}
3688
Chao Yuc1c63382020-03-30 17:13:29 +08003689
3690static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3691{
3692#ifdef CONFIG_F2FS_FS_COMPRESSION
3693 struct dnode_of_data dn;
3694 sector_t start_idx, blknr = 0;
3695 int ret;
3696
3697 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3698
3699 set_new_dnode(&dn, inode, NULL, NULL, 0);
3700 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3701 if (ret)
3702 return 0;
3703
3704 if (dn.data_blkaddr != COMPRESS_ADDR) {
3705 dn.ofs_in_node += block - start_idx;
3706 blknr = f2fs_data_blkaddr(&dn);
3707 if (!__is_valid_data_blkaddr(blknr))
3708 blknr = 0;
3709 }
3710
3711 f2fs_put_dnode(&dn);
Chao Yuc1c63382020-03-30 17:13:29 +08003712 return blknr;
3713#else
Chao Yu250e84d2020-06-28 20:29:38 +08003714 return 0;
Chao Yuc1c63382020-03-30 17:13:29 +08003715#endif
3716}
3717
3718
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09003719static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3720{
Chao Yu454ae7e2014-04-22 13:34:01 +08003721 struct inode *inode = mapping->host;
Chao Yub79b0a32020-06-29 20:13:12 +08003722 struct buffer_head tmp = {
3723 .b_size = i_blocksize(inode),
3724 };
3725 sector_t blknr = 0;
Chao Yu454ae7e2014-04-22 13:34:01 +08003726
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07003727 if (f2fs_has_inline_data(inode))
Chao Yub79b0a32020-06-29 20:13:12 +08003728 goto out;
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07003729
3730 /* make sure allocating whole blocks */
3731 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3732 filemap_write_and_wait(mapping);
3733
Chao Yuc1c63382020-03-30 17:13:29 +08003734 if (f2fs_compressed_file(inode))
Chao Yub79b0a32020-06-29 20:13:12 +08003735 blknr = f2fs_bmap_compress(inode, block);
Chao Yuc1c63382020-03-30 17:13:29 +08003736
Chao Yub79b0a32020-06-29 20:13:12 +08003737 if (!get_data_block_bmap(inode, block, &tmp, 0))
3738 blknr = tmp.b_blocknr;
3739out:
3740 trace_f2fs_bmap(inode, block, blknr);
3741 return blknr;
Chao Yu429511c2015-02-05 17:54:31 +08003742}
3743
Weichao Guo5b7a4872016-09-20 05:03:27 +08003744#ifdef CONFIG_MIGRATION
3745#include <linux/migrate.h>
3746
3747int f2fs_migrate_page(struct address_space *mapping,
3748 struct page *newpage, struct page *page, enum migrate_mode mode)
3749{
3750 int rc, extra_count;
3751 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3752 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3753
3754 BUG_ON(PageWriteback(page));
3755
3756 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07003757 if (atomic_written) {
3758 if (mode != MIGRATE_SYNC)
3759 return -EBUSY;
3760 if (!mutex_trylock(&fi->inmem_lock))
3761 return -EAGAIN;
3762 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08003763
Chao Yu240a5912019-03-06 17:30:59 +08003764 /* one extra reference was held for atomic_write page */
3765 extra_count = atomic_written ? 1 : 0;
Weichao Guo5b7a4872016-09-20 05:03:27 +08003766 rc = migrate_page_move_mapping(mapping, newpage,
Keith Busch37109692019-07-18 15:58:46 -07003767 page, extra_count);
Weichao Guo5b7a4872016-09-20 05:03:27 +08003768 if (rc != MIGRATEPAGE_SUCCESS) {
3769 if (atomic_written)
3770 mutex_unlock(&fi->inmem_lock);
3771 return rc;
3772 }
3773
3774 if (atomic_written) {
3775 struct inmem_pages *cur;
3776 list_for_each_entry(cur, &fi->inmem_pages, list)
3777 if (cur->page == page) {
3778 cur->page = newpage;
3779 break;
3780 }
3781 mutex_unlock(&fi->inmem_lock);
3782 put_page(page);
3783 get_page(newpage);
3784 }
3785
Chao Yu240a5912019-03-06 17:30:59 +08003786 if (PagePrivate(page)) {
3787 f2fs_set_page_private(newpage, page_private(page));
3788 f2fs_clear_page_private(page);
3789 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08003790
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07003791 if (mode != MIGRATE_SYNC_NO_COPY)
3792 migrate_page_copy(newpage, page);
3793 else
3794 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08003795
3796 return MIGRATEPAGE_SUCCESS;
3797}
3798#endif
3799
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003800#ifdef CONFIG_SWAP
3801/* Copied from generic_swapfile_activate() to check any holes */
Chao Yu3e5e4792019-12-27 18:44:56 +08003802static int check_swap_activate(struct swap_info_struct *sis,
3803 struct file *swap_file, sector_t *span)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003804{
3805 struct address_space *mapping = swap_file->f_mapping;
3806 struct inode *inode = mapping->host;
3807 unsigned blocks_per_page;
3808 unsigned long page_no;
3809 unsigned blkbits;
3810 sector_t probe_block;
3811 sector_t last_block;
3812 sector_t lowest_block = -1;
3813 sector_t highest_block = 0;
Chao Yu3e5e4792019-12-27 18:44:56 +08003814 int nr_extents = 0;
3815 int ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003816
3817 blkbits = inode->i_blkbits;
3818 blocks_per_page = PAGE_SIZE >> blkbits;
3819
3820 /*
3821 * Map all the blocks into the extent list. This code doesn't try
3822 * to be very smart.
3823 */
3824 probe_block = 0;
3825 page_no = 0;
3826 last_block = i_size_read(inode) >> blkbits;
Chao Yu3e5e4792019-12-27 18:44:56 +08003827 while ((probe_block + blocks_per_page) <= last_block &&
3828 page_no < sis->max) {
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003829 unsigned block_in_page;
3830 sector_t first_block;
Carlos Maiolino30460e12020-01-09 14:30:41 +01003831 sector_t block = 0;
3832 int err = 0;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003833
3834 cond_resched();
3835
Carlos Maiolino30460e12020-01-09 14:30:41 +01003836 block = probe_block;
3837 err = bmap(inode, &block);
3838 if (err || !block)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003839 goto bad_bmap;
Carlos Maiolino30460e12020-01-09 14:30:41 +01003840 first_block = block;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003841
3842 /*
3843 * It must be PAGE_SIZE aligned on-disk
3844 */
3845 if (first_block & (blocks_per_page - 1)) {
3846 probe_block++;
3847 goto reprobe;
3848 }
3849
3850 for (block_in_page = 1; block_in_page < blocks_per_page;
3851 block_in_page++) {
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003852
Carlos Maiolino30460e12020-01-09 14:30:41 +01003853 block = probe_block + block_in_page;
3854 err = bmap(inode, &block);
3855
3856 if (err || !block)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003857 goto bad_bmap;
Carlos Maiolino30460e12020-01-09 14:30:41 +01003858
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003859 if (block != first_block + block_in_page) {
3860 /* Discontiguity */
3861 probe_block++;
3862 goto reprobe;
3863 }
3864 }
3865
3866 first_block >>= (PAGE_SHIFT - blkbits);
3867 if (page_no) { /* exclude the header page */
3868 if (first_block < lowest_block)
3869 lowest_block = first_block;
3870 if (first_block > highest_block)
3871 highest_block = first_block;
3872 }
3873
Chao Yu3e5e4792019-12-27 18:44:56 +08003874 /*
3875 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3876 */
3877 ret = add_swap_extent(sis, page_no, 1, first_block);
3878 if (ret < 0)
3879 goto out;
3880 nr_extents += ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003881 page_no++;
3882 probe_block += blocks_per_page;
3883reprobe:
3884 continue;
3885 }
Chao Yu3e5e4792019-12-27 18:44:56 +08003886 ret = nr_extents;
3887 *span = 1 + highest_block - lowest_block;
3888 if (page_no == 0)
3889 page_no = 1; /* force Empty message */
3890 sis->max = page_no;
3891 sis->pages = page_no - 1;
3892 sis->highest_bit = page_no - 1;
3893out:
3894 return ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003895bad_bmap:
3896 pr_err("swapon: swapfile has holes\n");
3897 return -EINVAL;
3898}
3899
3900static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3901 sector_t *span)
3902{
3903 struct inode *inode = file_inode(file);
3904 int ret;
3905
3906 if (!S_ISREG(inode->i_mode))
3907 return -EINVAL;
3908
3909 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3910 return -EROFS;
3911
3912 ret = f2fs_convert_inline_inode(inode);
3913 if (ret)
3914 return ret;
3915
Chao Yu4c8ff702019-11-01 18:07:14 +08003916 if (f2fs_disable_compressed_file(inode))
3917 return -EINVAL;
3918
Chao Yu3e5e4792019-12-27 18:44:56 +08003919 ret = check_swap_activate(sis, file, span);
3920 if (ret < 0)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003921 return ret;
3922
3923 set_inode_flag(inode, FI_PIN_FILE);
3924 f2fs_precache_extents(inode);
3925 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu3e5e4792019-12-27 18:44:56 +08003926 return ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003927}
3928
3929static void f2fs_swap_deactivate(struct file *file)
3930{
3931 struct inode *inode = file_inode(file);
3932
3933 clear_inode_flag(inode, FI_PIN_FILE);
3934}
3935#else
3936static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3937 sector_t *span)
3938{
3939 return -EOPNOTSUPP;
3940}
3941
3942static void f2fs_swap_deactivate(struct file *file)
3943{
3944}
3945#endif
3946
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003947const struct address_space_operations f2fs_dblock_aops = {
3948 .readpage = f2fs_read_data_page,
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07003949 .readahead = f2fs_readahead,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003950 .writepage = f2fs_write_data_page,
3951 .writepages = f2fs_write_data_pages,
3952 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003953 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003954 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08003955 .invalidatepage = f2fs_invalidate_page,
3956 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003957 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09003958 .bmap = f2fs_bmap,
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003959 .swap_activate = f2fs_swap_activate,
3960 .swap_deactivate = f2fs_swap_deactivate,
Weichao Guo5b7a4872016-09-20 05:03:27 +08003961#ifdef CONFIG_MIGRATION
3962 .migratepage = f2fs_migrate_page,
3963#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003964};
Eric Biggers6dbb1792018-04-18 11:09:48 -07003965
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05003966void f2fs_clear_page_cache_dirty_tag(struct page *page)
Chao Yuaec2f722018-05-26 18:03:35 +08003967{
3968 struct address_space *mapping = page_mapping(page);
3969 unsigned long flags;
3970
3971 xa_lock_irqsave(&mapping->i_pages, flags);
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05003972 __xa_clear_mark(&mapping->i_pages, page_index(page),
Chao Yuaec2f722018-05-26 18:03:35 +08003973 PAGECACHE_TAG_DIRTY);
3974 xa_unlock_irqrestore(&mapping->i_pages, flags);
3975}
3976
Eric Biggers6dbb1792018-04-18 11:09:48 -07003977int __init f2fs_init_post_read_processing(void)
3978{
Eric Biggers95ae2512019-07-22 09:26:24 -07003979 bio_post_read_ctx_cache =
3980 kmem_cache_create("f2fs_bio_post_read_ctx",
3981 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
Eric Biggers6dbb1792018-04-18 11:09:48 -07003982 if (!bio_post_read_ctx_cache)
3983 goto fail;
3984 bio_post_read_ctx_pool =
3985 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
3986 bio_post_read_ctx_cache);
3987 if (!bio_post_read_ctx_pool)
3988 goto fail_free_cache;
3989 return 0;
3990
3991fail_free_cache:
3992 kmem_cache_destroy(bio_post_read_ctx_cache);
3993fail:
3994 return -ENOMEM;
3995}
3996
Chao Yu0b20fce2019-09-30 18:53:25 +08003997void f2fs_destroy_post_read_processing(void)
Eric Biggers6dbb1792018-04-18 11:09:48 -07003998{
3999 mempool_destroy(bio_post_read_ctx_pool);
4000 kmem_cache_destroy(bio_post_read_ctx_cache);
4001}
Chao Yu0b20fce2019-09-30 18:53:25 +08004002
Chao Yu4c8ff702019-11-01 18:07:14 +08004003int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4004{
4005 if (!f2fs_sb_has_encrypt(sbi) &&
4006 !f2fs_sb_has_verity(sbi) &&
4007 !f2fs_sb_has_compression(sbi))
4008 return 0;
4009
4010 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4011 WQ_UNBOUND | WQ_HIGHPRI,
4012 num_online_cpus());
4013 if (!sbi->post_read_wq)
4014 return -ENOMEM;
4015 return 0;
4016}
4017
4018void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4019{
4020 if (sbi->post_read_wq)
4021 destroy_workqueue(sbi->post_read_wq);
4022}
4023
Chao Yu0b20fce2019-09-30 18:53:25 +08004024int __init f2fs_init_bio_entry_cache(void)
4025{
Chao Yu98510002020-02-17 17:46:20 +08004026 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
Chao Yu0b20fce2019-09-30 18:53:25 +08004027 sizeof(struct bio_entry));
4028 if (!bio_entry_slab)
4029 return -ENOMEM;
4030 return 0;
4031}
4032
Chao Yuf5438052019-12-04 09:52:58 +08004033void f2fs_destroy_bio_entry_cache(void)
Chao Yu0b20fce2019-09-30 18:53:25 +08004034{
4035 kmem_cache_destroy(bio_entry_slab);
4036}