blob: 1b11a42847c48e92b8a3af471c5793c724ffcc66 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * fs/f2fs/data.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/buffer_head.h>
11#include <linux/mpage.h>
12#include <linux/writeback.h>
13#include <linux/backing-dev.h>
14#include <linux/pagevec.h>
15#include <linux/blkdev.h>
16#include <linux/bio.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020017#include <linux/blk-crypto.h>
David Brazdil0f672f62019-12-10 10:32:29 +000018#include <linux/swap.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000019#include <linux/prefetch.h>
20#include <linux/uio.h>
21#include <linux/cleancache.h>
22#include <linux/sched/signal.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020023#include <linux/fiemap.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024
25#include "f2fs.h"
26#include "node.h"
27#include "segment.h"
28#include "trace.h"
29#include <trace/events/f2fs.h>
30
31#define NUM_PREALLOC_POST_READ_CTXS 128
32
33static struct kmem_cache *bio_post_read_ctx_cache;
Olivier Deprez157378f2022-04-04 15:47:50 +020034static struct kmem_cache *bio_entry_slab;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000035static mempool_t *bio_post_read_ctx_pool;
Olivier Deprez157378f2022-04-04 15:47:50 +020036static struct bio_set f2fs_bioset;
37
38#define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
39
40int __init f2fs_init_bioset(void)
41{
42 if (bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE,
43 0, BIOSET_NEED_BVECS))
44 return -ENOMEM;
45 return 0;
46}
47
48void f2fs_destroy_bioset(void)
49{
50 bioset_exit(&f2fs_bioset);
51}
52
53static inline struct bio *__f2fs_bio_alloc(gfp_t gfp_mask,
54 unsigned int nr_iovecs)
55{
56 return bio_alloc_bioset(gfp_mask, nr_iovecs, &f2fs_bioset);
57}
58
59struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool noio)
60{
61 if (noio) {
62 /* No failure on bio allocation */
63 return __f2fs_bio_alloc(GFP_NOIO, npages);
64 }
65
66 if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
67 f2fs_show_injection_info(sbi, FAULT_ALLOC_BIO);
68 return NULL;
69 }
70
71 return __f2fs_bio_alloc(GFP_KERNEL, npages);
72}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073
74static bool __is_cp_guaranteed(struct page *page)
75{
76 struct address_space *mapping = page->mapping;
77 struct inode *inode;
78 struct f2fs_sb_info *sbi;
79
80 if (!mapping)
81 return false;
82
Olivier Deprez157378f2022-04-04 15:47:50 +020083 if (f2fs_is_compressed_page(page))
84 return false;
85
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086 inode = mapping->host;
87 sbi = F2FS_I_SB(inode);
88
89 if (inode->i_ino == F2FS_META_INO(sbi) ||
Olivier Deprez157378f2022-04-04 15:47:50 +020090 inode->i_ino == F2FS_NODE_INO(sbi) ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000091 S_ISDIR(inode->i_mode) ||
92 (S_ISREG(inode->i_mode) &&
David Brazdil0f672f62019-12-10 10:32:29 +000093 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094 is_cold_data(page))
95 return true;
96 return false;
97}
98
David Brazdil0f672f62019-12-10 10:32:29 +000099static enum count_type __read_io_type(struct page *page)
100{
101 struct address_space *mapping = page_file_mapping(page);
102
103 if (mapping) {
104 struct inode *inode = mapping->host;
105 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
106
107 if (inode->i_ino == F2FS_META_INO(sbi))
108 return F2FS_RD_META;
109
110 if (inode->i_ino == F2FS_NODE_INO(sbi))
111 return F2FS_RD_NODE;
112 }
113 return F2FS_RD_DATA;
114}
115
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000116/* postprocessing steps for read bios */
117enum bio_post_read_step {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000118 STEP_DECRYPT,
Olivier Deprez157378f2022-04-04 15:47:50 +0200119 STEP_DECOMPRESS_NOWQ, /* handle normal cluster data inplace */
120 STEP_DECOMPRESS, /* handle compressed cluster data in workqueue */
David Brazdil0f672f62019-12-10 10:32:29 +0000121 STEP_VERITY,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000122};
123
124struct bio_post_read_ctx {
125 struct bio *bio;
Olivier Deprez157378f2022-04-04 15:47:50 +0200126 struct f2fs_sb_info *sbi;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000127 struct work_struct work;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128 unsigned int enabled_steps;
129};
130
Olivier Deprez157378f2022-04-04 15:47:50 +0200131static void __read_end_io(struct bio *bio, bool compr, bool verity)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132{
133 struct page *page;
134 struct bio_vec *bv;
David Brazdil0f672f62019-12-10 10:32:29 +0000135 struct bvec_iter_all iter_all;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136
David Brazdil0f672f62019-12-10 10:32:29 +0000137 bio_for_each_segment_all(bv, bio, iter_all) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000138 page = bv->bv_page;
139
Olivier Deprez157378f2022-04-04 15:47:50 +0200140#ifdef CONFIG_F2FS_FS_COMPRESSION
141 if (compr && f2fs_is_compressed_page(page)) {
142 f2fs_decompress_pages(bio, page, verity);
143 continue;
144 }
145 if (verity)
146 continue;
147#endif
148
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000149 /* PG_error was set if any post_read step failed */
150 if (bio->bi_status || PageError(page)) {
151 ClearPageUptodate(page);
152 /* will re-read again later */
153 ClearPageError(page);
154 } else {
155 SetPageUptodate(page);
156 }
David Brazdil0f672f62019-12-10 10:32:29 +0000157 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000158 unlock_page(page);
159 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200160}
161
162static void f2fs_release_read_bio(struct bio *bio);
163static void __f2fs_read_end_io(struct bio *bio, bool compr, bool verity)
164{
165 if (!compr)
166 __read_end_io(bio, false, verity);
167 f2fs_release_read_bio(bio);
168}
169
170static void f2fs_decompress_bio(struct bio *bio, bool verity)
171{
172 __read_end_io(bio, true, verity);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000173}
174
175static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
176
Olivier Deprez157378f2022-04-04 15:47:50 +0200177static void f2fs_decrypt_work(struct bio_post_read_ctx *ctx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000179 fscrypt_decrypt_bio(ctx->bio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000180}
181
Olivier Deprez157378f2022-04-04 15:47:50 +0200182static void f2fs_decompress_work(struct bio_post_read_ctx *ctx)
183{
184 f2fs_decompress_bio(ctx->bio, ctx->enabled_steps & (1 << STEP_VERITY));
185}
186
187#ifdef CONFIG_F2FS_FS_COMPRESSION
188static void f2fs_verify_pages(struct page **rpages, unsigned int cluster_size)
189{
190 f2fs_decompress_end_io(rpages, cluster_size, false, true);
191}
192
193static void f2fs_verify_bio(struct bio *bio)
194{
195 struct bio_vec *bv;
196 struct bvec_iter_all iter_all;
197
198 bio_for_each_segment_all(bv, bio, iter_all) {
199 struct page *page = bv->bv_page;
200 struct decompress_io_ctx *dic;
201
202 dic = (struct decompress_io_ctx *)page_private(page);
203
204 if (dic) {
205 if (atomic_dec_return(&dic->verity_pages))
206 continue;
207 f2fs_verify_pages(dic->rpages,
208 dic->cluster_size);
209 f2fs_free_dic(dic);
210 continue;
211 }
212
213 if (bio->bi_status || PageError(page))
214 goto clear_uptodate;
215
216 if (fsverity_verify_page(page)) {
217 SetPageUptodate(page);
218 goto unlock;
219 }
220clear_uptodate:
221 ClearPageUptodate(page);
222 ClearPageError(page);
223unlock:
224 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
225 unlock_page(page);
226 }
227}
228#endif
229
230static void f2fs_verity_work(struct work_struct *work)
231{
232 struct bio_post_read_ctx *ctx =
233 container_of(work, struct bio_post_read_ctx, work);
234 struct bio *bio = ctx->bio;
235#ifdef CONFIG_F2FS_FS_COMPRESSION
236 unsigned int enabled_steps = ctx->enabled_steps;
237#endif
238
239 /*
240 * fsverity_verify_bio() may call readpages() again, and while verity
241 * will be disabled for this, decryption may still be needed, resulting
242 * in another bio_post_read_ctx being allocated. So to prevent
243 * deadlocks we need to release the current ctx to the mempool first.
244 * This assumes that verity is the last post-read step.
245 */
246 mempool_free(ctx, bio_post_read_ctx_pool);
247 bio->bi_private = NULL;
248
249#ifdef CONFIG_F2FS_FS_COMPRESSION
250 /* previous step is decompression */
251 if (enabled_steps & (1 << STEP_DECOMPRESS)) {
252 f2fs_verify_bio(bio);
253 f2fs_release_read_bio(bio);
254 return;
255 }
256#endif
257
258 fsverity_verify_bio(bio);
259 __f2fs_read_end_io(bio, false, false);
260}
261
262static void f2fs_post_read_work(struct work_struct *work)
David Brazdil0f672f62019-12-10 10:32:29 +0000263{
264 struct bio_post_read_ctx *ctx =
265 container_of(work, struct bio_post_read_ctx, work);
266
Olivier Deprez157378f2022-04-04 15:47:50 +0200267 if (ctx->enabled_steps & (1 << STEP_DECRYPT))
268 f2fs_decrypt_work(ctx);
David Brazdil0f672f62019-12-10 10:32:29 +0000269
Olivier Deprez157378f2022-04-04 15:47:50 +0200270 if (ctx->enabled_steps & (1 << STEP_DECOMPRESS))
271 f2fs_decompress_work(ctx);
272
273 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
274 INIT_WORK(&ctx->work, f2fs_verity_work);
275 fsverity_enqueue_verify_work(&ctx->work);
276 return;
277 }
278
279 __f2fs_read_end_io(ctx->bio,
280 ctx->enabled_steps & (1 << STEP_DECOMPRESS), false);
281}
282
283static void f2fs_enqueue_post_read_work(struct f2fs_sb_info *sbi,
284 struct work_struct *work)
285{
286 queue_work(sbi->post_read_wq, work);
David Brazdil0f672f62019-12-10 10:32:29 +0000287}
288
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000289static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
290{
David Brazdil0f672f62019-12-10 10:32:29 +0000291 /*
292 * We use different work queues for decryption and for verity because
293 * verity may require reading metadata pages that need decryption, and
294 * we shouldn't recurse to the same workqueue.
295 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200296
297 if (ctx->enabled_steps & (1 << STEP_DECRYPT) ||
298 ctx->enabled_steps & (1 << STEP_DECOMPRESS)) {
299 INIT_WORK(&ctx->work, f2fs_post_read_work);
300 f2fs_enqueue_post_read_work(ctx->sbi, &ctx->work);
301 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200303
304 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
305 INIT_WORK(&ctx->work, f2fs_verity_work);
306 fsverity_enqueue_verify_work(&ctx->work);
307 return;
308 }
309
310 __f2fs_read_end_io(ctx->bio, false, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000311}
312
313static bool f2fs_bio_post_read_required(struct bio *bio)
314{
Olivier Deprez157378f2022-04-04 15:47:50 +0200315 return bio->bi_private;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000316}
317
318static void f2fs_read_end_io(struct bio *bio)
319{
Olivier Deprez0e641232021-09-23 10:07:05 +0200320 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
321
322 if (time_to_inject(sbi, FAULT_READ_IO)) {
323 f2fs_show_injection_info(sbi, FAULT_READ_IO);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000324 bio->bi_status = BLK_STS_IOERR;
325 }
326
327 if (f2fs_bio_post_read_required(bio)) {
328 struct bio_post_read_ctx *ctx = bio->bi_private;
329
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000330 bio_post_read_processing(ctx);
331 return;
332 }
333
Olivier Deprez157378f2022-04-04 15:47:50 +0200334 __f2fs_read_end_io(bio, false, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000335}
336
337static void f2fs_write_end_io(struct bio *bio)
338{
339 struct f2fs_sb_info *sbi = bio->bi_private;
340 struct bio_vec *bvec;
David Brazdil0f672f62019-12-10 10:32:29 +0000341 struct bvec_iter_all iter_all;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000342
David Brazdil0f672f62019-12-10 10:32:29 +0000343 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200344 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
David Brazdil0f672f62019-12-10 10:32:29 +0000345 bio->bi_status = BLK_STS_IOERR;
346 }
347
348 bio_for_each_segment_all(bvec, bio, iter_all) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349 struct page *page = bvec->bv_page;
350 enum count_type type = WB_DATA_TYPE(page);
351
352 if (IS_DUMMY_WRITTEN_PAGE(page)) {
353 set_page_private(page, (unsigned long)NULL);
354 ClearPagePrivate(page);
355 unlock_page(page);
356 mempool_free(page, sbi->write_io_dummy);
357
358 if (unlikely(bio->bi_status))
359 f2fs_stop_checkpoint(sbi, true);
360 continue;
361 }
362
David Brazdil0f672f62019-12-10 10:32:29 +0000363 fscrypt_finalize_bounce_page(&page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000364
Olivier Deprez157378f2022-04-04 15:47:50 +0200365#ifdef CONFIG_F2FS_FS_COMPRESSION
366 if (f2fs_is_compressed_page(page)) {
367 f2fs_compress_write_end_io(bio, page);
368 continue;
369 }
370#endif
371
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000372 if (unlikely(bio->bi_status)) {
373 mapping_set_error(page->mapping, -EIO);
374 if (type == F2FS_WB_CP_DATA)
375 f2fs_stop_checkpoint(sbi, true);
376 }
377
378 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
379 page->index != nid_of_node(page));
380
381 dec_page_count(sbi, type);
382 if (f2fs_in_warm_node_list(sbi, page))
383 f2fs_del_fsync_node_entry(sbi, page);
384 clear_cold_data(page);
385 end_page_writeback(page);
386 }
387 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
388 wq_has_sleeper(&sbi->cp_wait))
389 wake_up(&sbi->cp_wait);
390
391 bio_put(bio);
392}
393
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000394struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
395 block_t blk_addr, struct bio *bio)
396{
397 struct block_device *bdev = sbi->sb->s_bdev;
398 int i;
399
David Brazdil0f672f62019-12-10 10:32:29 +0000400 if (f2fs_is_multi_device(sbi)) {
401 for (i = 0; i < sbi->s_ndevs; i++) {
402 if (FDEV(i).start_blk <= blk_addr &&
403 FDEV(i).end_blk >= blk_addr) {
404 blk_addr -= FDEV(i).start_blk;
405 bdev = FDEV(i).bdev;
406 break;
407 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000408 }
409 }
410 if (bio) {
411 bio_set_dev(bio, bdev);
412 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
413 }
414 return bdev;
415}
416
417int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
418{
419 int i;
420
David Brazdil0f672f62019-12-10 10:32:29 +0000421 if (!f2fs_is_multi_device(sbi))
422 return 0;
423
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000424 for (i = 0; i < sbi->s_ndevs; i++)
425 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
426 return i;
427 return 0;
428}
429
Olivier Deprez157378f2022-04-04 15:47:50 +0200430/*
431 * Return true, if pre_bio's bdev is same as its target device.
432 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000433static bool __same_bdev(struct f2fs_sb_info *sbi,
434 block_t blk_addr, struct bio *bio)
435{
436 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
437 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
438}
439
David Brazdil0f672f62019-12-10 10:32:29 +0000440static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000441{
David Brazdil0f672f62019-12-10 10:32:29 +0000442 struct f2fs_sb_info *sbi = fio->sbi;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000443 struct bio *bio;
444
445 bio = f2fs_bio_alloc(sbi, npages, true);
446
David Brazdil0f672f62019-12-10 10:32:29 +0000447 f2fs_target_device(sbi, fio->new_blkaddr, bio);
448 if (is_read_io(fio->op)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000449 bio->bi_end_io = f2fs_read_end_io;
450 bio->bi_private = NULL;
451 } else {
452 bio->bi_end_io = f2fs_write_end_io;
453 bio->bi_private = sbi;
David Brazdil0f672f62019-12-10 10:32:29 +0000454 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
455 fio->type, fio->temp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000456 }
David Brazdil0f672f62019-12-10 10:32:29 +0000457 if (fio->io_wbc)
458 wbc_init_bio(fio->io_wbc, bio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000459
460 return bio;
461}
462
Olivier Deprez157378f2022-04-04 15:47:50 +0200463static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
464 pgoff_t first_idx,
465 const struct f2fs_io_info *fio,
466 gfp_t gfp_mask)
467{
468 /*
469 * The f2fs garbage collector sets ->encrypted_page when it wants to
470 * read/write raw data without encryption.
471 */
472 if (!fio || !fio->encrypted_page)
473 fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask);
474}
475
476static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode,
477 pgoff_t next_idx,
478 const struct f2fs_io_info *fio)
479{
480 /*
481 * The f2fs garbage collector sets ->encrypted_page when it wants to
482 * read/write raw data without encryption.
483 */
484 if (fio && fio->encrypted_page)
485 return !bio_has_crypt_ctx(bio);
486
487 return fscrypt_mergeable_bio(bio, inode, next_idx);
488}
489
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000490static inline void __submit_bio(struct f2fs_sb_info *sbi,
491 struct bio *bio, enum page_type type)
492{
493 if (!is_read_io(bio_op(bio))) {
494 unsigned int start;
495
496 if (type != DATA && type != NODE)
497 goto submit_io;
498
Olivier Deprez157378f2022-04-04 15:47:50 +0200499 if (f2fs_lfs_mode(sbi) && current->plug)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500 blk_finish_plug(current->plug);
501
Olivier Deprez0e641232021-09-23 10:07:05 +0200502 if (!F2FS_IO_ALIGNED(sbi))
David Brazdil0f672f62019-12-10 10:32:29 +0000503 goto submit_io;
504
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000505 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
506 start %= F2FS_IO_SIZE(sbi);
507
508 if (start == 0)
509 goto submit_io;
510
511 /* fill dummy pages */
512 for (; start < F2FS_IO_SIZE(sbi); start++) {
513 struct page *page =
514 mempool_alloc(sbi->write_io_dummy,
David Brazdil0f672f62019-12-10 10:32:29 +0000515 GFP_NOIO | __GFP_NOFAIL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000516 f2fs_bug_on(sbi, !page);
517
David Brazdil0f672f62019-12-10 10:32:29 +0000518 zero_user_segment(page, 0, PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000519 SetPagePrivate(page);
Olivier Deprez157378f2022-04-04 15:47:50 +0200520 set_page_private(page, DUMMY_WRITTEN_PAGE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000521 lock_page(page);
522 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
523 f2fs_bug_on(sbi, 1);
524 }
525 /*
526 * In the NODE case, we lose next block address chain. So, we
527 * need to do checkpoint in f2fs_sync_file.
528 */
529 if (type == NODE)
530 set_sbi_flag(sbi, SBI_NEED_CP);
531 }
532submit_io:
533 if (is_read_io(bio_op(bio)))
534 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
535 else
536 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
537 submit_bio(bio);
538}
539
Olivier Deprez157378f2022-04-04 15:47:50 +0200540void f2fs_submit_bio(struct f2fs_sb_info *sbi,
541 struct bio *bio, enum page_type type)
542{
543 __submit_bio(sbi, bio, type);
544}
545
546static void __attach_io_flag(struct f2fs_io_info *fio)
547{
548 struct f2fs_sb_info *sbi = fio->sbi;
549 unsigned int temp_mask = (1 << NR_TEMP_TYPE) - 1;
550 unsigned int io_flag, fua_flag, meta_flag;
551
552 if (fio->type == DATA)
553 io_flag = sbi->data_io_flag;
554 else if (fio->type == NODE)
555 io_flag = sbi->node_io_flag;
556 else
557 return;
558
559 fua_flag = io_flag & temp_mask;
560 meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask;
561
562 /*
563 * data/node io flag bits per temp:
564 * REQ_META | REQ_FUA |
565 * 5 | 4 | 3 | 2 | 1 | 0 |
566 * Cold | Warm | Hot | Cold | Warm | Hot |
567 */
568 if ((1 << fio->temp) & meta_flag)
569 fio->op_flags |= REQ_META;
570 if ((1 << fio->temp) & fua_flag)
571 fio->op_flags |= REQ_FUA;
572}
573
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000574static void __submit_merged_bio(struct f2fs_bio_info *io)
575{
576 struct f2fs_io_info *fio = &io->fio;
577
578 if (!io->bio)
579 return;
580
Olivier Deprez157378f2022-04-04 15:47:50 +0200581 __attach_io_flag(fio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000582 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
583
584 if (is_read_io(fio->op))
585 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
586 else
587 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
588
589 __submit_bio(io->sbi, io->bio, fio->type);
590 io->bio = NULL;
591}
592
David Brazdil0f672f62019-12-10 10:32:29 +0000593static bool __has_merged_page(struct bio *bio, struct inode *inode,
594 struct page *page, nid_t ino)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000595{
596 struct bio_vec *bvec;
David Brazdil0f672f62019-12-10 10:32:29 +0000597 struct bvec_iter_all iter_all;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000598
David Brazdil0f672f62019-12-10 10:32:29 +0000599 if (!bio)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000600 return false;
601
David Brazdil0f672f62019-12-10 10:32:29 +0000602 if (!inode && !page && !ino)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000603 return true;
604
David Brazdil0f672f62019-12-10 10:32:29 +0000605 bio_for_each_segment_all(bvec, bio, iter_all) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200606 struct page *target = bvec->bv_page;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000607
Olivier Deprez157378f2022-04-04 15:47:50 +0200608 if (fscrypt_is_bounce_page(target)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000609 target = fscrypt_pagecache_page(target);
Olivier Deprez157378f2022-04-04 15:47:50 +0200610 if (IS_ERR(target))
611 continue;
612 }
613 if (f2fs_is_compressed_page(target)) {
614 target = f2fs_compress_control_page(target);
615 if (IS_ERR(target))
616 continue;
617 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000618
619 if (inode && inode == target->mapping->host)
620 return true;
David Brazdil0f672f62019-12-10 10:32:29 +0000621 if (page && page == target)
622 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000623 if (ino && ino == ino_of_node(target))
624 return true;
625 }
626
627 return false;
628}
629
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000630static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
631 enum page_type type, enum temp_type temp)
632{
633 enum page_type btype = PAGE_TYPE_OF_BIO(type);
634 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
635
636 down_write(&io->io_rwsem);
637
638 /* change META to META_FLUSH in the checkpoint procedure */
639 if (type >= META_FLUSH) {
640 io->fio.type = META_FLUSH;
641 io->fio.op = REQ_OP_WRITE;
642 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
643 if (!test_opt(sbi, NOBARRIER))
644 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
645 }
646 __submit_merged_bio(io);
647 up_write(&io->io_rwsem);
648}
649
650static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
David Brazdil0f672f62019-12-10 10:32:29 +0000651 struct inode *inode, struct page *page,
652 nid_t ino, enum page_type type, bool force)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000653{
654 enum temp_type temp;
David Brazdil0f672f62019-12-10 10:32:29 +0000655 bool ret = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000656
657 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
David Brazdil0f672f62019-12-10 10:32:29 +0000658 if (!force) {
659 enum page_type btype = PAGE_TYPE_OF_BIO(type);
660 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000661
David Brazdil0f672f62019-12-10 10:32:29 +0000662 down_read(&io->io_rwsem);
663 ret = __has_merged_page(io->bio, inode, page, ino);
664 up_read(&io->io_rwsem);
665 }
666 if (ret)
667 __f2fs_submit_merged_write(sbi, type, temp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000668
669 /* TODO: use HOT temp only for meta pages now. */
670 if (type >= META)
671 break;
672 }
673}
674
675void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
676{
David Brazdil0f672f62019-12-10 10:32:29 +0000677 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000678}
679
680void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
David Brazdil0f672f62019-12-10 10:32:29 +0000681 struct inode *inode, struct page *page,
682 nid_t ino, enum page_type type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000683{
David Brazdil0f672f62019-12-10 10:32:29 +0000684 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000685}
686
687void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
688{
689 f2fs_submit_merged_write(sbi, DATA);
690 f2fs_submit_merged_write(sbi, NODE);
691 f2fs_submit_merged_write(sbi, META);
692}
693
694/*
695 * Fill the locked page with data located in the block address.
696 * A caller needs to unlock the page on failure.
697 */
698int f2fs_submit_page_bio(struct f2fs_io_info *fio)
699{
700 struct bio *bio;
701 struct page *page = fio->encrypted_page ?
702 fio->encrypted_page : fio->page;
703
704 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
David Brazdil0f672f62019-12-10 10:32:29 +0000705 fio->is_por ? META_POR : (__is_meta_io(fio) ?
706 META_GENERIC : DATA_GENERIC_ENHANCE)))
707 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000708
709 trace_f2fs_submit_page_bio(page, fio);
710 f2fs_trace_ios(fio, 0);
711
712 /* Allocate a new bio */
David Brazdil0f672f62019-12-10 10:32:29 +0000713 bio = __bio_alloc(fio, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000714
Olivier Deprez157378f2022-04-04 15:47:50 +0200715 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
716 fio->page->index, fio, GFP_NOIO);
717
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000718 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
719 bio_put(bio);
720 return -EFAULT;
721 }
722
723 if (fio->io_wbc && !is_read_io(fio->op))
David Brazdil0f672f62019-12-10 10:32:29 +0000724 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000725
Olivier Deprez157378f2022-04-04 15:47:50 +0200726 __attach_io_flag(fio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000727 bio_set_op_attrs(bio, fio->op, fio->op_flags);
728
David Brazdil0f672f62019-12-10 10:32:29 +0000729 inc_page_count(fio->sbi, is_read_io(fio->op) ?
730 __read_io_type(page): WB_DATA_TYPE(fio->page));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000731
732 __submit_bio(fio->sbi, bio, fio->type);
733 return 0;
734}
735
David Brazdil0f672f62019-12-10 10:32:29 +0000736static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
737 block_t last_blkaddr, block_t cur_blkaddr)
738{
739 if (last_blkaddr + 1 != cur_blkaddr)
740 return false;
741 return __same_bdev(sbi, cur_blkaddr, bio);
742}
743
744static bool io_type_is_mergeable(struct f2fs_bio_info *io,
745 struct f2fs_io_info *fio)
746{
747 if (io->fio.op != fio->op)
748 return false;
749 return io->fio.op_flags == fio->op_flags;
750}
751
752static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
753 struct f2fs_bio_info *io,
754 struct f2fs_io_info *fio,
755 block_t last_blkaddr,
756 block_t cur_blkaddr)
757{
758 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
759 unsigned int filled_blocks =
760 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
761 unsigned int io_size = F2FS_IO_SIZE(sbi);
762 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
763
764 /* IOs in bio is aligned and left space of vectors is not enough */
765 if (!(filled_blocks % io_size) && left_vecs < io_size)
766 return false;
767 }
768 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
769 return false;
770 return io_type_is_mergeable(io, fio);
771}
772
Olivier Deprez157378f2022-04-04 15:47:50 +0200773static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio,
774 struct page *page, enum temp_type temp)
775{
776 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
777 struct bio_entry *be;
778
779 be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
780 be->bio = bio;
781 bio_get(bio);
782
783 if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE)
784 f2fs_bug_on(sbi, 1);
785
786 down_write(&io->bio_list_lock);
787 list_add_tail(&be->list, &io->bio_list);
788 up_write(&io->bio_list_lock);
789}
790
791static void del_bio_entry(struct bio_entry *be)
792{
793 list_del(&be->list);
794 kmem_cache_free(bio_entry_slab, be);
795}
796
797static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio,
798 struct page *page)
799{
800 struct f2fs_sb_info *sbi = fio->sbi;
801 enum temp_type temp;
802 bool found = false;
803 int ret = -EAGAIN;
804
805 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
806 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
807 struct list_head *head = &io->bio_list;
808 struct bio_entry *be;
809
810 down_write(&io->bio_list_lock);
811 list_for_each_entry(be, head, list) {
812 if (be->bio != *bio)
813 continue;
814
815 found = true;
816
817 f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio,
818 *fio->last_block,
819 fio->new_blkaddr));
820 if (f2fs_crypt_mergeable_bio(*bio,
821 fio->page->mapping->host,
822 fio->page->index, fio) &&
823 bio_add_page(*bio, page, PAGE_SIZE, 0) ==
824 PAGE_SIZE) {
825 ret = 0;
826 break;
827 }
828
829 /* page can't be merged into bio; submit the bio */
830 del_bio_entry(be);
831 __submit_bio(sbi, *bio, DATA);
832 break;
833 }
834 up_write(&io->bio_list_lock);
835 }
836
837 if (ret) {
838 bio_put(*bio);
839 *bio = NULL;
840 }
841
842 return ret;
843}
844
845void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
846 struct bio **bio, struct page *page)
847{
848 enum temp_type temp;
849 bool found = false;
850 struct bio *target = bio ? *bio : NULL;
851
852 for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) {
853 struct f2fs_bio_info *io = sbi->write_io[DATA] + temp;
854 struct list_head *head = &io->bio_list;
855 struct bio_entry *be;
856
857 if (list_empty(head))
858 continue;
859
860 down_read(&io->bio_list_lock);
861 list_for_each_entry(be, head, list) {
862 if (target)
863 found = (target == be->bio);
864 else
865 found = __has_merged_page(be->bio, NULL,
866 page, 0);
867 if (found)
868 break;
869 }
870 up_read(&io->bio_list_lock);
871
872 if (!found)
873 continue;
874
875 found = false;
876
877 down_write(&io->bio_list_lock);
878 list_for_each_entry(be, head, list) {
879 if (target)
880 found = (target == be->bio);
881 else
882 found = __has_merged_page(be->bio, NULL,
883 page, 0);
884 if (found) {
885 target = be->bio;
886 del_bio_entry(be);
887 break;
888 }
889 }
890 up_write(&io->bio_list_lock);
891 }
892
893 if (found)
894 __submit_bio(sbi, target, DATA);
895 if (bio && *bio) {
896 bio_put(*bio);
897 *bio = NULL;
898 }
899}
900
David Brazdil0f672f62019-12-10 10:32:29 +0000901int f2fs_merge_page_bio(struct f2fs_io_info *fio)
902{
903 struct bio *bio = *fio->bio;
904 struct page *page = fio->encrypted_page ?
905 fio->encrypted_page : fio->page;
906
907 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
908 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
909 return -EFSCORRUPTED;
910
911 trace_f2fs_submit_page_bio(page, fio);
912 f2fs_trace_ios(fio, 0);
913
914 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
Olivier Deprez157378f2022-04-04 15:47:50 +0200915 fio->new_blkaddr))
916 f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL);
David Brazdil0f672f62019-12-10 10:32:29 +0000917alloc_new:
918 if (!bio) {
919 bio = __bio_alloc(fio, BIO_MAX_PAGES);
Olivier Deprez157378f2022-04-04 15:47:50 +0200920 __attach_io_flag(fio);
921 f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host,
922 fio->page->index, fio, GFP_NOIO);
David Brazdil0f672f62019-12-10 10:32:29 +0000923 bio_set_op_attrs(bio, fio->op, fio->op_flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000924
Olivier Deprez157378f2022-04-04 15:47:50 +0200925 add_bio_entry(fio->sbi, bio, page, fio->temp);
926 } else {
927 if (add_ipu_page(fio, &bio, page))
928 goto alloc_new;
David Brazdil0f672f62019-12-10 10:32:29 +0000929 }
930
931 if (fio->io_wbc)
932 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
933
934 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
935
936 *fio->last_block = fio->new_blkaddr;
937 *fio->bio = bio;
938
939 return 0;
940}
941
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000942void f2fs_submit_page_write(struct f2fs_io_info *fio)
943{
944 struct f2fs_sb_info *sbi = fio->sbi;
945 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
946 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
947 struct page *bio_page;
948
949 f2fs_bug_on(sbi, is_read_io(fio->op));
950
951 down_write(&io->io_rwsem);
952next:
953 if (fio->in_list) {
954 spin_lock(&io->io_lock);
955 if (list_empty(&io->io_list)) {
956 spin_unlock(&io->io_lock);
957 goto out;
958 }
959 fio = list_first_entry(&io->io_list,
960 struct f2fs_io_info, list);
961 list_del(&fio->list);
962 spin_unlock(&io->io_lock);
963 }
964
David Brazdil0f672f62019-12-10 10:32:29 +0000965 verify_fio_blkaddr(fio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000966
Olivier Deprez157378f2022-04-04 15:47:50 +0200967 if (fio->encrypted_page)
968 bio_page = fio->encrypted_page;
969 else if (fio->compressed_page)
970 bio_page = fio->compressed_page;
971 else
972 bio_page = fio->page;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000973
974 /* set submitted = true as a return value */
975 fio->submitted = true;
976
977 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
978
Olivier Deprez157378f2022-04-04 15:47:50 +0200979 if (io->bio &&
980 (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio,
981 fio->new_blkaddr) ||
982 !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host,
983 bio_page->index, fio)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000984 __submit_merged_bio(io);
985alloc_new:
986 if (io->bio == NULL) {
David Brazdil0f672f62019-12-10 10:32:29 +0000987 if (F2FS_IO_ALIGNED(sbi) &&
988 (fio->type == DATA || fio->type == NODE) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000989 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
990 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
991 fio->retry = true;
992 goto skip;
993 }
David Brazdil0f672f62019-12-10 10:32:29 +0000994 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
Olivier Deprez157378f2022-04-04 15:47:50 +0200995 f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host,
996 bio_page->index, fio, GFP_NOIO);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000997 io->fio = *fio;
998 }
999
1000 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
1001 __submit_merged_bio(io);
1002 goto alloc_new;
1003 }
1004
1005 if (fio->io_wbc)
David Brazdil0f672f62019-12-10 10:32:29 +00001006 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001007
1008 io->last_block_in_bio = fio->new_blkaddr;
1009 f2fs_trace_ios(fio, 0);
1010
1011 trace_f2fs_submit_page_write(fio->page, fio);
1012skip:
1013 if (fio->in_list)
1014 goto next;
1015out:
David Brazdil0f672f62019-12-10 10:32:29 +00001016 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1017 !f2fs_is_checkpoint_ready(sbi))
1018 __submit_merged_bio(io);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001019 up_write(&io->io_rwsem);
1020}
1021
David Brazdil0f672f62019-12-10 10:32:29 +00001022static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
1023{
1024 return fsverity_active(inode) &&
1025 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
1026}
1027
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001028static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
David Brazdil0f672f62019-12-10 10:32:29 +00001029 unsigned nr_pages, unsigned op_flag,
Olivier Deprez157378f2022-04-04 15:47:50 +02001030 pgoff_t first_idx, bool for_write,
1031 bool for_verity)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001032{
1033 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1034 struct bio *bio;
1035 struct bio_post_read_ctx *ctx;
1036 unsigned int post_read_steps = 0;
1037
Olivier Deprez157378f2022-04-04 15:47:50 +02001038 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES),
1039 for_write);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001040 if (!bio)
1041 return ERR_PTR(-ENOMEM);
Olivier Deprez157378f2022-04-04 15:47:50 +02001042
1043 f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS);
1044
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001045 f2fs_target_device(sbi, blkaddr, bio);
1046 bio->bi_end_io = f2fs_read_end_io;
1047 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
1048
Olivier Deprez157378f2022-04-04 15:47:50 +02001049 if (fscrypt_inode_uses_fs_layer_crypto(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001050 post_read_steps |= 1 << STEP_DECRYPT;
Olivier Deprez157378f2022-04-04 15:47:50 +02001051 if (f2fs_compressed_file(inode))
1052 post_read_steps |= 1 << STEP_DECOMPRESS_NOWQ;
1053 if (for_verity && f2fs_need_verity(inode, first_idx))
David Brazdil0f672f62019-12-10 10:32:29 +00001054 post_read_steps |= 1 << STEP_VERITY;
1055
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001056 if (post_read_steps) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001057 /* Due to the mempool, this never fails. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001058 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001059 ctx->bio = bio;
Olivier Deprez157378f2022-04-04 15:47:50 +02001060 ctx->sbi = sbi;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001061 ctx->enabled_steps = post_read_steps;
1062 bio->bi_private = ctx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001063 }
1064
1065 return bio;
1066}
1067
Olivier Deprez157378f2022-04-04 15:47:50 +02001068static void f2fs_release_read_bio(struct bio *bio)
1069{
1070 if (bio->bi_private)
1071 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
1072 bio_put(bio);
1073}
1074
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001075/* This can handle encryption stuffs */
1076static int f2fs_submit_page_read(struct inode *inode, struct page *page,
Olivier Deprez157378f2022-04-04 15:47:50 +02001077 block_t blkaddr, int op_flags, bool for_write)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001078{
David Brazdil0f672f62019-12-10 10:32:29 +00001079 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1080 struct bio *bio;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001081
Olivier Deprez157378f2022-04-04 15:47:50 +02001082 bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags,
1083 page->index, for_write, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001084 if (IS_ERR(bio))
1085 return PTR_ERR(bio);
1086
David Brazdil0f672f62019-12-10 10:32:29 +00001087 /* wait for GCed page writeback via META_MAPPING */
1088 f2fs_wait_on_block_writeback(inode, blkaddr);
1089
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001090 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1091 bio_put(bio);
1092 return -EFAULT;
1093 }
1094 ClearPageError(page);
David Brazdil0f672f62019-12-10 10:32:29 +00001095 inc_page_count(sbi, F2FS_RD_DATA);
Olivier Deprez157378f2022-04-04 15:47:50 +02001096 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
David Brazdil0f672f62019-12-10 10:32:29 +00001097 __submit_bio(sbi, bio, DATA);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001098 return 0;
1099}
1100
1101static void __set_data_blkaddr(struct dnode_of_data *dn)
1102{
1103 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
1104 __le32 *addr_array;
1105 int base = 0;
1106
1107 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
1108 base = get_extra_isize(dn->inode);
1109
1110 /* Get physical address of data block */
1111 addr_array = blkaddr_in_node(rn);
1112 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
1113}
1114
1115/*
1116 * Lock ordering for the change of data block address:
1117 * ->data_page
1118 * ->node_page
1119 * update block addresses in the node page
1120 */
1121void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
1122{
David Brazdil0f672f62019-12-10 10:32:29 +00001123 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001124 __set_data_blkaddr(dn);
1125 if (set_page_dirty(dn->node_page))
1126 dn->node_changed = true;
1127}
1128
1129void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
1130{
1131 dn->data_blkaddr = blkaddr;
1132 f2fs_set_data_blkaddr(dn);
1133 f2fs_update_extent_cache(dn);
1134}
1135
1136/* dn->ofs_in_node will be returned with up-to-date last block pointer */
1137int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
1138{
1139 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1140 int err;
1141
1142 if (!count)
1143 return 0;
1144
1145 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1146 return -EPERM;
1147 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1148 return err;
1149
1150 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
1151 dn->ofs_in_node, count);
1152
David Brazdil0f672f62019-12-10 10:32:29 +00001153 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001154
1155 for (; count > 0; dn->ofs_in_node++) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001156 block_t blkaddr = f2fs_data_blkaddr(dn);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001157 if (blkaddr == NULL_ADDR) {
1158 dn->data_blkaddr = NEW_ADDR;
1159 __set_data_blkaddr(dn);
1160 count--;
1161 }
1162 }
1163
1164 if (set_page_dirty(dn->node_page))
1165 dn->node_changed = true;
1166 return 0;
1167}
1168
1169/* Should keep dn->ofs_in_node unchanged */
1170int f2fs_reserve_new_block(struct dnode_of_data *dn)
1171{
1172 unsigned int ofs_in_node = dn->ofs_in_node;
1173 int ret;
1174
1175 ret = f2fs_reserve_new_blocks(dn, 1);
1176 dn->ofs_in_node = ofs_in_node;
1177 return ret;
1178}
1179
1180int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
1181{
1182 bool need_put = dn->inode_page ? false : true;
1183 int err;
1184
1185 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
1186 if (err)
1187 return err;
1188
1189 if (dn->data_blkaddr == NULL_ADDR)
1190 err = f2fs_reserve_new_block(dn);
1191 if (err || need_put)
1192 f2fs_put_dnode(dn);
1193 return err;
1194}
1195
1196int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
1197{
Olivier Deprez157378f2022-04-04 15:47:50 +02001198 struct extent_info ei = {0, 0, 0};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001199 struct inode *inode = dn->inode;
1200
1201 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1202 dn->data_blkaddr = ei.blk + index - ei.fofs;
1203 return 0;
1204 }
1205
1206 return f2fs_reserve_block(dn, index);
1207}
1208
1209struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
1210 int op_flags, bool for_write)
1211{
1212 struct address_space *mapping = inode->i_mapping;
1213 struct dnode_of_data dn;
1214 struct page *page;
1215 struct extent_info ei = {0,0,0};
1216 int err;
1217
1218 page = f2fs_grab_cache_page(mapping, index, for_write);
1219 if (!page)
1220 return ERR_PTR(-ENOMEM);
1221
1222 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1223 dn.data_blkaddr = ei.blk + index - ei.fofs;
David Brazdil0f672f62019-12-10 10:32:29 +00001224 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
1225 DATA_GENERIC_ENHANCE_READ)) {
1226 err = -EFSCORRUPTED;
1227 goto put_err;
1228 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001229 goto got_it;
1230 }
1231
1232 set_new_dnode(&dn, inode, NULL, NULL, 0);
1233 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
1234 if (err)
1235 goto put_err;
1236 f2fs_put_dnode(&dn);
1237
1238 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
1239 err = -ENOENT;
1240 goto put_err;
1241 }
David Brazdil0f672f62019-12-10 10:32:29 +00001242 if (dn.data_blkaddr != NEW_ADDR &&
1243 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
1244 dn.data_blkaddr,
1245 DATA_GENERIC_ENHANCE)) {
1246 err = -EFSCORRUPTED;
1247 goto put_err;
1248 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001249got_it:
1250 if (PageUptodate(page)) {
1251 unlock_page(page);
1252 return page;
1253 }
1254
1255 /*
1256 * A new dentry page is allocated but not able to be written, since its
1257 * new inode page couldn't be allocated due to -ENOSPC.
1258 * In such the case, its blkaddr can be remained as NEW_ADDR.
1259 * see, f2fs_add_link -> f2fs_get_new_data_page ->
1260 * f2fs_init_inode_metadata.
1261 */
1262 if (dn.data_blkaddr == NEW_ADDR) {
1263 zero_user_segment(page, 0, PAGE_SIZE);
1264 if (!PageUptodate(page))
1265 SetPageUptodate(page);
1266 unlock_page(page);
1267 return page;
1268 }
1269
Olivier Deprez157378f2022-04-04 15:47:50 +02001270 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr,
1271 op_flags, for_write);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001272 if (err)
1273 goto put_err;
1274 return page;
1275
1276put_err:
1277 f2fs_put_page(page, 1);
1278 return ERR_PTR(err);
1279}
1280
1281struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
1282{
1283 struct address_space *mapping = inode->i_mapping;
1284 struct page *page;
1285
1286 page = find_get_page(mapping, index);
1287 if (page && PageUptodate(page))
1288 return page;
1289 f2fs_put_page(page, 0);
1290
1291 page = f2fs_get_read_data_page(inode, index, 0, false);
1292 if (IS_ERR(page))
1293 return page;
1294
1295 if (PageUptodate(page))
1296 return page;
1297
1298 wait_on_page_locked(page);
1299 if (unlikely(!PageUptodate(page))) {
1300 f2fs_put_page(page, 0);
1301 return ERR_PTR(-EIO);
1302 }
1303 return page;
1304}
1305
1306/*
1307 * If it tries to access a hole, return an error.
1308 * Because, the callers, functions in dir.c and GC, should be able to know
1309 * whether this page exists or not.
1310 */
1311struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
1312 bool for_write)
1313{
1314 struct address_space *mapping = inode->i_mapping;
1315 struct page *page;
1316repeat:
1317 page = f2fs_get_read_data_page(inode, index, 0, for_write);
1318 if (IS_ERR(page))
1319 return page;
1320
1321 /* wait for read completion */
1322 lock_page(page);
1323 if (unlikely(page->mapping != mapping)) {
1324 f2fs_put_page(page, 1);
1325 goto repeat;
1326 }
1327 if (unlikely(!PageUptodate(page))) {
1328 f2fs_put_page(page, 1);
1329 return ERR_PTR(-EIO);
1330 }
1331 return page;
1332}
1333
1334/*
1335 * Caller ensures that this data page is never allocated.
1336 * A new zero-filled data page is allocated in the page cache.
1337 *
1338 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1339 * f2fs_unlock_op().
1340 * Note that, ipage is set only by make_empty_dir, and if any error occur,
1341 * ipage should be released by this function.
1342 */
1343struct page *f2fs_get_new_data_page(struct inode *inode,
1344 struct page *ipage, pgoff_t index, bool new_i_size)
1345{
1346 struct address_space *mapping = inode->i_mapping;
1347 struct page *page;
1348 struct dnode_of_data dn;
1349 int err;
1350
1351 page = f2fs_grab_cache_page(mapping, index, true);
1352 if (!page) {
1353 /*
1354 * before exiting, we should make sure ipage will be released
1355 * if any error occur.
1356 */
1357 f2fs_put_page(ipage, 1);
1358 return ERR_PTR(-ENOMEM);
1359 }
1360
1361 set_new_dnode(&dn, inode, ipage, NULL, 0);
1362 err = f2fs_reserve_block(&dn, index);
1363 if (err) {
1364 f2fs_put_page(page, 1);
1365 return ERR_PTR(err);
1366 }
1367 if (!ipage)
1368 f2fs_put_dnode(&dn);
1369
1370 if (PageUptodate(page))
1371 goto got_it;
1372
1373 if (dn.data_blkaddr == NEW_ADDR) {
1374 zero_user_segment(page, 0, PAGE_SIZE);
1375 if (!PageUptodate(page))
1376 SetPageUptodate(page);
1377 } else {
1378 f2fs_put_page(page, 1);
1379
1380 /* if ipage exists, blkaddr should be NEW_ADDR */
1381 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1382 page = f2fs_get_lock_data_page(inode, index, true);
1383 if (IS_ERR(page))
1384 return page;
1385 }
1386got_it:
1387 if (new_i_size && i_size_read(inode) <
1388 ((loff_t)(index + 1) << PAGE_SHIFT))
1389 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1390 return page;
1391}
1392
1393static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1394{
1395 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1396 struct f2fs_summary sum;
1397 struct node_info ni;
1398 block_t old_blkaddr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001399 blkcnt_t count = 1;
1400 int err;
1401
1402 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1403 return -EPERM;
1404
1405 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1406 if (err)
1407 return err;
1408
Olivier Deprez157378f2022-04-04 15:47:50 +02001409 dn->data_blkaddr = f2fs_data_blkaddr(dn);
David Brazdil0f672f62019-12-10 10:32:29 +00001410 if (dn->data_blkaddr != NULL_ADDR)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001411 goto alloc;
1412
1413 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1414 return err;
1415
1416alloc:
1417 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1418 old_blkaddr = dn->data_blkaddr;
1419 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
Olivier Deprez157378f2022-04-04 15:47:50 +02001420 &sum, seg_type, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001421 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1422 invalidate_mapping_pages(META_MAPPING(sbi),
1423 old_blkaddr, old_blkaddr);
David Brazdil0f672f62019-12-10 10:32:29 +00001424 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001425
David Brazdil0f672f62019-12-10 10:32:29 +00001426 /*
1427 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1428 * data from unwritten block via dio_read.
1429 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001430 return 0;
1431}
1432
1433int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1434{
1435 struct inode *inode = file_inode(iocb->ki_filp);
1436 struct f2fs_map_blocks map;
1437 int flag;
1438 int err = 0;
1439 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1440
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001441 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1442 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1443 if (map.m_len > map.m_lblk)
1444 map.m_len -= map.m_lblk;
1445 else
1446 map.m_len = 0;
1447
1448 map.m_next_pgofs = NULL;
1449 map.m_next_extent = NULL;
1450 map.m_seg_type = NO_CHECK_TYPE;
David Brazdil0f672f62019-12-10 10:32:29 +00001451 map.m_may_create = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001452
1453 if (direct_io) {
1454 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
David Brazdil0f672f62019-12-10 10:32:29 +00001455 flag = f2fs_force_buffered_io(inode, iocb, from) ?
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001456 F2FS_GET_BLOCK_PRE_AIO :
1457 F2FS_GET_BLOCK_PRE_DIO;
1458 goto map_blocks;
1459 }
1460 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1461 err = f2fs_convert_inline_inode(inode);
1462 if (err)
1463 return err;
1464 }
1465 if (f2fs_has_inline_data(inode))
1466 return err;
1467
1468 flag = F2FS_GET_BLOCK_PRE_AIO;
1469
1470map_blocks:
1471 err = f2fs_map_blocks(inode, &map, 1, flag);
1472 if (map.m_len > 0 && err == -ENOSPC) {
1473 if (!direct_io)
1474 set_inode_flag(inode, FI_NO_PREALLOC);
1475 err = 0;
1476 }
1477 return err;
1478}
1479
Olivier Deprez157378f2022-04-04 15:47:50 +02001480void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001481{
1482 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1483 if (lock)
1484 down_read(&sbi->node_change);
1485 else
1486 up_read(&sbi->node_change);
1487 } else {
1488 if (lock)
1489 f2fs_lock_op(sbi);
1490 else
1491 f2fs_unlock_op(sbi);
1492 }
1493}
1494
1495/*
Olivier Deprez157378f2022-04-04 15:47:50 +02001496 * f2fs_map_blocks() tries to find or build mapping relationship which
1497 * maps continuous logical blocks to physical blocks, and return such
1498 * info via f2fs_map_blocks structure.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001499 */
1500int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1501 int create, int flag)
1502{
1503 unsigned int maxblocks = map->m_len;
1504 struct dnode_of_data dn;
1505 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001506 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001507 pgoff_t pgofs, end_offset, end;
1508 int err = 0, ofs = 1;
1509 unsigned int ofs_in_node, last_ofs_in_node;
1510 blkcnt_t prealloc;
1511 struct extent_info ei = {0,0,0};
1512 block_t blkaddr;
1513 unsigned int start_pgofs;
1514
1515 if (!maxblocks)
1516 return 0;
1517
1518 map->m_len = 0;
1519 map->m_flags = 0;
1520
1521 /* it only supports block size == page size */
1522 pgofs = (pgoff_t)map->m_lblk;
1523 end = pgofs + maxblocks;
1524
1525 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001526 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
David Brazdil0f672f62019-12-10 10:32:29 +00001527 map->m_may_create)
1528 goto next_dnode;
1529
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001530 map->m_pblk = ei.blk + pgofs - ei.fofs;
1531 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1532 map->m_flags = F2FS_MAP_MAPPED;
1533 if (map->m_next_extent)
1534 *map->m_next_extent = pgofs + map->m_len;
David Brazdil0f672f62019-12-10 10:32:29 +00001535
1536 /* for hardware encryption, but to avoid potential issue in future */
1537 if (flag == F2FS_GET_BLOCK_DIO)
1538 f2fs_wait_on_block_writeback_range(inode,
1539 map->m_pblk, map->m_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001540 goto out;
1541 }
1542
1543next_dnode:
David Brazdil0f672f62019-12-10 10:32:29 +00001544 if (map->m_may_create)
Olivier Deprez157378f2022-04-04 15:47:50 +02001545 f2fs_do_map_lock(sbi, flag, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001546
1547 /* When reading holes, we need its node page */
1548 set_new_dnode(&dn, inode, NULL, NULL, 0);
1549 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1550 if (err) {
1551 if (flag == F2FS_GET_BLOCK_BMAP)
1552 map->m_pblk = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02001553
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001554 if (err == -ENOENT) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001555 /*
1556 * There is one exceptional case that read_node_page()
1557 * may return -ENOENT due to filesystem has been
1558 * shutdown or cp_error, so force to convert error
1559 * number to EIO for such case.
1560 */
1561 if (map->m_may_create &&
1562 (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1563 f2fs_cp_error(sbi))) {
1564 err = -EIO;
1565 goto unlock_out;
1566 }
1567
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001568 err = 0;
1569 if (map->m_next_pgofs)
1570 *map->m_next_pgofs =
1571 f2fs_get_next_page_offset(&dn, pgofs);
1572 if (map->m_next_extent)
1573 *map->m_next_extent =
1574 f2fs_get_next_page_offset(&dn, pgofs);
1575 }
1576 goto unlock_out;
1577 }
1578
1579 start_pgofs = pgofs;
1580 prealloc = 0;
1581 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1582 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1583
1584next_block:
Olivier Deprez157378f2022-04-04 15:47:50 +02001585 blkaddr = f2fs_data_blkaddr(&dn);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001586
1587 if (__is_valid_data_blkaddr(blkaddr) &&
David Brazdil0f672f62019-12-10 10:32:29 +00001588 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1589 err = -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001590 goto sync_out;
1591 }
1592
David Brazdil0f672f62019-12-10 10:32:29 +00001593 if (__is_valid_data_blkaddr(blkaddr)) {
1594 /* use out-place-update for driect IO under LFS mode */
Olivier Deprez157378f2022-04-04 15:47:50 +02001595 if (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO &&
David Brazdil0f672f62019-12-10 10:32:29 +00001596 map->m_may_create) {
1597 err = __allocate_data_block(&dn, map->m_seg_type);
1598 if (err)
1599 goto sync_out;
1600 blkaddr = dn.data_blkaddr;
1601 set_inode_flag(inode, FI_APPEND_WRITE);
1602 }
1603 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001604 if (create) {
1605 if (unlikely(f2fs_cp_error(sbi))) {
1606 err = -EIO;
1607 goto sync_out;
1608 }
1609 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1610 if (blkaddr == NULL_ADDR) {
1611 prealloc++;
1612 last_ofs_in_node = dn.ofs_in_node;
1613 }
1614 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00001615 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1616 flag != F2FS_GET_BLOCK_DIO);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001617 err = __allocate_data_block(&dn,
1618 map->m_seg_type);
1619 if (!err)
1620 set_inode_flag(inode, FI_APPEND_WRITE);
1621 }
1622 if (err)
1623 goto sync_out;
1624 map->m_flags |= F2FS_MAP_NEW;
1625 blkaddr = dn.data_blkaddr;
1626 } else {
1627 if (flag == F2FS_GET_BLOCK_BMAP) {
1628 map->m_pblk = 0;
1629 goto sync_out;
1630 }
1631 if (flag == F2FS_GET_BLOCK_PRECACHE)
1632 goto sync_out;
1633 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1634 blkaddr == NULL_ADDR) {
1635 if (map->m_next_pgofs)
1636 *map->m_next_pgofs = pgofs + 1;
1637 goto sync_out;
1638 }
1639 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1640 /* for defragment case */
1641 if (map->m_next_pgofs)
1642 *map->m_next_pgofs = pgofs + 1;
1643 goto sync_out;
1644 }
1645 }
1646 }
1647
1648 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1649 goto skip;
1650
1651 if (map->m_len == 0) {
1652 /* preallocated unwritten block should be mapped for fiemap. */
1653 if (blkaddr == NEW_ADDR)
1654 map->m_flags |= F2FS_MAP_UNWRITTEN;
1655 map->m_flags |= F2FS_MAP_MAPPED;
1656
1657 map->m_pblk = blkaddr;
1658 map->m_len = 1;
1659 } else if ((map->m_pblk != NEW_ADDR &&
1660 blkaddr == (map->m_pblk + ofs)) ||
1661 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1662 flag == F2FS_GET_BLOCK_PRE_DIO) {
1663 ofs++;
1664 map->m_len++;
1665 } else {
1666 goto sync_out;
1667 }
1668
1669skip:
1670 dn.ofs_in_node++;
1671 pgofs++;
1672
1673 /* preallocate blocks in batch for one dnode page */
1674 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1675 (pgofs == end || dn.ofs_in_node == end_offset)) {
1676
1677 dn.ofs_in_node = ofs_in_node;
1678 err = f2fs_reserve_new_blocks(&dn, prealloc);
1679 if (err)
1680 goto sync_out;
1681
1682 map->m_len += dn.ofs_in_node - ofs_in_node;
1683 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1684 err = -ENOSPC;
1685 goto sync_out;
1686 }
1687 dn.ofs_in_node = end_offset;
1688 }
1689
1690 if (pgofs >= end)
1691 goto sync_out;
1692 else if (dn.ofs_in_node < end_offset)
1693 goto next_block;
1694
1695 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1696 if (map->m_flags & F2FS_MAP_MAPPED) {
1697 unsigned int ofs = start_pgofs - map->m_lblk;
1698
1699 f2fs_update_extent_cache_range(&dn,
1700 start_pgofs, map->m_pblk + ofs,
1701 map->m_len - ofs);
1702 }
1703 }
1704
1705 f2fs_put_dnode(&dn);
1706
David Brazdil0f672f62019-12-10 10:32:29 +00001707 if (map->m_may_create) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001708 f2fs_do_map_lock(sbi, flag, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001709 f2fs_balance_fs(sbi, dn.node_changed);
1710 }
1711 goto next_dnode;
1712
1713sync_out:
David Brazdil0f672f62019-12-10 10:32:29 +00001714
1715 /* for hardware encryption, but to avoid potential issue in future */
1716 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1717 f2fs_wait_on_block_writeback_range(inode,
1718 map->m_pblk, map->m_len);
1719
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001720 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1721 if (map->m_flags & F2FS_MAP_MAPPED) {
1722 unsigned int ofs = start_pgofs - map->m_lblk;
1723
1724 f2fs_update_extent_cache_range(&dn,
1725 start_pgofs, map->m_pblk + ofs,
1726 map->m_len - ofs);
1727 }
1728 if (map->m_next_extent)
1729 *map->m_next_extent = pgofs + 1;
1730 }
1731 f2fs_put_dnode(&dn);
1732unlock_out:
David Brazdil0f672f62019-12-10 10:32:29 +00001733 if (map->m_may_create) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001734 f2fs_do_map_lock(sbi, flag, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001735 f2fs_balance_fs(sbi, dn.node_changed);
1736 }
1737out:
1738 trace_f2fs_map_blocks(inode, map, err);
1739 return err;
1740}
1741
1742bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1743{
1744 struct f2fs_map_blocks map;
1745 block_t last_lblk;
1746 int err;
1747
1748 if (pos + len > i_size_read(inode))
1749 return false;
1750
1751 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1752 map.m_next_pgofs = NULL;
1753 map.m_next_extent = NULL;
1754 map.m_seg_type = NO_CHECK_TYPE;
David Brazdil0f672f62019-12-10 10:32:29 +00001755 map.m_may_create = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001756 last_lblk = F2FS_BLK_ALIGN(pos + len);
1757
1758 while (map.m_lblk < last_lblk) {
1759 map.m_len = last_lblk - map.m_lblk;
1760 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1761 if (err || map.m_len == 0)
1762 return false;
1763 map.m_lblk += map.m_len;
1764 }
1765 return true;
1766}
1767
1768static int __get_data_block(struct inode *inode, sector_t iblock,
1769 struct buffer_head *bh, int create, int flag,
David Brazdil0f672f62019-12-10 10:32:29 +00001770 pgoff_t *next_pgofs, int seg_type, bool may_write)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001771{
1772 struct f2fs_map_blocks map;
1773 int err;
1774
1775 map.m_lblk = iblock;
1776 map.m_len = bh->b_size >> inode->i_blkbits;
1777 map.m_next_pgofs = next_pgofs;
1778 map.m_next_extent = NULL;
1779 map.m_seg_type = seg_type;
David Brazdil0f672f62019-12-10 10:32:29 +00001780 map.m_may_create = may_write;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001781
1782 err = f2fs_map_blocks(inode, &map, create, flag);
1783 if (!err) {
1784 map_bh(bh, inode->i_sb, map.m_pblk);
1785 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1786 bh->b_size = (u64)map.m_len << inode->i_blkbits;
1787 }
1788 return err;
1789}
1790
1791static int get_data_block(struct inode *inode, sector_t iblock,
1792 struct buffer_head *bh_result, int create, int flag,
1793 pgoff_t *next_pgofs)
1794{
1795 return __get_data_block(inode, iblock, bh_result, create,
1796 flag, next_pgofs,
David Brazdil0f672f62019-12-10 10:32:29 +00001797 NO_CHECK_TYPE, create);
1798}
1799
1800static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1801 struct buffer_head *bh_result, int create)
1802{
1803 return __get_data_block(inode, iblock, bh_result, create,
1804 F2FS_GET_BLOCK_DIO, NULL,
1805 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1806 IS_SWAPFILE(inode) ? false : true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001807}
1808
1809static int get_data_block_dio(struct inode *inode, sector_t iblock,
1810 struct buffer_head *bh_result, int create)
1811{
1812 return __get_data_block(inode, iblock, bh_result, create,
David Brazdil0f672f62019-12-10 10:32:29 +00001813 F2FS_GET_BLOCK_DIO, NULL,
1814 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1815 false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001816}
1817
1818static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1819 struct buffer_head *bh_result, int create)
1820{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001821 return __get_data_block(inode, iblock, bh_result, create,
1822 F2FS_GET_BLOCK_BMAP, NULL,
David Brazdil0f672f62019-12-10 10:32:29 +00001823 NO_CHECK_TYPE, create);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001824}
1825
1826static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1827{
1828 return (offset >> inode->i_blkbits);
1829}
1830
1831static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1832{
1833 return (blk << inode->i_blkbits);
1834}
1835
1836static int f2fs_xattr_fiemap(struct inode *inode,
1837 struct fiemap_extent_info *fieinfo)
1838{
1839 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1840 struct page *page;
1841 struct node_info ni;
1842 __u64 phys = 0, len;
1843 __u32 flags;
1844 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1845 int err = 0;
1846
1847 if (f2fs_has_inline_xattr(inode)) {
1848 int offset;
1849
1850 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1851 inode->i_ino, false);
1852 if (!page)
1853 return -ENOMEM;
1854
1855 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1856 if (err) {
1857 f2fs_put_page(page, 1);
1858 return err;
1859 }
1860
1861 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1862 offset = offsetof(struct f2fs_inode, i_addr) +
1863 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1864 get_inline_xattr_addrs(inode));
1865
1866 phys += offset;
1867 len = inline_xattr_size(inode);
1868
1869 f2fs_put_page(page, 1);
1870
1871 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1872
1873 if (!xnid)
1874 flags |= FIEMAP_EXTENT_LAST;
1875
1876 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02001877 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001878 if (err || err == 1)
1879 return err;
1880 }
1881
1882 if (xnid) {
1883 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1884 if (!page)
1885 return -ENOMEM;
1886
1887 err = f2fs_get_node_info(sbi, xnid, &ni);
1888 if (err) {
1889 f2fs_put_page(page, 1);
1890 return err;
1891 }
1892
1893 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1894 len = inode->i_sb->s_blocksize;
1895
1896 f2fs_put_page(page, 1);
1897
1898 flags = FIEMAP_EXTENT_LAST;
1899 }
1900
Olivier Deprez157378f2022-04-04 15:47:50 +02001901 if (phys) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001902 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02001903 trace_f2fs_fiemap(inode, 0, phys, len, flags, err);
1904 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001905
1906 return (err < 0 ? err : 0);
1907}
1908
Olivier Deprez157378f2022-04-04 15:47:50 +02001909static loff_t max_inode_blocks(struct inode *inode)
1910{
1911 loff_t result = ADDRS_PER_INODE(inode);
1912 loff_t leaf_count = ADDRS_PER_BLOCK(inode);
1913
1914 /* two direct node blocks */
1915 result += (leaf_count * 2);
1916
1917 /* two indirect node blocks */
1918 leaf_count *= NIDS_PER_BLOCK;
1919 result += (leaf_count * 2);
1920
1921 /* one double indirect node block */
1922 leaf_count *= NIDS_PER_BLOCK;
1923 result += leaf_count;
1924
1925 return result;
1926}
1927
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001928int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1929 u64 start, u64 len)
1930{
1931 struct buffer_head map_bh;
1932 sector_t start_blk, last_blk;
1933 pgoff_t next_pgofs;
1934 u64 logical = 0, phys = 0, size = 0;
1935 u32 flags = 0;
1936 int ret = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02001937 bool compr_cluster = false;
1938 unsigned int cluster_size = F2FS_I(inode)->i_cluster_size;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001939
1940 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1941 ret = f2fs_precache_extents(inode);
1942 if (ret)
1943 return ret;
1944 }
1945
Olivier Deprez157378f2022-04-04 15:47:50 +02001946 ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001947 if (ret)
1948 return ret;
1949
1950 inode_lock(inode);
1951
1952 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1953 ret = f2fs_xattr_fiemap(inode, fieinfo);
1954 goto out;
1955 }
1956
David Brazdil0f672f62019-12-10 10:32:29 +00001957 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001958 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1959 if (ret != -EAGAIN)
1960 goto out;
1961 }
1962
1963 if (logical_to_blk(inode, len) == 0)
1964 len = blk_to_logical(inode, 1);
1965
1966 start_blk = logical_to_blk(inode, start);
1967 last_blk = logical_to_blk(inode, start + len - 1);
1968
1969next:
1970 memset(&map_bh, 0, sizeof(struct buffer_head));
1971 map_bh.b_size = len;
1972
Olivier Deprez157378f2022-04-04 15:47:50 +02001973 if (compr_cluster)
1974 map_bh.b_size = blk_to_logical(inode, cluster_size - 1);
1975
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001976 ret = get_data_block(inode, start_blk, &map_bh, 0,
1977 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1978 if (ret)
1979 goto out;
1980
1981 /* HOLE */
1982 if (!buffer_mapped(&map_bh)) {
1983 start_blk = next_pgofs;
1984
1985 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
Olivier Deprez157378f2022-04-04 15:47:50 +02001986 max_inode_blocks(inode)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001987 goto prep_next;
1988
1989 flags |= FIEMAP_EXTENT_LAST;
1990 }
1991
1992 if (size) {
David Brazdil0f672f62019-12-10 10:32:29 +00001993 if (IS_ENCRYPTED(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001994 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1995
1996 ret = fiemap_fill_next_extent(fieinfo, logical,
1997 phys, size, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02001998 trace_f2fs_fiemap(inode, logical, phys, size, flags, ret);
1999 if (ret)
2000 goto out;
2001 size = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002002 }
2003
Olivier Deprez157378f2022-04-04 15:47:50 +02002004 if (start_blk > last_blk)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002005 goto out;
2006
Olivier Deprez157378f2022-04-04 15:47:50 +02002007 if (compr_cluster) {
2008 compr_cluster = false;
2009
2010
2011 logical = blk_to_logical(inode, start_blk - 1);
2012 phys = blk_to_logical(inode, map_bh.b_blocknr);
2013 size = blk_to_logical(inode, cluster_size);
2014
2015 flags |= FIEMAP_EXTENT_ENCODED;
2016
2017 start_blk += cluster_size - 1;
2018
2019 if (start_blk > last_blk)
2020 goto out;
2021
2022 goto prep_next;
2023 }
2024
2025 if (map_bh.b_blocknr == COMPRESS_ADDR) {
2026 compr_cluster = true;
2027 start_blk++;
2028 goto prep_next;
2029 }
2030
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002031 logical = blk_to_logical(inode, start_blk);
2032 phys = blk_to_logical(inode, map_bh.b_blocknr);
2033 size = map_bh.b_size;
2034 flags = 0;
2035 if (buffer_unwritten(&map_bh))
2036 flags = FIEMAP_EXTENT_UNWRITTEN;
2037
2038 start_blk += logical_to_blk(inode, size);
2039
2040prep_next:
2041 cond_resched();
2042 if (fatal_signal_pending(current))
2043 ret = -EINTR;
2044 else
2045 goto next;
2046out:
2047 if (ret == 1)
2048 ret = 0;
2049
2050 inode_unlock(inode);
2051 return ret;
2052}
2053
David Brazdil0f672f62019-12-10 10:32:29 +00002054static inline loff_t f2fs_readpage_limit(struct inode *inode)
2055{
2056 if (IS_ENABLED(CONFIG_FS_VERITY) &&
2057 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
2058 return inode->i_sb->s_maxbytes;
2059
2060 return i_size_read(inode);
2061}
2062
2063static int f2fs_read_single_page(struct inode *inode, struct page *page,
2064 unsigned nr_pages,
2065 struct f2fs_map_blocks *map,
2066 struct bio **bio_ret,
2067 sector_t *last_block_in_bio,
2068 bool is_readahead)
2069{
2070 struct bio *bio = *bio_ret;
2071 const unsigned blkbits = inode->i_blkbits;
2072 const unsigned blocksize = 1 << blkbits;
2073 sector_t block_in_file;
2074 sector_t last_block;
2075 sector_t last_block_in_file;
2076 sector_t block_nr;
2077 int ret = 0;
2078
2079 block_in_file = (sector_t)page_index(page);
2080 last_block = block_in_file + nr_pages;
2081 last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
2082 blkbits;
2083 if (last_block > last_block_in_file)
2084 last_block = last_block_in_file;
2085
2086 /* just zeroing out page which is beyond EOF */
2087 if (block_in_file >= last_block)
2088 goto zero_out;
2089 /*
2090 * Map blocks using the previous result first.
2091 */
2092 if ((map->m_flags & F2FS_MAP_MAPPED) &&
2093 block_in_file > map->m_lblk &&
2094 block_in_file < (map->m_lblk + map->m_len))
2095 goto got_it;
2096
2097 /*
2098 * Then do more f2fs_map_blocks() calls until we are
2099 * done with this page.
2100 */
2101 map->m_lblk = block_in_file;
2102 map->m_len = last_block - block_in_file;
2103
2104 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
2105 if (ret)
2106 goto out;
2107got_it:
2108 if ((map->m_flags & F2FS_MAP_MAPPED)) {
2109 block_nr = map->m_pblk + block_in_file - map->m_lblk;
2110 SetPageMappedToDisk(page);
2111
2112 if (!PageUptodate(page) && (!PageSwapCache(page) &&
2113 !cleancache_get_page(page))) {
2114 SetPageUptodate(page);
2115 goto confused;
2116 }
2117
2118 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
2119 DATA_GENERIC_ENHANCE_READ)) {
2120 ret = -EFSCORRUPTED;
2121 goto out;
2122 }
2123 } else {
2124zero_out:
2125 zero_user_segment(page, 0, PAGE_SIZE);
2126 if (f2fs_need_verity(inode, page->index) &&
2127 !fsverity_verify_page(page)) {
2128 ret = -EIO;
2129 goto out;
2130 }
2131 if (!PageUptodate(page))
2132 SetPageUptodate(page);
2133 unlock_page(page);
2134 goto out;
2135 }
2136
2137 /*
2138 * This page will go to BIO. Do we need to send this
2139 * BIO off first?
2140 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002141 if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio,
2142 *last_block_in_bio, block_nr) ||
2143 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
David Brazdil0f672f62019-12-10 10:32:29 +00002144submit_and_realloc:
2145 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2146 bio = NULL;
2147 }
2148 if (bio == NULL) {
2149 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
Olivier Deprez157378f2022-04-04 15:47:50 +02002150 is_readahead ? REQ_RAHEAD : 0, page->index,
2151 false, true);
David Brazdil0f672f62019-12-10 10:32:29 +00002152 if (IS_ERR(bio)) {
2153 ret = PTR_ERR(bio);
2154 bio = NULL;
2155 goto out;
2156 }
2157 }
2158
2159 /*
2160 * If the page is under writeback, we need to wait for
2161 * its completion to see the correct decrypted data.
2162 */
2163 f2fs_wait_on_block_writeback(inode, block_nr);
2164
2165 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2166 goto submit_and_realloc;
2167
2168 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
Olivier Deprez157378f2022-04-04 15:47:50 +02002169 f2fs_update_iostat(F2FS_I_SB(inode), FS_DATA_READ_IO, F2FS_BLKSIZE);
David Brazdil0f672f62019-12-10 10:32:29 +00002170 ClearPageError(page);
2171 *last_block_in_bio = block_nr;
2172 goto out;
2173confused:
2174 if (bio) {
2175 __submit_bio(F2FS_I_SB(inode), bio, DATA);
2176 bio = NULL;
2177 }
2178 unlock_page(page);
2179out:
2180 *bio_ret = bio;
2181 return ret;
2182}
2183
Olivier Deprez157378f2022-04-04 15:47:50 +02002184#ifdef CONFIG_F2FS_FS_COMPRESSION
2185int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
2186 unsigned nr_pages, sector_t *last_block_in_bio,
2187 bool is_readahead, bool for_write)
2188{
2189 struct dnode_of_data dn;
2190 struct inode *inode = cc->inode;
2191 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2192 struct bio *bio = *bio_ret;
2193 unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size;
2194 sector_t last_block_in_file;
2195 const unsigned blkbits = inode->i_blkbits;
2196 const unsigned blocksize = 1 << blkbits;
2197 struct decompress_io_ctx *dic = NULL;
2198 struct bio_post_read_ctx *ctx;
2199 bool for_verity = false;
2200 int i;
2201 int ret = 0;
2202
2203 f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc));
2204
2205 last_block_in_file = (f2fs_readpage_limit(inode) +
2206 blocksize - 1) >> blkbits;
2207
2208 /* get rid of pages beyond EOF */
2209 for (i = 0; i < cc->cluster_size; i++) {
2210 struct page *page = cc->rpages[i];
2211
2212 if (!page)
2213 continue;
2214 if ((sector_t)page->index >= last_block_in_file) {
2215 zero_user_segment(page, 0, PAGE_SIZE);
2216 if (!PageUptodate(page))
2217 SetPageUptodate(page);
2218 } else if (!PageUptodate(page)) {
2219 continue;
2220 }
2221 unlock_page(page);
2222 if (for_write)
2223 put_page(page);
2224 cc->rpages[i] = NULL;
2225 cc->nr_rpages--;
2226 }
2227
2228 /* we are done since all pages are beyond EOF */
2229 if (f2fs_cluster_is_empty(cc))
2230 goto out;
2231
2232 set_new_dnode(&dn, inode, NULL, NULL, 0);
2233 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
2234 if (ret)
2235 goto out;
2236
2237 f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR);
2238
2239 for (i = 1; i < cc->cluster_size; i++) {
2240 block_t blkaddr;
2241
2242 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2243 dn.ofs_in_node + i);
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
2255 /* nothing to decompress */
2256 if (cc->nr_cpages == 0) {
2257 ret = 0;
2258 goto out_put_dnode;
2259 }
2260
2261 dic = f2fs_alloc_dic(cc);
2262 if (IS_ERR(dic)) {
2263 ret = PTR_ERR(dic);
2264 goto out_put_dnode;
2265 }
2266
2267 /*
2268 * It's possible to enable fsverity on the fly when handling a cluster,
2269 * which requires complicated error handling. Instead of adding more
2270 * complexity, let's give a rule where end_io post-processes fsverity
2271 * per cluster. In order to do that, we need to submit bio, if previous
2272 * bio sets a different post-process policy.
2273 */
2274 if (fsverity_active(cc->inode)) {
2275 atomic_set(&dic->verity_pages, cc->nr_cpages);
2276 for_verity = true;
2277
2278 if (bio) {
2279 ctx = bio->bi_private;
2280 if (!(ctx->enabled_steps & (1 << STEP_VERITY))) {
2281 __submit_bio(sbi, bio, DATA);
2282 bio = NULL;
2283 }
2284 }
2285 }
2286
2287 for (i = 0; i < dic->nr_cpages; i++) {
2288 struct page *page = dic->cpages[i];
2289 block_t blkaddr;
2290
2291 blkaddr = data_blkaddr(dn.inode, dn.node_page,
2292 dn.ofs_in_node + i + 1);
2293
2294 if (bio && (!page_is_mergeable(sbi, bio,
2295 *last_block_in_bio, blkaddr) ||
2296 !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) {
2297submit_and_realloc:
2298 __submit_bio(sbi, bio, DATA);
2299 bio = NULL;
2300 }
2301
2302 if (!bio) {
2303 bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages,
2304 is_readahead ? REQ_RAHEAD : 0,
2305 page->index, for_write, for_verity);
2306 if (IS_ERR(bio)) {
2307 unsigned int remained = dic->nr_cpages - i;
2308 bool release = false;
2309
2310 ret = PTR_ERR(bio);
2311 dic->failed = true;
2312
2313 if (for_verity) {
2314 if (!atomic_sub_return(remained,
2315 &dic->verity_pages))
2316 release = true;
2317 } else {
2318 if (!atomic_sub_return(remained,
2319 &dic->pending_pages))
2320 release = true;
2321 }
2322
2323 if (release) {
2324 f2fs_decompress_end_io(dic->rpages,
2325 cc->cluster_size, true,
2326 false);
2327 f2fs_free_dic(dic);
2328 }
2329
2330 f2fs_put_dnode(&dn);
2331 *bio_ret = NULL;
2332 return ret;
2333 }
2334 }
2335
2336 f2fs_wait_on_block_writeback(inode, blkaddr);
2337
2338 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
2339 goto submit_and_realloc;
2340
2341 /* tag STEP_DECOMPRESS to handle IO in wq */
2342 ctx = bio->bi_private;
2343 if (!(ctx->enabled_steps & (1 << STEP_DECOMPRESS)))
2344 ctx->enabled_steps |= 1 << STEP_DECOMPRESS;
2345
2346 inc_page_count(sbi, F2FS_RD_DATA);
2347 f2fs_update_iostat(sbi, FS_DATA_READ_IO, F2FS_BLKSIZE);
2348 f2fs_update_iostat(sbi, FS_CDATA_READ_IO, F2FS_BLKSIZE);
2349 ClearPageError(page);
2350 *last_block_in_bio = blkaddr;
2351 }
2352
2353 f2fs_put_dnode(&dn);
2354
2355 *bio_ret = bio;
2356 return 0;
2357
2358out_put_dnode:
2359 f2fs_put_dnode(&dn);
2360out:
2361 f2fs_decompress_end_io(cc->rpages, cc->cluster_size, true, false);
2362 *bio_ret = bio;
2363 return ret;
2364}
2365#endif
2366
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002367/*
2368 * This function was originally taken from fs/mpage.c, and customized for f2fs.
2369 * Major change was from block_size == page_size in f2fs by default.
2370 *
2371 * Note that the aops->readpages() function is ONLY used for read-ahead. If
2372 * this function ever deviates from doing just read-ahead, it should either
2373 * use ->readpage() or do the necessary surgery to decouple ->readpages()
2374 * from read-ahead.
2375 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002376static int f2fs_mpage_readpages(struct inode *inode,
2377 struct readahead_control *rac, struct page *page)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002378{
2379 struct bio *bio = NULL;
2380 sector_t last_block_in_bio = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002381 struct f2fs_map_blocks map;
Olivier Deprez157378f2022-04-04 15:47:50 +02002382#ifdef CONFIG_F2FS_FS_COMPRESSION
2383 struct compress_ctx cc = {
2384 .inode = inode,
2385 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2386 .cluster_size = F2FS_I(inode)->i_cluster_size,
2387 .cluster_idx = NULL_CLUSTER,
2388 .rpages = NULL,
2389 .cpages = NULL,
2390 .nr_rpages = 0,
2391 .nr_cpages = 0,
2392 };
2393#endif
2394 unsigned nr_pages = rac ? readahead_count(rac) : 1;
2395 unsigned max_nr_pages = nr_pages;
David Brazdil0f672f62019-12-10 10:32:29 +00002396 int ret = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02002397 bool drop_ra = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002398
2399 map.m_pblk = 0;
2400 map.m_lblk = 0;
2401 map.m_len = 0;
2402 map.m_flags = 0;
2403 map.m_next_pgofs = NULL;
2404 map.m_next_extent = NULL;
2405 map.m_seg_type = NO_CHECK_TYPE;
David Brazdil0f672f62019-12-10 10:32:29 +00002406 map.m_may_create = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002407
Olivier Deprez157378f2022-04-04 15:47:50 +02002408 /*
2409 * Two readahead threads for same address range can cause race condition
2410 * which fragments sequential read IOs. So let's avoid each other.
2411 */
2412 if (rac && readahead_count(rac)) {
2413 if (READ_ONCE(F2FS_I(inode)->ra_offset) == readahead_index(rac))
2414 drop_ra = true;
2415 else
2416 WRITE_ONCE(F2FS_I(inode)->ra_offset,
2417 readahead_index(rac));
2418 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002419
Olivier Deprez157378f2022-04-04 15:47:50 +02002420 for (; nr_pages; nr_pages--) {
2421 if (rac) {
2422 page = readahead_page(rac);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002423 prefetchw(&page->flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02002424 if (drop_ra) {
2425 f2fs_put_page(page, 1);
2426 continue;
2427 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002428 }
2429
Olivier Deprez157378f2022-04-04 15:47:50 +02002430#ifdef CONFIG_F2FS_FS_COMPRESSION
2431 if (f2fs_compressed_file(inode)) {
2432 /* there are remained comressed pages, submit them */
2433 if (!f2fs_cluster_can_merge_page(&cc, page->index)) {
2434 ret = f2fs_read_multi_pages(&cc, &bio,
2435 max_nr_pages,
2436 &last_block_in_bio,
2437 rac != NULL, false);
2438 f2fs_destroy_compress_ctx(&cc, false);
2439 if (ret)
2440 goto set_error_page;
2441 }
2442 ret = f2fs_is_compressed_cluster(inode, page->index);
2443 if (ret < 0)
2444 goto set_error_page;
2445 else if (!ret)
2446 goto read_single_page;
2447
2448 ret = f2fs_init_compress_ctx(&cc);
2449 if (ret)
2450 goto set_error_page;
2451
2452 f2fs_compress_ctx_add_page(&cc, page);
2453
2454 goto next_page;
2455 }
2456read_single_page:
2457#endif
2458
2459 ret = f2fs_read_single_page(inode, page, max_nr_pages, &map,
2460 &bio, &last_block_in_bio, rac);
David Brazdil0f672f62019-12-10 10:32:29 +00002461 if (ret) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002462#ifdef CONFIG_F2FS_FS_COMPRESSION
2463set_error_page:
2464#endif
David Brazdil0f672f62019-12-10 10:32:29 +00002465 SetPageError(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002466 zero_user_segment(page, 0, PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002467 unlock_page(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002468 }
Olivier Deprez157378f2022-04-04 15:47:50 +02002469#ifdef CONFIG_F2FS_FS_COMPRESSION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002470next_page:
Olivier Deprez157378f2022-04-04 15:47:50 +02002471#endif
2472 if (rac)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002473 put_page(page);
Olivier Deprez157378f2022-04-04 15:47:50 +02002474
2475#ifdef CONFIG_F2FS_FS_COMPRESSION
2476 if (f2fs_compressed_file(inode)) {
2477 /* last page */
2478 if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) {
2479 ret = f2fs_read_multi_pages(&cc, &bio,
2480 max_nr_pages,
2481 &last_block_in_bio,
2482 rac != NULL, false);
2483 f2fs_destroy_compress_ctx(&cc, false);
2484 }
2485 }
2486#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002487 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002488 if (bio)
2489 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Olivier Deprez157378f2022-04-04 15:47:50 +02002490
2491 if (rac && readahead_count(rac) && !drop_ra)
2492 WRITE_ONCE(F2FS_I(inode)->ra_offset, -1);
2493 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002494}
2495
2496static int f2fs_read_data_page(struct file *file, struct page *page)
2497{
David Brazdil0f672f62019-12-10 10:32:29 +00002498 struct inode *inode = page_file_mapping(page)->host;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002499 int ret = -EAGAIN;
2500
2501 trace_f2fs_readpage(page, DATA);
2502
Olivier Deprez157378f2022-04-04 15:47:50 +02002503 if (!f2fs_is_compress_backend_ready(inode)) {
2504 unlock_page(page);
2505 return -EOPNOTSUPP;
2506 }
2507
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002508 /* If the file has inline data, try to read it directly */
2509 if (f2fs_has_inline_data(inode))
2510 ret = f2fs_read_inline_data(inode, page);
2511 if (ret == -EAGAIN)
Olivier Deprez157378f2022-04-04 15:47:50 +02002512 ret = f2fs_mpage_readpages(inode, NULL, page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002513 return ret;
2514}
2515
Olivier Deprez157378f2022-04-04 15:47:50 +02002516static void f2fs_readahead(struct readahead_control *rac)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002517{
Olivier Deprez157378f2022-04-04 15:47:50 +02002518 struct inode *inode = rac->mapping->host;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002519
Olivier Deprez157378f2022-04-04 15:47:50 +02002520 trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac));
2521
2522 if (!f2fs_is_compress_backend_ready(inode))
2523 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002524
2525 /* If the file has inline data, skip readpages */
2526 if (f2fs_has_inline_data(inode))
Olivier Deprez157378f2022-04-04 15:47:50 +02002527 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002528
Olivier Deprez157378f2022-04-04 15:47:50 +02002529 f2fs_mpage_readpages(inode, rac, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002530}
2531
Olivier Deprez157378f2022-04-04 15:47:50 +02002532int f2fs_encrypt_one_page(struct f2fs_io_info *fio)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002533{
2534 struct inode *inode = fio->page->mapping->host;
Olivier Deprez157378f2022-04-04 15:47:50 +02002535 struct page *mpage, *page;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002536 gfp_t gfp_flags = GFP_NOFS;
2537
2538 if (!f2fs_encrypted_file(inode))
2539 return 0;
2540
Olivier Deprez157378f2022-04-04 15:47:50 +02002541 page = fio->compressed_page ? fio->compressed_page : fio->page;
2542
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002543 /* wait for GCed page writeback via META_MAPPING */
David Brazdil0f672f62019-12-10 10:32:29 +00002544 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002545
Olivier Deprez157378f2022-04-04 15:47:50 +02002546 if (fscrypt_inode_uses_inline_crypto(inode))
2547 return 0;
2548
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002549retry_encrypt:
Olivier Deprez157378f2022-04-04 15:47:50 +02002550 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page,
2551 PAGE_SIZE, 0, gfp_flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002552 if (IS_ERR(fio->encrypted_page)) {
2553 /* flush pending IOs and wait for a while in the ENOMEM case */
2554 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
2555 f2fs_flush_merged_writes(fio->sbi);
Olivier Deprez157378f2022-04-04 15:47:50 +02002556 congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002557 gfp_flags |= __GFP_NOFAIL;
2558 goto retry_encrypt;
2559 }
2560 return PTR_ERR(fio->encrypted_page);
2561 }
2562
2563 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
2564 if (mpage) {
2565 if (PageUptodate(mpage))
2566 memcpy(page_address(mpage),
2567 page_address(fio->encrypted_page), PAGE_SIZE);
2568 f2fs_put_page(mpage, 1);
2569 }
2570 return 0;
2571}
2572
2573static inline bool check_inplace_update_policy(struct inode *inode,
2574 struct f2fs_io_info *fio)
2575{
2576 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2577 unsigned int policy = SM_I(sbi)->ipu_policy;
2578
2579 if (policy & (0x1 << F2FS_IPU_FORCE))
2580 return true;
2581 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
2582 return true;
2583 if (policy & (0x1 << F2FS_IPU_UTIL) &&
2584 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2585 return true;
2586 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
2587 utilization(sbi) > SM_I(sbi)->min_ipu_util)
2588 return true;
2589
2590 /*
2591 * IPU for rewrite async pages
2592 */
2593 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
2594 fio && fio->op == REQ_OP_WRITE &&
2595 !(fio->op_flags & REQ_SYNC) &&
David Brazdil0f672f62019-12-10 10:32:29 +00002596 !IS_ENCRYPTED(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002597 return true;
2598
2599 /* this is only set during fdatasync */
2600 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
2601 is_inode_flag_set(inode, FI_NEED_IPU))
2602 return true;
2603
David Brazdil0f672f62019-12-10 10:32:29 +00002604 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2605 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2606 return true;
2607
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002608 return false;
2609}
2610
2611bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
2612{
2613 if (f2fs_is_pinned_file(inode))
2614 return true;
2615
2616 /* if this is cold file, we should overwrite to avoid fragmentation */
2617 if (file_is_cold(inode))
2618 return true;
2619
2620 return check_inplace_update_policy(inode, fio);
2621}
2622
2623bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
2624{
2625 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2626
Olivier Deprez157378f2022-04-04 15:47:50 +02002627 if (f2fs_lfs_mode(sbi))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002628 return true;
2629 if (S_ISDIR(inode->i_mode))
2630 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00002631 if (IS_NOQUOTA(inode))
2632 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002633 if (f2fs_is_atomic_file(inode))
2634 return true;
2635 if (fio) {
2636 if (is_cold_data(fio->page))
2637 return true;
2638 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
2639 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00002640 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
2641 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
2642 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002643 }
2644 return false;
2645}
2646
2647static inline bool need_inplace_update(struct f2fs_io_info *fio)
2648{
2649 struct inode *inode = fio->page->mapping->host;
2650
2651 if (f2fs_should_update_outplace(inode, fio))
2652 return false;
2653
2654 return f2fs_should_update_inplace(inode, fio);
2655}
2656
2657int f2fs_do_write_data_page(struct f2fs_io_info *fio)
2658{
2659 struct page *page = fio->page;
2660 struct inode *inode = page->mapping->host;
2661 struct dnode_of_data dn;
2662 struct extent_info ei = {0,0,0};
2663 struct node_info ni;
2664 bool ipu_force = false;
2665 int err = 0;
2666
2667 set_new_dnode(&dn, inode, NULL, NULL, 0);
2668 if (need_inplace_update(fio) &&
2669 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
2670 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
2671
2672 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
David Brazdil0f672f62019-12-10 10:32:29 +00002673 DATA_GENERIC_ENHANCE))
2674 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002675
2676 ipu_force = true;
2677 fio->need_lock = LOCK_DONE;
2678 goto got_it;
2679 }
2680
2681 /* Deadlock due to between page->lock and f2fs_lock_op */
2682 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2683 return -EAGAIN;
2684
2685 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2686 if (err)
2687 goto out;
2688
2689 fio->old_blkaddr = dn.data_blkaddr;
2690
2691 /* This page is already truncated */
2692 if (fio->old_blkaddr == NULL_ADDR) {
2693 ClearPageUptodate(page);
David Brazdil0f672f62019-12-10 10:32:29 +00002694 clear_cold_data(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002695 goto out_writepage;
2696 }
2697got_it:
2698 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2699 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
David Brazdil0f672f62019-12-10 10:32:29 +00002700 DATA_GENERIC_ENHANCE)) {
2701 err = -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002702 goto out_writepage;
2703 }
2704 /*
2705 * If current allocation needs SSR,
2706 * it had better in-place writes for updated data.
2707 */
David Brazdil0f672f62019-12-10 10:32:29 +00002708 if (ipu_force ||
2709 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002710 need_inplace_update(fio))) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002711 err = f2fs_encrypt_one_page(fio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002712 if (err)
2713 goto out_writepage;
2714
2715 set_page_writeback(page);
2716 ClearPageError(page);
2717 f2fs_put_dnode(&dn);
2718 if (fio->need_lock == LOCK_REQ)
2719 f2fs_unlock_op(fio->sbi);
2720 err = f2fs_inplace_write_data(fio);
David Brazdil0f672f62019-12-10 10:32:29 +00002721 if (err) {
Olivier Deprez157378f2022-04-04 15:47:50 +02002722 if (fscrypt_inode_uses_fs_layer_crypto(inode))
David Brazdil0f672f62019-12-10 10:32:29 +00002723 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2724 if (PageWriteback(page))
2725 end_page_writeback(page);
2726 } else {
2727 set_inode_flag(inode, FI_UPDATE_WRITE);
2728 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002729 trace_f2fs_do_write_data_page(fio->page, IPU);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002730 return err;
2731 }
2732
2733 if (fio->need_lock == LOCK_RETRY) {
2734 if (!f2fs_trylock_op(fio->sbi)) {
2735 err = -EAGAIN;
2736 goto out_writepage;
2737 }
2738 fio->need_lock = LOCK_REQ;
2739 }
2740
2741 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2742 if (err)
2743 goto out_writepage;
2744
2745 fio->version = ni.version;
2746
Olivier Deprez157378f2022-04-04 15:47:50 +02002747 err = f2fs_encrypt_one_page(fio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002748 if (err)
2749 goto out_writepage;
2750
2751 set_page_writeback(page);
2752 ClearPageError(page);
2753
Olivier Deprez157378f2022-04-04 15:47:50 +02002754 if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR)
2755 f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false);
2756
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002757 /* LFS mode write path */
2758 f2fs_outplace_write_data(&dn, fio);
2759 trace_f2fs_do_write_data_page(page, OPU);
2760 set_inode_flag(inode, FI_APPEND_WRITE);
2761 if (page->index == 0)
2762 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2763out_writepage:
2764 f2fs_put_dnode(&dn);
2765out:
2766 if (fio->need_lock == LOCK_REQ)
2767 f2fs_unlock_op(fio->sbi);
2768 return err;
2769}
2770
Olivier Deprez157378f2022-04-04 15:47:50 +02002771int f2fs_write_single_data_page(struct page *page, int *submitted,
David Brazdil0f672f62019-12-10 10:32:29 +00002772 struct bio **bio,
2773 sector_t *last_block,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002774 struct writeback_control *wbc,
Olivier Deprez157378f2022-04-04 15:47:50 +02002775 enum iostat_type io_type,
2776 int compr_blocks,
2777 bool allow_balance)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002778{
2779 struct inode *inode = page->mapping->host;
2780 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2781 loff_t i_size = i_size_read(inode);
Olivier Deprez157378f2022-04-04 15:47:50 +02002782 const pgoff_t end_index = ((unsigned long long)i_size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002783 >> PAGE_SHIFT;
Olivier Deprez0e641232021-09-23 10:07:05 +02002784 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002785 unsigned offset = 0;
2786 bool need_balance_fs = false;
2787 int err = 0;
2788 struct f2fs_io_info fio = {
2789 .sbi = sbi,
2790 .ino = inode->i_ino,
2791 .type = DATA,
2792 .op = REQ_OP_WRITE,
2793 .op_flags = wbc_to_write_flags(wbc),
2794 .old_blkaddr = NULL_ADDR,
2795 .page = page,
2796 .encrypted_page = NULL,
2797 .submitted = false,
Olivier Deprez157378f2022-04-04 15:47:50 +02002798 .compr_blocks = compr_blocks,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002799 .need_lock = LOCK_RETRY,
2800 .io_type = io_type,
2801 .io_wbc = wbc,
David Brazdil0f672f62019-12-10 10:32:29 +00002802 .bio = bio,
2803 .last_block = last_block,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002804 };
2805
2806 trace_f2fs_writepage(page, DATA);
2807
2808 /* we should bypass data pages to proceed the kworkder jobs */
2809 if (unlikely(f2fs_cp_error(sbi))) {
2810 mapping_set_error(page->mapping, -EIO);
2811 /*
2812 * don't drop any dirty dentry pages for keeping lastest
2813 * directory structure.
2814 */
2815 if (S_ISDIR(inode->i_mode))
2816 goto redirty_out;
2817 goto out;
2818 }
2819
2820 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2821 goto redirty_out;
2822
Olivier Deprez157378f2022-04-04 15:47:50 +02002823 if (page->index < end_index ||
2824 f2fs_verity_in_progress(inode) ||
2825 compr_blocks)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002826 goto write;
2827
2828 /*
2829 * If the offset is out-of-range of file size,
2830 * this page does not have to be written to disk.
2831 */
2832 offset = i_size & (PAGE_SIZE - 1);
2833 if ((page->index >= end_index + 1) || !offset)
2834 goto out;
2835
2836 zero_user_segment(page, offset, PAGE_SIZE);
2837write:
2838 if (f2fs_is_drop_cache(inode))
2839 goto out;
2840 /* we should not write 0'th page having journal header */
2841 if (f2fs_is_volatile_file(inode) && (!page->index ||
2842 (!wbc->for_reclaim &&
2843 f2fs_available_free_memory(sbi, BASE_CHECK))))
2844 goto redirty_out;
2845
Olivier Deprez157378f2022-04-04 15:47:50 +02002846 /* Dentry/quota blocks are controlled by checkpoint */
2847 if (S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) {
2848 /*
2849 * We need to wait for node_write to avoid block allocation during
2850 * checkpoint. This can only happen to quota writes which can cause
2851 * the below discard race condition.
2852 */
2853 if (IS_NOQUOTA(inode))
2854 down_read(&sbi->node_write);
2855
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002856 fio.need_lock = LOCK_DONE;
2857 err = f2fs_do_write_data_page(&fio);
Olivier Deprez157378f2022-04-04 15:47:50 +02002858
2859 if (IS_NOQUOTA(inode))
2860 up_read(&sbi->node_write);
2861
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002862 goto done;
2863 }
2864
2865 if (!wbc->for_reclaim)
2866 need_balance_fs = true;
2867 else if (has_not_enough_free_secs(sbi, 0, 0))
2868 goto redirty_out;
2869 else
2870 set_inode_flag(inode, FI_HOT_DATA);
2871
2872 err = -EAGAIN;
2873 if (f2fs_has_inline_data(inode)) {
2874 err = f2fs_write_inline_data(inode, page);
2875 if (!err)
2876 goto out;
2877 }
2878
2879 if (err == -EAGAIN) {
2880 err = f2fs_do_write_data_page(&fio);
2881 if (err == -EAGAIN) {
2882 fio.need_lock = LOCK_REQ;
2883 err = f2fs_do_write_data_page(&fio);
2884 }
2885 }
2886
2887 if (err) {
2888 file_set_keep_isize(inode);
2889 } else {
Olivier Deprez157378f2022-04-04 15:47:50 +02002890 spin_lock(&F2FS_I(inode)->i_size_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002891 if (F2FS_I(inode)->last_disk_size < psize)
2892 F2FS_I(inode)->last_disk_size = psize;
Olivier Deprez157378f2022-04-04 15:47:50 +02002893 spin_unlock(&F2FS_I(inode)->i_size_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002894 }
2895
2896done:
2897 if (err && err != -ENOENT)
2898 goto redirty_out;
2899
2900out:
2901 inode_dec_dirty_pages(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00002902 if (err) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002903 ClearPageUptodate(page);
David Brazdil0f672f62019-12-10 10:32:29 +00002904 clear_cold_data(page);
2905 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002906
2907 if (wbc->for_reclaim) {
David Brazdil0f672f62019-12-10 10:32:29 +00002908 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002909 clear_inode_flag(inode, FI_HOT_DATA);
2910 f2fs_remove_dirty_inode(inode);
2911 submitted = NULL;
2912 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002913 unlock_page(page);
David Brazdil0f672f62019-12-10 10:32:29 +00002914 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02002915 !F2FS_I(inode)->cp_task && allow_balance)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002916 f2fs_balance_fs(sbi, need_balance_fs);
2917
2918 if (unlikely(f2fs_cp_error(sbi))) {
2919 f2fs_submit_merged_write(sbi, DATA);
Olivier Deprez157378f2022-04-04 15:47:50 +02002920 f2fs_submit_merged_ipu_write(sbi, bio, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002921 submitted = NULL;
2922 }
2923
2924 if (submitted)
Olivier Deprez157378f2022-04-04 15:47:50 +02002925 *submitted = fio.submitted ? 1 : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002926
2927 return 0;
2928
2929redirty_out:
2930 redirty_page_for_writepage(wbc, page);
2931 /*
2932 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2933 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2934 * file_write_and_wait_range() will see EIO error, which is critical
2935 * to return value of fsync() followed by atomic_write failure to user.
2936 */
2937 if (!err || wbc->for_reclaim)
2938 return AOP_WRITEPAGE_ACTIVATE;
2939 unlock_page(page);
2940 return err;
2941}
2942
2943static int f2fs_write_data_page(struct page *page,
2944 struct writeback_control *wbc)
2945{
Olivier Deprez157378f2022-04-04 15:47:50 +02002946#ifdef CONFIG_F2FS_FS_COMPRESSION
2947 struct inode *inode = page->mapping->host;
2948
2949 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2950 goto out;
2951
2952 if (f2fs_compressed_file(inode)) {
2953 if (f2fs_is_compressed_cluster(inode, page->index)) {
2954 redirty_page_for_writepage(wbc, page);
2955 return AOP_WRITEPAGE_ACTIVATE;
2956 }
2957 }
2958out:
2959#endif
2960
2961 return f2fs_write_single_data_page(page, NULL, NULL, NULL,
2962 wbc, FS_DATA_IO, 0, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002963}
2964
2965/*
2966 * This function was copied from write_cche_pages from mm/page-writeback.c.
2967 * The major change is making write step of cold data page separately from
2968 * warm/hot data page.
2969 */
2970static int f2fs_write_cache_pages(struct address_space *mapping,
2971 struct writeback_control *wbc,
2972 enum iostat_type io_type)
2973{
2974 int ret = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02002975 int done = 0, retry = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002976 struct pagevec pvec;
2977 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
David Brazdil0f672f62019-12-10 10:32:29 +00002978 struct bio *bio = NULL;
2979 sector_t last_block;
Olivier Deprez157378f2022-04-04 15:47:50 +02002980#ifdef CONFIG_F2FS_FS_COMPRESSION
2981 struct inode *inode = mapping->host;
2982 struct compress_ctx cc = {
2983 .inode = inode,
2984 .log_cluster_size = F2FS_I(inode)->i_log_cluster_size,
2985 .cluster_size = F2FS_I(inode)->i_cluster_size,
2986 .cluster_idx = NULL_CLUSTER,
2987 .rpages = NULL,
2988 .nr_rpages = 0,
2989 .cpages = NULL,
2990 .rbuf = NULL,
2991 .cbuf = NULL,
2992 .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size,
2993 .private = NULL,
2994 };
2995#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002996 int nr_pages;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002997 pgoff_t index;
2998 pgoff_t end; /* Inclusive */
2999 pgoff_t done_index;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003000 int range_whole = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00003001 xa_mark_t tag;
3002 int nwritten = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02003003 int submitted = 0;
3004 int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003005
3006 pagevec_init(&pvec);
3007
3008 if (get_dirty_pages(mapping->host) <=
3009 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
3010 set_inode_flag(mapping->host, FI_HOT_DATA);
3011 else
3012 clear_inode_flag(mapping->host, FI_HOT_DATA);
3013
3014 if (wbc->range_cyclic) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003015 index = mapping->writeback_index; /* prev offset */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003016 end = -1;
3017 } else {
3018 index = wbc->range_start >> PAGE_SHIFT;
3019 end = wbc->range_end >> PAGE_SHIFT;
3020 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
3021 range_whole = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003022 }
3023 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
3024 tag = PAGECACHE_TAG_TOWRITE;
3025 else
3026 tag = PAGECACHE_TAG_DIRTY;
3027retry:
Olivier Deprez157378f2022-04-04 15:47:50 +02003028 retry = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003029 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
3030 tag_pages_for_writeback(mapping, index, end);
3031 done_index = index;
Olivier Deprez157378f2022-04-04 15:47:50 +02003032 while (!done && !retry && (index <= end)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003033 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
3034 tag);
3035 if (nr_pages == 0)
3036 break;
3037
3038 for (i = 0; i < nr_pages; i++) {
3039 struct page *page = pvec.pages[i];
Olivier Deprez157378f2022-04-04 15:47:50 +02003040 bool need_readd;
3041readd:
3042 need_readd = false;
3043#ifdef CONFIG_F2FS_FS_COMPRESSION
3044 if (f2fs_compressed_file(inode)) {
3045 ret = f2fs_init_compress_ctx(&cc);
3046 if (ret) {
3047 done = 1;
3048 break;
3049 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003050
Olivier Deprez157378f2022-04-04 15:47:50 +02003051 if (!f2fs_cluster_can_merge_page(&cc,
3052 page->index)) {
3053 ret = f2fs_write_multi_pages(&cc,
3054 &submitted, wbc, io_type);
3055 if (!ret)
3056 need_readd = true;
3057 goto result;
3058 }
3059
3060 if (unlikely(f2fs_cp_error(sbi)))
3061 goto lock_page;
3062
3063 if (f2fs_cluster_is_empty(&cc)) {
3064 void *fsdata = NULL;
3065 struct page *pagep;
3066 int ret2;
3067
3068 ret2 = f2fs_prepare_compress_overwrite(
3069 inode, &pagep,
3070 page->index, &fsdata);
3071 if (ret2 < 0) {
3072 ret = ret2;
3073 done = 1;
3074 break;
3075 } else if (ret2 &&
3076 !f2fs_compress_write_end(inode,
3077 fsdata, page->index,
3078 1)) {
3079 retry = 1;
3080 break;
3081 }
3082 } else {
3083 goto lock_page;
3084 }
3085 }
3086#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003087 /* give a priority to WB_SYNC threads */
3088 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
3089 wbc->sync_mode == WB_SYNC_NONE) {
3090 done = 1;
3091 break;
3092 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003093#ifdef CONFIG_F2FS_FS_COMPRESSION
3094lock_page:
3095#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003096 done_index = page->index;
3097retry_write:
3098 lock_page(page);
3099
3100 if (unlikely(page->mapping != mapping)) {
3101continue_unlock:
3102 unlock_page(page);
3103 continue;
3104 }
3105
3106 if (!PageDirty(page)) {
3107 /* someone wrote it for us */
3108 goto continue_unlock;
3109 }
3110
3111 if (PageWriteback(page)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003112 if (wbc->sync_mode != WB_SYNC_NONE)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003113 f2fs_wait_on_page_writeback(page,
David Brazdil0f672f62019-12-10 10:32:29 +00003114 DATA, true, true);
Olivier Deprez157378f2022-04-04 15:47:50 +02003115 else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003116 goto continue_unlock;
3117 }
3118
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003119 if (!clear_page_dirty_for_io(page))
3120 goto continue_unlock;
3121
Olivier Deprez157378f2022-04-04 15:47:50 +02003122#ifdef CONFIG_F2FS_FS_COMPRESSION
3123 if (f2fs_compressed_file(inode)) {
3124 get_page(page);
3125 f2fs_compress_ctx_add_page(&cc, page);
3126 continue;
3127 }
3128#endif
3129 ret = f2fs_write_single_data_page(page, &submitted,
3130 &bio, &last_block, wbc, io_type,
3131 0, true);
3132 if (ret == AOP_WRITEPAGE_ACTIVATE)
3133 unlock_page(page);
3134#ifdef CONFIG_F2FS_FS_COMPRESSION
3135result:
3136#endif
3137 nwritten += submitted;
3138 wbc->nr_to_write -= submitted;
3139
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003140 if (unlikely(ret)) {
3141 /*
3142 * keep nr_to_write, since vfs uses this to
3143 * get # of written pages.
3144 */
3145 if (ret == AOP_WRITEPAGE_ACTIVATE) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003146 ret = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02003147 goto next;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003148 } else if (ret == -EAGAIN) {
3149 ret = 0;
3150 if (wbc->sync_mode == WB_SYNC_ALL) {
3151 cond_resched();
3152 congestion_wait(BLK_RW_ASYNC,
Olivier Deprez157378f2022-04-04 15:47:50 +02003153 DEFAULT_IO_TIMEOUT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003154 goto retry_write;
3155 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003156 goto next;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003157 }
3158 done_index = page->index + 1;
3159 done = 1;
3160 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003161 }
3162
Olivier Deprez157378f2022-04-04 15:47:50 +02003163 if (wbc->nr_to_write <= 0 &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003164 wbc->sync_mode == WB_SYNC_NONE) {
3165 done = 1;
3166 break;
3167 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003168next:
3169 if (need_readd)
3170 goto readd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003171 }
3172 pagevec_release(&pvec);
3173 cond_resched();
3174 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003175#ifdef CONFIG_F2FS_FS_COMPRESSION
3176 /* flush remained pages in compress cluster */
3177 if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) {
3178 ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type);
3179 nwritten += submitted;
3180 wbc->nr_to_write -= submitted;
3181 if (ret) {
3182 done = 1;
3183 retry = 0;
3184 }
3185 }
3186 if (f2fs_compressed_file(inode))
3187 f2fs_destroy_compress_ctx(&cc, false);
3188#endif
3189 if (retry) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003190 index = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02003191 end = -1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003192 goto retry;
3193 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003194 if (wbc->range_cyclic && !done)
3195 done_index = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003196 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
3197 mapping->writeback_index = done_index;
3198
David Brazdil0f672f62019-12-10 10:32:29 +00003199 if (nwritten)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003200 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
David Brazdil0f672f62019-12-10 10:32:29 +00003201 NULL, 0, DATA);
3202 /* submit cached bio of IPU write */
3203 if (bio)
Olivier Deprez157378f2022-04-04 15:47:50 +02003204 f2fs_submit_merged_ipu_write(sbi, &bio, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003205
3206 return ret;
3207}
3208
3209static inline bool __should_serialize_io(struct inode *inode,
3210 struct writeback_control *wbc)
3211{
Olivier Deprez157378f2022-04-04 15:47:50 +02003212 /* to avoid deadlock in path of data flush */
3213 if (F2FS_I(inode)->cp_task)
3214 return false;
3215
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003216 if (!S_ISREG(inode->i_mode))
3217 return false;
David Brazdil0f672f62019-12-10 10:32:29 +00003218 if (IS_NOQUOTA(inode))
3219 return false;
Olivier Deprez157378f2022-04-04 15:47:50 +02003220
3221 if (f2fs_compressed_file(inode))
3222 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003223 if (wbc->sync_mode != WB_SYNC_ALL)
3224 return true;
3225 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
3226 return true;
3227 return false;
3228}
3229
3230static int __f2fs_write_data_pages(struct address_space *mapping,
3231 struct writeback_control *wbc,
3232 enum iostat_type io_type)
3233{
3234 struct inode *inode = mapping->host;
3235 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3236 struct blk_plug plug;
3237 int ret;
3238 bool locked = false;
3239
3240 /* deal with chardevs and other special file */
3241 if (!mapping->a_ops->writepage)
3242 return 0;
3243
3244 /* skip writing if there is no dirty page in this inode */
3245 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
3246 return 0;
3247
3248 /* during POR, we don't need to trigger writepage at all. */
3249 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
3250 goto skip_write;
3251
David Brazdil0f672f62019-12-10 10:32:29 +00003252 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
3253 wbc->sync_mode == WB_SYNC_NONE &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003254 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
3255 f2fs_available_free_memory(sbi, DIRTY_DENTS))
3256 goto skip_write;
3257
3258 /* skip writing during file defragment */
3259 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
3260 goto skip_write;
3261
3262 trace_f2fs_writepages(mapping->host, wbc, DATA);
3263
3264 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
3265 if (wbc->sync_mode == WB_SYNC_ALL)
3266 atomic_inc(&sbi->wb_sync_req[DATA]);
3267 else if (atomic_read(&sbi->wb_sync_req[DATA]))
3268 goto skip_write;
3269
3270 if (__should_serialize_io(inode, wbc)) {
3271 mutex_lock(&sbi->writepages);
3272 locked = true;
3273 }
3274
3275 blk_start_plug(&plug);
3276 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
3277 blk_finish_plug(&plug);
3278
3279 if (locked)
3280 mutex_unlock(&sbi->writepages);
3281
3282 if (wbc->sync_mode == WB_SYNC_ALL)
3283 atomic_dec(&sbi->wb_sync_req[DATA]);
3284 /*
3285 * if some pages were truncated, we cannot guarantee its mapping->host
3286 * to detect pending bios.
3287 */
3288
3289 f2fs_remove_dirty_inode(inode);
3290 return ret;
3291
3292skip_write:
3293 wbc->pages_skipped += get_dirty_pages(inode);
3294 trace_f2fs_writepages(mapping->host, wbc, DATA);
3295 return 0;
3296}
3297
3298static int f2fs_write_data_pages(struct address_space *mapping,
3299 struct writeback_control *wbc)
3300{
3301 struct inode *inode = mapping->host;
3302
3303 return __f2fs_write_data_pages(mapping, wbc,
3304 F2FS_I(inode)->cp_task == current ?
3305 FS_CP_DATA_IO : FS_DATA_IO);
3306}
3307
3308static void f2fs_write_failed(struct address_space *mapping, loff_t to)
3309{
3310 struct inode *inode = mapping->host;
3311 loff_t i_size = i_size_read(inode);
3312
Olivier Deprez157378f2022-04-04 15:47:50 +02003313 if (IS_NOQUOTA(inode))
3314 return;
3315
David Brazdil0f672f62019-12-10 10:32:29 +00003316 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
3317 if (to > i_size && !f2fs_verity_in_progress(inode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003318 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3319 down_write(&F2FS_I(inode)->i_mmap_sem);
3320
3321 truncate_pagecache(inode, i_size);
Olivier Deprez157378f2022-04-04 15:47:50 +02003322 f2fs_truncate_blocks(inode, i_size, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003323
3324 up_write(&F2FS_I(inode)->i_mmap_sem);
3325 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
3326 }
3327}
3328
3329static int prepare_write_begin(struct f2fs_sb_info *sbi,
3330 struct page *page, loff_t pos, unsigned len,
3331 block_t *blk_addr, bool *node_changed)
3332{
3333 struct inode *inode = page->mapping->host;
3334 pgoff_t index = page->index;
3335 struct dnode_of_data dn;
3336 struct page *ipage;
3337 bool locked = false;
3338 struct extent_info ei = {0,0,0};
3339 int err = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00003340 int flag;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003341
3342 /*
3343 * we already allocated all the blocks, so we don't need to get
3344 * the block addresses when there is no need to fill the page.
3345 */
3346 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
David Brazdil0f672f62019-12-10 10:32:29 +00003347 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
3348 !f2fs_verity_in_progress(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003349 return 0;
3350
David Brazdil0f672f62019-12-10 10:32:29 +00003351 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
3352 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
3353 flag = F2FS_GET_BLOCK_DEFAULT;
3354 else
3355 flag = F2FS_GET_BLOCK_PRE_AIO;
3356
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003357 if (f2fs_has_inline_data(inode) ||
3358 (pos & PAGE_MASK) >= i_size_read(inode)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02003359 f2fs_do_map_lock(sbi, flag, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003360 locked = true;
3361 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003362
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003363restart:
3364 /* check inline_data */
3365 ipage = f2fs_get_node_page(sbi, inode->i_ino);
3366 if (IS_ERR(ipage)) {
3367 err = PTR_ERR(ipage);
3368 goto unlock_out;
3369 }
3370
3371 set_new_dnode(&dn, inode, ipage, ipage, 0);
3372
3373 if (f2fs_has_inline_data(inode)) {
3374 if (pos + len <= MAX_INLINE_DATA(inode)) {
3375 f2fs_do_read_inline_data(page, ipage);
3376 set_inode_flag(inode, FI_DATA_EXIST);
3377 if (inode->i_nlink)
3378 set_inline_node(ipage);
3379 } else {
3380 err = f2fs_convert_inline_page(&dn, page);
3381 if (err)
3382 goto out;
3383 if (dn.data_blkaddr == NULL_ADDR)
3384 err = f2fs_get_block(&dn, index);
3385 }
3386 } else if (locked) {
3387 err = f2fs_get_block(&dn, index);
3388 } else {
3389 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
3390 dn.data_blkaddr = ei.blk + index - ei.fofs;
3391 } else {
3392 /* hole case */
3393 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
3394 if (err || dn.data_blkaddr == NULL_ADDR) {
3395 f2fs_put_dnode(&dn);
Olivier Deprez157378f2022-04-04 15:47:50 +02003396 f2fs_do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003397 true);
David Brazdil0f672f62019-12-10 10:32:29 +00003398 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003399 locked = true;
3400 goto restart;
3401 }
3402 }
3403 }
3404
3405 /* convert_inline_page can make node_changed */
3406 *blk_addr = dn.data_blkaddr;
3407 *node_changed = dn.node_changed;
3408out:
3409 f2fs_put_dnode(&dn);
3410unlock_out:
3411 if (locked)
Olivier Deprez157378f2022-04-04 15:47:50 +02003412 f2fs_do_map_lock(sbi, flag, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003413 return err;
3414}
3415
3416static int f2fs_write_begin(struct file *file, struct address_space *mapping,
3417 loff_t pos, unsigned len, unsigned flags,
3418 struct page **pagep, void **fsdata)
3419{
3420 struct inode *inode = mapping->host;
3421 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3422 struct page *page = NULL;
3423 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
3424 bool need_balance = false, drop_atomic = false;
3425 block_t blkaddr = NULL_ADDR;
3426 int err = 0;
3427
3428 trace_f2fs_write_begin(inode, pos, len, flags);
3429
David Brazdil0f672f62019-12-10 10:32:29 +00003430 if (!f2fs_is_checkpoint_ready(sbi)) {
3431 err = -ENOSPC;
3432 goto fail;
3433 }
3434
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003435 if ((f2fs_is_atomic_file(inode) &&
3436 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
3437 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
3438 err = -ENOMEM;
3439 drop_atomic = true;
3440 goto fail;
3441 }
3442
3443 /*
3444 * We should check this at this moment to avoid deadlock on inode page
3445 * and #0 page. The locking rule for inline_data conversion should be:
3446 * lock_page(page #0) -> lock_page(inode_page)
3447 */
3448 if (index != 0) {
3449 err = f2fs_convert_inline_inode(inode);
3450 if (err)
3451 goto fail;
3452 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003453
3454#ifdef CONFIG_F2FS_FS_COMPRESSION
3455 if (f2fs_compressed_file(inode)) {
3456 int ret;
3457
3458 *fsdata = NULL;
3459
3460 ret = f2fs_prepare_compress_overwrite(inode, pagep,
3461 index, fsdata);
3462 if (ret < 0) {
3463 err = ret;
3464 goto fail;
3465 } else if (ret) {
3466 return 0;
3467 }
3468 }
3469#endif
3470
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003471repeat:
3472 /*
3473 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
3474 * wait_for_stable_page. Will wait that below with our IO control.
3475 */
3476 page = f2fs_pagecache_get_page(mapping, index,
3477 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
3478 if (!page) {
3479 err = -ENOMEM;
3480 goto fail;
3481 }
3482
Olivier Deprez157378f2022-04-04 15:47:50 +02003483 /* TODO: cluster can be compressed due to race with .writepage */
3484
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003485 *pagep = page;
3486
3487 err = prepare_write_begin(sbi, page, pos, len,
3488 &blkaddr, &need_balance);
3489 if (err)
3490 goto fail;
3491
David Brazdil0f672f62019-12-10 10:32:29 +00003492 if (need_balance && !IS_NOQUOTA(inode) &&
3493 has_not_enough_free_secs(sbi, 0, 0)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003494 unlock_page(page);
3495 f2fs_balance_fs(sbi, true);
3496 lock_page(page);
3497 if (page->mapping != mapping) {
3498 /* The page got truncated from under us */
3499 f2fs_put_page(page, 1);
3500 goto repeat;
3501 }
3502 }
3503
David Brazdil0f672f62019-12-10 10:32:29 +00003504 f2fs_wait_on_page_writeback(page, DATA, false, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003505
3506 if (len == PAGE_SIZE || PageUptodate(page))
3507 return 0;
3508
David Brazdil0f672f62019-12-10 10:32:29 +00003509 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
3510 !f2fs_verity_in_progress(inode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003511 zero_user_segment(page, len, PAGE_SIZE);
3512 return 0;
3513 }
3514
3515 if (blkaddr == NEW_ADDR) {
3516 zero_user_segment(page, 0, PAGE_SIZE);
3517 SetPageUptodate(page);
3518 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00003519 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
3520 DATA_GENERIC_ENHANCE_READ)) {
3521 err = -EFSCORRUPTED;
3522 goto fail;
3523 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003524 err = f2fs_submit_page_read(inode, page, blkaddr, 0, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003525 if (err)
3526 goto fail;
3527
3528 lock_page(page);
3529 if (unlikely(page->mapping != mapping)) {
3530 f2fs_put_page(page, 1);
3531 goto repeat;
3532 }
3533 if (unlikely(!PageUptodate(page))) {
3534 err = -EIO;
3535 goto fail;
3536 }
3537 }
3538 return 0;
3539
3540fail:
3541 f2fs_put_page(page, 1);
3542 f2fs_write_failed(mapping, pos + len);
3543 if (drop_atomic)
3544 f2fs_drop_inmem_pages_all(sbi, false);
3545 return err;
3546}
3547
3548static int f2fs_write_end(struct file *file,
3549 struct address_space *mapping,
3550 loff_t pos, unsigned len, unsigned copied,
3551 struct page *page, void *fsdata)
3552{
3553 struct inode *inode = page->mapping->host;
3554
3555 trace_f2fs_write_end(inode, pos, len, copied);
3556
3557 /*
3558 * This should be come from len == PAGE_SIZE, and we expect copied
3559 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
3560 * let generic_perform_write() try to copy data again through copied=0.
3561 */
3562 if (!PageUptodate(page)) {
3563 if (unlikely(copied != len))
3564 copied = 0;
3565 else
3566 SetPageUptodate(page);
3567 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003568
3569#ifdef CONFIG_F2FS_FS_COMPRESSION
3570 /* overwrite compressed file */
3571 if (f2fs_compressed_file(inode) && fsdata) {
3572 f2fs_compress_write_end(inode, fsdata, page->index, copied);
3573 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3574
3575 if (pos + copied > i_size_read(inode) &&
3576 !f2fs_verity_in_progress(inode))
3577 f2fs_i_size_write(inode, pos + copied);
3578 return copied;
3579 }
3580#endif
3581
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003582 if (!copied)
3583 goto unlock_out;
3584
3585 set_page_dirty(page);
3586
David Brazdil0f672f62019-12-10 10:32:29 +00003587 if (pos + copied > i_size_read(inode) &&
3588 !f2fs_verity_in_progress(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003589 f2fs_i_size_write(inode, pos + copied);
3590unlock_out:
3591 f2fs_put_page(page, 1);
3592 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
3593 return copied;
3594}
3595
3596static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3597 loff_t offset)
3598{
3599 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3600 unsigned blkbits = i_blkbits;
3601 unsigned blocksize_mask = (1 << blkbits) - 1;
3602 unsigned long align = offset | iov_iter_alignment(iter);
3603 struct block_device *bdev = inode->i_sb->s_bdev;
3604
Olivier Deprez0e641232021-09-23 10:07:05 +02003605 if (iov_iter_rw(iter) == READ && offset >= i_size_read(inode))
3606 return 1;
3607
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003608 if (align & blocksize_mask) {
3609 if (bdev)
3610 blkbits = blksize_bits(bdev_logical_block_size(bdev));
3611 blocksize_mask = (1 << blkbits) - 1;
3612 if (align & blocksize_mask)
3613 return -EINVAL;
3614 return 1;
3615 }
3616 return 0;
3617}
3618
David Brazdil0f672f62019-12-10 10:32:29 +00003619static void f2fs_dio_end_io(struct bio *bio)
3620{
3621 struct f2fs_private_dio *dio = bio->bi_private;
3622
3623 dec_page_count(F2FS_I_SB(dio->inode),
3624 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3625
3626 bio->bi_private = dio->orig_private;
3627 bio->bi_end_io = dio->orig_end_io;
3628
Olivier Deprez157378f2022-04-04 15:47:50 +02003629 kfree(dio);
David Brazdil0f672f62019-12-10 10:32:29 +00003630
3631 bio_endio(bio);
3632}
3633
3634static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3635 loff_t file_offset)
3636{
3637 struct f2fs_private_dio *dio;
3638 bool write = (bio_op(bio) == REQ_OP_WRITE);
3639
3640 dio = f2fs_kzalloc(F2FS_I_SB(inode),
3641 sizeof(struct f2fs_private_dio), GFP_NOFS);
3642 if (!dio)
3643 goto out;
3644
3645 dio->inode = inode;
3646 dio->orig_end_io = bio->bi_end_io;
3647 dio->orig_private = bio->bi_private;
3648 dio->write = write;
3649
3650 bio->bi_end_io = f2fs_dio_end_io;
3651 bio->bi_private = dio;
3652
3653 inc_page_count(F2FS_I_SB(inode),
3654 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3655
3656 submit_bio(bio);
3657 return;
3658out:
3659 bio->bi_status = BLK_STS_IOERR;
3660 bio_endio(bio);
3661}
3662
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003663static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3664{
3665 struct address_space *mapping = iocb->ki_filp->f_mapping;
3666 struct inode *inode = mapping->host;
3667 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00003668 struct f2fs_inode_info *fi = F2FS_I(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003669 size_t count = iov_iter_count(iter);
3670 loff_t offset = iocb->ki_pos;
3671 int rw = iov_iter_rw(iter);
3672 int err;
3673 enum rw_hint hint = iocb->ki_hint;
3674 int whint_mode = F2FS_OPTION(sbi).whint_mode;
David Brazdil0f672f62019-12-10 10:32:29 +00003675 bool do_opu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003676
3677 err = check_direct_IO(inode, iter, offset);
3678 if (err)
3679 return err < 0 ? err : 0;
3680
David Brazdil0f672f62019-12-10 10:32:29 +00003681 if (f2fs_force_buffered_io(inode, iocb, iter))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003682 return 0;
3683
David Brazdil0f672f62019-12-10 10:32:29 +00003684 do_opu = allow_outplace_dio(inode, iocb, iter);
3685
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003686 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3687
3688 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3689 iocb->ki_hint = WRITE_LIFE_NOT_SET;
3690
David Brazdil0f672f62019-12-10 10:32:29 +00003691 if (iocb->ki_flags & IOCB_NOWAIT) {
3692 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003693 iocb->ki_hint = hint;
3694 err = -EAGAIN;
3695 goto out;
3696 }
David Brazdil0f672f62019-12-10 10:32:29 +00003697 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3698 up_read(&fi->i_gc_rwsem[rw]);
3699 iocb->ki_hint = hint;
3700 err = -EAGAIN;
3701 goto out;
3702 }
3703 } else {
3704 down_read(&fi->i_gc_rwsem[rw]);
3705 if (do_opu)
3706 down_read(&fi->i_gc_rwsem[READ]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003707 }
3708
David Brazdil0f672f62019-12-10 10:32:29 +00003709 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3710 iter, rw == WRITE ? get_data_block_dio_write :
3711 get_data_block_dio, NULL, f2fs_dio_submit_bio,
Olivier Deprez157378f2022-04-04 15:47:50 +02003712 rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3713 DIO_SKIP_HOLES);
David Brazdil0f672f62019-12-10 10:32:29 +00003714
3715 if (do_opu)
3716 up_read(&fi->i_gc_rwsem[READ]);
3717
3718 up_read(&fi->i_gc_rwsem[rw]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003719
3720 if (rw == WRITE) {
3721 if (whint_mode == WHINT_MODE_OFF)
3722 iocb->ki_hint = hint;
3723 if (err > 0) {
3724 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3725 err);
David Brazdil0f672f62019-12-10 10:32:29 +00003726 if (!do_opu)
3727 set_inode_flag(inode, FI_UPDATE_WRITE);
Olivier Deprez157378f2022-04-04 15:47:50 +02003728 } else if (err == -EIOCBQUEUED) {
3729 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3730 count - iov_iter_count(iter));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003731 } else if (err < 0) {
3732 f2fs_write_failed(mapping, offset + count);
3733 }
Olivier Deprez157378f2022-04-04 15:47:50 +02003734 } else {
3735 if (err > 0)
3736 f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3737 else if (err == -EIOCBQUEUED)
3738 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO,
3739 count - iov_iter_count(iter));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003740 }
3741
3742out:
3743 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3744
3745 return err;
3746}
3747
3748void f2fs_invalidate_page(struct page *page, unsigned int offset,
3749 unsigned int length)
3750{
3751 struct inode *inode = page->mapping->host;
3752 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3753
3754 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
3755 (offset % PAGE_SIZE || length != PAGE_SIZE))
3756 return;
3757
3758 if (PageDirty(page)) {
3759 if (inode->i_ino == F2FS_META_INO(sbi)) {
3760 dec_page_count(sbi, F2FS_DIRTY_META);
3761 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
3762 dec_page_count(sbi, F2FS_DIRTY_NODES);
3763 } else {
3764 inode_dec_dirty_pages(inode);
3765 f2fs_remove_dirty_inode(inode);
3766 }
3767 }
3768
David Brazdil0f672f62019-12-10 10:32:29 +00003769 clear_cold_data(page);
3770
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003771 if (IS_ATOMIC_WRITTEN_PAGE(page))
3772 return f2fs_drop_inmem_page(inode, page);
3773
David Brazdil0f672f62019-12-10 10:32:29 +00003774 f2fs_clear_page_private(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003775}
3776
3777int f2fs_release_page(struct page *page, gfp_t wait)
3778{
3779 /* If this is dirty page, keep PagePrivate */
3780 if (PageDirty(page))
3781 return 0;
3782
3783 /* This is atomic written page, keep Private */
3784 if (IS_ATOMIC_WRITTEN_PAGE(page))
3785 return 0;
3786
David Brazdil0f672f62019-12-10 10:32:29 +00003787 clear_cold_data(page);
3788 f2fs_clear_page_private(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003789 return 1;
3790}
3791
3792static int f2fs_set_data_page_dirty(struct page *page)
3793{
David Brazdil0f672f62019-12-10 10:32:29 +00003794 struct inode *inode = page_file_mapping(page)->host;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003795
3796 trace_f2fs_set_page_dirty(page, DATA);
3797
3798 if (!PageUptodate(page))
3799 SetPageUptodate(page);
David Brazdil0f672f62019-12-10 10:32:29 +00003800 if (PageSwapCache(page))
3801 return __set_page_dirty_nobuffers(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003802
3803 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
3804 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
3805 f2fs_register_inmem_page(inode, page);
3806 return 1;
3807 }
3808 /*
3809 * Previously, this page has been registered, we just
3810 * return here.
3811 */
3812 return 0;
3813 }
3814
3815 if (!PageDirty(page)) {
3816 __set_page_dirty_nobuffers(page);
3817 f2fs_update_dirty_page(inode, page);
3818 return 1;
3819 }
3820 return 0;
3821}
3822
Olivier Deprez157378f2022-04-04 15:47:50 +02003823
3824static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block)
3825{
3826#ifdef CONFIG_F2FS_FS_COMPRESSION
3827 struct dnode_of_data dn;
3828 sector_t start_idx, blknr = 0;
3829 int ret;
3830
3831 start_idx = round_down(block, F2FS_I(inode)->i_cluster_size);
3832
3833 set_new_dnode(&dn, inode, NULL, NULL, 0);
3834 ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE);
3835 if (ret)
3836 return 0;
3837
3838 if (dn.data_blkaddr != COMPRESS_ADDR) {
3839 dn.ofs_in_node += block - start_idx;
3840 blknr = f2fs_data_blkaddr(&dn);
3841 if (!__is_valid_data_blkaddr(blknr))
3842 blknr = 0;
3843 }
3844
3845 f2fs_put_dnode(&dn);
3846 return blknr;
3847#else
3848 return 0;
3849#endif
3850}
3851
3852
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003853static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
3854{
3855 struct inode *inode = mapping->host;
Olivier Deprez157378f2022-04-04 15:47:50 +02003856 struct buffer_head tmp = {
3857 .b_size = i_blocksize(inode),
3858 };
3859 sector_t blknr = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003860
3861 if (f2fs_has_inline_data(inode))
Olivier Deprez157378f2022-04-04 15:47:50 +02003862 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003863
3864 /* make sure allocating whole blocks */
3865 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
3866 filemap_write_and_wait(mapping);
3867
Olivier Deprez157378f2022-04-04 15:47:50 +02003868 /* Block number less than F2FS MAX BLOCKS */
3869 if (unlikely(block >= F2FS_I_SB(inode)->max_file_blocks))
3870 goto out;
3871
3872 if (f2fs_compressed_file(inode)) {
3873 blknr = f2fs_bmap_compress(inode, block);
3874 } else {
3875 if (!get_data_block_bmap(inode, block, &tmp, 0))
3876 blknr = tmp.b_blocknr;
3877 }
3878out:
3879 trace_f2fs_bmap(inode, block, blknr);
3880 return blknr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003881}
3882
3883#ifdef CONFIG_MIGRATION
3884#include <linux/migrate.h>
3885
3886int f2fs_migrate_page(struct address_space *mapping,
3887 struct page *newpage, struct page *page, enum migrate_mode mode)
3888{
3889 int rc, extra_count;
3890 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3891 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3892
3893 BUG_ON(PageWriteback(page));
3894
3895 /* migrating an atomic written page is safe with the inmem_lock hold */
3896 if (atomic_written) {
3897 if (mode != MIGRATE_SYNC)
3898 return -EBUSY;
3899 if (!mutex_trylock(&fi->inmem_lock))
3900 return -EAGAIN;
3901 }
3902
David Brazdil0f672f62019-12-10 10:32:29 +00003903 /* one extra reference was held for atomic_write page */
3904 extra_count = atomic_written ? 1 : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003905 rc = migrate_page_move_mapping(mapping, newpage,
David Brazdil0f672f62019-12-10 10:32:29 +00003906 page, extra_count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003907 if (rc != MIGRATEPAGE_SUCCESS) {
3908 if (atomic_written)
3909 mutex_unlock(&fi->inmem_lock);
3910 return rc;
3911 }
3912
3913 if (atomic_written) {
3914 struct inmem_pages *cur;
3915 list_for_each_entry(cur, &fi->inmem_pages, list)
3916 if (cur->page == page) {
3917 cur->page = newpage;
3918 break;
3919 }
3920 mutex_unlock(&fi->inmem_lock);
3921 put_page(page);
3922 get_page(newpage);
3923 }
3924
David Brazdil0f672f62019-12-10 10:32:29 +00003925 if (PagePrivate(page)) {
3926 f2fs_set_page_private(newpage, page_private(page));
3927 f2fs_clear_page_private(page);
3928 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003929
3930 if (mode != MIGRATE_SYNC_NO_COPY)
3931 migrate_page_copy(newpage, page);
3932 else
3933 migrate_page_states(newpage, page);
3934
3935 return MIGRATEPAGE_SUCCESS;
3936}
3937#endif
3938
David Brazdil0f672f62019-12-10 10:32:29 +00003939#ifdef CONFIG_SWAP
Olivier Deprez157378f2022-04-04 15:47:50 +02003940static int check_swap_activate_fast(struct swap_info_struct *sis,
3941 struct file *swap_file, sector_t *span)
3942{
3943 struct address_space *mapping = swap_file->f_mapping;
3944 struct inode *inode = mapping->host;
3945 sector_t cur_lblock;
3946 sector_t last_lblock;
3947 sector_t pblock;
3948 sector_t lowest_pblock = -1;
3949 sector_t highest_pblock = 0;
3950 int nr_extents = 0;
3951 unsigned long nr_pblocks;
3952 unsigned long len;
3953 int ret;
3954
3955 /*
3956 * Map all the blocks into the extent list. This code doesn't try
3957 * to be very smart.
3958 */
3959 cur_lblock = 0;
3960 last_lblock = logical_to_blk(inode, i_size_read(inode));
3961 len = i_size_read(inode);
3962
3963 while (cur_lblock <= last_lblock && cur_lblock < sis->max) {
3964 struct buffer_head map_bh;
3965 pgoff_t next_pgofs;
3966
3967 cond_resched();
3968
3969 memset(&map_bh, 0, sizeof(struct buffer_head));
3970 map_bh.b_size = len - cur_lblock;
3971
3972 ret = get_data_block(inode, cur_lblock, &map_bh, 0,
3973 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
3974 if (ret)
3975 goto err_out;
3976
3977 /* hole */
3978 if (!buffer_mapped(&map_bh))
3979 goto err_out;
3980
3981 pblock = map_bh.b_blocknr;
3982 nr_pblocks = logical_to_blk(inode, map_bh.b_size);
3983
3984 if (cur_lblock + nr_pblocks >= sis->max)
3985 nr_pblocks = sis->max - cur_lblock;
3986
3987 if (cur_lblock) { /* exclude the header page */
3988 if (pblock < lowest_pblock)
3989 lowest_pblock = pblock;
3990 if (pblock + nr_pblocks - 1 > highest_pblock)
3991 highest_pblock = pblock + nr_pblocks - 1;
3992 }
3993
3994 /*
3995 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3996 */
3997 ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock);
3998 if (ret < 0)
3999 goto out;
4000 nr_extents += ret;
4001 cur_lblock += nr_pblocks;
4002 }
4003 ret = nr_extents;
4004 *span = 1 + highest_pblock - lowest_pblock;
4005 if (cur_lblock == 0)
4006 cur_lblock = 1; /* force Empty message */
4007 sis->max = cur_lblock;
4008 sis->pages = cur_lblock - 1;
4009 sis->highest_bit = cur_lblock - 1;
4010out:
4011 return ret;
4012err_out:
4013 pr_err("swapon: swapfile has holes\n");
4014 return -EINVAL;
4015}
4016
David Brazdil0f672f62019-12-10 10:32:29 +00004017/* Copied from generic_swapfile_activate() to check any holes */
Olivier Deprez0e641232021-09-23 10:07:05 +02004018static int check_swap_activate(struct swap_info_struct *sis,
4019 struct file *swap_file, sector_t *span)
David Brazdil0f672f62019-12-10 10:32:29 +00004020{
4021 struct address_space *mapping = swap_file->f_mapping;
4022 struct inode *inode = mapping->host;
4023 unsigned blocks_per_page;
4024 unsigned long page_no;
4025 unsigned blkbits;
4026 sector_t probe_block;
4027 sector_t last_block;
4028 sector_t lowest_block = -1;
4029 sector_t highest_block = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02004030 int nr_extents = 0;
4031 int ret;
David Brazdil0f672f62019-12-10 10:32:29 +00004032
Olivier Deprez157378f2022-04-04 15:47:50 +02004033 if (PAGE_SIZE == F2FS_BLKSIZE)
4034 return check_swap_activate_fast(sis, swap_file, span);
4035
David Brazdil0f672f62019-12-10 10:32:29 +00004036 blkbits = inode->i_blkbits;
4037 blocks_per_page = PAGE_SIZE >> blkbits;
4038
4039 /*
4040 * Map all the blocks into the extent list. This code doesn't try
4041 * to be very smart.
4042 */
4043 probe_block = 0;
4044 page_no = 0;
4045 last_block = i_size_read(inode) >> blkbits;
Olivier Deprez0e641232021-09-23 10:07:05 +02004046 while ((probe_block + blocks_per_page) <= last_block &&
4047 page_no < sis->max) {
David Brazdil0f672f62019-12-10 10:32:29 +00004048 unsigned block_in_page;
4049 sector_t first_block;
Olivier Deprez157378f2022-04-04 15:47:50 +02004050 sector_t block = 0;
4051 int err = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00004052
4053 cond_resched();
4054
Olivier Deprez157378f2022-04-04 15:47:50 +02004055 block = probe_block;
4056 err = bmap(inode, &block);
4057 if (err || !block)
David Brazdil0f672f62019-12-10 10:32:29 +00004058 goto bad_bmap;
Olivier Deprez157378f2022-04-04 15:47:50 +02004059 first_block = block;
David Brazdil0f672f62019-12-10 10:32:29 +00004060
4061 /*
4062 * It must be PAGE_SIZE aligned on-disk
4063 */
4064 if (first_block & (blocks_per_page - 1)) {
4065 probe_block++;
4066 goto reprobe;
4067 }
4068
4069 for (block_in_page = 1; block_in_page < blocks_per_page;
4070 block_in_page++) {
David Brazdil0f672f62019-12-10 10:32:29 +00004071
Olivier Deprez157378f2022-04-04 15:47:50 +02004072 block = probe_block + block_in_page;
4073 err = bmap(inode, &block);
4074
4075 if (err || !block)
David Brazdil0f672f62019-12-10 10:32:29 +00004076 goto bad_bmap;
Olivier Deprez157378f2022-04-04 15:47:50 +02004077
David Brazdil0f672f62019-12-10 10:32:29 +00004078 if (block != first_block + block_in_page) {
4079 /* Discontiguity */
4080 probe_block++;
4081 goto reprobe;
4082 }
4083 }
4084
4085 first_block >>= (PAGE_SHIFT - blkbits);
4086 if (page_no) { /* exclude the header page */
4087 if (first_block < lowest_block)
4088 lowest_block = first_block;
4089 if (first_block > highest_block)
4090 highest_block = first_block;
4091 }
4092
Olivier Deprez0e641232021-09-23 10:07:05 +02004093 /*
4094 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
4095 */
4096 ret = add_swap_extent(sis, page_no, 1, first_block);
4097 if (ret < 0)
4098 goto out;
4099 nr_extents += ret;
David Brazdil0f672f62019-12-10 10:32:29 +00004100 page_no++;
4101 probe_block += blocks_per_page;
4102reprobe:
4103 continue;
4104 }
Olivier Deprez0e641232021-09-23 10:07:05 +02004105 ret = nr_extents;
4106 *span = 1 + highest_block - lowest_block;
4107 if (page_no == 0)
4108 page_no = 1; /* force Empty message */
4109 sis->max = page_no;
4110 sis->pages = page_no - 1;
4111 sis->highest_bit = page_no - 1;
4112out:
4113 return ret;
David Brazdil0f672f62019-12-10 10:32:29 +00004114bad_bmap:
4115 pr_err("swapon: swapfile has holes\n");
4116 return -EINVAL;
4117}
4118
4119static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4120 sector_t *span)
4121{
4122 struct inode *inode = file_inode(file);
4123 int ret;
4124
4125 if (!S_ISREG(inode->i_mode))
4126 return -EINVAL;
4127
4128 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
4129 return -EROFS;
4130
Olivier Deprez157378f2022-04-04 15:47:50 +02004131 if (f2fs_lfs_mode(F2FS_I_SB(inode))) {
4132 f2fs_err(F2FS_I_SB(inode),
4133 "Swapfile not supported in LFS mode");
4134 return -EINVAL;
4135 }
4136
David Brazdil0f672f62019-12-10 10:32:29 +00004137 ret = f2fs_convert_inline_inode(inode);
4138 if (ret)
4139 return ret;
4140
Olivier Deprez157378f2022-04-04 15:47:50 +02004141 if (!f2fs_disable_compressed_file(inode))
4142 return -EINVAL;
4143
Olivier Deprez0e641232021-09-23 10:07:05 +02004144 ret = check_swap_activate(sis, file, span);
4145 if (ret < 0)
David Brazdil0f672f62019-12-10 10:32:29 +00004146 return ret;
4147
4148 set_inode_flag(inode, FI_PIN_FILE);
4149 f2fs_precache_extents(inode);
4150 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Olivier Deprez0e641232021-09-23 10:07:05 +02004151 return ret;
David Brazdil0f672f62019-12-10 10:32:29 +00004152}
4153
4154static void f2fs_swap_deactivate(struct file *file)
4155{
4156 struct inode *inode = file_inode(file);
4157
4158 clear_inode_flag(inode, FI_PIN_FILE);
4159}
4160#else
4161static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4162 sector_t *span)
4163{
4164 return -EOPNOTSUPP;
4165}
4166
4167static void f2fs_swap_deactivate(struct file *file)
4168{
4169}
4170#endif
4171
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004172const struct address_space_operations f2fs_dblock_aops = {
4173 .readpage = f2fs_read_data_page,
Olivier Deprez157378f2022-04-04 15:47:50 +02004174 .readahead = f2fs_readahead,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004175 .writepage = f2fs_write_data_page,
4176 .writepages = f2fs_write_data_pages,
4177 .write_begin = f2fs_write_begin,
4178 .write_end = f2fs_write_end,
4179 .set_page_dirty = f2fs_set_data_page_dirty,
4180 .invalidatepage = f2fs_invalidate_page,
4181 .releasepage = f2fs_release_page,
4182 .direct_IO = f2fs_direct_IO,
4183 .bmap = f2fs_bmap,
David Brazdil0f672f62019-12-10 10:32:29 +00004184 .swap_activate = f2fs_swap_activate,
4185 .swap_deactivate = f2fs_swap_deactivate,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004186#ifdef CONFIG_MIGRATION
4187 .migratepage = f2fs_migrate_page,
4188#endif
4189};
4190
David Brazdil0f672f62019-12-10 10:32:29 +00004191void f2fs_clear_page_cache_dirty_tag(struct page *page)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004192{
4193 struct address_space *mapping = page_mapping(page);
4194 unsigned long flags;
4195
4196 xa_lock_irqsave(&mapping->i_pages, flags);
David Brazdil0f672f62019-12-10 10:32:29 +00004197 __xa_clear_mark(&mapping->i_pages, page_index(page),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004198 PAGECACHE_TAG_DIRTY);
4199 xa_unlock_irqrestore(&mapping->i_pages, flags);
4200}
4201
4202int __init f2fs_init_post_read_processing(void)
4203{
David Brazdil0f672f62019-12-10 10:32:29 +00004204 bio_post_read_ctx_cache =
4205 kmem_cache_create("f2fs_bio_post_read_ctx",
4206 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004207 if (!bio_post_read_ctx_cache)
4208 goto fail;
4209 bio_post_read_ctx_pool =
4210 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4211 bio_post_read_ctx_cache);
4212 if (!bio_post_read_ctx_pool)
4213 goto fail_free_cache;
4214 return 0;
4215
4216fail_free_cache:
4217 kmem_cache_destroy(bio_post_read_ctx_cache);
4218fail:
4219 return -ENOMEM;
4220}
4221
Olivier Deprez157378f2022-04-04 15:47:50 +02004222void f2fs_destroy_post_read_processing(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004223{
4224 mempool_destroy(bio_post_read_ctx_pool);
4225 kmem_cache_destroy(bio_post_read_ctx_cache);
4226}
Olivier Deprez157378f2022-04-04 15:47:50 +02004227
4228int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi)
4229{
4230 if (!f2fs_sb_has_encrypt(sbi) &&
4231 !f2fs_sb_has_verity(sbi) &&
4232 !f2fs_sb_has_compression(sbi))
4233 return 0;
4234
4235 sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq",
4236 WQ_UNBOUND | WQ_HIGHPRI,
4237 num_online_cpus());
4238 if (!sbi->post_read_wq)
4239 return -ENOMEM;
4240 return 0;
4241}
4242
4243void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi)
4244{
4245 if (sbi->post_read_wq)
4246 destroy_workqueue(sbi->post_read_wq);
4247}
4248
4249int __init f2fs_init_bio_entry_cache(void)
4250{
4251 bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab",
4252 sizeof(struct bio_entry));
4253 if (!bio_entry_slab)
4254 return -ENOMEM;
4255 return 0;
4256}
4257
4258void f2fs_destroy_bio_entry_cache(void)
4259{
4260 kmem_cache_destroy(bio_entry_slab);
4261}