blob: ae5c41444e8f0d82153c74ea56073afb55aa5448 [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 */
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800203static void __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;
Jaegeuk Kime1509cf2014-12-30 22:57:55 -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
260 set_buffer_new(bh_result);
261 map_bh(bh_result, sb, ei->blk + pgofs - ei->fofs);
262 count = ei->fofs + ei->len - pgofs;
263 if (count < (UINT_MAX >> blkbits))
264 bh_result->b_size = (count << blkbits);
265 else
266 bh_result->b_size = UINT_MAX;
267}
268
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
787 /* Update the page address in the parent node */
788 __set_data_blkaddr(dn);
789
Chao Yu91c5d9b2015-02-05 18:02:44 +0800790 if (is_inode_flag_set(fi, FI_NO_EXTENT))
791 return;
792
Chao Yu7e4dde72015-02-05 17:51:34 +0800793 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
794 dn->ofs_in_node;
795
Chao Yu1dcc3362015-02-05 17:57:31 +0800796 if (test_opt(F2FS_I_SB(dn->inode), EXTENT_CACHE))
797 return f2fs_update_extent_tree(dn->inode, fofs,
798 dn->data_blkaddr);
799
Chao Yu7e4dde72015-02-05 17:51:34 +0800800 if (update_extent_info(dn->inode, fofs, dn->data_blkaddr))
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900801 sync_inode_page(dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900802}
803
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900804struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900805{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900806 struct address_space *mapping = inode->i_mapping;
807 struct dnode_of_data dn;
808 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800809 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900810 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800811 struct f2fs_io_info fio = {
812 .type = DATA,
813 .rw = sync ? READ_SYNC : READA,
814 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900815
816 page = find_get_page(mapping, index);
817 if (page && PageUptodate(page))
818 return page;
819 f2fs_put_page(page, 0);
820
Chao Yucb3bc9e2015-02-05 18:03:40 +0800821 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
822 dn.data_blkaddr = ei.blk + index - ei.fofs;
823 goto got_it;
824 }
825
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900826 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900827 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900828 if (err)
829 return ERR_PTR(err);
830 f2fs_put_dnode(&dn);
831
832 if (dn.data_blkaddr == NULL_ADDR)
833 return ERR_PTR(-ENOENT);
834
835 /* By fallocate(), there is no cached page, but with NEW_ADDR */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900836 if (unlikely(dn.data_blkaddr == NEW_ADDR))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900837 return ERR_PTR(-EINVAL);
838
Chao Yucb3bc9e2015-02-05 18:03:40 +0800839got_it:
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900840 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900841 if (!page)
842 return ERR_PTR(-ENOMEM);
843
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900844 if (PageUptodate(page)) {
845 unlock_page(page);
846 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900847 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900848
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800849 fio.blk_addr = dn.data_blkaddr;
850 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Chao Yu1069bbf2013-11-28 15:43:43 +0800851 if (err)
852 return ERR_PTR(err);
853
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900854 if (sync) {
855 wait_on_page_locked(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900856 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900857 f2fs_put_page(page, 0);
858 return ERR_PTR(-EIO);
859 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900860 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900861 return page;
862}
863
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900864/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900865 * If it tries to access a hole, return an error.
866 * Because, the callers, functions in dir.c and GC, should be able to know
867 * whether this page exists or not.
868 */
869struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
870{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900871 struct address_space *mapping = inode->i_mapping;
872 struct dnode_of_data dn;
873 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800874 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900875 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800876 struct f2fs_io_info fio = {
877 .type = DATA,
878 .rw = READ_SYNC,
879 };
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900880repeat:
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900881 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900882 if (!page)
883 return ERR_PTR(-ENOMEM);
884
Chao Yucb3bc9e2015-02-05 18:03:40 +0800885 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
886 dn.data_blkaddr = ei.blk + index - ei.fofs;
887 goto got_it;
888 }
889
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900890 set_new_dnode(&dn, inode, NULL, NULL, 0);
891 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
892 if (err) {
893 f2fs_put_page(page, 1);
894 return ERR_PTR(err);
895 }
896 f2fs_put_dnode(&dn);
897
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900898 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900899 f2fs_put_page(page, 1);
900 return ERR_PTR(-ENOENT);
901 }
902
Chao Yucb3bc9e2015-02-05 18:03:40 +0800903got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900904 if (PageUptodate(page))
905 return page;
906
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900907 /*
908 * A new dentry page is allocated but not able to be written, since its
909 * new inode page couldn't be allocated due to -ENOSPC.
910 * In such the case, its blkaddr can be remained as NEW_ADDR.
911 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
912 */
913 if (dn.data_blkaddr == NEW_ADDR) {
914 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
915 SetPageUptodate(page);
916 return page;
917 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900918
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800919 fio.blk_addr = dn.data_blkaddr;
920 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900921 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900922 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900923
924 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900925 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900926 f2fs_put_page(page, 1);
927 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900928 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900929 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900930 f2fs_put_page(page, 1);
931 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900932 }
933 return page;
934}
935
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900936/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900937 * Caller ensures that this data page is never allocated.
938 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900939 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800940 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
941 * f2fs_unlock_op().
Jaegeuk Kima8865372013-12-27 17:04:17 +0900942 * Note that, ipage is set only by make_empty_dir.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900943 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900944struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900945 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900946{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900947 struct address_space *mapping = inode->i_mapping;
948 struct page *page;
949 struct dnode_of_data dn;
950 int err;
951
Jaegeuk Kima8865372013-12-27 17:04:17 +0900952 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800953 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900954 if (err)
955 return ERR_PTR(err);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900956repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900957 page = grab_cache_page(mapping, index);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900958 if (!page) {
959 err = -ENOMEM;
960 goto put_err;
961 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900962
963 if (PageUptodate(page))
964 return page;
965
966 if (dn.data_blkaddr == NEW_ADDR) {
967 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900968 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900969 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800970 struct f2fs_io_info fio = {
971 .type = DATA,
972 .rw = READ_SYNC,
973 .blk_addr = dn.data_blkaddr,
974 };
975 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900976 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900977 goto put_err;
978
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900979 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900980 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900981 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900982 err = -EIO;
983 goto put_err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900984 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900985 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900986 f2fs_put_page(page, 1);
987 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900988 }
989 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900990
991 if (new_i_size &&
992 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
993 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900994 /* Only the directory inode sets new_i_size */
995 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900996 }
997 return page;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900998
999put_err:
1000 f2fs_put_dnode(&dn);
1001 return ERR_PTR(err);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001002}
1003
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001004static int __allocate_data_block(struct dnode_of_data *dn)
1005{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001006 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001007 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001008 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001009 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001010 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001011 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001012
1013 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
1014 return -EPERM;
1015 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
1016 return -ENOSPC;
1017
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001018 get_node_info(sbi, dn->nid, &ni);
1019 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1020
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001021 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
1022 seg = CURSEG_DIRECT_IO;
1023
1024 allocate_data_block(sbi, NULL, NULL_ADDR, &dn->data_blkaddr, &sum, seg);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001025
1026 /* direct IO doesn't use extent cache to maximize the performance */
Jaegeuk Kim41ef94b2014-12-30 23:12:11 -08001027 __set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001028
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001029 /* update i_size */
1030 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
1031 dn->ofs_in_node;
1032 if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT))
1033 i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT));
1034
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001035 return 0;
1036}
1037
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001038static void __allocate_data_blocks(struct inode *inode, loff_t offset,
1039 size_t count)
1040{
1041 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1042 struct dnode_of_data dn;
1043 u64 start = F2FS_BYTES_TO_BLK(offset);
1044 u64 len = F2FS_BYTES_TO_BLK(count);
1045 bool allocated;
1046 u64 end_offset;
1047
1048 while (len) {
1049 f2fs_balance_fs(sbi);
1050 f2fs_lock_op(sbi);
1051
1052 /* When reading holes, we need its node page */
1053 set_new_dnode(&dn, inode, NULL, NULL, 0);
1054 if (get_dnode_of_data(&dn, start, ALLOC_NODE))
1055 goto out;
1056
1057 allocated = false;
1058 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
1059
1060 while (dn.ofs_in_node < end_offset && len) {
1061 if (dn.data_blkaddr == NULL_ADDR) {
1062 if (__allocate_data_block(&dn))
1063 goto sync_out;
1064 allocated = true;
1065 }
1066 len--;
1067 start++;
1068 dn.ofs_in_node++;
1069 }
1070
1071 if (allocated)
1072 sync_inode_page(&dn);
1073
1074 f2fs_put_dnode(&dn);
1075 f2fs_unlock_op(sbi);
1076 }
1077 return;
1078
1079sync_out:
1080 if (allocated)
1081 sync_inode_page(&dn);
1082 f2fs_put_dnode(&dn);
1083out:
1084 f2fs_unlock_op(sbi);
1085 return;
1086}
1087
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001088/*
Chao Yu4f4124d2013-12-21 18:02:14 +08001089 * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
1090 * If original data blocks are allocated, then give them to blockdev.
1091 * Otherwise,
1092 * a. preallocate requested block addresses
1093 * b. do not use extent cache for better performance
1094 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001095 */
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001096static int __get_data_block(struct inode *inode, sector_t iblock,
1097 struct buffer_head *bh_result, int create, bool fiemap)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001098{
1099 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
1100 unsigned maxblocks = bh_result->b_size >> blkbits;
1101 struct dnode_of_data dn;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001102 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
1103 pgoff_t pgofs, end_offset;
1104 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001105 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001106 bool allocated = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001107
1108 /* Get the page offset from the block offset(iblock) */
1109 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
1110
Chao Yu7e4dde72015-02-05 17:51:34 +08001111 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Chao Yua2e7d1b2015-02-05 17:50:30 +08001112 f2fs_map_bh(inode->i_sb, pgofs, &ei, bh_result);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001113 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001114 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001115
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001116 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001117 f2fs_lock_op(F2FS_I_SB(inode));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001118
1119 /* When reading holes, we need its node page */
1120 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001121 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001122 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001123 if (err == -ENOENT)
1124 err = 0;
1125 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001126 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001127 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001128 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001129
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001130 if (dn.data_blkaddr != NULL_ADDR) {
Jaegeuk Kimda17eec2015-02-09 10:34:38 -08001131 set_buffer_new(bh_result);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001132 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001133 } else if (create) {
1134 err = __allocate_data_block(&dn);
1135 if (err)
1136 goto put_out;
1137 allocated = true;
Jaegeuk Kimda17eec2015-02-09 10:34:38 -08001138 set_buffer_new(bh_result);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001139 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
1140 } else {
1141 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001142 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001143
Chao Yu6403eb12014-04-26 19:59:52 +08001144 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001145 bh_result->b_size = (((size_t)1) << blkbits);
1146 dn.ofs_in_node++;
1147 pgofs++;
1148
1149get_next:
1150 if (dn.ofs_in_node >= end_offset) {
1151 if (allocated)
1152 sync_inode_page(&dn);
1153 allocated = false;
1154 f2fs_put_dnode(&dn);
1155
1156 set_new_dnode(&dn, inode, NULL, NULL, 0);
1157 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001158 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001159 if (err == -ENOENT)
1160 err = 0;
1161 goto unlock_out;
1162 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001163 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001164 goto put_out;
1165
Chao Yu6403eb12014-04-26 19:59:52 +08001166 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001167 }
1168
1169 if (maxblocks > (bh_result->b_size >> blkbits)) {
1170 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
1171 if (blkaddr == NULL_ADDR && create) {
1172 err = __allocate_data_block(&dn);
1173 if (err)
1174 goto sync_out;
1175 allocated = true;
1176 blkaddr = dn.data_blkaddr;
1177 }
arter97e1c42042014-08-06 23:22:50 +09001178 /* Give more consecutive addresses for the readahead */
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001179 if (blkaddr == (bh_result->b_blocknr + ofs)) {
1180 ofs++;
1181 dn.ofs_in_node++;
1182 pgofs++;
1183 bh_result->b_size += (((size_t)1) << blkbits);
1184 goto get_next;
1185 }
1186 }
1187sync_out:
1188 if (allocated)
1189 sync_inode_page(&dn);
1190put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001191 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001192unlock_out:
1193 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001194 f2fs_unlock_op(F2FS_I_SB(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001195out:
1196 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
1197 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001198}
1199
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001200static int get_data_block(struct inode *inode, sector_t iblock,
1201 struct buffer_head *bh_result, int create)
1202{
1203 return __get_data_block(inode, iblock, bh_result, create, false);
1204}
1205
1206static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
1207 struct buffer_head *bh_result, int create)
1208{
1209 return __get_data_block(inode, iblock, bh_result, create, true);
1210}
1211
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001212int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1213 u64 start, u64 len)
1214{
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001215 return generic_block_fiemap(inode, fieinfo,
1216 start, len, get_data_block_fiemap);
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001217}
1218
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001219static int f2fs_read_data_page(struct file *file, struct page *page)
1220{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001221 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001222 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001223
Chao Yuc20e89c2014-05-06 16:53:08 +08001224 trace_f2fs_readpage(page, DATA);
1225
arter97e1c42042014-08-06 23:22:50 +09001226 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001227 if (f2fs_has_inline_data(inode))
1228 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001229 if (ret == -EAGAIN)
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001230 ret = mpage_readpage(page, get_data_block);
1231
1232 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001233}
1234
1235static int f2fs_read_data_pages(struct file *file,
1236 struct address_space *mapping,
1237 struct list_head *pages, unsigned nr_pages)
1238{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001239 struct inode *inode = file->f_mapping->host;
1240
1241 /* If the file has inline data, skip readpages */
1242 if (f2fs_has_inline_data(inode))
1243 return 0;
1244
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001245 return mpage_readpages(mapping, pages, nr_pages, get_data_block);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001246}
1247
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001248int do_write_data_page(struct page *page, struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001249{
1250 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001251 struct dnode_of_data dn;
1252 int err = 0;
1253
1254 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001255 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001256 if (err)
1257 return err;
1258
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001259 fio->blk_addr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001260
1261 /* This page is already truncated */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001262 if (fio->blk_addr == NULL_ADDR)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001263 goto out_writepage;
1264
1265 set_page_writeback(page);
1266
1267 /*
1268 * If current allocation needs SSR,
1269 * it had better in-place writes for updated data.
1270 */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001271 if (unlikely(fio->blk_addr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001272 !is_cold_data(page) &&
1273 need_inplace_update(inode))) {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001274 rewrite_data_page(page, fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001275 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001276 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001277 write_data_page(page, &dn, fio);
Chao Yu7e4dde72015-02-05 17:51:34 +08001278 f2fs_update_extent_cache(&dn);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001279 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001280 }
1281out_writepage:
1282 f2fs_put_dnode(&dn);
1283 return err;
1284}
1285
1286static int f2fs_write_data_page(struct page *page,
1287 struct writeback_control *wbc)
1288{
1289 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001290 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001291 loff_t i_size = i_size_read(inode);
1292 const pgoff_t end_index = ((unsigned long long) i_size)
1293 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001294 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001295 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001296 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001297 struct f2fs_io_info fio = {
1298 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001299 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001300 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001301
Chao Yuecda0de2014-05-06 16:48:26 +08001302 trace_f2fs_writepage(page, DATA);
1303
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001304 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001305 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001306
1307 /*
1308 * If the offset is out-of-range of file size,
1309 * this page does not have to be written to disk.
1310 */
1311 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001312 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001313 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001314
1315 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001316write:
Chao Yucaf00472015-01-28 17:48:42 +08001317 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001318 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001319 if (f2fs_is_drop_cache(inode))
1320 goto out;
1321 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1322 available_free_memory(sbi, BASE_CHECK))
1323 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001324
Jaegeuk Kim39936832012-11-22 16:21:29 +09001325 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001326 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001327 if (unlikely(f2fs_cp_error(sbi)))
1328 goto redirty_out;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001329 err = do_write_data_page(page, &fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001330 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001331 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001332
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001333 /* we should bypass data pages to proceed the kworkder jobs */
1334 if (unlikely(f2fs_cp_error(sbi))) {
1335 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001336 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001337 }
1338
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001339 if (!wbc->for_reclaim)
1340 need_balance_fs = true;
1341 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001342 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001343
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001344 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001345 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001346 if (f2fs_has_inline_data(inode))
1347 err = f2fs_write_inline_data(inode, page);
1348 if (err == -EAGAIN)
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001349 err = do_write_data_page(page, &fio);
1350 f2fs_unlock_op(sbi);
1351done:
1352 if (err && err != -ENOENT)
1353 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001354
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001355 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001356out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001357 inode_dec_dirty_pages(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001358 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001359 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001360 f2fs_balance_fs(sbi);
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +09001361 if (wbc->for_reclaim)
1362 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001363 return 0;
1364
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001365redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001366 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001367 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001368}
1369
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001370static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1371 void *data)
1372{
1373 struct address_space *mapping = data;
1374 int ret = mapping->a_ops->writepage(page, wbc);
1375 mapping_set_error(mapping, ret);
1376 return ret;
1377}
1378
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001379static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001380 struct writeback_control *wbc)
1381{
1382 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001383 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001384 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001385 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001386 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001387
Chao Yue5748432014-05-06 16:51:24 +08001388 trace_f2fs_writepages(mapping->host, wbc, DATA);
1389
P J Pcfb185a2013-04-03 11:38:00 +09001390 /* deal with chardevs and other special file */
1391 if (!mapping->a_ops->writepage)
1392 return 0;
1393
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001394 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001395 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001396 available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001397 goto skip_write;
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001398
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001399 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001400
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001401 if (!S_ISDIR(inode->i_mode)) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001402 mutex_lock(&sbi->writepages);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001403 locked = true;
1404 }
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001405 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +09001406 if (locked)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001407 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001408
1409 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001410
1411 remove_dirty_dir_inode(inode);
1412
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001413 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001414 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001415
1416skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001417 wbc->pages_skipped += get_dirty_pages(inode);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001418 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001419}
1420
Chao Yu3aab8f82014-07-02 13:25:04 +08001421static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1422{
1423 struct inode *inode = mapping->host;
1424
1425 if (to > inode->i_size) {
1426 truncate_pagecache(inode, inode->i_size);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -07001427 truncate_blocks(inode, inode->i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001428 }
1429}
1430
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001431static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1432 loff_t pos, unsigned len, unsigned flags,
1433 struct page **pagep, void **fsdata)
1434{
1435 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001436 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001437 struct page *page, *ipage;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001438 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
1439 struct dnode_of_data dn;
1440 int err = 0;
1441
Chao Yu62aed042014-05-06 16:46:04 +08001442 trace_f2fs_write_begin(inode, pos, len, flags);
1443
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001444 f2fs_balance_fs(sbi);
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001445
1446 /*
1447 * We should check this at this moment to avoid deadlock on inode page
1448 * and #0 page. The locking rule for inline_data conversion should be:
1449 * lock_page(page #0) -> lock_page(inode_page)
1450 */
1451 if (index != 0) {
1452 err = f2fs_convert_inline_inode(inode);
1453 if (err)
1454 goto fail;
1455 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001456repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001457 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001458 if (!page) {
1459 err = -ENOMEM;
1460 goto fail;
1461 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001462
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001463 *pagep = page;
1464
Gu Zhenge4795562013-09-27 18:08:30 +08001465 f2fs_lock_op(sbi);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001466
1467 /* check inline_data */
1468 ipage = get_node_page(sbi, inode->i_ino);
Chao Yucd34e292014-12-01 11:30:20 +08001469 if (IS_ERR(ipage)) {
1470 err = PTR_ERR(ipage);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001471 goto unlock_fail;
Chao Yucd34e292014-12-01 11:30:20 +08001472 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001473
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001474 set_new_dnode(&dn, inode, ipage, ipage, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001475
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001476 if (f2fs_has_inline_data(inode)) {
1477 if (pos + len <= MAX_INLINE_DATA) {
1478 read_inline_data(page, ipage);
1479 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1480 sync_inode_page(&dn);
1481 goto put_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001482 }
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001483 err = f2fs_convert_inline_page(&dn, page);
1484 if (err)
1485 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001486 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001487 err = f2fs_reserve_block(&dn, index);
1488 if (err)
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001489 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001490put_next:
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001491 f2fs_put_dnode(&dn);
1492 f2fs_unlock_op(sbi);
1493
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001494 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
1495 return 0;
1496
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001497 f2fs_wait_on_page_writeback(page, DATA);
1498
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001499 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1500 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1501 unsigned end = start + len;
1502
1503 /* Reading beyond i_size is simple: memset to zero */
1504 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001505 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001506 }
1507
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001508 if (dn.data_blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001509 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1510 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001511 struct f2fs_io_info fio = {
1512 .type = DATA,
1513 .rw = READ_SYNC,
1514 .blk_addr = dn.data_blkaddr,
1515 };
1516 err = f2fs_submit_page_bio(sbi, page, &fio);
Jan Kara9234f312014-10-22 15:21:47 +02001517 if (err)
1518 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001519
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001520 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001521 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001522 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001523 err = -EIO;
1524 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001525 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001526 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001527 f2fs_put_page(page, 1);
1528 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001529 }
1530 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001531out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001532 SetPageUptodate(page);
1533 clear_cold_data(page);
1534 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001535
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001536put_fail:
1537 f2fs_put_dnode(&dn);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001538unlock_fail:
1539 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001540 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001541fail:
1542 f2fs_write_failed(mapping, pos + len);
1543 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001544}
1545
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001546static int f2fs_write_end(struct file *file,
1547 struct address_space *mapping,
1548 loff_t pos, unsigned len, unsigned copied,
1549 struct page *page, void *fsdata)
1550{
1551 struct inode *inode = page->mapping->host;
1552
Chao Yudfb2bf32014-05-06 16:47:23 +08001553 trace_f2fs_write_end(inode, pos, len, copied);
1554
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001555 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001556
1557 if (pos + copied > i_size_read(inode)) {
1558 i_size_write(inode, pos + copied);
1559 mark_inode_dirty(inode);
1560 update_inode_page(inode);
1561 }
1562
Chao Yu75c3c8b2013-11-16 14:15:59 +08001563 f2fs_put_page(page, 1);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001564 return copied;
1565}
1566
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001567static int check_direct_IO(struct inode *inode, int rw,
Al Viro5b46f252014-03-16 18:07:34 -04001568 struct iov_iter *iter, loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001569{
1570 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001571
1572 if (rw == READ)
1573 return 0;
1574
1575 if (offset & blocksize_mask)
1576 return -EINVAL;
1577
Al Viro5b46f252014-03-16 18:07:34 -04001578 if (iov_iter_alignment(iter) & blocksize_mask)
1579 return -EINVAL;
1580
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001581 return 0;
1582}
1583
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001584static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05001585 struct iov_iter *iter, loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001586{
1587 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001588 struct address_space *mapping = file->f_mapping;
1589 struct inode *inode = mapping->host;
1590 size_t count = iov_iter_count(iter);
1591 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001592
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001593 /* we don't need to use inline_data strictly */
1594 if (f2fs_has_inline_data(inode)) {
1595 err = f2fs_convert_inline_inode(inode);
1596 if (err)
1597 return err;
1598 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001599
Al Viro5b46f252014-03-16 18:07:34 -04001600 if (check_direct_IO(inode, rw, iter, offset))
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001601 return 0;
1602
Chao Yu70407fa2014-07-31 21:11:22 +08001603 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
1604
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001605 if (rw & WRITE)
1606 __allocate_data_blocks(inode, offset, count);
1607
Chao Yu3aab8f82014-07-02 13:25:04 +08001608 err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block);
1609 if (err < 0 && (rw & WRITE))
1610 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001611
1612 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
1613
Chao Yu3aab8f82014-07-02 13:25:04 +08001614 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001615}
1616
Chao Yu487261f2015-02-05 17:44:29 +08001617void f2fs_invalidate_page(struct page *page, unsigned int offset,
1618 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001619{
1620 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001621 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001622
Chao Yu487261f2015-02-05 17:44:29 +08001623 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1624 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001625 return;
1626
Chao Yu487261f2015-02-05 17:44:29 +08001627 if (PageDirty(page)) {
1628 if (inode->i_ino == F2FS_META_INO(sbi))
1629 dec_page_count(sbi, F2FS_DIRTY_META);
1630 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1631 dec_page_count(sbi, F2FS_DIRTY_NODES);
1632 else
1633 inode_dec_dirty_pages(inode);
1634 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001635 ClearPagePrivate(page);
1636}
1637
Chao Yu487261f2015-02-05 17:44:29 +08001638int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001639{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001640 /* If this is dirty page, keep PagePrivate */
1641 if (PageDirty(page))
1642 return 0;
1643
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001644 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001645 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001646}
1647
1648static int f2fs_set_data_page_dirty(struct page *page)
1649{
1650 struct address_space *mapping = page->mapping;
1651 struct inode *inode = mapping->host;
1652
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001653 trace_f2fs_set_page_dirty(page, DATA);
1654
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001655 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001656
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001657 if (f2fs_is_atomic_file(inode)) {
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001658 register_inmem_page(inode, page);
1659 return 1;
1660 }
1661
Jaegeuk Kima18ff062014-01-21 13:32:12 +09001662 mark_inode_dirty(inode);
1663
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001664 if (!PageDirty(page)) {
1665 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001666 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001667 return 1;
1668 }
1669 return 0;
1670}
1671
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001672static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1673{
Chao Yu454ae7e2014-04-22 13:34:01 +08001674 struct inode *inode = mapping->host;
1675
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001676 /* we don't need to use inline_data strictly */
1677 if (f2fs_has_inline_data(inode)) {
1678 int err = f2fs_convert_inline_inode(inode);
1679 if (err)
1680 return err;
1681 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001682 return generic_block_bmap(mapping, block, get_data_block);
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001683}
1684
Chao Yu429511c2015-02-05 17:54:31 +08001685void init_extent_cache_info(struct f2fs_sb_info *sbi)
1686{
1687 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
1688 init_rwsem(&sbi->extent_tree_lock);
1689 INIT_LIST_HEAD(&sbi->extent_list);
1690 spin_lock_init(&sbi->extent_lock);
1691 sbi->total_ext_tree = 0;
1692 atomic_set(&sbi->total_ext_node, 0);
1693}
1694
1695int __init create_extent_cache(void)
1696{
1697 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
1698 sizeof(struct extent_tree));
1699 if (!extent_tree_slab)
1700 return -ENOMEM;
1701 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
1702 sizeof(struct extent_node));
1703 if (!extent_node_slab) {
1704 kmem_cache_destroy(extent_tree_slab);
1705 return -ENOMEM;
1706 }
1707 return 0;
1708}
1709
1710void destroy_extent_cache(void)
1711{
1712 kmem_cache_destroy(extent_node_slab);
1713 kmem_cache_destroy(extent_tree_slab);
1714}
1715
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001716const struct address_space_operations f2fs_dblock_aops = {
1717 .readpage = f2fs_read_data_page,
1718 .readpages = f2fs_read_data_pages,
1719 .writepage = f2fs_write_data_page,
1720 .writepages = f2fs_write_data_pages,
1721 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001722 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001723 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001724 .invalidatepage = f2fs_invalidate_page,
1725 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001726 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001727 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001728};