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