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