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