blob: 0a1d236212f852dff6cf7d18ccbc9b2e10808986 [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>
NeilBrown40342472022-01-14 14:07:14 -080011#include <linux/sched/mm.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090012#include <linux/mpage.h>
13#include <linux/writeback.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>
Satya Tangirala27aacd22020-07-02 01:56:06 +000017#include <linux/blk-crypto.h>
Jaegeuk Kim4969c062019-07-01 19:15:29 -070018#include <linux/swap.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010019#include <linux/prefetch.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080020#include <linux/uio.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070021#include <linux/cleancache.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010022#include <linux/sched/signal.h>
Christoph Hellwig10c5db22020-05-23 09:30:11 +020023#include <linux/fiemap.h>
Eric Biggers1517c1a2021-07-23 00:59:20 -070024#include <linux/iomap.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090025
26#include "f2fs.h"
27#include "node.h"
28#include "segment.h"
Daeho Jeong52118742021-08-19 20:52:28 -070029#include "iostat.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090030#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090031
Eric Biggers6dbb1792018-04-18 11:09:48 -070032#define NUM_PREALLOC_POST_READ_CTXS 128
33
34static struct kmem_cache *bio_post_read_ctx_cache;
Chao Yu0b20fce2019-09-30 18:53:25 +080035static struct kmem_cache *bio_entry_slab;
Eric Biggers6dbb1792018-04-18 11:09:48 -070036static mempool_t *bio_post_read_ctx_pool;
Chao Yuf5438052019-12-04 09:52:58 +080037static struct bio_set f2fs_bioset;
38
39#define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
40
41int __init f2fs_init_bioset(void)
42{
43 if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
44 0, BIOSET_NEED_BVECS))
45 return -ENOMEM;
46 return 0;
47}
48
49void f2fs_destroy_bioset(void)
50{
51 bioset_exit(&f2fs_bioset);
52}
53
Chao Yu36951b32016-11-16 10:41:20 +080054static bool __is_cp_guaranteed(struct page *page)
55{
56 struct address_space *mapping = page->mapping;
57 struct inode *inode;
58 struct f2fs_sb_info *sbi;
59
60 if (!mapping)
61 return false;
62
63 inode = mapping->host;
64 sbi = F2FS_I_SB(inode);
65
66 if (inode->i_ino == F2FS_META_INO(sbi) ||
Jack Qiua87aff12020-07-24 16:55:28 +080067 inode->i_ino == F2FS_NODE_INO(sbi) ||
Chao Yub763f3b2021-04-28 17:20:31 +080068 S_ISDIR(inode->i_mode))
69 return true;
70
71 if (f2fs_is_compressed_page(page))
72 return false;
73 if ((S_ISREG(inode->i_mode) &&
Chao Yuaf033b22018-09-20 20:05:00 +080074 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
Chao Yub763f3b2021-04-28 17:20:31 +080075 page_private_gcing(page))
Chao Yu36951b32016-11-16 10:41:20 +080076 return true;
77 return false;
78}
79
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -070080static enum count_type __read_io_type(struct page *page)
81{
Jaegeuk Kim4969c062019-07-01 19:15:29 -070082 struct address_space *mapping = page_file_mapping(page);
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -070083
84 if (mapping) {
85 struct inode *inode = mapping->host;
86 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
87
88 if (inode->i_ino == F2FS_META_INO(sbi))
89 return F2FS_RD_META;
90
91 if (inode->i_ino == F2FS_NODE_INO(sbi))
92 return F2FS_RD_NODE;
93 }
94 return F2FS_RD_DATA;
95}
96
Eric Biggers6dbb1792018-04-18 11:09:48 -070097/* postprocessing steps for read bios */
98enum bio_post_read_step {
Eric Biggers7f59b272021-01-04 22:33:02 -080099#ifdef CONFIG_FS_ENCRYPTION
100 STEP_DECRYPT = 1 << 0,
101#else
102 STEP_DECRYPT = 0, /* compile out the decryption-related code */
103#endif
104#ifdef CONFIG_F2FS_FS_COMPRESSION
105 STEP_DECOMPRESS = 1 << 1,
106#else
107 STEP_DECOMPRESS = 0, /* compile out the decompression-related code */
108#endif
109#ifdef CONFIG_FS_VERITY
110 STEP_VERITY = 1 << 2,
111#else
112 STEP_VERITY = 0, /* compile out the verity-related code */
113#endif
Eric Biggers6dbb1792018-04-18 11:09:48 -0700114};
115
116struct bio_post_read_ctx {
117 struct bio *bio;
Chao Yu4c8ff702019-11-01 18:07:14 +0800118 struct f2fs_sb_info *sbi;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700119 struct work_struct work;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700120 unsigned int enabled_steps;
Daeho Jeong4931e0c2021-07-28 12:38:11 -0700121 block_t fs_blkaddr;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700122};
123
Eric Biggers7f59b272021-01-04 22:33:02 -0800124static void f2fs_finish_read_bio(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900125{
Eric Biggers6dbb1792018-04-18 11:09:48 -0700126 struct bio_vec *bv;
Ming Lei6dc4f102019-02-15 19:13:19 +0800127 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900128
Eric Biggers7f59b272021-01-04 22:33:02 -0800129 /*
130 * Update and unlock the bio's pagecache pages, and put the
131 * decompression context for any compressed pages.
132 */
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200133 bio_for_each_segment_all(bv, bio, iter_all) {
Eric Biggers7f59b272021-01-04 22:33:02 -0800134 struct page *page = bv->bv_page;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700135
Eric Biggers7f59b272021-01-04 22:33:02 -0800136 if (f2fs_is_compressed_page(page)) {
137 if (bio->bi_status)
Chao Yu6ce19af2021-05-20 19:51:50 +0800138 f2fs_end_read_compressed_page(page, true, 0);
Eric Biggers7f59b272021-01-04 22:33:02 -0800139 f2fs_put_page_dic(page);
Chao Yu4c8ff702019-11-01 18:07:14 +0800140 continue;
141 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800142
Eric Biggers7f59b272021-01-04 22:33:02 -0800143 /* PG_error was set if decryption or verity failed. */
Eric Biggers6dbb1792018-04-18 11:09:48 -0700144 if (bio->bi_status || PageError(page)) {
145 ClearPageUptodate(page);
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -0700146 /* will re-read again later */
147 ClearPageError(page);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700148 } else {
149 SetPageUptodate(page);
150 }
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700151 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
Eric Biggers6dbb1792018-04-18 11:09:48 -0700152 unlock_page(page);
153 }
Eric Biggers7f59b272021-01-04 22:33:02 -0800154
155 if (bio->bi_private)
156 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
157 bio_put(bio);
Chao Yu4c8ff702019-11-01 18:07:14 +0800158}
159
Eric Biggers7f59b272021-01-04 22:33:02 -0800160static void f2fs_verify_bio(struct work_struct *work)
Eric Biggers95ae2512019-07-22 09:26:24 -0700161{
162 struct bio_post_read_ctx *ctx =
163 container_of(work, struct bio_post_read_ctx, work);
Eric Biggers644c8c92019-12-31 12:14:16 -0600164 struct bio *bio = ctx->bio;
Eric Biggers7f59b272021-01-04 22:33:02 -0800165 bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
Eric Biggers644c8c92019-12-31 12:14:16 -0600166
167 /*
168 * fsverity_verify_bio() may call readpages() again, and while verity
Eric Biggers7f59b272021-01-04 22:33:02 -0800169 * will be disabled for this, decryption and/or decompression may still
170 * be needed, resulting in another bio_post_read_ctx being allocated.
171 * So to prevent deadlocks we need to release the current ctx to the
172 * mempool first. This assumes that verity is the last post-read step.
Eric Biggers644c8c92019-12-31 12:14:16 -0600173 */
174 mempool_free(ctx, bio_post_read_ctx_pool);
175 bio->bi_private = NULL;
Eric Biggers95ae2512019-07-22 09:26:24 -0700176
Eric Biggers7f59b272021-01-04 22:33:02 -0800177 /*
178 * Verify the bio's pages with fs-verity. Exclude compressed pages,
179 * as those were handled separately by f2fs_end_read_compressed_page().
180 */
181 if (may_have_compressed_pages) {
182 struct bio_vec *bv;
183 struct bvec_iter_all iter_all;
Chao Yu4c8ff702019-11-01 18:07:14 +0800184
Eric Biggers7f59b272021-01-04 22:33:02 -0800185 bio_for_each_segment_all(bv, bio, iter_all) {
186 struct page *page = bv->bv_page;
187
188 if (!f2fs_is_compressed_page(page) &&
189 !PageError(page) && !fsverity_verify_page(page))
190 SetPageError(page);
191 }
192 } else {
193 fsverity_verify_bio(bio);
194 }
195
196 f2fs_finish_read_bio(bio);
197}
198
199/*
200 * If the bio's data needs to be verified with fs-verity, then enqueue the
201 * verity work for the bio. Otherwise finish the bio now.
202 *
203 * Note that to avoid deadlocks, the verity work can't be done on the
204 * decryption/decompression workqueue. This is because verifying the data pages
205 * can involve reading verity metadata pages from the file, and these verity
206 * metadata pages may be encrypted and/or compressed.
207 */
208static void f2fs_verify_and_finish_bio(struct bio *bio)
209{
210 struct bio_post_read_ctx *ctx = bio->bi_private;
211
212 if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
213 INIT_WORK(&ctx->work, f2fs_verify_bio);
214 fsverity_enqueue_verify_work(&ctx->work);
215 } else {
216 f2fs_finish_read_bio(bio);
217 }
218}
219
220/*
221 * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
222 * remaining page was read by @ctx->bio.
223 *
224 * Note that a bio may span clusters (even a mix of compressed and uncompressed
225 * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates
226 * that the bio includes at least one compressed page. The actual decompression
227 * is done on a per-cluster basis, not a per-bio basis.
228 */
229static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx)
230{
231 struct bio_vec *bv;
232 struct bvec_iter_all iter_all;
233 bool all_compressed = true;
Daeho Jeong4931e0c2021-07-28 12:38:11 -0700234 block_t blkaddr = ctx->fs_blkaddr;
Eric Biggers7f59b272021-01-04 22:33:02 -0800235
236 bio_for_each_segment_all(bv, ctx->bio, iter_all) {
237 struct page *page = bv->bv_page;
238
239 /* PG_error was set if decryption failed. */
240 if (f2fs_is_compressed_page(page))
Chao Yu6ce19af2021-05-20 19:51:50 +0800241 f2fs_end_read_compressed_page(page, PageError(page),
242 blkaddr);
Eric Biggers7f59b272021-01-04 22:33:02 -0800243 else
244 all_compressed = false;
Chao Yu6ce19af2021-05-20 19:51:50 +0800245
246 blkaddr++;
Eric Biggers7f59b272021-01-04 22:33:02 -0800247 }
248
249 /*
250 * Optimization: if all the bio's pages are compressed, then scheduling
251 * the per-bio verity work is unnecessary, as verity will be fully
252 * handled at the compression cluster level.
253 */
254 if (all_compressed)
255 ctx->enabled_steps &= ~STEP_VERITY;
Chao Yu4c8ff702019-11-01 18:07:14 +0800256}
257
258static void f2fs_post_read_work(struct work_struct *work)
259{
260 struct bio_post_read_ctx *ctx =
261 container_of(work, struct bio_post_read_ctx, work);
262
Eric Biggers7f59b272021-01-04 22:33:02 -0800263 if (ctx->enabled_steps & STEP_DECRYPT)
264 fscrypt_decrypt_bio(ctx->bio);
Chao Yu4c8ff702019-11-01 18:07:14 +0800265
Eric Biggers7f59b272021-01-04 22:33:02 -0800266 if (ctx->enabled_steps & STEP_DECOMPRESS)
267 f2fs_handle_step_decompress(ctx);
Chao Yu4c8ff702019-11-01 18:07:14 +0800268
Eric Biggers7f59b272021-01-04 22:33:02 -0800269 f2fs_verify_and_finish_bio(ctx->bio);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700270}
271
272static void f2fs_read_end_io(struct bio *bio)
273{
Chao Yuc45d6002019-11-01 17:53:23 +0800274 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
Daeho Jeonga4b68172021-08-20 15:29:09 -0700275 struct bio_post_read_ctx *ctx;
276
277 iostat_update_and_unbind_ctx(bio, 0);
278 ctx = bio->bi_private;
Chao Yuc45d6002019-11-01 17:53:23 +0800279
280 if (time_to_inject(sbi, FAULT_READ_IO)) {
281 f2fs_show_injection_info(sbi, FAULT_READ_IO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200282 bio->bi_status = BLK_STS_IOERR;
Chao Yu55523512017-02-25 11:08:28 +0800283 }
Chao Yu8b038c72016-09-18 23:30:07 +0800284
Eric Biggers7f59b272021-01-04 22:33:02 -0800285 if (bio->bi_status) {
286 f2fs_finish_read_bio(bio);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700287 return;
Chao Yu12377022015-05-25 18:03:38 +0800288 }
289
Eric Biggers7f59b272021-01-04 22:33:02 -0800290 if (ctx && (ctx->enabled_steps & (STEP_DECRYPT | STEP_DECOMPRESS))) {
291 INIT_WORK(&ctx->work, f2fs_post_read_work);
292 queue_work(ctx->sbi->post_read_wq, &ctx->work);
293 } else {
294 f2fs_verify_and_finish_bio(bio);
295 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900296}
297
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200298static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900299{
Daeho Jeonga4b68172021-08-20 15:29:09 -0700300 struct f2fs_sb_info *sbi;
Linus Torvaldsf5688492014-01-30 11:19:05 -0800301 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800302 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900303
Daeho Jeonga4b68172021-08-20 15:29:09 -0700304 iostat_update_and_unbind_ctx(bio, 1);
305 sbi = bio->bi_private;
306
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800307 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800308 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800309 bio->bi_status = BLK_STS_IOERR;
310 }
311
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200312 bio_for_each_segment_all(bvec, bio, iter_all) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900313 struct page *page = bvec->bv_page;
Chao Yu36951b32016-11-16 10:41:20 +0800314 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900315
Chao Yub763f3b2021-04-28 17:20:31 +0800316 if (page_private_dummy(page)) {
317 clear_page_private_dummy(page);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800318 unlock_page(page);
319 mempool_free(page, sbi->write_io_dummy);
320
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200321 if (unlikely(bio->bi_status))
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800322 f2fs_stop_checkpoint(sbi, true);
323 continue;
324 }
325
Eric Biggersd2d07272019-05-20 09:29:39 -0700326 fscrypt_finalize_bounce_page(&page);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700327
Chao Yu4c8ff702019-11-01 18:07:14 +0800328#ifdef CONFIG_F2FS_FS_COMPRESSION
329 if (f2fs_is_compressed_page(page)) {
330 f2fs_compress_write_end_io(bio, page);
331 continue;
332 }
333#endif
334
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200335 if (unlikely(bio->bi_status)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700336 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kimb1ca3212017-12-31 16:26:38 -0800337 if (type == F2FS_WB_CP_DATA)
338 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900339 }
Yunlei He7dff55d2018-01-11 14:19:32 +0800340
341 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
342 page->index != nid_of_node(page));
343
Chao Yu36951b32016-11-16 10:41:20 +0800344 dec_page_count(sbi, type);
Chao Yu50fa53e2018-08-02 23:03:19 +0800345 if (f2fs_in_warm_node_list(sbi, page))
346 f2fs_del_fsync_node_entry(sbi, page);
Chao Yub763f3b2021-04-28 17:20:31 +0800347 clear_page_private_gcing(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900348 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800349 }
Chao Yu36951b32016-11-16 10:41:20 +0800350 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700351 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900352 wake_up(&sbi->cp_wait);
353
354 bio_put(bio);
355}
356
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700357struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
358 block_t blk_addr, struct bio *bio)
359{
360 struct block_device *bdev = sbi->sb->s_bdev;
361 int i;
362
Damien Le Moal09168782019-03-16 09:13:06 +0900363 if (f2fs_is_multi_device(sbi)) {
364 for (i = 0; i < sbi->s_ndevs; i++) {
365 if (FDEV(i).start_blk <= blk_addr &&
366 FDEV(i).end_blk >= blk_addr) {
367 blk_addr -= FDEV(i).start_blk;
368 bdev = FDEV(i).bdev;
369 break;
370 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700371 }
372 }
373 if (bio) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200374 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700375 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
376 }
377 return bdev;
378}
379
380int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
381{
382 int i;
383
Damien Le Moal09168782019-03-16 09:13:06 +0900384 if (!f2fs_is_multi_device(sbi))
385 return 0;
386
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700387 for (i = 0; i < sbi->s_ndevs; i++)
388 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
389 return i;
390 return 0;
391}
392
Chao Yub757f6e2019-08-23 17:58:35 +0800393static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
Gu Zheng940a6d32013-12-20 17:39:59 +0800394{
Chao Yub757f6e2019-08-23 17:58:35 +0800395 struct f2fs_sb_info *sbi = fio->sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800396 struct bio *bio;
397
Christoph Hellwig67883ad2021-01-26 15:52:38 +0100398 bio = bio_alloc_bioset(GFP_NOIO, npages, &f2fs_bioset);
Gu Zheng940a6d32013-12-20 17:39:59 +0800399
Chao Yub757f6e2019-08-23 17:58:35 +0800400 f2fs_target_device(sbi, fio->new_blkaddr, bio);
401 if (is_read_io(fio->op)) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900402 bio->bi_end_io = f2fs_read_end_io;
403 bio->bi_private = NULL;
404 } else {
405 bio->bi_end_io = f2fs_write_end_io;
406 bio->bi_private = sbi;
Chao Yub757f6e2019-08-23 17:58:35 +0800407 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
408 fio->type, fio->temp);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900409 }
Daeho Jeonga4b68172021-08-20 15:29:09 -0700410 iostat_alloc_and_bind_ctx(sbi, bio, NULL);
411
Chao Yub757f6e2019-08-23 17:58:35 +0800412 if (fio->io_wbc)
413 wbc_init_bio(fio->io_wbc, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800414
415 return bio;
416}
417
Satya Tangirala27aacd22020-07-02 01:56:06 +0000418static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
419 pgoff_t first_idx,
420 const struct f2fs_io_info *fio,
421 gfp_t gfp_mask)
422{
423 /*
424 * The f2fs garbage collector sets ->encrypted_page when it wants to
425 * read/write raw data without encryption.
426 */
427 if (!fio || !fio->encrypted_page)
428 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
429}
430
431static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
432 pgoff_t next_idx,
433 const struct f2fs_io_info *fio)
434{
435 /*
436 * The f2fs garbage collector sets ->encrypted_page when it wants to
437 * read/write raw data without encryption.
438 */
439 if (fio && fio->encrypted_page)
440 return !bio_has_crypt_ctx(bio);
441
442 return fscrypt_mergeable_bio(bio, inode, next_idx);
443}
444
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700445static inline void __submit_bio(struct f2fs_sb_info *sbi,
446 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700447{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700448 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800449 unsigned int start;
450
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800451 if (type != DATA && type != NODE)
452 goto submit_io;
453
Chao Yub0332a02020-02-14 17:44:12 +0800454 if (f2fs_lfs_mode(sbi) && current->plug)
Tiezhu Yang3bb09a02018-02-06 08:21:45 +0800455 blk_finish_plug(current->plug);
456
Dehe Gu39f71b72021-02-02 17:39:22 +0800457 if (!F2FS_IO_ALIGNED(sbi))
Chao Yu8223ecc2019-08-28 17:33:38 +0800458 goto submit_io;
459
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800460 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
461 start %= F2FS_IO_SIZE(sbi);
462
463 if (start == 0)
464 goto submit_io;
465
466 /* fill dummy pages */
467 for (; start < F2FS_IO_SIZE(sbi); start++) {
468 struct page *page =
469 mempool_alloc(sbi->write_io_dummy,
Gao Xiangbc73a4b2019-02-19 10:31:52 +0800470 GFP_NOIO | __GFP_NOFAIL);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800471 f2fs_bug_on(sbi, !page);
472
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800473 lock_page(page);
Chao Yub763f3b2021-04-28 17:20:31 +0800474
475 zero_user_segment(page, 0, PAGE_SIZE);
476 set_page_private_dummy(page);
477
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800478 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
479 f2fs_bug_on(sbi, 1);
480 }
481 /*
482 * In the NODE case, we lose next block address chain. So, we
483 * need to do checkpoint in f2fs_sync_file.
484 */
485 if (type == NODE)
486 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700487 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800488submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800489 if (is_read_io(bio_op(bio)))
490 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
491 else
492 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Daeho Jeonga4b68172021-08-20 15:29:09 -0700493
494 iostat_update_submit_ctx(bio, type);
Mike Christie4e49ea42016-06-05 14:31:41 -0500495 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700496}
497
Chao Yu4c8ff702019-11-01 18:07:14 +0800498void f2fs_submit_bio(struct f2fs_sb_info *sbi,
499 struct bio *bio, enum page_type type)
500{
501 __submit_bio(sbi, bio, type);
502}
503
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700504static void __attach_io_flag(struct f2fs_io_info *fio)
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700505{
506 struct f2fs_sb_info *sbi = fio->sbi;
507 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700508 unsigned int io_flag, fua_flag, meta_flag;
509
510 if (fio->type == DATA)
511 io_flag = sbi->data_io_flag;
512 else if (fio->type == NODE)
513 io_flag = sbi->node_io_flag;
514 else
515 return;
516
517 fua_flag = io_flag & temp_mask;
518 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
519
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700520 /*
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700521 * data/node io flag bits per temp:
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700522 * REQ_META | REQ_FUA |
523 * 5 | 4 | 3 | 2 | 1 | 0 |
524 * Cold | Warm | Hot | Cold | Warm | Hot |
525 */
Jaegeuk Kimda9953b2020-04-02 09:32:35 -0700526 if ((1 << fio->temp) & meta_flag)
527 fio->op_flags |= REQ_META;
528 if ((1 << fio->temp) & fua_flag)
529 fio->op_flags |= REQ_FUA;
530}
531
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900532static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900533{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900534 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900535
536 if (!io->bio)
537 return;
538
Jaegeuk Kim32b6aba2020-06-04 11:49:43 -0700539 __attach_io_flag(fio);
Mike Christie04d328d2016-06-05 14:31:55 -0500540 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
541
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800542 if (is_read_io(fio->op))
543 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
544 else
545 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
546
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700547 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900548 io->bio = NULL;
549}
550
Chao Yu8648de22019-02-19 16:15:29 +0800551static bool __has_merged_page(struct bio *bio, struct inode *inode,
Chao Yubab475c2018-09-27 23:41:16 +0800552 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800553{
Chao Yu0fd785e2016-01-18 18:24:59 +0800554 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800555 struct bvec_iter_all iter_all;
Chao Yu0fd785e2016-01-18 18:24:59 +0800556
Chao Yu8648de22019-02-19 16:15:29 +0800557 if (!bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800558 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800559
Chao Yubab475c2018-09-27 23:41:16 +0800560 if (!inode && !page && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800561 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800562
Chao Yu8648de22019-02-19 16:15:29 +0800563 bio_for_each_segment_all(bvec, bio, iter_all) {
Chao Yu4c8ff702019-11-01 18:07:14 +0800564 struct page *target = bvec->bv_page;
Chao Yu0fd785e2016-01-18 18:24:59 +0800565
Chao Yu4c8ff702019-11-01 18:07:14 +0800566 if (fscrypt_is_bounce_page(target)) {
Eric Biggersd2d07272019-05-20 09:29:39 -0700567 target = fscrypt_pagecache_page(target);
Chao Yu4c8ff702019-11-01 18:07:14 +0800568 if (IS_ERR(target))
569 continue;
570 }
571 if (f2fs_is_compressed_page(target)) {
572 target = f2fs_compress_control_page(target);
573 if (IS_ERR(target))
574 continue;
575 }
Chao Yu0fd785e2016-01-18 18:24:59 +0800576
Chao Yu0c3a5792016-01-18 18:28:11 +0800577 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800578 return true;
Chao Yubab475c2018-09-27 23:41:16 +0800579 if (page && page == target)
580 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800581 if (ino && ino == ino_of_node(target))
582 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800583 }
584
Chao Yu0fd785e2016-01-18 18:24:59 +0800585 return false;
586}
587
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700588static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700589 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900590{
591 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700592 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900593
Chao Yudf0f8dc2014-03-22 14:57:23 +0800594 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900595
596 /* change META to META_FLUSH in the checkpoint procedure */
597 if (type >= META_FLUSH) {
598 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500599 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200600 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600601 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800602 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900603 }
604 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800605 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900606}
607
Jaegeuk Kima912b542017-05-10 11:18:25 -0700608static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800609 struct inode *inode, struct page *page,
610 nid_t ino, enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800611{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700612 enum temp_type temp;
Yunlong Song1e771e82018-11-13 11:57:32 +0800613 bool ret = true;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700614
615 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
Yunlong Song1e771e82018-11-13 11:57:32 +0800616 if (!force) {
617 enum page_type btype = PAGE_TYPE_OF_BIO(type);
618 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700619
Yunlong Song1e771e82018-11-13 11:57:32 +0800620 down_read(&io->io_rwsem);
Chao Yu8648de22019-02-19 16:15:29 +0800621 ret = __has_merged_page(io->bio, inode, page, ino);
Yunlong Song1e771e82018-11-13 11:57:32 +0800622 up_read(&io->io_rwsem);
623 }
624 if (ret)
625 __f2fs_submit_merged_write(sbi, type, temp);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700626
627 /* TODO: use HOT temp only for meta pages now. */
628 if (type >= META)
629 break;
630 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800631}
632
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700633void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800634{
Hariprasad Kelamadcc00f2019-04-06 16:29:36 +0530635 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900636}
637
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700638void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800639 struct inode *inode, struct page *page,
640 nid_t ino, enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900641{
Chao Yubab475c2018-09-27 23:41:16 +0800642 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900643}
644
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700645void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800646{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700647 f2fs_submit_merged_write(sbi, DATA);
648 f2fs_submit_merged_write(sbi, NODE);
649 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800650}
651
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900652/*
653 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300654 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900655 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700656int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900657{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900658 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700659 struct page *page = fio->encrypted_page ?
660 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900661
Chao Yuc9b60782018-08-01 19:13:44 +0800662 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800663 fio->is_por ? META_POR : (__is_meta_io(fio) ?
664 META_GENERIC : DATA_GENERIC_ENHANCE)))
Chao Yu10f966b2019-06-20 11:36:14 +0800665 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +0800666
Chao Yu2ace38e2014-12-24 16:08:14 +0800667 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900668
669 /* Allocate a new bio */
Chao Yub757f6e2019-08-23 17:58:35 +0800670 bio = __bio_alloc(fio, 1);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900671
Satya Tangirala27aacd22020-07-02 01:56:06 +0000672 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
673 fio->page->index, fio, GFP_NOIO);
674
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300675 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900676 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900677 return -EFAULT;
678 }
Chao Yu78efac52018-10-22 23:24:28 +0800679
680 if (fio->io_wbc && !is_read_io(fio->op))
Tejun Heo34e51a52019-06-27 13:39:49 -0700681 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu78efac52018-10-22 23:24:28 +0800682
Jaegeuk Kimb7b911d2020-06-04 16:45:30 -0700683 __attach_io_flag(fio);
Mike Christie04d328d2016-06-05 14:31:55 -0500684 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900685
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700686 inc_page_count(fio->sbi, is_read_io(fio->op) ?
687 __read_io_type(page): WB_DATA_TYPE(fio->page));
Chao Yu4c58ed02018-10-22 09:12:51 +0800688
689 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900690 return 0;
691}
692
Chao Yu8896cbd2019-07-12 16:55:41 +0800693static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
694 block_t last_blkaddr, block_t cur_blkaddr)
695{
Jaegeuk Kim10208562020-12-03 09:52:45 -0800696 if (unlikely(sbi->max_io_bytes &&
697 bio->bi_iter.bi_size >= sbi->max_io_bytes))
698 return false;
Chao Yu8896cbd2019-07-12 16:55:41 +0800699 if (last_blkaddr + 1 != cur_blkaddr)
700 return false;
Christoph Hellwig309dca302021-01-24 11:02:34 +0100701 return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
Chao Yu8896cbd2019-07-12 16:55:41 +0800702}
703
704static bool io_type_is_mergeable(struct f2fs_bio_info *io,
705 struct f2fs_io_info *fio)
706{
707 if (io->fio.op != fio->op)
708 return false;
709 return io->fio.op_flags == fio->op_flags;
710}
711
712static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
713 struct f2fs_bio_info *io,
714 struct f2fs_io_info *fio,
715 block_t last_blkaddr,
716 block_t cur_blkaddr)
717{
Chao Yuc72db712019-07-12 16:55:42 +0800718 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
719 unsigned int filled_blocks =
720 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
721 unsigned int io_size = F2FS_IO_SIZE(sbi);
722 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
723
724 /* IOs in bio is aligned and left space of vectors is not enough */
725 if (!(filled_blocks % io_size) && left_vecs < io_size)
726 return false;
727 }
Chao Yu8896cbd2019-07-12 16:55:41 +0800728 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
729 return false;
730 return io_type_is_mergeable(io, fio);
731}
732
Chao Yu0b20fce2019-09-30 18:53:25 +0800733static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
734 struct page *page, enum temp_type temp)
735{
736 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
737 struct bio_entry *be;
738
Chao Yu32410572021-08-09 08:24:48 +0800739 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL);
Chao Yu0b20fce2019-09-30 18:53:25 +0800740 be->bio = bio;
741 bio_get(bio);
742
743 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
744 f2fs_bug_on(sbi, 1);
745
746 down_write(&io->bio_list_lock);
747 list_add_tail(&be->list, &io->bio_list);
748 up_write(&io->bio_list_lock);
749}
750
751static void del_bio_entry(struct bio_entry *be)
752{
753 list_del(&be->list);
754 kmem_cache_free(bio_entry_slab, be);
755}
756
Satya Tangirala27aacd22020-07-02 01:56:06 +0000757static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
Chao Yu0b20fce2019-09-30 18:53:25 +0800758 struct page *page)
759{
Satya Tangirala27aacd22020-07-02 01:56:06 +0000760 struct f2fs_sb_info *sbi = fio->sbi;
Chao Yu0b20fce2019-09-30 18:53:25 +0800761 enum temp_type temp;
762 bool found = false;
763 int ret = -EAGAIN;
764
765 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
766 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
767 struct list_head *head = &io->bio_list;
768 struct bio_entry *be;
769
770 down_write(&io->bio_list_lock);
771 list_for_each_entry(be, head, list) {
772 if (be->bio != *bio)
773 continue;
774
775 found = true;
776
Satya Tangirala27aacd22020-07-02 01:56:06 +0000777 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
778 *fio->last_block,
779 fio->new_blkaddr));
780 if (f2fs_crypt_mergeable_bio(*bio,
781 fio->page->mapping->host,
782 fio->page->index, fio) &&
783 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
784 PAGE_SIZE) {
Chao Yu0b20fce2019-09-30 18:53:25 +0800785 ret = 0;
786 break;
787 }
788
Satya Tangirala27aacd22020-07-02 01:56:06 +0000789 /* page can't be merged into bio; submit the bio */
Chao Yu0b20fce2019-09-30 18:53:25 +0800790 del_bio_entry(be);
791 __submit_bio(sbi, *bio, DATA);
792 break;
793 }
794 up_write(&io->bio_list_lock);
795 }
796
797 if (ret) {
798 bio_put(*bio);
799 *bio = NULL;
800 }
801
802 return ret;
803}
804
805void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
806 struct bio **bio, struct page *page)
807{
808 enum temp_type temp;
809 bool found = false;
810 struct bio *target = bio ? *bio : NULL;
811
812 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
813 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
814 struct list_head *head = &io->bio_list;
815 struct bio_entry *be;
816
817 if (list_empty(head))
818 continue;
819
820 down_read(&io->bio_list_lock);
821 list_for_each_entry(be, head, list) {
822 if (target)
823 found = (target == be->bio);
824 else
825 found = __has_merged_page(be->bio, NULL,
826 page, 0);
827 if (found)
828 break;
829 }
830 up_read(&io->bio_list_lock);
831
832 if (!found)
833 continue;
834
835 found = false;
836
837 down_write(&io->bio_list_lock);
838 list_for_each_entry(be, head, list) {
839 if (target)
840 found = (target == be->bio);
841 else
842 found = __has_merged_page(be->bio, NULL,
843 page, 0);
844 if (found) {
845 target = be->bio;
846 del_bio_entry(be);
847 break;
848 }
849 }
850 up_write(&io->bio_list_lock);
851 }
852
853 if (found)
854 __submit_bio(sbi, target, DATA);
855 if (bio && *bio) {
856 bio_put(*bio);
857 *bio = NULL;
858 }
859}
860
Chao Yu8648de22019-02-19 16:15:29 +0800861int f2fs_merge_page_bio(struct f2fs_io_info *fio)
862{
863 struct bio *bio = *fio->bio;
864 struct page *page = fio->encrypted_page ?
865 fio->encrypted_page : fio->page;
866
867 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
868 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
Chao Yu10f966b2019-06-20 11:36:14 +0800869 return -EFSCORRUPTED;
Chao Yu8648de22019-02-19 16:15:29 +0800870
871 trace_f2fs_submit_page_bio(page, fio);
Chao Yu8648de22019-02-19 16:15:29 +0800872
Chao Yu8896cbd2019-07-12 16:55:41 +0800873 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
Chao Yu0b20fce2019-09-30 18:53:25 +0800874 fio->new_blkaddr))
875 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
Chao Yu8648de22019-02-19 16:15:29 +0800876alloc_new:
877 if (!bio) {
Christoph Hellwiga8affc02021-03-11 12:01:37 +0100878 bio = __bio_alloc(fio, BIO_MAX_VECS);
Jaegeuk Kimb7b911d2020-06-04 16:45:30 -0700879 __attach_io_flag(fio);
Satya Tangirala27aacd22020-07-02 01:56:06 +0000880 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
881 fio->page->index, fio, GFP_NOIO);
Chao Yu8648de22019-02-19 16:15:29 +0800882 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Chao Yu8648de22019-02-19 16:15:29 +0800883
Chao Yu0b20fce2019-09-30 18:53:25 +0800884 add_bio_entry(fio->sbi, bio, page, fio->temp);
885 } else {
Satya Tangirala27aacd22020-07-02 01:56:06 +0000886 if (add_ipu_page(fio, &bio, page))
Chao Yu0b20fce2019-09-30 18:53:25 +0800887 goto alloc_new;
Chao Yu8648de22019-02-19 16:15:29 +0800888 }
889
890 if (fio->io_wbc)
Linus Torvalds9637d512019-07-15 21:20:52 -0700891 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu8648de22019-02-19 16:15:29 +0800892
893 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
894
895 *fio->last_block = fio->new_blkaddr;
896 *fio->bio = bio;
897
898 return 0;
899}
900
Chao Yufe16efe2018-05-28 23:47:18 +0800901void f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900902{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700903 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900904 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700905 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700906 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900907
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700908 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900909
Chao Yufb830fc2017-05-19 23:37:01 +0800910 down_write(&io->io_rwsem);
911next:
912 if (fio->in_list) {
913 spin_lock(&io->io_lock);
914 if (list_empty(&io->io_list)) {
915 spin_unlock(&io->io_lock);
Chao Yufe16efe2018-05-28 23:47:18 +0800916 goto out;
Chao Yufb830fc2017-05-19 23:37:01 +0800917 }
918 fio = list_first_entry(&io->io_list,
919 struct f2fs_io_info, list);
920 list_del(&fio->list);
921 spin_unlock(&io->io_lock);
922 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900923
Chao Yu93770ab2019-04-15 15:26:32 +0800924 verify_fio_blkaddr(fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900925
Chao Yu4c8ff702019-11-01 18:07:14 +0800926 if (fio->encrypted_page)
927 bio_page = fio->encrypted_page;
928 else if (fio->compressed_page)
929 bio_page = fio->compressed_page;
930 else
931 bio_page = fio->page;
Chao Yu36951b32016-11-16 10:41:20 +0800932
Thomas Meyerebf7c522017-10-07 16:02:21 +0200933 /* set submitted = true as a return value */
934 fio->submitted = true;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800935
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700936 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900937
Satya Tangirala27aacd22020-07-02 01:56:06 +0000938 if (io->bio &&
939 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
940 fio->new_blkaddr) ||
941 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
942 bio_page->index, fio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900943 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900944alloc_new:
945 if (io->bio == NULL) {
Chao Yu8223ecc2019-08-28 17:33:38 +0800946 if (F2FS_IO_ALIGNED(sbi) &&
947 (fio->type == DATA || fio->type == NODE) &&
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800948 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700949 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Chao Yufe16efe2018-05-28 23:47:18 +0800950 fio->retry = true;
951 goto skip;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800952 }
Christoph Hellwiga8affc02021-03-11 12:01:37 +0100953 io->bio = __bio_alloc(fio, BIO_MAX_VECS);
Satya Tangirala27aacd22020-07-02 01:56:06 +0000954 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
955 bio_page->index, fio, GFP_NOIO);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900956 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900957 }
958
Jaegeuk Kima912b542017-05-10 11:18:25 -0700959 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900960 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900961 goto alloc_new;
962 }
963
Yufen Yu578c6472018-01-09 19:33:39 +0800964 if (fio->io_wbc)
Tejun Heo34e51a52019-06-27 13:39:49 -0700965 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
Yufen Yu578c6472018-01-09 19:33:39 +0800966
Chao Yu7a9d7542016-02-22 18:36:38 +0800967 io->last_block_in_bio = fio->new_blkaddr;
Chao Yufb830fc2017-05-19 23:37:01 +0800968
969 trace_f2fs_submit_page_write(fio->page, fio);
Chao Yufe16efe2018-05-28 23:47:18 +0800970skip:
Chao Yufb830fc2017-05-19 23:37:01 +0800971 if (fio->in_list)
972 goto next;
Chao Yufe16efe2018-05-28 23:47:18 +0800973out:
Daniel Rosenberg43549942018-08-20 19:21:43 -0700974 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
Chao Yu00e09c02019-08-23 17:58:36 +0800975 !f2fs_is_checkpoint_ready(sbi))
Jaegeuk Kim5ce80582018-09-06 11:40:12 -0700976 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800977 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900978}
979
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700980static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
Eric Biggers95ae2512019-07-22 09:26:24 -0700981 unsigned nr_pages, unsigned op_flag,
Eric Biggers7f59b272021-01-04 22:33:02 -0800982 pgoff_t first_idx, bool for_write)
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700983{
984 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700985 struct bio *bio;
Daeho Jeonga4b68172021-08-20 15:29:09 -0700986 struct bio_post_read_ctx *ctx = NULL;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700987 unsigned int post_read_steps = 0;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700988
Christoph Hellwig67883ad2021-01-26 15:52:38 +0100989 bio = bio_alloc_bioset(for_write ? GFP_NOIO : GFP_KERNEL,
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +0000990 bio_max_segs(nr_pages), &f2fs_bioset);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700991 if (!bio)
992 return ERR_PTR(-ENOMEM);
Satya Tangirala27aacd22020-07-02 01:56:06 +0000993
994 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
995
Eric Biggers6dbb1792018-04-18 11:09:48 -0700996 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
Satya Tangirala27aacd22020-07-02 01:56:06 +00001000 if (fscrypt_inode_uses_fs_layer_crypto(inode))
Eric Biggers7f59b272021-01-04 22:33:02 -08001001 post_read_steps |= STEP_DECRYPT;
Eric Biggers95ae2512019-07-22 09:26:24 -07001002
Eric Biggers7f59b272021-01-04 22:33:02 -08001003 if (f2fs_need_verity(inode, first_idx))
1004 post_read_steps |= STEP_VERITY;
1005
1006 /*
1007 * STEP_DECOMPRESS is handled specially, since a compressed file might
1008 * contain both compressed and uncompressed clusters. We'll allocate a
1009 * bio_post_read_ctx if the file is compressed, but the caller is
1010 * responsible for enabling STEP_DECOMPRESS if it's actually needed.
1011 */
1012
1013 if (post_read_steps || f2fs_compressed_file(inode)) {
Eric Biggerse8ce5742019-12-31 12:14:56 -06001014 /* Due to the mempool, this never fails. */
Eric Biggers6dbb1792018-04-18 11:09:48 -07001015 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
Eric Biggers6dbb1792018-04-18 11:09:48 -07001016 ctx->bio = bio;
Chao Yu4c8ff702019-11-01 18:07:14 +08001017 ctx->sbi = sbi;
Eric Biggers6dbb1792018-04-18 11:09:48 -07001018 ctx->enabled_steps = post_read_steps;
Daeho Jeong4931e0c2021-07-28 12:38:11 -07001019 ctx->fs_blkaddr = blkaddr;
Eric Biggers6dbb1792018-04-18 11:09:48 -07001020 bio->bi_private = ctx;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001021 }
Daeho Jeonga4b68172021-08-20 15:29:09 -07001022 iostat_alloc_and_bind_ctx(sbi, bio, ctx);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001023
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001024 return bio;
1025}
1026
1027/* This can handle encryption stuffs */
1028static int f2fs_submit_page_read(struct inode *inode, struct page *page,
Jia Yangb7973092020-07-01 10:27:40 +08001029 block_t blkaddr, int op_flags, bool for_write)
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001030{
Chao Yu93770ab2019-04-15 15:26:32 +08001031 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1032 struct bio *bio;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001033
Jia Yangb7973092020-07-01 10:27:40 +08001034 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
Eric Biggers7f59b272021-01-04 22:33:02 -08001035 page->index, for_write);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001036 if (IS_ERR(bio))
1037 return PTR_ERR(bio);
1038
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07001039 /* wait for GCed page writeback via META_MAPPING */
1040 f2fs_wait_on_block_writeback(inode, blkaddr);
1041
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001042 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1043 bio_put(bio);
1044 return -EFAULT;
1045 }
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -07001046 ClearPageError(page);
Chao Yu93770ab2019-04-15 15:26:32 +08001047 inc_page_count(sbi, F2FS_RD_DATA);
Chao Yu8b83ac82020-04-16 18:16:56 +08001048 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
Chao Yu93770ab2019-04-15 15:26:32 +08001049 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001050 return 0;
1051}
1052
Chao Yu46008c62016-05-09 19:56:30 +08001053static void __set_data_blkaddr(struct dnode_of_data *dn)
1054{
1055 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1056 __le32 *addr_array;
Chao Yu7a2af762017-07-19 00:19:06 +08001057 int base = 0;
1058
1059 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1060 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +08001061
1062 /* Get physical address of data block */
1063 addr_array = blkaddr_in_node(rn);
Chao Yu7a2af762017-07-19 00:19:06 +08001064 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +08001065}
1066
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001067/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001068 * Lock ordering for the change of data block address:
1069 * ->data_page
1070 * ->node_page
1071 * update block addresses in the node page
1072 */
Chao Yu4d57b862018-05-30 00:20:41 +08001073void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001074{
Chao Yubae0ee72018-12-25 17:43:42 +08001075 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +08001076 __set_data_blkaddr(dn);
1077 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001078 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001079}
1080
Chao Yuf28b3432016-02-24 17:16:47 +08001081void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1082{
1083 dn->data_blkaddr = blkaddr;
Chao Yu4d57b862018-05-30 00:20:41 +08001084 f2fs_set_data_blkaddr(dn);
Chao Yuf28b3432016-02-24 17:16:47 +08001085 f2fs_update_extent_cache(dn);
1086}
1087
Chao Yu46008c62016-05-09 19:56:30 +08001088/* dn->ofs_in_node will be returned with up-to-date last block pointer */
Chao Yu4d57b862018-05-30 00:20:41 +08001089int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001090{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001091 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +08001092 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001093
Chao Yu46008c62016-05-09 19:56:30 +08001094 if (!count)
1095 return 0;
1096
Jaegeuk Kim91942322016-05-20 10:13:22 -07001097 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001098 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +08001099 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1100 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001101
Chao Yu46008c62016-05-09 19:56:30 +08001102 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1103 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +09001104
Chao Yubae0ee72018-12-25 17:43:42 +08001105 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +08001106
1107 for (; count > 0; dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001108 block_t blkaddr = f2fs_data_blkaddr(dn);
Yi Zhuang5f029c02021-04-06 09:47:35 +08001109
Chao Yu46008c62016-05-09 19:56:30 +08001110 if (blkaddr == NULL_ADDR) {
1111 dn->data_blkaddr = NEW_ADDR;
1112 __set_data_blkaddr(dn);
1113 count--;
1114 }
1115 }
1116
1117 if (set_page_dirty(dn->node_page))
1118 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001119 return 0;
1120}
1121
Chao Yu46008c62016-05-09 19:56:30 +08001122/* Should keep dn->ofs_in_node unchanged */
Chao Yu4d57b862018-05-30 00:20:41 +08001123int f2fs_reserve_new_block(struct dnode_of_data *dn)
Chao Yu46008c62016-05-09 19:56:30 +08001124{
1125 unsigned int ofs_in_node = dn->ofs_in_node;
1126 int ret;
1127
Chao Yu4d57b862018-05-30 00:20:41 +08001128 ret = f2fs_reserve_new_blocks(dn, 1);
Chao Yu46008c62016-05-09 19:56:30 +08001129 dn->ofs_in_node = ofs_in_node;
1130 return ret;
1131}
1132
Huajun Lib6009652013-11-10 23:13:18 +08001133int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1134{
1135 bool need_put = dn->inode_page ? false : true;
1136 int err;
1137
Chao Yu4d57b862018-05-30 00:20:41 +08001138 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
Huajun Lib6009652013-11-10 23:13:18 +08001139 if (err)
1140 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +09001141
Huajun Lib6009652013-11-10 23:13:18 +08001142 if (dn->data_blkaddr == NULL_ADDR)
Chao Yu4d57b862018-05-30 00:20:41 +08001143 err = f2fs_reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001144 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +08001145 f2fs_put_dnode(dn);
1146 return err;
1147}
1148
Fan Li759af1c2015-08-05 15:52:16 +08001149int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001150{
Chao Yu94afd6d2021-08-04 10:23:48 +08001151 struct extent_info ei = {0, };
Fan Li759af1c2015-08-05 15:52:16 +08001152 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +08001153
Fan Li759af1c2015-08-05 15:52:16 +08001154 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1155 dn->data_blkaddr = ei.blk + index - ei.fofs;
1156 return 0;
Chao Yu429511c2015-02-05 17:54:31 +08001157 }
1158
Fan Li759af1c2015-08-05 15:52:16 +08001159 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001160}
1161
Chao Yu4d57b862018-05-30 00:20:41 +08001162struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -05001163 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001164{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001165 struct address_space *mapping = inode->i_mapping;
1166 struct dnode_of_data dn;
1167 struct page *page;
Chao Yu94afd6d2021-08-04 10:23:48 +08001168 struct extent_info ei = {0, };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001169 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001170
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001171 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001172 if (!page)
1173 return ERR_PTR(-ENOMEM);
1174
Chao Yucb3bc9e2015-02-05 18:03:40 +08001175 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1176 dn.data_blkaddr = ei.blk + index - ei.fofs;
Chao Yu93770ab2019-04-15 15:26:32 +08001177 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1178 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001179 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001180 goto put_err;
1181 }
Chao Yucb3bc9e2015-02-05 18:03:40 +08001182 goto got_it;
1183 }
1184
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001185 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001186 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001187 if (err)
1188 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001189 f2fs_put_dnode(&dn);
1190
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001191 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001192 err = -ENOENT;
1193 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001194 }
Chao Yu93770ab2019-04-15 15:26:32 +08001195 if (dn.data_blkaddr != NEW_ADDR &&
1196 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1197 dn.data_blkaddr,
1198 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001199 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001200 goto put_err;
1201 }
Chao Yucb3bc9e2015-02-05 18:03:40 +08001202got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001203 if (PageUptodate(page)) {
1204 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001205 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001206 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001207
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001208 /*
1209 * A new dentry page is allocated but not able to be written, since its
1210 * new inode page couldn't be allocated due to -ENOSPC.
1211 * In such the case, its blkaddr can be remained as NEW_ADDR.
Chao Yu4d57b862018-05-30 00:20:41 +08001212 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1213 * f2fs_init_inode_metadata.
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001214 */
1215 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001216 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001217 if (!PageUptodate(page))
1218 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001219 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001220 return page;
1221 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001222
Jia Yangb7973092020-07-01 10:27:40 +08001223 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1224 op_flags, for_write);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001225 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001226 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001227 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001228
1229put_err:
1230 f2fs_put_page(page, 1);
1231 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001232}
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001233
Chao Yu4d57b862018-05-30 00:20:41 +08001234struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001235{
1236 struct address_space *mapping = inode->i_mapping;
1237 struct page *page;
1238
1239 page = find_get_page(mapping, index);
1240 if (page && PageUptodate(page))
1241 return page;
1242 f2fs_put_page(page, 0);
1243
Chao Yu4d57b862018-05-30 00:20:41 +08001244 page = f2fs_get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001245 if (IS_ERR(page))
1246 return page;
1247
1248 if (PageUptodate(page))
1249 return page;
1250
1251 wait_on_page_locked(page);
1252 if (unlikely(!PageUptodate(page))) {
1253 f2fs_put_page(page, 0);
1254 return ERR_PTR(-EIO);
1255 }
1256 return page;
1257}
1258
1259/*
1260 * If it tries to access a hole, return an error.
1261 * Because, the callers, functions in dir.c and GC, should be able to know
1262 * whether this page exists or not.
1263 */
Chao Yu4d57b862018-05-30 00:20:41 +08001264struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001265 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001266{
1267 struct address_space *mapping = inode->i_mapping;
1268 struct page *page;
1269repeat:
Chao Yu4d57b862018-05-30 00:20:41 +08001270 page = f2fs_get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001271 if (IS_ERR(page))
1272 return page;
1273
1274 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001275 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001276 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001277 f2fs_put_page(page, 1);
1278 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001279 }
Chao Yu1563ac72016-07-03 22:05:12 +08001280 if (unlikely(!PageUptodate(page))) {
1281 f2fs_put_page(page, 1);
1282 return ERR_PTR(-EIO);
1283 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001284 return page;
1285}
1286
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001287/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001288 * Caller ensures that this data page is never allocated.
1289 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +09001290 *
Chao Yu4f4124d2013-12-21 18:02:14 +08001291 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1292 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +08001293 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1294 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001295 */
Chao Yu4d57b862018-05-30 00:20:41 +08001296struct page *f2fs_get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +09001297 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001298{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001299 struct address_space *mapping = inode->i_mapping;
1300 struct page *page;
1301 struct dnode_of_data dn;
1302 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -08001303
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001304 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +08001305 if (!page) {
1306 /*
1307 * before exiting, we should make sure ipage will be released
1308 * if any error occur.
1309 */
1310 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001311 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +08001312 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001313
Jaegeuk Kima8865372013-12-27 17:04:17 +09001314 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +08001315 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001316 if (err) {
1317 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001318 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001319 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001320 if (!ipage)
1321 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001322
1323 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001324 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001325
1326 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001327 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001328 if (!PageUptodate(page))
1329 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001330 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001331 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001332
Jaegeuk Kim76121182016-01-01 22:03:47 -08001333 /* if ipage exists, blkaddr should be NEW_ADDR */
1334 f2fs_bug_on(F2FS_I_SB(inode), ipage);
Chao Yu4d57b862018-05-30 00:20:41 +08001335 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001336 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -08001337 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001338 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001339got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +08001340 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001341 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001342 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001343 return page;
1344}
1345
Hyunchul Leed5097be2017-11-28 09:23:00 +09001346static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001347{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001348 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001349 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001350 struct node_info ni;
Chao Yu6aa58d82018-08-14 22:37:25 +08001351 block_t old_blkaddr;
Chao Yu46008c62016-05-09 19:56:30 +08001352 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +08001353 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001354
Jaegeuk Kim91942322016-05-20 10:13:22 -07001355 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001356 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +08001357
Jaegeuk Kima9419b62021-12-13 14:16:32 -08001358 err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
Chao Yu77357302018-07-17 00:02:17 +08001359 if (err)
1360 return err;
1361
Chao Yua2ced1c2020-02-14 17:44:10 +08001362 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Chao Yuf847c692018-09-27 18:34:52 +08001363 if (dn->data_blkaddr != NULL_ADDR)
Chao Yudf6136e2015-03-23 10:33:37 +08001364 goto alloc;
1365
Chao Yu0abd6752017-07-09 00:13:07 +08001366 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1367 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001368
Chao Yudf6136e2015-03-23 10:33:37 +08001369alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001370 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Chao Yu6aa58d82018-08-14 22:37:25 +08001371 old_blkaddr = dn->data_blkaddr;
1372 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
Chao Yu093749e2020-08-04 21:14:49 +08001373 &sum, seg_type, NULL);
Chao Yu6ce19af2021-05-20 19:51:50 +08001374 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
Chao Yu6aa58d82018-08-14 22:37:25 +08001375 invalidate_mapping_pages(META_MAPPING(sbi),
1376 old_blkaddr, old_blkaddr);
Chao Yu6ce19af2021-05-20 19:51:50 +08001377 f2fs_invalidate_compress_page(sbi, old_blkaddr);
1378 }
Chao Yu86f35dc2019-08-28 17:33:35 +08001379 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001380 return 0;
1381}
1382
Chao Yu0ef81832020-06-18 14:36:22 +08001383void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
Yunlei He59c90812017-03-13 20:22:18 +08001384{
1385 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1386 if (lock)
1387 down_read(&sbi->node_change);
1388 else
1389 up_read(&sbi->node_change);
1390 } else {
1391 if (lock)
1392 f2fs_lock_op(sbi);
1393 else
1394 f2fs_unlock_op(sbi);
1395 }
1396}
1397
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001398/*
Chao Yu7a88ddb2020-02-27 19:30:05 +08001399 * f2fs_map_blocks() tries to find or build mapping relationship which
1400 * maps continuous logical blocks to physical blocks, and return such
1401 * info via f2fs_map_blocks structure.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001402 */
Chao Yud323d002015-10-27 09:53:45 +08001403int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +08001404 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001405{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001406 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001407 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +08001408 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf9d6d052018-11-13 14:33:45 +08001409 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +08001410 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001411 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +08001412 unsigned int ofs_in_node, last_ofs_in_node;
1413 blkcnt_t prealloc;
Chao Yu94afd6d2021-08-04 10:23:48 +08001414 struct extent_info ei = {0, };
Fan Li7df3a432015-12-17 13:20:59 +08001415 block_t blkaddr;
Chao Yuc4020b22018-01-11 14:42:30 +08001416 unsigned int start_pgofs;
Chao Yu71f2c822021-09-01 14:39:20 +08001417 int bidx = 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001418
Chao Yudfd02e42016-08-20 15:12:01 +08001419 if (!maxblocks)
1420 return 0;
1421
Chao Yu71f2c822021-09-01 14:39:20 +08001422 map->m_bdev = inode->i_sb->s_bdev;
1423 map->m_multidev_dio =
1424 f2fs_allow_multi_device_dio(F2FS_I_SB(inode), flag);
1425
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001426 map->m_len = 0;
1427 map->m_flags = 0;
1428
1429 /* it only supports block size == page size */
1430 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +08001431 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001432
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001433 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Chao Yub0332a02020-02-14 17:44:12 +08001434 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
Jia Zhuf4f0b672018-11-20 04:29:35 +08001435 map->m_may_create)
1436 goto next_dnode;
1437
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001438 map->m_pblk = ei.blk + pgofs - ei.fofs;
1439 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1440 map->m_flags = F2FS_MAP_MAPPED;
Chao Yuc4020b22018-01-11 14:42:30 +08001441 if (map->m_next_extent)
1442 *map->m_next_extent = pgofs + map->m_len;
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301443
1444 /* for hardware encryption, but to avoid potential issue in future */
1445 if (flag == F2FS_GET_BLOCK_DIO)
1446 f2fs_wait_on_block_writeback_range(inode,
1447 map->m_pblk, map->m_len);
Chao Yu71f2c822021-09-01 14:39:20 +08001448
1449 if (map->m_multidev_dio) {
1450 block_t blk_addr = map->m_pblk;
1451
1452 bidx = f2fs_target_device_index(sbi, map->m_pblk);
1453
1454 map->m_bdev = FDEV(bidx).bdev;
1455 map->m_pblk -= FDEV(bidx).start_blk;
1456 map->m_len = min(map->m_len,
1457 FDEV(bidx).end_blk + 1 - map->m_pblk);
1458
1459 if (map->m_may_create)
1460 f2fs_update_device_state(sbi, inode->i_ino,
1461 blk_addr, map->m_len);
1462 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001463 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001464 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001465
Chao Yu4fe71e82016-01-26 15:37:38 +08001466next_dnode:
Chao Yuf9d6d052018-11-13 14:33:45 +08001467 if (map->m_may_create)
Chao Yu0ef81832020-06-18 14:36:22 +08001468 f2fs_do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001469
1470 /* When reading holes, we need its node page */
1471 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001472 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001473 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +08001474 if (flag == F2FS_GET_BLOCK_BMAP)
1475 map->m_pblk = 0;
Chao Yuadf9ea82021-08-26 10:03:15 +08001476
Chao Yuda859852016-01-26 15:42:58 +08001477 if (err == -ENOENT) {
Chao Yuadf9ea82021-08-26 10:03:15 +08001478 /*
1479 * There is one exceptional case that read_node_page()
1480 * may return -ENOENT due to filesystem has been
1481 * shutdown or cp_error, so force to convert error
1482 * number to EIO for such case.
1483 */
1484 if (map->m_may_create &&
1485 (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1486 f2fs_cp_error(sbi))) {
1487 err = -EIO;
1488 goto unlock_out;
1489 }
1490
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001491 err = 0;
Chao Yuda859852016-01-26 15:42:58 +08001492 if (map->m_next_pgofs)
1493 *map->m_next_pgofs =
Chao Yu4d57b862018-05-30 00:20:41 +08001494 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuc4020b22018-01-11 14:42:30 +08001495 if (map->m_next_extent)
1496 *map->m_next_extent =
Chao Yu4d57b862018-05-30 00:20:41 +08001497 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +08001498 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001499 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001500 }
Chao Yu973163f2015-09-18 16:51:51 +08001501
Chao Yuc4020b22018-01-11 14:42:30 +08001502 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +08001503 prealloc = 0;
Arnd Bergmann230436b32016-11-02 14:52:15 +01001504 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +08001505 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001506
Chao Yu4fe71e82016-01-26 15:37:38 +08001507next_block:
Chao Yua2ced1c2020-02-14 17:44:10 +08001508 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu973163f2015-09-18 16:51:51 +08001509
Chao Yuc9b60782018-08-01 19:13:44 +08001510 if (__is_valid_data_blkaddr(blkaddr) &&
Chao Yu93770ab2019-04-15 15:26:32 +08001511 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001512 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08001513 goto sync_out;
1514 }
1515
Chao Yu93770ab2019-04-15 15:26:32 +08001516 if (__is_valid_data_blkaddr(blkaddr)) {
Chao Yuf847c692018-09-27 18:34:52 +08001517 /* use out-place-update for driect IO under LFS mode */
Chao Yub0332a02020-02-14 17:44:12 +08001518 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
Chao Yuf9d6d052018-11-13 14:33:45 +08001519 map->m_may_create) {
Chao Yuf847c692018-09-27 18:34:52 +08001520 err = __allocate_data_block(&dn, map->m_seg_type);
Chao Yu05e3600612019-08-28 17:33:36 +08001521 if (err)
1522 goto sync_out;
1523 blkaddr = dn.data_blkaddr;
1524 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yuf847c692018-09-27 18:34:52 +08001525 }
1526 } else {
Fan Li7df3a432015-12-17 13:20:59 +08001527 if (create) {
1528 if (unlikely(f2fs_cp_error(sbi))) {
1529 err = -EIO;
1530 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +08001531 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001532 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +08001533 if (blkaddr == NULL_ADDR) {
1534 prealloc++;
1535 last_ofs_in_node = dn.ofs_in_node;
1536 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001537 } else {
Jaegeuk Kim0a4daae2018-09-19 15:28:40 -07001538 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1539 flag != F2FS_GET_BLOCK_DIO);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001540 err = __allocate_data_block(&dn,
1541 map->m_seg_type);
Jaegeuk Kimd4dd19e2021-11-12 14:31:16 -08001542 if (!err) {
1543 if (flag == F2FS_GET_BLOCK_PRE_DIO)
1544 file_need_truncate(inode);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001545 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kimd4dd19e2021-11-12 14:31:16 -08001546 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001547 }
Fan Li7df3a432015-12-17 13:20:59 +08001548 if (err)
1549 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +08001550 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001551 blkaddr = dn.data_blkaddr;
1552 } else {
Chao Yubbe1da72021-08-06 08:02:50 +08001553 if (f2fs_compressed_file(inode) &&
1554 f2fs_sanity_check_cluster(&dn) &&
1555 (flag != F2FS_GET_BLOCK_FIEMAP ||
1556 IS_ENABLED(CONFIG_F2FS_CHECK_FS))) {
1557 err = -EFSCORRUPTED;
1558 goto sync_out;
1559 }
Chao Yu43473f92016-05-05 19:13:02 +08001560 if (flag == F2FS_GET_BLOCK_BMAP) {
1561 map->m_pblk = 0;
1562 goto sync_out;
1563 }
Chao Yuc4020b22018-01-11 14:42:30 +08001564 if (flag == F2FS_GET_BLOCK_PRECACHE)
1565 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001566 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1567 blkaddr == NULL_ADDR) {
1568 if (map->m_next_pgofs)
1569 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001570 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001571 }
Chao Yuf3d98e72018-01-10 18:18:52 +08001572 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1573 /* for defragment case */
1574 if (map->m_next_pgofs)
1575 *map->m_next_pgofs = pgofs + 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001576 goto sync_out;
Chao Yuf3d98e72018-01-10 18:18:52 +08001577 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001578 }
1579 }
Fan Li7df3a432015-12-17 13:20:59 +08001580
Chao Yu46008c62016-05-09 19:56:30 +08001581 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1582 goto skip;
1583
Chao Yu71f2c822021-09-01 14:39:20 +08001584 if (map->m_multidev_dio)
1585 bidx = f2fs_target_device_index(sbi, blkaddr);
1586
Chao Yu4fe71e82016-01-26 15:37:38 +08001587 if (map->m_len == 0) {
1588 /* preallocated unwritten block should be mapped for fiemap. */
1589 if (blkaddr == NEW_ADDR)
1590 map->m_flags |= F2FS_MAP_UNWRITTEN;
1591 map->m_flags |= F2FS_MAP_MAPPED;
1592
1593 map->m_pblk = blkaddr;
1594 map->m_len = 1;
Chao Yu71f2c822021-09-01 14:39:20 +08001595
1596 if (map->m_multidev_dio)
1597 map->m_bdev = FDEV(bidx).bdev;
Chao Yu4fe71e82016-01-26 15:37:38 +08001598 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001599 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001600 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001601 flag == F2FS_GET_BLOCK_PRE_DIO) {
Chao Yu71f2c822021-09-01 14:39:20 +08001602 if (map->m_multidev_dio && map->m_bdev != FDEV(bidx).bdev)
1603 goto sync_out;
Fan Li7df3a432015-12-17 13:20:59 +08001604 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001605 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001606 } else {
1607 goto sync_out;
1608 }
1609
Chao Yu46008c62016-05-09 19:56:30 +08001610skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001611 dn.ofs_in_node++;
1612 pgofs++;
1613
Chao Yu46008c62016-05-09 19:56:30 +08001614 /* preallocate blocks in batch for one dnode page */
1615 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1616 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001617
Chao Yu46008c62016-05-09 19:56:30 +08001618 dn.ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001619 err = f2fs_reserve_new_blocks(&dn, prealloc);
Chao Yu46008c62016-05-09 19:56:30 +08001620 if (err)
1621 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001622
Chao Yu46008c62016-05-09 19:56:30 +08001623 map->m_len += dn.ofs_in_node - ofs_in_node;
1624 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1625 err = -ENOSPC;
1626 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001627 }
Chao Yu46008c62016-05-09 19:56:30 +08001628 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001629 }
1630
Chao Yu46008c62016-05-09 19:56:30 +08001631 if (pgofs >= end)
1632 goto sync_out;
1633 else if (dn.ofs_in_node < end_offset)
1634 goto next_block;
1635
Chao Yuc4020b22018-01-11 14:42:30 +08001636 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1637 if (map->m_flags & F2FS_MAP_MAPPED) {
1638 unsigned int ofs = start_pgofs - map->m_lblk;
1639
1640 f2fs_update_extent_cache_range(&dn,
1641 start_pgofs, map->m_pblk + ofs,
1642 map->m_len - ofs);
1643 }
1644 }
1645
Chao Yu46008c62016-05-09 19:56:30 +08001646 f2fs_put_dnode(&dn);
1647
Chao Yuf9d6d052018-11-13 14:33:45 +08001648 if (map->m_may_create) {
Chao Yu0ef81832020-06-18 14:36:22 +08001649 f2fs_do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001650 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001651 }
Chao Yu46008c62016-05-09 19:56:30 +08001652 goto next_dnode;
1653
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001654sync_out:
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301655
Chao Yu71f2c822021-09-01 14:39:20 +08001656 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) {
1657 /*
1658 * for hardware encryption, but to avoid potential issue
1659 * in future
1660 */
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301661 f2fs_wait_on_block_writeback_range(inode,
1662 map->m_pblk, map->m_len);
Hyeong-Jun Kime3b49ea2021-11-02 16:10:02 +09001663 invalidate_mapping_pages(META_MAPPING(sbi),
1664 map->m_pblk, map->m_pblk);
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301665
Chao Yu71f2c822021-09-01 14:39:20 +08001666 if (map->m_multidev_dio) {
1667 block_t blk_addr = map->m_pblk;
1668
1669 bidx = f2fs_target_device_index(sbi, map->m_pblk);
1670
1671 map->m_bdev = FDEV(bidx).bdev;
1672 map->m_pblk -= FDEV(bidx).start_blk;
1673
1674 if (map->m_may_create)
1675 f2fs_update_device_state(sbi, inode->i_ino,
1676 blk_addr, map->m_len);
1677
1678 f2fs_bug_on(sbi, blk_addr + map->m_len >
1679 FDEV(bidx).end_blk + 1);
1680 }
1681 }
1682
Chao Yuc4020b22018-01-11 14:42:30 +08001683 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1684 if (map->m_flags & F2FS_MAP_MAPPED) {
1685 unsigned int ofs = start_pgofs - map->m_lblk;
1686
1687 f2fs_update_extent_cache_range(&dn,
1688 start_pgofs, map->m_pblk + ofs,
1689 map->m_len - ofs);
1690 }
1691 if (map->m_next_extent)
1692 *map->m_next_extent = pgofs + 1;
1693 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001694 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001695unlock_out:
Chao Yuf9d6d052018-11-13 14:33:45 +08001696 if (map->m_may_create) {
Chao Yu0ef81832020-06-18 14:36:22 +08001697 f2fs_do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001698 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001699 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001700out:
Chao Yu71f2c822021-09-01 14:39:20 +08001701 trace_f2fs_map_blocks(inode, map, create, flag, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001702 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001703}
1704
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001705bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1706{
1707 struct f2fs_map_blocks map;
1708 block_t last_lblk;
1709 int err;
1710
1711 if (pos + len > i_size_read(inode))
1712 return false;
1713
1714 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1715 map.m_next_pgofs = NULL;
1716 map.m_next_extent = NULL;
1717 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08001718 map.m_may_create = false;
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001719 last_lblk = F2FS_BLK_ALIGN(pos + len);
1720
1721 while (map.m_lblk < last_lblk) {
1722 map.m_len = last_lblk - map.m_lblk;
1723 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1724 if (err || map.m_len == 0)
1725 return false;
1726 map.m_lblk += map.m_len;
1727 }
1728 return true;
1729}
1730
Jaegeuk Kim43b9d4b2020-11-24 15:04:27 -08001731static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1732{
1733 return (bytes >> inode->i_blkbits);
1734}
1735
1736static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1737{
1738 return (blks << inode->i_blkbits);
1739}
1740
Chao Yu442a9db2018-01-11 14:39:57 +08001741static int f2fs_xattr_fiemap(struct inode *inode,
1742 struct fiemap_extent_info *fieinfo)
1743{
1744 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1745 struct page *page;
1746 struct node_info ni;
1747 __u64 phys = 0, len;
1748 __u32 flags;
1749 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1750 int err = 0;
1751
1752 if (f2fs_has_inline_xattr(inode)) {
1753 int offset;
1754
1755 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1756 inode->i_ino, false);
1757 if (!page)
1758 return -ENOMEM;
1759
Jaegeuk Kima9419b62021-12-13 14:16:32 -08001760 err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false);
Chao Yu77357302018-07-17 00:02:17 +08001761 if (err) {
1762 f2fs_put_page(page, 1);
1763 return err;
1764 }
Chao Yu442a9db2018-01-11 14:39:57 +08001765
Jaegeuk Kim6cbfcab2020-11-24 14:55:47 -08001766 phys = blks_to_bytes(inode, ni.blk_addr);
Chao Yu442a9db2018-01-11 14:39:57 +08001767 offset = offsetof(struct f2fs_inode, i_addr) +
1768 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yub323fd22018-01-17 16:31:36 +08001769 get_inline_xattr_addrs(inode));
Chao Yu442a9db2018-01-11 14:39:57 +08001770
1771 phys += offset;
1772 len = inline_xattr_size(inode);
1773
1774 f2fs_put_page(page, 1);
1775
1776 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1777
1778 if (!xnid)
1779 flags |= FIEMAP_EXTENT_LAST;
1780
1781 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
Chao Yudd5a09b2020-06-29 20:13:13 +08001782 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
Chao Yu442a9db2018-01-11 14:39:57 +08001783 if (err || err == 1)
1784 return err;
1785 }
1786
1787 if (xnid) {
1788 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1789 if (!page)
1790 return -ENOMEM;
1791
Jaegeuk Kima9419b62021-12-13 14:16:32 -08001792 err = f2fs_get_node_info(sbi, xnid, &ni, false);
Chao Yu77357302018-07-17 00:02:17 +08001793 if (err) {
1794 f2fs_put_page(page, 1);
1795 return err;
1796 }
Chao Yu442a9db2018-01-11 14:39:57 +08001797
Jaegeuk Kim6cbfcab2020-11-24 14:55:47 -08001798 phys = blks_to_bytes(inode, ni.blk_addr);
Chao Yu442a9db2018-01-11 14:39:57 +08001799 len = inode->i_sb->s_blocksize;
1800
1801 f2fs_put_page(page, 1);
1802
1803 flags = FIEMAP_EXTENT_LAST;
1804 }
1805
Chao Yudd5a09b2020-06-29 20:13:13 +08001806 if (phys) {
Chao Yu442a9db2018-01-11 14:39:57 +08001807 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
Chao Yudd5a09b2020-06-29 20:13:13 +08001808 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1809 }
Chao Yu442a9db2018-01-11 14:39:57 +08001810
1811 return (err < 0 ? err : 0);
1812}
1813
Chao Yubf38fba2020-03-28 17:40:40 +08001814static loff_t max_inode_blocks(struct inode *inode)
1815{
1816 loff_t result = ADDRS_PER_INODE(inode);
1817 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1818
1819 /* two direct node blocks */
1820 result += (leaf_count * 2);
1821
1822 /* two indirect node blocks */
1823 leaf_count *= NIDS_PER_BLOCK;
1824 result += (leaf_count * 2);
1825
1826 /* one double indirect node block */
1827 leaf_count *= NIDS_PER_BLOCK;
1828 result += leaf_count;
1829
1830 return result;
1831}
1832
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001833int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1834 u64 start, u64 len)
1835{
Jaegeuk Kimb876f4c92020-11-24 15:19:10 -08001836 struct f2fs_map_blocks map;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001837 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001838 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001839 u64 logical = 0, phys = 0, size = 0;
1840 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001841 int ret = 0;
Daeho Jeong093f0ba2021-07-25 21:18:19 -07001842 bool compr_cluster = false, compr_appended;
Chao Yubf38fba2020-03-28 17:40:40 +08001843 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
Daeho Jeong093f0ba2021-07-25 21:18:19 -07001844 unsigned int count_in_cluster = 0;
Chengguang Xu0bb20452021-03-09 13:21:18 +08001845 loff_t maxbytes;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001846
Chao Yuc4020b22018-01-11 14:42:30 +08001847 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1848 ret = f2fs_precache_extents(inode);
1849 if (ret)
1850 return ret;
1851 }
1852
Christoph Hellwig45dd0522020-05-23 09:30:14 +02001853 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001854 if (ret)
1855 return ret;
1856
Chao Yuf1b43d42018-01-11 14:37:35 +08001857 inode_lock(inode);
1858
Chengguang Xu0bb20452021-03-09 13:21:18 +08001859 maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
1860 if (start > maxbytes) {
1861 ret = -EFBIG;
1862 goto out;
1863 }
1864
1865 if (len > maxbytes || (maxbytes - len) < start)
1866 len = maxbytes - start;
1867
Chao Yu442a9db2018-01-11 14:39:57 +08001868 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1869 ret = f2fs_xattr_fiemap(inode, fieinfo);
1870 goto out;
1871 }
1872
Chao Yu7975f342019-07-22 18:03:50 +08001873 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001874 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1875 if (ret != -EAGAIN)
Chao Yuf1b43d42018-01-11 14:37:35 +08001876 goto out;
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001877 }
1878
Jaegeuk Kim6cbfcab2020-11-24 14:55:47 -08001879 if (bytes_to_blks(inode, len) == 0)
1880 len = blks_to_bytes(inode, 1);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001881
Jaegeuk Kim6cbfcab2020-11-24 14:55:47 -08001882 start_blk = bytes_to_blks(inode, start);
1883 last_blk = bytes_to_blks(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001884
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001885next:
Jaegeuk Kimb876f4c92020-11-24 15:19:10 -08001886 memset(&map, 0, sizeof(map));
1887 map.m_lblk = start_blk;
1888 map.m_len = bytes_to_blks(inode, len);
1889 map.m_next_pgofs = &next_pgofs;
1890 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001891
Daeho Jeong093f0ba2021-07-25 21:18:19 -07001892 if (compr_cluster) {
1893 map.m_lblk += 1;
1894 map.m_len = cluster_size - count_in_cluster;
1895 }
Chao Yubf38fba2020-03-28 17:40:40 +08001896
Jaegeuk Kimb876f4c92020-11-24 15:19:10 -08001897 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001898 if (ret)
1899 goto out;
1900
1901 /* HOLE */
Daeho Jeong093f0ba2021-07-25 21:18:19 -07001902 if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) {
Chao Yuda859852016-01-26 15:42:58 +08001903 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001904
Jaegeuk Kim6cbfcab2020-11-24 14:55:47 -08001905 if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
Chao Yubf38fba2020-03-28 17:40:40 +08001906 max_inode_blocks(inode)))
Fan Li9a950d52015-12-26 18:07:41 +08001907 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001908
Fan Li9a950d52015-12-26 18:07:41 +08001909 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001910 }
Fan Li9a950d52015-12-26 18:07:41 +08001911
Daeho Jeong093f0ba2021-07-25 21:18:19 -07001912 compr_appended = false;
1913 /* In a case of compressed cluster, append this to the last extent */
1914 if (compr_cluster && ((map.m_flags & F2FS_MAP_UNWRITTEN) ||
1915 !(map.m_flags & F2FS_MAP_FLAGS))) {
1916 compr_appended = true;
1917 goto skip_fill;
1918 }
1919
Chao Yuda5af122016-01-08 20:19:27 +08001920 if (size) {
Chao Yu0953fe82020-12-14 17:20:57 +08001921 flags |= FIEMAP_EXTENT_MERGED;
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301922 if (IS_ENCRYPTED(inode))
Chao Yuda5af122016-01-08 20:19:27 +08001923 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1924
Fan Li9a950d52015-12-26 18:07:41 +08001925 ret = fiemap_fill_next_extent(fieinfo, logical,
1926 phys, size, flags);
Chao Yudd5a09b2020-06-29 20:13:13 +08001927 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
Chao Yubf38fba2020-03-28 17:40:40 +08001928 if (ret)
1929 goto out;
1930 size = 0;
Chao Yuda5af122016-01-08 20:19:27 +08001931 }
Fan Li9a950d52015-12-26 18:07:41 +08001932
Chao Yubf38fba2020-03-28 17:40:40 +08001933 if (start_blk > last_blk)
Fan Li9a950d52015-12-26 18:07:41 +08001934 goto out;
1935
Daeho Jeong093f0ba2021-07-25 21:18:19 -07001936skip_fill:
Jaegeuk Kimb876f4c92020-11-24 15:19:10 -08001937 if (map.m_pblk == COMPRESS_ADDR) {
Chao Yubf38fba2020-03-28 17:40:40 +08001938 compr_cluster = true;
Daeho Jeong093f0ba2021-07-25 21:18:19 -07001939 count_in_cluster = 1;
1940 } else if (compr_appended) {
1941 unsigned int appended_blks = cluster_size -
1942 count_in_cluster + 1;
1943 size += blks_to_bytes(inode, appended_blks);
1944 start_blk += appended_blks;
1945 compr_cluster = false;
1946 } else {
1947 logical = blks_to_bytes(inode, start_blk);
1948 phys = __is_valid_data_blkaddr(map.m_pblk) ?
1949 blks_to_bytes(inode, map.m_pblk) : 0;
1950 size = blks_to_bytes(inode, map.m_len);
1951 flags = 0;
1952
1953 if (compr_cluster) {
1954 flags = FIEMAP_EXTENT_ENCODED;
1955 count_in_cluster += map.m_len;
1956 if (count_in_cluster == cluster_size) {
1957 compr_cluster = false;
1958 size += blks_to_bytes(inode, 1);
1959 }
1960 } else if (map.m_flags & F2FS_MAP_UNWRITTEN) {
1961 flags = FIEMAP_EXTENT_UNWRITTEN;
1962 }
1963
1964 start_blk += bytes_to_blks(inode, size);
Chao Yubf38fba2020-03-28 17:40:40 +08001965 }
1966
Fan Li9a950d52015-12-26 18:07:41 +08001967prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001968 cond_resched();
1969 if (fatal_signal_pending(current))
1970 ret = -EINTR;
1971 else
1972 goto next;
1973out:
1974 if (ret == 1)
1975 ret = 0;
1976
Al Viro59551022016-01-22 15:40:57 -05001977 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001978 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001979}
1980
Eric Biggers95ae2512019-07-22 09:26:24 -07001981static inline loff_t f2fs_readpage_limit(struct inode *inode)
1982{
1983 if (IS_ENABLED(CONFIG_FS_VERITY) &&
1984 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1985 return inode->i_sb->s_maxbytes;
1986
1987 return i_size_read(inode);
1988}
1989
Chao Yu2df0ab02019-03-25 21:07:30 +08001990static int f2fs_read_single_page(struct inode *inode, struct page *page,
1991 unsigned nr_pages,
1992 struct f2fs_map_blocks *map,
1993 struct bio **bio_ret,
1994 sector_t *last_block_in_bio,
1995 bool is_readahead)
1996{
1997 struct bio *bio = *bio_ret;
Jaegeuk Kim43b9d4b2020-11-24 15:04:27 -08001998 const unsigned blocksize = blks_to_bytes(inode, 1);
Chao Yu2df0ab02019-03-25 21:07:30 +08001999 sector_t block_in_file;
2000 sector_t last_block;
2001 sector_t last_block_in_file;
2002 sector_t block_nr;
2003 int ret = 0;
2004
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002005 block_in_file = (sector_t)page_index(page);
Chao Yu2df0ab02019-03-25 21:07:30 +08002006 last_block = block_in_file + nr_pages;
Jaegeuk Kim43b9d4b2020-11-24 15:04:27 -08002007 last_block_in_file = bytes_to_blks(inode,
2008 f2fs_readpage_limit(inode) + blocksize - 1);
Chao Yu2df0ab02019-03-25 21:07:30 +08002009 if (last_block > last_block_in_file)
2010 last_block = last_block_in_file;
2011
2012 /* just zeroing out page which is beyond EOF */
2013 if (block_in_file >= last_block)
2014 goto zero_out;
2015 /*
2016 * Map blocks using the previous result first.
2017 */
2018 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2019 block_in_file > map->m_lblk &&
2020 block_in_file < (map->m_lblk + map->m_len))
2021 goto got_it;
2022
2023 /*
2024 * Then do more f2fs_map_blocks() calls until we are
2025 * done with this page.
2026 */
2027 map->m_lblk = block_in_file;
2028 map->m_len = last_block - block_in_file;
2029
2030 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2031 if (ret)
2032 goto out;
2033got_it:
2034 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2035 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2036 SetPageMappedToDisk(page);
2037
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002038 if (!PageUptodate(page) && (!PageSwapCache(page) &&
2039 !cleancache_get_page(page))) {
Chao Yu2df0ab02019-03-25 21:07:30 +08002040 SetPageUptodate(page);
2041 goto confused;
2042 }
2043
2044 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
Chao Yu93770ab2019-04-15 15:26:32 +08002045 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08002046 ret = -EFSCORRUPTED;
Chao Yu2df0ab02019-03-25 21:07:30 +08002047 goto out;
2048 }
2049 } else {
2050zero_out:
2051 zero_user_segment(page, 0, PAGE_SIZE);
Eric Biggers95ae2512019-07-22 09:26:24 -07002052 if (f2fs_need_verity(inode, page->index) &&
2053 !fsverity_verify_page(page)) {
2054 ret = -EIO;
2055 goto out;
2056 }
Chao Yu2df0ab02019-03-25 21:07:30 +08002057 if (!PageUptodate(page))
2058 SetPageUptodate(page);
2059 unlock_page(page);
2060 goto out;
2061 }
2062
2063 /*
2064 * This page will go to BIO. Do we need to send this
2065 * BIO off first?
2066 */
Satya Tangirala27aacd22020-07-02 01:56:06 +00002067 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2068 *last_block_in_bio, block_nr) ||
2069 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
Chao Yu2df0ab02019-03-25 21:07:30 +08002070submit_and_realloc:
2071 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2072 bio = NULL;
2073 }
2074 if (bio == NULL) {
2075 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
Chao Yu06837282020-02-18 18:21:35 +08002076 is_readahead ? REQ_RAHEAD : 0, page->index,
Eric Biggers7f59b272021-01-04 22:33:02 -08002077 false);
Chao Yu2df0ab02019-03-25 21:07:30 +08002078 if (IS_ERR(bio)) {
2079 ret = PTR_ERR(bio);
2080 bio = NULL;
2081 goto out;
2082 }
2083 }
2084
2085 /*
2086 * If the page is under writeback, we need to wait for
2087 * its completion to see the correct decrypted data.
2088 */
2089 f2fs_wait_on_block_writeback(inode, block_nr);
2090
2091 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2092 goto submit_and_realloc;
2093
2094 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
Chao Yu8b83ac82020-04-16 18:16:56 +08002095 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
Chao Yu2df0ab02019-03-25 21:07:30 +08002096 ClearPageError(page);
2097 *last_block_in_bio = block_nr;
2098 goto out;
2099confused:
2100 if (bio) {
2101 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2102 bio = NULL;
2103 }
2104 unlock_page(page);
2105out:
2106 *bio_ret = bio;
2107 return ret;
2108}
2109
Chao Yu4c8ff702019-11-01 18:07:14 +08002110#ifdef CONFIG_F2FS_FS_COMPRESSION
2111int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2112 unsigned nr_pages, sector_t *last_block_in_bio,
Chao Yu06837282020-02-18 18:21:35 +08002113 bool is_readahead, bool for_write)
Chao Yu4c8ff702019-11-01 18:07:14 +08002114{
2115 struct dnode_of_data dn;
2116 struct inode *inode = cc->inode;
2117 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2118 struct bio *bio = *bio_ret;
2119 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2120 sector_t last_block_in_file;
Jaegeuk Kim43b9d4b2020-11-24 15:04:27 -08002121 const unsigned blocksize = blks_to_bytes(inode, 1);
Chao Yu4c8ff702019-11-01 18:07:14 +08002122 struct decompress_io_ctx *dic = NULL;
Chao Yu94afd6d2021-08-04 10:23:48 +08002123 struct extent_info ei = {0, };
2124 bool from_dnode = true;
Chao Yu4c8ff702019-11-01 18:07:14 +08002125 int i;
2126 int ret = 0;
2127
2128 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2129
Jaegeuk Kim43b9d4b2020-11-24 15:04:27 -08002130 last_block_in_file = bytes_to_blks(inode,
2131 f2fs_readpage_limit(inode) + blocksize - 1);
Chao Yu4c8ff702019-11-01 18:07:14 +08002132
2133 /* get rid of pages beyond EOF */
2134 for (i = 0; i < cc->cluster_size; i++) {
2135 struct page *page = cc->rpages[i];
2136
2137 if (!page)
2138 continue;
2139 if ((sector_t)page->index >= last_block_in_file) {
2140 zero_user_segment(page, 0, PAGE_SIZE);
2141 if (!PageUptodate(page))
2142 SetPageUptodate(page);
2143 } else if (!PageUptodate(page)) {
2144 continue;
2145 }
2146 unlock_page(page);
Jaegeuk Kim9605f752021-08-30 13:30:45 -07002147 if (for_write)
2148 put_page(page);
Chao Yu4c8ff702019-11-01 18:07:14 +08002149 cc->rpages[i] = NULL;
2150 cc->nr_rpages--;
2151 }
2152
2153 /* we are done since all pages are beyond EOF */
2154 if (f2fs_cluster_is_empty(cc))
2155 goto out;
2156
Chao Yu94afd6d2021-08-04 10:23:48 +08002157 if (f2fs_lookup_extent_cache(inode, start_idx, &ei))
2158 from_dnode = false;
2159
2160 if (!from_dnode)
2161 goto skip_reading_dnode;
2162
Chao Yu4c8ff702019-11-01 18:07:14 +08002163 set_new_dnode(&dn, inode, NULL, NULL, 0);
2164 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2165 if (ret)
2166 goto out;
2167
Chao Yua86d27d2020-07-29 21:21:35 +08002168 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
Chao Yu4c8ff702019-11-01 18:07:14 +08002169
Chao Yu94afd6d2021-08-04 10:23:48 +08002170skip_reading_dnode:
Chao Yu4c8ff702019-11-01 18:07:14 +08002171 for (i = 1; i < cc->cluster_size; i++) {
2172 block_t blkaddr;
2173
Chao Yu94afd6d2021-08-04 10:23:48 +08002174 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2175 dn.ofs_in_node + i) :
2176 ei.blk + i - 1;
Chao Yu4c8ff702019-11-01 18:07:14 +08002177
2178 if (!__is_valid_data_blkaddr(blkaddr))
2179 break;
2180
2181 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2182 ret = -EFAULT;
2183 goto out_put_dnode;
2184 }
2185 cc->nr_cpages++;
Chao Yu94afd6d2021-08-04 10:23:48 +08002186
2187 if (!from_dnode && i >= ei.c_len)
2188 break;
Chao Yu4c8ff702019-11-01 18:07:14 +08002189 }
2190
2191 /* nothing to decompress */
2192 if (cc->nr_cpages == 0) {
2193 ret = 0;
2194 goto out_put_dnode;
2195 }
2196
2197 dic = f2fs_alloc_dic(cc);
2198 if (IS_ERR(dic)) {
2199 ret = PTR_ERR(dic);
2200 goto out_put_dnode;
2201 }
2202
Chao Yu6ce19af2021-05-20 19:51:50 +08002203 for (i = 0; i < cc->nr_cpages; i++) {
Chao Yu4c8ff702019-11-01 18:07:14 +08002204 struct page *page = dic->cpages[i];
2205 block_t blkaddr;
Eric Biggers7f59b272021-01-04 22:33:02 -08002206 struct bio_post_read_ctx *ctx;
Chao Yu4c8ff702019-11-01 18:07:14 +08002207
Chao Yu94afd6d2021-08-04 10:23:48 +08002208 blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2209 dn.ofs_in_node + i + 1) :
2210 ei.blk + i;
Chao Yu4c8ff702019-11-01 18:07:14 +08002211
Chao Yu6ce19af2021-05-20 19:51:50 +08002212 f2fs_wait_on_block_writeback(inode, blkaddr);
2213
2214 if (f2fs_load_compressed_page(sbi, page, blkaddr)) {
2215 if (atomic_dec_and_test(&dic->remaining_pages))
2216 f2fs_decompress_cluster(dic);
2217 continue;
2218 }
2219
Satya Tangirala27aacd22020-07-02 01:56:06 +00002220 if (bio && (!page_is_mergeable(sbi, bio,
2221 *last_block_in_bio, blkaddr) ||
2222 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
Chao Yu4c8ff702019-11-01 18:07:14 +08002223submit_and_realloc:
2224 __submit_bio(sbi, bio, DATA);
2225 bio = NULL;
2226 }
2227
2228 if (!bio) {
2229 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2230 is_readahead ? REQ_RAHEAD : 0,
Eric Biggers7f59b272021-01-04 22:33:02 -08002231 page->index, for_write);
Chao Yu4c8ff702019-11-01 18:07:14 +08002232 if (IS_ERR(bio)) {
2233 ret = PTR_ERR(bio);
Eric Biggers7f59b272021-01-04 22:33:02 -08002234 f2fs_decompress_end_io(dic, ret);
Chao Yu4c8ff702019-11-01 18:07:14 +08002235 f2fs_put_dnode(&dn);
Chao Yuf3494342020-04-23 17:57:33 +08002236 *bio_ret = NULL;
Chao Yu4c8ff702019-11-01 18:07:14 +08002237 return ret;
2238 }
2239 }
2240
Chao Yu4c8ff702019-11-01 18:07:14 +08002241 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2242 goto submit_and_realloc;
2243
Daeho Jeonga4b68172021-08-20 15:29:09 -07002244 ctx = get_post_read_ctx(bio);
Eric Biggers7f59b272021-01-04 22:33:02 -08002245 ctx->enabled_steps |= STEP_DECOMPRESS;
2246 refcount_inc(&dic->refcnt);
Chao Yu03382f12020-04-21 19:36:21 +08002247
Chao Yu4c8ff702019-11-01 18:07:14 +08002248 inc_page_count(sbi, F2FS_RD_DATA);
Chao Yu8b83ac82020-04-16 18:16:56 +08002249 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
Chao Yu9c122382020-04-23 18:03:06 +08002250 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
Chao Yu4c8ff702019-11-01 18:07:14 +08002251 ClearPageError(page);
2252 *last_block_in_bio = blkaddr;
2253 }
2254
Chao Yu94afd6d2021-08-04 10:23:48 +08002255 if (from_dnode)
2256 f2fs_put_dnode(&dn);
Chao Yu4c8ff702019-11-01 18:07:14 +08002257
2258 *bio_ret = bio;
2259 return 0;
2260
2261out_put_dnode:
Chao Yu94afd6d2021-08-04 10:23:48 +08002262 if (from_dnode)
2263 f2fs_put_dnode(&dn);
Chao Yu4c8ff702019-11-01 18:07:14 +08002264out:
Eric Biggers7f59b272021-01-04 22:33:02 -08002265 for (i = 0; i < cc->cluster_size; i++) {
2266 if (cc->rpages[i]) {
2267 ClearPageUptodate(cc->rpages[i]);
2268 ClearPageError(cc->rpages[i]);
2269 unlock_page(cc->rpages[i]);
2270 }
2271 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002272 *bio_ret = bio;
2273 return ret;
2274}
2275#endif
2276
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002277/*
2278 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2279 * Major change was from block_size == page_size in f2fs by default.
2280 */
Matthew Wilcox (Oracle)e20a7692020-06-01 21:47:27 -07002281static int f2fs_mpage_readpages(struct inode *inode,
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002282 struct readahead_control *rac, struct page *page)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002283{
2284 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002285 sector_t last_block_in_bio = 0;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002286 struct f2fs_map_blocks map;
Chao Yu4c8ff702019-11-01 18:07:14 +08002287#ifdef CONFIG_F2FS_FS_COMPRESSION
2288 struct compress_ctx cc = {
2289 .inode = inode,
2290 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2291 .cluster_size = F2FS_I(inode)->i_cluster_size,
2292 .cluster_idx = NULL_CLUSTER,
2293 .rpages = NULL,
2294 .cpages = NULL,
2295 .nr_rpages = 0,
2296 .nr_cpages = 0,
2297 };
Fengnan Changa2649312021-08-12 19:36:41 +08002298 pgoff_t nc_cluster_idx = NULL_CLUSTER;
Chao Yu4c8ff702019-11-01 18:07:14 +08002299#endif
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002300 unsigned nr_pages = rac ? readahead_count(rac) : 1;
Chao Yu4c8ff702019-11-01 18:07:14 +08002301 unsigned max_nr_pages = nr_pages;
Chao Yu2df0ab02019-03-25 21:07:30 +08002302 int ret = 0;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002303
2304 map.m_pblk = 0;
2305 map.m_lblk = 0;
2306 map.m_len = 0;
2307 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08002308 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08002309 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09002310 map.m_seg_type = NO_CHECK_TYPE;
Chao Yuf9d6d052018-11-13 14:33:45 +08002311 map.m_may_create = false;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002312
LiFan736c0a72017-11-25 11:46:18 +08002313 for (; nr_pages; nr_pages--) {
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002314 if (rac) {
2315 page = readahead_page(rac);
Kinglong Meea83d50b2017-03-13 16:35:13 +08002316 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002317 }
2318
Chao Yu4c8ff702019-11-01 18:07:14 +08002319#ifdef CONFIG_F2FS_FS_COMPRESSION
2320 if (f2fs_compressed_file(inode)) {
2321 /* there are remained comressed pages, submit them */
2322 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2323 ret = f2fs_read_multi_pages(&cc, &bio,
2324 max_nr_pages,
2325 &last_block_in_bio,
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002326 rac != NULL, false);
Chao Yu8bfbfb02021-05-10 17:30:32 +08002327 f2fs_destroy_compress_ctx(&cc, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08002328 if (ret)
2329 goto set_error_page;
2330 }
Fengnan Changa2649312021-08-12 19:36:41 +08002331 if (cc.cluster_idx == NULL_CLUSTER) {
2332 if (nc_cluster_idx ==
2333 page->index >> cc.log_cluster_size) {
2334 goto read_single_page;
2335 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002336
Fengnan Changa2649312021-08-12 19:36:41 +08002337 ret = f2fs_is_compressed_cluster(inode, page->index);
2338 if (ret < 0)
2339 goto set_error_page;
2340 else if (!ret) {
2341 nc_cluster_idx =
2342 page->index >> cc.log_cluster_size;
2343 goto read_single_page;
2344 }
2345
2346 nc_cluster_idx = NULL_CLUSTER;
2347 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002348 ret = f2fs_init_compress_ctx(&cc);
2349 if (ret)
2350 goto set_error_page;
2351
2352 f2fs_compress_ctx_add_page(&cc, page);
2353
2354 goto next_page;
2355 }
2356read_single_page:
2357#endif
2358
2359 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002360 &bio, &last_block_in_bio, rac);
Chao Yu2df0ab02019-03-25 21:07:30 +08002361 if (ret) {
Chao Yu4c8ff702019-11-01 18:07:14 +08002362#ifdef CONFIG_F2FS_FS_COMPRESSION
2363set_error_page:
2364#endif
Chao Yu2df0ab02019-03-25 21:07:30 +08002365 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002366 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002367 unlock_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002368 }
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002369#ifdef CONFIG_F2FS_FS_COMPRESSION
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002370next_page:
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002371#endif
2372 if (rac)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002373 put_page(page);
Chao Yu4c8ff702019-11-01 18:07:14 +08002374
2375#ifdef CONFIG_F2FS_FS_COMPRESSION
2376 if (f2fs_compressed_file(inode)) {
2377 /* last page */
2378 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2379 ret = f2fs_read_multi_pages(&cc, &bio,
2380 max_nr_pages,
2381 &last_block_in_bio,
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002382 rac != NULL, false);
Chao Yu8bfbfb02021-05-10 17:30:32 +08002383 f2fs_destroy_compress_ctx(&cc, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08002384 }
2385 }
2386#endif
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002387 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002388 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07002389 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002390 return ret;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002391}
2392
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002393static int f2fs_read_data_page(struct file *file, struct page *page)
2394{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002395 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002396 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002397
Chao Yuc20e89cd2014-05-06 16:53:08 +08002398 trace_f2fs_readpage(page, DATA);
2399
Chao Yu4c8ff702019-11-01 18:07:14 +08002400 if (!f2fs_is_compress_backend_ready(inode)) {
2401 unlock_page(page);
2402 return -EOPNOTSUPP;
2403 }
2404
arter97e1c42042014-08-06 23:22:50 +09002405 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002406 if (f2fs_has_inline_data(inode))
2407 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002408 if (ret == -EAGAIN)
Matthew Wilcox (Oracle)e20a7692020-06-01 21:47:27 -07002409 ret = f2fs_mpage_readpages(inode, NULL, page);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002410 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002411}
2412
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002413static void f2fs_readahead(struct readahead_control *rac)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002414{
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002415 struct inode *inode = rac->mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08002416
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002417 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002418
Chao Yu4c8ff702019-11-01 18:07:14 +08002419 if (!f2fs_is_compress_backend_ready(inode))
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002420 return;
Chao Yu4c8ff702019-11-01 18:07:14 +08002421
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002422 /* If the file has inline data, skip readpages */
2423 if (f2fs_has_inline_data(inode))
Matthew Wilcox (Oracle)23323192020-06-01 21:47:23 -07002424 return;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002425
Matthew Wilcox (Oracle)e20a7692020-06-01 21:47:27 -07002426 f2fs_mpage_readpages(inode, rac, NULL);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002427}
2428
Chao Yu4c8ff702019-11-01 18:07:14 +08002429int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002430{
2431 struct inode *inode = fio->page->mapping->host;
Chao Yu4c8ff702019-11-01 18:07:14 +08002432 struct page *mpage, *page;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002433 gfp_t gfp_flags = GFP_NOFS;
2434
Jaegeuk Kim19585932017-09-05 16:54:24 -07002435 if (!f2fs_encrypted_file(inode))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002436 return 0;
2437
Chao Yu4c8ff702019-11-01 18:07:14 +08002438 page = fio->compressed_page ? fio->compressed_page : fio->page;
2439
Eric Biggers6dbb1792018-04-18 11:09:48 -07002440 /* wait for GCed page writeback via META_MAPPING */
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07002441 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002442
Satya Tangirala27aacd22020-07-02 01:56:06 +00002443 if (fscrypt_inode_uses_inline_crypto(inode))
2444 return 0;
2445
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002446retry_encrypt:
Chao Yu4c8ff702019-11-01 18:07:14 +08002447 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2448 PAGE_SIZE, 0, gfp_flags);
Chao Yu6aa58d82018-08-14 22:37:25 +08002449 if (IS_ERR(fio->encrypted_page)) {
2450 /* flush pending IOs and wait for a while in the ENOMEM case */
2451 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2452 f2fs_flush_merged_writes(fio->sbi);
NeilBrown40342472022-01-14 14:07:14 -08002453 memalloc_retry_wait(GFP_NOFS);
Chao Yu6aa58d82018-08-14 22:37:25 +08002454 gfp_flags |= __GFP_NOFAIL;
2455 goto retry_encrypt;
2456 }
2457 return PTR_ERR(fio->encrypted_page);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002458 }
Chao Yu6aa58d82018-08-14 22:37:25 +08002459
2460 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2461 if (mpage) {
2462 if (PageUptodate(mpage))
2463 memcpy(page_address(mpage),
2464 page_address(fio->encrypted_page), PAGE_SIZE);
2465 f2fs_put_page(mpage, 1);
2466 }
2467 return 0;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002468}
2469
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002470static inline bool check_inplace_update_policy(struct inode *inode,
2471 struct f2fs_io_info *fio)
2472{
2473 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2474 unsigned int policy = SM_I(sbi)->ipu_policy;
2475
2476 if (policy & (0x1 << F2FS_IPU_FORCE))
2477 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08002478 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002479 return true;
2480 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2481 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2482 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08002483 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002484 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2485 return true;
2486
2487 /*
2488 * IPU for rewrite async pages
2489 */
2490 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2491 fio && fio->op == REQ_OP_WRITE &&
2492 !(fio->op_flags & REQ_SYNC) &&
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302493 !IS_ENCRYPTED(inode))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002494 return true;
2495
2496 /* this is only set during fdatasync */
2497 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2498 is_inode_flag_set(inode, FI_NEED_IPU))
2499 return true;
2500
Daniel Rosenberg43549942018-08-20 19:21:43 -07002501 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2502 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2503 return true;
2504
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002505 return false;
2506}
2507
Chao Yu4d57b862018-05-30 00:20:41 +08002508bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002509{
Chao Yu859fca62021-05-26 14:29:27 +08002510 /* swap file is migrating in aligned write mode */
2511 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2512 return false;
2513
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002514 if (f2fs_is_pinned_file(inode))
2515 return true;
2516
2517 /* if this is cold file, we should overwrite to avoid fragmentation */
2518 if (file_is_cold(inode))
2519 return true;
2520
2521 return check_inplace_update_policy(inode, fio);
2522}
2523
Chao Yu4d57b862018-05-30 00:20:41 +08002524bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002525{
2526 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2527
Jaegeuk Kim19bdba52021-12-09 10:25:43 -08002528 /* The below cases were checked when setting it. */
2529 if (f2fs_is_pinned_file(inode))
2530 return false;
2531 if (fio && is_sbi_flag_set(sbi, SBI_NEED_FSCK))
2532 return true;
Chao Yub0332a02020-02-14 17:44:12 +08002533 if (f2fs_lfs_mode(sbi))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002534 return true;
2535 if (S_ISDIR(inode->i_mode))
2536 return true;
Chao Yuaf033b22018-09-20 20:05:00 +08002537 if (IS_NOQUOTA(inode))
2538 return true;
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002539 if (f2fs_is_atomic_file(inode))
2540 return true;
Chao Yu859fca62021-05-26 14:29:27 +08002541
2542 /* swap file is migrating in aligned write mode */
2543 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2544 return true;
2545
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002546 if (fio) {
Chao Yub763f3b2021-04-28 17:20:31 +08002547 if (page_private_gcing(fio->page))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002548 return true;
Chao Yub763f3b2021-04-28 17:20:31 +08002549 if (page_private_dummy(fio->page))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002550 return true;
Daniel Rosenberg43549942018-08-20 19:21:43 -07002551 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2552 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2553 return true;
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002554 }
2555 return false;
2556}
2557
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002558static inline bool need_inplace_update(struct f2fs_io_info *fio)
2559{
2560 struct inode *inode = fio->page->mapping->host;
2561
Chao Yu4d57b862018-05-30 00:20:41 +08002562 if (f2fs_should_update_outplace(inode, fio))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002563 return false;
2564
Chao Yu4d57b862018-05-30 00:20:41 +08002565 return f2fs_should_update_inplace(inode, fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002566}
2567
Chao Yu4d57b862018-05-30 00:20:41 +08002568int f2fs_do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002569{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002570 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002571 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002572 struct dnode_of_data dn;
Chao Yu94afd6d2021-08-04 10:23:48 +08002573 struct extent_info ei = {0, };
Chao Yu77357302018-07-17 00:02:17 +08002574 struct node_info ni;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002575 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002576 int err = 0;
2577
2578 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00002579 if (need_inplace_update(fio) &&
2580 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2581 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07002582
Chao Yuc9b60782018-08-01 19:13:44 +08002583 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08002584 DATA_GENERIC_ENHANCE))
Chao Yu10f966b2019-06-20 11:36:14 +08002585 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08002586
2587 ipu_force = true;
2588 fio->need_lock = LOCK_DONE;
2589 goto got_it;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002590 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002591
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002592 /* Deadlock due to between page->lock and f2fs_lock_op */
2593 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2594 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08002595
Chao Yu4d57b862018-05-30 00:20:41 +08002596 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002597 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002598 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002599
Chao Yu28bc1062016-02-06 14:40:34 +08002600 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002601
2602 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08002603 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002604 ClearPageUptodate(page);
Chao Yub763f3b2021-04-28 17:20:31 +08002605 clear_page_private_gcing(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002606 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002607 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00002608got_it:
Chao Yuc9b60782018-08-01 19:13:44 +08002609 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2610 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08002611 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08002612 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08002613 goto out_writepage;
2614 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002615 /*
2616 * If current allocation needs SSR,
2617 * it had better in-place writes for updated data.
2618 */
Chao Yu93770ab2019-04-15 15:26:32 +08002619 if (ipu_force ||
2620 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
Chao Yu7b525dd2018-05-23 22:25:08 +08002621 need_inplace_update(fio))) {
Chao Yu4c8ff702019-11-01 18:07:14 +08002622 err = f2fs_encrypt_one_page(fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002623 if (err)
2624 goto out_writepage;
2625
2626 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07002627 ClearPageError(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002628 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002629 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002630 f2fs_unlock_op(fio->sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08002631 err = f2fs_inplace_write_data(fio);
Chao Yu6492a332019-02-21 20:37:14 +08002632 if (err) {
Satya Tangirala27aacd22020-07-02 01:56:06 +00002633 if (fscrypt_inode_uses_fs_layer_crypto(inode))
Eric Biggersd2d07272019-05-20 09:29:39 -07002634 fscrypt_finalize_bounce_page(&fio->encrypted_page);
Chao Yu6492a332019-02-21 20:37:14 +08002635 if (PageWriteback(page))
2636 end_page_writeback(page);
Chao Yucd23ffa92019-04-15 15:30:53 +08002637 } else {
2638 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu6492a332019-02-21 20:37:14 +08002639 }
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002640 trace_f2fs_do_write_data_page(fio->page, IPU);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002641 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002642 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002643
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002644 if (fio->need_lock == LOCK_RETRY) {
2645 if (!f2fs_trylock_op(fio->sbi)) {
2646 err = -EAGAIN;
2647 goto out_writepage;
2648 }
2649 fio->need_lock = LOCK_REQ;
2650 }
2651
Jaegeuk Kima9419b62021-12-13 14:16:32 -08002652 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false);
Chao Yu77357302018-07-17 00:02:17 +08002653 if (err)
2654 goto out_writepage;
2655
2656 fio->version = ni.version;
2657
Chao Yu4c8ff702019-11-01 18:07:14 +08002658 err = f2fs_encrypt_one_page(fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002659 if (err)
2660 goto out_writepage;
2661
2662 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07002663 ClearPageError(page);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002664
Chao Yu4c8ff702019-11-01 18:07:14 +08002665 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2666 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2667
Hou Pengyang279d6df2017-04-27 00:17:21 +08002668 /* LFS mode write path */
Chao Yu4d57b862018-05-30 00:20:41 +08002669 f2fs_outplace_write_data(&dn, fio);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002670 trace_f2fs_do_write_data_page(page, OPU);
2671 set_inode_flag(inode, FI_APPEND_WRITE);
2672 if (page->index == 0)
2673 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002674out_writepage:
2675 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002676out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002677 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002678 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002679 return err;
2680}
2681
Chao Yu4c8ff702019-11-01 18:07:14 +08002682int f2fs_write_single_data_page(struct page *page, int *submitted,
Chao Yu8648de22019-02-19 16:15:29 +08002683 struct bio **bio,
2684 sector_t *last_block,
Chao Yub0af6d42017-08-02 23:21:48 +08002685 struct writeback_control *wbc,
Chao Yu4c8ff702019-11-01 18:07:14 +08002686 enum iostat_type io_type,
Chao Yu3afae092021-01-11 17:42:53 +08002687 int compr_blocks,
2688 bool allow_balance)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002689{
2690 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002691 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002692 loff_t i_size = i_size_read(inode);
Chao Yu4c8ff702019-11-01 18:07:14 +08002693 const pgoff_t end_index = ((unsigned long long)i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002694 >> PAGE_SHIFT;
Chao Yu1f0d5c92019-11-07 17:29:00 +08002695 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002696 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09002697 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002698 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002699 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002700 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08002701 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002702 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05002703 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06002704 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00002705 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002706 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002707 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002708 .submitted = false,
Chao Yu4c8ff702019-11-01 18:07:14 +08002709 .compr_blocks = compr_blocks,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002710 .need_lock = LOCK_RETRY,
Chao Yub0af6d42017-08-02 23:21:48 +08002711 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08002712 .io_wbc = wbc,
Chao Yu8648de22019-02-19 16:15:29 +08002713 .bio = bio,
2714 .last_block = last_block,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002715 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002716
Chao Yuecda0de2014-05-06 16:48:26 +08002717 trace_f2fs_writepage(page, DATA);
2718
Chao Yudb198ae2018-01-18 17:29:10 +08002719 /* we should bypass data pages to proceed the kworkder jobs */
2720 if (unlikely(f2fs_cp_error(sbi))) {
2721 mapping_set_error(page->mapping, -EIO);
Chao Yu1174abf2018-05-28 16:59:26 +08002722 /*
2723 * don't drop any dirty dentry pages for keeping lastest
2724 * directory structure.
2725 */
2726 if (S_ISDIR(inode->i_mode))
2727 goto redirty_out;
Chao Yudb198ae2018-01-18 17:29:10 +08002728 goto out;
2729 }
2730
Chao Yu0771fcc2017-06-29 23:20:45 +08002731 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2732 goto redirty_out;
2733
Chao Yu4c8ff702019-11-01 18:07:14 +08002734 if (page->index < end_index ||
2735 f2fs_verity_in_progress(inode) ||
2736 compr_blocks)
Jaegeuk Kim39936832012-11-22 16:21:29 +09002737 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002738
2739 /*
2740 * If the offset is out-of-range of file size,
2741 * this page does not have to be written to disk.
2742 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002743 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002744 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09002745 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002746
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002747 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09002748write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002749 if (f2fs_is_drop_cache(inode))
2750 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07002751 /* we should not write 0'th page having journal header */
2752 if (f2fs_is_volatile_file(inode) && (!page->index ||
2753 (!wbc->for_reclaim &&
Chao Yu4d57b862018-05-30 00:20:41 +08002754 f2fs_available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002755 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002756
Jaegeuk Kim435cbab2020-04-09 10:25:21 -07002757 /* Dentry/quota blocks are controlled by checkpoint */
2758 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
Chao Yu79963d92020-06-18 14:36:23 +08002759 /*
2760 * We need to wait for node_write to avoid block allocation during
2761 * checkpoint. This can only happen to quota writes which can cause
2762 * the below discard race condition.
2763 */
2764 if (IS_NOQUOTA(inode))
2765 down_read(&sbi->node_write);
2766
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002767 fio.need_lock = LOCK_DONE;
Chao Yu4d57b862018-05-30 00:20:41 +08002768 err = f2fs_do_write_data_page(&fio);
Chao Yu79963d92020-06-18 14:36:23 +08002769
2770 if (IS_NOQUOTA(inode))
2771 up_read(&sbi->node_write);
2772
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002773 goto done;
2774 }
2775
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002776 if (!wbc->for_reclaim)
2777 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002778 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09002779 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002780 else
2781 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002782
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002783 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08002784 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002785 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08002786 if (!err)
2787 goto out;
2788 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002789
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002790 if (err == -EAGAIN) {
Chao Yu4d57b862018-05-30 00:20:41 +08002791 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002792 if (err == -EAGAIN) {
2793 fio.need_lock = LOCK_REQ;
Chao Yu4d57b862018-05-30 00:20:41 +08002794 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002795 }
2796 }
Chao Yua0d00fa2017-10-09 17:55:19 +08002797
Chao Yueb449792018-01-17 16:31:37 +08002798 if (err) {
2799 file_set_keep_isize(inode);
2800 } else {
Chao Yuc10c9822020-02-27 19:30:03 +08002801 spin_lock(&F2FS_I(inode)->i_size_lock);
Chao Yueb449792018-01-17 16:31:37 +08002802 if (F2FS_I(inode)->last_disk_size < psize)
2803 F2FS_I(inode)->last_disk_size = psize;
Chao Yuc10c9822020-02-27 19:30:03 +08002804 spin_unlock(&F2FS_I(inode)->i_size_lock);
Chao Yueb449792018-01-17 16:31:37 +08002805 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002806
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002807done:
2808 if (err && err != -ENOENT)
2809 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002810
Jaegeuk Kim39936832012-11-22 16:21:29 +09002811out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002812 inode_dec_dirty_pages(inode);
Chao Yu2baf0782018-07-27 18:15:16 +08002813 if (err) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002814 ClearPageUptodate(page);
Chao Yub763f3b2021-04-28 17:20:31 +08002815 clear_page_private_gcing(page);
Chao Yu2baf0782018-07-27 18:15:16 +08002816 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002817
2818 if (wbc->for_reclaim) {
Chao Yubab475c2018-09-27 23:41:16 +08002819 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002820 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu4d57b862018-05-30 00:20:41 +08002821 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002822 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08002823 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002824 unlock_page(page);
Chao Yu186857c2019-04-02 18:52:19 +08002825 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
Chao Yu3afae092021-01-11 17:42:53 +08002826 !F2FS_I(inode)->cp_task && allow_balance)
Jaegeuk Kima7881892017-04-20 13:51:57 -07002827 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08002828
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002829 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002830 f2fs_submit_merged_write(sbi, DATA);
Chao Yu0b20fce2019-09-30 18:53:25 +08002831 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002832 submitted = NULL;
2833 }
2834
2835 if (submitted)
Chao Yu4c8ff702019-11-01 18:07:14 +08002836 *submitted = fio.submitted ? 1 : 0;
Chao Yu0c3a5792016-01-18 18:28:11 +08002837
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002838 return 0;
2839
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002840redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002841 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim5b19d282018-05-03 23:26:02 -07002842 /*
2843 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2844 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2845 * file_write_and_wait_range() will see EIO error, which is critical
2846 * to return value of fsync() followed by atomic_write failure to user.
2847 */
2848 if (!err || wbc->for_reclaim)
Chao Yu0002b612016-11-28 19:13:43 -08002849 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002850 unlock_page(page);
2851 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09002852}
2853
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002854static int f2fs_write_data_page(struct page *page,
2855 struct writeback_control *wbc)
2856{
Chao Yu4c8ff702019-11-01 18:07:14 +08002857#ifdef CONFIG_F2FS_FS_COMPRESSION
2858 struct inode *inode = page->mapping->host;
2859
2860 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2861 goto out;
2862
2863 if (f2fs_compressed_file(inode)) {
2864 if (f2fs_is_compressed_cluster(inode, page->index)) {
2865 redirty_page_for_writepage(wbc, page);
2866 return AOP_WRITEPAGE_ACTIVATE;
2867 }
2868 }
2869out:
2870#endif
2871
2872 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
Chao Yu3afae092021-01-11 17:42:53 +08002873 wbc, FS_DATA_IO, 0, true);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002874}
2875
Chao Yu8f46dca2015-07-14 18:56:10 +08002876/*
2877 * This function was copied from write_cche_pages from mm/page-writeback.c.
2878 * The major change is making write step of cold data page separately from
2879 * warm/hot data page.
2880 */
2881static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08002882 struct writeback_control *wbc,
2883 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08002884{
2885 int ret = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08002886 int done = 0, retry = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08002887 struct pagevec pvec;
Chao Yuc29fd0c2018-06-04 23:20:36 +08002888 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Chao Yu8648de22019-02-19 16:15:29 +08002889 struct bio *bio = NULL;
2890 sector_t last_block;
Chao Yu4c8ff702019-11-01 18:07:14 +08002891#ifdef CONFIG_F2FS_FS_COMPRESSION
2892 struct inode *inode = mapping->host;
2893 struct compress_ctx cc = {
2894 .inode = inode,
2895 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2896 .cluster_size = F2FS_I(inode)->i_cluster_size,
2897 .cluster_idx = NULL_CLUSTER,
2898 .rpages = NULL,
2899 .nr_rpages = 0,
2900 .cpages = NULL,
Fengnan Chang3271d7e2021-11-10 10:37:13 +08002901 .valid_nr_cpages = 0,
Chao Yu4c8ff702019-11-01 18:07:14 +08002902 .rbuf = NULL,
2903 .cbuf = NULL,
2904 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2905 .private = NULL,
2906 };
2907#endif
Chao Yu8f46dca2015-07-14 18:56:10 +08002908 int nr_pages;
Chao Yu8f46dca2015-07-14 18:56:10 +08002909 pgoff_t index;
2910 pgoff_t end; /* Inclusive */
2911 pgoff_t done_index;
Chao Yu8f46dca2015-07-14 18:56:10 +08002912 int range_whole = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05002913 xa_mark_t tag;
Chao Yubab475c2018-09-27 23:41:16 +08002914 int nwritten = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08002915 int submitted = 0;
2916 int i;
Chao Yu8f46dca2015-07-14 18:56:10 +08002917
Mel Gorman86679822017-11-15 17:37:52 -08002918 pagevec_init(&pvec);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07002919
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002920 if (get_dirty_pages(mapping->host) <=
2921 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2922 set_inode_flag(mapping->host, FI_HOT_DATA);
2923 else
2924 clear_inode_flag(mapping->host, FI_HOT_DATA);
2925
Chao Yu8f46dca2015-07-14 18:56:10 +08002926 if (wbc->range_cyclic) {
Jason Yan4df7a75f2020-06-15 16:51:32 +08002927 index = mapping->writeback_index; /* prev offset */
Chao Yu8f46dca2015-07-14 18:56:10 +08002928 end = -1;
2929 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002930 index = wbc->range_start >> PAGE_SHIFT;
2931 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08002932 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2933 range_whole = 1;
Chao Yu8f46dca2015-07-14 18:56:10 +08002934 }
2935 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2936 tag = PAGECACHE_TAG_TOWRITE;
2937 else
2938 tag = PAGECACHE_TAG_DIRTY;
2939retry:
Chao Yu4c8ff702019-11-01 18:07:14 +08002940 retry = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08002941 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2942 tag_pages_for_writeback(mapping, index, end);
2943 done_index = index;
Chao Yu4c8ff702019-11-01 18:07:14 +08002944 while (!done && !retry && (index <= end)) {
Jan Kara69c4f352017-11-15 17:34:48 -08002945 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08002946 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08002947 if (nr_pages == 0)
2948 break;
2949
2950 for (i = 0; i < nr_pages; i++) {
2951 struct page *page = pvec.pages[i];
Chao Yu4c8ff702019-11-01 18:07:14 +08002952 bool need_readd;
2953readd:
2954 need_readd = false;
2955#ifdef CONFIG_F2FS_FS_COMPRESSION
2956 if (f2fs_compressed_file(inode)) {
Fengnan Changb368cc52021-10-22 20:08:00 -07002957 void *fsdata = NULL;
2958 struct page *pagep;
2959 int ret2;
2960
Chao Yu4c8ff702019-11-01 18:07:14 +08002961 ret = f2fs_init_compress_ctx(&cc);
2962 if (ret) {
2963 done = 1;
2964 break;
2965 }
Chao Yu8f46dca2015-07-14 18:56:10 +08002966
Chao Yu4c8ff702019-11-01 18:07:14 +08002967 if (!f2fs_cluster_can_merge_page(&cc,
2968 page->index)) {
2969 ret = f2fs_write_multi_pages(&cc,
2970 &submitted, wbc, io_type);
2971 if (!ret)
2972 need_readd = true;
2973 goto result;
2974 }
2975
2976 if (unlikely(f2fs_cp_error(sbi)))
2977 goto lock_page;
2978
Fengnan Changb368cc52021-10-22 20:08:00 -07002979 if (!f2fs_cluster_is_empty(&cc))
2980 goto lock_page;
Chao Yu4c8ff702019-11-01 18:07:14 +08002981
Fengnan Changb368cc52021-10-22 20:08:00 -07002982 ret2 = f2fs_prepare_compress_overwrite(
Chao Yu4c8ff702019-11-01 18:07:14 +08002983 inode, &pagep,
2984 page->index, &fsdata);
Fengnan Changb368cc52021-10-22 20:08:00 -07002985 if (ret2 < 0) {
2986 ret = ret2;
2987 done = 1;
2988 break;
2989 } else if (ret2 &&
2990 (!f2fs_compress_write_end(inode,
2991 fsdata, page->index, 1) ||
2992 !f2fs_all_cluster_page_loaded(&cc,
2993 &pvec, i, nr_pages))) {
2994 retry = 1;
2995 break;
Chao Yu4c8ff702019-11-01 18:07:14 +08002996 }
2997 }
2998#endif
Chao Yuf8de4332018-05-23 22:25:09 +08002999 /* give a priority to WB_SYNC threads */
Chao Yuc29fd0c2018-06-04 23:20:36 +08003000 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
Chao Yuf8de4332018-05-23 22:25:09 +08003001 wbc->sync_mode == WB_SYNC_NONE) {
3002 done = 1;
3003 break;
3004 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003005#ifdef CONFIG_F2FS_FS_COMPRESSION
3006lock_page:
3007#endif
Chao Yu8f46dca2015-07-14 18:56:10 +08003008 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07003009retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08003010 lock_page(page);
3011
3012 if (unlikely(page->mapping != mapping)) {
3013continue_unlock:
3014 unlock_page(page);
3015 continue;
3016 }
3017
3018 if (!PageDirty(page)) {
3019 /* someone wrote it for us */
3020 goto continue_unlock;
3021 }
3022
Chao Yu8f46dca2015-07-14 18:56:10 +08003023 if (PageWriteback(page)) {
Chao Yu0b20fce2019-09-30 18:53:25 +08003024 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08003025 f2fs_wait_on_page_writeback(page,
Chao Yubae0ee72018-12-25 17:43:42 +08003026 DATA, true, true);
Chao Yu0b20fce2019-09-30 18:53:25 +08003027 else
Chao Yu8f46dca2015-07-14 18:56:10 +08003028 goto continue_unlock;
3029 }
3030
Chao Yu8f46dca2015-07-14 18:56:10 +08003031 if (!clear_page_dirty_for_io(page))
3032 goto continue_unlock;
3033
Chao Yu4c8ff702019-11-01 18:07:14 +08003034#ifdef CONFIG_F2FS_FS_COMPRESSION
3035 if (f2fs_compressed_file(inode)) {
3036 get_page(page);
3037 f2fs_compress_ctx_add_page(&cc, page);
3038 continue;
3039 }
3040#endif
3041 ret = f2fs_write_single_data_page(page, &submitted,
Chao Yu3afae092021-01-11 17:42:53 +08003042 &bio, &last_block, wbc, io_type,
3043 0, true);
Chao Yu4c8ff702019-11-01 18:07:14 +08003044 if (ret == AOP_WRITEPAGE_ACTIVATE)
3045 unlock_page(page);
3046#ifdef CONFIG_F2FS_FS_COMPRESSION
3047result:
3048#endif
3049 nwritten += submitted;
3050 wbc->nr_to_write -= submitted;
3051
Chao Yu8f46dca2015-07-14 18:56:10 +08003052 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08003053 /*
3054 * keep nr_to_write, since vfs uses this to
3055 * get # of written pages.
3056 */
3057 if (ret == AOP_WRITEPAGE_ACTIVATE) {
Chao Yu0002b612016-11-28 19:13:43 -08003058 ret = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08003059 goto next;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07003060 } else if (ret == -EAGAIN) {
3061 ret = 0;
3062 if (wbc->sync_mode == WB_SYNC_ALL) {
3063 cond_resched();
3064 congestion_wait(BLK_RW_ASYNC,
Chao Yu5df7731f2020-02-17 17:45:44 +08003065 DEFAULT_IO_TIMEOUT);
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07003066 goto retry_write;
3067 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003068 goto next;
Chao Yu0002b612016-11-28 19:13:43 -08003069 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07003070 done_index = page->index + 1;
3071 done = 1;
3072 break;
Chao Yu8f46dca2015-07-14 18:56:10 +08003073 }
3074
Chao Yu4c8ff702019-11-01 18:07:14 +08003075 if (wbc->nr_to_write <= 0 &&
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003076 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08003077 done = 1;
3078 break;
3079 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003080next:
3081 if (need_readd)
3082 goto readd;
Chao Yu8f46dca2015-07-14 18:56:10 +08003083 }
3084 pagevec_release(&pvec);
3085 cond_resched();
3086 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003087#ifdef CONFIG_F2FS_FS_COMPRESSION
3088 /* flush remained pages in compress cluster */
3089 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3090 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3091 nwritten += submitted;
3092 wbc->nr_to_write -= submitted;
3093 if (ret) {
3094 done = 1;
3095 retry = 0;
3096 }
3097 }
Jaegeuk Kimadfc6942020-09-23 00:54:50 -07003098 if (f2fs_compressed_file(inode))
Chao Yu8bfbfb02021-05-10 17:30:32 +08003099 f2fs_destroy_compress_ctx(&cc, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08003100#endif
Sahitya Tummalae78790f2020-06-02 18:11:47 +05303101 if (retry) {
Chao Yu8f46dca2015-07-14 18:56:10 +08003102 index = 0;
Sahitya Tummalae78790f2020-06-02 18:11:47 +05303103 end = -1;
Chao Yu8f46dca2015-07-14 18:56:10 +08003104 goto retry;
3105 }
Sahitya Tummalae78790f2020-06-02 18:11:47 +05303106 if (wbc->range_cyclic && !done)
3107 done_index = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08003108 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3109 mapping->writeback_index = done_index;
3110
Chao Yubab475c2018-09-27 23:41:16 +08003111 if (nwritten)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07003112 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
Chao Yubab475c2018-09-27 23:41:16 +08003113 NULL, 0, DATA);
Chao Yu8648de22019-02-19 16:15:29 +08003114 /* submit cached bio of IPU write */
3115 if (bio)
Chao Yu0b20fce2019-09-30 18:53:25 +08003116 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
Chao Yu6ca56ca2016-09-29 18:50:11 +08003117
Chao Yu8f46dca2015-07-14 18:56:10 +08003118 return ret;
3119}
3120
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003121static inline bool __should_serialize_io(struct inode *inode,
3122 struct writeback_control *wbc)
3123{
Chao Yu040d2bb2019-05-20 17:36:59 +08003124 /* to avoid deadlock in path of data flush */
3125 if (F2FS_I(inode)->cp_task)
3126 return false;
Chao Yub13f67ff2020-03-19 19:57:57 +08003127
3128 if (!S_ISREG(inode->i_mode))
3129 return false;
3130 if (IS_NOQUOTA(inode))
3131 return false;
3132
Daeho Jeong602a16d2020-12-01 13:08:02 +09003133 if (f2fs_need_compress_data(inode))
Chao Yub13f67ff2020-03-19 19:57:57 +08003134 return true;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003135 if (wbc->sync_mode != WB_SYNC_ALL)
3136 return true;
3137 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3138 return true;
3139 return false;
3140}
3141
Chao Yufc99fe22018-05-30 00:20:39 +08003142static int __f2fs_write_data_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08003143 struct writeback_control *wbc,
3144 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003145{
3146 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07003147 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003148 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003149 int ret;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003150 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003151
P J Pcfb185a2013-04-03 11:38:00 +09003152 /* deal with chardevs and other special file */
3153 if (!mapping->a_ops->writepage)
3154 return 0;
3155
Chao Yu6a290542015-07-17 18:02:39 +08003156 /* skip writing if there is no dirty page in this inode */
3157 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3158 return 0;
3159
Chao Yu0771fcc2017-06-29 23:20:45 +08003160 /* during POR, we don't need to trigger writepage at all. */
3161 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3162 goto skip_write;
3163
Chao Yuaf033b22018-09-20 20:05:00 +08003164 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3165 wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima1257022015-10-08 10:40:07 -07003166 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Chao Yu4d57b862018-05-30 00:20:41 +08003167 f2fs_available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kima1257022015-10-08 10:40:07 -07003168 goto skip_write;
3169
Chao Yud323d002015-10-27 09:53:45 +08003170 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07003171 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08003172 goto skip_write;
3173
Yunlei Hed31c7c32016-02-04 16:14:00 +08003174 trace_f2fs_writepages(mapping->host, wbc, DATA);
3175
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003176 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3177 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08003178 atomic_inc(&sbi->wb_sync_req[DATA]);
3179 else if (atomic_read(&sbi->wb_sync_req[DATA]))
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003180 goto skip_write;
3181
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003182 if (__should_serialize_io(inode, wbc)) {
3183 mutex_lock(&sbi->writepages);
3184 locked = true;
3185 }
3186
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003187 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08003188 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003189 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003190
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003191 if (locked)
3192 mutex_unlock(&sbi->writepages);
3193
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003194 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08003195 atomic_dec(&sbi->wb_sync_req[DATA]);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07003196 /*
3197 * if some pages were truncated, we cannot guarantee its mapping->host
3198 * to detect pending bios.
3199 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003200
Chao Yu4d57b862018-05-30 00:20:41 +08003201 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003202 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09003203
3204skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003205 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08003206 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09003207 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003208}
3209
Chao Yub0af6d42017-08-02 23:21:48 +08003210static int f2fs_write_data_pages(struct address_space *mapping,
3211 struct writeback_control *wbc)
3212{
3213 struct inode *inode = mapping->host;
3214
3215 return __f2fs_write_data_pages(mapping, wbc,
3216 F2FS_I(inode)->cp_task == current ?
3217 FS_CP_DATA_IO : FS_DATA_IO);
3218}
3219
Eric Biggersa1e09b02021-07-23 00:59:21 -07003220void f2fs_write_failed(struct inode *inode, loff_t to)
Chao Yu3aab8f82014-07-02 13:25:04 +08003221{
Jaegeuk Kim819d9152015-12-28 13:48:11 -08003222 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08003223
Jaegeuk Kim3f188c22019-12-03 18:54:29 -08003224 if (IS_NOQUOTA(inode))
3225 return;
3226
Eric Biggers95ae2512019-07-22 09:26:24 -07003227 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3228 if (to > i_size && !f2fs_verity_in_progress(inode)) {
Chao Yua33c1502018-08-05 23:04:25 +08003229 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Linus Torvalds6abaa832021-09-04 10:48:47 -07003230 filemap_invalidate_lock(inode->i_mapping);
Chao Yua33c1502018-08-05 23:04:25 +08003231
Jaegeuk Kim819d9152015-12-28 13:48:11 -08003232 truncate_pagecache(inode, i_size);
Jaegeuk Kim3f188c22019-12-03 18:54:29 -08003233 f2fs_truncate_blocks(inode, i_size, true);
Chao Yua33c1502018-08-05 23:04:25 +08003234
Linus Torvalds6abaa832021-09-04 10:48:47 -07003235 filemap_invalidate_unlock(inode->i_mapping);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09003236 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu3aab8f82014-07-02 13:25:04 +08003237 }
3238}
3239
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003240static int prepare_write_begin(struct f2fs_sb_info *sbi,
3241 struct page *page, loff_t pos, unsigned len,
3242 block_t *blk_addr, bool *node_changed)
3243{
3244 struct inode *inode = page->mapping->host;
3245 pgoff_t index = page->index;
3246 struct dnode_of_data dn;
3247 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003248 bool locked = false;
Chao Yu94afd6d2021-08-04 10:23:48 +08003249 struct extent_info ei = {0, };
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003250 int err = 0;
Sheng Yong2866fb12018-11-14 19:34:28 +08003251 int flag;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003252
Jaegeuk Kim24b84912016-02-03 13:49:44 -08003253 /*
Eric Biggers3d697a42021-07-16 09:39:13 -05003254 * If a whole page is being written and we already preallocated all the
3255 * blocks, then there is no need to get a block address now.
Jaegeuk Kim24b84912016-02-03 13:49:44 -08003256 */
Eric Biggers3d697a42021-07-16 09:39:13 -05003257 if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08003258 return 0;
3259
Sheng Yong2866fb12018-11-14 19:34:28 +08003260 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3261 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3262 flag = F2FS_GET_BLOCK_DEFAULT;
3263 else
3264 flag = F2FS_GET_BLOCK_PRE_AIO;
3265
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003266 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003267 (pos & PAGE_MASK) >= i_size_read(inode)) {
Chao Yu0ef81832020-06-18 14:36:22 +08003268 f2fs_do_map_lock(sbi, flag, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003269 locked = true;
3270 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003271
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003272restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003273 /* check inline_data */
Chao Yu4d57b862018-05-30 00:20:41 +08003274 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003275 if (IS_ERR(ipage)) {
3276 err = PTR_ERR(ipage);
3277 goto unlock_out;
3278 }
3279
3280 set_new_dnode(&dn, inode, ipage, ipage, 0);
3281
3282 if (f2fs_has_inline_data(inode)) {
Chao Yuf2470372017-07-19 00:19:05 +08003283 if (pos + len <= MAX_INLINE_DATA(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003284 f2fs_do_read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07003285 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08003286 if (inode->i_nlink)
Chao Yub763f3b2021-04-28 17:20:31 +08003287 set_page_private_inline(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003288 } else {
3289 err = f2fs_convert_inline_page(&dn, page);
3290 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003291 goto out;
3292 if (dn.data_blkaddr == NULL_ADDR)
3293 err = f2fs_get_block(&dn, index);
3294 }
3295 } else if (locked) {
3296 err = f2fs_get_block(&dn, index);
3297 } else {
3298 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3299 dn.data_blkaddr = ei.blk + index - ei.fofs;
3300 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003301 /* hole case */
Chao Yu4d57b862018-05-30 00:20:41 +08003302 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07003303 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003304 f2fs_put_dnode(&dn);
Chao Yu0ef81832020-06-18 14:36:22 +08003305 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
Yunlei He59c90812017-03-13 20:22:18 +08003306 true);
Sheng Yong2866fb12018-11-14 19:34:28 +08003307 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003308 locked = true;
3309 goto restart;
3310 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003311 }
3312 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003313
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003314 /* convert_inline_page can make node_changed */
3315 *blk_addr = dn.data_blkaddr;
3316 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003317out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003318 f2fs_put_dnode(&dn);
3319unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003320 if (locked)
Chao Yu0ef81832020-06-18 14:36:22 +08003321 f2fs_do_map_lock(sbi, flag, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003322 return err;
3323}
3324
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003325static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3326 loff_t pos, unsigned len, unsigned flags,
3327 struct page **pagep, void **fsdata)
3328{
3329 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07003330 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07003331 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003332 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yua2e2e762018-01-15 17:16:46 +08003333 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003334 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003335 int err = 0;
3336
Chao Yu62aed042014-05-06 16:46:04 +08003337 trace_f2fs_write_begin(inode, pos, len, flags);
3338
Chao Yu00e09c02019-08-23 17:58:36 +08003339 if (!f2fs_is_checkpoint_ready(sbi)) {
3340 err = -ENOSPC;
Daniel Rosenberg43549942018-08-20 19:21:43 -07003341 goto fail;
Chao Yu00e09c02019-08-23 17:58:36 +08003342 }
Daniel Rosenberg43549942018-08-20 19:21:43 -07003343
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09003344 if ((f2fs_is_atomic_file(inode) &&
3345 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3346 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07003347 err = -ENOMEM;
Chao Yua2e2e762018-01-15 17:16:46 +08003348 drop_atomic = true;
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07003349 goto fail;
3350 }
3351
Jaegeuk Kim5f727392014-11-25 10:59:45 -08003352 /*
3353 * We should check this at this moment to avoid deadlock on inode page
3354 * and #0 page. The locking rule for inline_data conversion should be:
3355 * lock_page(page #0) -> lock_page(inode_page)
3356 */
3357 if (index != 0) {
3358 err = f2fs_convert_inline_inode(inode);
3359 if (err)
3360 goto fail;
3361 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003362
3363#ifdef CONFIG_F2FS_FS_COMPRESSION
3364 if (f2fs_compressed_file(inode)) {
3365 int ret;
3366
3367 *fsdata = NULL;
3368
Fengnan Chang7eab7a62021-06-22 19:50:59 +08003369 if (len == PAGE_SIZE)
3370 goto repeat;
3371
Chao Yu4c8ff702019-11-01 18:07:14 +08003372 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3373 index, fsdata);
3374 if (ret < 0) {
3375 err = ret;
3376 goto fail;
3377 } else if (ret) {
3378 return 0;
3379 }
3380 }
3381#endif
3382
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09003383repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08003384 /*
3385 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3386 * wait_for_stable_page. Will wait that below with our IO control.
3387 */
Chao Yu01eccef2017-10-28 16:52:30 +08003388 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kim86d54792017-02-17 09:55:55 -08003389 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08003390 if (!page) {
3391 err = -ENOMEM;
3392 goto fail;
3393 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09003394
Chao Yu4c8ff702019-11-01 18:07:14 +08003395 /* TODO: cluster can be compressed due to race with .writepage */
3396
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003397 *pagep = page;
3398
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003399 err = prepare_write_begin(sbi, page, pos, len,
3400 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003401 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003402 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003403
Chao Yuaf033b22018-09-20 20:05:00 +08003404 if (need_balance && !IS_NOQUOTA(inode) &&
3405 has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08003406 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08003407 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08003408 lock_page(page);
3409 if (page->mapping != mapping) {
3410 /* The page got truncated from under us */
3411 f2fs_put_page(page, 1);
3412 goto repeat;
3413 }
3414 }
3415
Chao Yubae0ee72018-12-25 17:43:42 +08003416 f2fs_wait_on_page_writeback(page, DATA, false, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07003417
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003418 if (len == PAGE_SIZE || PageUptodate(page))
3419 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003420
Eric Biggers95ae2512019-07-22 09:26:24 -07003421 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3422 !f2fs_verity_in_progress(inode)) {
Yunlei He746e2402016-12-20 11:11:35 +08003423 zero_user_segment(page, len, PAGE_SIZE);
3424 return 0;
3425 }
3426
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003427 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003428 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003429 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003430 } else {
Chao Yu93770ab2019-04-15 15:26:32 +08003431 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3432 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08003433 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08003434 goto fail;
3435 }
Jia Yangb7973092020-07-01 10:27:40 +08003436 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07003437 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08003438 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08003439
Jaegeuk Kim393ff912013-03-08 21:29:23 +09003440 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09003441 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09003442 f2fs_put_page(page, 1);
3443 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003444 }
Chao Yu1563ac72016-07-03 22:05:12 +08003445 if (unlikely(!PageUptodate(page))) {
3446 err = -EIO;
3447 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07003448 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003449 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003450 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003451
Chao Yu3aab8f82014-07-02 13:25:04 +08003452fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07003453 f2fs_put_page(page, 1);
Eric Biggers3e679dc2021-07-16 09:39:11 -05003454 f2fs_write_failed(inode, pos + len);
Chao Yua2e2e762018-01-15 17:16:46 +08003455 if (drop_atomic)
Chao Yu4d57b862018-05-30 00:20:41 +08003456 f2fs_drop_inmem_pages_all(sbi, false);
Chao Yu3aab8f82014-07-02 13:25:04 +08003457 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003458}
3459
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003460static int f2fs_write_end(struct file *file,
3461 struct address_space *mapping,
3462 loff_t pos, unsigned len, unsigned copied,
3463 struct page *page, void *fsdata)
3464{
3465 struct inode *inode = page->mapping->host;
3466
Chao Yudfb2bf32014-05-06 16:47:23 +08003467 trace_f2fs_write_end(inode, pos, len, copied);
3468
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003469 /*
3470 * This should be come from len == PAGE_SIZE, and we expect copied
3471 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3472 * let generic_perform_write() try to copy data again through copied=0.
3473 */
3474 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08003475 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003476 copied = 0;
3477 else
3478 SetPageUptodate(page);
3479 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003480
3481#ifdef CONFIG_F2FS_FS_COMPRESSION
3482 /* overwrite compressed file */
3483 if (f2fs_compressed_file(inode) && fsdata) {
3484 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3485 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu944dd22e2020-07-24 18:21:36 +08003486
3487 if (pos + copied > i_size_read(inode) &&
3488 !f2fs_verity_in_progress(inode))
3489 f2fs_i_size_write(inode, pos + copied);
Chao Yu4c8ff702019-11-01 18:07:14 +08003490 return copied;
3491 }
3492#endif
3493
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003494 if (!copied)
3495 goto unlock_out;
3496
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003497 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003498
Eric Biggers95ae2512019-07-22 09:26:24 -07003499 if (pos + copied > i_size_read(inode) &&
3500 !f2fs_verity_in_progress(inode))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07003501 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003502unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08003503 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08003504 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003505 return copied;
3506}
3507
Chao Yu487261f2015-02-05 17:44:29 +08003508void f2fs_invalidate_page(struct page *page, unsigned int offset,
3509 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003510{
3511 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08003512 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003513
Chao Yu487261f2015-02-05 17:44:29 +08003514 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003515 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003516 return;
3517
Chao Yu487261f2015-02-05 17:44:29 +08003518 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08003519 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08003520 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08003521 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08003522 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08003523 } else {
Chao Yu487261f2015-02-05 17:44:29 +08003524 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +08003525 f2fs_remove_dirty_inode(inode);
Chao Yu933439c2016-10-11 22:57:01 +08003526 }
Chao Yu487261f2015-02-05 17:44:29 +08003527 }
Chao Yudecd36b2015-08-07 18:42:09 +08003528
Chao Yub763f3b2021-04-28 17:20:31 +08003529 clear_page_private_gcing(page);
Chao Yu2baf0782018-07-27 18:15:16 +08003530
Chao Yu2a64e302021-12-16 17:13:56 +08003531 if (test_opt(sbi, COMPRESS_CACHE) &&
3532 inode->i_ino == F2FS_COMPRESS_INO(sbi))
3533 clear_page_private_data(page);
Chao Yu6ce19af2021-05-20 19:51:50 +08003534
Chao Yub763f3b2021-04-28 17:20:31 +08003535 if (page_private_atomic(page))
Chao Yu4d57b862018-05-30 00:20:41 +08003536 return f2fs_drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08003537
Chao Yub763f3b2021-04-28 17:20:31 +08003538 detach_page_private(page);
3539 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003540}
3541
Chao Yu487261f2015-02-05 17:44:29 +08003542int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003543{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08003544 /* If this is dirty page, keep PagePrivate */
3545 if (PageDirty(page))
3546 return 0;
3547
Chao Yudecd36b2015-08-07 18:42:09 +08003548 /* This is atomic written page, keep Private */
Chao Yub763f3b2021-04-28 17:20:31 +08003549 if (page_private_atomic(page))
Chao Yudecd36b2015-08-07 18:42:09 +08003550 return 0;
3551
Chao Yu6ce19af2021-05-20 19:51:50 +08003552 if (test_opt(F2FS_P_SB(page), COMPRESS_CACHE)) {
Chao Yu6ce19af2021-05-20 19:51:50 +08003553 struct inode *inode = page->mapping->host;
3554
Chao Yu2a64e302021-12-16 17:13:56 +08003555 if (inode->i_ino == F2FS_COMPRESS_INO(F2FS_I_SB(inode)))
Chao Yu6ce19af2021-05-20 19:51:50 +08003556 clear_page_private_data(page);
3557 }
3558
Chao Yub763f3b2021-04-28 17:20:31 +08003559 clear_page_private_gcing(page);
3560
3561 detach_page_private(page);
3562 set_page_private(page, 0);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09003563 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003564}
3565
3566static int f2fs_set_data_page_dirty(struct page *page)
3567{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003568 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003569
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09003570 trace_f2fs_set_page_dirty(page, DATA);
3571
Jaegeuk Kim237c0792016-06-30 18:49:15 -07003572 if (!PageUptodate(page))
3573 SetPageUptodate(page);
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003574 if (PageSwapCache(page))
3575 return __set_page_dirty_nobuffers(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003576
Chao Yu5fe45742017-01-07 18:50:26 +08003577 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yub763f3b2021-04-28 17:20:31 +08003578 if (!page_private_atomic(page)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003579 f2fs_register_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08003580 return 1;
3581 }
3582 /*
3583 * Previously, this page has been registered, we just
3584 * return here.
3585 */
3586 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003587 }
3588
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003589 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07003590 __set_page_dirty_nobuffers(page);
Chao Yu4d57b862018-05-30 00:20:41 +08003591 f2fs_update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003592 return 1;
3593 }
3594 return 0;
3595}
3596
Chao Yuc1c63382020-03-30 17:13:29 +08003597
3598static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3599{
3600#ifdef CONFIG_F2FS_FS_COMPRESSION
3601 struct dnode_of_data dn;
3602 sector_t start_idx, blknr = 0;
3603 int ret;
3604
3605 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3606
3607 set_new_dnode(&dn, inode, NULL, NULL, 0);
3608 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3609 if (ret)
3610 return 0;
3611
3612 if (dn.data_blkaddr != COMPRESS_ADDR) {
3613 dn.ofs_in_node += block - start_idx;
3614 blknr = f2fs_data_blkaddr(&dn);
3615 if (!__is_valid_data_blkaddr(blknr))
3616 blknr = 0;
3617 }
3618
3619 f2fs_put_dnode(&dn);
Chao Yuc1c63382020-03-30 17:13:29 +08003620 return blknr;
3621#else
Chao Yu250e84d2020-06-28 20:29:38 +08003622 return 0;
Chao Yuc1c63382020-03-30 17:13:29 +08003623#endif
3624}
3625
3626
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09003627static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3628{
Chao Yu454ae7e2014-04-22 13:34:01 +08003629 struct inode *inode = mapping->host;
Chao Yub79b0a32020-06-29 20:13:12 +08003630 sector_t blknr = 0;
Chao Yu454ae7e2014-04-22 13:34:01 +08003631
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07003632 if (f2fs_has_inline_data(inode))
Chao Yub79b0a32020-06-29 20:13:12 +08003633 goto out;
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07003634
3635 /* make sure allocating whole blocks */
3636 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3637 filemap_write_and_wait(mapping);
3638
Daeho Jeong4eda1682020-08-31 09:24:01 +09003639 /* Block number less than F2FS MAX BLOCKS */
Chengguang Xu6d1451b2021-01-13 13:21:54 +08003640 if (unlikely(block >= max_file_blocks(inode)))
Daeho Jeong4eda1682020-08-31 09:24:01 +09003641 goto out;
Chao Yuc1c63382020-03-30 17:13:29 +08003642
Daeho Jeong4eda1682020-08-31 09:24:01 +09003643 if (f2fs_compressed_file(inode)) {
3644 blknr = f2fs_bmap_compress(inode, block);
3645 } else {
Jaegeuk Kimb876f4c92020-11-24 15:19:10 -08003646 struct f2fs_map_blocks map;
3647
3648 memset(&map, 0, sizeof(map));
3649 map.m_lblk = block;
3650 map.m_len = 1;
3651 map.m_next_pgofs = NULL;
3652 map.m_seg_type = NO_CHECK_TYPE;
3653
3654 if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3655 blknr = map.m_pblk;
Daeho Jeong4eda1682020-08-31 09:24:01 +09003656 }
Chao Yub79b0a32020-06-29 20:13:12 +08003657out:
3658 trace_f2fs_bmap(inode, block, blknr);
3659 return blknr;
Chao Yu429511c2015-02-05 17:54:31 +08003660}
3661
Weichao Guo5b7a4872016-09-20 05:03:27 +08003662#ifdef CONFIG_MIGRATION
3663#include <linux/migrate.h>
3664
3665int f2fs_migrate_page(struct address_space *mapping,
3666 struct page *newpage, struct page *page, enum migrate_mode mode)
3667{
3668 int rc, extra_count;
3669 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
Chao Yub763f3b2021-04-28 17:20:31 +08003670 bool atomic_written = page_private_atomic(page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08003671
3672 BUG_ON(PageWriteback(page));
3673
3674 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07003675 if (atomic_written) {
3676 if (mode != MIGRATE_SYNC)
3677 return -EBUSY;
3678 if (!mutex_trylock(&fi->inmem_lock))
3679 return -EAGAIN;
3680 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08003681
Chao Yu240a5912019-03-06 17:30:59 +08003682 /* one extra reference was held for atomic_write page */
3683 extra_count = atomic_written ? 1 : 0;
Weichao Guo5b7a4872016-09-20 05:03:27 +08003684 rc = migrate_page_move_mapping(mapping, newpage,
Keith Busch37109692019-07-18 15:58:46 -07003685 page, extra_count);
Weichao Guo5b7a4872016-09-20 05:03:27 +08003686 if (rc != MIGRATEPAGE_SUCCESS) {
3687 if (atomic_written)
3688 mutex_unlock(&fi->inmem_lock);
3689 return rc;
3690 }
3691
3692 if (atomic_written) {
3693 struct inmem_pages *cur;
Yi Zhuang5f029c02021-04-06 09:47:35 +08003694
Weichao Guo5b7a4872016-09-20 05:03:27 +08003695 list_for_each_entry(cur, &fi->inmem_pages, list)
3696 if (cur->page == page) {
3697 cur->page = newpage;
3698 break;
3699 }
3700 mutex_unlock(&fi->inmem_lock);
3701 put_page(page);
3702 get_page(newpage);
3703 }
3704
Jaegeuk Kimc9ebd3d2021-07-04 22:11:25 -07003705 /* guarantee to start from no stale private field */
3706 set_page_private(newpage, 0);
Chao Yu240a5912019-03-06 17:30:59 +08003707 if (PagePrivate(page)) {
Chao Yub763f3b2021-04-28 17:20:31 +08003708 set_page_private(newpage, page_private(page));
3709 SetPagePrivate(newpage);
3710 get_page(newpage);
3711
3712 set_page_private(page, 0);
3713 ClearPagePrivate(page);
3714 put_page(page);
Chao Yu240a5912019-03-06 17:30:59 +08003715 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08003716
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07003717 if (mode != MIGRATE_SYNC_NO_COPY)
3718 migrate_page_copy(newpage, page);
3719 else
3720 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08003721
3722 return MIGRATEPAGE_SUCCESS;
3723}
3724#endif
3725
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003726#ifdef CONFIG_SWAP
Chao Yu859fca62021-05-26 14:29:27 +08003727static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
3728 unsigned int blkcnt)
3729{
3730 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3731 unsigned int blkofs;
3732 unsigned int blk_per_sec = BLKS_PER_SEC(sbi);
3733 unsigned int secidx = start_blk / blk_per_sec;
3734 unsigned int end_sec = secidx + blkcnt / blk_per_sec;
3735 int ret = 0;
3736
3737 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jan Karaedc6d012021-04-13 18:10:37 +02003738 filemap_invalidate_lock(inode->i_mapping);
Chao Yu859fca62021-05-26 14:29:27 +08003739
3740 set_inode_flag(inode, FI_ALIGNED_WRITE);
3741
3742 for (; secidx < end_sec; secidx++) {
3743 down_write(&sbi->pin_sem);
3744
3745 f2fs_lock_op(sbi);
3746 f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
3747 f2fs_unlock_op(sbi);
3748
3749 set_inode_flag(inode, FI_DO_DEFRAG);
3750
3751 for (blkofs = 0; blkofs < blk_per_sec; blkofs++) {
3752 struct page *page;
3753 unsigned int blkidx = secidx * blk_per_sec + blkofs;
3754
3755 page = f2fs_get_lock_data_page(inode, blkidx, true);
3756 if (IS_ERR(page)) {
3757 up_write(&sbi->pin_sem);
3758 ret = PTR_ERR(page);
3759 goto done;
3760 }
3761
3762 set_page_dirty(page);
3763 f2fs_put_page(page, 1);
3764 }
3765
3766 clear_inode_flag(inode, FI_DO_DEFRAG);
3767
3768 ret = filemap_fdatawrite(inode->i_mapping);
3769
3770 up_write(&sbi->pin_sem);
3771
3772 if (ret)
3773 break;
3774 }
3775
3776done:
3777 clear_inode_flag(inode, FI_DO_DEFRAG);
3778 clear_inode_flag(inode, FI_ALIGNED_WRITE);
3779
Jan Karaedc6d012021-04-13 18:10:37 +02003780 filemap_invalidate_unlock(inode->i_mapping);
Chao Yu859fca62021-05-26 14:29:27 +08003781 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3782
3783 return ret;
3784}
3785
Chao Yu0b8fc002021-05-26 14:29:26 +08003786static int check_swap_activate(struct swap_info_struct *sis,
Chao Yuaf4b6b82020-10-12 17:06:05 +08003787 struct file *swap_file, sector_t *span)
3788{
3789 struct address_space *mapping = swap_file->f_mapping;
3790 struct inode *inode = mapping->host;
huangjianan@oppo.com36e4d952021-03-01 12:58:44 +08003791 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuaf4b6b82020-10-12 17:06:05 +08003792 sector_t cur_lblock;
3793 sector_t last_lblock;
3794 sector_t pblock;
3795 sector_t lowest_pblock = -1;
3796 sector_t highest_pblock = 0;
3797 int nr_extents = 0;
3798 unsigned long nr_pblocks;
Chao Yu859fca62021-05-26 14:29:27 +08003799 unsigned int blks_per_sec = BLKS_PER_SEC(sbi);
3800 unsigned int sec_blks_mask = BLKS_PER_SEC(sbi) - 1;
Jaegeuk Kimca298242021-05-11 14:38:47 -07003801 unsigned int not_aligned = 0;
huangjianan@oppo.com36e4d952021-03-01 12:58:44 +08003802 int ret = 0;
Chao Yuaf4b6b82020-10-12 17:06:05 +08003803
3804 /*
3805 * Map all the blocks into the extent list. This code doesn't try
3806 * to be very smart.
3807 */
3808 cur_lblock = 0;
Jaegeuk Kim6cbfcab2020-11-24 14:55:47 -08003809 last_lblock = bytes_to_blks(inode, i_size_read(inode));
Chao Yuaf4b6b82020-10-12 17:06:05 +08003810
huangjianan@oppo.com1da66102021-02-27 20:02:30 +08003811 while (cur_lblock < last_lblock && cur_lblock < sis->max) {
Jaegeuk Kimb876f4c92020-11-24 15:19:10 -08003812 struct f2fs_map_blocks map;
Chao Yu859fca62021-05-26 14:29:27 +08003813retry:
Chao Yuaf4b6b82020-10-12 17:06:05 +08003814 cond_resched();
3815
Jaegeuk Kimb876f4c92020-11-24 15:19:10 -08003816 memset(&map, 0, sizeof(map));
3817 map.m_lblk = cur_lblock;
huangjianan@oppo.com36e4d952021-03-01 12:58:44 +08003818 map.m_len = last_lblock - cur_lblock;
3819 map.m_next_pgofs = NULL;
3820 map.m_next_extent = NULL;
Jaegeuk Kimb876f4c92020-11-24 15:19:10 -08003821 map.m_seg_type = NO_CHECK_TYPE;
huangjianan@oppo.com36e4d952021-03-01 12:58:44 +08003822 map.m_may_create = false;
Chao Yuaf4b6b82020-10-12 17:06:05 +08003823
Jaegeuk Kimb876f4c92020-11-24 15:19:10 -08003824 ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
Chao Yuaf4b6b82020-10-12 17:06:05 +08003825 if (ret)
huangjianan@oppo.com36e4d952021-03-01 12:58:44 +08003826 goto out;
Chao Yuaf4b6b82020-10-12 17:06:05 +08003827
3828 /* hole */
huangjianan@oppo.com36e4d952021-03-01 12:58:44 +08003829 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
Joe Perches833dcd32021-05-26 13:05:36 -07003830 f2fs_err(sbi, "Swapfile has holes");
Jaegeuk Kimf3951832021-05-12 07:38:00 -07003831 ret = -EINVAL;
huangjianan@oppo.com36e4d952021-03-01 12:58:44 +08003832 goto out;
3833 }
Chao Yuaf4b6b82020-10-12 17:06:05 +08003834
Jaegeuk Kimb876f4c92020-11-24 15:19:10 -08003835 pblock = map.m_pblk;
3836 nr_pblocks = map.m_len;
Chao Yuaf4b6b82020-10-12 17:06:05 +08003837
Chao Yu859fca62021-05-26 14:29:27 +08003838 if ((pblock - SM_I(sbi)->main_blkaddr) & sec_blks_mask ||
3839 nr_pblocks & sec_blks_mask) {
Jaegeuk Kimca298242021-05-11 14:38:47 -07003840 not_aligned++;
huangjianan@oppo.com36e4d952021-03-01 12:58:44 +08003841
Chao Yu859fca62021-05-26 14:29:27 +08003842 nr_pblocks = roundup(nr_pblocks, blks_per_sec);
3843 if (cur_lblock + nr_pblocks > sis->max)
3844 nr_pblocks -= blks_per_sec;
3845
3846 if (!nr_pblocks) {
3847 /* this extent is last one */
3848 nr_pblocks = map.m_len;
3849 f2fs_warn(sbi, "Swapfile: last extent is not aligned to section");
3850 goto next;
3851 }
3852
3853 ret = f2fs_migrate_blocks(inode, cur_lblock,
3854 nr_pblocks);
3855 if (ret)
3856 goto out;
3857 goto retry;
3858 }
3859next:
Chao Yuaf4b6b82020-10-12 17:06:05 +08003860 if (cur_lblock + nr_pblocks >= sis->max)
3861 nr_pblocks = sis->max - cur_lblock;
3862
3863 if (cur_lblock) { /* exclude the header page */
3864 if (pblock < lowest_pblock)
3865 lowest_pblock = pblock;
3866 if (pblock + nr_pblocks - 1 > highest_pblock)
3867 highest_pblock = pblock + nr_pblocks - 1;
3868 }
3869
3870 /*
3871 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3872 */
3873 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
3874 if (ret < 0)
3875 goto out;
3876 nr_extents += ret;
3877 cur_lblock += nr_pblocks;
3878 }
3879 ret = nr_extents;
3880 *span = 1 + highest_pblock - lowest_pblock;
3881 if (cur_lblock == 0)
3882 cur_lblock = 1; /* force Empty message */
3883 sis->max = cur_lblock;
3884 sis->pages = cur_lblock - 1;
3885 sis->highest_bit = cur_lblock - 1;
3886out:
Chao Yu859fca62021-05-26 14:29:27 +08003887 if (not_aligned)
3888 f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%u * N)",
3889 not_aligned, blks_per_sec * F2FS_BLKSIZE);
Chao Yuaf4b6b82020-10-12 17:06:05 +08003890 return ret;
Chao Yuaf4b6b82020-10-12 17:06:05 +08003891}
3892
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003893static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3894 sector_t *span)
3895{
3896 struct inode *inode = file_inode(file);
3897 int ret;
3898
3899 if (!S_ISREG(inode->i_mode))
3900 return -EINVAL;
3901
3902 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3903 return -EROFS;
3904
Shin'ichiro Kawasakid927ccf2021-05-10 20:24:44 +09003905 if (f2fs_lfs_mode(F2FS_I_SB(inode))) {
3906 f2fs_err(F2FS_I_SB(inode),
3907 "Swapfile not supported in LFS mode");
3908 return -EINVAL;
3909 }
3910
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003911 ret = f2fs_convert_inline_inode(inode);
3912 if (ret)
3913 return ret;
3914
Daeho Jeong78134d02020-09-08 11:44:11 +09003915 if (!f2fs_disable_compressed_file(inode))
Chao Yu4c8ff702019-11-01 18:07:14 +08003916 return -EINVAL;
3917
Chao Yu0b979f12020-12-26 18:07:41 +08003918 f2fs_precache_extents(inode);
3919
Chao Yu3e5e4792019-12-27 18:44:56 +08003920 ret = check_swap_activate(sis, file, span);
3921 if (ret < 0)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003922 return ret;
3923
3924 set_inode_flag(inode, FI_PIN_FILE);
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003925 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,
Eric Biggersa1e09b02021-07-23 00:59:21 -07003957 .direct_IO = noop_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}
Eric Biggers1517c1a2021-07-23 00:59:20 -07004037
4038static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
4039 unsigned int flags, struct iomap *iomap,
4040 struct iomap *srcmap)
4041{
4042 struct f2fs_map_blocks map = {};
4043 pgoff_t next_pgofs = 0;
4044 int err;
4045
4046 map.m_lblk = bytes_to_blks(inode, offset);
4047 map.m_len = bytes_to_blks(inode, offset + length - 1) - map.m_lblk + 1;
4048 map.m_next_pgofs = &next_pgofs;
4049 map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4050 if (flags & IOMAP_WRITE)
4051 map.m_may_create = true;
4052
4053 err = f2fs_map_blocks(inode, &map, flags & IOMAP_WRITE,
4054 F2FS_GET_BLOCK_DIO);
4055 if (err)
4056 return err;
4057
4058 iomap->offset = blks_to_bytes(inode, map.m_lblk);
4059
4060 if (map.m_flags & (F2FS_MAP_MAPPED | F2FS_MAP_UNWRITTEN)) {
4061 iomap->length = blks_to_bytes(inode, map.m_len);
4062 if (map.m_flags & F2FS_MAP_MAPPED) {
4063 iomap->type = IOMAP_MAPPED;
4064 iomap->flags |= IOMAP_F_MERGED;
4065 } else {
4066 iomap->type = IOMAP_UNWRITTEN;
4067 }
4068 if (WARN_ON_ONCE(!__is_valid_data_blkaddr(map.m_pblk)))
4069 return -EINVAL;
4070
4071 iomap->bdev = map.m_bdev;
4072 iomap->addr = blks_to_bytes(inode, map.m_pblk);
4073 } else {
4074 iomap->length = blks_to_bytes(inode, next_pgofs) -
4075 iomap->offset;
4076 iomap->type = IOMAP_HOLE;
4077 iomap->addr = IOMAP_NULL_ADDR;
4078 }
4079
4080 if (map.m_flags & F2FS_MAP_NEW)
4081 iomap->flags |= IOMAP_F_NEW;
4082 if ((inode->i_state & I_DIRTY_DATASYNC) ||
4083 offset + length > i_size_read(inode))
4084 iomap->flags |= IOMAP_F_DIRTY;
4085
4086 return 0;
4087}
4088
4089const struct iomap_ops f2fs_iomap_ops = {
4090 .iomap_begin = f2fs_iomap_begin,
4091};