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