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