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