blob: 6492f88a2cc486ce4b31038ec23d14255135a576 [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
397static 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
Chao Yu62c8af62015-02-05 18:01:39 +0800403 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
Chao Yu429511c2015-02-05 17:54:31 +0800410 while (node) {
411 en = rb_entry(node, struct extent_node, rb_node);
412
Chao Yu62c8af62015-02-05 18:01:39 +0800413 if (fofs < en->ei.fofs) {
Chao Yu429511c2015-02-05 17:54:31 +0800414 node = node->rb_left;
Chao Yu62c8af62015-02-05 18:01:39 +0800415 } else if (fofs >= en->ei.fofs + en->ei.len) {
Chao Yu429511c2015-02-05 17:54:31 +0800416 node = node->rb_right;
Chao Yu62c8af62015-02-05 18:01:39 +0800417 } else {
418 et->cached_en = en;
Chao Yu429511c2015-02-05 17:54:31 +0800419 return en;
Chao Yu62c8af62015-02-05 18:01:39 +0800420 }
Chao Yu429511c2015-02-05 17:54:31 +0800421 }
422 return NULL;
423}
424
425static 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
446static 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
465static 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
503static 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
532static 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
Chao Yu1ec46102015-02-05 17:59:59 +0800539 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
540
Chao Yu429511c2015-02-05 17:54:31 +0800541 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
Chao Yu1ec46102015-02-05 17:59:59 +0800563 trace_f2fs_lookup_extent_tree_end(inode, pgofs, en);
564
Chao Yu429511c2015-02-05 17:54:31 +0800565 atomic_dec(&et->refcount);
566 return en ? true : false;
567}
568
569static 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
Chao Yu1ec46102015-02-05 17:59:59 +0800580 trace_f2fs_update_extent_tree(inode, fofs, blkaddr);
581
Chao Yu429511c2015-02-05 17:54:31 +0800582 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;
Chao Yu62c8af62015-02-05 18:01:39 +0800590 et->cached_en = NULL;
Chao Yu429511c2015-02-05 17:54:31 +0800591 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
627update_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
667void 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;
Chao Yu1ec46102015-02-05 17:59:59 +0800675 unsigned int node_cnt = 0, tree_cnt = 0;
Chao Yu429511c2015-02-05 17:54:31 +0800676
Chao Yu1dcc3362015-02-05 17:57:31 +0800677 if (!test_opt(sbi, EXTENT_CACHE))
678 return;
679
Chao Yu429511c2015-02-05 17:54:31 +0800680 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);
Chao Yu1ec46102015-02-05 17:59:59 +0800702 node_cnt += __free_extent_tree(sbi, et, false);
Chao Yu429511c2015-02-05 17:54:31 +0800703 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--;
Chao Yu1ec46102015-02-05 17:59:59 +0800718 tree_cnt++;
Chao Yu429511c2015-02-05 17:54:31 +0800719 }
720 }
721 up_write(&sbi->extent_tree_lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800722
723 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
Chao Yu429511c2015-02-05 17:54:31 +0800724}
725
726void f2fs_destroy_extent_tree(struct inode *inode)
727{
728 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
729 struct extent_tree *et;
Chao Yu1ec46102015-02-05 17:59:59 +0800730 unsigned int node_cnt = 0;
Chao Yu429511c2015-02-05 17:54:31 +0800731
Chao Yu1dcc3362015-02-05 17:57:31 +0800732 if (!test_opt(sbi, EXTENT_CACHE))
733 return;
734
Chao Yu429511c2015-02-05 17:54:31 +0800735 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);
Chao Yu1ec46102015-02-05 17:59:59 +0800746 node_cnt = __free_extent_tree(sbi, et, true);
Chao Yu429511c2015-02-05 17:54:31 +0800747 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);
763out:
Chao Yu1ec46102015-02-05 17:59:59 +0800764 trace_f2fs_destroy_extent_tree(inode, node_cnt);
Chao Yu429511c2015-02-05 17:54:31 +0800765 return;
766}
767
Chao Yu7e4dde72015-02-05 17:51:34 +0800768static bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
769 struct extent_info *ei)
770{
Chao Yu91c5d9b2015-02-05 18:02:44 +0800771 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
772 return false;
773
Chao Yu1dcc3362015-02-05 17:57:31 +0800774 if (test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
775 return f2fs_lookup_extent_tree(inode, pgofs, ei);
776
Chao Yu7e4dde72015-02-05 17:51:34 +0800777 return lookup_extent_info(inode, pgofs, ei);
778}
779
780void 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
Chao Yu91c5d9b2015-02-05 18:02:44 +0800787 if (is_inode_flag_set(fi, FI_NO_EXTENT))
788 return;
789
Chao Yu7e4dde72015-02-05 17:51:34 +0800790 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
791 dn->ofs_in_node;
792
Chao Yu1dcc3362015-02-05 17:57:31 +0800793 if (test_opt(F2FS_I_SB(dn->inode), EXTENT_CACHE))
794 return f2fs_update_extent_tree(dn->inode, fofs,
795 dn->data_blkaddr);
796
Chao Yu7e4dde72015-02-05 17:51:34 +0800797 if (update_extent_info(dn->inode, fofs, dn->data_blkaddr))
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900798 sync_inode_page(dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900799}
800
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900801struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900802{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900803 struct address_space *mapping = inode->i_mapping;
804 struct dnode_of_data dn;
805 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800806 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900807 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800808 struct f2fs_io_info fio = {
809 .type = DATA,
810 .rw = sync ? READ_SYNC : READA,
811 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900812
Jaegeuk Kimb7f204c2015-02-25 19:54:48 -0800813 /*
814 * If sync is false, it needs to check its block allocation.
815 * This is need and triggered by two flows:
816 * gc and truncate_partial_data_page.
817 */
818 if (!sync)
819 goto search;
820
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900821 page = find_get_page(mapping, index);
822 if (page && PageUptodate(page))
823 return page;
824 f2fs_put_page(page, 0);
Jaegeuk Kimb7f204c2015-02-25 19:54:48 -0800825search:
Chao Yucb3bc9e2015-02-05 18:03:40 +0800826 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
827 dn.data_blkaddr = ei.blk + index - ei.fofs;
828 goto got_it;
829 }
830
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900831 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900832 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900833 if (err)
834 return ERR_PTR(err);
835 f2fs_put_dnode(&dn);
836
837 if (dn.data_blkaddr == NULL_ADDR)
838 return ERR_PTR(-ENOENT);
839
840 /* By fallocate(), there is no cached page, but with NEW_ADDR */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900841 if (unlikely(dn.data_blkaddr == NEW_ADDR))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900842 return ERR_PTR(-EINVAL);
843
Chao Yucb3bc9e2015-02-05 18:03:40 +0800844got_it:
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900845 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900846 if (!page)
847 return ERR_PTR(-ENOMEM);
848
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900849 if (PageUptodate(page)) {
850 unlock_page(page);
851 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900852 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900853
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800854 fio.blk_addr = dn.data_blkaddr;
855 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Chao Yu1069bbf2013-11-28 15:43:43 +0800856 if (err)
857 return ERR_PTR(err);
858
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900859 if (sync) {
860 wait_on_page_locked(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900861 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900862 f2fs_put_page(page, 0);
863 return ERR_PTR(-EIO);
864 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900865 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900866 return page;
867}
868
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900869/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900870 * If it tries to access a hole, return an error.
871 * Because, the callers, functions in dir.c and GC, should be able to know
872 * whether this page exists or not.
873 */
874struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
875{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900876 struct address_space *mapping = inode->i_mapping;
877 struct dnode_of_data dn;
878 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800879 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900880 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800881 struct f2fs_io_info fio = {
882 .type = DATA,
883 .rw = READ_SYNC,
884 };
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900885repeat:
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900886 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900887 if (!page)
888 return ERR_PTR(-ENOMEM);
889
Chao Yucb3bc9e2015-02-05 18:03:40 +0800890 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
891 dn.data_blkaddr = ei.blk + index - ei.fofs;
892 goto got_it;
893 }
894
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900895 set_new_dnode(&dn, inode, NULL, NULL, 0);
896 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
897 if (err) {
898 f2fs_put_page(page, 1);
899 return ERR_PTR(err);
900 }
901 f2fs_put_dnode(&dn);
902
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900903 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900904 f2fs_put_page(page, 1);
905 return ERR_PTR(-ENOENT);
906 }
907
Chao Yucb3bc9e2015-02-05 18:03:40 +0800908got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900909 if (PageUptodate(page))
910 return page;
911
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900912 /*
913 * A new dentry page is allocated but not able to be written, since its
914 * new inode page couldn't be allocated due to -ENOSPC.
915 * In such the case, its blkaddr can be remained as NEW_ADDR.
916 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
917 */
918 if (dn.data_blkaddr == NEW_ADDR) {
919 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
920 SetPageUptodate(page);
921 return page;
922 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900923
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800924 fio.blk_addr = dn.data_blkaddr;
925 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900926 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900927 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900928
929 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900930 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900931 f2fs_put_page(page, 1);
932 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900933 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900934 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900935 f2fs_put_page(page, 1);
936 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900937 }
938 return page;
939}
940
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900941/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900942 * Caller ensures that this data page is never allocated.
943 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900944 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800945 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
946 * f2fs_unlock_op().
Jaegeuk Kima8865372013-12-27 17:04:17 +0900947 * Note that, ipage is set only by make_empty_dir.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900948 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900949struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900950 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900951{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900952 struct address_space *mapping = inode->i_mapping;
953 struct page *page;
954 struct dnode_of_data dn;
955 int err;
956
Jaegeuk Kima8865372013-12-27 17:04:17 +0900957 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800958 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900959 if (err)
960 return ERR_PTR(err);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900961repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900962 page = grab_cache_page(mapping, index);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900963 if (!page) {
964 err = -ENOMEM;
965 goto put_err;
966 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900967
968 if (PageUptodate(page))
969 return page;
970
971 if (dn.data_blkaddr == NEW_ADDR) {
972 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900973 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900974 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800975 struct f2fs_io_info fio = {
976 .type = DATA,
977 .rw = READ_SYNC,
978 .blk_addr = dn.data_blkaddr,
979 };
980 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900981 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900982 goto put_err;
983
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900984 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900985 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900986 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900987 err = -EIO;
988 goto put_err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900989 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900990 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900991 f2fs_put_page(page, 1);
992 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900993 }
994 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900995
996 if (new_i_size &&
997 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
998 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900999 /* Only the directory inode sets new_i_size */
1000 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001001 }
1002 return page;
Jaegeuk Kima8865372013-12-27 17:04:17 +09001003
1004put_err:
1005 f2fs_put_dnode(&dn);
1006 return ERR_PTR(err);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001007}
1008
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001009static int __allocate_data_block(struct dnode_of_data *dn)
1010{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001011 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001012 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001013 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001014 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001015 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001016 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001017
1018 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
1019 return -EPERM;
1020 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
1021 return -ENOSPC;
1022
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001023 get_node_info(sbi, dn->nid, &ni);
1024 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1025
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001026 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
1027 seg = CURSEG_DIRECT_IO;
1028
1029 allocate_data_block(sbi, NULL, NULL_ADDR, &dn->data_blkaddr, &sum, seg);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001030
1031 /* direct IO doesn't use extent cache to maximize the performance */
Chao Yu216a6202015-03-19 19:23:32 +08001032 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001033
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001034 /* update i_size */
1035 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
1036 dn->ofs_in_node;
1037 if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT))
1038 i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT));
1039
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001040 return 0;
1041}
1042
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001043static void __allocate_data_blocks(struct inode *inode, loff_t offset,
1044 size_t count)
1045{
1046 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1047 struct dnode_of_data dn;
1048 u64 start = F2FS_BYTES_TO_BLK(offset);
1049 u64 len = F2FS_BYTES_TO_BLK(count);
1050 bool allocated;
1051 u64 end_offset;
1052
1053 while (len) {
1054 f2fs_balance_fs(sbi);
1055 f2fs_lock_op(sbi);
1056
1057 /* When reading holes, we need its node page */
1058 set_new_dnode(&dn, inode, NULL, NULL, 0);
1059 if (get_dnode_of_data(&dn, start, ALLOC_NODE))
1060 goto out;
1061
1062 allocated = false;
1063 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
1064
1065 while (dn.ofs_in_node < end_offset && len) {
Chao Yud6d4f1c2015-03-12 17:04:24 +08001066 block_t blkaddr;
1067
1068 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
1069 if (blkaddr == NULL_ADDR) {
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001070 if (__allocate_data_block(&dn))
1071 goto sync_out;
1072 allocated = true;
1073 }
1074 len--;
1075 start++;
1076 dn.ofs_in_node++;
1077 }
1078
1079 if (allocated)
1080 sync_inode_page(&dn);
1081
1082 f2fs_put_dnode(&dn);
1083 f2fs_unlock_op(sbi);
1084 }
1085 return;
1086
1087sync_out:
1088 if (allocated)
1089 sync_inode_page(&dn);
1090 f2fs_put_dnode(&dn);
1091out:
1092 f2fs_unlock_op(sbi);
1093 return;
1094}
1095
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001096/*
Chao Yu4f4124d2013-12-21 18:02:14 +08001097 * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
1098 * If original data blocks are allocated, then give them to blockdev.
1099 * Otherwise,
1100 * a. preallocate requested block addresses
1101 * b. do not use extent cache for better performance
1102 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001103 */
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001104static int __get_data_block(struct inode *inode, sector_t iblock,
1105 struct buffer_head *bh_result, int create, bool fiemap)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001106{
1107 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
1108 unsigned maxblocks = bh_result->b_size >> blkbits;
1109 struct dnode_of_data dn;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001110 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
1111 pgoff_t pgofs, end_offset;
1112 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001113 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001114 bool allocated = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001115
1116 /* Get the page offset from the block offset(iblock) */
1117 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
1118
Chao Yu7e4dde72015-02-05 17:51:34 +08001119 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Chao Yua2e7d1b2015-02-05 17:50:30 +08001120 f2fs_map_bh(inode->i_sb, pgofs, &ei, bh_result);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001121 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001122 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001123
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001124 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001125 f2fs_lock_op(F2FS_I_SB(inode));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001126
1127 /* When reading holes, we need its node page */
1128 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001129 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001130 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001131 if (err == -ENOENT)
1132 err = 0;
1133 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001134 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001135 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001136 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001137
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001138 if (dn.data_blkaddr != NULL_ADDR) {
Jaegeuk Kim3402e872015-03-11 23:27:25 -04001139 clear_buffer_new(bh_result);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001140 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001141 } else if (create) {
1142 err = __allocate_data_block(&dn);
1143 if (err)
1144 goto put_out;
1145 allocated = true;
Jaegeuk Kimda17eec2015-02-09 10:34:38 -08001146 set_buffer_new(bh_result);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001147 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
1148 } else {
1149 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001150 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001151
Chao Yu6403eb12014-04-26 19:59:52 +08001152 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001153 bh_result->b_size = (((size_t)1) << blkbits);
1154 dn.ofs_in_node++;
1155 pgofs++;
1156
1157get_next:
1158 if (dn.ofs_in_node >= end_offset) {
1159 if (allocated)
1160 sync_inode_page(&dn);
1161 allocated = false;
1162 f2fs_put_dnode(&dn);
1163
1164 set_new_dnode(&dn, inode, NULL, NULL, 0);
1165 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001166 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001167 if (err == -ENOENT)
1168 err = 0;
1169 goto unlock_out;
1170 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001171 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001172 goto put_out;
1173
Chao Yu6403eb12014-04-26 19:59:52 +08001174 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001175 }
1176
1177 if (maxblocks > (bh_result->b_size >> blkbits)) {
1178 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
1179 if (blkaddr == NULL_ADDR && create) {
1180 err = __allocate_data_block(&dn);
1181 if (err)
1182 goto sync_out;
1183 allocated = true;
Jaegeuk Kim3402e872015-03-11 23:27:25 -04001184 set_buffer_new(bh_result);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001185 blkaddr = dn.data_blkaddr;
1186 }
arter97e1c42042014-08-06 23:22:50 +09001187 /* Give more consecutive addresses for the readahead */
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001188 if (blkaddr == (bh_result->b_blocknr + ofs)) {
1189 ofs++;
1190 dn.ofs_in_node++;
1191 pgofs++;
1192 bh_result->b_size += (((size_t)1) << blkbits);
1193 goto get_next;
1194 }
1195 }
1196sync_out:
1197 if (allocated)
1198 sync_inode_page(&dn);
1199put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001200 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001201unlock_out:
1202 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001203 f2fs_unlock_op(F2FS_I_SB(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001204out:
1205 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
1206 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001207}
1208
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001209static int get_data_block(struct inode *inode, sector_t iblock,
1210 struct buffer_head *bh_result, int create)
1211{
1212 return __get_data_block(inode, iblock, bh_result, create, false);
1213}
1214
1215static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
1216 struct buffer_head *bh_result, int create)
1217{
1218 return __get_data_block(inode, iblock, bh_result, create, true);
1219}
1220
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001221int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1222 u64 start, u64 len)
1223{
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001224 return generic_block_fiemap(inode, fieinfo,
1225 start, len, get_data_block_fiemap);
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001226}
1227
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001228static int f2fs_read_data_page(struct file *file, struct page *page)
1229{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001230 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001231 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001232
Chao Yuc20e89c2014-05-06 16:53:08 +08001233 trace_f2fs_readpage(page, DATA);
1234
arter97e1c42042014-08-06 23:22:50 +09001235 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001236 if (f2fs_has_inline_data(inode))
1237 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001238 if (ret == -EAGAIN)
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001239 ret = mpage_readpage(page, get_data_block);
1240
1241 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001242}
1243
1244static int f2fs_read_data_pages(struct file *file,
1245 struct address_space *mapping,
1246 struct list_head *pages, unsigned nr_pages)
1247{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001248 struct inode *inode = file->f_mapping->host;
1249
1250 /* If the file has inline data, skip readpages */
1251 if (f2fs_has_inline_data(inode))
1252 return 0;
1253
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001254 return mpage_readpages(mapping, pages, nr_pages, get_data_block);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001255}
1256
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001257int do_write_data_page(struct page *page, struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001258{
1259 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001260 struct dnode_of_data dn;
1261 int err = 0;
1262
1263 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001264 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001265 if (err)
1266 return err;
1267
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001268 fio->blk_addr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001269
1270 /* This page is already truncated */
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001271 if (fio->blk_addr == NULL_ADDR) {
1272 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001273 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001274 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001275
1276 set_page_writeback(page);
1277
1278 /*
1279 * If current allocation needs SSR,
1280 * it had better in-place writes for updated data.
1281 */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001282 if (unlikely(fio->blk_addr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001283 !is_cold_data(page) &&
1284 need_inplace_update(inode))) {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001285 rewrite_data_page(page, fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001286 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001287 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001288 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001289 write_data_page(page, &dn, fio);
Chao Yu216a6202015-03-19 19:23:32 +08001290 set_data_blkaddr(&dn);
Chao Yu7e4dde72015-02-05 17:51:34 +08001291 f2fs_update_extent_cache(&dn);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001292 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001293 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001294 if (page->index == 0)
1295 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001296 }
1297out_writepage:
1298 f2fs_put_dnode(&dn);
1299 return err;
1300}
1301
1302static int f2fs_write_data_page(struct page *page,
1303 struct writeback_control *wbc)
1304{
1305 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001306 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001307 loff_t i_size = i_size_read(inode);
1308 const pgoff_t end_index = ((unsigned long long) i_size)
1309 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001310 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001311 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001312 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001313 struct f2fs_io_info fio = {
1314 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001315 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001316 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001317
Chao Yuecda0de2014-05-06 16:48:26 +08001318 trace_f2fs_writepage(page, DATA);
1319
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001320 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001321 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001322
1323 /*
1324 * If the offset is out-of-range of file size,
1325 * this page does not have to be written to disk.
1326 */
1327 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001328 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001329 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001330
1331 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001332write:
Chao Yucaf00472015-01-28 17:48:42 +08001333 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001334 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001335 if (f2fs_is_drop_cache(inode))
1336 goto out;
1337 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1338 available_free_memory(sbi, BASE_CHECK))
1339 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001340
Jaegeuk Kim39936832012-11-22 16:21:29 +09001341 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001342 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001343 if (unlikely(f2fs_cp_error(sbi)))
1344 goto redirty_out;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001345 err = do_write_data_page(page, &fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001346 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001347 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001348
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001349 /* we should bypass data pages to proceed the kworkder jobs */
1350 if (unlikely(f2fs_cp_error(sbi))) {
1351 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001352 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001353 }
1354
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001355 if (!wbc->for_reclaim)
1356 need_balance_fs = true;
1357 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001358 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001359
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001360 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001361 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001362 if (f2fs_has_inline_data(inode))
1363 err = f2fs_write_inline_data(inode, page);
1364 if (err == -EAGAIN)
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001365 err = do_write_data_page(page, &fio);
1366 f2fs_unlock_op(sbi);
1367done:
1368 if (err && err != -ENOENT)
1369 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001370
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001371 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001372out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001373 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001374 if (err)
1375 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001376 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001377 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001378 f2fs_balance_fs(sbi);
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +09001379 if (wbc->for_reclaim)
1380 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001381 return 0;
1382
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001383redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001384 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001385 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001386}
1387
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001388static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1389 void *data)
1390{
1391 struct address_space *mapping = data;
1392 int ret = mapping->a_ops->writepage(page, wbc);
1393 mapping_set_error(mapping, ret);
1394 return ret;
1395}
1396
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001397static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001398 struct writeback_control *wbc)
1399{
1400 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001401 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001402 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001403 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001404
Chao Yue5748432014-05-06 16:51:24 +08001405 trace_f2fs_writepages(mapping->host, wbc, DATA);
1406
P J Pcfb185a2013-04-03 11:38:00 +09001407 /* deal with chardevs and other special file */
1408 if (!mapping->a_ops->writepage)
1409 return 0;
1410
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001411 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001412 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001413 available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001414 goto skip_write;
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001415
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001416 /* during POR, we don't need to trigger writepage at all. */
1417 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1418 goto skip_write;
1419
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001420 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001421
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001422 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001423
1424 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001425
1426 remove_dirty_dir_inode(inode);
1427
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001428 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001429 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001430
1431skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001432 wbc->pages_skipped += get_dirty_pages(inode);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001433 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001434}
1435
Chao Yu3aab8f82014-07-02 13:25:04 +08001436static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1437{
1438 struct inode *inode = mapping->host;
1439
1440 if (to > inode->i_size) {
1441 truncate_pagecache(inode, inode->i_size);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -07001442 truncate_blocks(inode, inode->i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001443 }
1444}
1445
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001446static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1447 loff_t pos, unsigned len, unsigned flags,
1448 struct page **pagep, void **fsdata)
1449{
1450 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001451 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001452 struct page *page, *ipage;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001453 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
1454 struct dnode_of_data dn;
1455 int err = 0;
1456
Chao Yu62aed042014-05-06 16:46:04 +08001457 trace_f2fs_write_begin(inode, pos, len, flags);
1458
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001459 f2fs_balance_fs(sbi);
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001460
1461 /*
1462 * We should check this at this moment to avoid deadlock on inode page
1463 * and #0 page. The locking rule for inline_data conversion should be:
1464 * lock_page(page #0) -> lock_page(inode_page)
1465 */
1466 if (index != 0) {
1467 err = f2fs_convert_inline_inode(inode);
1468 if (err)
1469 goto fail;
1470 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001471repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001472 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001473 if (!page) {
1474 err = -ENOMEM;
1475 goto fail;
1476 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001477
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001478 *pagep = page;
1479
Gu Zhenge4795562013-09-27 18:08:30 +08001480 f2fs_lock_op(sbi);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001481
1482 /* check inline_data */
1483 ipage = get_node_page(sbi, inode->i_ino);
Chao Yucd34e292014-12-01 11:30:20 +08001484 if (IS_ERR(ipage)) {
1485 err = PTR_ERR(ipage);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001486 goto unlock_fail;
Chao Yucd34e292014-12-01 11:30:20 +08001487 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001488
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001489 set_new_dnode(&dn, inode, ipage, ipage, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001490
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001491 if (f2fs_has_inline_data(inode)) {
1492 if (pos + len <= MAX_INLINE_DATA) {
1493 read_inline_data(page, ipage);
1494 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1495 sync_inode_page(&dn);
1496 goto put_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001497 }
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001498 err = f2fs_convert_inline_page(&dn, page);
1499 if (err)
1500 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001501 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001502 err = f2fs_reserve_block(&dn, index);
1503 if (err)
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001504 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001505put_next:
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001506 f2fs_put_dnode(&dn);
1507 f2fs_unlock_op(sbi);
1508
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001509 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
1510 return 0;
1511
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001512 f2fs_wait_on_page_writeback(page, DATA);
1513
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001514 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1515 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1516 unsigned end = start + len;
1517
1518 /* Reading beyond i_size is simple: memset to zero */
1519 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001520 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001521 }
1522
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001523 if (dn.data_blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001524 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1525 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001526 struct f2fs_io_info fio = {
1527 .type = DATA,
1528 .rw = READ_SYNC,
1529 .blk_addr = dn.data_blkaddr,
1530 };
1531 err = f2fs_submit_page_bio(sbi, page, &fio);
Jan Kara9234f312014-10-22 15:21:47 +02001532 if (err)
1533 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001534
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001535 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001536 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001537 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001538 err = -EIO;
1539 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001540 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001541 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001542 f2fs_put_page(page, 1);
1543 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001544 }
1545 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001546out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001547 SetPageUptodate(page);
1548 clear_cold_data(page);
1549 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001550
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001551put_fail:
1552 f2fs_put_dnode(&dn);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001553unlock_fail:
1554 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001555 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001556fail:
1557 f2fs_write_failed(mapping, pos + len);
1558 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001559}
1560
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001561static int f2fs_write_end(struct file *file,
1562 struct address_space *mapping,
1563 loff_t pos, unsigned len, unsigned copied,
1564 struct page *page, void *fsdata)
1565{
1566 struct inode *inode = page->mapping->host;
1567
Chao Yudfb2bf32014-05-06 16:47:23 +08001568 trace_f2fs_write_end(inode, pos, len, copied);
1569
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001570 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001571
1572 if (pos + copied > i_size_read(inode)) {
1573 i_size_write(inode, pos + copied);
1574 mark_inode_dirty(inode);
1575 update_inode_page(inode);
1576 }
1577
Chao Yu75c3c8b2013-11-16 14:15:59 +08001578 f2fs_put_page(page, 1);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001579 return copied;
1580}
1581
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001582static int check_direct_IO(struct inode *inode, int rw,
Al Viro5b46f252014-03-16 18:07:34 -04001583 struct iov_iter *iter, loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001584{
1585 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001586
1587 if (rw == READ)
1588 return 0;
1589
1590 if (offset & blocksize_mask)
1591 return -EINVAL;
1592
Al Viro5b46f252014-03-16 18:07:34 -04001593 if (iov_iter_alignment(iter) & blocksize_mask)
1594 return -EINVAL;
1595
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001596 return 0;
1597}
1598
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001599static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05001600 struct iov_iter *iter, loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001601{
1602 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001603 struct address_space *mapping = file->f_mapping;
1604 struct inode *inode = mapping->host;
1605 size_t count = iov_iter_count(iter);
1606 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001607
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001608 /* we don't need to use inline_data strictly */
1609 if (f2fs_has_inline_data(inode)) {
1610 err = f2fs_convert_inline_inode(inode);
1611 if (err)
1612 return err;
1613 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001614
Al Viro5b46f252014-03-16 18:07:34 -04001615 if (check_direct_IO(inode, rw, iter, offset))
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001616 return 0;
1617
Chao Yu70407fa2014-07-31 21:11:22 +08001618 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
1619
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001620 if (rw & WRITE)
1621 __allocate_data_blocks(inode, offset, count);
1622
Chao Yu3aab8f82014-07-02 13:25:04 +08001623 err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block);
1624 if (err < 0 && (rw & WRITE))
1625 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001626
1627 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
1628
Chao Yu3aab8f82014-07-02 13:25:04 +08001629 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001630}
1631
Chao Yu487261f2015-02-05 17:44:29 +08001632void f2fs_invalidate_page(struct page *page, unsigned int offset,
1633 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001634{
1635 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001636 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001637
Chao Yu487261f2015-02-05 17:44:29 +08001638 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1639 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001640 return;
1641
Chao Yu487261f2015-02-05 17:44:29 +08001642 if (PageDirty(page)) {
1643 if (inode->i_ino == F2FS_META_INO(sbi))
1644 dec_page_count(sbi, F2FS_DIRTY_META);
1645 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1646 dec_page_count(sbi, F2FS_DIRTY_NODES);
1647 else
1648 inode_dec_dirty_pages(inode);
1649 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001650 ClearPagePrivate(page);
1651}
1652
Chao Yu487261f2015-02-05 17:44:29 +08001653int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001654{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001655 /* If this is dirty page, keep PagePrivate */
1656 if (PageDirty(page))
1657 return 0;
1658
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001659 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001660 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001661}
1662
1663static int f2fs_set_data_page_dirty(struct page *page)
1664{
1665 struct address_space *mapping = page->mapping;
1666 struct inode *inode = mapping->host;
1667
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001668 trace_f2fs_set_page_dirty(page, DATA);
1669
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001670 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001671
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001672 if (f2fs_is_atomic_file(inode)) {
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001673 register_inmem_page(inode, page);
1674 return 1;
1675 }
1676
Jaegeuk Kima18ff062014-01-21 13:32:12 +09001677 mark_inode_dirty(inode);
1678
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001679 if (!PageDirty(page)) {
1680 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001681 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001682 return 1;
1683 }
1684 return 0;
1685}
1686
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001687static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1688{
Chao Yu454ae7e2014-04-22 13:34:01 +08001689 struct inode *inode = mapping->host;
1690
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001691 /* we don't need to use inline_data strictly */
1692 if (f2fs_has_inline_data(inode)) {
1693 int err = f2fs_convert_inline_inode(inode);
1694 if (err)
1695 return err;
1696 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001697 return generic_block_bmap(mapping, block, get_data_block);
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001698}
1699
Chao Yu429511c2015-02-05 17:54:31 +08001700void init_extent_cache_info(struct f2fs_sb_info *sbi)
1701{
1702 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
1703 init_rwsem(&sbi->extent_tree_lock);
1704 INIT_LIST_HEAD(&sbi->extent_list);
1705 spin_lock_init(&sbi->extent_lock);
1706 sbi->total_ext_tree = 0;
1707 atomic_set(&sbi->total_ext_node, 0);
1708}
1709
1710int __init create_extent_cache(void)
1711{
1712 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
1713 sizeof(struct extent_tree));
1714 if (!extent_tree_slab)
1715 return -ENOMEM;
1716 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
1717 sizeof(struct extent_node));
1718 if (!extent_node_slab) {
1719 kmem_cache_destroy(extent_tree_slab);
1720 return -ENOMEM;
1721 }
1722 return 0;
1723}
1724
1725void destroy_extent_cache(void)
1726{
1727 kmem_cache_destroy(extent_node_slab);
1728 kmem_cache_destroy(extent_tree_slab);
1729}
1730
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001731const struct address_space_operations f2fs_dblock_aops = {
1732 .readpage = f2fs_read_data_page,
1733 .readpages = f2fs_read_data_pages,
1734 .writepage = f2fs_write_data_page,
1735 .writepages = f2fs_write_data_pages,
1736 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001737 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001738 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001739 .invalidatepage = f2fs_invalidate_page,
1740 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001741 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001742 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001743};