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