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