• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/f2fs/data.c
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/buffer_head.h>
11 #include <linux/mpage.h>
12 #include <linux/writeback.h>
13 #include <linux/backing-dev.h>
14 #include <linux/pagevec.h>
15 #include <linux/blkdev.h>
16 #include <linux/bio.h>
17 #include <linux/swap.h>
18 #include <linux/prefetch.h>
19 #include <linux/uio.h>
20 #include <linux/cleancache.h>
21 #include <linux/sched/signal.h>
22 
23 #include "f2fs.h"
24 #include "node.h"
25 #include "segment.h"
26 #include "trace.h"
27 #include <trace/events/f2fs.h>
28 #include <trace/events/android_fs.h>
29 
30 #define NUM_PREALLOC_POST_READ_CTXS	128
31 
32 static struct kmem_cache *bio_post_read_ctx_cache;
33 static struct kmem_cache *bio_entry_slab;
34 static mempool_t *bio_post_read_ctx_pool;
35 
__is_cp_guaranteed(struct page * page)36 static bool __is_cp_guaranteed(struct page *page)
37 {
38 	struct address_space *mapping = page->mapping;
39 	struct inode *inode;
40 	struct f2fs_sb_info *sbi;
41 
42 	if (!mapping)
43 		return false;
44 
45 	inode = mapping->host;
46 	sbi = F2FS_I_SB(inode);
47 
48 	if (inode->i_ino == F2FS_META_INO(sbi) ||
49 			inode->i_ino ==  F2FS_NODE_INO(sbi) ||
50 			S_ISDIR(inode->i_mode) ||
51 			(S_ISREG(inode->i_mode) &&
52 			(f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
53 			is_cold_data(page))
54 		return true;
55 	return false;
56 }
57 
__read_io_type(struct page * page)58 static enum count_type __read_io_type(struct page *page)
59 {
60 	struct address_space *mapping = page_file_mapping(page);
61 
62 	if (mapping) {
63 		struct inode *inode = mapping->host;
64 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
65 
66 		if (inode->i_ino == F2FS_META_INO(sbi))
67 			return F2FS_RD_META;
68 
69 		if (inode->i_ino == F2FS_NODE_INO(sbi))
70 			return F2FS_RD_NODE;
71 	}
72 	return F2FS_RD_DATA;
73 }
74 
75 /* postprocessing steps for read bios */
76 enum bio_post_read_step {
77 	STEP_INITIAL = 0,
78 	STEP_DECRYPT,
79 	STEP_VERITY,
80 };
81 
82 struct bio_post_read_ctx {
83 	struct bio *bio;
84 	struct work_struct work;
85 	unsigned int cur_step;
86 	unsigned int enabled_steps;
87 };
88 
__read_end_io(struct bio * bio)89 static void __read_end_io(struct bio *bio)
90 {
91 	struct page *page;
92 	struct bio_vec *bv;
93 	struct bvec_iter_all iter_all;
94 
95 	bio_for_each_segment_all(bv, bio, iter_all) {
96 		page = bv->bv_page;
97 
98 		/* PG_error was set if any post_read step failed */
99 		if (bio->bi_status || PageError(page)) {
100 			ClearPageUptodate(page);
101 			/* will re-read again later */
102 			ClearPageError(page);
103 		} else {
104 			SetPageUptodate(page);
105 		}
106 		dec_page_count(F2FS_P_SB(page), __read_io_type(page));
107 		unlock_page(page);
108 	}
109 	if (bio->bi_private)
110 		mempool_free(bio->bi_private, bio_post_read_ctx_pool);
111 	bio_put(bio);
112 }
113 
114 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
115 
decrypt_work(struct work_struct * work)116 static void decrypt_work(struct work_struct *work)
117 {
118 	struct bio_post_read_ctx *ctx =
119 		container_of(work, struct bio_post_read_ctx, work);
120 
121 	fscrypt_decrypt_bio(ctx->bio);
122 
123 	bio_post_read_processing(ctx);
124 }
125 
verity_work(struct work_struct * work)126 static void verity_work(struct work_struct *work)
127 {
128 	struct bio_post_read_ctx *ctx =
129 		container_of(work, struct bio_post_read_ctx, work);
130 
131 	fsverity_verify_bio(ctx->bio);
132 
133 	bio_post_read_processing(ctx);
134 }
135 
bio_post_read_processing(struct bio_post_read_ctx * ctx)136 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
137 {
138 	/*
139 	 * We use different work queues for decryption and for verity because
140 	 * verity may require reading metadata pages that need decryption, and
141 	 * we shouldn't recurse to the same workqueue.
142 	 */
143 	switch (++ctx->cur_step) {
144 	case STEP_DECRYPT:
145 		if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
146 			INIT_WORK(&ctx->work, decrypt_work);
147 			fscrypt_enqueue_decrypt_work(&ctx->work);
148 			return;
149 		}
150 		ctx->cur_step++;
151 		/* fall-through */
152 	case STEP_VERITY:
153 		if (ctx->enabled_steps & (1 << STEP_VERITY)) {
154 			INIT_WORK(&ctx->work, verity_work);
155 			fsverity_enqueue_verify_work(&ctx->work);
156 			return;
157 		}
158 		ctx->cur_step++;
159 		/* fall-through */
160 	default:
161 		__read_end_io(ctx->bio);
162 	}
163 }
164 
f2fs_bio_post_read_required(struct bio * bio)165 static bool f2fs_bio_post_read_required(struct bio *bio)
166 {
167 	return bio->bi_private && !bio->bi_status;
168 }
169 
f2fs_read_end_io(struct bio * bio)170 static void f2fs_read_end_io(struct bio *bio)
171 {
172 	struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
173 
174 	if (time_to_inject(sbi, FAULT_READ_IO)) {
175 		f2fs_show_injection_info(sbi, FAULT_READ_IO);
176 		bio->bi_status = BLK_STS_IOERR;
177 	}
178 
179 	if (f2fs_bio_post_read_required(bio)) {
180 		struct bio_post_read_ctx *ctx = bio->bi_private;
181 
182 		ctx->cur_step = STEP_INITIAL;
183 		bio_post_read_processing(ctx);
184 		return;
185 	}
186 
187 	__read_end_io(bio);
188 }
189 
f2fs_write_end_io(struct bio * bio)190 static void f2fs_write_end_io(struct bio *bio)
191 {
192 	struct f2fs_sb_info *sbi = bio->bi_private;
193 	struct bio_vec *bvec;
194 	struct bvec_iter_all iter_all;
195 
196 	if (time_to_inject(sbi, FAULT_WRITE_IO)) {
197 		f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
198 		bio->bi_status = BLK_STS_IOERR;
199 	}
200 
201 	bio_for_each_segment_all(bvec, bio, iter_all) {
202 		struct page *page = bvec->bv_page;
203 		enum count_type type = WB_DATA_TYPE(page);
204 
205 		if (IS_DUMMY_WRITTEN_PAGE(page)) {
206 			set_page_private(page, (unsigned long)NULL);
207 			ClearPagePrivate(page);
208 			unlock_page(page);
209 			mempool_free(page, sbi->write_io_dummy);
210 
211 			if (unlikely(bio->bi_status))
212 				f2fs_stop_checkpoint(sbi, true);
213 			continue;
214 		}
215 
216 		fscrypt_finalize_bounce_page(&page);
217 
218 		if (unlikely(bio->bi_status)) {
219 			mapping_set_error(page->mapping, -EIO);
220 			if (type == F2FS_WB_CP_DATA)
221 				f2fs_stop_checkpoint(sbi, true);
222 		}
223 
224 		f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
225 					page->index != nid_of_node(page));
226 
227 		dec_page_count(sbi, type);
228 		if (f2fs_in_warm_node_list(sbi, page))
229 			f2fs_del_fsync_node_entry(sbi, page);
230 		clear_cold_data(page);
231 		end_page_writeback(page);
232 	}
233 	if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
234 				wq_has_sleeper(&sbi->cp_wait))
235 		wake_up(&sbi->cp_wait);
236 
237 	bio_put(bio);
238 }
239 
240 /*
241  * Return true, if pre_bio's bdev is same as its target device.
242  */
f2fs_target_device(struct f2fs_sb_info * sbi,block_t blk_addr,struct bio * bio)243 struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
244 				block_t blk_addr, struct bio *bio)
245 {
246 	struct block_device *bdev = sbi->sb->s_bdev;
247 	int i;
248 
249 	if (f2fs_is_multi_device(sbi)) {
250 		for (i = 0; i < sbi->s_ndevs; i++) {
251 			if (FDEV(i).start_blk <= blk_addr &&
252 			    FDEV(i).end_blk >= blk_addr) {
253 				blk_addr -= FDEV(i).start_blk;
254 				bdev = FDEV(i).bdev;
255 				break;
256 			}
257 		}
258 	}
259 	if (bio) {
260 		bio_set_dev(bio, bdev);
261 		bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
262 	}
263 	return bdev;
264 }
265 
f2fs_target_device_index(struct f2fs_sb_info * sbi,block_t blkaddr)266 int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
267 {
268 	int i;
269 
270 	if (!f2fs_is_multi_device(sbi))
271 		return 0;
272 
273 	for (i = 0; i < sbi->s_ndevs; i++)
274 		if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
275 			return i;
276 	return 0;
277 }
278 
__same_bdev(struct f2fs_sb_info * sbi,block_t blk_addr,struct bio * bio)279 static bool __same_bdev(struct f2fs_sb_info *sbi,
280 				block_t blk_addr, struct bio *bio)
281 {
282 	struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
283 	return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
284 }
285 
286 /*
287  * Low-level block read/write IO operations.
288  */
__bio_alloc(struct f2fs_io_info * fio,int npages)289 static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
290 {
291 	struct f2fs_sb_info *sbi = fio->sbi;
292 	struct bio *bio;
293 
294 	bio = f2fs_bio_alloc(sbi, npages, true);
295 
296 	f2fs_target_device(sbi, fio->new_blkaddr, bio);
297 	if (is_read_io(fio->op)) {
298 		bio->bi_end_io = f2fs_read_end_io;
299 		bio->bi_private = NULL;
300 	} else {
301 		bio->bi_end_io = f2fs_write_end_io;
302 		bio->bi_private = sbi;
303 		bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
304 						fio->type, fio->temp);
305 	}
306 	if (fio->io_wbc)
307 		wbc_init_bio(fio->io_wbc, bio);
308 
309 	return bio;
310 }
311 
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)312 static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
313 				  pgoff_t first_idx,
314 				  const struct f2fs_io_info *fio,
315 				  gfp_t gfp_mask)
316 {
317 	/*
318 	 * The f2fs garbage collector sets ->encrypted_page when it wants to
319 	 * read/write raw data without encryption.
320 	 */
321 	if (!fio || !fio->encrypted_page)
322 		fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
323 	else if (fscrypt_inode_should_skip_dm_default_key(inode))
324 		bio_set_skip_dm_default_key(bio);
325 }
326 
f2fs_crypt_mergeable_bio(struct bio * bio,const struct inode * inode,pgoff_t next_idx,const struct f2fs_io_info * fio)327 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
328 				     pgoff_t next_idx,
329 				     const struct f2fs_io_info *fio)
330 {
331 	/*
332 	 * The f2fs garbage collector sets ->encrypted_page when it wants to
333 	 * read/write raw data without encryption.
334 	 */
335 	if (fio && fio->encrypted_page)
336 		return !bio_has_crypt_ctx(bio) &&
337 			(bio_should_skip_dm_default_key(bio) ==
338 			 fscrypt_inode_should_skip_dm_default_key(inode));
339 
340 	return fscrypt_mergeable_bio(bio, inode, next_idx);
341 }
342 
__submit_bio(struct f2fs_sb_info * sbi,struct bio * bio,enum page_type type)343 static inline void __submit_bio(struct f2fs_sb_info *sbi,
344 				struct bio *bio, enum page_type type)
345 {
346 	if (!is_read_io(bio_op(bio))) {
347 		unsigned int start;
348 
349 		if (type != DATA && type != NODE)
350 			goto submit_io;
351 
352 		if (test_opt(sbi, LFS) && current->plug)
353 			blk_finish_plug(current->plug);
354 
355 		if (F2FS_IO_ALIGNED(sbi))
356 			goto submit_io;
357 
358 		start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
359 		start %= F2FS_IO_SIZE(sbi);
360 
361 		if (start == 0)
362 			goto submit_io;
363 
364 		/* fill dummy pages */
365 		for (; start < F2FS_IO_SIZE(sbi); start++) {
366 			struct page *page =
367 				mempool_alloc(sbi->write_io_dummy,
368 					      GFP_NOIO | __GFP_NOFAIL);
369 			f2fs_bug_on(sbi, !page);
370 
371 			zero_user_segment(page, 0, PAGE_SIZE);
372 			SetPagePrivate(page);
373 			set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
374 			lock_page(page);
375 			if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
376 				f2fs_bug_on(sbi, 1);
377 		}
378 		/*
379 		 * In the NODE case, we lose next block address chain. So, we
380 		 * need to do checkpoint in f2fs_sync_file.
381 		 */
382 		if (type == NODE)
383 			set_sbi_flag(sbi, SBI_NEED_CP);
384 	}
385 submit_io:
386 	if (is_read_io(bio_op(bio)))
387 		trace_f2fs_submit_read_bio(sbi->sb, type, bio);
388 	else
389 		trace_f2fs_submit_write_bio(sbi->sb, type, bio);
390 	submit_bio(bio);
391 }
392 
__submit_merged_bio(struct f2fs_bio_info * io)393 static void __submit_merged_bio(struct f2fs_bio_info *io)
394 {
395 	struct f2fs_io_info *fio = &io->fio;
396 
397 	if (!io->bio)
398 		return;
399 
400 	bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
401 
402 	if (is_read_io(fio->op))
403 		trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
404 	else
405 		trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
406 
407 	__submit_bio(io->sbi, io->bio, fio->type);
408 	io->bio = NULL;
409 }
410 
__has_merged_page(struct bio * bio,struct inode * inode,struct page * page,nid_t ino)411 static bool __has_merged_page(struct bio *bio, struct inode *inode,
412 						struct page *page, nid_t ino)
413 {
414 	struct bio_vec *bvec;
415 	struct page *target;
416 	struct bvec_iter_all iter_all;
417 
418 	if (!bio)
419 		return false;
420 
421 	if (!inode && !page && !ino)
422 		return true;
423 
424 	bio_for_each_segment_all(bvec, bio, iter_all) {
425 
426 		target = bvec->bv_page;
427 		if (fscrypt_is_bounce_page(target))
428 			target = fscrypt_pagecache_page(target);
429 
430 		if (inode && inode == target->mapping->host)
431 			return true;
432 		if (page && page == target)
433 			return true;
434 		if (ino && ino == ino_of_node(target))
435 			return true;
436 	}
437 
438 	return false;
439 }
440 
__f2fs_submit_merged_write(struct f2fs_sb_info * sbi,enum page_type type,enum temp_type temp)441 static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
442 				enum page_type type, enum temp_type temp)
443 {
444 	enum page_type btype = PAGE_TYPE_OF_BIO(type);
445 	struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
446 
447 	down_write(&io->io_rwsem);
448 
449 	/* change META to META_FLUSH in the checkpoint procedure */
450 	if (type >= META_FLUSH) {
451 		io->fio.type = META_FLUSH;
452 		io->fio.op = REQ_OP_WRITE;
453 		io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
454 		if (!test_opt(sbi, NOBARRIER))
455 			io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
456 	}
457 	__submit_merged_bio(io);
458 	up_write(&io->io_rwsem);
459 }
460 
__submit_merged_write_cond(struct f2fs_sb_info * sbi,struct inode * inode,struct page * page,nid_t ino,enum page_type type,bool force)461 static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
462 				struct inode *inode, struct page *page,
463 				nid_t ino, enum page_type type, bool force)
464 {
465 	enum temp_type temp;
466 	bool ret = true;
467 
468 	for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
469 		if (!force)	{
470 			enum page_type btype = PAGE_TYPE_OF_BIO(type);
471 			struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
472 
473 			down_read(&io->io_rwsem);
474 			ret = __has_merged_page(io->bio, inode, page, ino);
475 			up_read(&io->io_rwsem);
476 		}
477 		if (ret)
478 			__f2fs_submit_merged_write(sbi, type, temp);
479 
480 		/* TODO: use HOT temp only for meta pages now. */
481 		if (type >= META)
482 			break;
483 	}
484 }
485 
f2fs_submit_merged_write(struct f2fs_sb_info * sbi,enum page_type type)486 void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
487 {
488 	__submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
489 }
490 
f2fs_submit_merged_write_cond(struct f2fs_sb_info * sbi,struct inode * inode,struct page * page,nid_t ino,enum page_type type)491 void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
492 				struct inode *inode, struct page *page,
493 				nid_t ino, enum page_type type)
494 {
495 	__submit_merged_write_cond(sbi, inode, page, ino, type, false);
496 }
497 
f2fs_flush_merged_writes(struct f2fs_sb_info * sbi)498 void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
499 {
500 	f2fs_submit_merged_write(sbi, DATA);
501 	f2fs_submit_merged_write(sbi, NODE);
502 	f2fs_submit_merged_write(sbi, META);
503 }
504 
505 /*
506  * Fill the locked page with data located in the block address.
507  * A caller needs to unlock the page on failure.
508  */
f2fs_submit_page_bio(struct f2fs_io_info * fio)509 int f2fs_submit_page_bio(struct f2fs_io_info *fio)
510 {
511 	struct bio *bio;
512 	struct page *page = fio->encrypted_page ?
513 			fio->encrypted_page : fio->page;
514 
515 	if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
516 			fio->is_por ? META_POR : (__is_meta_io(fio) ?
517 			META_GENERIC : DATA_GENERIC_ENHANCE)))
518 		return -EFSCORRUPTED;
519 
520 	trace_f2fs_submit_page_bio(page, fio);
521 	f2fs_trace_ios(fio, 0);
522 
523 	/* Allocate a new bio */
524 	bio = __bio_alloc(fio, 1);
525 
526 	f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
527 			       fio->page->index, fio, GFP_NOIO);
528 
529 	if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
530 		bio_put(bio);
531 		return -EFAULT;
532 	}
533 
534 	if (fio->io_wbc && !is_read_io(fio->op))
535 		wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
536 
537 	bio_set_op_attrs(bio, fio->op, fio->op_flags);
538 
539 	inc_page_count(fio->sbi, is_read_io(fio->op) ?
540 			__read_io_type(page): WB_DATA_TYPE(fio->page));
541 
542 	__submit_bio(fio->sbi, bio, fio->type);
543 	return 0;
544 }
545 
page_is_mergeable(struct f2fs_sb_info * sbi,struct bio * bio,block_t last_blkaddr,block_t cur_blkaddr)546 static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
547 				block_t last_blkaddr, block_t cur_blkaddr)
548 {
549 	if (last_blkaddr + 1 != cur_blkaddr)
550 		return false;
551 	return __same_bdev(sbi, cur_blkaddr, bio);
552 }
553 
io_type_is_mergeable(struct f2fs_bio_info * io,struct f2fs_io_info * fio)554 static bool io_type_is_mergeable(struct f2fs_bio_info *io,
555 						struct f2fs_io_info *fio)
556 {
557 	if (io->fio.op != fio->op)
558 		return false;
559 	return io->fio.op_flags == fio->op_flags;
560 }
561 
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)562 static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
563 					struct f2fs_bio_info *io,
564 					struct f2fs_io_info *fio,
565 					block_t last_blkaddr,
566 					block_t cur_blkaddr)
567 {
568 	if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
569 		unsigned int filled_blocks =
570 				F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
571 		unsigned int io_size = F2FS_IO_SIZE(sbi);
572 		unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
573 
574 		/* IOs in bio is aligned and left space of vectors is not enough */
575 		if (!(filled_blocks % io_size) && left_vecs < io_size)
576 			return false;
577 	}
578 	if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
579 		return false;
580 	return io_type_is_mergeable(io, fio);
581 }
582 
add_bio_entry(struct f2fs_sb_info * sbi,struct bio * bio,struct page * page,enum temp_type temp)583 static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
584 				struct page *page, enum temp_type temp)
585 {
586 	struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
587 	struct bio_entry *be;
588 
589 	be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
590 	be->bio = bio;
591 	bio_get(bio);
592 
593 	if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
594 		f2fs_bug_on(sbi, 1);
595 
596 	down_write(&io->bio_list_lock);
597 	list_add_tail(&be->list, &io->bio_list);
598 	up_write(&io->bio_list_lock);
599 }
600 
del_bio_entry(struct bio_entry * be)601 static void del_bio_entry(struct bio_entry *be)
602 {
603 	list_del(&be->list);
604 	kmem_cache_free(bio_entry_slab, be);
605 }
606 
add_ipu_page(struct f2fs_sb_info * sbi,struct bio ** bio,struct page * page)607 static int add_ipu_page(struct f2fs_sb_info *sbi, struct bio **bio,
608 							struct page *page)
609 {
610 	enum temp_type temp;
611 	bool found = false;
612 	int ret = -EAGAIN;
613 
614 	for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
615 		struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
616 		struct list_head *head = &io->bio_list;
617 		struct bio_entry *be;
618 
619 		down_write(&io->bio_list_lock);
620 		list_for_each_entry(be, head, list) {
621 			if (be->bio != *bio)
622 				continue;
623 
624 			found = true;
625 
626 			if (bio_add_page(*bio, page, PAGE_SIZE, 0) == PAGE_SIZE) {
627 				ret = 0;
628 				break;
629 			}
630 
631 			/* bio is full */
632 			del_bio_entry(be);
633 			__submit_bio(sbi, *bio, DATA);
634 			break;
635 		}
636 		up_write(&io->bio_list_lock);
637 	}
638 
639 	if (ret) {
640 		bio_put(*bio);
641 		*bio = NULL;
642 	}
643 
644 	return ret;
645 }
646 
f2fs_submit_merged_ipu_write(struct f2fs_sb_info * sbi,struct bio ** bio,struct page * page)647 void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
648 					struct bio **bio, struct page *page)
649 {
650 	enum temp_type temp;
651 	bool found = false;
652 	struct bio *target = bio ? *bio : NULL;
653 
654 	for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
655 		struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
656 		struct list_head *head = &io->bio_list;
657 		struct bio_entry *be;
658 
659 		if (list_empty(head))
660 			continue;
661 
662 		down_read(&io->bio_list_lock);
663 		list_for_each_entry(be, head, list) {
664 			if (target)
665 				found = (target == be->bio);
666 			else
667 				found = __has_merged_page(be->bio, NULL,
668 								page, 0);
669 			if (found)
670 				break;
671 		}
672 		up_read(&io->bio_list_lock);
673 
674 		if (!found)
675 			continue;
676 
677 		found = false;
678 
679 		down_write(&io->bio_list_lock);
680 		list_for_each_entry(be, head, list) {
681 			if (target)
682 				found = (target == be->bio);
683 			else
684 				found = __has_merged_page(be->bio, NULL,
685 								page, 0);
686 			if (found) {
687 				target = be->bio;
688 				del_bio_entry(be);
689 				break;
690 			}
691 		}
692 		up_write(&io->bio_list_lock);
693 	}
694 
695 	if (found)
696 		__submit_bio(sbi, target, DATA);
697 	if (bio && *bio) {
698 		bio_put(*bio);
699 		*bio = NULL;
700 	}
701 }
702 
f2fs_merge_page_bio(struct f2fs_io_info * fio)703 int f2fs_merge_page_bio(struct f2fs_io_info *fio)
704 {
705 	struct bio *bio = *fio->bio;
706 	struct page *page = fio->encrypted_page ?
707 			fio->encrypted_page : fio->page;
708 
709 	if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
710 			__is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
711 		return -EFSCORRUPTED;
712 
713 	trace_f2fs_submit_page_bio(page, fio);
714 	f2fs_trace_ios(fio, 0);
715 
716 	if (bio && (!page_is_mergeable(fio->sbi, bio, *fio->last_block,
717 				       fio->new_blkaddr) ||
718 		    !f2fs_crypt_mergeable_bio(bio, fio->page->mapping->host,
719 					      fio->page->index, fio)))
720 		f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
721 alloc_new:
722 	if (!bio) {
723 		bio = __bio_alloc(fio, BIO_MAX_PAGES);
724 		f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
725 				       fio->page->index, fio,
726 				       GFP_NOIO);
727 		bio_set_op_attrs(bio, fio->op, fio->op_flags);
728 
729 		add_bio_entry(fio->sbi, bio, page, fio->temp);
730 	} else {
731 		if (add_ipu_page(fio->sbi, &bio, page))
732 			goto alloc_new;
733 	}
734 
735 	if (fio->io_wbc)
736 		wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
737 
738 	inc_page_count(fio->sbi, WB_DATA_TYPE(page));
739 
740 	*fio->last_block = fio->new_blkaddr;
741 	*fio->bio = bio;
742 
743 	return 0;
744 }
745 
f2fs_submit_page_write(struct f2fs_io_info * fio)746 void f2fs_submit_page_write(struct f2fs_io_info *fio)
747 {
748 	struct f2fs_sb_info *sbi = fio->sbi;
749 	enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
750 	struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
751 	struct page *bio_page;
752 
753 	f2fs_bug_on(sbi, is_read_io(fio->op));
754 
755 	down_write(&io->io_rwsem);
756 next:
757 	if (fio->in_list) {
758 		spin_lock(&io->io_lock);
759 		if (list_empty(&io->io_list)) {
760 			spin_unlock(&io->io_lock);
761 			goto out;
762 		}
763 		fio = list_first_entry(&io->io_list,
764 						struct f2fs_io_info, list);
765 		list_del(&fio->list);
766 		spin_unlock(&io->io_lock);
767 	}
768 
769 	verify_fio_blkaddr(fio);
770 
771 	bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
772 
773 	/* set submitted = true as a return value */
774 	fio->submitted = true;
775 
776 	inc_page_count(sbi, WB_DATA_TYPE(bio_page));
777 
778 	if (io->bio &&
779 	    (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
780 			      fio->new_blkaddr) ||
781 	     !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
782 				       fio->page->index, fio)))
783 		__submit_merged_bio(io);
784 alloc_new:
785 	if (io->bio == NULL) {
786 		if (F2FS_IO_ALIGNED(sbi) &&
787 				(fio->type == DATA || fio->type == NODE) &&
788 				fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
789 			dec_page_count(sbi, WB_DATA_TYPE(bio_page));
790 			fio->retry = true;
791 			goto skip;
792 		}
793 		io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
794 		f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
795 				       fio->page->index, fio,
796 				       GFP_NOIO);
797 		io->fio = *fio;
798 	}
799 
800 	if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
801 		__submit_merged_bio(io);
802 		goto alloc_new;
803 	}
804 
805 	if (fio->io_wbc)
806 		wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
807 
808 	io->last_block_in_bio = fio->new_blkaddr;
809 	f2fs_trace_ios(fio, 0);
810 
811 	trace_f2fs_submit_page_write(fio->page, fio);
812 skip:
813 	if (fio->in_list)
814 		goto next;
815 out:
816 	if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
817 				!f2fs_is_checkpoint_ready(sbi))
818 		__submit_merged_bio(io);
819 	up_write(&io->io_rwsem);
820 }
821 
f2fs_need_verity(const struct inode * inode,pgoff_t idx)822 static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
823 {
824 	return fsverity_active(inode) &&
825 	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
826 }
827 
f2fs_grab_read_bio(struct inode * inode,block_t blkaddr,unsigned nr_pages,unsigned op_flag,pgoff_t first_idx)828 static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
829 				      unsigned nr_pages, unsigned op_flag,
830 				      pgoff_t first_idx)
831 {
832 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
833 	struct bio *bio;
834 	struct bio_post_read_ctx *ctx;
835 	unsigned int post_read_steps = 0;
836 
837 	bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
838 	if (!bio)
839 		return ERR_PTR(-ENOMEM);
840 
841 	f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
842 
843 	f2fs_target_device(sbi, blkaddr, bio);
844 	bio->bi_end_io = f2fs_read_end_io;
845 	bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
846 
847 	if (fscrypt_inode_uses_fs_layer_crypto(inode))
848 		post_read_steps |= 1 << STEP_DECRYPT;
849 
850 	if (f2fs_need_verity(inode, first_idx))
851 		post_read_steps |= 1 << STEP_VERITY;
852 
853 	if (post_read_steps) {
854 		ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
855 		if (!ctx) {
856 			bio_put(bio);
857 			return ERR_PTR(-ENOMEM);
858 		}
859 		ctx->bio = bio;
860 		ctx->enabled_steps = post_read_steps;
861 		bio->bi_private = ctx;
862 	}
863 
864 	return bio;
865 }
866 
867 /* This can handle encryption stuffs */
f2fs_submit_page_read(struct inode * inode,struct page * page,block_t blkaddr)868 static int f2fs_submit_page_read(struct inode *inode, struct page *page,
869 							block_t blkaddr)
870 {
871 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
872 	struct bio *bio;
873 
874 	bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0, page->index);
875 	if (IS_ERR(bio))
876 		return PTR_ERR(bio);
877 
878 	/* wait for GCed page writeback via META_MAPPING */
879 	f2fs_wait_on_block_writeback(inode, blkaddr);
880 
881 	if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
882 		bio_put(bio);
883 		return -EFAULT;
884 	}
885 	ClearPageError(page);
886 	inc_page_count(sbi, F2FS_RD_DATA);
887 	__submit_bio(sbi, bio, DATA);
888 	return 0;
889 }
890 
__set_data_blkaddr(struct dnode_of_data * dn)891 static void __set_data_blkaddr(struct dnode_of_data *dn)
892 {
893 	struct f2fs_node *rn = F2FS_NODE(dn->node_page);
894 	__le32 *addr_array;
895 	int base = 0;
896 
897 	if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
898 		base = get_extra_isize(dn->inode);
899 
900 	/* Get physical address of data block */
901 	addr_array = blkaddr_in_node(rn);
902 	addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
903 }
904 
905 /*
906  * Lock ordering for the change of data block address:
907  * ->data_page
908  *  ->node_page
909  *    update block addresses in the node page
910  */
f2fs_set_data_blkaddr(struct dnode_of_data * dn)911 void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
912 {
913 	f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
914 	__set_data_blkaddr(dn);
915 	if (set_page_dirty(dn->node_page))
916 		dn->node_changed = true;
917 }
918 
f2fs_update_data_blkaddr(struct dnode_of_data * dn,block_t blkaddr)919 void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
920 {
921 	dn->data_blkaddr = blkaddr;
922 	f2fs_set_data_blkaddr(dn);
923 	f2fs_update_extent_cache(dn);
924 }
925 
926 /* 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)927 int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
928 {
929 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
930 	int err;
931 
932 	if (!count)
933 		return 0;
934 
935 	if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
936 		return -EPERM;
937 	if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
938 		return err;
939 
940 	trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
941 						dn->ofs_in_node, count);
942 
943 	f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
944 
945 	for (; count > 0; dn->ofs_in_node++) {
946 		block_t blkaddr = datablock_addr(dn->inode,
947 					dn->node_page, dn->ofs_in_node);
948 		if (blkaddr == NULL_ADDR) {
949 			dn->data_blkaddr = NEW_ADDR;
950 			__set_data_blkaddr(dn);
951 			count--;
952 		}
953 	}
954 
955 	if (set_page_dirty(dn->node_page))
956 		dn->node_changed = true;
957 	return 0;
958 }
959 
960 /* Should keep dn->ofs_in_node unchanged */
f2fs_reserve_new_block(struct dnode_of_data * dn)961 int f2fs_reserve_new_block(struct dnode_of_data *dn)
962 {
963 	unsigned int ofs_in_node = dn->ofs_in_node;
964 	int ret;
965 
966 	ret = f2fs_reserve_new_blocks(dn, 1);
967 	dn->ofs_in_node = ofs_in_node;
968 	return ret;
969 }
970 
f2fs_reserve_block(struct dnode_of_data * dn,pgoff_t index)971 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
972 {
973 	bool need_put = dn->inode_page ? false : true;
974 	int err;
975 
976 	err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
977 	if (err)
978 		return err;
979 
980 	if (dn->data_blkaddr == NULL_ADDR)
981 		err = f2fs_reserve_new_block(dn);
982 	if (err || need_put)
983 		f2fs_put_dnode(dn);
984 	return err;
985 }
986 
f2fs_get_block(struct dnode_of_data * dn,pgoff_t index)987 int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
988 {
989 	struct extent_info ei  = {0,0,0};
990 	struct inode *inode = dn->inode;
991 
992 	if (f2fs_lookup_extent_cache(inode, index, &ei)) {
993 		dn->data_blkaddr = ei.blk + index - ei.fofs;
994 		return 0;
995 	}
996 
997 	return f2fs_reserve_block(dn, index);
998 }
999 
f2fs_get_read_data_page(struct inode * inode,pgoff_t index,int op_flags,bool for_write)1000 struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1001 						int op_flags, bool for_write)
1002 {
1003 	struct address_space *mapping = inode->i_mapping;
1004 	struct dnode_of_data dn;
1005 	struct page *page;
1006 	struct extent_info ei = {0,0,0};
1007 	int err;
1008 
1009 	page = f2fs_grab_cache_page(mapping, index, for_write);
1010 	if (!page)
1011 		return ERR_PTR(-ENOMEM);
1012 
1013 	if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1014 		dn.data_blkaddr = ei.blk + index - ei.fofs;
1015 		if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1016 						DATA_GENERIC_ENHANCE_READ)) {
1017 			err = -EFSCORRUPTED;
1018 			goto put_err;
1019 		}
1020 		goto got_it;
1021 	}
1022 
1023 	set_new_dnode(&dn, inode, NULL, NULL, 0);
1024 	err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1025 	if (err)
1026 		goto put_err;
1027 	f2fs_put_dnode(&dn);
1028 
1029 	if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1030 		err = -ENOENT;
1031 		goto put_err;
1032 	}
1033 	if (dn.data_blkaddr != NEW_ADDR &&
1034 			!f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1035 						dn.data_blkaddr,
1036 						DATA_GENERIC_ENHANCE)) {
1037 		err = -EFSCORRUPTED;
1038 		goto put_err;
1039 	}
1040 got_it:
1041 	if (PageUptodate(page)) {
1042 		unlock_page(page);
1043 		return page;
1044 	}
1045 
1046 	/*
1047 	 * A new dentry page is allocated but not able to be written, since its
1048 	 * new inode page couldn't be allocated due to -ENOSPC.
1049 	 * In such the case, its blkaddr can be remained as NEW_ADDR.
1050 	 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1051 	 * f2fs_init_inode_metadata.
1052 	 */
1053 	if (dn.data_blkaddr == NEW_ADDR) {
1054 		zero_user_segment(page, 0, PAGE_SIZE);
1055 		if (!PageUptodate(page))
1056 			SetPageUptodate(page);
1057 		unlock_page(page);
1058 		return page;
1059 	}
1060 
1061 	err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
1062 	if (err)
1063 		goto put_err;
1064 	return page;
1065 
1066 put_err:
1067 	f2fs_put_page(page, 1);
1068 	return ERR_PTR(err);
1069 }
1070 
f2fs_find_data_page(struct inode * inode,pgoff_t index)1071 struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1072 {
1073 	struct address_space *mapping = inode->i_mapping;
1074 	struct page *page;
1075 
1076 	page = find_get_page(mapping, index);
1077 	if (page && PageUptodate(page))
1078 		return page;
1079 	f2fs_put_page(page, 0);
1080 
1081 	page = f2fs_get_read_data_page(inode, index, 0, false);
1082 	if (IS_ERR(page))
1083 		return page;
1084 
1085 	if (PageUptodate(page))
1086 		return page;
1087 
1088 	wait_on_page_locked(page);
1089 	if (unlikely(!PageUptodate(page))) {
1090 		f2fs_put_page(page, 0);
1091 		return ERR_PTR(-EIO);
1092 	}
1093 	return page;
1094 }
1095 
1096 /*
1097  * If it tries to access a hole, return an error.
1098  * Because, the callers, functions in dir.c and GC, should be able to know
1099  * whether this page exists or not.
1100  */
f2fs_get_lock_data_page(struct inode * inode,pgoff_t index,bool for_write)1101 struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1102 							bool for_write)
1103 {
1104 	struct address_space *mapping = inode->i_mapping;
1105 	struct page *page;
1106 repeat:
1107 	page = f2fs_get_read_data_page(inode, index, 0, for_write);
1108 	if (IS_ERR(page))
1109 		return page;
1110 
1111 	/* wait for read completion */
1112 	lock_page(page);
1113 	if (unlikely(page->mapping != mapping)) {
1114 		f2fs_put_page(page, 1);
1115 		goto repeat;
1116 	}
1117 	if (unlikely(!PageUptodate(page))) {
1118 		f2fs_put_page(page, 1);
1119 		return ERR_PTR(-EIO);
1120 	}
1121 	return page;
1122 }
1123 
1124 /*
1125  * Caller ensures that this data page is never allocated.
1126  * A new zero-filled data page is allocated in the page cache.
1127  *
1128  * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1129  * f2fs_unlock_op().
1130  * Note that, ipage is set only by make_empty_dir, and if any error occur,
1131  * ipage should be released by this function.
1132  */
f2fs_get_new_data_page(struct inode * inode,struct page * ipage,pgoff_t index,bool new_i_size)1133 struct page *f2fs_get_new_data_page(struct inode *inode,
1134 		struct page *ipage, pgoff_t index, bool new_i_size)
1135 {
1136 	struct address_space *mapping = inode->i_mapping;
1137 	struct page *page;
1138 	struct dnode_of_data dn;
1139 	int err;
1140 
1141 	page = f2fs_grab_cache_page(mapping, index, true);
1142 	if (!page) {
1143 		/*
1144 		 * before exiting, we should make sure ipage will be released
1145 		 * if any error occur.
1146 		 */
1147 		f2fs_put_page(ipage, 1);
1148 		return ERR_PTR(-ENOMEM);
1149 	}
1150 
1151 	set_new_dnode(&dn, inode, ipage, NULL, 0);
1152 	err = f2fs_reserve_block(&dn, index);
1153 	if (err) {
1154 		f2fs_put_page(page, 1);
1155 		return ERR_PTR(err);
1156 	}
1157 	if (!ipage)
1158 		f2fs_put_dnode(&dn);
1159 
1160 	if (PageUptodate(page))
1161 		goto got_it;
1162 
1163 	if (dn.data_blkaddr == NEW_ADDR) {
1164 		zero_user_segment(page, 0, PAGE_SIZE);
1165 		if (!PageUptodate(page))
1166 			SetPageUptodate(page);
1167 	} else {
1168 		f2fs_put_page(page, 1);
1169 
1170 		/* if ipage exists, blkaddr should be NEW_ADDR */
1171 		f2fs_bug_on(F2FS_I_SB(inode), ipage);
1172 		page = f2fs_get_lock_data_page(inode, index, true);
1173 		if (IS_ERR(page))
1174 			return page;
1175 	}
1176 got_it:
1177 	if (new_i_size && i_size_read(inode) <
1178 				((loff_t)(index + 1) << PAGE_SHIFT))
1179 		f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1180 	return page;
1181 }
1182 
__allocate_data_block(struct dnode_of_data * dn,int seg_type)1183 static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1184 {
1185 	struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1186 	struct f2fs_summary sum;
1187 	struct node_info ni;
1188 	block_t old_blkaddr;
1189 	blkcnt_t count = 1;
1190 	int err;
1191 
1192 	if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1193 		return -EPERM;
1194 
1195 	err = f2fs_get_node_info(sbi, dn->nid, &ni);
1196 	if (err)
1197 		return err;
1198 
1199 	dn->data_blkaddr = datablock_addr(dn->inode,
1200 				dn->node_page, dn->ofs_in_node);
1201 	if (dn->data_blkaddr != NULL_ADDR)
1202 		goto alloc;
1203 
1204 	if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1205 		return err;
1206 
1207 alloc:
1208 	set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1209 	old_blkaddr = dn->data_blkaddr;
1210 	f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1211 					&sum, seg_type, NULL, false);
1212 	if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1213 		invalidate_mapping_pages(META_MAPPING(sbi),
1214 					old_blkaddr, old_blkaddr);
1215 	f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1216 
1217 	/*
1218 	 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1219 	 * data from unwritten block via dio_read.
1220 	 */
1221 	return 0;
1222 }
1223 
f2fs_preallocate_blocks(struct kiocb * iocb,struct iov_iter * from)1224 int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1225 {
1226 	struct inode *inode = file_inode(iocb->ki_filp);
1227 	struct f2fs_map_blocks map;
1228 	int flag;
1229 	int err = 0;
1230 	bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1231 
1232 	/* convert inline data for Direct I/O*/
1233 	if (direct_io) {
1234 		err = f2fs_convert_inline_inode(inode);
1235 		if (err)
1236 			return err;
1237 	}
1238 
1239 	if (direct_io && allow_outplace_dio(inode, iocb, from))
1240 		return 0;
1241 
1242 	if (is_inode_flag_set(inode, FI_NO_PREALLOC))
1243 		return 0;
1244 
1245 	map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1246 	map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1247 	if (map.m_len > map.m_lblk)
1248 		map.m_len -= map.m_lblk;
1249 	else
1250 		map.m_len = 0;
1251 
1252 	map.m_next_pgofs = NULL;
1253 	map.m_next_extent = NULL;
1254 	map.m_seg_type = NO_CHECK_TYPE;
1255 	map.m_may_create = true;
1256 
1257 	if (direct_io) {
1258 		map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
1259 		flag = f2fs_force_buffered_io(inode, iocb, from) ?
1260 					F2FS_GET_BLOCK_PRE_AIO :
1261 					F2FS_GET_BLOCK_PRE_DIO;
1262 		goto map_blocks;
1263 	}
1264 	if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1265 		err = f2fs_convert_inline_inode(inode);
1266 		if (err)
1267 			return err;
1268 	}
1269 	if (f2fs_has_inline_data(inode))
1270 		return err;
1271 
1272 	flag = F2FS_GET_BLOCK_PRE_AIO;
1273 
1274 map_blocks:
1275 	err = f2fs_map_blocks(inode, &map, 1, flag);
1276 	if (map.m_len > 0 && err == -ENOSPC) {
1277 		if (!direct_io)
1278 			set_inode_flag(inode, FI_NO_PREALLOC);
1279 		err = 0;
1280 	}
1281 	return err;
1282 }
1283 
__do_map_lock(struct f2fs_sb_info * sbi,int flag,bool lock)1284 void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
1285 {
1286 	if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1287 		if (lock)
1288 			down_read(&sbi->node_change);
1289 		else
1290 			up_read(&sbi->node_change);
1291 	} else {
1292 		if (lock)
1293 			f2fs_lock_op(sbi);
1294 		else
1295 			f2fs_unlock_op(sbi);
1296 	}
1297 }
1298 
1299 /*
1300  * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1301  * f2fs_map_blocks structure.
1302  * If original data blocks are allocated, then give them to blockdev.
1303  * Otherwise,
1304  *     a. preallocate requested block addresses
1305  *     b. do not use extent cache for better performance
1306  *     c. give the block addresses to blockdev
1307  */
f2fs_map_blocks(struct inode * inode,struct f2fs_map_blocks * map,int create,int flag)1308 int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1309 						int create, int flag)
1310 {
1311 	unsigned int maxblocks = map->m_len;
1312 	struct dnode_of_data dn;
1313 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1314 	int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
1315 	pgoff_t pgofs, end_offset, end;
1316 	int err = 0, ofs = 1;
1317 	unsigned int ofs_in_node, last_ofs_in_node;
1318 	blkcnt_t prealloc;
1319 	struct extent_info ei = {0,0,0};
1320 	block_t blkaddr;
1321 	unsigned int start_pgofs;
1322 
1323 	if (!maxblocks)
1324 		return 0;
1325 
1326 	map->m_len = 0;
1327 	map->m_flags = 0;
1328 
1329 	/* it only supports block size == page size */
1330 	pgofs =	(pgoff_t)map->m_lblk;
1331 	end = pgofs + maxblocks;
1332 
1333 	if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1334 		if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1335 							map->m_may_create)
1336 			goto next_dnode;
1337 
1338 		map->m_pblk = ei.blk + pgofs - ei.fofs;
1339 		map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1340 		map->m_flags = F2FS_MAP_MAPPED;
1341 		if (map->m_next_extent)
1342 			*map->m_next_extent = pgofs + map->m_len;
1343 
1344 		/* for hardware encryption, but to avoid potential issue in future */
1345 		if (flag == F2FS_GET_BLOCK_DIO)
1346 			f2fs_wait_on_block_writeback_range(inode,
1347 						map->m_pblk, map->m_len);
1348 		goto out;
1349 	}
1350 
1351 next_dnode:
1352 	if (map->m_may_create)
1353 		__do_map_lock(sbi, flag, true);
1354 
1355 	/* When reading holes, we need its node page */
1356 	set_new_dnode(&dn, inode, NULL, NULL, 0);
1357 	err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1358 	if (err) {
1359 		if (flag == F2FS_GET_BLOCK_BMAP)
1360 			map->m_pblk = 0;
1361 		if (err == -ENOENT) {
1362 			err = 0;
1363 			if (map->m_next_pgofs)
1364 				*map->m_next_pgofs =
1365 					f2fs_get_next_page_offset(&dn, pgofs);
1366 			if (map->m_next_extent)
1367 				*map->m_next_extent =
1368 					f2fs_get_next_page_offset(&dn, pgofs);
1369 		}
1370 		goto unlock_out;
1371 	}
1372 
1373 	start_pgofs = pgofs;
1374 	prealloc = 0;
1375 	last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1376 	end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1377 
1378 next_block:
1379 	blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
1380 
1381 	if (__is_valid_data_blkaddr(blkaddr) &&
1382 		!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1383 		err = -EFSCORRUPTED;
1384 		goto sync_out;
1385 	}
1386 
1387 	if (__is_valid_data_blkaddr(blkaddr)) {
1388 		/* use out-place-update for driect IO under LFS mode */
1389 		if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1390 							map->m_may_create) {
1391 			err = __allocate_data_block(&dn, map->m_seg_type);
1392 			if (err)
1393 				goto sync_out;
1394 			blkaddr = dn.data_blkaddr;
1395 			set_inode_flag(inode, FI_APPEND_WRITE);
1396 		}
1397 	} else {
1398 		if (create) {
1399 			if (unlikely(f2fs_cp_error(sbi))) {
1400 				err = -EIO;
1401 				goto sync_out;
1402 			}
1403 			if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1404 				if (blkaddr == NULL_ADDR) {
1405 					prealloc++;
1406 					last_ofs_in_node = dn.ofs_in_node;
1407 				}
1408 			} else {
1409 				WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1410 					flag != F2FS_GET_BLOCK_DIO);
1411 				err = __allocate_data_block(&dn,
1412 							map->m_seg_type);
1413 				if (!err)
1414 					set_inode_flag(inode, FI_APPEND_WRITE);
1415 			}
1416 			if (err)
1417 				goto sync_out;
1418 			map->m_flags |= F2FS_MAP_NEW;
1419 			blkaddr = dn.data_blkaddr;
1420 		} else {
1421 			if (flag == F2FS_GET_BLOCK_BMAP) {
1422 				map->m_pblk = 0;
1423 				goto sync_out;
1424 			}
1425 			if (flag == F2FS_GET_BLOCK_PRECACHE)
1426 				goto sync_out;
1427 			if (flag == F2FS_GET_BLOCK_FIEMAP &&
1428 						blkaddr == NULL_ADDR) {
1429 				if (map->m_next_pgofs)
1430 					*map->m_next_pgofs = pgofs + 1;
1431 				goto sync_out;
1432 			}
1433 			if (flag != F2FS_GET_BLOCK_FIEMAP) {
1434 				/* for defragment case */
1435 				if (map->m_next_pgofs)
1436 					*map->m_next_pgofs = pgofs + 1;
1437 				goto sync_out;
1438 			}
1439 		}
1440 	}
1441 
1442 	if (flag == F2FS_GET_BLOCK_PRE_AIO)
1443 		goto skip;
1444 
1445 	if (map->m_len == 0) {
1446 		/* preallocated unwritten block should be mapped for fiemap. */
1447 		if (blkaddr == NEW_ADDR)
1448 			map->m_flags |= F2FS_MAP_UNWRITTEN;
1449 		map->m_flags |= F2FS_MAP_MAPPED;
1450 
1451 		map->m_pblk = blkaddr;
1452 		map->m_len = 1;
1453 	} else if ((map->m_pblk != NEW_ADDR &&
1454 			blkaddr == (map->m_pblk + ofs)) ||
1455 			(map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1456 			flag == F2FS_GET_BLOCK_PRE_DIO) {
1457 		ofs++;
1458 		map->m_len++;
1459 	} else {
1460 		goto sync_out;
1461 	}
1462 
1463 skip:
1464 	dn.ofs_in_node++;
1465 	pgofs++;
1466 
1467 	/* preallocate blocks in batch for one dnode page */
1468 	if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1469 			(pgofs == end || dn.ofs_in_node == end_offset)) {
1470 
1471 		dn.ofs_in_node = ofs_in_node;
1472 		err = f2fs_reserve_new_blocks(&dn, prealloc);
1473 		if (err)
1474 			goto sync_out;
1475 
1476 		map->m_len += dn.ofs_in_node - ofs_in_node;
1477 		if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1478 			err = -ENOSPC;
1479 			goto sync_out;
1480 		}
1481 		dn.ofs_in_node = end_offset;
1482 	}
1483 
1484 	if (pgofs >= end)
1485 		goto sync_out;
1486 	else if (dn.ofs_in_node < end_offset)
1487 		goto next_block;
1488 
1489 	if (flag == F2FS_GET_BLOCK_PRECACHE) {
1490 		if (map->m_flags & F2FS_MAP_MAPPED) {
1491 			unsigned int ofs = start_pgofs - map->m_lblk;
1492 
1493 			f2fs_update_extent_cache_range(&dn,
1494 				start_pgofs, map->m_pblk + ofs,
1495 				map->m_len - ofs);
1496 		}
1497 	}
1498 
1499 	f2fs_put_dnode(&dn);
1500 
1501 	if (map->m_may_create) {
1502 		__do_map_lock(sbi, flag, false);
1503 		f2fs_balance_fs(sbi, dn.node_changed);
1504 	}
1505 	goto next_dnode;
1506 
1507 sync_out:
1508 
1509 	/* for hardware encryption, but to avoid potential issue in future */
1510 	if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1511 		f2fs_wait_on_block_writeback_range(inode,
1512 						map->m_pblk, map->m_len);
1513 
1514 	if (flag == F2FS_GET_BLOCK_PRECACHE) {
1515 		if (map->m_flags & F2FS_MAP_MAPPED) {
1516 			unsigned int ofs = start_pgofs - map->m_lblk;
1517 
1518 			f2fs_update_extent_cache_range(&dn,
1519 				start_pgofs, map->m_pblk + ofs,
1520 				map->m_len - ofs);
1521 		}
1522 		if (map->m_next_extent)
1523 			*map->m_next_extent = pgofs + 1;
1524 	}
1525 	f2fs_put_dnode(&dn);
1526 unlock_out:
1527 	if (map->m_may_create) {
1528 		__do_map_lock(sbi, flag, false);
1529 		f2fs_balance_fs(sbi, dn.node_changed);
1530 	}
1531 out:
1532 	trace_f2fs_map_blocks(inode, map, err);
1533 	return err;
1534 }
1535 
f2fs_overwrite_io(struct inode * inode,loff_t pos,size_t len)1536 bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1537 {
1538 	struct f2fs_map_blocks map;
1539 	block_t last_lblk;
1540 	int err;
1541 
1542 	if (pos + len > i_size_read(inode))
1543 		return false;
1544 
1545 	map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1546 	map.m_next_pgofs = NULL;
1547 	map.m_next_extent = NULL;
1548 	map.m_seg_type = NO_CHECK_TYPE;
1549 	map.m_may_create = false;
1550 	last_lblk = F2FS_BLK_ALIGN(pos + len);
1551 
1552 	while (map.m_lblk < last_lblk) {
1553 		map.m_len = last_lblk - map.m_lblk;
1554 		err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1555 		if (err || map.m_len == 0)
1556 			return false;
1557 		map.m_lblk += map.m_len;
1558 	}
1559 	return true;
1560 }
1561 
__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)1562 static int __get_data_block(struct inode *inode, sector_t iblock,
1563 			struct buffer_head *bh, int create, int flag,
1564 			pgoff_t *next_pgofs, int seg_type, bool may_write)
1565 {
1566 	struct f2fs_map_blocks map;
1567 	int err;
1568 
1569 	map.m_lblk = iblock;
1570 	map.m_len = bh->b_size >> inode->i_blkbits;
1571 	map.m_next_pgofs = next_pgofs;
1572 	map.m_next_extent = NULL;
1573 	map.m_seg_type = seg_type;
1574 	map.m_may_create = may_write;
1575 
1576 	err = f2fs_map_blocks(inode, &map, create, flag);
1577 	if (!err) {
1578 		map_bh(bh, inode->i_sb, map.m_pblk);
1579 		bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1580 		bh->b_size = (u64)map.m_len << inode->i_blkbits;
1581 	}
1582 	return err;
1583 }
1584 
get_data_block(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create,int flag,pgoff_t * next_pgofs)1585 static int get_data_block(struct inode *inode, sector_t iblock,
1586 			struct buffer_head *bh_result, int create, int flag,
1587 			pgoff_t *next_pgofs)
1588 {
1589 	return __get_data_block(inode, iblock, bh_result, create,
1590 							flag, next_pgofs,
1591 							NO_CHECK_TYPE, create);
1592 }
1593 
get_data_block_dio_write(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)1594 static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1595 			struct buffer_head *bh_result, int create)
1596 {
1597 	return __get_data_block(inode, iblock, bh_result, create,
1598 				F2FS_GET_BLOCK_DIO, NULL,
1599 				f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1600 				IS_SWAPFILE(inode) ? false : true);
1601 }
1602 
get_data_block_dio(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)1603 static int get_data_block_dio(struct inode *inode, sector_t iblock,
1604 			struct buffer_head *bh_result, int create)
1605 {
1606 	return __get_data_block(inode, iblock, bh_result, create,
1607 				F2FS_GET_BLOCK_DIO, NULL,
1608 				f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1609 				false);
1610 }
1611 
get_data_block_bmap(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)1612 static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1613 			struct buffer_head *bh_result, int create)
1614 {
1615 	/* Block number less than F2FS MAX BLOCKS */
1616 	if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1617 		return -EFBIG;
1618 
1619 	return __get_data_block(inode, iblock, bh_result, create,
1620 						F2FS_GET_BLOCK_BMAP, NULL,
1621 						NO_CHECK_TYPE, create);
1622 }
1623 
logical_to_blk(struct inode * inode,loff_t offset)1624 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1625 {
1626 	return (offset >> inode->i_blkbits);
1627 }
1628 
blk_to_logical(struct inode * inode,sector_t blk)1629 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1630 {
1631 	return (blk << inode->i_blkbits);
1632 }
1633 
f2fs_xattr_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo)1634 static int f2fs_xattr_fiemap(struct inode *inode,
1635 				struct fiemap_extent_info *fieinfo)
1636 {
1637 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1638 	struct page *page;
1639 	struct node_info ni;
1640 	__u64 phys = 0, len;
1641 	__u32 flags;
1642 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1643 	int err = 0;
1644 
1645 	if (f2fs_has_inline_xattr(inode)) {
1646 		int offset;
1647 
1648 		page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1649 						inode->i_ino, false);
1650 		if (!page)
1651 			return -ENOMEM;
1652 
1653 		err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1654 		if (err) {
1655 			f2fs_put_page(page, 1);
1656 			return err;
1657 		}
1658 
1659 		phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1660 		offset = offsetof(struct f2fs_inode, i_addr) +
1661 					sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1662 					get_inline_xattr_addrs(inode));
1663 
1664 		phys += offset;
1665 		len = inline_xattr_size(inode);
1666 
1667 		f2fs_put_page(page, 1);
1668 
1669 		flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1670 
1671 		if (!xnid)
1672 			flags |= FIEMAP_EXTENT_LAST;
1673 
1674 		err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1675 		if (err || err == 1)
1676 			return err;
1677 	}
1678 
1679 	if (xnid) {
1680 		page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1681 		if (!page)
1682 			return -ENOMEM;
1683 
1684 		err = f2fs_get_node_info(sbi, xnid, &ni);
1685 		if (err) {
1686 			f2fs_put_page(page, 1);
1687 			return err;
1688 		}
1689 
1690 		phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1691 		len = inode->i_sb->s_blocksize;
1692 
1693 		f2fs_put_page(page, 1);
1694 
1695 		flags = FIEMAP_EXTENT_LAST;
1696 	}
1697 
1698 	if (phys)
1699 		err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1700 
1701 	return (err < 0 ? err : 0);
1702 }
1703 
f2fs_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,u64 start,u64 len)1704 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1705 		u64 start, u64 len)
1706 {
1707 	struct buffer_head map_bh;
1708 	sector_t start_blk, last_blk;
1709 	pgoff_t next_pgofs;
1710 	u64 logical = 0, phys = 0, size = 0;
1711 	u32 flags = 0;
1712 	int ret = 0;
1713 
1714 	if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1715 		ret = f2fs_precache_extents(inode);
1716 		if (ret)
1717 			return ret;
1718 	}
1719 
1720 	ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
1721 	if (ret)
1722 		return ret;
1723 
1724 	inode_lock(inode);
1725 
1726 	if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1727 		ret = f2fs_xattr_fiemap(inode, fieinfo);
1728 		goto out;
1729 	}
1730 
1731 	if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
1732 		ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1733 		if (ret != -EAGAIN)
1734 			goto out;
1735 	}
1736 
1737 	if (logical_to_blk(inode, len) == 0)
1738 		len = blk_to_logical(inode, 1);
1739 
1740 	start_blk = logical_to_blk(inode, start);
1741 	last_blk = logical_to_blk(inode, start + len - 1);
1742 
1743 next:
1744 	memset(&map_bh, 0, sizeof(struct buffer_head));
1745 	map_bh.b_size = len;
1746 
1747 	ret = get_data_block(inode, start_blk, &map_bh, 0,
1748 					F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1749 	if (ret)
1750 		goto out;
1751 
1752 	/* HOLE */
1753 	if (!buffer_mapped(&map_bh)) {
1754 		start_blk = next_pgofs;
1755 
1756 		if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1757 					F2FS_I_SB(inode)->max_file_blocks))
1758 			goto prep_next;
1759 
1760 		flags |= FIEMAP_EXTENT_LAST;
1761 	}
1762 
1763 	if (size) {
1764 		if (IS_ENCRYPTED(inode))
1765 			flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1766 
1767 		ret = fiemap_fill_next_extent(fieinfo, logical,
1768 				phys, size, flags);
1769 	}
1770 
1771 	if (start_blk > last_blk || ret)
1772 		goto out;
1773 
1774 	logical = blk_to_logical(inode, start_blk);
1775 	phys = blk_to_logical(inode, map_bh.b_blocknr);
1776 	size = map_bh.b_size;
1777 	flags = 0;
1778 	if (buffer_unwritten(&map_bh))
1779 		flags = FIEMAP_EXTENT_UNWRITTEN;
1780 
1781 	start_blk += logical_to_blk(inode, size);
1782 
1783 prep_next:
1784 	cond_resched();
1785 	if (fatal_signal_pending(current))
1786 		ret = -EINTR;
1787 	else
1788 		goto next;
1789 out:
1790 	if (ret == 1)
1791 		ret = 0;
1792 
1793 	inode_unlock(inode);
1794 	return ret;
1795 }
1796 
f2fs_readpage_limit(struct inode * inode)1797 static inline loff_t f2fs_readpage_limit(struct inode *inode)
1798 {
1799 	if (IS_ENABLED(CONFIG_FS_VERITY) &&
1800 	    (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1801 		return inode->i_sb->s_maxbytes;
1802 
1803 	return i_size_read(inode);
1804 }
1805 
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)1806 static int f2fs_read_single_page(struct inode *inode, struct page *page,
1807 					unsigned nr_pages,
1808 					struct f2fs_map_blocks *map,
1809 					struct bio **bio_ret,
1810 					sector_t *last_block_in_bio,
1811 					bool is_readahead)
1812 {
1813 	struct bio *bio = *bio_ret;
1814 	const unsigned blkbits = inode->i_blkbits;
1815 	const unsigned blocksize = 1 << blkbits;
1816 	sector_t block_in_file;
1817 	sector_t last_block;
1818 	sector_t last_block_in_file;
1819 	sector_t block_nr;
1820 	int ret = 0;
1821 
1822 	block_in_file = (sector_t)page_index(page);
1823 	last_block = block_in_file + nr_pages;
1824 	last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
1825 							blkbits;
1826 	if (last_block > last_block_in_file)
1827 		last_block = last_block_in_file;
1828 
1829 	/* just zeroing out page which is beyond EOF */
1830 	if (block_in_file >= last_block)
1831 		goto zero_out;
1832 	/*
1833 	 * Map blocks using the previous result first.
1834 	 */
1835 	if ((map->m_flags & F2FS_MAP_MAPPED) &&
1836 			block_in_file > map->m_lblk &&
1837 			block_in_file < (map->m_lblk + map->m_len))
1838 		goto got_it;
1839 
1840 	/*
1841 	 * Then do more f2fs_map_blocks() calls until we are
1842 	 * done with this page.
1843 	 */
1844 	map->m_lblk = block_in_file;
1845 	map->m_len = last_block - block_in_file;
1846 
1847 	ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
1848 	if (ret)
1849 		goto out;
1850 got_it:
1851 	if ((map->m_flags & F2FS_MAP_MAPPED)) {
1852 		block_nr = map->m_pblk + block_in_file - map->m_lblk;
1853 		SetPageMappedToDisk(page);
1854 
1855 		if (!PageUptodate(page) && (!PageSwapCache(page) &&
1856 					!cleancache_get_page(page))) {
1857 			SetPageUptodate(page);
1858 			goto confused;
1859 		}
1860 
1861 		if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1862 						DATA_GENERIC_ENHANCE_READ)) {
1863 			ret = -EFSCORRUPTED;
1864 			goto out;
1865 		}
1866 	} else {
1867 zero_out:
1868 		zero_user_segment(page, 0, PAGE_SIZE);
1869 		if (f2fs_need_verity(inode, page->index) &&
1870 		    !fsverity_verify_page(page)) {
1871 			ret = -EIO;
1872 			goto out;
1873 		}
1874 		if (!PageUptodate(page))
1875 			SetPageUptodate(page);
1876 		unlock_page(page);
1877 		goto out;
1878 	}
1879 
1880 	/*
1881 	 * This page will go to BIO.  Do we need to send this
1882 	 * BIO off first?
1883 	 */
1884 	if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
1885 				       *last_block_in_bio, block_nr) ||
1886 		    !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
1887 submit_and_realloc:
1888 		__submit_bio(F2FS_I_SB(inode), bio, DATA);
1889 		bio = NULL;
1890 	}
1891 	if (bio == NULL) {
1892 		bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
1893 				is_readahead ? REQ_RAHEAD : 0, page->index);
1894 		if (IS_ERR(bio)) {
1895 			ret = PTR_ERR(bio);
1896 			bio = NULL;
1897 			goto out;
1898 		}
1899 	}
1900 
1901 	/*
1902 	 * If the page is under writeback, we need to wait for
1903 	 * its completion to see the correct decrypted data.
1904 	 */
1905 	f2fs_wait_on_block_writeback(inode, block_nr);
1906 
1907 	if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1908 		goto submit_and_realloc;
1909 
1910 	inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
1911 	ClearPageError(page);
1912 	*last_block_in_bio = block_nr;
1913 	goto out;
1914 confused:
1915 	if (bio) {
1916 		__submit_bio(F2FS_I_SB(inode), bio, DATA);
1917 		bio = NULL;
1918 	}
1919 	unlock_page(page);
1920 out:
1921 	*bio_ret = bio;
1922 	return ret;
1923 }
1924 
1925 /*
1926  * This function was originally taken from fs/mpage.c, and customized for f2fs.
1927  * Major change was from block_size == page_size in f2fs by default.
1928  *
1929  * Note that the aops->readpages() function is ONLY used for read-ahead. If
1930  * this function ever deviates from doing just read-ahead, it should either
1931  * use ->readpage() or do the necessary surgery to decouple ->readpages()
1932  * from read-ahead.
1933  */
f2fs_mpage_readpages(struct address_space * mapping,struct list_head * pages,struct page * page,unsigned nr_pages,bool is_readahead)1934 static int f2fs_mpage_readpages(struct address_space *mapping,
1935 			struct list_head *pages, struct page *page,
1936 			unsigned nr_pages, bool is_readahead)
1937 {
1938 	struct bio *bio = NULL;
1939 	sector_t last_block_in_bio = 0;
1940 	struct inode *inode = mapping->host;
1941 	struct f2fs_map_blocks map;
1942 	int ret = 0;
1943 
1944 	map.m_pblk = 0;
1945 	map.m_lblk = 0;
1946 	map.m_len = 0;
1947 	map.m_flags = 0;
1948 	map.m_next_pgofs = NULL;
1949 	map.m_next_extent = NULL;
1950 	map.m_seg_type = NO_CHECK_TYPE;
1951 	map.m_may_create = false;
1952 
1953 	for (; nr_pages; nr_pages--) {
1954 		if (pages) {
1955 			page = list_last_entry(pages, struct page, lru);
1956 
1957 			prefetchw(&page->flags);
1958 			list_del(&page->lru);
1959 			if (add_to_page_cache_lru(page, mapping,
1960 						  page_index(page),
1961 						  readahead_gfp_mask(mapping)))
1962 				goto next_page;
1963 		}
1964 
1965 		ret = f2fs_read_single_page(inode, page, nr_pages, &map, &bio,
1966 					&last_block_in_bio, is_readahead);
1967 		if (ret) {
1968 			SetPageError(page);
1969 			zero_user_segment(page, 0, PAGE_SIZE);
1970 			unlock_page(page);
1971 		}
1972 next_page:
1973 		if (pages)
1974 			put_page(page);
1975 	}
1976 	BUG_ON(pages && !list_empty(pages));
1977 	if (bio)
1978 		__submit_bio(F2FS_I_SB(inode), bio, DATA);
1979 	return pages ? 0 : ret;
1980 }
1981 
f2fs_read_data_page(struct file * file,struct page * page)1982 static int f2fs_read_data_page(struct file *file, struct page *page)
1983 {
1984 	struct inode *inode = page_file_mapping(page)->host;
1985 	int ret = -EAGAIN;
1986 
1987 	trace_f2fs_readpage(page, DATA);
1988 
1989 	/* If the file has inline data, try to read it directly */
1990 	if (f2fs_has_inline_data(inode))
1991 		ret = f2fs_read_inline_data(inode, page);
1992 	if (ret == -EAGAIN)
1993 		ret = f2fs_mpage_readpages(page_file_mapping(page),
1994 						NULL, page, 1, false);
1995 	return ret;
1996 }
1997 
f2fs_read_data_pages(struct file * file,struct address_space * mapping,struct list_head * pages,unsigned nr_pages)1998 static int f2fs_read_data_pages(struct file *file,
1999 			struct address_space *mapping,
2000 			struct list_head *pages, unsigned nr_pages)
2001 {
2002 	struct inode *inode = mapping->host;
2003 	struct page *page = list_last_entry(pages, struct page, lru);
2004 
2005 	trace_f2fs_readpages(inode, page, nr_pages);
2006 
2007 	/* If the file has inline data, skip readpages */
2008 	if (f2fs_has_inline_data(inode))
2009 		return 0;
2010 
2011 	return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
2012 }
2013 
encrypt_one_page(struct f2fs_io_info * fio)2014 static int encrypt_one_page(struct f2fs_io_info *fio)
2015 {
2016 	struct inode *inode = fio->page->mapping->host;
2017 	struct page *mpage;
2018 	gfp_t gfp_flags = GFP_NOFS;
2019 
2020 	if (!f2fs_encrypted_file(inode))
2021 		return 0;
2022 
2023 	/* wait for GCed page writeback via META_MAPPING */
2024 	f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
2025 
2026 	if (fscrypt_inode_uses_inline_crypto(inode))
2027 		return 0;
2028 
2029 retry_encrypt:
2030 	fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(fio->page,
2031 							       PAGE_SIZE, 0,
2032 							       gfp_flags);
2033 	if (IS_ERR(fio->encrypted_page)) {
2034 		/* flush pending IOs and wait for a while in the ENOMEM case */
2035 		if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2036 			f2fs_flush_merged_writes(fio->sbi);
2037 			congestion_wait(BLK_RW_ASYNC, HZ/50);
2038 			gfp_flags |= __GFP_NOFAIL;
2039 			goto retry_encrypt;
2040 		}
2041 		return PTR_ERR(fio->encrypted_page);
2042 	}
2043 
2044 	mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2045 	if (mpage) {
2046 		if (PageUptodate(mpage))
2047 			memcpy(page_address(mpage),
2048 				page_address(fio->encrypted_page), PAGE_SIZE);
2049 		f2fs_put_page(mpage, 1);
2050 	}
2051 	return 0;
2052 }
2053 
check_inplace_update_policy(struct inode * inode,struct f2fs_io_info * fio)2054 static inline bool check_inplace_update_policy(struct inode *inode,
2055 				struct f2fs_io_info *fio)
2056 {
2057 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2058 	unsigned int policy = SM_I(sbi)->ipu_policy;
2059 
2060 	if (policy & (0x1 << F2FS_IPU_FORCE))
2061 		return true;
2062 	if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2063 		return true;
2064 	if (policy & (0x1 << F2FS_IPU_UTIL) &&
2065 			utilization(sbi) > SM_I(sbi)->min_ipu_util)
2066 		return true;
2067 	if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2068 			utilization(sbi) > SM_I(sbi)->min_ipu_util)
2069 		return true;
2070 
2071 	/*
2072 	 * IPU for rewrite async pages
2073 	 */
2074 	if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2075 			fio && fio->op == REQ_OP_WRITE &&
2076 			!(fio->op_flags & REQ_SYNC) &&
2077 			!IS_ENCRYPTED(inode))
2078 		return true;
2079 
2080 	/* this is only set during fdatasync */
2081 	if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2082 			is_inode_flag_set(inode, FI_NEED_IPU))
2083 		return true;
2084 
2085 	if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2086 			!f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2087 		return true;
2088 
2089 	return false;
2090 }
2091 
f2fs_should_update_inplace(struct inode * inode,struct f2fs_io_info * fio)2092 bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2093 {
2094 	if (f2fs_is_pinned_file(inode))
2095 		return true;
2096 
2097 	/* if this is cold file, we should overwrite to avoid fragmentation */
2098 	if (file_is_cold(inode))
2099 		return true;
2100 
2101 	return check_inplace_update_policy(inode, fio);
2102 }
2103 
f2fs_should_update_outplace(struct inode * inode,struct f2fs_io_info * fio)2104 bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2105 {
2106 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2107 
2108 	if (test_opt(sbi, LFS))
2109 		return true;
2110 	if (S_ISDIR(inode->i_mode))
2111 		return true;
2112 	if (IS_NOQUOTA(inode))
2113 		return true;
2114 	if (f2fs_is_atomic_file(inode))
2115 		return true;
2116 	if (fio) {
2117 		if (is_cold_data(fio->page))
2118 			return true;
2119 		if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2120 			return true;
2121 		if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2122 			f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2123 			return true;
2124 	}
2125 	return false;
2126 }
2127 
need_inplace_update(struct f2fs_io_info * fio)2128 static inline bool need_inplace_update(struct f2fs_io_info *fio)
2129 {
2130 	struct inode *inode = fio->page->mapping->host;
2131 
2132 	if (f2fs_should_update_outplace(inode, fio))
2133 		return false;
2134 
2135 	return f2fs_should_update_inplace(inode, fio);
2136 }
2137 
f2fs_do_write_data_page(struct f2fs_io_info * fio)2138 int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2139 {
2140 	struct page *page = fio->page;
2141 	struct inode *inode = page->mapping->host;
2142 	struct dnode_of_data dn;
2143 	struct extent_info ei = {0,0,0};
2144 	struct node_info ni;
2145 	bool ipu_force = false;
2146 	int err = 0;
2147 
2148 	set_new_dnode(&dn, inode, NULL, NULL, 0);
2149 	if (need_inplace_update(fio) &&
2150 			f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2151 		fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2152 
2153 		if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2154 						DATA_GENERIC_ENHANCE))
2155 			return -EFSCORRUPTED;
2156 
2157 		ipu_force = true;
2158 		fio->need_lock = LOCK_DONE;
2159 		goto got_it;
2160 	}
2161 
2162 	/* Deadlock due to between page->lock and f2fs_lock_op */
2163 	if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2164 		return -EAGAIN;
2165 
2166 	err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2167 	if (err)
2168 		goto out;
2169 
2170 	fio->old_blkaddr = dn.data_blkaddr;
2171 
2172 	/* This page is already truncated */
2173 	if (fio->old_blkaddr == NULL_ADDR) {
2174 		ClearPageUptodate(page);
2175 		clear_cold_data(page);
2176 		goto out_writepage;
2177 	}
2178 got_it:
2179 	if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2180 		!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
2181 						DATA_GENERIC_ENHANCE)) {
2182 		err = -EFSCORRUPTED;
2183 		goto out_writepage;
2184 	}
2185 	/*
2186 	 * If current allocation needs SSR,
2187 	 * it had better in-place writes for updated data.
2188 	 */
2189 	if (ipu_force ||
2190 		(__is_valid_data_blkaddr(fio->old_blkaddr) &&
2191 					need_inplace_update(fio))) {
2192 		err = encrypt_one_page(fio);
2193 		if (err)
2194 			goto out_writepage;
2195 
2196 		set_page_writeback(page);
2197 		ClearPageError(page);
2198 		f2fs_put_dnode(&dn);
2199 		if (fio->need_lock == LOCK_REQ)
2200 			f2fs_unlock_op(fio->sbi);
2201 		err = f2fs_inplace_write_data(fio);
2202 		if (err) {
2203 			if (fscrypt_inode_uses_fs_layer_crypto(inode))
2204 				fscrypt_finalize_bounce_page(&fio->encrypted_page);
2205 			if (PageWriteback(page))
2206 				end_page_writeback(page);
2207 		} else {
2208 			set_inode_flag(inode, FI_UPDATE_WRITE);
2209 		}
2210 		trace_f2fs_do_write_data_page(fio->page, IPU);
2211 		return err;
2212 	}
2213 
2214 	if (fio->need_lock == LOCK_RETRY) {
2215 		if (!f2fs_trylock_op(fio->sbi)) {
2216 			err = -EAGAIN;
2217 			goto out_writepage;
2218 		}
2219 		fio->need_lock = LOCK_REQ;
2220 	}
2221 
2222 	err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2223 	if (err)
2224 		goto out_writepage;
2225 
2226 	fio->version = ni.version;
2227 
2228 	err = encrypt_one_page(fio);
2229 	if (err)
2230 		goto out_writepage;
2231 
2232 	set_page_writeback(page);
2233 	ClearPageError(page);
2234 
2235 	/* LFS mode write path */
2236 	f2fs_outplace_write_data(&dn, fio);
2237 	trace_f2fs_do_write_data_page(page, OPU);
2238 	set_inode_flag(inode, FI_APPEND_WRITE);
2239 	if (page->index == 0)
2240 		set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2241 out_writepage:
2242 	f2fs_put_dnode(&dn);
2243 out:
2244 	if (fio->need_lock == LOCK_REQ)
2245 		f2fs_unlock_op(fio->sbi);
2246 	return err;
2247 }
2248 
__write_data_page(struct page * page,bool * submitted,struct bio ** bio,sector_t * last_block,struct writeback_control * wbc,enum iostat_type io_type)2249 static int __write_data_page(struct page *page, bool *submitted,
2250 				struct bio **bio,
2251 				sector_t *last_block,
2252 				struct writeback_control *wbc,
2253 				enum iostat_type io_type)
2254 {
2255 	struct inode *inode = page->mapping->host;
2256 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2257 	loff_t i_size = i_size_read(inode);
2258 	const pgoff_t end_index = ((unsigned long long) i_size)
2259 							>> PAGE_SHIFT;
2260 	loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
2261 	unsigned offset = 0;
2262 	bool need_balance_fs = false;
2263 	int err = 0;
2264 	struct f2fs_io_info fio = {
2265 		.sbi = sbi,
2266 		.ino = inode->i_ino,
2267 		.type = DATA,
2268 		.op = REQ_OP_WRITE,
2269 		.op_flags = wbc_to_write_flags(wbc),
2270 		.old_blkaddr = NULL_ADDR,
2271 		.page = page,
2272 		.encrypted_page = NULL,
2273 		.submitted = false,
2274 		.need_lock = LOCK_RETRY,
2275 		.io_type = io_type,
2276 		.io_wbc = wbc,
2277 		.bio = bio,
2278 		.last_block = last_block,
2279 	};
2280 
2281 	trace_f2fs_writepage(page, DATA);
2282 
2283 	/* we should bypass data pages to proceed the kworkder jobs */
2284 	if (unlikely(f2fs_cp_error(sbi))) {
2285 		mapping_set_error(page->mapping, -EIO);
2286 		/*
2287 		 * don't drop any dirty dentry pages for keeping lastest
2288 		 * directory structure.
2289 		 */
2290 		if (S_ISDIR(inode->i_mode))
2291 			goto redirty_out;
2292 		goto out;
2293 	}
2294 
2295 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2296 		goto redirty_out;
2297 
2298 	if (page->index < end_index || f2fs_verity_in_progress(inode))
2299 		goto write;
2300 
2301 	/*
2302 	 * If the offset is out-of-range of file size,
2303 	 * this page does not have to be written to disk.
2304 	 */
2305 	offset = i_size & (PAGE_SIZE - 1);
2306 	if ((page->index >= end_index + 1) || !offset)
2307 		goto out;
2308 
2309 	zero_user_segment(page, offset, PAGE_SIZE);
2310 write:
2311 	if (f2fs_is_drop_cache(inode))
2312 		goto out;
2313 	/* we should not write 0'th page having journal header */
2314 	if (f2fs_is_volatile_file(inode) && (!page->index ||
2315 			(!wbc->for_reclaim &&
2316 			f2fs_available_free_memory(sbi, BASE_CHECK))))
2317 		goto redirty_out;
2318 
2319 	/* Dentry blocks are controlled by checkpoint */
2320 	if (S_ISDIR(inode->i_mode)) {
2321 		fio.need_lock = LOCK_DONE;
2322 		err = f2fs_do_write_data_page(&fio);
2323 		goto done;
2324 	}
2325 
2326 	if (!wbc->for_reclaim)
2327 		need_balance_fs = true;
2328 	else if (has_not_enough_free_secs(sbi, 0, 0))
2329 		goto redirty_out;
2330 	else
2331 		set_inode_flag(inode, FI_HOT_DATA);
2332 
2333 	err = -EAGAIN;
2334 	if (f2fs_has_inline_data(inode)) {
2335 		err = f2fs_write_inline_data(inode, page);
2336 		if (!err)
2337 			goto out;
2338 	}
2339 
2340 	if (err == -EAGAIN) {
2341 		err = f2fs_do_write_data_page(&fio);
2342 		if (err == -EAGAIN) {
2343 			fio.need_lock = LOCK_REQ;
2344 			err = f2fs_do_write_data_page(&fio);
2345 		}
2346 	}
2347 
2348 	if (err) {
2349 		file_set_keep_isize(inode);
2350 	} else {
2351 		down_write(&F2FS_I(inode)->i_sem);
2352 		if (F2FS_I(inode)->last_disk_size < psize)
2353 			F2FS_I(inode)->last_disk_size = psize;
2354 		up_write(&F2FS_I(inode)->i_sem);
2355 	}
2356 
2357 done:
2358 	if (err && err != -ENOENT)
2359 		goto redirty_out;
2360 
2361 out:
2362 	inode_dec_dirty_pages(inode);
2363 	if (err) {
2364 		ClearPageUptodate(page);
2365 		clear_cold_data(page);
2366 	}
2367 
2368 	if (wbc->for_reclaim) {
2369 		f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
2370 		clear_inode_flag(inode, FI_HOT_DATA);
2371 		f2fs_remove_dirty_inode(inode);
2372 		submitted = NULL;
2373 	}
2374 
2375 	unlock_page(page);
2376 	if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2377 					!F2FS_I(inode)->cp_task)
2378 		f2fs_balance_fs(sbi, need_balance_fs);
2379 
2380 	if (unlikely(f2fs_cp_error(sbi))) {
2381 		f2fs_submit_merged_write(sbi, DATA);
2382 		f2fs_submit_merged_ipu_write(sbi, bio, NULL);
2383 		submitted = NULL;
2384 	}
2385 
2386 	if (submitted)
2387 		*submitted = fio.submitted;
2388 
2389 	return 0;
2390 
2391 redirty_out:
2392 	redirty_page_for_writepage(wbc, page);
2393 	/*
2394 	 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2395 	 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2396 	 * file_write_and_wait_range() will see EIO error, which is critical
2397 	 * to return value of fsync() followed by atomic_write failure to user.
2398 	 */
2399 	if (!err || wbc->for_reclaim)
2400 		return AOP_WRITEPAGE_ACTIVATE;
2401 	unlock_page(page);
2402 	return err;
2403 }
2404 
f2fs_write_data_page(struct page * page,struct writeback_control * wbc)2405 static int f2fs_write_data_page(struct page *page,
2406 					struct writeback_control *wbc)
2407 {
2408 	return __write_data_page(page, NULL, NULL, NULL, wbc, FS_DATA_IO);
2409 }
2410 
2411 /*
2412  * This function was copied from write_cche_pages from mm/page-writeback.c.
2413  * The major change is making write step of cold data page separately from
2414  * warm/hot data page.
2415  */
f2fs_write_cache_pages(struct address_space * mapping,struct writeback_control * wbc,enum iostat_type io_type)2416 static int f2fs_write_cache_pages(struct address_space *mapping,
2417 					struct writeback_control *wbc,
2418 					enum iostat_type io_type)
2419 {
2420 	int ret = 0;
2421 	int done = 0;
2422 	struct pagevec pvec;
2423 	struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
2424 	struct bio *bio = NULL;
2425 	sector_t last_block;
2426 	int nr_pages;
2427 	pgoff_t uninitialized_var(writeback_index);
2428 	pgoff_t index;
2429 	pgoff_t end;		/* Inclusive */
2430 	pgoff_t done_index;
2431 	int cycled;
2432 	int range_whole = 0;
2433 	xa_mark_t tag;
2434 	int nwritten = 0;
2435 
2436 	pagevec_init(&pvec);
2437 
2438 	if (get_dirty_pages(mapping->host) <=
2439 				SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2440 		set_inode_flag(mapping->host, FI_HOT_DATA);
2441 	else
2442 		clear_inode_flag(mapping->host, FI_HOT_DATA);
2443 
2444 	if (wbc->range_cyclic) {
2445 		writeback_index = mapping->writeback_index; /* prev offset */
2446 		index = writeback_index;
2447 		if (index == 0)
2448 			cycled = 1;
2449 		else
2450 			cycled = 0;
2451 		end = -1;
2452 	} else {
2453 		index = wbc->range_start >> PAGE_SHIFT;
2454 		end = wbc->range_end >> PAGE_SHIFT;
2455 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2456 			range_whole = 1;
2457 		cycled = 1; /* ignore range_cyclic tests */
2458 	}
2459 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2460 		tag = PAGECACHE_TAG_TOWRITE;
2461 	else
2462 		tag = PAGECACHE_TAG_DIRTY;
2463 retry:
2464 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2465 		tag_pages_for_writeback(mapping, index, end);
2466 	done_index = index;
2467 	while (!done && (index <= end)) {
2468 		int i;
2469 
2470 		nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2471 				tag);
2472 		if (nr_pages == 0)
2473 			break;
2474 
2475 		for (i = 0; i < nr_pages; i++) {
2476 			struct page *page = pvec.pages[i];
2477 			bool submitted = false;
2478 
2479 			/* give a priority to WB_SYNC threads */
2480 			if (atomic_read(&sbi->wb_sync_req[DATA]) &&
2481 					wbc->sync_mode == WB_SYNC_NONE) {
2482 				done = 1;
2483 				break;
2484 			}
2485 
2486 			done_index = page->index;
2487 retry_write:
2488 			lock_page(page);
2489 
2490 			if (unlikely(page->mapping != mapping)) {
2491 continue_unlock:
2492 				unlock_page(page);
2493 				continue;
2494 			}
2495 
2496 			if (!PageDirty(page)) {
2497 				/* someone wrote it for us */
2498 				goto continue_unlock;
2499 			}
2500 
2501 			if (PageWriteback(page)) {
2502 				if (wbc->sync_mode != WB_SYNC_NONE)
2503 					f2fs_wait_on_page_writeback(page,
2504 							DATA, true, true);
2505 				else
2506 					goto continue_unlock;
2507 			}
2508 
2509 			if (!clear_page_dirty_for_io(page))
2510 				goto continue_unlock;
2511 
2512 			ret = __write_data_page(page, &submitted, &bio,
2513 					&last_block, wbc, io_type);
2514 			if (unlikely(ret)) {
2515 				/*
2516 				 * keep nr_to_write, since vfs uses this to
2517 				 * get # of written pages.
2518 				 */
2519 				if (ret == AOP_WRITEPAGE_ACTIVATE) {
2520 					unlock_page(page);
2521 					ret = 0;
2522 					continue;
2523 				} else if (ret == -EAGAIN) {
2524 					ret = 0;
2525 					if (wbc->sync_mode == WB_SYNC_ALL) {
2526 						cond_resched();
2527 						congestion_wait(BLK_RW_ASYNC,
2528 									HZ/50);
2529 						goto retry_write;
2530 					}
2531 					continue;
2532 				}
2533 				done_index = page->index + 1;
2534 				done = 1;
2535 				break;
2536 			} else if (submitted) {
2537 				nwritten++;
2538 			}
2539 
2540 			if (--wbc->nr_to_write <= 0 &&
2541 					wbc->sync_mode == WB_SYNC_NONE) {
2542 				done = 1;
2543 				break;
2544 			}
2545 		}
2546 		pagevec_release(&pvec);
2547 		cond_resched();
2548 	}
2549 
2550 	if (!cycled && !done) {
2551 		cycled = 1;
2552 		index = 0;
2553 		end = writeback_index - 1;
2554 		goto retry;
2555 	}
2556 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2557 		mapping->writeback_index = done_index;
2558 
2559 	if (nwritten)
2560 		f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
2561 								NULL, 0, DATA);
2562 	/* submit cached bio of IPU write */
2563 	if (bio)
2564 		f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
2565 
2566 	return ret;
2567 }
2568 
__should_serialize_io(struct inode * inode,struct writeback_control * wbc)2569 static inline bool __should_serialize_io(struct inode *inode,
2570 					struct writeback_control *wbc)
2571 {
2572 	if (!S_ISREG(inode->i_mode))
2573 		return false;
2574 	if (IS_NOQUOTA(inode))
2575 		return false;
2576 	/* to avoid deadlock in path of data flush */
2577 	if (F2FS_I(inode)->cp_task)
2578 		return false;
2579 	if (wbc->sync_mode != WB_SYNC_ALL)
2580 		return true;
2581 	if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
2582 		return true;
2583 	return false;
2584 }
2585 
__f2fs_write_data_pages(struct address_space * mapping,struct writeback_control * wbc,enum iostat_type io_type)2586 static int __f2fs_write_data_pages(struct address_space *mapping,
2587 						struct writeback_control *wbc,
2588 						enum iostat_type io_type)
2589 {
2590 	struct inode *inode = mapping->host;
2591 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2592 	struct blk_plug plug;
2593 	int ret;
2594 	bool locked = false;
2595 
2596 	/* deal with chardevs and other special file */
2597 	if (!mapping->a_ops->writepage)
2598 		return 0;
2599 
2600 	/* skip writing if there is no dirty page in this inode */
2601 	if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2602 		return 0;
2603 
2604 	/* during POR, we don't need to trigger writepage at all. */
2605 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2606 		goto skip_write;
2607 
2608 	if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
2609 			wbc->sync_mode == WB_SYNC_NONE &&
2610 			get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
2611 			f2fs_available_free_memory(sbi, DIRTY_DENTS))
2612 		goto skip_write;
2613 
2614 	/* skip writing during file defragment */
2615 	if (is_inode_flag_set(inode, FI_DO_DEFRAG))
2616 		goto skip_write;
2617 
2618 	trace_f2fs_writepages(mapping->host, wbc, DATA);
2619 
2620 	/* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2621 	if (wbc->sync_mode == WB_SYNC_ALL)
2622 		atomic_inc(&sbi->wb_sync_req[DATA]);
2623 	else if (atomic_read(&sbi->wb_sync_req[DATA]))
2624 		goto skip_write;
2625 
2626 	if (__should_serialize_io(inode, wbc)) {
2627 		mutex_lock(&sbi->writepages);
2628 		locked = true;
2629 	}
2630 
2631 	blk_start_plug(&plug);
2632 	ret = f2fs_write_cache_pages(mapping, wbc, io_type);
2633 	blk_finish_plug(&plug);
2634 
2635 	if (locked)
2636 		mutex_unlock(&sbi->writepages);
2637 
2638 	if (wbc->sync_mode == WB_SYNC_ALL)
2639 		atomic_dec(&sbi->wb_sync_req[DATA]);
2640 	/*
2641 	 * if some pages were truncated, we cannot guarantee its mapping->host
2642 	 * to detect pending bios.
2643 	 */
2644 
2645 	f2fs_remove_dirty_inode(inode);
2646 	return ret;
2647 
2648 skip_write:
2649 	wbc->pages_skipped += get_dirty_pages(inode);
2650 	trace_f2fs_writepages(mapping->host, wbc, DATA);
2651 	return 0;
2652 }
2653 
f2fs_write_data_pages(struct address_space * mapping,struct writeback_control * wbc)2654 static int f2fs_write_data_pages(struct address_space *mapping,
2655 			    struct writeback_control *wbc)
2656 {
2657 	struct inode *inode = mapping->host;
2658 
2659 	return __f2fs_write_data_pages(mapping, wbc,
2660 			F2FS_I(inode)->cp_task == current ?
2661 			FS_CP_DATA_IO : FS_DATA_IO);
2662 }
2663 
f2fs_write_failed(struct address_space * mapping,loff_t to)2664 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2665 {
2666 	struct inode *inode = mapping->host;
2667 	loff_t i_size = i_size_read(inode);
2668 
2669 	/* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
2670 	if (to > i_size && !f2fs_verity_in_progress(inode)) {
2671 		down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2672 		down_write(&F2FS_I(inode)->i_mmap_sem);
2673 
2674 		truncate_pagecache(inode, i_size);
2675 		if (!IS_NOQUOTA(inode))
2676 			f2fs_truncate_blocks(inode, i_size, true);
2677 
2678 		up_write(&F2FS_I(inode)->i_mmap_sem);
2679 		up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2680 	}
2681 }
2682 
prepare_write_begin(struct f2fs_sb_info * sbi,struct page * page,loff_t pos,unsigned len,block_t * blk_addr,bool * node_changed)2683 static int prepare_write_begin(struct f2fs_sb_info *sbi,
2684 			struct page *page, loff_t pos, unsigned len,
2685 			block_t *blk_addr, bool *node_changed)
2686 {
2687 	struct inode *inode = page->mapping->host;
2688 	pgoff_t index = page->index;
2689 	struct dnode_of_data dn;
2690 	struct page *ipage;
2691 	bool locked = false;
2692 	struct extent_info ei = {0,0,0};
2693 	int err = 0;
2694 	int flag;
2695 
2696 	/*
2697 	 * we already allocated all the blocks, so we don't need to get
2698 	 * the block addresses when there is no need to fill the page.
2699 	 */
2700 	if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2701 	    !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
2702 	    !f2fs_verity_in_progress(inode))
2703 		return 0;
2704 
2705 	/* f2fs_lock_op avoids race between write CP and convert_inline_page */
2706 	if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
2707 		flag = F2FS_GET_BLOCK_DEFAULT;
2708 	else
2709 		flag = F2FS_GET_BLOCK_PRE_AIO;
2710 
2711 	if (f2fs_has_inline_data(inode) ||
2712 			(pos & PAGE_MASK) >= i_size_read(inode)) {
2713 		__do_map_lock(sbi, flag, true);
2714 		locked = true;
2715 	}
2716 restart:
2717 	/* check inline_data */
2718 	ipage = f2fs_get_node_page(sbi, inode->i_ino);
2719 	if (IS_ERR(ipage)) {
2720 		err = PTR_ERR(ipage);
2721 		goto unlock_out;
2722 	}
2723 
2724 	set_new_dnode(&dn, inode, ipage, ipage, 0);
2725 
2726 	if (f2fs_has_inline_data(inode)) {
2727 		if (pos + len <= MAX_INLINE_DATA(inode)) {
2728 			f2fs_do_read_inline_data(page, ipage);
2729 			set_inode_flag(inode, FI_DATA_EXIST);
2730 			if (inode->i_nlink)
2731 				set_inline_node(ipage);
2732 		} else {
2733 			err = f2fs_convert_inline_page(&dn, page);
2734 			if (err)
2735 				goto out;
2736 			if (dn.data_blkaddr == NULL_ADDR)
2737 				err = f2fs_get_block(&dn, index);
2738 		}
2739 	} else if (locked) {
2740 		err = f2fs_get_block(&dn, index);
2741 	} else {
2742 		if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2743 			dn.data_blkaddr = ei.blk + index - ei.fofs;
2744 		} else {
2745 			/* hole case */
2746 			err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
2747 			if (err || dn.data_blkaddr == NULL_ADDR) {
2748 				f2fs_put_dnode(&dn);
2749 				__do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2750 								true);
2751 				WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
2752 				locked = true;
2753 				goto restart;
2754 			}
2755 		}
2756 	}
2757 
2758 	/* convert_inline_page can make node_changed */
2759 	*blk_addr = dn.data_blkaddr;
2760 	*node_changed = dn.node_changed;
2761 out:
2762 	f2fs_put_dnode(&dn);
2763 unlock_out:
2764 	if (locked)
2765 		__do_map_lock(sbi, flag, false);
2766 	return err;
2767 }
2768 
f2fs_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata)2769 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2770 		loff_t pos, unsigned len, unsigned flags,
2771 		struct page **pagep, void **fsdata)
2772 {
2773 	struct inode *inode = mapping->host;
2774 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2775 	struct page *page = NULL;
2776 	pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
2777 	bool need_balance = false, drop_atomic = false;
2778 	block_t blkaddr = NULL_ADDR;
2779 	int err = 0;
2780 
2781 	if (trace_android_fs_datawrite_start_enabled()) {
2782 		char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
2783 
2784 		path = android_fstrace_get_pathname(pathbuf,
2785 						    MAX_TRACE_PATHBUF_LEN,
2786 						    inode);
2787 		trace_android_fs_datawrite_start(inode, pos, len,
2788 						 current->pid, path,
2789 						 current->comm);
2790 	}
2791 	trace_f2fs_write_begin(inode, pos, len, flags);
2792 
2793 	if (!f2fs_is_checkpoint_ready(sbi)) {
2794 		err = -ENOSPC;
2795 		goto fail;
2796 	}
2797 
2798 	if ((f2fs_is_atomic_file(inode) &&
2799 			!f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
2800 			is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2801 		err = -ENOMEM;
2802 		drop_atomic = true;
2803 		goto fail;
2804 	}
2805 
2806 	/*
2807 	 * We should check this at this moment to avoid deadlock on inode page
2808 	 * and #0 page. The locking rule for inline_data conversion should be:
2809 	 * lock_page(page #0) -> lock_page(inode_page)
2810 	 */
2811 	if (index != 0) {
2812 		err = f2fs_convert_inline_inode(inode);
2813 		if (err)
2814 			goto fail;
2815 	}
2816 repeat:
2817 	/*
2818 	 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2819 	 * wait_for_stable_page. Will wait that below with our IO control.
2820 	 */
2821 	page = f2fs_pagecache_get_page(mapping, index,
2822 				FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
2823 	if (!page) {
2824 		err = -ENOMEM;
2825 		goto fail;
2826 	}
2827 
2828 	*pagep = page;
2829 
2830 	err = prepare_write_begin(sbi, page, pos, len,
2831 					&blkaddr, &need_balance);
2832 	if (err)
2833 		goto fail;
2834 
2835 	if (need_balance && !IS_NOQUOTA(inode) &&
2836 			has_not_enough_free_secs(sbi, 0, 0)) {
2837 		unlock_page(page);
2838 		f2fs_balance_fs(sbi, true);
2839 		lock_page(page);
2840 		if (page->mapping != mapping) {
2841 			/* The page got truncated from under us */
2842 			f2fs_put_page(page, 1);
2843 			goto repeat;
2844 		}
2845 	}
2846 
2847 	f2fs_wait_on_page_writeback(page, DATA, false, true);
2848 
2849 	if (len == PAGE_SIZE || PageUptodate(page))
2850 		return 0;
2851 
2852 	if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
2853 	    !f2fs_verity_in_progress(inode)) {
2854 		zero_user_segment(page, len, PAGE_SIZE);
2855 		return 0;
2856 	}
2857 
2858 	if (blkaddr == NEW_ADDR) {
2859 		zero_user_segment(page, 0, PAGE_SIZE);
2860 		SetPageUptodate(page);
2861 	} else {
2862 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
2863 				DATA_GENERIC_ENHANCE_READ)) {
2864 			err = -EFSCORRUPTED;
2865 			goto fail;
2866 		}
2867 		err = f2fs_submit_page_read(inode, page, blkaddr);
2868 		if (err)
2869 			goto fail;
2870 
2871 		lock_page(page);
2872 		if (unlikely(page->mapping != mapping)) {
2873 			f2fs_put_page(page, 1);
2874 			goto repeat;
2875 		}
2876 		if (unlikely(!PageUptodate(page))) {
2877 			err = -EIO;
2878 			goto fail;
2879 		}
2880 	}
2881 	return 0;
2882 
2883 fail:
2884 	f2fs_put_page(page, 1);
2885 	f2fs_write_failed(mapping, pos + len);
2886 	if (drop_atomic)
2887 		f2fs_drop_inmem_pages_all(sbi, false);
2888 	return err;
2889 }
2890 
f2fs_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)2891 static int f2fs_write_end(struct file *file,
2892 			struct address_space *mapping,
2893 			loff_t pos, unsigned len, unsigned copied,
2894 			struct page *page, void *fsdata)
2895 {
2896 	struct inode *inode = page->mapping->host;
2897 
2898 	trace_android_fs_datawrite_end(inode, pos, len);
2899 	trace_f2fs_write_end(inode, pos, len, copied);
2900 
2901 	/*
2902 	 * This should be come from len == PAGE_SIZE, and we expect copied
2903 	 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2904 	 * let generic_perform_write() try to copy data again through copied=0.
2905 	 */
2906 	if (!PageUptodate(page)) {
2907 		if (unlikely(copied != len))
2908 			copied = 0;
2909 		else
2910 			SetPageUptodate(page);
2911 	}
2912 	if (!copied)
2913 		goto unlock_out;
2914 
2915 	set_page_dirty(page);
2916 
2917 	if (pos + copied > i_size_read(inode) &&
2918 	    !f2fs_verity_in_progress(inode))
2919 		f2fs_i_size_write(inode, pos + copied);
2920 unlock_out:
2921 	f2fs_put_page(page, 1);
2922 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2923 	return copied;
2924 }
2925 
check_direct_IO(struct inode * inode,struct iov_iter * iter,loff_t offset)2926 static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2927 			   loff_t offset)
2928 {
2929 	unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2930 	unsigned blkbits = i_blkbits;
2931 	unsigned blocksize_mask = (1 << blkbits) - 1;
2932 	unsigned long align = offset | iov_iter_alignment(iter);
2933 	struct block_device *bdev = inode->i_sb->s_bdev;
2934 
2935 	if (align & blocksize_mask) {
2936 		if (bdev)
2937 			blkbits = blksize_bits(bdev_logical_block_size(bdev));
2938 		blocksize_mask = (1 << blkbits) - 1;
2939 		if (align & blocksize_mask)
2940 			return -EINVAL;
2941 		return 1;
2942 	}
2943 	return 0;
2944 }
2945 
f2fs_dio_end_io(struct bio * bio)2946 static void f2fs_dio_end_io(struct bio *bio)
2947 {
2948 	struct f2fs_private_dio *dio = bio->bi_private;
2949 
2950 	dec_page_count(F2FS_I_SB(dio->inode),
2951 			dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2952 
2953 	bio->bi_private = dio->orig_private;
2954 	bio->bi_end_io = dio->orig_end_io;
2955 
2956 	kvfree(dio);
2957 
2958 	bio_endio(bio);
2959 }
2960 
f2fs_dio_submit_bio(struct bio * bio,struct inode * inode,loff_t file_offset)2961 static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
2962 							loff_t file_offset)
2963 {
2964 	struct f2fs_private_dio *dio;
2965 	bool write = (bio_op(bio) == REQ_OP_WRITE);
2966 
2967 	dio = f2fs_kzalloc(F2FS_I_SB(inode),
2968 			sizeof(struct f2fs_private_dio), GFP_NOFS);
2969 	if (!dio)
2970 		goto out;
2971 
2972 	dio->inode = inode;
2973 	dio->orig_end_io = bio->bi_end_io;
2974 	dio->orig_private = bio->bi_private;
2975 	dio->write = write;
2976 
2977 	bio->bi_end_io = f2fs_dio_end_io;
2978 	bio->bi_private = dio;
2979 
2980 	inc_page_count(F2FS_I_SB(inode),
2981 			write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2982 
2983 	submit_bio(bio);
2984 	return;
2985 out:
2986 	bio->bi_status = BLK_STS_IOERR;
2987 	bio_endio(bio);
2988 }
2989 
f2fs_direct_IO(struct kiocb * iocb,struct iov_iter * iter)2990 static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2991 {
2992 	struct address_space *mapping = iocb->ki_filp->f_mapping;
2993 	struct inode *inode = mapping->host;
2994 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2995 	struct f2fs_inode_info *fi = F2FS_I(inode);
2996 	size_t count = iov_iter_count(iter);
2997 	loff_t offset = iocb->ki_pos;
2998 	int rw = iov_iter_rw(iter);
2999 	int err;
3000 	enum rw_hint hint = iocb->ki_hint;
3001 	int whint_mode = F2FS_OPTION(sbi).whint_mode;
3002 	bool do_opu;
3003 
3004 	err = check_direct_IO(inode, iter, offset);
3005 	if (err)
3006 		return err < 0 ? err : 0;
3007 
3008 	if (f2fs_force_buffered_io(inode, iocb, iter))
3009 		return 0;
3010 
3011 	do_opu = allow_outplace_dio(inode, iocb, iter);
3012 
3013 	trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3014 
3015 	if (trace_android_fs_dataread_start_enabled() &&
3016 	    (rw == READ)) {
3017 		char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
3018 
3019 		path = android_fstrace_get_pathname(pathbuf,
3020 						    MAX_TRACE_PATHBUF_LEN,
3021 						    inode);
3022 		trace_android_fs_dataread_start(inode, offset,
3023 						count, current->pid, path,
3024 						current->comm);
3025 	}
3026 	if (trace_android_fs_datawrite_start_enabled() &&
3027 	    (rw == WRITE)) {
3028 		char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
3029 
3030 		path = android_fstrace_get_pathname(pathbuf,
3031 						    MAX_TRACE_PATHBUF_LEN,
3032 						    inode);
3033 		trace_android_fs_datawrite_start(inode, offset, count,
3034 						 current->pid, path,
3035 						 current->comm);
3036 	}
3037 
3038 	if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3039 		iocb->ki_hint = WRITE_LIFE_NOT_SET;
3040 
3041 	if (iocb->ki_flags & IOCB_NOWAIT) {
3042 		if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3043 			iocb->ki_hint = hint;
3044 			err = -EAGAIN;
3045 			goto out;
3046 		}
3047 		if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3048 			up_read(&fi->i_gc_rwsem[rw]);
3049 			iocb->ki_hint = hint;
3050 			err = -EAGAIN;
3051 			goto out;
3052 		}
3053 	} else {
3054 		down_read(&fi->i_gc_rwsem[rw]);
3055 		if (do_opu)
3056 			down_read(&fi->i_gc_rwsem[READ]);
3057 	}
3058 
3059 	err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3060 			iter, rw == WRITE ? get_data_block_dio_write :
3061 			get_data_block_dio, NULL, f2fs_dio_submit_bio,
3062 			DIO_LOCKING | DIO_SKIP_HOLES);
3063 
3064 	if (do_opu)
3065 		up_read(&fi->i_gc_rwsem[READ]);
3066 
3067 	up_read(&fi->i_gc_rwsem[rw]);
3068 
3069 	if (rw == WRITE) {
3070 		if (whint_mode == WHINT_MODE_OFF)
3071 			iocb->ki_hint = hint;
3072 		if (err > 0) {
3073 			f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3074 									err);
3075 			if (!do_opu)
3076 				set_inode_flag(inode, FI_UPDATE_WRITE);
3077 		} else if (err < 0) {
3078 			f2fs_write_failed(mapping, offset + count);
3079 		}
3080 	}
3081 
3082 out:
3083 	if (trace_android_fs_dataread_start_enabled() &&
3084 	    (rw == READ))
3085 		trace_android_fs_dataread_end(inode, offset, count);
3086 	if (trace_android_fs_datawrite_start_enabled() &&
3087 	    (rw == WRITE))
3088 		trace_android_fs_datawrite_end(inode, offset, count);
3089 
3090 	trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3091 
3092 	return err;
3093 }
3094 
f2fs_invalidate_page(struct page * page,unsigned int offset,unsigned int length)3095 void f2fs_invalidate_page(struct page *page, unsigned int offset,
3096 							unsigned int length)
3097 {
3098 	struct inode *inode = page->mapping->host;
3099 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3100 
3101 	if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3102 		(offset % PAGE_SIZE || length != PAGE_SIZE))
3103 		return;
3104 
3105 	if (PageDirty(page)) {
3106 		if (inode->i_ino == F2FS_META_INO(sbi)) {
3107 			dec_page_count(sbi, F2FS_DIRTY_META);
3108 		} else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3109 			dec_page_count(sbi, F2FS_DIRTY_NODES);
3110 		} else {
3111 			inode_dec_dirty_pages(inode);
3112 			f2fs_remove_dirty_inode(inode);
3113 		}
3114 	}
3115 
3116 	clear_cold_data(page);
3117 
3118 	if (IS_ATOMIC_WRITTEN_PAGE(page))
3119 		return f2fs_drop_inmem_page(inode, page);
3120 
3121 	f2fs_clear_page_private(page);
3122 }
3123 
f2fs_release_page(struct page * page,gfp_t wait)3124 int f2fs_release_page(struct page *page, gfp_t wait)
3125 {
3126 	/* If this is dirty page, keep PagePrivate */
3127 	if (PageDirty(page))
3128 		return 0;
3129 
3130 	/* This is atomic written page, keep Private */
3131 	if (IS_ATOMIC_WRITTEN_PAGE(page))
3132 		return 0;
3133 
3134 	clear_cold_data(page);
3135 	f2fs_clear_page_private(page);
3136 	return 1;
3137 }
3138 
f2fs_set_data_page_dirty(struct page * page)3139 static int f2fs_set_data_page_dirty(struct page *page)
3140 {
3141 	struct inode *inode = page_file_mapping(page)->host;
3142 
3143 	trace_f2fs_set_page_dirty(page, DATA);
3144 
3145 	if (!PageUptodate(page))
3146 		SetPageUptodate(page);
3147 	if (PageSwapCache(page))
3148 		return __set_page_dirty_nobuffers(page);
3149 
3150 	if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3151 		if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
3152 			f2fs_register_inmem_page(inode, page);
3153 			return 1;
3154 		}
3155 		/*
3156 		 * Previously, this page has been registered, we just
3157 		 * return here.
3158 		 */
3159 		return 0;
3160 	}
3161 
3162 	if (!PageDirty(page)) {
3163 		__set_page_dirty_nobuffers(page);
3164 		f2fs_update_dirty_page(inode, page);
3165 		return 1;
3166 	}
3167 	return 0;
3168 }
3169 
f2fs_bmap(struct address_space * mapping,sector_t block)3170 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3171 {
3172 	struct inode *inode = mapping->host;
3173 
3174 	if (f2fs_has_inline_data(inode))
3175 		return 0;
3176 
3177 	/* make sure allocating whole blocks */
3178 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3179 		filemap_write_and_wait(mapping);
3180 
3181 	return generic_block_bmap(mapping, block, get_data_block_bmap);
3182 }
3183 
3184 #ifdef CONFIG_MIGRATION
3185 #include <linux/migrate.h>
3186 
f2fs_migrate_page(struct address_space * mapping,struct page * newpage,struct page * page,enum migrate_mode mode)3187 int f2fs_migrate_page(struct address_space *mapping,
3188 		struct page *newpage, struct page *page, enum migrate_mode mode)
3189 {
3190 	int rc, extra_count;
3191 	struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3192 	bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3193 
3194 	BUG_ON(PageWriteback(page));
3195 
3196 	/* migrating an atomic written page is safe with the inmem_lock hold */
3197 	if (atomic_written) {
3198 		if (mode != MIGRATE_SYNC)
3199 			return -EBUSY;
3200 		if (!mutex_trylock(&fi->inmem_lock))
3201 			return -EAGAIN;
3202 	}
3203 
3204 	/* one extra reference was held for atomic_write page */
3205 	extra_count = atomic_written ? 1 : 0;
3206 	rc = migrate_page_move_mapping(mapping, newpage,
3207 				page, extra_count);
3208 	if (rc != MIGRATEPAGE_SUCCESS) {
3209 		if (atomic_written)
3210 			mutex_unlock(&fi->inmem_lock);
3211 		return rc;
3212 	}
3213 
3214 	if (atomic_written) {
3215 		struct inmem_pages *cur;
3216 		list_for_each_entry(cur, &fi->inmem_pages, list)
3217 			if (cur->page == page) {
3218 				cur->page = newpage;
3219 				break;
3220 			}
3221 		mutex_unlock(&fi->inmem_lock);
3222 		put_page(page);
3223 		get_page(newpage);
3224 	}
3225 
3226 	if (PagePrivate(page)) {
3227 		f2fs_set_page_private(newpage, page_private(page));
3228 		f2fs_clear_page_private(page);
3229 	}
3230 
3231 	if (mode != MIGRATE_SYNC_NO_COPY)
3232 		migrate_page_copy(newpage, page);
3233 	else
3234 		migrate_page_states(newpage, page);
3235 
3236 	return MIGRATEPAGE_SUCCESS;
3237 }
3238 #endif
3239 
3240 #ifdef CONFIG_SWAP
3241 /* Copied from generic_swapfile_activate() to check any holes */
check_swap_activate(struct file * swap_file,unsigned int max)3242 static int check_swap_activate(struct file *swap_file, unsigned int max)
3243 {
3244 	struct address_space *mapping = swap_file->f_mapping;
3245 	struct inode *inode = mapping->host;
3246 	unsigned blocks_per_page;
3247 	unsigned long page_no;
3248 	unsigned blkbits;
3249 	sector_t probe_block;
3250 	sector_t last_block;
3251 	sector_t lowest_block = -1;
3252 	sector_t highest_block = 0;
3253 
3254 	blkbits = inode->i_blkbits;
3255 	blocks_per_page = PAGE_SIZE >> blkbits;
3256 
3257 	/*
3258 	 * Map all the blocks into the extent list.  This code doesn't try
3259 	 * to be very smart.
3260 	 */
3261 	probe_block = 0;
3262 	page_no = 0;
3263 	last_block = i_size_read(inode) >> blkbits;
3264 	while ((probe_block + blocks_per_page) <= last_block && page_no < max) {
3265 		unsigned block_in_page;
3266 		sector_t first_block;
3267 
3268 		cond_resched();
3269 
3270 		first_block = bmap(inode, probe_block);
3271 		if (first_block == 0)
3272 			goto bad_bmap;
3273 
3274 		/*
3275 		 * It must be PAGE_SIZE aligned on-disk
3276 		 */
3277 		if (first_block & (blocks_per_page - 1)) {
3278 			probe_block++;
3279 			goto reprobe;
3280 		}
3281 
3282 		for (block_in_page = 1; block_in_page < blocks_per_page;
3283 					block_in_page++) {
3284 			sector_t block;
3285 
3286 			block = bmap(inode, probe_block + block_in_page);
3287 			if (block == 0)
3288 				goto bad_bmap;
3289 			if (block != first_block + block_in_page) {
3290 				/* Discontiguity */
3291 				probe_block++;
3292 				goto reprobe;
3293 			}
3294 		}
3295 
3296 		first_block >>= (PAGE_SHIFT - blkbits);
3297 		if (page_no) {	/* exclude the header page */
3298 			if (first_block < lowest_block)
3299 				lowest_block = first_block;
3300 			if (first_block > highest_block)
3301 				highest_block = first_block;
3302 		}
3303 
3304 		page_no++;
3305 		probe_block += blocks_per_page;
3306 reprobe:
3307 		continue;
3308 	}
3309 	return 0;
3310 
3311 bad_bmap:
3312 	pr_err("swapon: swapfile has holes\n");
3313 	return -EINVAL;
3314 }
3315 
f2fs_swap_activate(struct swap_info_struct * sis,struct file * file,sector_t * span)3316 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3317 				sector_t *span)
3318 {
3319 	struct inode *inode = file_inode(file);
3320 	int ret;
3321 
3322 	if (!S_ISREG(inode->i_mode))
3323 		return -EINVAL;
3324 
3325 	if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3326 		return -EROFS;
3327 
3328 	ret = f2fs_convert_inline_inode(inode);
3329 	if (ret)
3330 		return ret;
3331 
3332 	ret = check_swap_activate(file, sis->max);
3333 	if (ret)
3334 		return ret;
3335 
3336 	set_inode_flag(inode, FI_PIN_FILE);
3337 	f2fs_precache_extents(inode);
3338 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3339 	return 0;
3340 }
3341 
f2fs_swap_deactivate(struct file * file)3342 static void f2fs_swap_deactivate(struct file *file)
3343 {
3344 	struct inode *inode = file_inode(file);
3345 
3346 	clear_inode_flag(inode, FI_PIN_FILE);
3347 }
3348 #else
f2fs_swap_activate(struct swap_info_struct * sis,struct file * file,sector_t * span)3349 static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3350 				sector_t *span)
3351 {
3352 	return -EOPNOTSUPP;
3353 }
3354 
f2fs_swap_deactivate(struct file * file)3355 static void f2fs_swap_deactivate(struct file *file)
3356 {
3357 }
3358 #endif
3359 
3360 const struct address_space_operations f2fs_dblock_aops = {
3361 	.readpage	= f2fs_read_data_page,
3362 	.readpages	= f2fs_read_data_pages,
3363 	.writepage	= f2fs_write_data_page,
3364 	.writepages	= f2fs_write_data_pages,
3365 	.write_begin	= f2fs_write_begin,
3366 	.write_end	= f2fs_write_end,
3367 	.set_page_dirty	= f2fs_set_data_page_dirty,
3368 	.invalidatepage	= f2fs_invalidate_page,
3369 	.releasepage	= f2fs_release_page,
3370 	.direct_IO	= f2fs_direct_IO,
3371 	.bmap		= f2fs_bmap,
3372 	.swap_activate  = f2fs_swap_activate,
3373 	.swap_deactivate = f2fs_swap_deactivate,
3374 #ifdef CONFIG_MIGRATION
3375 	.migratepage    = f2fs_migrate_page,
3376 #endif
3377 };
3378 
f2fs_clear_page_cache_dirty_tag(struct page * page)3379 void f2fs_clear_page_cache_dirty_tag(struct page *page)
3380 {
3381 	struct address_space *mapping = page_mapping(page);
3382 	unsigned long flags;
3383 
3384 	xa_lock_irqsave(&mapping->i_pages, flags);
3385 	__xa_clear_mark(&mapping->i_pages, page_index(page),
3386 						PAGECACHE_TAG_DIRTY);
3387 	xa_unlock_irqrestore(&mapping->i_pages, flags);
3388 }
3389 
f2fs_init_post_read_processing(void)3390 int __init f2fs_init_post_read_processing(void)
3391 {
3392 	bio_post_read_ctx_cache =
3393 		kmem_cache_create("f2fs_bio_post_read_ctx",
3394 				  sizeof(struct bio_post_read_ctx), 0, 0, NULL);
3395 	if (!bio_post_read_ctx_cache)
3396 		goto fail;
3397 	bio_post_read_ctx_pool =
3398 		mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
3399 					 bio_post_read_ctx_cache);
3400 	if (!bio_post_read_ctx_pool)
3401 		goto fail_free_cache;
3402 	return 0;
3403 
3404 fail_free_cache:
3405 	kmem_cache_destroy(bio_post_read_ctx_cache);
3406 fail:
3407 	return -ENOMEM;
3408 }
3409 
f2fs_destroy_post_read_processing(void)3410 void f2fs_destroy_post_read_processing(void)
3411 {
3412 	mempool_destroy(bio_post_read_ctx_pool);
3413 	kmem_cache_destroy(bio_post_read_ctx_cache);
3414 }
3415 
f2fs_init_bio_entry_cache(void)3416 int __init f2fs_init_bio_entry_cache(void)
3417 {
3418 	bio_entry_slab = f2fs_kmem_cache_create("bio_entry_slab",
3419 			sizeof(struct bio_entry));
3420 	if (!bio_entry_slab)
3421 		return -ENOMEM;
3422 	return 0;
3423 }
3424 
f2fs_destroy_bio_entry_cache(void)3425 void __exit f2fs_destroy_bio_entry_cache(void)
3426 {
3427 	kmem_cache_destroy(bio_entry_slab);
3428 }
3429