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