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