blob: 9bedfa8dd3a5305d1407ce04c5fa3319a6882cd3 [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
Chao Yu12377022015-05-25 18:03:38 +080037 if (f2fs_bio_encrypted(bio)) {
38 if (err) {
39 f2fs_release_crypto_ctx(bio->bi_private);
40 } else {
41 f2fs_end_io_crypto_work(bio->bi_private, bio);
42 return;
43 }
44 }
45
Linus Torvaldsf5688492014-01-30 11:19:05 -080046 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090047 struct page *page = bvec->bv_page;
48
Linus Torvaldsf5688492014-01-30 11:19:05 -080049 if (!err) {
50 SetPageUptodate(page);
51 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090052 ClearPageUptodate(page);
53 SetPageError(page);
54 }
55 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080056 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090057 bio_put(bio);
58}
59
60static void f2fs_write_end_io(struct bio *bio, int err)
61{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090062 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080063 struct bio_vec *bvec;
64 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090065
Linus Torvaldsf5688492014-01-30 11:19:05 -080066 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090067 struct page *page = bvec->bv_page;
68
Jaegeuk Kim4375a332015-04-23 12:04:33 -070069 f2fs_restore_and_release_control_page(&page);
70
Linus Torvaldsf5688492014-01-30 11:19:05 -080071 if (unlikely(err)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -070072 set_page_dirty(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090073 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim744602c2014-01-24 09:42:16 +090074 f2fs_stop_checkpoint(sbi);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090075 }
76 end_page_writeback(page);
77 dec_page_count(sbi, F2FS_WRITEBACK);
Linus Torvaldsf5688492014-01-30 11:19:05 -080078 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090079
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090080 if (!get_pages(sbi, F2FS_WRITEBACK) &&
81 !list_empty(&sbi->cp_wait.task_list))
82 wake_up(&sbi->cp_wait);
83
84 bio_put(bio);
85}
86
Gu Zheng940a6d32013-12-20 17:39:59 +080087/*
88 * Low-level block read/write IO operations.
89 */
90static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
91 int npages, bool is_read)
92{
93 struct bio *bio;
94
95 /* No failure on bio allocation */
96 bio = bio_alloc(GFP_NOIO, npages);
97
98 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +080099 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +0800100 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +0800101 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800102
103 return bio;
104}
105
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900106static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900107{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900108 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900109
110 if (!io->bio)
111 return;
112
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700113 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800114 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700115 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800116 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900117
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700118 submit_bio(fio->rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900119 io->bio = NULL;
120}
121
122void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900123 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900124{
125 enum page_type btype = PAGE_TYPE_OF_BIO(type);
126 struct f2fs_bio_info *io;
127
128 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
129
Chao Yudf0f8dc2014-03-22 14:57:23 +0800130 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900131
132 /* change META to META_FLUSH in the checkpoint procedure */
133 if (type >= META_FLUSH) {
134 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700135 if (test_opt(sbi, NOBARRIER))
136 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
137 else
138 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900139 }
140 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800141 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900142}
143
144/*
145 * Fill the locked page with data located in the block address.
146 * Return unlocked page.
147 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700148int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900149{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900150 struct bio *bio;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700151 struct page *page = fio->encrypted_page ? fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900152
Chao Yu2ace38e2014-12-24 16:08:14 +0800153 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700154 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900155
156 /* Allocate a new bio */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700157 bio = __bio_alloc(fio->sbi, fio->blk_addr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900158
159 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
160 bio_put(bio);
161 f2fs_put_page(page, 1);
162 return -EFAULT;
163 }
164
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800165 submit_bio(fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900166 return 0;
167}
168
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700169void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900170{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700171 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900172 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900173 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800174 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700175 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900176
Gu Zheng940a6d32013-12-20 17:39:59 +0800177 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900178
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800179 verify_block_addr(sbi, fio->blk_addr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900180
Chao Yudf0f8dc2014-03-22 14:57:23 +0800181 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900182
Gu Zheng940a6d32013-12-20 17:39:59 +0800183 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900184 inc_page_count(sbi, F2FS_WRITEBACK);
185
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800186 if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900187 io->fio.rw != fio->rw))
188 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900189alloc_new:
190 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700191 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800192
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800193 io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900194 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900195 }
196
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700197 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
198
199 if (bio_add_page(io->bio, bio_page, PAGE_CACHE_SIZE, 0) <
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900200 PAGE_CACHE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900201 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900202 goto alloc_new;
203 }
204
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800205 io->last_block_in_bio = fio->blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700206 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900207
Chao Yudf0f8dc2014-03-22 14:57:23 +0800208 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700209 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900210}
211
212/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900213 * Lock ordering for the change of data block address:
214 * ->data_page
215 * ->node_page
216 * update block addresses in the node page
217 */
Chao Yu216a6202015-03-19 19:23:32 +0800218void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900219{
220 struct f2fs_node *rn;
221 __le32 *addr_array;
222 struct page *node_page = dn->node_page;
223 unsigned int ofs_in_node = dn->ofs_in_node;
224
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000225 f2fs_wait_on_page_writeback(node_page, NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900226
Gu Zheng45590712013-07-15 17:57:38 +0800227 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900228
229 /* Get physical address of data block */
230 addr_array = blkaddr_in_node(rn);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800231 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900232 set_page_dirty(node_page);
233}
234
235int reserve_new_block(struct dnode_of_data *dn)
236{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700237 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900238
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900239 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900240 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800241 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900242 return -ENOSPC;
243
Namjae Jeonc01e2852013-04-23 17:00:52 +0900244 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
245
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900246 dn->data_blkaddr = NEW_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800247 set_data_blkaddr(dn);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900248 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900249 sync_inode_page(dn);
250 return 0;
251}
252
Huajun Lib6009652013-11-10 23:13:18 +0800253int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
254{
255 bool need_put = dn->inode_page ? false : true;
256 int err;
257
258 err = get_dnode_of_data(dn, index, ALLOC_NODE);
259 if (err)
260 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900261
Huajun Lib6009652013-11-10 23:13:18 +0800262 if (dn->data_blkaddr == NULL_ADDR)
263 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900264 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800265 f2fs_put_dnode(dn);
266 return err;
267}
268
Chao Yu7e4dde72015-02-05 17:51:34 +0800269static bool lookup_extent_info(struct inode *inode, pgoff_t pgofs,
270 struct extent_info *ei)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900271{
272 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900273 pgoff_t start_fofs, end_fofs;
274 block_t start_blkaddr;
275
Chao Yu0c872e22015-02-05 17:46:29 +0800276 read_lock(&fi->ext_lock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900277 if (fi->ext.len == 0) {
Chao Yu0c872e22015-02-05 17:46:29 +0800278 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800279 return false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900280 }
281
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900282 stat_inc_total_hit(inode->i_sb);
283
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900284 start_fofs = fi->ext.fofs;
285 end_fofs = fi->ext.fofs + fi->ext.len - 1;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800286 start_blkaddr = fi->ext.blk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900287
288 if (pgofs >= start_fofs && pgofs <= end_fofs) {
Chao Yua2e7d1b2015-02-05 17:50:30 +0800289 *ei = fi->ext;
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900290 stat_inc_read_hit(inode->i_sb);
Chao Yu0c872e22015-02-05 17:46:29 +0800291 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800292 return true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900293 }
Chao Yu0c872e22015-02-05 17:46:29 +0800294 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800295 return false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900296}
297
Chao Yu7e4dde72015-02-05 17:51:34 +0800298static bool update_extent_info(struct inode *inode, pgoff_t fofs,
299 block_t blkaddr)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900300{
Chao Yu7e4dde72015-02-05 17:51:34 +0800301 struct f2fs_inode_info *fi = F2FS_I(inode);
302 pgoff_t start_fofs, end_fofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900303 block_t start_blkaddr, end_blkaddr;
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900304 int need_update = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900305
Chao Yu0c872e22015-02-05 17:46:29 +0800306 write_lock(&fi->ext_lock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900307
308 start_fofs = fi->ext.fofs;
309 end_fofs = fi->ext.fofs + fi->ext.len - 1;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800310 start_blkaddr = fi->ext.blk;
311 end_blkaddr = fi->ext.blk + fi->ext.len - 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900312
313 /* Drop and initialize the matched extent */
314 if (fi->ext.len == 1 && fofs == start_fofs)
315 fi->ext.len = 0;
316
317 /* Initial extent */
318 if (fi->ext.len == 0) {
Chao Yu7e4dde72015-02-05 17:51:34 +0800319 if (blkaddr != NULL_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900320 fi->ext.fofs = fofs;
Chao Yu7e4dde72015-02-05 17:51:34 +0800321 fi->ext.blk = blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900322 fi->ext.len = 1;
323 }
324 goto end_update;
325 }
326
Namjae Jeon6224da82013-04-06 14:44:32 +0900327 /* Front merge */
Chao Yu7e4dde72015-02-05 17:51:34 +0800328 if (fofs == start_fofs - 1 && blkaddr == start_blkaddr - 1) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900329 fi->ext.fofs--;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800330 fi->ext.blk--;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900331 fi->ext.len++;
332 goto end_update;
333 }
334
335 /* Back merge */
Chao Yu7e4dde72015-02-05 17:51:34 +0800336 if (fofs == end_fofs + 1 && blkaddr == end_blkaddr + 1) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900337 fi->ext.len++;
338 goto end_update;
339 }
340
341 /* Split the existing extent */
342 if (fi->ext.len > 1 &&
343 fofs >= start_fofs && fofs <= end_fofs) {
344 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
345 fi->ext.len = fofs - start_fofs;
346 } else {
347 fi->ext.fofs = fofs + 1;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800348 fi->ext.blk = start_blkaddr + fofs - start_fofs + 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900349 fi->ext.len -= fofs - start_fofs + 1;
350 }
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900351 } else {
352 need_update = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900353 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900354
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900355 /* Finally, if the extent is very fragmented, let's drop the cache. */
356 if (fi->ext.len < F2FS_MIN_EXTENT_LEN) {
357 fi->ext.len = 0;
358 set_inode_flag(fi, FI_NO_EXTENT);
359 need_update = true;
360 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900361end_update:
Chao Yu0c872e22015-02-05 17:46:29 +0800362 write_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800363 return need_update;
364}
365
Chao Yu429511c2015-02-05 17:54:31 +0800366static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
367 struct extent_tree *et, struct extent_info *ei,
368 struct rb_node *parent, struct rb_node **p)
369{
370 struct extent_node *en;
371
372 en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
373 if (!en)
374 return NULL;
375
376 en->ei = *ei;
377 INIT_LIST_HEAD(&en->list);
378
379 rb_link_node(&en->rb_node, parent, p);
380 rb_insert_color(&en->rb_node, &et->root);
381 et->count++;
382 atomic_inc(&sbi->total_ext_node);
383 return en;
384}
385
386static void __detach_extent_node(struct f2fs_sb_info *sbi,
387 struct extent_tree *et, struct extent_node *en)
388{
389 rb_erase(&en->rb_node, &et->root);
390 et->count--;
391 atomic_dec(&sbi->total_ext_node);
Chao Yu62c8af62015-02-05 18:01:39 +0800392
393 if (et->cached_en == en)
394 et->cached_en = NULL;
Chao Yu429511c2015-02-05 17:54:31 +0800395}
396
Chao Yu93dfc522015-03-19 19:24:59 +0800397static struct extent_tree *__find_extent_tree(struct f2fs_sb_info *sbi,
398 nid_t ino)
399{
400 struct extent_tree *et;
401
402 down_read(&sbi->extent_tree_lock);
403 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
404 if (!et) {
405 up_read(&sbi->extent_tree_lock);
406 return NULL;
407 }
408 atomic_inc(&et->refcount);
409 up_read(&sbi->extent_tree_lock);
410
411 return et;
412}
413
414static struct extent_tree *__grab_extent_tree(struct inode *inode)
415{
416 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
417 struct extent_tree *et;
418 nid_t ino = inode->i_ino;
419
420 down_write(&sbi->extent_tree_lock);
421 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
422 if (!et) {
423 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
424 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
425 memset(et, 0, sizeof(struct extent_tree));
426 et->ino = ino;
427 et->root = RB_ROOT;
428 et->cached_en = NULL;
429 rwlock_init(&et->lock);
430 atomic_set(&et->refcount, 0);
431 et->count = 0;
432 sbi->total_ext_tree++;
433 }
434 atomic_inc(&et->refcount);
435 up_write(&sbi->extent_tree_lock);
436
437 return et;
438}
439
Chao Yu429511c2015-02-05 17:54:31 +0800440static struct extent_node *__lookup_extent_tree(struct extent_tree *et,
441 unsigned int fofs)
442{
443 struct rb_node *node = et->root.rb_node;
444 struct extent_node *en;
445
Chao Yu62c8af62015-02-05 18:01:39 +0800446 if (et->cached_en) {
447 struct extent_info *cei = &et->cached_en->ei;
448
449 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
450 return et->cached_en;
451 }
452
Chao Yu429511c2015-02-05 17:54:31 +0800453 while (node) {
454 en = rb_entry(node, struct extent_node, rb_node);
455
Chao Yu62c8af62015-02-05 18:01:39 +0800456 if (fofs < en->ei.fofs) {
Chao Yu429511c2015-02-05 17:54:31 +0800457 node = node->rb_left;
Chao Yu62c8af62015-02-05 18:01:39 +0800458 } else if (fofs >= en->ei.fofs + en->ei.len) {
Chao Yu429511c2015-02-05 17:54:31 +0800459 node = node->rb_right;
Chao Yu62c8af62015-02-05 18:01:39 +0800460 } else {
461 et->cached_en = en;
Chao Yu429511c2015-02-05 17:54:31 +0800462 return en;
Chao Yu62c8af62015-02-05 18:01:39 +0800463 }
Chao Yu429511c2015-02-05 17:54:31 +0800464 }
465 return NULL;
466}
467
468static struct extent_node *__try_back_merge(struct f2fs_sb_info *sbi,
469 struct extent_tree *et, struct extent_node *en)
470{
471 struct extent_node *prev;
472 struct rb_node *node;
473
474 node = rb_prev(&en->rb_node);
475 if (!node)
476 return NULL;
477
478 prev = rb_entry(node, struct extent_node, rb_node);
479 if (__is_back_mergeable(&en->ei, &prev->ei)) {
480 en->ei.fofs = prev->ei.fofs;
481 en->ei.blk = prev->ei.blk;
482 en->ei.len += prev->ei.len;
483 __detach_extent_node(sbi, et, prev);
484 return prev;
485 }
486 return NULL;
487}
488
489static struct extent_node *__try_front_merge(struct f2fs_sb_info *sbi,
490 struct extent_tree *et, struct extent_node *en)
491{
492 struct extent_node *next;
493 struct rb_node *node;
494
495 node = rb_next(&en->rb_node);
496 if (!node)
497 return NULL;
498
499 next = rb_entry(node, struct extent_node, rb_node);
500 if (__is_front_mergeable(&en->ei, &next->ei)) {
501 en->ei.len += next->ei.len;
502 __detach_extent_node(sbi, et, next);
503 return next;
504 }
505 return NULL;
506}
507
508static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
509 struct extent_tree *et, struct extent_info *ei,
510 struct extent_node **den)
511{
512 struct rb_node **p = &et->root.rb_node;
513 struct rb_node *parent = NULL;
514 struct extent_node *en;
515
516 while (*p) {
517 parent = *p;
518 en = rb_entry(parent, struct extent_node, rb_node);
519
520 if (ei->fofs < en->ei.fofs) {
521 if (__is_front_mergeable(ei, &en->ei)) {
522 f2fs_bug_on(sbi, !den);
523 en->ei.fofs = ei->fofs;
524 en->ei.blk = ei->blk;
525 en->ei.len += ei->len;
526 *den = __try_back_merge(sbi, et, en);
527 return en;
528 }
529 p = &(*p)->rb_left;
530 } else if (ei->fofs >= en->ei.fofs + en->ei.len) {
531 if (__is_back_mergeable(ei, &en->ei)) {
532 f2fs_bug_on(sbi, !den);
533 en->ei.len += ei->len;
534 *den = __try_front_merge(sbi, et, en);
535 return en;
536 }
537 p = &(*p)->rb_right;
538 } else {
539 f2fs_bug_on(sbi, 1);
540 }
541 }
542
543 return __attach_extent_node(sbi, et, ei, parent, p);
544}
545
546static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
547 struct extent_tree *et, bool free_all)
548{
549 struct rb_node *node, *next;
550 struct extent_node *en;
551 unsigned int count = et->count;
552
553 node = rb_first(&et->root);
554 while (node) {
555 next = rb_next(node);
556 en = rb_entry(node, struct extent_node, rb_node);
557
558 if (free_all) {
559 spin_lock(&sbi->extent_lock);
560 if (!list_empty(&en->list))
561 list_del_init(&en->list);
562 spin_unlock(&sbi->extent_lock);
563 }
564
565 if (free_all || list_empty(&en->list)) {
566 __detach_extent_node(sbi, et, en);
567 kmem_cache_free(extent_node_slab, en);
568 }
569 node = next;
570 }
571
572 return count - et->count;
573}
574
Chao Yu028a41e2015-03-19 19:26:02 +0800575static void f2fs_init_extent_tree(struct inode *inode,
576 struct f2fs_extent *i_ext)
577{
578 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
579 struct extent_tree *et;
580 struct extent_node *en;
581 struct extent_info ei;
582
583 if (le32_to_cpu(i_ext->len) < F2FS_MIN_EXTENT_LEN)
584 return;
585
586 et = __grab_extent_tree(inode);
587
588 write_lock(&et->lock);
589 if (et->count)
590 goto out;
591
592 set_extent_info(&ei, le32_to_cpu(i_ext->fofs),
593 le32_to_cpu(i_ext->blk), le32_to_cpu(i_ext->len));
594
595 en = __insert_extent_tree(sbi, et, &ei, NULL);
596 if (en) {
597 et->cached_en = en;
598
599 spin_lock(&sbi->extent_lock);
600 list_add_tail(&en->list, &sbi->extent_list);
601 spin_unlock(&sbi->extent_lock);
602 }
603out:
604 write_unlock(&et->lock);
605 atomic_dec(&et->refcount);
606}
607
Chao Yu429511c2015-02-05 17:54:31 +0800608static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
609 struct extent_info *ei)
610{
611 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
612 struct extent_tree *et;
613 struct extent_node *en;
614
Chao Yu1ec46102015-02-05 17:59:59 +0800615 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
616
Chao Yu93dfc522015-03-19 19:24:59 +0800617 et = __find_extent_tree(sbi, inode->i_ino);
618 if (!et)
Chao Yu429511c2015-02-05 17:54:31 +0800619 return false;
Chao Yu429511c2015-02-05 17:54:31 +0800620
621 read_lock(&et->lock);
622 en = __lookup_extent_tree(et, pgofs);
623 if (en) {
624 *ei = en->ei;
625 spin_lock(&sbi->extent_lock);
626 if (!list_empty(&en->list))
627 list_move_tail(&en->list, &sbi->extent_list);
628 spin_unlock(&sbi->extent_lock);
629 stat_inc_read_hit(sbi->sb);
630 }
631 stat_inc_total_hit(sbi->sb);
632 read_unlock(&et->lock);
633
Chao Yu1ec46102015-02-05 17:59:59 +0800634 trace_f2fs_lookup_extent_tree_end(inode, pgofs, en);
635
Chao Yu429511c2015-02-05 17:54:31 +0800636 atomic_dec(&et->refcount);
637 return en ? true : false;
638}
639
640static void f2fs_update_extent_tree(struct inode *inode, pgoff_t fofs,
641 block_t blkaddr)
642{
643 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu429511c2015-02-05 17:54:31 +0800644 struct extent_tree *et;
645 struct extent_node *en = NULL, *en1 = NULL, *en2 = NULL, *en3 = NULL;
646 struct extent_node *den = NULL;
647 struct extent_info ei, dei;
648 unsigned int endofs;
649
Chao Yu1ec46102015-02-05 17:59:59 +0800650 trace_f2fs_update_extent_tree(inode, fofs, blkaddr);
651
Chao Yu93dfc522015-03-19 19:24:59 +0800652 et = __grab_extent_tree(inode);
Chao Yu429511c2015-02-05 17:54:31 +0800653
654 write_lock(&et->lock);
655
656 /* 1. lookup and remove existing extent info in cache */
657 en = __lookup_extent_tree(et, fofs);
658 if (!en)
659 goto update_extent;
660
661 dei = en->ei;
662 __detach_extent_node(sbi, et, en);
663
664 /* 2. if extent can be split more, split and insert the left part */
665 if (dei.len > 1) {
666 /* insert left part of split extent into cache */
667 if (fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
668 set_extent_info(&ei, dei.fofs, dei.blk,
669 fofs - dei.fofs);
670 en1 = __insert_extent_tree(sbi, et, &ei, NULL);
671 }
672
673 /* insert right part of split extent into cache */
674 endofs = dei.fofs + dei.len - 1;
675 if (endofs - fofs >= F2FS_MIN_EXTENT_LEN) {
676 set_extent_info(&ei, fofs + 1,
677 fofs - dei.fofs + dei.blk, endofs - fofs);
678 en2 = __insert_extent_tree(sbi, et, &ei, NULL);
679 }
680 }
681
682update_extent:
683 /* 3. update extent in extent cache */
684 if (blkaddr) {
685 set_extent_info(&ei, fofs, blkaddr, 1);
686 en3 = __insert_extent_tree(sbi, et, &ei, &den);
687 }
688
689 /* 4. update in global extent list */
690 spin_lock(&sbi->extent_lock);
691 if (en && !list_empty(&en->list))
692 list_del(&en->list);
693 /*
694 * en1 and en2 split from en, they will become more and more smaller
695 * fragments after splitting several times. So if the length is smaller
696 * than F2FS_MIN_EXTENT_LEN, we will not add them into extent tree.
697 */
698 if (en1)
699 list_add_tail(&en1->list, &sbi->extent_list);
700 if (en2)
701 list_add_tail(&en2->list, &sbi->extent_list);
702 if (en3) {
703 if (list_empty(&en3->list))
704 list_add_tail(&en3->list, &sbi->extent_list);
705 else
706 list_move_tail(&en3->list, &sbi->extent_list);
707 }
708 if (den && !list_empty(&den->list))
709 list_del(&den->list);
710 spin_unlock(&sbi->extent_lock);
711
712 /* 5. release extent node */
713 if (en)
714 kmem_cache_free(extent_node_slab, en);
715 if (den)
716 kmem_cache_free(extent_node_slab, den);
717
718 write_unlock(&et->lock);
719 atomic_dec(&et->refcount);
720}
721
Chao Yu0bdee482015-03-19 19:27:51 +0800722void f2fs_preserve_extent_tree(struct inode *inode)
723{
724 struct extent_tree *et;
725 struct extent_info *ext = &F2FS_I(inode)->ext;
726 bool sync = false;
727
728 if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
729 return;
730
731 et = __find_extent_tree(F2FS_I_SB(inode), inode->i_ino);
732 if (!et) {
733 if (ext->len) {
734 ext->len = 0;
735 update_inode_page(inode);
736 }
737 return;
738 }
739
740 read_lock(&et->lock);
741 if (et->count) {
742 struct extent_node *en;
743
744 if (et->cached_en) {
745 en = et->cached_en;
746 } else {
747 struct rb_node *node = rb_first(&et->root);
748
749 if (!node)
750 node = rb_last(&et->root);
751 en = rb_entry(node, struct extent_node, rb_node);
752 }
753
754 if (__is_extent_same(ext, &en->ei))
755 goto out;
756
757 *ext = en->ei;
758 sync = true;
759 } else if (ext->len) {
760 ext->len = 0;
761 sync = true;
762 }
763out:
764 read_unlock(&et->lock);
765 atomic_dec(&et->refcount);
766
767 if (sync)
768 update_inode_page(inode);
769}
770
Chao Yu429511c2015-02-05 17:54:31 +0800771void f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
772{
773 struct extent_tree *treevec[EXT_TREE_VEC_SIZE];
774 struct extent_node *en, *tmp;
775 unsigned long ino = F2FS_ROOT_INO(sbi);
776 struct radix_tree_iter iter;
777 void **slot;
778 unsigned int found;
Chao Yu1ec46102015-02-05 17:59:59 +0800779 unsigned int node_cnt = 0, tree_cnt = 0;
Chao Yu429511c2015-02-05 17:54:31 +0800780
Chao Yu1dcc3362015-02-05 17:57:31 +0800781 if (!test_opt(sbi, EXTENT_CACHE))
782 return;
783
Chao Yu429511c2015-02-05 17:54:31 +0800784 if (available_free_memory(sbi, EXTENT_CACHE))
785 return;
786
787 spin_lock(&sbi->extent_lock);
788 list_for_each_entry_safe(en, tmp, &sbi->extent_list, list) {
789 if (!nr_shrink--)
790 break;
791 list_del_init(&en->list);
792 }
793 spin_unlock(&sbi->extent_lock);
794
795 down_read(&sbi->extent_tree_lock);
796 while ((found = radix_tree_gang_lookup(&sbi->extent_tree_root,
797 (void **)treevec, ino, EXT_TREE_VEC_SIZE))) {
798 unsigned i;
799
800 ino = treevec[found - 1]->ino + 1;
801 for (i = 0; i < found; i++) {
802 struct extent_tree *et = treevec[i];
803
804 atomic_inc(&et->refcount);
805 write_lock(&et->lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800806 node_cnt += __free_extent_tree(sbi, et, false);
Chao Yu429511c2015-02-05 17:54:31 +0800807 write_unlock(&et->lock);
808 atomic_dec(&et->refcount);
809 }
810 }
811 up_read(&sbi->extent_tree_lock);
812
813 down_write(&sbi->extent_tree_lock);
814 radix_tree_for_each_slot(slot, &sbi->extent_tree_root, &iter,
815 F2FS_ROOT_INO(sbi)) {
816 struct extent_tree *et = (struct extent_tree *)*slot;
817
818 if (!atomic_read(&et->refcount) && !et->count) {
819 radix_tree_delete(&sbi->extent_tree_root, et->ino);
820 kmem_cache_free(extent_tree_slab, et);
821 sbi->total_ext_tree--;
Chao Yu1ec46102015-02-05 17:59:59 +0800822 tree_cnt++;
Chao Yu429511c2015-02-05 17:54:31 +0800823 }
824 }
825 up_write(&sbi->extent_tree_lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800826
827 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
Chao Yu429511c2015-02-05 17:54:31 +0800828}
829
830void f2fs_destroy_extent_tree(struct inode *inode)
831{
832 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
833 struct extent_tree *et;
Chao Yu1ec46102015-02-05 17:59:59 +0800834 unsigned int node_cnt = 0;
Chao Yu429511c2015-02-05 17:54:31 +0800835
Chao Yu1dcc3362015-02-05 17:57:31 +0800836 if (!test_opt(sbi, EXTENT_CACHE))
837 return;
838
Chao Yu93dfc522015-03-19 19:24:59 +0800839 et = __find_extent_tree(sbi, inode->i_ino);
840 if (!et)
Chao Yu429511c2015-02-05 17:54:31 +0800841 goto out;
Chao Yu429511c2015-02-05 17:54:31 +0800842
843 /* free all extent info belong to this extent tree */
844 write_lock(&et->lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800845 node_cnt = __free_extent_tree(sbi, et, true);
Chao Yu429511c2015-02-05 17:54:31 +0800846 write_unlock(&et->lock);
847
848 atomic_dec(&et->refcount);
849
850 /* try to find and delete extent tree entry in radix tree */
851 down_write(&sbi->extent_tree_lock);
852 et = radix_tree_lookup(&sbi->extent_tree_root, inode->i_ino);
853 if (!et) {
854 up_write(&sbi->extent_tree_lock);
855 goto out;
856 }
857 f2fs_bug_on(sbi, atomic_read(&et->refcount) || et->count);
858 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
859 kmem_cache_free(extent_tree_slab, et);
860 sbi->total_ext_tree--;
861 up_write(&sbi->extent_tree_lock);
862out:
Chao Yu1ec46102015-02-05 17:59:59 +0800863 trace_f2fs_destroy_extent_tree(inode, node_cnt);
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900864 return;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900865}
866
Chao Yu028a41e2015-03-19 19:26:02 +0800867void f2fs_init_extent_cache(struct inode *inode, struct f2fs_extent *i_ext)
868{
869 if (test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
870 f2fs_init_extent_tree(inode, i_ext);
871
872 write_lock(&F2FS_I(inode)->ext_lock);
873 get_extent_info(&F2FS_I(inode)->ext, *i_ext);
874 write_unlock(&F2FS_I(inode)->ext_lock);
875}
876
Chao Yu7e4dde72015-02-05 17:51:34 +0800877static bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
878 struct extent_info *ei)
879{
Chao Yu91c5d9b2015-02-05 18:02:44 +0800880 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
881 return false;
882
Chao Yu1dcc3362015-02-05 17:57:31 +0800883 if (test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
884 return f2fs_lookup_extent_tree(inode, pgofs, ei);
885
Chao Yu7e4dde72015-02-05 17:51:34 +0800886 return lookup_extent_info(inode, pgofs, ei);
887}
888
889void f2fs_update_extent_cache(struct dnode_of_data *dn)
890{
891 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
892 pgoff_t fofs;
893
894 f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR);
895
Chao Yu91c5d9b2015-02-05 18:02:44 +0800896 if (is_inode_flag_set(fi, FI_NO_EXTENT))
897 return;
898
Chao Yu7e4dde72015-02-05 17:51:34 +0800899 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
900 dn->ofs_in_node;
901
Chao Yu1dcc3362015-02-05 17:57:31 +0800902 if (test_opt(F2FS_I_SB(dn->inode), EXTENT_CACHE))
903 return f2fs_update_extent_tree(dn->inode, fofs,
904 dn->data_blkaddr);
905
Chao Yu7e4dde72015-02-05 17:51:34 +0800906 if (update_extent_info(dn->inode, fofs, dn->data_blkaddr))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900907 sync_inode_page(dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900908}
909
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700910struct page *get_read_data_page(struct inode *inode, pgoff_t index, int rw)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900911{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900912 struct address_space *mapping = inode->i_mapping;
913 struct dnode_of_data dn;
914 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800915 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900916 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800917 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700918 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800919 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700920 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700921 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800922 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900923
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700924 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
925 return read_mapping_page(mapping, index, NULL);
926
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900927 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900928 if (!page)
929 return ERR_PTR(-ENOMEM);
930
Chao Yucb3bc9e2015-02-05 18:03:40 +0800931 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
932 dn.data_blkaddr = ei.blk + index - ei.fofs;
933 goto got_it;
934 }
935
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900936 set_new_dnode(&dn, inode, NULL, NULL, 0);
937 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
938 if (err) {
939 f2fs_put_page(page, 1);
940 return ERR_PTR(err);
941 }
942 f2fs_put_dnode(&dn);
943
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900944 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900945 f2fs_put_page(page, 1);
946 return ERR_PTR(-ENOENT);
947 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800948got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700949 if (PageUptodate(page)) {
950 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900951 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700952 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900953
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900954 /*
955 * A new dentry page is allocated but not able to be written, since its
956 * new inode page couldn't be allocated due to -ENOSPC.
957 * In such the case, its blkaddr can be remained as NEW_ADDR.
958 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
959 */
960 if (dn.data_blkaddr == NEW_ADDR) {
961 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
962 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700963 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900964 return page;
965 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900966
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800967 fio.blk_addr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700968 fio.page = page;
969 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900970 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900971 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700972 return page;
973}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900974
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700975struct page *find_data_page(struct inode *inode, pgoff_t index)
976{
977 struct address_space *mapping = inode->i_mapping;
978 struct page *page;
979
980 page = find_get_page(mapping, index);
981 if (page && PageUptodate(page))
982 return page;
983 f2fs_put_page(page, 0);
984
985 page = get_read_data_page(inode, index, READ_SYNC);
986 if (IS_ERR(page))
987 return page;
988
989 if (PageUptodate(page))
990 return page;
991
992 wait_on_page_locked(page);
993 if (unlikely(!PageUptodate(page))) {
994 f2fs_put_page(page, 0);
995 return ERR_PTR(-EIO);
996 }
997 return page;
998}
999
1000/*
1001 * If it tries to access a hole, return an error.
1002 * Because, the callers, functions in dir.c and GC, should be able to know
1003 * whether this page exists or not.
1004 */
1005struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
1006{
1007 struct address_space *mapping = inode->i_mapping;
1008 struct page *page;
1009repeat:
1010 page = get_read_data_page(inode, index, READ_SYNC);
1011 if (IS_ERR(page))
1012 return page;
1013
1014 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001015 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001016 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001017 f2fs_put_page(page, 1);
1018 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001019 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001020 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001021 f2fs_put_page(page, 1);
1022 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001023 }
1024 return page;
1025}
1026
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001027/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001028 * Caller ensures that this data page is never allocated.
1029 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +09001030 *
Chao Yu4f4124d2013-12-21 18:02:14 +08001031 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1032 * f2fs_unlock_op().
Jaegeuk Kima8865372013-12-27 17:04:17 +09001033 * Note that, ipage is set only by make_empty_dir.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001034 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +09001035struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +09001036 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001037{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001038 struct address_space *mapping = inode->i_mapping;
1039 struct page *page;
1040 struct dnode_of_data dn;
1041 int err;
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001042repeat:
1043 page = grab_cache_page(mapping, index);
1044 if (!page)
1045 return ERR_PTR(-ENOMEM);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001046
Jaegeuk Kima8865372013-12-27 17:04:17 +09001047 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +08001048 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001049 if (err) {
1050 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001051 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001052 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001053 if (!ipage)
1054 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001055
1056 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001057 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001058
1059 if (dn.data_blkaddr == NEW_ADDR) {
1060 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001061 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001062 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001063 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001064
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001065 page = get_read_data_page(inode, index, READ_SYNC);
1066 if (IS_ERR(page))
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001067 goto repeat;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001068
1069 /* wait for read completion */
1070 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001071 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -07001072got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001073 if (new_i_size &&
1074 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
1075 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +09001076 /* Only the directory inode sets new_i_size */
1077 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001078 }
1079 return page;
1080}
1081
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001082static int __allocate_data_block(struct dnode_of_data *dn)
1083{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001084 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001085 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001086 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001087 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001088 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001089 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001090
1091 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
1092 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +08001093
1094 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
1095 if (dn->data_blkaddr == NEW_ADDR)
1096 goto alloc;
1097
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001098 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
1099 return -ENOSPC;
1100
Chao Yudf6136e2015-03-23 10:33:37 +08001101alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001102 get_node_info(sbi, dn->nid, &ni);
1103 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1104
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001105 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
1106 seg = CURSEG_DIRECT_IO;
1107
Chao Yudf6136e2015-03-23 10:33:37 +08001108 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
1109 &sum, seg);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001110
1111 /* direct IO doesn't use extent cache to maximize the performance */
Chao Yu216a6202015-03-19 19:23:32 +08001112 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001113
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001114 /* update i_size */
1115 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
1116 dn->ofs_in_node;
1117 if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT))
1118 i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT));
1119
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001120 return 0;
1121}
1122
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001123static void __allocate_data_blocks(struct inode *inode, loff_t offset,
1124 size_t count)
1125{
1126 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1127 struct dnode_of_data dn;
1128 u64 start = F2FS_BYTES_TO_BLK(offset);
1129 u64 len = F2FS_BYTES_TO_BLK(count);
1130 bool allocated;
1131 u64 end_offset;
1132
1133 while (len) {
1134 f2fs_balance_fs(sbi);
1135 f2fs_lock_op(sbi);
1136
1137 /* When reading holes, we need its node page */
1138 set_new_dnode(&dn, inode, NULL, NULL, 0);
1139 if (get_dnode_of_data(&dn, start, ALLOC_NODE))
1140 goto out;
1141
1142 allocated = false;
1143 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
1144
1145 while (dn.ofs_in_node < end_offset && len) {
Chao Yud6d4f1c2015-03-12 17:04:24 +08001146 block_t blkaddr;
1147
1148 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +08001149 if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR) {
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001150 if (__allocate_data_block(&dn))
1151 goto sync_out;
1152 allocated = true;
1153 }
1154 len--;
1155 start++;
1156 dn.ofs_in_node++;
1157 }
1158
1159 if (allocated)
1160 sync_inode_page(&dn);
1161
1162 f2fs_put_dnode(&dn);
1163 f2fs_unlock_op(sbi);
1164 }
1165 return;
1166
1167sync_out:
1168 if (allocated)
1169 sync_inode_page(&dn);
1170 f2fs_put_dnode(&dn);
1171out:
1172 f2fs_unlock_op(sbi);
1173 return;
1174}
1175
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001176/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001177 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1178 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +08001179 * If original data blocks are allocated, then give them to blockdev.
1180 * Otherwise,
1181 * a. preallocate requested block addresses
1182 * b. do not use extent cache for better performance
1183 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001184 */
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001185static int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
1186 int create, bool fiemap)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001187{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001188 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001189 struct dnode_of_data dn;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001190 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
1191 pgoff_t pgofs, end_offset;
1192 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001193 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001194 bool allocated = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001195
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001196 map->m_len = 0;
1197 map->m_flags = 0;
1198
1199 /* it only supports block size == page size */
1200 pgofs = (pgoff_t)map->m_lblk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001201
Chao Yu7e4dde72015-02-05 17:51:34 +08001202 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001203 map->m_pblk = ei.blk + pgofs - ei.fofs;
1204 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1205 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001206 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001207 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001208
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001209 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001210 f2fs_lock_op(F2FS_I_SB(inode));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001211
1212 /* When reading holes, we need its node page */
1213 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001214 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001215 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001216 if (err == -ENOENT)
1217 err = 0;
1218 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001219 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001220 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001221 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001222
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001223 if (dn.data_blkaddr != NULL_ADDR) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001224 map->m_flags = F2FS_MAP_MAPPED;
1225 map->m_pblk = dn.data_blkaddr;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001226 if (dn.data_blkaddr == NEW_ADDR)
1227 map->m_flags |= F2FS_MAP_UNWRITTEN;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001228 } else if (create) {
1229 err = __allocate_data_block(&dn);
1230 if (err)
1231 goto put_out;
1232 allocated = true;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001233 map->m_flags = F2FS_MAP_NEW | F2FS_MAP_MAPPED;
1234 map->m_pblk = dn.data_blkaddr;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001235 } else {
1236 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001237 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001238
Chao Yu6403eb12014-04-26 19:59:52 +08001239 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001240 map->m_len = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001241 dn.ofs_in_node++;
1242 pgofs++;
1243
1244get_next:
1245 if (dn.ofs_in_node >= end_offset) {
1246 if (allocated)
1247 sync_inode_page(&dn);
1248 allocated = false;
1249 f2fs_put_dnode(&dn);
1250
1251 set_new_dnode(&dn, inode, NULL, NULL, 0);
1252 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001253 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001254 if (err == -ENOENT)
1255 err = 0;
1256 goto unlock_out;
1257 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001258 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001259 goto put_out;
1260
Chao Yu6403eb12014-04-26 19:59:52 +08001261 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001262 }
1263
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001264 if (maxblocks > map->m_len) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001265 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
1266 if (blkaddr == NULL_ADDR && create) {
1267 err = __allocate_data_block(&dn);
1268 if (err)
1269 goto sync_out;
1270 allocated = true;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001271 map->m_flags |= F2FS_MAP_NEW;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001272 blkaddr = dn.data_blkaddr;
1273 }
arter97e1c42042014-08-06 23:22:50 +09001274 /* Give more consecutive addresses for the readahead */
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001275 if ((map->m_pblk != NEW_ADDR &&
1276 blkaddr == (map->m_pblk + ofs)) ||
1277 (map->m_pblk == NEW_ADDR &&
1278 blkaddr == NEW_ADDR)) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001279 ofs++;
1280 dn.ofs_in_node++;
1281 pgofs++;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001282 map->m_len++;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001283 goto get_next;
1284 }
1285 }
1286sync_out:
1287 if (allocated)
1288 sync_inode_page(&dn);
1289put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001290 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001291unlock_out:
1292 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001293 f2fs_unlock_op(F2FS_I_SB(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001294out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001295 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001296 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001297}
1298
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001299static int __get_data_block(struct inode *inode, sector_t iblock,
1300 struct buffer_head *bh, int create, bool fiemap)
1301{
1302 struct f2fs_map_blocks map;
1303 int ret;
1304
1305 map.m_lblk = iblock;
1306 map.m_len = bh->b_size >> inode->i_blkbits;
1307
1308 ret = f2fs_map_blocks(inode, &map, create, fiemap);
1309 if (!ret) {
1310 map_bh(bh, inode->i_sb, map.m_pblk);
1311 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1312 bh->b_size = map.m_len << inode->i_blkbits;
1313 }
1314 return ret;
1315}
1316
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001317static int get_data_block(struct inode *inode, sector_t iblock,
1318 struct buffer_head *bh_result, int create)
1319{
1320 return __get_data_block(inode, iblock, bh_result, create, false);
1321}
1322
1323static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
1324 struct buffer_head *bh_result, int create)
1325{
1326 return __get_data_block(inode, iblock, bh_result, create, true);
1327}
1328
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001329static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1330{
1331 return (offset >> inode->i_blkbits);
1332}
1333
1334static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1335{
1336 return (blk << inode->i_blkbits);
1337}
1338
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001339int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1340 u64 start, u64 len)
1341{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001342 struct buffer_head map_bh;
1343 sector_t start_blk, last_blk;
1344 loff_t isize = i_size_read(inode);
1345 u64 logical = 0, phys = 0, size = 0;
1346 u32 flags = 0;
1347 bool past_eof = false, whole_file = false;
1348 int ret = 0;
1349
1350 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1351 if (ret)
1352 return ret;
1353
1354 mutex_lock(&inode->i_mutex);
1355
1356 if (len >= isize) {
1357 whole_file = true;
1358 len = isize;
1359 }
1360
1361 if (logical_to_blk(inode, len) == 0)
1362 len = blk_to_logical(inode, 1);
1363
1364 start_blk = logical_to_blk(inode, start);
1365 last_blk = logical_to_blk(inode, start + len - 1);
1366next:
1367 memset(&map_bh, 0, sizeof(struct buffer_head));
1368 map_bh.b_size = len;
1369
1370 ret = get_data_block_fiemap(inode, start_blk, &map_bh, 0);
1371 if (ret)
1372 goto out;
1373
1374 /* HOLE */
1375 if (!buffer_mapped(&map_bh)) {
1376 start_blk++;
1377
1378 if (!past_eof && blk_to_logical(inode, start_blk) >= isize)
1379 past_eof = 1;
1380
1381 if (past_eof && size) {
1382 flags |= FIEMAP_EXTENT_LAST;
1383 ret = fiemap_fill_next_extent(fieinfo, logical,
1384 phys, size, flags);
1385 } else if (size) {
1386 ret = fiemap_fill_next_extent(fieinfo, logical,
1387 phys, size, flags);
1388 size = 0;
1389 }
1390
1391 /* if we have holes up to/past EOF then we're done */
1392 if (start_blk > last_blk || past_eof || ret)
1393 goto out;
1394 } else {
1395 if (start_blk > last_blk && !whole_file) {
1396 ret = fiemap_fill_next_extent(fieinfo, logical,
1397 phys, size, flags);
1398 goto out;
1399 }
1400
1401 /*
1402 * if size != 0 then we know we already have an extent
1403 * to add, so add it.
1404 */
1405 if (size) {
1406 ret = fiemap_fill_next_extent(fieinfo, logical,
1407 phys, size, flags);
1408 if (ret)
1409 goto out;
1410 }
1411
1412 logical = blk_to_logical(inode, start_blk);
1413 phys = blk_to_logical(inode, map_bh.b_blocknr);
1414 size = map_bh.b_size;
1415 flags = 0;
1416 if (buffer_unwritten(&map_bh))
1417 flags = FIEMAP_EXTENT_UNWRITTEN;
1418
1419 start_blk += logical_to_blk(inode, size);
1420
1421 /*
1422 * If we are past the EOF, then we need to make sure as
1423 * soon as we find a hole that the last extent we found
1424 * is marked with FIEMAP_EXTENT_LAST
1425 */
1426 if (!past_eof && logical + size >= isize)
1427 past_eof = true;
1428 }
1429 cond_resched();
1430 if (fatal_signal_pending(current))
1431 ret = -EINTR;
1432 else
1433 goto next;
1434out:
1435 if (ret == 1)
1436 ret = 0;
1437
1438 mutex_unlock(&inode->i_mutex);
1439 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001440}
1441
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001442/*
1443 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1444 * Major change was from block_size == page_size in f2fs by default.
1445 */
1446static int f2fs_mpage_readpages(struct address_space *mapping,
1447 struct list_head *pages, struct page *page,
1448 unsigned nr_pages)
1449{
1450 struct bio *bio = NULL;
1451 unsigned page_idx;
1452 sector_t last_block_in_bio = 0;
1453 struct inode *inode = mapping->host;
1454 const unsigned blkbits = inode->i_blkbits;
1455 const unsigned blocksize = 1 << blkbits;
1456 sector_t block_in_file;
1457 sector_t last_block;
1458 sector_t last_block_in_file;
1459 sector_t block_nr;
1460 struct block_device *bdev = inode->i_sb->s_bdev;
1461 struct f2fs_map_blocks map;
1462
1463 map.m_pblk = 0;
1464 map.m_lblk = 0;
1465 map.m_len = 0;
1466 map.m_flags = 0;
1467
1468 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1469
1470 prefetchw(&page->flags);
1471 if (pages) {
1472 page = list_entry(pages->prev, struct page, lru);
1473 list_del(&page->lru);
1474 if (add_to_page_cache_lru(page, mapping,
1475 page->index, GFP_KERNEL))
1476 goto next_page;
1477 }
1478
1479 block_in_file = (sector_t)page->index;
1480 last_block = block_in_file + nr_pages;
1481 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1482 blkbits;
1483 if (last_block > last_block_in_file)
1484 last_block = last_block_in_file;
1485
1486 /*
1487 * Map blocks using the previous result first.
1488 */
1489 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1490 block_in_file > map.m_lblk &&
1491 block_in_file < (map.m_lblk + map.m_len))
1492 goto got_it;
1493
1494 /*
1495 * Then do more f2fs_map_blocks() calls until we are
1496 * done with this page.
1497 */
1498 map.m_flags = 0;
1499
1500 if (block_in_file < last_block) {
1501 map.m_lblk = block_in_file;
1502 map.m_len = last_block - block_in_file;
1503
1504 if (f2fs_map_blocks(inode, &map, 0, false))
1505 goto set_error_page;
1506 }
1507got_it:
1508 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1509 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1510 SetPageMappedToDisk(page);
1511
1512 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1513 SetPageUptodate(page);
1514 goto confused;
1515 }
1516 } else {
1517 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1518 SetPageUptodate(page);
1519 unlock_page(page);
1520 goto next_page;
1521 }
1522
1523 /*
1524 * This page will go to BIO. Do we need to send this
1525 * BIO off first?
1526 */
1527 if (bio && (last_block_in_bio != block_nr - 1)) {
1528submit_and_realloc:
1529 submit_bio(READ, bio);
1530 bio = NULL;
1531 }
1532 if (bio == NULL) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001533 struct f2fs_crypto_ctx *ctx = NULL;
1534
1535 if (f2fs_encrypted_inode(inode) &&
1536 S_ISREG(inode->i_mode)) {
1537 struct page *cpage;
1538
1539 ctx = f2fs_get_crypto_ctx(inode);
1540 if (IS_ERR(ctx))
1541 goto set_error_page;
1542
1543 /* wait the page to be moved by cleaning */
1544 cpage = find_lock_page(
1545 META_MAPPING(F2FS_I_SB(inode)),
1546 block_nr);
1547 if (cpage) {
1548 f2fs_wait_on_page_writeback(cpage,
1549 DATA);
1550 f2fs_put_page(cpage, 1);
1551 }
1552 }
1553
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001554 bio = bio_alloc(GFP_KERNEL,
1555 min_t(int, nr_pages, bio_get_nr_vecs(bdev)));
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001556 if (!bio) {
1557 if (ctx)
1558 f2fs_release_crypto_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001559 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001560 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001561 bio->bi_bdev = bdev;
1562 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +08001563 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001564 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001565 }
1566
1567 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1568 goto submit_and_realloc;
1569
1570 last_block_in_bio = block_nr;
1571 goto next_page;
1572set_error_page:
1573 SetPageError(page);
1574 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1575 unlock_page(page);
1576 goto next_page;
1577confused:
1578 if (bio) {
1579 submit_bio(READ, bio);
1580 bio = NULL;
1581 }
1582 unlock_page(page);
1583next_page:
1584 if (pages)
1585 page_cache_release(page);
1586 }
1587 BUG_ON(pages && !list_empty(pages));
1588 if (bio)
1589 submit_bio(READ, bio);
1590 return 0;
1591}
1592
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001593static int f2fs_read_data_page(struct file *file, struct page *page)
1594{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001595 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001596 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001597
Chao Yuc20e89c2014-05-06 16:53:08 +08001598 trace_f2fs_readpage(page, DATA);
1599
arter97e1c42042014-08-06 23:22:50 +09001600 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001601 if (f2fs_has_inline_data(inode))
1602 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001603 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001604 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001605 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001606}
1607
1608static int f2fs_read_data_pages(struct file *file,
1609 struct address_space *mapping,
1610 struct list_head *pages, unsigned nr_pages)
1611{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001612 struct inode *inode = file->f_mapping->host;
1613
1614 /* If the file has inline data, skip readpages */
1615 if (f2fs_has_inline_data(inode))
1616 return 0;
1617
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001618 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001619}
1620
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001621int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001622{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001623 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001624 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001625 struct dnode_of_data dn;
1626 int err = 0;
1627
1628 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001629 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001630 if (err)
1631 return err;
1632
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001633 fio->blk_addr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001634
1635 /* This page is already truncated */
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001636 if (fio->blk_addr == NULL_ADDR) {
1637 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001638 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001639 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001640
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001641 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1642 fio->encrypted_page = f2fs_encrypt(inode, fio->page);
1643 if (IS_ERR(fio->encrypted_page)) {
1644 err = PTR_ERR(fio->encrypted_page);
1645 goto out_writepage;
1646 }
1647 }
1648
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001649 set_page_writeback(page);
1650
1651 /*
1652 * If current allocation needs SSR,
1653 * it had better in-place writes for updated data.
1654 */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001655 if (unlikely(fio->blk_addr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001656 !is_cold_data(page) &&
1657 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001658 rewrite_data_page(fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001659 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001660 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001661 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001662 write_data_page(&dn, fio);
Chao Yu216a6202015-03-19 19:23:32 +08001663 set_data_blkaddr(&dn);
Chao Yu7e4dde72015-02-05 17:51:34 +08001664 f2fs_update_extent_cache(&dn);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001665 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001666 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001667 if (page->index == 0)
1668 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001669 }
1670out_writepage:
1671 f2fs_put_dnode(&dn);
1672 return err;
1673}
1674
1675static int f2fs_write_data_page(struct page *page,
1676 struct writeback_control *wbc)
1677{
1678 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001679 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001680 loff_t i_size = i_size_read(inode);
1681 const pgoff_t end_index = ((unsigned long long) i_size)
1682 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001683 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001684 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001685 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001686 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001687 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001688 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001689 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001690 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001691 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001692 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001693
Chao Yuecda0de2014-05-06 16:48:26 +08001694 trace_f2fs_writepage(page, DATA);
1695
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001696 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001697 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001698
1699 /*
1700 * If the offset is out-of-range of file size,
1701 * this page does not have to be written to disk.
1702 */
1703 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001704 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001705 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001706
1707 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001708write:
Chao Yucaf00472015-01-28 17:48:42 +08001709 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001710 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001711 if (f2fs_is_drop_cache(inode))
1712 goto out;
1713 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1714 available_free_memory(sbi, BASE_CHECK))
1715 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001716
Jaegeuk Kim39936832012-11-22 16:21:29 +09001717 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001718 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001719 if (unlikely(f2fs_cp_error(sbi)))
1720 goto redirty_out;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001721 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001722 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001723 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001724
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001725 /* we should bypass data pages to proceed the kworkder jobs */
1726 if (unlikely(f2fs_cp_error(sbi))) {
1727 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001728 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001729 }
1730
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001731 if (!wbc->for_reclaim)
1732 need_balance_fs = true;
1733 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001734 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001735
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001736 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001737 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001738 if (f2fs_has_inline_data(inode))
1739 err = f2fs_write_inline_data(inode, page);
1740 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001741 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001742 f2fs_unlock_op(sbi);
1743done:
1744 if (err && err != -ENOENT)
1745 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001746
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001747 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001748out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001749 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001750 if (err)
1751 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001752 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001753 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001754 f2fs_balance_fs(sbi);
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +09001755 if (wbc->for_reclaim)
1756 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001757 return 0;
1758
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001759redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001760 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001761 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001762}
1763
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001764static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1765 void *data)
1766{
1767 struct address_space *mapping = data;
1768 int ret = mapping->a_ops->writepage(page, wbc);
1769 mapping_set_error(mapping, ret);
1770 return ret;
1771}
1772
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001773static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001774 struct writeback_control *wbc)
1775{
1776 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001777 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001778 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001779 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001780 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001781
Chao Yue5748432014-05-06 16:51:24 +08001782 trace_f2fs_writepages(mapping->host, wbc, DATA);
1783
P J Pcfb185a2013-04-03 11:38:00 +09001784 /* deal with chardevs and other special file */
1785 if (!mapping->a_ops->writepage)
1786 return 0;
1787
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001788 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001789 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001790 available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001791 goto skip_write;
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001792
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001793 /* during POR, we don't need to trigger writepage at all. */
1794 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1795 goto skip_write;
1796
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001797 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001798
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001799 if (!S_ISDIR(inode->i_mode)) {
1800 mutex_lock(&sbi->writepages);
1801 locked = true;
1802 }
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001803 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001804 if (locked)
1805 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001806
1807 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001808
1809 remove_dirty_dir_inode(inode);
1810
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001811 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001812 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001813
1814skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001815 wbc->pages_skipped += get_dirty_pages(inode);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001816 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001817}
1818
Chao Yu3aab8f82014-07-02 13:25:04 +08001819static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1820{
1821 struct inode *inode = mapping->host;
1822
1823 if (to > inode->i_size) {
1824 truncate_pagecache(inode, inode->i_size);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -07001825 truncate_blocks(inode, inode->i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001826 }
1827}
1828
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001829static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1830 loff_t pos, unsigned len, unsigned flags,
1831 struct page **pagep, void **fsdata)
1832{
1833 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001834 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001835 struct page *page, *ipage;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001836 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
1837 struct dnode_of_data dn;
1838 int err = 0;
1839
Chao Yu62aed042014-05-06 16:46:04 +08001840 trace_f2fs_write_begin(inode, pos, len, flags);
1841
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001842 f2fs_balance_fs(sbi);
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001843
1844 /*
1845 * We should check this at this moment to avoid deadlock on inode page
1846 * and #0 page. The locking rule for inline_data conversion should be:
1847 * lock_page(page #0) -> lock_page(inode_page)
1848 */
1849 if (index != 0) {
1850 err = f2fs_convert_inline_inode(inode);
1851 if (err)
1852 goto fail;
1853 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001854repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001855 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001856 if (!page) {
1857 err = -ENOMEM;
1858 goto fail;
1859 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001860
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001861 *pagep = page;
1862
Gu Zhenge4795562013-09-27 18:08:30 +08001863 f2fs_lock_op(sbi);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001864
1865 /* check inline_data */
1866 ipage = get_node_page(sbi, inode->i_ino);
Chao Yucd34e292014-12-01 11:30:20 +08001867 if (IS_ERR(ipage)) {
1868 err = PTR_ERR(ipage);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001869 goto unlock_fail;
Chao Yucd34e292014-12-01 11:30:20 +08001870 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001871
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001872 set_new_dnode(&dn, inode, ipage, ipage, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001873
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001874 if (f2fs_has_inline_data(inode)) {
1875 if (pos + len <= MAX_INLINE_DATA) {
1876 read_inline_data(page, ipage);
1877 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1878 sync_inode_page(&dn);
1879 goto put_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001880 }
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001881 err = f2fs_convert_inline_page(&dn, page);
1882 if (err)
1883 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001884 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001885 err = f2fs_reserve_block(&dn, index);
1886 if (err)
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001887 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001888put_next:
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001889 f2fs_put_dnode(&dn);
1890 f2fs_unlock_op(sbi);
1891
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001892 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
1893 return 0;
1894
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001895 f2fs_wait_on_page_writeback(page, DATA);
1896
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001897 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1898 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1899 unsigned end = start + len;
1900
1901 /* Reading beyond i_size is simple: memset to zero */
1902 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001903 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001904 }
1905
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001906 if (dn.data_blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001907 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1908 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001909 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001910 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001911 .type = DATA,
1912 .rw = READ_SYNC,
1913 .blk_addr = dn.data_blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001914 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001915 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001916 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001917 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001918 if (err)
1919 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001920
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001921 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001922 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001923 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001924 err = -EIO;
1925 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001926 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001927 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001928 f2fs_put_page(page, 1);
1929 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001930 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001931
1932 /* avoid symlink page */
1933 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1934 err = f2fs_decrypt_one(inode, page);
1935 if (err) {
1936 f2fs_put_page(page, 1);
1937 goto fail;
1938 }
1939 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001940 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001941out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001942 SetPageUptodate(page);
1943 clear_cold_data(page);
1944 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001945
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001946put_fail:
1947 f2fs_put_dnode(&dn);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001948unlock_fail:
1949 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001950 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001951fail:
1952 f2fs_write_failed(mapping, pos + len);
1953 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001954}
1955
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001956static int f2fs_write_end(struct file *file,
1957 struct address_space *mapping,
1958 loff_t pos, unsigned len, unsigned copied,
1959 struct page *page, void *fsdata)
1960{
1961 struct inode *inode = page->mapping->host;
1962
Chao Yudfb2bf32014-05-06 16:47:23 +08001963 trace_f2fs_write_end(inode, pos, len, copied);
1964
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001965 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001966
1967 if (pos + copied > i_size_read(inode)) {
1968 i_size_write(inode, pos + copied);
1969 mark_inode_dirty(inode);
1970 update_inode_page(inode);
1971 }
1972
Chao Yu75c3c8b2013-11-16 14:15:59 +08001973 f2fs_put_page(page, 1);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001974 return copied;
1975}
1976
Omar Sandoval6f673762015-03-16 04:33:52 -07001977static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1978 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001979{
1980 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001981
Omar Sandoval6f673762015-03-16 04:33:52 -07001982 if (iov_iter_rw(iter) == READ)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001983 return 0;
1984
1985 if (offset & blocksize_mask)
1986 return -EINVAL;
1987
Al Viro5b46f252014-03-16 18:07:34 -04001988 if (iov_iter_alignment(iter) & blocksize_mask)
1989 return -EINVAL;
1990
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001991 return 0;
1992}
1993
Omar Sandoval22c61862015-03-16 04:33:53 -07001994static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
1995 loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001996{
1997 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001998 struct address_space *mapping = file->f_mapping;
1999 struct inode *inode = mapping->host;
2000 size_t count = iov_iter_count(iter);
2001 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002002
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002003 /* we don't need to use inline_data strictly */
2004 if (f2fs_has_inline_data(inode)) {
2005 err = f2fs_convert_inline_inode(inode);
2006 if (err)
2007 return err;
2008 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002009
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002010 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
2011 return 0;
2012
Omar Sandoval6f673762015-03-16 04:33:52 -07002013 if (check_direct_IO(inode, iter, offset))
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002014 return 0;
2015
Omar Sandoval6f673762015-03-16 04:33:52 -07002016 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08002017
Omar Sandoval6f673762015-03-16 04:33:52 -07002018 if (iov_iter_rw(iter) == WRITE)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08002019 __allocate_data_blocks(inode, offset, count);
2020
Omar Sandoval17f8c842015-03-16 04:33:50 -07002021 err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block);
Omar Sandoval6f673762015-03-16 04:33:52 -07002022 if (err < 0 && iov_iter_rw(iter) == WRITE)
Chao Yu3aab8f82014-07-02 13:25:04 +08002023 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08002024
Omar Sandoval6f673762015-03-16 04:33:52 -07002025 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08002026
Chao Yu3aab8f82014-07-02 13:25:04 +08002027 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002028}
2029
Chao Yu487261f2015-02-05 17:44:29 +08002030void f2fs_invalidate_page(struct page *page, unsigned int offset,
2031 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002032{
2033 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002034 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002035
Chao Yu487261f2015-02-05 17:44:29 +08002036 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
2037 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002038 return;
2039
Chao Yu487261f2015-02-05 17:44:29 +08002040 if (PageDirty(page)) {
2041 if (inode->i_ino == F2FS_META_INO(sbi))
2042 dec_page_count(sbi, F2FS_DIRTY_META);
2043 else if (inode->i_ino == F2FS_NODE_INO(sbi))
2044 dec_page_count(sbi, F2FS_DIRTY_NODES);
2045 else
2046 inode_dec_dirty_pages(inode);
2047 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002048 ClearPagePrivate(page);
2049}
2050
Chao Yu487261f2015-02-05 17:44:29 +08002051int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002052{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002053 /* If this is dirty page, keep PagePrivate */
2054 if (PageDirty(page))
2055 return 0;
2056
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002057 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002058 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002059}
2060
2061static int f2fs_set_data_page_dirty(struct page *page)
2062{
2063 struct address_space *mapping = page->mapping;
2064 struct inode *inode = mapping->host;
2065
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002066 trace_f2fs_set_page_dirty(page, DATA);
2067
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002068 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002069
Jaegeuk Kim1e843712014-12-09 06:08:59 -08002070 if (f2fs_is_atomic_file(inode)) {
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002071 register_inmem_page(inode, page);
2072 return 1;
2073 }
2074
Jaegeuk Kima18ff062014-01-21 13:32:12 +09002075 mark_inode_dirty(inode);
2076
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002077 if (!PageDirty(page)) {
2078 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002079 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002080 return 1;
2081 }
2082 return 0;
2083}
2084
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002085static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2086{
Chao Yu454ae7e2014-04-22 13:34:01 +08002087 struct inode *inode = mapping->host;
2088
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002089 /* we don't need to use inline_data strictly */
2090 if (f2fs_has_inline_data(inode)) {
2091 int err = f2fs_convert_inline_inode(inode);
2092 if (err)
2093 return err;
2094 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09002095 return generic_block_bmap(mapping, block, get_data_block);
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002096}
2097
Chao Yu429511c2015-02-05 17:54:31 +08002098void init_extent_cache_info(struct f2fs_sb_info *sbi)
2099{
2100 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
2101 init_rwsem(&sbi->extent_tree_lock);
2102 INIT_LIST_HEAD(&sbi->extent_list);
2103 spin_lock_init(&sbi->extent_lock);
2104 sbi->total_ext_tree = 0;
2105 atomic_set(&sbi->total_ext_node, 0);
2106}
2107
2108int __init create_extent_cache(void)
2109{
2110 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
2111 sizeof(struct extent_tree));
2112 if (!extent_tree_slab)
2113 return -ENOMEM;
2114 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
2115 sizeof(struct extent_node));
2116 if (!extent_node_slab) {
2117 kmem_cache_destroy(extent_tree_slab);
2118 return -ENOMEM;
2119 }
2120 return 0;
2121}
2122
2123void destroy_extent_cache(void)
2124{
2125 kmem_cache_destroy(extent_node_slab);
2126 kmem_cache_destroy(extent_tree_slab);
2127}
2128
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002129const struct address_space_operations f2fs_dblock_aops = {
2130 .readpage = f2fs_read_data_page,
2131 .readpages = f2fs_read_data_pages,
2132 .writepage = f2fs_write_data_page,
2133 .writepages = f2fs_write_data_pages,
2134 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002135 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002136 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002137 .invalidatepage = f2fs_invalidate_page,
2138 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002139 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002140 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002141};