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