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