blob: 0197b7b80d191736f8041fa87576110ad51ba399 [file] [log] [blame]
Chao Yu7c1a0002018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003 * fs/f2fs/data.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/buffer_head.h>
11#include <linux/mpage.h>
12#include <linux/writeback.h>
13#include <linux/backing-dev.h>
Chao Yu8f46dca2015-07-14 18:56:10 +080014#include <linux/pagevec.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090015#include <linux/blkdev.h>
16#include <linux/bio.h>
Jaegeuk Kim4969c062019-07-01 19:15:29 -070017#include <linux/swap.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010018#include <linux/prefetch.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080019#include <linux/uio.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070020#include <linux/cleancache.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010021#include <linux/sched/signal.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090022
23#include "f2fs.h"
24#include "node.h"
25#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080026#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090027#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090028
Eric Biggers6dbb1792018-04-18 11:09:48 -070029#define NUM_PREALLOC_POST_READ_CTXS 128
30
31static struct kmem_cache *bio_post_read_ctx_cache;
Chao Yu0b20fce2019-09-30 18:53:25 +080032static struct kmem_cache *bio_entry_slab;
Eric Biggers6dbb1792018-04-18 11:09:48 -070033static mempool_t *bio_post_read_ctx_pool;
Chao Yuf5438052019-12-04 09:52:58 +080034static struct bio_set f2fs_bioset;
35
36#define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
37
38int __init f2fs_init_bioset(void)
39{
40 if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
41 0, BIOSET_NEED_BVECS))
42 return -ENOMEM;
43 return 0;
44}
45
46void f2fs_destroy_bioset(void)
47{
48 bioset_exit(&f2fs_bioset);
49}
50
51static inline struct bio *__f2fs_bio_alloc(gfp_t gfp_mask,
52 unsigned int nr_iovecs)
53{
54 return bio_alloc_bioset(gfp_mask, nr_iovecs, &f2fs_bioset);
55}
56
Chao Yuca9e9682020-02-18 18:21:34 +080057struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool noio)
Chao Yuf5438052019-12-04 09:52:58 +080058{
Chao Yuca9e9682020-02-18 18:21:34 +080059 if (noio) {
Chao Yuf5438052019-12-04 09:52:58 +080060 /* No failure on bio allocation */
Chao Yuca9e9682020-02-18 18:21:34 +080061 return __f2fs_bio_alloc(GFP_NOIO, npages);
Chao Yuf5438052019-12-04 09:52:58 +080062 }
Chao Yuca9e9682020-02-18 18:21:34 +080063
Chao Yuf5438052019-12-04 09:52:58 +080064 if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
65 f2fs_show_injection_info(sbi, FAULT_ALLOC_BIO);
66 return NULL;
67 }
68
69 return __f2fs_bio_alloc(GFP_KERNEL, npages);
70}
Eric Biggers6dbb1792018-04-18 11:09:48 -070071
Chao Yu36951b32016-11-16 10:41:20 +080072static bool __is_cp_guaranteed(struct page *page)
73{
74 struct address_space *mapping = page->mapping;
75 struct inode *inode;
76 struct f2fs_sb_info *sbi;
77
78 if (!mapping)
79 return false;
80
Chao Yu4c8ff702019-11-01 18:07:14 +080081 if (f2fs_is_compressed_page(page))
82 return false;
83
Chao Yu36951b32016-11-16 10:41:20 +080084 inode = mapping->host;
85 sbi = F2FS_I_SB(inode);
86
87 if (inode->i_ino == F2FS_META_INO(sbi) ||
88 inode->i_ino == F2FS_NODE_INO(sbi) ||
89 S_ISDIR(inode->i_mode) ||
Chao Yue7a4feb2018-05-08 14:06:03 +080090 (S_ISREG(inode->i_mode) &&
Chao Yuaf033b22018-09-20 20:05:00 +080091 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
Chao Yu36951b32016-11-16 10:41:20 +080092 is_cold_data(page))
93 return true;
94 return false;
95}
96
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -070097static enum count_type __read_io_type(struct page *page)
98{
Jaegeuk Kim4969c062019-07-01 19:15:29 -070099 struct address_space *mapping = page_file_mapping(page);
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700100
101 if (mapping) {
102 struct inode *inode = mapping->host;
103 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
104
105 if (inode->i_ino == F2FS_META_INO(sbi))
106 return F2FS_RD_META;
107
108 if (inode->i_ino == F2FS_NODE_INO(sbi))
109 return F2FS_RD_NODE;
110 }
111 return F2FS_RD_DATA;
112}
113
Eric Biggers6dbb1792018-04-18 11:09:48 -0700114/* postprocessing steps for read bios */
115enum bio_post_read_step {
Eric Biggers6dbb1792018-04-18 11:09:48 -0700116 STEP_DECRYPT,
Chao Yu4c8ff702019-11-01 18:07:14 +0800117 STEP_DECOMPRESS,
Eric Biggers95ae2512019-07-22 09:26:24 -0700118 STEP_VERITY,
Eric Biggers6dbb1792018-04-18 11:09:48 -0700119};
120
121struct bio_post_read_ctx {
122 struct bio *bio;
Chao Yu4c8ff702019-11-01 18:07:14 +0800123 struct f2fs_sb_info *sbi;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700124 struct work_struct work;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700125 unsigned int enabled_steps;
126};
127
Chao Yu4c8ff702019-11-01 18:07:14 +0800128static void __read_end_io(struct bio *bio, bool compr, bool verity)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900129{
Eric Biggers6dbb1792018-04-18 11:09:48 -0700130 struct page *page;
131 struct bio_vec *bv;
Ming Lei6dc4f102019-02-15 19:13:19 +0800132 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900133
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200134 bio_for_each_segment_all(bv, bio, iter_all) {
Eric Biggers6dbb1792018-04-18 11:09:48 -0700135 page = bv->bv_page;
136
Chao Yu4c8ff702019-11-01 18:07:14 +0800137#ifdef CONFIG_F2FS_FS_COMPRESSION
138 if (compr && f2fs_is_compressed_page(page)) {
139 f2fs_decompress_pages(bio, page, verity);
140 continue;
141 }
142#endif
143
Eric Biggers6dbb1792018-04-18 11:09:48 -0700144 /* PG_error was set if any post_read step failed */
145 if (bio->bi_status || PageError(page)) {
146 ClearPageUptodate(page);
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -0700147 /* will re-read again later */
148 ClearPageError(page);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700149 } else {
150 SetPageUptodate(page);
151 }
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700152 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
Eric Biggers6dbb1792018-04-18 11:09:48 -0700153 unlock_page(page);
154 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800155}
156
157static void f2fs_release_read_bio(struct bio *bio);
158static void __f2fs_read_end_io(struct bio *bio, bool compr, bool verity)
159{
160 if (!compr)
161 __read_end_io(bio, false, verity);
162 f2fs_release_read_bio(bio);
163}
164
165static void f2fs_decompress_bio(struct bio *bio, bool verity)
166{
167 __read_end_io(bio, true, verity);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700168}
169
170static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
171
Chao Yu4c8ff702019-11-01 18:07:14 +0800172static void f2fs_decrypt_work(struct bio_post_read_ctx *ctx)
Eric Biggers6dbb1792018-04-18 11:09:48 -0700173{
Eric Biggers6dbb1792018-04-18 11:09:48 -0700174 fscrypt_decrypt_bio(ctx->bio);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700175}
176
Chao Yu4c8ff702019-11-01 18:07:14 +0800177static void f2fs_decompress_work(struct bio_post_read_ctx *ctx)
178{
179 f2fs_decompress_bio(ctx->bio, ctx->enabled_steps & (1 << STEP_VERITY));
180}
181
182#ifdef CONFIG_F2FS_FS_COMPRESSION
183static void f2fs_verify_pages(struct page **rpages, unsigned int cluster_size)
184{
185 f2fs_decompress_end_io(rpages, cluster_size, false, true);
186}
187
188static void f2fs_verify_bio(struct bio *bio)
189{
Chao Yu79bbefb2020-03-23 17:43:04 +0800190 struct bio_vec *bv;
191 struct bvec_iter_all iter_all;
Chao Yu4c8ff702019-11-01 18:07:14 +0800192
Chao Yu79bbefb2020-03-23 17:43:04 +0800193 bio_for_each_segment_all(bv, bio, iter_all) {
194 struct page *page = bv->bv_page;
195 struct decompress_io_ctx *dic;
196
197 dic = (struct decompress_io_ctx *)page_private(page);
198
199 if (dic) {
200 if (refcount_dec_not_one(&dic->ref))
201 continue;
202 f2fs_verify_pages(dic->rpages,
203 dic->cluster_size);
204 f2fs_free_dic(dic);
205 continue;
206 }
207
208 if (bio->bi_status || PageError(page))
209 goto clear_uptodate;
210
211 if (fsverity_verify_page(page)) {
212 SetPageUptodate(page);
213 goto unlock;
214 }
215clear_uptodate:
216 ClearPageUptodate(page);
217 ClearPageError(page);
218unlock:
219 unlock_page(page);
220 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800221}
222#endif
223
224static void f2fs_verity_work(struct work_struct *work)
Eric Biggers95ae2512019-07-22 09:26:24 -0700225{
226 struct bio_post_read_ctx *ctx =
227 container_of(work, struct bio_post_read_ctx, work);
Eric Biggers644c8c92019-12-31 12:14:16 -0600228 struct bio *bio = ctx->bio;
229#ifdef CONFIG_F2FS_FS_COMPRESSION
230 unsigned int enabled_steps = ctx->enabled_steps;
231#endif
232
233 /*
234 * fsverity_verify_bio() may call readpages() again, and while verity
235 * will be disabled for this, decryption may still be needed, resulting
236 * in another bio_post_read_ctx being allocated. So to prevent
237 * deadlocks we need to release the current ctx to the mempool first.
238 * This assumes that verity is the last post-read step.
239 */
240 mempool_free(ctx, bio_post_read_ctx_pool);
241 bio->bi_private = NULL;
Eric Biggers95ae2512019-07-22 09:26:24 -0700242
Chao Yu4c8ff702019-11-01 18:07:14 +0800243#ifdef CONFIG_F2FS_FS_COMPRESSION
244 /* previous step is decompression */
Eric Biggers644c8c92019-12-31 12:14:16 -0600245 if (enabled_steps & (1 << STEP_DECOMPRESS)) {
246 f2fs_verify_bio(bio);
247 f2fs_release_read_bio(bio);
Chao Yu4c8ff702019-11-01 18:07:14 +0800248 return;
249 }
250#endif
251
Eric Biggers644c8c92019-12-31 12:14:16 -0600252 fsverity_verify_bio(bio);
253 __f2fs_read_end_io(bio, false, false);
Chao Yu4c8ff702019-11-01 18:07:14 +0800254}
255
256static void f2fs_post_read_work(struct work_struct *work)
257{
258 struct bio_post_read_ctx *ctx =
259 container_of(work, struct bio_post_read_ctx, work);
260
261 if (ctx->enabled_steps & (1 << STEP_DECRYPT))
262 f2fs_decrypt_work(ctx);
263
264 if (ctx->enabled_steps & (1 << STEP_DECOMPRESS))
265 f2fs_decompress_work(ctx);
266
267 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
268 INIT_WORK(&ctx->work, f2fs_verity_work);
269 fsverity_enqueue_verify_work(&ctx->work);
270 return;
271 }
272
273 __f2fs_read_end_io(ctx->bio,
274 ctx->enabled_steps & (1 << STEP_DECOMPRESS), false);
275}
276
277static void f2fs_enqueue_post_read_work(struct f2fs_sb_info *sbi,
278 struct work_struct *work)
279{
280 queue_work(sbi->post_read_wq, work);
Eric Biggers95ae2512019-07-22 09:26:24 -0700281}
282
Eric Biggers6dbb1792018-04-18 11:09:48 -0700283static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
284{
Eric Biggers95ae2512019-07-22 09:26:24 -0700285 /*
286 * We use different work queues for decryption and for verity because
287 * verity may require reading metadata pages that need decryption, and
288 * we shouldn't recurse to the same workqueue.
289 */
Chao Yu4c8ff702019-11-01 18:07:14 +0800290
291 if (ctx->enabled_steps & (1 << STEP_DECRYPT) ||
292 ctx->enabled_steps & (1 << STEP_DECOMPRESS)) {
293 INIT_WORK(&ctx->work, f2fs_post_read_work);
294 f2fs_enqueue_post_read_work(ctx->sbi, &ctx->work);
295 return;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700296 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800297
298 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
299 INIT_WORK(&ctx->work, f2fs_verity_work);
300 fsverity_enqueue_verify_work(&ctx->work);
301 return;
302 }
303
304 __f2fs_read_end_io(ctx->bio, false, false);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700305}
306
307static bool f2fs_bio_post_read_required(struct bio *bio)
308{
Chao Yu4c8ff702019-11-01 18:07:14 +0800309 return bio->bi_private;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700310}
311
312static void f2fs_read_end_io(struct bio *bio)
313{
Chao Yuc45d6002019-11-01 17:53:23 +0800314 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
315
316 if (time_to_inject(sbi, FAULT_READ_IO)) {
317 f2fs_show_injection_info(sbi, FAULT_READ_IO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200318 bio->bi_status = BLK_STS_IOERR;
Chao Yu55523512017-02-25 11:08:28 +0800319 }
Chao Yu8b038c72016-09-18 23:30:07 +0800320
Eric Biggers6dbb1792018-04-18 11:09:48 -0700321 if (f2fs_bio_post_read_required(bio)) {
322 struct bio_post_read_ctx *ctx = bio->bi_private;
323
Eric Biggers6dbb1792018-04-18 11:09:48 -0700324 bio_post_read_processing(ctx);
325 return;
Chao Yu12377022015-05-25 18:03:38 +0800326 }
327
Chao Yu4c8ff702019-11-01 18:07:14 +0800328 __f2fs_read_end_io(bio, false, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900329}
330
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200331static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900332{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +0900333 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -0800334 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800335 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900336
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800337 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800338 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800339 bio->bi_status = BLK_STS_IOERR;
340 }
341
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200342 bio_for_each_segment_all(bvec, bio, iter_all) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900343 struct page *page = bvec->bv_page;
Chao Yu36951b32016-11-16 10:41:20 +0800344 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900345
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800346 if (IS_DUMMY_WRITTEN_PAGE(page)) {
347 set_page_private(page, (unsigned long)NULL);
348 ClearPagePrivate(page);
349 unlock_page(page);
350 mempool_free(page, sbi->write_io_dummy);
351
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200352 if (unlikely(bio->bi_status))
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800353 f2fs_stop_checkpoint(sbi, true);
354 continue;
355 }
356
Eric Biggersd2d07272019-05-20 09:29:39 -0700357 fscrypt_finalize_bounce_page(&page);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700358
Chao Yu4c8ff702019-11-01 18:07:14 +0800359#ifdef CONFIG_F2FS_FS_COMPRESSION
360 if (f2fs_is_compressed_page(page)) {
361 f2fs_compress_write_end_io(bio, page);
362 continue;
363 }
364#endif
365
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200366 if (unlikely(bio->bi_status)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700367 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kimb1ca3212017-12-31 16:26:38 -0800368 if (type == F2FS_WB_CP_DATA)
369 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900370 }
Yunlei He7dff55d2018-01-11 14:19:32 +0800371
372 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
373 page->index != nid_of_node(page));
374
Chao Yu36951b32016-11-16 10:41:20 +0800375 dec_page_count(sbi, type);
Chao Yu50fa53e2018-08-02 23:03:19 +0800376 if (f2fs_in_warm_node_list(sbi, page))
377 f2fs_del_fsync_node_entry(sbi, page);
Chao Yu36951b32016-11-16 10:41:20 +0800378 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900379 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800380 }
Chao Yu36951b32016-11-16 10:41:20 +0800381 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700382 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900383 wake_up(&sbi->cp_wait);
384
385 bio_put(bio);
386}
387
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700388struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
389 block_t blk_addr, struct bio *bio)
390{
391 struct block_device *bdev = sbi->sb->s_bdev;
392 int i;
393
Damien Le Moal09168782019-03-16 09:13:06 +0900394 if (f2fs_is_multi_device(sbi)) {
395 for (i = 0; i < sbi->s_ndevs; i++) {
396 if (FDEV(i).start_blk <= blk_addr &&
397 FDEV(i).end_blk >= blk_addr) {
398 blk_addr -= FDEV(i).start_blk;
399 bdev = FDEV(i).bdev;
400 break;
401 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700402 }
403 }
404 if (bio) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200405 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700406 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
407 }
408 return bdev;
409}
410
411int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
412{
413 int i;
414
Damien Le Moal09168782019-03-16 09:13:06 +0900415 if (!f2fs_is_multi_device(sbi))
416 return 0;
417
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700418 for (i = 0; i < sbi->s_ndevs; i++)
419 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
420 return i;
421 return 0;
422}
423
Chao Yu7a88ddb2020-02-27 19:30:05 +0800424/*
425 * Return true, if pre_bio's bdev is same as its target device.
426 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700427static bool __same_bdev(struct f2fs_sb_info *sbi,
428 block_t blk_addr, struct bio *bio)
429{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200430 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
431 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700432}
433
Chao Yub757f6e2019-08-23 17:58:35 +0800434static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
Gu Zheng940a6d32013-12-20 17:39:59 +0800435{
Chao Yub757f6e2019-08-23 17:58:35 +0800436 struct f2fs_sb_info *sbi = fio->sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800437 struct bio *bio;
438
Chao Yud62fe972017-10-28 16:52:31 +0800439 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800440
Chao Yub757f6e2019-08-23 17:58:35 +0800441 f2fs_target_device(sbi, fio->new_blkaddr, bio);
442 if (is_read_io(fio->op)) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900443 bio->bi_end_io = f2fs_read_end_io;
444 bio->bi_private = NULL;
445 } else {
446 bio->bi_end_io = f2fs_write_end_io;
447 bio->bi_private = sbi;
Chao Yub757f6e2019-08-23 17:58:35 +0800448 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
449 fio->type, fio->temp);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900450 }
Chao Yub757f6e2019-08-23 17:58:35 +0800451 if (fio->io_wbc)
452 wbc_init_bio(fio->io_wbc, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800453
454 return bio;
455}
456
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700457static inline void __submit_bio(struct f2fs_sb_info *sbi,
458 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700459{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700460 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800461 unsigned int start;
462
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800463 if (type != DATA && type != NODE)
464 goto submit_io;
465
Chao Yub0332a02020-02-14 17:44:12 +0800466 if (f2fs_lfs_mode(sbi) && current->plug)
Tiezhu Yang3bb09a02018-02-06 08:21:45 +0800467 blk_finish_plug(current->plug);
468
Chao Yu8223ecc2019-08-28 17:33:38 +0800469 if (F2FS_IO_ALIGNED(sbi))
470 goto submit_io;
471
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800472 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
473 start %= F2FS_IO_SIZE(sbi);
474
475 if (start == 0)
476 goto submit_io;
477
478 /* fill dummy pages */
479 for (; start < F2FS_IO_SIZE(sbi); start++) {
480 struct page *page =
481 mempool_alloc(sbi->write_io_dummy,
Gao Xiangbc73a4b2019-02-19 10:31:52 +0800482 GFP_NOIO | __GFP_NOFAIL);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800483 f2fs_bug_on(sbi, !page);
484
Gao Xiangbc73a4b2019-02-19 10:31:52 +0800485 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800486 SetPagePrivate(page);
487 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
488 lock_page(page);
489 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
490 f2fs_bug_on(sbi, 1);
491 }
492 /*
493 * In the NODE case, we lose next block address chain. So, we
494 * need to do checkpoint in f2fs_sync_file.
495 */
496 if (type == NODE)
497 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700498 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800499submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800500 if (is_read_io(bio_op(bio)))
501 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
502 else
503 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500504 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700505}
506
Chao Yu4c8ff702019-11-01 18:07:14 +0800507void f2fs_submit_bio(struct f2fs_sb_info *sbi,
508 struct bio *bio, enum page_type type)
509{
510 __submit_bio(sbi, bio, type);
511}
512
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900513static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900514{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900515 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900516
517 if (!io->bio)
518 return;
519
Mike Christie04d328d2016-06-05 14:31:55 -0500520 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
521
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800522 if (is_read_io(fio->op))
523 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
524 else
525 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
526
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700527 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900528 io->bio = NULL;
529}
530
Chao Yu8648de22019-02-19 16:15:29 +0800531static bool __has_merged_page(struct bio *bio, struct inode *inode,
Chao Yubab475c2018-09-27 23:41:16 +0800532 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800533{
Chao Yu0fd785e2016-01-18 18:24:59 +0800534 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800535 struct bvec_iter_all iter_all;
Chao Yu0fd785e2016-01-18 18:24:59 +0800536
Chao Yu8648de22019-02-19 16:15:29 +0800537 if (!bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800538 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800539
Chao Yubab475c2018-09-27 23:41:16 +0800540 if (!inode && !page && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800541 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800542
Chao Yu8648de22019-02-19 16:15:29 +0800543 bio_for_each_segment_all(bvec, bio, iter_all) {
Chao Yu4c8ff702019-11-01 18:07:14 +0800544 struct page *target = bvec->bv_page;
Chao Yu0fd785e2016-01-18 18:24:59 +0800545
Chao Yu4c8ff702019-11-01 18:07:14 +0800546 if (fscrypt_is_bounce_page(target)) {
Eric Biggersd2d07272019-05-20 09:29:39 -0700547 target = fscrypt_pagecache_page(target);
Chao Yu4c8ff702019-11-01 18:07:14 +0800548 if (IS_ERR(target))
549 continue;
550 }
551 if (f2fs_is_compressed_page(target)) {
552 target = f2fs_compress_control_page(target);
553 if (IS_ERR(target))
554 continue;
555 }
Chao Yu0fd785e2016-01-18 18:24:59 +0800556
Chao Yu0c3a5792016-01-18 18:28:11 +0800557 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800558 return true;
Chao Yubab475c2018-09-27 23:41:16 +0800559 if (page && page == target)
560 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800561 if (ino && ino == ino_of_node(target))
562 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800563 }
564
Chao Yu0fd785e2016-01-18 18:24:59 +0800565 return false;
566}
567
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700568static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700569 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900570{
571 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700572 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900573
Chao Yudf0f8dc2014-03-22 14:57:23 +0800574 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900575
576 /* change META to META_FLUSH in the checkpoint procedure */
577 if (type >= META_FLUSH) {
578 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500579 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200580 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600581 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800582 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900583 }
584 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800585 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900586}
587
Jaegeuk Kima912b542017-05-10 11:18:25 -0700588static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800589 struct inode *inode, struct page *page,
590 nid_t ino, enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800591{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700592 enum temp_type temp;
Yunlong Song1e771e82018-11-13 11:57:32 +0800593 bool ret = true;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700594
595 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
Yunlong Song1e771e82018-11-13 11:57:32 +0800596 if (!force) {
597 enum page_type btype = PAGE_TYPE_OF_BIO(type);
598 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700599
Yunlong Song1e771e82018-11-13 11:57:32 +0800600 down_read(&io->io_rwsem);
Chao Yu8648de22019-02-19 16:15:29 +0800601 ret = __has_merged_page(io->bio, inode, page, ino);
Yunlong Song1e771e82018-11-13 11:57:32 +0800602 up_read(&io->io_rwsem);
603 }
604 if (ret)
605 __f2fs_submit_merged_write(sbi, type, temp);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700606
607 /* TODO: use HOT temp only for meta pages now. */
608 if (type >= META)
609 break;
610 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800611}
612
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700613void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800614{
Hariprasad Kelamadcc00f2019-04-06 16:29:36 +0530615 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900616}
617
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700618void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800619 struct inode *inode, struct page *page,
620 nid_t ino, enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900621{
Chao Yubab475c2018-09-27 23:41:16 +0800622 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900623}
624
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700625void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800626{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700627 f2fs_submit_merged_write(sbi, DATA);
628 f2fs_submit_merged_write(sbi, NODE);
629 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800630}
631
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900632/*
633 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300634 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900635 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700636int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900637{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900638 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700639 struct page *page = fio->encrypted_page ?
640 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900641
Chao Yuc9b60782018-08-01 19:13:44 +0800642 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800643 fio->is_por ? META_POR : (__is_meta_io(fio) ?
644 META_GENERIC : DATA_GENERIC_ENHANCE)))
Chao Yu10f966b2019-06-20 11:36:14 +0800645 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +0800646
Chao Yu2ace38e2014-12-24 16:08:14 +0800647 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700648 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900649
650 /* Allocate a new bio */
Chao Yub757f6e2019-08-23 17:58:35 +0800651 bio = __bio_alloc(fio, 1);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900652
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300653 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900654 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900655 return -EFAULT;
656 }
Chao Yu78efac52018-10-22 23:24:28 +0800657
658 if (fio->io_wbc && !is_read_io(fio->op))
Tejun Heo34e51a52019-06-27 13:39:49 -0700659 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu78efac52018-10-22 23:24:28 +0800660
Mike Christie04d328d2016-06-05 14:31:55 -0500661 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900662
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700663 inc_page_count(fio->sbi, is_read_io(fio->op) ?
664 __read_io_type(page): WB_DATA_TYPE(fio->page));
Chao Yu4c58ed02018-10-22 09:12:51 +0800665
666 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900667 return 0;
668}
669
Chao Yu8896cbd2019-07-12 16:55:41 +0800670static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
671 block_t last_blkaddr, block_t cur_blkaddr)
672{
673 if (last_blkaddr + 1 != cur_blkaddr)
674 return false;
675 return __same_bdev(sbi, cur_blkaddr, bio);
676}
677
678static bool io_type_is_mergeable(struct f2fs_bio_info *io,
679 struct f2fs_io_info *fio)
680{
681 if (io->fio.op != fio->op)
682 return false;
683 return io->fio.op_flags == fio->op_flags;
684}
685
686static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
687 struct f2fs_bio_info *io,
688 struct f2fs_io_info *fio,
689 block_t last_blkaddr,
690 block_t cur_blkaddr)
691{
Chao Yuc72db712019-07-12 16:55:42 +0800692 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
693 unsigned int filled_blocks =
694 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
695 unsigned int io_size = F2FS_IO_SIZE(sbi);
696 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
697
698 /* IOs in bio is aligned and left space of vectors is not enough */
699 if (!(filled_blocks % io_size) && left_vecs < io_size)
700 return false;
701 }
Chao Yu8896cbd2019-07-12 16:55:41 +0800702 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
703 return false;
704 return io_type_is_mergeable(io, fio);
705}
706
Chao Yu0b20fce2019-09-30 18:53:25 +0800707static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
708 struct page *page, enum temp_type temp)
709{
710 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
711 struct bio_entry *be;
712
713 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
714 be->bio = bio;
715 bio_get(bio);
716
717 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
718 f2fs_bug_on(sbi, 1);
719
720 down_write(&io->bio_list_lock);
721 list_add_tail(&be->list, &io->bio_list);
722 up_write(&io->bio_list_lock);
723}
724
725static void del_bio_entry(struct bio_entry *be)
726{
727 list_del(&be->list);
728 kmem_cache_free(bio_entry_slab, be);
729}
730
731static int add_ipu_page(struct f2fs_sb_info *sbi, struct bio **bio,
732 struct page *page)
733{
734 enum temp_type temp;
735 bool found = false;
736 int ret = -EAGAIN;
737
738 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
739 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
740 struct list_head *head = &io->bio_list;
741 struct bio_entry *be;
742
743 down_write(&io->bio_list_lock);
744 list_for_each_entry(be, head, list) {
745 if (be->bio != *bio)
746 continue;
747
748 found = true;
749
Chao Yu4c8ff702019-11-01 18:07:14 +0800750 if (bio_add_page(*bio, page, PAGE_SIZE, 0) ==
751 PAGE_SIZE) {
Chao Yu0b20fce2019-09-30 18:53:25 +0800752 ret = 0;
753 break;
754 }
755
756 /* bio is full */
757 del_bio_entry(be);
758 __submit_bio(sbi, *bio, DATA);
759 break;
760 }
761 up_write(&io->bio_list_lock);
762 }
763
764 if (ret) {
765 bio_put(*bio);
766 *bio = NULL;
767 }
768
769 return ret;
770}
771
772void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
773 struct bio **bio, struct page *page)
774{
775 enum temp_type temp;
776 bool found = false;
777 struct bio *target = bio ? *bio : NULL;
778
779 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
780 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
781 struct list_head *head = &io->bio_list;
782 struct bio_entry *be;
783
784 if (list_empty(head))
785 continue;
786
787 down_read(&io->bio_list_lock);
788 list_for_each_entry(be, head, list) {
789 if (target)
790 found = (target == be->bio);
791 else
792 found = __has_merged_page(be->bio, NULL,
793 page, 0);
794 if (found)
795 break;
796 }
797 up_read(&io->bio_list_lock);
798
799 if (!found)
800 continue;
801
802 found = false;
803
804 down_write(&io->bio_list_lock);
805 list_for_each_entry(be, head, list) {
806 if (target)
807 found = (target == be->bio);
808 else
809 found = __has_merged_page(be->bio, NULL,
810 page, 0);
811 if (found) {
812 target = be->bio;
813 del_bio_entry(be);
814 break;
815 }
816 }
817 up_write(&io->bio_list_lock);
818 }
819
820 if (found)
821 __submit_bio(sbi, target, DATA);
822 if (bio && *bio) {
823 bio_put(*bio);
824 *bio = NULL;
825 }
826}
827
Chao Yu8648de22019-02-19 16:15:29 +0800828int f2fs_merge_page_bio(struct f2fs_io_info *fio)
829{
830 struct bio *bio = *fio->bio;
831 struct page *page = fio->encrypted_page ?
832 fio->encrypted_page : fio->page;
833
834 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
835 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
Chao Yu10f966b2019-06-20 11:36:14 +0800836 return -EFSCORRUPTED;
Chao Yu8648de22019-02-19 16:15:29 +0800837
838 trace_f2fs_submit_page_bio(page, fio);
839 f2fs_trace_ios(fio, 0);
840
Chao Yu8896cbd2019-07-12 16:55:41 +0800841 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
Chao Yu0b20fce2019-09-30 18:53:25 +0800842 fio->new_blkaddr))
843 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
Chao Yu8648de22019-02-19 16:15:29 +0800844alloc_new:
845 if (!bio) {
Chao Yub757f6e2019-08-23 17:58:35 +0800846 bio = __bio_alloc(fio, BIO_MAX_PAGES);
Chao Yu8648de22019-02-19 16:15:29 +0800847 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Chao Yu8648de22019-02-19 16:15:29 +0800848
Chao Yu0b20fce2019-09-30 18:53:25 +0800849 add_bio_entry(fio->sbi, bio, page, fio->temp);
850 } else {
851 if (add_ipu_page(fio->sbi, &bio, page))
852 goto alloc_new;
Chao Yu8648de22019-02-19 16:15:29 +0800853 }
854
855 if (fio->io_wbc)
Linus Torvalds9637d512019-07-15 21:20:52 -0700856 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu8648de22019-02-19 16:15:29 +0800857
858 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
859
860 *fio->last_block = fio->new_blkaddr;
861 *fio->bio = bio;
862
863 return 0;
864}
865
Chao Yufe16efe2018-05-28 23:47:18 +0800866void f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900867{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700868 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900869 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700870 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700871 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900872
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700873 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900874
Chao Yufb830fc2017-05-19 23:37:01 +0800875 down_write(&io->io_rwsem);
876next:
877 if (fio->in_list) {
878 spin_lock(&io->io_lock);
879 if (list_empty(&io->io_list)) {
880 spin_unlock(&io->io_lock);
Chao Yufe16efe2018-05-28 23:47:18 +0800881 goto out;
Chao Yufb830fc2017-05-19 23:37:01 +0800882 }
883 fio = list_first_entry(&io->io_list,
884 struct f2fs_io_info, list);
885 list_del(&fio->list);
886 spin_unlock(&io->io_lock);
887 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900888
Chao Yu93770ab2019-04-15 15:26:32 +0800889 verify_fio_blkaddr(fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900890
Chao Yu4c8ff702019-11-01 18:07:14 +0800891 if (fio->encrypted_page)
892 bio_page = fio->encrypted_page;
893 else if (fio->compressed_page)
894 bio_page = fio->compressed_page;
895 else
896 bio_page = fio->page;
Chao Yu36951b32016-11-16 10:41:20 +0800897
Thomas Meyerebf7c522017-10-07 16:02:21 +0200898 /* set submitted = true as a return value */
899 fio->submitted = true;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800900
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700901 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900902
Chao Yu8896cbd2019-07-12 16:55:41 +0800903 if (io->bio && !io_is_mergeable(sbi, io->bio, io, fio,
904 io->last_block_in_bio, fio->new_blkaddr))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900905 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900906alloc_new:
907 if (io->bio == NULL) {
Chao Yu8223ecc2019-08-28 17:33:38 +0800908 if (F2FS_IO_ALIGNED(sbi) &&
909 (fio->type == DATA || fio->type == NODE) &&
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800910 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700911 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Chao Yufe16efe2018-05-28 23:47:18 +0800912 fio->retry = true;
913 goto skip;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800914 }
Chao Yub757f6e2019-08-23 17:58:35 +0800915 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900916 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900917 }
918
Jaegeuk Kima912b542017-05-10 11:18:25 -0700919 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900920 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900921 goto alloc_new;
922 }
923
Yufen Yu578c6472018-01-09 19:33:39 +0800924 if (fio->io_wbc)
Tejun Heo34e51a52019-06-27 13:39:49 -0700925 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
Yufen Yu578c6472018-01-09 19:33:39 +0800926
Chao Yu7a9d7542016-02-22 18:36:38 +0800927 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700928 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800929
930 trace_f2fs_submit_page_write(fio->page, fio);
Chao Yufe16efe2018-05-28 23:47:18 +0800931skip:
Chao Yufb830fc2017-05-19 23:37:01 +0800932 if (fio->in_list)
933 goto next;
Chao Yufe16efe2018-05-28 23:47:18 +0800934out:
Daniel Rosenberg43549942018-08-20 19:21:43 -0700935 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
Chao Yu00e09c02019-08-23 17:58:36 +0800936 !f2fs_is_checkpoint_ready(sbi))
Jaegeuk Kim5ce80582018-09-06 11:40:12 -0700937 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800938 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900939}
940
Eric Biggers95ae2512019-07-22 09:26:24 -0700941static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
942{
943 return fsverity_active(inode) &&
944 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
945}
946
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700947static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
Eric Biggers95ae2512019-07-22 09:26:24 -0700948 unsigned nr_pages, unsigned op_flag,
Chao Yu06837282020-02-18 18:21:35 +0800949 pgoff_t first_idx, bool for_write)
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700950{
951 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700952 struct bio *bio;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700953 struct bio_post_read_ctx *ctx;
954 unsigned int post_read_steps = 0;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700955
Chao Yu06837282020-02-18 18:21:35 +0800956 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES),
957 for_write);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700958 if (!bio)
959 return ERR_PTR(-ENOMEM);
960 f2fs_target_device(sbi, blkaddr, bio);
961 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kime2e59412018-06-21 11:29:43 -0700962 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700963
964 if (f2fs_encrypted_file(inode))
965 post_read_steps |= 1 << STEP_DECRYPT;
Chao Yu4c8ff702019-11-01 18:07:14 +0800966 if (f2fs_compressed_file(inode))
967 post_read_steps |= 1 << STEP_DECOMPRESS;
Eric Biggers95ae2512019-07-22 09:26:24 -0700968 if (f2fs_need_verity(inode, first_idx))
969 post_read_steps |= 1 << STEP_VERITY;
970
Eric Biggers6dbb1792018-04-18 11:09:48 -0700971 if (post_read_steps) {
Eric Biggerse8ce5742019-12-31 12:14:56 -0600972 /* Due to the mempool, this never fails. */
Eric Biggers6dbb1792018-04-18 11:09:48 -0700973 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700974 ctx->bio = bio;
Chao Yu4c8ff702019-11-01 18:07:14 +0800975 ctx->sbi = sbi;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700976 ctx->enabled_steps = post_read_steps;
977 bio->bi_private = ctx;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700978 }
979
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700980 return bio;
981}
982
Chao Yu4c8ff702019-11-01 18:07:14 +0800983static void f2fs_release_read_bio(struct bio *bio)
984{
985 if (bio->bi_private)
986 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
987 bio_put(bio);
988}
989
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700990/* This can handle encryption stuffs */
991static int f2fs_submit_page_read(struct inode *inode, struct page *page,
Chao Yu06837282020-02-18 18:21:35 +0800992 block_t blkaddr, bool for_write)
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700993{
Chao Yu93770ab2019-04-15 15:26:32 +0800994 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
995 struct bio *bio;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700996
Chao Yu06837282020-02-18 18:21:35 +0800997 bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0, page->index, for_write);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700998 if (IS_ERR(bio))
999 return PTR_ERR(bio);
1000
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07001001 /* wait for GCed page writeback via META_MAPPING */
1002 f2fs_wait_on_block_writeback(inode, blkaddr);
1003
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001004 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1005 bio_put(bio);
1006 return -EFAULT;
1007 }
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -07001008 ClearPageError(page);
Chao Yu93770ab2019-04-15 15:26:32 +08001009 inc_page_count(sbi, F2FS_RD_DATA);
1010 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001011 return 0;
1012}
1013
Chao Yu46008c62016-05-09 19:56:30 +08001014static void __set_data_blkaddr(struct dnode_of_data *dn)
1015{
1016 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1017 __le32 *addr_array;
Chao Yu7a2af762017-07-19 00:19:06 +08001018 int base = 0;
1019
1020 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1021 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +08001022
1023 /* Get physical address of data block */
1024 addr_array = blkaddr_in_node(rn);
Chao Yu7a2af762017-07-19 00:19:06 +08001025 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +08001026}
1027
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001028/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001029 * Lock ordering for the change of data block address:
1030 * ->data_page
1031 * ->node_page
1032 * update block addresses in the node page
1033 */
Chao Yu4d57b862018-05-30 00:20:41 +08001034void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001035{
Chao Yubae0ee72018-12-25 17:43:42 +08001036 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +08001037 __set_data_blkaddr(dn);
1038 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001039 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001040}
1041
Chao Yuf28b3432016-02-24 17:16:47 +08001042void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1043{
1044 dn->data_blkaddr = blkaddr;
Chao Yu4d57b862018-05-30 00:20:41 +08001045 f2fs_set_data_blkaddr(dn);
Chao Yuf28b3432016-02-24 17:16:47 +08001046 f2fs_update_extent_cache(dn);
1047}
1048
Chao Yu46008c62016-05-09 19:56:30 +08001049/* dn->ofs_in_node will be returned with up-to-date last block pointer */
Chao Yu4d57b862018-05-30 00:20:41 +08001050int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001051{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001052 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +08001053 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001054
Chao Yu46008c62016-05-09 19:56:30 +08001055 if (!count)
1056 return 0;
1057
Jaegeuk Kim91942322016-05-20 10:13:22 -07001058 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001059 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +08001060 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1061 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001062
Chao Yu46008c62016-05-09 19:56:30 +08001063 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1064 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +09001065
Chao Yubae0ee72018-12-25 17:43:42 +08001066 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +08001067
1068 for (; count > 0; dn->ofs_in_node++) {
Chao Yua2ced1c2020-02-14 17:44:10 +08001069 block_t blkaddr = f2fs_data_blkaddr(dn);
Chao Yu46008c62016-05-09 19:56:30 +08001070 if (blkaddr == NULL_ADDR) {
1071 dn->data_blkaddr = NEW_ADDR;
1072 __set_data_blkaddr(dn);
1073 count--;
1074 }
1075 }
1076
1077 if (set_page_dirty(dn->node_page))
1078 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001079 return 0;
1080}
1081
Chao Yu46008c62016-05-09 19:56:30 +08001082/* Should keep dn->ofs_in_node unchanged */
Chao Yu4d57b862018-05-30 00:20:41 +08001083int f2fs_reserve_new_block(struct dnode_of_data *dn)
Chao Yu46008c62016-05-09 19:56:30 +08001084{
1085 unsigned int ofs_in_node = dn->ofs_in_node;
1086 int ret;
1087
Chao Yu4d57b862018-05-30 00:20:41 +08001088 ret = f2fs_reserve_new_blocks(dn, 1);
Chao Yu46008c62016-05-09 19:56:30 +08001089 dn->ofs_in_node = ofs_in_node;
1090 return ret;
1091}
1092
Huajun Lib6009652013-11-10 23:13:18 +08001093int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1094{
1095 bool need_put = dn->inode_page ? false : true;
1096 int err;
1097
Chao Yu4d57b862018-05-30 00:20:41 +08001098 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
Huajun Lib6009652013-11-10 23:13:18 +08001099 if (err)
1100 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +09001101
Huajun Lib6009652013-11-10 23:13:18 +08001102 if (dn->data_blkaddr == NULL_ADDR)
Chao Yu4d57b862018-05-30 00:20:41 +08001103 err = f2fs_reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001104 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +08001105 f2fs_put_dnode(dn);
1106 return err;
1107}
1108
Fan Li759af1c2015-08-05 15:52:16 +08001109int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001110{
Hou Pengyange15882b2017-02-23 09:18:05 +00001111 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +08001112 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +08001113
Fan Li759af1c2015-08-05 15:52:16 +08001114 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1115 dn->data_blkaddr = ei.blk + index - ei.fofs;
1116 return 0;
Chao Yu429511c2015-02-05 17:54:31 +08001117 }
1118
Fan Li759af1c2015-08-05 15:52:16 +08001119 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001120}
1121
Chao Yu4d57b862018-05-30 00:20:41 +08001122struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -05001123 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001124{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001125 struct address_space *mapping = inode->i_mapping;
1126 struct dnode_of_data dn;
1127 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +00001128 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001129 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001130
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001131 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001132 if (!page)
1133 return ERR_PTR(-ENOMEM);
1134
Chao Yucb3bc9e2015-02-05 18:03:40 +08001135 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1136 dn.data_blkaddr = ei.blk + index - ei.fofs;
Chao Yu93770ab2019-04-15 15:26:32 +08001137 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1138 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001139 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001140 goto put_err;
1141 }
Chao Yucb3bc9e2015-02-05 18:03:40 +08001142 goto got_it;
1143 }
1144
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001145 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001146 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001147 if (err)
1148 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001149 f2fs_put_dnode(&dn);
1150
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001151 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001152 err = -ENOENT;
1153 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001154 }
Chao Yu93770ab2019-04-15 15:26:32 +08001155 if (dn.data_blkaddr != NEW_ADDR &&
1156 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1157 dn.data_blkaddr,
1158 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001159 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001160 goto put_err;
1161 }
Chao Yucb3bc9e2015-02-05 18:03:40 +08001162got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001163 if (PageUptodate(page)) {
1164 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001165 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001166 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001167
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001168 /*
1169 * A new dentry page is allocated but not able to be written, since its
1170 * new inode page couldn't be allocated due to -ENOSPC.
1171 * In such the case, its blkaddr can be remained as NEW_ADDR.
Chao Yu4d57b862018-05-30 00:20:41 +08001172 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1173 * f2fs_init_inode_metadata.
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001174 */
1175 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001176 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001177 if (!PageUptodate(page))
1178 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001179 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001180 return page;
1181 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001182
Chao Yu06837282020-02-18 18:21:35 +08001183 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr, for_write);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001184 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001185 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001186 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001187
1188put_err:
1189 f2fs_put_page(page, 1);
1190 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001191}
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001192
Chao Yu4d57b862018-05-30 00:20:41 +08001193struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001194{
1195 struct address_space *mapping = inode->i_mapping;
1196 struct page *page;
1197
1198 page = find_get_page(mapping, index);
1199 if (page && PageUptodate(page))
1200 return page;
1201 f2fs_put_page(page, 0);
1202
Chao Yu4d57b862018-05-30 00:20:41 +08001203 page = f2fs_get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001204 if (IS_ERR(page))
1205 return page;
1206
1207 if (PageUptodate(page))
1208 return page;
1209
1210 wait_on_page_locked(page);
1211 if (unlikely(!PageUptodate(page))) {
1212 f2fs_put_page(page, 0);
1213 return ERR_PTR(-EIO);
1214 }
1215 return page;
1216}
1217
1218/*
1219 * If it tries to access a hole, return an error.
1220 * Because, the callers, functions in dir.c and GC, should be able to know
1221 * whether this page exists or not.
1222 */
Chao Yu4d57b862018-05-30 00:20:41 +08001223struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001224 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001225{
1226 struct address_space *mapping = inode->i_mapping;
1227 struct page *page;
1228repeat:
Chao Yu4d57b862018-05-30 00:20:41 +08001229 page = f2fs_get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001230 if (IS_ERR(page))
1231 return page;
1232
1233 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001234 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001235 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001236 f2fs_put_page(page, 1);
1237 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001238 }
Chao Yu1563ac72016-07-03 22:05:12 +08001239 if (unlikely(!PageUptodate(page))) {
1240 f2fs_put_page(page, 1);
1241 return ERR_PTR(-EIO);
1242 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001243 return page;
1244}
1245
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001246/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001247 * Caller ensures that this data page is never allocated.
1248 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +09001249 *
Chao Yu4f4124d2013-12-21 18:02:14 +08001250 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1251 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +08001252 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1253 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001254 */
Chao Yu4d57b862018-05-30 00:20:41 +08001255struct page *f2fs_get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +09001256 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001257{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001258 struct address_space *mapping = inode->i_mapping;
1259 struct page *page;
1260 struct dnode_of_data dn;
1261 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -08001262
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001263 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +08001264 if (!page) {
1265 /*
1266 * before exiting, we should make sure ipage will be released
1267 * if any error occur.
1268 */
1269 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001270 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +08001271 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001272
Jaegeuk Kima8865372013-12-27 17:04:17 +09001273 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +08001274 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001275 if (err) {
1276 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001277 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001278 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001279 if (!ipage)
1280 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001281
1282 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001283 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001284
1285 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001286 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001287 if (!PageUptodate(page))
1288 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001289 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001290 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001291
Jaegeuk Kim76121182016-01-01 22:03:47 -08001292 /* if ipage exists, blkaddr should be NEW_ADDR */
1293 f2fs_bug_on(F2FS_I_SB(inode), ipage);
Chao Yu4d57b862018-05-30 00:20:41 +08001294 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001295 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -08001296 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001297 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001298got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +08001299 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001300 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001301 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001302 return page;
1303}
1304
Hyunchul Leed5097be2017-11-28 09:23:00 +09001305static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001306{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001307 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001308 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001309 struct node_info ni;
Chao Yu6aa58d82018-08-14 22:37:25 +08001310 block_t old_blkaddr;
Chao Yu46008c62016-05-09 19:56:30 +08001311 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +08001312 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001313
Jaegeuk Kim91942322016-05-20 10:13:22 -07001314 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001315 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +08001316
Chao Yu77357302018-07-17 00:02:17 +08001317 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1318 if (err)
1319 return err;
1320
Chao Yua2ced1c2020-02-14 17:44:10 +08001321 dn->data_blkaddr = f2fs_data_blkaddr(dn);
Chao Yuf847c692018-09-27 18:34:52 +08001322 if (dn->data_blkaddr != NULL_ADDR)
Chao Yudf6136e2015-03-23 10:33:37 +08001323 goto alloc;
1324
Chao Yu0abd6752017-07-09 00:13:07 +08001325 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1326 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001327
Chao Yudf6136e2015-03-23 10:33:37 +08001328alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001329 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Chao Yu6aa58d82018-08-14 22:37:25 +08001330 old_blkaddr = dn->data_blkaddr;
1331 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001332 &sum, seg_type, NULL, false);
Chao Yu6aa58d82018-08-14 22:37:25 +08001333 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1334 invalidate_mapping_pages(META_MAPPING(sbi),
1335 old_blkaddr, old_blkaddr);
Chao Yu86f35dc2019-08-28 17:33:35 +08001336 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001337
Jaegeuk Kim0a4daae2018-09-19 15:28:40 -07001338 /*
1339 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1340 * data from unwritten block via dio_read.
1341 */
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001342 return 0;
1343}
1344
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001345int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001346{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001347 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +08001348 struct f2fs_map_blocks map;
Chao Yud6d478a12018-01-03 17:30:19 +08001349 int flag;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001350 int err = 0;
Chao Yud6d478a12018-01-03 17:30:19 +08001351 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001352
Jaegeuk Kim0080c502016-05-07 08:52:57 -07001353 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +08001354 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1355 if (map.m_len > map.m_lblk)
1356 map.m_len -= map.m_lblk;
1357 else
1358 map.m_len = 0;
1359
Chao Yuda859852016-01-26 15:42:58 +08001360 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08001361 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001362 map.m_seg_type = NO_CHECK_TYPE;
Chao Yuf9d6d052018-11-13 14:33:45 +08001363 map.m_may_create = true;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001364
Chao Yud6d478a12018-01-03 17:30:19 +08001365 if (direct_io) {
Chao Yu4d57b862018-05-30 00:20:41 +08001366 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
Chao Yuf847c692018-09-27 18:34:52 +08001367 flag = f2fs_force_buffered_io(inode, iocb, from) ?
Chao Yud6d478a12018-01-03 17:30:19 +08001368 F2FS_GET_BLOCK_PRE_AIO :
1369 F2FS_GET_BLOCK_PRE_DIO;
1370 goto map_blocks;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001371 }
Chao Yuf2470372017-07-19 00:19:05 +08001372 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001373 err = f2fs_convert_inline_inode(inode);
1374 if (err)
1375 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001376 }
Chao Yud6d478a12018-01-03 17:30:19 +08001377 if (f2fs_has_inline_data(inode))
Sheng Yong250066452017-11-22 18:23:39 +08001378 return err;
Chao Yud6d478a12018-01-03 17:30:19 +08001379
1380 flag = F2FS_GET_BLOCK_PRE_AIO;
1381
1382map_blocks:
1383 err = f2fs_map_blocks(inode, &map, 1, flag);
1384 if (map.m_len > 0 && err == -ENOSPC) {
1385 if (!direct_io)
1386 set_inode_flag(inode, FI_NO_PREALLOC);
1387 err = 0;
Sheng Yong250066452017-11-22 18:23:39 +08001388 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001389 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001390}
1391
Chao Yu39a86952018-09-27 18:33:18 +08001392void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
Yunlei He59c90812017-03-13 20:22:18 +08001393{
1394 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1395 if (lock)
1396 down_read(&sbi->node_change);
1397 else
1398 up_read(&sbi->node_change);
1399 } else {
1400 if (lock)
1401 f2fs_lock_op(sbi);
1402 else
1403 f2fs_unlock_op(sbi);
1404 }
1405}
1406
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001407/*
Chao Yu7a88ddb2020-02-27 19:30:05 +08001408 * f2fs_map_blocks() tries to find or build mapping relationship which
1409 * maps continuous logical blocks to physical blocks, and return such
1410 * info via f2fs_map_blocks structure.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001411 */
Chao Yud323d002015-10-27 09:53:45 +08001412int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +08001413 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001414{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001415 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001416 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +08001417 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf9d6d052018-11-13 14:33:45 +08001418 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +08001419 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001420 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +08001421 unsigned int ofs_in_node, last_ofs_in_node;
1422 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +00001423 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +08001424 block_t blkaddr;
Chao Yuc4020b22018-01-11 14:42:30 +08001425 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001426
Chao Yudfd02e42016-08-20 15:12:01 +08001427 if (!maxblocks)
1428 return 0;
1429
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001430 map->m_len = 0;
1431 map->m_flags = 0;
1432
1433 /* it only supports block size == page size */
1434 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +08001435 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001436
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001437 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Chao Yub0332a02020-02-14 17:44:12 +08001438 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
Jia Zhuf4f0b672018-11-20 04:29:35 +08001439 map->m_may_create)
1440 goto next_dnode;
1441
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001442 map->m_pblk = ei.blk + pgofs - ei.fofs;
1443 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1444 map->m_flags = F2FS_MAP_MAPPED;
Chao Yuc4020b22018-01-11 14:42:30 +08001445 if (map->m_next_extent)
1446 *map->m_next_extent = pgofs + map->m_len;
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301447
1448 /* for hardware encryption, but to avoid potential issue in future */
1449 if (flag == F2FS_GET_BLOCK_DIO)
1450 f2fs_wait_on_block_writeback_range(inode,
1451 map->m_pblk, map->m_len);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001452 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001453 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001454
Chao Yu4fe71e82016-01-26 15:37:38 +08001455next_dnode:
Chao Yuf9d6d052018-11-13 14:33:45 +08001456 if (map->m_may_create)
Yunlei He59c90812017-03-13 20:22:18 +08001457 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001458
1459 /* When reading holes, we need its node page */
1460 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001461 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001462 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +08001463 if (flag == F2FS_GET_BLOCK_BMAP)
1464 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +08001465 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001466 err = 0;
Chao Yuda859852016-01-26 15:42:58 +08001467 if (map->m_next_pgofs)
1468 *map->m_next_pgofs =
Chao Yu4d57b862018-05-30 00:20:41 +08001469 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuc4020b22018-01-11 14:42:30 +08001470 if (map->m_next_extent)
1471 *map->m_next_extent =
Chao Yu4d57b862018-05-30 00:20:41 +08001472 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +08001473 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001474 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001475 }
Chao Yu973163f2015-09-18 16:51:51 +08001476
Chao Yuc4020b22018-01-11 14:42:30 +08001477 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +08001478 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +01001479 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +08001480 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001481
Chao Yu4fe71e82016-01-26 15:37:38 +08001482next_block:
Chao Yua2ced1c2020-02-14 17:44:10 +08001483 blkaddr = f2fs_data_blkaddr(&dn);
Chao Yu973163f2015-09-18 16:51:51 +08001484
Chao Yuc9b60782018-08-01 19:13:44 +08001485 if (__is_valid_data_blkaddr(blkaddr) &&
Chao Yu93770ab2019-04-15 15:26:32 +08001486 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001487 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08001488 goto sync_out;
1489 }
1490
Chao Yu93770ab2019-04-15 15:26:32 +08001491 if (__is_valid_data_blkaddr(blkaddr)) {
Chao Yuf847c692018-09-27 18:34:52 +08001492 /* use out-place-update for driect IO under LFS mode */
Chao Yub0332a02020-02-14 17:44:12 +08001493 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
Chao Yuf9d6d052018-11-13 14:33:45 +08001494 map->m_may_create) {
Chao Yuf847c692018-09-27 18:34:52 +08001495 err = __allocate_data_block(&dn, map->m_seg_type);
Chao Yu05e3600612019-08-28 17:33:36 +08001496 if (err)
1497 goto sync_out;
1498 blkaddr = dn.data_blkaddr;
1499 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yuf847c692018-09-27 18:34:52 +08001500 }
1501 } else {
Fan Li7df3a432015-12-17 13:20:59 +08001502 if (create) {
1503 if (unlikely(f2fs_cp_error(sbi))) {
1504 err = -EIO;
1505 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +08001506 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001507 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +08001508 if (blkaddr == NULL_ADDR) {
1509 prealloc++;
1510 last_ofs_in_node = dn.ofs_in_node;
1511 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001512 } else {
Jaegeuk Kim0a4daae2018-09-19 15:28:40 -07001513 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1514 flag != F2FS_GET_BLOCK_DIO);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001515 err = __allocate_data_block(&dn,
1516 map->m_seg_type);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001517 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001518 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001519 }
Fan Li7df3a432015-12-17 13:20:59 +08001520 if (err)
1521 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +08001522 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001523 blkaddr = dn.data_blkaddr;
1524 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001525 if (flag == F2FS_GET_BLOCK_BMAP) {
1526 map->m_pblk = 0;
1527 goto sync_out;
1528 }
Chao Yuc4020b22018-01-11 14:42:30 +08001529 if (flag == F2FS_GET_BLOCK_PRECACHE)
1530 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001531 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1532 blkaddr == NULL_ADDR) {
1533 if (map->m_next_pgofs)
1534 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001535 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001536 }
Chao Yuf3d98e72018-01-10 18:18:52 +08001537 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1538 /* for defragment case */
1539 if (map->m_next_pgofs)
1540 *map->m_next_pgofs = pgofs + 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001541 goto sync_out;
Chao Yuf3d98e72018-01-10 18:18:52 +08001542 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001543 }
1544 }
Fan Li7df3a432015-12-17 13:20:59 +08001545
Chao Yu46008c62016-05-09 19:56:30 +08001546 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1547 goto skip;
1548
Chao Yu4fe71e82016-01-26 15:37:38 +08001549 if (map->m_len == 0) {
1550 /* preallocated unwritten block should be mapped for fiemap. */
1551 if (blkaddr == NEW_ADDR)
1552 map->m_flags |= F2FS_MAP_UNWRITTEN;
1553 map->m_flags |= F2FS_MAP_MAPPED;
1554
1555 map->m_pblk = blkaddr;
1556 map->m_len = 1;
1557 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001558 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001559 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001560 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001561 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001562 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001563 } else {
1564 goto sync_out;
1565 }
1566
Chao Yu46008c62016-05-09 19:56:30 +08001567skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001568 dn.ofs_in_node++;
1569 pgofs++;
1570
Chao Yu46008c62016-05-09 19:56:30 +08001571 /* preallocate blocks in batch for one dnode page */
1572 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1573 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001574
Chao Yu46008c62016-05-09 19:56:30 +08001575 dn.ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001576 err = f2fs_reserve_new_blocks(&dn, prealloc);
Chao Yu46008c62016-05-09 19:56:30 +08001577 if (err)
1578 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001579
Chao Yu46008c62016-05-09 19:56:30 +08001580 map->m_len += dn.ofs_in_node - ofs_in_node;
1581 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1582 err = -ENOSPC;
1583 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001584 }
Chao Yu46008c62016-05-09 19:56:30 +08001585 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001586 }
1587
Chao Yu46008c62016-05-09 19:56:30 +08001588 if (pgofs >= end)
1589 goto sync_out;
1590 else if (dn.ofs_in_node < end_offset)
1591 goto next_block;
1592
Chao Yuc4020b22018-01-11 14:42:30 +08001593 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1594 if (map->m_flags & F2FS_MAP_MAPPED) {
1595 unsigned int ofs = start_pgofs - map->m_lblk;
1596
1597 f2fs_update_extent_cache_range(&dn,
1598 start_pgofs, map->m_pblk + ofs,
1599 map->m_len - ofs);
1600 }
1601 }
1602
Chao Yu46008c62016-05-09 19:56:30 +08001603 f2fs_put_dnode(&dn);
1604
Chao Yuf9d6d052018-11-13 14:33:45 +08001605 if (map->m_may_create) {
Yunlei He59c90812017-03-13 20:22:18 +08001606 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001607 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001608 }
Chao Yu46008c62016-05-09 19:56:30 +08001609 goto next_dnode;
1610
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001611sync_out:
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301612
1613 /* for hardware encryption, but to avoid potential issue in future */
1614 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1615 f2fs_wait_on_block_writeback_range(inode,
1616 map->m_pblk, map->m_len);
1617
Chao Yuc4020b22018-01-11 14:42:30 +08001618 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1619 if (map->m_flags & F2FS_MAP_MAPPED) {
1620 unsigned int ofs = start_pgofs - map->m_lblk;
1621
1622 f2fs_update_extent_cache_range(&dn,
1623 start_pgofs, map->m_pblk + ofs,
1624 map->m_len - ofs);
1625 }
1626 if (map->m_next_extent)
1627 *map->m_next_extent = pgofs + 1;
1628 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001629 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001630unlock_out:
Chao Yuf9d6d052018-11-13 14:33:45 +08001631 if (map->m_may_create) {
Yunlei He59c90812017-03-13 20:22:18 +08001632 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001633 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001634 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001635out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001636 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001637 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001638}
1639
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001640bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1641{
1642 struct f2fs_map_blocks map;
1643 block_t last_lblk;
1644 int err;
1645
1646 if (pos + len > i_size_read(inode))
1647 return false;
1648
1649 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1650 map.m_next_pgofs = NULL;
1651 map.m_next_extent = NULL;
1652 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08001653 map.m_may_create = false;
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001654 last_lblk = F2FS_BLK_ALIGN(pos + len);
1655
1656 while (map.m_lblk < last_lblk) {
1657 map.m_len = last_lblk - map.m_lblk;
1658 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1659 if (err || map.m_len == 0)
1660 return false;
1661 map.m_lblk += map.m_len;
1662 }
1663 return true;
1664}
1665
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001666static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001667 struct buffer_head *bh, int create, int flag,
Chao Yuf9d6d052018-11-13 14:33:45 +08001668 pgoff_t *next_pgofs, int seg_type, bool may_write)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001669{
1670 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001671 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001672
1673 map.m_lblk = iblock;
1674 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001675 map.m_next_pgofs = next_pgofs;
Chao Yuc4020b22018-01-11 14:42:30 +08001676 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001677 map.m_seg_type = seg_type;
Chao Yuf9d6d052018-11-13 14:33:45 +08001678 map.m_may_create = may_write;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001679
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001680 err = f2fs_map_blocks(inode, &map, create, flag);
1681 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001682 map_bh(bh, inode->i_sb, map.m_pblk);
1683 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001684 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001685 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001686 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001687}
1688
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001689static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001690 struct buffer_head *bh_result, int create, int flag,
1691 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001692{
Chao Yuda859852016-01-26 15:42:58 +08001693 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001694 flag, next_pgofs,
Chao Yuf9d6d052018-11-13 14:33:45 +08001695 NO_CHECK_TYPE, create);
1696}
1697
1698static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1699 struct buffer_head *bh_result, int create)
1700{
1701 return __get_data_block(inode, iblock, bh_result, create,
1702 F2FS_GET_BLOCK_DIO, NULL,
1703 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
Jaegeuk Kim75a037f2019-07-31 13:27:05 -07001704 IS_SWAPFILE(inode) ? false : true);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001705}
1706
Chao Yue2b4e2b2015-08-19 19:11:19 +08001707static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001708 struct buffer_head *bh_result, int create)
1709{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001710 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuf9d6d052018-11-13 14:33:45 +08001711 F2FS_GET_BLOCK_DIO, NULL,
1712 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1713 false);
Chao Yue2b4e2b2015-08-19 19:11:19 +08001714}
1715
1716static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1717 struct buffer_head *bh_result, int create)
1718{
Yunlei He179448b2015-12-28 21:48:32 +08001719 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001720 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001721 return -EFBIG;
1722
Chao Yue2b4e2b2015-08-19 19:11:19 +08001723 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001724 F2FS_GET_BLOCK_BMAP, NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001725 NO_CHECK_TYPE, create);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001726}
1727
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001728static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1729{
1730 return (offset >> inode->i_blkbits);
1731}
1732
1733static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1734{
1735 return (blk << inode->i_blkbits);
1736}
1737
Chao Yu442a9db2018-01-11 14:39:57 +08001738static int f2fs_xattr_fiemap(struct inode *inode,
1739 struct fiemap_extent_info *fieinfo)
1740{
1741 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1742 struct page *page;
1743 struct node_info ni;
1744 __u64 phys = 0, len;
1745 __u32 flags;
1746 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1747 int err = 0;
1748
1749 if (f2fs_has_inline_xattr(inode)) {
1750 int offset;
1751
1752 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1753 inode->i_ino, false);
1754 if (!page)
1755 return -ENOMEM;
1756
Chao Yu77357302018-07-17 00:02:17 +08001757 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1758 if (err) {
1759 f2fs_put_page(page, 1);
1760 return err;
1761 }
Chao Yu442a9db2018-01-11 14:39:57 +08001762
1763 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1764 offset = offsetof(struct f2fs_inode, i_addr) +
1765 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yub323fd22018-01-17 16:31:36 +08001766 get_inline_xattr_addrs(inode));
Chao Yu442a9db2018-01-11 14:39:57 +08001767
1768 phys += offset;
1769 len = inline_xattr_size(inode);
1770
1771 f2fs_put_page(page, 1);
1772
1773 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1774
1775 if (!xnid)
1776 flags |= FIEMAP_EXTENT_LAST;
1777
1778 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1779 if (err || err == 1)
1780 return err;
1781 }
1782
1783 if (xnid) {
1784 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1785 if (!page)
1786 return -ENOMEM;
1787
Chao Yu77357302018-07-17 00:02:17 +08001788 err = f2fs_get_node_info(sbi, xnid, &ni);
1789 if (err) {
1790 f2fs_put_page(page, 1);
1791 return err;
1792 }
Chao Yu442a9db2018-01-11 14:39:57 +08001793
1794 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1795 len = inode->i_sb->s_blocksize;
1796
1797 f2fs_put_page(page, 1);
1798
1799 flags = FIEMAP_EXTENT_LAST;
1800 }
1801
1802 if (phys)
1803 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1804
1805 return (err < 0 ? err : 0);
1806}
1807
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001808int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1809 u64 start, u64 len)
1810{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001811 struct buffer_head map_bh;
1812 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001813 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001814 u64 logical = 0, phys = 0, size = 0;
1815 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001816 int ret = 0;
1817
Chao Yuc4020b22018-01-11 14:42:30 +08001818 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1819 ret = f2fs_precache_extents(inode);
1820 if (ret)
1821 return ret;
1822 }
1823
Chao Yu442a9db2018-01-11 14:39:57 +08001824 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001825 if (ret)
1826 return ret;
1827
Chao Yuf1b43d42018-01-11 14:37:35 +08001828 inode_lock(inode);
1829
Chao Yu442a9db2018-01-11 14:39:57 +08001830 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1831 ret = f2fs_xattr_fiemap(inode, fieinfo);
1832 goto out;
1833 }
1834
Chao Yu7975f342019-07-22 18:03:50 +08001835 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001836 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1837 if (ret != -EAGAIN)
Chao Yuf1b43d42018-01-11 14:37:35 +08001838 goto out;
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001839 }
1840
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001841 if (logical_to_blk(inode, len) == 0)
1842 len = blk_to_logical(inode, 1);
1843
1844 start_blk = logical_to_blk(inode, start);
1845 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001846
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001847next:
1848 memset(&map_bh, 0, sizeof(struct buffer_head));
1849 map_bh.b_size = len;
1850
Chao Yue2b4e2b2015-08-19 19:11:19 +08001851 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001852 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001853 if (ret)
1854 goto out;
1855
1856 /* HOLE */
1857 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001858 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001859
1860 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1861 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001862 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001863
Fan Li9a950d52015-12-26 18:07:41 +08001864 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001865 }
Fan Li9a950d52015-12-26 18:07:41 +08001866
Chao Yuda5af122016-01-08 20:19:27 +08001867 if (size) {
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301868 if (IS_ENCRYPTED(inode))
Chao Yuda5af122016-01-08 20:19:27 +08001869 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1870
Fan Li9a950d52015-12-26 18:07:41 +08001871 ret = fiemap_fill_next_extent(fieinfo, logical,
1872 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001873 }
Fan Li9a950d52015-12-26 18:07:41 +08001874
1875 if (start_blk > last_blk || ret)
1876 goto out;
1877
1878 logical = blk_to_logical(inode, start_blk);
1879 phys = blk_to_logical(inode, map_bh.b_blocknr);
1880 size = map_bh.b_size;
1881 flags = 0;
1882 if (buffer_unwritten(&map_bh))
1883 flags = FIEMAP_EXTENT_UNWRITTEN;
1884
1885 start_blk += logical_to_blk(inode, size);
1886
1887prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001888 cond_resched();
1889 if (fatal_signal_pending(current))
1890 ret = -EINTR;
1891 else
1892 goto next;
1893out:
1894 if (ret == 1)
1895 ret = 0;
1896
Al Viro59551022016-01-22 15:40:57 -05001897 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001898 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001899}
1900
Eric Biggers95ae2512019-07-22 09:26:24 -07001901static inline loff_t f2fs_readpage_limit(struct inode *inode)
1902{
1903 if (IS_ENABLED(CONFIG_FS_VERITY) &&
1904 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1905 return inode->i_sb->s_maxbytes;
1906
1907 return i_size_read(inode);
1908}
1909
Chao Yu2df0ab02019-03-25 21:07:30 +08001910static int f2fs_read_single_page(struct inode *inode, struct page *page,
1911 unsigned nr_pages,
1912 struct f2fs_map_blocks *map,
1913 struct bio **bio_ret,
1914 sector_t *last_block_in_bio,
1915 bool is_readahead)
1916{
1917 struct bio *bio = *bio_ret;
1918 const unsigned blkbits = inode->i_blkbits;
1919 const unsigned blocksize = 1 << blkbits;
1920 sector_t block_in_file;
1921 sector_t last_block;
1922 sector_t last_block_in_file;
1923 sector_t block_nr;
1924 int ret = 0;
1925
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001926 block_in_file = (sector_t)page_index(page);
Chao Yu2df0ab02019-03-25 21:07:30 +08001927 last_block = block_in_file + nr_pages;
Eric Biggers95ae2512019-07-22 09:26:24 -07001928 last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
Chao Yu2df0ab02019-03-25 21:07:30 +08001929 blkbits;
1930 if (last_block > last_block_in_file)
1931 last_block = last_block_in_file;
1932
1933 /* just zeroing out page which is beyond EOF */
1934 if (block_in_file >= last_block)
1935 goto zero_out;
1936 /*
1937 * Map blocks using the previous result first.
1938 */
1939 if ((map->m_flags & F2FS_MAP_MAPPED) &&
1940 block_in_file > map->m_lblk &&
1941 block_in_file < (map->m_lblk + map->m_len))
1942 goto got_it;
1943
1944 /*
1945 * Then do more f2fs_map_blocks() calls until we are
1946 * done with this page.
1947 */
1948 map->m_lblk = block_in_file;
1949 map->m_len = last_block - block_in_file;
1950
1951 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
1952 if (ret)
1953 goto out;
1954got_it:
1955 if ((map->m_flags & F2FS_MAP_MAPPED)) {
1956 block_nr = map->m_pblk + block_in_file - map->m_lblk;
1957 SetPageMappedToDisk(page);
1958
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001959 if (!PageUptodate(page) && (!PageSwapCache(page) &&
1960 !cleancache_get_page(page))) {
Chao Yu2df0ab02019-03-25 21:07:30 +08001961 SetPageUptodate(page);
1962 goto confused;
1963 }
1964
1965 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
Chao Yu93770ab2019-04-15 15:26:32 +08001966 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001967 ret = -EFSCORRUPTED;
Chao Yu2df0ab02019-03-25 21:07:30 +08001968 goto out;
1969 }
1970 } else {
1971zero_out:
1972 zero_user_segment(page, 0, PAGE_SIZE);
Eric Biggers95ae2512019-07-22 09:26:24 -07001973 if (f2fs_need_verity(inode, page->index) &&
1974 !fsverity_verify_page(page)) {
1975 ret = -EIO;
1976 goto out;
1977 }
Chao Yu2df0ab02019-03-25 21:07:30 +08001978 if (!PageUptodate(page))
1979 SetPageUptodate(page);
1980 unlock_page(page);
1981 goto out;
1982 }
1983
1984 /*
1985 * This page will go to BIO. Do we need to send this
1986 * BIO off first?
1987 */
Chao Yu8896cbd2019-07-12 16:55:41 +08001988 if (bio && !page_is_mergeable(F2FS_I_SB(inode), bio,
1989 *last_block_in_bio, block_nr)) {
Chao Yu2df0ab02019-03-25 21:07:30 +08001990submit_and_realloc:
1991 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1992 bio = NULL;
1993 }
1994 if (bio == NULL) {
1995 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
Chao Yu06837282020-02-18 18:21:35 +08001996 is_readahead ? REQ_RAHEAD : 0, page->index,
1997 false);
Chao Yu2df0ab02019-03-25 21:07:30 +08001998 if (IS_ERR(bio)) {
1999 ret = PTR_ERR(bio);
2000 bio = NULL;
2001 goto out;
2002 }
2003 }
2004
2005 /*
2006 * If the page is under writeback, we need to wait for
2007 * its completion to see the correct decrypted data.
2008 */
2009 f2fs_wait_on_block_writeback(inode, block_nr);
2010
2011 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2012 goto submit_and_realloc;
2013
2014 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2015 ClearPageError(page);
2016 *last_block_in_bio = block_nr;
2017 goto out;
2018confused:
2019 if (bio) {
2020 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2021 bio = NULL;
2022 }
2023 unlock_page(page);
2024out:
2025 *bio_ret = bio;
2026 return ret;
2027}
2028
Chao Yu4c8ff702019-11-01 18:07:14 +08002029#ifdef CONFIG_F2FS_FS_COMPRESSION
2030int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2031 unsigned nr_pages, sector_t *last_block_in_bio,
Chao Yu06837282020-02-18 18:21:35 +08002032 bool is_readahead, bool for_write)
Chao Yu4c8ff702019-11-01 18:07:14 +08002033{
2034 struct dnode_of_data dn;
2035 struct inode *inode = cc->inode;
2036 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2037 struct bio *bio = *bio_ret;
2038 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2039 sector_t last_block_in_file;
2040 const unsigned blkbits = inode->i_blkbits;
2041 const unsigned blocksize = 1 << blkbits;
2042 struct decompress_io_ctx *dic = NULL;
2043 int i;
2044 int ret = 0;
2045
2046 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2047
2048 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
2049
2050 /* get rid of pages beyond EOF */
2051 for (i = 0; i < cc->cluster_size; i++) {
2052 struct page *page = cc->rpages[i];
2053
2054 if (!page)
2055 continue;
2056 if ((sector_t)page->index >= last_block_in_file) {
2057 zero_user_segment(page, 0, PAGE_SIZE);
2058 if (!PageUptodate(page))
2059 SetPageUptodate(page);
2060 } else if (!PageUptodate(page)) {
2061 continue;
2062 }
2063 unlock_page(page);
2064 cc->rpages[i] = NULL;
2065 cc->nr_rpages--;
2066 }
2067
2068 /* we are done since all pages are beyond EOF */
2069 if (f2fs_cluster_is_empty(cc))
2070 goto out;
2071
2072 set_new_dnode(&dn, inode, NULL, NULL, 0);
2073 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2074 if (ret)
2075 goto out;
2076
2077 /* cluster was overwritten as normal cluster */
2078 if (dn.data_blkaddr != COMPRESS_ADDR)
2079 goto out;
2080
2081 for (i = 1; i < cc->cluster_size; i++) {
2082 block_t blkaddr;
2083
Chao Yua2ced1c2020-02-14 17:44:10 +08002084 blkaddr = data_blkaddr(dn.inode, dn.node_page,
Chao Yu4c8ff702019-11-01 18:07:14 +08002085 dn.ofs_in_node + i);
2086
2087 if (!__is_valid_data_blkaddr(blkaddr))
2088 break;
2089
2090 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2091 ret = -EFAULT;
2092 goto out_put_dnode;
2093 }
2094 cc->nr_cpages++;
2095 }
2096
2097 /* nothing to decompress */
2098 if (cc->nr_cpages == 0) {
2099 ret = 0;
2100 goto out_put_dnode;
2101 }
2102
2103 dic = f2fs_alloc_dic(cc);
2104 if (IS_ERR(dic)) {
2105 ret = PTR_ERR(dic);
2106 goto out_put_dnode;
2107 }
2108
2109 for (i = 0; i < dic->nr_cpages; i++) {
2110 struct page *page = dic->cpages[i];
2111 block_t blkaddr;
2112
Chao Yua2ced1c2020-02-14 17:44:10 +08002113 blkaddr = data_blkaddr(dn.inode, dn.node_page,
Chao Yu4c8ff702019-11-01 18:07:14 +08002114 dn.ofs_in_node + i + 1);
2115
2116 if (bio && !page_is_mergeable(sbi, bio,
2117 *last_block_in_bio, blkaddr)) {
2118submit_and_realloc:
2119 __submit_bio(sbi, bio, DATA);
2120 bio = NULL;
2121 }
2122
2123 if (!bio) {
2124 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2125 is_readahead ? REQ_RAHEAD : 0,
Chao Yu06837282020-02-18 18:21:35 +08002126 page->index, for_write);
Chao Yu4c8ff702019-11-01 18:07:14 +08002127 if (IS_ERR(bio)) {
2128 ret = PTR_ERR(bio);
2129 bio = NULL;
2130 dic->failed = true;
2131 if (refcount_sub_and_test(dic->nr_cpages - i,
2132 &dic->ref))
2133 f2fs_decompress_end_io(dic->rpages,
2134 cc->cluster_size, true,
2135 false);
2136 f2fs_free_dic(dic);
2137 f2fs_put_dnode(&dn);
2138 *bio_ret = bio;
2139 return ret;
2140 }
2141 }
2142
2143 f2fs_wait_on_block_writeback(inode, blkaddr);
2144
2145 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2146 goto submit_and_realloc;
2147
2148 inc_page_count(sbi, F2FS_RD_DATA);
2149 ClearPageError(page);
2150 *last_block_in_bio = blkaddr;
2151 }
2152
2153 f2fs_put_dnode(&dn);
2154
2155 *bio_ret = bio;
2156 return 0;
2157
2158out_put_dnode:
2159 f2fs_put_dnode(&dn);
2160out:
2161 f2fs_decompress_end_io(cc->rpages, cc->cluster_size, true, false);
2162 *bio_ret = bio;
2163 return ret;
2164}
2165#endif
2166
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002167/*
2168 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2169 * Major change was from block_size == page_size in f2fs by default.
Jaegeuk Kime2e59412018-06-21 11:29:43 -07002170 *
2171 * Note that the aops->readpages() function is ONLY used for read-ahead. If
2172 * this function ever deviates from doing just read-ahead, it should either
2173 * use ->readpage() or do the necessary surgery to decouple ->readpages()
2174 * from read-ahead.
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002175 */
Chao Yu4c8ff702019-11-01 18:07:14 +08002176int f2fs_mpage_readpages(struct address_space *mapping,
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002177 struct list_head *pages, struct page *page,
Jaegeuk Kime2e59412018-06-21 11:29:43 -07002178 unsigned nr_pages, bool is_readahead)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002179{
2180 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002181 sector_t last_block_in_bio = 0;
2182 struct inode *inode = mapping->host;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002183 struct f2fs_map_blocks map;
Chao Yu4c8ff702019-11-01 18:07:14 +08002184#ifdef CONFIG_F2FS_FS_COMPRESSION
2185 struct compress_ctx cc = {
2186 .inode = inode,
2187 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2188 .cluster_size = F2FS_I(inode)->i_cluster_size,
2189 .cluster_idx = NULL_CLUSTER,
2190 .rpages = NULL,
2191 .cpages = NULL,
2192 .nr_rpages = 0,
2193 .nr_cpages = 0,
2194 };
2195#endif
2196 unsigned max_nr_pages = nr_pages;
Chao Yu2df0ab02019-03-25 21:07:30 +08002197 int ret = 0;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002198
2199 map.m_pblk = 0;
2200 map.m_lblk = 0;
2201 map.m_len = 0;
2202 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08002203 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08002204 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09002205 map.m_seg_type = NO_CHECK_TYPE;
Chao Yuf9d6d052018-11-13 14:33:45 +08002206 map.m_may_create = false;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002207
LiFan736c0a72017-11-25 11:46:18 +08002208 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002209 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08002210 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08002211
2212 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002213 list_del(&page->lru);
2214 if (add_to_page_cache_lru(page, mapping,
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002215 page_index(page),
Michal Hocko8a5c7432016-07-26 15:24:53 -07002216 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002217 goto next_page;
2218 }
2219
Chao Yu4c8ff702019-11-01 18:07:14 +08002220#ifdef CONFIG_F2FS_FS_COMPRESSION
2221 if (f2fs_compressed_file(inode)) {
2222 /* there are remained comressed pages, submit them */
2223 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2224 ret = f2fs_read_multi_pages(&cc, &bio,
2225 max_nr_pages,
2226 &last_block_in_bio,
Chao Yu06837282020-02-18 18:21:35 +08002227 is_readahead, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08002228 f2fs_destroy_compress_ctx(&cc);
2229 if (ret)
2230 goto set_error_page;
2231 }
2232 ret = f2fs_is_compressed_cluster(inode, page->index);
2233 if (ret < 0)
2234 goto set_error_page;
2235 else if (!ret)
2236 goto read_single_page;
2237
2238 ret = f2fs_init_compress_ctx(&cc);
2239 if (ret)
2240 goto set_error_page;
2241
2242 f2fs_compress_ctx_add_page(&cc, page);
2243
2244 goto next_page;
2245 }
2246read_single_page:
2247#endif
2248
2249 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2250 &bio, &last_block_in_bio, is_readahead);
Chao Yu2df0ab02019-03-25 21:07:30 +08002251 if (ret) {
Chao Yu4c8ff702019-11-01 18:07:14 +08002252#ifdef CONFIG_F2FS_FS_COMPRESSION
2253set_error_page:
2254#endif
Chao Yu2df0ab02019-03-25 21:07:30 +08002255 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002256 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002257 unlock_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002258 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002259next_page:
2260 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002261 put_page(page);
Chao Yu4c8ff702019-11-01 18:07:14 +08002262
2263#ifdef CONFIG_F2FS_FS_COMPRESSION
2264 if (f2fs_compressed_file(inode)) {
2265 /* last page */
2266 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2267 ret = f2fs_read_multi_pages(&cc, &bio,
2268 max_nr_pages,
2269 &last_block_in_bio,
Chao Yu06837282020-02-18 18:21:35 +08002270 is_readahead, false);
Chao Yu4c8ff702019-11-01 18:07:14 +08002271 f2fs_destroy_compress_ctx(&cc);
2272 }
2273 }
2274#endif
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002275 }
2276 BUG_ON(pages && !list_empty(pages));
2277 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07002278 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Chao Yu2df0ab02019-03-25 21:07:30 +08002279 return pages ? 0 : ret;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002280}
2281
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002282static int f2fs_read_data_page(struct file *file, struct page *page)
2283{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002284 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002285 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002286
Chao Yuc20e89c2014-05-06 16:53:08 +08002287 trace_f2fs_readpage(page, DATA);
2288
Chao Yu4c8ff702019-11-01 18:07:14 +08002289 if (!f2fs_is_compress_backend_ready(inode)) {
2290 unlock_page(page);
2291 return -EOPNOTSUPP;
2292 }
2293
arter97e1c42042014-08-06 23:22:50 +09002294 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002295 if (f2fs_has_inline_data(inode))
2296 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002297 if (ret == -EAGAIN)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002298 ret = f2fs_mpage_readpages(page_file_mapping(page),
2299 NULL, page, 1, false);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002300 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002301}
2302
2303static int f2fs_read_data_pages(struct file *file,
2304 struct address_space *mapping,
2305 struct list_head *pages, unsigned nr_pages)
2306{
Hsiang Kao71cb4af2017-09-24 02:45:42 +08002307 struct inode *inode = mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08002308 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08002309
2310 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002311
Chao Yu4c8ff702019-11-01 18:07:14 +08002312 if (!f2fs_is_compress_backend_ready(inode))
2313 return 0;
2314
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002315 /* If the file has inline data, skip readpages */
2316 if (f2fs_has_inline_data(inode))
2317 return 0;
2318
Jaegeuk Kime2e59412018-06-21 11:29:43 -07002319 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002320}
2321
Chao Yu4c8ff702019-11-01 18:07:14 +08002322int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002323{
2324 struct inode *inode = fio->page->mapping->host;
Chao Yu4c8ff702019-11-01 18:07:14 +08002325 struct page *mpage, *page;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002326 gfp_t gfp_flags = GFP_NOFS;
2327
Jaegeuk Kim19585932017-09-05 16:54:24 -07002328 if (!f2fs_encrypted_file(inode))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002329 return 0;
2330
Chao Yu4c8ff702019-11-01 18:07:14 +08002331 page = fio->compressed_page ? fio->compressed_page : fio->page;
2332
Eric Biggers6dbb1792018-04-18 11:09:48 -07002333 /* wait for GCed page writeback via META_MAPPING */
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07002334 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002335
2336retry_encrypt:
Chao Yu4c8ff702019-11-01 18:07:14 +08002337 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2338 PAGE_SIZE, 0, gfp_flags);
Chao Yu6aa58d82018-08-14 22:37:25 +08002339 if (IS_ERR(fio->encrypted_page)) {
2340 /* flush pending IOs and wait for a while in the ENOMEM case */
2341 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2342 f2fs_flush_merged_writes(fio->sbi);
Chao Yu5df7731f2020-02-17 17:45:44 +08002343 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
Chao Yu6aa58d82018-08-14 22:37:25 +08002344 gfp_flags |= __GFP_NOFAIL;
2345 goto retry_encrypt;
2346 }
2347 return PTR_ERR(fio->encrypted_page);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002348 }
Chao Yu6aa58d82018-08-14 22:37:25 +08002349
2350 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2351 if (mpage) {
2352 if (PageUptodate(mpage))
2353 memcpy(page_address(mpage),
2354 page_address(fio->encrypted_page), PAGE_SIZE);
2355 f2fs_put_page(mpage, 1);
2356 }
2357 return 0;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002358}
2359
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002360static inline bool check_inplace_update_policy(struct inode *inode,
2361 struct f2fs_io_info *fio)
2362{
2363 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2364 unsigned int policy = SM_I(sbi)->ipu_policy;
2365
2366 if (policy & (0x1 << F2FS_IPU_FORCE))
2367 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08002368 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002369 return true;
2370 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2371 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2372 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08002373 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002374 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2375 return true;
2376
2377 /*
2378 * IPU for rewrite async pages
2379 */
2380 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2381 fio && fio->op == REQ_OP_WRITE &&
2382 !(fio->op_flags & REQ_SYNC) &&
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302383 !IS_ENCRYPTED(inode))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002384 return true;
2385
2386 /* this is only set during fdatasync */
2387 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2388 is_inode_flag_set(inode, FI_NEED_IPU))
2389 return true;
2390
Daniel Rosenberg43549942018-08-20 19:21:43 -07002391 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2392 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2393 return true;
2394
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002395 return false;
2396}
2397
Chao Yu4d57b862018-05-30 00:20:41 +08002398bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002399{
2400 if (f2fs_is_pinned_file(inode))
2401 return true;
2402
2403 /* if this is cold file, we should overwrite to avoid fragmentation */
2404 if (file_is_cold(inode))
2405 return true;
2406
2407 return check_inplace_update_policy(inode, fio);
2408}
2409
Chao Yu4d57b862018-05-30 00:20:41 +08002410bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002411{
2412 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2413
Chao Yub0332a02020-02-14 17:44:12 +08002414 if (f2fs_lfs_mode(sbi))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002415 return true;
2416 if (S_ISDIR(inode->i_mode))
2417 return true;
Chao Yuaf033b22018-09-20 20:05:00 +08002418 if (IS_NOQUOTA(inode))
2419 return true;
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002420 if (f2fs_is_atomic_file(inode))
2421 return true;
2422 if (fio) {
2423 if (is_cold_data(fio->page))
2424 return true;
2425 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2426 return true;
Daniel Rosenberg43549942018-08-20 19:21:43 -07002427 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2428 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2429 return true;
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002430 }
2431 return false;
2432}
2433
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002434static inline bool need_inplace_update(struct f2fs_io_info *fio)
2435{
2436 struct inode *inode = fio->page->mapping->host;
2437
Chao Yu4d57b862018-05-30 00:20:41 +08002438 if (f2fs_should_update_outplace(inode, fio))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002439 return false;
2440
Chao Yu4d57b862018-05-30 00:20:41 +08002441 return f2fs_should_update_inplace(inode, fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002442}
2443
Chao Yu4d57b862018-05-30 00:20:41 +08002444int f2fs_do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002445{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002446 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002447 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002448 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002449 struct extent_info ei = {0,0,0};
Chao Yu77357302018-07-17 00:02:17 +08002450 struct node_info ni;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002451 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002452 int err = 0;
2453
2454 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00002455 if (need_inplace_update(fio) &&
2456 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2457 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07002458
Chao Yuc9b60782018-08-01 19:13:44 +08002459 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08002460 DATA_GENERIC_ENHANCE))
Chao Yu10f966b2019-06-20 11:36:14 +08002461 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08002462
2463 ipu_force = true;
2464 fio->need_lock = LOCK_DONE;
2465 goto got_it;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002466 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002467
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002468 /* Deadlock due to between page->lock and f2fs_lock_op */
2469 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2470 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08002471
Chao Yu4d57b862018-05-30 00:20:41 +08002472 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002473 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002474 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002475
Chao Yu28bc1062016-02-06 14:40:34 +08002476 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002477
2478 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08002479 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002480 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +08002481 clear_cold_data(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002482 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002483 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00002484got_it:
Chao Yuc9b60782018-08-01 19:13:44 +08002485 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2486 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08002487 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08002488 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08002489 goto out_writepage;
2490 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002491 /*
2492 * If current allocation needs SSR,
2493 * it had better in-place writes for updated data.
2494 */
Chao Yu93770ab2019-04-15 15:26:32 +08002495 if (ipu_force ||
2496 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
Chao Yu7b525dd2018-05-23 22:25:08 +08002497 need_inplace_update(fio))) {
Chao Yu4c8ff702019-11-01 18:07:14 +08002498 err = f2fs_encrypt_one_page(fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002499 if (err)
2500 goto out_writepage;
2501
2502 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07002503 ClearPageError(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002504 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002505 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002506 f2fs_unlock_op(fio->sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08002507 err = f2fs_inplace_write_data(fio);
Chao Yu6492a332019-02-21 20:37:14 +08002508 if (err) {
2509 if (f2fs_encrypted_file(inode))
Eric Biggersd2d07272019-05-20 09:29:39 -07002510 fscrypt_finalize_bounce_page(&fio->encrypted_page);
Chao Yu6492a332019-02-21 20:37:14 +08002511 if (PageWriteback(page))
2512 end_page_writeback(page);
Chao Yucd23ffa92019-04-15 15:30:53 +08002513 } else {
2514 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu6492a332019-02-21 20:37:14 +08002515 }
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002516 trace_f2fs_do_write_data_page(fio->page, IPU);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002517 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002518 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002519
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002520 if (fio->need_lock == LOCK_RETRY) {
2521 if (!f2fs_trylock_op(fio->sbi)) {
2522 err = -EAGAIN;
2523 goto out_writepage;
2524 }
2525 fio->need_lock = LOCK_REQ;
2526 }
2527
Chao Yu77357302018-07-17 00:02:17 +08002528 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2529 if (err)
2530 goto out_writepage;
2531
2532 fio->version = ni.version;
2533
Chao Yu4c8ff702019-11-01 18:07:14 +08002534 err = f2fs_encrypt_one_page(fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002535 if (err)
2536 goto out_writepage;
2537
2538 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07002539 ClearPageError(page);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002540
Chao Yu4c8ff702019-11-01 18:07:14 +08002541 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2542 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2543
Hou Pengyang279d6df2017-04-27 00:17:21 +08002544 /* LFS mode write path */
Chao Yu4d57b862018-05-30 00:20:41 +08002545 f2fs_outplace_write_data(&dn, fio);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002546 trace_f2fs_do_write_data_page(page, OPU);
2547 set_inode_flag(inode, FI_APPEND_WRITE);
2548 if (page->index == 0)
2549 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002550out_writepage:
2551 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002552out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002553 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002554 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002555 return err;
2556}
2557
Chao Yu4c8ff702019-11-01 18:07:14 +08002558int f2fs_write_single_data_page(struct page *page, int *submitted,
Chao Yu8648de22019-02-19 16:15:29 +08002559 struct bio **bio,
2560 sector_t *last_block,
Chao Yub0af6d42017-08-02 23:21:48 +08002561 struct writeback_control *wbc,
Chao Yu4c8ff702019-11-01 18:07:14 +08002562 enum iostat_type io_type,
2563 int compr_blocks)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002564{
2565 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002566 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002567 loff_t i_size = i_size_read(inode);
Chao Yu4c8ff702019-11-01 18:07:14 +08002568 const pgoff_t end_index = ((unsigned long long)i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002569 >> PAGE_SHIFT;
Chao Yu1f0d5c92019-11-07 17:29:00 +08002570 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002571 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09002572 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002573 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002574 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002575 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08002576 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002577 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05002578 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06002579 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00002580 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002581 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002582 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002583 .submitted = false,
Chao Yu4c8ff702019-11-01 18:07:14 +08002584 .compr_blocks = compr_blocks,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002585 .need_lock = LOCK_RETRY,
Chao Yub0af6d42017-08-02 23:21:48 +08002586 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08002587 .io_wbc = wbc,
Chao Yu8648de22019-02-19 16:15:29 +08002588 .bio = bio,
2589 .last_block = last_block,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002590 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002591
Chao Yuecda0de2014-05-06 16:48:26 +08002592 trace_f2fs_writepage(page, DATA);
2593
Chao Yudb198ae2018-01-18 17:29:10 +08002594 /* we should bypass data pages to proceed the kworkder jobs */
2595 if (unlikely(f2fs_cp_error(sbi))) {
2596 mapping_set_error(page->mapping, -EIO);
Chao Yu1174abf2018-05-28 16:59:26 +08002597 /*
2598 * don't drop any dirty dentry pages for keeping lastest
2599 * directory structure.
2600 */
2601 if (S_ISDIR(inode->i_mode))
2602 goto redirty_out;
Chao Yudb198ae2018-01-18 17:29:10 +08002603 goto out;
2604 }
2605
Chao Yu0771fcc2017-06-29 23:20:45 +08002606 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2607 goto redirty_out;
2608
Chao Yu4c8ff702019-11-01 18:07:14 +08002609 if (page->index < end_index ||
2610 f2fs_verity_in_progress(inode) ||
2611 compr_blocks)
Jaegeuk Kim39936832012-11-22 16:21:29 +09002612 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002613
2614 /*
2615 * If the offset is out-of-range of file size,
2616 * this page does not have to be written to disk.
2617 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002618 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002619 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09002620 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002621
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002622 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09002623write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002624 if (f2fs_is_drop_cache(inode))
2625 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07002626 /* we should not write 0'th page having journal header */
2627 if (f2fs_is_volatile_file(inode) && (!page->index ||
2628 (!wbc->for_reclaim &&
Chao Yu4d57b862018-05-30 00:20:41 +08002629 f2fs_available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002630 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002631
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002632 /* Dentry blocks are controlled by checkpoint */
2633 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002634 fio.need_lock = LOCK_DONE;
Chao Yu4d57b862018-05-30 00:20:41 +08002635 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002636 goto done;
2637 }
2638
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002639 if (!wbc->for_reclaim)
2640 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002641 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09002642 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002643 else
2644 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002645
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002646 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08002647 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002648 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08002649 if (!err)
2650 goto out;
2651 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002652
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002653 if (err == -EAGAIN) {
Chao Yu4d57b862018-05-30 00:20:41 +08002654 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002655 if (err == -EAGAIN) {
2656 fio.need_lock = LOCK_REQ;
Chao Yu4d57b862018-05-30 00:20:41 +08002657 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002658 }
2659 }
Chao Yua0d00fa2017-10-09 17:55:19 +08002660
Chao Yueb449792018-01-17 16:31:37 +08002661 if (err) {
2662 file_set_keep_isize(inode);
2663 } else {
Chao Yuc10c9822020-02-27 19:30:03 +08002664 spin_lock(&F2FS_I(inode)->i_size_lock);
Chao Yueb449792018-01-17 16:31:37 +08002665 if (F2FS_I(inode)->last_disk_size < psize)
2666 F2FS_I(inode)->last_disk_size = psize;
Chao Yuc10c9822020-02-27 19:30:03 +08002667 spin_unlock(&F2FS_I(inode)->i_size_lock);
Chao Yueb449792018-01-17 16:31:37 +08002668 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002669
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002670done:
2671 if (err && err != -ENOENT)
2672 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002673
Jaegeuk Kim39936832012-11-22 16:21:29 +09002674out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002675 inode_dec_dirty_pages(inode);
Chao Yu2baf0782018-07-27 18:15:16 +08002676 if (err) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002677 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +08002678 clear_cold_data(page);
2679 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002680
2681 if (wbc->for_reclaim) {
Chao Yubab475c2018-09-27 23:41:16 +08002682 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002683 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu4d57b862018-05-30 00:20:41 +08002684 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002685 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08002686 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002687 unlock_page(page);
Chao Yu186857c2019-04-02 18:52:19 +08002688 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
Chao Yu0b20fce2019-09-30 18:53:25 +08002689 !F2FS_I(inode)->cp_task)
Jaegeuk Kima7881892017-04-20 13:51:57 -07002690 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08002691
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002692 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002693 f2fs_submit_merged_write(sbi, DATA);
Chao Yu0b20fce2019-09-30 18:53:25 +08002694 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002695 submitted = NULL;
2696 }
2697
2698 if (submitted)
Chao Yu4c8ff702019-11-01 18:07:14 +08002699 *submitted = fio.submitted ? 1 : 0;
Chao Yu0c3a5792016-01-18 18:28:11 +08002700
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002701 return 0;
2702
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002703redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002704 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim5b19d282018-05-03 23:26:02 -07002705 /*
2706 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2707 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2708 * file_write_and_wait_range() will see EIO error, which is critical
2709 * to return value of fsync() followed by atomic_write failure to user.
2710 */
2711 if (!err || wbc->for_reclaim)
Chao Yu0002b612016-11-28 19:13:43 -08002712 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002713 unlock_page(page);
2714 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09002715}
2716
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002717static int f2fs_write_data_page(struct page *page,
2718 struct writeback_control *wbc)
2719{
Chao Yu4c8ff702019-11-01 18:07:14 +08002720#ifdef CONFIG_F2FS_FS_COMPRESSION
2721 struct inode *inode = page->mapping->host;
2722
2723 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2724 goto out;
2725
2726 if (f2fs_compressed_file(inode)) {
2727 if (f2fs_is_compressed_cluster(inode, page->index)) {
2728 redirty_page_for_writepage(wbc, page);
2729 return AOP_WRITEPAGE_ACTIVATE;
2730 }
2731 }
2732out:
2733#endif
2734
2735 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2736 wbc, FS_DATA_IO, 0);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002737}
2738
Chao Yu8f46dca2015-07-14 18:56:10 +08002739/*
2740 * This function was copied from write_cche_pages from mm/page-writeback.c.
2741 * The major change is making write step of cold data page separately from
2742 * warm/hot data page.
2743 */
2744static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08002745 struct writeback_control *wbc,
2746 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08002747{
2748 int ret = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08002749 int done = 0, retry = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08002750 struct pagevec pvec;
Chao Yuc29fd0c2018-06-04 23:20:36 +08002751 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Chao Yu8648de22019-02-19 16:15:29 +08002752 struct bio *bio = NULL;
2753 sector_t last_block;
Chao Yu4c8ff702019-11-01 18:07:14 +08002754#ifdef CONFIG_F2FS_FS_COMPRESSION
2755 struct inode *inode = mapping->host;
2756 struct compress_ctx cc = {
2757 .inode = inode,
2758 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2759 .cluster_size = F2FS_I(inode)->i_cluster_size,
2760 .cluster_idx = NULL_CLUSTER,
2761 .rpages = NULL,
2762 .nr_rpages = 0,
2763 .cpages = NULL,
2764 .rbuf = NULL,
2765 .cbuf = NULL,
2766 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2767 .private = NULL,
2768 };
2769#endif
Chao Yu8f46dca2015-07-14 18:56:10 +08002770 int nr_pages;
2771 pgoff_t uninitialized_var(writeback_index);
2772 pgoff_t index;
2773 pgoff_t end; /* Inclusive */
2774 pgoff_t done_index;
2775 int cycled;
2776 int range_whole = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05002777 xa_mark_t tag;
Chao Yubab475c2018-09-27 23:41:16 +08002778 int nwritten = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08002779 int submitted = 0;
2780 int i;
Chao Yu8f46dca2015-07-14 18:56:10 +08002781
Mel Gorman86679822017-11-15 17:37:52 -08002782 pagevec_init(&pvec);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07002783
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002784 if (get_dirty_pages(mapping->host) <=
2785 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2786 set_inode_flag(mapping->host, FI_HOT_DATA);
2787 else
2788 clear_inode_flag(mapping->host, FI_HOT_DATA);
2789
Chao Yu8f46dca2015-07-14 18:56:10 +08002790 if (wbc->range_cyclic) {
2791 writeback_index = mapping->writeback_index; /* prev offset */
2792 index = writeback_index;
2793 if (index == 0)
2794 cycled = 1;
2795 else
2796 cycled = 0;
2797 end = -1;
2798 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002799 index = wbc->range_start >> PAGE_SHIFT;
2800 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08002801 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2802 range_whole = 1;
2803 cycled = 1; /* ignore range_cyclic tests */
2804 }
2805 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2806 tag = PAGECACHE_TAG_TOWRITE;
2807 else
2808 tag = PAGECACHE_TAG_DIRTY;
2809retry:
Chao Yu4c8ff702019-11-01 18:07:14 +08002810 retry = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08002811 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2812 tag_pages_for_writeback(mapping, index, end);
2813 done_index = index;
Chao Yu4c8ff702019-11-01 18:07:14 +08002814 while (!done && !retry && (index <= end)) {
Jan Kara69c4f352017-11-15 17:34:48 -08002815 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08002816 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08002817 if (nr_pages == 0)
2818 break;
2819
2820 for (i = 0; i < nr_pages; i++) {
2821 struct page *page = pvec.pages[i];
Chao Yu4c8ff702019-11-01 18:07:14 +08002822 bool need_readd;
2823readd:
2824 need_readd = false;
2825#ifdef CONFIG_F2FS_FS_COMPRESSION
2826 if (f2fs_compressed_file(inode)) {
2827 ret = f2fs_init_compress_ctx(&cc);
2828 if (ret) {
2829 done = 1;
2830 break;
2831 }
Chao Yu8f46dca2015-07-14 18:56:10 +08002832
Chao Yu4c8ff702019-11-01 18:07:14 +08002833 if (!f2fs_cluster_can_merge_page(&cc,
2834 page->index)) {
2835 ret = f2fs_write_multi_pages(&cc,
2836 &submitted, wbc, io_type);
2837 if (!ret)
2838 need_readd = true;
2839 goto result;
2840 }
2841
2842 if (unlikely(f2fs_cp_error(sbi)))
2843 goto lock_page;
2844
2845 if (f2fs_cluster_is_empty(&cc)) {
2846 void *fsdata = NULL;
2847 struct page *pagep;
2848 int ret2;
2849
2850 ret2 = f2fs_prepare_compress_overwrite(
2851 inode, &pagep,
2852 page->index, &fsdata);
2853 if (ret2 < 0) {
2854 ret = ret2;
2855 done = 1;
2856 break;
2857 } else if (ret2 &&
2858 !f2fs_compress_write_end(inode,
2859 fsdata, page->index,
2860 1)) {
2861 retry = 1;
2862 break;
2863 }
2864 } else {
2865 goto lock_page;
2866 }
2867 }
2868#endif
Chao Yuf8de4332018-05-23 22:25:09 +08002869 /* give a priority to WB_SYNC threads */
Chao Yuc29fd0c2018-06-04 23:20:36 +08002870 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
Chao Yuf8de4332018-05-23 22:25:09 +08002871 wbc->sync_mode == WB_SYNC_NONE) {
2872 done = 1;
2873 break;
2874 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002875#ifdef CONFIG_F2FS_FS_COMPRESSION
2876lock_page:
2877#endif
Chao Yu8f46dca2015-07-14 18:56:10 +08002878 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002879retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08002880 lock_page(page);
2881
2882 if (unlikely(page->mapping != mapping)) {
2883continue_unlock:
2884 unlock_page(page);
2885 continue;
2886 }
2887
2888 if (!PageDirty(page)) {
2889 /* someone wrote it for us */
2890 goto continue_unlock;
2891 }
2892
Chao Yu8f46dca2015-07-14 18:56:10 +08002893 if (PageWriteback(page)) {
Chao Yu0b20fce2019-09-30 18:53:25 +08002894 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002895 f2fs_wait_on_page_writeback(page,
Chao Yubae0ee72018-12-25 17:43:42 +08002896 DATA, true, true);
Chao Yu0b20fce2019-09-30 18:53:25 +08002897 else
Chao Yu8f46dca2015-07-14 18:56:10 +08002898 goto continue_unlock;
2899 }
2900
Chao Yu8f46dca2015-07-14 18:56:10 +08002901 if (!clear_page_dirty_for_io(page))
2902 goto continue_unlock;
2903
Chao Yu4c8ff702019-11-01 18:07:14 +08002904#ifdef CONFIG_F2FS_FS_COMPRESSION
2905 if (f2fs_compressed_file(inode)) {
2906 get_page(page);
2907 f2fs_compress_ctx_add_page(&cc, page);
2908 continue;
2909 }
2910#endif
2911 ret = f2fs_write_single_data_page(page, &submitted,
2912 &bio, &last_block, wbc, io_type, 0);
2913 if (ret == AOP_WRITEPAGE_ACTIVATE)
2914 unlock_page(page);
2915#ifdef CONFIG_F2FS_FS_COMPRESSION
2916result:
2917#endif
2918 nwritten += submitted;
2919 wbc->nr_to_write -= submitted;
2920
Chao Yu8f46dca2015-07-14 18:56:10 +08002921 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08002922 /*
2923 * keep nr_to_write, since vfs uses this to
2924 * get # of written pages.
2925 */
2926 if (ret == AOP_WRITEPAGE_ACTIVATE) {
Chao Yu0002b612016-11-28 19:13:43 -08002927 ret = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08002928 goto next;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002929 } else if (ret == -EAGAIN) {
2930 ret = 0;
2931 if (wbc->sync_mode == WB_SYNC_ALL) {
2932 cond_resched();
2933 congestion_wait(BLK_RW_ASYNC,
Chao Yu5df7731f2020-02-17 17:45:44 +08002934 DEFAULT_IO_TIMEOUT);
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002935 goto retry_write;
2936 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002937 goto next;
Chao Yu0002b612016-11-28 19:13:43 -08002938 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002939 done_index = page->index + 1;
2940 done = 1;
2941 break;
Chao Yu8f46dca2015-07-14 18:56:10 +08002942 }
2943
Chao Yu4c8ff702019-11-01 18:07:14 +08002944 if (wbc->nr_to_write <= 0 &&
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002945 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08002946 done = 1;
2947 break;
2948 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002949next:
2950 if (need_readd)
2951 goto readd;
Chao Yu8f46dca2015-07-14 18:56:10 +08002952 }
2953 pagevec_release(&pvec);
2954 cond_resched();
2955 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002956#ifdef CONFIG_F2FS_FS_COMPRESSION
2957 /* flush remained pages in compress cluster */
2958 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
2959 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
2960 nwritten += submitted;
2961 wbc->nr_to_write -= submitted;
2962 if (ret) {
2963 done = 1;
2964 retry = 0;
2965 }
2966 }
2967#endif
2968 if ((!cycled && !done) || retry) {
Chao Yu8f46dca2015-07-14 18:56:10 +08002969 cycled = 1;
2970 index = 0;
2971 end = writeback_index - 1;
2972 goto retry;
2973 }
2974 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2975 mapping->writeback_index = done_index;
2976
Chao Yubab475c2018-09-27 23:41:16 +08002977 if (nwritten)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002978 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
Chao Yubab475c2018-09-27 23:41:16 +08002979 NULL, 0, DATA);
Chao Yu8648de22019-02-19 16:15:29 +08002980 /* submit cached bio of IPU write */
2981 if (bio)
Chao Yu0b20fce2019-09-30 18:53:25 +08002982 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
Chao Yu6ca56ca2016-09-29 18:50:11 +08002983
Chao Yu8f46dca2015-07-14 18:56:10 +08002984 return ret;
2985}
2986
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002987static inline bool __should_serialize_io(struct inode *inode,
2988 struct writeback_control *wbc)
2989{
Chao Yu040d2bb2019-05-20 17:36:59 +08002990 /* to avoid deadlock in path of data flush */
2991 if (F2FS_I(inode)->cp_task)
2992 return false;
Chao Yub13f67ff2020-03-19 19:57:57 +08002993
2994 if (!S_ISREG(inode->i_mode))
2995 return false;
2996 if (IS_NOQUOTA(inode))
2997 return false;
2998
2999 if (f2fs_compressed_file(inode))
3000 return true;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003001 if (wbc->sync_mode != WB_SYNC_ALL)
3002 return true;
3003 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3004 return true;
3005 return false;
3006}
3007
Chao Yufc99fe22018-05-30 00:20:39 +08003008static int __f2fs_write_data_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08003009 struct writeback_control *wbc,
3010 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003011{
3012 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07003013 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003014 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003015 int ret;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003016 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003017
P J Pcfb185a2013-04-03 11:38:00 +09003018 /* deal with chardevs and other special file */
3019 if (!mapping->a_ops->writepage)
3020 return 0;
3021
Chao Yu6a290542015-07-17 18:02:39 +08003022 /* skip writing if there is no dirty page in this inode */
3023 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3024 return 0;
3025
Chao Yu0771fcc2017-06-29 23:20:45 +08003026 /* during POR, we don't need to trigger writepage at all. */
3027 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3028 goto skip_write;
3029
Chao Yuaf033b22018-09-20 20:05:00 +08003030 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3031 wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima1257022015-10-08 10:40:07 -07003032 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Chao Yu4d57b862018-05-30 00:20:41 +08003033 f2fs_available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kima1257022015-10-08 10:40:07 -07003034 goto skip_write;
3035
Chao Yud323d002015-10-27 09:53:45 +08003036 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07003037 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08003038 goto skip_write;
3039
Yunlei Hed31c7c32016-02-04 16:14:00 +08003040 trace_f2fs_writepages(mapping->host, wbc, DATA);
3041
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003042 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3043 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08003044 atomic_inc(&sbi->wb_sync_req[DATA]);
3045 else if (atomic_read(&sbi->wb_sync_req[DATA]))
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003046 goto skip_write;
3047
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003048 if (__should_serialize_io(inode, wbc)) {
3049 mutex_lock(&sbi->writepages);
3050 locked = true;
3051 }
3052
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003053 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08003054 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003055 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003056
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003057 if (locked)
3058 mutex_unlock(&sbi->writepages);
3059
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003060 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08003061 atomic_dec(&sbi->wb_sync_req[DATA]);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07003062 /*
3063 * if some pages were truncated, we cannot guarantee its mapping->host
3064 * to detect pending bios.
3065 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003066
Chao Yu4d57b862018-05-30 00:20:41 +08003067 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003068 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09003069
3070skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003071 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08003072 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09003073 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003074}
3075
Chao Yub0af6d42017-08-02 23:21:48 +08003076static int f2fs_write_data_pages(struct address_space *mapping,
3077 struct writeback_control *wbc)
3078{
3079 struct inode *inode = mapping->host;
3080
3081 return __f2fs_write_data_pages(mapping, wbc,
3082 F2FS_I(inode)->cp_task == current ?
3083 FS_CP_DATA_IO : FS_DATA_IO);
3084}
3085
Chao Yu3aab8f82014-07-02 13:25:04 +08003086static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3087{
3088 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08003089 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08003090
Jaegeuk Kim3f188c22019-12-03 18:54:29 -08003091 if (IS_NOQUOTA(inode))
3092 return;
3093
Eric Biggers95ae2512019-07-22 09:26:24 -07003094 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3095 if (to > i_size && !f2fs_verity_in_progress(inode)) {
Chao Yua33c1502018-08-05 23:04:25 +08003096 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09003097 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08003098
Jaegeuk Kim819d9152015-12-28 13:48:11 -08003099 truncate_pagecache(inode, i_size);
Jaegeuk Kim3f188c22019-12-03 18:54:29 -08003100 f2fs_truncate_blocks(inode, i_size, true);
Chao Yua33c1502018-08-05 23:04:25 +08003101
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08003102 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09003103 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu3aab8f82014-07-02 13:25:04 +08003104 }
3105}
3106
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003107static int prepare_write_begin(struct f2fs_sb_info *sbi,
3108 struct page *page, loff_t pos, unsigned len,
3109 block_t *blk_addr, bool *node_changed)
3110{
3111 struct inode *inode = page->mapping->host;
3112 pgoff_t index = page->index;
3113 struct dnode_of_data dn;
3114 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003115 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00003116 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003117 int err = 0;
Sheng Yong2866fb12018-11-14 19:34:28 +08003118 int flag;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003119
Jaegeuk Kim24b84912016-02-03 13:49:44 -08003120 /*
3121 * we already allocated all the blocks, so we don't need to get
3122 * the block addresses when there is no need to fill the page.
3123 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003124 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
Eric Biggers95ae2512019-07-22 09:26:24 -07003125 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3126 !f2fs_verity_in_progress(inode))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08003127 return 0;
3128
Sheng Yong2866fb12018-11-14 19:34:28 +08003129 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3130 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3131 flag = F2FS_GET_BLOCK_DEFAULT;
3132 else
3133 flag = F2FS_GET_BLOCK_PRE_AIO;
3134
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003135 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003136 (pos & PAGE_MASK) >= i_size_read(inode)) {
Sheng Yong2866fb12018-11-14 19:34:28 +08003137 __do_map_lock(sbi, flag, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003138 locked = true;
3139 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003140
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003141restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003142 /* check inline_data */
Chao Yu4d57b862018-05-30 00:20:41 +08003143 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003144 if (IS_ERR(ipage)) {
3145 err = PTR_ERR(ipage);
3146 goto unlock_out;
3147 }
3148
3149 set_new_dnode(&dn, inode, ipage, ipage, 0);
3150
3151 if (f2fs_has_inline_data(inode)) {
Chao Yuf2470372017-07-19 00:19:05 +08003152 if (pos + len <= MAX_INLINE_DATA(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003153 f2fs_do_read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07003154 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08003155 if (inode->i_nlink)
3156 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003157 } else {
3158 err = f2fs_convert_inline_page(&dn, page);
3159 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003160 goto out;
3161 if (dn.data_blkaddr == NULL_ADDR)
3162 err = f2fs_get_block(&dn, index);
3163 }
3164 } else if (locked) {
3165 err = f2fs_get_block(&dn, index);
3166 } else {
3167 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3168 dn.data_blkaddr = ei.blk + index - ei.fofs;
3169 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003170 /* hole case */
Chao Yu4d57b862018-05-30 00:20:41 +08003171 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07003172 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003173 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08003174 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3175 true);
Sheng Yong2866fb12018-11-14 19:34:28 +08003176 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003177 locked = true;
3178 goto restart;
3179 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003180 }
3181 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003182
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003183 /* convert_inline_page can make node_changed */
3184 *blk_addr = dn.data_blkaddr;
3185 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003186out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003187 f2fs_put_dnode(&dn);
3188unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003189 if (locked)
Sheng Yong2866fb12018-11-14 19:34:28 +08003190 __do_map_lock(sbi, flag, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003191 return err;
3192}
3193
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003194static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3195 loff_t pos, unsigned len, unsigned flags,
3196 struct page **pagep, void **fsdata)
3197{
3198 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07003199 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07003200 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003201 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yua2e2e762018-01-15 17:16:46 +08003202 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003203 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003204 int err = 0;
3205
Chao Yu62aed042014-05-06 16:46:04 +08003206 trace_f2fs_write_begin(inode, pos, len, flags);
3207
Chao Yu00e09c02019-08-23 17:58:36 +08003208 if (!f2fs_is_checkpoint_ready(sbi)) {
3209 err = -ENOSPC;
Daniel Rosenberg43549942018-08-20 19:21:43 -07003210 goto fail;
Chao Yu00e09c02019-08-23 17:58:36 +08003211 }
Daniel Rosenberg43549942018-08-20 19:21:43 -07003212
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09003213 if ((f2fs_is_atomic_file(inode) &&
3214 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3215 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07003216 err = -ENOMEM;
Chao Yua2e2e762018-01-15 17:16:46 +08003217 drop_atomic = true;
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07003218 goto fail;
3219 }
3220
Jaegeuk Kim5f727392014-11-25 10:59:45 -08003221 /*
3222 * We should check this at this moment to avoid deadlock on inode page
3223 * and #0 page. The locking rule for inline_data conversion should be:
3224 * lock_page(page #0) -> lock_page(inode_page)
3225 */
3226 if (index != 0) {
3227 err = f2fs_convert_inline_inode(inode);
3228 if (err)
3229 goto fail;
3230 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003231
3232#ifdef CONFIG_F2FS_FS_COMPRESSION
3233 if (f2fs_compressed_file(inode)) {
3234 int ret;
3235
3236 *fsdata = NULL;
3237
3238 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3239 index, fsdata);
3240 if (ret < 0) {
3241 err = ret;
3242 goto fail;
3243 } else if (ret) {
3244 return 0;
3245 }
3246 }
3247#endif
3248
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09003249repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08003250 /*
3251 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3252 * wait_for_stable_page. Will wait that below with our IO control.
3253 */
Chao Yu01eccef2017-10-28 16:52:30 +08003254 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kim86d54792017-02-17 09:55:55 -08003255 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08003256 if (!page) {
3257 err = -ENOMEM;
3258 goto fail;
3259 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09003260
Chao Yu4c8ff702019-11-01 18:07:14 +08003261 /* TODO: cluster can be compressed due to race with .writepage */
3262
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003263 *pagep = page;
3264
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003265 err = prepare_write_begin(sbi, page, pos, len,
3266 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003267 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003268 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003269
Chao Yuaf033b22018-09-20 20:05:00 +08003270 if (need_balance && !IS_NOQUOTA(inode) &&
3271 has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08003272 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08003273 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08003274 lock_page(page);
3275 if (page->mapping != mapping) {
3276 /* The page got truncated from under us */
3277 f2fs_put_page(page, 1);
3278 goto repeat;
3279 }
3280 }
3281
Chao Yubae0ee72018-12-25 17:43:42 +08003282 f2fs_wait_on_page_writeback(page, DATA, false, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07003283
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003284 if (len == PAGE_SIZE || PageUptodate(page))
3285 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003286
Eric Biggers95ae2512019-07-22 09:26:24 -07003287 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3288 !f2fs_verity_in_progress(inode)) {
Yunlei He746e2402016-12-20 11:11:35 +08003289 zero_user_segment(page, len, PAGE_SIZE);
3290 return 0;
3291 }
3292
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003293 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003294 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003295 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003296 } else {
Chao Yu93770ab2019-04-15 15:26:32 +08003297 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3298 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08003299 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08003300 goto fail;
3301 }
Chao Yu06837282020-02-18 18:21:35 +08003302 err = f2fs_submit_page_read(inode, page, blkaddr, true);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07003303 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08003304 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08003305
Jaegeuk Kim393ff912013-03-08 21:29:23 +09003306 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09003307 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09003308 f2fs_put_page(page, 1);
3309 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003310 }
Chao Yu1563ac72016-07-03 22:05:12 +08003311 if (unlikely(!PageUptodate(page))) {
3312 err = -EIO;
3313 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07003314 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003315 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003316 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003317
Chao Yu3aab8f82014-07-02 13:25:04 +08003318fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07003319 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08003320 f2fs_write_failed(mapping, pos + len);
Chao Yua2e2e762018-01-15 17:16:46 +08003321 if (drop_atomic)
Chao Yu4d57b862018-05-30 00:20:41 +08003322 f2fs_drop_inmem_pages_all(sbi, false);
Chao Yu3aab8f82014-07-02 13:25:04 +08003323 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003324}
3325
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003326static int f2fs_write_end(struct file *file,
3327 struct address_space *mapping,
3328 loff_t pos, unsigned len, unsigned copied,
3329 struct page *page, void *fsdata)
3330{
3331 struct inode *inode = page->mapping->host;
3332
Chao Yudfb2bf32014-05-06 16:47:23 +08003333 trace_f2fs_write_end(inode, pos, len, copied);
3334
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003335 /*
3336 * This should be come from len == PAGE_SIZE, and we expect copied
3337 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3338 * let generic_perform_write() try to copy data again through copied=0.
3339 */
3340 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08003341 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003342 copied = 0;
3343 else
3344 SetPageUptodate(page);
3345 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003346
3347#ifdef CONFIG_F2FS_FS_COMPRESSION
3348 /* overwrite compressed file */
3349 if (f2fs_compressed_file(inode) && fsdata) {
3350 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3351 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3352 return copied;
3353 }
3354#endif
3355
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003356 if (!copied)
3357 goto unlock_out;
3358
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003359 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003360
Eric Biggers95ae2512019-07-22 09:26:24 -07003361 if (pos + copied > i_size_read(inode) &&
3362 !f2fs_verity_in_progress(inode))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07003363 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003364unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08003365 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08003366 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003367 return copied;
3368}
3369
Omar Sandoval6f673762015-03-16 04:33:52 -07003370static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3371 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003372{
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07003373 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3374 unsigned blkbits = i_blkbits;
3375 unsigned blocksize_mask = (1 << blkbits) - 1;
3376 unsigned long align = offset | iov_iter_alignment(iter);
3377 struct block_device *bdev = inode->i_sb->s_bdev;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003378
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07003379 if (align & blocksize_mask) {
3380 if (bdev)
3381 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3382 blocksize_mask = (1 << blkbits) - 1;
3383 if (align & blocksize_mask)
3384 return -EINVAL;
3385 return 1;
3386 }
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003387 return 0;
3388}
3389
Chao Yu02b16d02018-11-12 00:46:46 +08003390static void f2fs_dio_end_io(struct bio *bio)
3391{
3392 struct f2fs_private_dio *dio = bio->bi_private;
3393
3394 dec_page_count(F2FS_I_SB(dio->inode),
3395 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3396
3397 bio->bi_private = dio->orig_private;
3398 bio->bi_end_io = dio->orig_end_io;
3399
Jaegeuk Kim52225952018-12-13 18:38:33 -08003400 kvfree(dio);
Chao Yu02b16d02018-11-12 00:46:46 +08003401
3402 bio_endio(bio);
3403}
3404
3405static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3406 loff_t file_offset)
3407{
3408 struct f2fs_private_dio *dio;
3409 bool write = (bio_op(bio) == REQ_OP_WRITE);
Chao Yu02b16d02018-11-12 00:46:46 +08003410
3411 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3412 sizeof(struct f2fs_private_dio), GFP_NOFS);
YueHaibing8e114032019-01-04 01:38:29 +00003413 if (!dio)
Chao Yu02b16d02018-11-12 00:46:46 +08003414 goto out;
Chao Yu02b16d02018-11-12 00:46:46 +08003415
3416 dio->inode = inode;
3417 dio->orig_end_io = bio->bi_end_io;
3418 dio->orig_private = bio->bi_private;
3419 dio->write = write;
3420
3421 bio->bi_end_io = f2fs_dio_end_io;
3422 bio->bi_private = dio;
3423
3424 inc_page_count(F2FS_I_SB(inode),
3425 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3426
3427 submit_bio(bio);
3428 return;
3429out:
3430 bio->bi_status = BLK_STS_IOERR;
3431 bio_endio(bio);
3432}
3433
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003434static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003435{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003436 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08003437 struct inode *inode = mapping->host;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003438 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf847c692018-09-27 18:34:52 +08003439 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08003440 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003441 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08003442 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08003443 int err;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003444 enum rw_hint hint = iocb->ki_hint;
Chao Yu63189b72018-03-08 14:22:56 +08003445 int whint_mode = F2FS_OPTION(sbi).whint_mode;
Chao Yuf847c692018-09-27 18:34:52 +08003446 bool do_opu;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003447
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003448 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08003449 if (err)
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07003450 return err < 0 ? err : 0;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08003451
Chao Yuf847c692018-09-27 18:34:52 +08003452 if (f2fs_force_buffered_io(inode, iocb, iter))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07003453 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003454
Chao Yuf847c692018-09-27 18:34:52 +08003455 do_opu = allow_outplace_dio(inode, iocb, iter);
3456
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07003457 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08003458
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003459 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3460 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3461
Chao Yuf847c692018-09-27 18:34:52 +08003462 if (iocb->ki_flags & IOCB_NOWAIT) {
3463 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003464 iocb->ki_hint = hint;
3465 err = -EAGAIN;
3466 goto out;
3467 }
Chao Yuf847c692018-09-27 18:34:52 +08003468 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3469 up_read(&fi->i_gc_rwsem[rw]);
3470 iocb->ki_hint = hint;
3471 err = -EAGAIN;
3472 goto out;
3473 }
3474 } else {
3475 down_read(&fi->i_gc_rwsem[rw]);
3476 if (do_opu)
3477 down_read(&fi->i_gc_rwsem[READ]);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003478 }
3479
Chao Yu02b16d02018-11-12 00:46:46 +08003480 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
Chao Yuf9d6d052018-11-13 14:33:45 +08003481 iter, rw == WRITE ? get_data_block_dio_write :
3482 get_data_block_dio, NULL, f2fs_dio_submit_bio,
DongDongJuad8d6a02020-03-20 15:01:32 +09003483 rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3484 DIO_SKIP_HOLES);
Chao Yuf847c692018-09-27 18:34:52 +08003485
3486 if (do_opu)
3487 up_read(&fi->i_gc_rwsem[READ]);
3488
3489 up_read(&fi->i_gc_rwsem[rw]);
Chao Yu82e0a5a2016-07-13 09:18:29 +08003490
3491 if (rw == WRITE) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003492 if (whint_mode == WHINT_MODE_OFF)
3493 iocb->ki_hint = hint;
Chao Yub0af6d42017-08-02 23:21:48 +08003494 if (err > 0) {
3495 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3496 err);
Chao Yuf847c692018-09-27 18:34:52 +08003497 if (!do_opu)
3498 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yub0af6d42017-08-02 23:21:48 +08003499 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04003500 f2fs_write_failed(mapping, offset + count);
Chao Yub0af6d42017-08-02 23:21:48 +08003501 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04003502 }
Chao Yu70407fa2014-07-31 21:11:22 +08003503
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003504out:
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07003505 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08003506
Chao Yu3aab8f82014-07-02 13:25:04 +08003507 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003508}
3509
Chao Yu487261f2015-02-05 17:44:29 +08003510void f2fs_invalidate_page(struct page *page, unsigned int offset,
3511 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003512{
3513 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08003514 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003515
Chao Yu487261f2015-02-05 17:44:29 +08003516 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003517 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003518 return;
3519
Chao Yu487261f2015-02-05 17:44:29 +08003520 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08003521 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08003522 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08003523 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08003524 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08003525 } else {
Chao Yu487261f2015-02-05 17:44:29 +08003526 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +08003527 f2fs_remove_dirty_inode(inode);
Chao Yu933439c2016-10-11 22:57:01 +08003528 }
Chao Yu487261f2015-02-05 17:44:29 +08003529 }
Chao Yudecd36b2015-08-07 18:42:09 +08003530
Chao Yu2baf0782018-07-27 18:15:16 +08003531 clear_cold_data(page);
3532
Chao Yudecd36b2015-08-07 18:42:09 +08003533 if (IS_ATOMIC_WRITTEN_PAGE(page))
Chao Yu4d57b862018-05-30 00:20:41 +08003534 return f2fs_drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08003535
Chao Yu240a5912019-03-06 17:30:59 +08003536 f2fs_clear_page_private(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003537}
3538
Chao Yu487261f2015-02-05 17:44:29 +08003539int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003540{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08003541 /* If this is dirty page, keep PagePrivate */
3542 if (PageDirty(page))
3543 return 0;
3544
Chao Yudecd36b2015-08-07 18:42:09 +08003545 /* This is atomic written page, keep Private */
3546 if (IS_ATOMIC_WRITTEN_PAGE(page))
3547 return 0;
3548
Chao Yu2baf0782018-07-27 18:15:16 +08003549 clear_cold_data(page);
Chao Yu240a5912019-03-06 17:30:59 +08003550 f2fs_clear_page_private(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09003551 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003552}
3553
3554static int f2fs_set_data_page_dirty(struct page *page)
3555{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003556 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003557
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09003558 trace_f2fs_set_page_dirty(page, DATA);
3559
Jaegeuk Kim237c0792016-06-30 18:49:15 -07003560 if (!PageUptodate(page))
3561 SetPageUptodate(page);
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003562 if (PageSwapCache(page))
3563 return __set_page_dirty_nobuffers(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003564
Chao Yu5fe45742017-01-07 18:50:26 +08003565 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08003566 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003567 f2fs_register_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08003568 return 1;
3569 }
3570 /*
3571 * Previously, this page has been registered, we just
3572 * return here.
3573 */
3574 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003575 }
3576
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003577 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07003578 __set_page_dirty_nobuffers(page);
Chao Yu4d57b862018-05-30 00:20:41 +08003579 f2fs_update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003580 return 1;
3581 }
3582 return 0;
3583}
3584
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09003585static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3586{
Chao Yu454ae7e2014-04-22 13:34:01 +08003587 struct inode *inode = mapping->host;
3588
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07003589 if (f2fs_has_inline_data(inode))
3590 return 0;
3591
3592 /* make sure allocating whole blocks */
3593 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3594 filemap_write_and_wait(mapping);
3595
Chao Yue2b4e2b2015-08-19 19:11:19 +08003596 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08003597}
3598
Weichao Guo5b7a4872016-09-20 05:03:27 +08003599#ifdef CONFIG_MIGRATION
3600#include <linux/migrate.h>
3601
3602int f2fs_migrate_page(struct address_space *mapping,
3603 struct page *newpage, struct page *page, enum migrate_mode mode)
3604{
3605 int rc, extra_count;
3606 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3607 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3608
3609 BUG_ON(PageWriteback(page));
3610
3611 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07003612 if (atomic_written) {
3613 if (mode != MIGRATE_SYNC)
3614 return -EBUSY;
3615 if (!mutex_trylock(&fi->inmem_lock))
3616 return -EAGAIN;
3617 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08003618
Chao Yu240a5912019-03-06 17:30:59 +08003619 /* one extra reference was held for atomic_write page */
3620 extra_count = atomic_written ? 1 : 0;
Weichao Guo5b7a4872016-09-20 05:03:27 +08003621 rc = migrate_page_move_mapping(mapping, newpage,
Keith Busch37109692019-07-18 15:58:46 -07003622 page, extra_count);
Weichao Guo5b7a4872016-09-20 05:03:27 +08003623 if (rc != MIGRATEPAGE_SUCCESS) {
3624 if (atomic_written)
3625 mutex_unlock(&fi->inmem_lock);
3626 return rc;
3627 }
3628
3629 if (atomic_written) {
3630 struct inmem_pages *cur;
3631 list_for_each_entry(cur, &fi->inmem_pages, list)
3632 if (cur->page == page) {
3633 cur->page = newpage;
3634 break;
3635 }
3636 mutex_unlock(&fi->inmem_lock);
3637 put_page(page);
3638 get_page(newpage);
3639 }
3640
Chao Yu240a5912019-03-06 17:30:59 +08003641 if (PagePrivate(page)) {
3642 f2fs_set_page_private(newpage, page_private(page));
3643 f2fs_clear_page_private(page);
3644 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08003645
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07003646 if (mode != MIGRATE_SYNC_NO_COPY)
3647 migrate_page_copy(newpage, page);
3648 else
3649 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08003650
3651 return MIGRATEPAGE_SUCCESS;
3652}
3653#endif
3654
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003655#ifdef CONFIG_SWAP
3656/* Copied from generic_swapfile_activate() to check any holes */
Chao Yu3e5e4792019-12-27 18:44:56 +08003657static int check_swap_activate(struct swap_info_struct *sis,
3658 struct file *swap_file, sector_t *span)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003659{
3660 struct address_space *mapping = swap_file->f_mapping;
3661 struct inode *inode = mapping->host;
3662 unsigned blocks_per_page;
3663 unsigned long page_no;
3664 unsigned blkbits;
3665 sector_t probe_block;
3666 sector_t last_block;
3667 sector_t lowest_block = -1;
3668 sector_t highest_block = 0;
Chao Yu3e5e4792019-12-27 18:44:56 +08003669 int nr_extents = 0;
3670 int ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003671
3672 blkbits = inode->i_blkbits;
3673 blocks_per_page = PAGE_SIZE >> blkbits;
3674
3675 /*
3676 * Map all the blocks into the extent list. This code doesn't try
3677 * to be very smart.
3678 */
3679 probe_block = 0;
3680 page_no = 0;
3681 last_block = i_size_read(inode) >> blkbits;
Chao Yu3e5e4792019-12-27 18:44:56 +08003682 while ((probe_block + blocks_per_page) <= last_block &&
3683 page_no < sis->max) {
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003684 unsigned block_in_page;
3685 sector_t first_block;
Carlos Maiolino30460e12020-01-09 14:30:41 +01003686 sector_t block = 0;
3687 int err = 0;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003688
3689 cond_resched();
3690
Carlos Maiolino30460e12020-01-09 14:30:41 +01003691 block = probe_block;
3692 err = bmap(inode, &block);
3693 if (err || !block)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003694 goto bad_bmap;
Carlos Maiolino30460e12020-01-09 14:30:41 +01003695 first_block = block;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003696
3697 /*
3698 * It must be PAGE_SIZE aligned on-disk
3699 */
3700 if (first_block & (blocks_per_page - 1)) {
3701 probe_block++;
3702 goto reprobe;
3703 }
3704
3705 for (block_in_page = 1; block_in_page < blocks_per_page;
3706 block_in_page++) {
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003707
Carlos Maiolino30460e12020-01-09 14:30:41 +01003708 block = probe_block + block_in_page;
3709 err = bmap(inode, &block);
3710
3711 if (err || !block)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003712 goto bad_bmap;
Carlos Maiolino30460e12020-01-09 14:30:41 +01003713
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003714 if (block != first_block + block_in_page) {
3715 /* Discontiguity */
3716 probe_block++;
3717 goto reprobe;
3718 }
3719 }
3720
3721 first_block >>= (PAGE_SHIFT - blkbits);
3722 if (page_no) { /* exclude the header page */
3723 if (first_block < lowest_block)
3724 lowest_block = first_block;
3725 if (first_block > highest_block)
3726 highest_block = first_block;
3727 }
3728
Chao Yu3e5e4792019-12-27 18:44:56 +08003729 /*
3730 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3731 */
3732 ret = add_swap_extent(sis, page_no, 1, first_block);
3733 if (ret < 0)
3734 goto out;
3735 nr_extents += ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003736 page_no++;
3737 probe_block += blocks_per_page;
3738reprobe:
3739 continue;
3740 }
Chao Yu3e5e4792019-12-27 18:44:56 +08003741 ret = nr_extents;
3742 *span = 1 + highest_block - lowest_block;
3743 if (page_no == 0)
3744 page_no = 1; /* force Empty message */
3745 sis->max = page_no;
3746 sis->pages = page_no - 1;
3747 sis->highest_bit = page_no - 1;
3748out:
3749 return ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003750bad_bmap:
3751 pr_err("swapon: swapfile has holes\n");
3752 return -EINVAL;
3753}
3754
3755static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3756 sector_t *span)
3757{
3758 struct inode *inode = file_inode(file);
3759 int ret;
3760
3761 if (!S_ISREG(inode->i_mode))
3762 return -EINVAL;
3763
3764 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3765 return -EROFS;
3766
3767 ret = f2fs_convert_inline_inode(inode);
3768 if (ret)
3769 return ret;
3770
Chao Yu4c8ff702019-11-01 18:07:14 +08003771 if (f2fs_disable_compressed_file(inode))
3772 return -EINVAL;
3773
Chao Yu3e5e4792019-12-27 18:44:56 +08003774 ret = check_swap_activate(sis, file, span);
3775 if (ret < 0)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003776 return ret;
3777
3778 set_inode_flag(inode, FI_PIN_FILE);
3779 f2fs_precache_extents(inode);
3780 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu3e5e4792019-12-27 18:44:56 +08003781 return ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003782}
3783
3784static void f2fs_swap_deactivate(struct file *file)
3785{
3786 struct inode *inode = file_inode(file);
3787
3788 clear_inode_flag(inode, FI_PIN_FILE);
3789}
3790#else
3791static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3792 sector_t *span)
3793{
3794 return -EOPNOTSUPP;
3795}
3796
3797static void f2fs_swap_deactivate(struct file *file)
3798{
3799}
3800#endif
3801
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003802const struct address_space_operations f2fs_dblock_aops = {
3803 .readpage = f2fs_read_data_page,
3804 .readpages = f2fs_read_data_pages,
3805 .writepage = f2fs_write_data_page,
3806 .writepages = f2fs_write_data_pages,
3807 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003808 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003809 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08003810 .invalidatepage = f2fs_invalidate_page,
3811 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003812 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09003813 .bmap = f2fs_bmap,
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003814 .swap_activate = f2fs_swap_activate,
3815 .swap_deactivate = f2fs_swap_deactivate,
Weichao Guo5b7a4872016-09-20 05:03:27 +08003816#ifdef CONFIG_MIGRATION
3817 .migratepage = f2fs_migrate_page,
3818#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003819};
Eric Biggers6dbb1792018-04-18 11:09:48 -07003820
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05003821void f2fs_clear_page_cache_dirty_tag(struct page *page)
Chao Yuaec2f722018-05-26 18:03:35 +08003822{
3823 struct address_space *mapping = page_mapping(page);
3824 unsigned long flags;
3825
3826 xa_lock_irqsave(&mapping->i_pages, flags);
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05003827 __xa_clear_mark(&mapping->i_pages, page_index(page),
Chao Yuaec2f722018-05-26 18:03:35 +08003828 PAGECACHE_TAG_DIRTY);
3829 xa_unlock_irqrestore(&mapping->i_pages, flags);
3830}
3831
Eric Biggers6dbb1792018-04-18 11:09:48 -07003832int __init f2fs_init_post_read_processing(void)
3833{
Eric Biggers95ae2512019-07-22 09:26:24 -07003834 bio_post_read_ctx_cache =
3835 kmem_cache_create("f2fs_bio_post_read_ctx",
3836 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
Eric Biggers6dbb1792018-04-18 11:09:48 -07003837 if (!bio_post_read_ctx_cache)
3838 goto fail;
3839 bio_post_read_ctx_pool =
3840 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
3841 bio_post_read_ctx_cache);
3842 if (!bio_post_read_ctx_pool)
3843 goto fail_free_cache;
3844 return 0;
3845
3846fail_free_cache:
3847 kmem_cache_destroy(bio_post_read_ctx_cache);
3848fail:
3849 return -ENOMEM;
3850}
3851
Chao Yu0b20fce2019-09-30 18:53:25 +08003852void f2fs_destroy_post_read_processing(void)
Eric Biggers6dbb1792018-04-18 11:09:48 -07003853{
3854 mempool_destroy(bio_post_read_ctx_pool);
3855 kmem_cache_destroy(bio_post_read_ctx_cache);
3856}
Chao Yu0b20fce2019-09-30 18:53:25 +08003857
Chao Yu4c8ff702019-11-01 18:07:14 +08003858int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
3859{
3860 if (!f2fs_sb_has_encrypt(sbi) &&
3861 !f2fs_sb_has_verity(sbi) &&
3862 !f2fs_sb_has_compression(sbi))
3863 return 0;
3864
3865 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
3866 WQ_UNBOUND | WQ_HIGHPRI,
3867 num_online_cpus());
3868 if (!sbi->post_read_wq)
3869 return -ENOMEM;
3870 return 0;
3871}
3872
3873void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
3874{
3875 if (sbi->post_read_wq)
3876 destroy_workqueue(sbi->post_read_wq);
3877}
3878
Chao Yu0b20fce2019-09-30 18:53:25 +08003879int __init f2fs_init_bio_entry_cache(void)
3880{
Chao Yu98510002020-02-17 17:46:20 +08003881 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
Chao Yu0b20fce2019-09-30 18:53:25 +08003882 sizeof(struct bio_entry));
3883 if (!bio_entry_slab)
3884 return -ENOMEM;
3885 return 0;
3886}
3887
Chao Yuf5438052019-12-04 09:52:58 +08003888void f2fs_destroy_bio_entry_cache(void)
Chao Yu0b20fce2019-09-30 18:53:25 +08003889{
3890 kmem_cache_destroy(bio_entry_slab);
3891}