blob: 1679f9c0b63b32b906fee0139f371dcdb39b7ad3 [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>
David Brazdil0f672f62019-12-10 10:32:29 +000017#include <linux/swap.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018#include <linux/prefetch.h>
19#include <linux/uio.h>
20#include <linux/cleancache.h>
21#include <linux/sched/signal.h>
22
23#include "f2fs.h"
24#include "node.h"
25#include "segment.h"
26#include "trace.h"
27#include <trace/events/f2fs.h>
28
29#define NUM_PREALLOC_POST_READ_CTXS 128
30
31static struct kmem_cache *bio_post_read_ctx_cache;
32static mempool_t *bio_post_read_ctx_pool;
33
34static bool __is_cp_guaranteed(struct page *page)
35{
36 struct address_space *mapping = page->mapping;
37 struct inode *inode;
38 struct f2fs_sb_info *sbi;
39
40 if (!mapping)
41 return false;
42
43 inode = mapping->host;
44 sbi = F2FS_I_SB(inode);
45
46 if (inode->i_ino == F2FS_META_INO(sbi) ||
47 inode->i_ino == F2FS_NODE_INO(sbi) ||
48 S_ISDIR(inode->i_mode) ||
49 (S_ISREG(inode->i_mode) &&
David Brazdil0f672f62019-12-10 10:32:29 +000050 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051 is_cold_data(page))
52 return true;
53 return false;
54}
55
David Brazdil0f672f62019-12-10 10:32:29 +000056static enum count_type __read_io_type(struct page *page)
57{
58 struct address_space *mapping = page_file_mapping(page);
59
60 if (mapping) {
61 struct inode *inode = mapping->host;
62 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
63
64 if (inode->i_ino == F2FS_META_INO(sbi))
65 return F2FS_RD_META;
66
67 if (inode->i_ino == F2FS_NODE_INO(sbi))
68 return F2FS_RD_NODE;
69 }
70 return F2FS_RD_DATA;
71}
72
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000073/* postprocessing steps for read bios */
74enum bio_post_read_step {
75 STEP_INITIAL = 0,
76 STEP_DECRYPT,
David Brazdil0f672f62019-12-10 10:32:29 +000077 STEP_VERITY,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078};
79
80struct bio_post_read_ctx {
81 struct bio *bio;
82 struct work_struct work;
83 unsigned int cur_step;
84 unsigned int enabled_steps;
85};
86
87static void __read_end_io(struct bio *bio)
88{
89 struct page *page;
90 struct bio_vec *bv;
David Brazdil0f672f62019-12-10 10:32:29 +000091 struct bvec_iter_all iter_all;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092
David Brazdil0f672f62019-12-10 10:32:29 +000093 bio_for_each_segment_all(bv, bio, iter_all) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000094 page = bv->bv_page;
95
96 /* PG_error was set if any post_read step failed */
97 if (bio->bi_status || PageError(page)) {
98 ClearPageUptodate(page);
99 /* will re-read again later */
100 ClearPageError(page);
101 } else {
102 SetPageUptodate(page);
103 }
David Brazdil0f672f62019-12-10 10:32:29 +0000104 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105 unlock_page(page);
106 }
107 if (bio->bi_private)
108 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
109 bio_put(bio);
110}
111
112static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
113
114static void decrypt_work(struct work_struct *work)
115{
116 struct bio_post_read_ctx *ctx =
117 container_of(work, struct bio_post_read_ctx, work);
118
119 fscrypt_decrypt_bio(ctx->bio);
120
121 bio_post_read_processing(ctx);
122}
123
David Brazdil0f672f62019-12-10 10:32:29 +0000124static void verity_work(struct work_struct *work)
125{
126 struct bio_post_read_ctx *ctx =
127 container_of(work, struct bio_post_read_ctx, work);
128
129 fsverity_verify_bio(ctx->bio);
130
131 bio_post_read_processing(ctx);
132}
133
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000134static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
135{
David Brazdil0f672f62019-12-10 10:32:29 +0000136 /*
137 * We use different work queues for decryption and for verity because
138 * verity may require reading metadata pages that need decryption, and
139 * we shouldn't recurse to the same workqueue.
140 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141 switch (++ctx->cur_step) {
142 case STEP_DECRYPT:
143 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
144 INIT_WORK(&ctx->work, decrypt_work);
145 fscrypt_enqueue_decrypt_work(&ctx->work);
146 return;
147 }
148 ctx->cur_step++;
149 /* fall-through */
David Brazdil0f672f62019-12-10 10:32:29 +0000150 case STEP_VERITY:
151 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
152 INIT_WORK(&ctx->work, verity_work);
153 fsverity_enqueue_verify_work(&ctx->work);
154 return;
155 }
156 ctx->cur_step++;
157 /* fall-through */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000158 default:
159 __read_end_io(ctx->bio);
160 }
161}
162
163static bool f2fs_bio_post_read_required(struct bio *bio)
164{
165 return bio->bi_private && !bio->bi_status;
166}
167
168static void f2fs_read_end_io(struct bio *bio)
169{
Olivier Deprez0e641232021-09-23 10:07:05 +0200170 struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio));
171
172 if (time_to_inject(sbi, FAULT_READ_IO)) {
173 f2fs_show_injection_info(sbi, FAULT_READ_IO);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000174 bio->bi_status = BLK_STS_IOERR;
175 }
176
177 if (f2fs_bio_post_read_required(bio)) {
178 struct bio_post_read_ctx *ctx = bio->bi_private;
179
180 ctx->cur_step = STEP_INITIAL;
181 bio_post_read_processing(ctx);
182 return;
183 }
184
185 __read_end_io(bio);
186}
187
188static void f2fs_write_end_io(struct bio *bio)
189{
190 struct f2fs_sb_info *sbi = bio->bi_private;
191 struct bio_vec *bvec;
David Brazdil0f672f62019-12-10 10:32:29 +0000192 struct bvec_iter_all iter_all;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000193
David Brazdil0f672f62019-12-10 10:32:29 +0000194 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200195 f2fs_show_injection_info(sbi, FAULT_WRITE_IO);
David Brazdil0f672f62019-12-10 10:32:29 +0000196 bio->bi_status = BLK_STS_IOERR;
197 }
198
199 bio_for_each_segment_all(bvec, bio, iter_all) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200 struct page *page = bvec->bv_page;
201 enum count_type type = WB_DATA_TYPE(page);
202
203 if (IS_DUMMY_WRITTEN_PAGE(page)) {
204 set_page_private(page, (unsigned long)NULL);
205 ClearPagePrivate(page);
206 unlock_page(page);
207 mempool_free(page, sbi->write_io_dummy);
208
209 if (unlikely(bio->bi_status))
210 f2fs_stop_checkpoint(sbi, true);
211 continue;
212 }
213
David Brazdil0f672f62019-12-10 10:32:29 +0000214 fscrypt_finalize_bounce_page(&page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000215
216 if (unlikely(bio->bi_status)) {
217 mapping_set_error(page->mapping, -EIO);
218 if (type == F2FS_WB_CP_DATA)
219 f2fs_stop_checkpoint(sbi, true);
220 }
221
222 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
223 page->index != nid_of_node(page));
224
225 dec_page_count(sbi, type);
226 if (f2fs_in_warm_node_list(sbi, page))
227 f2fs_del_fsync_node_entry(sbi, page);
228 clear_cold_data(page);
229 end_page_writeback(page);
230 }
231 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
232 wq_has_sleeper(&sbi->cp_wait))
233 wake_up(&sbi->cp_wait);
234
235 bio_put(bio);
236}
237
238/*
239 * Return true, if pre_bio's bdev is same as its target device.
240 */
241struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
242 block_t blk_addr, struct bio *bio)
243{
244 struct block_device *bdev = sbi->sb->s_bdev;
245 int i;
246
David Brazdil0f672f62019-12-10 10:32:29 +0000247 if (f2fs_is_multi_device(sbi)) {
248 for (i = 0; i < sbi->s_ndevs; i++) {
249 if (FDEV(i).start_blk <= blk_addr &&
250 FDEV(i).end_blk >= blk_addr) {
251 blk_addr -= FDEV(i).start_blk;
252 bdev = FDEV(i).bdev;
253 break;
254 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000255 }
256 }
257 if (bio) {
258 bio_set_dev(bio, bdev);
259 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
260 }
261 return bdev;
262}
263
264int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
265{
266 int i;
267
David Brazdil0f672f62019-12-10 10:32:29 +0000268 if (!f2fs_is_multi_device(sbi))
269 return 0;
270
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271 for (i = 0; i < sbi->s_ndevs; i++)
272 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
273 return i;
274 return 0;
275}
276
277static bool __same_bdev(struct f2fs_sb_info *sbi,
278 block_t blk_addr, struct bio *bio)
279{
280 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
281 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
282}
283
284/*
285 * Low-level block read/write IO operations.
286 */
David Brazdil0f672f62019-12-10 10:32:29 +0000287static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288{
David Brazdil0f672f62019-12-10 10:32:29 +0000289 struct f2fs_sb_info *sbi = fio->sbi;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000290 struct bio *bio;
291
292 bio = f2fs_bio_alloc(sbi, npages, true);
293
David Brazdil0f672f62019-12-10 10:32:29 +0000294 f2fs_target_device(sbi, fio->new_blkaddr, bio);
295 if (is_read_io(fio->op)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000296 bio->bi_end_io = f2fs_read_end_io;
297 bio->bi_private = NULL;
298 } else {
299 bio->bi_end_io = f2fs_write_end_io;
300 bio->bi_private = sbi;
David Brazdil0f672f62019-12-10 10:32:29 +0000301 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi,
302 fio->type, fio->temp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000303 }
David Brazdil0f672f62019-12-10 10:32:29 +0000304 if (fio->io_wbc)
305 wbc_init_bio(fio->io_wbc, bio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000306
307 return bio;
308}
309
310static inline void __submit_bio(struct f2fs_sb_info *sbi,
311 struct bio *bio, enum page_type type)
312{
313 if (!is_read_io(bio_op(bio))) {
314 unsigned int start;
315
316 if (type != DATA && type != NODE)
317 goto submit_io;
318
319 if (test_opt(sbi, LFS) && current->plug)
320 blk_finish_plug(current->plug);
321
Olivier Deprez0e641232021-09-23 10:07:05 +0200322 if (!F2FS_IO_ALIGNED(sbi))
David Brazdil0f672f62019-12-10 10:32:29 +0000323 goto submit_io;
324
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000325 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
326 start %= F2FS_IO_SIZE(sbi);
327
328 if (start == 0)
329 goto submit_io;
330
331 /* fill dummy pages */
332 for (; start < F2FS_IO_SIZE(sbi); start++) {
333 struct page *page =
334 mempool_alloc(sbi->write_io_dummy,
David Brazdil0f672f62019-12-10 10:32:29 +0000335 GFP_NOIO | __GFP_NOFAIL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000336 f2fs_bug_on(sbi, !page);
337
David Brazdil0f672f62019-12-10 10:32:29 +0000338 zero_user_segment(page, 0, PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000339 SetPagePrivate(page);
340 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
341 lock_page(page);
342 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
343 f2fs_bug_on(sbi, 1);
344 }
345 /*
346 * In the NODE case, we lose next block address chain. So, we
347 * need to do checkpoint in f2fs_sync_file.
348 */
349 if (type == NODE)
350 set_sbi_flag(sbi, SBI_NEED_CP);
351 }
352submit_io:
353 if (is_read_io(bio_op(bio)))
354 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
355 else
356 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
357 submit_bio(bio);
358}
359
360static void __submit_merged_bio(struct f2fs_bio_info *io)
361{
362 struct f2fs_io_info *fio = &io->fio;
363
364 if (!io->bio)
365 return;
366
367 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
368
369 if (is_read_io(fio->op))
370 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
371 else
372 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
373
374 __submit_bio(io->sbi, io->bio, fio->type);
375 io->bio = NULL;
376}
377
David Brazdil0f672f62019-12-10 10:32:29 +0000378static bool __has_merged_page(struct bio *bio, struct inode *inode,
379 struct page *page, nid_t ino)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000380{
381 struct bio_vec *bvec;
382 struct page *target;
David Brazdil0f672f62019-12-10 10:32:29 +0000383 struct bvec_iter_all iter_all;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000384
David Brazdil0f672f62019-12-10 10:32:29 +0000385 if (!bio)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000386 return false;
387
David Brazdil0f672f62019-12-10 10:32:29 +0000388 if (!inode && !page && !ino)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000389 return true;
390
David Brazdil0f672f62019-12-10 10:32:29 +0000391 bio_for_each_segment_all(bvec, bio, iter_all) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000392
David Brazdil0f672f62019-12-10 10:32:29 +0000393 target = bvec->bv_page;
394 if (fscrypt_is_bounce_page(target))
395 target = fscrypt_pagecache_page(target);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000396
397 if (inode && inode == target->mapping->host)
398 return true;
David Brazdil0f672f62019-12-10 10:32:29 +0000399 if (page && page == target)
400 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000401 if (ino && ino == ino_of_node(target))
402 return true;
403 }
404
405 return false;
406}
407
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000408static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
409 enum page_type type, enum temp_type temp)
410{
411 enum page_type btype = PAGE_TYPE_OF_BIO(type);
412 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
413
414 down_write(&io->io_rwsem);
415
416 /* change META to META_FLUSH in the checkpoint procedure */
417 if (type >= META_FLUSH) {
418 io->fio.type = META_FLUSH;
419 io->fio.op = REQ_OP_WRITE;
420 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
421 if (!test_opt(sbi, NOBARRIER))
422 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
423 }
424 __submit_merged_bio(io);
425 up_write(&io->io_rwsem);
426}
427
428static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
David Brazdil0f672f62019-12-10 10:32:29 +0000429 struct inode *inode, struct page *page,
430 nid_t ino, enum page_type type, bool force)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000431{
432 enum temp_type temp;
David Brazdil0f672f62019-12-10 10:32:29 +0000433 bool ret = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000434
435 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
David Brazdil0f672f62019-12-10 10:32:29 +0000436 if (!force) {
437 enum page_type btype = PAGE_TYPE_OF_BIO(type);
438 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000439
David Brazdil0f672f62019-12-10 10:32:29 +0000440 down_read(&io->io_rwsem);
441 ret = __has_merged_page(io->bio, inode, page, ino);
442 up_read(&io->io_rwsem);
443 }
444 if (ret)
445 __f2fs_submit_merged_write(sbi, type, temp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000446
447 /* TODO: use HOT temp only for meta pages now. */
448 if (type >= META)
449 break;
450 }
451}
452
453void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
454{
David Brazdil0f672f62019-12-10 10:32:29 +0000455 __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000456}
457
458void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
David Brazdil0f672f62019-12-10 10:32:29 +0000459 struct inode *inode, struct page *page,
460 nid_t ino, enum page_type type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000461{
David Brazdil0f672f62019-12-10 10:32:29 +0000462 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000463}
464
465void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
466{
467 f2fs_submit_merged_write(sbi, DATA);
468 f2fs_submit_merged_write(sbi, NODE);
469 f2fs_submit_merged_write(sbi, META);
470}
471
472/*
473 * Fill the locked page with data located in the block address.
474 * A caller needs to unlock the page on failure.
475 */
476int f2fs_submit_page_bio(struct f2fs_io_info *fio)
477{
478 struct bio *bio;
479 struct page *page = fio->encrypted_page ?
480 fio->encrypted_page : fio->page;
481
482 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
David Brazdil0f672f62019-12-10 10:32:29 +0000483 fio->is_por ? META_POR : (__is_meta_io(fio) ?
484 META_GENERIC : DATA_GENERIC_ENHANCE)))
485 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000486
487 trace_f2fs_submit_page_bio(page, fio);
488 f2fs_trace_ios(fio, 0);
489
490 /* Allocate a new bio */
David Brazdil0f672f62019-12-10 10:32:29 +0000491 bio = __bio_alloc(fio, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000492
493 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
494 bio_put(bio);
495 return -EFAULT;
496 }
497
498 if (fio->io_wbc && !is_read_io(fio->op))
David Brazdil0f672f62019-12-10 10:32:29 +0000499 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500
501 bio_set_op_attrs(bio, fio->op, fio->op_flags);
502
David Brazdil0f672f62019-12-10 10:32:29 +0000503 inc_page_count(fio->sbi, is_read_io(fio->op) ?
504 __read_io_type(page): WB_DATA_TYPE(fio->page));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000505
506 __submit_bio(fio->sbi, bio, fio->type);
507 return 0;
508}
509
David Brazdil0f672f62019-12-10 10:32:29 +0000510static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
511 block_t last_blkaddr, block_t cur_blkaddr)
512{
513 if (last_blkaddr + 1 != cur_blkaddr)
514 return false;
515 return __same_bdev(sbi, cur_blkaddr, bio);
516}
517
518static bool io_type_is_mergeable(struct f2fs_bio_info *io,
519 struct f2fs_io_info *fio)
520{
521 if (io->fio.op != fio->op)
522 return false;
523 return io->fio.op_flags == fio->op_flags;
524}
525
526static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio,
527 struct f2fs_bio_info *io,
528 struct f2fs_io_info *fio,
529 block_t last_blkaddr,
530 block_t cur_blkaddr)
531{
532 if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) {
533 unsigned int filled_blocks =
534 F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size);
535 unsigned int io_size = F2FS_IO_SIZE(sbi);
536 unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt;
537
538 /* IOs in bio is aligned and left space of vectors is not enough */
539 if (!(filled_blocks % io_size) && left_vecs < io_size)
540 return false;
541 }
542 if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr))
543 return false;
544 return io_type_is_mergeable(io, fio);
545}
546
547int f2fs_merge_page_bio(struct f2fs_io_info *fio)
548{
549 struct bio *bio = *fio->bio;
550 struct page *page = fio->encrypted_page ?
551 fio->encrypted_page : fio->page;
552
553 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
554 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
555 return -EFSCORRUPTED;
556
557 trace_f2fs_submit_page_bio(page, fio);
558 f2fs_trace_ios(fio, 0);
559
560 if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block,
561 fio->new_blkaddr)) {
562 __submit_bio(fio->sbi, bio, fio->type);
563 bio = NULL;
564 }
565alloc_new:
566 if (!bio) {
567 bio = __bio_alloc(fio, BIO_MAX_PAGES);
568 bio_set_op_attrs(bio, fio->op, fio->op_flags);
569 }
570
571 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
572 __submit_bio(fio->sbi, bio, fio->type);
573 bio = NULL;
574 goto alloc_new;
575 }
576
577 if (fio->io_wbc)
578 wbc_account_cgroup_owner(fio->io_wbc, page, PAGE_SIZE);
579
580 inc_page_count(fio->sbi, WB_DATA_TYPE(page));
581
582 *fio->last_block = fio->new_blkaddr;
583 *fio->bio = bio;
584
585 return 0;
586}
587
588static void f2fs_submit_ipu_bio(struct f2fs_sb_info *sbi, struct bio **bio,
589 struct page *page)
590{
591 if (!bio)
592 return;
593
594 if (!__has_merged_page(*bio, NULL, page, 0))
595 return;
596
597 __submit_bio(sbi, *bio, DATA);
598 *bio = NULL;
599}
600
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000601void f2fs_submit_page_write(struct f2fs_io_info *fio)
602{
603 struct f2fs_sb_info *sbi = fio->sbi;
604 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
605 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
606 struct page *bio_page;
607
608 f2fs_bug_on(sbi, is_read_io(fio->op));
609
610 down_write(&io->io_rwsem);
611next:
612 if (fio->in_list) {
613 spin_lock(&io->io_lock);
614 if (list_empty(&io->io_list)) {
615 spin_unlock(&io->io_lock);
616 goto out;
617 }
618 fio = list_first_entry(&io->io_list,
619 struct f2fs_io_info, list);
620 list_del(&fio->list);
621 spin_unlock(&io->io_lock);
622 }
623
David Brazdil0f672f62019-12-10 10:32:29 +0000624 verify_fio_blkaddr(fio);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000625
626 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
627
628 /* set submitted = true as a return value */
629 fio->submitted = true;
630
631 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
632
David Brazdil0f672f62019-12-10 10:32:29 +0000633 if (io->bio && !io_is_mergeable(sbi, io->bio, io, fio,
634 io->last_block_in_bio, fio->new_blkaddr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000635 __submit_merged_bio(io);
636alloc_new:
637 if (io->bio == NULL) {
David Brazdil0f672f62019-12-10 10:32:29 +0000638 if (F2FS_IO_ALIGNED(sbi) &&
639 (fio->type == DATA || fio->type == NODE) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000640 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
641 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
642 fio->retry = true;
643 goto skip;
644 }
David Brazdil0f672f62019-12-10 10:32:29 +0000645 io->bio = __bio_alloc(fio, BIO_MAX_PAGES);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000646 io->fio = *fio;
647 }
648
649 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
650 __submit_merged_bio(io);
651 goto alloc_new;
652 }
653
654 if (fio->io_wbc)
David Brazdil0f672f62019-12-10 10:32:29 +0000655 wbc_account_cgroup_owner(fio->io_wbc, bio_page, PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000656
657 io->last_block_in_bio = fio->new_blkaddr;
658 f2fs_trace_ios(fio, 0);
659
660 trace_f2fs_submit_page_write(fio->page, fio);
661skip:
662 if (fio->in_list)
663 goto next;
664out:
David Brazdil0f672f62019-12-10 10:32:29 +0000665 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
666 !f2fs_is_checkpoint_ready(sbi))
667 __submit_merged_bio(io);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000668 up_write(&io->io_rwsem);
669}
670
David Brazdil0f672f62019-12-10 10:32:29 +0000671static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
672{
673 return fsverity_active(inode) &&
674 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
675}
676
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000677static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
David Brazdil0f672f62019-12-10 10:32:29 +0000678 unsigned nr_pages, unsigned op_flag,
679 pgoff_t first_idx)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000680{
681 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
682 struct bio *bio;
683 struct bio_post_read_ctx *ctx;
684 unsigned int post_read_steps = 0;
685
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000686 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
687 if (!bio)
688 return ERR_PTR(-ENOMEM);
689 f2fs_target_device(sbi, blkaddr, bio);
690 bio->bi_end_io = f2fs_read_end_io;
691 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
692
693 if (f2fs_encrypted_file(inode))
694 post_read_steps |= 1 << STEP_DECRYPT;
David Brazdil0f672f62019-12-10 10:32:29 +0000695
696 if (f2fs_need_verity(inode, first_idx))
697 post_read_steps |= 1 << STEP_VERITY;
698
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000699 if (post_read_steps) {
700 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
701 if (!ctx) {
702 bio_put(bio);
703 return ERR_PTR(-ENOMEM);
704 }
705 ctx->bio = bio;
706 ctx->enabled_steps = post_read_steps;
707 bio->bi_private = ctx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000708 }
709
710 return bio;
711}
712
713/* This can handle encryption stuffs */
714static int f2fs_submit_page_read(struct inode *inode, struct page *page,
715 block_t blkaddr)
716{
David Brazdil0f672f62019-12-10 10:32:29 +0000717 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
718 struct bio *bio;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000719
David Brazdil0f672f62019-12-10 10:32:29 +0000720 bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0, page->index);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000721 if (IS_ERR(bio))
722 return PTR_ERR(bio);
723
David Brazdil0f672f62019-12-10 10:32:29 +0000724 /* wait for GCed page writeback via META_MAPPING */
725 f2fs_wait_on_block_writeback(inode, blkaddr);
726
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000727 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
728 bio_put(bio);
729 return -EFAULT;
730 }
731 ClearPageError(page);
David Brazdil0f672f62019-12-10 10:32:29 +0000732 inc_page_count(sbi, F2FS_RD_DATA);
733 __submit_bio(sbi, bio, DATA);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000734 return 0;
735}
736
737static void __set_data_blkaddr(struct dnode_of_data *dn)
738{
739 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
740 __le32 *addr_array;
741 int base = 0;
742
743 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
744 base = get_extra_isize(dn->inode);
745
746 /* Get physical address of data block */
747 addr_array = blkaddr_in_node(rn);
748 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
749}
750
751/*
752 * Lock ordering for the change of data block address:
753 * ->data_page
754 * ->node_page
755 * update block addresses in the node page
756 */
757void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
758{
David Brazdil0f672f62019-12-10 10:32:29 +0000759 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000760 __set_data_blkaddr(dn);
761 if (set_page_dirty(dn->node_page))
762 dn->node_changed = true;
763}
764
765void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
766{
767 dn->data_blkaddr = blkaddr;
768 f2fs_set_data_blkaddr(dn);
769 f2fs_update_extent_cache(dn);
770}
771
772/* dn->ofs_in_node will be returned with up-to-date last block pointer */
773int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
774{
775 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
776 int err;
777
778 if (!count)
779 return 0;
780
781 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
782 return -EPERM;
783 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
784 return err;
785
786 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
787 dn->ofs_in_node, count);
788
David Brazdil0f672f62019-12-10 10:32:29 +0000789 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000790
791 for (; count > 0; dn->ofs_in_node++) {
792 block_t blkaddr = datablock_addr(dn->inode,
793 dn->node_page, dn->ofs_in_node);
794 if (blkaddr == NULL_ADDR) {
795 dn->data_blkaddr = NEW_ADDR;
796 __set_data_blkaddr(dn);
797 count--;
798 }
799 }
800
801 if (set_page_dirty(dn->node_page))
802 dn->node_changed = true;
803 return 0;
804}
805
806/* Should keep dn->ofs_in_node unchanged */
807int f2fs_reserve_new_block(struct dnode_of_data *dn)
808{
809 unsigned int ofs_in_node = dn->ofs_in_node;
810 int ret;
811
812 ret = f2fs_reserve_new_blocks(dn, 1);
813 dn->ofs_in_node = ofs_in_node;
814 return ret;
815}
816
817int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
818{
819 bool need_put = dn->inode_page ? false : true;
820 int err;
821
822 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
823 if (err)
824 return err;
825
826 if (dn->data_blkaddr == NULL_ADDR)
827 err = f2fs_reserve_new_block(dn);
828 if (err || need_put)
829 f2fs_put_dnode(dn);
830 return err;
831}
832
833int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
834{
835 struct extent_info ei = {0,0,0};
836 struct inode *inode = dn->inode;
837
838 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
839 dn->data_blkaddr = ei.blk + index - ei.fofs;
840 return 0;
841 }
842
843 return f2fs_reserve_block(dn, index);
844}
845
846struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
847 int op_flags, bool for_write)
848{
849 struct address_space *mapping = inode->i_mapping;
850 struct dnode_of_data dn;
851 struct page *page;
852 struct extent_info ei = {0,0,0};
853 int err;
854
855 page = f2fs_grab_cache_page(mapping, index, for_write);
856 if (!page)
857 return ERR_PTR(-ENOMEM);
858
859 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
860 dn.data_blkaddr = ei.blk + index - ei.fofs;
David Brazdil0f672f62019-12-10 10:32:29 +0000861 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr,
862 DATA_GENERIC_ENHANCE_READ)) {
863 err = -EFSCORRUPTED;
864 goto put_err;
865 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000866 goto got_it;
867 }
868
869 set_new_dnode(&dn, inode, NULL, NULL, 0);
870 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
871 if (err)
872 goto put_err;
873 f2fs_put_dnode(&dn);
874
875 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
876 err = -ENOENT;
877 goto put_err;
878 }
David Brazdil0f672f62019-12-10 10:32:29 +0000879 if (dn.data_blkaddr != NEW_ADDR &&
880 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
881 dn.data_blkaddr,
882 DATA_GENERIC_ENHANCE)) {
883 err = -EFSCORRUPTED;
884 goto put_err;
885 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000886got_it:
887 if (PageUptodate(page)) {
888 unlock_page(page);
889 return page;
890 }
891
892 /*
893 * A new dentry page is allocated but not able to be written, since its
894 * new inode page couldn't be allocated due to -ENOSPC.
895 * In such the case, its blkaddr can be remained as NEW_ADDR.
896 * see, f2fs_add_link -> f2fs_get_new_data_page ->
897 * f2fs_init_inode_metadata.
898 */
899 if (dn.data_blkaddr == NEW_ADDR) {
900 zero_user_segment(page, 0, PAGE_SIZE);
901 if (!PageUptodate(page))
902 SetPageUptodate(page);
903 unlock_page(page);
904 return page;
905 }
906
907 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
908 if (err)
909 goto put_err;
910 return page;
911
912put_err:
913 f2fs_put_page(page, 1);
914 return ERR_PTR(err);
915}
916
917struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
918{
919 struct address_space *mapping = inode->i_mapping;
920 struct page *page;
921
922 page = find_get_page(mapping, index);
923 if (page && PageUptodate(page))
924 return page;
925 f2fs_put_page(page, 0);
926
927 page = f2fs_get_read_data_page(inode, index, 0, false);
928 if (IS_ERR(page))
929 return page;
930
931 if (PageUptodate(page))
932 return page;
933
934 wait_on_page_locked(page);
935 if (unlikely(!PageUptodate(page))) {
936 f2fs_put_page(page, 0);
937 return ERR_PTR(-EIO);
938 }
939 return page;
940}
941
942/*
943 * If it tries to access a hole, return an error.
944 * Because, the callers, functions in dir.c and GC, should be able to know
945 * whether this page exists or not.
946 */
947struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
948 bool for_write)
949{
950 struct address_space *mapping = inode->i_mapping;
951 struct page *page;
952repeat:
953 page = f2fs_get_read_data_page(inode, index, 0, for_write);
954 if (IS_ERR(page))
955 return page;
956
957 /* wait for read completion */
958 lock_page(page);
959 if (unlikely(page->mapping != mapping)) {
960 f2fs_put_page(page, 1);
961 goto repeat;
962 }
963 if (unlikely(!PageUptodate(page))) {
964 f2fs_put_page(page, 1);
965 return ERR_PTR(-EIO);
966 }
967 return page;
968}
969
970/*
971 * Caller ensures that this data page is never allocated.
972 * A new zero-filled data page is allocated in the page cache.
973 *
974 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
975 * f2fs_unlock_op().
976 * Note that, ipage is set only by make_empty_dir, and if any error occur,
977 * ipage should be released by this function.
978 */
979struct page *f2fs_get_new_data_page(struct inode *inode,
980 struct page *ipage, pgoff_t index, bool new_i_size)
981{
982 struct address_space *mapping = inode->i_mapping;
983 struct page *page;
984 struct dnode_of_data dn;
985 int err;
986
987 page = f2fs_grab_cache_page(mapping, index, true);
988 if (!page) {
989 /*
990 * before exiting, we should make sure ipage will be released
991 * if any error occur.
992 */
993 f2fs_put_page(ipage, 1);
994 return ERR_PTR(-ENOMEM);
995 }
996
997 set_new_dnode(&dn, inode, ipage, NULL, 0);
998 err = f2fs_reserve_block(&dn, index);
999 if (err) {
1000 f2fs_put_page(page, 1);
1001 return ERR_PTR(err);
1002 }
1003 if (!ipage)
1004 f2fs_put_dnode(&dn);
1005
1006 if (PageUptodate(page))
1007 goto got_it;
1008
1009 if (dn.data_blkaddr == NEW_ADDR) {
1010 zero_user_segment(page, 0, PAGE_SIZE);
1011 if (!PageUptodate(page))
1012 SetPageUptodate(page);
1013 } else {
1014 f2fs_put_page(page, 1);
1015
1016 /* if ipage exists, blkaddr should be NEW_ADDR */
1017 f2fs_bug_on(F2FS_I_SB(inode), ipage);
1018 page = f2fs_get_lock_data_page(inode, index, true);
1019 if (IS_ERR(page))
1020 return page;
1021 }
1022got_it:
1023 if (new_i_size && i_size_read(inode) <
1024 ((loff_t)(index + 1) << PAGE_SHIFT))
1025 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
1026 return page;
1027}
1028
1029static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
1030{
1031 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1032 struct f2fs_summary sum;
1033 struct node_info ni;
1034 block_t old_blkaddr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001035 blkcnt_t count = 1;
1036 int err;
1037
1038 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
1039 return -EPERM;
1040
1041 err = f2fs_get_node_info(sbi, dn->nid, &ni);
1042 if (err)
1043 return err;
1044
1045 dn->data_blkaddr = datablock_addr(dn->inode,
1046 dn->node_page, dn->ofs_in_node);
David Brazdil0f672f62019-12-10 10:32:29 +00001047 if (dn->data_blkaddr != NULL_ADDR)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001048 goto alloc;
1049
1050 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
1051 return err;
1052
1053alloc:
1054 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1055 old_blkaddr = dn->data_blkaddr;
1056 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
1057 &sum, seg_type, NULL, false);
1058 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
1059 invalidate_mapping_pages(META_MAPPING(sbi),
1060 old_blkaddr, old_blkaddr);
David Brazdil0f672f62019-12-10 10:32:29 +00001061 f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001062
David Brazdil0f672f62019-12-10 10:32:29 +00001063 /*
1064 * i_size will be updated by direct_IO. Otherwise, we'll get stale
1065 * data from unwritten block via dio_read.
1066 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001067 return 0;
1068}
1069
1070int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
1071{
1072 struct inode *inode = file_inode(iocb->ki_filp);
1073 struct f2fs_map_blocks map;
1074 int flag;
1075 int err = 0;
1076 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
1077
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001078 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
1079 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
1080 if (map.m_len > map.m_lblk)
1081 map.m_len -= map.m_lblk;
1082 else
1083 map.m_len = 0;
1084
1085 map.m_next_pgofs = NULL;
1086 map.m_next_extent = NULL;
1087 map.m_seg_type = NO_CHECK_TYPE;
David Brazdil0f672f62019-12-10 10:32:29 +00001088 map.m_may_create = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001089
1090 if (direct_io) {
1091 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
David Brazdil0f672f62019-12-10 10:32:29 +00001092 flag = f2fs_force_buffered_io(inode, iocb, from) ?
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001093 F2FS_GET_BLOCK_PRE_AIO :
1094 F2FS_GET_BLOCK_PRE_DIO;
1095 goto map_blocks;
1096 }
1097 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
1098 err = f2fs_convert_inline_inode(inode);
1099 if (err)
1100 return err;
1101 }
1102 if (f2fs_has_inline_data(inode))
1103 return err;
1104
1105 flag = F2FS_GET_BLOCK_PRE_AIO;
1106
1107map_blocks:
1108 err = f2fs_map_blocks(inode, &map, 1, flag);
1109 if (map.m_len > 0 && err == -ENOSPC) {
1110 if (!direct_io)
1111 set_inode_flag(inode, FI_NO_PREALLOC);
1112 err = 0;
1113 }
1114 return err;
1115}
1116
David Brazdil0f672f62019-12-10 10:32:29 +00001117void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001118{
1119 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1120 if (lock)
1121 down_read(&sbi->node_change);
1122 else
1123 up_read(&sbi->node_change);
1124 } else {
1125 if (lock)
1126 f2fs_lock_op(sbi);
1127 else
1128 f2fs_unlock_op(sbi);
1129 }
1130}
1131
1132/*
1133 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1134 * f2fs_map_blocks structure.
1135 * If original data blocks are allocated, then give them to blockdev.
1136 * Otherwise,
1137 * a. preallocate requested block addresses
1138 * b. do not use extent cache for better performance
1139 * c. give the block addresses to blockdev
1140 */
1141int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1142 int create, int flag)
1143{
1144 unsigned int maxblocks = map->m_len;
1145 struct dnode_of_data dn;
1146 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00001147 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001148 pgoff_t pgofs, end_offset, end;
1149 int err = 0, ofs = 1;
1150 unsigned int ofs_in_node, last_ofs_in_node;
1151 blkcnt_t prealloc;
1152 struct extent_info ei = {0,0,0};
1153 block_t blkaddr;
1154 unsigned int start_pgofs;
1155
1156 if (!maxblocks)
1157 return 0;
1158
1159 map->m_len = 0;
1160 map->m_flags = 0;
1161
1162 /* it only supports block size == page size */
1163 pgofs = (pgoff_t)map->m_lblk;
1164 end = pgofs + maxblocks;
1165
1166 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001167 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1168 map->m_may_create)
1169 goto next_dnode;
1170
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001171 map->m_pblk = ei.blk + pgofs - ei.fofs;
1172 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1173 map->m_flags = F2FS_MAP_MAPPED;
1174 if (map->m_next_extent)
1175 *map->m_next_extent = pgofs + map->m_len;
David Brazdil0f672f62019-12-10 10:32:29 +00001176
1177 /* for hardware encryption, but to avoid potential issue in future */
1178 if (flag == F2FS_GET_BLOCK_DIO)
1179 f2fs_wait_on_block_writeback_range(inode,
1180 map->m_pblk, map->m_len);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001181 goto out;
1182 }
1183
1184next_dnode:
David Brazdil0f672f62019-12-10 10:32:29 +00001185 if (map->m_may_create)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001186 __do_map_lock(sbi, flag, true);
1187
1188 /* When reading holes, we need its node page */
1189 set_new_dnode(&dn, inode, NULL, NULL, 0);
1190 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
1191 if (err) {
1192 if (flag == F2FS_GET_BLOCK_BMAP)
1193 map->m_pblk = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02001194
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001195 if (err == -ENOENT) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001196 /*
1197 * There is one exceptional case that read_node_page()
1198 * may return -ENOENT due to filesystem has been
1199 * shutdown or cp_error, so force to convert error
1200 * number to EIO for such case.
1201 */
1202 if (map->m_may_create &&
1203 (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
1204 f2fs_cp_error(sbi))) {
1205 err = -EIO;
1206 goto unlock_out;
1207 }
1208
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001209 err = 0;
1210 if (map->m_next_pgofs)
1211 *map->m_next_pgofs =
1212 f2fs_get_next_page_offset(&dn, pgofs);
1213 if (map->m_next_extent)
1214 *map->m_next_extent =
1215 f2fs_get_next_page_offset(&dn, pgofs);
1216 }
1217 goto unlock_out;
1218 }
1219
1220 start_pgofs = pgofs;
1221 prealloc = 0;
1222 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
1223 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1224
1225next_block:
1226 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
1227
1228 if (__is_valid_data_blkaddr(blkaddr) &&
David Brazdil0f672f62019-12-10 10:32:29 +00001229 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) {
1230 err = -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001231 goto sync_out;
1232 }
1233
David Brazdil0f672f62019-12-10 10:32:29 +00001234 if (__is_valid_data_blkaddr(blkaddr)) {
1235 /* use out-place-update for driect IO under LFS mode */
1236 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1237 map->m_may_create) {
1238 err = __allocate_data_block(&dn, map->m_seg_type);
1239 if (err)
1240 goto sync_out;
1241 blkaddr = dn.data_blkaddr;
1242 set_inode_flag(inode, FI_APPEND_WRITE);
1243 }
1244 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001245 if (create) {
1246 if (unlikely(f2fs_cp_error(sbi))) {
1247 err = -EIO;
1248 goto sync_out;
1249 }
1250 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
1251 if (blkaddr == NULL_ADDR) {
1252 prealloc++;
1253 last_ofs_in_node = dn.ofs_in_node;
1254 }
1255 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00001256 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1257 flag != F2FS_GET_BLOCK_DIO);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001258 err = __allocate_data_block(&dn,
1259 map->m_seg_type);
1260 if (!err)
1261 set_inode_flag(inode, FI_APPEND_WRITE);
1262 }
1263 if (err)
1264 goto sync_out;
1265 map->m_flags |= F2FS_MAP_NEW;
1266 blkaddr = dn.data_blkaddr;
1267 } else {
1268 if (flag == F2FS_GET_BLOCK_BMAP) {
1269 map->m_pblk = 0;
1270 goto sync_out;
1271 }
1272 if (flag == F2FS_GET_BLOCK_PRECACHE)
1273 goto sync_out;
1274 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1275 blkaddr == NULL_ADDR) {
1276 if (map->m_next_pgofs)
1277 *map->m_next_pgofs = pgofs + 1;
1278 goto sync_out;
1279 }
1280 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1281 /* for defragment case */
1282 if (map->m_next_pgofs)
1283 *map->m_next_pgofs = pgofs + 1;
1284 goto sync_out;
1285 }
1286 }
1287 }
1288
1289 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1290 goto skip;
1291
1292 if (map->m_len == 0) {
1293 /* preallocated unwritten block should be mapped for fiemap. */
1294 if (blkaddr == NEW_ADDR)
1295 map->m_flags |= F2FS_MAP_UNWRITTEN;
1296 map->m_flags |= F2FS_MAP_MAPPED;
1297
1298 map->m_pblk = blkaddr;
1299 map->m_len = 1;
1300 } else if ((map->m_pblk != NEW_ADDR &&
1301 blkaddr == (map->m_pblk + ofs)) ||
1302 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
1303 flag == F2FS_GET_BLOCK_PRE_DIO) {
1304 ofs++;
1305 map->m_len++;
1306 } else {
1307 goto sync_out;
1308 }
1309
1310skip:
1311 dn.ofs_in_node++;
1312 pgofs++;
1313
1314 /* preallocate blocks in batch for one dnode page */
1315 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1316 (pgofs == end || dn.ofs_in_node == end_offset)) {
1317
1318 dn.ofs_in_node = ofs_in_node;
1319 err = f2fs_reserve_new_blocks(&dn, prealloc);
1320 if (err)
1321 goto sync_out;
1322
1323 map->m_len += dn.ofs_in_node - ofs_in_node;
1324 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1325 err = -ENOSPC;
1326 goto sync_out;
1327 }
1328 dn.ofs_in_node = end_offset;
1329 }
1330
1331 if (pgofs >= end)
1332 goto sync_out;
1333 else if (dn.ofs_in_node < end_offset)
1334 goto next_block;
1335
1336 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1337 if (map->m_flags & F2FS_MAP_MAPPED) {
1338 unsigned int ofs = start_pgofs - map->m_lblk;
1339
1340 f2fs_update_extent_cache_range(&dn,
1341 start_pgofs, map->m_pblk + ofs,
1342 map->m_len - ofs);
1343 }
1344 }
1345
1346 f2fs_put_dnode(&dn);
1347
David Brazdil0f672f62019-12-10 10:32:29 +00001348 if (map->m_may_create) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001349 __do_map_lock(sbi, flag, false);
1350 f2fs_balance_fs(sbi, dn.node_changed);
1351 }
1352 goto next_dnode;
1353
1354sync_out:
David Brazdil0f672f62019-12-10 10:32:29 +00001355
1356 /* for hardware encryption, but to avoid potential issue in future */
1357 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1358 f2fs_wait_on_block_writeback_range(inode,
1359 map->m_pblk, map->m_len);
1360
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001361 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1362 if (map->m_flags & F2FS_MAP_MAPPED) {
1363 unsigned int ofs = start_pgofs - map->m_lblk;
1364
1365 f2fs_update_extent_cache_range(&dn,
1366 start_pgofs, map->m_pblk + ofs,
1367 map->m_len - ofs);
1368 }
1369 if (map->m_next_extent)
1370 *map->m_next_extent = pgofs + 1;
1371 }
1372 f2fs_put_dnode(&dn);
1373unlock_out:
David Brazdil0f672f62019-12-10 10:32:29 +00001374 if (map->m_may_create) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001375 __do_map_lock(sbi, flag, false);
1376 f2fs_balance_fs(sbi, dn.node_changed);
1377 }
1378out:
1379 trace_f2fs_map_blocks(inode, map, err);
1380 return err;
1381}
1382
1383bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1384{
1385 struct f2fs_map_blocks map;
1386 block_t last_lblk;
1387 int err;
1388
1389 if (pos + len > i_size_read(inode))
1390 return false;
1391
1392 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1393 map.m_next_pgofs = NULL;
1394 map.m_next_extent = NULL;
1395 map.m_seg_type = NO_CHECK_TYPE;
David Brazdil0f672f62019-12-10 10:32:29 +00001396 map.m_may_create = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001397 last_lblk = F2FS_BLK_ALIGN(pos + len);
1398
1399 while (map.m_lblk < last_lblk) {
1400 map.m_len = last_lblk - map.m_lblk;
1401 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1402 if (err || map.m_len == 0)
1403 return false;
1404 map.m_lblk += map.m_len;
1405 }
1406 return true;
1407}
1408
1409static int __get_data_block(struct inode *inode, sector_t iblock,
1410 struct buffer_head *bh, int create, int flag,
David Brazdil0f672f62019-12-10 10:32:29 +00001411 pgoff_t *next_pgofs, int seg_type, bool may_write)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001412{
1413 struct f2fs_map_blocks map;
1414 int err;
1415
1416 map.m_lblk = iblock;
1417 map.m_len = bh->b_size >> inode->i_blkbits;
1418 map.m_next_pgofs = next_pgofs;
1419 map.m_next_extent = NULL;
1420 map.m_seg_type = seg_type;
David Brazdil0f672f62019-12-10 10:32:29 +00001421 map.m_may_create = may_write;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001422
1423 err = f2fs_map_blocks(inode, &map, create, flag);
1424 if (!err) {
1425 map_bh(bh, inode->i_sb, map.m_pblk);
1426 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1427 bh->b_size = (u64)map.m_len << inode->i_blkbits;
1428 }
1429 return err;
1430}
1431
1432static int get_data_block(struct inode *inode, sector_t iblock,
1433 struct buffer_head *bh_result, int create, int flag,
1434 pgoff_t *next_pgofs)
1435{
1436 return __get_data_block(inode, iblock, bh_result, create,
1437 flag, next_pgofs,
David Brazdil0f672f62019-12-10 10:32:29 +00001438 NO_CHECK_TYPE, create);
1439}
1440
1441static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1442 struct buffer_head *bh_result, int create)
1443{
1444 return __get_data_block(inode, iblock, bh_result, create,
1445 F2FS_GET_BLOCK_DIO, NULL,
1446 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1447 IS_SWAPFILE(inode) ? false : true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001448}
1449
1450static int get_data_block_dio(struct inode *inode, sector_t iblock,
1451 struct buffer_head *bh_result, int create)
1452{
1453 return __get_data_block(inode, iblock, bh_result, create,
David Brazdil0f672f62019-12-10 10:32:29 +00001454 F2FS_GET_BLOCK_DIO, NULL,
1455 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1456 false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001457}
1458
1459static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1460 struct buffer_head *bh_result, int create)
1461{
1462 /* Block number less than F2FS MAX BLOCKS */
1463 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
1464 return -EFBIG;
1465
1466 return __get_data_block(inode, iblock, bh_result, create,
1467 F2FS_GET_BLOCK_BMAP, NULL,
David Brazdil0f672f62019-12-10 10:32:29 +00001468 NO_CHECK_TYPE, create);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001469}
1470
1471static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1472{
1473 return (offset >> inode->i_blkbits);
1474}
1475
1476static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1477{
1478 return (blk << inode->i_blkbits);
1479}
1480
1481static int f2fs_xattr_fiemap(struct inode *inode,
1482 struct fiemap_extent_info *fieinfo)
1483{
1484 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1485 struct page *page;
1486 struct node_info ni;
1487 __u64 phys = 0, len;
1488 __u32 flags;
1489 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1490 int err = 0;
1491
1492 if (f2fs_has_inline_xattr(inode)) {
1493 int offset;
1494
1495 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1496 inode->i_ino, false);
1497 if (!page)
1498 return -ENOMEM;
1499
1500 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1501 if (err) {
1502 f2fs_put_page(page, 1);
1503 return err;
1504 }
1505
1506 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1507 offset = offsetof(struct f2fs_inode, i_addr) +
1508 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1509 get_inline_xattr_addrs(inode));
1510
1511 phys += offset;
1512 len = inline_xattr_size(inode);
1513
1514 f2fs_put_page(page, 1);
1515
1516 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1517
1518 if (!xnid)
1519 flags |= FIEMAP_EXTENT_LAST;
1520
1521 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1522 if (err || err == 1)
1523 return err;
1524 }
1525
1526 if (xnid) {
1527 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1528 if (!page)
1529 return -ENOMEM;
1530
1531 err = f2fs_get_node_info(sbi, xnid, &ni);
1532 if (err) {
1533 f2fs_put_page(page, 1);
1534 return err;
1535 }
1536
1537 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1538 len = inode->i_sb->s_blocksize;
1539
1540 f2fs_put_page(page, 1);
1541
1542 flags = FIEMAP_EXTENT_LAST;
1543 }
1544
1545 if (phys)
1546 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1547
1548 return (err < 0 ? err : 0);
1549}
1550
1551int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1552 u64 start, u64 len)
1553{
1554 struct buffer_head map_bh;
1555 sector_t start_blk, last_blk;
1556 pgoff_t next_pgofs;
1557 u64 logical = 0, phys = 0, size = 0;
1558 u32 flags = 0;
1559 int ret = 0;
1560
1561 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1562 ret = f2fs_precache_extents(inode);
1563 if (ret)
1564 return ret;
1565 }
1566
1567 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
1568 if (ret)
1569 return ret;
1570
1571 inode_lock(inode);
1572
1573 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1574 ret = f2fs_xattr_fiemap(inode, fieinfo);
1575 goto out;
1576 }
1577
David Brazdil0f672f62019-12-10 10:32:29 +00001578 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001579 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1580 if (ret != -EAGAIN)
1581 goto out;
1582 }
1583
1584 if (logical_to_blk(inode, len) == 0)
1585 len = blk_to_logical(inode, 1);
1586
1587 start_blk = logical_to_blk(inode, start);
1588 last_blk = logical_to_blk(inode, start + len - 1);
1589
1590next:
1591 memset(&map_bh, 0, sizeof(struct buffer_head));
1592 map_bh.b_size = len;
1593
1594 ret = get_data_block(inode, start_blk, &map_bh, 0,
1595 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
1596 if (ret)
1597 goto out;
1598
1599 /* HOLE */
1600 if (!buffer_mapped(&map_bh)) {
1601 start_blk = next_pgofs;
1602
1603 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1604 F2FS_I_SB(inode)->max_file_blocks))
1605 goto prep_next;
1606
1607 flags |= FIEMAP_EXTENT_LAST;
1608 }
1609
1610 if (size) {
David Brazdil0f672f62019-12-10 10:32:29 +00001611 if (IS_ENCRYPTED(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001612 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1613
1614 ret = fiemap_fill_next_extent(fieinfo, logical,
1615 phys, size, flags);
1616 }
1617
1618 if (start_blk > last_blk || ret)
1619 goto out;
1620
1621 logical = blk_to_logical(inode, start_blk);
1622 phys = blk_to_logical(inode, map_bh.b_blocknr);
1623 size = map_bh.b_size;
1624 flags = 0;
1625 if (buffer_unwritten(&map_bh))
1626 flags = FIEMAP_EXTENT_UNWRITTEN;
1627
1628 start_blk += logical_to_blk(inode, size);
1629
1630prep_next:
1631 cond_resched();
1632 if (fatal_signal_pending(current))
1633 ret = -EINTR;
1634 else
1635 goto next;
1636out:
1637 if (ret == 1)
1638 ret = 0;
1639
1640 inode_unlock(inode);
1641 return ret;
1642}
1643
David Brazdil0f672f62019-12-10 10:32:29 +00001644static inline loff_t f2fs_readpage_limit(struct inode *inode)
1645{
1646 if (IS_ENABLED(CONFIG_FS_VERITY) &&
1647 (IS_VERITY(inode) || f2fs_verity_in_progress(inode)))
1648 return inode->i_sb->s_maxbytes;
1649
1650 return i_size_read(inode);
1651}
1652
1653static int f2fs_read_single_page(struct inode *inode, struct page *page,
1654 unsigned nr_pages,
1655 struct f2fs_map_blocks *map,
1656 struct bio **bio_ret,
1657 sector_t *last_block_in_bio,
1658 bool is_readahead)
1659{
1660 struct bio *bio = *bio_ret;
1661 const unsigned blkbits = inode->i_blkbits;
1662 const unsigned blocksize = 1 << blkbits;
1663 sector_t block_in_file;
1664 sector_t last_block;
1665 sector_t last_block_in_file;
1666 sector_t block_nr;
1667 int ret = 0;
1668
1669 block_in_file = (sector_t)page_index(page);
1670 last_block = block_in_file + nr_pages;
1671 last_block_in_file = (f2fs_readpage_limit(inode) + blocksize - 1) >>
1672 blkbits;
1673 if (last_block > last_block_in_file)
1674 last_block = last_block_in_file;
1675
1676 /* just zeroing out page which is beyond EOF */
1677 if (block_in_file >= last_block)
1678 goto zero_out;
1679 /*
1680 * Map blocks using the previous result first.
1681 */
1682 if ((map->m_flags & F2FS_MAP_MAPPED) &&
1683 block_in_file > map->m_lblk &&
1684 block_in_file < (map->m_lblk + map->m_len))
1685 goto got_it;
1686
1687 /*
1688 * Then do more f2fs_map_blocks() calls until we are
1689 * done with this page.
1690 */
1691 map->m_lblk = block_in_file;
1692 map->m_len = last_block - block_in_file;
1693
1694 ret = f2fs_map_blocks(inode, map, 0, F2FS_GET_BLOCK_DEFAULT);
1695 if (ret)
1696 goto out;
1697got_it:
1698 if ((map->m_flags & F2FS_MAP_MAPPED)) {
1699 block_nr = map->m_pblk + block_in_file - map->m_lblk;
1700 SetPageMappedToDisk(page);
1701
1702 if (!PageUptodate(page) && (!PageSwapCache(page) &&
1703 !cleancache_get_page(page))) {
1704 SetPageUptodate(page);
1705 goto confused;
1706 }
1707
1708 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1709 DATA_GENERIC_ENHANCE_READ)) {
1710 ret = -EFSCORRUPTED;
1711 goto out;
1712 }
1713 } else {
1714zero_out:
1715 zero_user_segment(page, 0, PAGE_SIZE);
1716 if (f2fs_need_verity(inode, page->index) &&
1717 !fsverity_verify_page(page)) {
1718 ret = -EIO;
1719 goto out;
1720 }
1721 if (!PageUptodate(page))
1722 SetPageUptodate(page);
1723 unlock_page(page);
1724 goto out;
1725 }
1726
1727 /*
1728 * This page will go to BIO. Do we need to send this
1729 * BIO off first?
1730 */
1731 if (bio && !page_is_mergeable(F2FS_I_SB(inode), bio,
1732 *last_block_in_bio, block_nr)) {
1733submit_and_realloc:
1734 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1735 bio = NULL;
1736 }
1737 if (bio == NULL) {
1738 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
1739 is_readahead ? REQ_RAHEAD : 0, page->index);
1740 if (IS_ERR(bio)) {
1741 ret = PTR_ERR(bio);
1742 bio = NULL;
1743 goto out;
1744 }
1745 }
1746
1747 /*
1748 * If the page is under writeback, we need to wait for
1749 * its completion to see the correct decrypted data.
1750 */
1751 f2fs_wait_on_block_writeback(inode, block_nr);
1752
1753 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1754 goto submit_and_realloc;
1755
1756 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
1757 ClearPageError(page);
1758 *last_block_in_bio = block_nr;
1759 goto out;
1760confused:
1761 if (bio) {
1762 __submit_bio(F2FS_I_SB(inode), bio, DATA);
1763 bio = NULL;
1764 }
1765 unlock_page(page);
1766out:
1767 *bio_ret = bio;
1768 return ret;
1769}
1770
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001771/*
1772 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1773 * Major change was from block_size == page_size in f2fs by default.
1774 *
1775 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1776 * this function ever deviates from doing just read-ahead, it should either
1777 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1778 * from read-ahead.
1779 */
1780static int f2fs_mpage_readpages(struct address_space *mapping,
1781 struct list_head *pages, struct page *page,
1782 unsigned nr_pages, bool is_readahead)
1783{
1784 struct bio *bio = NULL;
1785 sector_t last_block_in_bio = 0;
1786 struct inode *inode = mapping->host;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001787 struct f2fs_map_blocks map;
David Brazdil0f672f62019-12-10 10:32:29 +00001788 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001789
1790 map.m_pblk = 0;
1791 map.m_lblk = 0;
1792 map.m_len = 0;
1793 map.m_flags = 0;
1794 map.m_next_pgofs = NULL;
1795 map.m_next_extent = NULL;
1796 map.m_seg_type = NO_CHECK_TYPE;
David Brazdil0f672f62019-12-10 10:32:29 +00001797 map.m_may_create = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001798
1799 for (; nr_pages; nr_pages--) {
1800 if (pages) {
1801 page = list_last_entry(pages, struct page, lru);
1802
1803 prefetchw(&page->flags);
1804 list_del(&page->lru);
1805 if (add_to_page_cache_lru(page, mapping,
David Brazdil0f672f62019-12-10 10:32:29 +00001806 page_index(page),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001807 readahead_gfp_mask(mapping)))
1808 goto next_page;
1809 }
1810
David Brazdil0f672f62019-12-10 10:32:29 +00001811 ret = f2fs_read_single_page(inode, page, nr_pages, &map, &bio,
1812 &last_block_in_bio, is_readahead);
1813 if (ret) {
1814 SetPageError(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001815 zero_user_segment(page, 0, PAGE_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001816 unlock_page(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001817 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001818next_page:
1819 if (pages)
1820 put_page(page);
1821 }
1822 BUG_ON(pages && !list_empty(pages));
1823 if (bio)
1824 __submit_bio(F2FS_I_SB(inode), bio, DATA);
David Brazdil0f672f62019-12-10 10:32:29 +00001825 return pages ? 0 : ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001826}
1827
1828static int f2fs_read_data_page(struct file *file, struct page *page)
1829{
David Brazdil0f672f62019-12-10 10:32:29 +00001830 struct inode *inode = page_file_mapping(page)->host;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001831 int ret = -EAGAIN;
1832
1833 trace_f2fs_readpage(page, DATA);
1834
1835 /* If the file has inline data, try to read it directly */
1836 if (f2fs_has_inline_data(inode))
1837 ret = f2fs_read_inline_data(inode, page);
1838 if (ret == -EAGAIN)
David Brazdil0f672f62019-12-10 10:32:29 +00001839 ret = f2fs_mpage_readpages(page_file_mapping(page),
1840 NULL, page, 1, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001841 return ret;
1842}
1843
1844static int f2fs_read_data_pages(struct file *file,
1845 struct address_space *mapping,
1846 struct list_head *pages, unsigned nr_pages)
1847{
1848 struct inode *inode = mapping->host;
1849 struct page *page = list_last_entry(pages, struct page, lru);
1850
1851 trace_f2fs_readpages(inode, page, nr_pages);
1852
1853 /* If the file has inline data, skip readpages */
1854 if (f2fs_has_inline_data(inode))
1855 return 0;
1856
1857 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
1858}
1859
1860static int encrypt_one_page(struct f2fs_io_info *fio)
1861{
1862 struct inode *inode = fio->page->mapping->host;
1863 struct page *mpage;
1864 gfp_t gfp_flags = GFP_NOFS;
1865
1866 if (!f2fs_encrypted_file(inode))
1867 return 0;
1868
1869 /* wait for GCed page writeback via META_MAPPING */
David Brazdil0f672f62019-12-10 10:32:29 +00001870 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001871
1872retry_encrypt:
David Brazdil0f672f62019-12-10 10:32:29 +00001873 fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(fio->page,
1874 PAGE_SIZE, 0,
1875 gfp_flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001876 if (IS_ERR(fio->encrypted_page)) {
1877 /* flush pending IOs and wait for a while in the ENOMEM case */
1878 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
1879 f2fs_flush_merged_writes(fio->sbi);
1880 congestion_wait(BLK_RW_ASYNC, HZ/50);
1881 gfp_flags |= __GFP_NOFAIL;
1882 goto retry_encrypt;
1883 }
1884 return PTR_ERR(fio->encrypted_page);
1885 }
1886
1887 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
1888 if (mpage) {
1889 if (PageUptodate(mpage))
1890 memcpy(page_address(mpage),
1891 page_address(fio->encrypted_page), PAGE_SIZE);
1892 f2fs_put_page(mpage, 1);
1893 }
1894 return 0;
1895}
1896
1897static inline bool check_inplace_update_policy(struct inode *inode,
1898 struct f2fs_io_info *fio)
1899{
1900 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1901 unsigned int policy = SM_I(sbi)->ipu_policy;
1902
1903 if (policy & (0x1 << F2FS_IPU_FORCE))
1904 return true;
1905 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
1906 return true;
1907 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1908 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1909 return true;
1910 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
1911 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1912 return true;
1913
1914 /*
1915 * IPU for rewrite async pages
1916 */
1917 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1918 fio && fio->op == REQ_OP_WRITE &&
1919 !(fio->op_flags & REQ_SYNC) &&
David Brazdil0f672f62019-12-10 10:32:29 +00001920 !IS_ENCRYPTED(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001921 return true;
1922
1923 /* this is only set during fdatasync */
1924 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1925 is_inode_flag_set(inode, FI_NEED_IPU))
1926 return true;
1927
David Brazdil0f672f62019-12-10 10:32:29 +00001928 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1929 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1930 return true;
1931
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001932 return false;
1933}
1934
1935bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
1936{
1937 if (f2fs_is_pinned_file(inode))
1938 return true;
1939
1940 /* if this is cold file, we should overwrite to avoid fragmentation */
1941 if (file_is_cold(inode))
1942 return true;
1943
1944 return check_inplace_update_policy(inode, fio);
1945}
1946
1947bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
1948{
1949 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1950
1951 if (test_opt(sbi, LFS))
1952 return true;
1953 if (S_ISDIR(inode->i_mode))
1954 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00001955 if (IS_NOQUOTA(inode))
1956 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001957 if (f2fs_is_atomic_file(inode))
1958 return true;
1959 if (fio) {
1960 if (is_cold_data(fio->page))
1961 return true;
1962 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1963 return true;
David Brazdil0f672f62019-12-10 10:32:29 +00001964 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1965 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1966 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001967 }
1968 return false;
1969}
1970
1971static inline bool need_inplace_update(struct f2fs_io_info *fio)
1972{
1973 struct inode *inode = fio->page->mapping->host;
1974
1975 if (f2fs_should_update_outplace(inode, fio))
1976 return false;
1977
1978 return f2fs_should_update_inplace(inode, fio);
1979}
1980
1981int f2fs_do_write_data_page(struct f2fs_io_info *fio)
1982{
1983 struct page *page = fio->page;
1984 struct inode *inode = page->mapping->host;
1985 struct dnode_of_data dn;
1986 struct extent_info ei = {0,0,0};
1987 struct node_info ni;
1988 bool ipu_force = false;
1989 int err = 0;
1990
1991 set_new_dnode(&dn, inode, NULL, NULL, 0);
1992 if (need_inplace_update(fio) &&
1993 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1994 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
1995
1996 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
David Brazdil0f672f62019-12-10 10:32:29 +00001997 DATA_GENERIC_ENHANCE))
1998 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001999
2000 ipu_force = true;
2001 fio->need_lock = LOCK_DONE;
2002 goto got_it;
2003 }
2004
2005 /* Deadlock due to between page->lock and f2fs_lock_op */
2006 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
2007 return -EAGAIN;
2008
2009 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
2010 if (err)
2011 goto out;
2012
2013 fio->old_blkaddr = dn.data_blkaddr;
2014
2015 /* This page is already truncated */
2016 if (fio->old_blkaddr == NULL_ADDR) {
2017 ClearPageUptodate(page);
David Brazdil0f672f62019-12-10 10:32:29 +00002018 clear_cold_data(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002019 goto out_writepage;
2020 }
2021got_it:
2022 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
2023 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
David Brazdil0f672f62019-12-10 10:32:29 +00002024 DATA_GENERIC_ENHANCE)) {
2025 err = -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002026 goto out_writepage;
2027 }
2028 /*
2029 * If current allocation needs SSR,
2030 * it had better in-place writes for updated data.
2031 */
David Brazdil0f672f62019-12-10 10:32:29 +00002032 if (ipu_force ||
2033 (__is_valid_data_blkaddr(fio->old_blkaddr) &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002034 need_inplace_update(fio))) {
2035 err = encrypt_one_page(fio);
2036 if (err)
2037 goto out_writepage;
2038
2039 set_page_writeback(page);
2040 ClearPageError(page);
2041 f2fs_put_dnode(&dn);
2042 if (fio->need_lock == LOCK_REQ)
2043 f2fs_unlock_op(fio->sbi);
2044 err = f2fs_inplace_write_data(fio);
David Brazdil0f672f62019-12-10 10:32:29 +00002045 if (err) {
2046 if (f2fs_encrypted_file(inode))
2047 fscrypt_finalize_bounce_page(&fio->encrypted_page);
2048 if (PageWriteback(page))
2049 end_page_writeback(page);
2050 } else {
2051 set_inode_flag(inode, FI_UPDATE_WRITE);
2052 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002053 trace_f2fs_do_write_data_page(fio->page, IPU);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002054 return err;
2055 }
2056
2057 if (fio->need_lock == LOCK_RETRY) {
2058 if (!f2fs_trylock_op(fio->sbi)) {
2059 err = -EAGAIN;
2060 goto out_writepage;
2061 }
2062 fio->need_lock = LOCK_REQ;
2063 }
2064
2065 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
2066 if (err)
2067 goto out_writepage;
2068
2069 fio->version = ni.version;
2070
2071 err = encrypt_one_page(fio);
2072 if (err)
2073 goto out_writepage;
2074
2075 set_page_writeback(page);
2076 ClearPageError(page);
2077
2078 /* LFS mode write path */
2079 f2fs_outplace_write_data(&dn, fio);
2080 trace_f2fs_do_write_data_page(page, OPU);
2081 set_inode_flag(inode, FI_APPEND_WRITE);
2082 if (page->index == 0)
2083 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
2084out_writepage:
2085 f2fs_put_dnode(&dn);
2086out:
2087 if (fio->need_lock == LOCK_REQ)
2088 f2fs_unlock_op(fio->sbi);
2089 return err;
2090}
2091
2092static int __write_data_page(struct page *page, bool *submitted,
David Brazdil0f672f62019-12-10 10:32:29 +00002093 struct bio **bio,
2094 sector_t *last_block,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002095 struct writeback_control *wbc,
2096 enum iostat_type io_type)
2097{
2098 struct inode *inode = page->mapping->host;
2099 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2100 loff_t i_size = i_size_read(inode);
2101 const pgoff_t end_index = ((unsigned long long) i_size)
2102 >> PAGE_SHIFT;
Olivier Deprez0e641232021-09-23 10:07:05 +02002103 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002104 unsigned offset = 0;
2105 bool need_balance_fs = false;
2106 int err = 0;
2107 struct f2fs_io_info fio = {
2108 .sbi = sbi,
2109 .ino = inode->i_ino,
2110 .type = DATA,
2111 .op = REQ_OP_WRITE,
2112 .op_flags = wbc_to_write_flags(wbc),
2113 .old_blkaddr = NULL_ADDR,
2114 .page = page,
2115 .encrypted_page = NULL,
2116 .submitted = false,
2117 .need_lock = LOCK_RETRY,
2118 .io_type = io_type,
2119 .io_wbc = wbc,
David Brazdil0f672f62019-12-10 10:32:29 +00002120 .bio = bio,
2121 .last_block = last_block,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002122 };
2123
2124 trace_f2fs_writepage(page, DATA);
2125
2126 /* we should bypass data pages to proceed the kworkder jobs */
2127 if (unlikely(f2fs_cp_error(sbi))) {
2128 mapping_set_error(page->mapping, -EIO);
2129 /*
2130 * don't drop any dirty dentry pages for keeping lastest
2131 * directory structure.
2132 */
2133 if (S_ISDIR(inode->i_mode))
2134 goto redirty_out;
2135 goto out;
2136 }
2137
2138 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2139 goto redirty_out;
2140
David Brazdil0f672f62019-12-10 10:32:29 +00002141 if (page->index < end_index || f2fs_verity_in_progress(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002142 goto write;
2143
2144 /*
2145 * If the offset is out-of-range of file size,
2146 * this page does not have to be written to disk.
2147 */
2148 offset = i_size & (PAGE_SIZE - 1);
2149 if ((page->index >= end_index + 1) || !offset)
2150 goto out;
2151
2152 zero_user_segment(page, offset, PAGE_SIZE);
2153write:
2154 if (f2fs_is_drop_cache(inode))
2155 goto out;
2156 /* we should not write 0'th page having journal header */
2157 if (f2fs_is_volatile_file(inode) && (!page->index ||
2158 (!wbc->for_reclaim &&
2159 f2fs_available_free_memory(sbi, BASE_CHECK))))
2160 goto redirty_out;
2161
2162 /* Dentry blocks are controlled by checkpoint */
2163 if (S_ISDIR(inode->i_mode)) {
2164 fio.need_lock = LOCK_DONE;
2165 err = f2fs_do_write_data_page(&fio);
2166 goto done;
2167 }
2168
2169 if (!wbc->for_reclaim)
2170 need_balance_fs = true;
2171 else if (has_not_enough_free_secs(sbi, 0, 0))
2172 goto redirty_out;
2173 else
2174 set_inode_flag(inode, FI_HOT_DATA);
2175
2176 err = -EAGAIN;
2177 if (f2fs_has_inline_data(inode)) {
2178 err = f2fs_write_inline_data(inode, page);
2179 if (!err)
2180 goto out;
2181 }
2182
2183 if (err == -EAGAIN) {
2184 err = f2fs_do_write_data_page(&fio);
2185 if (err == -EAGAIN) {
2186 fio.need_lock = LOCK_REQ;
2187 err = f2fs_do_write_data_page(&fio);
2188 }
2189 }
2190
2191 if (err) {
2192 file_set_keep_isize(inode);
2193 } else {
2194 down_write(&F2FS_I(inode)->i_sem);
2195 if (F2FS_I(inode)->last_disk_size < psize)
2196 F2FS_I(inode)->last_disk_size = psize;
2197 up_write(&F2FS_I(inode)->i_sem);
2198 }
2199
2200done:
2201 if (err && err != -ENOENT)
2202 goto redirty_out;
2203
2204out:
2205 inode_dec_dirty_pages(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00002206 if (err) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002207 ClearPageUptodate(page);
David Brazdil0f672f62019-12-10 10:32:29 +00002208 clear_cold_data(page);
2209 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002210
2211 if (wbc->for_reclaim) {
David Brazdil0f672f62019-12-10 10:32:29 +00002212 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002213 clear_inode_flag(inode, FI_HOT_DATA);
2214 f2fs_remove_dirty_inode(inode);
2215 submitted = NULL;
2216 }
2217
2218 unlock_page(page);
David Brazdil0f672f62019-12-10 10:32:29 +00002219 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) &&
2220 !F2FS_I(inode)->cp_task) {
2221 f2fs_submit_ipu_bio(sbi, bio, page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002222 f2fs_balance_fs(sbi, need_balance_fs);
David Brazdil0f672f62019-12-10 10:32:29 +00002223 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002224
2225 if (unlikely(f2fs_cp_error(sbi))) {
David Brazdil0f672f62019-12-10 10:32:29 +00002226 f2fs_submit_ipu_bio(sbi, bio, page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002227 f2fs_submit_merged_write(sbi, DATA);
2228 submitted = NULL;
2229 }
2230
2231 if (submitted)
2232 *submitted = fio.submitted;
2233
2234 return 0;
2235
2236redirty_out:
2237 redirty_page_for_writepage(wbc, page);
2238 /*
2239 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2240 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2241 * file_write_and_wait_range() will see EIO error, which is critical
2242 * to return value of fsync() followed by atomic_write failure to user.
2243 */
2244 if (!err || wbc->for_reclaim)
2245 return AOP_WRITEPAGE_ACTIVATE;
2246 unlock_page(page);
2247 return err;
2248}
2249
2250static int f2fs_write_data_page(struct page *page,
2251 struct writeback_control *wbc)
2252{
David Brazdil0f672f62019-12-10 10:32:29 +00002253 return __write_data_page(page, NULL, NULL, NULL, wbc, FS_DATA_IO);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002254}
2255
2256/*
2257 * This function was copied from write_cche_pages from mm/page-writeback.c.
2258 * The major change is making write step of cold data page separately from
2259 * warm/hot data page.
2260 */
2261static int f2fs_write_cache_pages(struct address_space *mapping,
2262 struct writeback_control *wbc,
2263 enum iostat_type io_type)
2264{
2265 int ret = 0;
2266 int done = 0;
2267 struct pagevec pvec;
2268 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
David Brazdil0f672f62019-12-10 10:32:29 +00002269 struct bio *bio = NULL;
2270 sector_t last_block;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002271 int nr_pages;
2272 pgoff_t uninitialized_var(writeback_index);
2273 pgoff_t index;
2274 pgoff_t end; /* Inclusive */
2275 pgoff_t done_index;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002276 int cycled;
2277 int range_whole = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002278 xa_mark_t tag;
2279 int nwritten = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002280
2281 pagevec_init(&pvec);
2282
2283 if (get_dirty_pages(mapping->host) <=
2284 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2285 set_inode_flag(mapping->host, FI_HOT_DATA);
2286 else
2287 clear_inode_flag(mapping->host, FI_HOT_DATA);
2288
2289 if (wbc->range_cyclic) {
2290 writeback_index = mapping->writeback_index; /* prev offset */
2291 index = writeback_index;
2292 if (index == 0)
2293 cycled = 1;
2294 else
2295 cycled = 0;
2296 end = -1;
2297 } else {
2298 index = wbc->range_start >> PAGE_SHIFT;
2299 end = wbc->range_end >> PAGE_SHIFT;
2300 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2301 range_whole = 1;
2302 cycled = 1; /* ignore range_cyclic tests */
2303 }
2304 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2305 tag = PAGECACHE_TAG_TOWRITE;
2306 else
2307 tag = PAGECACHE_TAG_DIRTY;
2308retry:
2309 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2310 tag_pages_for_writeback(mapping, index, end);
2311 done_index = index;
2312 while (!done && (index <= end)) {
2313 int i;
2314
2315 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
2316 tag);
2317 if (nr_pages == 0)
2318 break;
2319
2320 for (i = 0; i < nr_pages; i++) {
2321 struct page *page = pvec.pages[i];
2322 bool submitted = false;
2323
2324 /* give a priority to WB_SYNC threads */
2325 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
2326 wbc->sync_mode == WB_SYNC_NONE) {
2327 done = 1;
2328 break;
2329 }
2330
2331 done_index = page->index;
2332retry_write:
2333 lock_page(page);
2334
2335 if (unlikely(page->mapping != mapping)) {
2336continue_unlock:
2337 unlock_page(page);
2338 continue;
2339 }
2340
2341 if (!PageDirty(page)) {
2342 /* someone wrote it for us */
2343 goto continue_unlock;
2344 }
2345
2346 if (PageWriteback(page)) {
David Brazdil0f672f62019-12-10 10:32:29 +00002347 if (wbc->sync_mode != WB_SYNC_NONE) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002348 f2fs_wait_on_page_writeback(page,
David Brazdil0f672f62019-12-10 10:32:29 +00002349 DATA, true, true);
2350 f2fs_submit_ipu_bio(sbi, &bio, page);
2351 } else {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002352 goto continue_unlock;
David Brazdil0f672f62019-12-10 10:32:29 +00002353 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002354 }
2355
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002356 if (!clear_page_dirty_for_io(page))
2357 goto continue_unlock;
2358
David Brazdil0f672f62019-12-10 10:32:29 +00002359 ret = __write_data_page(page, &submitted, &bio,
2360 &last_block, wbc, io_type);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002361 if (unlikely(ret)) {
2362 /*
2363 * keep nr_to_write, since vfs uses this to
2364 * get # of written pages.
2365 */
2366 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2367 unlock_page(page);
2368 ret = 0;
2369 continue;
2370 } else if (ret == -EAGAIN) {
2371 ret = 0;
2372 if (wbc->sync_mode == WB_SYNC_ALL) {
2373 cond_resched();
2374 congestion_wait(BLK_RW_ASYNC,
2375 HZ/50);
2376 goto retry_write;
2377 }
2378 continue;
2379 }
2380 done_index = page->index + 1;
2381 done = 1;
2382 break;
2383 } else if (submitted) {
David Brazdil0f672f62019-12-10 10:32:29 +00002384 nwritten++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002385 }
2386
2387 if (--wbc->nr_to_write <= 0 &&
2388 wbc->sync_mode == WB_SYNC_NONE) {
2389 done = 1;
2390 break;
2391 }
2392 }
2393 pagevec_release(&pvec);
2394 cond_resched();
2395 }
2396
2397 if (!cycled && !done) {
2398 cycled = 1;
2399 index = 0;
2400 end = writeback_index - 1;
2401 goto retry;
2402 }
2403 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2404 mapping->writeback_index = done_index;
2405
David Brazdil0f672f62019-12-10 10:32:29 +00002406 if (nwritten)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002407 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
David Brazdil0f672f62019-12-10 10:32:29 +00002408 NULL, 0, DATA);
2409 /* submit cached bio of IPU write */
2410 if (bio)
2411 __submit_bio(sbi, bio, DATA);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002412
2413 return ret;
2414}
2415
2416static inline bool __should_serialize_io(struct inode *inode,
2417 struct writeback_control *wbc)
2418{
2419 if (!S_ISREG(inode->i_mode))
2420 return false;
David Brazdil0f672f62019-12-10 10:32:29 +00002421 if (IS_NOQUOTA(inode))
2422 return false;
2423 /* to avoid deadlock in path of data flush */
2424 if (F2FS_I(inode)->cp_task)
2425 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002426 if (wbc->sync_mode != WB_SYNC_ALL)
2427 return true;
2428 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
2429 return true;
2430 return false;
2431}
2432
2433static int __f2fs_write_data_pages(struct address_space *mapping,
2434 struct writeback_control *wbc,
2435 enum iostat_type io_type)
2436{
2437 struct inode *inode = mapping->host;
2438 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2439 struct blk_plug plug;
2440 int ret;
2441 bool locked = false;
2442
2443 /* deal with chardevs and other special file */
2444 if (!mapping->a_ops->writepage)
2445 return 0;
2446
2447 /* skip writing if there is no dirty page in this inode */
2448 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2449 return 0;
2450
2451 /* during POR, we don't need to trigger writepage at all. */
2452 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2453 goto skip_write;
2454
David Brazdil0f672f62019-12-10 10:32:29 +00002455 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
2456 wbc->sync_mode == WB_SYNC_NONE &&
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002457 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
2458 f2fs_available_free_memory(sbi, DIRTY_DENTS))
2459 goto skip_write;
2460
2461 /* skip writing during file defragment */
2462 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
2463 goto skip_write;
2464
2465 trace_f2fs_writepages(mapping->host, wbc, DATA);
2466
2467 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2468 if (wbc->sync_mode == WB_SYNC_ALL)
2469 atomic_inc(&sbi->wb_sync_req[DATA]);
2470 else if (atomic_read(&sbi->wb_sync_req[DATA]))
2471 goto skip_write;
2472
2473 if (__should_serialize_io(inode, wbc)) {
2474 mutex_lock(&sbi->writepages);
2475 locked = true;
2476 }
2477
2478 blk_start_plug(&plug);
2479 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
2480 blk_finish_plug(&plug);
2481
2482 if (locked)
2483 mutex_unlock(&sbi->writepages);
2484
2485 if (wbc->sync_mode == WB_SYNC_ALL)
2486 atomic_dec(&sbi->wb_sync_req[DATA]);
2487 /*
2488 * if some pages were truncated, we cannot guarantee its mapping->host
2489 * to detect pending bios.
2490 */
2491
2492 f2fs_remove_dirty_inode(inode);
2493 return ret;
2494
2495skip_write:
2496 wbc->pages_skipped += get_dirty_pages(inode);
2497 trace_f2fs_writepages(mapping->host, wbc, DATA);
2498 return 0;
2499}
2500
2501static int f2fs_write_data_pages(struct address_space *mapping,
2502 struct writeback_control *wbc)
2503{
2504 struct inode *inode = mapping->host;
2505
2506 return __f2fs_write_data_pages(mapping, wbc,
2507 F2FS_I(inode)->cp_task == current ?
2508 FS_CP_DATA_IO : FS_DATA_IO);
2509}
2510
2511static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2512{
2513 struct inode *inode = mapping->host;
2514 loff_t i_size = i_size_read(inode);
2515
David Brazdil0f672f62019-12-10 10:32:29 +00002516 /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */
2517 if (to > i_size && !f2fs_verity_in_progress(inode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002518 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2519 down_write(&F2FS_I(inode)->i_mmap_sem);
2520
2521 truncate_pagecache(inode, i_size);
David Brazdil0f672f62019-12-10 10:32:29 +00002522 if (!IS_NOQUOTA(inode))
2523 f2fs_truncate_blocks(inode, i_size, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002524
2525 up_write(&F2FS_I(inode)->i_mmap_sem);
2526 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2527 }
2528}
2529
2530static int prepare_write_begin(struct f2fs_sb_info *sbi,
2531 struct page *page, loff_t pos, unsigned len,
2532 block_t *blk_addr, bool *node_changed)
2533{
2534 struct inode *inode = page->mapping->host;
2535 pgoff_t index = page->index;
2536 struct dnode_of_data dn;
2537 struct page *ipage;
2538 bool locked = false;
2539 struct extent_info ei = {0,0,0};
2540 int err = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00002541 int flag;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002542
2543 /*
2544 * we already allocated all the blocks, so we don't need to get
2545 * the block addresses when there is no need to fill the page.
2546 */
2547 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
David Brazdil0f672f62019-12-10 10:32:29 +00002548 !is_inode_flag_set(inode, FI_NO_PREALLOC) &&
2549 !f2fs_verity_in_progress(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002550 return 0;
2551
David Brazdil0f672f62019-12-10 10:32:29 +00002552 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
2553 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
2554 flag = F2FS_GET_BLOCK_DEFAULT;
2555 else
2556 flag = F2FS_GET_BLOCK_PRE_AIO;
2557
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002558 if (f2fs_has_inline_data(inode) ||
2559 (pos & PAGE_MASK) >= i_size_read(inode)) {
David Brazdil0f672f62019-12-10 10:32:29 +00002560 __do_map_lock(sbi, flag, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002561 locked = true;
2562 }
2563restart:
2564 /* check inline_data */
2565 ipage = f2fs_get_node_page(sbi, inode->i_ino);
2566 if (IS_ERR(ipage)) {
2567 err = PTR_ERR(ipage);
2568 goto unlock_out;
2569 }
2570
2571 set_new_dnode(&dn, inode, ipage, ipage, 0);
2572
2573 if (f2fs_has_inline_data(inode)) {
2574 if (pos + len <= MAX_INLINE_DATA(inode)) {
2575 f2fs_do_read_inline_data(page, ipage);
2576 set_inode_flag(inode, FI_DATA_EXIST);
2577 if (inode->i_nlink)
2578 set_inline_node(ipage);
2579 } else {
2580 err = f2fs_convert_inline_page(&dn, page);
2581 if (err)
2582 goto out;
2583 if (dn.data_blkaddr == NULL_ADDR)
2584 err = f2fs_get_block(&dn, index);
2585 }
2586 } else if (locked) {
2587 err = f2fs_get_block(&dn, index);
2588 } else {
2589 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2590 dn.data_blkaddr = ei.blk + index - ei.fofs;
2591 } else {
2592 /* hole case */
2593 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
2594 if (err || dn.data_blkaddr == NULL_ADDR) {
2595 f2fs_put_dnode(&dn);
2596 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2597 true);
David Brazdil0f672f62019-12-10 10:32:29 +00002598 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002599 locked = true;
2600 goto restart;
2601 }
2602 }
2603 }
2604
2605 /* convert_inline_page can make node_changed */
2606 *blk_addr = dn.data_blkaddr;
2607 *node_changed = dn.node_changed;
2608out:
2609 f2fs_put_dnode(&dn);
2610unlock_out:
2611 if (locked)
David Brazdil0f672f62019-12-10 10:32:29 +00002612 __do_map_lock(sbi, flag, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002613 return err;
2614}
2615
2616static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2617 loff_t pos, unsigned len, unsigned flags,
2618 struct page **pagep, void **fsdata)
2619{
2620 struct inode *inode = mapping->host;
2621 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2622 struct page *page = NULL;
2623 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
2624 bool need_balance = false, drop_atomic = false;
2625 block_t blkaddr = NULL_ADDR;
2626 int err = 0;
2627
2628 trace_f2fs_write_begin(inode, pos, len, flags);
2629
David Brazdil0f672f62019-12-10 10:32:29 +00002630 if (!f2fs_is_checkpoint_ready(sbi)) {
2631 err = -ENOSPC;
2632 goto fail;
2633 }
2634
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002635 if ((f2fs_is_atomic_file(inode) &&
2636 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
2637 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
2638 err = -ENOMEM;
2639 drop_atomic = true;
2640 goto fail;
2641 }
2642
2643 /*
2644 * We should check this at this moment to avoid deadlock on inode page
2645 * and #0 page. The locking rule for inline_data conversion should be:
2646 * lock_page(page #0) -> lock_page(inode_page)
2647 */
2648 if (index != 0) {
2649 err = f2fs_convert_inline_inode(inode);
2650 if (err)
2651 goto fail;
2652 }
2653repeat:
2654 /*
2655 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2656 * wait_for_stable_page. Will wait that below with our IO control.
2657 */
2658 page = f2fs_pagecache_get_page(mapping, index,
2659 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
2660 if (!page) {
2661 err = -ENOMEM;
2662 goto fail;
2663 }
2664
2665 *pagep = page;
2666
2667 err = prepare_write_begin(sbi, page, pos, len,
2668 &blkaddr, &need_balance);
2669 if (err)
2670 goto fail;
2671
David Brazdil0f672f62019-12-10 10:32:29 +00002672 if (need_balance && !IS_NOQUOTA(inode) &&
2673 has_not_enough_free_secs(sbi, 0, 0)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002674 unlock_page(page);
2675 f2fs_balance_fs(sbi, true);
2676 lock_page(page);
2677 if (page->mapping != mapping) {
2678 /* The page got truncated from under us */
2679 f2fs_put_page(page, 1);
2680 goto repeat;
2681 }
2682 }
2683
David Brazdil0f672f62019-12-10 10:32:29 +00002684 f2fs_wait_on_page_writeback(page, DATA, false, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002685
2686 if (len == PAGE_SIZE || PageUptodate(page))
2687 return 0;
2688
David Brazdil0f672f62019-12-10 10:32:29 +00002689 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) &&
2690 !f2fs_verity_in_progress(inode)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002691 zero_user_segment(page, len, PAGE_SIZE);
2692 return 0;
2693 }
2694
2695 if (blkaddr == NEW_ADDR) {
2696 zero_user_segment(page, 0, PAGE_SIZE);
2697 SetPageUptodate(page);
2698 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00002699 if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
2700 DATA_GENERIC_ENHANCE_READ)) {
2701 err = -EFSCORRUPTED;
2702 goto fail;
2703 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002704 err = f2fs_submit_page_read(inode, page, blkaddr);
2705 if (err)
2706 goto fail;
2707
2708 lock_page(page);
2709 if (unlikely(page->mapping != mapping)) {
2710 f2fs_put_page(page, 1);
2711 goto repeat;
2712 }
2713 if (unlikely(!PageUptodate(page))) {
2714 err = -EIO;
2715 goto fail;
2716 }
2717 }
2718 return 0;
2719
2720fail:
2721 f2fs_put_page(page, 1);
2722 f2fs_write_failed(mapping, pos + len);
2723 if (drop_atomic)
2724 f2fs_drop_inmem_pages_all(sbi, false);
2725 return err;
2726}
2727
2728static int f2fs_write_end(struct file *file,
2729 struct address_space *mapping,
2730 loff_t pos, unsigned len, unsigned copied,
2731 struct page *page, void *fsdata)
2732{
2733 struct inode *inode = page->mapping->host;
2734
2735 trace_f2fs_write_end(inode, pos, len, copied);
2736
2737 /*
2738 * This should be come from len == PAGE_SIZE, and we expect copied
2739 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2740 * let generic_perform_write() try to copy data again through copied=0.
2741 */
2742 if (!PageUptodate(page)) {
2743 if (unlikely(copied != len))
2744 copied = 0;
2745 else
2746 SetPageUptodate(page);
2747 }
2748 if (!copied)
2749 goto unlock_out;
2750
2751 set_page_dirty(page);
2752
David Brazdil0f672f62019-12-10 10:32:29 +00002753 if (pos + copied > i_size_read(inode) &&
2754 !f2fs_verity_in_progress(inode))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002755 f2fs_i_size_write(inode, pos + copied);
2756unlock_out:
2757 f2fs_put_page(page, 1);
2758 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2759 return copied;
2760}
2761
2762static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2763 loff_t offset)
2764{
2765 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2766 unsigned blkbits = i_blkbits;
2767 unsigned blocksize_mask = (1 << blkbits) - 1;
2768 unsigned long align = offset | iov_iter_alignment(iter);
2769 struct block_device *bdev = inode->i_sb->s_bdev;
2770
Olivier Deprez0e641232021-09-23 10:07:05 +02002771 if (iov_iter_rw(iter) == READ && offset >= i_size_read(inode))
2772 return 1;
2773
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002774 if (align & blocksize_mask) {
2775 if (bdev)
2776 blkbits = blksize_bits(bdev_logical_block_size(bdev));
2777 blocksize_mask = (1 << blkbits) - 1;
2778 if (align & blocksize_mask)
2779 return -EINVAL;
2780 return 1;
2781 }
2782 return 0;
2783}
2784
David Brazdil0f672f62019-12-10 10:32:29 +00002785static void f2fs_dio_end_io(struct bio *bio)
2786{
2787 struct f2fs_private_dio *dio = bio->bi_private;
2788
2789 dec_page_count(F2FS_I_SB(dio->inode),
2790 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2791
2792 bio->bi_private = dio->orig_private;
2793 bio->bi_end_io = dio->orig_end_io;
2794
2795 kvfree(dio);
2796
2797 bio_endio(bio);
2798}
2799
2800static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
2801 loff_t file_offset)
2802{
2803 struct f2fs_private_dio *dio;
2804 bool write = (bio_op(bio) == REQ_OP_WRITE);
2805
2806 dio = f2fs_kzalloc(F2FS_I_SB(inode),
2807 sizeof(struct f2fs_private_dio), GFP_NOFS);
2808 if (!dio)
2809 goto out;
2810
2811 dio->inode = inode;
2812 dio->orig_end_io = bio->bi_end_io;
2813 dio->orig_private = bio->bi_private;
2814 dio->write = write;
2815
2816 bio->bi_end_io = f2fs_dio_end_io;
2817 bio->bi_private = dio;
2818
2819 inc_page_count(F2FS_I_SB(inode),
2820 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2821
2822 submit_bio(bio);
2823 return;
2824out:
2825 bio->bi_status = BLK_STS_IOERR;
2826 bio_endio(bio);
2827}
2828
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002829static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2830{
2831 struct address_space *mapping = iocb->ki_filp->f_mapping;
2832 struct inode *inode = mapping->host;
2833 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
David Brazdil0f672f62019-12-10 10:32:29 +00002834 struct f2fs_inode_info *fi = F2FS_I(inode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002835 size_t count = iov_iter_count(iter);
2836 loff_t offset = iocb->ki_pos;
2837 int rw = iov_iter_rw(iter);
2838 int err;
2839 enum rw_hint hint = iocb->ki_hint;
2840 int whint_mode = F2FS_OPTION(sbi).whint_mode;
David Brazdil0f672f62019-12-10 10:32:29 +00002841 bool do_opu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002842
2843 err = check_direct_IO(inode, iter, offset);
2844 if (err)
2845 return err < 0 ? err : 0;
2846
David Brazdil0f672f62019-12-10 10:32:29 +00002847 if (f2fs_force_buffered_io(inode, iocb, iter))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002848 return 0;
2849
David Brazdil0f672f62019-12-10 10:32:29 +00002850 do_opu = allow_outplace_dio(inode, iocb, iter);
2851
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002852 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
2853
2854 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2855 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2856
David Brazdil0f672f62019-12-10 10:32:29 +00002857 if (iocb->ki_flags & IOCB_NOWAIT) {
2858 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002859 iocb->ki_hint = hint;
2860 err = -EAGAIN;
2861 goto out;
2862 }
David Brazdil0f672f62019-12-10 10:32:29 +00002863 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
2864 up_read(&fi->i_gc_rwsem[rw]);
2865 iocb->ki_hint = hint;
2866 err = -EAGAIN;
2867 goto out;
2868 }
2869 } else {
2870 down_read(&fi->i_gc_rwsem[rw]);
2871 if (do_opu)
2872 down_read(&fi->i_gc_rwsem[READ]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002873 }
2874
David Brazdil0f672f62019-12-10 10:32:29 +00002875 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
2876 iter, rw == WRITE ? get_data_block_dio_write :
2877 get_data_block_dio, NULL, f2fs_dio_submit_bio,
2878 DIO_LOCKING | DIO_SKIP_HOLES);
2879
2880 if (do_opu)
2881 up_read(&fi->i_gc_rwsem[READ]);
2882
2883 up_read(&fi->i_gc_rwsem[rw]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002884
2885 if (rw == WRITE) {
2886 if (whint_mode == WHINT_MODE_OFF)
2887 iocb->ki_hint = hint;
2888 if (err > 0) {
2889 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2890 err);
David Brazdil0f672f62019-12-10 10:32:29 +00002891 if (!do_opu)
2892 set_inode_flag(inode, FI_UPDATE_WRITE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002893 } else if (err < 0) {
2894 f2fs_write_failed(mapping, offset + count);
2895 }
2896 }
2897
2898out:
2899 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
2900
2901 return err;
2902}
2903
2904void f2fs_invalidate_page(struct page *page, unsigned int offset,
2905 unsigned int length)
2906{
2907 struct inode *inode = page->mapping->host;
2908 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2909
2910 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
2911 (offset % PAGE_SIZE || length != PAGE_SIZE))
2912 return;
2913
2914 if (PageDirty(page)) {
2915 if (inode->i_ino == F2FS_META_INO(sbi)) {
2916 dec_page_count(sbi, F2FS_DIRTY_META);
2917 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
2918 dec_page_count(sbi, F2FS_DIRTY_NODES);
2919 } else {
2920 inode_dec_dirty_pages(inode);
2921 f2fs_remove_dirty_inode(inode);
2922 }
2923 }
2924
David Brazdil0f672f62019-12-10 10:32:29 +00002925 clear_cold_data(page);
2926
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002927 if (IS_ATOMIC_WRITTEN_PAGE(page))
2928 return f2fs_drop_inmem_page(inode, page);
2929
David Brazdil0f672f62019-12-10 10:32:29 +00002930 f2fs_clear_page_private(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002931}
2932
2933int f2fs_release_page(struct page *page, gfp_t wait)
2934{
2935 /* If this is dirty page, keep PagePrivate */
2936 if (PageDirty(page))
2937 return 0;
2938
2939 /* This is atomic written page, keep Private */
2940 if (IS_ATOMIC_WRITTEN_PAGE(page))
2941 return 0;
2942
David Brazdil0f672f62019-12-10 10:32:29 +00002943 clear_cold_data(page);
2944 f2fs_clear_page_private(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002945 return 1;
2946}
2947
2948static int f2fs_set_data_page_dirty(struct page *page)
2949{
David Brazdil0f672f62019-12-10 10:32:29 +00002950 struct inode *inode = page_file_mapping(page)->host;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002951
2952 trace_f2fs_set_page_dirty(page, DATA);
2953
2954 if (!PageUptodate(page))
2955 SetPageUptodate(page);
David Brazdil0f672f62019-12-10 10:32:29 +00002956 if (PageSwapCache(page))
2957 return __set_page_dirty_nobuffers(page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002958
2959 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
2960 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2961 f2fs_register_inmem_page(inode, page);
2962 return 1;
2963 }
2964 /*
2965 * Previously, this page has been registered, we just
2966 * return here.
2967 */
2968 return 0;
2969 }
2970
2971 if (!PageDirty(page)) {
2972 __set_page_dirty_nobuffers(page);
2973 f2fs_update_dirty_page(inode, page);
2974 return 1;
2975 }
2976 return 0;
2977}
2978
2979static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2980{
2981 struct inode *inode = mapping->host;
2982
2983 if (f2fs_has_inline_data(inode))
2984 return 0;
2985
2986 /* make sure allocating whole blocks */
2987 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2988 filemap_write_and_wait(mapping);
2989
2990 return generic_block_bmap(mapping, block, get_data_block_bmap);
2991}
2992
2993#ifdef CONFIG_MIGRATION
2994#include <linux/migrate.h>
2995
2996int f2fs_migrate_page(struct address_space *mapping,
2997 struct page *newpage, struct page *page, enum migrate_mode mode)
2998{
2999 int rc, extra_count;
3000 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
3001 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
3002
3003 BUG_ON(PageWriteback(page));
3004
3005 /* migrating an atomic written page is safe with the inmem_lock hold */
3006 if (atomic_written) {
3007 if (mode != MIGRATE_SYNC)
3008 return -EBUSY;
3009 if (!mutex_trylock(&fi->inmem_lock))
3010 return -EAGAIN;
3011 }
3012
David Brazdil0f672f62019-12-10 10:32:29 +00003013 /* one extra reference was held for atomic_write page */
3014 extra_count = atomic_written ? 1 : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003015 rc = migrate_page_move_mapping(mapping, newpage,
David Brazdil0f672f62019-12-10 10:32:29 +00003016 page, extra_count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003017 if (rc != MIGRATEPAGE_SUCCESS) {
3018 if (atomic_written)
3019 mutex_unlock(&fi->inmem_lock);
3020 return rc;
3021 }
3022
3023 if (atomic_written) {
3024 struct inmem_pages *cur;
3025 list_for_each_entry(cur, &fi->inmem_pages, list)
3026 if (cur->page == page) {
3027 cur->page = newpage;
3028 break;
3029 }
3030 mutex_unlock(&fi->inmem_lock);
3031 put_page(page);
3032 get_page(newpage);
3033 }
3034
David Brazdil0f672f62019-12-10 10:32:29 +00003035 if (PagePrivate(page)) {
3036 f2fs_set_page_private(newpage, page_private(page));
3037 f2fs_clear_page_private(page);
3038 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003039
3040 if (mode != MIGRATE_SYNC_NO_COPY)
3041 migrate_page_copy(newpage, page);
3042 else
3043 migrate_page_states(newpage, page);
3044
3045 return MIGRATEPAGE_SUCCESS;
3046}
3047#endif
3048
David Brazdil0f672f62019-12-10 10:32:29 +00003049#ifdef CONFIG_SWAP
3050/* Copied from generic_swapfile_activate() to check any holes */
Olivier Deprez0e641232021-09-23 10:07:05 +02003051static int check_swap_activate(struct swap_info_struct *sis,
3052 struct file *swap_file, sector_t *span)
David Brazdil0f672f62019-12-10 10:32:29 +00003053{
3054 struct address_space *mapping = swap_file->f_mapping;
3055 struct inode *inode = mapping->host;
3056 unsigned blocks_per_page;
3057 unsigned long page_no;
3058 unsigned blkbits;
3059 sector_t probe_block;
3060 sector_t last_block;
3061 sector_t lowest_block = -1;
3062 sector_t highest_block = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02003063 int nr_extents = 0;
3064 int ret;
David Brazdil0f672f62019-12-10 10:32:29 +00003065
3066 blkbits = inode->i_blkbits;
3067 blocks_per_page = PAGE_SIZE >> blkbits;
3068
3069 /*
3070 * Map all the blocks into the extent list. This code doesn't try
3071 * to be very smart.
3072 */
3073 probe_block = 0;
3074 page_no = 0;
3075 last_block = i_size_read(inode) >> blkbits;
Olivier Deprez0e641232021-09-23 10:07:05 +02003076 while ((probe_block + blocks_per_page) <= last_block &&
3077 page_no < sis->max) {
David Brazdil0f672f62019-12-10 10:32:29 +00003078 unsigned block_in_page;
3079 sector_t first_block;
3080
3081 cond_resched();
3082
3083 first_block = bmap(inode, probe_block);
3084 if (first_block == 0)
3085 goto bad_bmap;
3086
3087 /*
3088 * It must be PAGE_SIZE aligned on-disk
3089 */
3090 if (first_block & (blocks_per_page - 1)) {
3091 probe_block++;
3092 goto reprobe;
3093 }
3094
3095 for (block_in_page = 1; block_in_page < blocks_per_page;
3096 block_in_page++) {
3097 sector_t block;
3098
3099 block = bmap(inode, probe_block + block_in_page);
3100 if (block == 0)
3101 goto bad_bmap;
3102 if (block != first_block + block_in_page) {
3103 /* Discontiguity */
3104 probe_block++;
3105 goto reprobe;
3106 }
3107 }
3108
3109 first_block >>= (PAGE_SHIFT - blkbits);
3110 if (page_no) { /* exclude the header page */
3111 if (first_block < lowest_block)
3112 lowest_block = first_block;
3113 if (first_block > highest_block)
3114 highest_block = first_block;
3115 }
3116
Olivier Deprez0e641232021-09-23 10:07:05 +02003117 /*
3118 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
3119 */
3120 ret = add_swap_extent(sis, page_no, 1, first_block);
3121 if (ret < 0)
3122 goto out;
3123 nr_extents += ret;
David Brazdil0f672f62019-12-10 10:32:29 +00003124 page_no++;
3125 probe_block += blocks_per_page;
3126reprobe:
3127 continue;
3128 }
Olivier Deprez0e641232021-09-23 10:07:05 +02003129 ret = nr_extents;
3130 *span = 1 + highest_block - lowest_block;
3131 if (page_no == 0)
3132 page_no = 1; /* force Empty message */
3133 sis->max = page_no;
3134 sis->pages = page_no - 1;
3135 sis->highest_bit = page_no - 1;
3136out:
3137 return ret;
David Brazdil0f672f62019-12-10 10:32:29 +00003138bad_bmap:
3139 pr_err("swapon: swapfile has holes\n");
3140 return -EINVAL;
3141}
3142
3143static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3144 sector_t *span)
3145{
3146 struct inode *inode = file_inode(file);
3147 int ret;
3148
3149 if (!S_ISREG(inode->i_mode))
3150 return -EINVAL;
3151
3152 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
3153 return -EROFS;
3154
3155 ret = f2fs_convert_inline_inode(inode);
3156 if (ret)
3157 return ret;
3158
Olivier Deprez0e641232021-09-23 10:07:05 +02003159 ret = check_swap_activate(sis, file, span);
3160 if (ret < 0)
David Brazdil0f672f62019-12-10 10:32:29 +00003161 return ret;
3162
3163 set_inode_flag(inode, FI_PIN_FILE);
3164 f2fs_precache_extents(inode);
3165 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Olivier Deprez0e641232021-09-23 10:07:05 +02003166 return ret;
David Brazdil0f672f62019-12-10 10:32:29 +00003167}
3168
3169static void f2fs_swap_deactivate(struct file *file)
3170{
3171 struct inode *inode = file_inode(file);
3172
3173 clear_inode_flag(inode, FI_PIN_FILE);
3174}
3175#else
3176static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
3177 sector_t *span)
3178{
3179 return -EOPNOTSUPP;
3180}
3181
3182static void f2fs_swap_deactivate(struct file *file)
3183{
3184}
3185#endif
3186
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003187const struct address_space_operations f2fs_dblock_aops = {
3188 .readpage = f2fs_read_data_page,
3189 .readpages = f2fs_read_data_pages,
3190 .writepage = f2fs_write_data_page,
3191 .writepages = f2fs_write_data_pages,
3192 .write_begin = f2fs_write_begin,
3193 .write_end = f2fs_write_end,
3194 .set_page_dirty = f2fs_set_data_page_dirty,
3195 .invalidatepage = f2fs_invalidate_page,
3196 .releasepage = f2fs_release_page,
3197 .direct_IO = f2fs_direct_IO,
3198 .bmap = f2fs_bmap,
David Brazdil0f672f62019-12-10 10:32:29 +00003199 .swap_activate = f2fs_swap_activate,
3200 .swap_deactivate = f2fs_swap_deactivate,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003201#ifdef CONFIG_MIGRATION
3202 .migratepage = f2fs_migrate_page,
3203#endif
3204};
3205
David Brazdil0f672f62019-12-10 10:32:29 +00003206void f2fs_clear_page_cache_dirty_tag(struct page *page)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003207{
3208 struct address_space *mapping = page_mapping(page);
3209 unsigned long flags;
3210
3211 xa_lock_irqsave(&mapping->i_pages, flags);
David Brazdil0f672f62019-12-10 10:32:29 +00003212 __xa_clear_mark(&mapping->i_pages, page_index(page),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003213 PAGECACHE_TAG_DIRTY);
3214 xa_unlock_irqrestore(&mapping->i_pages, flags);
3215}
3216
3217int __init f2fs_init_post_read_processing(void)
3218{
David Brazdil0f672f62019-12-10 10:32:29 +00003219 bio_post_read_ctx_cache =
3220 kmem_cache_create("f2fs_bio_post_read_ctx",
3221 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003222 if (!bio_post_read_ctx_cache)
3223 goto fail;
3224 bio_post_read_ctx_pool =
3225 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
3226 bio_post_read_ctx_cache);
3227 if (!bio_post_read_ctx_pool)
3228 goto fail_free_cache;
3229 return 0;
3230
3231fail_free_cache:
3232 kmem_cache_destroy(bio_post_read_ctx_cache);
3233fail:
3234 return -ENOMEM;
3235}
3236
3237void __exit f2fs_destroy_post_read_processing(void)
3238{
3239 mempool_destroy(bio_post_read_ctx_pool);
3240 kmem_cache_destroy(bio_post_read_ctx_cache);
3241}