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