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