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