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