blob: 81d1fd5810786e47a2a6b91599ae786ecba08901 [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>
15#include <linux/writeback.h>
16#include <linux/backing-dev.h>
17#include <linux/blkdev.h>
18#include <linux/bio.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010019#include <linux/prefetch.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080020#include <linux/uio.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070021#include <linux/cleancache.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090022
23#include "f2fs.h"
24#include "node.h"
25#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080026#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090027#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090028
Chao Yu429511c2015-02-05 17:54:31 +080029static struct kmem_cache *extent_tree_slab;
30static struct kmem_cache *extent_node_slab;
31
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090032static void f2fs_read_end_io(struct bio *bio, int err)
33{
Linus Torvaldsf5688492014-01-30 11:19:05 -080034 struct bio_vec *bvec;
35 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090036
Linus Torvaldsf5688492014-01-30 11:19:05 -080037 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090038 struct page *page = bvec->bv_page;
39
Linus Torvaldsf5688492014-01-30 11:19:05 -080040 if (!err) {
41 SetPageUptodate(page);
42 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090043 ClearPageUptodate(page);
44 SetPageError(page);
45 }
46 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080047 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090048 bio_put(bio);
49}
50
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070051/*
52 * I/O completion handler for multipage BIOs.
53 * copied from fs/mpage.c
54 */
55static void mpage_end_io(struct bio *bio, int err)
56{
57 struct bio_vec *bv;
58 int i;
59
60 bio_for_each_segment_all(bv, bio, i) {
61 struct page *page = bv->bv_page;
62
63 if (!err) {
64 SetPageUptodate(page);
65 } else {
66 ClearPageUptodate(page);
67 SetPageError(page);
68 }
69 unlock_page(page);
70 }
71
72 bio_put(bio);
73}
74
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090075static void f2fs_write_end_io(struct bio *bio, int err)
76{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090077 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080078 struct bio_vec *bvec;
79 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090080
Linus Torvaldsf5688492014-01-30 11:19:05 -080081 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090082 struct page *page = bvec->bv_page;
83
Linus Torvaldsf5688492014-01-30 11:19:05 -080084 if (unlikely(err)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -070085 set_page_dirty(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090086 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim744602c2014-01-24 09:42:16 +090087 f2fs_stop_checkpoint(sbi);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090088 }
89 end_page_writeback(page);
90 dec_page_count(sbi, F2FS_WRITEBACK);
Linus Torvaldsf5688492014-01-30 11:19:05 -080091 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090092
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090093 if (!get_pages(sbi, F2FS_WRITEBACK) &&
94 !list_empty(&sbi->cp_wait.task_list))
95 wake_up(&sbi->cp_wait);
96
97 bio_put(bio);
98}
99
Gu Zheng940a6d32013-12-20 17:39:59 +0800100/*
101 * Low-level block read/write IO operations.
102 */
103static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
104 int npages, bool is_read)
105{
106 struct bio *bio;
107
108 /* No failure on bio allocation */
109 bio = bio_alloc(GFP_NOIO, npages);
110
111 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +0800112 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +0800113 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +0900114 bio->bi_private = sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800115
116 return bio;
117}
118
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900119static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900120{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900121 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900122
123 if (!io->bio)
124 return;
125
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700126 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800127 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700128 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800129 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900130
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700131 submit_bio(fio->rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900132 io->bio = NULL;
133}
134
135void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900136 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900137{
138 enum page_type btype = PAGE_TYPE_OF_BIO(type);
139 struct f2fs_bio_info *io;
140
141 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
142
Chao Yudf0f8dc2014-03-22 14:57:23 +0800143 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900144
145 /* change META to META_FLUSH in the checkpoint procedure */
146 if (type >= META_FLUSH) {
147 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700148 if (test_opt(sbi, NOBARRIER))
149 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
150 else
151 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900152 }
153 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800154 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900155}
156
157/*
158 * Fill the locked page with data located in the block address.
159 * Return unlocked page.
160 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700161int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900162{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900163 struct bio *bio;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700164 struct page *page = fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900165
Chao Yu2ace38e2014-12-24 16:08:14 +0800166 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700167 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900168
169 /* Allocate a new bio */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700170 bio = __bio_alloc(fio->sbi, fio->blk_addr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900171
172 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
173 bio_put(bio);
174 f2fs_put_page(page, 1);
175 return -EFAULT;
176 }
177
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800178 submit_bio(fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900179 return 0;
180}
181
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700182void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900183{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700184 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900185 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900186 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800187 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900188
Gu Zheng940a6d32013-12-20 17:39:59 +0800189 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900190
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800191 verify_block_addr(sbi, fio->blk_addr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900192
Chao Yudf0f8dc2014-03-22 14:57:23 +0800193 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900194
Gu Zheng940a6d32013-12-20 17:39:59 +0800195 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900196 inc_page_count(sbi, F2FS_WRITEBACK);
197
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800198 if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900199 io->fio.rw != fio->rw))
200 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900201alloc_new:
202 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700203 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800204
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800205 io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900206 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900207 }
208
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700209 if (bio_add_page(io->bio, fio->page, PAGE_CACHE_SIZE, 0) <
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900210 PAGE_CACHE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900211 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900212 goto alloc_new;
213 }
214
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800215 io->last_block_in_bio = fio->blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700216 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900217
Chao Yudf0f8dc2014-03-22 14:57:23 +0800218 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700219 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900220}
221
222/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900223 * Lock ordering for the change of data block address:
224 * ->data_page
225 * ->node_page
226 * update block addresses in the node page
227 */
Chao Yu216a6202015-03-19 19:23:32 +0800228void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900229{
230 struct f2fs_node *rn;
231 __le32 *addr_array;
232 struct page *node_page = dn->node_page;
233 unsigned int ofs_in_node = dn->ofs_in_node;
234
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000235 f2fs_wait_on_page_writeback(node_page, NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900236
Gu Zheng45590712013-07-15 17:57:38 +0800237 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900238
239 /* Get physical address of data block */
240 addr_array = blkaddr_in_node(rn);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800241 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900242 set_page_dirty(node_page);
243}
244
245int reserve_new_block(struct dnode_of_data *dn)
246{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700247 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900248
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900249 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900250 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800251 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900252 return -ENOSPC;
253
Namjae Jeonc01e2852013-04-23 17:00:52 +0900254 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
255
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900256 dn->data_blkaddr = NEW_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800257 set_data_blkaddr(dn);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900258 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900259 sync_inode_page(dn);
260 return 0;
261}
262
Huajun Lib6009652013-11-10 23:13:18 +0800263int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
264{
265 bool need_put = dn->inode_page ? false : true;
266 int err;
267
268 err = get_dnode_of_data(dn, index, ALLOC_NODE);
269 if (err)
270 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900271
Huajun Lib6009652013-11-10 23:13:18 +0800272 if (dn->data_blkaddr == NULL_ADDR)
273 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900274 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800275 f2fs_put_dnode(dn);
276 return err;
277}
278
Chao Yu7e4dde72015-02-05 17:51:34 +0800279static bool lookup_extent_info(struct inode *inode, pgoff_t pgofs,
280 struct extent_info *ei)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900281{
282 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900283 pgoff_t start_fofs, end_fofs;
284 block_t start_blkaddr;
285
Chao Yu0c872e22015-02-05 17:46:29 +0800286 read_lock(&fi->ext_lock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900287 if (fi->ext.len == 0) {
Chao Yu0c872e22015-02-05 17:46:29 +0800288 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800289 return false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900290 }
291
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900292 stat_inc_total_hit(inode->i_sb);
293
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900294 start_fofs = fi->ext.fofs;
295 end_fofs = fi->ext.fofs + fi->ext.len - 1;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800296 start_blkaddr = fi->ext.blk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900297
298 if (pgofs >= start_fofs && pgofs <= end_fofs) {
Chao Yua2e7d1b2015-02-05 17:50:30 +0800299 *ei = fi->ext;
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900300 stat_inc_read_hit(inode->i_sb);
Chao Yu0c872e22015-02-05 17:46:29 +0800301 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800302 return true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900303 }
Chao Yu0c872e22015-02-05 17:46:29 +0800304 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800305 return false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900306}
307
Chao Yu7e4dde72015-02-05 17:51:34 +0800308static bool update_extent_info(struct inode *inode, pgoff_t fofs,
309 block_t blkaddr)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900310{
Chao Yu7e4dde72015-02-05 17:51:34 +0800311 struct f2fs_inode_info *fi = F2FS_I(inode);
312 pgoff_t start_fofs, end_fofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900313 block_t start_blkaddr, end_blkaddr;
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900314 int need_update = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900315
Chao Yu0c872e22015-02-05 17:46:29 +0800316 write_lock(&fi->ext_lock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900317
318 start_fofs = fi->ext.fofs;
319 end_fofs = fi->ext.fofs + fi->ext.len - 1;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800320 start_blkaddr = fi->ext.blk;
321 end_blkaddr = fi->ext.blk + fi->ext.len - 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900322
323 /* Drop and initialize the matched extent */
324 if (fi->ext.len == 1 && fofs == start_fofs)
325 fi->ext.len = 0;
326
327 /* Initial extent */
328 if (fi->ext.len == 0) {
Chao Yu7e4dde72015-02-05 17:51:34 +0800329 if (blkaddr != NULL_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900330 fi->ext.fofs = fofs;
Chao Yu7e4dde72015-02-05 17:51:34 +0800331 fi->ext.blk = blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900332 fi->ext.len = 1;
333 }
334 goto end_update;
335 }
336
Namjae Jeon6224da82013-04-06 14:44:32 +0900337 /* Front merge */
Chao Yu7e4dde72015-02-05 17:51:34 +0800338 if (fofs == start_fofs - 1 && blkaddr == start_blkaddr - 1) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900339 fi->ext.fofs--;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800340 fi->ext.blk--;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900341 fi->ext.len++;
342 goto end_update;
343 }
344
345 /* Back merge */
Chao Yu7e4dde72015-02-05 17:51:34 +0800346 if (fofs == end_fofs + 1 && blkaddr == end_blkaddr + 1) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900347 fi->ext.len++;
348 goto end_update;
349 }
350
351 /* Split the existing extent */
352 if (fi->ext.len > 1 &&
353 fofs >= start_fofs && fofs <= end_fofs) {
354 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
355 fi->ext.len = fofs - start_fofs;
356 } else {
357 fi->ext.fofs = fofs + 1;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800358 fi->ext.blk = start_blkaddr + fofs - start_fofs + 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900359 fi->ext.len -= fofs - start_fofs + 1;
360 }
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900361 } else {
362 need_update = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900363 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900364
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900365 /* Finally, if the extent is very fragmented, let's drop the cache. */
366 if (fi->ext.len < F2FS_MIN_EXTENT_LEN) {
367 fi->ext.len = 0;
368 set_inode_flag(fi, FI_NO_EXTENT);
369 need_update = true;
370 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900371end_update:
Chao Yu0c872e22015-02-05 17:46:29 +0800372 write_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800373 return need_update;
374}
375
Chao Yu429511c2015-02-05 17:54:31 +0800376static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
377 struct extent_tree *et, struct extent_info *ei,
378 struct rb_node *parent, struct rb_node **p)
379{
380 struct extent_node *en;
381
382 en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
383 if (!en)
384 return NULL;
385
386 en->ei = *ei;
387 INIT_LIST_HEAD(&en->list);
388
389 rb_link_node(&en->rb_node, parent, p);
390 rb_insert_color(&en->rb_node, &et->root);
391 et->count++;
392 atomic_inc(&sbi->total_ext_node);
393 return en;
394}
395
396static void __detach_extent_node(struct f2fs_sb_info *sbi,
397 struct extent_tree *et, struct extent_node *en)
398{
399 rb_erase(&en->rb_node, &et->root);
400 et->count--;
401 atomic_dec(&sbi->total_ext_node);
Chao Yu62c8af62015-02-05 18:01:39 +0800402
403 if (et->cached_en == en)
404 et->cached_en = NULL;
Chao Yu429511c2015-02-05 17:54:31 +0800405}
406
Chao Yu93dfc522015-03-19 19:24:59 +0800407static struct extent_tree *__find_extent_tree(struct f2fs_sb_info *sbi,
408 nid_t ino)
409{
410 struct extent_tree *et;
411
412 down_read(&sbi->extent_tree_lock);
413 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
414 if (!et) {
415 up_read(&sbi->extent_tree_lock);
416 return NULL;
417 }
418 atomic_inc(&et->refcount);
419 up_read(&sbi->extent_tree_lock);
420
421 return et;
422}
423
424static struct extent_tree *__grab_extent_tree(struct inode *inode)
425{
426 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
427 struct extent_tree *et;
428 nid_t ino = inode->i_ino;
429
430 down_write(&sbi->extent_tree_lock);
431 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
432 if (!et) {
433 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
434 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
435 memset(et, 0, sizeof(struct extent_tree));
436 et->ino = ino;
437 et->root = RB_ROOT;
438 et->cached_en = NULL;
439 rwlock_init(&et->lock);
440 atomic_set(&et->refcount, 0);
441 et->count = 0;
442 sbi->total_ext_tree++;
443 }
444 atomic_inc(&et->refcount);
445 up_write(&sbi->extent_tree_lock);
446
447 return et;
448}
449
Chao Yu429511c2015-02-05 17:54:31 +0800450static struct extent_node *__lookup_extent_tree(struct extent_tree *et,
451 unsigned int fofs)
452{
453 struct rb_node *node = et->root.rb_node;
454 struct extent_node *en;
455
Chao Yu62c8af62015-02-05 18:01:39 +0800456 if (et->cached_en) {
457 struct extent_info *cei = &et->cached_en->ei;
458
459 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
460 return et->cached_en;
461 }
462
Chao Yu429511c2015-02-05 17:54:31 +0800463 while (node) {
464 en = rb_entry(node, struct extent_node, rb_node);
465
Chao Yu62c8af62015-02-05 18:01:39 +0800466 if (fofs < en->ei.fofs) {
Chao Yu429511c2015-02-05 17:54:31 +0800467 node = node->rb_left;
Chao Yu62c8af62015-02-05 18:01:39 +0800468 } else if (fofs >= en->ei.fofs + en->ei.len) {
Chao Yu429511c2015-02-05 17:54:31 +0800469 node = node->rb_right;
Chao Yu62c8af62015-02-05 18:01:39 +0800470 } else {
471 et->cached_en = en;
Chao Yu429511c2015-02-05 17:54:31 +0800472 return en;
Chao Yu62c8af62015-02-05 18:01:39 +0800473 }
Chao Yu429511c2015-02-05 17:54:31 +0800474 }
475 return NULL;
476}
477
478static struct extent_node *__try_back_merge(struct f2fs_sb_info *sbi,
479 struct extent_tree *et, struct extent_node *en)
480{
481 struct extent_node *prev;
482 struct rb_node *node;
483
484 node = rb_prev(&en->rb_node);
485 if (!node)
486 return NULL;
487
488 prev = rb_entry(node, struct extent_node, rb_node);
489 if (__is_back_mergeable(&en->ei, &prev->ei)) {
490 en->ei.fofs = prev->ei.fofs;
491 en->ei.blk = prev->ei.blk;
492 en->ei.len += prev->ei.len;
493 __detach_extent_node(sbi, et, prev);
494 return prev;
495 }
496 return NULL;
497}
498
499static struct extent_node *__try_front_merge(struct f2fs_sb_info *sbi,
500 struct extent_tree *et, struct extent_node *en)
501{
502 struct extent_node *next;
503 struct rb_node *node;
504
505 node = rb_next(&en->rb_node);
506 if (!node)
507 return NULL;
508
509 next = rb_entry(node, struct extent_node, rb_node);
510 if (__is_front_mergeable(&en->ei, &next->ei)) {
511 en->ei.len += next->ei.len;
512 __detach_extent_node(sbi, et, next);
513 return next;
514 }
515 return NULL;
516}
517
518static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
519 struct extent_tree *et, struct extent_info *ei,
520 struct extent_node **den)
521{
522 struct rb_node **p = &et->root.rb_node;
523 struct rb_node *parent = NULL;
524 struct extent_node *en;
525
526 while (*p) {
527 parent = *p;
528 en = rb_entry(parent, struct extent_node, rb_node);
529
530 if (ei->fofs < en->ei.fofs) {
531 if (__is_front_mergeable(ei, &en->ei)) {
532 f2fs_bug_on(sbi, !den);
533 en->ei.fofs = ei->fofs;
534 en->ei.blk = ei->blk;
535 en->ei.len += ei->len;
536 *den = __try_back_merge(sbi, et, en);
537 return en;
538 }
539 p = &(*p)->rb_left;
540 } else if (ei->fofs >= en->ei.fofs + en->ei.len) {
541 if (__is_back_mergeable(ei, &en->ei)) {
542 f2fs_bug_on(sbi, !den);
543 en->ei.len += ei->len;
544 *den = __try_front_merge(sbi, et, en);
545 return en;
546 }
547 p = &(*p)->rb_right;
548 } else {
549 f2fs_bug_on(sbi, 1);
550 }
551 }
552
553 return __attach_extent_node(sbi, et, ei, parent, p);
554}
555
556static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
557 struct extent_tree *et, bool free_all)
558{
559 struct rb_node *node, *next;
560 struct extent_node *en;
561 unsigned int count = et->count;
562
563 node = rb_first(&et->root);
564 while (node) {
565 next = rb_next(node);
566 en = rb_entry(node, struct extent_node, rb_node);
567
568 if (free_all) {
569 spin_lock(&sbi->extent_lock);
570 if (!list_empty(&en->list))
571 list_del_init(&en->list);
572 spin_unlock(&sbi->extent_lock);
573 }
574
575 if (free_all || list_empty(&en->list)) {
576 __detach_extent_node(sbi, et, en);
577 kmem_cache_free(extent_node_slab, en);
578 }
579 node = next;
580 }
581
582 return count - et->count;
583}
584
Chao Yu028a41e2015-03-19 19:26:02 +0800585static void f2fs_init_extent_tree(struct inode *inode,
586 struct f2fs_extent *i_ext)
587{
588 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
589 struct extent_tree *et;
590 struct extent_node *en;
591 struct extent_info ei;
592
593 if (le32_to_cpu(i_ext->len) < F2FS_MIN_EXTENT_LEN)
594 return;
595
596 et = __grab_extent_tree(inode);
597
598 write_lock(&et->lock);
599 if (et->count)
600 goto out;
601
602 set_extent_info(&ei, le32_to_cpu(i_ext->fofs),
603 le32_to_cpu(i_ext->blk), le32_to_cpu(i_ext->len));
604
605 en = __insert_extent_tree(sbi, et, &ei, NULL);
606 if (en) {
607 et->cached_en = en;
608
609 spin_lock(&sbi->extent_lock);
610 list_add_tail(&en->list, &sbi->extent_list);
611 spin_unlock(&sbi->extent_lock);
612 }
613out:
614 write_unlock(&et->lock);
615 atomic_dec(&et->refcount);
616}
617
Chao Yu429511c2015-02-05 17:54:31 +0800618static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
619 struct extent_info *ei)
620{
621 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
622 struct extent_tree *et;
623 struct extent_node *en;
624
Chao Yu1ec46102015-02-05 17:59:59 +0800625 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
626
Chao Yu93dfc522015-03-19 19:24:59 +0800627 et = __find_extent_tree(sbi, inode->i_ino);
628 if (!et)
Chao Yu429511c2015-02-05 17:54:31 +0800629 return false;
Chao Yu429511c2015-02-05 17:54:31 +0800630
631 read_lock(&et->lock);
632 en = __lookup_extent_tree(et, pgofs);
633 if (en) {
634 *ei = en->ei;
635 spin_lock(&sbi->extent_lock);
636 if (!list_empty(&en->list))
637 list_move_tail(&en->list, &sbi->extent_list);
638 spin_unlock(&sbi->extent_lock);
639 stat_inc_read_hit(sbi->sb);
640 }
641 stat_inc_total_hit(sbi->sb);
642 read_unlock(&et->lock);
643
Chao Yu1ec46102015-02-05 17:59:59 +0800644 trace_f2fs_lookup_extent_tree_end(inode, pgofs, en);
645
Chao Yu429511c2015-02-05 17:54:31 +0800646 atomic_dec(&et->refcount);
647 return en ? true : false;
648}
649
650static void f2fs_update_extent_tree(struct inode *inode, pgoff_t fofs,
651 block_t blkaddr)
652{
653 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu429511c2015-02-05 17:54:31 +0800654 struct extent_tree *et;
655 struct extent_node *en = NULL, *en1 = NULL, *en2 = NULL, *en3 = NULL;
656 struct extent_node *den = NULL;
657 struct extent_info ei, dei;
658 unsigned int endofs;
659
Chao Yu1ec46102015-02-05 17:59:59 +0800660 trace_f2fs_update_extent_tree(inode, fofs, blkaddr);
661
Chao Yu93dfc522015-03-19 19:24:59 +0800662 et = __grab_extent_tree(inode);
Chao Yu429511c2015-02-05 17:54:31 +0800663
664 write_lock(&et->lock);
665
666 /* 1. lookup and remove existing extent info in cache */
667 en = __lookup_extent_tree(et, fofs);
668 if (!en)
669 goto update_extent;
670
671 dei = en->ei;
672 __detach_extent_node(sbi, et, en);
673
674 /* 2. if extent can be split more, split and insert the left part */
675 if (dei.len > 1) {
676 /* insert left part of split extent into cache */
677 if (fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
678 set_extent_info(&ei, dei.fofs, dei.blk,
679 fofs - dei.fofs);
680 en1 = __insert_extent_tree(sbi, et, &ei, NULL);
681 }
682
683 /* insert right part of split extent into cache */
684 endofs = dei.fofs + dei.len - 1;
685 if (endofs - fofs >= F2FS_MIN_EXTENT_LEN) {
686 set_extent_info(&ei, fofs + 1,
687 fofs - dei.fofs + dei.blk, endofs - fofs);
688 en2 = __insert_extent_tree(sbi, et, &ei, NULL);
689 }
690 }
691
692update_extent:
693 /* 3. update extent in extent cache */
694 if (blkaddr) {
695 set_extent_info(&ei, fofs, blkaddr, 1);
696 en3 = __insert_extent_tree(sbi, et, &ei, &den);
697 }
698
699 /* 4. update in global extent list */
700 spin_lock(&sbi->extent_lock);
701 if (en && !list_empty(&en->list))
702 list_del(&en->list);
703 /*
704 * en1 and en2 split from en, they will become more and more smaller
705 * fragments after splitting several times. So if the length is smaller
706 * than F2FS_MIN_EXTENT_LEN, we will not add them into extent tree.
707 */
708 if (en1)
709 list_add_tail(&en1->list, &sbi->extent_list);
710 if (en2)
711 list_add_tail(&en2->list, &sbi->extent_list);
712 if (en3) {
713 if (list_empty(&en3->list))
714 list_add_tail(&en3->list, &sbi->extent_list);
715 else
716 list_move_tail(&en3->list, &sbi->extent_list);
717 }
718 if (den && !list_empty(&den->list))
719 list_del(&den->list);
720 spin_unlock(&sbi->extent_lock);
721
722 /* 5. release extent node */
723 if (en)
724 kmem_cache_free(extent_node_slab, en);
725 if (den)
726 kmem_cache_free(extent_node_slab, den);
727
728 write_unlock(&et->lock);
729 atomic_dec(&et->refcount);
730}
731
Chao Yu0bdee482015-03-19 19:27:51 +0800732void f2fs_preserve_extent_tree(struct inode *inode)
733{
734 struct extent_tree *et;
735 struct extent_info *ext = &F2FS_I(inode)->ext;
736 bool sync = false;
737
738 if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
739 return;
740
741 et = __find_extent_tree(F2FS_I_SB(inode), inode->i_ino);
742 if (!et) {
743 if (ext->len) {
744 ext->len = 0;
745 update_inode_page(inode);
746 }
747 return;
748 }
749
750 read_lock(&et->lock);
751 if (et->count) {
752 struct extent_node *en;
753
754 if (et->cached_en) {
755 en = et->cached_en;
756 } else {
757 struct rb_node *node = rb_first(&et->root);
758
759 if (!node)
760 node = rb_last(&et->root);
761 en = rb_entry(node, struct extent_node, rb_node);
762 }
763
764 if (__is_extent_same(ext, &en->ei))
765 goto out;
766
767 *ext = en->ei;
768 sync = true;
769 } else if (ext->len) {
770 ext->len = 0;
771 sync = true;
772 }
773out:
774 read_unlock(&et->lock);
775 atomic_dec(&et->refcount);
776
777 if (sync)
778 update_inode_page(inode);
779}
780
Chao Yu429511c2015-02-05 17:54:31 +0800781void f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
782{
783 struct extent_tree *treevec[EXT_TREE_VEC_SIZE];
784 struct extent_node *en, *tmp;
785 unsigned long ino = F2FS_ROOT_INO(sbi);
786 struct radix_tree_iter iter;
787 void **slot;
788 unsigned int found;
Chao Yu1ec46102015-02-05 17:59:59 +0800789 unsigned int node_cnt = 0, tree_cnt = 0;
Chao Yu429511c2015-02-05 17:54:31 +0800790
Chao Yu1dcc3362015-02-05 17:57:31 +0800791 if (!test_opt(sbi, EXTENT_CACHE))
792 return;
793
Chao Yu429511c2015-02-05 17:54:31 +0800794 if (available_free_memory(sbi, EXTENT_CACHE))
795 return;
796
797 spin_lock(&sbi->extent_lock);
798 list_for_each_entry_safe(en, tmp, &sbi->extent_list, list) {
799 if (!nr_shrink--)
800 break;
801 list_del_init(&en->list);
802 }
803 spin_unlock(&sbi->extent_lock);
804
805 down_read(&sbi->extent_tree_lock);
806 while ((found = radix_tree_gang_lookup(&sbi->extent_tree_root,
807 (void **)treevec, ino, EXT_TREE_VEC_SIZE))) {
808 unsigned i;
809
810 ino = treevec[found - 1]->ino + 1;
811 for (i = 0; i < found; i++) {
812 struct extent_tree *et = treevec[i];
813
814 atomic_inc(&et->refcount);
815 write_lock(&et->lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800816 node_cnt += __free_extent_tree(sbi, et, false);
Chao Yu429511c2015-02-05 17:54:31 +0800817 write_unlock(&et->lock);
818 atomic_dec(&et->refcount);
819 }
820 }
821 up_read(&sbi->extent_tree_lock);
822
823 down_write(&sbi->extent_tree_lock);
824 radix_tree_for_each_slot(slot, &sbi->extent_tree_root, &iter,
825 F2FS_ROOT_INO(sbi)) {
826 struct extent_tree *et = (struct extent_tree *)*slot;
827
828 if (!atomic_read(&et->refcount) && !et->count) {
829 radix_tree_delete(&sbi->extent_tree_root, et->ino);
830 kmem_cache_free(extent_tree_slab, et);
831 sbi->total_ext_tree--;
Chao Yu1ec46102015-02-05 17:59:59 +0800832 tree_cnt++;
Chao Yu429511c2015-02-05 17:54:31 +0800833 }
834 }
835 up_write(&sbi->extent_tree_lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800836
837 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
Chao Yu429511c2015-02-05 17:54:31 +0800838}
839
840void f2fs_destroy_extent_tree(struct inode *inode)
841{
842 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
843 struct extent_tree *et;
Chao Yu1ec46102015-02-05 17:59:59 +0800844 unsigned int node_cnt = 0;
Chao Yu429511c2015-02-05 17:54:31 +0800845
Chao Yu1dcc3362015-02-05 17:57:31 +0800846 if (!test_opt(sbi, EXTENT_CACHE))
847 return;
848
Chao Yu93dfc522015-03-19 19:24:59 +0800849 et = __find_extent_tree(sbi, inode->i_ino);
850 if (!et)
Chao Yu429511c2015-02-05 17:54:31 +0800851 goto out;
Chao Yu429511c2015-02-05 17:54:31 +0800852
853 /* free all extent info belong to this extent tree */
854 write_lock(&et->lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800855 node_cnt = __free_extent_tree(sbi, et, true);
Chao Yu429511c2015-02-05 17:54:31 +0800856 write_unlock(&et->lock);
857
858 atomic_dec(&et->refcount);
859
860 /* try to find and delete extent tree entry in radix tree */
861 down_write(&sbi->extent_tree_lock);
862 et = radix_tree_lookup(&sbi->extent_tree_root, inode->i_ino);
863 if (!et) {
864 up_write(&sbi->extent_tree_lock);
865 goto out;
866 }
867 f2fs_bug_on(sbi, atomic_read(&et->refcount) || et->count);
868 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
869 kmem_cache_free(extent_tree_slab, et);
870 sbi->total_ext_tree--;
871 up_write(&sbi->extent_tree_lock);
872out:
Chao Yu1ec46102015-02-05 17:59:59 +0800873 trace_f2fs_destroy_extent_tree(inode, node_cnt);
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900874 return;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900875}
876
Chao Yu028a41e2015-03-19 19:26:02 +0800877void f2fs_init_extent_cache(struct inode *inode, struct f2fs_extent *i_ext)
878{
879 if (test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
880 f2fs_init_extent_tree(inode, i_ext);
881
882 write_lock(&F2FS_I(inode)->ext_lock);
883 get_extent_info(&F2FS_I(inode)->ext, *i_ext);
884 write_unlock(&F2FS_I(inode)->ext_lock);
885}
886
Chao Yu7e4dde72015-02-05 17:51:34 +0800887static bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
888 struct extent_info *ei)
889{
Chao Yu91c5d9b2015-02-05 18:02:44 +0800890 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
891 return false;
892
Chao Yu1dcc3362015-02-05 17:57:31 +0800893 if (test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
894 return f2fs_lookup_extent_tree(inode, pgofs, ei);
895
Chao Yu7e4dde72015-02-05 17:51:34 +0800896 return lookup_extent_info(inode, pgofs, ei);
897}
898
899void f2fs_update_extent_cache(struct dnode_of_data *dn)
900{
901 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
902 pgoff_t fofs;
903
904 f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR);
905
Chao Yu91c5d9b2015-02-05 18:02:44 +0800906 if (is_inode_flag_set(fi, FI_NO_EXTENT))
907 return;
908
Chao Yu7e4dde72015-02-05 17:51:34 +0800909 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
910 dn->ofs_in_node;
911
Chao Yu1dcc3362015-02-05 17:57:31 +0800912 if (test_opt(F2FS_I_SB(dn->inode), EXTENT_CACHE))
913 return f2fs_update_extent_tree(dn->inode, fofs,
914 dn->data_blkaddr);
915
Chao Yu7e4dde72015-02-05 17:51:34 +0800916 if (update_extent_info(dn->inode, fofs, dn->data_blkaddr))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900917 sync_inode_page(dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900918}
919
920struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
921{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900922 struct address_space *mapping = inode->i_mapping;
923 struct dnode_of_data dn;
924 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800925 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900926 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800927 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700928 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800929 .type = DATA,
930 .rw = sync ? READ_SYNC : READA,
931 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900932
Jaegeuk Kimb7f204c2015-02-25 19:54:48 -0800933 /*
934 * If sync is false, it needs to check its block allocation.
935 * This is need and triggered by two flows:
936 * gc and truncate_partial_data_page.
937 */
938 if (!sync)
939 goto search;
940
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900941 page = find_get_page(mapping, index);
942 if (page && PageUptodate(page))
943 return page;
944 f2fs_put_page(page, 0);
Jaegeuk Kimb7f204c2015-02-25 19:54:48 -0800945search:
Chao Yucb3bc9e2015-02-05 18:03:40 +0800946 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
947 dn.data_blkaddr = ei.blk + index - ei.fofs;
948 goto got_it;
949 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900950
951 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900952 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900953 if (err)
954 return ERR_PTR(err);
955 f2fs_put_dnode(&dn);
956
957 if (dn.data_blkaddr == NULL_ADDR)
958 return ERR_PTR(-ENOENT);
959
960 /* By fallocate(), there is no cached page, but with NEW_ADDR */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900961 if (unlikely(dn.data_blkaddr == NEW_ADDR))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900962 return ERR_PTR(-EINVAL);
963
Chao Yucb3bc9e2015-02-05 18:03:40 +0800964got_it:
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900965 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900966 if (!page)
967 return ERR_PTR(-ENOMEM);
968
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900969 if (PageUptodate(page)) {
970 unlock_page(page);
971 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900972 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900973
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800974 fio.blk_addr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700975 fio.page = page;
976 err = f2fs_submit_page_bio(&fio);
Chao Yu1069bbf2013-11-28 15:43:43 +0800977 if (err)
978 return ERR_PTR(err);
979
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900980 if (sync) {
981 wait_on_page_locked(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900982 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900983 f2fs_put_page(page, 0);
984 return ERR_PTR(-EIO);
985 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900986 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900987 return page;
988}
989
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900990/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900991 * If it tries to access a hole, return an error.
992 * Because, the callers, functions in dir.c and GC, should be able to know
993 * whether this page exists or not.
994 */
995struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
996{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900997 struct address_space *mapping = inode->i_mapping;
998 struct dnode_of_data dn;
999 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +08001000 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001001 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001002 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001003 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001004 .type = DATA,
1005 .rw = READ_SYNC,
1006 };
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001007repeat:
Jaegeuk Kim9ac13492014-04-29 17:35:10 +09001008 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001009 if (!page)
1010 return ERR_PTR(-ENOMEM);
1011
Chao Yucb3bc9e2015-02-05 18:03:40 +08001012 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1013 dn.data_blkaddr = ei.blk + index - ei.fofs;
1014 goto got_it;
1015 }
1016
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001017 set_new_dnode(&dn, inode, NULL, NULL, 0);
1018 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
1019 if (err) {
1020 f2fs_put_page(page, 1);
1021 return ERR_PTR(err);
1022 }
1023 f2fs_put_dnode(&dn);
1024
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001025 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001026 f2fs_put_page(page, 1);
1027 return ERR_PTR(-ENOENT);
1028 }
1029
Chao Yucb3bc9e2015-02-05 18:03:40 +08001030got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001031 if (PageUptodate(page))
1032 return page;
1033
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001034 /*
1035 * A new dentry page is allocated but not able to be written, since its
1036 * new inode page couldn't be allocated due to -ENOSPC.
1037 * In such the case, its blkaddr can be remained as NEW_ADDR.
1038 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
1039 */
1040 if (dn.data_blkaddr == NEW_ADDR) {
1041 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1042 SetPageUptodate(page);
1043 return page;
1044 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001045
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001046 fio.blk_addr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001047 fio.page = page;
1048 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001049 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001050 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001051
1052 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001053 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001054 f2fs_put_page(page, 1);
1055 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001056 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001057 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001058 f2fs_put_page(page, 1);
1059 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001060 }
1061 return page;
1062}
1063
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001064/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001065 * Caller ensures that this data page is never allocated.
1066 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +09001067 *
Chao Yu4f4124d2013-12-21 18:02:14 +08001068 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1069 * f2fs_unlock_op().
Jaegeuk Kima8865372013-12-27 17:04:17 +09001070 * Note that, ipage is set only by make_empty_dir.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001071 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +09001072struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +09001073 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001074{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001075 struct address_space *mapping = inode->i_mapping;
1076 struct page *page;
1077 struct dnode_of_data dn;
1078 int err;
1079
Jaegeuk Kima8865372013-12-27 17:04:17 +09001080 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +08001081 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001082 if (err)
1083 return ERR_PTR(err);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001084repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001085 page = grab_cache_page(mapping, index);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001086 if (!page) {
1087 err = -ENOMEM;
1088 goto put_err;
1089 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001090
1091 if (PageUptodate(page))
1092 return page;
1093
1094 if (dn.data_blkaddr == NEW_ADDR) {
1095 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001096 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001097 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001098 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001099 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001100 .type = DATA,
1101 .rw = READ_SYNC,
1102 .blk_addr = dn.data_blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001103 .page = page,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001104 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001105 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001106 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +09001107 goto put_err;
1108
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001109 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001110 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001111 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001112 err = -EIO;
1113 goto put_err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001114 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001115 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001116 f2fs_put_page(page, 1);
1117 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001118 }
1119 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001120
1121 if (new_i_size &&
1122 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
1123 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +09001124 /* Only the directory inode sets new_i_size */
1125 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001126 }
1127 return page;
Jaegeuk Kima8865372013-12-27 17:04:17 +09001128
1129put_err:
1130 f2fs_put_dnode(&dn);
1131 return ERR_PTR(err);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001132}
1133
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001134static int __allocate_data_block(struct dnode_of_data *dn)
1135{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001136 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001137 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001138 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001139 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001140 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001141 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001142
1143 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
1144 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +08001145
1146 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
1147 if (dn->data_blkaddr == NEW_ADDR)
1148 goto alloc;
1149
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001150 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
1151 return -ENOSPC;
1152
Chao Yudf6136e2015-03-23 10:33:37 +08001153alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001154 get_node_info(sbi, dn->nid, &ni);
1155 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1156
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001157 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
1158 seg = CURSEG_DIRECT_IO;
1159
Chao Yudf6136e2015-03-23 10:33:37 +08001160 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
1161 &sum, seg);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001162
1163 /* direct IO doesn't use extent cache to maximize the performance */
Chao Yu216a6202015-03-19 19:23:32 +08001164 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001165
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001166 /* update i_size */
1167 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
1168 dn->ofs_in_node;
1169 if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT))
1170 i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT));
1171
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001172 return 0;
1173}
1174
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001175static void __allocate_data_blocks(struct inode *inode, loff_t offset,
1176 size_t count)
1177{
1178 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1179 struct dnode_of_data dn;
1180 u64 start = F2FS_BYTES_TO_BLK(offset);
1181 u64 len = F2FS_BYTES_TO_BLK(count);
1182 bool allocated;
1183 u64 end_offset;
1184
1185 while (len) {
1186 f2fs_balance_fs(sbi);
1187 f2fs_lock_op(sbi);
1188
1189 /* When reading holes, we need its node page */
1190 set_new_dnode(&dn, inode, NULL, NULL, 0);
1191 if (get_dnode_of_data(&dn, start, ALLOC_NODE))
1192 goto out;
1193
1194 allocated = false;
1195 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
1196
1197 while (dn.ofs_in_node < end_offset && len) {
Chao Yud6d4f1c2015-03-12 17:04:24 +08001198 block_t blkaddr;
1199
1200 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +08001201 if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR) {
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001202 if (__allocate_data_block(&dn))
1203 goto sync_out;
1204 allocated = true;
1205 }
1206 len--;
1207 start++;
1208 dn.ofs_in_node++;
1209 }
1210
1211 if (allocated)
1212 sync_inode_page(&dn);
1213
1214 f2fs_put_dnode(&dn);
1215 f2fs_unlock_op(sbi);
1216 }
1217 return;
1218
1219sync_out:
1220 if (allocated)
1221 sync_inode_page(&dn);
1222 f2fs_put_dnode(&dn);
1223out:
1224 f2fs_unlock_op(sbi);
1225 return;
1226}
1227
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001228/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001229 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1230 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +08001231 * If original data blocks are allocated, then give them to blockdev.
1232 * Otherwise,
1233 * a. preallocate requested block addresses
1234 * b. do not use extent cache for better performance
1235 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001236 */
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001237static int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1238 int create, bool fiemap)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001239{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001240 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001241 struct dnode_of_data dn;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001242 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
1243 pgoff_t pgofs, end_offset;
1244 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001245 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001246 bool allocated = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001247
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001248 map->m_len = 0;
1249 map->m_flags = 0;
1250
1251 /* it only supports block size == page size */
1252 pgofs = (pgoff_t)map->m_lblk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001253
Chao Yu7e4dde72015-02-05 17:51:34 +08001254 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001255 map->m_pblk = ei.blk + pgofs - ei.fofs;
1256 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1257 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001258 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001259 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001260
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001261 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001262 f2fs_lock_op(F2FS_I_SB(inode));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001263
1264 /* When reading holes, we need its node page */
1265 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001266 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001267 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001268 if (err == -ENOENT)
1269 err = 0;
1270 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001271 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001272 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001273 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001274
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001275 if (dn.data_blkaddr != NULL_ADDR) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001276 map->m_flags = F2FS_MAP_MAPPED;
1277 map->m_pblk = dn.data_blkaddr;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001278 } else if (create) {
1279 err = __allocate_data_block(&dn);
1280 if (err)
1281 goto put_out;
1282 allocated = true;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001283 map->m_flags = F2FS_MAP_NEW | F2FS_MAP_MAPPED;
1284 map->m_pblk = dn.data_blkaddr;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001285 } else {
1286 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001287 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001288
Chao Yu6403eb12014-04-26 19:59:52 +08001289 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001290 map->m_len = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001291 dn.ofs_in_node++;
1292 pgofs++;
1293
1294get_next:
1295 if (dn.ofs_in_node >= end_offset) {
1296 if (allocated)
1297 sync_inode_page(&dn);
1298 allocated = false;
1299 f2fs_put_dnode(&dn);
1300
1301 set_new_dnode(&dn, inode, NULL, NULL, 0);
1302 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001303 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001304 if (err == -ENOENT)
1305 err = 0;
1306 goto unlock_out;
1307 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001308 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001309 goto put_out;
1310
Chao Yu6403eb12014-04-26 19:59:52 +08001311 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001312 }
1313
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001314 if (maxblocks > map->m_len) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001315 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
1316 if (blkaddr == NULL_ADDR && create) {
1317 err = __allocate_data_block(&dn);
1318 if (err)
1319 goto sync_out;
1320 allocated = true;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001321 map->m_flags |= F2FS_MAP_NEW;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001322 blkaddr = dn.data_blkaddr;
1323 }
arter97e1c42042014-08-06 23:22:50 +09001324 /* Give more consecutive addresses for the readahead */
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001325 if (map->m_pblk != NEW_ADDR && blkaddr == (map->m_pblk + ofs)) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001326 ofs++;
1327 dn.ofs_in_node++;
1328 pgofs++;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001329 map->m_len++;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001330 goto get_next;
1331 }
1332 }
1333sync_out:
1334 if (allocated)
1335 sync_inode_page(&dn);
1336put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001337 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001338unlock_out:
1339 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001340 f2fs_unlock_op(F2FS_I_SB(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001341out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001342 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001343 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001344}
1345
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001346static int __get_data_block(struct inode *inode, sector_t iblock,
1347 struct buffer_head *bh, int create, bool fiemap)
1348{
1349 struct f2fs_map_blocks map;
1350 int ret;
1351
1352 map.m_lblk = iblock;
1353 map.m_len = bh->b_size >> inode->i_blkbits;
1354
1355 ret = f2fs_map_blocks(inode, &map, create, fiemap);
1356 if (!ret) {
1357 map_bh(bh, inode->i_sb, map.m_pblk);
1358 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1359 bh->b_size = map.m_len << inode->i_blkbits;
1360 }
1361 return ret;
1362}
1363
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001364static int get_data_block(struct inode *inode, sector_t iblock,
1365 struct buffer_head *bh_result, int create)
1366{
1367 return __get_data_block(inode, iblock, bh_result, create, false);
1368}
1369
1370static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
1371 struct buffer_head *bh_result, int create)
1372{
1373 return __get_data_block(inode, iblock, bh_result, create, true);
1374}
1375
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001376int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1377 u64 start, u64 len)
1378{
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001379 return generic_block_fiemap(inode, fieinfo,
1380 start, len, get_data_block_fiemap);
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001381}
1382
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001383/*
1384 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1385 * Major change was from block_size == page_size in f2fs by default.
1386 */
1387static int f2fs_mpage_readpages(struct address_space *mapping,
1388 struct list_head *pages, struct page *page,
1389 unsigned nr_pages)
1390{
1391 struct bio *bio = NULL;
1392 unsigned page_idx;
1393 sector_t last_block_in_bio = 0;
1394 struct inode *inode = mapping->host;
1395 const unsigned blkbits = inode->i_blkbits;
1396 const unsigned blocksize = 1 << blkbits;
1397 sector_t block_in_file;
1398 sector_t last_block;
1399 sector_t last_block_in_file;
1400 sector_t block_nr;
1401 struct block_device *bdev = inode->i_sb->s_bdev;
1402 struct f2fs_map_blocks map;
1403
1404 map.m_pblk = 0;
1405 map.m_lblk = 0;
1406 map.m_len = 0;
1407 map.m_flags = 0;
1408
1409 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1410
1411 prefetchw(&page->flags);
1412 if (pages) {
1413 page = list_entry(pages->prev, struct page, lru);
1414 list_del(&page->lru);
1415 if (add_to_page_cache_lru(page, mapping,
1416 page->index, GFP_KERNEL))
1417 goto next_page;
1418 }
1419
1420 block_in_file = (sector_t)page->index;
1421 last_block = block_in_file + nr_pages;
1422 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1423 blkbits;
1424 if (last_block > last_block_in_file)
1425 last_block = last_block_in_file;
1426
1427 /*
1428 * Map blocks using the previous result first.
1429 */
1430 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1431 block_in_file > map.m_lblk &&
1432 block_in_file < (map.m_lblk + map.m_len))
1433 goto got_it;
1434
1435 /*
1436 * Then do more f2fs_map_blocks() calls until we are
1437 * done with this page.
1438 */
1439 map.m_flags = 0;
1440
1441 if (block_in_file < last_block) {
1442 map.m_lblk = block_in_file;
1443 map.m_len = last_block - block_in_file;
1444
1445 if (f2fs_map_blocks(inode, &map, 0, false))
1446 goto set_error_page;
1447 }
1448got_it:
1449 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1450 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1451 SetPageMappedToDisk(page);
1452
1453 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1454 SetPageUptodate(page);
1455 goto confused;
1456 }
1457 } else {
1458 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1459 SetPageUptodate(page);
1460 unlock_page(page);
1461 goto next_page;
1462 }
1463
1464 /*
1465 * This page will go to BIO. Do we need to send this
1466 * BIO off first?
1467 */
1468 if (bio && (last_block_in_bio != block_nr - 1)) {
1469submit_and_realloc:
1470 submit_bio(READ, bio);
1471 bio = NULL;
1472 }
1473 if (bio == NULL) {
1474 bio = bio_alloc(GFP_KERNEL,
1475 min_t(int, nr_pages, bio_get_nr_vecs(bdev)));
1476 if (!bio)
1477 goto set_error_page;
1478 bio->bi_bdev = bdev;
1479 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
1480 bio->bi_end_io = mpage_end_io;
1481 bio->bi_private = NULL;
1482 }
1483
1484 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1485 goto submit_and_realloc;
1486
1487 last_block_in_bio = block_nr;
1488 goto next_page;
1489set_error_page:
1490 SetPageError(page);
1491 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1492 unlock_page(page);
1493 goto next_page;
1494confused:
1495 if (bio) {
1496 submit_bio(READ, bio);
1497 bio = NULL;
1498 }
1499 unlock_page(page);
1500next_page:
1501 if (pages)
1502 page_cache_release(page);
1503 }
1504 BUG_ON(pages && !list_empty(pages));
1505 if (bio)
1506 submit_bio(READ, bio);
1507 return 0;
1508}
1509
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001510static int f2fs_read_data_page(struct file *file, struct page *page)
1511{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001512 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001513 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001514
Chao Yuc20e89c2014-05-06 16:53:08 +08001515 trace_f2fs_readpage(page, DATA);
1516
arter97e1c42042014-08-06 23:22:50 +09001517 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001518 if (f2fs_has_inline_data(inode))
1519 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001520 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001521 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001522 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001523}
1524
1525static int f2fs_read_data_pages(struct file *file,
1526 struct address_space *mapping,
1527 struct list_head *pages, unsigned nr_pages)
1528{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001529 struct inode *inode = file->f_mapping->host;
1530
1531 /* If the file has inline data, skip readpages */
1532 if (f2fs_has_inline_data(inode))
1533 return 0;
1534
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001535 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001536}
1537
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001538int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001539{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001540 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001541 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001542 struct dnode_of_data dn;
1543 int err = 0;
1544
1545 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001546 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001547 if (err)
1548 return err;
1549
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001550 fio->blk_addr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001551
1552 /* This page is already truncated */
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001553 if (fio->blk_addr == NULL_ADDR) {
1554 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001555 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001556 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001557
1558 set_page_writeback(page);
1559
1560 /*
1561 * If current allocation needs SSR,
1562 * it had better in-place writes for updated data.
1563 */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001564 if (unlikely(fio->blk_addr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001565 !is_cold_data(page) &&
1566 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001567 rewrite_data_page(fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001568 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001569 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001570 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001571 write_data_page(&dn, fio);
Chao Yu216a6202015-03-19 19:23:32 +08001572 set_data_blkaddr(&dn);
Chao Yu7e4dde72015-02-05 17:51:34 +08001573 f2fs_update_extent_cache(&dn);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001574 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001575 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001576 if (page->index == 0)
1577 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001578 }
1579out_writepage:
1580 f2fs_put_dnode(&dn);
1581 return err;
1582}
1583
1584static int f2fs_write_data_page(struct page *page,
1585 struct writeback_control *wbc)
1586{
1587 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001588 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001589 loff_t i_size = i_size_read(inode);
1590 const pgoff_t end_index = ((unsigned long long) i_size)
1591 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001592 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001593 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001594 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001595 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001596 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001597 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001598 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001599 .page = page,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001600 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001601
Chao Yuecda0de2014-05-06 16:48:26 +08001602 trace_f2fs_writepage(page, DATA);
1603
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001604 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001605 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001606
1607 /*
1608 * If the offset is out-of-range of file size,
1609 * this page does not have to be written to disk.
1610 */
1611 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001612 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001613 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001614
1615 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001616write:
Chao Yucaf00472015-01-28 17:48:42 +08001617 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001618 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001619 if (f2fs_is_drop_cache(inode))
1620 goto out;
1621 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1622 available_free_memory(sbi, BASE_CHECK))
1623 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001624
Jaegeuk Kim39936832012-11-22 16:21:29 +09001625 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001626 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001627 if (unlikely(f2fs_cp_error(sbi)))
1628 goto redirty_out;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001629 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001630 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001631 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001632
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001633 /* we should bypass data pages to proceed the kworkder jobs */
1634 if (unlikely(f2fs_cp_error(sbi))) {
1635 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001636 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001637 }
1638
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001639 if (!wbc->for_reclaim)
1640 need_balance_fs = true;
1641 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001642 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001643
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001644 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001645 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001646 if (f2fs_has_inline_data(inode))
1647 err = f2fs_write_inline_data(inode, page);
1648 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001649 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001650 f2fs_unlock_op(sbi);
1651done:
1652 if (err && err != -ENOENT)
1653 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001654
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001655 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001656out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001657 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001658 if (err)
1659 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001660 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001661 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001662 f2fs_balance_fs(sbi);
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +09001663 if (wbc->for_reclaim)
1664 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001665 return 0;
1666
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001667redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001668 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001669 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001670}
1671
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001672static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1673 void *data)
1674{
1675 struct address_space *mapping = data;
1676 int ret = mapping->a_ops->writepage(page, wbc);
1677 mapping_set_error(mapping, ret);
1678 return ret;
1679}
1680
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001681static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001682 struct writeback_control *wbc)
1683{
1684 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001685 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001686 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001687 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001688 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001689
Chao Yue5748432014-05-06 16:51:24 +08001690 trace_f2fs_writepages(mapping->host, wbc, DATA);
1691
P J Pcfb185a2013-04-03 11:38:00 +09001692 /* deal with chardevs and other special file */
1693 if (!mapping->a_ops->writepage)
1694 return 0;
1695
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001696 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001697 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001698 available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001699 goto skip_write;
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001700
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001701 /* during POR, we don't need to trigger writepage at all. */
1702 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1703 goto skip_write;
1704
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001705 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001706
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001707 if (!S_ISDIR(inode->i_mode)) {
1708 mutex_lock(&sbi->writepages);
1709 locked = true;
1710 }
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001711 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001712 if (locked)
1713 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001714
1715 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001716
1717 remove_dirty_dir_inode(inode);
1718
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001719 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001720 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001721
1722skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001723 wbc->pages_skipped += get_dirty_pages(inode);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001724 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001725}
1726
Chao Yu3aab8f82014-07-02 13:25:04 +08001727static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1728{
1729 struct inode *inode = mapping->host;
1730
1731 if (to > inode->i_size) {
1732 truncate_pagecache(inode, inode->i_size);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -07001733 truncate_blocks(inode, inode->i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001734 }
1735}
1736
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001737static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1738 loff_t pos, unsigned len, unsigned flags,
1739 struct page **pagep, void **fsdata)
1740{
1741 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001742 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001743 struct page *page, *ipage;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001744 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
1745 struct dnode_of_data dn;
1746 int err = 0;
1747
Chao Yu62aed042014-05-06 16:46:04 +08001748 trace_f2fs_write_begin(inode, pos, len, flags);
1749
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001750 f2fs_balance_fs(sbi);
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001751
1752 /*
1753 * We should check this at this moment to avoid deadlock on inode page
1754 * and #0 page. The locking rule for inline_data conversion should be:
1755 * lock_page(page #0) -> lock_page(inode_page)
1756 */
1757 if (index != 0) {
1758 err = f2fs_convert_inline_inode(inode);
1759 if (err)
1760 goto fail;
1761 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001762repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001763 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001764 if (!page) {
1765 err = -ENOMEM;
1766 goto fail;
1767 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001768
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001769 *pagep = page;
1770
Gu Zhenge4795562013-09-27 18:08:30 +08001771 f2fs_lock_op(sbi);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001772
1773 /* check inline_data */
1774 ipage = get_node_page(sbi, inode->i_ino);
Chao Yucd34e292014-12-01 11:30:20 +08001775 if (IS_ERR(ipage)) {
1776 err = PTR_ERR(ipage);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001777 goto unlock_fail;
Chao Yucd34e292014-12-01 11:30:20 +08001778 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001779
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001780 set_new_dnode(&dn, inode, ipage, ipage, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001781
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001782 if (f2fs_has_inline_data(inode)) {
1783 if (pos + len <= MAX_INLINE_DATA) {
1784 read_inline_data(page, ipage);
1785 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1786 sync_inode_page(&dn);
1787 goto put_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001788 }
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001789 err = f2fs_convert_inline_page(&dn, page);
1790 if (err)
1791 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001792 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001793 err = f2fs_reserve_block(&dn, index);
1794 if (err)
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001795 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001796put_next:
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001797 f2fs_put_dnode(&dn);
1798 f2fs_unlock_op(sbi);
1799
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001800 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
1801 return 0;
1802
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001803 f2fs_wait_on_page_writeback(page, DATA);
1804
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001805 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1806 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1807 unsigned end = start + len;
1808
1809 /* Reading beyond i_size is simple: memset to zero */
1810 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001811 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001812 }
1813
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001814 if (dn.data_blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001815 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1816 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001817 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001818 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001819 .type = DATA,
1820 .rw = READ_SYNC,
1821 .blk_addr = dn.data_blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001822 .page = page,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001823 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001824 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001825 if (err)
1826 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001827
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001828 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001829 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001830 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001831 err = -EIO;
1832 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001833 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001834 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001835 f2fs_put_page(page, 1);
1836 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001837 }
1838 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001839out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001840 SetPageUptodate(page);
1841 clear_cold_data(page);
1842 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001843
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001844put_fail:
1845 f2fs_put_dnode(&dn);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001846unlock_fail:
1847 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001848 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001849fail:
1850 f2fs_write_failed(mapping, pos + len);
1851 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001852}
1853
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001854static int f2fs_write_end(struct file *file,
1855 struct address_space *mapping,
1856 loff_t pos, unsigned len, unsigned copied,
1857 struct page *page, void *fsdata)
1858{
1859 struct inode *inode = page->mapping->host;
1860
Chao Yudfb2bf32014-05-06 16:47:23 +08001861 trace_f2fs_write_end(inode, pos, len, copied);
1862
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001863 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001864
1865 if (pos + copied > i_size_read(inode)) {
1866 i_size_write(inode, pos + copied);
1867 mark_inode_dirty(inode);
1868 update_inode_page(inode);
1869 }
1870
Chao Yu75c3c8b2013-11-16 14:15:59 +08001871 f2fs_put_page(page, 1);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001872 return copied;
1873}
1874
Omar Sandoval6f673762015-03-16 04:33:52 -07001875static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1876 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001877{
1878 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001879
Omar Sandoval6f673762015-03-16 04:33:52 -07001880 if (iov_iter_rw(iter) == READ)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001881 return 0;
1882
1883 if (offset & blocksize_mask)
1884 return -EINVAL;
1885
Al Viro5b46f252014-03-16 18:07:34 -04001886 if (iov_iter_alignment(iter) & blocksize_mask)
1887 return -EINVAL;
1888
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001889 return 0;
1890}
1891
Omar Sandoval22c61862015-03-16 04:33:53 -07001892static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
1893 loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001894{
1895 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001896 struct address_space *mapping = file->f_mapping;
1897 struct inode *inode = mapping->host;
1898 size_t count = iov_iter_count(iter);
1899 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001900
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001901 /* we don't need to use inline_data strictly */
1902 if (f2fs_has_inline_data(inode)) {
1903 err = f2fs_convert_inline_inode(inode);
1904 if (err)
1905 return err;
1906 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001907
Omar Sandoval6f673762015-03-16 04:33:52 -07001908 if (check_direct_IO(inode, iter, offset))
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001909 return 0;
1910
Omar Sandoval6f673762015-03-16 04:33:52 -07001911 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001912
Omar Sandoval6f673762015-03-16 04:33:52 -07001913 if (iov_iter_rw(iter) == WRITE)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001914 __allocate_data_blocks(inode, offset, count);
1915
Omar Sandoval17f8c842015-03-16 04:33:50 -07001916 err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block);
Omar Sandoval6f673762015-03-16 04:33:52 -07001917 if (err < 0 && iov_iter_rw(iter) == WRITE)
Chao Yu3aab8f82014-07-02 13:25:04 +08001918 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001919
Omar Sandoval6f673762015-03-16 04:33:52 -07001920 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001921
Chao Yu3aab8f82014-07-02 13:25:04 +08001922 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001923}
1924
Chao Yu487261f2015-02-05 17:44:29 +08001925void f2fs_invalidate_page(struct page *page, unsigned int offset,
1926 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001927{
1928 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001929 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001930
Chao Yu487261f2015-02-05 17:44:29 +08001931 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1932 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001933 return;
1934
Chao Yu487261f2015-02-05 17:44:29 +08001935 if (PageDirty(page)) {
1936 if (inode->i_ino == F2FS_META_INO(sbi))
1937 dec_page_count(sbi, F2FS_DIRTY_META);
1938 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1939 dec_page_count(sbi, F2FS_DIRTY_NODES);
1940 else
1941 inode_dec_dirty_pages(inode);
1942 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001943 ClearPagePrivate(page);
1944}
1945
Chao Yu487261f2015-02-05 17:44:29 +08001946int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001947{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001948 /* If this is dirty page, keep PagePrivate */
1949 if (PageDirty(page))
1950 return 0;
1951
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001952 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001953 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001954}
1955
1956static int f2fs_set_data_page_dirty(struct page *page)
1957{
1958 struct address_space *mapping = page->mapping;
1959 struct inode *inode = mapping->host;
1960
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001961 trace_f2fs_set_page_dirty(page, DATA);
1962
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001963 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001964
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001965 if (f2fs_is_atomic_file(inode)) {
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001966 register_inmem_page(inode, page);
1967 return 1;
1968 }
1969
Jaegeuk Kima18ff062014-01-21 13:32:12 +09001970 mark_inode_dirty(inode);
1971
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001972 if (!PageDirty(page)) {
1973 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001974 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001975 return 1;
1976 }
1977 return 0;
1978}
1979
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001980static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1981{
Chao Yu454ae7e2014-04-22 13:34:01 +08001982 struct inode *inode = mapping->host;
1983
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001984 /* we don't need to use inline_data strictly */
1985 if (f2fs_has_inline_data(inode)) {
1986 int err = f2fs_convert_inline_inode(inode);
1987 if (err)
1988 return err;
1989 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001990 return generic_block_bmap(mapping, block, get_data_block);
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001991}
1992
Chao Yu429511c2015-02-05 17:54:31 +08001993void init_extent_cache_info(struct f2fs_sb_info *sbi)
1994{
1995 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
1996 init_rwsem(&sbi->extent_tree_lock);
1997 INIT_LIST_HEAD(&sbi->extent_list);
1998 spin_lock_init(&sbi->extent_lock);
1999 sbi->total_ext_tree = 0;
2000 atomic_set(&sbi->total_ext_node, 0);
2001}
2002
2003int __init create_extent_cache(void)
2004{
2005 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
2006 sizeof(struct extent_tree));
2007 if (!extent_tree_slab)
2008 return -ENOMEM;
2009 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
2010 sizeof(struct extent_node));
2011 if (!extent_node_slab) {
2012 kmem_cache_destroy(extent_tree_slab);
2013 return -ENOMEM;
2014 }
2015 return 0;
2016}
2017
2018void destroy_extent_cache(void)
2019{
2020 kmem_cache_destroy(extent_node_slab);
2021 kmem_cache_destroy(extent_tree_slab);
2022}
2023
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002024const struct address_space_operations f2fs_dblock_aops = {
2025 .readpage = f2fs_read_data_page,
2026 .readpages = f2fs_read_data_pages,
2027 .writepage = f2fs_write_data_page,
2028 .writepages = f2fs_write_data_pages,
2029 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002030 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002031 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002032 .invalidatepage = f2fs_invalidate_page,
2033 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002034 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002035 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002036};