f2fs: fix the number of orphan inode blocks
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / fs / f2fs / data.c
1 /*
2 * fs/f2fs/data.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include <linux/buffer_head.h>
14 #include <linux/mpage.h>
15 #include <linux/aio.h>
16 #include <linux/writeback.h>
17 #include <linux/backing-dev.h>
18 #include <linux/blkdev.h>
19 #include <linux/bio.h>
20 #include <linux/prefetch.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 static struct kmem_cache *extent_tree_slab;
29 static struct kmem_cache *extent_node_slab;
30
31 static void f2fs_read_end_io(struct bio *bio, int err)
32 {
33 struct bio_vec *bvec;
34 int i;
35
36 bio_for_each_segment_all(bvec, bio, i) {
37 struct page *page = bvec->bv_page;
38
39 if (!err) {
40 SetPageUptodate(page);
41 } else {
42 ClearPageUptodate(page);
43 SetPageError(page);
44 }
45 unlock_page(page);
46 }
47 bio_put(bio);
48 }
49
50 static void f2fs_write_end_io(struct bio *bio, int err)
51 {
52 struct f2fs_sb_info *sbi = bio->bi_private;
53 struct bio_vec *bvec;
54 int i;
55
56 bio_for_each_segment_all(bvec, bio, i) {
57 struct page *page = bvec->bv_page;
58
59 if (unlikely(err)) {
60 set_page_dirty(page);
61 set_bit(AS_EIO, &page->mapping->flags);
62 f2fs_stop_checkpoint(sbi);
63 }
64 end_page_writeback(page);
65 dec_page_count(sbi, F2FS_WRITEBACK);
66 }
67
68 if (!get_pages(sbi, F2FS_WRITEBACK) &&
69 !list_empty(&sbi->cp_wait.task_list))
70 wake_up(&sbi->cp_wait);
71
72 bio_put(bio);
73 }
74
75 /*
76 * Low-level block read/write IO operations.
77 */
78 static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
79 int npages, bool is_read)
80 {
81 struct bio *bio;
82
83 /* No failure on bio allocation */
84 bio = bio_alloc(GFP_NOIO, npages);
85
86 bio->bi_bdev = sbi->sb->s_bdev;
87 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
88 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
89 bio->bi_private = sbi;
90
91 return bio;
92 }
93
94 static void __submit_merged_bio(struct f2fs_bio_info *io)
95 {
96 struct f2fs_io_info *fio = &io->fio;
97
98 if (!io->bio)
99 return;
100
101 if (is_read_io(fio->rw))
102 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
103 else
104 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
105
106 submit_bio(fio->rw, io->bio);
107 io->bio = NULL;
108 }
109
110 void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
111 enum page_type type, int rw)
112 {
113 enum page_type btype = PAGE_TYPE_OF_BIO(type);
114 struct f2fs_bio_info *io;
115
116 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
117
118 down_write(&io->io_rwsem);
119
120 /* change META to META_FLUSH in the checkpoint procedure */
121 if (type >= META_FLUSH) {
122 io->fio.type = META_FLUSH;
123 if (test_opt(sbi, NOBARRIER))
124 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
125 else
126 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
127 }
128 __submit_merged_bio(io);
129 up_write(&io->io_rwsem);
130 }
131
132 /*
133 * Fill the locked page with data located in the block address.
134 * Return unlocked page.
135 */
136 int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page,
137 struct f2fs_io_info *fio)
138 {
139 struct bio *bio;
140
141 trace_f2fs_submit_page_bio(page, fio);
142 f2fs_trace_ios(page, fio, 0);
143
144 /* Allocate a new bio */
145 bio = __bio_alloc(sbi, fio->blk_addr, 1, is_read_io(fio->rw));
146
147 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
148 bio_put(bio);
149 f2fs_put_page(page, 1);
150 return -EFAULT;
151 }
152
153 submit_bio(fio->rw, bio);
154 return 0;
155 }
156
157 void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page,
158 struct f2fs_io_info *fio)
159 {
160 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
161 struct f2fs_bio_info *io;
162 bool is_read = is_read_io(fio->rw);
163
164 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
165
166 verify_block_addr(sbi, fio->blk_addr);
167
168 down_write(&io->io_rwsem);
169
170 if (!is_read)
171 inc_page_count(sbi, F2FS_WRITEBACK);
172
173 if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
174 io->fio.rw != fio->rw))
175 __submit_merged_bio(io);
176 alloc_new:
177 if (io->bio == NULL) {
178 int bio_blocks = MAX_BIO_BLOCKS(sbi);
179
180 io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
181 io->fio = *fio;
182 }
183
184 if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
185 PAGE_CACHE_SIZE) {
186 __submit_merged_bio(io);
187 goto alloc_new;
188 }
189
190 io->last_block_in_bio = fio->blk_addr;
191 f2fs_trace_ios(page, fio, 0);
192
193 up_write(&io->io_rwsem);
194 trace_f2fs_submit_page_mbio(page, fio);
195 }
196
197 /*
198 * Lock ordering for the change of data block address:
199 * ->data_page
200 * ->node_page
201 * update block addresses in the node page
202 */
203 static void __set_data_blkaddr(struct dnode_of_data *dn)
204 {
205 struct f2fs_node *rn;
206 __le32 *addr_array;
207 struct page *node_page = dn->node_page;
208 unsigned int ofs_in_node = dn->ofs_in_node;
209
210 f2fs_wait_on_page_writeback(node_page, NODE);
211
212 rn = F2FS_NODE(node_page);
213
214 /* Get physical address of data block */
215 addr_array = blkaddr_in_node(rn);
216 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
217 set_page_dirty(node_page);
218 }
219
220 int reserve_new_block(struct dnode_of_data *dn)
221 {
222 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
223
224 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
225 return -EPERM;
226 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
227 return -ENOSPC;
228
229 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
230
231 dn->data_blkaddr = NEW_ADDR;
232 __set_data_blkaddr(dn);
233 mark_inode_dirty(dn->inode);
234 sync_inode_page(dn);
235 return 0;
236 }
237
238 int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
239 {
240 bool need_put = dn->inode_page ? false : true;
241 int err;
242
243 err = get_dnode_of_data(dn, index, ALLOC_NODE);
244 if (err)
245 return err;
246
247 if (dn->data_blkaddr == NULL_ADDR)
248 err = reserve_new_block(dn);
249 if (err || need_put)
250 f2fs_put_dnode(dn);
251 return err;
252 }
253
254 static void f2fs_map_bh(struct super_block *sb, pgoff_t pgofs,
255 struct extent_info *ei, struct buffer_head *bh_result)
256 {
257 unsigned int blkbits = sb->s_blocksize_bits;
258 size_t count;
259
260 set_buffer_new(bh_result);
261 map_bh(bh_result, sb, ei->blk + pgofs - ei->fofs);
262 count = ei->fofs + ei->len - pgofs;
263 if (count < (UINT_MAX >> blkbits))
264 bh_result->b_size = (count << blkbits);
265 else
266 bh_result->b_size = UINT_MAX;
267 }
268
269 static bool lookup_extent_info(struct inode *inode, pgoff_t pgofs,
270 struct extent_info *ei)
271 {
272 struct f2fs_inode_info *fi = F2FS_I(inode);
273 pgoff_t start_fofs, end_fofs;
274 block_t start_blkaddr;
275
276 read_lock(&fi->ext_lock);
277 if (fi->ext.len == 0) {
278 read_unlock(&fi->ext_lock);
279 return false;
280 }
281
282 stat_inc_total_hit(inode->i_sb);
283
284 start_fofs = fi->ext.fofs;
285 end_fofs = fi->ext.fofs + fi->ext.len - 1;
286 start_blkaddr = fi->ext.blk;
287
288 if (pgofs >= start_fofs && pgofs <= end_fofs) {
289 *ei = fi->ext;
290 stat_inc_read_hit(inode->i_sb);
291 read_unlock(&fi->ext_lock);
292 return true;
293 }
294 read_unlock(&fi->ext_lock);
295 return false;
296 }
297
298 static bool update_extent_info(struct inode *inode, pgoff_t fofs,
299 block_t blkaddr)
300 {
301 struct f2fs_inode_info *fi = F2FS_I(inode);
302 pgoff_t start_fofs, end_fofs;
303 block_t start_blkaddr, end_blkaddr;
304 int need_update = true;
305
306 write_lock(&fi->ext_lock);
307
308 start_fofs = fi->ext.fofs;
309 end_fofs = fi->ext.fofs + fi->ext.len - 1;
310 start_blkaddr = fi->ext.blk;
311 end_blkaddr = fi->ext.blk + fi->ext.len - 1;
312
313 /* Drop and initialize the matched extent */
314 if (fi->ext.len == 1 && fofs == start_fofs)
315 fi->ext.len = 0;
316
317 /* Initial extent */
318 if (fi->ext.len == 0) {
319 if (blkaddr != NULL_ADDR) {
320 fi->ext.fofs = fofs;
321 fi->ext.blk = blkaddr;
322 fi->ext.len = 1;
323 }
324 goto end_update;
325 }
326
327 /* Front merge */
328 if (fofs == start_fofs - 1 && blkaddr == start_blkaddr - 1) {
329 fi->ext.fofs--;
330 fi->ext.blk--;
331 fi->ext.len++;
332 goto end_update;
333 }
334
335 /* Back merge */
336 if (fofs == end_fofs + 1 && blkaddr == end_blkaddr + 1) {
337 fi->ext.len++;
338 goto end_update;
339 }
340
341 /* Split the existing extent */
342 if (fi->ext.len > 1 &&
343 fofs >= start_fofs && fofs <= end_fofs) {
344 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
345 fi->ext.len = fofs - start_fofs;
346 } else {
347 fi->ext.fofs = fofs + 1;
348 fi->ext.blk = start_blkaddr + fofs - start_fofs + 1;
349 fi->ext.len -= fofs - start_fofs + 1;
350 }
351 } else {
352 need_update = false;
353 }
354
355 /* Finally, if the extent is very fragmented, let's drop the cache. */
356 if (fi->ext.len < F2FS_MIN_EXTENT_LEN) {
357 fi->ext.len = 0;
358 set_inode_flag(fi, FI_NO_EXTENT);
359 need_update = true;
360 }
361 end_update:
362 write_unlock(&fi->ext_lock);
363 return need_update;
364 }
365
366 static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
367 struct extent_tree *et, struct extent_info *ei,
368 struct rb_node *parent, struct rb_node **p)
369 {
370 struct extent_node *en;
371
372 en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
373 if (!en)
374 return NULL;
375
376 en->ei = *ei;
377 INIT_LIST_HEAD(&en->list);
378
379 rb_link_node(&en->rb_node, parent, p);
380 rb_insert_color(&en->rb_node, &et->root);
381 et->count++;
382 atomic_inc(&sbi->total_ext_node);
383 return en;
384 }
385
386 static void __detach_extent_node(struct f2fs_sb_info *sbi,
387 struct extent_tree *et, struct extent_node *en)
388 {
389 rb_erase(&en->rb_node, &et->root);
390 et->count--;
391 atomic_dec(&sbi->total_ext_node);
392
393 if (et->cached_en == en)
394 et->cached_en = NULL;
395 }
396
397 static struct extent_node *__lookup_extent_tree(struct extent_tree *et,
398 unsigned int fofs)
399 {
400 struct rb_node *node = et->root.rb_node;
401 struct extent_node *en;
402
403 if (et->cached_en) {
404 struct extent_info *cei = &et->cached_en->ei;
405
406 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
407 return et->cached_en;
408 }
409
410 while (node) {
411 en = rb_entry(node, struct extent_node, rb_node);
412
413 if (fofs < en->ei.fofs) {
414 node = node->rb_left;
415 } else if (fofs >= en->ei.fofs + en->ei.len) {
416 node = node->rb_right;
417 } else {
418 et->cached_en = en;
419 return en;
420 }
421 }
422 return NULL;
423 }
424
425 static struct extent_node *__try_back_merge(struct f2fs_sb_info *sbi,
426 struct extent_tree *et, struct extent_node *en)
427 {
428 struct extent_node *prev;
429 struct rb_node *node;
430
431 node = rb_prev(&en->rb_node);
432 if (!node)
433 return NULL;
434
435 prev = rb_entry(node, struct extent_node, rb_node);
436 if (__is_back_mergeable(&en->ei, &prev->ei)) {
437 en->ei.fofs = prev->ei.fofs;
438 en->ei.blk = prev->ei.blk;
439 en->ei.len += prev->ei.len;
440 __detach_extent_node(sbi, et, prev);
441 return prev;
442 }
443 return NULL;
444 }
445
446 static struct extent_node *__try_front_merge(struct f2fs_sb_info *sbi,
447 struct extent_tree *et, struct extent_node *en)
448 {
449 struct extent_node *next;
450 struct rb_node *node;
451
452 node = rb_next(&en->rb_node);
453 if (!node)
454 return NULL;
455
456 next = rb_entry(node, struct extent_node, rb_node);
457 if (__is_front_mergeable(&en->ei, &next->ei)) {
458 en->ei.len += next->ei.len;
459 __detach_extent_node(sbi, et, next);
460 return next;
461 }
462 return NULL;
463 }
464
465 static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
466 struct extent_tree *et, struct extent_info *ei,
467 struct extent_node **den)
468 {
469 struct rb_node **p = &et->root.rb_node;
470 struct rb_node *parent = NULL;
471 struct extent_node *en;
472
473 while (*p) {
474 parent = *p;
475 en = rb_entry(parent, struct extent_node, rb_node);
476
477 if (ei->fofs < en->ei.fofs) {
478 if (__is_front_mergeable(ei, &en->ei)) {
479 f2fs_bug_on(sbi, !den);
480 en->ei.fofs = ei->fofs;
481 en->ei.blk = ei->blk;
482 en->ei.len += ei->len;
483 *den = __try_back_merge(sbi, et, en);
484 return en;
485 }
486 p = &(*p)->rb_left;
487 } else if (ei->fofs >= en->ei.fofs + en->ei.len) {
488 if (__is_back_mergeable(ei, &en->ei)) {
489 f2fs_bug_on(sbi, !den);
490 en->ei.len += ei->len;
491 *den = __try_front_merge(sbi, et, en);
492 return en;
493 }
494 p = &(*p)->rb_right;
495 } else {
496 f2fs_bug_on(sbi, 1);
497 }
498 }
499
500 return __attach_extent_node(sbi, et, ei, parent, p);
501 }
502
503 static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
504 struct extent_tree *et, bool free_all)
505 {
506 struct rb_node *node, *next;
507 struct extent_node *en;
508 unsigned int count = et->count;
509
510 node = rb_first(&et->root);
511 while (node) {
512 next = rb_next(node);
513 en = rb_entry(node, struct extent_node, rb_node);
514
515 if (free_all) {
516 spin_lock(&sbi->extent_lock);
517 if (!list_empty(&en->list))
518 list_del_init(&en->list);
519 spin_unlock(&sbi->extent_lock);
520 }
521
522 if (free_all || list_empty(&en->list)) {
523 __detach_extent_node(sbi, et, en);
524 kmem_cache_free(extent_node_slab, en);
525 }
526 node = next;
527 }
528
529 return count - et->count;
530 }
531
532 static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
533 struct extent_info *ei)
534 {
535 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
536 struct extent_tree *et;
537 struct extent_node *en;
538
539 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
540
541 down_read(&sbi->extent_tree_lock);
542 et = radix_tree_lookup(&sbi->extent_tree_root, inode->i_ino);
543 if (!et) {
544 up_read(&sbi->extent_tree_lock);
545 return false;
546 }
547 atomic_inc(&et->refcount);
548 up_read(&sbi->extent_tree_lock);
549
550 read_lock(&et->lock);
551 en = __lookup_extent_tree(et, pgofs);
552 if (en) {
553 *ei = en->ei;
554 spin_lock(&sbi->extent_lock);
555 if (!list_empty(&en->list))
556 list_move_tail(&en->list, &sbi->extent_list);
557 spin_unlock(&sbi->extent_lock);
558 stat_inc_read_hit(sbi->sb);
559 }
560 stat_inc_total_hit(sbi->sb);
561 read_unlock(&et->lock);
562
563 trace_f2fs_lookup_extent_tree_end(inode, pgofs, en);
564
565 atomic_dec(&et->refcount);
566 return en ? true : false;
567 }
568
569 static void f2fs_update_extent_tree(struct inode *inode, pgoff_t fofs,
570 block_t blkaddr)
571 {
572 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
573 nid_t ino = inode->i_ino;
574 struct extent_tree *et;
575 struct extent_node *en = NULL, *en1 = NULL, *en2 = NULL, *en3 = NULL;
576 struct extent_node *den = NULL;
577 struct extent_info ei, dei;
578 unsigned int endofs;
579
580 trace_f2fs_update_extent_tree(inode, fofs, blkaddr);
581
582 down_write(&sbi->extent_tree_lock);
583 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
584 if (!et) {
585 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
586 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
587 memset(et, 0, sizeof(struct extent_tree));
588 et->ino = ino;
589 et->root = RB_ROOT;
590 et->cached_en = NULL;
591 rwlock_init(&et->lock);
592 atomic_set(&et->refcount, 0);
593 et->count = 0;
594 sbi->total_ext_tree++;
595 }
596 atomic_inc(&et->refcount);
597 up_write(&sbi->extent_tree_lock);
598
599 write_lock(&et->lock);
600
601 /* 1. lookup and remove existing extent info in cache */
602 en = __lookup_extent_tree(et, fofs);
603 if (!en)
604 goto update_extent;
605
606 dei = en->ei;
607 __detach_extent_node(sbi, et, en);
608
609 /* 2. if extent can be split more, split and insert the left part */
610 if (dei.len > 1) {
611 /* insert left part of split extent into cache */
612 if (fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
613 set_extent_info(&ei, dei.fofs, dei.blk,
614 fofs - dei.fofs);
615 en1 = __insert_extent_tree(sbi, et, &ei, NULL);
616 }
617
618 /* insert right part of split extent into cache */
619 endofs = dei.fofs + dei.len - 1;
620 if (endofs - fofs >= F2FS_MIN_EXTENT_LEN) {
621 set_extent_info(&ei, fofs + 1,
622 fofs - dei.fofs + dei.blk, endofs - fofs);
623 en2 = __insert_extent_tree(sbi, et, &ei, NULL);
624 }
625 }
626
627 update_extent:
628 /* 3. update extent in extent cache */
629 if (blkaddr) {
630 set_extent_info(&ei, fofs, blkaddr, 1);
631 en3 = __insert_extent_tree(sbi, et, &ei, &den);
632 }
633
634 /* 4. update in global extent list */
635 spin_lock(&sbi->extent_lock);
636 if (en && !list_empty(&en->list))
637 list_del(&en->list);
638 /*
639 * en1 and en2 split from en, they will become more and more smaller
640 * fragments after splitting several times. So if the length is smaller
641 * than F2FS_MIN_EXTENT_LEN, we will not add them into extent tree.
642 */
643 if (en1)
644 list_add_tail(&en1->list, &sbi->extent_list);
645 if (en2)
646 list_add_tail(&en2->list, &sbi->extent_list);
647 if (en3) {
648 if (list_empty(&en3->list))
649 list_add_tail(&en3->list, &sbi->extent_list);
650 else
651 list_move_tail(&en3->list, &sbi->extent_list);
652 }
653 if (den && !list_empty(&den->list))
654 list_del(&den->list);
655 spin_unlock(&sbi->extent_lock);
656
657 /* 5. release extent node */
658 if (en)
659 kmem_cache_free(extent_node_slab, en);
660 if (den)
661 kmem_cache_free(extent_node_slab, den);
662
663 write_unlock(&et->lock);
664 atomic_dec(&et->refcount);
665 }
666
667 void f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
668 {
669 struct extent_tree *treevec[EXT_TREE_VEC_SIZE];
670 struct extent_node *en, *tmp;
671 unsigned long ino = F2FS_ROOT_INO(sbi);
672 struct radix_tree_iter iter;
673 void **slot;
674 unsigned int found;
675 unsigned int node_cnt = 0, tree_cnt = 0;
676
677 if (!test_opt(sbi, EXTENT_CACHE))
678 return;
679
680 if (available_free_memory(sbi, EXTENT_CACHE))
681 return;
682
683 spin_lock(&sbi->extent_lock);
684 list_for_each_entry_safe(en, tmp, &sbi->extent_list, list) {
685 if (!nr_shrink--)
686 break;
687 list_del_init(&en->list);
688 }
689 spin_unlock(&sbi->extent_lock);
690
691 down_read(&sbi->extent_tree_lock);
692 while ((found = radix_tree_gang_lookup(&sbi->extent_tree_root,
693 (void **)treevec, ino, EXT_TREE_VEC_SIZE))) {
694 unsigned i;
695
696 ino = treevec[found - 1]->ino + 1;
697 for (i = 0; i < found; i++) {
698 struct extent_tree *et = treevec[i];
699
700 atomic_inc(&et->refcount);
701 write_lock(&et->lock);
702 node_cnt += __free_extent_tree(sbi, et, false);
703 write_unlock(&et->lock);
704 atomic_dec(&et->refcount);
705 }
706 }
707 up_read(&sbi->extent_tree_lock);
708
709 down_write(&sbi->extent_tree_lock);
710 radix_tree_for_each_slot(slot, &sbi->extent_tree_root, &iter,
711 F2FS_ROOT_INO(sbi)) {
712 struct extent_tree *et = (struct extent_tree *)*slot;
713
714 if (!atomic_read(&et->refcount) && !et->count) {
715 radix_tree_delete(&sbi->extent_tree_root, et->ino);
716 kmem_cache_free(extent_tree_slab, et);
717 sbi->total_ext_tree--;
718 tree_cnt++;
719 }
720 }
721 up_write(&sbi->extent_tree_lock);
722
723 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
724 }
725
726 void f2fs_destroy_extent_tree(struct inode *inode)
727 {
728 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
729 struct extent_tree *et;
730 unsigned int node_cnt = 0;
731
732 if (!test_opt(sbi, EXTENT_CACHE))
733 return;
734
735 down_read(&sbi->extent_tree_lock);
736 et = radix_tree_lookup(&sbi->extent_tree_root, inode->i_ino);
737 if (!et) {
738 up_read(&sbi->extent_tree_lock);
739 goto out;
740 }
741 atomic_inc(&et->refcount);
742 up_read(&sbi->extent_tree_lock);
743
744 /* free all extent info belong to this extent tree */
745 write_lock(&et->lock);
746 node_cnt = __free_extent_tree(sbi, et, true);
747 write_unlock(&et->lock);
748
749 atomic_dec(&et->refcount);
750
751 /* try to find and delete extent tree entry in radix tree */
752 down_write(&sbi->extent_tree_lock);
753 et = radix_tree_lookup(&sbi->extent_tree_root, inode->i_ino);
754 if (!et) {
755 up_write(&sbi->extent_tree_lock);
756 goto out;
757 }
758 f2fs_bug_on(sbi, atomic_read(&et->refcount) || et->count);
759 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
760 kmem_cache_free(extent_tree_slab, et);
761 sbi->total_ext_tree--;
762 up_write(&sbi->extent_tree_lock);
763 out:
764 trace_f2fs_destroy_extent_tree(inode, node_cnt);
765 return;
766 }
767
768 static bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
769 struct extent_info *ei)
770 {
771 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
772 return false;
773
774 if (test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
775 return f2fs_lookup_extent_tree(inode, pgofs, ei);
776
777 return lookup_extent_info(inode, pgofs, ei);
778 }
779
780 void f2fs_update_extent_cache(struct dnode_of_data *dn)
781 {
782 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
783 pgoff_t fofs;
784
785 f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR);
786
787 /* Update the page address in the parent node */
788 __set_data_blkaddr(dn);
789
790 if (is_inode_flag_set(fi, FI_NO_EXTENT))
791 return;
792
793 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
794 dn->ofs_in_node;
795
796 if (test_opt(F2FS_I_SB(dn->inode), EXTENT_CACHE))
797 return f2fs_update_extent_tree(dn->inode, fofs,
798 dn->data_blkaddr);
799
800 if (update_extent_info(dn->inode, fofs, dn->data_blkaddr))
801 sync_inode_page(dn);
802 }
803
804 struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
805 {
806 struct address_space *mapping = inode->i_mapping;
807 struct dnode_of_data dn;
808 struct page *page;
809 struct extent_info ei;
810 int err;
811 struct f2fs_io_info fio = {
812 .type = DATA,
813 .rw = sync ? READ_SYNC : READA,
814 };
815
816 page = find_get_page(mapping, index);
817 if (page && PageUptodate(page))
818 return page;
819 f2fs_put_page(page, 0);
820
821 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
822 dn.data_blkaddr = ei.blk + index - ei.fofs;
823 goto got_it;
824 }
825
826 set_new_dnode(&dn, inode, NULL, NULL, 0);
827 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
828 if (err)
829 return ERR_PTR(err);
830 f2fs_put_dnode(&dn);
831
832 if (dn.data_blkaddr == NULL_ADDR)
833 return ERR_PTR(-ENOENT);
834
835 /* By fallocate(), there is no cached page, but with NEW_ADDR */
836 if (unlikely(dn.data_blkaddr == NEW_ADDR))
837 return ERR_PTR(-EINVAL);
838
839 got_it:
840 page = grab_cache_page(mapping, index);
841 if (!page)
842 return ERR_PTR(-ENOMEM);
843
844 if (PageUptodate(page)) {
845 unlock_page(page);
846 return page;
847 }
848
849 fio.blk_addr = dn.data_blkaddr;
850 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
851 if (err)
852 return ERR_PTR(err);
853
854 if (sync) {
855 wait_on_page_locked(page);
856 if (unlikely(!PageUptodate(page))) {
857 f2fs_put_page(page, 0);
858 return ERR_PTR(-EIO);
859 }
860 }
861 return page;
862 }
863
864 /*
865 * If it tries to access a hole, return an error.
866 * Because, the callers, functions in dir.c and GC, should be able to know
867 * whether this page exists or not.
868 */
869 struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
870 {
871 struct address_space *mapping = inode->i_mapping;
872 struct dnode_of_data dn;
873 struct page *page;
874 struct extent_info ei;
875 int err;
876 struct f2fs_io_info fio = {
877 .type = DATA,
878 .rw = READ_SYNC,
879 };
880 repeat:
881 page = grab_cache_page(mapping, index);
882 if (!page)
883 return ERR_PTR(-ENOMEM);
884
885 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
886 dn.data_blkaddr = ei.blk + index - ei.fofs;
887 goto got_it;
888 }
889
890 set_new_dnode(&dn, inode, NULL, NULL, 0);
891 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
892 if (err) {
893 f2fs_put_page(page, 1);
894 return ERR_PTR(err);
895 }
896 f2fs_put_dnode(&dn);
897
898 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
899 f2fs_put_page(page, 1);
900 return ERR_PTR(-ENOENT);
901 }
902
903 got_it:
904 if (PageUptodate(page))
905 return page;
906
907 /*
908 * A new dentry page is allocated but not able to be written, since its
909 * new inode page couldn't be allocated due to -ENOSPC.
910 * In such the case, its blkaddr can be remained as NEW_ADDR.
911 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
912 */
913 if (dn.data_blkaddr == NEW_ADDR) {
914 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
915 SetPageUptodate(page);
916 return page;
917 }
918
919 fio.blk_addr = dn.data_blkaddr;
920 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
921 if (err)
922 return ERR_PTR(err);
923
924 lock_page(page);
925 if (unlikely(!PageUptodate(page))) {
926 f2fs_put_page(page, 1);
927 return ERR_PTR(-EIO);
928 }
929 if (unlikely(page->mapping != mapping)) {
930 f2fs_put_page(page, 1);
931 goto repeat;
932 }
933 return page;
934 }
935
936 /*
937 * Caller ensures that this data page is never allocated.
938 * A new zero-filled data page is allocated in the page cache.
939 *
940 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
941 * f2fs_unlock_op().
942 * Note that, ipage is set only by make_empty_dir.
943 */
944 struct page *get_new_data_page(struct inode *inode,
945 struct page *ipage, pgoff_t index, bool new_i_size)
946 {
947 struct address_space *mapping = inode->i_mapping;
948 struct page *page;
949 struct dnode_of_data dn;
950 int err;
951
952 set_new_dnode(&dn, inode, ipage, NULL, 0);
953 err = f2fs_reserve_block(&dn, index);
954 if (err)
955 return ERR_PTR(err);
956 repeat:
957 page = grab_cache_page(mapping, index);
958 if (!page) {
959 err = -ENOMEM;
960 goto put_err;
961 }
962
963 if (PageUptodate(page))
964 return page;
965
966 if (dn.data_blkaddr == NEW_ADDR) {
967 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
968 SetPageUptodate(page);
969 } else {
970 struct f2fs_io_info fio = {
971 .type = DATA,
972 .rw = READ_SYNC,
973 .blk_addr = dn.data_blkaddr,
974 };
975 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
976 if (err)
977 goto put_err;
978
979 lock_page(page);
980 if (unlikely(!PageUptodate(page))) {
981 f2fs_put_page(page, 1);
982 err = -EIO;
983 goto put_err;
984 }
985 if (unlikely(page->mapping != mapping)) {
986 f2fs_put_page(page, 1);
987 goto repeat;
988 }
989 }
990
991 if (new_i_size &&
992 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
993 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
994 /* Only the directory inode sets new_i_size */
995 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
996 }
997 return page;
998
999 put_err:
1000 f2fs_put_dnode(&dn);
1001 return ERR_PTR(err);
1002 }
1003
1004 static int __allocate_data_block(struct dnode_of_data *dn)
1005 {
1006 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1007 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
1008 struct f2fs_summary sum;
1009 struct node_info ni;
1010 int seg = CURSEG_WARM_DATA;
1011 pgoff_t fofs;
1012
1013 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
1014 return -EPERM;
1015 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
1016 return -ENOSPC;
1017
1018 get_node_info(sbi, dn->nid, &ni);
1019 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1020
1021 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
1022 seg = CURSEG_DIRECT_IO;
1023
1024 allocate_data_block(sbi, NULL, NULL_ADDR, &dn->data_blkaddr, &sum, seg);
1025
1026 /* direct IO doesn't use extent cache to maximize the performance */
1027 __set_data_blkaddr(dn);
1028
1029 /* update i_size */
1030 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
1031 dn->ofs_in_node;
1032 if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT))
1033 i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT));
1034
1035 return 0;
1036 }
1037
1038 static void __allocate_data_blocks(struct inode *inode, loff_t offset,
1039 size_t count)
1040 {
1041 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1042 struct dnode_of_data dn;
1043 u64 start = F2FS_BYTES_TO_BLK(offset);
1044 u64 len = F2FS_BYTES_TO_BLK(count);
1045 bool allocated;
1046 u64 end_offset;
1047
1048 while (len) {
1049 f2fs_balance_fs(sbi);
1050 f2fs_lock_op(sbi);
1051
1052 /* When reading holes, we need its node page */
1053 set_new_dnode(&dn, inode, NULL, NULL, 0);
1054 if (get_dnode_of_data(&dn, start, ALLOC_NODE))
1055 goto out;
1056
1057 allocated = false;
1058 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
1059
1060 while (dn.ofs_in_node < end_offset && len) {
1061 if (dn.data_blkaddr == NULL_ADDR) {
1062 if (__allocate_data_block(&dn))
1063 goto sync_out;
1064 allocated = true;
1065 }
1066 len--;
1067 start++;
1068 dn.ofs_in_node++;
1069 }
1070
1071 if (allocated)
1072 sync_inode_page(&dn);
1073
1074 f2fs_put_dnode(&dn);
1075 f2fs_unlock_op(sbi);
1076 }
1077 return;
1078
1079 sync_out:
1080 if (allocated)
1081 sync_inode_page(&dn);
1082 f2fs_put_dnode(&dn);
1083 out:
1084 f2fs_unlock_op(sbi);
1085 return;
1086 }
1087
1088 /*
1089 * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
1090 * If original data blocks are allocated, then give them to blockdev.
1091 * Otherwise,
1092 * a. preallocate requested block addresses
1093 * b. do not use extent cache for better performance
1094 * c. give the block addresses to blockdev
1095 */
1096 static int __get_data_block(struct inode *inode, sector_t iblock,
1097 struct buffer_head *bh_result, int create, bool fiemap)
1098 {
1099 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
1100 unsigned maxblocks = bh_result->b_size >> blkbits;
1101 struct dnode_of_data dn;
1102 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
1103 pgoff_t pgofs, end_offset;
1104 int err = 0, ofs = 1;
1105 struct extent_info ei;
1106 bool allocated = false;
1107
1108 /* Get the page offset from the block offset(iblock) */
1109 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
1110
1111 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
1112 f2fs_map_bh(inode->i_sb, pgofs, &ei, bh_result);
1113 goto out;
1114 }
1115
1116 if (create)
1117 f2fs_lock_op(F2FS_I_SB(inode));
1118
1119 /* When reading holes, we need its node page */
1120 set_new_dnode(&dn, inode, NULL, NULL, 0);
1121 err = get_dnode_of_data(&dn, pgofs, mode);
1122 if (err) {
1123 if (err == -ENOENT)
1124 err = 0;
1125 goto unlock_out;
1126 }
1127 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
1128 goto put_out;
1129
1130 if (dn.data_blkaddr != NULL_ADDR) {
1131 set_buffer_new(bh_result);
1132 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
1133 } else if (create) {
1134 err = __allocate_data_block(&dn);
1135 if (err)
1136 goto put_out;
1137 allocated = true;
1138 set_buffer_new(bh_result);
1139 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
1140 } else {
1141 goto put_out;
1142 }
1143
1144 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
1145 bh_result->b_size = (((size_t)1) << blkbits);
1146 dn.ofs_in_node++;
1147 pgofs++;
1148
1149 get_next:
1150 if (dn.ofs_in_node >= end_offset) {
1151 if (allocated)
1152 sync_inode_page(&dn);
1153 allocated = false;
1154 f2fs_put_dnode(&dn);
1155
1156 set_new_dnode(&dn, inode, NULL, NULL, 0);
1157 err = get_dnode_of_data(&dn, pgofs, mode);
1158 if (err) {
1159 if (err == -ENOENT)
1160 err = 0;
1161 goto unlock_out;
1162 }
1163 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
1164 goto put_out;
1165
1166 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
1167 }
1168
1169 if (maxblocks > (bh_result->b_size >> blkbits)) {
1170 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
1171 if (blkaddr == NULL_ADDR && create) {
1172 err = __allocate_data_block(&dn);
1173 if (err)
1174 goto sync_out;
1175 allocated = true;
1176 blkaddr = dn.data_blkaddr;
1177 }
1178 /* Give more consecutive addresses for the readahead */
1179 if (blkaddr == (bh_result->b_blocknr + ofs)) {
1180 ofs++;
1181 dn.ofs_in_node++;
1182 pgofs++;
1183 bh_result->b_size += (((size_t)1) << blkbits);
1184 goto get_next;
1185 }
1186 }
1187 sync_out:
1188 if (allocated)
1189 sync_inode_page(&dn);
1190 put_out:
1191 f2fs_put_dnode(&dn);
1192 unlock_out:
1193 if (create)
1194 f2fs_unlock_op(F2FS_I_SB(inode));
1195 out:
1196 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
1197 return err;
1198 }
1199
1200 static int get_data_block(struct inode *inode, sector_t iblock,
1201 struct buffer_head *bh_result, int create)
1202 {
1203 return __get_data_block(inode, iblock, bh_result, create, false);
1204 }
1205
1206 static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
1207 struct buffer_head *bh_result, int create)
1208 {
1209 return __get_data_block(inode, iblock, bh_result, create, true);
1210 }
1211
1212 int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1213 u64 start, u64 len)
1214 {
1215 return generic_block_fiemap(inode, fieinfo,
1216 start, len, get_data_block_fiemap);
1217 }
1218
1219 static int f2fs_read_data_page(struct file *file, struct page *page)
1220 {
1221 struct inode *inode = page->mapping->host;
1222 int ret = -EAGAIN;
1223
1224 trace_f2fs_readpage(page, DATA);
1225
1226 /* If the file has inline data, try to read it directly */
1227 if (f2fs_has_inline_data(inode))
1228 ret = f2fs_read_inline_data(inode, page);
1229 if (ret == -EAGAIN)
1230 ret = mpage_readpage(page, get_data_block);
1231
1232 return ret;
1233 }
1234
1235 static int f2fs_read_data_pages(struct file *file,
1236 struct address_space *mapping,
1237 struct list_head *pages, unsigned nr_pages)
1238 {
1239 struct inode *inode = file->f_mapping->host;
1240
1241 /* If the file has inline data, skip readpages */
1242 if (f2fs_has_inline_data(inode))
1243 return 0;
1244
1245 return mpage_readpages(mapping, pages, nr_pages, get_data_block);
1246 }
1247
1248 int do_write_data_page(struct page *page, struct f2fs_io_info *fio)
1249 {
1250 struct inode *inode = page->mapping->host;
1251 struct dnode_of_data dn;
1252 int err = 0;
1253
1254 set_new_dnode(&dn, inode, NULL, NULL, 0);
1255 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
1256 if (err)
1257 return err;
1258
1259 fio->blk_addr = dn.data_blkaddr;
1260
1261 /* This page is already truncated */
1262 if (fio->blk_addr == NULL_ADDR)
1263 goto out_writepage;
1264
1265 set_page_writeback(page);
1266
1267 /*
1268 * If current allocation needs SSR,
1269 * it had better in-place writes for updated data.
1270 */
1271 if (unlikely(fio->blk_addr != NEW_ADDR &&
1272 !is_cold_data(page) &&
1273 need_inplace_update(inode))) {
1274 rewrite_data_page(page, fio);
1275 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
1276 } else {
1277 write_data_page(page, &dn, fio);
1278 f2fs_update_extent_cache(&dn);
1279 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
1280 }
1281 out_writepage:
1282 f2fs_put_dnode(&dn);
1283 return err;
1284 }
1285
1286 static int f2fs_write_data_page(struct page *page,
1287 struct writeback_control *wbc)
1288 {
1289 struct inode *inode = page->mapping->host;
1290 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1291 loff_t i_size = i_size_read(inode);
1292 const pgoff_t end_index = ((unsigned long long) i_size)
1293 >> PAGE_CACHE_SHIFT;
1294 unsigned offset = 0;
1295 bool need_balance_fs = false;
1296 int err = 0;
1297 struct f2fs_io_info fio = {
1298 .type = DATA,
1299 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
1300 };
1301
1302 trace_f2fs_writepage(page, DATA);
1303
1304 if (page->index < end_index)
1305 goto write;
1306
1307 /*
1308 * If the offset is out-of-range of file size,
1309 * this page does not have to be written to disk.
1310 */
1311 offset = i_size & (PAGE_CACHE_SIZE - 1);
1312 if ((page->index >= end_index + 1) || !offset)
1313 goto out;
1314
1315 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
1316 write:
1317 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1318 goto redirty_out;
1319 if (f2fs_is_drop_cache(inode))
1320 goto out;
1321 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1322 available_free_memory(sbi, BASE_CHECK))
1323 goto redirty_out;
1324
1325 /* Dentry blocks are controlled by checkpoint */
1326 if (S_ISDIR(inode->i_mode)) {
1327 if (unlikely(f2fs_cp_error(sbi)))
1328 goto redirty_out;
1329 err = do_write_data_page(page, &fio);
1330 goto done;
1331 }
1332
1333 /* we should bypass data pages to proceed the kworkder jobs */
1334 if (unlikely(f2fs_cp_error(sbi))) {
1335 SetPageError(page);
1336 goto out;
1337 }
1338
1339 if (!wbc->for_reclaim)
1340 need_balance_fs = true;
1341 else if (has_not_enough_free_secs(sbi, 0))
1342 goto redirty_out;
1343
1344 err = -EAGAIN;
1345 f2fs_lock_op(sbi);
1346 if (f2fs_has_inline_data(inode))
1347 err = f2fs_write_inline_data(inode, page);
1348 if (err == -EAGAIN)
1349 err = do_write_data_page(page, &fio);
1350 f2fs_unlock_op(sbi);
1351 done:
1352 if (err && err != -ENOENT)
1353 goto redirty_out;
1354
1355 clear_cold_data(page);
1356 out:
1357 inode_dec_dirty_pages(inode);
1358 unlock_page(page);
1359 if (need_balance_fs)
1360 f2fs_balance_fs(sbi);
1361 if (wbc->for_reclaim)
1362 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1363 return 0;
1364
1365 redirty_out:
1366 redirty_page_for_writepage(wbc, page);
1367 return AOP_WRITEPAGE_ACTIVATE;
1368 }
1369
1370 static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1371 void *data)
1372 {
1373 struct address_space *mapping = data;
1374 int ret = mapping->a_ops->writepage(page, wbc);
1375 mapping_set_error(mapping, ret);
1376 return ret;
1377 }
1378
1379 static int f2fs_write_data_pages(struct address_space *mapping,
1380 struct writeback_control *wbc)
1381 {
1382 struct inode *inode = mapping->host;
1383 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1384 bool locked = false;
1385 int ret;
1386 long diff;
1387
1388 trace_f2fs_writepages(mapping->host, wbc, DATA);
1389
1390 /* deal with chardevs and other special file */
1391 if (!mapping->a_ops->writepage)
1392 return 0;
1393
1394 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1395 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1396 available_free_memory(sbi, DIRTY_DENTS))
1397 goto skip_write;
1398
1399 diff = nr_pages_to_write(sbi, DATA, wbc);
1400
1401 if (!S_ISDIR(inode->i_mode)) {
1402 mutex_lock(&sbi->writepages);
1403 locked = true;
1404 }
1405 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
1406 if (locked)
1407 mutex_unlock(&sbi->writepages);
1408
1409 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1410
1411 remove_dirty_dir_inode(inode);
1412
1413 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
1414 return ret;
1415
1416 skip_write:
1417 wbc->pages_skipped += get_dirty_pages(inode);
1418 return 0;
1419 }
1420
1421 static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1422 {
1423 struct inode *inode = mapping->host;
1424
1425 if (to > inode->i_size) {
1426 truncate_pagecache(inode, inode->i_size);
1427 truncate_blocks(inode, inode->i_size, true);
1428 }
1429 }
1430
1431 static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1432 loff_t pos, unsigned len, unsigned flags,
1433 struct page **pagep, void **fsdata)
1434 {
1435 struct inode *inode = mapping->host;
1436 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1437 struct page *page, *ipage;
1438 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
1439 struct dnode_of_data dn;
1440 int err = 0;
1441
1442 trace_f2fs_write_begin(inode, pos, len, flags);
1443
1444 f2fs_balance_fs(sbi);
1445
1446 /*
1447 * We should check this at this moment to avoid deadlock on inode page
1448 * and #0 page. The locking rule for inline_data conversion should be:
1449 * lock_page(page #0) -> lock_page(inode_page)
1450 */
1451 if (index != 0) {
1452 err = f2fs_convert_inline_inode(inode);
1453 if (err)
1454 goto fail;
1455 }
1456 repeat:
1457 page = grab_cache_page_write_begin(mapping, index, flags);
1458 if (!page) {
1459 err = -ENOMEM;
1460 goto fail;
1461 }
1462
1463 *pagep = page;
1464
1465 f2fs_lock_op(sbi);
1466
1467 /* check inline_data */
1468 ipage = get_node_page(sbi, inode->i_ino);
1469 if (IS_ERR(ipage)) {
1470 err = PTR_ERR(ipage);
1471 goto unlock_fail;
1472 }
1473
1474 set_new_dnode(&dn, inode, ipage, ipage, 0);
1475
1476 if (f2fs_has_inline_data(inode)) {
1477 if (pos + len <= MAX_INLINE_DATA) {
1478 read_inline_data(page, ipage);
1479 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1480 sync_inode_page(&dn);
1481 goto put_next;
1482 }
1483 err = f2fs_convert_inline_page(&dn, page);
1484 if (err)
1485 goto put_fail;
1486 }
1487 err = f2fs_reserve_block(&dn, index);
1488 if (err)
1489 goto put_fail;
1490 put_next:
1491 f2fs_put_dnode(&dn);
1492 f2fs_unlock_op(sbi);
1493
1494 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
1495 return 0;
1496
1497 f2fs_wait_on_page_writeback(page, DATA);
1498
1499 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1500 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1501 unsigned end = start + len;
1502
1503 /* Reading beyond i_size is simple: memset to zero */
1504 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
1505 goto out;
1506 }
1507
1508 if (dn.data_blkaddr == NEW_ADDR) {
1509 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1510 } else {
1511 struct f2fs_io_info fio = {
1512 .type = DATA,
1513 .rw = READ_SYNC,
1514 .blk_addr = dn.data_blkaddr,
1515 };
1516 err = f2fs_submit_page_bio(sbi, page, &fio);
1517 if (err)
1518 goto fail;
1519
1520 lock_page(page);
1521 if (unlikely(!PageUptodate(page))) {
1522 f2fs_put_page(page, 1);
1523 err = -EIO;
1524 goto fail;
1525 }
1526 if (unlikely(page->mapping != mapping)) {
1527 f2fs_put_page(page, 1);
1528 goto repeat;
1529 }
1530 }
1531 out:
1532 SetPageUptodate(page);
1533 clear_cold_data(page);
1534 return 0;
1535
1536 put_fail:
1537 f2fs_put_dnode(&dn);
1538 unlock_fail:
1539 f2fs_unlock_op(sbi);
1540 f2fs_put_page(page, 1);
1541 fail:
1542 f2fs_write_failed(mapping, pos + len);
1543 return err;
1544 }
1545
1546 static int f2fs_write_end(struct file *file,
1547 struct address_space *mapping,
1548 loff_t pos, unsigned len, unsigned copied,
1549 struct page *page, void *fsdata)
1550 {
1551 struct inode *inode = page->mapping->host;
1552
1553 trace_f2fs_write_end(inode, pos, len, copied);
1554
1555 set_page_dirty(page);
1556
1557 if (pos + copied > i_size_read(inode)) {
1558 i_size_write(inode, pos + copied);
1559 mark_inode_dirty(inode);
1560 update_inode_page(inode);
1561 }
1562
1563 f2fs_put_page(page, 1);
1564 return copied;
1565 }
1566
1567 static int check_direct_IO(struct inode *inode, int rw,
1568 struct iov_iter *iter, loff_t offset)
1569 {
1570 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
1571
1572 if (rw == READ)
1573 return 0;
1574
1575 if (offset & blocksize_mask)
1576 return -EINVAL;
1577
1578 if (iov_iter_alignment(iter) & blocksize_mask)
1579 return -EINVAL;
1580
1581 return 0;
1582 }
1583
1584 static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
1585 struct iov_iter *iter, loff_t offset)
1586 {
1587 struct file *file = iocb->ki_filp;
1588 struct address_space *mapping = file->f_mapping;
1589 struct inode *inode = mapping->host;
1590 size_t count = iov_iter_count(iter);
1591 int err;
1592
1593 /* we don't need to use inline_data strictly */
1594 if (f2fs_has_inline_data(inode)) {
1595 err = f2fs_convert_inline_inode(inode);
1596 if (err)
1597 return err;
1598 }
1599
1600 if (check_direct_IO(inode, rw, iter, offset))
1601 return 0;
1602
1603 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
1604
1605 if (rw & WRITE)
1606 __allocate_data_blocks(inode, offset, count);
1607
1608 err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block);
1609 if (err < 0 && (rw & WRITE))
1610 f2fs_write_failed(mapping, offset + count);
1611
1612 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
1613
1614 return err;
1615 }
1616
1617 void f2fs_invalidate_page(struct page *page, unsigned int offset,
1618 unsigned int length)
1619 {
1620 struct inode *inode = page->mapping->host;
1621 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1622
1623 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1624 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
1625 return;
1626
1627 if (PageDirty(page)) {
1628 if (inode->i_ino == F2FS_META_INO(sbi))
1629 dec_page_count(sbi, F2FS_DIRTY_META);
1630 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1631 dec_page_count(sbi, F2FS_DIRTY_NODES);
1632 else
1633 inode_dec_dirty_pages(inode);
1634 }
1635 ClearPagePrivate(page);
1636 }
1637
1638 int f2fs_release_page(struct page *page, gfp_t wait)
1639 {
1640 /* If this is dirty page, keep PagePrivate */
1641 if (PageDirty(page))
1642 return 0;
1643
1644 ClearPagePrivate(page);
1645 return 1;
1646 }
1647
1648 static int f2fs_set_data_page_dirty(struct page *page)
1649 {
1650 struct address_space *mapping = page->mapping;
1651 struct inode *inode = mapping->host;
1652
1653 trace_f2fs_set_page_dirty(page, DATA);
1654
1655 SetPageUptodate(page);
1656
1657 if (f2fs_is_atomic_file(inode)) {
1658 register_inmem_page(inode, page);
1659 return 1;
1660 }
1661
1662 mark_inode_dirty(inode);
1663
1664 if (!PageDirty(page)) {
1665 __set_page_dirty_nobuffers(page);
1666 update_dirty_page(inode, page);
1667 return 1;
1668 }
1669 return 0;
1670 }
1671
1672 static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1673 {
1674 struct inode *inode = mapping->host;
1675
1676 /* we don't need to use inline_data strictly */
1677 if (f2fs_has_inline_data(inode)) {
1678 int err = f2fs_convert_inline_inode(inode);
1679 if (err)
1680 return err;
1681 }
1682 return generic_block_bmap(mapping, block, get_data_block);
1683 }
1684
1685 void init_extent_cache_info(struct f2fs_sb_info *sbi)
1686 {
1687 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
1688 init_rwsem(&sbi->extent_tree_lock);
1689 INIT_LIST_HEAD(&sbi->extent_list);
1690 spin_lock_init(&sbi->extent_lock);
1691 sbi->total_ext_tree = 0;
1692 atomic_set(&sbi->total_ext_node, 0);
1693 }
1694
1695 int __init create_extent_cache(void)
1696 {
1697 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
1698 sizeof(struct extent_tree));
1699 if (!extent_tree_slab)
1700 return -ENOMEM;
1701 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
1702 sizeof(struct extent_node));
1703 if (!extent_node_slab) {
1704 kmem_cache_destroy(extent_tree_slab);
1705 return -ENOMEM;
1706 }
1707 return 0;
1708 }
1709
1710 void destroy_extent_cache(void)
1711 {
1712 kmem_cache_destroy(extent_node_slab);
1713 kmem_cache_destroy(extent_tree_slab);
1714 }
1715
1716 const struct address_space_operations f2fs_dblock_aops = {
1717 .readpage = f2fs_read_data_page,
1718 .readpages = f2fs_read_data_pages,
1719 .writepage = f2fs_write_data_page,
1720 .writepages = f2fs_write_data_pages,
1721 .write_begin = f2fs_write_begin,
1722 .write_end = f2fs_write_end,
1723 .set_page_dirty = f2fs_set_data_page_dirty,
1724 .invalidatepage = f2fs_invalidate_page,
1725 .releasepage = f2fs_release_page,
1726 .direct_IO = f2fs_direct_IO,
1727 .bmap = f2fs_bmap,
1728 };