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