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