1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/f2fs/data.c
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/buffer_head.h>
11 #include <linux/sched/mm.h>
12 #include <linux/mpage.h>
13 #include <linux/writeback.h>
14 #include <linux/pagevec.h>
15 #include <linux/blkdev.h>
16 #include <linux/bio.h>
17 #include <linux/blk-crypto.h>
18 #include <linux/swap.h>
19 #include <linux/prefetch.h>
20 #include <linux/uio.h>
21 #include <linux/sched/signal.h>
22 #include <linux/fiemap.h>
23 #include <linux/iomap.h>
24 
25 #include "f2fs.h"
26 #include "node.h"
27 #include "segment.h"
28 #include "iostat.h"
29 #include <trace/events/f2fs.h>
30 
31 #define NUM_PREALLOC_POST_READ_CTXS	128
32 
33 static struct kmem_cache *bio_post_read_ctx_cache;
34 static struct kmem_cache *bio_entry_slab;
35 static mempool_t *bio_post_read_ctx_pool;
36 static struct bio_set f2fs_bioset;
37 
38 #define	F2FS_BIO_POOL_SIZE	NR_CURSEG_TYPE
39 
f2fs_init_bioset(void)40 int __init f2fs_init_bioset(void)
41 {
42 	if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
43 					0, BIOSET_NEED_BVECS))
44 		return -ENOMEM;
45 	return 0;
46 }
47 
f2fs_destroy_bioset(void)48 void f2fs_destroy_bioset(void)
49 {
50 	bioset_exit(&f2fs_bioset);
51 }
52 
__is_cp_guaranteed(struct page * page)53 static bool __is_cp_guaranteed(struct page *page)
54 {
55 	struct address_space *mapping = page->mapping;
56 	struct inode *inode;
57 	struct f2fs_sb_info *sbi;
58 
59 	if (!mapping)
60 		return false;
61 
62 	inode = mapping->host;
63 	sbi = F2FS_I_SB(inode);
64 
65 	if (inode->i_ino == F2FS_META_INO(sbi) ||
66 			inode->i_ino == F2FS_NODE_INO(sbi) ||
67 			S_ISDIR(inode->i_mode))
68 		return true;
69 
70 	if (f2fs_is_compressed_page(page))
71 		return false;
72 	if ((S_ISREG(inode->i_mode) && IS_NOQUOTA(inode)) ||
73 			page_private_gcing(page))
74 		return true;
75 	return false;
76 }
77 
__read_io_type(struct page * page)78 static enum count_type __read_io_type(struct page *page)
79 {
80 	struct address_space *mapping = page_file_mapping(page);
81 
82 	if (mapping) {
83 		struct inode *inode = mapping->host;
84 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
85 
86 		if (inode->i_ino == F2FS_META_INO(sbi))
87 			return F2FS_RD_META;
88 
89 		if (inode->i_ino == F2FS_NODE_INO(sbi))
90 			return F2FS_RD_NODE;
91 	}
92 	return F2FS_RD_DATA;
93 }
94 
95 /* postprocessing steps for read bios */
96 enum bio_post_read_step {
97 #ifdef CONFIG_FS_ENCRYPTION
98 	STEP_DECRYPT	= 1 << 0,
99 #else
100 	STEP_DECRYPT	= 0,	/* compile out the decryption-related code */
101 #endif
102 #ifdef CONFIG_F2FS_FS_COMPRESSION
103 	STEP_DECOMPRESS	= 1 << 1,
104 #else
105 	STEP_DECOMPRESS	= 0,	/* compile out the decompression-related code */
106 #endif
107 #ifdef CONFIG_FS_VERITY
108 	STEP_VERITY	= 1 << 2,
109 #else
110 	STEP_VERITY	= 0,	/* compile out the verity-related code */
111 #endif
112 };
113 
114 struct bio_post_read_ctx {
115 	struct bio *bio;
116 	struct f2fs_sb_info *sbi;
117 	struct work_struct work;
118 	unsigned int enabled_steps;
119 	block_t fs_blkaddr;
120 };
121 
f2fs_finish_read_bio(struct bio * bio)122 static void f2fs_finish_read_bio(struct bio *bio)
123 {
124 	struct bio_vec *bv;
125 	struct bvec_iter_all iter_all;
126 
127 	/*
128 	 * Update and unlock the bio's pagecache pages, and put the
129 	 * decompression context for any compressed pages.
130 	 */
131 	bio_for_each_segment_all(bv, bio, iter_all) {
132 		struct page *page = bv->bv_page;
133 
134 		if (f2fs_is_compressed_page(page)) {
135 			if (bio->bi_status)
136 				f2fs_end_read_compressed_page(page, true, 0);
137 			f2fs_put_page_dic(page);
138 			continue;
139 		}
140 
141 		/* PG_error was set if decryption or verity failed. */
142 		if (bio->bi_status || PageError(page)) {
143 			ClearPageUptodate(page);
144 			/* will re-read again later */
145 			ClearPageError(page);
146 		} else {
147 			SetPageUptodate(page);
148 		}
149 		dec_page_count(F2FS_P_SB(page), __read_io_type(page));
150 		unlock_page(page);
151 	}
152 
153 	if (bio->bi_private)
154 		mempool_free(bio->bi_private, bio_post_read_ctx_pool);
155 	bio_put(bio);
156 }
157 
f2fs_verify_bio(struct work_struct * work)158 static void f2fs_verify_bio(struct work_struct *work)
159 {
160 	struct bio_post_read_ctx *ctx =
161 		container_of(work, struct bio_post_read_ctx, work);
162 	struct bio *bio = ctx->bio;
163 	bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS);
164 
165 	/*
166 	 * fsverity_verify_bio() may call readahead() again, and while verity
167 	 * will be disabled for this, decryption and/or decompression may still
168 	 * be needed, resulting in another bio_post_read_ctx being allocated.
169 	 * So to prevent deadlocks we need to release the current ctx to the
170 	 * mempool first.  This assumes that verity is the last post-read step.
171 	 */
172 	mempool_free(ctx, bio_post_read_ctx_pool);
173 	bio->bi_private = NULL;
174 
175 	/*
176 	 * Verify the bio's pages with fs-verity.  Exclude compressed pages,
177 	 * as those were handled separately by f2fs_end_read_compressed_page().
178 	 */
179 	if (may_have_compressed_pages) {
180 		struct bio_vec *bv;
181 		struct bvec_iter_all iter_all;
182 
183 		bio_for_each_segment_all(bv, bio, iter_all) {
184 			struct page *page = bv->bv_page;
185 
186 			if (!f2fs_is_compressed_page(page) &&
187 			    !PageError(page) && !fsverity_verify_page(page))
188 				SetPageError(page);
189 		}
190 	} else {
191 		fsverity_verify_bio(bio);
192 	}
193 
194 	f2fs_finish_read_bio(bio);
195 }
196 
197 /*
198  * If the bio's data needs to be verified with fs-verity, then enqueue the
199  * verity work for the bio.  Otherwise finish the bio now.
200  *
201  * Note that to avoid deadlocks, the verity work can't be done on the
202  * decryption/decompression workqueue.  This is because verifying the data pages
203  * can involve reading verity metadata pages from the file, and these verity
204  * metadata pages may be encrypted and/or compressed.
205  */
f2fs_verify_and_finish_bio(struct bio * bio)206 static void f2fs_verify_and_finish_bio(struct bio *bio)
207 {
208 	struct bio_post_read_ctx *ctx = bio->bi_private;
209 
210 	if (ctx && (ctx->enabled_steps & STEP_VERITY)) {
211 		INIT_WORK(&ctx->work, f2fs_verify_bio);
212 		fsverity_enqueue_verify_work(&ctx->work);
213 	} else {
214 		f2fs_finish_read_bio(bio);
215 	}
216 }
217 
218 /*
219  * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last
220  * remaining page was read by @ctx->bio.
221  *
222  * Note that a bio may span clusters (even a mix of compressed and uncompressed
223  * clusters) or be for just part of a cluster.  STEP_DECOMPRESS just indicates
224  * that the bio includes at least one compressed page.  The actual decompression
225  * is done on a per-cluster basis, not a per-bio basis.
226  */
f2fs_handle_step_decompress(struct bio_post_read_ctx * ctx)227 static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx)
228 {
229 	struct bio_vec *bv;
230 	struct bvec_iter_all iter_all;
231 	bool all_compressed = true;
232 	block_t blkaddr = ctx->fs_blkaddr;
233 
234 	bio_for_each_segment_all(bv, ctx->bio, iter_all) {
235 		struct page *page = bv->bv_page;
236 
237 		/* PG_error was set if decryption failed. */
238 		if (f2fs_is_compressed_page(page))
239 			f2fs_end_read_compressed_page(page, PageError(page),
240 						blkaddr);
241 		else
242 			all_compressed = false;
243 
244 		blkaddr++;
245 	}
246 
247 	/*
248 	 * Optimization: if all the bio's pages are compressed, then scheduling
249 	 * the per-bio verity work is unnecessary, as verity will be fully
250 	 * handled at the compression cluster level.
251 	 */
252 	if (all_compressed)
253 		ctx->enabled_steps &= ~STEP_VERITY;
254 }
255 
f2fs_post_read_work(struct work_struct * work)256 static void f2fs_post_read_work(struct work_struct *work)
257 {
258 	struct bio_post_read_ctx *ctx =
259 		container_of(work, struct bio_post_read_ctx, work);
260 
261 	if (ctx->enabled_steps & STEP_DECRYPT)
262 		fscrypt_decrypt_bio(ctx->bio);
263 
264 	if (ctx->enabled_steps & STEP_DECOMPRESS)
265 		f2fs_handle_step_decompress(ctx);
266 
267 	f2fs_verify_and_finish_bio(ctx->bio);
268 }
269 
f2fs_read_end_io(struct bio * bio)270 static void f2fs_read_end_io(struct bio *bio)
271 {
272 	struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
273 	struct bio_post_read_ctx *ctx;
274 
275 	iostat_update_and_unbind_ctx(bio, 0);
276 	ctx = bio->bi_private;
277 
278 	if (time_to_inject(sbi, FAULT_READ_IO)) {
279 		f2fs_show_injection_info(sbi, FAULT_READ_IO);
280 		bio->bi_status = BLK_STS_IOERR;
281 	}
282 
283 	if (bio->bi_status) {
284 		f2fs_finish_read_bio(bio);
285 		return;
286 	}
287 
288 	if (ctx && (ctx->enabled_steps & (STEP_DECRYPT | STEP_DECOMPRESS))) {
289 		INIT_WORK(&ctx->work, f2fs_post_read_work);
290 		queue_work(ctx->sbi->post_read_wq, &ctx->work);
291 	} else {
292 		f2fs_verify_and_finish_bio(bio);
293 	}
294 }
295 
f2fs_write_end_io(struct bio * bio)296 static void f2fs_write_end_io(struct bio *bio)
297 {
298 	struct f2fs_sb_info *sbi;
299 	struct bio_vec *bvec;
300 	struct bvec_iter_all iter_all;
301 
302 	iostat_update_and_unbind_ctx(bio, 1);
303 	sbi = bio->bi_private;
304 
305 	if (time_to_inject(sbi, FAULT_WRITE_IO)) {
306 		f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
307 		bio->bi_status = BLK_STS_IOERR;
308 	}
309 
310 	bio_for_each_segment_all(bvec, bio, iter_all) {
311 		struct page *page = bvec->bv_page;
312 		enum count_type type = WB_DATA_TYPE(page);
313 
314 		if (page_private_dummy(page)) {
315 			clear_page_private_dummy(page);
316 			unlock_page(page);
317 			mempool_free(page, sbi->write_io_dummy);
318 
319 			if (unlikely(bio->bi_status))
320 				f2fs_stop_checkpoint(sbi, true);
321 			continue;
322 		}
323 
324 		fscrypt_finalize_bounce_page(&page);
325 
326 #ifdef CONFIG_F2FS_FS_COMPRESSION
327 		if (f2fs_is_compressed_page(page)) {
328 			f2fs_compress_write_end_io(bio, page);
329 			continue;
330 		}
331 #endif
332 
333 		if (unlikely(bio->bi_status)) {
334 			mapping_set_error(page->mapping, -EIO);
335 			if (type == F2FS_WB_CP_DATA)
336 				f2fs_stop_checkpoint(sbi, true);
337 		}
338 
339 		f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
340 					page->index != nid_of_node(page));
341 
342 		dec_page_count(sbi, type);
343 		if (f2fs_in_warm_node_list(sbi, page))
344 			f2fs_del_fsync_node_entry(sbi, page);
345 		clear_page_private_gcing(page);
346 		end_page_writeback(page);
347 	}
348 	if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
349 				wq_has_sleeper(&sbi->cp_wait))
350 		wake_up(&sbi->cp_wait);
351 
352 	bio_put(bio);
353 }
354 
f2fs_target_device(struct f2fs_sb_info * sbi,block_t blk_addr,sector_t * sector)355 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
356 		block_t blk_addr, sector_t *sector)
357 {
358 	struct block_device *bdev = sbi->sb->s_bdev;
359 	int i;
360 
361 	if (f2fs_is_multi_device(sbi)) {
362 		for (i = 0; i < sbi->s_ndevs; i++) {
363 			if (FDEV(i).start_blk <= blk_addr &&
364 			    FDEV(i).end_blk >= blk_addr) {
365 				blk_addr -= FDEV(i).start_blk;
366 				bdev = FDEV(i).bdev;
367 				break;
368 			}
369 		}
370 	}
371 
372 	if (sector)
373 		*sector = SECTOR_FROM_BLOCK(blk_addr);
374 	return bdev;
375 }
376 
f2fs_target_device_index(struct f2fs_sb_info * sbi,block_t blkaddr)377 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
378 {
379 	int i;
380 
381 	if (!f2fs_is_multi_device(sbi))
382 		return 0;
383 
384 	for (i = 0; i < sbi->s_ndevs; i++)
385 		if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
386 			return i;
387 	return 0;
388 }
389 
f2fs_io_flags(struct f2fs_io_info * fio)390 static unsigned int f2fs_io_flags(struct f2fs_io_info *fio)
391 {
392 	unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
393 	unsigned int fua_flag, meta_flag, io_flag;
394 	unsigned int op_flags = 0;
395 
396 	if (fio->op != REQ_OP_WRITE)
397 		return 0;
398 	if (fio->type == DATA)
399 		io_flag = fio->sbi->data_io_flag;
400 	else if (fio->type == NODE)
401 		io_flag = fio->sbi->node_io_flag;
402 	else
403 		return 0;
404 
405 	fua_flag = io_flag & temp_mask;
406 	meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
407 
408 	/*
409 	 * data/node io flag bits per temp:
410 	 *      REQ_META     |      REQ_FUA      |
411 	 *    5 |    4 |   3 |    2 |    1 |   0 |
412 	 * Cold | Warm | Hot | Cold | Warm | Hot |
413 	 */
414 	if ((1 << fio->temp) & meta_flag)
415 		op_flags |= REQ_META;
416 	if ((1 << fio->temp) & fua_flag)
417 		op_flags |= REQ_FUA;
418 	return op_flags;
419 }
420 
__bio_alloc(struct f2fs_io_info * fio,int npages)421 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
422 {
423 	struct f2fs_sb_info *sbi = fio->sbi;
424 	struct block_device *bdev;
425 	sector_t sector;
426 	struct bio *bio;
427 
428 	bdev = f2fs_target_device(sbi, fio->new_blkaddr, &sector);
429 	bio = bio_alloc_bioset(bdev, npages,
430 				fio->op | fio->op_flags | f2fs_io_flags(fio),
431 				GFP_NOIO, &f2fs_bioset);
432 	bio->bi_iter.bi_sector = sector;
433 	if (is_read_io(fio->op)) {
434 		bio->bi_end_io = f2fs_read_end_io;
435 		bio->bi_private = NULL;
436 	} else {
437 		bio->bi_end_io = f2fs_write_end_io;
438 		bio->bi_private = sbi;
439 	}
440 	iostat_alloc_and_bind_ctx(sbi, bio, NULL);
441 
442 	if (fio->io_wbc)
443 		wbc_init_bio(fio->io_wbc, bio);
444 
445 	return bio;
446 }
447 
f2fs_set_bio_crypt_ctx(struct bio * bio,const struct inode * inode,pgoff_t first_idx,const struct f2fs_io_info * fio,gfp_t gfp_mask)448 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
449 				  pgoff_t first_idx,
450 				  const struct f2fs_io_info *fio,
451 				  gfp_t gfp_mask)
452 {
453 	/*
454 	 * The f2fs garbage collector sets ->encrypted_page when it wants to
455 	 * read/write raw data without encryption.
456 	 */
457 	if (!fio || !fio->encrypted_page)
458 		fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
459 }
460 
f2fs_crypt_mergeable_bio(struct bio * bio,const struct inode * inode,pgoff_t next_idx,const struct f2fs_io_info * fio)461 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
462 				     pgoff_t next_idx,
463 				     const struct f2fs_io_info *fio)
464 {
465 	/*
466 	 * The f2fs garbage collector sets ->encrypted_page when it wants to
467 	 * read/write raw data without encryption.
468 	 */
469 	if (fio && fio->encrypted_page)
470 		return !bio_has_crypt_ctx(bio);
471 
472 	return fscrypt_mergeable_bio(bio, inode, next_idx);
473 }
474 
__submit_bio(struct f2fs_sb_info * sbi,struct bio * bio,enum page_type type)475 static inline void __submit_bio(struct f2fs_sb_info *sbi,
476 				struct bio *bio, enum page_type type)
477 {
478 	if (!is_read_io(bio_op(bio))) {
479 		unsigned int start;
480 
481 		if (type != DATA && type != NODE)
482 			goto submit_io;
483 
484 		if (f2fs_lfs_mode(sbi) && current->plug)
485 			blk_finish_plug(current->plug);
486 
487 		if (!F2FS_IO_ALIGNED(sbi))
488 			goto submit_io;
489 
490 		start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
491 		start %= F2FS_IO_SIZE(sbi);
492 
493 		if (start == 0)
494 			goto submit_io;
495 
496 		/* fill dummy pages */
497 		for (; start < F2FS_IO_SIZE(sbi); start++) {
498 			struct page *page =
499 				mempool_alloc(sbi->write_io_dummy,
500 					      GFP_NOIO | __GFP_NOFAIL);
501 			f2fs_bug_on(sbi, !page);
502 
503 			lock_page(page);
504 
505 			zero_user_segment(page, 0, PAGE_SIZE);
506 			set_page_private_dummy(page);
507 
508 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
509 				f2fs_bug_on(sbi, 1);
510 		}
511 		/*
512 		 * In the NODE case, we lose next block address chain. So, we
513 		 * need to do checkpoint in f2fs_sync_file.
514 		 */
515 		if (type == NODE)
516 			set_sbi_flag(sbi, SBI_NEED_CP);
517 	}
518 submit_io:
519 	if (is_read_io(bio_op(bio)))
520 		trace_f2fs_submit_read_bio(sbi->sb, type, bio);
521 	else
522 		trace_f2fs_submit_write_bio(sbi->sb, type, bio);
523 
524 	iostat_update_submit_ctx(bio, type);
525 	submit_bio(bio);
526 }
527 
f2fs_submit_bio(struct f2fs_sb_info * sbi,struct bio * bio,enum page_type type)528 void f2fs_submit_bio(struct f2fs_sb_info *sbi,
529 				struct bio *bio, enum page_type type)
530 {
531 	__submit_bio(sbi, bio, type);
532 }
533 
__submit_merged_bio(struct f2fs_bio_info * io)534 static void __submit_merged_bio(struct f2fs_bio_info *io)
535 {
536 	struct f2fs_io_info *fio = &io->fio;
537 
538 	if (!io->bio)
539 		return;
540 
541 	if (is_read_io(fio->op))
542 		trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
543 	else
544 		trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
545 
546 	__submit_bio(io->sbi, io->bio, fio->type);
547 	io->bio = NULL;
548 }
549 
__has_merged_page(struct bio * bio,struct inode * inode,struct page * page,nid_t ino)550 static bool __has_merged_page(struct bio *bio, struct inode *inode,
551 						struct page *page, nid_t ino)
552 {
553 	struct bio_vec *bvec;
554 	struct bvec_iter_all iter_all;
555 
556 	if (!bio)
557 		return false;
558 
559 	if (!inode && !page && !ino)
560 		return true;
561 
562 	bio_for_each_segment_all(bvec, bio, iter_all) {
563 		struct page *target = bvec->bv_page;
564 
565 		if (fscrypt_is_bounce_page(target)) {
566 			target = fscrypt_pagecache_page(target);
567 			if (IS_ERR(target))
568 				continue;
569 		}
570 		if (f2fs_is_compressed_page(target)) {
571 			target = f2fs_compress_control_page(target);
572 			if (IS_ERR(target))
573 				continue;
574 		}
575 
576 		if (inode && inode == target->mapping->host)
577 			return true;
578 		if (page && page == target)
579 			return true;
580 		if (ino && ino == ino_of_node(target))
581 			return true;
582 	}
583 
584 	return false;
585 }
586 
f2fs_init_write_merge_io(struct f2fs_sb_info * sbi)587 int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi)
588 {
589 	int i;
590 
591 	for (i = 0; i < NR_PAGE_TYPE; i++) {
592 		int n = (i == META) ? 1 : NR_TEMP_TYPE;
593 		int j;
594 
595 		sbi->write_io[i] = f2fs_kmalloc(sbi,
596 				array_size(n, sizeof(struct f2fs_bio_info)),
597 				GFP_KERNEL);
598 		if (!sbi->write_io[i])
599 			return -ENOMEM;
600 
601 		for (j = HOT; j < n; j++) {
602 			init_f2fs_rwsem(&sbi->write_io[i][j].io_rwsem);
603 			sbi->write_io[i][j].sbi = sbi;
604 			sbi->write_io[i][j].bio = NULL;
605 			spin_lock_init(&sbi->write_io[i][j].io_lock);
606 			INIT_LIST_HEAD(&sbi->write_io[i][j].io_list);
607 			INIT_LIST_HEAD(&sbi->write_io[i][j].bio_list);
608 			init_f2fs_rwsem(&sbi->write_io[i][j].bio_list_lock);
609 		}
610 	}
611 
612 	return 0;
613 }
614 
__f2fs_submit_merged_write(struct f2fs_sb_info * sbi,enum page_type type,enum temp_type temp)615 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
616 				enum page_type type, enum temp_type temp)
617 {
618 	enum page_type btype = PAGE_TYPE_OF_BIO(type);
619 	struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
620 
621 	f2fs_down_write(&io->io_rwsem);
622 
623 	/* change META to META_FLUSH in the checkpoint procedure */
624 	if (type >= META_FLUSH) {
625 		io->fio.type = META_FLUSH;
626 		io->bio->bi_opf |= REQ_META | REQ_PRIO | REQ_SYNC;
627 		if (!test_opt(sbi, NOBARRIER))
628 			io->bio->bi_opf |= REQ_PREFLUSH | REQ_FUA;
629 	}
630 	__submit_merged_bio(io);
631 	f2fs_up_write(&io->io_rwsem);
632 }
633 
__submit_merged_write_cond(struct f2fs_sb_info * sbi,struct inode * inode,struct page * page,nid_t ino,enum page_type type,bool force)634 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
635 				struct inode *inode, struct page *page,
636 				nid_t ino, enum page_type type, bool force)
637 {
638 	enum temp_type temp;
639 	bool ret = true;
640 
641 	for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
642 		if (!force)	{
643 			enum page_type btype = PAGE_TYPE_OF_BIO(type);
644 			struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
645 
646 			f2fs_down_read(&io->io_rwsem);
647 			ret = __has_merged_page(io->bio, inode, page, ino);
648 			f2fs_up_read(&io->io_rwsem);
649 		}
650 		if (ret)
651 			__f2fs_submit_merged_write(sbi, type, temp);
652 
653 		/* TODO: use HOT temp only for meta pages now. */
654 		if (type >= META)
655 			break;
656 	}
657 }
658 
f2fs_submit_merged_write(struct f2fs_sb_info * sbi,enum page_type type)659 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
660 {
661 	__submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
662 }
663 
f2fs_submit_merged_write_cond(struct f2fs_sb_info * sbi,struct inode * inode,struct page * page,nid_t ino,enum page_type type)664 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
665 				struct inode *inode, struct page *page,
666 				nid_t ino, enum page_type type)
667 {
668 	__submit_merged_write_cond(sbi, inode, page, ino, type, false);
669 }
670 
f2fs_flush_merged_writes(struct f2fs_sb_info * sbi)671 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
672 {
673 	f2fs_submit_merged_write(sbi, DATA);
674 	f2fs_submit_merged_write(sbi, NODE);
675 	f2fs_submit_merged_write(sbi, META);
676 }
677 
678 /*
679  * Fill the locked page with data located in the block address.
680  * A caller needs to unlock the page on failure.
681  */
f2fs_submit_page_bio(struct f2fs_io_info * fio)682 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
683 {
684 	struct bio *bio;
685 	struct page *page = fio->encrypted_page ?
686 			fio->encrypted_page : fio->page;
687 
688 	if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
689 			fio->is_por ? META_POR : (__is_meta_io(fio) ?
690 			META_GENERIC : DATA_GENERIC_ENHANCE)))
691 		return -EFSCORRUPTED;
692 
693 	trace_f2fs_submit_page_bio(page, fio);
694 
695 	/* Allocate a new bio */
696 	bio = __bio_alloc(fio, 1);
697 
698 	f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
699 			       fio->page->index, fio, GFP_NOIO);
700 
701 	if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
702 		bio_put(bio);
703 		return -EFAULT;
704 	}
705 
706 	if (fio->io_wbc && !is_read_io(fio->op))
707 		wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
708 
709 	inc_page_count(fio->sbi, is_read_io(fio->op) ?
710 			__read_io_type(page): WB_DATA_TYPE(fio->page));
711 
712 	__submit_bio(fio->sbi, bio, fio->type);
713 	return 0;
714 }
715 
page_is_mergeable(struct f2fs_sb_info * sbi,struct bio * bio,block_t last_blkaddr,block_t cur_blkaddr)716 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
717 				block_t last_blkaddr, block_t cur_blkaddr)
718 {
719 	if (unlikely(sbi->max_io_bytes &&
720 			bio->bi_iter.bi_size >= sbi->max_io_bytes))
721 		return false;
722 	if (last_blkaddr + 1 != cur_blkaddr)
723 		return false;
724 	return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL);
725 }
726 
io_type_is_mergeable(struct f2fs_bio_info * io,struct f2fs_io_info * fio)727 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
728 						struct f2fs_io_info *fio)
729 {
730 	if (io->fio.op != fio->op)
731 		return false;
732 	return io->fio.op_flags == fio->op_flags;
733 }
734 
io_is_mergeable(struct f2fs_sb_info * sbi,struct bio * bio,struct f2fs_bio_info * io,struct f2fs_io_info * fio,block_t last_blkaddr,block_t cur_blkaddr)735 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
736 					struct f2fs_bio_info *io,
737 					struct f2fs_io_info *fio,
738 					block_t last_blkaddr,
739 					block_t cur_blkaddr)
740 {
741 	if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
742 		unsigned int filled_blocks =
743 				F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
744 		unsigned int io_size = F2FS_IO_SIZE(sbi);
745 		unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
746 
747 		/* IOs in bio is aligned and left space of vectors is not enough */
748 		if (!(filled_blocks % io_size) && left_vecs < io_size)
749 			return false;
750 	}
751 	if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
752 		return false;
753 	return io_type_is_mergeable(io, fio);
754 }
755 
add_bio_entry(struct f2fs_sb_info * sbi,struct bio * bio,struct page * page,enum temp_type temp)756 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
757 				struct page *page, enum temp_type temp)
758 {
759 	struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
760 	struct bio_entry *be;
761 
762 	be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL);
763 	be->bio = bio;
764 	bio_get(bio);
765 
766 	if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
767 		f2fs_bug_on(sbi, 1);
768 
769 	f2fs_down_write(&io->bio_list_lock);
770 	list_add_tail(&be->list, &io->bio_list);
771 	f2fs_up_write(&io->bio_list_lock);
772 }
773 
del_bio_entry(struct bio_entry * be)774 static void del_bio_entry(struct bio_entry *be)
775 {
776 	list_del(&be->list);
777 	kmem_cache_free(bio_entry_slab, be);
778 }
779 
add_ipu_page(struct f2fs_io_info * fio,struct bio ** bio,struct page * page)780 static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
781 							struct page *page)
782 {
783 	struct f2fs_sb_info *sbi = fio->sbi;
784 	enum temp_type temp;
785 	bool found = false;
786 	int ret = -EAGAIN;
787 
788 	for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
789 		struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
790 		struct list_head *head = &io->bio_list;
791 		struct bio_entry *be;
792 
793 		f2fs_down_write(&io->bio_list_lock);
794 		list_for_each_entry(be, head, list) {
795 			if (be->bio != *bio)
796 				continue;
797 
798 			found = true;
799 
800 			f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
801 							    *fio->last_block,
802 							    fio->new_blkaddr));
803 			if (f2fs_crypt_mergeable_bio(*bio,
804 					fio->page->mapping->host,
805 					fio->page->index, fio) &&
806 			    bio_add_page(*bio, page, PAGE_SIZE, 0) ==
807 					PAGE_SIZE) {
808 				ret = 0;
809 				break;
810 			}
811 
812 			/* page can't be merged into bio; submit the bio */
813 			del_bio_entry(be);
814 			__submit_bio(sbi, *bio, DATA);
815 			break;
816 		}
817 		f2fs_up_write(&io->bio_list_lock);
818 	}
819 
820 	if (ret) {
821 		bio_put(*bio);
822 		*bio = NULL;
823 	}
824 
825 	return ret;
826 }
827 
f2fs_submit_merged_ipu_write(struct f2fs_sb_info * sbi,struct bio ** bio,struct page * page)828 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
829 					struct bio **bio, struct page *page)
830 {
831 	enum temp_type temp;
832 	bool found = false;
833 	struct bio *target = bio ? *bio : NULL;
834 
835 	for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
836 		struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
837 		struct list_head *head = &io->bio_list;
838 		struct bio_entry *be;
839 
840 		if (list_empty(head))
841 			continue;
842 
843 		f2fs_down_read(&io->bio_list_lock);
844 		list_for_each_entry(be, head, list) {
845 			if (target)
846 				found = (target == be->bio);
847 			else
848 				found = __has_merged_page(be->bio, NULL,
849 								page, 0);
850 			if (found)
851 				break;
852 		}
853 		f2fs_up_read(&io->bio_list_lock);
854 
855 		if (!found)
856 			continue;
857 
858 		found = false;
859 
860 		f2fs_down_write(&io->bio_list_lock);
861 		list_for_each_entry(be, head, list) {
862 			if (target)
863 				found = (target == be->bio);
864 			else
865 				found = __has_merged_page(be->bio, NULL,
866 								page, 0);
867 			if (found) {
868 				target = be->bio;
869 				del_bio_entry(be);
870 				break;
871 			}
872 		}
873 		f2fs_up_write(&io->bio_list_lock);
874 	}
875 
876 	if (found)
877 		__submit_bio(sbi, target, DATA);
878 	if (bio && *bio) {
879 		bio_put(*bio);
880 		*bio = NULL;
881 	}
882 }
883 
f2fs_merge_page_bio(struct f2fs_io_info * fio)884 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
885 {
886 	struct bio *bio = *fio->bio;
887 	struct page *page = fio->encrypted_page ?
888 			fio->encrypted_page : fio->page;
889 
890 	if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
891 			__is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
892 		return -EFSCORRUPTED;
893 
894 	trace_f2fs_submit_page_bio(page, fio);
895 
896 	if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
897 						fio->new_blkaddr))
898 		f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
899 alloc_new:
900 	if (!bio) {
901 		bio = __bio_alloc(fio, BIO_MAX_VECS);
902 		f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
903 				       fio->page->index, fio, GFP_NOIO);
904 
905 		add_bio_entry(fio->sbi, bio, page, fio->temp);
906 	} else {
907 		if (add_ipu_page(fio, &bio, page))
908 			goto alloc_new;
909 	}
910 
911 	if (fio->io_wbc)
912 		wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
913 
914 	inc_page_count(fio->sbi, WB_DATA_TYPE(page));
915 
916 	*fio->last_block = fio->new_blkaddr;
917 	*fio->bio = bio;
918 
919 	return 0;
920 }
921 
f2fs_submit_page_write(struct f2fs_io_info * fio)922 void f2fs_submit_page_write(struct f2fs_io_info *fio)
923 {
924 	struct f2fs_sb_info *sbi = fio->sbi;
925 	enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
926 	struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
927 	struct page *bio_page;
928 
929 	f2fs_bug_on(sbi, is_read_io(fio->op));
930 
931 	f2fs_down_write(&io->io_rwsem);
932 next:
933 	if (fio->in_list) {
934 		spin_lock(&io->io_lock);
935 		if (list_empty(&io->io_list)) {
936 			spin_unlock(&io->io_lock);
937 			goto out;
938 		}
939 		fio = list_first_entry(&io->io_list,
940 						struct f2fs_io_info, list);
941 		list_del(&fio->list);
942 		spin_unlock(&io->io_lock);
943 	}
944 
945 	verify_fio_blkaddr(fio);
946 
947 	if (fio->encrypted_page)
948 		bio_page = fio->encrypted_page;
949 	else if (fio->compressed_page)
950 		bio_page = fio->compressed_page;
951 	else
952 		bio_page = fio->page;
953 
954 	/* set submitted = true as a return value */
955 	fio->submitted = true;
956 
957 	inc_page_count(sbi, WB_DATA_TYPE(bio_page));
958 
959 	if (io->bio &&
960 	    (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
961 			      fio->new_blkaddr) ||
962 	     !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
963 				       bio_page->index, fio)))
964 		__submit_merged_bio(io);
965 alloc_new:
966 	if (io->bio == NULL) {
967 		if (F2FS_IO_ALIGNED(sbi) &&
968 				(fio->type == DATA || fio->type == NODE) &&
969 				fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
970 			dec_page_count(sbi, WB_DATA_TYPE(bio_page));
971 			fio->retry = true;
972 			goto skip;
973 		}
974 		io->bio = __bio_alloc(fio, BIO_MAX_VECS);
975 		f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
976 				       bio_page->index, fio, GFP_NOIO);
977 		io->fio = *fio;
978 	}
979 
980 	if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
981 		__submit_merged_bio(io);
982 		goto alloc_new;
983 	}
984 
985 	if (fio->io_wbc)
986 		wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
987 
988 	io->last_block_in_bio = fio->new_blkaddr;
989 
990 	trace_f2fs_submit_page_write(fio->page, fio);
991 skip:
992 	if (fio->in_list)
993 		goto next;
994 out:
995 	if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
996 				!f2fs_is_checkpoint_ready(sbi))
997 		__submit_merged_bio(io);
998 	f2fs_up_write(&io->io_rwsem);
999 }
1000 
f2fs_grab_read_bio(struct inode * inode,block_t blkaddr,unsigned nr_pages,unsigned op_flag,pgoff_t first_idx,bool for_write)1001 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
1002 				      unsigned nr_pages, unsigned op_flag,
1003 				      pgoff_t first_idx, bool for_write)
1004 {
1005 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1006 	struct bio *bio;
1007 	struct bio_post_read_ctx *ctx = NULL;
1008 	unsigned int post_read_steps = 0;
1009 	sector_t sector;
1010 	struct block_device *bdev = f2fs_target_device(sbi, blkaddr, &sector);
1011 
1012 	bio = bio_alloc_bioset(bdev, bio_max_segs(nr_pages),
1013 			       REQ_OP_READ | op_flag,
1014 			       for_write ? GFP_NOIO : GFP_KERNEL, &f2fs_bioset);
1015 	if (!bio)
1016 		return ERR_PTR(-ENOMEM);
1017 	bio->bi_iter.bi_sector = sector;
1018 	f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
1019 	bio->bi_end_io = f2fs_read_end_io;
1020 
1021 	if (fscrypt_inode_uses_fs_layer_crypto(inode))
1022 		post_read_steps |= STEP_DECRYPT;
1023 
1024 	if (f2fs_need_verity(inode, first_idx))
1025 		post_read_steps |= STEP_VERITY;
1026 
1027 	/*
1028 	 * STEP_DECOMPRESS is handled specially, since a compressed file might
1029 	 * contain both compressed and uncompressed clusters.  We'll allocate a
1030 	 * bio_post_read_ctx if the file is compressed, but the caller is
1031 	 * responsible for enabling STEP_DECOMPRESS if it's actually needed.
1032 	 */
1033 
1034 	if (post_read_steps || f2fs_compressed_file(inode)) {
1035 		/* Due to the mempool, this never fails. */
1036 		ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1037 		ctx->bio = bio;
1038 		ctx->sbi = sbi;
1039 		ctx->enabled_steps = post_read_steps;
1040 		ctx->fs_blkaddr = blkaddr;
1041 		bio->bi_private = ctx;
1042 	}
1043 	iostat_alloc_and_bind_ctx(sbi, bio, ctx);
1044 
1045 	return bio;
1046 }
1047 
1048 /* This can handle encryption stuffs */
f2fs_submit_page_read(struct inode * inode,struct page * page,block_t blkaddr,int op_flags,bool for_write)1049 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1050 				 block_t blkaddr, int op_flags, bool for_write)
1051 {
1052 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1053 	struct bio *bio;
1054 
1055 	bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1056 					page->index, for_write);
1057 	if (IS_ERR(bio))
1058 		return PTR_ERR(bio);
1059 
1060 	/* wait for GCed page writeback via META_MAPPING */
1061 	f2fs_wait_on_block_writeback(inode, blkaddr);
1062 
1063 	if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1064 		bio_put(bio);
1065 		return -EFAULT;
1066 	}
1067 	ClearPageError(page);
1068 	inc_page_count(sbi, F2FS_RD_DATA);
1069 	f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1070 	__submit_bio(sbi, bio, DATA);
1071 	return 0;
1072 }
1073 
__set_data_blkaddr(struct dnode_of_data * dn)1074 static void __set_data_blkaddr(struct dnode_of_data *dn)
1075 {
1076 	struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1077 	__le32 *addr_array;
1078 	int base = 0;
1079 
1080 	if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1081 		base = get_extra_isize(dn->inode);
1082 
1083 	/* Get physical address of data block */
1084 	addr_array = blkaddr_in_node(rn);
1085 	addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1086 }
1087 
1088 /*
1089  * Lock ordering for the change of data block address:
1090  * ->data_page
1091  *  ->node_page
1092  *    update block addresses in the node page
1093  */
f2fs_set_data_blkaddr(struct dnode_of_data * dn)1094 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1095 {
1096 	f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1097 	__set_data_blkaddr(dn);
1098 	if (set_page_dirty(dn->node_page))
1099 		dn->node_changed = true;
1100 }
1101 
f2fs_update_data_blkaddr(struct dnode_of_data * dn,block_t blkaddr)1102 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1103 {
1104 	dn->data_blkaddr = blkaddr;
1105 	f2fs_set_data_blkaddr(dn);
1106 	f2fs_update_extent_cache(dn);
1107 }
1108 
1109 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
f2fs_reserve_new_blocks(struct dnode_of_data * dn,blkcnt_t count)1110 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1111 {
1112 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1113 	int err;
1114 
1115 	if (!count)
1116 		return 0;
1117 
1118 	if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1119 		return -EPERM;
1120 	if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1121 		return err;
1122 
1123 	trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1124 						dn->ofs_in_node, count);
1125 
1126 	f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1127 
1128 	for (; count > 0; dn->ofs_in_node++) {
1129 		block_t blkaddr = f2fs_data_blkaddr(dn);
1130 
1131 		if (blkaddr == NULL_ADDR) {
1132 			dn->data_blkaddr = NEW_ADDR;
1133 			__set_data_blkaddr(dn);
1134 			count--;
1135 		}
1136 	}
1137 
1138 	if (set_page_dirty(dn->node_page))
1139 		dn->node_changed = true;
1140 	return 0;
1141 }
1142 
1143 /* Should keep dn->ofs_in_node unchanged */
f2fs_reserve_new_block(struct dnode_of_data * dn)1144 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1145 {
1146 	unsigned int ofs_in_node = dn->ofs_in_node;
1147 	int ret;
1148 
1149 	ret = f2fs_reserve_new_blocks(dn, 1);
1150 	dn->ofs_in_node = ofs_in_node;
1151 	return ret;
1152 }
1153 
f2fs_reserve_block(struct dnode_of_data * dn,pgoff_t index)1154 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1155 {
1156 	bool need_put = dn->inode_page ? false : true;
1157 	int err;
1158 
1159 	err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1160 	if (err)
1161 		return err;
1162 
1163 	if (dn->data_blkaddr == NULL_ADDR)
1164 		err = f2fs_reserve_new_block(dn);
1165 	if (err || need_put)
1166 		f2fs_put_dnode(dn);
1167 	return err;
1168 }
1169 
f2fs_get_block(struct dnode_of_data * dn,pgoff_t index)1170 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1171 {
1172 	struct extent_info ei = {0, };
1173 	struct inode *inode = dn->inode;
1174 
1175 	if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1176 		dn->data_blkaddr = ei.blk + index - ei.fofs;
1177 		return 0;
1178 	}
1179 
1180 	return f2fs_reserve_block(dn, index);
1181 }
1182 
f2fs_get_read_data_page(struct inode * inode,pgoff_t index,int op_flags,bool for_write)1183 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1184 						int op_flags, bool for_write)
1185 {
1186 	struct address_space *mapping = inode->i_mapping;
1187 	struct dnode_of_data dn;
1188 	struct page *page;
1189 	struct extent_info ei = {0, };
1190 	int err;
1191 
1192 	page = f2fs_grab_cache_page(mapping, index, for_write);
1193 	if (!page)
1194 		return ERR_PTR(-ENOMEM);
1195 
1196 	if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1197 		dn.data_blkaddr = ei.blk + index - ei.fofs;
1198 		if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1199 						DATA_GENERIC_ENHANCE_READ)) {
1200 			err = -EFSCORRUPTED;
1201 			goto put_err;
1202 		}
1203 		goto got_it;
1204 	}
1205 
1206 	set_new_dnode(&dn, inode, NULL, NULL, 0);
1207 	err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1208 	if (err)
1209 		goto put_err;
1210 	f2fs_put_dnode(&dn);
1211 
1212 	if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1213 		err = -ENOENT;
1214 		goto put_err;
1215 	}
1216 	if (dn.data_blkaddr != NEW_ADDR &&
1217 			!f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1218 						dn.data_blkaddr,
1219 						DATA_GENERIC_ENHANCE)) {
1220 		err = -EFSCORRUPTED;
1221 		goto put_err;
1222 	}
1223 got_it:
1224 	if (PageUptodate(page)) {
1225 		unlock_page(page);
1226 		return page;
1227 	}
1228 
1229 	/*
1230 	 * A new dentry page is allocated but not able to be written, since its
1231 	 * new inode page couldn't be allocated due to -ENOSPC.
1232 	 * In such the case, its blkaddr can be remained as NEW_ADDR.
1233 	 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1234 	 * f2fs_init_inode_metadata.
1235 	 */
1236 	if (dn.data_blkaddr == NEW_ADDR) {
1237 		zero_user_segment(page, 0, PAGE_SIZE);
1238 		if (!PageUptodate(page))
1239 			SetPageUptodate(page);
1240 		unlock_page(page);
1241 		return page;
1242 	}
1243 
1244 	err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1245 						op_flags, for_write);
1246 	if (err)
1247 		goto put_err;
1248 	return page;
1249 
1250 put_err:
1251 	f2fs_put_page(page, 1);
1252 	return ERR_PTR(err);
1253 }
1254 
f2fs_find_data_page(struct inode * inode,pgoff_t index)1255 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1256 {
1257 	struct address_space *mapping = inode->i_mapping;
1258 	struct page *page;
1259 
1260 	page = find_get_page(mapping, index);
1261 	if (page && PageUptodate(page))
1262 		return page;
1263 	f2fs_put_page(page, 0);
1264 
1265 	page = f2fs_get_read_data_page(inode, index, 0, false);
1266 	if (IS_ERR(page))
1267 		return page;
1268 
1269 	if (PageUptodate(page))
1270 		return page;
1271 
1272 	wait_on_page_locked(page);
1273 	if (unlikely(!PageUptodate(page))) {
1274 		f2fs_put_page(page, 0);
1275 		return ERR_PTR(-EIO);
1276 	}
1277 	return page;
1278 }
1279 
1280 /*
1281  * If it tries to access a hole, return an error.
1282  * Because, the callers, functions in dir.c and GC, should be able to know
1283  * whether this page exists or not.
1284  */
f2fs_get_lock_data_page(struct inode * inode,pgoff_t index,bool for_write)1285 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1286 							bool for_write)
1287 {
1288 	struct address_space *mapping = inode->i_mapping;
1289 	struct page *page;
1290 repeat:
1291 	page = f2fs_get_read_data_page(inode, index, 0, for_write);
1292 	if (IS_ERR(page))
1293 		return page;
1294 
1295 	/* wait for read completion */
1296 	lock_page(page);
1297 	if (unlikely(page->mapping != mapping)) {
1298 		f2fs_put_page(page, 1);
1299 		goto repeat;
1300 	}
1301 	if (unlikely(!PageUptodate(page))) {
1302 		f2fs_put_page(page, 1);
1303 		return ERR_PTR(-EIO);
1304 	}
1305 	return page;
1306 }
1307 
1308 /*
1309  * Caller ensures that this data page is never allocated.
1310  * A new zero-filled data page is allocated in the page cache.
1311  *
1312  * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1313  * f2fs_unlock_op().
1314  * Note that, ipage is set only by make_empty_dir, and if any error occur,
1315  * ipage should be released by this function.
1316  */
f2fs_get_new_data_page(struct inode * inode,struct page * ipage,pgoff_t index,bool new_i_size)1317 struct page *f2fs_get_new_data_page(struct inode *inode,
1318 		struct page *ipage, pgoff_t index, bool new_i_size)
1319 {
1320 	struct address_space *mapping = inode->i_mapping;
1321 	struct page *page;
1322 	struct dnode_of_data dn;
1323 	int err;
1324 
1325 	page = f2fs_grab_cache_page(mapping, index, true);
1326 	if (!page) {
1327 		/*
1328 		 * before exiting, we should make sure ipage will be released
1329 		 * if any error occur.
1330 		 */
1331 		f2fs_put_page(ipage, 1);
1332 		return ERR_PTR(-ENOMEM);
1333 	}
1334 
1335 	set_new_dnode(&dn, inode, ipage, NULL, 0);
1336 	err = f2fs_reserve_block(&dn, index);
1337 	if (err) {
1338 		f2fs_put_page(page, 1);
1339 		return ERR_PTR(err);
1340 	}
1341 	if (!ipage)
1342 		f2fs_put_dnode(&dn);
1343 
1344 	if (PageUptodate(page))
1345 		goto got_it;
1346 
1347 	if (dn.data_blkaddr == NEW_ADDR) {
1348 		zero_user_segment(page, 0, PAGE_SIZE);
1349 		if (!PageUptodate(page))
1350 			SetPageUptodate(page);
1351 	} else {
1352 		f2fs_put_page(page, 1);
1353 
1354 		/* if ipage exists, blkaddr should be NEW_ADDR */
1355 		f2fs_bug_on(F2FS_I_SB(inode), ipage);
1356 		page = f2fs_get_lock_data_page(inode, index, true);
1357 		if (IS_ERR(page))
1358 			return page;
1359 	}
1360 got_it:
1361 	if (new_i_size && i_size_read(inode) <
1362 				((loff_t)(index + 1) << PAGE_SHIFT))
1363 		f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1364 	return page;
1365 }
1366 
__allocate_data_block(struct dnode_of_data * dn,int seg_type)1367 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1368 {
1369 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1370 	struct f2fs_summary sum;
1371 	struct node_info ni;
1372 	block_t old_blkaddr;
1373 	blkcnt_t count = 1;
1374 	int err;
1375 
1376 	if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1377 		return -EPERM;
1378 
1379 	err = f2fs_get_node_info(sbi, dn->nid, &ni, false);
1380 	if (err)
1381 		return err;
1382 
1383 	dn->data_blkaddr = f2fs_data_blkaddr(dn);
1384 	if (dn->data_blkaddr != NULL_ADDR)
1385 		goto alloc;
1386 
1387 	if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1388 		return err;
1389 
1390 alloc:
1391 	set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1392 	old_blkaddr = dn->data_blkaddr;
1393 	f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1394 				&sum, seg_type, NULL);
1395 	if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
1396 		invalidate_mapping_pages(META_MAPPING(sbi),
1397 					old_blkaddr, old_blkaddr);
1398 		f2fs_invalidate_compress_page(sbi, old_blkaddr);
1399 	}
1400 	f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1401 	return 0;
1402 }
1403 
f2fs_do_map_lock(struct f2fs_sb_info * sbi,int flag,bool lock)1404 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1405 {
1406 	if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1407 		if (lock)
1408 			f2fs_down_read(&sbi->node_change);
1409 		else
1410 			f2fs_up_read(&sbi->node_change);
1411 	} else {
1412 		if (lock)
1413 			f2fs_lock_op(sbi);
1414 		else
1415 			f2fs_unlock_op(sbi);
1416 	}
1417 }
1418 
1419 /*
1420  * f2fs_map_blocks() tries to find or build mapping relationship which
1421  * maps continuous logical blocks to physical blocks, and return such
1422  * info via f2fs_map_blocks structure.
1423  */
f2fs_map_blocks(struct inode * inode,struct f2fs_map_blocks * map,int create,int flag)1424 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1425 						int create, int flag)
1426 {
1427 	unsigned int maxblocks = map->m_len;
1428 	struct dnode_of_data dn;
1429 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1430 	int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1431 	pgoff_t pgofs, end_offset, end;
1432 	int err = 0, ofs = 1;
1433 	unsigned int ofs_in_node, last_ofs_in_node;
1434 	blkcnt_t prealloc;
1435 	struct extent_info ei = {0, };
1436 	block_t blkaddr;
1437 	unsigned int start_pgofs;
1438 	int bidx = 0;
1439 
1440 	if (!maxblocks)
1441 		return 0;
1442 
1443 	map->m_bdev = inode->i_sb->s_bdev;
1444 	map->m_multidev_dio =
1445 		f2fs_allow_multi_device_dio(F2FS_I_SB(inode), flag);
1446 
1447 	map->m_len = 0;
1448 	map->m_flags = 0;
1449 
1450 	/* it only supports block size == page size */
1451 	pgofs =	(pgoff_t)map->m_lblk;
1452 	end = pgofs + maxblocks;
1453 
1454 	if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1455 		if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1456 							map->m_may_create)
1457 			goto next_dnode;
1458 
1459 		map->m_pblk = ei.blk + pgofs - ei.fofs;
1460 		map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1461 		map->m_flags = F2FS_MAP_MAPPED;
1462 		if (map->m_next_extent)
1463 			*map->m_next_extent = pgofs + map->m_len;
1464 
1465 		/* for hardware encryption, but to avoid potential issue in future */
1466 		if (flag == F2FS_GET_BLOCK_DIO) {
1467 			f2fs_wait_on_block_writeback_range(inode,
1468 						map->m_pblk, map->m_len);
1469 			invalidate_mapping_pages(META_MAPPING(sbi),
1470 				map->m_pblk, map->m_pblk + map->m_len - 1);
1471 		}
1472 
1473 		if (map->m_multidev_dio) {
1474 			block_t blk_addr = map->m_pblk;
1475 
1476 			bidx = f2fs_target_device_index(sbi, map->m_pblk);
1477 
1478 			map->m_bdev = FDEV(bidx).bdev;
1479 			map->m_pblk -= FDEV(bidx).start_blk;
1480 			map->m_len = min(map->m_len,
1481 				FDEV(bidx).end_blk + 1 - map->m_pblk);
1482 
1483 			if (map->m_may_create)
1484 				f2fs_update_device_state(sbi, inode->i_ino,
1485 							blk_addr, map->m_len);
1486 		}
1487 		goto out;
1488 	}
1489 
1490 next_dnode:
1491 	if (map->m_may_create)
1492 		f2fs_do_map_lock(sbi, flag, true);
1493 
1494 	/* When reading holes, we need its node page */
1495 	set_new_dnode(&dn, inode, NULL, NULL, 0);
1496 	err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1497 	if (err) {
1498 		if (flag == F2FS_GET_BLOCK_BMAP)
1499 			map->m_pblk = 0;
1500 
1501 		if (err == -ENOENT) {
1502 			/*
1503 			 * There is one exceptional case that read_node_page()
1504 			 * may return -ENOENT due to filesystem has been
1505 			 * shutdown or cp_error, so force to convert error
1506 			 * number to EIO for such case.
1507 			 */
1508 			if (map->m_may_create &&
1509 				(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1510 				f2fs_cp_error(sbi))) {
1511 				err = -EIO;
1512 				goto unlock_out;
1513 			}
1514 
1515 			err = 0;
1516 			if (map->m_next_pgofs)
1517 				*map->m_next_pgofs =
1518 					f2fs_get_next_page_offset(&dn, pgofs);
1519 			if (map->m_next_extent)
1520 				*map->m_next_extent =
1521 					f2fs_get_next_page_offset(&dn, pgofs);
1522 		}
1523 		goto unlock_out;
1524 	}
1525 
1526 	start_pgofs = pgofs;
1527 	prealloc = 0;
1528 	last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1529 	end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1530 
1531 next_block:
1532 	blkaddr = f2fs_data_blkaddr(&dn);
1533 
1534 	if (__is_valid_data_blkaddr(blkaddr) &&
1535 		!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1536 		err = -EFSCORRUPTED;
1537 		goto sync_out;
1538 	}
1539 
1540 	if (__is_valid_data_blkaddr(blkaddr)) {
1541 		/* use out-place-update for driect IO under LFS mode */
1542 		if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1543 							map->m_may_create) {
1544 			err = __allocate_data_block(&dn, map->m_seg_type);
1545 			if (err)
1546 				goto sync_out;
1547 			blkaddr = dn.data_blkaddr;
1548 			set_inode_flag(inode, FI_APPEND_WRITE);
1549 		}
1550 	} else {
1551 		if (create) {
1552 			if (unlikely(f2fs_cp_error(sbi))) {
1553 				err = -EIO;
1554 				goto sync_out;
1555 			}
1556 			if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1557 				if (blkaddr == NULL_ADDR) {
1558 					prealloc++;
1559 					last_ofs_in_node = dn.ofs_in_node;
1560 				}
1561 			} else {
1562 				WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1563 					flag != F2FS_GET_BLOCK_DIO);
1564 				err = __allocate_data_block(&dn,
1565 							map->m_seg_type);
1566 				if (!err) {
1567 					if (flag == F2FS_GET_BLOCK_PRE_DIO)
1568 						file_need_truncate(inode);
1569 					set_inode_flag(inode, FI_APPEND_WRITE);
1570 				}
1571 			}
1572 			if (err)
1573 				goto sync_out;
1574 			map->m_flags |= F2FS_MAP_NEW;
1575 			blkaddr = dn.data_blkaddr;
1576 		} else {
1577 			if (f2fs_compressed_file(inode) &&
1578 					f2fs_sanity_check_cluster(&dn) &&
1579 					(flag != F2FS_GET_BLOCK_FIEMAP ||
1580 					IS_ENABLED(CONFIG_F2FS_CHECK_FS))) {
1581 				err = -EFSCORRUPTED;
1582 				goto sync_out;
1583 			}
1584 			if (flag == F2FS_GET_BLOCK_BMAP) {
1585 				map->m_pblk = 0;
1586 				goto sync_out;
1587 			}
1588 			if (flag == F2FS_GET_BLOCK_PRECACHE)
1589 				goto sync_out;
1590 			if (flag == F2FS_GET_BLOCK_FIEMAP &&
1591 						blkaddr == NULL_ADDR) {
1592 				if (map->m_next_pgofs)
1593 					*map->m_next_pgofs = pgofs + 1;
1594 				goto sync_out;
1595 			}
1596 			if (flag != F2FS_GET_BLOCK_FIEMAP) {
1597 				/* for defragment case */
1598 				if (map->m_next_pgofs)
1599 					*map->m_next_pgofs = pgofs + 1;
1600 				goto sync_out;
1601 			}
1602 		}
1603 	}
1604 
1605 	if (flag == F2FS_GET_BLOCK_PRE_AIO)
1606 		goto skip;
1607 
1608 	if (map->m_multidev_dio)
1609 		bidx = f2fs_target_device_index(sbi, blkaddr);
1610 
1611 	if (map->m_len == 0) {
1612 		/* preallocated unwritten block should be mapped for fiemap. */
1613 		if (blkaddr == NEW_ADDR)
1614 			map->m_flags |= F2FS_MAP_UNWRITTEN;
1615 		map->m_flags |= F2FS_MAP_MAPPED;
1616 
1617 		map->m_pblk = blkaddr;
1618 		map->m_len = 1;
1619 
1620 		if (map->m_multidev_dio)
1621 			map->m_bdev = FDEV(bidx).bdev;
1622 	} else if ((map->m_pblk != NEW_ADDR &&
1623 			blkaddr == (map->m_pblk + ofs)) ||
1624 			(map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1625 			flag == F2FS_GET_BLOCK_PRE_DIO) {
1626 		if (map->m_multidev_dio && map->m_bdev != FDEV(bidx).bdev)
1627 			goto sync_out;
1628 		ofs++;
1629 		map->m_len++;
1630 	} else {
1631 		goto sync_out;
1632 	}
1633 
1634 skip:
1635 	dn.ofs_in_node++;
1636 	pgofs++;
1637 
1638 	/* preallocate blocks in batch for one dnode page */
1639 	if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1640 			(pgofs == end || dn.ofs_in_node == end_offset)) {
1641 
1642 		dn.ofs_in_node = ofs_in_node;
1643 		err = f2fs_reserve_new_blocks(&dn, prealloc);
1644 		if (err)
1645 			goto sync_out;
1646 
1647 		map->m_len += dn.ofs_in_node - ofs_in_node;
1648 		if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1649 			err = -ENOSPC;
1650 			goto sync_out;
1651 		}
1652 		dn.ofs_in_node = end_offset;
1653 	}
1654 
1655 	if (pgofs >= end)
1656 		goto sync_out;
1657 	else if (dn.ofs_in_node < end_offset)
1658 		goto next_block;
1659 
1660 	if (flag == F2FS_GET_BLOCK_PRECACHE) {
1661 		if (map->m_flags & F2FS_MAP_MAPPED) {
1662 			unsigned int ofs = start_pgofs - map->m_lblk;
1663 
1664 			f2fs_update_extent_cache_range(&dn,
1665 				start_pgofs, map->m_pblk + ofs,
1666 				map->m_len - ofs);
1667 		}
1668 	}
1669 
1670 	f2fs_put_dnode(&dn);
1671 
1672 	if (map->m_may_create) {
1673 		f2fs_do_map_lock(sbi, flag, false);
1674 		f2fs_balance_fs(sbi, dn.node_changed);
1675 	}
1676 	goto next_dnode;
1677 
1678 sync_out:
1679 
1680 	if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) {
1681 		/*
1682 		 * for hardware encryption, but to avoid potential issue
1683 		 * in future
1684 		 */
1685 		f2fs_wait_on_block_writeback_range(inode,
1686 						map->m_pblk, map->m_len);
1687 		invalidate_mapping_pages(META_MAPPING(sbi),
1688 				map->m_pblk, map->m_pblk + map->m_len - 1);
1689 
1690 		if (map->m_multidev_dio) {
1691 			block_t blk_addr = map->m_pblk;
1692 
1693 			bidx = f2fs_target_device_index(sbi, map->m_pblk);
1694 
1695 			map->m_bdev = FDEV(bidx).bdev;
1696 			map->m_pblk -= FDEV(bidx).start_blk;
1697 
1698 			if (map->m_may_create)
1699 				f2fs_update_device_state(sbi, inode->i_ino,
1700 							blk_addr, map->m_len);
1701 
1702 			f2fs_bug_on(sbi, blk_addr + map->m_len >
1703 						FDEV(bidx).end_blk + 1);
1704 		}
1705 	}
1706 
1707 	if (flag == F2FS_GET_BLOCK_PRECACHE) {
1708 		if (map->m_flags & F2FS_MAP_MAPPED) {
1709 			unsigned int ofs = start_pgofs - map->m_lblk;
1710 
1711 			f2fs_update_extent_cache_range(&dn,
1712 				start_pgofs, map->m_pblk + ofs,
1713 				map->m_len - ofs);
1714 		}
1715 		if (map->m_next_extent)
1716 			*map->m_next_extent = pgofs + 1;
1717 	}
1718 	f2fs_put_dnode(&dn);
1719 unlock_out:
1720 	if (map->m_may_create) {
1721 		f2fs_do_map_lock(sbi, flag, false);
1722 		f2fs_balance_fs(sbi, dn.node_changed);
1723 	}
1724 out:
1725 	trace_f2fs_map_blocks(inode, map, create, flag, err);
1726 	return err;
1727 }
1728 
f2fs_overwrite_io(struct inode * inode,loff_t pos,size_t len)1729 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1730 {
1731 	struct f2fs_map_blocks map;
1732 	block_t last_lblk;
1733 	int err;
1734 
1735 	if (pos + len > i_size_read(inode))
1736 		return false;
1737 
1738 	map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1739 	map.m_next_pgofs = NULL;
1740 	map.m_next_extent = NULL;
1741 	map.m_seg_type = NO_CHECK_TYPE;
1742 	map.m_may_create = false;
1743 	last_lblk = F2FS_BLK_ALIGN(pos + len);
1744 
1745 	while (map.m_lblk < last_lblk) {
1746 		map.m_len = last_lblk - map.m_lblk;
1747 		err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1748 		if (err || map.m_len == 0)
1749 			return false;
1750 		map.m_lblk += map.m_len;
1751 	}
1752 	return true;
1753 }
1754 
bytes_to_blks(struct inode * inode,u64 bytes)1755 static inline u64 bytes_to_blks(struct inode *inode, u64 bytes)
1756 {
1757 	return (bytes >> inode->i_blkbits);
1758 }
1759 
blks_to_bytes(struct inode * inode,u64 blks)1760 static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
1761 {
1762 	return (blks << inode->i_blkbits);
1763 }
1764 
f2fs_xattr_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo)1765 static int f2fs_xattr_fiemap(struct inode *inode,
1766 				struct fiemap_extent_info *fieinfo)
1767 {
1768 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1769 	struct page *page;
1770 	struct node_info ni;
1771 	__u64 phys = 0, len;
1772 	__u32 flags;
1773 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1774 	int err = 0;
1775 
1776 	if (f2fs_has_inline_xattr(inode)) {
1777 		int offset;
1778 
1779 		page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1780 						inode->i_ino, false);
1781 		if (!page)
1782 			return -ENOMEM;
1783 
1784 		err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false);
1785 		if (err) {
1786 			f2fs_put_page(page, 1);
1787 			return err;
1788 		}
1789 
1790 		phys = blks_to_bytes(inode, ni.blk_addr);
1791 		offset = offsetof(struct f2fs_inode, i_addr) +
1792 					sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1793 					get_inline_xattr_addrs(inode));
1794 
1795 		phys += offset;
1796 		len = inline_xattr_size(inode);
1797 
1798 		f2fs_put_page(page, 1);
1799 
1800 		flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1801 
1802 		if (!xnid)
1803 			flags |= FIEMAP_EXTENT_LAST;
1804 
1805 		err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1806 		trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1807 		if (err || err == 1)
1808 			return err;
1809 	}
1810 
1811 	if (xnid) {
1812 		page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1813 		if (!page)
1814 			return -ENOMEM;
1815 
1816 		err = f2fs_get_node_info(sbi, xnid, &ni, false);
1817 		if (err) {
1818 			f2fs_put_page(page, 1);
1819 			return err;
1820 		}
1821 
1822 		phys = blks_to_bytes(inode, ni.blk_addr);
1823 		len = inode->i_sb->s_blocksize;
1824 
1825 		f2fs_put_page(page, 1);
1826 
1827 		flags = FIEMAP_EXTENT_LAST;
1828 	}
1829 
1830 	if (phys) {
1831 		err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1832 		trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1833 	}
1834 
1835 	return (err < 0 ? err : 0);
1836 }
1837 
max_inode_blocks(struct inode * inode)1838 static loff_t max_inode_blocks(struct inode *inode)
1839 {
1840 	loff_t result = ADDRS_PER_INODE(inode);
1841 	loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1842 
1843 	/* two direct node blocks */
1844 	result += (leaf_count * 2);
1845 
1846 	/* two indirect node blocks */
1847 	leaf_count *= NIDS_PER_BLOCK;
1848 	result += (leaf_count * 2);
1849 
1850 	/* one double indirect node block */
1851 	leaf_count *= NIDS_PER_BLOCK;
1852 	result += leaf_count;
1853 
1854 	return result;
1855 }
1856 
f2fs_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,u64 start,u64 len)1857 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1858 		u64 start, u64 len)
1859 {
1860 	struct f2fs_map_blocks map;
1861 	sector_t start_blk, last_blk;
1862 	pgoff_t next_pgofs;
1863 	u64 logical = 0, phys = 0, size = 0;
1864 	u32 flags = 0;
1865 	int ret = 0;
1866 	bool compr_cluster = false, compr_appended;
1867 	unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1868 	unsigned int count_in_cluster = 0;
1869 	loff_t maxbytes;
1870 
1871 	if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1872 		ret = f2fs_precache_extents(inode);
1873 		if (ret)
1874 			return ret;
1875 	}
1876 
1877 	ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1878 	if (ret)
1879 		return ret;
1880 
1881 	inode_lock(inode);
1882 
1883 	maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS;
1884 	if (start > maxbytes) {
1885 		ret = -EFBIG;
1886 		goto out;
1887 	}
1888 
1889 	if (len > maxbytes || (maxbytes - len) < start)
1890 		len = maxbytes - start;
1891 
1892 	if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1893 		ret = f2fs_xattr_fiemap(inode, fieinfo);
1894 		goto out;
1895 	}
1896 
1897 	if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1898 		ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1899 		if (ret != -EAGAIN)
1900 			goto out;
1901 	}
1902 
1903 	if (bytes_to_blks(inode, len) == 0)
1904 		len = blks_to_bytes(inode, 1);
1905 
1906 	start_blk = bytes_to_blks(inode, start);
1907 	last_blk = bytes_to_blks(inode, start + len - 1);
1908 
1909 next:
1910 	memset(&map, 0, sizeof(map));
1911 	map.m_lblk = start_blk;
1912 	map.m_len = bytes_to_blks(inode, len);
1913 	map.m_next_pgofs = &next_pgofs;
1914 	map.m_seg_type = NO_CHECK_TYPE;
1915 
1916 	if (compr_cluster) {
1917 		map.m_lblk += 1;
1918 		map.m_len = cluster_size - count_in_cluster;
1919 	}
1920 
1921 	ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
1922 	if (ret)
1923 		goto out;
1924 
1925 	/* HOLE */
1926 	if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) {
1927 		start_blk = next_pgofs;
1928 
1929 		if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode,
1930 						max_inode_blocks(inode)))
1931 			goto prep_next;
1932 
1933 		flags |= FIEMAP_EXTENT_LAST;
1934 	}
1935 
1936 	compr_appended = false;
1937 	/* In a case of compressed cluster, append this to the last extent */
1938 	if (compr_cluster && ((map.m_flags & F2FS_MAP_UNWRITTEN) ||
1939 			!(map.m_flags & F2FS_MAP_FLAGS))) {
1940 		compr_appended = true;
1941 		goto skip_fill;
1942 	}
1943 
1944 	if (size) {
1945 		flags |= FIEMAP_EXTENT_MERGED;
1946 		if (IS_ENCRYPTED(inode))
1947 			flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1948 
1949 		ret = fiemap_fill_next_extent(fieinfo, logical,
1950 				phys, size, flags);
1951 		trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1952 		if (ret)
1953 			goto out;
1954 		size = 0;
1955 	}
1956 
1957 	if (start_blk > last_blk)
1958 		goto out;
1959 
1960 skip_fill:
1961 	if (map.m_pblk == COMPRESS_ADDR) {
1962 		compr_cluster = true;
1963 		count_in_cluster = 1;
1964 	} else if (compr_appended) {
1965 		unsigned int appended_blks = cluster_size -
1966 						count_in_cluster + 1;
1967 		size += blks_to_bytes(inode, appended_blks);
1968 		start_blk += appended_blks;
1969 		compr_cluster = false;
1970 	} else {
1971 		logical = blks_to_bytes(inode, start_blk);
1972 		phys = __is_valid_data_blkaddr(map.m_pblk) ?
1973 			blks_to_bytes(inode, map.m_pblk) : 0;
1974 		size = blks_to_bytes(inode, map.m_len);
1975 		flags = 0;
1976 
1977 		if (compr_cluster) {
1978 			flags = FIEMAP_EXTENT_ENCODED;
1979 			count_in_cluster += map.m_len;
1980 			if (count_in_cluster == cluster_size) {
1981 				compr_cluster = false;
1982 				size += blks_to_bytes(inode, 1);
1983 			}
1984 		} else if (map.m_flags & F2FS_MAP_UNWRITTEN) {
1985 			flags = FIEMAP_EXTENT_UNWRITTEN;
1986 		}
1987 
1988 		start_blk += bytes_to_blks(inode, size);
1989 	}
1990 
1991 prep_next:
1992 	cond_resched();
1993 	if (fatal_signal_pending(current))
1994 		ret = -EINTR;
1995 	else
1996 		goto next;
1997 out:
1998 	if (ret == 1)
1999 		ret = 0;
2000 
2001 	inode_unlock(inode);
2002 	return ret;
2003 }
2004 
f2fs_readpage_limit(struct inode * inode)2005 static inline loff_t f2fs_readpage_limit(struct inode *inode)
2006 {
2007 	if (IS_ENABLED(CONFIG_FS_VERITY) &&
2008 	    (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
2009 		return inode->i_sb->s_maxbytes;
2010 
2011 	return i_size_read(inode);
2012 }
2013 
f2fs_read_single_page(struct inode * inode,struct page * page,unsigned nr_pages,struct f2fs_map_blocks * map,struct bio ** bio_ret,sector_t * last_block_in_bio,bool is_readahead)2014 static int f2fs_read_single_page(struct inode *inode, struct page *page,
2015 					unsigned nr_pages,
2016 					struct f2fs_map_blocks *map,
2017 					struct bio **bio_ret,
2018 					sector_t *last_block_in_bio,
2019 					bool is_readahead)
2020 {
2021 	struct bio *bio = *bio_ret;
2022 	const unsigned blocksize = blks_to_bytes(inode, 1);
2023 	sector_t block_in_file;
2024 	sector_t last_block;
2025 	sector_t last_block_in_file;
2026 	sector_t block_nr;
2027 	int ret = 0;
2028 
2029 	block_in_file = (sector_t)page_index(page);
2030 	last_block = block_in_file + nr_pages;
2031 	last_block_in_file = bytes_to_blks(inode,
2032 			f2fs_readpage_limit(inode) + blocksize - 1);
2033 	if (last_block > last_block_in_file)
2034 		last_block = last_block_in_file;
2035 
2036 	/* just zeroing out page which is beyond EOF */
2037 	if (block_in_file >= last_block)
2038 		goto zero_out;
2039 	/*
2040 	 * Map blocks using the previous result first.
2041 	 */
2042 	if ((map->m_flags & F2FS_MAP_MAPPED) &&
2043 			block_in_file > map->m_lblk &&
2044 			block_in_file < (map->m_lblk + map->m_len))
2045 		goto got_it;
2046 
2047 	/*
2048 	 * Then do more f2fs_map_blocks() calls until we are
2049 	 * done with this page.
2050 	 */
2051 	map->m_lblk = block_in_file;
2052 	map->m_len = last_block - block_in_file;
2053 
2054 	ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2055 	if (ret)
2056 		goto out;
2057 got_it:
2058 	if ((map->m_flags & F2FS_MAP_MAPPED)) {
2059 		block_nr = map->m_pblk + block_in_file - map->m_lblk;
2060 		SetPageMappedToDisk(page);
2061 
2062 		if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2063 						DATA_GENERIC_ENHANCE_READ)) {
2064 			ret = -EFSCORRUPTED;
2065 			goto out;
2066 		}
2067 	} else {
2068 zero_out:
2069 		zero_user_segment(page, 0, PAGE_SIZE);
2070 		if (f2fs_need_verity(inode, page->index) &&
2071 		    !fsverity_verify_page(page)) {
2072 			ret = -EIO;
2073 			goto out;
2074 		}
2075 		if (!PageUptodate(page))
2076 			SetPageUptodate(page);
2077 		unlock_page(page);
2078 		goto out;
2079 	}
2080 
2081 	/*
2082 	 * This page will go to BIO.  Do we need to send this
2083 	 * BIO off first?
2084 	 */
2085 	if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2086 				       *last_block_in_bio, block_nr) ||
2087 		    !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2088 submit_and_realloc:
2089 		__submit_bio(F2FS_I_SB(inode), bio, DATA);
2090 		bio = NULL;
2091 	}
2092 	if (bio == NULL) {
2093 		bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2094 				is_readahead ? REQ_RAHEAD : 0, page->index,
2095 				false);
2096 		if (IS_ERR(bio)) {
2097 			ret = PTR_ERR(bio);
2098 			bio = NULL;
2099 			goto out;
2100 		}
2101 	}
2102 
2103 	/*
2104 	 * If the page is under writeback, we need to wait for
2105 	 * its completion to see the correct decrypted data.
2106 	 */
2107 	f2fs_wait_on_block_writeback(inode, block_nr);
2108 
2109 	if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2110 		goto submit_and_realloc;
2111 
2112 	inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2113 	f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2114 	ClearPageError(page);
2115 	*last_block_in_bio = block_nr;
2116 	goto out;
2117 out:
2118 	*bio_ret = bio;
2119 	return ret;
2120 }
2121 
2122 #ifdef CONFIG_F2FS_FS_COMPRESSION
f2fs_read_multi_pages(struct compress_ctx * cc,struct bio ** bio_ret,unsigned nr_pages,sector_t * last_block_in_bio,bool is_readahead,bool for_write)2123 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2124 				unsigned nr_pages, sector_t *last_block_in_bio,
2125 				bool is_readahead, bool for_write)
2126 {
2127 	struct dnode_of_data dn;
2128 	struct inode *inode = cc->inode;
2129 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2130 	struct bio *bio = *bio_ret;
2131 	unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2132 	sector_t last_block_in_file;
2133 	const unsigned blocksize = blks_to_bytes(inode, 1);
2134 	struct decompress_io_ctx *dic = NULL;
2135 	struct extent_info ei = {0, };
2136 	bool from_dnode = true;
2137 	int i;
2138 	int ret = 0;
2139 
2140 	f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2141 
2142 	last_block_in_file = bytes_to_blks(inode,
2143 			f2fs_readpage_limit(inode) + blocksize - 1);
2144 
2145 	/* get rid of pages beyond EOF */
2146 	for (i = 0; i < cc->cluster_size; i++) {
2147 		struct page *page = cc->rpages[i];
2148 
2149 		if (!page)
2150 			continue;
2151 		if ((sector_t)page->index >= last_block_in_file) {
2152 			zero_user_segment(page, 0, PAGE_SIZE);
2153 			if (!PageUptodate(page))
2154 				SetPageUptodate(page);
2155 		} else if (!PageUptodate(page)) {
2156 			continue;
2157 		}
2158 		unlock_page(page);
2159 		if (for_write)
2160 			put_page(page);
2161 		cc->rpages[i] = NULL;
2162 		cc->nr_rpages--;
2163 	}
2164 
2165 	/* we are done since all pages are beyond EOF */
2166 	if (f2fs_cluster_is_empty(cc))
2167 		goto out;
2168 
2169 	if (f2fs_lookup_extent_cache(inode, start_idx, &ei))
2170 		from_dnode = false;
2171 
2172 	if (!from_dnode)
2173 		goto skip_reading_dnode;
2174 
2175 	set_new_dnode(&dn, inode, NULL, NULL, 0);
2176 	ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2177 	if (ret)
2178 		goto out;
2179 
2180 	f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2181 
2182 skip_reading_dnode:
2183 	for (i = 1; i < cc->cluster_size; i++) {
2184 		block_t blkaddr;
2185 
2186 		blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2187 					dn.ofs_in_node + i) :
2188 					ei.blk + i - 1;
2189 
2190 		if (!__is_valid_data_blkaddr(blkaddr))
2191 			break;
2192 
2193 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2194 			ret = -EFAULT;
2195 			goto out_put_dnode;
2196 		}
2197 		cc->nr_cpages++;
2198 
2199 		if (!from_dnode && i >= ei.c_len)
2200 			break;
2201 	}
2202 
2203 	/* nothing to decompress */
2204 	if (cc->nr_cpages == 0) {
2205 		ret = 0;
2206 		goto out_put_dnode;
2207 	}
2208 
2209 	dic = f2fs_alloc_dic(cc);
2210 	if (IS_ERR(dic)) {
2211 		ret = PTR_ERR(dic);
2212 		goto out_put_dnode;
2213 	}
2214 
2215 	for (i = 0; i < cc->nr_cpages; i++) {
2216 		struct page *page = dic->cpages[i];
2217 		block_t blkaddr;
2218 		struct bio_post_read_ctx *ctx;
2219 
2220 		blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page,
2221 					dn.ofs_in_node + i + 1) :
2222 					ei.blk + i;
2223 
2224 		f2fs_wait_on_block_writeback(inode, blkaddr);
2225 
2226 		if (f2fs_load_compressed_page(sbi, page, blkaddr)) {
2227 			if (atomic_dec_and_test(&dic->remaining_pages))
2228 				f2fs_decompress_cluster(dic);
2229 			continue;
2230 		}
2231 
2232 		if (bio && (!page_is_mergeable(sbi, bio,
2233 					*last_block_in_bio, blkaddr) ||
2234 		    !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2235 submit_and_realloc:
2236 			__submit_bio(sbi, bio, DATA);
2237 			bio = NULL;
2238 		}
2239 
2240 		if (!bio) {
2241 			bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2242 					is_readahead ? REQ_RAHEAD : 0,
2243 					page->index, for_write);
2244 			if (IS_ERR(bio)) {
2245 				ret = PTR_ERR(bio);
2246 				f2fs_decompress_end_io(dic, ret);
2247 				f2fs_put_dnode(&dn);
2248 				*bio_ret = NULL;
2249 				return ret;
2250 			}
2251 		}
2252 
2253 		if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2254 			goto submit_and_realloc;
2255 
2256 		ctx = get_post_read_ctx(bio);
2257 		ctx->enabled_steps |= STEP_DECOMPRESS;
2258 		refcount_inc(&dic->refcnt);
2259 
2260 		inc_page_count(sbi, F2FS_RD_DATA);
2261 		f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2262 		f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2263 		ClearPageError(page);
2264 		*last_block_in_bio = blkaddr;
2265 	}
2266 
2267 	if (from_dnode)
2268 		f2fs_put_dnode(&dn);
2269 
2270 	*bio_ret = bio;
2271 	return 0;
2272 
2273 out_put_dnode:
2274 	if (from_dnode)
2275 		f2fs_put_dnode(&dn);
2276 out:
2277 	for (i = 0; i < cc->cluster_size; i++) {
2278 		if (cc->rpages[i]) {
2279 			ClearPageUptodate(cc->rpages[i]);
2280 			ClearPageError(cc->rpages[i]);
2281 			unlock_page(cc->rpages[i]);
2282 		}
2283 	}
2284 	*bio_ret = bio;
2285 	return ret;
2286 }
2287 #endif
2288 
2289 /*
2290  * This function was originally taken from fs/mpage.c, and customized for f2fs.
2291  * Major change was from block_size == page_size in f2fs by default.
2292  */
f2fs_mpage_readpages(struct inode * inode,struct readahead_control * rac,struct page * page)2293 static int f2fs_mpage_readpages(struct inode *inode,
2294 		struct readahead_control *rac, struct page *page)
2295 {
2296 	struct bio *bio = NULL;
2297 	sector_t last_block_in_bio = 0;
2298 	struct f2fs_map_blocks map;
2299 #ifdef CONFIG_F2FS_FS_COMPRESSION
2300 	struct compress_ctx cc = {
2301 		.inode = inode,
2302 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2303 		.cluster_size = F2FS_I(inode)->i_cluster_size,
2304 		.cluster_idx = NULL_CLUSTER,
2305 		.rpages = NULL,
2306 		.cpages = NULL,
2307 		.nr_rpages = 0,
2308 		.nr_cpages = 0,
2309 	};
2310 	pgoff_t nc_cluster_idx = NULL_CLUSTER;
2311 #endif
2312 	unsigned nr_pages = rac ? readahead_count(rac) : 1;
2313 	unsigned max_nr_pages = nr_pages;
2314 	int ret = 0;
2315 
2316 	map.m_pblk = 0;
2317 	map.m_lblk = 0;
2318 	map.m_len = 0;
2319 	map.m_flags = 0;
2320 	map.m_next_pgofs = NULL;
2321 	map.m_next_extent = NULL;
2322 	map.m_seg_type = NO_CHECK_TYPE;
2323 	map.m_may_create = false;
2324 
2325 	for (; nr_pages; nr_pages--) {
2326 		if (rac) {
2327 			page = readahead_page(rac);
2328 			prefetchw(&page->flags);
2329 		}
2330 
2331 #ifdef CONFIG_F2FS_FS_COMPRESSION
2332 		if (f2fs_compressed_file(inode)) {
2333 			/* there are remained comressed pages, submit them */
2334 			if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2335 				ret = f2fs_read_multi_pages(&cc, &bio,
2336 							max_nr_pages,
2337 							&last_block_in_bio,
2338 							rac != NULL, false);
2339 				f2fs_destroy_compress_ctx(&cc, false);
2340 				if (ret)
2341 					goto set_error_page;
2342 			}
2343 			if (cc.cluster_idx == NULL_CLUSTER) {
2344 				if (nc_cluster_idx ==
2345 					page->index >> cc.log_cluster_size) {
2346 					goto read_single_page;
2347 				}
2348 
2349 				ret = f2fs_is_compressed_cluster(inode, page->index);
2350 				if (ret < 0)
2351 					goto set_error_page;
2352 				else if (!ret) {
2353 					nc_cluster_idx =
2354 						page->index >> cc.log_cluster_size;
2355 					goto read_single_page;
2356 				}
2357 
2358 				nc_cluster_idx = NULL_CLUSTER;
2359 			}
2360 			ret = f2fs_init_compress_ctx(&cc);
2361 			if (ret)
2362 				goto set_error_page;
2363 
2364 			f2fs_compress_ctx_add_page(&cc, page);
2365 
2366 			goto next_page;
2367 		}
2368 read_single_page:
2369 #endif
2370 
2371 		ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2372 					&bio, &last_block_in_bio, rac);
2373 		if (ret) {
2374 #ifdef CONFIG_F2FS_FS_COMPRESSION
2375 set_error_page:
2376 #endif
2377 			SetPageError(page);
2378 			zero_user_segment(page, 0, PAGE_SIZE);
2379 			unlock_page(page);
2380 		}
2381 #ifdef CONFIG_F2FS_FS_COMPRESSION
2382 next_page:
2383 #endif
2384 		if (rac)
2385 			put_page(page);
2386 
2387 #ifdef CONFIG_F2FS_FS_COMPRESSION
2388 		if (f2fs_compressed_file(inode)) {
2389 			/* last page */
2390 			if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2391 				ret = f2fs_read_multi_pages(&cc, &bio,
2392 							max_nr_pages,
2393 							&last_block_in_bio,
2394 							rac != NULL, false);
2395 				f2fs_destroy_compress_ctx(&cc, false);
2396 			}
2397 		}
2398 #endif
2399 	}
2400 	if (bio)
2401 		__submit_bio(F2FS_I_SB(inode), bio, DATA);
2402 	return ret;
2403 }
2404 
f2fs_read_data_folio(struct file * file,struct folio * folio)2405 static int f2fs_read_data_folio(struct file *file, struct folio *folio)
2406 {
2407 	struct page *page = &folio->page;
2408 	struct inode *inode = page_file_mapping(page)->host;
2409 	int ret = -EAGAIN;
2410 
2411 	trace_f2fs_readpage(page, DATA);
2412 
2413 	if (!f2fs_is_compress_backend_ready(inode)) {
2414 		unlock_page(page);
2415 		return -EOPNOTSUPP;
2416 	}
2417 
2418 	/* If the file has inline data, try to read it directly */
2419 	if (f2fs_has_inline_data(inode))
2420 		ret = f2fs_read_inline_data(inode, page);
2421 	if (ret == -EAGAIN)
2422 		ret = f2fs_mpage_readpages(inode, NULL, page);
2423 	return ret;
2424 }
2425 
f2fs_readahead(struct readahead_control * rac)2426 static void f2fs_readahead(struct readahead_control *rac)
2427 {
2428 	struct inode *inode = rac->mapping->host;
2429 
2430 	trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2431 
2432 	if (!f2fs_is_compress_backend_ready(inode))
2433 		return;
2434 
2435 	/* If the file has inline data, skip readahead */
2436 	if (f2fs_has_inline_data(inode))
2437 		return;
2438 
2439 	f2fs_mpage_readpages(inode, rac, NULL);
2440 }
2441 
f2fs_encrypt_one_page(struct f2fs_io_info * fio)2442 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2443 {
2444 	struct inode *inode = fio->page->mapping->host;
2445 	struct page *mpage, *page;
2446 	gfp_t gfp_flags = GFP_NOFS;
2447 
2448 	if (!f2fs_encrypted_file(inode))
2449 		return 0;
2450 
2451 	page = fio->compressed_page ? fio->compressed_page : fio->page;
2452 
2453 	/* wait for GCed page writeback via META_MAPPING */
2454 	f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2455 
2456 	if (fscrypt_inode_uses_inline_crypto(inode))
2457 		return 0;
2458 
2459 retry_encrypt:
2460 	fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2461 					PAGE_SIZE, 0, gfp_flags);
2462 	if (IS_ERR(fio->encrypted_page)) {
2463 		/* flush pending IOs and wait for a while in the ENOMEM case */
2464 		if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2465 			f2fs_flush_merged_writes(fio->sbi);
2466 			memalloc_retry_wait(GFP_NOFS);
2467 			gfp_flags |= __GFP_NOFAIL;
2468 			goto retry_encrypt;
2469 		}
2470 		return PTR_ERR(fio->encrypted_page);
2471 	}
2472 
2473 	mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2474 	if (mpage) {
2475 		if (PageUptodate(mpage))
2476 			memcpy(page_address(mpage),
2477 				page_address(fio->encrypted_page), PAGE_SIZE);
2478 		f2fs_put_page(mpage, 1);
2479 	}
2480 	return 0;
2481 }
2482 
check_inplace_update_policy(struct inode * inode,struct f2fs_io_info * fio)2483 static inline bool check_inplace_update_policy(struct inode *inode,
2484 				struct f2fs_io_info *fio)
2485 {
2486 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2487 	unsigned int policy = SM_I(sbi)->ipu_policy;
2488 
2489 	if (policy & (0x1 << F2FS_IPU_HONOR_OPU_WRITE) &&
2490 			is_inode_flag_set(inode, FI_OPU_WRITE))
2491 		return false;
2492 	if (policy & (0x1 << F2FS_IPU_FORCE))
2493 		return true;
2494 	if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2495 		return true;
2496 	if (policy & (0x1 << F2FS_IPU_UTIL) &&
2497 			utilization(sbi) > SM_I(sbi)->min_ipu_util)
2498 		return true;
2499 	if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2500 			utilization(sbi) > SM_I(sbi)->min_ipu_util)
2501 		return true;
2502 
2503 	/*
2504 	 * IPU for rewrite async pages
2505 	 */
2506 	if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2507 			fio && fio->op == REQ_OP_WRITE &&
2508 			!(fio->op_flags & REQ_SYNC) &&
2509 			!IS_ENCRYPTED(inode))
2510 		return true;
2511 
2512 	/* this is only set during fdatasync */
2513 	if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2514 			is_inode_flag_set(inode, FI_NEED_IPU))
2515 		return true;
2516 
2517 	if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2518 			!f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2519 		return true;
2520 
2521 	return false;
2522 }
2523 
f2fs_should_update_inplace(struct inode * inode,struct f2fs_io_info * fio)2524 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2525 {
2526 	/* swap file is migrating in aligned write mode */
2527 	if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2528 		return false;
2529 
2530 	if (f2fs_is_pinned_file(inode))
2531 		return true;
2532 
2533 	/* if this is cold file, we should overwrite to avoid fragmentation */
2534 	if (file_is_cold(inode))
2535 		return true;
2536 
2537 	return check_inplace_update_policy(inode, fio);
2538 }
2539 
f2fs_should_update_outplace(struct inode * inode,struct f2fs_io_info * fio)2540 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2541 {
2542 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2543 
2544 	/* The below cases were checked when setting it. */
2545 	if (f2fs_is_pinned_file(inode))
2546 		return false;
2547 	if (fio && is_sbi_flag_set(sbi, SBI_NEED_FSCK))
2548 		return true;
2549 	if (f2fs_lfs_mode(sbi))
2550 		return true;
2551 	if (S_ISDIR(inode->i_mode))
2552 		return true;
2553 	if (IS_NOQUOTA(inode))
2554 		return true;
2555 	if (f2fs_is_atomic_file(inode))
2556 		return true;
2557 
2558 	/* swap file is migrating in aligned write mode */
2559 	if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
2560 		return true;
2561 
2562 	if (is_inode_flag_set(inode, FI_OPU_WRITE))
2563 		return true;
2564 
2565 	if (fio) {
2566 		if (page_private_gcing(fio->page))
2567 			return true;
2568 		if (page_private_dummy(fio->page))
2569 			return true;
2570 		if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2571 			f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2572 			return true;
2573 	}
2574 	return false;
2575 }
2576 
need_inplace_update(struct f2fs_io_info * fio)2577 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2578 {
2579 	struct inode *inode = fio->page->mapping->host;
2580 
2581 	if (f2fs_should_update_outplace(inode, fio))
2582 		return false;
2583 
2584 	return f2fs_should_update_inplace(inode, fio);
2585 }
2586 
f2fs_do_write_data_page(struct f2fs_io_info * fio)2587 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2588 {
2589 	struct page *page = fio->page;
2590 	struct inode *inode = page->mapping->host;
2591 	struct dnode_of_data dn;
2592 	struct extent_info ei = {0, };
2593 	struct node_info ni;
2594 	bool ipu_force = false;
2595 	int err = 0;
2596 
2597 	/* Use COW inode to make dnode_of_data for atomic write */
2598 	if (f2fs_is_atomic_file(inode))
2599 		set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0);
2600 	else
2601 		set_new_dnode(&dn, inode, NULL, NULL, 0);
2602 
2603 	if (need_inplace_update(fio) &&
2604 			f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2605 		fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2606 
2607 		if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2608 						DATA_GENERIC_ENHANCE))
2609 			return -EFSCORRUPTED;
2610 
2611 		ipu_force = true;
2612 		fio->need_lock = LOCK_DONE;
2613 		goto got_it;
2614 	}
2615 
2616 	/* Deadlock due to between page->lock and f2fs_lock_op */
2617 	if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2618 		return -EAGAIN;
2619 
2620 	err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2621 	if (err)
2622 		goto out;
2623 
2624 	fio->old_blkaddr = dn.data_blkaddr;
2625 
2626 	/* This page is already truncated */
2627 	if (fio->old_blkaddr == NULL_ADDR) {
2628 		ClearPageUptodate(page);
2629 		clear_page_private_gcing(page);
2630 		goto out_writepage;
2631 	}
2632 got_it:
2633 	if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2634 		!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2635 						DATA_GENERIC_ENHANCE)) {
2636 		err = -EFSCORRUPTED;
2637 		goto out_writepage;
2638 	}
2639 
2640 	/*
2641 	 * If current allocation needs SSR,
2642 	 * it had better in-place writes for updated data.
2643 	 */
2644 	if (ipu_force ||
2645 		(__is_valid_data_blkaddr(fio->old_blkaddr) &&
2646 					need_inplace_update(fio))) {
2647 		err = f2fs_encrypt_one_page(fio);
2648 		if (err)
2649 			goto out_writepage;
2650 
2651 		set_page_writeback(page);
2652 		ClearPageError(page);
2653 		f2fs_put_dnode(&dn);
2654 		if (fio->need_lock == LOCK_REQ)
2655 			f2fs_unlock_op(fio->sbi);
2656 		err = f2fs_inplace_write_data(fio);
2657 		if (err) {
2658 			if (fscrypt_inode_uses_fs_layer_crypto(inode))
2659 				fscrypt_finalize_bounce_page(&fio->encrypted_page);
2660 			if (PageWriteback(page))
2661 				end_page_writeback(page);
2662 		} else {
2663 			set_inode_flag(inode, FI_UPDATE_WRITE);
2664 		}
2665 		trace_f2fs_do_write_data_page(fio->page, IPU);
2666 		return err;
2667 	}
2668 
2669 	if (fio->need_lock == LOCK_RETRY) {
2670 		if (!f2fs_trylock_op(fio->sbi)) {
2671 			err = -EAGAIN;
2672 			goto out_writepage;
2673 		}
2674 		fio->need_lock = LOCK_REQ;
2675 	}
2676 
2677 	err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false);
2678 	if (err)
2679 		goto out_writepage;
2680 
2681 	fio->version = ni.version;
2682 
2683 	err = f2fs_encrypt_one_page(fio);
2684 	if (err)
2685 		goto out_writepage;
2686 
2687 	set_page_writeback(page);
2688 	ClearPageError(page);
2689 
2690 	if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2691 		f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2692 
2693 	/* LFS mode write path */
2694 	f2fs_outplace_write_data(&dn, fio);
2695 	trace_f2fs_do_write_data_page(page, OPU);
2696 	set_inode_flag(inode, FI_APPEND_WRITE);
2697 	if (page->index == 0)
2698 		set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2699 out_writepage:
2700 	f2fs_put_dnode(&dn);
2701 out:
2702 	if (fio->need_lock == LOCK_REQ)
2703 		f2fs_unlock_op(fio->sbi);
2704 	return err;
2705 }
2706 
f2fs_write_single_data_page(struct page * page,int * submitted,struct bio ** bio,sector_t * last_block,struct writeback_control * wbc,enum iostat_type io_type,int compr_blocks,bool allow_balance)2707 int f2fs_write_single_data_page(struct page *page, int *submitted,
2708 				struct bio **bio,
2709 				sector_t *last_block,
2710 				struct writeback_control *wbc,
2711 				enum iostat_type io_type,
2712 				int compr_blocks,
2713 				bool allow_balance)
2714 {
2715 	struct inode *inode = page->mapping->host;
2716 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2717 	loff_t i_size = i_size_read(inode);
2718 	const pgoff_t end_index = ((unsigned long long)i_size)
2719 							>> PAGE_SHIFT;
2720 	loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2721 	unsigned offset = 0;
2722 	bool need_balance_fs = false;
2723 	int err = 0;
2724 	struct f2fs_io_info fio = {
2725 		.sbi = sbi,
2726 		.ino = inode->i_ino,
2727 		.type = DATA,
2728 		.op = REQ_OP_WRITE,
2729 		.op_flags = wbc_to_write_flags(wbc),
2730 		.old_blkaddr = NULL_ADDR,
2731 		.page = page,
2732 		.encrypted_page = NULL,
2733 		.submitted = false,
2734 		.compr_blocks = compr_blocks,
2735 		.need_lock = LOCK_RETRY,
2736 		.io_type = io_type,
2737 		.io_wbc = wbc,
2738 		.bio = bio,
2739 		.last_block = last_block,
2740 	};
2741 
2742 	trace_f2fs_writepage(page, DATA);
2743 
2744 	/* we should bypass data pages to proceed the kworkder jobs */
2745 	if (unlikely(f2fs_cp_error(sbi))) {
2746 		mapping_set_error(page->mapping, -EIO);
2747 		/*
2748 		 * don't drop any dirty dentry pages for keeping lastest
2749 		 * directory structure.
2750 		 */
2751 		if (S_ISDIR(inode->i_mode))
2752 			goto redirty_out;
2753 		goto out;
2754 	}
2755 
2756 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2757 		goto redirty_out;
2758 
2759 	if (page->index < end_index ||
2760 			f2fs_verity_in_progress(inode) ||
2761 			compr_blocks)
2762 		goto write;
2763 
2764 	/*
2765 	 * If the offset is out-of-range of file size,
2766 	 * this page does not have to be written to disk.
2767 	 */
2768 	offset = i_size & (PAGE_SIZE - 1);
2769 	if ((page->index >= end_index + 1) || !offset)
2770 		goto out;
2771 
2772 	zero_user_segment(page, offset, PAGE_SIZE);
2773 write:
2774 	if (f2fs_is_drop_cache(inode))
2775 		goto out;
2776 
2777 	/* Dentry/quota blocks are controlled by checkpoint */
2778 	if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2779 		/*
2780 		 * We need to wait for node_write to avoid block allocation during
2781 		 * checkpoint. This can only happen to quota writes which can cause
2782 		 * the below discard race condition.
2783 		 */
2784 		if (IS_NOQUOTA(inode))
2785 			f2fs_down_read(&sbi->node_write);
2786 
2787 		fio.need_lock = LOCK_DONE;
2788 		err = f2fs_do_write_data_page(&fio);
2789 
2790 		if (IS_NOQUOTA(inode))
2791 			f2fs_up_read(&sbi->node_write);
2792 
2793 		goto done;
2794 	}
2795 
2796 	if (!wbc->for_reclaim)
2797 		need_balance_fs = true;
2798 	else if (has_not_enough_free_secs(sbi, 0, 0))
2799 		goto redirty_out;
2800 	else
2801 		set_inode_flag(inode, FI_HOT_DATA);
2802 
2803 	err = -EAGAIN;
2804 	if (f2fs_has_inline_data(inode)) {
2805 		err = f2fs_write_inline_data(inode, page);
2806 		if (!err)
2807 			goto out;
2808 	}
2809 
2810 	if (err == -EAGAIN) {
2811 		err = f2fs_do_write_data_page(&fio);
2812 		if (err == -EAGAIN) {
2813 			fio.need_lock = LOCK_REQ;
2814 			err = f2fs_do_write_data_page(&fio);
2815 		}
2816 	}
2817 
2818 	if (err) {
2819 		file_set_keep_isize(inode);
2820 	} else {
2821 		spin_lock(&F2FS_I(inode)->i_size_lock);
2822 		if (F2FS_I(inode)->last_disk_size < psize)
2823 			F2FS_I(inode)->last_disk_size = psize;
2824 		spin_unlock(&F2FS_I(inode)->i_size_lock);
2825 	}
2826 
2827 done:
2828 	if (err && err != -ENOENT)
2829 		goto redirty_out;
2830 
2831 out:
2832 	inode_dec_dirty_pages(inode);
2833 	if (err) {
2834 		ClearPageUptodate(page);
2835 		clear_page_private_gcing(page);
2836 	}
2837 
2838 	if (wbc->for_reclaim) {
2839 		f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2840 		clear_inode_flag(inode, FI_HOT_DATA);
2841 		f2fs_remove_dirty_inode(inode);
2842 		submitted = NULL;
2843 	}
2844 	unlock_page(page);
2845 	if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2846 			!F2FS_I(inode)->cp_task && allow_balance)
2847 		f2fs_balance_fs(sbi, need_balance_fs);
2848 
2849 	if (unlikely(f2fs_cp_error(sbi))) {
2850 		f2fs_submit_merged_write(sbi, DATA);
2851 		f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2852 		submitted = NULL;
2853 	}
2854 
2855 	if (submitted)
2856 		*submitted = fio.submitted ? 1 : 0;
2857 
2858 	return 0;
2859 
2860 redirty_out:
2861 	redirty_page_for_writepage(wbc, page);
2862 	/*
2863 	 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2864 	 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2865 	 * file_write_and_wait_range() will see EIO error, which is critical
2866 	 * to return value of fsync() followed by atomic_write failure to user.
2867 	 */
2868 	if (!err || wbc->for_reclaim)
2869 		return AOP_WRITEPAGE_ACTIVATE;
2870 	unlock_page(page);
2871 	return err;
2872 }
2873 
f2fs_write_data_page(struct page * page,struct writeback_control * wbc)2874 static int f2fs_write_data_page(struct page *page,
2875 					struct writeback_control *wbc)
2876 {
2877 #ifdef CONFIG_F2FS_FS_COMPRESSION
2878 	struct inode *inode = page->mapping->host;
2879 
2880 	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2881 		goto out;
2882 
2883 	if (f2fs_compressed_file(inode)) {
2884 		if (f2fs_is_compressed_cluster(inode, page->index)) {
2885 			redirty_page_for_writepage(wbc, page);
2886 			return AOP_WRITEPAGE_ACTIVATE;
2887 		}
2888 	}
2889 out:
2890 #endif
2891 
2892 	return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2893 						wbc, FS_DATA_IO, 0, true);
2894 }
2895 
2896 /*
2897  * This function was copied from write_cche_pages from mm/page-writeback.c.
2898  * The major change is making write step of cold data page separately from
2899  * warm/hot data page.
2900  */
f2fs_write_cache_pages(struct address_space * mapping,struct writeback_control * wbc,enum iostat_type io_type)2901 static int f2fs_write_cache_pages(struct address_space *mapping,
2902 					struct writeback_control *wbc,
2903 					enum iostat_type io_type)
2904 {
2905 	int ret = 0;
2906 	int done = 0, retry = 0;
2907 	struct pagevec pvec;
2908 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2909 	struct bio *bio = NULL;
2910 	sector_t last_block;
2911 #ifdef CONFIG_F2FS_FS_COMPRESSION
2912 	struct inode *inode = mapping->host;
2913 	struct compress_ctx cc = {
2914 		.inode = inode,
2915 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2916 		.cluster_size = F2FS_I(inode)->i_cluster_size,
2917 		.cluster_idx = NULL_CLUSTER,
2918 		.rpages = NULL,
2919 		.nr_rpages = 0,
2920 		.cpages = NULL,
2921 		.valid_nr_cpages = 0,
2922 		.rbuf = NULL,
2923 		.cbuf = NULL,
2924 		.rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2925 		.private = NULL,
2926 	};
2927 #endif
2928 	int nr_pages;
2929 	pgoff_t index;
2930 	pgoff_t end;		/* Inclusive */
2931 	pgoff_t done_index;
2932 	int range_whole = 0;
2933 	xa_mark_t tag;
2934 	int nwritten = 0;
2935 	int submitted = 0;
2936 	int i;
2937 
2938 	pagevec_init(&pvec);
2939 
2940 	if (get_dirty_pages(mapping->host) <=
2941 				SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2942 		set_inode_flag(mapping->host, FI_HOT_DATA);
2943 	else
2944 		clear_inode_flag(mapping->host, FI_HOT_DATA);
2945 
2946 	if (wbc->range_cyclic) {
2947 		index = mapping->writeback_index; /* prev offset */
2948 		end = -1;
2949 	} else {
2950 		index = wbc->range_start >> PAGE_SHIFT;
2951 		end = wbc->range_end >> PAGE_SHIFT;
2952 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2953 			range_whole = 1;
2954 	}
2955 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2956 		tag = PAGECACHE_TAG_TOWRITE;
2957 	else
2958 		tag = PAGECACHE_TAG_DIRTY;
2959 retry:
2960 	retry = 0;
2961 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2962 		tag_pages_for_writeback(mapping, index, end);
2963 	done_index = index;
2964 	while (!done && !retry && (index <= end)) {
2965 		nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2966 				tag);
2967 		if (nr_pages == 0)
2968 			break;
2969 
2970 		for (i = 0; i < nr_pages; i++) {
2971 			struct page *page = pvec.pages[i];
2972 			bool need_readd;
2973 readd:
2974 			need_readd = false;
2975 #ifdef CONFIG_F2FS_FS_COMPRESSION
2976 			if (f2fs_compressed_file(inode)) {
2977 				void *fsdata = NULL;
2978 				struct page *pagep;
2979 				int ret2;
2980 
2981 				ret = f2fs_init_compress_ctx(&cc);
2982 				if (ret) {
2983 					done = 1;
2984 					break;
2985 				}
2986 
2987 				if (!f2fs_cluster_can_merge_page(&cc,
2988 								page->index)) {
2989 					ret = f2fs_write_multi_pages(&cc,
2990 						&submitted, wbc, io_type);
2991 					if (!ret)
2992 						need_readd = true;
2993 					goto result;
2994 				}
2995 
2996 				if (unlikely(f2fs_cp_error(sbi)))
2997 					goto lock_page;
2998 
2999 				if (!f2fs_cluster_is_empty(&cc))
3000 					goto lock_page;
3001 
3002 				ret2 = f2fs_prepare_compress_overwrite(
3003 							inode, &pagep,
3004 							page->index, &fsdata);
3005 				if (ret2 < 0) {
3006 					ret = ret2;
3007 					done = 1;
3008 					break;
3009 				} else if (ret2 &&
3010 					(!f2fs_compress_write_end(inode,
3011 						fsdata, page->index, 1) ||
3012 					 !f2fs_all_cluster_page_loaded(&cc,
3013 						&pvec, i, nr_pages))) {
3014 					retry = 1;
3015 					break;
3016 				}
3017 			}
3018 #endif
3019 			/* give a priority to WB_SYNC threads */
3020 			if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3021 					wbc->sync_mode == WB_SYNC_NONE) {
3022 				done = 1;
3023 				break;
3024 			}
3025 #ifdef CONFIG_F2FS_FS_COMPRESSION
3026 lock_page:
3027 #endif
3028 			done_index = page->index;
3029 retry_write:
3030 			lock_page(page);
3031 
3032 			if (unlikely(page->mapping != mapping)) {
3033 continue_unlock:
3034 				unlock_page(page);
3035 				continue;
3036 			}
3037 
3038 			if (!PageDirty(page)) {
3039 				/* someone wrote it for us */
3040 				goto continue_unlock;
3041 			}
3042 
3043 			if (PageWriteback(page)) {
3044 				if (wbc->sync_mode != WB_SYNC_NONE)
3045 					f2fs_wait_on_page_writeback(page,
3046 							DATA, true, true);
3047 				else
3048 					goto continue_unlock;
3049 			}
3050 
3051 			if (!clear_page_dirty_for_io(page))
3052 				goto continue_unlock;
3053 
3054 #ifdef CONFIG_F2FS_FS_COMPRESSION
3055 			if (f2fs_compressed_file(inode)) {
3056 				get_page(page);
3057 				f2fs_compress_ctx_add_page(&cc, page);
3058 				continue;
3059 			}
3060 #endif
3061 			ret = f2fs_write_single_data_page(page, &submitted,
3062 					&bio, &last_block, wbc, io_type,
3063 					0, true);
3064 			if (ret == AOP_WRITEPAGE_ACTIVATE)
3065 				unlock_page(page);
3066 #ifdef CONFIG_F2FS_FS_COMPRESSION
3067 result:
3068 #endif
3069 			nwritten += submitted;
3070 			wbc->nr_to_write -= submitted;
3071 
3072 			if (unlikely(ret)) {
3073 				/*
3074 				 * keep nr_to_write, since vfs uses this to
3075 				 * get # of written pages.
3076 				 */
3077 				if (ret == AOP_WRITEPAGE_ACTIVATE) {
3078 					ret = 0;
3079 					goto next;
3080 				} else if (ret == -EAGAIN) {
3081 					ret = 0;
3082 					if (wbc->sync_mode == WB_SYNC_ALL) {
3083 						f2fs_io_schedule_timeout(
3084 							DEFAULT_IO_TIMEOUT);
3085 						goto retry_write;
3086 					}
3087 					goto next;
3088 				}
3089 				done_index = page->index + 1;
3090 				done = 1;
3091 				break;
3092 			}
3093 
3094 			if (wbc->nr_to_write <= 0 &&
3095 					wbc->sync_mode == WB_SYNC_NONE) {
3096 				done = 1;
3097 				break;
3098 			}
3099 next:
3100 			if (need_readd)
3101 				goto readd;
3102 		}
3103 		pagevec_release(&pvec);
3104 		cond_resched();
3105 	}
3106 #ifdef CONFIG_F2FS_FS_COMPRESSION
3107 	/* flush remained pages in compress cluster */
3108 	if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3109 		ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3110 		nwritten += submitted;
3111 		wbc->nr_to_write -= submitted;
3112 		if (ret) {
3113 			done = 1;
3114 			retry = 0;
3115 		}
3116 	}
3117 	if (f2fs_compressed_file(inode))
3118 		f2fs_destroy_compress_ctx(&cc, false);
3119 #endif
3120 	if (retry) {
3121 		index = 0;
3122 		end = -1;
3123 		goto retry;
3124 	}
3125 	if (wbc->range_cyclic && !done)
3126 		done_index = 0;
3127 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3128 		mapping->writeback_index = done_index;
3129 
3130 	if (nwritten)
3131 		f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3132 								NULL, 0, DATA);
3133 	/* submit cached bio of IPU write */
3134 	if (bio)
3135 		f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3136 
3137 	return ret;
3138 }
3139 
__should_serialize_io(struct inode * inode,struct writeback_control * wbc)3140 static inline bool __should_serialize_io(struct inode *inode,
3141 					struct writeback_control *wbc)
3142 {
3143 	/* to avoid deadlock in path of data flush */
3144 	if (F2FS_I(inode)->cp_task)
3145 		return false;
3146 
3147 	if (!S_ISREG(inode->i_mode))
3148 		return false;
3149 	if (IS_NOQUOTA(inode))
3150 		return false;
3151 
3152 	if (f2fs_need_compress_data(inode))
3153 		return true;
3154 	if (wbc->sync_mode != WB_SYNC_ALL)
3155 		return true;
3156 	if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3157 		return true;
3158 	return false;
3159 }
3160 
__f2fs_write_data_pages(struct address_space * mapping,struct writeback_control * wbc,enum iostat_type io_type)3161 static int __f2fs_write_data_pages(struct address_space *mapping,
3162 						struct writeback_control *wbc,
3163 						enum iostat_type io_type)
3164 {
3165 	struct inode *inode = mapping->host;
3166 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3167 	struct blk_plug plug;
3168 	int ret;
3169 	bool locked = false;
3170 
3171 	/* deal with chardevs and other special file */
3172 	if (!mapping->a_ops->writepage)
3173 		return 0;
3174 
3175 	/* skip writing if there is no dirty page in this inode */
3176 	if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3177 		return 0;
3178 
3179 	/* during POR, we don't need to trigger writepage at all. */
3180 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3181 		goto skip_write;
3182 
3183 	if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3184 			wbc->sync_mode == WB_SYNC_NONE &&
3185 			get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3186 			f2fs_available_free_memory(sbi, DIRTY_DENTS))
3187 		goto skip_write;
3188 
3189 	/* skip writing in file defragment preparing stage */
3190 	if (is_inode_flag_set(inode, FI_SKIP_WRITES))
3191 		goto skip_write;
3192 
3193 	trace_f2fs_writepages(mapping->host, wbc, DATA);
3194 
3195 	/* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3196 	if (wbc->sync_mode == WB_SYNC_ALL)
3197 		atomic_inc(&sbi->wb_sync_req[DATA]);
3198 	else if (atomic_read(&sbi->wb_sync_req[DATA])) {
3199 		/* to avoid potential deadlock */
3200 		if (current->plug)
3201 			blk_finish_plug(current->plug);
3202 		goto skip_write;
3203 	}
3204 
3205 	if (__should_serialize_io(inode, wbc)) {
3206 		mutex_lock(&sbi->writepages);
3207 		locked = true;
3208 	}
3209 
3210 	blk_start_plug(&plug);
3211 	ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3212 	blk_finish_plug(&plug);
3213 
3214 	if (locked)
3215 		mutex_unlock(&sbi->writepages);
3216 
3217 	if (wbc->sync_mode == WB_SYNC_ALL)
3218 		atomic_dec(&sbi->wb_sync_req[DATA]);
3219 	/*
3220 	 * if some pages were truncated, we cannot guarantee its mapping->host
3221 	 * to detect pending bios.
3222 	 */
3223 
3224 	f2fs_remove_dirty_inode(inode);
3225 	return ret;
3226 
3227 skip_write:
3228 	wbc->pages_skipped += get_dirty_pages(inode);
3229 	trace_f2fs_writepages(mapping->host, wbc, DATA);
3230 	return 0;
3231 }
3232 
f2fs_write_data_pages(struct address_space * mapping,struct writeback_control * wbc)3233 static int f2fs_write_data_pages(struct address_space *mapping,
3234 			    struct writeback_control *wbc)
3235 {
3236 	struct inode *inode = mapping->host;
3237 
3238 	return __f2fs_write_data_pages(mapping, wbc,
3239 			F2FS_I(inode)->cp_task == current ?
3240 			FS_CP_DATA_IO : FS_DATA_IO);
3241 }
3242 
f2fs_write_failed(struct inode * inode,loff_t to)3243 void f2fs_write_failed(struct inode *inode, loff_t to)
3244 {
3245 	loff_t i_size = i_size_read(inode);
3246 
3247 	if (IS_NOQUOTA(inode))
3248 		return;
3249 
3250 	/* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3251 	if (to > i_size && !f2fs_verity_in_progress(inode)) {
3252 		f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3253 		filemap_invalidate_lock(inode->i_mapping);
3254 
3255 		truncate_pagecache(inode, i_size);
3256 		f2fs_truncate_blocks(inode, i_size, true);
3257 
3258 		filemap_invalidate_unlock(inode->i_mapping);
3259 		f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3260 	}
3261 }
3262 
prepare_write_begin(struct f2fs_sb_info * sbi,struct page * page,loff_t pos,unsigned len,block_t * blk_addr,bool * node_changed)3263 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3264 			struct page *page, loff_t pos, unsigned len,
3265 			block_t *blk_addr, bool *node_changed)
3266 {
3267 	struct inode *inode = page->mapping->host;
3268 	pgoff_t index = page->index;
3269 	struct dnode_of_data dn;
3270 	struct page *ipage;
3271 	bool locked = false;
3272 	struct extent_info ei = {0, };
3273 	int err = 0;
3274 	int flag;
3275 
3276 	/*
3277 	 * If a whole page is being written and we already preallocated all the
3278 	 * blocks, then there is no need to get a block address now.
3279 	 */
3280 	if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL))
3281 		return 0;
3282 
3283 	/* f2fs_lock_op avoids race between write CP and convert_inline_page */
3284 	if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3285 		flag = F2FS_GET_BLOCK_DEFAULT;
3286 	else
3287 		flag = F2FS_GET_BLOCK_PRE_AIO;
3288 
3289 	if (f2fs_has_inline_data(inode) ||
3290 			(pos & PAGE_MASK) >= i_size_read(inode)) {
3291 		f2fs_do_map_lock(sbi, flag, true);
3292 		locked = true;
3293 	}
3294 
3295 restart:
3296 	/* check inline_data */
3297 	ipage = f2fs_get_node_page(sbi, inode->i_ino);
3298 	if (IS_ERR(ipage)) {
3299 		err = PTR_ERR(ipage);
3300 		goto unlock_out;
3301 	}
3302 
3303 	set_new_dnode(&dn, inode, ipage, ipage, 0);
3304 
3305 	if (f2fs_has_inline_data(inode)) {
3306 		if (pos + len <= MAX_INLINE_DATA(inode)) {
3307 			f2fs_do_read_inline_data(page, ipage);
3308 			set_inode_flag(inode, FI_DATA_EXIST);
3309 			if (inode->i_nlink)
3310 				set_page_private_inline(ipage);
3311 		} else {
3312 			err = f2fs_convert_inline_page(&dn, page);
3313 			if (err)
3314 				goto out;
3315 			if (dn.data_blkaddr == NULL_ADDR)
3316 				err = f2fs_get_block(&dn, index);
3317 		}
3318 	} else if (locked) {
3319 		err = f2fs_get_block(&dn, index);
3320 	} else {
3321 		if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3322 			dn.data_blkaddr = ei.blk + index - ei.fofs;
3323 		} else {
3324 			/* hole case */
3325 			err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3326 			if (err || dn.data_blkaddr == NULL_ADDR) {
3327 				f2fs_put_dnode(&dn);
3328 				f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3329 								true);
3330 				WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3331 				locked = true;
3332 				goto restart;
3333 			}
3334 		}
3335 	}
3336 
3337 	/* convert_inline_page can make node_changed */
3338 	*blk_addr = dn.data_blkaddr;
3339 	*node_changed = dn.node_changed;
3340 out:
3341 	f2fs_put_dnode(&dn);
3342 unlock_out:
3343 	if (locked)
3344 		f2fs_do_map_lock(sbi, flag, false);
3345 	return err;
3346 }
3347 
__find_data_block(struct inode * inode,pgoff_t index,block_t * blk_addr)3348 static int __find_data_block(struct inode *inode, pgoff_t index,
3349 				block_t *blk_addr)
3350 {
3351 	struct dnode_of_data dn;
3352 	struct page *ipage;
3353 	struct extent_info ei = {0, };
3354 	int err = 0;
3355 
3356 	ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
3357 	if (IS_ERR(ipage))
3358 		return PTR_ERR(ipage);
3359 
3360 	set_new_dnode(&dn, inode, ipage, ipage, 0);
3361 
3362 	if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3363 		dn.data_blkaddr = ei.blk + index - ei.fofs;
3364 	} else {
3365 		/* hole case */
3366 		err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3367 		if (err) {
3368 			dn.data_blkaddr = NULL_ADDR;
3369 			err = 0;
3370 		}
3371 	}
3372 	*blk_addr = dn.data_blkaddr;
3373 	f2fs_put_dnode(&dn);
3374 	return err;
3375 }
3376 
__reserve_data_block(struct inode * inode,pgoff_t index,block_t * blk_addr,bool * node_changed)3377 static int __reserve_data_block(struct inode *inode, pgoff_t index,
3378 				block_t *blk_addr, bool *node_changed)
3379 {
3380 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3381 	struct dnode_of_data dn;
3382 	struct page *ipage;
3383 	int err = 0;
3384 
3385 	f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
3386 
3387 	ipage = f2fs_get_node_page(sbi, inode->i_ino);
3388 	if (IS_ERR(ipage)) {
3389 		err = PTR_ERR(ipage);
3390 		goto unlock_out;
3391 	}
3392 	set_new_dnode(&dn, inode, ipage, ipage, 0);
3393 
3394 	err = f2fs_get_block(&dn, index);
3395 
3396 	*blk_addr = dn.data_blkaddr;
3397 	*node_changed = dn.node_changed;
3398 	f2fs_put_dnode(&dn);
3399 
3400 unlock_out:
3401 	f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
3402 	return err;
3403 }
3404 
prepare_atomic_write_begin(struct f2fs_sb_info * sbi,struct page * page,loff_t pos,unsigned int len,block_t * blk_addr,bool * node_changed)3405 static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi,
3406 			struct page *page, loff_t pos, unsigned int len,
3407 			block_t *blk_addr, bool *node_changed)
3408 {
3409 	struct inode *inode = page->mapping->host;
3410 	struct inode *cow_inode = F2FS_I(inode)->cow_inode;
3411 	pgoff_t index = page->index;
3412 	int err = 0;
3413 	block_t ori_blk_addr;
3414 
3415 	/* If pos is beyond the end of file, reserve a new block in COW inode */
3416 	if ((pos & PAGE_MASK) >= i_size_read(inode))
3417 		return __reserve_data_block(cow_inode, index, blk_addr,
3418 					node_changed);
3419 
3420 	/* Look for the block in COW inode first */
3421 	err = __find_data_block(cow_inode, index, blk_addr);
3422 	if (err)
3423 		return err;
3424 	else if (*blk_addr != NULL_ADDR)
3425 		return 0;
3426 
3427 	/* Look for the block in the original inode */
3428 	err = __find_data_block(inode, index, &ori_blk_addr);
3429 	if (err)
3430 		return err;
3431 
3432 	/* Finally, we should reserve a new block in COW inode for the update */
3433 	err = __reserve_data_block(cow_inode, index, blk_addr, node_changed);
3434 	if (err)
3435 		return err;
3436 
3437 	if (ori_blk_addr != NULL_ADDR)
3438 		*blk_addr = ori_blk_addr;
3439 	return 0;
3440 }
3441 
f2fs_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct page ** pagep,void ** fsdata)3442 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3443 		loff_t pos, unsigned len, struct page **pagep, void **fsdata)
3444 {
3445 	struct inode *inode = mapping->host;
3446 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3447 	struct page *page = NULL;
3448 	pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3449 	bool need_balance = false;
3450 	block_t blkaddr = NULL_ADDR;
3451 	int err = 0;
3452 
3453 	trace_f2fs_write_begin(inode, pos, len);
3454 
3455 	if (!f2fs_is_checkpoint_ready(sbi)) {
3456 		err = -ENOSPC;
3457 		goto fail;
3458 	}
3459 
3460 	/*
3461 	 * We should check this at this moment to avoid deadlock on inode page
3462 	 * and #0 page. The locking rule for inline_data conversion should be:
3463 	 * lock_page(page #0) -> lock_page(inode_page)
3464 	 */
3465 	if (index != 0) {
3466 		err = f2fs_convert_inline_inode(inode);
3467 		if (err)
3468 			goto fail;
3469 	}
3470 
3471 #ifdef CONFIG_F2FS_FS_COMPRESSION
3472 	if (f2fs_compressed_file(inode)) {
3473 		int ret;
3474 
3475 		*fsdata = NULL;
3476 
3477 		if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode)))
3478 			goto repeat;
3479 
3480 		ret = f2fs_prepare_compress_overwrite(inode, pagep,
3481 							index, fsdata);
3482 		if (ret < 0) {
3483 			err = ret;
3484 			goto fail;
3485 		} else if (ret) {
3486 			return 0;
3487 		}
3488 	}
3489 #endif
3490 
3491 repeat:
3492 	/*
3493 	 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3494 	 * wait_for_stable_page. Will wait that below with our IO control.
3495 	 */
3496 	page = f2fs_pagecache_get_page(mapping, index,
3497 				FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3498 	if (!page) {
3499 		err = -ENOMEM;
3500 		goto fail;
3501 	}
3502 
3503 	/* TODO: cluster can be compressed due to race with .writepage */
3504 
3505 	*pagep = page;
3506 
3507 	if (f2fs_is_atomic_file(inode))
3508 		err = prepare_atomic_write_begin(sbi, page, pos, len,
3509 					&blkaddr, &need_balance);
3510 	else
3511 		err = prepare_write_begin(sbi, page, pos, len,
3512 					&blkaddr, &need_balance);
3513 	if (err)
3514 		goto fail;
3515 
3516 	if (need_balance && !IS_NOQUOTA(inode) &&
3517 			has_not_enough_free_secs(sbi, 0, 0)) {
3518 		unlock_page(page);
3519 		f2fs_balance_fs(sbi, true);
3520 		lock_page(page);
3521 		if (page->mapping != mapping) {
3522 			/* The page got truncated from under us */
3523 			f2fs_put_page(page, 1);
3524 			goto repeat;
3525 		}
3526 	}
3527 
3528 	f2fs_wait_on_page_writeback(page, DATA, false, true);
3529 
3530 	if (len == PAGE_SIZE || PageUptodate(page))
3531 		return 0;
3532 
3533 	if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3534 	    !f2fs_verity_in_progress(inode)) {
3535 		zero_user_segment(page, len, PAGE_SIZE);
3536 		return 0;
3537 	}
3538 
3539 	if (blkaddr == NEW_ADDR) {
3540 		zero_user_segment(page, 0, PAGE_SIZE);
3541 		SetPageUptodate(page);
3542 	} else {
3543 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3544 				DATA_GENERIC_ENHANCE_READ)) {
3545 			err = -EFSCORRUPTED;
3546 			goto fail;
3547 		}
3548 		err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
3549 		if (err)
3550 			goto fail;
3551 
3552 		lock_page(page);
3553 		if (unlikely(page->mapping != mapping)) {
3554 			f2fs_put_page(page, 1);
3555 			goto repeat;
3556 		}
3557 		if (unlikely(!PageUptodate(page))) {
3558 			err = -EIO;
3559 			goto fail;
3560 		}
3561 	}
3562 	return 0;
3563 
3564 fail:
3565 	f2fs_put_page(page, 1);
3566 	f2fs_write_failed(inode, pos + len);
3567 	return err;
3568 }
3569 
f2fs_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)3570 static int f2fs_write_end(struct file *file,
3571 			struct address_space *mapping,
3572 			loff_t pos, unsigned len, unsigned copied,
3573 			struct page *page, void *fsdata)
3574 {
3575 	struct inode *inode = page->mapping->host;
3576 
3577 	trace_f2fs_write_end(inode, pos, len, copied);
3578 
3579 	/*
3580 	 * This should be come from len == PAGE_SIZE, and we expect copied
3581 	 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3582 	 * let generic_perform_write() try to copy data again through copied=0.
3583 	 */
3584 	if (!PageUptodate(page)) {
3585 		if (unlikely(copied != len))
3586 			copied = 0;
3587 		else
3588 			SetPageUptodate(page);
3589 	}
3590 
3591 #ifdef CONFIG_F2FS_FS_COMPRESSION
3592 	/* overwrite compressed file */
3593 	if (f2fs_compressed_file(inode) && fsdata) {
3594 		f2fs_compress_write_end(inode, fsdata, page->index, copied);
3595 		f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3596 
3597 		if (pos + copied > i_size_read(inode) &&
3598 				!f2fs_verity_in_progress(inode))
3599 			f2fs_i_size_write(inode, pos + copied);
3600 		return copied;
3601 	}
3602 #endif
3603 
3604 	if (!copied)
3605 		goto unlock_out;
3606 
3607 	set_page_dirty(page);
3608 
3609 	if (pos + copied > i_size_read(inode) &&
3610 	    !f2fs_verity_in_progress(inode)) {
3611 		f2fs_i_size_write(inode, pos + copied);
3612 		if (f2fs_is_atomic_file(inode))
3613 			f2fs_i_size_write(F2FS_I(inode)->cow_inode,
3614 					pos + copied);
3615 	}
3616 unlock_out:
3617 	f2fs_put_page(page, 1);
3618 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3619 	return copied;
3620 }
3621 
f2fs_invalidate_folio(struct folio * folio,size_t offset,size_t length)3622 void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
3623 {
3624 	struct inode *inode = folio->mapping->host;
3625 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3626 
3627 	if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3628 				(offset || length != folio_size(folio)))
3629 		return;
3630 
3631 	if (folio_test_dirty(folio)) {
3632 		if (inode->i_ino == F2FS_META_INO(sbi)) {
3633 			dec_page_count(sbi, F2FS_DIRTY_META);
3634 		} else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3635 			dec_page_count(sbi, F2FS_DIRTY_NODES);
3636 		} else {
3637 			inode_dec_dirty_pages(inode);
3638 			f2fs_remove_dirty_inode(inode);
3639 		}
3640 	}
3641 
3642 	clear_page_private_gcing(&folio->page);
3643 
3644 	if (test_opt(sbi, COMPRESS_CACHE) &&
3645 			inode->i_ino == F2FS_COMPRESS_INO(sbi))
3646 		clear_page_private_data(&folio->page);
3647 
3648 	folio_detach_private(folio);
3649 }
3650 
f2fs_release_folio(struct folio * folio,gfp_t wait)3651 bool f2fs_release_folio(struct folio *folio, gfp_t wait)
3652 {
3653 	struct f2fs_sb_info *sbi;
3654 
3655 	/* If this is dirty folio, keep private data */
3656 	if (folio_test_dirty(folio))
3657 		return false;
3658 
3659 	sbi = F2FS_M_SB(folio->mapping);
3660 	if (test_opt(sbi, COMPRESS_CACHE)) {
3661 		struct inode *inode = folio->mapping->host;
3662 
3663 		if (inode->i_ino == F2FS_COMPRESS_INO(sbi))
3664 			clear_page_private_data(&folio->page);
3665 	}
3666 
3667 	clear_page_private_gcing(&folio->page);
3668 
3669 	folio_detach_private(folio);
3670 	return true;
3671 }
3672 
f2fs_dirty_data_folio(struct address_space * mapping,struct folio * folio)3673 static bool f2fs_dirty_data_folio(struct address_space *mapping,
3674 		struct folio *folio)
3675 {
3676 	struct inode *inode = mapping->host;
3677 
3678 	trace_f2fs_set_page_dirty(&folio->page, DATA);
3679 
3680 	if (!folio_test_uptodate(folio))
3681 		folio_mark_uptodate(folio);
3682 	BUG_ON(folio_test_swapcache(folio));
3683 
3684 	if (!folio_test_dirty(folio)) {
3685 		filemap_dirty_folio(mapping, folio);
3686 		f2fs_update_dirty_folio(inode, folio);
3687 		return true;
3688 	}
3689 	return false;
3690 }
3691 
3692 
f2fs_bmap_compress(struct inode * inode,sector_t block)3693 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3694 {
3695 #ifdef CONFIG_F2FS_FS_COMPRESSION
3696 	struct dnode_of_data dn;
3697 	sector_t start_idx, blknr = 0;
3698 	int ret;
3699 
3700 	start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3701 
3702 	set_new_dnode(&dn, inode, NULL, NULL, 0);
3703 	ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3704 	if (ret)
3705 		return 0;
3706 
3707 	if (dn.data_blkaddr != COMPRESS_ADDR) {
3708 		dn.ofs_in_node += block - start_idx;
3709 		blknr = f2fs_data_blkaddr(&dn);
3710 		if (!__is_valid_data_blkaddr(blknr))
3711 			blknr = 0;
3712 	}
3713 
3714 	f2fs_put_dnode(&dn);
3715 	return blknr;
3716 #else
3717 	return 0;
3718 #endif
3719 }
3720 
3721 
f2fs_bmap(struct address_space * mapping,sector_t block)3722 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3723 {
3724 	struct inode *inode = mapping->host;
3725 	sector_t blknr = 0;
3726 
3727 	if (f2fs_has_inline_data(inode))
3728 		goto out;
3729 
3730 	/* make sure allocating whole blocks */
3731 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3732 		filemap_write_and_wait(mapping);
3733 
3734 	/* Block number less than F2FS MAX BLOCKS */
3735 	if (unlikely(block >= max_file_blocks(inode)))
3736 		goto out;
3737 
3738 	if (f2fs_compressed_file(inode)) {
3739 		blknr = f2fs_bmap_compress(inode, block);
3740 	} else {
3741 		struct f2fs_map_blocks map;
3742 
3743 		memset(&map, 0, sizeof(map));
3744 		map.m_lblk = block;
3745 		map.m_len = 1;
3746 		map.m_next_pgofs = NULL;
3747 		map.m_seg_type = NO_CHECK_TYPE;
3748 
3749 		if (!f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_BMAP))
3750 			blknr = map.m_pblk;
3751 	}
3752 out:
3753 	trace_f2fs_bmap(inode, block, blknr);
3754 	return blknr;
3755 }
3756 
3757 #ifdef CONFIG_MIGRATION
3758 #include <linux/migrate.h>
3759 
f2fs_migrate_page(struct address_space * mapping,struct page * newpage,struct page * page,enum migrate_mode mode)3760 int f2fs_migrate_page(struct address_space *mapping,
3761 		struct page *newpage, struct page *page, enum migrate_mode mode)
3762 {
3763 	int rc, extra_count = 0;
3764 
3765 	BUG_ON(PageWriteback(page));
3766 
3767 	rc = migrate_page_move_mapping(mapping, newpage,
3768 				page, extra_count);
3769 	if (rc != MIGRATEPAGE_SUCCESS)
3770 		return rc;
3771 
3772 	/* guarantee to start from no stale private field */
3773 	set_page_private(newpage, 0);
3774 	if (PagePrivate(page)) {
3775 		set_page_private(newpage, page_private(page));
3776 		SetPagePrivate(newpage);
3777 		get_page(newpage);
3778 
3779 		set_page_private(page, 0);
3780 		ClearPagePrivate(page);
3781 		put_page(page);
3782 	}
3783 
3784 	if (mode != MIGRATE_SYNC_NO_COPY)
3785 		migrate_page_copy(newpage, page);
3786 	else
3787 		migrate_page_states(newpage, page);
3788 
3789 	return MIGRATEPAGE_SUCCESS;
3790 }
3791 #endif
3792 
3793 #ifdef CONFIG_SWAP
f2fs_migrate_blocks(struct inode * inode,block_t start_blk,unsigned int blkcnt)3794 static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
3795 							unsigned int blkcnt)
3796 {
3797 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3798 	unsigned int blkofs;
3799 	unsigned int blk_per_sec = BLKS_PER_SEC(sbi);
3800 	unsigned int secidx = start_blk / blk_per_sec;
3801 	unsigned int end_sec = secidx + blkcnt / blk_per_sec;
3802 	int ret = 0;
3803 
3804 	f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3805 	filemap_invalidate_lock(inode->i_mapping);
3806 
3807 	set_inode_flag(inode, FI_ALIGNED_WRITE);
3808 	set_inode_flag(inode, FI_OPU_WRITE);
3809 
3810 	for (; secidx < end_sec; secidx++) {
3811 		f2fs_down_write(&sbi->pin_sem);
3812 
3813 		f2fs_lock_op(sbi);
3814 		f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false);
3815 		f2fs_unlock_op(sbi);
3816 
3817 		set_inode_flag(inode, FI_SKIP_WRITES);
3818 
3819 		for (blkofs = 0; blkofs < blk_per_sec; blkofs++) {
3820 			struct page *page;
3821 			unsigned int blkidx = secidx * blk_per_sec + blkofs;
3822 
3823 			page = f2fs_get_lock_data_page(inode, blkidx, true);
3824 			if (IS_ERR(page)) {
3825 				f2fs_up_write(&sbi->pin_sem);
3826 				ret = PTR_ERR(page);
3827 				goto done;
3828 			}
3829 
3830 			set_page_dirty(page);
3831 			f2fs_put_page(page, 1);
3832 		}
3833 
3834 		clear_inode_flag(inode, FI_SKIP_WRITES);
3835 
3836 		ret = filemap_fdatawrite(inode->i_mapping);
3837 
3838 		f2fs_up_write(&sbi->pin_sem);
3839 
3840 		if (ret)
3841 			break;
3842 	}
3843 
3844 done:
3845 	clear_inode_flag(inode, FI_SKIP_WRITES);
3846 	clear_inode_flag(inode, FI_OPU_WRITE);
3847 	clear_inode_flag(inode, FI_ALIGNED_WRITE);
3848 
3849 	filemap_invalidate_unlock(inode->i_mapping);
3850 	f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3851 
3852 	return ret;
3853 }
3854 
check_swap_activate(struct swap_info_struct * sis,struct file * swap_file,sector_t * span)3855 static int check_swap_activate(struct swap_info_struct *sis,
3856 				struct file *swap_file, sector_t *span)
3857 {
3858 	struct address_space *mapping = swap_file->f_mapping;
3859 	struct inode *inode = mapping->host;
3860 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3861 	sector_t cur_lblock;
3862 	sector_t last_lblock;
3863 	sector_t pblock;
3864 	sector_t lowest_pblock = -1;
3865 	sector_t highest_pblock = 0;
3866 	int nr_extents = 0;
3867 	unsigned long nr_pblocks;
3868 	unsigned int blks_per_sec = BLKS_PER_SEC(sbi);
3869 	unsigned int sec_blks_mask = BLKS_PER_SEC(sbi) - 1;
3870 	unsigned int not_aligned = 0;
3871 	int ret = 0;
3872 
3873 	/*
3874 	 * Map all the blocks into the extent list.  This code doesn't try
3875 	 * to be very smart.
3876 	 */
3877 	cur_lblock = 0;
3878 	last_lblock = bytes_to_blks(inode, i_size_read(inode));
3879 
3880 	while (cur_lblock < last_lblock && cur_lblock < sis->max) {
3881 		struct f2fs_map_blocks map;
3882 retry:
3883 		cond_resched();
3884 
3885 		memset(&map, 0, sizeof(map));
3886 		map.m_lblk = cur_lblock;
3887 		map.m_len = last_lblock - cur_lblock;
3888 		map.m_next_pgofs = NULL;
3889 		map.m_next_extent = NULL;
3890 		map.m_seg_type = NO_CHECK_TYPE;
3891 		map.m_may_create = false;
3892 
3893 		ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
3894 		if (ret)
3895 			goto out;
3896 
3897 		/* hole */
3898 		if (!(map.m_flags & F2FS_MAP_FLAGS)) {
3899 			f2fs_err(sbi, "Swapfile has holes");
3900 			ret = -EINVAL;
3901 			goto out;
3902 		}
3903 
3904 		pblock = map.m_pblk;
3905 		nr_pblocks = map.m_len;
3906 
3907 		if ((pblock - SM_I(sbi)->main_blkaddr) & sec_blks_mask ||
3908 				nr_pblocks & sec_blks_mask) {
3909 			not_aligned++;
3910 
3911 			nr_pblocks = roundup(nr_pblocks, blks_per_sec);
3912 			if (cur_lblock + nr_pblocks > sis->max)
3913 				nr_pblocks -= blks_per_sec;
3914 
3915 			if (!nr_pblocks) {
3916 				/* this extent is last one */
3917 				nr_pblocks = map.m_len;
3918 				f2fs_warn(sbi, "Swapfile: last extent is not aligned to section");
3919 				goto next;
3920 			}
3921 
3922 			ret = f2fs_migrate_blocks(inode, cur_lblock,
3923 							nr_pblocks);
3924 			if (ret)
3925 				goto out;
3926 			goto retry;
3927 		}
3928 next:
3929 		if (cur_lblock + nr_pblocks >= sis->max)
3930 			nr_pblocks = sis->max - cur_lblock;
3931 
3932 		if (cur_lblock) {	/* exclude the header page */
3933 			if (pblock < lowest_pblock)
3934 				lowest_pblock = pblock;
3935 			if (pblock + nr_pblocks - 1 > highest_pblock)
3936 				highest_pblock = pblock + nr_pblocks - 1;
3937 		}
3938 
3939 		/*
3940 		 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3941 		 */
3942 		ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
3943 		if (ret < 0)
3944 			goto out;
3945 		nr_extents += ret;
3946 		cur_lblock += nr_pblocks;
3947 	}
3948 	ret = nr_extents;
3949 	*span = 1 + highest_pblock - lowest_pblock;
3950 	if (cur_lblock == 0)
3951 		cur_lblock = 1;	/* force Empty message */
3952 	sis->max = cur_lblock;
3953 	sis->pages = cur_lblock - 1;
3954 	sis->highest_bit = cur_lblock - 1;
3955 out:
3956 	if (not_aligned)
3957 		f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%u * N)",
3958 			  not_aligned, blks_per_sec * F2FS_BLKSIZE);
3959 	return ret;
3960 }
3961 
f2fs_swap_activate(struct swap_info_struct * sis,struct file * file,sector_t * span)3962 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3963 				sector_t *span)
3964 {
3965 	struct inode *inode = file_inode(file);
3966 	int ret;
3967 
3968 	if (!S_ISREG(inode->i_mode))
3969 		return -EINVAL;
3970 
3971 	if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3972 		return -EROFS;
3973 
3974 	if (f2fs_lfs_mode(F2FS_I_SB(inode))) {
3975 		f2fs_err(F2FS_I_SB(inode),
3976 			"Swapfile not supported in LFS mode");
3977 		return -EINVAL;
3978 	}
3979 
3980 	ret = f2fs_convert_inline_inode(inode);
3981 	if (ret)
3982 		return ret;
3983 
3984 	if (!f2fs_disable_compressed_file(inode))
3985 		return -EINVAL;
3986 
3987 	f2fs_precache_extents(inode);
3988 
3989 	ret = check_swap_activate(sis, file, span);
3990 	if (ret < 0)
3991 		return ret;
3992 
3993 	set_inode_flag(inode, FI_PIN_FILE);
3994 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3995 	return ret;
3996 }
3997 
f2fs_swap_deactivate(struct file * file)3998 static void f2fs_swap_deactivate(struct file *file)
3999 {
4000 	struct inode *inode = file_inode(file);
4001 
4002 	clear_inode_flag(inode, FI_PIN_FILE);
4003 }
4004 #else
f2fs_swap_activate(struct swap_info_struct * sis,struct file * file,sector_t * span)4005 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4006 				sector_t *span)
4007 {
4008 	return -EOPNOTSUPP;
4009 }
4010 
f2fs_swap_deactivate(struct file * file)4011 static void f2fs_swap_deactivate(struct file *file)
4012 {
4013 }
4014 #endif
4015 
4016 const struct address_space_operations f2fs_dblock_aops = {
4017 	.read_folio	= f2fs_read_data_folio,
4018 	.readahead	= f2fs_readahead,
4019 	.writepage	= f2fs_write_data_page,
4020 	.writepages	= f2fs_write_data_pages,
4021 	.write_begin	= f2fs_write_begin,
4022 	.write_end	= f2fs_write_end,
4023 	.dirty_folio	= f2fs_dirty_data_folio,
4024 	.invalidate_folio = f2fs_invalidate_folio,
4025 	.release_folio	= f2fs_release_folio,
4026 	.direct_IO	= noop_direct_IO,
4027 	.bmap		= f2fs_bmap,
4028 	.swap_activate  = f2fs_swap_activate,
4029 	.swap_deactivate = f2fs_swap_deactivate,
4030 #ifdef CONFIG_MIGRATION
4031 	.migratepage    = f2fs_migrate_page,
4032 #endif
4033 };
4034 
f2fs_clear_page_cache_dirty_tag(struct page * page)4035 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4036 {
4037 	struct address_space *mapping = page_mapping(page);
4038 	unsigned long flags;
4039 
4040 	xa_lock_irqsave(&mapping->i_pages, flags);
4041 	__xa_clear_mark(&mapping->i_pages, page_index(page),
4042 						PAGECACHE_TAG_DIRTY);
4043 	xa_unlock_irqrestore(&mapping->i_pages, flags);
4044 }
4045 
f2fs_init_post_read_processing(void)4046 int __init f2fs_init_post_read_processing(void)
4047 {
4048 	bio_post_read_ctx_cache =
4049 		kmem_cache_create("f2fs_bio_post_read_ctx",
4050 				  sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4051 	if (!bio_post_read_ctx_cache)
4052 		goto fail;
4053 	bio_post_read_ctx_pool =
4054 		mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4055 					 bio_post_read_ctx_cache);
4056 	if (!bio_post_read_ctx_pool)
4057 		goto fail_free_cache;
4058 	return 0;
4059 
4060 fail_free_cache:
4061 	kmem_cache_destroy(bio_post_read_ctx_cache);
4062 fail:
4063 	return -ENOMEM;
4064 }
4065 
f2fs_destroy_post_read_processing(void)4066 void f2fs_destroy_post_read_processing(void)
4067 {
4068 	mempool_destroy(bio_post_read_ctx_pool);
4069 	kmem_cache_destroy(bio_post_read_ctx_cache);
4070 }
4071 
f2fs_init_post_read_wq(struct f2fs_sb_info * sbi)4072 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4073 {
4074 	if (!f2fs_sb_has_encrypt(sbi) &&
4075 		!f2fs_sb_has_verity(sbi) &&
4076 		!f2fs_sb_has_compression(sbi))
4077 		return 0;
4078 
4079 	sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4080 						 WQ_UNBOUND | WQ_HIGHPRI,
4081 						 num_online_cpus());
4082 	if (!sbi->post_read_wq)
4083 		return -ENOMEM;
4084 	return 0;
4085 }
4086 
f2fs_destroy_post_read_wq(struct f2fs_sb_info * sbi)4087 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4088 {
4089 	if (sbi->post_read_wq)
4090 		destroy_workqueue(sbi->post_read_wq);
4091 }
4092 
f2fs_init_bio_entry_cache(void)4093 int __init f2fs_init_bio_entry_cache(void)
4094 {
4095 	bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4096 			sizeof(struct bio_entry));
4097 	if (!bio_entry_slab)
4098 		return -ENOMEM;
4099 	return 0;
4100 }
4101 
f2fs_destroy_bio_entry_cache(void)4102 void f2fs_destroy_bio_entry_cache(void)
4103 {
4104 	kmem_cache_destroy(bio_entry_slab);
4105 }
4106 
f2fs_iomap_begin(struct inode * inode,loff_t offset,loff_t length,unsigned int flags,struct iomap * iomap,struct iomap * srcmap)4107 static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
4108 			    unsigned int flags, struct iomap *iomap,
4109 			    struct iomap *srcmap)
4110 {
4111 	struct f2fs_map_blocks map = {};
4112 	pgoff_t next_pgofs = 0;
4113 	int err;
4114 
4115 	map.m_lblk = bytes_to_blks(inode, offset);
4116 	map.m_len = bytes_to_blks(inode, offset + length - 1) - map.m_lblk + 1;
4117 	map.m_next_pgofs = &next_pgofs;
4118 	map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint);
4119 	if (flags & IOMAP_WRITE)
4120 		map.m_may_create = true;
4121 
4122 	err = f2fs_map_blocks(inode, &map, flags & IOMAP_WRITE,
4123 			      F2FS_GET_BLOCK_DIO);
4124 	if (err)
4125 		return err;
4126 
4127 	iomap->offset = blks_to_bytes(inode, map.m_lblk);
4128 
4129 	/*
4130 	 * When inline encryption is enabled, sometimes I/O to an encrypted file
4131 	 * has to be broken up to guarantee DUN contiguity.  Handle this by
4132 	 * limiting the length of the mapping returned.
4133 	 */
4134 	map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len);
4135 
4136 	if (map.m_flags & (F2FS_MAP_MAPPED | F2FS_MAP_UNWRITTEN)) {
4137 		iomap->length = blks_to_bytes(inode, map.m_len);
4138 		if (map.m_flags & F2FS_MAP_MAPPED) {
4139 			iomap->type = IOMAP_MAPPED;
4140 			iomap->flags |= IOMAP_F_MERGED;
4141 		} else {
4142 			iomap->type = IOMAP_UNWRITTEN;
4143 		}
4144 		if (WARN_ON_ONCE(!__is_valid_data_blkaddr(map.m_pblk)))
4145 			return -EINVAL;
4146 
4147 		iomap->bdev = map.m_bdev;
4148 		iomap->addr = blks_to_bytes(inode, map.m_pblk);
4149 	} else {
4150 		iomap->length = blks_to_bytes(inode, next_pgofs) -
4151 				iomap->offset;
4152 		iomap->type = IOMAP_HOLE;
4153 		iomap->addr = IOMAP_NULL_ADDR;
4154 	}
4155 
4156 	if (map.m_flags & F2FS_MAP_NEW)
4157 		iomap->flags |= IOMAP_F_NEW;
4158 	if ((inode->i_state & I_DIRTY_DATASYNC) ||
4159 	    offset + length > i_size_read(inode))
4160 		iomap->flags |= IOMAP_F_DIRTY;
4161 
4162 	return 0;
4163 }
4164 
4165 const struct iomap_ops f2fs_iomap_ops = {
4166 	.iomap_begin	= f2fs_iomap_begin,
4167 };
4168