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