blob: 67b3a99e03d53edad5953b46600e1d6f06ae2473 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002 * 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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070015#include <linux/aio.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090016#include <linux/writeback.h>
17#include <linux/backing-dev.h>
18#include <linux/blkdev.h>
19#include <linux/bio.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010020#include <linux/prefetch.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090021
22#include "f2fs.h"
23#include "node.h"
24#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080025#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090026#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090027
Chao Yu429511c2015-02-05 17:54:31 +080028static struct kmem_cache *extent_tree_slab;
29static struct kmem_cache *extent_node_slab;
30
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090031static void f2fs_read_end_io(struct bio *bio, int err)
32{
Linus Torvaldsf5688492014-01-30 11:19:05 -080033 struct bio_vec *bvec;
34 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090035
Linus Torvaldsf5688492014-01-30 11:19:05 -080036 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090037 struct page *page = bvec->bv_page;
38
Linus Torvaldsf5688492014-01-30 11:19:05 -080039 if (!err) {
40 SetPageUptodate(page);
41 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090042 ClearPageUptodate(page);
43 SetPageError(page);
44 }
45 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080046 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090047 bio_put(bio);
48}
49
50static void f2fs_write_end_io(struct bio *bio, int err)
51{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090052 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080053 struct bio_vec *bvec;
54 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090055
Linus Torvaldsf5688492014-01-30 11:19:05 -080056 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090057 struct page *page = bvec->bv_page;
58
Linus Torvaldsf5688492014-01-30 11:19:05 -080059 if (unlikely(err)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -070060 set_page_dirty(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090061 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim744602c2014-01-24 09:42:16 +090062 f2fs_stop_checkpoint(sbi);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090063 }
64 end_page_writeback(page);
65 dec_page_count(sbi, F2FS_WRITEBACK);
Linus Torvaldsf5688492014-01-30 11:19:05 -080066 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090067
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090068 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
Gu Zheng940a6d32013-12-20 17:39:59 +080075/*
76 * Low-level block read/write IO operations.
77 */
78static 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;
Chao Yu55cf9cb2014-09-15 18:01:10 +080087 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +080088 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090089 bio->bi_private = sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +080090
91 return bio;
92}
93
Jaegeuk Kim458e6192013-12-11 13:54:01 +090094static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090095{
Jaegeuk Kim458e6192013-12-11 13:54:01 +090096 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090097
98 if (!io->bio)
99 return;
100
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700101 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800102 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700103 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800104 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900105
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700106 submit_bio(fio->rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900107 io->bio = NULL;
108}
109
110void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900111 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900112{
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
Chao Yudf0f8dc2014-03-22 14:57:23 +0800118 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900119
120 /* change META to META_FLUSH in the checkpoint procedure */
121 if (type >= META_FLUSH) {
122 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700123 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;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900127 }
128 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800129 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900130}
131
132/*
133 * Fill the locked page with data located in the block address.
134 * Return unlocked page.
135 */
136int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800137 struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900138{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900139 struct bio *bio;
140
Chao Yu2ace38e2014-12-24 16:08:14 +0800141 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -0800142 f2fs_trace_ios(page, fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900143
144 /* Allocate a new bio */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800145 bio = __bio_alloc(sbi, fio->blk_addr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900146
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
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800153 submit_bio(fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900154 return 0;
155}
156
157void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800158 struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900159{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900160 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900161 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800162 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900163
Gu Zheng940a6d32013-12-20 17:39:59 +0800164 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900165
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800166 verify_block_addr(sbi, fio->blk_addr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900167
Chao Yudf0f8dc2014-03-22 14:57:23 +0800168 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900169
Gu Zheng940a6d32013-12-20 17:39:59 +0800170 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900171 inc_page_count(sbi, F2FS_WRITEBACK);
172
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800173 if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900174 io->fio.rw != fio->rw))
175 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900176alloc_new:
177 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700178 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800179
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800180 io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900181 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900182 }
183
184 if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
185 PAGE_CACHE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900186 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900187 goto alloc_new;
188 }
189
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800190 io->last_block_in_bio = fio->blk_addr;
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -0800191 f2fs_trace_ios(page, fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900192
Chao Yudf0f8dc2014-03-22 14:57:23 +0800193 up_write(&io->io_rwsem);
Chao Yu2ace38e2014-12-24 16:08:14 +0800194 trace_f2fs_submit_page_mbio(page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900195}
196
197/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900198 * Lock ordering for the change of data block address:
199 * ->data_page
200 * ->node_page
201 * update block addresses in the node page
202 */
Chao Yu216a6202015-03-19 19:23:32 +0800203void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900204{
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
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000210 f2fs_wait_on_page_writeback(node_page, NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900211
Gu Zheng45590712013-07-15 17:57:38 +0800212 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900213
214 /* Get physical address of data block */
215 addr_array = blkaddr_in_node(rn);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800216 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900217 set_page_dirty(node_page);
218}
219
220int reserve_new_block(struct dnode_of_data *dn)
221{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700222 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900223
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900224 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900225 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800226 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900227 return -ENOSPC;
228
Namjae Jeonc01e2852013-04-23 17:00:52 +0900229 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
230
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900231 dn->data_blkaddr = NEW_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800232 set_data_blkaddr(dn);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900233 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900234 sync_inode_page(dn);
235 return 0;
236}
237
Huajun Lib6009652013-11-10 23:13:18 +0800238int 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;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900246
Huajun Lib6009652013-11-10 23:13:18 +0800247 if (dn->data_blkaddr == NULL_ADDR)
248 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900249 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800250 f2fs_put_dnode(dn);
251 return err;
252}
253
Chao Yua2e7d1b2015-02-05 17:50:30 +0800254static 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
Jaegeuk Kim3402e872015-03-11 23:27:25 -0400260 clear_buffer_new(bh_result);
Chao Yua2e7d1b2015-02-05 17:50:30 +0800261 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
Chao Yu7e4dde72015-02-05 17:51:34 +0800269static bool lookup_extent_info(struct inode *inode, pgoff_t pgofs,
270 struct extent_info *ei)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900271{
272 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900273 pgoff_t start_fofs, end_fofs;
274 block_t start_blkaddr;
275
Chao Yu0c872e22015-02-05 17:46:29 +0800276 read_lock(&fi->ext_lock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900277 if (fi->ext.len == 0) {
Chao Yu0c872e22015-02-05 17:46:29 +0800278 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800279 return false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900280 }
281
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900282 stat_inc_total_hit(inode->i_sb);
283
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900284 start_fofs = fi->ext.fofs;
285 end_fofs = fi->ext.fofs + fi->ext.len - 1;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800286 start_blkaddr = fi->ext.blk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900287
288 if (pgofs >= start_fofs && pgofs <= end_fofs) {
Chao Yua2e7d1b2015-02-05 17:50:30 +0800289 *ei = fi->ext;
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900290 stat_inc_read_hit(inode->i_sb);
Chao Yu0c872e22015-02-05 17:46:29 +0800291 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800292 return true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900293 }
Chao Yu0c872e22015-02-05 17:46:29 +0800294 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800295 return false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900296}
297
Chao Yu7e4dde72015-02-05 17:51:34 +0800298static bool update_extent_info(struct inode *inode, pgoff_t fofs,
299 block_t blkaddr)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900300{
Chao Yu7e4dde72015-02-05 17:51:34 +0800301 struct f2fs_inode_info *fi = F2FS_I(inode);
302 pgoff_t start_fofs, end_fofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900303 block_t start_blkaddr, end_blkaddr;
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900304 int need_update = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900305
Chao Yu0c872e22015-02-05 17:46:29 +0800306 write_lock(&fi->ext_lock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900307
308 start_fofs = fi->ext.fofs;
309 end_fofs = fi->ext.fofs + fi->ext.len - 1;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800310 start_blkaddr = fi->ext.blk;
311 end_blkaddr = fi->ext.blk + fi->ext.len - 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900312
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) {
Chao Yu7e4dde72015-02-05 17:51:34 +0800319 if (blkaddr != NULL_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900320 fi->ext.fofs = fofs;
Chao Yu7e4dde72015-02-05 17:51:34 +0800321 fi->ext.blk = blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900322 fi->ext.len = 1;
323 }
324 goto end_update;
325 }
326
Namjae Jeon6224da82013-04-06 14:44:32 +0900327 /* Front merge */
Chao Yu7e4dde72015-02-05 17:51:34 +0800328 if (fofs == start_fofs - 1 && blkaddr == start_blkaddr - 1) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900329 fi->ext.fofs--;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800330 fi->ext.blk--;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900331 fi->ext.len++;
332 goto end_update;
333 }
334
335 /* Back merge */
Chao Yu7e4dde72015-02-05 17:51:34 +0800336 if (fofs == end_fofs + 1 && blkaddr == end_blkaddr + 1) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900337 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;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800348 fi->ext.blk = start_blkaddr + fofs - start_fofs + 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900349 fi->ext.len -= fofs - start_fofs + 1;
350 }
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900351 } else {
352 need_update = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900353 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900354
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900355 /* 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 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900361end_update:
Chao Yu0c872e22015-02-05 17:46:29 +0800362 write_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800363 return need_update;
364}
365
Chao Yu429511c2015-02-05 17:54:31 +0800366static 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
386static 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);
Chao Yu62c8af62015-02-05 18:01:39 +0800392
393 if (et->cached_en == en)
394 et->cached_en = NULL;
Chao Yu429511c2015-02-05 17:54:31 +0800395}
396
Chao Yu93dfc522015-03-19 19:24:59 +0800397static struct extent_tree *__find_extent_tree(struct f2fs_sb_info *sbi,
398 nid_t ino)
399{
400 struct extent_tree *et;
401
402 down_read(&sbi->extent_tree_lock);
403 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
404 if (!et) {
405 up_read(&sbi->extent_tree_lock);
406 return NULL;
407 }
408 atomic_inc(&et->refcount);
409 up_read(&sbi->extent_tree_lock);
410
411 return et;
412}
413
414static struct extent_tree *__grab_extent_tree(struct inode *inode)
415{
416 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
417 struct extent_tree *et;
418 nid_t ino = inode->i_ino;
419
420 down_write(&sbi->extent_tree_lock);
421 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
422 if (!et) {
423 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
424 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
425 memset(et, 0, sizeof(struct extent_tree));
426 et->ino = ino;
427 et->root = RB_ROOT;
428 et->cached_en = NULL;
429 rwlock_init(&et->lock);
430 atomic_set(&et->refcount, 0);
431 et->count = 0;
432 sbi->total_ext_tree++;
433 }
434 atomic_inc(&et->refcount);
435 up_write(&sbi->extent_tree_lock);
436
437 return et;
438}
439
Chao Yu429511c2015-02-05 17:54:31 +0800440static struct extent_node *__lookup_extent_tree(struct extent_tree *et,
441 unsigned int fofs)
442{
443 struct rb_node *node = et->root.rb_node;
444 struct extent_node *en;
445
Chao Yu62c8af62015-02-05 18:01:39 +0800446 if (et->cached_en) {
447 struct extent_info *cei = &et->cached_en->ei;
448
449 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
450 return et->cached_en;
451 }
452
Chao Yu429511c2015-02-05 17:54:31 +0800453 while (node) {
454 en = rb_entry(node, struct extent_node, rb_node);
455
Chao Yu62c8af62015-02-05 18:01:39 +0800456 if (fofs < en->ei.fofs) {
Chao Yu429511c2015-02-05 17:54:31 +0800457 node = node->rb_left;
Chao Yu62c8af62015-02-05 18:01:39 +0800458 } else if (fofs >= en->ei.fofs + en->ei.len) {
Chao Yu429511c2015-02-05 17:54:31 +0800459 node = node->rb_right;
Chao Yu62c8af62015-02-05 18:01:39 +0800460 } else {
461 et->cached_en = en;
Chao Yu429511c2015-02-05 17:54:31 +0800462 return en;
Chao Yu62c8af62015-02-05 18:01:39 +0800463 }
Chao Yu429511c2015-02-05 17:54:31 +0800464 }
465 return NULL;
466}
467
468static struct extent_node *__try_back_merge(struct f2fs_sb_info *sbi,
469 struct extent_tree *et, struct extent_node *en)
470{
471 struct extent_node *prev;
472 struct rb_node *node;
473
474 node = rb_prev(&en->rb_node);
475 if (!node)
476 return NULL;
477
478 prev = rb_entry(node, struct extent_node, rb_node);
479 if (__is_back_mergeable(&en->ei, &prev->ei)) {
480 en->ei.fofs = prev->ei.fofs;
481 en->ei.blk = prev->ei.blk;
482 en->ei.len += prev->ei.len;
483 __detach_extent_node(sbi, et, prev);
484 return prev;
485 }
486 return NULL;
487}
488
489static struct extent_node *__try_front_merge(struct f2fs_sb_info *sbi,
490 struct extent_tree *et, struct extent_node *en)
491{
492 struct extent_node *next;
493 struct rb_node *node;
494
495 node = rb_next(&en->rb_node);
496 if (!node)
497 return NULL;
498
499 next = rb_entry(node, struct extent_node, rb_node);
500 if (__is_front_mergeable(&en->ei, &next->ei)) {
501 en->ei.len += next->ei.len;
502 __detach_extent_node(sbi, et, next);
503 return next;
504 }
505 return NULL;
506}
507
508static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
509 struct extent_tree *et, struct extent_info *ei,
510 struct extent_node **den)
511{
512 struct rb_node **p = &et->root.rb_node;
513 struct rb_node *parent = NULL;
514 struct extent_node *en;
515
516 while (*p) {
517 parent = *p;
518 en = rb_entry(parent, struct extent_node, rb_node);
519
520 if (ei->fofs < en->ei.fofs) {
521 if (__is_front_mergeable(ei, &en->ei)) {
522 f2fs_bug_on(sbi, !den);
523 en->ei.fofs = ei->fofs;
524 en->ei.blk = ei->blk;
525 en->ei.len += ei->len;
526 *den = __try_back_merge(sbi, et, en);
527 return en;
528 }
529 p = &(*p)->rb_left;
530 } else if (ei->fofs >= en->ei.fofs + en->ei.len) {
531 if (__is_back_mergeable(ei, &en->ei)) {
532 f2fs_bug_on(sbi, !den);
533 en->ei.len += ei->len;
534 *den = __try_front_merge(sbi, et, en);
535 return en;
536 }
537 p = &(*p)->rb_right;
538 } else {
539 f2fs_bug_on(sbi, 1);
540 }
541 }
542
543 return __attach_extent_node(sbi, et, ei, parent, p);
544}
545
546static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
547 struct extent_tree *et, bool free_all)
548{
549 struct rb_node *node, *next;
550 struct extent_node *en;
551 unsigned int count = et->count;
552
553 node = rb_first(&et->root);
554 while (node) {
555 next = rb_next(node);
556 en = rb_entry(node, struct extent_node, rb_node);
557
558 if (free_all) {
559 spin_lock(&sbi->extent_lock);
560 if (!list_empty(&en->list))
561 list_del_init(&en->list);
562 spin_unlock(&sbi->extent_lock);
563 }
564
565 if (free_all || list_empty(&en->list)) {
566 __detach_extent_node(sbi, et, en);
567 kmem_cache_free(extent_node_slab, en);
568 }
569 node = next;
570 }
571
572 return count - et->count;
573}
574
575static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
576 struct extent_info *ei)
577{
578 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
579 struct extent_tree *et;
580 struct extent_node *en;
581
Chao Yu1ec46102015-02-05 17:59:59 +0800582 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
583
Chao Yu93dfc522015-03-19 19:24:59 +0800584 et = __find_extent_tree(sbi, inode->i_ino);
585 if (!et)
Chao Yu429511c2015-02-05 17:54:31 +0800586 return false;
Chao Yu429511c2015-02-05 17:54:31 +0800587
588 read_lock(&et->lock);
589 en = __lookup_extent_tree(et, pgofs);
590 if (en) {
591 *ei = en->ei;
592 spin_lock(&sbi->extent_lock);
593 if (!list_empty(&en->list))
594 list_move_tail(&en->list, &sbi->extent_list);
595 spin_unlock(&sbi->extent_lock);
596 stat_inc_read_hit(sbi->sb);
597 }
598 stat_inc_total_hit(sbi->sb);
599 read_unlock(&et->lock);
600
Chao Yu1ec46102015-02-05 17:59:59 +0800601 trace_f2fs_lookup_extent_tree_end(inode, pgofs, en);
602
Chao Yu429511c2015-02-05 17:54:31 +0800603 atomic_dec(&et->refcount);
604 return en ? true : false;
605}
606
607static void f2fs_update_extent_tree(struct inode *inode, pgoff_t fofs,
608 block_t blkaddr)
609{
610 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu429511c2015-02-05 17:54:31 +0800611 struct extent_tree *et;
612 struct extent_node *en = NULL, *en1 = NULL, *en2 = NULL, *en3 = NULL;
613 struct extent_node *den = NULL;
614 struct extent_info ei, dei;
615 unsigned int endofs;
616
Chao Yu1ec46102015-02-05 17:59:59 +0800617 trace_f2fs_update_extent_tree(inode, fofs, blkaddr);
618
Chao Yu93dfc522015-03-19 19:24:59 +0800619 et = __grab_extent_tree(inode);
Chao Yu429511c2015-02-05 17:54:31 +0800620
621 write_lock(&et->lock);
622
623 /* 1. lookup and remove existing extent info in cache */
624 en = __lookup_extent_tree(et, fofs);
625 if (!en)
626 goto update_extent;
627
628 dei = en->ei;
629 __detach_extent_node(sbi, et, en);
630
631 /* 2. if extent can be split more, split and insert the left part */
632 if (dei.len > 1) {
633 /* insert left part of split extent into cache */
634 if (fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
635 set_extent_info(&ei, dei.fofs, dei.blk,
636 fofs - dei.fofs);
637 en1 = __insert_extent_tree(sbi, et, &ei, NULL);
638 }
639
640 /* insert right part of split extent into cache */
641 endofs = dei.fofs + dei.len - 1;
642 if (endofs - fofs >= F2FS_MIN_EXTENT_LEN) {
643 set_extent_info(&ei, fofs + 1,
644 fofs - dei.fofs + dei.blk, endofs - fofs);
645 en2 = __insert_extent_tree(sbi, et, &ei, NULL);
646 }
647 }
648
649update_extent:
650 /* 3. update extent in extent cache */
651 if (blkaddr) {
652 set_extent_info(&ei, fofs, blkaddr, 1);
653 en3 = __insert_extent_tree(sbi, et, &ei, &den);
654 }
655
656 /* 4. update in global extent list */
657 spin_lock(&sbi->extent_lock);
658 if (en && !list_empty(&en->list))
659 list_del(&en->list);
660 /*
661 * en1 and en2 split from en, they will become more and more smaller
662 * fragments after splitting several times. So if the length is smaller
663 * than F2FS_MIN_EXTENT_LEN, we will not add them into extent tree.
664 */
665 if (en1)
666 list_add_tail(&en1->list, &sbi->extent_list);
667 if (en2)
668 list_add_tail(&en2->list, &sbi->extent_list);
669 if (en3) {
670 if (list_empty(&en3->list))
671 list_add_tail(&en3->list, &sbi->extent_list);
672 else
673 list_move_tail(&en3->list, &sbi->extent_list);
674 }
675 if (den && !list_empty(&den->list))
676 list_del(&den->list);
677 spin_unlock(&sbi->extent_lock);
678
679 /* 5. release extent node */
680 if (en)
681 kmem_cache_free(extent_node_slab, en);
682 if (den)
683 kmem_cache_free(extent_node_slab, den);
684
685 write_unlock(&et->lock);
686 atomic_dec(&et->refcount);
687}
688
689void f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
690{
691 struct extent_tree *treevec[EXT_TREE_VEC_SIZE];
692 struct extent_node *en, *tmp;
693 unsigned long ino = F2FS_ROOT_INO(sbi);
694 struct radix_tree_iter iter;
695 void **slot;
696 unsigned int found;
Chao Yu1ec46102015-02-05 17:59:59 +0800697 unsigned int node_cnt = 0, tree_cnt = 0;
Chao Yu429511c2015-02-05 17:54:31 +0800698
Chao Yu1dcc3362015-02-05 17:57:31 +0800699 if (!test_opt(sbi, EXTENT_CACHE))
700 return;
701
Chao Yu429511c2015-02-05 17:54:31 +0800702 if (available_free_memory(sbi, EXTENT_CACHE))
703 return;
704
705 spin_lock(&sbi->extent_lock);
706 list_for_each_entry_safe(en, tmp, &sbi->extent_list, list) {
707 if (!nr_shrink--)
708 break;
709 list_del_init(&en->list);
710 }
711 spin_unlock(&sbi->extent_lock);
712
713 down_read(&sbi->extent_tree_lock);
714 while ((found = radix_tree_gang_lookup(&sbi->extent_tree_root,
715 (void **)treevec, ino, EXT_TREE_VEC_SIZE))) {
716 unsigned i;
717
718 ino = treevec[found - 1]->ino + 1;
719 for (i = 0; i < found; i++) {
720 struct extent_tree *et = treevec[i];
721
722 atomic_inc(&et->refcount);
723 write_lock(&et->lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800724 node_cnt += __free_extent_tree(sbi, et, false);
Chao Yu429511c2015-02-05 17:54:31 +0800725 write_unlock(&et->lock);
726 atomic_dec(&et->refcount);
727 }
728 }
729 up_read(&sbi->extent_tree_lock);
730
731 down_write(&sbi->extent_tree_lock);
732 radix_tree_for_each_slot(slot, &sbi->extent_tree_root, &iter,
733 F2FS_ROOT_INO(sbi)) {
734 struct extent_tree *et = (struct extent_tree *)*slot;
735
736 if (!atomic_read(&et->refcount) && !et->count) {
737 radix_tree_delete(&sbi->extent_tree_root, et->ino);
738 kmem_cache_free(extent_tree_slab, et);
739 sbi->total_ext_tree--;
Chao Yu1ec46102015-02-05 17:59:59 +0800740 tree_cnt++;
Chao Yu429511c2015-02-05 17:54:31 +0800741 }
742 }
743 up_write(&sbi->extent_tree_lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800744
745 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
Chao Yu429511c2015-02-05 17:54:31 +0800746}
747
748void f2fs_destroy_extent_tree(struct inode *inode)
749{
750 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
751 struct extent_tree *et;
Chao Yu1ec46102015-02-05 17:59:59 +0800752 unsigned int node_cnt = 0;
Chao Yu429511c2015-02-05 17:54:31 +0800753
Chao Yu1dcc3362015-02-05 17:57:31 +0800754 if (!test_opt(sbi, EXTENT_CACHE))
755 return;
756
Chao Yu93dfc522015-03-19 19:24:59 +0800757 et = __find_extent_tree(sbi, inode->i_ino);
758 if (!et)
Chao Yu429511c2015-02-05 17:54:31 +0800759 goto out;
Chao Yu429511c2015-02-05 17:54:31 +0800760
761 /* free all extent info belong to this extent tree */
762 write_lock(&et->lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800763 node_cnt = __free_extent_tree(sbi, et, true);
Chao Yu429511c2015-02-05 17:54:31 +0800764 write_unlock(&et->lock);
765
766 atomic_dec(&et->refcount);
767
768 /* try to find and delete extent tree entry in radix tree */
769 down_write(&sbi->extent_tree_lock);
770 et = radix_tree_lookup(&sbi->extent_tree_root, inode->i_ino);
771 if (!et) {
772 up_write(&sbi->extent_tree_lock);
773 goto out;
774 }
775 f2fs_bug_on(sbi, atomic_read(&et->refcount) || et->count);
776 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
777 kmem_cache_free(extent_tree_slab, et);
778 sbi->total_ext_tree--;
779 up_write(&sbi->extent_tree_lock);
780out:
Chao Yu1ec46102015-02-05 17:59:59 +0800781 trace_f2fs_destroy_extent_tree(inode, node_cnt);
Chao Yu429511c2015-02-05 17:54:31 +0800782 return;
783}
784
Chao Yu7e4dde72015-02-05 17:51:34 +0800785static bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
786 struct extent_info *ei)
787{
Chao Yu91c5d9b2015-02-05 18:02:44 +0800788 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
789 return false;
790
Chao Yu1dcc3362015-02-05 17:57:31 +0800791 if (test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
792 return f2fs_lookup_extent_tree(inode, pgofs, ei);
793
Chao Yu7e4dde72015-02-05 17:51:34 +0800794 return lookup_extent_info(inode, pgofs, ei);
795}
796
797void f2fs_update_extent_cache(struct dnode_of_data *dn)
798{
799 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
800 pgoff_t fofs;
801
802 f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR);
803
Chao Yu91c5d9b2015-02-05 18:02:44 +0800804 if (is_inode_flag_set(fi, FI_NO_EXTENT))
805 return;
806
Chao Yu7e4dde72015-02-05 17:51:34 +0800807 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
808 dn->ofs_in_node;
809
Chao Yu1dcc3362015-02-05 17:57:31 +0800810 if (test_opt(F2FS_I_SB(dn->inode), EXTENT_CACHE))
811 return f2fs_update_extent_tree(dn->inode, fofs,
812 dn->data_blkaddr);
813
Chao Yu7e4dde72015-02-05 17:51:34 +0800814 if (update_extent_info(dn->inode, fofs, dn->data_blkaddr))
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900815 sync_inode_page(dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900816}
817
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900818struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900819{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900820 struct address_space *mapping = inode->i_mapping;
821 struct dnode_of_data dn;
822 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800823 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900824 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800825 struct f2fs_io_info fio = {
826 .type = DATA,
827 .rw = sync ? READ_SYNC : READA,
828 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900829
Jaegeuk Kimb7f204c2015-02-25 19:54:48 -0800830 /*
831 * If sync is false, it needs to check its block allocation.
832 * This is need and triggered by two flows:
833 * gc and truncate_partial_data_page.
834 */
835 if (!sync)
836 goto search;
837
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900838 page = find_get_page(mapping, index);
839 if (page && PageUptodate(page))
840 return page;
841 f2fs_put_page(page, 0);
Jaegeuk Kimb7f204c2015-02-25 19:54:48 -0800842search:
Chao Yucb3bc9e2015-02-05 18:03:40 +0800843 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
844 dn.data_blkaddr = ei.blk + index - ei.fofs;
845 goto got_it;
846 }
847
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900848 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900849 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900850 if (err)
851 return ERR_PTR(err);
852 f2fs_put_dnode(&dn);
853
854 if (dn.data_blkaddr == NULL_ADDR)
855 return ERR_PTR(-ENOENT);
856
857 /* By fallocate(), there is no cached page, but with NEW_ADDR */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900858 if (unlikely(dn.data_blkaddr == NEW_ADDR))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900859 return ERR_PTR(-EINVAL);
860
Chao Yucb3bc9e2015-02-05 18:03:40 +0800861got_it:
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900862 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900863 if (!page)
864 return ERR_PTR(-ENOMEM);
865
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900866 if (PageUptodate(page)) {
867 unlock_page(page);
868 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900869 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900870
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800871 fio.blk_addr = dn.data_blkaddr;
872 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Chao Yu1069bbf2013-11-28 15:43:43 +0800873 if (err)
874 return ERR_PTR(err);
875
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900876 if (sync) {
877 wait_on_page_locked(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900878 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900879 f2fs_put_page(page, 0);
880 return ERR_PTR(-EIO);
881 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900882 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900883 return page;
884}
885
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900886/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900887 * If it tries to access a hole, return an error.
888 * Because, the callers, functions in dir.c and GC, should be able to know
889 * whether this page exists or not.
890 */
891struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
892{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900893 struct address_space *mapping = inode->i_mapping;
894 struct dnode_of_data dn;
895 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800896 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900897 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800898 struct f2fs_io_info fio = {
899 .type = DATA,
900 .rw = READ_SYNC,
901 };
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900902repeat:
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900903 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900904 if (!page)
905 return ERR_PTR(-ENOMEM);
906
Chao Yucb3bc9e2015-02-05 18:03:40 +0800907 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
908 dn.data_blkaddr = ei.blk + index - ei.fofs;
909 goto got_it;
910 }
911
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900912 set_new_dnode(&dn, inode, NULL, NULL, 0);
913 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
914 if (err) {
915 f2fs_put_page(page, 1);
916 return ERR_PTR(err);
917 }
918 f2fs_put_dnode(&dn);
919
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900920 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900921 f2fs_put_page(page, 1);
922 return ERR_PTR(-ENOENT);
923 }
924
Chao Yucb3bc9e2015-02-05 18:03:40 +0800925got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900926 if (PageUptodate(page))
927 return page;
928
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900929 /*
930 * A new dentry page is allocated but not able to be written, since its
931 * new inode page couldn't be allocated due to -ENOSPC.
932 * In such the case, its blkaddr can be remained as NEW_ADDR.
933 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
934 */
935 if (dn.data_blkaddr == NEW_ADDR) {
936 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
937 SetPageUptodate(page);
938 return page;
939 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900940
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800941 fio.blk_addr = dn.data_blkaddr;
942 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900943 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900944 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900945
946 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900947 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900948 f2fs_put_page(page, 1);
949 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900950 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900951 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900952 f2fs_put_page(page, 1);
953 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900954 }
955 return page;
956}
957
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900958/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900959 * Caller ensures that this data page is never allocated.
960 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900961 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800962 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
963 * f2fs_unlock_op().
Jaegeuk Kima8865372013-12-27 17:04:17 +0900964 * Note that, ipage is set only by make_empty_dir.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900965 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900966struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900967 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900968{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900969 struct address_space *mapping = inode->i_mapping;
970 struct page *page;
971 struct dnode_of_data dn;
972 int err;
973
Jaegeuk Kima8865372013-12-27 17:04:17 +0900974 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800975 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900976 if (err)
977 return ERR_PTR(err);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900978repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900979 page = grab_cache_page(mapping, index);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900980 if (!page) {
981 err = -ENOMEM;
982 goto put_err;
983 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900984
985 if (PageUptodate(page))
986 return page;
987
988 if (dn.data_blkaddr == NEW_ADDR) {
989 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900990 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900991 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800992 struct f2fs_io_info fio = {
993 .type = DATA,
994 .rw = READ_SYNC,
995 .blk_addr = dn.data_blkaddr,
996 };
997 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900998 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900999 goto put_err;
1000
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001001 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001002 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001003 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001004 err = -EIO;
1005 goto put_err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001006 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001007 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001008 f2fs_put_page(page, 1);
1009 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001010 }
1011 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001012
1013 if (new_i_size &&
1014 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
1015 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +09001016 /* Only the directory inode sets new_i_size */
1017 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001018 }
1019 return page;
Jaegeuk Kima8865372013-12-27 17:04:17 +09001020
1021put_err:
1022 f2fs_put_dnode(&dn);
1023 return ERR_PTR(err);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001024}
1025
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001026static int __allocate_data_block(struct dnode_of_data *dn)
1027{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001028 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001029 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001030 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001031 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001032 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001033 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001034
1035 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
1036 return -EPERM;
1037 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
1038 return -ENOSPC;
1039
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001040 get_node_info(sbi, dn->nid, &ni);
1041 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1042
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001043 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
1044 seg = CURSEG_DIRECT_IO;
1045
1046 allocate_data_block(sbi, NULL, NULL_ADDR, &dn->data_blkaddr, &sum, seg);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001047
1048 /* direct IO doesn't use extent cache to maximize the performance */
Chao Yu216a6202015-03-19 19:23:32 +08001049 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001050
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001051 /* update i_size */
1052 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
1053 dn->ofs_in_node;
1054 if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT))
1055 i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT));
1056
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001057 return 0;
1058}
1059
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001060static void __allocate_data_blocks(struct inode *inode, loff_t offset,
1061 size_t count)
1062{
1063 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1064 struct dnode_of_data dn;
1065 u64 start = F2FS_BYTES_TO_BLK(offset);
1066 u64 len = F2FS_BYTES_TO_BLK(count);
1067 bool allocated;
1068 u64 end_offset;
1069
1070 while (len) {
1071 f2fs_balance_fs(sbi);
1072 f2fs_lock_op(sbi);
1073
1074 /* When reading holes, we need its node page */
1075 set_new_dnode(&dn, inode, NULL, NULL, 0);
1076 if (get_dnode_of_data(&dn, start, ALLOC_NODE))
1077 goto out;
1078
1079 allocated = false;
1080 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
1081
1082 while (dn.ofs_in_node < end_offset && len) {
Chao Yud6d4f1c2015-03-12 17:04:24 +08001083 block_t blkaddr;
1084
1085 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
1086 if (blkaddr == NULL_ADDR) {
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001087 if (__allocate_data_block(&dn))
1088 goto sync_out;
1089 allocated = true;
1090 }
1091 len--;
1092 start++;
1093 dn.ofs_in_node++;
1094 }
1095
1096 if (allocated)
1097 sync_inode_page(&dn);
1098
1099 f2fs_put_dnode(&dn);
1100 f2fs_unlock_op(sbi);
1101 }
1102 return;
1103
1104sync_out:
1105 if (allocated)
1106 sync_inode_page(&dn);
1107 f2fs_put_dnode(&dn);
1108out:
1109 f2fs_unlock_op(sbi);
1110 return;
1111}
1112
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001113/*
Chao Yu4f4124d2013-12-21 18:02:14 +08001114 * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
1115 * If original data blocks are allocated, then give them to blockdev.
1116 * Otherwise,
1117 * a. preallocate requested block addresses
1118 * b. do not use extent cache for better performance
1119 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001120 */
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001121static int __get_data_block(struct inode *inode, sector_t iblock,
1122 struct buffer_head *bh_result, int create, bool fiemap)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001123{
1124 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
1125 unsigned maxblocks = bh_result->b_size >> blkbits;
1126 struct dnode_of_data dn;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001127 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
1128 pgoff_t pgofs, end_offset;
1129 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001130 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001131 bool allocated = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001132
1133 /* Get the page offset from the block offset(iblock) */
1134 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
1135
Chao Yu7e4dde72015-02-05 17:51:34 +08001136 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Chao Yua2e7d1b2015-02-05 17:50:30 +08001137 f2fs_map_bh(inode->i_sb, pgofs, &ei, bh_result);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001138 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001139 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001140
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001141 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001142 f2fs_lock_op(F2FS_I_SB(inode));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001143
1144 /* When reading holes, we need its node page */
1145 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001146 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001147 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001148 if (err == -ENOENT)
1149 err = 0;
1150 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001151 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001152 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001153 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001154
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001155 if (dn.data_blkaddr != NULL_ADDR) {
Jaegeuk Kim3402e872015-03-11 23:27:25 -04001156 clear_buffer_new(bh_result);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001157 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001158 } else if (create) {
1159 err = __allocate_data_block(&dn);
1160 if (err)
1161 goto put_out;
1162 allocated = true;
Jaegeuk Kimda17eec2015-02-09 10:34:38 -08001163 set_buffer_new(bh_result);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001164 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
1165 } else {
1166 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001167 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001168
Chao Yu6403eb12014-04-26 19:59:52 +08001169 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001170 bh_result->b_size = (((size_t)1) << blkbits);
1171 dn.ofs_in_node++;
1172 pgofs++;
1173
1174get_next:
1175 if (dn.ofs_in_node >= end_offset) {
1176 if (allocated)
1177 sync_inode_page(&dn);
1178 allocated = false;
1179 f2fs_put_dnode(&dn);
1180
1181 set_new_dnode(&dn, inode, NULL, NULL, 0);
1182 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001183 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001184 if (err == -ENOENT)
1185 err = 0;
1186 goto unlock_out;
1187 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001188 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001189 goto put_out;
1190
Chao Yu6403eb12014-04-26 19:59:52 +08001191 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001192 }
1193
1194 if (maxblocks > (bh_result->b_size >> blkbits)) {
1195 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
1196 if (blkaddr == NULL_ADDR && create) {
1197 err = __allocate_data_block(&dn);
1198 if (err)
1199 goto sync_out;
1200 allocated = true;
Jaegeuk Kim3402e872015-03-11 23:27:25 -04001201 set_buffer_new(bh_result);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001202 blkaddr = dn.data_blkaddr;
1203 }
arter97e1c42042014-08-06 23:22:50 +09001204 /* Give more consecutive addresses for the readahead */
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001205 if (blkaddr == (bh_result->b_blocknr + ofs)) {
1206 ofs++;
1207 dn.ofs_in_node++;
1208 pgofs++;
1209 bh_result->b_size += (((size_t)1) << blkbits);
1210 goto get_next;
1211 }
1212 }
1213sync_out:
1214 if (allocated)
1215 sync_inode_page(&dn);
1216put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001217 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001218unlock_out:
1219 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001220 f2fs_unlock_op(F2FS_I_SB(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001221out:
1222 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
1223 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001224}
1225
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001226static int get_data_block(struct inode *inode, sector_t iblock,
1227 struct buffer_head *bh_result, int create)
1228{
1229 return __get_data_block(inode, iblock, bh_result, create, false);
1230}
1231
1232static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
1233 struct buffer_head *bh_result, int create)
1234{
1235 return __get_data_block(inode, iblock, bh_result, create, true);
1236}
1237
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001238int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1239 u64 start, u64 len)
1240{
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001241 return generic_block_fiemap(inode, fieinfo,
1242 start, len, get_data_block_fiemap);
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001243}
1244
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001245static int f2fs_read_data_page(struct file *file, struct page *page)
1246{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001247 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001248 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001249
Chao Yuc20e89c2014-05-06 16:53:08 +08001250 trace_f2fs_readpage(page, DATA);
1251
arter97e1c42042014-08-06 23:22:50 +09001252 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001253 if (f2fs_has_inline_data(inode))
1254 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001255 if (ret == -EAGAIN)
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001256 ret = mpage_readpage(page, get_data_block);
1257
1258 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001259}
1260
1261static int f2fs_read_data_pages(struct file *file,
1262 struct address_space *mapping,
1263 struct list_head *pages, unsigned nr_pages)
1264{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001265 struct inode *inode = file->f_mapping->host;
1266
1267 /* If the file has inline data, skip readpages */
1268 if (f2fs_has_inline_data(inode))
1269 return 0;
1270
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001271 return mpage_readpages(mapping, pages, nr_pages, get_data_block);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001272}
1273
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001274int do_write_data_page(struct page *page, struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001275{
1276 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001277 struct dnode_of_data dn;
1278 int err = 0;
1279
1280 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001281 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001282 if (err)
1283 return err;
1284
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001285 fio->blk_addr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001286
1287 /* This page is already truncated */
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001288 if (fio->blk_addr == NULL_ADDR) {
1289 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001290 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001291 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001292
1293 set_page_writeback(page);
1294
1295 /*
1296 * If current allocation needs SSR,
1297 * it had better in-place writes for updated data.
1298 */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001299 if (unlikely(fio->blk_addr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001300 !is_cold_data(page) &&
1301 need_inplace_update(inode))) {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001302 rewrite_data_page(page, fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001303 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001304 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001305 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001306 write_data_page(page, &dn, fio);
Chao Yu216a6202015-03-19 19:23:32 +08001307 set_data_blkaddr(&dn);
Chao Yu7e4dde72015-02-05 17:51:34 +08001308 f2fs_update_extent_cache(&dn);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001309 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001310 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001311 if (page->index == 0)
1312 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001313 }
1314out_writepage:
1315 f2fs_put_dnode(&dn);
1316 return err;
1317}
1318
1319static int f2fs_write_data_page(struct page *page,
1320 struct writeback_control *wbc)
1321{
1322 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001323 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001324 loff_t i_size = i_size_read(inode);
1325 const pgoff_t end_index = ((unsigned long long) i_size)
1326 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001327 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001328 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001329 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001330 struct f2fs_io_info fio = {
1331 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001332 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001333 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001334
Chao Yuecda0de2014-05-06 16:48:26 +08001335 trace_f2fs_writepage(page, DATA);
1336
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001337 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001338 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001339
1340 /*
1341 * If the offset is out-of-range of file size,
1342 * this page does not have to be written to disk.
1343 */
1344 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001345 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001346 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001347
1348 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001349write:
Chao Yucaf00472015-01-28 17:48:42 +08001350 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001351 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001352 if (f2fs_is_drop_cache(inode))
1353 goto out;
1354 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1355 available_free_memory(sbi, BASE_CHECK))
1356 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001357
Jaegeuk Kim39936832012-11-22 16:21:29 +09001358 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001359 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001360 if (unlikely(f2fs_cp_error(sbi)))
1361 goto redirty_out;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001362 err = do_write_data_page(page, &fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001363 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001364 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001365
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001366 /* we should bypass data pages to proceed the kworkder jobs */
1367 if (unlikely(f2fs_cp_error(sbi))) {
1368 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001369 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001370 }
1371
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001372 if (!wbc->for_reclaim)
1373 need_balance_fs = true;
1374 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001375 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001376
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001377 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001378 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001379 if (f2fs_has_inline_data(inode))
1380 err = f2fs_write_inline_data(inode, page);
1381 if (err == -EAGAIN)
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001382 err = do_write_data_page(page, &fio);
1383 f2fs_unlock_op(sbi);
1384done:
1385 if (err && err != -ENOENT)
1386 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001387
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001388 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001389out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001390 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001391 if (err)
1392 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001393 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001394 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001395 f2fs_balance_fs(sbi);
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +09001396 if (wbc->for_reclaim)
1397 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001398 return 0;
1399
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001400redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001401 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001402 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001403}
1404
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001405static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1406 void *data)
1407{
1408 struct address_space *mapping = data;
1409 int ret = mapping->a_ops->writepage(page, wbc);
1410 mapping_set_error(mapping, ret);
1411 return ret;
1412}
1413
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001414static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001415 struct writeback_control *wbc)
1416{
1417 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001418 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001419 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001420 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001421
Chao Yue5748432014-05-06 16:51:24 +08001422 trace_f2fs_writepages(mapping->host, wbc, DATA);
1423
P J Pcfb185a2013-04-03 11:38:00 +09001424 /* deal with chardevs and other special file */
1425 if (!mapping->a_ops->writepage)
1426 return 0;
1427
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001428 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001429 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001430 available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001431 goto skip_write;
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001432
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001433 /* during POR, we don't need to trigger writepage at all. */
1434 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1435 goto skip_write;
1436
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001437 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001438
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001439 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001440
1441 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001442
1443 remove_dirty_dir_inode(inode);
1444
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001445 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001446 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001447
1448skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001449 wbc->pages_skipped += get_dirty_pages(inode);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001450 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001451}
1452
Chao Yu3aab8f82014-07-02 13:25:04 +08001453static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1454{
1455 struct inode *inode = mapping->host;
1456
1457 if (to > inode->i_size) {
1458 truncate_pagecache(inode, inode->i_size);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -07001459 truncate_blocks(inode, inode->i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001460 }
1461}
1462
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001463static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1464 loff_t pos, unsigned len, unsigned flags,
1465 struct page **pagep, void **fsdata)
1466{
1467 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001468 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001469 struct page *page, *ipage;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001470 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
1471 struct dnode_of_data dn;
1472 int err = 0;
1473
Chao Yu62aed042014-05-06 16:46:04 +08001474 trace_f2fs_write_begin(inode, pos, len, flags);
1475
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001476 f2fs_balance_fs(sbi);
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001477
1478 /*
1479 * We should check this at this moment to avoid deadlock on inode page
1480 * and #0 page. The locking rule for inline_data conversion should be:
1481 * lock_page(page #0) -> lock_page(inode_page)
1482 */
1483 if (index != 0) {
1484 err = f2fs_convert_inline_inode(inode);
1485 if (err)
1486 goto fail;
1487 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001488repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001489 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001490 if (!page) {
1491 err = -ENOMEM;
1492 goto fail;
1493 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001494
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001495 *pagep = page;
1496
Gu Zhenge4795562013-09-27 18:08:30 +08001497 f2fs_lock_op(sbi);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001498
1499 /* check inline_data */
1500 ipage = get_node_page(sbi, inode->i_ino);
Chao Yucd34e292014-12-01 11:30:20 +08001501 if (IS_ERR(ipage)) {
1502 err = PTR_ERR(ipage);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001503 goto unlock_fail;
Chao Yucd34e292014-12-01 11:30:20 +08001504 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001505
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001506 set_new_dnode(&dn, inode, ipage, ipage, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001507
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001508 if (f2fs_has_inline_data(inode)) {
1509 if (pos + len <= MAX_INLINE_DATA) {
1510 read_inline_data(page, ipage);
1511 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1512 sync_inode_page(&dn);
1513 goto put_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001514 }
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001515 err = f2fs_convert_inline_page(&dn, page);
1516 if (err)
1517 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001518 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001519 err = f2fs_reserve_block(&dn, index);
1520 if (err)
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001521 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001522put_next:
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001523 f2fs_put_dnode(&dn);
1524 f2fs_unlock_op(sbi);
1525
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001526 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
1527 return 0;
1528
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001529 f2fs_wait_on_page_writeback(page, DATA);
1530
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001531 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1532 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1533 unsigned end = start + len;
1534
1535 /* Reading beyond i_size is simple: memset to zero */
1536 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001537 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001538 }
1539
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001540 if (dn.data_blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001541 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1542 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001543 struct f2fs_io_info fio = {
1544 .type = DATA,
1545 .rw = READ_SYNC,
1546 .blk_addr = dn.data_blkaddr,
1547 };
1548 err = f2fs_submit_page_bio(sbi, page, &fio);
Jan Kara9234f312014-10-22 15:21:47 +02001549 if (err)
1550 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001551
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001552 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001553 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001554 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001555 err = -EIO;
1556 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001557 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001558 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001559 f2fs_put_page(page, 1);
1560 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001561 }
1562 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001563out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001564 SetPageUptodate(page);
1565 clear_cold_data(page);
1566 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001567
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001568put_fail:
1569 f2fs_put_dnode(&dn);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001570unlock_fail:
1571 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001572 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001573fail:
1574 f2fs_write_failed(mapping, pos + len);
1575 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001576}
1577
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001578static int f2fs_write_end(struct file *file,
1579 struct address_space *mapping,
1580 loff_t pos, unsigned len, unsigned copied,
1581 struct page *page, void *fsdata)
1582{
1583 struct inode *inode = page->mapping->host;
1584
Chao Yudfb2bf32014-05-06 16:47:23 +08001585 trace_f2fs_write_end(inode, pos, len, copied);
1586
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001587 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001588
1589 if (pos + copied > i_size_read(inode)) {
1590 i_size_write(inode, pos + copied);
1591 mark_inode_dirty(inode);
1592 update_inode_page(inode);
1593 }
1594
Chao Yu75c3c8b2013-11-16 14:15:59 +08001595 f2fs_put_page(page, 1);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001596 return copied;
1597}
1598
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001599static int check_direct_IO(struct inode *inode, int rw,
Al Viro5b46f252014-03-16 18:07:34 -04001600 struct iov_iter *iter, loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001601{
1602 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001603
1604 if (rw == READ)
1605 return 0;
1606
1607 if (offset & blocksize_mask)
1608 return -EINVAL;
1609
Al Viro5b46f252014-03-16 18:07:34 -04001610 if (iov_iter_alignment(iter) & blocksize_mask)
1611 return -EINVAL;
1612
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001613 return 0;
1614}
1615
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001616static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05001617 struct iov_iter *iter, loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001618{
1619 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001620 struct address_space *mapping = file->f_mapping;
1621 struct inode *inode = mapping->host;
1622 size_t count = iov_iter_count(iter);
1623 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001624
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001625 /* we don't need to use inline_data strictly */
1626 if (f2fs_has_inline_data(inode)) {
1627 err = f2fs_convert_inline_inode(inode);
1628 if (err)
1629 return err;
1630 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001631
Al Viro5b46f252014-03-16 18:07:34 -04001632 if (check_direct_IO(inode, rw, iter, offset))
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001633 return 0;
1634
Chao Yu70407fa2014-07-31 21:11:22 +08001635 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
1636
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001637 if (rw & WRITE)
1638 __allocate_data_blocks(inode, offset, count);
1639
Chao Yu3aab8f82014-07-02 13:25:04 +08001640 err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block);
1641 if (err < 0 && (rw & WRITE))
1642 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001643
1644 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
1645
Chao Yu3aab8f82014-07-02 13:25:04 +08001646 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001647}
1648
Chao Yu487261f2015-02-05 17:44:29 +08001649void f2fs_invalidate_page(struct page *page, unsigned int offset,
1650 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001651{
1652 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001653 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001654
Chao Yu487261f2015-02-05 17:44:29 +08001655 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1656 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001657 return;
1658
Chao Yu487261f2015-02-05 17:44:29 +08001659 if (PageDirty(page)) {
1660 if (inode->i_ino == F2FS_META_INO(sbi))
1661 dec_page_count(sbi, F2FS_DIRTY_META);
1662 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1663 dec_page_count(sbi, F2FS_DIRTY_NODES);
1664 else
1665 inode_dec_dirty_pages(inode);
1666 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001667 ClearPagePrivate(page);
1668}
1669
Chao Yu487261f2015-02-05 17:44:29 +08001670int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001671{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001672 /* If this is dirty page, keep PagePrivate */
1673 if (PageDirty(page))
1674 return 0;
1675
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001676 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001677 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001678}
1679
1680static int f2fs_set_data_page_dirty(struct page *page)
1681{
1682 struct address_space *mapping = page->mapping;
1683 struct inode *inode = mapping->host;
1684
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001685 trace_f2fs_set_page_dirty(page, DATA);
1686
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001687 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001688
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001689 if (f2fs_is_atomic_file(inode)) {
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001690 register_inmem_page(inode, page);
1691 return 1;
1692 }
1693
Jaegeuk Kima18ff062014-01-21 13:32:12 +09001694 mark_inode_dirty(inode);
1695
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001696 if (!PageDirty(page)) {
1697 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001698 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001699 return 1;
1700 }
1701 return 0;
1702}
1703
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001704static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1705{
Chao Yu454ae7e2014-04-22 13:34:01 +08001706 struct inode *inode = mapping->host;
1707
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001708 /* we don't need to use inline_data strictly */
1709 if (f2fs_has_inline_data(inode)) {
1710 int err = f2fs_convert_inline_inode(inode);
1711 if (err)
1712 return err;
1713 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001714 return generic_block_bmap(mapping, block, get_data_block);
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001715}
1716
Chao Yu429511c2015-02-05 17:54:31 +08001717void init_extent_cache_info(struct f2fs_sb_info *sbi)
1718{
1719 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
1720 init_rwsem(&sbi->extent_tree_lock);
1721 INIT_LIST_HEAD(&sbi->extent_list);
1722 spin_lock_init(&sbi->extent_lock);
1723 sbi->total_ext_tree = 0;
1724 atomic_set(&sbi->total_ext_node, 0);
1725}
1726
1727int __init create_extent_cache(void)
1728{
1729 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
1730 sizeof(struct extent_tree));
1731 if (!extent_tree_slab)
1732 return -ENOMEM;
1733 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
1734 sizeof(struct extent_node));
1735 if (!extent_node_slab) {
1736 kmem_cache_destroy(extent_tree_slab);
1737 return -ENOMEM;
1738 }
1739 return 0;
1740}
1741
1742void destroy_extent_cache(void)
1743{
1744 kmem_cache_destroy(extent_node_slab);
1745 kmem_cache_destroy(extent_tree_slab);
1746}
1747
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001748const struct address_space_operations f2fs_dblock_aops = {
1749 .readpage = f2fs_read_data_page,
1750 .readpages = f2fs_read_data_pages,
1751 .writepage = f2fs_write_data_page,
1752 .writepages = f2fs_write_data_pages,
1753 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001754 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001755 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001756 .invalidatepage = f2fs_invalidate_page,
1757 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001758 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001759 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001760};