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