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->pending_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)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 {
1032 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1033 	struct bio *bio;
1034 	struct bio_post_read_ctx *ctx;
1035 	unsigned int post_read_steps = 0;
1036 
1037 	bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES),
1038 								for_write);
1039 	if (!bio)
1040 		return ERR_PTR(-ENOMEM);
1041 
1042 	f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
1043 
1044 	f2fs_target_device(sbi, blkaddr, bio);
1045 	bio->bi_end_io = f2fs_read_end_io;
1046 	bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
1047 
1048 	if (fscrypt_inode_uses_fs_layer_crypto(inode))
1049 		post_read_steps |= 1 << STEP_DECRYPT;
1050 	if (f2fs_compressed_file(inode))
1051 		post_read_steps |= 1 << STEP_DECOMPRESS_NOWQ;
1052 	if (f2fs_need_verity(inode, first_idx))
1053 		post_read_steps |= 1 << STEP_VERITY;
1054 
1055 	if (post_read_steps) {
1056 		/* Due to the mempool, this never fails. */
1057 		ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
1058 		ctx->bio = bio;
1059 		ctx->sbi = sbi;
1060 		ctx->enabled_steps = post_read_steps;
1061 		bio->bi_private = ctx;
1062 	}
1063 
1064 	return bio;
1065 }
1066 
f2fs_release_read_bio(struct bio * bio)1067 static void f2fs_release_read_bio(struct bio *bio)
1068 {
1069 	if (bio->bi_private)
1070 		mempool_free(bio->bi_private, bio_post_read_ctx_pool);
1071 	bio_put(bio);
1072 }
1073 
1074 /* This can handle encryption stuffs */
f2fs_submit_page_read(struct inode * inode,struct page * page,block_t blkaddr,int op_flags,bool for_write)1075 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
1076 				 block_t blkaddr, int op_flags, bool for_write)
1077 {
1078 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1079 	struct bio *bio;
1080 
1081 	bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1082 					page->index, for_write);
1083 	if (IS_ERR(bio))
1084 		return PTR_ERR(bio);
1085 
1086 	/* wait for GCed page writeback via META_MAPPING */
1087 	f2fs_wait_on_block_writeback(inode, blkaddr);
1088 
1089 	if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1090 		bio_put(bio);
1091 		return -EFAULT;
1092 	}
1093 	ClearPageError(page);
1094 	inc_page_count(sbi, F2FS_RD_DATA);
1095 	f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
1096 	__submit_bio(sbi, bio, DATA);
1097 	return 0;
1098 }
1099 
__set_data_blkaddr(struct dnode_of_data * dn)1100 static void __set_data_blkaddr(struct dnode_of_data *dn)
1101 {
1102 	struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1103 	__le32 *addr_array;
1104 	int base = 0;
1105 
1106 	if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1107 		base = get_extra_isize(dn->inode);
1108 
1109 	/* Get physical address of data block */
1110 	addr_array = blkaddr_in_node(rn);
1111 	addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1112 }
1113 
1114 /*
1115  * Lock ordering for the change of data block address:
1116  * ->data_page
1117  *  ->node_page
1118  *    update block addresses in the node page
1119  */
f2fs_set_data_blkaddr(struct dnode_of_data * dn)1120 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1121 {
1122 	f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1123 	__set_data_blkaddr(dn);
1124 	if (set_page_dirty(dn->node_page))
1125 		dn->node_changed = true;
1126 }
1127 
f2fs_update_data_blkaddr(struct dnode_of_data * dn,block_t blkaddr)1128 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1129 {
1130 	dn->data_blkaddr = blkaddr;
1131 	f2fs_set_data_blkaddr(dn);
1132 	f2fs_update_extent_cache(dn);
1133 }
1134 
1135 /* dn->ofs_in_node will be returned with up-to-date last block pointer */
f2fs_reserve_new_blocks(struct dnode_of_data * dn,blkcnt_t count)1136 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1137 {
1138 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1139 	int err;
1140 
1141 	if (!count)
1142 		return 0;
1143 
1144 	if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1145 		return -EPERM;
1146 	if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1147 		return err;
1148 
1149 	trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1150 						dn->ofs_in_node, count);
1151 
1152 	f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
1153 
1154 	for (; count > 0; dn->ofs_in_node++) {
1155 		block_t blkaddr = f2fs_data_blkaddr(dn);
1156 		if (blkaddr == NULL_ADDR) {
1157 			dn->data_blkaddr = NEW_ADDR;
1158 			__set_data_blkaddr(dn);
1159 			count--;
1160 		}
1161 	}
1162 
1163 	if (set_page_dirty(dn->node_page))
1164 		dn->node_changed = true;
1165 	return 0;
1166 }
1167 
1168 /* Should keep dn->ofs_in_node unchanged */
f2fs_reserve_new_block(struct dnode_of_data * dn)1169 int f2fs_reserve_new_block(struct dnode_of_data *dn)
1170 {
1171 	unsigned int ofs_in_node = dn->ofs_in_node;
1172 	int ret;
1173 
1174 	ret = f2fs_reserve_new_blocks(dn, 1);
1175 	dn->ofs_in_node = ofs_in_node;
1176 	return ret;
1177 }
1178 
f2fs_reserve_block(struct dnode_of_data * dn,pgoff_t index)1179 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1180 {
1181 	bool need_put = dn->inode_page ? false : true;
1182 	int err;
1183 
1184 	err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1185 	if (err)
1186 		return err;
1187 
1188 	if (dn->data_blkaddr == NULL_ADDR)
1189 		err = f2fs_reserve_new_block(dn);
1190 	if (err || need_put)
1191 		f2fs_put_dnode(dn);
1192 	return err;
1193 }
1194 
f2fs_get_block(struct dnode_of_data * dn,pgoff_t index)1195 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1196 {
1197 	struct extent_info ei = {0, 0, 0};
1198 	struct inode *inode = dn->inode;
1199 
1200 	if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1201 		dn->data_blkaddr = ei.blk + index - ei.fofs;
1202 		return 0;
1203 	}
1204 
1205 	return f2fs_reserve_block(dn, index);
1206 }
1207 
f2fs_get_read_data_page(struct inode * inode,pgoff_t index,int op_flags,bool for_write)1208 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1209 						int op_flags, bool for_write)
1210 {
1211 	struct address_space *mapping = inode->i_mapping;
1212 	struct dnode_of_data dn;
1213 	struct page *page;
1214 	struct extent_info ei = {0,0,0};
1215 	int err;
1216 
1217 	page = f2fs_grab_cache_page(mapping, index, for_write);
1218 	if (!page)
1219 		return ERR_PTR(-ENOMEM);
1220 
1221 	if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1222 		dn.data_blkaddr = ei.blk + index - ei.fofs;
1223 		if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1224 						DATA_GENERIC_ENHANCE_READ)) {
1225 			err = -EFSCORRUPTED;
1226 			goto put_err;
1227 		}
1228 		goto got_it;
1229 	}
1230 
1231 	set_new_dnode(&dn, inode, NULL, NULL, 0);
1232 	err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1233 	if (err)
1234 		goto put_err;
1235 	f2fs_put_dnode(&dn);
1236 
1237 	if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1238 		err = -ENOENT;
1239 		goto put_err;
1240 	}
1241 	if (dn.data_blkaddr != NEW_ADDR &&
1242 			!f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1243 						dn.data_blkaddr,
1244 						DATA_GENERIC_ENHANCE)) {
1245 		err = -EFSCORRUPTED;
1246 		goto put_err;
1247 	}
1248 got_it:
1249 	if (PageUptodate(page)) {
1250 		unlock_page(page);
1251 		return page;
1252 	}
1253 
1254 	/*
1255 	 * A new dentry page is allocated but not able to be written, since its
1256 	 * new inode page couldn't be allocated due to -ENOSPC.
1257 	 * In such the case, its blkaddr can be remained as NEW_ADDR.
1258 	 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1259 	 * f2fs_init_inode_metadata.
1260 	 */
1261 	if (dn.data_blkaddr == NEW_ADDR) {
1262 		zero_user_segment(page, 0, PAGE_SIZE);
1263 		if (!PageUptodate(page))
1264 			SetPageUptodate(page);
1265 		unlock_page(page);
1266 		return page;
1267 	}
1268 
1269 	err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1270 						op_flags, for_write);
1271 	if (err)
1272 		goto put_err;
1273 	return page;
1274 
1275 put_err:
1276 	f2fs_put_page(page, 1);
1277 	return ERR_PTR(err);
1278 }
1279 
f2fs_find_data_page(struct inode * inode,pgoff_t index)1280 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1281 {
1282 	struct address_space *mapping = inode->i_mapping;
1283 	struct page *page;
1284 
1285 	page = find_get_page(mapping, index);
1286 	if (page && PageUptodate(page))
1287 		return page;
1288 	f2fs_put_page(page, 0);
1289 
1290 	page = f2fs_get_read_data_page(inode, index, 0, false);
1291 	if (IS_ERR(page))
1292 		return page;
1293 
1294 	if (PageUptodate(page))
1295 		return page;
1296 
1297 	wait_on_page_locked(page);
1298 	if (unlikely(!PageUptodate(page))) {
1299 		f2fs_put_page(page, 0);
1300 		return ERR_PTR(-EIO);
1301 	}
1302 	return page;
1303 }
1304 
1305 /*
1306  * If it tries to access a hole, return an error.
1307  * Because, the callers, functions in dir.c and GC, should be able to know
1308  * whether this page exists or not.
1309  */
f2fs_get_lock_data_page(struct inode * inode,pgoff_t index,bool for_write)1310 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1311 							bool for_write)
1312 {
1313 	struct address_space *mapping = inode->i_mapping;
1314 	struct page *page;
1315 repeat:
1316 	page = f2fs_get_read_data_page(inode, index, 0, for_write);
1317 	if (IS_ERR(page))
1318 		return page;
1319 
1320 	/* wait for read completion */
1321 	lock_page(page);
1322 	if (unlikely(page->mapping != mapping)) {
1323 		f2fs_put_page(page, 1);
1324 		goto repeat;
1325 	}
1326 	if (unlikely(!PageUptodate(page))) {
1327 		f2fs_put_page(page, 1);
1328 		return ERR_PTR(-EIO);
1329 	}
1330 	return page;
1331 }
1332 
1333 /*
1334  * Caller ensures that this data page is never allocated.
1335  * A new zero-filled data page is allocated in the page cache.
1336  *
1337  * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1338  * f2fs_unlock_op().
1339  * Note that, ipage is set only by make_empty_dir, and if any error occur,
1340  * ipage should be released by this function.
1341  */
f2fs_get_new_data_page(struct inode * inode,struct page * ipage,pgoff_t index,bool new_i_size)1342 struct page *f2fs_get_new_data_page(struct inode *inode,
1343 		struct page *ipage, pgoff_t index, bool new_i_size)
1344 {
1345 	struct address_space *mapping = inode->i_mapping;
1346 	struct page *page;
1347 	struct dnode_of_data dn;
1348 	int err;
1349 
1350 	page = f2fs_grab_cache_page(mapping, index, true);
1351 	if (!page) {
1352 		/*
1353 		 * before exiting, we should make sure ipage will be released
1354 		 * if any error occur.
1355 		 */
1356 		f2fs_put_page(ipage, 1);
1357 		return ERR_PTR(-ENOMEM);
1358 	}
1359 
1360 	set_new_dnode(&dn, inode, ipage, NULL, 0);
1361 	err = f2fs_reserve_block(&dn, index);
1362 	if (err) {
1363 		f2fs_put_page(page, 1);
1364 		return ERR_PTR(err);
1365 	}
1366 	if (!ipage)
1367 		f2fs_put_dnode(&dn);
1368 
1369 	if (PageUptodate(page))
1370 		goto got_it;
1371 
1372 	if (dn.data_blkaddr == NEW_ADDR) {
1373 		zero_user_segment(page, 0, PAGE_SIZE);
1374 		if (!PageUptodate(page))
1375 			SetPageUptodate(page);
1376 	} else {
1377 		f2fs_put_page(page, 1);
1378 
1379 		/* if ipage exists, blkaddr should be NEW_ADDR */
1380 		f2fs_bug_on(F2FS_I_SB(inode), ipage);
1381 		page = f2fs_get_lock_data_page(inode, index, true);
1382 		if (IS_ERR(page))
1383 			return page;
1384 	}
1385 got_it:
1386 	if (new_i_size && i_size_read(inode) <
1387 				((loff_t)(index + 1) << PAGE_SHIFT))
1388 		f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1389 	return page;
1390 }
1391 
__allocate_data_block(struct dnode_of_data * dn,int seg_type)1392 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1393 {
1394 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1395 	struct f2fs_summary sum;
1396 	struct node_info ni;
1397 	block_t old_blkaddr;
1398 	blkcnt_t count = 1;
1399 	int err;
1400 
1401 	if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1402 		return -EPERM;
1403 
1404 	err = f2fs_get_node_info(sbi, dn->nid, &ni);
1405 	if (err)
1406 		return err;
1407 
1408 	dn->data_blkaddr = f2fs_data_blkaddr(dn);
1409 	if (dn->data_blkaddr != NULL_ADDR)
1410 		goto alloc;
1411 
1412 	if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1413 		return err;
1414 
1415 alloc:
1416 	set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1417 	old_blkaddr = dn->data_blkaddr;
1418 	f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1419 				&sum, seg_type, NULL);
1420 	if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1421 		invalidate_mapping_pages(META_MAPPING(sbi),
1422 					old_blkaddr, old_blkaddr);
1423 	f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1424 
1425 	/*
1426 	 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1427 	 * data from unwritten block via dio_read.
1428 	 */
1429 	return 0;
1430 }
1431 
f2fs_preallocate_blocks(struct kiocb * iocb,struct iov_iter * from)1432 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1433 {
1434 	struct inode *inode = file_inode(iocb->ki_filp);
1435 	struct f2fs_map_blocks map;
1436 	int flag;
1437 	int err = 0;
1438 	bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1439 
1440 	map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1441 	map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1442 	if (map.m_len > map.m_lblk)
1443 		map.m_len -= map.m_lblk;
1444 	else
1445 		map.m_len = 0;
1446 
1447 	map.m_next_pgofs = NULL;
1448 	map.m_next_extent = NULL;
1449 	map.m_seg_type = NO_CHECK_TYPE;
1450 	map.m_may_create = true;
1451 
1452 	if (direct_io) {
1453 		map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1454 		flag = f2fs_force_buffered_io(inode, iocb, from) ?
1455 					F2FS_GET_BLOCK_PRE_AIO :
1456 					F2FS_GET_BLOCK_PRE_DIO;
1457 		goto map_blocks;
1458 	}
1459 	if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1460 		err = f2fs_convert_inline_inode(inode);
1461 		if (err)
1462 			return err;
1463 	}
1464 	if (f2fs_has_inline_data(inode))
1465 		return err;
1466 
1467 	flag = F2FS_GET_BLOCK_PRE_AIO;
1468 
1469 map_blocks:
1470 	err = f2fs_map_blocks(inode, &map, 1, flag);
1471 	if (map.m_len > 0 && err == -ENOSPC) {
1472 		if (!direct_io)
1473 			set_inode_flag(inode, FI_NO_PREALLOC);
1474 		err = 0;
1475 	}
1476 	return err;
1477 }
1478 
f2fs_do_map_lock(struct f2fs_sb_info * sbi,int flag,bool lock)1479 void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1480 {
1481 	if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1482 		if (lock)
1483 			down_read(&sbi->node_change);
1484 		else
1485 			up_read(&sbi->node_change);
1486 	} else {
1487 		if (lock)
1488 			f2fs_lock_op(sbi);
1489 		else
1490 			f2fs_unlock_op(sbi);
1491 	}
1492 }
1493 
1494 /*
1495  * f2fs_map_blocks() tries to find or build mapping relationship which
1496  * maps continuous logical blocks to physical blocks, and return such
1497  * info via f2fs_map_blocks structure.
1498  */
f2fs_map_blocks(struct inode * inode,struct f2fs_map_blocks * map,int create,int flag)1499 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1500 						int create, int flag)
1501 {
1502 	unsigned int maxblocks = map->m_len;
1503 	struct dnode_of_data dn;
1504 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1505 	int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1506 	pgoff_t pgofs, end_offset, end;
1507 	int err = 0, ofs = 1;
1508 	unsigned int ofs_in_node, last_ofs_in_node;
1509 	blkcnt_t prealloc;
1510 	struct extent_info ei = {0,0,0};
1511 	block_t blkaddr;
1512 	unsigned int start_pgofs;
1513 
1514 	if (!maxblocks)
1515 		return 0;
1516 
1517 	map->m_len = 0;
1518 	map->m_flags = 0;
1519 
1520 	/* it only supports block size == page size */
1521 	pgofs =	(pgoff_t)map->m_lblk;
1522 	end = pgofs + maxblocks;
1523 
1524 	if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1525 		if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1526 							map->m_may_create)
1527 			goto next_dnode;
1528 
1529 		map->m_pblk = ei.blk + pgofs - ei.fofs;
1530 		map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1531 		map->m_flags = F2FS_MAP_MAPPED;
1532 		if (map->m_next_extent)
1533 			*map->m_next_extent = pgofs + map->m_len;
1534 
1535 		/* for hardware encryption, but to avoid potential issue in future */
1536 		if (flag == F2FS_GET_BLOCK_DIO)
1537 			f2fs_wait_on_block_writeback_range(inode,
1538 						map->m_pblk, map->m_len);
1539 		goto out;
1540 	}
1541 
1542 next_dnode:
1543 	if (map->m_may_create)
1544 		f2fs_do_map_lock(sbi, flag, true);
1545 
1546 	/* When reading holes, we need its node page */
1547 	set_new_dnode(&dn, inode, NULL, NULL, 0);
1548 	err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1549 	if (err) {
1550 		if (flag == F2FS_GET_BLOCK_BMAP)
1551 			map->m_pblk = 0;
1552 		if (err == -ENOENT) {
1553 			err = 0;
1554 			if (map->m_next_pgofs)
1555 				*map->m_next_pgofs =
1556 					f2fs_get_next_page_offset(&dn, pgofs);
1557 			if (map->m_next_extent)
1558 				*map->m_next_extent =
1559 					f2fs_get_next_page_offset(&dn, pgofs);
1560 		}
1561 		goto unlock_out;
1562 	}
1563 
1564 	start_pgofs = pgofs;
1565 	prealloc = 0;
1566 	last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1567 	end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1568 
1569 next_block:
1570 	blkaddr = f2fs_data_blkaddr(&dn);
1571 
1572 	if (__is_valid_data_blkaddr(blkaddr) &&
1573 		!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1574 		err = -EFSCORRUPTED;
1575 		goto sync_out;
1576 	}
1577 
1578 	if (__is_valid_data_blkaddr(blkaddr)) {
1579 		/* use out-place-update for driect IO under LFS mode */
1580 		if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
1581 							map->m_may_create) {
1582 			err = __allocate_data_block(&dn, map->m_seg_type);
1583 			if (err)
1584 				goto sync_out;
1585 			blkaddr = dn.data_blkaddr;
1586 			set_inode_flag(inode, FI_APPEND_WRITE);
1587 		}
1588 	} else {
1589 		if (create) {
1590 			if (unlikely(f2fs_cp_error(sbi))) {
1591 				err = -EIO;
1592 				goto sync_out;
1593 			}
1594 			if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1595 				if (blkaddr == NULL_ADDR) {
1596 					prealloc++;
1597 					last_ofs_in_node = dn.ofs_in_node;
1598 				}
1599 			} else {
1600 				WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1601 					flag != F2FS_GET_BLOCK_DIO);
1602 				err = __allocate_data_block(&dn,
1603 							map->m_seg_type);
1604 				if (!err)
1605 					set_inode_flag(inode, FI_APPEND_WRITE);
1606 			}
1607 			if (err)
1608 				goto sync_out;
1609 			map->m_flags |= F2FS_MAP_NEW;
1610 			blkaddr = dn.data_blkaddr;
1611 		} else {
1612 			if (flag == F2FS_GET_BLOCK_BMAP) {
1613 				map->m_pblk = 0;
1614 				goto sync_out;
1615 			}
1616 			if (flag == F2FS_GET_BLOCK_PRECACHE)
1617 				goto sync_out;
1618 			if (flag == F2FS_GET_BLOCK_FIEMAP &&
1619 						blkaddr == NULL_ADDR) {
1620 				if (map->m_next_pgofs)
1621 					*map->m_next_pgofs = pgofs + 1;
1622 				goto sync_out;
1623 			}
1624 			if (flag != F2FS_GET_BLOCK_FIEMAP) {
1625 				/* for defragment case */
1626 				if (map->m_next_pgofs)
1627 					*map->m_next_pgofs = pgofs + 1;
1628 				goto sync_out;
1629 			}
1630 		}
1631 	}
1632 
1633 	if (flag == F2FS_GET_BLOCK_PRE_AIO)
1634 		goto skip;
1635 
1636 	if (map->m_len == 0) {
1637 		/* preallocated unwritten block should be mapped for fiemap. */
1638 		if (blkaddr == NEW_ADDR)
1639 			map->m_flags |= F2FS_MAP_UNWRITTEN;
1640 		map->m_flags |= F2FS_MAP_MAPPED;
1641 
1642 		map->m_pblk = blkaddr;
1643 		map->m_len = 1;
1644 	} else if ((map->m_pblk != NEW_ADDR &&
1645 			blkaddr == (map->m_pblk + ofs)) ||
1646 			(map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1647 			flag == F2FS_GET_BLOCK_PRE_DIO) {
1648 		ofs++;
1649 		map->m_len++;
1650 	} else {
1651 		goto sync_out;
1652 	}
1653 
1654 skip:
1655 	dn.ofs_in_node++;
1656 	pgofs++;
1657 
1658 	/* preallocate blocks in batch for one dnode page */
1659 	if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1660 			(pgofs == end || dn.ofs_in_node == end_offset)) {
1661 
1662 		dn.ofs_in_node = ofs_in_node;
1663 		err = f2fs_reserve_new_blocks(&dn, prealloc);
1664 		if (err)
1665 			goto sync_out;
1666 
1667 		map->m_len += dn.ofs_in_node - ofs_in_node;
1668 		if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1669 			err = -ENOSPC;
1670 			goto sync_out;
1671 		}
1672 		dn.ofs_in_node = end_offset;
1673 	}
1674 
1675 	if (pgofs >= end)
1676 		goto sync_out;
1677 	else if (dn.ofs_in_node < end_offset)
1678 		goto next_block;
1679 
1680 	if (flag == F2FS_GET_BLOCK_PRECACHE) {
1681 		if (map->m_flags & F2FS_MAP_MAPPED) {
1682 			unsigned int ofs = start_pgofs - map->m_lblk;
1683 
1684 			f2fs_update_extent_cache_range(&dn,
1685 				start_pgofs, map->m_pblk + ofs,
1686 				map->m_len - ofs);
1687 		}
1688 	}
1689 
1690 	f2fs_put_dnode(&dn);
1691 
1692 	if (map->m_may_create) {
1693 		f2fs_do_map_lock(sbi, flag, false);
1694 		f2fs_balance_fs(sbi, dn.node_changed);
1695 	}
1696 	goto next_dnode;
1697 
1698 sync_out:
1699 
1700 	/* for hardware encryption, but to avoid potential issue in future */
1701 	if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1702 		f2fs_wait_on_block_writeback_range(inode,
1703 						map->m_pblk, map->m_len);
1704 
1705 	if (flag == F2FS_GET_BLOCK_PRECACHE) {
1706 		if (map->m_flags & F2FS_MAP_MAPPED) {
1707 			unsigned int ofs = start_pgofs - map->m_lblk;
1708 
1709 			f2fs_update_extent_cache_range(&dn,
1710 				start_pgofs, map->m_pblk + ofs,
1711 				map->m_len - ofs);
1712 		}
1713 		if (map->m_next_extent)
1714 			*map->m_next_extent = pgofs + 1;
1715 	}
1716 	f2fs_put_dnode(&dn);
1717 unlock_out:
1718 	if (map->m_may_create) {
1719 		f2fs_do_map_lock(sbi, flag, false);
1720 		f2fs_balance_fs(sbi, dn.node_changed);
1721 	}
1722 out:
1723 	trace_f2fs_map_blocks(inode, map, err);
1724 	return err;
1725 }
1726 
f2fs_overwrite_io(struct inode * inode,loff_t pos,size_t len)1727 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1728 {
1729 	struct f2fs_map_blocks map;
1730 	block_t last_lblk;
1731 	int err;
1732 
1733 	if (pos + len > i_size_read(inode))
1734 		return false;
1735 
1736 	map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1737 	map.m_next_pgofs = NULL;
1738 	map.m_next_extent = NULL;
1739 	map.m_seg_type = NO_CHECK_TYPE;
1740 	map.m_may_create = false;
1741 	last_lblk = F2FS_BLK_ALIGN(pos + len);
1742 
1743 	while (map.m_lblk < last_lblk) {
1744 		map.m_len = last_lblk - map.m_lblk;
1745 		err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1746 		if (err || map.m_len == 0)
1747 			return false;
1748 		map.m_lblk += map.m_len;
1749 	}
1750 	return true;
1751 }
1752 
__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)1753 static int __get_data_block(struct inode *inode, sector_t iblock,
1754 			struct buffer_head *bh, int create, int flag,
1755 			pgoff_t *next_pgofs, int seg_type, bool may_write)
1756 {
1757 	struct f2fs_map_blocks map;
1758 	int err;
1759 
1760 	map.m_lblk = iblock;
1761 	map.m_len = bh->b_size >> inode->i_blkbits;
1762 	map.m_next_pgofs = next_pgofs;
1763 	map.m_next_extent = NULL;
1764 	map.m_seg_type = seg_type;
1765 	map.m_may_create = may_write;
1766 
1767 	err = f2fs_map_blocks(inode, &map, create, flag);
1768 	if (!err) {
1769 		map_bh(bh, inode->i_sb, map.m_pblk);
1770 		bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1771 		bh->b_size = (u64)map.m_len << inode->i_blkbits;
1772 	}
1773 	return err;
1774 }
1775 
get_data_block(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create,int flag,pgoff_t * next_pgofs)1776 static int get_data_block(struct inode *inode, sector_t iblock,
1777 			struct buffer_head *bh_result, int create, int flag,
1778 			pgoff_t *next_pgofs)
1779 {
1780 	return __get_data_block(inode, iblock, bh_result, create,
1781 							flag, next_pgofs,
1782 							NO_CHECK_TYPE, create);
1783 }
1784 
get_data_block_dio_write(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)1785 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1786 			struct buffer_head *bh_result, int create)
1787 {
1788 	return __get_data_block(inode, iblock, bh_result, create,
1789 				F2FS_GET_BLOCK_DIO, NULL,
1790 				f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1791 				IS_SWAPFILE(inode) ? false : true);
1792 }
1793 
get_data_block_dio(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)1794 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1795 			struct buffer_head *bh_result, int create)
1796 {
1797 	return __get_data_block(inode, iblock, bh_result, create,
1798 				F2FS_GET_BLOCK_DIO, NULL,
1799 				f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1800 				false);
1801 }
1802 
get_data_block_bmap(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)1803 static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1804 			struct buffer_head *bh_result, int create)
1805 {
1806 	return __get_data_block(inode, iblock, bh_result, create,
1807 						F2FS_GET_BLOCK_BMAP, NULL,
1808 						NO_CHECK_TYPE, create);
1809 }
1810 
logical_to_blk(struct inode * inode,loff_t offset)1811 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1812 {
1813 	return (offset >> inode->i_blkbits);
1814 }
1815 
blk_to_logical(struct inode * inode,sector_t blk)1816 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1817 {
1818 	return (blk << inode->i_blkbits);
1819 }
1820 
f2fs_xattr_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo)1821 static int f2fs_xattr_fiemap(struct inode *inode,
1822 				struct fiemap_extent_info *fieinfo)
1823 {
1824 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1825 	struct page *page;
1826 	struct node_info ni;
1827 	__u64 phys = 0, len;
1828 	__u32 flags;
1829 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1830 	int err = 0;
1831 
1832 	if (f2fs_has_inline_xattr(inode)) {
1833 		int offset;
1834 
1835 		page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1836 						inode->i_ino, false);
1837 		if (!page)
1838 			return -ENOMEM;
1839 
1840 		err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1841 		if (err) {
1842 			f2fs_put_page(page, 1);
1843 			return err;
1844 		}
1845 
1846 		phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1847 		offset = offsetof(struct f2fs_inode, i_addr) +
1848 					sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1849 					get_inline_xattr_addrs(inode));
1850 
1851 		phys += offset;
1852 		len = inline_xattr_size(inode);
1853 
1854 		f2fs_put_page(page, 1);
1855 
1856 		flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1857 
1858 		if (!xnid)
1859 			flags |= FIEMAP_EXTENT_LAST;
1860 
1861 		err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1862 		trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1863 		if (err || err == 1)
1864 			return err;
1865 	}
1866 
1867 	if (xnid) {
1868 		page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1869 		if (!page)
1870 			return -ENOMEM;
1871 
1872 		err = f2fs_get_node_info(sbi, xnid, &ni);
1873 		if (err) {
1874 			f2fs_put_page(page, 1);
1875 			return err;
1876 		}
1877 
1878 		phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1879 		len = inode->i_sb->s_blocksize;
1880 
1881 		f2fs_put_page(page, 1);
1882 
1883 		flags = FIEMAP_EXTENT_LAST;
1884 	}
1885 
1886 	if (phys) {
1887 		err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1888 		trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1889 	}
1890 
1891 	return (err < 0 ? err : 0);
1892 }
1893 
max_inode_blocks(struct inode * inode)1894 static loff_t max_inode_blocks(struct inode *inode)
1895 {
1896 	loff_t result = ADDRS_PER_INODE(inode);
1897 	loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1898 
1899 	/* two direct node blocks */
1900 	result += (leaf_count * 2);
1901 
1902 	/* two indirect node blocks */
1903 	leaf_count *= NIDS_PER_BLOCK;
1904 	result += (leaf_count * 2);
1905 
1906 	/* one double indirect node block */
1907 	leaf_count *= NIDS_PER_BLOCK;
1908 	result += leaf_count;
1909 
1910 	return result;
1911 }
1912 
f2fs_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,u64 start,u64 len)1913 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1914 		u64 start, u64 len)
1915 {
1916 	struct buffer_head map_bh;
1917 	sector_t start_blk, last_blk;
1918 	pgoff_t next_pgofs;
1919 	u64 logical = 0, phys = 0, size = 0;
1920 	u32 flags = 0;
1921 	int ret = 0;
1922 	bool compr_cluster = false;
1923 	unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
1924 
1925 	if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1926 		ret = f2fs_precache_extents(inode);
1927 		if (ret)
1928 			return ret;
1929 	}
1930 
1931 	ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
1932 	if (ret)
1933 		return ret;
1934 
1935 	inode_lock(inode);
1936 
1937 	if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1938 		ret = f2fs_xattr_fiemap(inode, fieinfo);
1939 		goto out;
1940 	}
1941 
1942 	if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1943 		ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1944 		if (ret != -EAGAIN)
1945 			goto out;
1946 	}
1947 
1948 	if (logical_to_blk(inode, len) == 0)
1949 		len = blk_to_logical(inode, 1);
1950 
1951 	start_blk = logical_to_blk(inode, start);
1952 	last_blk = logical_to_blk(inode, start + len - 1);
1953 
1954 next:
1955 	memset(&map_bh, 0, sizeof(struct buffer_head));
1956 	map_bh.b_size = len;
1957 
1958 	if (compr_cluster)
1959 		map_bh.b_size = blk_to_logical(inode, cluster_size - 1);
1960 
1961 	ret = get_data_block(inode, start_blk, &map_bh, 0,
1962 					F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1963 	if (ret)
1964 		goto out;
1965 
1966 	/* HOLE */
1967 	if (!buffer_mapped(&map_bh)) {
1968 		start_blk = next_pgofs;
1969 
1970 		if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1971 						max_inode_blocks(inode)))
1972 			goto prep_next;
1973 
1974 		flags |= FIEMAP_EXTENT_LAST;
1975 	}
1976 
1977 	if (size) {
1978 		if (IS_ENCRYPTED(inode))
1979 			flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1980 
1981 		ret = fiemap_fill_next_extent(fieinfo, logical,
1982 				phys, size, flags);
1983 		trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1984 		if (ret)
1985 			goto out;
1986 		size = 0;
1987 	}
1988 
1989 	if (start_blk > last_blk)
1990 		goto out;
1991 
1992 	if (compr_cluster) {
1993 		compr_cluster = false;
1994 
1995 
1996 		logical = blk_to_logical(inode, start_blk - 1);
1997 		phys = blk_to_logical(inode, map_bh.b_blocknr);
1998 		size = blk_to_logical(inode, cluster_size);
1999 
2000 		flags |= FIEMAP_EXTENT_ENCODED;
2001 
2002 		start_blk += cluster_size - 1;
2003 
2004 		if (start_blk > last_blk)
2005 			goto out;
2006 
2007 		goto prep_next;
2008 	}
2009 
2010 	if (map_bh.b_blocknr == COMPRESS_ADDR) {
2011 		compr_cluster = true;
2012 		start_blk++;
2013 		goto prep_next;
2014 	}
2015 
2016 	logical = blk_to_logical(inode, start_blk);
2017 	phys = blk_to_logical(inode, map_bh.b_blocknr);
2018 	size = map_bh.b_size;
2019 	flags = 0;
2020 	if (buffer_unwritten(&map_bh))
2021 		flags = FIEMAP_EXTENT_UNWRITTEN;
2022 
2023 	start_blk += logical_to_blk(inode, size);
2024 
2025 prep_next:
2026 	cond_resched();
2027 	if (fatal_signal_pending(current))
2028 		ret = -EINTR;
2029 	else
2030 		goto next;
2031 out:
2032 	if (ret == 1)
2033 		ret = 0;
2034 
2035 	inode_unlock(inode);
2036 	return ret;
2037 }
2038 
f2fs_readpage_limit(struct inode * inode)2039 static inline loff_t f2fs_readpage_limit(struct inode *inode)
2040 {
2041 	if (IS_ENABLED(CONFIG_FS_VERITY) &&
2042 	    (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
2043 		return inode->i_sb->s_maxbytes;
2044 
2045 	return i_size_read(inode);
2046 }
2047 
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)2048 static int f2fs_read_single_page(struct inode *inode, struct page *page,
2049 					unsigned nr_pages,
2050 					struct f2fs_map_blocks *map,
2051 					struct bio **bio_ret,
2052 					sector_t *last_block_in_bio,
2053 					bool is_readahead)
2054 {
2055 	struct bio *bio = *bio_ret;
2056 	const unsigned blkbits = inode->i_blkbits;
2057 	const unsigned blocksize = 1 << blkbits;
2058 	sector_t block_in_file;
2059 	sector_t last_block;
2060 	sector_t last_block_in_file;
2061 	sector_t block_nr;
2062 	int ret = 0;
2063 
2064 	block_in_file = (sector_t)page_index(page);
2065 	last_block = block_in_file + nr_pages;
2066 	last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
2067 							blkbits;
2068 	if (last_block > last_block_in_file)
2069 		last_block = last_block_in_file;
2070 
2071 	/* just zeroing out page which is beyond EOF */
2072 	if (block_in_file >= last_block)
2073 		goto zero_out;
2074 	/*
2075 	 * Map blocks using the previous result first.
2076 	 */
2077 	if ((map->m_flags & F2FS_MAP_MAPPED) &&
2078 			block_in_file > map->m_lblk &&
2079 			block_in_file < (map->m_lblk + map->m_len))
2080 		goto got_it;
2081 
2082 	/*
2083 	 * Then do more f2fs_map_blocks() calls until we are
2084 	 * done with this page.
2085 	 */
2086 	map->m_lblk = block_in_file;
2087 	map->m_len = last_block - block_in_file;
2088 
2089 	ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2090 	if (ret)
2091 		goto out;
2092 got_it:
2093 	if ((map->m_flags & F2FS_MAP_MAPPED)) {
2094 		block_nr = map->m_pblk + block_in_file - map->m_lblk;
2095 		SetPageMappedToDisk(page);
2096 
2097 		if (!PageUptodate(page) && (!PageSwapCache(page) &&
2098 					!cleancache_get_page(page))) {
2099 			SetPageUptodate(page);
2100 			goto confused;
2101 		}
2102 
2103 		if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2104 						DATA_GENERIC_ENHANCE_READ)) {
2105 			ret = -EFSCORRUPTED;
2106 			goto out;
2107 		}
2108 	} else {
2109 zero_out:
2110 		zero_user_segment(page, 0, PAGE_SIZE);
2111 		if (f2fs_need_verity(inode, page->index) &&
2112 		    !fsverity_verify_page(page)) {
2113 			ret = -EIO;
2114 			goto out;
2115 		}
2116 		if (!PageUptodate(page))
2117 			SetPageUptodate(page);
2118 		unlock_page(page);
2119 		goto out;
2120 	}
2121 
2122 	/*
2123 	 * This page will go to BIO.  Do we need to send this
2124 	 * BIO off first?
2125 	 */
2126 	if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2127 				       *last_block_in_bio, block_nr) ||
2128 		    !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2129 submit_and_realloc:
2130 		__submit_bio(F2FS_I_SB(inode), bio, DATA);
2131 		bio = NULL;
2132 	}
2133 	if (bio == NULL) {
2134 		bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
2135 				is_readahead ? REQ_RAHEAD : 0, page->index,
2136 				false);
2137 		if (IS_ERR(bio)) {
2138 			ret = PTR_ERR(bio);
2139 			bio = NULL;
2140 			goto out;
2141 		}
2142 	}
2143 
2144 	/*
2145 	 * If the page is under writeback, we need to wait for
2146 	 * its completion to see the correct decrypted data.
2147 	 */
2148 	f2fs_wait_on_block_writeback(inode, block_nr);
2149 
2150 	if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2151 		goto submit_and_realloc;
2152 
2153 	inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
2154 	f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
2155 	ClearPageError(page);
2156 	*last_block_in_bio = block_nr;
2157 	goto out;
2158 confused:
2159 	if (bio) {
2160 		__submit_bio(F2FS_I_SB(inode), bio, DATA);
2161 		bio = NULL;
2162 	}
2163 	unlock_page(page);
2164 out:
2165 	*bio_ret = bio;
2166 	return ret;
2167 }
2168 
2169 #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)2170 int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2171 				unsigned nr_pages, sector_t *last_block_in_bio,
2172 				bool is_readahead, bool for_write)
2173 {
2174 	struct dnode_of_data dn;
2175 	struct inode *inode = cc->inode;
2176 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2177 	struct bio *bio = *bio_ret;
2178 	unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2179 	sector_t last_block_in_file;
2180 	const unsigned blkbits = inode->i_blkbits;
2181 	const unsigned blocksize = 1 << blkbits;
2182 	struct decompress_io_ctx *dic = NULL;
2183 	int i;
2184 	int ret = 0;
2185 
2186 	f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2187 
2188 	last_block_in_file = (f2fs_readpage_limit(inode) +
2189 					blocksize - 1) >> blkbits;
2190 
2191 	/* get rid of pages beyond EOF */
2192 	for (i = 0; i < cc->cluster_size; i++) {
2193 		struct page *page = cc->rpages[i];
2194 
2195 		if (!page)
2196 			continue;
2197 		if ((sector_t)page->index >= last_block_in_file) {
2198 			zero_user_segment(page, 0, PAGE_SIZE);
2199 			if (!PageUptodate(page))
2200 				SetPageUptodate(page);
2201 		} else if (!PageUptodate(page)) {
2202 			continue;
2203 		}
2204 		unlock_page(page);
2205 		cc->rpages[i] = NULL;
2206 		cc->nr_rpages--;
2207 	}
2208 
2209 	/* we are done since all pages are beyond EOF */
2210 	if (f2fs_cluster_is_empty(cc))
2211 		goto out;
2212 
2213 	set_new_dnode(&dn, inode, NULL, NULL, 0);
2214 	ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2215 	if (ret)
2216 		goto out;
2217 
2218 	f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2219 
2220 	for (i = 1; i < cc->cluster_size; i++) {
2221 		block_t blkaddr;
2222 
2223 		blkaddr = data_blkaddr(dn.inode, dn.node_page,
2224 						dn.ofs_in_node + i);
2225 
2226 		if (!__is_valid_data_blkaddr(blkaddr))
2227 			break;
2228 
2229 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
2230 			ret = -EFAULT;
2231 			goto out_put_dnode;
2232 		}
2233 		cc->nr_cpages++;
2234 	}
2235 
2236 	/* nothing to decompress */
2237 	if (cc->nr_cpages == 0) {
2238 		ret = 0;
2239 		goto out_put_dnode;
2240 	}
2241 
2242 	dic = f2fs_alloc_dic(cc);
2243 	if (IS_ERR(dic)) {
2244 		ret = PTR_ERR(dic);
2245 		goto out_put_dnode;
2246 	}
2247 
2248 	for (i = 0; i < dic->nr_cpages; i++) {
2249 		struct page *page = dic->cpages[i];
2250 		block_t blkaddr;
2251 		struct bio_post_read_ctx *ctx;
2252 
2253 		blkaddr = data_blkaddr(dn.inode, dn.node_page,
2254 						dn.ofs_in_node + i + 1);
2255 
2256 		if (bio && (!page_is_mergeable(sbi, bio,
2257 					*last_block_in_bio, blkaddr) ||
2258 		    !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2259 submit_and_realloc:
2260 			__submit_bio(sbi, bio, DATA);
2261 			bio = NULL;
2262 		}
2263 
2264 		if (!bio) {
2265 			bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2266 					is_readahead ? REQ_RAHEAD : 0,
2267 					page->index, for_write);
2268 			if (IS_ERR(bio)) {
2269 				ret = PTR_ERR(bio);
2270 				dic->failed = true;
2271 				if (!atomic_sub_return(dic->nr_cpages - i,
2272 							&dic->pending_pages)) {
2273 					f2fs_decompress_end_io(dic->rpages,
2274 							cc->cluster_size, true,
2275 							false);
2276 					f2fs_free_dic(dic);
2277 				}
2278 				f2fs_put_dnode(&dn);
2279 				*bio_ret = NULL;
2280 				return ret;
2281 			}
2282 		}
2283 
2284 		f2fs_wait_on_block_writeback(inode, blkaddr);
2285 
2286 		if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2287 			goto submit_and_realloc;
2288 
2289 		/* tag STEP_DECOMPRESS to handle IO in wq */
2290 		ctx = bio->bi_private;
2291 		if (!(ctx->enabled_steps & (1 << STEP_DECOMPRESS)))
2292 			ctx->enabled_steps |= 1 << STEP_DECOMPRESS;
2293 
2294 		inc_page_count(sbi, F2FS_RD_DATA);
2295 		f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2296 		f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2297 		ClearPageError(page);
2298 		*last_block_in_bio = blkaddr;
2299 	}
2300 
2301 	f2fs_put_dnode(&dn);
2302 
2303 	*bio_ret = bio;
2304 	return 0;
2305 
2306 out_put_dnode:
2307 	f2fs_put_dnode(&dn);
2308 out:
2309 	f2fs_decompress_end_io(cc->rpages, cc->cluster_size, true, false);
2310 	*bio_ret = bio;
2311 	return ret;
2312 }
2313 #endif
2314 
2315 /*
2316  * This function was originally taken from fs/mpage.c, and customized for f2fs.
2317  * Major change was from block_size == page_size in f2fs by default.
2318  *
2319  * Note that the aops->readpages() function is ONLY used for read-ahead. If
2320  * this function ever deviates from doing just read-ahead, it should either
2321  * use ->readpage() or do the necessary surgery to decouple ->readpages()
2322  * from read-ahead.
2323  */
f2fs_mpage_readpages(struct inode * inode,struct readahead_control * rac,struct page * page)2324 static int f2fs_mpage_readpages(struct inode *inode,
2325 		struct readahead_control *rac, struct page *page)
2326 {
2327 	struct bio *bio = NULL;
2328 	sector_t last_block_in_bio = 0;
2329 	struct f2fs_map_blocks map;
2330 #ifdef CONFIG_F2FS_FS_COMPRESSION
2331 	struct compress_ctx cc = {
2332 		.inode = inode,
2333 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2334 		.cluster_size = F2FS_I(inode)->i_cluster_size,
2335 		.cluster_idx = NULL_CLUSTER,
2336 		.rpages = NULL,
2337 		.cpages = NULL,
2338 		.nr_rpages = 0,
2339 		.nr_cpages = 0,
2340 	};
2341 #endif
2342 	unsigned nr_pages = rac ? readahead_count(rac) : 1;
2343 	unsigned max_nr_pages = nr_pages;
2344 	int ret = 0;
2345 	bool drop_ra = false;
2346 
2347 	map.m_pblk = 0;
2348 	map.m_lblk = 0;
2349 	map.m_len = 0;
2350 	map.m_flags = 0;
2351 	map.m_next_pgofs = NULL;
2352 	map.m_next_extent = NULL;
2353 	map.m_seg_type = NO_CHECK_TYPE;
2354 	map.m_may_create = false;
2355 
2356 	/*
2357 	 * Two readahead threads for same address range can cause race condition
2358 	 * which fragments sequential read IOs. So let's avoid each other.
2359 	 */
2360 	if (rac && readahead_count(rac)) {
2361 		if (READ_ONCE(F2FS_I(inode)->ra_offset) == readahead_index(rac))
2362 			drop_ra = true;
2363 		else
2364 			WRITE_ONCE(F2FS_I(inode)->ra_offset,
2365 						readahead_index(rac));
2366 	}
2367 
2368 	for (; nr_pages; nr_pages--) {
2369 		if (rac) {
2370 			page = readahead_page(rac);
2371 			prefetchw(&page->flags);
2372 			if (drop_ra) {
2373 				f2fs_put_page(page, 1);
2374 				continue;
2375 			}
2376 		}
2377 
2378 #ifdef CONFIG_F2FS_FS_COMPRESSION
2379 		if (f2fs_compressed_file(inode)) {
2380 			/* there are remained comressed pages, submit them */
2381 			if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2382 				ret = f2fs_read_multi_pages(&cc, &bio,
2383 							max_nr_pages,
2384 							&last_block_in_bio,
2385 							rac != NULL, false);
2386 				f2fs_destroy_compress_ctx(&cc);
2387 				if (ret)
2388 					goto set_error_page;
2389 			}
2390 			ret = f2fs_is_compressed_cluster(inode, page->index);
2391 			if (ret < 0)
2392 				goto set_error_page;
2393 			else if (!ret)
2394 				goto read_single_page;
2395 
2396 			ret = f2fs_init_compress_ctx(&cc);
2397 			if (ret)
2398 				goto set_error_page;
2399 
2400 			f2fs_compress_ctx_add_page(&cc, page);
2401 
2402 			goto next_page;
2403 		}
2404 read_single_page:
2405 #endif
2406 
2407 		ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2408 					&bio, &last_block_in_bio, rac);
2409 		if (ret) {
2410 #ifdef CONFIG_F2FS_FS_COMPRESSION
2411 set_error_page:
2412 #endif
2413 			SetPageError(page);
2414 			zero_user_segment(page, 0, PAGE_SIZE);
2415 			unlock_page(page);
2416 		}
2417 #ifdef CONFIG_F2FS_FS_COMPRESSION
2418 next_page:
2419 #endif
2420 		if (rac)
2421 			put_page(page);
2422 
2423 #ifdef CONFIG_F2FS_FS_COMPRESSION
2424 		if (f2fs_compressed_file(inode)) {
2425 			/* last page */
2426 			if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2427 				ret = f2fs_read_multi_pages(&cc, &bio,
2428 							max_nr_pages,
2429 							&last_block_in_bio,
2430 							rac != NULL, false);
2431 				f2fs_destroy_compress_ctx(&cc);
2432 			}
2433 		}
2434 #endif
2435 	}
2436 	if (bio)
2437 		__submit_bio(F2FS_I_SB(inode), bio, DATA);
2438 
2439 	if (rac && readahead_count(rac) && !drop_ra)
2440 		WRITE_ONCE(F2FS_I(inode)->ra_offset, -1);
2441 	return ret;
2442 }
2443 
f2fs_read_data_page(struct file * file,struct page * page)2444 static int f2fs_read_data_page(struct file *file, struct page *page)
2445 {
2446 	struct inode *inode = page_file_mapping(page)->host;
2447 	int ret = -EAGAIN;
2448 
2449 	trace_f2fs_readpage(page, DATA);
2450 
2451 	if (!f2fs_is_compress_backend_ready(inode)) {
2452 		unlock_page(page);
2453 		return -EOPNOTSUPP;
2454 	}
2455 
2456 	/* If the file has inline data, try to read it directly */
2457 	if (f2fs_has_inline_data(inode))
2458 		ret = f2fs_read_inline_data(inode, page);
2459 	if (ret == -EAGAIN)
2460 		ret = f2fs_mpage_readpages(inode, NULL, page);
2461 	return ret;
2462 }
2463 
f2fs_readahead(struct readahead_control * rac)2464 static void f2fs_readahead(struct readahead_control *rac)
2465 {
2466 	struct inode *inode = rac->mapping->host;
2467 
2468 	trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2469 
2470 	if (!f2fs_is_compress_backend_ready(inode))
2471 		return;
2472 
2473 	/* If the file has inline data, skip readpages */
2474 	if (f2fs_has_inline_data(inode))
2475 		return;
2476 
2477 	f2fs_mpage_readpages(inode, rac, NULL);
2478 }
2479 
f2fs_encrypt_one_page(struct f2fs_io_info * fio)2480 int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
2481 {
2482 	struct inode *inode = fio->page->mapping->host;
2483 	struct page *mpage, *page;
2484 	gfp_t gfp_flags = GFP_NOFS;
2485 
2486 	if (!f2fs_encrypted_file(inode))
2487 		return 0;
2488 
2489 	page = fio->compressed_page ? fio->compressed_page : fio->page;
2490 
2491 	/* wait for GCed page writeback via META_MAPPING */
2492 	f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2493 
2494 	if (fscrypt_inode_uses_inline_crypto(inode))
2495 		return 0;
2496 
2497 retry_encrypt:
2498 	fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2499 					PAGE_SIZE, 0, gfp_flags);
2500 	if (IS_ERR(fio->encrypted_page)) {
2501 		/* flush pending IOs and wait for a while in the ENOMEM case */
2502 		if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2503 			f2fs_flush_merged_writes(fio->sbi);
2504 			congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
2505 			gfp_flags |= __GFP_NOFAIL;
2506 			goto retry_encrypt;
2507 		}
2508 		return PTR_ERR(fio->encrypted_page);
2509 	}
2510 
2511 	mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2512 	if (mpage) {
2513 		if (PageUptodate(mpage))
2514 			memcpy(page_address(mpage),
2515 				page_address(fio->encrypted_page), PAGE_SIZE);
2516 		f2fs_put_page(mpage, 1);
2517 	}
2518 	return 0;
2519 }
2520 
check_inplace_update_policy(struct inode * inode,struct f2fs_io_info * fio)2521 static inline bool check_inplace_update_policy(struct inode *inode,
2522 				struct f2fs_io_info *fio)
2523 {
2524 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2525 	unsigned int policy = SM_I(sbi)->ipu_policy;
2526 
2527 	if (policy & (0x1 << F2FS_IPU_FORCE))
2528 		return true;
2529 	if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2530 		return true;
2531 	if (policy & (0x1 << F2FS_IPU_UTIL) &&
2532 			utilization(sbi) > SM_I(sbi)->min_ipu_util)
2533 		return true;
2534 	if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2535 			utilization(sbi) > SM_I(sbi)->min_ipu_util)
2536 		return true;
2537 
2538 	/*
2539 	 * IPU for rewrite async pages
2540 	 */
2541 	if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2542 			fio && fio->op == REQ_OP_WRITE &&
2543 			!(fio->op_flags & REQ_SYNC) &&
2544 			!IS_ENCRYPTED(inode))
2545 		return true;
2546 
2547 	/* this is only set during fdatasync */
2548 	if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2549 			is_inode_flag_set(inode, FI_NEED_IPU))
2550 		return true;
2551 
2552 	if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2553 			!f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2554 		return true;
2555 
2556 	return false;
2557 }
2558 
f2fs_should_update_inplace(struct inode * inode,struct f2fs_io_info * fio)2559 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2560 {
2561 	if (f2fs_is_pinned_file(inode))
2562 		return true;
2563 
2564 	/* if this is cold file, we should overwrite to avoid fragmentation */
2565 	if (file_is_cold(inode))
2566 		return true;
2567 
2568 	return check_inplace_update_policy(inode, fio);
2569 }
2570 
f2fs_should_update_outplace(struct inode * inode,struct f2fs_io_info * fio)2571 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2572 {
2573 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2574 
2575 	if (f2fs_lfs_mode(sbi))
2576 		return true;
2577 	if (S_ISDIR(inode->i_mode))
2578 		return true;
2579 	if (IS_NOQUOTA(inode))
2580 		return true;
2581 	if (f2fs_is_atomic_file(inode))
2582 		return true;
2583 	if (fio) {
2584 		if (is_cold_data(fio->page))
2585 			return true;
2586 		if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2587 			return true;
2588 		if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2589 			f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2590 			return true;
2591 	}
2592 	return false;
2593 }
2594 
need_inplace_update(struct f2fs_io_info * fio)2595 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2596 {
2597 	struct inode *inode = fio->page->mapping->host;
2598 
2599 	if (f2fs_should_update_outplace(inode, fio))
2600 		return false;
2601 
2602 	return f2fs_should_update_inplace(inode, fio);
2603 }
2604 
f2fs_do_write_data_page(struct f2fs_io_info * fio)2605 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2606 {
2607 	struct page *page = fio->page;
2608 	struct inode *inode = page->mapping->host;
2609 	struct dnode_of_data dn;
2610 	struct extent_info ei = {0,0,0};
2611 	struct node_info ni;
2612 	bool ipu_force = false;
2613 	int err = 0;
2614 
2615 	set_new_dnode(&dn, inode, NULL, NULL, 0);
2616 	if (need_inplace_update(fio) &&
2617 			f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2618 		fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2619 
2620 		if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2621 						DATA_GENERIC_ENHANCE))
2622 			return -EFSCORRUPTED;
2623 
2624 		ipu_force = true;
2625 		fio->need_lock = LOCK_DONE;
2626 		goto got_it;
2627 	}
2628 
2629 	/* Deadlock due to between page->lock and f2fs_lock_op */
2630 	if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2631 		return -EAGAIN;
2632 
2633 	err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2634 	if (err)
2635 		goto out;
2636 
2637 	fio->old_blkaddr = dn.data_blkaddr;
2638 
2639 	/* This page is already truncated */
2640 	if (fio->old_blkaddr == NULL_ADDR) {
2641 		ClearPageUptodate(page);
2642 		clear_cold_data(page);
2643 		goto out_writepage;
2644 	}
2645 got_it:
2646 	if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2647 		!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2648 						DATA_GENERIC_ENHANCE)) {
2649 		err = -EFSCORRUPTED;
2650 		goto out_writepage;
2651 	}
2652 	/*
2653 	 * If current allocation needs SSR,
2654 	 * it had better in-place writes for updated data.
2655 	 */
2656 	if (ipu_force ||
2657 		(__is_valid_data_blkaddr(fio->old_blkaddr) &&
2658 					need_inplace_update(fio))) {
2659 		err = f2fs_encrypt_one_page(fio);
2660 		if (err)
2661 			goto out_writepage;
2662 
2663 		set_page_writeback(page);
2664 		ClearPageError(page);
2665 		f2fs_put_dnode(&dn);
2666 		if (fio->need_lock == LOCK_REQ)
2667 			f2fs_unlock_op(fio->sbi);
2668 		err = f2fs_inplace_write_data(fio);
2669 		if (err) {
2670 			if (fscrypt_inode_uses_fs_layer_crypto(inode))
2671 				fscrypt_finalize_bounce_page(&fio->encrypted_page);
2672 			if (PageWriteback(page))
2673 				end_page_writeback(page);
2674 		} else {
2675 			set_inode_flag(inode, FI_UPDATE_WRITE);
2676 		}
2677 		trace_f2fs_do_write_data_page(fio->page, IPU);
2678 		return err;
2679 	}
2680 
2681 	if (fio->need_lock == LOCK_RETRY) {
2682 		if (!f2fs_trylock_op(fio->sbi)) {
2683 			err = -EAGAIN;
2684 			goto out_writepage;
2685 		}
2686 		fio->need_lock = LOCK_REQ;
2687 	}
2688 
2689 	err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2690 	if (err)
2691 		goto out_writepage;
2692 
2693 	fio->version = ni.version;
2694 
2695 	err = f2fs_encrypt_one_page(fio);
2696 	if (err)
2697 		goto out_writepage;
2698 
2699 	set_page_writeback(page);
2700 	ClearPageError(page);
2701 
2702 	if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2703 		f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2704 
2705 	/* LFS mode write path */
2706 	f2fs_outplace_write_data(&dn, fio);
2707 	trace_f2fs_do_write_data_page(page, OPU);
2708 	set_inode_flag(inode, FI_APPEND_WRITE);
2709 	if (page->index == 0)
2710 		set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2711 out_writepage:
2712 	f2fs_put_dnode(&dn);
2713 out:
2714 	if (fio->need_lock == LOCK_REQ)
2715 		f2fs_unlock_op(fio->sbi);
2716 	return err;
2717 }
2718 
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)2719 int f2fs_write_single_data_page(struct page *page, int *submitted,
2720 				struct bio **bio,
2721 				sector_t *last_block,
2722 				struct writeback_control *wbc,
2723 				enum iostat_type io_type,
2724 				int compr_blocks)
2725 {
2726 	struct inode *inode = page->mapping->host;
2727 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2728 	loff_t i_size = i_size_read(inode);
2729 	const pgoff_t end_index = ((unsigned long long)i_size)
2730 							>> PAGE_SHIFT;
2731 	loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2732 	unsigned offset = 0;
2733 	bool need_balance_fs = false;
2734 	int err = 0;
2735 	struct f2fs_io_info fio = {
2736 		.sbi = sbi,
2737 		.ino = inode->i_ino,
2738 		.type = DATA,
2739 		.op = REQ_OP_WRITE,
2740 		.op_flags = wbc_to_write_flags(wbc),
2741 		.old_blkaddr = NULL_ADDR,
2742 		.page = page,
2743 		.encrypted_page = NULL,
2744 		.submitted = false,
2745 		.compr_blocks = compr_blocks,
2746 		.need_lock = LOCK_RETRY,
2747 		.io_type = io_type,
2748 		.io_wbc = wbc,
2749 		.bio = bio,
2750 		.last_block = last_block,
2751 	};
2752 
2753 	trace_f2fs_writepage(page, DATA);
2754 
2755 	/* we should bypass data pages to proceed the kworkder jobs */
2756 	if (unlikely(f2fs_cp_error(sbi))) {
2757 		mapping_set_error(page->mapping, -EIO);
2758 		/*
2759 		 * don't drop any dirty dentry pages for keeping lastest
2760 		 * directory structure.
2761 		 */
2762 		if (S_ISDIR(inode->i_mode))
2763 			goto redirty_out;
2764 		goto out;
2765 	}
2766 
2767 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2768 		goto redirty_out;
2769 
2770 	if (page->index < end_index ||
2771 			f2fs_verity_in_progress(inode) ||
2772 			compr_blocks)
2773 		goto write;
2774 
2775 	/*
2776 	 * If the offset is out-of-range of file size,
2777 	 * this page does not have to be written to disk.
2778 	 */
2779 	offset = i_size & (PAGE_SIZE - 1);
2780 	if ((page->index >= end_index + 1) || !offset)
2781 		goto out;
2782 
2783 	zero_user_segment(page, offset, PAGE_SIZE);
2784 write:
2785 	if (f2fs_is_drop_cache(inode))
2786 		goto out;
2787 	/* we should not write 0'th page having journal header */
2788 	if (f2fs_is_volatile_file(inode) && (!page->index ||
2789 			(!wbc->for_reclaim &&
2790 			f2fs_available_free_memory(sbi, BASE_CHECK))))
2791 		goto redirty_out;
2792 
2793 	/* Dentry/quota blocks are controlled by checkpoint */
2794 	if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2795 		/*
2796 		 * We need to wait for node_write to avoid block allocation during
2797 		 * checkpoint. This can only happen to quota writes which can cause
2798 		 * the below discard race condition.
2799 		 */
2800 		if (IS_NOQUOTA(inode))
2801 			down_read(&sbi->node_write);
2802 
2803 		fio.need_lock = LOCK_DONE;
2804 		err = f2fs_do_write_data_page(&fio);
2805 
2806 		if (IS_NOQUOTA(inode))
2807 			up_read(&sbi->node_write);
2808 
2809 		goto done;
2810 	}
2811 
2812 	if (!wbc->for_reclaim)
2813 		need_balance_fs = true;
2814 	else if (has_not_enough_free_secs(sbi, 0, 0))
2815 		goto redirty_out;
2816 	else
2817 		set_inode_flag(inode, FI_HOT_DATA);
2818 
2819 	err = -EAGAIN;
2820 	if (f2fs_has_inline_data(inode)) {
2821 		err = f2fs_write_inline_data(inode, page);
2822 		if (!err)
2823 			goto out;
2824 	}
2825 
2826 	if (err == -EAGAIN) {
2827 		err = f2fs_do_write_data_page(&fio);
2828 		if (err == -EAGAIN) {
2829 			fio.need_lock = LOCK_REQ;
2830 			err = f2fs_do_write_data_page(&fio);
2831 		}
2832 	}
2833 
2834 	if (err) {
2835 		file_set_keep_isize(inode);
2836 	} else {
2837 		spin_lock(&F2FS_I(inode)->i_size_lock);
2838 		if (F2FS_I(inode)->last_disk_size < psize)
2839 			F2FS_I(inode)->last_disk_size = psize;
2840 		spin_unlock(&F2FS_I(inode)->i_size_lock);
2841 	}
2842 
2843 done:
2844 	if (err && err != -ENOENT)
2845 		goto redirty_out;
2846 
2847 out:
2848 	inode_dec_dirty_pages(inode);
2849 	if (err) {
2850 		ClearPageUptodate(page);
2851 		clear_cold_data(page);
2852 	}
2853 
2854 	if (wbc->for_reclaim) {
2855 		f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2856 		clear_inode_flag(inode, FI_HOT_DATA);
2857 		f2fs_remove_dirty_inode(inode);
2858 		submitted = NULL;
2859 	}
2860 	unlock_page(page);
2861 	if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2862 					!F2FS_I(inode)->cp_task)
2863 		f2fs_balance_fs(sbi, need_balance_fs);
2864 
2865 	if (unlikely(f2fs_cp_error(sbi))) {
2866 		f2fs_submit_merged_write(sbi, DATA);
2867 		f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2868 		submitted = NULL;
2869 	}
2870 
2871 	if (submitted)
2872 		*submitted = fio.submitted ? 1 : 0;
2873 
2874 	return 0;
2875 
2876 redirty_out:
2877 	redirty_page_for_writepage(wbc, page);
2878 	/*
2879 	 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2880 	 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2881 	 * file_write_and_wait_range() will see EIO error, which is critical
2882 	 * to return value of fsync() followed by atomic_write failure to user.
2883 	 */
2884 	if (!err || wbc->for_reclaim)
2885 		return AOP_WRITEPAGE_ACTIVATE;
2886 	unlock_page(page);
2887 	return err;
2888 }
2889 
f2fs_write_data_page(struct page * page,struct writeback_control * wbc)2890 static int f2fs_write_data_page(struct page *page,
2891 					struct writeback_control *wbc)
2892 {
2893 #ifdef CONFIG_F2FS_FS_COMPRESSION
2894 	struct inode *inode = page->mapping->host;
2895 
2896 	if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2897 		goto out;
2898 
2899 	if (f2fs_compressed_file(inode)) {
2900 		if (f2fs_is_compressed_cluster(inode, page->index)) {
2901 			redirty_page_for_writepage(wbc, page);
2902 			return AOP_WRITEPAGE_ACTIVATE;
2903 		}
2904 	}
2905 out:
2906 #endif
2907 
2908 	return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2909 						wbc, FS_DATA_IO, 0);
2910 }
2911 
2912 /*
2913  * This function was copied from write_cche_pages from mm/page-writeback.c.
2914  * The major change is making write step of cold data page separately from
2915  * warm/hot data page.
2916  */
f2fs_write_cache_pages(struct address_space * mapping,struct writeback_control * wbc,enum iostat_type io_type)2917 static int f2fs_write_cache_pages(struct address_space *mapping,
2918 					struct writeback_control *wbc,
2919 					enum iostat_type io_type)
2920 {
2921 	int ret = 0;
2922 	int done = 0, retry = 0;
2923 	struct pagevec pvec;
2924 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2925 	struct bio *bio = NULL;
2926 	sector_t last_block;
2927 #ifdef CONFIG_F2FS_FS_COMPRESSION
2928 	struct inode *inode = mapping->host;
2929 	struct compress_ctx cc = {
2930 		.inode = inode,
2931 		.log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2932 		.cluster_size = F2FS_I(inode)->i_cluster_size,
2933 		.cluster_idx = NULL_CLUSTER,
2934 		.rpages = NULL,
2935 		.nr_rpages = 0,
2936 		.cpages = NULL,
2937 		.rbuf = NULL,
2938 		.cbuf = NULL,
2939 		.rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2940 		.private = NULL,
2941 	};
2942 #endif
2943 	int nr_pages;
2944 	pgoff_t index;
2945 	pgoff_t end;		/* Inclusive */
2946 	pgoff_t done_index;
2947 	int range_whole = 0;
2948 	xa_mark_t tag;
2949 	int nwritten = 0;
2950 	int submitted = 0;
2951 	int i;
2952 
2953 	pagevec_init(&pvec);
2954 
2955 	if (get_dirty_pages(mapping->host) <=
2956 				SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2957 		set_inode_flag(mapping->host, FI_HOT_DATA);
2958 	else
2959 		clear_inode_flag(mapping->host, FI_HOT_DATA);
2960 
2961 	if (wbc->range_cyclic) {
2962 		index = mapping->writeback_index; /* prev offset */
2963 		end = -1;
2964 	} else {
2965 		index = wbc->range_start >> PAGE_SHIFT;
2966 		end = wbc->range_end >> PAGE_SHIFT;
2967 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2968 			range_whole = 1;
2969 	}
2970 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2971 		tag = PAGECACHE_TAG_TOWRITE;
2972 	else
2973 		tag = PAGECACHE_TAG_DIRTY;
2974 retry:
2975 	retry = 0;
2976 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2977 		tag_pages_for_writeback(mapping, index, end);
2978 	done_index = index;
2979 	while (!done && !retry && (index <= end)) {
2980 		nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2981 				tag);
2982 		if (nr_pages == 0)
2983 			break;
2984 
2985 		for (i = 0; i < nr_pages; i++) {
2986 			struct page *page = pvec.pages[i];
2987 			bool need_readd;
2988 readd:
2989 			need_readd = false;
2990 #ifdef CONFIG_F2FS_FS_COMPRESSION
2991 			if (f2fs_compressed_file(inode)) {
2992 				ret = f2fs_init_compress_ctx(&cc);
2993 				if (ret) {
2994 					done = 1;
2995 					break;
2996 				}
2997 
2998 				if (!f2fs_cluster_can_merge_page(&cc,
2999 								page->index)) {
3000 					ret = f2fs_write_multi_pages(&cc,
3001 						&submitted, wbc, io_type);
3002 					if (!ret)
3003 						need_readd = true;
3004 					goto result;
3005 				}
3006 
3007 				if (unlikely(f2fs_cp_error(sbi)))
3008 					goto lock_page;
3009 
3010 				if (f2fs_cluster_is_empty(&cc)) {
3011 					void *fsdata = NULL;
3012 					struct page *pagep;
3013 					int ret2;
3014 
3015 					ret2 = f2fs_prepare_compress_overwrite(
3016 							inode, &pagep,
3017 							page->index, &fsdata);
3018 					if (ret2 < 0) {
3019 						ret = ret2;
3020 						done = 1;
3021 						break;
3022 					} else if (ret2 &&
3023 						!f2fs_compress_write_end(inode,
3024 								fsdata, page->index,
3025 								1)) {
3026 						retry = 1;
3027 						break;
3028 					}
3029 				} else {
3030 					goto lock_page;
3031 				}
3032 			}
3033 #endif
3034 			/* give a priority to WB_SYNC threads */
3035 			if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3036 					wbc->sync_mode == WB_SYNC_NONE) {
3037 				done = 1;
3038 				break;
3039 			}
3040 #ifdef CONFIG_F2FS_FS_COMPRESSION
3041 lock_page:
3042 #endif
3043 			done_index = page->index;
3044 retry_write:
3045 			lock_page(page);
3046 
3047 			if (unlikely(page->mapping != mapping)) {
3048 continue_unlock:
3049 				unlock_page(page);
3050 				continue;
3051 			}
3052 
3053 			if (!PageDirty(page)) {
3054 				/* someone wrote it for us */
3055 				goto continue_unlock;
3056 			}
3057 
3058 			if (PageWriteback(page)) {
3059 				if (wbc->sync_mode != WB_SYNC_NONE)
3060 					f2fs_wait_on_page_writeback(page,
3061 							DATA, true, true);
3062 				else
3063 					goto continue_unlock;
3064 			}
3065 
3066 			if (!clear_page_dirty_for_io(page))
3067 				goto continue_unlock;
3068 
3069 #ifdef CONFIG_F2FS_FS_COMPRESSION
3070 			if (f2fs_compressed_file(inode)) {
3071 				get_page(page);
3072 				f2fs_compress_ctx_add_page(&cc, page);
3073 				continue;
3074 			}
3075 #endif
3076 			ret = f2fs_write_single_data_page(page, &submitted,
3077 					&bio, &last_block, wbc, io_type, 0);
3078 			if (ret == AOP_WRITEPAGE_ACTIVATE)
3079 				unlock_page(page);
3080 #ifdef CONFIG_F2FS_FS_COMPRESSION
3081 result:
3082 #endif
3083 			nwritten += submitted;
3084 			wbc->nr_to_write -= submitted;
3085 
3086 			if (unlikely(ret)) {
3087 				/*
3088 				 * keep nr_to_write, since vfs uses this to
3089 				 * get # of written pages.
3090 				 */
3091 				if (ret == AOP_WRITEPAGE_ACTIVATE) {
3092 					ret = 0;
3093 					goto next;
3094 				} else if (ret == -EAGAIN) {
3095 					ret = 0;
3096 					if (wbc->sync_mode == WB_SYNC_ALL) {
3097 						cond_resched();
3098 						congestion_wait(BLK_RW_ASYNC,
3099 							DEFAULT_IO_TIMEOUT);
3100 						goto retry_write;
3101 					}
3102 					goto next;
3103 				}
3104 				done_index = page->index + 1;
3105 				done = 1;
3106 				break;
3107 			}
3108 
3109 			if (wbc->nr_to_write <= 0 &&
3110 					wbc->sync_mode == WB_SYNC_NONE) {
3111 				done = 1;
3112 				break;
3113 			}
3114 next:
3115 			if (need_readd)
3116 				goto readd;
3117 		}
3118 		pagevec_release(&pvec);
3119 		cond_resched();
3120 	}
3121 #ifdef CONFIG_F2FS_FS_COMPRESSION
3122 	/* flush remained pages in compress cluster */
3123 	if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3124 		ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3125 		nwritten += submitted;
3126 		wbc->nr_to_write -= submitted;
3127 		if (ret) {
3128 			done = 1;
3129 			retry = 0;
3130 		}
3131 	}
3132 	if (f2fs_compressed_file(inode))
3133 		f2fs_destroy_compress_ctx(&cc);
3134 #endif
3135 	if (retry) {
3136 		index = 0;
3137 		end = -1;
3138 		goto retry;
3139 	}
3140 	if (wbc->range_cyclic && !done)
3141 		done_index = 0;
3142 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3143 		mapping->writeback_index = done_index;
3144 
3145 	if (nwritten)
3146 		f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
3147 								NULL, 0, DATA);
3148 	/* submit cached bio of IPU write */
3149 	if (bio)
3150 		f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
3151 
3152 	return ret;
3153 }
3154 
__should_serialize_io(struct inode * inode,struct writeback_control * wbc)3155 static inline bool __should_serialize_io(struct inode *inode,
3156 					struct writeback_control *wbc)
3157 {
3158 	/* to avoid deadlock in path of data flush */
3159 	if (F2FS_I(inode)->cp_task)
3160 		return false;
3161 
3162 	if (!S_ISREG(inode->i_mode))
3163 		return false;
3164 	if (IS_NOQUOTA(inode))
3165 		return false;
3166 
3167 	if (f2fs_compressed_file(inode))
3168 		return true;
3169 	if (wbc->sync_mode != WB_SYNC_ALL)
3170 		return true;
3171 	if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3172 		return true;
3173 	return false;
3174 }
3175 
__f2fs_write_data_pages(struct address_space * mapping,struct writeback_control * wbc,enum iostat_type io_type)3176 static int __f2fs_write_data_pages(struct address_space *mapping,
3177 						struct writeback_control *wbc,
3178 						enum iostat_type io_type)
3179 {
3180 	struct inode *inode = mapping->host;
3181 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3182 	struct blk_plug plug;
3183 	int ret;
3184 	bool locked = false;
3185 
3186 	/* deal with chardevs and other special file */
3187 	if (!mapping->a_ops->writepage)
3188 		return 0;
3189 
3190 	/* skip writing if there is no dirty page in this inode */
3191 	if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3192 		return 0;
3193 
3194 	/* during POR, we don't need to trigger writepage at all. */
3195 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3196 		goto skip_write;
3197 
3198 	if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3199 			wbc->sync_mode == WB_SYNC_NONE &&
3200 			get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3201 			f2fs_available_free_memory(sbi, DIRTY_DENTS))
3202 		goto skip_write;
3203 
3204 	/* skip writing during file defragment */
3205 	if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3206 		goto skip_write;
3207 
3208 	trace_f2fs_writepages(mapping->host, wbc, DATA);
3209 
3210 	/* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3211 	if (wbc->sync_mode == WB_SYNC_ALL)
3212 		atomic_inc(&sbi->wb_sync_req[DATA]);
3213 	else if (atomic_read(&sbi->wb_sync_req[DATA]))
3214 		goto skip_write;
3215 
3216 	if (__should_serialize_io(inode, wbc)) {
3217 		mutex_lock(&sbi->writepages);
3218 		locked = true;
3219 	}
3220 
3221 	blk_start_plug(&plug);
3222 	ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3223 	blk_finish_plug(&plug);
3224 
3225 	if (locked)
3226 		mutex_unlock(&sbi->writepages);
3227 
3228 	if (wbc->sync_mode == WB_SYNC_ALL)
3229 		atomic_dec(&sbi->wb_sync_req[DATA]);
3230 	/*
3231 	 * if some pages were truncated, we cannot guarantee its mapping->host
3232 	 * to detect pending bios.
3233 	 */
3234 
3235 	f2fs_remove_dirty_inode(inode);
3236 	return ret;
3237 
3238 skip_write:
3239 	wbc->pages_skipped += get_dirty_pages(inode);
3240 	trace_f2fs_writepages(mapping->host, wbc, DATA);
3241 	return 0;
3242 }
3243 
f2fs_write_data_pages(struct address_space * mapping,struct writeback_control * wbc)3244 static int f2fs_write_data_pages(struct address_space *mapping,
3245 			    struct writeback_control *wbc)
3246 {
3247 	struct inode *inode = mapping->host;
3248 
3249 	return __f2fs_write_data_pages(mapping, wbc,
3250 			F2FS_I(inode)->cp_task == current ?
3251 			FS_CP_DATA_IO : FS_DATA_IO);
3252 }
3253 
f2fs_write_failed(struct address_space * mapping,loff_t to)3254 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3255 {
3256 	struct inode *inode = mapping->host;
3257 	loff_t i_size = i_size_read(inode);
3258 
3259 	if (IS_NOQUOTA(inode))
3260 		return;
3261 
3262 	/* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3263 	if (to > i_size && !f2fs_verity_in_progress(inode)) {
3264 		down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3265 		down_write(&F2FS_I(inode)->i_mmap_sem);
3266 
3267 		truncate_pagecache(inode, i_size);
3268 		f2fs_truncate_blocks(inode, i_size, true);
3269 
3270 		up_write(&F2FS_I(inode)->i_mmap_sem);
3271 		up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3272 	}
3273 }
3274 
prepare_write_begin(struct f2fs_sb_info * sbi,struct page * page,loff_t pos,unsigned len,block_t * blk_addr,bool * node_changed)3275 static int prepare_write_begin(struct f2fs_sb_info *sbi,
3276 			struct page *page, loff_t pos, unsigned len,
3277 			block_t *blk_addr, bool *node_changed)
3278 {
3279 	struct inode *inode = page->mapping->host;
3280 	pgoff_t index = page->index;
3281 	struct dnode_of_data dn;
3282 	struct page *ipage;
3283 	bool locked = false;
3284 	struct extent_info ei = {0,0,0};
3285 	int err = 0;
3286 	int flag;
3287 
3288 	/*
3289 	 * we already allocated all the blocks, so we don't need to get
3290 	 * the block addresses when there is no need to fill the page.
3291 	 */
3292 	if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
3293 	    !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3294 	    !f2fs_verity_in_progress(inode))
3295 		return 0;
3296 
3297 	/* f2fs_lock_op avoids race between write CP and convert_inline_page */
3298 	if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3299 		flag = F2FS_GET_BLOCK_DEFAULT;
3300 	else
3301 		flag = F2FS_GET_BLOCK_PRE_AIO;
3302 
3303 	if (f2fs_has_inline_data(inode) ||
3304 			(pos & PAGE_MASK) >= i_size_read(inode)) {
3305 		f2fs_do_map_lock(sbi, flag, true);
3306 		locked = true;
3307 	}
3308 
3309 restart:
3310 	/* check inline_data */
3311 	ipage = f2fs_get_node_page(sbi, inode->i_ino);
3312 	if (IS_ERR(ipage)) {
3313 		err = PTR_ERR(ipage);
3314 		goto unlock_out;
3315 	}
3316 
3317 	set_new_dnode(&dn, inode, ipage, ipage, 0);
3318 
3319 	if (f2fs_has_inline_data(inode)) {
3320 		if (pos + len <= MAX_INLINE_DATA(inode)) {
3321 			f2fs_do_read_inline_data(page, ipage);
3322 			set_inode_flag(inode, FI_DATA_EXIST);
3323 			if (inode->i_nlink)
3324 				set_inline_node(ipage);
3325 		} else {
3326 			err = f2fs_convert_inline_page(&dn, page);
3327 			if (err)
3328 				goto out;
3329 			if (dn.data_blkaddr == NULL_ADDR)
3330 				err = f2fs_get_block(&dn, index);
3331 		}
3332 	} else if (locked) {
3333 		err = f2fs_get_block(&dn, index);
3334 	} else {
3335 		if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3336 			dn.data_blkaddr = ei.blk + index - ei.fofs;
3337 		} else {
3338 			/* hole case */
3339 			err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3340 			if (err || dn.data_blkaddr == NULL_ADDR) {
3341 				f2fs_put_dnode(&dn);
3342 				f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
3343 								true);
3344 				WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
3345 				locked = true;
3346 				goto restart;
3347 			}
3348 		}
3349 	}
3350 
3351 	/* convert_inline_page can make node_changed */
3352 	*blk_addr = dn.data_blkaddr;
3353 	*node_changed = dn.node_changed;
3354 out:
3355 	f2fs_put_dnode(&dn);
3356 unlock_out:
3357 	if (locked)
3358 		f2fs_do_map_lock(sbi, flag, false);
3359 	return err;
3360 }
3361 
f2fs_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata)3362 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3363 		loff_t pos, unsigned len, unsigned flags,
3364 		struct page **pagep, void **fsdata)
3365 {
3366 	struct inode *inode = mapping->host;
3367 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3368 	struct page *page = NULL;
3369 	pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3370 	bool need_balance = false, drop_atomic = false;
3371 	block_t blkaddr = NULL_ADDR;
3372 	int err = 0;
3373 
3374 	trace_f2fs_write_begin(inode, pos, len, flags);
3375 
3376 	if (!f2fs_is_checkpoint_ready(sbi)) {
3377 		err = -ENOSPC;
3378 		goto fail;
3379 	}
3380 
3381 	if ((f2fs_is_atomic_file(inode) &&
3382 			!f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3383 			is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
3384 		err = -ENOMEM;
3385 		drop_atomic = true;
3386 		goto fail;
3387 	}
3388 
3389 	/*
3390 	 * We should check this at this moment to avoid deadlock on inode page
3391 	 * and #0 page. The locking rule for inline_data conversion should be:
3392 	 * lock_page(page #0) -> lock_page(inode_page)
3393 	 */
3394 	if (index != 0) {
3395 		err = f2fs_convert_inline_inode(inode);
3396 		if (err)
3397 			goto fail;
3398 	}
3399 
3400 #ifdef CONFIG_F2FS_FS_COMPRESSION
3401 	if (f2fs_compressed_file(inode)) {
3402 		int ret;
3403 
3404 		*fsdata = NULL;
3405 
3406 		ret = f2fs_prepare_compress_overwrite(inode, pagep,
3407 							index, fsdata);
3408 		if (ret < 0) {
3409 			err = ret;
3410 			goto fail;
3411 		} else if (ret) {
3412 			return 0;
3413 		}
3414 	}
3415 #endif
3416 
3417 repeat:
3418 	/*
3419 	 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3420 	 * wait_for_stable_page. Will wait that below with our IO control.
3421 	 */
3422 	page = f2fs_pagecache_get_page(mapping, index,
3423 				FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3424 	if (!page) {
3425 		err = -ENOMEM;
3426 		goto fail;
3427 	}
3428 
3429 	/* TODO: cluster can be compressed due to race with .writepage */
3430 
3431 	*pagep = page;
3432 
3433 	err = prepare_write_begin(sbi, page, pos, len,
3434 					&blkaddr, &need_balance);
3435 	if (err)
3436 		goto fail;
3437 
3438 	if (need_balance && !IS_NOQUOTA(inode) &&
3439 			has_not_enough_free_secs(sbi, 0, 0)) {
3440 		unlock_page(page);
3441 		f2fs_balance_fs(sbi, true);
3442 		lock_page(page);
3443 		if (page->mapping != mapping) {
3444 			/* The page got truncated from under us */
3445 			f2fs_put_page(page, 1);
3446 			goto repeat;
3447 		}
3448 	}
3449 
3450 	f2fs_wait_on_page_writeback(page, DATA, false, true);
3451 
3452 	if (len == PAGE_SIZE || PageUptodate(page))
3453 		return 0;
3454 
3455 	if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3456 	    !f2fs_verity_in_progress(inode)) {
3457 		zero_user_segment(page, len, PAGE_SIZE);
3458 		return 0;
3459 	}
3460 
3461 	if (blkaddr == NEW_ADDR) {
3462 		zero_user_segment(page, 0, PAGE_SIZE);
3463 		SetPageUptodate(page);
3464 	} else {
3465 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3466 				DATA_GENERIC_ENHANCE_READ)) {
3467 			err = -EFSCORRUPTED;
3468 			goto fail;
3469 		}
3470 		err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
3471 		if (err)
3472 			goto fail;
3473 
3474 		lock_page(page);
3475 		if (unlikely(page->mapping != mapping)) {
3476 			f2fs_put_page(page, 1);
3477 			goto repeat;
3478 		}
3479 		if (unlikely(!PageUptodate(page))) {
3480 			err = -EIO;
3481 			goto fail;
3482 		}
3483 	}
3484 	return 0;
3485 
3486 fail:
3487 	f2fs_put_page(page, 1);
3488 	f2fs_write_failed(mapping, pos + len);
3489 	if (drop_atomic)
3490 		f2fs_drop_inmem_pages_all(sbi, false);
3491 	return err;
3492 }
3493 
f2fs_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)3494 static int f2fs_write_end(struct file *file,
3495 			struct address_space *mapping,
3496 			loff_t pos, unsigned len, unsigned copied,
3497 			struct page *page, void *fsdata)
3498 {
3499 	struct inode *inode = page->mapping->host;
3500 
3501 	trace_f2fs_write_end(inode, pos, len, copied);
3502 
3503 	/*
3504 	 * This should be come from len == PAGE_SIZE, and we expect copied
3505 	 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3506 	 * let generic_perform_write() try to copy data again through copied=0.
3507 	 */
3508 	if (!PageUptodate(page)) {
3509 		if (unlikely(copied != len))
3510 			copied = 0;
3511 		else
3512 			SetPageUptodate(page);
3513 	}
3514 
3515 #ifdef CONFIG_F2FS_FS_COMPRESSION
3516 	/* overwrite compressed file */
3517 	if (f2fs_compressed_file(inode) && fsdata) {
3518 		f2fs_compress_write_end(inode, fsdata, page->index, copied);
3519 		f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3520 
3521 		if (pos + copied > i_size_read(inode) &&
3522 				!f2fs_verity_in_progress(inode))
3523 			f2fs_i_size_write(inode, pos + copied);
3524 		return copied;
3525 	}
3526 #endif
3527 
3528 	if (!copied)
3529 		goto unlock_out;
3530 
3531 	set_page_dirty(page);
3532 
3533 	if (pos + copied > i_size_read(inode) &&
3534 	    !f2fs_verity_in_progress(inode))
3535 		f2fs_i_size_write(inode, pos + copied);
3536 unlock_out:
3537 	f2fs_put_page(page, 1);
3538 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3539 	return copied;
3540 }
3541 
check_direct_IO(struct inode * inode,struct iov_iter * iter,loff_t offset)3542 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3543 			   loff_t offset)
3544 {
3545 	unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3546 	unsigned blkbits = i_blkbits;
3547 	unsigned blocksize_mask = (1 << blkbits) - 1;
3548 	unsigned long align = offset | iov_iter_alignment(iter);
3549 	struct block_device *bdev = inode->i_sb->s_bdev;
3550 
3551 	if (iov_iter_rw(iter) == READ && offset >= i_size_read(inode))
3552 		return 1;
3553 
3554 	if (align & blocksize_mask) {
3555 		if (bdev)
3556 			blkbits = blksize_bits(bdev_logical_block_size(bdev));
3557 		blocksize_mask = (1 << blkbits) - 1;
3558 		if (align & blocksize_mask)
3559 			return -EINVAL;
3560 		return 1;
3561 	}
3562 	return 0;
3563 }
3564 
f2fs_dio_end_io(struct bio * bio)3565 static void f2fs_dio_end_io(struct bio *bio)
3566 {
3567 	struct f2fs_private_dio *dio = bio->bi_private;
3568 
3569 	dec_page_count(F2FS_I_SB(dio->inode),
3570 			dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3571 
3572 	bio->bi_private = dio->orig_private;
3573 	bio->bi_end_io = dio->orig_end_io;
3574 
3575 	kfree(dio);
3576 
3577 	bio_endio(bio);
3578 }
3579 
f2fs_dio_submit_bio(struct bio * bio,struct inode * inode,loff_t file_offset)3580 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3581 							loff_t file_offset)
3582 {
3583 	struct f2fs_private_dio *dio;
3584 	bool write = (bio_op(bio) == REQ_OP_WRITE);
3585 
3586 	dio = f2fs_kzalloc(F2FS_I_SB(inode),
3587 			sizeof(struct f2fs_private_dio), GFP_NOFS);
3588 	if (!dio)
3589 		goto out;
3590 
3591 	dio->inode = inode;
3592 	dio->orig_end_io = bio->bi_end_io;
3593 	dio->orig_private = bio->bi_private;
3594 	dio->write = write;
3595 
3596 	bio->bi_end_io = f2fs_dio_end_io;
3597 	bio->bi_private = dio;
3598 
3599 	inc_page_count(F2FS_I_SB(inode),
3600 			write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3601 
3602 	submit_bio(bio);
3603 	return;
3604 out:
3605 	bio->bi_status = BLK_STS_IOERR;
3606 	bio_endio(bio);
3607 }
3608 
f2fs_direct_IO(struct kiocb * iocb,struct iov_iter * iter)3609 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3610 {
3611 	struct address_space *mapping = iocb->ki_filp->f_mapping;
3612 	struct inode *inode = mapping->host;
3613 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3614 	struct f2fs_inode_info *fi = F2FS_I(inode);
3615 	size_t count = iov_iter_count(iter);
3616 	loff_t offset = iocb->ki_pos;
3617 	int rw = iov_iter_rw(iter);
3618 	int err;
3619 	enum rw_hint hint = iocb->ki_hint;
3620 	int whint_mode = F2FS_OPTION(sbi).whint_mode;
3621 	bool do_opu;
3622 
3623 	err = check_direct_IO(inode, iter, offset);
3624 	if (err)
3625 		return err < 0 ? err : 0;
3626 
3627 	if (f2fs_force_buffered_io(inode, iocb, iter))
3628 		return 0;
3629 
3630 	do_opu = allow_outplace_dio(inode, iocb, iter);
3631 
3632 	trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3633 
3634 	if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3635 		iocb->ki_hint = WRITE_LIFE_NOT_SET;
3636 
3637 	if (iocb->ki_flags & IOCB_NOWAIT) {
3638 		if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3639 			iocb->ki_hint = hint;
3640 			err = -EAGAIN;
3641 			goto out;
3642 		}
3643 		if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3644 			up_read(&fi->i_gc_rwsem[rw]);
3645 			iocb->ki_hint = hint;
3646 			err = -EAGAIN;
3647 			goto out;
3648 		}
3649 	} else {
3650 		down_read(&fi->i_gc_rwsem[rw]);
3651 		if (do_opu)
3652 			down_read(&fi->i_gc_rwsem[READ]);
3653 	}
3654 
3655 	err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3656 			iter, rw == WRITE ? get_data_block_dio_write :
3657 			get_data_block_dio, NULL, f2fs_dio_submit_bio,
3658 			rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3659 			DIO_SKIP_HOLES);
3660 
3661 	if (do_opu)
3662 		up_read(&fi->i_gc_rwsem[READ]);
3663 
3664 	up_read(&fi->i_gc_rwsem[rw]);
3665 
3666 	if (rw == WRITE) {
3667 		if (whint_mode == WHINT_MODE_OFF)
3668 			iocb->ki_hint = hint;
3669 		if (err > 0) {
3670 			f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3671 									err);
3672 			if (!do_opu)
3673 				set_inode_flag(inode, FI_UPDATE_WRITE);
3674 		} else if (err == -EIOCBQUEUED) {
3675 			f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3676 						count - iov_iter_count(iter));
3677 		} else if (err < 0) {
3678 			f2fs_write_failed(mapping, offset + count);
3679 		}
3680 	} else {
3681 		if (err > 0)
3682 			f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3683 		else if (err == -EIOCBQUEUED)
3684 			f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO,
3685 						count - iov_iter_count(iter));
3686 	}
3687 
3688 out:
3689 	trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3690 
3691 	return err;
3692 }
3693 
f2fs_invalidate_page(struct page * page,unsigned int offset,unsigned int length)3694 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3695 							unsigned int length)
3696 {
3697 	struct inode *inode = page->mapping->host;
3698 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3699 
3700 	if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3701 		(offset % PAGE_SIZE || length != PAGE_SIZE))
3702 		return;
3703 
3704 	if (PageDirty(page)) {
3705 		if (inode->i_ino == F2FS_META_INO(sbi)) {
3706 			dec_page_count(sbi, F2FS_DIRTY_META);
3707 		} else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3708 			dec_page_count(sbi, F2FS_DIRTY_NODES);
3709 		} else {
3710 			inode_dec_dirty_pages(inode);
3711 			f2fs_remove_dirty_inode(inode);
3712 		}
3713 	}
3714 
3715 	clear_cold_data(page);
3716 
3717 	if (IS_ATOMIC_WRITTEN_PAGE(page))
3718 		return f2fs_drop_inmem_page(inode, page);
3719 
3720 	f2fs_clear_page_private(page);
3721 }
3722 
f2fs_release_page(struct page * page,gfp_t wait)3723 int f2fs_release_page(struct page *page, gfp_t wait)
3724 {
3725 	/* If this is dirty page, keep PagePrivate */
3726 	if (PageDirty(page))
3727 		return 0;
3728 
3729 	/* This is atomic written page, keep Private */
3730 	if (IS_ATOMIC_WRITTEN_PAGE(page))
3731 		return 0;
3732 
3733 	clear_cold_data(page);
3734 	f2fs_clear_page_private(page);
3735 	return 1;
3736 }
3737 
f2fs_set_data_page_dirty(struct page * page)3738 static int f2fs_set_data_page_dirty(struct page *page)
3739 {
3740 	struct inode *inode = page_file_mapping(page)->host;
3741 
3742 	trace_f2fs_set_page_dirty(page, DATA);
3743 
3744 	if (!PageUptodate(page))
3745 		SetPageUptodate(page);
3746 	if (PageSwapCache(page))
3747 		return __set_page_dirty_nobuffers(page);
3748 
3749 	if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3750 		if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
3751 			f2fs_register_inmem_page(inode, page);
3752 			return 1;
3753 		}
3754 		/*
3755 		 * Previously, this page has been registered, we just
3756 		 * return here.
3757 		 */
3758 		return 0;
3759 	}
3760 
3761 	if (!PageDirty(page)) {
3762 		__set_page_dirty_nobuffers(page);
3763 		f2fs_update_dirty_page(inode, page);
3764 		return 1;
3765 	}
3766 	return 0;
3767 }
3768 
3769 
f2fs_bmap_compress(struct inode * inode,sector_t block)3770 static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3771 {
3772 #ifdef CONFIG_F2FS_FS_COMPRESSION
3773 	struct dnode_of_data dn;
3774 	sector_t start_idx, blknr = 0;
3775 	int ret;
3776 
3777 	start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3778 
3779 	set_new_dnode(&dn, inode, NULL, NULL, 0);
3780 	ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3781 	if (ret)
3782 		return 0;
3783 
3784 	if (dn.data_blkaddr != COMPRESS_ADDR) {
3785 		dn.ofs_in_node += block - start_idx;
3786 		blknr = f2fs_data_blkaddr(&dn);
3787 		if (!__is_valid_data_blkaddr(blknr))
3788 			blknr = 0;
3789 	}
3790 
3791 	f2fs_put_dnode(&dn);
3792 	return blknr;
3793 #else
3794 	return 0;
3795 #endif
3796 }
3797 
3798 
f2fs_bmap(struct address_space * mapping,sector_t block)3799 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3800 {
3801 	struct inode *inode = mapping->host;
3802 	struct buffer_head tmp = {
3803 		.b_size = i_blocksize(inode),
3804 	};
3805 	sector_t blknr = 0;
3806 
3807 	if (f2fs_has_inline_data(inode))
3808 		goto out;
3809 
3810 	/* make sure allocating whole blocks */
3811 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3812 		filemap_write_and_wait(mapping);
3813 
3814 	/* Block number less than F2FS MAX BLOCKS */
3815 	if (unlikely(block >= F2FS_I_SB(inode)->max_file_blocks))
3816 		goto out;
3817 
3818 	if (f2fs_compressed_file(inode)) {
3819 		blknr = f2fs_bmap_compress(inode, block);
3820 	} else {
3821 		if (!get_data_block_bmap(inode, block, &tmp, 0))
3822 			blknr = tmp.b_blocknr;
3823 	}
3824 out:
3825 	trace_f2fs_bmap(inode, block, blknr);
3826 	return blknr;
3827 }
3828 
3829 #ifdef CONFIG_MIGRATION
3830 #include <linux/migrate.h>
3831 
f2fs_migrate_page(struct address_space * mapping,struct page * newpage,struct page * page,enum migrate_mode mode)3832 int f2fs_migrate_page(struct address_space *mapping,
3833 		struct page *newpage, struct page *page, enum migrate_mode mode)
3834 {
3835 	int rc, extra_count;
3836 	struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3837 	bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3838 
3839 	BUG_ON(PageWriteback(page));
3840 
3841 	/* migrating an atomic written page is safe with the inmem_lock hold */
3842 	if (atomic_written) {
3843 		if (mode != MIGRATE_SYNC)
3844 			return -EBUSY;
3845 		if (!mutex_trylock(&fi->inmem_lock))
3846 			return -EAGAIN;
3847 	}
3848 
3849 	/* one extra reference was held for atomic_write page */
3850 	extra_count = atomic_written ? 1 : 0;
3851 	rc = migrate_page_move_mapping(mapping, newpage,
3852 				page, extra_count);
3853 	if (rc != MIGRATEPAGE_SUCCESS) {
3854 		if (atomic_written)
3855 			mutex_unlock(&fi->inmem_lock);
3856 		return rc;
3857 	}
3858 
3859 	if (atomic_written) {
3860 		struct inmem_pages *cur;
3861 		list_for_each_entry(cur, &fi->inmem_pages, list)
3862 			if (cur->page == page) {
3863 				cur->page = newpage;
3864 				break;
3865 			}
3866 		mutex_unlock(&fi->inmem_lock);
3867 		put_page(page);
3868 		get_page(newpage);
3869 	}
3870 
3871 	if (PagePrivate(page)) {
3872 		f2fs_set_page_private(newpage, page_private(page));
3873 		f2fs_clear_page_private(page);
3874 	}
3875 
3876 	if (mode != MIGRATE_SYNC_NO_COPY)
3877 		migrate_page_copy(newpage, page);
3878 	else
3879 		migrate_page_states(newpage, page);
3880 
3881 	return MIGRATEPAGE_SUCCESS;
3882 }
3883 #endif
3884 
3885 #ifdef CONFIG_SWAP
check_swap_activate_fast(struct swap_info_struct * sis,struct file * swap_file,sector_t * span)3886 static int check_swap_activate_fast(struct swap_info_struct *sis,
3887 				struct file *swap_file, sector_t *span)
3888 {
3889 	struct address_space *mapping = swap_file->f_mapping;
3890 	struct inode *inode = mapping->host;
3891 	sector_t cur_lblock;
3892 	sector_t last_lblock;
3893 	sector_t pblock;
3894 	sector_t lowest_pblock = -1;
3895 	sector_t highest_pblock = 0;
3896 	int nr_extents = 0;
3897 	unsigned long nr_pblocks;
3898 	unsigned long len;
3899 	int ret;
3900 
3901 	/*
3902 	 * Map all the blocks into the extent list.  This code doesn't try
3903 	 * to be very smart.
3904 	 */
3905 	cur_lblock = 0;
3906 	last_lblock = logical_to_blk(inode, i_size_read(inode));
3907 	len = i_size_read(inode);
3908 
3909 	while (cur_lblock <= last_lblock && cur_lblock < sis->max) {
3910 		struct buffer_head map_bh;
3911 		pgoff_t next_pgofs;
3912 
3913 		cond_resched();
3914 
3915 		memset(&map_bh, 0, sizeof(struct buffer_head));
3916 		map_bh.b_size = len - cur_lblock;
3917 
3918 		ret = get_data_block(inode, cur_lblock, &map_bh, 0,
3919 					F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
3920 		if (ret)
3921 			goto err_out;
3922 
3923 		/* hole */
3924 		if (!buffer_mapped(&map_bh))
3925 			goto err_out;
3926 
3927 		pblock = map_bh.b_blocknr;
3928 		nr_pblocks = logical_to_blk(inode, map_bh.b_size);
3929 
3930 		if (cur_lblock + nr_pblocks >= sis->max)
3931 			nr_pblocks = sis->max - cur_lblock;
3932 
3933 		if (cur_lblock) {	/* exclude the header page */
3934 			if (pblock < lowest_pblock)
3935 				lowest_pblock = pblock;
3936 			if (pblock + nr_pblocks - 1 > highest_pblock)
3937 				highest_pblock = pblock + nr_pblocks - 1;
3938 		}
3939 
3940 		/*
3941 		 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3942 		 */
3943 		ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
3944 		if (ret < 0)
3945 			goto out;
3946 		nr_extents += ret;
3947 		cur_lblock += nr_pblocks;
3948 	}
3949 	ret = nr_extents;
3950 	*span = 1 + highest_pblock - lowest_pblock;
3951 	if (cur_lblock == 0)
3952 		cur_lblock = 1;	/* force Empty message */
3953 	sis->max = cur_lblock;
3954 	sis->pages = cur_lblock - 1;
3955 	sis->highest_bit = cur_lblock - 1;
3956 out:
3957 	return ret;
3958 err_out:
3959 	pr_err("swapon: swapfile has holes\n");
3960 	return -EINVAL;
3961 }
3962 
3963 /* Copied from generic_swapfile_activate() to check any holes */
check_swap_activate(struct swap_info_struct * sis,struct file * swap_file,sector_t * span)3964 static int check_swap_activate(struct swap_info_struct *sis,
3965 				struct file *swap_file, sector_t *span)
3966 {
3967 	struct address_space *mapping = swap_file->f_mapping;
3968 	struct inode *inode = mapping->host;
3969 	unsigned blocks_per_page;
3970 	unsigned long page_no;
3971 	unsigned blkbits;
3972 	sector_t probe_block;
3973 	sector_t last_block;
3974 	sector_t lowest_block = -1;
3975 	sector_t highest_block = 0;
3976 	int nr_extents = 0;
3977 	int ret;
3978 
3979 	if (PAGE_SIZE == F2FS_BLKSIZE)
3980 		return check_swap_activate_fast(sis, swap_file, span);
3981 
3982 	blkbits = inode->i_blkbits;
3983 	blocks_per_page = PAGE_SIZE >> blkbits;
3984 
3985 	/*
3986 	 * Map all the blocks into the extent list.  This code doesn't try
3987 	 * to be very smart.
3988 	 */
3989 	probe_block = 0;
3990 	page_no = 0;
3991 	last_block = i_size_read(inode) >> blkbits;
3992 	while ((probe_block + blocks_per_page) <= last_block &&
3993 			page_no < sis->max) {
3994 		unsigned block_in_page;
3995 		sector_t first_block;
3996 		sector_t block = 0;
3997 		int	 err = 0;
3998 
3999 		cond_resched();
4000 
4001 		block = probe_block;
4002 		err = bmap(inode, &block);
4003 		if (err || !block)
4004 			goto bad_bmap;
4005 		first_block = block;
4006 
4007 		/*
4008 		 * It must be PAGE_SIZE aligned on-disk
4009 		 */
4010 		if (first_block & (blocks_per_page - 1)) {
4011 			probe_block++;
4012 			goto reprobe;
4013 		}
4014 
4015 		for (block_in_page = 1; block_in_page < blocks_per_page;
4016 					block_in_page++) {
4017 
4018 			block = probe_block + block_in_page;
4019 			err = bmap(inode, &block);
4020 
4021 			if (err || !block)
4022 				goto bad_bmap;
4023 
4024 			if (block != first_block + block_in_page) {
4025 				/* Discontiguity */
4026 				probe_block++;
4027 				goto reprobe;
4028 			}
4029 		}
4030 
4031 		first_block >>= (PAGE_SHIFT - blkbits);
4032 		if (page_no) {	/* exclude the header page */
4033 			if (first_block < lowest_block)
4034 				lowest_block = first_block;
4035 			if (first_block > highest_block)
4036 				highest_block = first_block;
4037 		}
4038 
4039 		/*
4040 		 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
4041 		 */
4042 		ret = add_swap_extent(sis, page_no, 1, first_block);
4043 		if (ret < 0)
4044 			goto out;
4045 		nr_extents += ret;
4046 		page_no++;
4047 		probe_block += blocks_per_page;
4048 reprobe:
4049 		continue;
4050 	}
4051 	ret = nr_extents;
4052 	*span = 1 + highest_block - lowest_block;
4053 	if (page_no == 0)
4054 		page_no = 1;	/* force Empty message */
4055 	sis->max = page_no;
4056 	sis->pages = page_no - 1;
4057 	sis->highest_bit = page_no - 1;
4058 out:
4059 	return ret;
4060 bad_bmap:
4061 	pr_err("swapon: swapfile has holes\n");
4062 	return -EINVAL;
4063 }
4064 
f2fs_swap_activate(struct swap_info_struct * sis,struct file * file,sector_t * span)4065 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4066 				sector_t *span)
4067 {
4068 	struct inode *inode = file_inode(file);
4069 	int ret;
4070 
4071 	if (!S_ISREG(inode->i_mode))
4072 		return -EINVAL;
4073 
4074 	if (f2fs_readonly(F2FS_I_SB(inode)->sb))
4075 		return -EROFS;
4076 
4077 	ret = f2fs_convert_inline_inode(inode);
4078 	if (ret)
4079 		return ret;
4080 
4081 	if (!f2fs_disable_compressed_file(inode))
4082 		return -EINVAL;
4083 
4084 	ret = check_swap_activate(sis, file, span);
4085 	if (ret < 0)
4086 		return ret;
4087 
4088 	set_inode_flag(inode, FI_PIN_FILE);
4089 	f2fs_precache_extents(inode);
4090 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
4091 	return ret;
4092 }
4093 
f2fs_swap_deactivate(struct file * file)4094 static void f2fs_swap_deactivate(struct file *file)
4095 {
4096 	struct inode *inode = file_inode(file);
4097 
4098 	clear_inode_flag(inode, FI_PIN_FILE);
4099 }
4100 #else
f2fs_swap_activate(struct swap_info_struct * sis,struct file * file,sector_t * span)4101 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4102 				sector_t *span)
4103 {
4104 	return -EOPNOTSUPP;
4105 }
4106 
f2fs_swap_deactivate(struct file * file)4107 static void f2fs_swap_deactivate(struct file *file)
4108 {
4109 }
4110 #endif
4111 
4112 const struct address_space_operations f2fs_dblock_aops = {
4113 	.readpage	= f2fs_read_data_page,
4114 	.readahead	= f2fs_readahead,
4115 	.writepage	= f2fs_write_data_page,
4116 	.writepages	= f2fs_write_data_pages,
4117 	.write_begin	= f2fs_write_begin,
4118 	.write_end	= f2fs_write_end,
4119 	.set_page_dirty	= f2fs_set_data_page_dirty,
4120 	.invalidatepage	= f2fs_invalidate_page,
4121 	.releasepage	= f2fs_release_page,
4122 	.direct_IO	= f2fs_direct_IO,
4123 	.bmap		= f2fs_bmap,
4124 	.swap_activate  = f2fs_swap_activate,
4125 	.swap_deactivate = f2fs_swap_deactivate,
4126 #ifdef CONFIG_MIGRATION
4127 	.migratepage    = f2fs_migrate_page,
4128 #endif
4129 };
4130 
f2fs_clear_page_cache_dirty_tag(struct page * page)4131 void f2fs_clear_page_cache_dirty_tag(struct page *page)
4132 {
4133 	struct address_space *mapping = page_mapping(page);
4134 	unsigned long flags;
4135 
4136 	xa_lock_irqsave(&mapping->i_pages, flags);
4137 	__xa_clear_mark(&mapping->i_pages, page_index(page),
4138 						PAGECACHE_TAG_DIRTY);
4139 	xa_unlock_irqrestore(&mapping->i_pages, flags);
4140 }
4141 
f2fs_init_post_read_processing(void)4142 int __init f2fs_init_post_read_processing(void)
4143 {
4144 	bio_post_read_ctx_cache =
4145 		kmem_cache_create("f2fs_bio_post_read_ctx",
4146 				  sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4147 	if (!bio_post_read_ctx_cache)
4148 		goto fail;
4149 	bio_post_read_ctx_pool =
4150 		mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4151 					 bio_post_read_ctx_cache);
4152 	if (!bio_post_read_ctx_pool)
4153 		goto fail_free_cache;
4154 	return 0;
4155 
4156 fail_free_cache:
4157 	kmem_cache_destroy(bio_post_read_ctx_cache);
4158 fail:
4159 	return -ENOMEM;
4160 }
4161 
f2fs_destroy_post_read_processing(void)4162 void f2fs_destroy_post_read_processing(void)
4163 {
4164 	mempool_destroy(bio_post_read_ctx_pool);
4165 	kmem_cache_destroy(bio_post_read_ctx_cache);
4166 }
4167 
f2fs_init_post_read_wq(struct f2fs_sb_info * sbi)4168 int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4169 {
4170 	if (!f2fs_sb_has_encrypt(sbi) &&
4171 		!f2fs_sb_has_verity(sbi) &&
4172 		!f2fs_sb_has_compression(sbi))
4173 		return 0;
4174 
4175 	sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4176 						 WQ_UNBOUND | WQ_HIGHPRI,
4177 						 num_online_cpus());
4178 	if (!sbi->post_read_wq)
4179 		return -ENOMEM;
4180 	return 0;
4181 }
4182 
f2fs_destroy_post_read_wq(struct f2fs_sb_info * sbi)4183 void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4184 {
4185 	if (sbi->post_read_wq)
4186 		destroy_workqueue(sbi->post_read_wq);
4187 }
4188 
f2fs_init_bio_entry_cache(void)4189 int __init f2fs_init_bio_entry_cache(void)
4190 {
4191 	bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4192 			sizeof(struct bio_entry));
4193 	if (!bio_entry_slab)
4194 		return -ENOMEM;
4195 	return 0;
4196 }
4197 
f2fs_destroy_bio_entry_cache(void)4198 void f2fs_destroy_bio_entry_cache(void)
4199 {
4200 	kmem_cache_destroy(bio_entry_slab);
4201 }
4202