blob: b56437d4973d60a0a5b964057a8508267040f92b [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
57struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool no_fail)
58{
59 struct bio *bio;
60
61 if (no_fail) {
62 /* No failure on bio allocation */
63 bio = __f2fs_bio_alloc(GFP_NOIO, npages);
64 if (!bio)
65 bio = __f2fs_bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
66 return bio;
67 }
68 if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
69 f2fs_show_injection_info(sbi, FAULT_ALLOC_BIO);
70 return NULL;
71 }
72
73 return __f2fs_bio_alloc(GFP_KERNEL, npages);
74}
Eric Biggers6dbb1792018-04-18 11:09:48 -070075
Chao Yu36951b32016-11-16 10:41:20 +080076static bool __is_cp_guaranteed(struct page *page)
77{
78 struct address_space *mapping = page->mapping;
79 struct inode *inode;
80 struct f2fs_sb_info *sbi;
81
82 if (!mapping)
83 return false;
84
Chao Yu4c8ff702019-11-01 18:07:14 +080085 if (f2fs_is_compressed_page(page))
86 return false;
87
Chao Yu36951b32016-11-16 10:41:20 +080088 inode = mapping->host;
89 sbi = F2FS_I_SB(inode);
90
91 if (inode->i_ino == F2FS_META_INO(sbi) ||
92 inode->i_ino == F2FS_NODE_INO(sbi) ||
93 S_ISDIR(inode->i_mode) ||
Chao Yue7a4feb2018-05-08 14:06:03 +080094 (S_ISREG(inode->i_mode) &&
Chao Yuaf033b22018-09-20 20:05:00 +080095 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
Chao Yu36951b32016-11-16 10:41:20 +080096 is_cold_data(page))
97 return true;
98 return false;
99}
100
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700101static enum count_type __read_io_type(struct page *page)
102{
Jaegeuk Kim4969c062019-07-01 19:15:29 -0700103 struct address_space *mapping = page_file_mapping(page);
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700104
105 if (mapping) {
106 struct inode *inode = mapping->host;
107 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
108
109 if (inode->i_ino == F2FS_META_INO(sbi))
110 return F2FS_RD_META;
111
112 if (inode->i_ino == F2FS_NODE_INO(sbi))
113 return F2FS_RD_NODE;
114 }
115 return F2FS_RD_DATA;
116}
117
Eric Biggers6dbb1792018-04-18 11:09:48 -0700118/* postprocessing steps for read bios */
119enum bio_post_read_step {
Eric Biggers6dbb1792018-04-18 11:09:48 -0700120 STEP_DECRYPT,
Chao Yu4c8ff702019-11-01 18:07:14 +0800121 STEP_DECOMPRESS,
Eric Biggers95ae2512019-07-22 09:26:24 -0700122 STEP_VERITY,
Eric Biggers6dbb1792018-04-18 11:09:48 -0700123};
124
125struct bio_post_read_ctx {
126 struct bio *bio;
Chao Yu4c8ff702019-11-01 18:07:14 +0800127 struct f2fs_sb_info *sbi;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700128 struct work_struct work;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700129 unsigned int enabled_steps;
130};
131
Chao Yu4c8ff702019-11-01 18:07:14 +0800132static void __read_end_io(struct bio *bio, bool compr, bool verity)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900133{
Eric Biggers6dbb1792018-04-18 11:09:48 -0700134 struct page *page;
135 struct bio_vec *bv;
Ming Lei6dc4f102019-02-15 19:13:19 +0800136 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900137
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200138 bio_for_each_segment_all(bv, bio, iter_all) {
Eric Biggers6dbb1792018-04-18 11:09:48 -0700139 page = bv->bv_page;
140
Chao Yu4c8ff702019-11-01 18:07:14 +0800141#ifdef CONFIG_F2FS_FS_COMPRESSION
142 if (compr && f2fs_is_compressed_page(page)) {
143 f2fs_decompress_pages(bio, page, verity);
144 continue;
145 }
146#endif
147
Eric Biggers6dbb1792018-04-18 11:09:48 -0700148 /* PG_error was set if any post_read step failed */
149 if (bio->bi_status || PageError(page)) {
150 ClearPageUptodate(page);
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -0700151 /* will re-read again later */
152 ClearPageError(page);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700153 } else {
154 SetPageUptodate(page);
155 }
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700156 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
Eric Biggers6dbb1792018-04-18 11:09:48 -0700157 unlock_page(page);
158 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800159}
160
161static void f2fs_release_read_bio(struct bio *bio);
162static void __f2fs_read_end_io(struct bio *bio, bool compr, bool verity)
163{
164 if (!compr)
165 __read_end_io(bio, false, verity);
166 f2fs_release_read_bio(bio);
167}
168
169static void f2fs_decompress_bio(struct bio *bio, bool verity)
170{
171 __read_end_io(bio, true, verity);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700172}
173
174static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
175
Chao Yu4c8ff702019-11-01 18:07:14 +0800176static void f2fs_decrypt_work(struct bio_post_read_ctx *ctx)
Eric Biggers6dbb1792018-04-18 11:09:48 -0700177{
Eric Biggers6dbb1792018-04-18 11:09:48 -0700178 fscrypt_decrypt_bio(ctx->bio);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700179}
180
Chao Yu4c8ff702019-11-01 18:07:14 +0800181static void f2fs_decompress_work(struct bio_post_read_ctx *ctx)
182{
183 f2fs_decompress_bio(ctx->bio, ctx->enabled_steps & (1 << STEP_VERITY));
184}
185
186#ifdef CONFIG_F2FS_FS_COMPRESSION
187static void f2fs_verify_pages(struct page **rpages, unsigned int cluster_size)
188{
189 f2fs_decompress_end_io(rpages, cluster_size, false, true);
190}
191
192static void f2fs_verify_bio(struct bio *bio)
193{
194 struct page *page = bio_first_page_all(bio);
195 struct decompress_io_ctx *dic =
196 (struct decompress_io_ctx *)page_private(page);
197
198 f2fs_verify_pages(dic->rpages, dic->cluster_size);
199 f2fs_free_dic(dic);
200}
201#endif
202
203static void f2fs_verity_work(struct work_struct *work)
Eric Biggers95ae2512019-07-22 09:26:24 -0700204{
205 struct bio_post_read_ctx *ctx =
206 container_of(work, struct bio_post_read_ctx, work);
207
Chao Yu4c8ff702019-11-01 18:07:14 +0800208#ifdef CONFIG_F2FS_FS_COMPRESSION
209 /* previous step is decompression */
210 if (ctx->enabled_steps & (1 << STEP_DECOMPRESS)) {
Eric Biggers95ae2512019-07-22 09:26:24 -0700211
Chao Yu4c8ff702019-11-01 18:07:14 +0800212 f2fs_verify_bio(ctx->bio);
213 f2fs_release_read_bio(ctx->bio);
214 return;
215 }
216#endif
217
218 fsverity_verify_bio(ctx->bio);
219 __f2fs_read_end_io(ctx->bio, false, false);
220}
221
222static void f2fs_post_read_work(struct work_struct *work)
223{
224 struct bio_post_read_ctx *ctx =
225 container_of(work, struct bio_post_read_ctx, work);
226
227 if (ctx->enabled_steps & (1 << STEP_DECRYPT))
228 f2fs_decrypt_work(ctx);
229
230 if (ctx->enabled_steps & (1 << STEP_DECOMPRESS))
231 f2fs_decompress_work(ctx);
232
233 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
234 INIT_WORK(&ctx->work, f2fs_verity_work);
235 fsverity_enqueue_verify_work(&ctx->work);
236 return;
237 }
238
239 __f2fs_read_end_io(ctx->bio,
240 ctx->enabled_steps & (1 << STEP_DECOMPRESS), false);
241}
242
243static void f2fs_enqueue_post_read_work(struct f2fs_sb_info *sbi,
244 struct work_struct *work)
245{
246 queue_work(sbi->post_read_wq, work);
Eric Biggers95ae2512019-07-22 09:26:24 -0700247}
248
Eric Biggers6dbb1792018-04-18 11:09:48 -0700249static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
250{
Eric Biggers95ae2512019-07-22 09:26:24 -0700251 /*
252 * We use different work queues for decryption and for verity because
253 * verity may require reading metadata pages that need decryption, and
254 * we shouldn't recurse to the same workqueue.
255 */
Chao Yu4c8ff702019-11-01 18:07:14 +0800256
257 if (ctx->enabled_steps & (1 << STEP_DECRYPT) ||
258 ctx->enabled_steps & (1 << STEP_DECOMPRESS)) {
259 INIT_WORK(&ctx->work, f2fs_post_read_work);
260 f2fs_enqueue_post_read_work(ctx->sbi, &ctx->work);
261 return;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700262 }
Chao Yu4c8ff702019-11-01 18:07:14 +0800263
264 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
265 INIT_WORK(&ctx->work, f2fs_verity_work);
266 fsverity_enqueue_verify_work(&ctx->work);
267 return;
268 }
269
270 __f2fs_read_end_io(ctx->bio, false, false);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700271}
272
273static bool f2fs_bio_post_read_required(struct bio *bio)
274{
Chao Yu4c8ff702019-11-01 18:07:14 +0800275 return bio->bi_private;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700276}
277
278static void f2fs_read_end_io(struct bio *bio)
279{
Chao Yuc45d6002019-11-01 17:53:23 +0800280 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
281
282 if (time_to_inject(sbi, FAULT_READ_IO)) {
283 f2fs_show_injection_info(sbi, FAULT_READ_IO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200284 bio->bi_status = BLK_STS_IOERR;
Chao Yu55523512017-02-25 11:08:28 +0800285 }
Chao Yu8b038c72016-09-18 23:30:07 +0800286
Eric Biggers6dbb1792018-04-18 11:09:48 -0700287 if (f2fs_bio_post_read_required(bio)) {
288 struct bio_post_read_ctx *ctx = bio->bi_private;
289
Eric Biggers6dbb1792018-04-18 11:09:48 -0700290 bio_post_read_processing(ctx);
291 return;
Chao Yu12377022015-05-25 18:03:38 +0800292 }
293
Chao Yu4c8ff702019-11-01 18:07:14 +0800294 __f2fs_read_end_io(bio, false, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900295}
296
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200297static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900298{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +0900299 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -0800300 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800301 struct bvec_iter_all iter_all;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900302
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800303 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
Chao Yuc45d6002019-11-01 17:53:23 +0800304 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
Chao Yu6f5c2ed2018-09-12 09:22:29 +0800305 bio->bi_status = BLK_STS_IOERR;
306 }
307
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200308 bio_for_each_segment_all(bvec, bio, iter_all) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900309 struct page *page = bvec->bv_page;
Chao Yu36951b32016-11-16 10:41:20 +0800310 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900311
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800312 if (IS_DUMMY_WRITTEN_PAGE(page)) {
313 set_page_private(page, (unsigned long)NULL);
314 ClearPagePrivate(page);
315 unlock_page(page);
316 mempool_free(page, sbi->write_io_dummy);
317
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200318 if (unlikely(bio->bi_status))
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800319 f2fs_stop_checkpoint(sbi, true);
320 continue;
321 }
322
Eric Biggersd2d07272019-05-20 09:29:39 -0700323 fscrypt_finalize_bounce_page(&page);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700324
Chao Yu4c8ff702019-11-01 18:07:14 +0800325#ifdef CONFIG_F2FS_FS_COMPRESSION
326 if (f2fs_is_compressed_page(page)) {
327 f2fs_compress_write_end_io(bio, page);
328 continue;
329 }
330#endif
331
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200332 if (unlikely(bio->bi_status)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700333 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kimb1ca3212017-12-31 16:26:38 -0800334 if (type == F2FS_WB_CP_DATA)
335 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900336 }
Yunlei He7dff55d2018-01-11 14:19:32 +0800337
338 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
339 page->index != nid_of_node(page));
340
Chao Yu36951b32016-11-16 10:41:20 +0800341 dec_page_count(sbi, type);
Chao Yu50fa53e2018-08-02 23:03:19 +0800342 if (f2fs_in_warm_node_list(sbi, page))
343 f2fs_del_fsync_node_entry(sbi, page);
Chao Yu36951b32016-11-16 10:41:20 +0800344 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900345 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800346 }
Chao Yu36951b32016-11-16 10:41:20 +0800347 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700348 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900349 wake_up(&sbi->cp_wait);
350
351 bio_put(bio);
352}
353
Gu Zheng940a6d32013-12-20 17:39:59 +0800354/*
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700355 * Return true, if pre_bio's bdev is same as its target device.
356 */
357struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
358 block_t blk_addr, struct bio *bio)
359{
360 struct block_device *bdev = sbi->sb->s_bdev;
361 int i;
362
Damien Le Moal09168782019-03-16 09:13:06 +0900363 if (f2fs_is_multi_device(sbi)) {
364 for (i = 0; i < sbi->s_ndevs; i++) {
365 if (FDEV(i).start_blk <= blk_addr &&
366 FDEV(i).end_blk >= blk_addr) {
367 blk_addr -= FDEV(i).start_blk;
368 bdev = FDEV(i).bdev;
369 break;
370 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700371 }
372 }
373 if (bio) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200374 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700375 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
376 }
377 return bdev;
378}
379
380int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
381{
382 int i;
383
Damien Le Moal09168782019-03-16 09:13:06 +0900384 if (!f2fs_is_multi_device(sbi))
385 return 0;
386
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700387 for (i = 0; i < sbi->s_ndevs; i++)
388 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
389 return i;
390 return 0;
391}
392
393static bool __same_bdev(struct f2fs_sb_info *sbi,
394 block_t blk_addr, struct bio *bio)
395{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200396 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
397 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700398}
399
400/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800401 * Low-level block read/write IO operations.
402 */
Chao Yub757f6e2019-08-23 17:58:35 +0800403static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
Gu Zheng940a6d32013-12-20 17:39:59 +0800404{
Chao Yub757f6e2019-08-23 17:58:35 +0800405 struct f2fs_sb_info *sbi = fio->sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800406 struct bio *bio;
407
Chao Yud62fe972017-10-28 16:52:31 +0800408 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800409
Chao Yub757f6e2019-08-23 17:58:35 +0800410 f2fs_target_device(sbi, fio->new_blkaddr, bio);
411 if (is_read_io(fio->op)) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900412 bio->bi_end_io = f2fs_read_end_io;
413 bio->bi_private = NULL;
414 } else {
415 bio->bi_end_io = f2fs_write_end_io;
416 bio->bi_private = sbi;
Chao Yub757f6e2019-08-23 17:58:35 +0800417 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
418 fio->type, fio->temp);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900419 }
Chao Yub757f6e2019-08-23 17:58:35 +0800420 if (fio->io_wbc)
421 wbc_init_bio(fio->io_wbc, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800422
423 return bio;
424}
425
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700426static inline void __submit_bio(struct f2fs_sb_info *sbi,
427 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700428{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700429 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800430 unsigned int start;
431
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800432 if (type != DATA && type != NODE)
433 goto submit_io;
434
Yunlong Song66415ce2018-07-12 23:09:28 +0800435 if (test_opt(sbi, LFS) && current->plug)
Tiezhu Yang3bb09a02018-02-06 08:21:45 +0800436 blk_finish_plug(current->plug);
437
Chao Yu8223ecc2019-08-28 17:33:38 +0800438 if (F2FS_IO_ALIGNED(sbi))
439 goto submit_io;
440
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800441 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
442 start %= F2FS_IO_SIZE(sbi);
443
444 if (start == 0)
445 goto submit_io;
446
447 /* fill dummy pages */
448 for (; start < F2FS_IO_SIZE(sbi); start++) {
449 struct page *page =
450 mempool_alloc(sbi->write_io_dummy,
Gao Xiangbc73a4b2019-02-19 10:31:52 +0800451 GFP_NOIO | __GFP_NOFAIL);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800452 f2fs_bug_on(sbi, !page);
453
Gao Xiangbc73a4b2019-02-19 10:31:52 +0800454 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800455 SetPagePrivate(page);
456 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
457 lock_page(page);
458 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
459 f2fs_bug_on(sbi, 1);
460 }
461 /*
462 * In the NODE case, we lose next block address chain. So, we
463 * need to do checkpoint in f2fs_sync_file.
464 */
465 if (type == NODE)
466 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700467 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800468submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800469 if (is_read_io(bio_op(bio)))
470 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
471 else
472 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500473 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700474}
475
Chao Yu4c8ff702019-11-01 18:07:14 +0800476void f2fs_submit_bio(struct f2fs_sb_info *sbi,
477 struct bio *bio, enum page_type type)
478{
479 __submit_bio(sbi, bio, type);
480}
481
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900482static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900483{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900484 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900485
486 if (!io->bio)
487 return;
488
Mike Christie04d328d2016-06-05 14:31:55 -0500489 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
490
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800491 if (is_read_io(fio->op))
492 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
493 else
494 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
495
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700496 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900497 io->bio = NULL;
498}
499
Chao Yu8648de22019-02-19 16:15:29 +0800500static bool __has_merged_page(struct bio *bio, struct inode *inode,
Chao Yubab475c2018-09-27 23:41:16 +0800501 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800502{
Chao Yu0fd785e2016-01-18 18:24:59 +0800503 struct bio_vec *bvec;
Ming Lei6dc4f102019-02-15 19:13:19 +0800504 struct bvec_iter_all iter_all;
Chao Yu0fd785e2016-01-18 18:24:59 +0800505
Chao Yu8648de22019-02-19 16:15:29 +0800506 if (!bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800507 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800508
Chao Yubab475c2018-09-27 23:41:16 +0800509 if (!inode && !page && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800510 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800511
Chao Yu8648de22019-02-19 16:15:29 +0800512 bio_for_each_segment_all(bvec, bio, iter_all) {
Chao Yu4c8ff702019-11-01 18:07:14 +0800513 struct page *target = bvec->bv_page;
Chao Yu0fd785e2016-01-18 18:24:59 +0800514
Chao Yu4c8ff702019-11-01 18:07:14 +0800515 if (fscrypt_is_bounce_page(target)) {
Eric Biggersd2d07272019-05-20 09:29:39 -0700516 target = fscrypt_pagecache_page(target);
Chao Yu4c8ff702019-11-01 18:07:14 +0800517 if (IS_ERR(target))
518 continue;
519 }
520 if (f2fs_is_compressed_page(target)) {
521 target = f2fs_compress_control_page(target);
522 if (IS_ERR(target))
523 continue;
524 }
Chao Yu0fd785e2016-01-18 18:24:59 +0800525
Chao Yu0c3a5792016-01-18 18:28:11 +0800526 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800527 return true;
Chao Yubab475c2018-09-27 23:41:16 +0800528 if (page && page == target)
529 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800530 if (ino && ino == ino_of_node(target))
531 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800532 }
533
Chao Yu0fd785e2016-01-18 18:24:59 +0800534 return false;
535}
536
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700537static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700538 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900539{
540 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700541 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900542
Chao Yudf0f8dc2014-03-22 14:57:23 +0800543 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900544
545 /* change META to META_FLUSH in the checkpoint procedure */
546 if (type >= META_FLUSH) {
547 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500548 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200549 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600550 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800551 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900552 }
553 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800554 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900555}
556
Jaegeuk Kima912b542017-05-10 11:18:25 -0700557static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800558 struct inode *inode, struct page *page,
559 nid_t ino, enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800560{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700561 enum temp_type temp;
Yunlong Song1e771e82018-11-13 11:57:32 +0800562 bool ret = true;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700563
564 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
Yunlong Song1e771e82018-11-13 11:57:32 +0800565 if (!force) {
566 enum page_type btype = PAGE_TYPE_OF_BIO(type);
567 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kima912b542017-05-10 11:18:25 -0700568
Yunlong Song1e771e82018-11-13 11:57:32 +0800569 down_read(&io->io_rwsem);
Chao Yu8648de22019-02-19 16:15:29 +0800570 ret = __has_merged_page(io->bio, inode, page, ino);
Yunlong Song1e771e82018-11-13 11:57:32 +0800571 up_read(&io->io_rwsem);
572 }
573 if (ret)
574 __f2fs_submit_merged_write(sbi, type, temp);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700575
576 /* TODO: use HOT temp only for meta pages now. */
577 if (type >= META)
578 break;
579 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800580}
581
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700582void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800583{
Hariprasad Kelamadcc00f2019-04-06 16:29:36 +0530584 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900585}
586
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700587void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yubab475c2018-09-27 23:41:16 +0800588 struct inode *inode, struct page *page,
589 nid_t ino, enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900590{
Chao Yubab475c2018-09-27 23:41:16 +0800591 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900592}
593
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700594void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800595{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700596 f2fs_submit_merged_write(sbi, DATA);
597 f2fs_submit_merged_write(sbi, NODE);
598 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800599}
600
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900601/*
602 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300603 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900604 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700605int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900606{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900607 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700608 struct page *page = fio->encrypted_page ?
609 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900610
Chao Yuc9b60782018-08-01 19:13:44 +0800611 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +0800612 fio->is_por ? META_POR : (__is_meta_io(fio) ?
613 META_GENERIC : DATA_GENERIC_ENHANCE)))
Chao Yu10f966b2019-06-20 11:36:14 +0800614 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +0800615
Chao Yu2ace38e2014-12-24 16:08:14 +0800616 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700617 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900618
619 /* Allocate a new bio */
Chao Yub757f6e2019-08-23 17:58:35 +0800620 bio = __bio_alloc(fio, 1);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900621
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300622 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900623 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900624 return -EFAULT;
625 }
Chao Yu78efac52018-10-22 23:24:28 +0800626
627 if (fio->io_wbc && !is_read_io(fio->op))
Tejun Heo34e51a52019-06-27 13:39:49 -0700628 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu78efac52018-10-22 23:24:28 +0800629
Mike Christie04d328d2016-06-05 14:31:55 -0500630 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900631
Jaegeuk Kim5f9abab2018-10-16 10:20:53 -0700632 inc_page_count(fio->sbi, is_read_io(fio->op) ?
633 __read_io_type(page): WB_DATA_TYPE(fio->page));
Chao Yu4c58ed02018-10-22 09:12:51 +0800634
635 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900636 return 0;
637}
638
Chao Yu8896cbd2019-07-12 16:55:41 +0800639static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
640 block_t last_blkaddr, block_t cur_blkaddr)
641{
642 if (last_blkaddr + 1 != cur_blkaddr)
643 return false;
644 return __same_bdev(sbi, cur_blkaddr, bio);
645}
646
647static bool io_type_is_mergeable(struct f2fs_bio_info *io,
648 struct f2fs_io_info *fio)
649{
650 if (io->fio.op != fio->op)
651 return false;
652 return io->fio.op_flags == fio->op_flags;
653}
654
655static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
656 struct f2fs_bio_info *io,
657 struct f2fs_io_info *fio,
658 block_t last_blkaddr,
659 block_t cur_blkaddr)
660{
Chao Yuc72db712019-07-12 16:55:42 +0800661 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
662 unsigned int filled_blocks =
663 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
664 unsigned int io_size = F2FS_IO_SIZE(sbi);
665 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
666
667 /* IOs in bio is aligned and left space of vectors is not enough */
668 if (!(filled_blocks % io_size) && left_vecs < io_size)
669 return false;
670 }
Chao Yu8896cbd2019-07-12 16:55:41 +0800671 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
672 return false;
673 return io_type_is_mergeable(io, fio);
674}
675
Chao Yu0b20fce2019-09-30 18:53:25 +0800676static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
677 struct page *page, enum temp_type temp)
678{
679 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
680 struct bio_entry *be;
681
682 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
683 be->bio = bio;
684 bio_get(bio);
685
686 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
687 f2fs_bug_on(sbi, 1);
688
689 down_write(&io->bio_list_lock);
690 list_add_tail(&be->list, &io->bio_list);
691 up_write(&io->bio_list_lock);
692}
693
694static void del_bio_entry(struct bio_entry *be)
695{
696 list_del(&be->list);
697 kmem_cache_free(bio_entry_slab, be);
698}
699
700static int add_ipu_page(struct f2fs_sb_info *sbi, struct bio **bio,
701 struct page *page)
702{
703 enum temp_type temp;
704 bool found = false;
705 int ret = -EAGAIN;
706
707 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
708 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
709 struct list_head *head = &io->bio_list;
710 struct bio_entry *be;
711
712 down_write(&io->bio_list_lock);
713 list_for_each_entry(be, head, list) {
714 if (be->bio != *bio)
715 continue;
716
717 found = true;
718
Chao Yu4c8ff702019-11-01 18:07:14 +0800719 if (bio_add_page(*bio, page, PAGE_SIZE, 0) ==
720 PAGE_SIZE) {
Chao Yu0b20fce2019-09-30 18:53:25 +0800721 ret = 0;
722 break;
723 }
724
725 /* bio is full */
726 del_bio_entry(be);
727 __submit_bio(sbi, *bio, DATA);
728 break;
729 }
730 up_write(&io->bio_list_lock);
731 }
732
733 if (ret) {
734 bio_put(*bio);
735 *bio = NULL;
736 }
737
738 return ret;
739}
740
741void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
742 struct bio **bio, struct page *page)
743{
744 enum temp_type temp;
745 bool found = false;
746 struct bio *target = bio ? *bio : NULL;
747
748 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
749 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
750 struct list_head *head = &io->bio_list;
751 struct bio_entry *be;
752
753 if (list_empty(head))
754 continue;
755
756 down_read(&io->bio_list_lock);
757 list_for_each_entry(be, head, list) {
758 if (target)
759 found = (target == be->bio);
760 else
761 found = __has_merged_page(be->bio, NULL,
762 page, 0);
763 if (found)
764 break;
765 }
766 up_read(&io->bio_list_lock);
767
768 if (!found)
769 continue;
770
771 found = false;
772
773 down_write(&io->bio_list_lock);
774 list_for_each_entry(be, head, list) {
775 if (target)
776 found = (target == be->bio);
777 else
778 found = __has_merged_page(be->bio, NULL,
779 page, 0);
780 if (found) {
781 target = be->bio;
782 del_bio_entry(be);
783 break;
784 }
785 }
786 up_write(&io->bio_list_lock);
787 }
788
789 if (found)
790 __submit_bio(sbi, target, DATA);
791 if (bio && *bio) {
792 bio_put(*bio);
793 *bio = NULL;
794 }
795}
796
Chao Yu8648de22019-02-19 16:15:29 +0800797int f2fs_merge_page_bio(struct f2fs_io_info *fio)
798{
799 struct bio *bio = *fio->bio;
800 struct page *page = fio->encrypted_page ?
801 fio->encrypted_page : fio->page;
802
803 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
804 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
Chao Yu10f966b2019-06-20 11:36:14 +0800805 return -EFSCORRUPTED;
Chao Yu8648de22019-02-19 16:15:29 +0800806
807 trace_f2fs_submit_page_bio(page, fio);
808 f2fs_trace_ios(fio, 0);
809
Chao Yu8896cbd2019-07-12 16:55:41 +0800810 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
Chao Yu0b20fce2019-09-30 18:53:25 +0800811 fio->new_blkaddr))
812 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
Chao Yu8648de22019-02-19 16:15:29 +0800813alloc_new:
814 if (!bio) {
Chao Yub757f6e2019-08-23 17:58:35 +0800815 bio = __bio_alloc(fio, BIO_MAX_PAGES);
Chao Yu8648de22019-02-19 16:15:29 +0800816 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Chao Yu8648de22019-02-19 16:15:29 +0800817
Chao Yu0b20fce2019-09-30 18:53:25 +0800818 add_bio_entry(fio->sbi, bio, page, fio->temp);
819 } else {
820 if (add_ipu_page(fio->sbi, &bio, page))
821 goto alloc_new;
Chao Yu8648de22019-02-19 16:15:29 +0800822 }
823
824 if (fio->io_wbc)
Linus Torvalds9637d512019-07-15 21:20:52 -0700825 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Chao Yu8648de22019-02-19 16:15:29 +0800826
827 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
828
829 *fio->last_block = fio->new_blkaddr;
830 *fio->bio = bio;
831
832 return 0;
833}
834
Chao Yufe16efe2018-05-28 23:47:18 +0800835void f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900836{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700837 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900838 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700839 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700840 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900841
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700842 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900843
Chao Yufb830fc2017-05-19 23:37:01 +0800844 down_write(&io->io_rwsem);
845next:
846 if (fio->in_list) {
847 spin_lock(&io->io_lock);
848 if (list_empty(&io->io_list)) {
849 spin_unlock(&io->io_lock);
Chao Yufe16efe2018-05-28 23:47:18 +0800850 goto out;
Chao Yufb830fc2017-05-19 23:37:01 +0800851 }
852 fio = list_first_entry(&io->io_list,
853 struct f2fs_io_info, list);
854 list_del(&fio->list);
855 spin_unlock(&io->io_lock);
856 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900857
Chao Yu93770ab2019-04-15 15:26:32 +0800858 verify_fio_blkaddr(fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900859
Chao Yu4c8ff702019-11-01 18:07:14 +0800860 if (fio->encrypted_page)
861 bio_page = fio->encrypted_page;
862 else if (fio->compressed_page)
863 bio_page = fio->compressed_page;
864 else
865 bio_page = fio->page;
Chao Yu36951b32016-11-16 10:41:20 +0800866
Thomas Meyerebf7c522017-10-07 16:02:21 +0200867 /* set submitted = true as a return value */
868 fio->submitted = true;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800869
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700870 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900871
Chao Yu8896cbd2019-07-12 16:55:41 +0800872 if (io->bio && !io_is_mergeable(sbi, io->bio, io, fio,
873 io->last_block_in_bio, fio->new_blkaddr))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900874 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900875alloc_new:
876 if (io->bio == NULL) {
Chao Yu8223ecc2019-08-28 17:33:38 +0800877 if (F2FS_IO_ALIGNED(sbi) &&
878 (fio->type == DATA || fio->type == NODE) &&
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800879 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700880 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Chao Yufe16efe2018-05-28 23:47:18 +0800881 fio->retry = true;
882 goto skip;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800883 }
Chao Yub757f6e2019-08-23 17:58:35 +0800884 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900885 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900886 }
887
Jaegeuk Kima912b542017-05-10 11:18:25 -0700888 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900889 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900890 goto alloc_new;
891 }
892
Yufen Yu578c6472018-01-09 19:33:39 +0800893 if (fio->io_wbc)
Tejun Heo34e51a52019-06-27 13:39:49 -0700894 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
Yufen Yu578c6472018-01-09 19:33:39 +0800895
Chao Yu7a9d7542016-02-22 18:36:38 +0800896 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700897 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800898
899 trace_f2fs_submit_page_write(fio->page, fio);
Chao Yufe16efe2018-05-28 23:47:18 +0800900skip:
Chao Yufb830fc2017-05-19 23:37:01 +0800901 if (fio->in_list)
902 goto next;
Chao Yufe16efe2018-05-28 23:47:18 +0800903out:
Daniel Rosenberg43549942018-08-20 19:21:43 -0700904 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
Chao Yu00e09c02019-08-23 17:58:36 +0800905 !f2fs_is_checkpoint_ready(sbi))
Jaegeuk Kim5ce80582018-09-06 11:40:12 -0700906 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800907 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900908}
909
Eric Biggers95ae2512019-07-22 09:26:24 -0700910static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
911{
912 return fsverity_active(inode) &&
913 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
914}
915
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700916static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
Eric Biggers95ae2512019-07-22 09:26:24 -0700917 unsigned nr_pages, unsigned op_flag,
918 pgoff_t first_idx)
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700919{
920 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700921 struct bio *bio;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700922 struct bio_post_read_ctx *ctx;
923 unsigned int post_read_steps = 0;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700924
Eric Biggers6dbb1792018-04-18 11:09:48 -0700925 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
926 if (!bio)
927 return ERR_PTR(-ENOMEM);
928 f2fs_target_device(sbi, blkaddr, bio);
929 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kime2e59412018-06-21 11:29:43 -0700930 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
Eric Biggers6dbb1792018-04-18 11:09:48 -0700931
932 if (f2fs_encrypted_file(inode))
933 post_read_steps |= 1 << STEP_DECRYPT;
Chao Yu4c8ff702019-11-01 18:07:14 +0800934 if (f2fs_compressed_file(inode))
935 post_read_steps |= 1 << STEP_DECOMPRESS;
Eric Biggers95ae2512019-07-22 09:26:24 -0700936 if (f2fs_need_verity(inode, first_idx))
937 post_read_steps |= 1 << STEP_VERITY;
938
Eric Biggers6dbb1792018-04-18 11:09:48 -0700939 if (post_read_steps) {
940 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
941 if (!ctx) {
942 bio_put(bio);
943 return ERR_PTR(-ENOMEM);
944 }
945 ctx->bio = bio;
Chao Yu4c8ff702019-11-01 18:07:14 +0800946 ctx->sbi = sbi;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700947 ctx->enabled_steps = post_read_steps;
948 bio->bi_private = ctx;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700949 }
950
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700951 return bio;
952}
953
Chao Yu4c8ff702019-11-01 18:07:14 +0800954static void f2fs_release_read_bio(struct bio *bio)
955{
956 if (bio->bi_private)
957 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
958 bio_put(bio);
959}
960
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700961/* This can handle encryption stuffs */
962static int f2fs_submit_page_read(struct inode *inode, struct page *page,
963 block_t blkaddr)
964{
Chao Yu93770ab2019-04-15 15:26:32 +0800965 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
966 struct bio *bio;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700967
Eric Biggers95ae2512019-07-22 09:26:24 -0700968 bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0, page->index);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700969 if (IS_ERR(bio))
970 return PTR_ERR(bio);
971
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -0700972 /* wait for GCed page writeback via META_MAPPING */
973 f2fs_wait_on_block_writeback(inode, blkaddr);
974
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700975 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
976 bio_put(bio);
977 return -EFAULT;
978 }
Jaegeuk Kimfb7d70d2018-09-25 13:54:33 -0700979 ClearPageError(page);
Chao Yu93770ab2019-04-15 15:26:32 +0800980 inc_page_count(sbi, F2FS_RD_DATA);
981 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700982 return 0;
983}
984
Chao Yu46008c62016-05-09 19:56:30 +0800985static void __set_data_blkaddr(struct dnode_of_data *dn)
986{
987 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
988 __le32 *addr_array;
Chao Yu7a2af762017-07-19 00:19:06 +0800989 int base = 0;
990
991 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
992 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800993
994 /* Get physical address of data block */
995 addr_array = blkaddr_in_node(rn);
Chao Yu7a2af762017-07-19 00:19:06 +0800996 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800997}
998
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900999/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001000 * Lock ordering for the change of data block address:
1001 * ->data_page
1002 * ->node_page
1003 * update block addresses in the node page
1004 */
Chao Yu4d57b862018-05-30 00:20:41 +08001005void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001006{
Chao Yubae0ee72018-12-25 17:43:42 +08001007 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +08001008 __set_data_blkaddr(dn);
1009 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -08001010 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001011}
1012
Chao Yuf28b3432016-02-24 17:16:47 +08001013void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1014{
1015 dn->data_blkaddr = blkaddr;
Chao Yu4d57b862018-05-30 00:20:41 +08001016 f2fs_set_data_blkaddr(dn);
Chao Yuf28b3432016-02-24 17:16:47 +08001017 f2fs_update_extent_cache(dn);
1018}
1019
Chao Yu46008c62016-05-09 19:56:30 +08001020/* dn->ofs_in_node will be returned with up-to-date last block pointer */
Chao Yu4d57b862018-05-30 00:20:41 +08001021int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001022{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001023 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +08001024 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001025
Chao Yu46008c62016-05-09 19:56:30 +08001026 if (!count)
1027 return 0;
1028
Jaegeuk Kim91942322016-05-20 10:13:22 -07001029 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001030 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +08001031 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1032 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001033
Chao Yu46008c62016-05-09 19:56:30 +08001034 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1035 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +09001036
Chao Yubae0ee72018-12-25 17:43:42 +08001037 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +08001038
1039 for (; count > 0; dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +08001040 block_t blkaddr = datablock_addr(dn->inode,
1041 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +08001042 if (blkaddr == NULL_ADDR) {
1043 dn->data_blkaddr = NEW_ADDR;
1044 __set_data_blkaddr(dn);
1045 count--;
1046 }
1047 }
1048
1049 if (set_page_dirty(dn->node_page))
1050 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001051 return 0;
1052}
1053
Chao Yu46008c62016-05-09 19:56:30 +08001054/* Should keep dn->ofs_in_node unchanged */
Chao Yu4d57b862018-05-30 00:20:41 +08001055int f2fs_reserve_new_block(struct dnode_of_data *dn)
Chao Yu46008c62016-05-09 19:56:30 +08001056{
1057 unsigned int ofs_in_node = dn->ofs_in_node;
1058 int ret;
1059
Chao Yu4d57b862018-05-30 00:20:41 +08001060 ret = f2fs_reserve_new_blocks(dn, 1);
Chao Yu46008c62016-05-09 19:56:30 +08001061 dn->ofs_in_node = ofs_in_node;
1062 return ret;
1063}
1064
Huajun Lib6009652013-11-10 23:13:18 +08001065int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1066{
1067 bool need_put = dn->inode_page ? false : true;
1068 int err;
1069
Chao Yu4d57b862018-05-30 00:20:41 +08001070 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
Huajun Lib6009652013-11-10 23:13:18 +08001071 if (err)
1072 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +09001073
Huajun Lib6009652013-11-10 23:13:18 +08001074 if (dn->data_blkaddr == NULL_ADDR)
Chao Yu4d57b862018-05-30 00:20:41 +08001075 err = f2fs_reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001076 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +08001077 f2fs_put_dnode(dn);
1078 return err;
1079}
1080
Fan Li759af1c2015-08-05 15:52:16 +08001081int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001082{
Hou Pengyange15882b2017-02-23 09:18:05 +00001083 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +08001084 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +08001085
Fan Li759af1c2015-08-05 15:52:16 +08001086 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1087 dn->data_blkaddr = ei.blk + index - ei.fofs;
1088 return 0;
Chao Yu429511c2015-02-05 17:54:31 +08001089 }
1090
Fan Li759af1c2015-08-05 15:52:16 +08001091 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001092}
1093
Chao Yu4d57b862018-05-30 00:20:41 +08001094struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -05001095 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001096{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001097 struct address_space *mapping = inode->i_mapping;
1098 struct dnode_of_data dn;
1099 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +00001100 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001101 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001102
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001103 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001104 if (!page)
1105 return ERR_PTR(-ENOMEM);
1106
Chao Yucb3bc9e2015-02-05 18:03:40 +08001107 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1108 dn.data_blkaddr = ei.blk + index - ei.fofs;
Chao Yu93770ab2019-04-15 15:26:32 +08001109 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1110 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001111 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001112 goto put_err;
1113 }
Chao Yucb3bc9e2015-02-05 18:03:40 +08001114 goto got_it;
1115 }
1116
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001117 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001118 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001119 if (err)
1120 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001121 f2fs_put_dnode(&dn);
1122
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001123 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001124 err = -ENOENT;
1125 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001126 }
Chao Yu93770ab2019-04-15 15:26:32 +08001127 if (dn.data_blkaddr != NEW_ADDR &&
1128 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1129 dn.data_blkaddr,
1130 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001131 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08001132 goto put_err;
1133 }
Chao Yucb3bc9e2015-02-05 18:03:40 +08001134got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001135 if (PageUptodate(page)) {
1136 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001137 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001138 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001139
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001140 /*
1141 * A new dentry page is allocated but not able to be written, since its
1142 * new inode page couldn't be allocated due to -ENOSPC.
1143 * In such the case, its blkaddr can be remained as NEW_ADDR.
Chao Yu4d57b862018-05-30 00:20:41 +08001144 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1145 * f2fs_init_inode_metadata.
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001146 */
1147 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001148 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001149 if (!PageUptodate(page))
1150 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001151 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001152 return page;
1153 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001154
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001155 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001156 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001157 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001158 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001159
1160put_err:
1161 f2fs_put_page(page, 1);
1162 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001163}
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001164
Chao Yu4d57b862018-05-30 00:20:41 +08001165struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001166{
1167 struct address_space *mapping = inode->i_mapping;
1168 struct page *page;
1169
1170 page = find_get_page(mapping, index);
1171 if (page && PageUptodate(page))
1172 return page;
1173 f2fs_put_page(page, 0);
1174
Chao Yu4d57b862018-05-30 00:20:41 +08001175 page = f2fs_get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001176 if (IS_ERR(page))
1177 return page;
1178
1179 if (PageUptodate(page))
1180 return page;
1181
1182 wait_on_page_locked(page);
1183 if (unlikely(!PageUptodate(page))) {
1184 f2fs_put_page(page, 0);
1185 return ERR_PTR(-EIO);
1186 }
1187 return page;
1188}
1189
1190/*
1191 * If it tries to access a hole, return an error.
1192 * Because, the callers, functions in dir.c and GC, should be able to know
1193 * whether this page exists or not.
1194 */
Chao Yu4d57b862018-05-30 00:20:41 +08001195struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001196 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001197{
1198 struct address_space *mapping = inode->i_mapping;
1199 struct page *page;
1200repeat:
Chao Yu4d57b862018-05-30 00:20:41 +08001201 page = f2fs_get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -07001202 if (IS_ERR(page))
1203 return page;
1204
1205 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001206 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001207 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001208 f2fs_put_page(page, 1);
1209 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001210 }
Chao Yu1563ac72016-07-03 22:05:12 +08001211 if (unlikely(!PageUptodate(page))) {
1212 f2fs_put_page(page, 1);
1213 return ERR_PTR(-EIO);
1214 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001215 return page;
1216}
1217
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001218/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001219 * Caller ensures that this data page is never allocated.
1220 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +09001221 *
Chao Yu4f4124d2013-12-21 18:02:14 +08001222 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1223 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +08001224 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1225 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001226 */
Chao Yu4d57b862018-05-30 00:20:41 +08001227struct page *f2fs_get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +09001228 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001229{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001230 struct address_space *mapping = inode->i_mapping;
1231 struct page *page;
1232 struct dnode_of_data dn;
1233 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -08001234
Jaegeuk Kima56c7c62015-10-09 15:11:38 -07001235 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +08001236 if (!page) {
1237 /*
1238 * before exiting, we should make sure ipage will be released
1239 * if any error occur.
1240 */
1241 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001242 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +08001243 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001244
Jaegeuk Kima8865372013-12-27 17:04:17 +09001245 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +08001246 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001247 if (err) {
1248 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001249 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001250 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001251 if (!ipage)
1252 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001253
1254 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001255 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001256
1257 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001258 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001259 if (!PageUptodate(page))
1260 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001261 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001262 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001263
Jaegeuk Kim76121182016-01-01 22:03:47 -08001264 /* if ipage exists, blkaddr should be NEW_ADDR */
1265 f2fs_bug_on(F2FS_I_SB(inode), ipage);
Chao Yu4d57b862018-05-30 00:20:41 +08001266 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001267 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -08001268 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001269 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001270got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +08001271 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -07001272 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001273 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001274 return page;
1275}
1276
Hyunchul Leed5097be2017-11-28 09:23:00 +09001277static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001278{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001279 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001280 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001281 struct node_info ni;
Chao Yu6aa58d82018-08-14 22:37:25 +08001282 block_t old_blkaddr;
Chao Yu46008c62016-05-09 19:56:30 +08001283 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +08001284 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001285
Jaegeuk Kim91942322016-05-20 10:13:22 -07001286 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001287 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +08001288
Chao Yu77357302018-07-17 00:02:17 +08001289 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1290 if (err)
1291 return err;
1292
Chao Yu7a2af762017-07-19 00:19:06 +08001293 dn->data_blkaddr = datablock_addr(dn->inode,
1294 dn->node_page, dn->ofs_in_node);
Chao Yuf847c692018-09-27 18:34:52 +08001295 if (dn->data_blkaddr != NULL_ADDR)
Chao Yudf6136e2015-03-23 10:33:37 +08001296 goto alloc;
1297
Chao Yu0abd6752017-07-09 00:13:07 +08001298 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1299 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001300
Chao Yudf6136e2015-03-23 10:33:37 +08001301alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001302 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Chao Yu6aa58d82018-08-14 22:37:25 +08001303 old_blkaddr = dn->data_blkaddr;
1304 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001305 &sum, seg_type, NULL, false);
Chao Yu6aa58d82018-08-14 22:37:25 +08001306 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1307 invalidate_mapping_pages(META_MAPPING(sbi),
1308 old_blkaddr, old_blkaddr);
Chao Yu86f35dc2019-08-28 17:33:35 +08001309 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001310
Jaegeuk Kim0a4daae2018-09-19 15:28:40 -07001311 /*
1312 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1313 * data from unwritten block via dio_read.
1314 */
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001315 return 0;
1316}
1317
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001318int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001319{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001320 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +08001321 struct f2fs_map_blocks map;
Chao Yud6d478a12018-01-03 17:30:19 +08001322 int flag;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001323 int err = 0;
Chao Yud6d478a12018-01-03 17:30:19 +08001324 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001325
Jaegeuk Kim0080c502016-05-07 08:52:57 -07001326 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +08001327 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1328 if (map.m_len > map.m_lblk)
1329 map.m_len -= map.m_lblk;
1330 else
1331 map.m_len = 0;
1332
Chao Yuda859852016-01-26 15:42:58 +08001333 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08001334 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001335 map.m_seg_type = NO_CHECK_TYPE;
Chao Yuf9d6d052018-11-13 14:33:45 +08001336 map.m_may_create = true;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001337
Chao Yud6d478a12018-01-03 17:30:19 +08001338 if (direct_io) {
Chao Yu4d57b862018-05-30 00:20:41 +08001339 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
Chao Yuf847c692018-09-27 18:34:52 +08001340 flag = f2fs_force_buffered_io(inode, iocb, from) ?
Chao Yud6d478a12018-01-03 17:30:19 +08001341 F2FS_GET_BLOCK_PRE_AIO :
1342 F2FS_GET_BLOCK_PRE_DIO;
1343 goto map_blocks;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001344 }
Chao Yuf2470372017-07-19 00:19:05 +08001345 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001346 err = f2fs_convert_inline_inode(inode);
1347 if (err)
1348 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001349 }
Chao Yud6d478a12018-01-03 17:30:19 +08001350 if (f2fs_has_inline_data(inode))
Sheng Yong250066452017-11-22 18:23:39 +08001351 return err;
Chao Yud6d478a12018-01-03 17:30:19 +08001352
1353 flag = F2FS_GET_BLOCK_PRE_AIO;
1354
1355map_blocks:
1356 err = f2fs_map_blocks(inode, &map, 1, flag);
1357 if (map.m_len > 0 && err == -ENOSPC) {
1358 if (!direct_io)
1359 set_inode_flag(inode, FI_NO_PREALLOC);
1360 err = 0;
Sheng Yong250066452017-11-22 18:23:39 +08001361 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001362 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001363}
1364
Chao Yu39a86952018-09-27 18:33:18 +08001365void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
Yunlei He59c90812017-03-13 20:22:18 +08001366{
1367 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1368 if (lock)
1369 down_read(&sbi->node_change);
1370 else
1371 up_read(&sbi->node_change);
1372 } else {
1373 if (lock)
1374 f2fs_lock_op(sbi);
1375 else
1376 f2fs_unlock_op(sbi);
1377 }
1378}
1379
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001380/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001381 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1382 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +08001383 * If original data blocks are allocated, then give them to blockdev.
1384 * Otherwise,
1385 * a. preallocate requested block addresses
1386 * b. do not use extent cache for better performance
1387 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001388 */
Chao Yud323d002015-10-27 09:53:45 +08001389int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +08001390 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001391{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001392 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001393 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +08001394 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf9d6d052018-11-13 14:33:45 +08001395 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +08001396 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001397 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +08001398 unsigned int ofs_in_node, last_ofs_in_node;
1399 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +00001400 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +08001401 block_t blkaddr;
Chao Yuc4020b22018-01-11 14:42:30 +08001402 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001403
Chao Yudfd02e42016-08-20 15:12:01 +08001404 if (!maxblocks)
1405 return 0;
1406
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001407 map->m_len = 0;
1408 map->m_flags = 0;
1409
1410 /* it only supports block size == page size */
1411 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +08001412 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001413
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001414 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jia Zhuf4f0b672018-11-20 04:29:35 +08001415 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1416 map->m_may_create)
1417 goto next_dnode;
1418
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001419 map->m_pblk = ei.blk + pgofs - ei.fofs;
1420 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1421 map->m_flags = F2FS_MAP_MAPPED;
Chao Yuc4020b22018-01-11 14:42:30 +08001422 if (map->m_next_extent)
1423 *map->m_next_extent = pgofs + map->m_len;
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301424
1425 /* for hardware encryption, but to avoid potential issue in future */
1426 if (flag == F2FS_GET_BLOCK_DIO)
1427 f2fs_wait_on_block_writeback_range(inode,
1428 map->m_pblk, map->m_len);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001429 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001430 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001431
Chao Yu4fe71e82016-01-26 15:37:38 +08001432next_dnode:
Chao Yuf9d6d052018-11-13 14:33:45 +08001433 if (map->m_may_create)
Yunlei He59c90812017-03-13 20:22:18 +08001434 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001435
1436 /* When reading holes, we need its node page */
1437 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001438 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001439 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +08001440 if (flag == F2FS_GET_BLOCK_BMAP)
1441 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +08001442 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001443 err = 0;
Chao Yuda859852016-01-26 15:42:58 +08001444 if (map->m_next_pgofs)
1445 *map->m_next_pgofs =
Chao Yu4d57b862018-05-30 00:20:41 +08001446 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuc4020b22018-01-11 14:42:30 +08001447 if (map->m_next_extent)
1448 *map->m_next_extent =
Chao Yu4d57b862018-05-30 00:20:41 +08001449 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +08001450 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001451 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001452 }
Chao Yu973163f2015-09-18 16:51:51 +08001453
Chao Yuc4020b22018-01-11 14:42:30 +08001454 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +08001455 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +01001456 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +08001457 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001458
Chao Yu4fe71e82016-01-26 15:37:38 +08001459next_block:
Chao Yu7a2af762017-07-19 00:19:06 +08001460 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +08001461
Chao Yuc9b60782018-08-01 19:13:44 +08001462 if (__is_valid_data_blkaddr(blkaddr) &&
Chao Yu93770ab2019-04-15 15:26:32 +08001463 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001464 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08001465 goto sync_out;
1466 }
1467
Chao Yu93770ab2019-04-15 15:26:32 +08001468 if (__is_valid_data_blkaddr(blkaddr)) {
Chao Yuf847c692018-09-27 18:34:52 +08001469 /* use out-place-update for driect IO under LFS mode */
Chao Yuf9d6d052018-11-13 14:33:45 +08001470 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1471 map->m_may_create) {
Chao Yuf847c692018-09-27 18:34:52 +08001472 err = __allocate_data_block(&dn, map->m_seg_type);
Chao Yu05e3600612019-08-28 17:33:36 +08001473 if (err)
1474 goto sync_out;
1475 blkaddr = dn.data_blkaddr;
1476 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yuf847c692018-09-27 18:34:52 +08001477 }
1478 } else {
Fan Li7df3a432015-12-17 13:20:59 +08001479 if (create) {
1480 if (unlikely(f2fs_cp_error(sbi))) {
1481 err = -EIO;
1482 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +08001483 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001484 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +08001485 if (blkaddr == NULL_ADDR) {
1486 prealloc++;
1487 last_ofs_in_node = dn.ofs_in_node;
1488 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001489 } else {
Jaegeuk Kim0a4daae2018-09-19 15:28:40 -07001490 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1491 flag != F2FS_GET_BLOCK_DIO);
Hyunchul Leed5097be2017-11-28 09:23:00 +09001492 err = __allocate_data_block(&dn,
1493 map->m_seg_type);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001494 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001495 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001496 }
Fan Li7df3a432015-12-17 13:20:59 +08001497 if (err)
1498 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +08001499 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001500 blkaddr = dn.data_blkaddr;
1501 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001502 if (flag == F2FS_GET_BLOCK_BMAP) {
1503 map->m_pblk = 0;
1504 goto sync_out;
1505 }
Chao Yuc4020b22018-01-11 14:42:30 +08001506 if (flag == F2FS_GET_BLOCK_PRECACHE)
1507 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001508 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1509 blkaddr == NULL_ADDR) {
1510 if (map->m_next_pgofs)
1511 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001512 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001513 }
Chao Yuf3d98e72018-01-10 18:18:52 +08001514 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1515 /* for defragment case */
1516 if (map->m_next_pgofs)
1517 *map->m_next_pgofs = pgofs + 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001518 goto sync_out;
Chao Yuf3d98e72018-01-10 18:18:52 +08001519 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001520 }
1521 }
Fan Li7df3a432015-12-17 13:20:59 +08001522
Chao Yu46008c62016-05-09 19:56:30 +08001523 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1524 goto skip;
1525
Chao Yu4fe71e82016-01-26 15:37:38 +08001526 if (map->m_len == 0) {
1527 /* preallocated unwritten block should be mapped for fiemap. */
1528 if (blkaddr == NEW_ADDR)
1529 map->m_flags |= F2FS_MAP_UNWRITTEN;
1530 map->m_flags |= F2FS_MAP_MAPPED;
1531
1532 map->m_pblk = blkaddr;
1533 map->m_len = 1;
1534 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001535 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001536 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001537 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001538 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001539 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001540 } else {
1541 goto sync_out;
1542 }
1543
Chao Yu46008c62016-05-09 19:56:30 +08001544skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001545 dn.ofs_in_node++;
1546 pgofs++;
1547
Chao Yu46008c62016-05-09 19:56:30 +08001548 /* preallocate blocks in batch for one dnode page */
1549 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1550 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001551
Chao Yu46008c62016-05-09 19:56:30 +08001552 dn.ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001553 err = f2fs_reserve_new_blocks(&dn, prealloc);
Chao Yu46008c62016-05-09 19:56:30 +08001554 if (err)
1555 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001556
Chao Yu46008c62016-05-09 19:56:30 +08001557 map->m_len += dn.ofs_in_node - ofs_in_node;
1558 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1559 err = -ENOSPC;
1560 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001561 }
Chao Yu46008c62016-05-09 19:56:30 +08001562 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001563 }
1564
Chao Yu46008c62016-05-09 19:56:30 +08001565 if (pgofs >= end)
1566 goto sync_out;
1567 else if (dn.ofs_in_node < end_offset)
1568 goto next_block;
1569
Chao Yuc4020b22018-01-11 14:42:30 +08001570 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1571 if (map->m_flags & F2FS_MAP_MAPPED) {
1572 unsigned int ofs = start_pgofs - map->m_lblk;
1573
1574 f2fs_update_extent_cache_range(&dn,
1575 start_pgofs, map->m_pblk + ofs,
1576 map->m_len - ofs);
1577 }
1578 }
1579
Chao Yu46008c62016-05-09 19:56:30 +08001580 f2fs_put_dnode(&dn);
1581
Chao Yuf9d6d052018-11-13 14:33:45 +08001582 if (map->m_may_create) {
Yunlei He59c90812017-03-13 20:22:18 +08001583 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001584 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001585 }
Chao Yu46008c62016-05-09 19:56:30 +08001586 goto next_dnode;
1587
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001588sync_out:
Sahitya Tummala1e78e8b2018-10-10 10:56:22 +05301589
1590 /* for hardware encryption, but to avoid potential issue in future */
1591 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1592 f2fs_wait_on_block_writeback_range(inode,
1593 map->m_pblk, map->m_len);
1594
Chao Yuc4020b22018-01-11 14:42:30 +08001595 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1596 if (map->m_flags & F2FS_MAP_MAPPED) {
1597 unsigned int ofs = start_pgofs - map->m_lblk;
1598
1599 f2fs_update_extent_cache_range(&dn,
1600 start_pgofs, map->m_pblk + ofs,
1601 map->m_len - ofs);
1602 }
1603 if (map->m_next_extent)
1604 *map->m_next_extent = pgofs + 1;
1605 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001606 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001607unlock_out:
Chao Yuf9d6d052018-11-13 14:33:45 +08001608 if (map->m_may_create) {
Yunlei He59c90812017-03-13 20:22:18 +08001609 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001610 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001611 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001612out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001613 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001614 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001615}
1616
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001617bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1618{
1619 struct f2fs_map_blocks map;
1620 block_t last_lblk;
1621 int err;
1622
1623 if (pos + len > i_size_read(inode))
1624 return false;
1625
1626 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1627 map.m_next_pgofs = NULL;
1628 map.m_next_extent = NULL;
1629 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhuf4f0b672018-11-20 04:29:35 +08001630 map.m_may_create = false;
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001631 last_lblk = F2FS_BLK_ALIGN(pos + len);
1632
1633 while (map.m_lblk < last_lblk) {
1634 map.m_len = last_lblk - map.m_lblk;
1635 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1636 if (err || map.m_len == 0)
1637 return false;
1638 map.m_lblk += map.m_len;
1639 }
1640 return true;
1641}
1642
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001643static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001644 struct buffer_head *bh, int create, int flag,
Chao Yuf9d6d052018-11-13 14:33:45 +08001645 pgoff_t *next_pgofs, int seg_type, bool may_write)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001646{
1647 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001648 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001649
1650 map.m_lblk = iblock;
1651 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001652 map.m_next_pgofs = next_pgofs;
Chao Yuc4020b22018-01-11 14:42:30 +08001653 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001654 map.m_seg_type = seg_type;
Chao Yuf9d6d052018-11-13 14:33:45 +08001655 map.m_may_create = may_write;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001656
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001657 err = f2fs_map_blocks(inode, &map, create, flag);
1658 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001659 map_bh(bh, inode->i_sb, map.m_pblk);
1660 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001661 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001662 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001663 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001664}
1665
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001666static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001667 struct buffer_head *bh_result, int create, int flag,
1668 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001669{
Chao Yuda859852016-01-26 15:42:58 +08001670 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001671 flag, next_pgofs,
Chao Yuf9d6d052018-11-13 14:33:45 +08001672 NO_CHECK_TYPE, create);
1673}
1674
1675static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1676 struct buffer_head *bh_result, int create)
1677{
1678 return __get_data_block(inode, iblock, bh_result, create,
1679 F2FS_GET_BLOCK_DIO, NULL,
1680 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
Jaegeuk Kim75a037f2019-07-31 13:27:05 -07001681 IS_SWAPFILE(inode) ? false : true);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001682}
1683
Chao Yue2b4e2b2015-08-19 19:11:19 +08001684static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001685 struct buffer_head *bh_result, int create)
1686{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001687 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuf9d6d052018-11-13 14:33:45 +08001688 F2FS_GET_BLOCK_DIO, NULL,
1689 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1690 false);
Chao Yue2b4e2b2015-08-19 19:11:19 +08001691}
1692
1693static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1694 struct buffer_head *bh_result, int create)
1695{
Yunlei He179448b2015-12-28 21:48:32 +08001696 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001697 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001698 return -EFBIG;
1699
Chao Yue2b4e2b2015-08-19 19:11:19 +08001700 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001701 F2FS_GET_BLOCK_BMAP, NULL,
Chao Yuf9d6d052018-11-13 14:33:45 +08001702 NO_CHECK_TYPE, create);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001703}
1704
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001705static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1706{
1707 return (offset >> inode->i_blkbits);
1708}
1709
1710static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1711{
1712 return (blk << inode->i_blkbits);
1713}
1714
Chao Yu442a9db2018-01-11 14:39:57 +08001715static int f2fs_xattr_fiemap(struct inode *inode,
1716 struct fiemap_extent_info *fieinfo)
1717{
1718 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1719 struct page *page;
1720 struct node_info ni;
1721 __u64 phys = 0, len;
1722 __u32 flags;
1723 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1724 int err = 0;
1725
1726 if (f2fs_has_inline_xattr(inode)) {
1727 int offset;
1728
1729 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1730 inode->i_ino, false);
1731 if (!page)
1732 return -ENOMEM;
1733
Chao Yu77357302018-07-17 00:02:17 +08001734 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1735 if (err) {
1736 f2fs_put_page(page, 1);
1737 return err;
1738 }
Chao Yu442a9db2018-01-11 14:39:57 +08001739
1740 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1741 offset = offsetof(struct f2fs_inode, i_addr) +
1742 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yub323fd22018-01-17 16:31:36 +08001743 get_inline_xattr_addrs(inode));
Chao Yu442a9db2018-01-11 14:39:57 +08001744
1745 phys += offset;
1746 len = inline_xattr_size(inode);
1747
1748 f2fs_put_page(page, 1);
1749
1750 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1751
1752 if (!xnid)
1753 flags |= FIEMAP_EXTENT_LAST;
1754
1755 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1756 if (err || err == 1)
1757 return err;
1758 }
1759
1760 if (xnid) {
1761 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1762 if (!page)
1763 return -ENOMEM;
1764
Chao Yu77357302018-07-17 00:02:17 +08001765 err = f2fs_get_node_info(sbi, xnid, &ni);
1766 if (err) {
1767 f2fs_put_page(page, 1);
1768 return err;
1769 }
Chao Yu442a9db2018-01-11 14:39:57 +08001770
1771 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1772 len = inode->i_sb->s_blocksize;
1773
1774 f2fs_put_page(page, 1);
1775
1776 flags = FIEMAP_EXTENT_LAST;
1777 }
1778
1779 if (phys)
1780 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1781
1782 return (err < 0 ? err : 0);
1783}
1784
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001785int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1786 u64 start, u64 len)
1787{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001788 struct buffer_head map_bh;
1789 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001790 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001791 u64 logical = 0, phys = 0, size = 0;
1792 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001793 int ret = 0;
1794
Chao Yuc4020b22018-01-11 14:42:30 +08001795 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1796 ret = f2fs_precache_extents(inode);
1797 if (ret)
1798 return ret;
1799 }
1800
Chao Yu442a9db2018-01-11 14:39:57 +08001801 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001802 if (ret)
1803 return ret;
1804
Chao Yuf1b43d42018-01-11 14:37:35 +08001805 inode_lock(inode);
1806
Chao Yu442a9db2018-01-11 14:39:57 +08001807 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1808 ret = f2fs_xattr_fiemap(inode, fieinfo);
1809 goto out;
1810 }
1811
Chao Yu7975f342019-07-22 18:03:50 +08001812 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001813 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1814 if (ret != -EAGAIN)
Chao Yuf1b43d42018-01-11 14:37:35 +08001815 goto out;
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001816 }
1817
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001818 if (logical_to_blk(inode, len) == 0)
1819 len = blk_to_logical(inode, 1);
1820
1821 start_blk = logical_to_blk(inode, start);
1822 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001823
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001824next:
1825 memset(&map_bh, 0, sizeof(struct buffer_head));
1826 map_bh.b_size = len;
1827
Chao Yue2b4e2b2015-08-19 19:11:19 +08001828 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001829 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001830 if (ret)
1831 goto out;
1832
1833 /* HOLE */
1834 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001835 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001836
1837 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1838 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001839 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001840
Fan Li9a950d52015-12-26 18:07:41 +08001841 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001842 }
Fan Li9a950d52015-12-26 18:07:41 +08001843
Chao Yuda5af122016-01-08 20:19:27 +08001844 if (size) {
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05301845 if (IS_ENCRYPTED(inode))
Chao Yuda5af122016-01-08 20:19:27 +08001846 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1847
Fan Li9a950d52015-12-26 18:07:41 +08001848 ret = fiemap_fill_next_extent(fieinfo, logical,
1849 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001850 }
Fan Li9a950d52015-12-26 18:07:41 +08001851
1852 if (start_blk > last_blk || ret)
1853 goto out;
1854
1855 logical = blk_to_logical(inode, start_blk);
1856 phys = blk_to_logical(inode, map_bh.b_blocknr);
1857 size = map_bh.b_size;
1858 flags = 0;
1859 if (buffer_unwritten(&map_bh))
1860 flags = FIEMAP_EXTENT_UNWRITTEN;
1861
1862 start_blk += logical_to_blk(inode, size);
1863
1864prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001865 cond_resched();
1866 if (fatal_signal_pending(current))
1867 ret = -EINTR;
1868 else
1869 goto next;
1870out:
1871 if (ret == 1)
1872 ret = 0;
1873
Al Viro59551022016-01-22 15:40:57 -05001874 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001875 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001876}
1877
Eric Biggers95ae2512019-07-22 09:26:24 -07001878static inline loff_t f2fs_readpage_limit(struct inode *inode)
1879{
1880 if (IS_ENABLED(CONFIG_FS_VERITY) &&
1881 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1882 return inode->i_sb->s_maxbytes;
1883
1884 return i_size_read(inode);
1885}
1886
Chao Yu2df0ab02019-03-25 21:07:30 +08001887static int f2fs_read_single_page(struct inode *inode, struct page *page,
1888 unsigned nr_pages,
1889 struct f2fs_map_blocks *map,
1890 struct bio **bio_ret,
1891 sector_t *last_block_in_bio,
1892 bool is_readahead)
1893{
1894 struct bio *bio = *bio_ret;
1895 const unsigned blkbits = inode->i_blkbits;
1896 const unsigned blocksize = 1 << blkbits;
1897 sector_t block_in_file;
1898 sector_t last_block;
1899 sector_t last_block_in_file;
1900 sector_t block_nr;
1901 int ret = 0;
1902
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001903 block_in_file = (sector_t)page_index(page);
Chao Yu2df0ab02019-03-25 21:07:30 +08001904 last_block = block_in_file + nr_pages;
Eric Biggers95ae2512019-07-22 09:26:24 -07001905 last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
Chao Yu2df0ab02019-03-25 21:07:30 +08001906 blkbits;
1907 if (last_block > last_block_in_file)
1908 last_block = last_block_in_file;
1909
1910 /* just zeroing out page which is beyond EOF */
1911 if (block_in_file >= last_block)
1912 goto zero_out;
1913 /*
1914 * Map blocks using the previous result first.
1915 */
1916 if ((map->m_flags & F2FS_MAP_MAPPED) &&
1917 block_in_file > map->m_lblk &&
1918 block_in_file < (map->m_lblk + map->m_len))
1919 goto got_it;
1920
1921 /*
1922 * Then do more f2fs_map_blocks() calls until we are
1923 * done with this page.
1924 */
1925 map->m_lblk = block_in_file;
1926 map->m_len = last_block - block_in_file;
1927
1928 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
1929 if (ret)
1930 goto out;
1931got_it:
1932 if ((map->m_flags & F2FS_MAP_MAPPED)) {
1933 block_nr = map->m_pblk + block_in_file - map->m_lblk;
1934 SetPageMappedToDisk(page);
1935
Jaegeuk Kim4969c062019-07-01 19:15:29 -07001936 if (!PageUptodate(page) && (!PageSwapCache(page) &&
1937 !cleancache_get_page(page))) {
Chao Yu2df0ab02019-03-25 21:07:30 +08001938 SetPageUptodate(page);
1939 goto confused;
1940 }
1941
1942 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
Chao Yu93770ab2019-04-15 15:26:32 +08001943 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08001944 ret = -EFSCORRUPTED;
Chao Yu2df0ab02019-03-25 21:07:30 +08001945 goto out;
1946 }
1947 } else {
1948zero_out:
1949 zero_user_segment(page, 0, PAGE_SIZE);
Eric Biggers95ae2512019-07-22 09:26:24 -07001950 if (f2fs_need_verity(inode, page->index) &&
1951 !fsverity_verify_page(page)) {
1952 ret = -EIO;
1953 goto out;
1954 }
Chao Yu2df0ab02019-03-25 21:07:30 +08001955 if (!PageUptodate(page))
1956 SetPageUptodate(page);
1957 unlock_page(page);
1958 goto out;
1959 }
1960
1961 /*
1962 * This page will go to BIO. Do we need to send this
1963 * BIO off first?
1964 */
Chao Yu8896cbd2019-07-12 16:55:41 +08001965 if (bio && !page_is_mergeable(F2FS_I_SB(inode), bio,
1966 *last_block_in_bio, block_nr)) {
Chao Yu2df0ab02019-03-25 21:07:30 +08001967submit_and_realloc:
1968 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1969 bio = NULL;
1970 }
1971 if (bio == NULL) {
1972 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
Eric Biggers95ae2512019-07-22 09:26:24 -07001973 is_readahead ? REQ_RAHEAD : 0, page->index);
Chao Yu2df0ab02019-03-25 21:07:30 +08001974 if (IS_ERR(bio)) {
1975 ret = PTR_ERR(bio);
1976 bio = NULL;
1977 goto out;
1978 }
1979 }
1980
1981 /*
1982 * If the page is under writeback, we need to wait for
1983 * its completion to see the correct decrypted data.
1984 */
1985 f2fs_wait_on_block_writeback(inode, block_nr);
1986
1987 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1988 goto submit_and_realloc;
1989
1990 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
1991 ClearPageError(page);
1992 *last_block_in_bio = block_nr;
1993 goto out;
1994confused:
1995 if (bio) {
1996 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1997 bio = NULL;
1998 }
1999 unlock_page(page);
2000out:
2001 *bio_ret = bio;
2002 return ret;
2003}
2004
Chao Yu4c8ff702019-11-01 18:07:14 +08002005#ifdef CONFIG_F2FS_FS_COMPRESSION
2006int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2007 unsigned nr_pages, sector_t *last_block_in_bio,
2008 bool is_readahead)
2009{
2010 struct dnode_of_data dn;
2011 struct inode *inode = cc->inode;
2012 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2013 struct bio *bio = *bio_ret;
2014 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2015 sector_t last_block_in_file;
2016 const unsigned blkbits = inode->i_blkbits;
2017 const unsigned blocksize = 1 << blkbits;
2018 struct decompress_io_ctx *dic = NULL;
2019 int i;
2020 int ret = 0;
2021
2022 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2023
2024 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
2025
2026 /* get rid of pages beyond EOF */
2027 for (i = 0; i < cc->cluster_size; i++) {
2028 struct page *page = cc->rpages[i];
2029
2030 if (!page)
2031 continue;
2032 if ((sector_t)page->index >= last_block_in_file) {
2033 zero_user_segment(page, 0, PAGE_SIZE);
2034 if (!PageUptodate(page))
2035 SetPageUptodate(page);
2036 } else if (!PageUptodate(page)) {
2037 continue;
2038 }
2039 unlock_page(page);
2040 cc->rpages[i] = NULL;
2041 cc->nr_rpages--;
2042 }
2043
2044 /* we are done since all pages are beyond EOF */
2045 if (f2fs_cluster_is_empty(cc))
2046 goto out;
2047
2048 set_new_dnode(&dn, inode, NULL, NULL, 0);
2049 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2050 if (ret)
2051 goto out;
2052
2053 /* cluster was overwritten as normal cluster */
2054 if (dn.data_blkaddr != COMPRESS_ADDR)
2055 goto out;
2056
2057 for (i = 1; i < cc->cluster_size; i++) {
2058 block_t blkaddr;
2059
2060 blkaddr = datablock_addr(dn.inode, dn.node_page,
2061 dn.ofs_in_node + i);
2062
2063 if (!__is_valid_data_blkaddr(blkaddr))
2064 break;
2065
2066 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2067 ret = -EFAULT;
2068 goto out_put_dnode;
2069 }
2070 cc->nr_cpages++;
2071 }
2072
2073 /* nothing to decompress */
2074 if (cc->nr_cpages == 0) {
2075 ret = 0;
2076 goto out_put_dnode;
2077 }
2078
2079 dic = f2fs_alloc_dic(cc);
2080 if (IS_ERR(dic)) {
2081 ret = PTR_ERR(dic);
2082 goto out_put_dnode;
2083 }
2084
2085 for (i = 0; i < dic->nr_cpages; i++) {
2086 struct page *page = dic->cpages[i];
2087 block_t blkaddr;
2088
2089 blkaddr = datablock_addr(dn.inode, dn.node_page,
2090 dn.ofs_in_node + i + 1);
2091
2092 if (bio && !page_is_mergeable(sbi, bio,
2093 *last_block_in_bio, blkaddr)) {
2094submit_and_realloc:
2095 __submit_bio(sbi, bio, DATA);
2096 bio = NULL;
2097 }
2098
2099 if (!bio) {
2100 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2101 is_readahead ? REQ_RAHEAD : 0,
2102 page->index);
2103 if (IS_ERR(bio)) {
2104 ret = PTR_ERR(bio);
2105 bio = NULL;
2106 dic->failed = true;
2107 if (refcount_sub_and_test(dic->nr_cpages - i,
2108 &dic->ref))
2109 f2fs_decompress_end_io(dic->rpages,
2110 cc->cluster_size, true,
2111 false);
2112 f2fs_free_dic(dic);
2113 f2fs_put_dnode(&dn);
2114 *bio_ret = bio;
2115 return ret;
2116 }
2117 }
2118
2119 f2fs_wait_on_block_writeback(inode, blkaddr);
2120
2121 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2122 goto submit_and_realloc;
2123
2124 inc_page_count(sbi, F2FS_RD_DATA);
2125 ClearPageError(page);
2126 *last_block_in_bio = blkaddr;
2127 }
2128
2129 f2fs_put_dnode(&dn);
2130
2131 *bio_ret = bio;
2132 return 0;
2133
2134out_put_dnode:
2135 f2fs_put_dnode(&dn);
2136out:
2137 f2fs_decompress_end_io(cc->rpages, cc->cluster_size, true, false);
2138 *bio_ret = bio;
2139 return ret;
2140}
2141#endif
2142
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002143/*
2144 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2145 * Major change was from block_size == page_size in f2fs by default.
Jaegeuk Kime2e59412018-06-21 11:29:43 -07002146 *
2147 * Note that the aops->readpages() function is ONLY used for read-ahead. If
2148 * this function ever deviates from doing just read-ahead, it should either
2149 * use ->readpage() or do the necessary surgery to decouple ->readpages()
2150 * from read-ahead.
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002151 */
Chao Yu4c8ff702019-11-01 18:07:14 +08002152int f2fs_mpage_readpages(struct address_space *mapping,
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002153 struct list_head *pages, struct page *page,
Jaegeuk Kime2e59412018-06-21 11:29:43 -07002154 unsigned nr_pages, bool is_readahead)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002155{
2156 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002157 sector_t last_block_in_bio = 0;
2158 struct inode *inode = mapping->host;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002159 struct f2fs_map_blocks map;
Chao Yu4c8ff702019-11-01 18:07:14 +08002160#ifdef CONFIG_F2FS_FS_COMPRESSION
2161 struct compress_ctx cc = {
2162 .inode = inode,
2163 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2164 .cluster_size = F2FS_I(inode)->i_cluster_size,
2165 .cluster_idx = NULL_CLUSTER,
2166 .rpages = NULL,
2167 .cpages = NULL,
2168 .nr_rpages = 0,
2169 .nr_cpages = 0,
2170 };
2171#endif
2172 unsigned max_nr_pages = nr_pages;
Chao Yu2df0ab02019-03-25 21:07:30 +08002173 int ret = 0;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002174
2175 map.m_pblk = 0;
2176 map.m_lblk = 0;
2177 map.m_len = 0;
2178 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08002179 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08002180 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09002181 map.m_seg_type = NO_CHECK_TYPE;
Chao Yuf9d6d052018-11-13 14:33:45 +08002182 map.m_may_create = false;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002183
LiFan736c0a72017-11-25 11:46:18 +08002184 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002185 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08002186 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08002187
2188 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002189 list_del(&page->lru);
2190 if (add_to_page_cache_lru(page, mapping,
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002191 page_index(page),
Michal Hocko8a5c7432016-07-26 15:24:53 -07002192 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002193 goto next_page;
2194 }
2195
Chao Yu4c8ff702019-11-01 18:07:14 +08002196#ifdef CONFIG_F2FS_FS_COMPRESSION
2197 if (f2fs_compressed_file(inode)) {
2198 /* there are remained comressed pages, submit them */
2199 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2200 ret = f2fs_read_multi_pages(&cc, &bio,
2201 max_nr_pages,
2202 &last_block_in_bio,
2203 is_readahead);
2204 f2fs_destroy_compress_ctx(&cc);
2205 if (ret)
2206 goto set_error_page;
2207 }
2208 ret = f2fs_is_compressed_cluster(inode, page->index);
2209 if (ret < 0)
2210 goto set_error_page;
2211 else if (!ret)
2212 goto read_single_page;
2213
2214 ret = f2fs_init_compress_ctx(&cc);
2215 if (ret)
2216 goto set_error_page;
2217
2218 f2fs_compress_ctx_add_page(&cc, page);
2219
2220 goto next_page;
2221 }
2222read_single_page:
2223#endif
2224
2225 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2226 &bio, &last_block_in_bio, is_readahead);
Chao Yu2df0ab02019-03-25 21:07:30 +08002227 if (ret) {
Chao Yu4c8ff702019-11-01 18:07:14 +08002228#ifdef CONFIG_F2FS_FS_COMPRESSION
2229set_error_page:
2230#endif
Chao Yu2df0ab02019-03-25 21:07:30 +08002231 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002232 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002233 unlock_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002234 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002235next_page:
2236 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002237 put_page(page);
Chao Yu4c8ff702019-11-01 18:07:14 +08002238
2239#ifdef CONFIG_F2FS_FS_COMPRESSION
2240 if (f2fs_compressed_file(inode)) {
2241 /* last page */
2242 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2243 ret = f2fs_read_multi_pages(&cc, &bio,
2244 max_nr_pages,
2245 &last_block_in_bio,
2246 is_readahead);
2247 f2fs_destroy_compress_ctx(&cc);
2248 }
2249 }
2250#endif
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002251 }
2252 BUG_ON(pages && !list_empty(pages));
2253 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07002254 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Chao Yu2df0ab02019-03-25 21:07:30 +08002255 return pages ? 0 : ret;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07002256}
2257
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002258static int f2fs_read_data_page(struct file *file, struct page *page)
2259{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002260 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002261 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002262
Chao Yuc20e89c2014-05-06 16:53:08 +08002263 trace_f2fs_readpage(page, DATA);
2264
Chao Yu4c8ff702019-11-01 18:07:14 +08002265 if (!f2fs_is_compress_backend_ready(inode)) {
2266 unlock_page(page);
2267 return -EOPNOTSUPP;
2268 }
2269
arter97e1c42042014-08-06 23:22:50 +09002270 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002271 if (f2fs_has_inline_data(inode))
2272 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002273 if (ret == -EAGAIN)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07002274 ret = f2fs_mpage_readpages(page_file_mapping(page),
2275 NULL, page, 1, false);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002276 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002277}
2278
2279static int f2fs_read_data_pages(struct file *file,
2280 struct address_space *mapping,
2281 struct list_head *pages, unsigned nr_pages)
2282{
Hsiang Kao71cb4af2017-09-24 02:45:42 +08002283 struct inode *inode = mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08002284 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08002285
2286 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002287
Chao Yu4c8ff702019-11-01 18:07:14 +08002288 if (!f2fs_is_compress_backend_ready(inode))
2289 return 0;
2290
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002291 /* If the file has inline data, skip readpages */
2292 if (f2fs_has_inline_data(inode))
2293 return 0;
2294
Jaegeuk Kime2e59412018-06-21 11:29:43 -07002295 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002296}
2297
Chao Yu4c8ff702019-11-01 18:07:14 +08002298int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002299{
2300 struct inode *inode = fio->page->mapping->host;
Chao Yu4c8ff702019-11-01 18:07:14 +08002301 struct page *mpage, *page;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002302 gfp_t gfp_flags = GFP_NOFS;
2303
Jaegeuk Kim19585932017-09-05 16:54:24 -07002304 if (!f2fs_encrypted_file(inode))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002305 return 0;
2306
Chao Yu4c8ff702019-11-01 18:07:14 +08002307 page = fio->compressed_page ? fio->compressed_page : fio->page;
2308
Eric Biggers6dbb1792018-04-18 11:09:48 -07002309 /* wait for GCed page writeback via META_MAPPING */
Jaegeuk Kim0ded69f2018-08-22 21:18:00 -07002310 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002311
2312retry_encrypt:
Chao Yu4c8ff702019-11-01 18:07:14 +08002313 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2314 PAGE_SIZE, 0, gfp_flags);
Chao Yu6aa58d82018-08-14 22:37:25 +08002315 if (IS_ERR(fio->encrypted_page)) {
2316 /* flush pending IOs and wait for a while in the ENOMEM case */
2317 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2318 f2fs_flush_merged_writes(fio->sbi);
2319 congestion_wait(BLK_RW_ASYNC, HZ/50);
2320 gfp_flags |= __GFP_NOFAIL;
2321 goto retry_encrypt;
2322 }
2323 return PTR_ERR(fio->encrypted_page);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002324 }
Chao Yu6aa58d82018-08-14 22:37:25 +08002325
2326 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2327 if (mpage) {
2328 if (PageUptodate(mpage))
2329 memcpy(page_address(mpage),
2330 page_address(fio->encrypted_page), PAGE_SIZE);
2331 f2fs_put_page(mpage, 1);
2332 }
2333 return 0;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002334}
2335
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002336static inline bool check_inplace_update_policy(struct inode *inode,
2337 struct f2fs_io_info *fio)
2338{
2339 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2340 unsigned int policy = SM_I(sbi)->ipu_policy;
2341
2342 if (policy & (0x1 << F2FS_IPU_FORCE))
2343 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08002344 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002345 return true;
2346 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2347 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2348 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08002349 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002350 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2351 return true;
2352
2353 /*
2354 * IPU for rewrite async pages
2355 */
2356 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2357 fio && fio->op == REQ_OP_WRITE &&
2358 !(fio->op_flags & REQ_SYNC) &&
Chandan Rajendra62230e0d2018-12-12 15:20:11 +05302359 !IS_ENCRYPTED(inode))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002360 return true;
2361
2362 /* this is only set during fdatasync */
2363 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2364 is_inode_flag_set(inode, FI_NEED_IPU))
2365 return true;
2366
Daniel Rosenberg43549942018-08-20 19:21:43 -07002367 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2368 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2369 return true;
2370
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002371 return false;
2372}
2373
Chao Yu4d57b862018-05-30 00:20:41 +08002374bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002375{
2376 if (f2fs_is_pinned_file(inode))
2377 return true;
2378
2379 /* if this is cold file, we should overwrite to avoid fragmentation */
2380 if (file_is_cold(inode))
2381 return true;
2382
2383 return check_inplace_update_policy(inode, fio);
2384}
2385
Chao Yu4d57b862018-05-30 00:20:41 +08002386bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002387{
2388 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2389
2390 if (test_opt(sbi, LFS))
2391 return true;
2392 if (S_ISDIR(inode->i_mode))
2393 return true;
Chao Yuaf033b22018-09-20 20:05:00 +08002394 if (IS_NOQUOTA(inode))
2395 return true;
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002396 if (f2fs_is_atomic_file(inode))
2397 return true;
2398 if (fio) {
2399 if (is_cold_data(fio->page))
2400 return true;
2401 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2402 return true;
Daniel Rosenberg43549942018-08-20 19:21:43 -07002403 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2404 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2405 return true;
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08002406 }
2407 return false;
2408}
2409
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002410static inline bool need_inplace_update(struct f2fs_io_info *fio)
2411{
2412 struct inode *inode = fio->page->mapping->host;
2413
Chao Yu4d57b862018-05-30 00:20:41 +08002414 if (f2fs_should_update_outplace(inode, fio))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002415 return false;
2416
Chao Yu4d57b862018-05-30 00:20:41 +08002417 return f2fs_should_update_inplace(inode, fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002418}
2419
Chao Yu4d57b862018-05-30 00:20:41 +08002420int f2fs_do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002421{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002422 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002423 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002424 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002425 struct extent_info ei = {0,0,0};
Chao Yu77357302018-07-17 00:02:17 +08002426 struct node_info ni;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002427 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002428 int err = 0;
2429
2430 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00002431 if (need_inplace_update(fio) &&
2432 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2433 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07002434
Chao Yuc9b60782018-08-01 19:13:44 +08002435 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08002436 DATA_GENERIC_ENHANCE))
Chao Yu10f966b2019-06-20 11:36:14 +08002437 return -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08002438
2439 ipu_force = true;
2440 fio->need_lock = LOCK_DONE;
2441 goto got_it;
Hou Pengyange959c8f2017-04-25 12:45:13 +00002442 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002443
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002444 /* Deadlock due to between page->lock and f2fs_lock_op */
2445 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2446 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08002447
Chao Yu4d57b862018-05-30 00:20:41 +08002448 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002449 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002450 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002451
Chao Yu28bc1062016-02-06 14:40:34 +08002452 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002453
2454 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08002455 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002456 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +08002457 clear_cold_data(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002458 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002459 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00002460got_it:
Chao Yuc9b60782018-08-01 19:13:44 +08002461 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2462 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
Chao Yu93770ab2019-04-15 15:26:32 +08002463 DATA_GENERIC_ENHANCE)) {
Chao Yu10f966b2019-06-20 11:36:14 +08002464 err = -EFSCORRUPTED;
Chao Yuc9b60782018-08-01 19:13:44 +08002465 goto out_writepage;
2466 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002467 /*
2468 * If current allocation needs SSR,
2469 * it had better in-place writes for updated data.
2470 */
Chao Yu93770ab2019-04-15 15:26:32 +08002471 if (ipu_force ||
2472 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
Chao Yu7b525dd2018-05-23 22:25:08 +08002473 need_inplace_update(fio))) {
Chao Yu4c8ff702019-11-01 18:07:14 +08002474 err = f2fs_encrypt_one_page(fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002475 if (err)
2476 goto out_writepage;
2477
2478 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07002479 ClearPageError(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002480 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002481 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002482 f2fs_unlock_op(fio->sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08002483 err = f2fs_inplace_write_data(fio);
Chao Yu6492a332019-02-21 20:37:14 +08002484 if (err) {
2485 if (f2fs_encrypted_file(inode))
Eric Biggersd2d07272019-05-20 09:29:39 -07002486 fscrypt_finalize_bounce_page(&fio->encrypted_page);
Chao Yu6492a332019-02-21 20:37:14 +08002487 if (PageWriteback(page))
2488 end_page_writeback(page);
Chao Yucd23ffa92019-04-15 15:30:53 +08002489 } else {
2490 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yu6492a332019-02-21 20:37:14 +08002491 }
Hou Pengyang7eab0c02017-04-25 12:45:12 +00002492 trace_f2fs_do_write_data_page(fio->page, IPU);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002493 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002494 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002495
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002496 if (fio->need_lock == LOCK_RETRY) {
2497 if (!f2fs_trylock_op(fio->sbi)) {
2498 err = -EAGAIN;
2499 goto out_writepage;
2500 }
2501 fio->need_lock = LOCK_REQ;
2502 }
2503
Chao Yu77357302018-07-17 00:02:17 +08002504 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2505 if (err)
2506 goto out_writepage;
2507
2508 fio->version = ni.version;
2509
Chao Yu4c8ff702019-11-01 18:07:14 +08002510 err = f2fs_encrypt_one_page(fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002511 if (err)
2512 goto out_writepage;
2513
2514 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07002515 ClearPageError(page);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002516
Chao Yu4c8ff702019-11-01 18:07:14 +08002517 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2518 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2519
Hou Pengyang279d6df2017-04-27 00:17:21 +08002520 /* LFS mode write path */
Chao Yu4d57b862018-05-30 00:20:41 +08002521 f2fs_outplace_write_data(&dn, fio);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002522 trace_f2fs_do_write_data_page(page, OPU);
2523 set_inode_flag(inode, FI_APPEND_WRITE);
2524 if (page->index == 0)
2525 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002526out_writepage:
2527 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08002528out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002529 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08002530 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002531 return err;
2532}
2533
Chao Yu4c8ff702019-11-01 18:07:14 +08002534int f2fs_write_single_data_page(struct page *page, int *submitted,
Chao Yu8648de22019-02-19 16:15:29 +08002535 struct bio **bio,
2536 sector_t *last_block,
Chao Yub0af6d42017-08-02 23:21:48 +08002537 struct writeback_control *wbc,
Chao Yu4c8ff702019-11-01 18:07:14 +08002538 enum iostat_type io_type,
2539 int compr_blocks)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002540{
2541 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002542 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002543 loff_t i_size = i_size_read(inode);
Chao Yu4c8ff702019-11-01 18:07:14 +08002544 const pgoff_t end_index = ((unsigned long long)i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002545 >> PAGE_SHIFT;
Chao Yu1f0d5c92019-11-07 17:29:00 +08002546 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002547 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09002548 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002549 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002550 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002551 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08002552 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002553 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05002554 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06002555 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00002556 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07002557 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002558 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002559 .submitted = false,
Chao Yu4c8ff702019-11-01 18:07:14 +08002560 .compr_blocks = compr_blocks,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002561 .need_lock = LOCK_RETRY,
Chao Yub0af6d42017-08-02 23:21:48 +08002562 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08002563 .io_wbc = wbc,
Chao Yu8648de22019-02-19 16:15:29 +08002564 .bio = bio,
2565 .last_block = last_block,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002566 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002567
Chao Yuecda0de2014-05-06 16:48:26 +08002568 trace_f2fs_writepage(page, DATA);
2569
Chao Yudb198ae2018-01-18 17:29:10 +08002570 /* we should bypass data pages to proceed the kworkder jobs */
2571 if (unlikely(f2fs_cp_error(sbi))) {
2572 mapping_set_error(page->mapping, -EIO);
Chao Yu1174abf2018-05-28 16:59:26 +08002573 /*
2574 * don't drop any dirty dentry pages for keeping lastest
2575 * directory structure.
2576 */
2577 if (S_ISDIR(inode->i_mode))
2578 goto redirty_out;
Chao Yudb198ae2018-01-18 17:29:10 +08002579 goto out;
2580 }
2581
Chao Yu0771fcc2017-06-29 23:20:45 +08002582 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2583 goto redirty_out;
2584
Chao Yu4c8ff702019-11-01 18:07:14 +08002585 if (page->index < end_index ||
2586 f2fs_verity_in_progress(inode) ||
2587 compr_blocks)
Jaegeuk Kim39936832012-11-22 16:21:29 +09002588 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002589
2590 /*
2591 * If the offset is out-of-range of file size,
2592 * this page does not have to be written to disk.
2593 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002594 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002595 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09002596 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002597
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002598 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09002599write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002600 if (f2fs_is_drop_cache(inode))
2601 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07002602 /* we should not write 0'th page having journal header */
2603 if (f2fs_is_volatile_file(inode) && (!page->index ||
2604 (!wbc->for_reclaim &&
Chao Yu4d57b862018-05-30 00:20:41 +08002605 f2fs_available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002606 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002607
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002608 /* Dentry blocks are controlled by checkpoint */
2609 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002610 fio.need_lock = LOCK_DONE;
Chao Yu4d57b862018-05-30 00:20:41 +08002611 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002612 goto done;
2613 }
2614
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002615 if (!wbc->for_reclaim)
2616 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002617 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09002618 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002619 else
2620 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002621
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002622 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08002623 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002624 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08002625 if (!err)
2626 goto out;
2627 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002628
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002629 if (err == -EAGAIN) {
Chao Yu4d57b862018-05-30 00:20:41 +08002630 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002631 if (err == -EAGAIN) {
2632 fio.need_lock = LOCK_REQ;
Chao Yu4d57b862018-05-30 00:20:41 +08002633 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07002634 }
2635 }
Chao Yua0d00fa2017-10-09 17:55:19 +08002636
Chao Yueb449792018-01-17 16:31:37 +08002637 if (err) {
2638 file_set_keep_isize(inode);
2639 } else {
2640 down_write(&F2FS_I(inode)->i_sem);
2641 if (F2FS_I(inode)->last_disk_size < psize)
2642 F2FS_I(inode)->last_disk_size = psize;
2643 up_write(&F2FS_I(inode)->i_sem);
2644 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08002645
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002646done:
2647 if (err && err != -ENOENT)
2648 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002649
Jaegeuk Kim39936832012-11-22 16:21:29 +09002650out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002651 inode_dec_dirty_pages(inode);
Chao Yu2baf0782018-07-27 18:15:16 +08002652 if (err) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002653 ClearPageUptodate(page);
Chao Yu2baf0782018-07-27 18:15:16 +08002654 clear_cold_data(page);
2655 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002656
2657 if (wbc->for_reclaim) {
Chao Yubab475c2018-09-27 23:41:16 +08002658 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002659 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu4d57b862018-05-30 00:20:41 +08002660 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002661 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08002662 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002663 unlock_page(page);
Chao Yu186857c2019-04-02 18:52:19 +08002664 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
Chao Yu0b20fce2019-09-30 18:53:25 +08002665 !F2FS_I(inode)->cp_task)
Jaegeuk Kima7881892017-04-20 13:51:57 -07002666 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08002667
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002668 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002669 f2fs_submit_merged_write(sbi, DATA);
Chao Yu0b20fce2019-09-30 18:53:25 +08002670 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002671 submitted = NULL;
2672 }
2673
2674 if (submitted)
Chao Yu4c8ff702019-11-01 18:07:14 +08002675 *submitted = fio.submitted ? 1 : 0;
Chao Yu0c3a5792016-01-18 18:28:11 +08002676
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002677 return 0;
2678
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002679redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002680 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim5b19d282018-05-03 23:26:02 -07002681 /*
2682 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2683 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2684 * file_write_and_wait_range() will see EIO error, which is critical
2685 * to return value of fsync() followed by atomic_write failure to user.
2686 */
2687 if (!err || wbc->for_reclaim)
Chao Yu0002b612016-11-28 19:13:43 -08002688 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002689 unlock_page(page);
2690 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09002691}
2692
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002693static int f2fs_write_data_page(struct page *page,
2694 struct writeback_control *wbc)
2695{
Chao Yu4c8ff702019-11-01 18:07:14 +08002696#ifdef CONFIG_F2FS_FS_COMPRESSION
2697 struct inode *inode = page->mapping->host;
2698
2699 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2700 goto out;
2701
2702 if (f2fs_compressed_file(inode)) {
2703 if (f2fs_is_compressed_cluster(inode, page->index)) {
2704 redirty_page_for_writepage(wbc, page);
2705 return AOP_WRITEPAGE_ACTIVATE;
2706 }
2707 }
2708out:
2709#endif
2710
2711 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2712 wbc, FS_DATA_IO, 0);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08002713}
2714
Chao Yu8f46dca2015-07-14 18:56:10 +08002715/*
2716 * This function was copied from write_cche_pages from mm/page-writeback.c.
2717 * The major change is making write step of cold data page separately from
2718 * warm/hot data page.
2719 */
2720static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08002721 struct writeback_control *wbc,
2722 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08002723{
2724 int ret = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08002725 int done = 0, retry = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08002726 struct pagevec pvec;
Chao Yuc29fd0c2018-06-04 23:20:36 +08002727 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Chao Yu8648de22019-02-19 16:15:29 +08002728 struct bio *bio = NULL;
2729 sector_t last_block;
Chao Yu4c8ff702019-11-01 18:07:14 +08002730#ifdef CONFIG_F2FS_FS_COMPRESSION
2731 struct inode *inode = mapping->host;
2732 struct compress_ctx cc = {
2733 .inode = inode,
2734 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2735 .cluster_size = F2FS_I(inode)->i_cluster_size,
2736 .cluster_idx = NULL_CLUSTER,
2737 .rpages = NULL,
2738 .nr_rpages = 0,
2739 .cpages = NULL,
2740 .rbuf = NULL,
2741 .cbuf = NULL,
2742 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2743 .private = NULL,
2744 };
2745#endif
Chao Yu8f46dca2015-07-14 18:56:10 +08002746 int nr_pages;
2747 pgoff_t uninitialized_var(writeback_index);
2748 pgoff_t index;
2749 pgoff_t end; /* Inclusive */
2750 pgoff_t done_index;
2751 int cycled;
2752 int range_whole = 0;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05002753 xa_mark_t tag;
Chao Yubab475c2018-09-27 23:41:16 +08002754 int nwritten = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08002755 int submitted = 0;
2756 int i;
Chao Yu8f46dca2015-07-14 18:56:10 +08002757
Mel Gorman86679822017-11-15 17:37:52 -08002758 pagevec_init(&pvec);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07002759
Jaegeuk Kimef095d12017-03-24 20:05:13 -04002760 if (get_dirty_pages(mapping->host) <=
2761 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2762 set_inode_flag(mapping->host, FI_HOT_DATA);
2763 else
2764 clear_inode_flag(mapping->host, FI_HOT_DATA);
2765
Chao Yu8f46dca2015-07-14 18:56:10 +08002766 if (wbc->range_cyclic) {
2767 writeback_index = mapping->writeback_index; /* prev offset */
2768 index = writeback_index;
2769 if (index == 0)
2770 cycled = 1;
2771 else
2772 cycled = 0;
2773 end = -1;
2774 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002775 index = wbc->range_start >> PAGE_SHIFT;
2776 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08002777 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2778 range_whole = 1;
2779 cycled = 1; /* ignore range_cyclic tests */
2780 }
2781 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2782 tag = PAGECACHE_TAG_TOWRITE;
2783 else
2784 tag = PAGECACHE_TAG_DIRTY;
2785retry:
Chao Yu4c8ff702019-11-01 18:07:14 +08002786 retry = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08002787 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2788 tag_pages_for_writeback(mapping, index, end);
2789 done_index = index;
Chao Yu4c8ff702019-11-01 18:07:14 +08002790 while (!done && !retry && (index <= end)) {
Jan Kara69c4f352017-11-15 17:34:48 -08002791 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08002792 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08002793 if (nr_pages == 0)
2794 break;
2795
2796 for (i = 0; i < nr_pages; i++) {
2797 struct page *page = pvec.pages[i];
Chao Yu4c8ff702019-11-01 18:07:14 +08002798 bool need_readd;
2799readd:
2800 need_readd = false;
2801#ifdef CONFIG_F2FS_FS_COMPRESSION
2802 if (f2fs_compressed_file(inode)) {
2803 ret = f2fs_init_compress_ctx(&cc);
2804 if (ret) {
2805 done = 1;
2806 break;
2807 }
Chao Yu8f46dca2015-07-14 18:56:10 +08002808
Chao Yu4c8ff702019-11-01 18:07:14 +08002809 if (!f2fs_cluster_can_merge_page(&cc,
2810 page->index)) {
2811 ret = f2fs_write_multi_pages(&cc,
2812 &submitted, wbc, io_type);
2813 if (!ret)
2814 need_readd = true;
2815 goto result;
2816 }
2817
2818 if (unlikely(f2fs_cp_error(sbi)))
2819 goto lock_page;
2820
2821 if (f2fs_cluster_is_empty(&cc)) {
2822 void *fsdata = NULL;
2823 struct page *pagep;
2824 int ret2;
2825
2826 ret2 = f2fs_prepare_compress_overwrite(
2827 inode, &pagep,
2828 page->index, &fsdata);
2829 if (ret2 < 0) {
2830 ret = ret2;
2831 done = 1;
2832 break;
2833 } else if (ret2 &&
2834 !f2fs_compress_write_end(inode,
2835 fsdata, page->index,
2836 1)) {
2837 retry = 1;
2838 break;
2839 }
2840 } else {
2841 goto lock_page;
2842 }
2843 }
2844#endif
Chao Yuf8de4332018-05-23 22:25:09 +08002845 /* give a priority to WB_SYNC threads */
Chao Yuc29fd0c2018-06-04 23:20:36 +08002846 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
Chao Yuf8de4332018-05-23 22:25:09 +08002847 wbc->sync_mode == WB_SYNC_NONE) {
2848 done = 1;
2849 break;
2850 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002851#ifdef CONFIG_F2FS_FS_COMPRESSION
2852lock_page:
2853#endif
Chao Yu8f46dca2015-07-14 18:56:10 +08002854 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002855retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08002856 lock_page(page);
2857
2858 if (unlikely(page->mapping != mapping)) {
2859continue_unlock:
2860 unlock_page(page);
2861 continue;
2862 }
2863
2864 if (!PageDirty(page)) {
2865 /* someone wrote it for us */
2866 goto continue_unlock;
2867 }
2868
Chao Yu8f46dca2015-07-14 18:56:10 +08002869 if (PageWriteback(page)) {
Chao Yu0b20fce2019-09-30 18:53:25 +08002870 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002871 f2fs_wait_on_page_writeback(page,
Chao Yubae0ee72018-12-25 17:43:42 +08002872 DATA, true, true);
Chao Yu0b20fce2019-09-30 18:53:25 +08002873 else
Chao Yu8f46dca2015-07-14 18:56:10 +08002874 goto continue_unlock;
2875 }
2876
Chao Yu8f46dca2015-07-14 18:56:10 +08002877 if (!clear_page_dirty_for_io(page))
2878 goto continue_unlock;
2879
Chao Yu4c8ff702019-11-01 18:07:14 +08002880#ifdef CONFIG_F2FS_FS_COMPRESSION
2881 if (f2fs_compressed_file(inode)) {
2882 get_page(page);
2883 f2fs_compress_ctx_add_page(&cc, page);
2884 continue;
2885 }
2886#endif
2887 ret = f2fs_write_single_data_page(page, &submitted,
2888 &bio, &last_block, wbc, io_type, 0);
2889 if (ret == AOP_WRITEPAGE_ACTIVATE)
2890 unlock_page(page);
2891#ifdef CONFIG_F2FS_FS_COMPRESSION
2892result:
2893#endif
2894 nwritten += submitted;
2895 wbc->nr_to_write -= submitted;
2896
Chao Yu8f46dca2015-07-14 18:56:10 +08002897 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08002898 /*
2899 * keep nr_to_write, since vfs uses this to
2900 * get # of written pages.
2901 */
2902 if (ret == AOP_WRITEPAGE_ACTIVATE) {
Chao Yu0002b612016-11-28 19:13:43 -08002903 ret = 0;
Chao Yu4c8ff702019-11-01 18:07:14 +08002904 goto next;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002905 } else if (ret == -EAGAIN) {
2906 ret = 0;
2907 if (wbc->sync_mode == WB_SYNC_ALL) {
2908 cond_resched();
2909 congestion_wait(BLK_RW_ASYNC,
Chao Yu4c8ff702019-11-01 18:07:14 +08002910 HZ/50);
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002911 goto retry_write;
2912 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002913 goto next;
Chao Yu0002b612016-11-28 19:13:43 -08002914 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002915 done_index = page->index + 1;
2916 done = 1;
2917 break;
Chao Yu8f46dca2015-07-14 18:56:10 +08002918 }
2919
Chao Yu4c8ff702019-11-01 18:07:14 +08002920 if (wbc->nr_to_write <= 0 &&
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002921 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08002922 done = 1;
2923 break;
2924 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002925next:
2926 if (need_readd)
2927 goto readd;
Chao Yu8f46dca2015-07-14 18:56:10 +08002928 }
2929 pagevec_release(&pvec);
2930 cond_resched();
2931 }
Chao Yu4c8ff702019-11-01 18:07:14 +08002932#ifdef CONFIG_F2FS_FS_COMPRESSION
2933 /* flush remained pages in compress cluster */
2934 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
2935 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
2936 nwritten += submitted;
2937 wbc->nr_to_write -= submitted;
2938 if (ret) {
2939 done = 1;
2940 retry = 0;
2941 }
2942 }
2943#endif
2944 if ((!cycled && !done) || retry) {
Chao Yu8f46dca2015-07-14 18:56:10 +08002945 cycled = 1;
2946 index = 0;
2947 end = writeback_index - 1;
2948 goto retry;
2949 }
2950 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2951 mapping->writeback_index = done_index;
2952
Chao Yubab475c2018-09-27 23:41:16 +08002953 if (nwritten)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002954 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
Chao Yubab475c2018-09-27 23:41:16 +08002955 NULL, 0, DATA);
Chao Yu8648de22019-02-19 16:15:29 +08002956 /* submit cached bio of IPU write */
2957 if (bio)
Chao Yu0b20fce2019-09-30 18:53:25 +08002958 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
Chao Yu6ca56ca2016-09-29 18:50:11 +08002959
Chao Yu8f46dca2015-07-14 18:56:10 +08002960 return ret;
2961}
2962
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002963static inline bool __should_serialize_io(struct inode *inode,
2964 struct writeback_control *wbc)
2965{
2966 if (!S_ISREG(inode->i_mode))
2967 return false;
Chao Yu4c8ff702019-11-01 18:07:14 +08002968 if (f2fs_compressed_file(inode))
2969 return true;
Chao Yuaf033b22018-09-20 20:05:00 +08002970 if (IS_NOQUOTA(inode))
2971 return false;
Chao Yu040d2bb2019-05-20 17:36:59 +08002972 /* to avoid deadlock in path of data flush */
2973 if (F2FS_I(inode)->cp_task)
2974 return false;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002975 if (wbc->sync_mode != WB_SYNC_ALL)
2976 return true;
2977 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
2978 return true;
2979 return false;
2980}
2981
Chao Yufc99fe22018-05-30 00:20:39 +08002982static int __f2fs_write_data_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08002983 struct writeback_control *wbc,
2984 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002985{
2986 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002987 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002988 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002989 int ret;
Jaegeuk Kim853137c2018-08-09 17:53:34 -07002990 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002991
P J Pcfb185a2013-04-03 11:38:00 +09002992 /* deal with chardevs and other special file */
2993 if (!mapping->a_ops->writepage)
2994 return 0;
2995
Chao Yu6a290542015-07-17 18:02:39 +08002996 /* skip writing if there is no dirty page in this inode */
2997 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2998 return 0;
2999
Chao Yu0771fcc2017-06-29 23:20:45 +08003000 /* during POR, we don't need to trigger writepage at all. */
3001 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3002 goto skip_write;
3003
Chao Yuaf033b22018-09-20 20:05:00 +08003004 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3005 wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima1257022015-10-08 10:40:07 -07003006 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Chao Yu4d57b862018-05-30 00:20:41 +08003007 f2fs_available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kima1257022015-10-08 10:40:07 -07003008 goto skip_write;
3009
Chao Yud323d002015-10-27 09:53:45 +08003010 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07003011 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08003012 goto skip_write;
3013
Yunlei Hed31c7c32016-02-04 16:14:00 +08003014 trace_f2fs_writepages(mapping->host, wbc, DATA);
3015
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003016 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3017 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08003018 atomic_inc(&sbi->wb_sync_req[DATA]);
3019 else if (atomic_read(&sbi->wb_sync_req[DATA]))
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003020 goto skip_write;
3021
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003022 if (__should_serialize_io(inode, wbc)) {
3023 mutex_lock(&sbi->writepages);
3024 locked = true;
3025 }
3026
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003027 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08003028 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07003029 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003030
Jaegeuk Kim853137c2018-08-09 17:53:34 -07003031 if (locked)
3032 mutex_unlock(&sbi->writepages);
3033
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07003034 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yuc29fd0c2018-06-04 23:20:36 +08003035 atomic_dec(&sbi->wb_sync_req[DATA]);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07003036 /*
3037 * if some pages were truncated, we cannot guarantee its mapping->host
3038 * to detect pending bios.
3039 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09003040
Chao Yu4d57b862018-05-30 00:20:41 +08003041 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003042 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09003043
3044skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003045 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08003046 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09003047 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003048}
3049
Chao Yub0af6d42017-08-02 23:21:48 +08003050static int f2fs_write_data_pages(struct address_space *mapping,
3051 struct writeback_control *wbc)
3052{
3053 struct inode *inode = mapping->host;
3054
3055 return __f2fs_write_data_pages(mapping, wbc,
3056 F2FS_I(inode)->cp_task == current ?
3057 FS_CP_DATA_IO : FS_DATA_IO);
3058}
3059
Chao Yu3aab8f82014-07-02 13:25:04 +08003060static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3061{
3062 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08003063 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08003064
Jaegeuk Kim3f188c22019-12-03 18:54:29 -08003065 if (IS_NOQUOTA(inode))
3066 return;
3067
Eric Biggers95ae2512019-07-22 09:26:24 -07003068 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3069 if (to > i_size && !f2fs_verity_in_progress(inode)) {
Chao Yua33c1502018-08-05 23:04:25 +08003070 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09003071 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yua33c1502018-08-05 23:04:25 +08003072
Jaegeuk Kim819d9152015-12-28 13:48:11 -08003073 truncate_pagecache(inode, i_size);
Jaegeuk Kim3f188c22019-12-03 18:54:29 -08003074 f2fs_truncate_blocks(inode, i_size, true);
Chao Yua33c1502018-08-05 23:04:25 +08003075
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08003076 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim6f8d4452018-07-25 12:11:56 +09003077 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu3aab8f82014-07-02 13:25:04 +08003078 }
3079}
3080
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003081static int prepare_write_begin(struct f2fs_sb_info *sbi,
3082 struct page *page, loff_t pos, unsigned len,
3083 block_t *blk_addr, bool *node_changed)
3084{
3085 struct inode *inode = page->mapping->host;
3086 pgoff_t index = page->index;
3087 struct dnode_of_data dn;
3088 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003089 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00003090 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003091 int err = 0;
Sheng Yong2866fb12018-11-14 19:34:28 +08003092 int flag;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003093
Jaegeuk Kim24b84912016-02-03 13:49:44 -08003094 /*
3095 * we already allocated all the blocks, so we don't need to get
3096 * the block addresses when there is no need to fill the page.
3097 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08003098 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
Eric Biggers95ae2512019-07-22 09:26:24 -07003099 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3100 !f2fs_verity_in_progress(inode))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08003101 return 0;
3102
Sheng Yong2866fb12018-11-14 19:34:28 +08003103 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3104 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3105 flag = F2FS_GET_BLOCK_DEFAULT;
3106 else
3107 flag = F2FS_GET_BLOCK_PRE_AIO;
3108
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003109 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003110 (pos & PAGE_MASK) >= i_size_read(inode)) {
Sheng Yong2866fb12018-11-14 19:34:28 +08003111 __do_map_lock(sbi, flag, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003112 locked = true;
3113 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003114
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003115restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003116 /* check inline_data */
Chao Yu4d57b862018-05-30 00:20:41 +08003117 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003118 if (IS_ERR(ipage)) {
3119 err = PTR_ERR(ipage);
3120 goto unlock_out;
3121 }
3122
3123 set_new_dnode(&dn, inode, ipage, ipage, 0);
3124
3125 if (f2fs_has_inline_data(inode)) {
Chao Yuf2470372017-07-19 00:19:05 +08003126 if (pos + len <= MAX_INLINE_DATA(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003127 f2fs_do_read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07003128 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08003129 if (inode->i_nlink)
3130 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003131 } else {
3132 err = f2fs_convert_inline_page(&dn, page);
3133 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003134 goto out;
3135 if (dn.data_blkaddr == NULL_ADDR)
3136 err = f2fs_get_block(&dn, index);
3137 }
3138 } else if (locked) {
3139 err = f2fs_get_block(&dn, index);
3140 } else {
3141 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3142 dn.data_blkaddr = ei.blk + index - ei.fofs;
3143 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003144 /* hole case */
Chao Yu4d57b862018-05-30 00:20:41 +08003145 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07003146 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003147 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08003148 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3149 true);
Sheng Yong2866fb12018-11-14 19:34:28 +08003150 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003151 locked = true;
3152 goto restart;
3153 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003154 }
3155 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003156
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003157 /* convert_inline_page can make node_changed */
3158 *blk_addr = dn.data_blkaddr;
3159 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003160out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003161 f2fs_put_dnode(&dn);
3162unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08003163 if (locked)
Sheng Yong2866fb12018-11-14 19:34:28 +08003164 __do_map_lock(sbi, flag, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003165 return err;
3166}
3167
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003168static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3169 loff_t pos, unsigned len, unsigned flags,
3170 struct page **pagep, void **fsdata)
3171{
3172 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07003173 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07003174 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003175 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yua2e2e762018-01-15 17:16:46 +08003176 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003177 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003178 int err = 0;
3179
Chao Yu62aed042014-05-06 16:46:04 +08003180 trace_f2fs_write_begin(inode, pos, len, flags);
3181
Chao Yu00e09c02019-08-23 17:58:36 +08003182 if (!f2fs_is_checkpoint_ready(sbi)) {
3183 err = -ENOSPC;
Daniel Rosenberg43549942018-08-20 19:21:43 -07003184 goto fail;
Chao Yu00e09c02019-08-23 17:58:36 +08003185 }
Daniel Rosenberg43549942018-08-20 19:21:43 -07003186
Jaegeuk Kim455e3a52018-07-27 18:15:11 +09003187 if ((f2fs_is_atomic_file(inode) &&
3188 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3189 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07003190 err = -ENOMEM;
Chao Yua2e2e762018-01-15 17:16:46 +08003191 drop_atomic = true;
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07003192 goto fail;
3193 }
3194
Jaegeuk Kim5f727392014-11-25 10:59:45 -08003195 /*
3196 * We should check this at this moment to avoid deadlock on inode page
3197 * and #0 page. The locking rule for inline_data conversion should be:
3198 * lock_page(page #0) -> lock_page(inode_page)
3199 */
3200 if (index != 0) {
3201 err = f2fs_convert_inline_inode(inode);
3202 if (err)
3203 goto fail;
3204 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003205
3206#ifdef CONFIG_F2FS_FS_COMPRESSION
3207 if (f2fs_compressed_file(inode)) {
3208 int ret;
3209
3210 *fsdata = NULL;
3211
3212 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3213 index, fsdata);
3214 if (ret < 0) {
3215 err = ret;
3216 goto fail;
3217 } else if (ret) {
3218 return 0;
3219 }
3220 }
3221#endif
3222
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09003223repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08003224 /*
3225 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3226 * wait_for_stable_page. Will wait that below with our IO control.
3227 */
Chao Yu01eccef2017-10-28 16:52:30 +08003228 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kim86d54792017-02-17 09:55:55 -08003229 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08003230 if (!page) {
3231 err = -ENOMEM;
3232 goto fail;
3233 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09003234
Chao Yu4c8ff702019-11-01 18:07:14 +08003235 /* TODO: cluster can be compressed due to race with .writepage */
3236
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003237 *pagep = page;
3238
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003239 err = prepare_write_begin(sbi, page, pos, len,
3240 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003241 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003242 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003243
Chao Yuaf033b22018-09-20 20:05:00 +08003244 if (need_balance && !IS_NOQUOTA(inode) &&
3245 has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08003246 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08003247 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08003248 lock_page(page);
3249 if (page->mapping != mapping) {
3250 /* The page got truncated from under us */
3251 f2fs_put_page(page, 1);
3252 goto repeat;
3253 }
3254 }
3255
Chao Yubae0ee72018-12-25 17:43:42 +08003256 f2fs_wait_on_page_writeback(page, DATA, false, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07003257
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003258 if (len == PAGE_SIZE || PageUptodate(page))
3259 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003260
Eric Biggers95ae2512019-07-22 09:26:24 -07003261 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3262 !f2fs_verity_in_progress(inode)) {
Yunlei He746e2402016-12-20 11:11:35 +08003263 zero_user_segment(page, len, PAGE_SIZE);
3264 return 0;
3265 }
3266
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08003267 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003268 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003269 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003270 } else {
Chao Yu93770ab2019-04-15 15:26:32 +08003271 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3272 DATA_GENERIC_ENHANCE_READ)) {
Chao Yu10f966b2019-06-20 11:36:14 +08003273 err = -EFSCORRUPTED;
Chao Yu93770ab2019-04-15 15:26:32 +08003274 goto fail;
3275 }
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07003276 err = f2fs_submit_page_read(inode, page, blkaddr);
3277 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08003278 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08003279
Jaegeuk Kim393ff912013-03-08 21:29:23 +09003280 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09003281 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09003282 f2fs_put_page(page, 1);
3283 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003284 }
Chao Yu1563ac72016-07-03 22:05:12 +08003285 if (unlikely(!PageUptodate(page))) {
3286 err = -EIO;
3287 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07003288 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003289 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003290 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07003291
Chao Yu3aab8f82014-07-02 13:25:04 +08003292fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07003293 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08003294 f2fs_write_failed(mapping, pos + len);
Chao Yua2e2e762018-01-15 17:16:46 +08003295 if (drop_atomic)
Chao Yu4d57b862018-05-30 00:20:41 +08003296 f2fs_drop_inmem_pages_all(sbi, false);
Chao Yu3aab8f82014-07-02 13:25:04 +08003297 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003298}
3299
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003300static int f2fs_write_end(struct file *file,
3301 struct address_space *mapping,
3302 loff_t pos, unsigned len, unsigned copied,
3303 struct page *page, void *fsdata)
3304{
3305 struct inode *inode = page->mapping->host;
3306
Chao Yudfb2bf32014-05-06 16:47:23 +08003307 trace_f2fs_write_end(inode, pos, len, copied);
3308
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003309 /*
3310 * This should be come from len == PAGE_SIZE, and we expect copied
3311 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3312 * let generic_perform_write() try to copy data again through copied=0.
3313 */
3314 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08003315 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003316 copied = 0;
3317 else
3318 SetPageUptodate(page);
3319 }
Chao Yu4c8ff702019-11-01 18:07:14 +08003320
3321#ifdef CONFIG_F2FS_FS_COMPRESSION
3322 /* overwrite compressed file */
3323 if (f2fs_compressed_file(inode) && fsdata) {
3324 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3325 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3326 return copied;
3327 }
3328#endif
3329
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003330 if (!copied)
3331 goto unlock_out;
3332
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003333 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003334
Eric Biggers95ae2512019-07-22 09:26:24 -07003335 if (pos + copied > i_size_read(inode) &&
3336 !f2fs_verity_in_progress(inode))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07003337 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07003338unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08003339 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08003340 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003341 return copied;
3342}
3343
Omar Sandoval6f673762015-03-16 04:33:52 -07003344static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3345 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003346{
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07003347 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3348 unsigned blkbits = i_blkbits;
3349 unsigned blocksize_mask = (1 << blkbits) - 1;
3350 unsigned long align = offset | iov_iter_alignment(iter);
3351 struct block_device *bdev = inode->i_sb->s_bdev;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003352
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07003353 if (align & blocksize_mask) {
3354 if (bdev)
3355 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3356 blocksize_mask = (1 << blkbits) - 1;
3357 if (align & blocksize_mask)
3358 return -EINVAL;
3359 return 1;
3360 }
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003361 return 0;
3362}
3363
Chao Yu02b16d02018-11-12 00:46:46 +08003364static void f2fs_dio_end_io(struct bio *bio)
3365{
3366 struct f2fs_private_dio *dio = bio->bi_private;
3367
3368 dec_page_count(F2FS_I_SB(dio->inode),
3369 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3370
3371 bio->bi_private = dio->orig_private;
3372 bio->bi_end_io = dio->orig_end_io;
3373
Jaegeuk Kim52225952018-12-13 18:38:33 -08003374 kvfree(dio);
Chao Yu02b16d02018-11-12 00:46:46 +08003375
3376 bio_endio(bio);
3377}
3378
3379static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3380 loff_t file_offset)
3381{
3382 struct f2fs_private_dio *dio;
3383 bool write = (bio_op(bio) == REQ_OP_WRITE);
Chao Yu02b16d02018-11-12 00:46:46 +08003384
3385 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3386 sizeof(struct f2fs_private_dio), GFP_NOFS);
YueHaibing8e114032019-01-04 01:38:29 +00003387 if (!dio)
Chao Yu02b16d02018-11-12 00:46:46 +08003388 goto out;
Chao Yu02b16d02018-11-12 00:46:46 +08003389
3390 dio->inode = inode;
3391 dio->orig_end_io = bio->bi_end_io;
3392 dio->orig_private = bio->bi_private;
3393 dio->write = write;
3394
3395 bio->bi_end_io = f2fs_dio_end_io;
3396 bio->bi_private = dio;
3397
3398 inc_page_count(F2FS_I_SB(inode),
3399 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3400
3401 submit_bio(bio);
3402 return;
3403out:
3404 bio->bi_status = BLK_STS_IOERR;
3405 bio_endio(bio);
3406}
3407
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003408static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003409{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003410 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08003411 struct inode *inode = mapping->host;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003412 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yuf847c692018-09-27 18:34:52 +08003413 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08003414 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003415 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08003416 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08003417 int err;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003418 enum rw_hint hint = iocb->ki_hint;
Chao Yu63189b72018-03-08 14:22:56 +08003419 int whint_mode = F2FS_OPTION(sbi).whint_mode;
Chao Yuf847c692018-09-27 18:34:52 +08003420 bool do_opu;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09003421
Jaegeuk Kimb439b102016-02-03 13:09:09 -08003422 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08003423 if (err)
Jaegeuk Kim8a56dd92018-06-29 18:55:12 -07003424 return err < 0 ? err : 0;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08003425
Chao Yuf847c692018-09-27 18:34:52 +08003426 if (f2fs_force_buffered_io(inode, iocb, iter))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07003427 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07003428
Chao Yuf847c692018-09-27 18:34:52 +08003429 do_opu = allow_outplace_dio(inode, iocb, iter);
3430
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07003431 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08003432
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003433 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3434 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3435
Chao Yuf847c692018-09-27 18:34:52 +08003436 if (iocb->ki_flags & IOCB_NOWAIT) {
3437 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003438 iocb->ki_hint = hint;
3439 err = -EAGAIN;
3440 goto out;
3441 }
Chao Yuf847c692018-09-27 18:34:52 +08003442 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3443 up_read(&fi->i_gc_rwsem[rw]);
3444 iocb->ki_hint = hint;
3445 err = -EAGAIN;
3446 goto out;
3447 }
3448 } else {
3449 down_read(&fi->i_gc_rwsem[rw]);
3450 if (do_opu)
3451 down_read(&fi->i_gc_rwsem[READ]);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003452 }
3453
Chao Yu02b16d02018-11-12 00:46:46 +08003454 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
Chao Yuf9d6d052018-11-13 14:33:45 +08003455 iter, rw == WRITE ? get_data_block_dio_write :
3456 get_data_block_dio, NULL, f2fs_dio_submit_bio,
Chao Yu02b16d02018-11-12 00:46:46 +08003457 DIO_LOCKING | DIO_SKIP_HOLES);
Chao Yuf847c692018-09-27 18:34:52 +08003458
3459 if (do_opu)
3460 up_read(&fi->i_gc_rwsem[READ]);
3461
3462 up_read(&fi->i_gc_rwsem[rw]);
Chao Yu82e0a5a2016-07-13 09:18:29 +08003463
3464 if (rw == WRITE) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09003465 if (whint_mode == WHINT_MODE_OFF)
3466 iocb->ki_hint = hint;
Chao Yub0af6d42017-08-02 23:21:48 +08003467 if (err > 0) {
3468 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3469 err);
Chao Yuf847c692018-09-27 18:34:52 +08003470 if (!do_opu)
3471 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yub0af6d42017-08-02 23:21:48 +08003472 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04003473 f2fs_write_failed(mapping, offset + count);
Chao Yub0af6d42017-08-02 23:21:48 +08003474 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04003475 }
Chao Yu70407fa2014-07-31 21:11:22 +08003476
Hyunchul Leeb91050a2018-03-08 19:34:38 +09003477out:
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07003478 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08003479
Chao Yu3aab8f82014-07-02 13:25:04 +08003480 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003481}
3482
Chao Yu487261f2015-02-05 17:44:29 +08003483void f2fs_invalidate_page(struct page *page, unsigned int offset,
3484 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003485{
3486 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08003487 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003488
Chao Yu487261f2015-02-05 17:44:29 +08003489 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003490 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07003491 return;
3492
Chao Yu487261f2015-02-05 17:44:29 +08003493 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08003494 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08003495 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08003496 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08003497 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08003498 } else {
Chao Yu487261f2015-02-05 17:44:29 +08003499 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +08003500 f2fs_remove_dirty_inode(inode);
Chao Yu933439c2016-10-11 22:57:01 +08003501 }
Chao Yu487261f2015-02-05 17:44:29 +08003502 }
Chao Yudecd36b2015-08-07 18:42:09 +08003503
Chao Yu2baf0782018-07-27 18:15:16 +08003504 clear_cold_data(page);
3505
Chao Yudecd36b2015-08-07 18:42:09 +08003506 if (IS_ATOMIC_WRITTEN_PAGE(page))
Chao Yu4d57b862018-05-30 00:20:41 +08003507 return f2fs_drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08003508
Chao Yu240a5912019-03-06 17:30:59 +08003509 f2fs_clear_page_private(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003510}
3511
Chao Yu487261f2015-02-05 17:44:29 +08003512int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003513{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08003514 /* If this is dirty page, keep PagePrivate */
3515 if (PageDirty(page))
3516 return 0;
3517
Chao Yudecd36b2015-08-07 18:42:09 +08003518 /* This is atomic written page, keep Private */
3519 if (IS_ATOMIC_WRITTEN_PAGE(page))
3520 return 0;
3521
Chao Yu2baf0782018-07-27 18:15:16 +08003522 clear_cold_data(page);
Chao Yu240a5912019-03-06 17:30:59 +08003523 f2fs_clear_page_private(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09003524 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003525}
3526
3527static int f2fs_set_data_page_dirty(struct page *page)
3528{
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003529 struct inode *inode = page_file_mapping(page)->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003530
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09003531 trace_f2fs_set_page_dirty(page, DATA);
3532
Jaegeuk Kim237c0792016-06-30 18:49:15 -07003533 if (!PageUptodate(page))
3534 SetPageUptodate(page);
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003535 if (PageSwapCache(page))
3536 return __set_page_dirty_nobuffers(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003537
Chao Yu5fe45742017-01-07 18:50:26 +08003538 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08003539 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
Chao Yu4d57b862018-05-30 00:20:41 +08003540 f2fs_register_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08003541 return 1;
3542 }
3543 /*
3544 * Previously, this page has been registered, we just
3545 * return here.
3546 */
3547 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07003548 }
3549
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003550 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07003551 __set_page_dirty_nobuffers(page);
Chao Yu4d57b862018-05-30 00:20:41 +08003552 f2fs_update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003553 return 1;
3554 }
3555 return 0;
3556}
3557
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09003558static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3559{
Chao Yu454ae7e2014-04-22 13:34:01 +08003560 struct inode *inode = mapping->host;
3561
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07003562 if (f2fs_has_inline_data(inode))
3563 return 0;
3564
3565 /* make sure allocating whole blocks */
3566 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3567 filemap_write_and_wait(mapping);
3568
Chao Yue2b4e2b2015-08-19 19:11:19 +08003569 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08003570}
3571
Weichao Guo5b7a4872016-09-20 05:03:27 +08003572#ifdef CONFIG_MIGRATION
3573#include <linux/migrate.h>
3574
3575int f2fs_migrate_page(struct address_space *mapping,
3576 struct page *newpage, struct page *page, enum migrate_mode mode)
3577{
3578 int rc, extra_count;
3579 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3580 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3581
3582 BUG_ON(PageWriteback(page));
3583
3584 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07003585 if (atomic_written) {
3586 if (mode != MIGRATE_SYNC)
3587 return -EBUSY;
3588 if (!mutex_trylock(&fi->inmem_lock))
3589 return -EAGAIN;
3590 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08003591
Chao Yu240a5912019-03-06 17:30:59 +08003592 /* one extra reference was held for atomic_write page */
3593 extra_count = atomic_written ? 1 : 0;
Weichao Guo5b7a4872016-09-20 05:03:27 +08003594 rc = migrate_page_move_mapping(mapping, newpage,
Keith Busch37109692019-07-18 15:58:46 -07003595 page, extra_count);
Weichao Guo5b7a4872016-09-20 05:03:27 +08003596 if (rc != MIGRATEPAGE_SUCCESS) {
3597 if (atomic_written)
3598 mutex_unlock(&fi->inmem_lock);
3599 return rc;
3600 }
3601
3602 if (atomic_written) {
3603 struct inmem_pages *cur;
3604 list_for_each_entry(cur, &fi->inmem_pages, list)
3605 if (cur->page == page) {
3606 cur->page = newpage;
3607 break;
3608 }
3609 mutex_unlock(&fi->inmem_lock);
3610 put_page(page);
3611 get_page(newpage);
3612 }
3613
Chao Yu240a5912019-03-06 17:30:59 +08003614 if (PagePrivate(page)) {
3615 f2fs_set_page_private(newpage, page_private(page));
3616 f2fs_clear_page_private(page);
3617 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08003618
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07003619 if (mode != MIGRATE_SYNC_NO_COPY)
3620 migrate_page_copy(newpage, page);
3621 else
3622 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08003623
3624 return MIGRATEPAGE_SUCCESS;
3625}
3626#endif
3627
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003628#ifdef CONFIG_SWAP
3629/* Copied from generic_swapfile_activate() to check any holes */
Chao Yu3e5e4792019-12-27 18:44:56 +08003630static int check_swap_activate(struct swap_info_struct *sis,
3631 struct file *swap_file, sector_t *span)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003632{
3633 struct address_space *mapping = swap_file->f_mapping;
3634 struct inode *inode = mapping->host;
3635 unsigned blocks_per_page;
3636 unsigned long page_no;
3637 unsigned blkbits;
3638 sector_t probe_block;
3639 sector_t last_block;
3640 sector_t lowest_block = -1;
3641 sector_t highest_block = 0;
Chao Yu3e5e4792019-12-27 18:44:56 +08003642 int nr_extents = 0;
3643 int ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003644
3645 blkbits = inode->i_blkbits;
3646 blocks_per_page = PAGE_SIZE >> blkbits;
3647
3648 /*
3649 * Map all the blocks into the extent list. This code doesn't try
3650 * to be very smart.
3651 */
3652 probe_block = 0;
3653 page_no = 0;
3654 last_block = i_size_read(inode) >> blkbits;
Chao Yu3e5e4792019-12-27 18:44:56 +08003655 while ((probe_block + blocks_per_page) <= last_block &&
3656 page_no < sis->max) {
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003657 unsigned block_in_page;
3658 sector_t first_block;
3659
3660 cond_resched();
3661
3662 first_block = bmap(inode, probe_block);
3663 if (first_block == 0)
3664 goto bad_bmap;
3665
3666 /*
3667 * It must be PAGE_SIZE aligned on-disk
3668 */
3669 if (first_block & (blocks_per_page - 1)) {
3670 probe_block++;
3671 goto reprobe;
3672 }
3673
3674 for (block_in_page = 1; block_in_page < blocks_per_page;
3675 block_in_page++) {
3676 sector_t block;
3677
3678 block = bmap(inode, probe_block + block_in_page);
3679 if (block == 0)
3680 goto bad_bmap;
3681 if (block != first_block + block_in_page) {
3682 /* Discontiguity */
3683 probe_block++;
3684 goto reprobe;
3685 }
3686 }
3687
3688 first_block >>= (PAGE_SHIFT - blkbits);
3689 if (page_no) { /* exclude the header page */
3690 if (first_block < lowest_block)
3691 lowest_block = first_block;
3692 if (first_block > highest_block)
3693 highest_block = first_block;
3694 }
3695
Chao Yu3e5e4792019-12-27 18:44:56 +08003696 /*
3697 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3698 */
3699 ret = add_swap_extent(sis, page_no, 1, first_block);
3700 if (ret < 0)
3701 goto out;
3702 nr_extents += ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003703 page_no++;
3704 probe_block += blocks_per_page;
3705reprobe:
3706 continue;
3707 }
Chao Yu3e5e4792019-12-27 18:44:56 +08003708 ret = nr_extents;
3709 *span = 1 + highest_block - lowest_block;
3710 if (page_no == 0)
3711 page_no = 1; /* force Empty message */
3712 sis->max = page_no;
3713 sis->pages = page_no - 1;
3714 sis->highest_bit = page_no - 1;
3715out:
3716 return ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003717bad_bmap:
3718 pr_err("swapon: swapfile has holes\n");
3719 return -EINVAL;
3720}
3721
3722static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3723 sector_t *span)
3724{
3725 struct inode *inode = file_inode(file);
3726 int ret;
3727
3728 if (!S_ISREG(inode->i_mode))
3729 return -EINVAL;
3730
3731 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3732 return -EROFS;
3733
3734 ret = f2fs_convert_inline_inode(inode);
3735 if (ret)
3736 return ret;
3737
Chao Yu4c8ff702019-11-01 18:07:14 +08003738 if (f2fs_disable_compressed_file(inode))
3739 return -EINVAL;
3740
Chao Yu3e5e4792019-12-27 18:44:56 +08003741 ret = check_swap_activate(sis, file, span);
3742 if (ret < 0)
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003743 return ret;
3744
3745 set_inode_flag(inode, FI_PIN_FILE);
3746 f2fs_precache_extents(inode);
3747 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Chao Yu3e5e4792019-12-27 18:44:56 +08003748 return ret;
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003749}
3750
3751static void f2fs_swap_deactivate(struct file *file)
3752{
3753 struct inode *inode = file_inode(file);
3754
3755 clear_inode_flag(inode, FI_PIN_FILE);
3756}
3757#else
3758static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3759 sector_t *span)
3760{
3761 return -EOPNOTSUPP;
3762}
3763
3764static void f2fs_swap_deactivate(struct file *file)
3765{
3766}
3767#endif
3768
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003769const struct address_space_operations f2fs_dblock_aops = {
3770 .readpage = f2fs_read_data_page,
3771 .readpages = f2fs_read_data_pages,
3772 .writepage = f2fs_write_data_page,
3773 .writepages = f2fs_write_data_pages,
3774 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09003775 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003776 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08003777 .invalidatepage = f2fs_invalidate_page,
3778 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003779 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09003780 .bmap = f2fs_bmap,
Jaegeuk Kim4969c062019-07-01 19:15:29 -07003781 .swap_activate = f2fs_swap_activate,
3782 .swap_deactivate = f2fs_swap_deactivate,
Weichao Guo5b7a4872016-09-20 05:03:27 +08003783#ifdef CONFIG_MIGRATION
3784 .migratepage = f2fs_migrate_page,
3785#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003786};
Eric Biggers6dbb1792018-04-18 11:09:48 -07003787
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05003788void f2fs_clear_page_cache_dirty_tag(struct page *page)
Chao Yuaec2f722018-05-26 18:03:35 +08003789{
3790 struct address_space *mapping = page_mapping(page);
3791 unsigned long flags;
3792
3793 xa_lock_irqsave(&mapping->i_pages, flags);
Matthew Wilcox5ec2d992017-12-04 20:25:25 -05003794 __xa_clear_mark(&mapping->i_pages, page_index(page),
Chao Yuaec2f722018-05-26 18:03:35 +08003795 PAGECACHE_TAG_DIRTY);
3796 xa_unlock_irqrestore(&mapping->i_pages, flags);
3797}
3798
Eric Biggers6dbb1792018-04-18 11:09:48 -07003799int __init f2fs_init_post_read_processing(void)
3800{
Eric Biggers95ae2512019-07-22 09:26:24 -07003801 bio_post_read_ctx_cache =
3802 kmem_cache_create("f2fs_bio_post_read_ctx",
3803 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
Eric Biggers6dbb1792018-04-18 11:09:48 -07003804 if (!bio_post_read_ctx_cache)
3805 goto fail;
3806 bio_post_read_ctx_pool =
3807 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
3808 bio_post_read_ctx_cache);
3809 if (!bio_post_read_ctx_pool)
3810 goto fail_free_cache;
3811 return 0;
3812
3813fail_free_cache:
3814 kmem_cache_destroy(bio_post_read_ctx_cache);
3815fail:
3816 return -ENOMEM;
3817}
3818
Chao Yu0b20fce2019-09-30 18:53:25 +08003819void f2fs_destroy_post_read_processing(void)
Eric Biggers6dbb1792018-04-18 11:09:48 -07003820{
3821 mempool_destroy(bio_post_read_ctx_pool);
3822 kmem_cache_destroy(bio_post_read_ctx_cache);
3823}
Chao Yu0b20fce2019-09-30 18:53:25 +08003824
Chao Yu4c8ff702019-11-01 18:07:14 +08003825int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
3826{
3827 if (!f2fs_sb_has_encrypt(sbi) &&
3828 !f2fs_sb_has_verity(sbi) &&
3829 !f2fs_sb_has_compression(sbi))
3830 return 0;
3831
3832 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
3833 WQ_UNBOUND | WQ_HIGHPRI,
3834 num_online_cpus());
3835 if (!sbi->post_read_wq)
3836 return -ENOMEM;
3837 return 0;
3838}
3839
3840void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
3841{
3842 if (sbi->post_read_wq)
3843 destroy_workqueue(sbi->post_read_wq);
3844}
3845
Chao Yu0b20fce2019-09-30 18:53:25 +08003846int __init f2fs_init_bio_entry_cache(void)
3847{
3848 bio_entry_slab = f2fs_kmem_cache_create("bio_entry_slab",
3849 sizeof(struct bio_entry));
3850 if (!bio_entry_slab)
3851 return -ENOMEM;
3852 return 0;
3853}
3854
Chao Yuf5438052019-12-04 09:52:58 +08003855void f2fs_destroy_bio_entry_cache(void)
Chao Yu0b20fce2019-09-30 18:53:25 +08003856{
3857 kmem_cache_destroy(bio_entry_slab);
3858}