blob: b91b0e10678eb1adcb87e45628f7794caa0dcc9b [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 Kimeb47b802012-11-02 17:10:12 +090021
22#include "f2fs.h"
23#include "node.h"
24#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080025#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090026#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090027
Chao Yu429511c2015-02-05 17:54:31 +080028static struct kmem_cache *extent_tree_slab;
29static struct kmem_cache *extent_node_slab;
30
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090031static void f2fs_read_end_io(struct bio *bio, int err)
32{
Linus Torvaldsf5688492014-01-30 11:19:05 -080033 struct bio_vec *bvec;
34 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090035
Linus Torvaldsf5688492014-01-30 11:19:05 -080036 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090037 struct page *page = bvec->bv_page;
38
Linus Torvaldsf5688492014-01-30 11:19:05 -080039 if (!err) {
40 SetPageUptodate(page);
41 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090042 ClearPageUptodate(page);
43 SetPageError(page);
44 }
45 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080046 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090047 bio_put(bio);
48}
49
50static void f2fs_write_end_io(struct bio *bio, int err)
51{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090052 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080053 struct bio_vec *bvec;
54 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090055
Linus Torvaldsf5688492014-01-30 11:19:05 -080056 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090057 struct page *page = bvec->bv_page;
58
Linus Torvaldsf5688492014-01-30 11:19:05 -080059 if (unlikely(err)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -070060 set_page_dirty(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090061 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim744602c2014-01-24 09:42:16 +090062 f2fs_stop_checkpoint(sbi);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090063 }
64 end_page_writeback(page);
65 dec_page_count(sbi, F2FS_WRITEBACK);
Linus Torvaldsf5688492014-01-30 11:19:05 -080066 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090067
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090068 if (!get_pages(sbi, F2FS_WRITEBACK) &&
69 !list_empty(&sbi->cp_wait.task_list))
70 wake_up(&sbi->cp_wait);
71
72 bio_put(bio);
73}
74
Gu Zheng940a6d32013-12-20 17:39:59 +080075/*
76 * Low-level block read/write IO operations.
77 */
78static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
79 int npages, bool is_read)
80{
81 struct bio *bio;
82
83 /* No failure on bio allocation */
84 bio = bio_alloc(GFP_NOIO, npages);
85
86 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +080087 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +080088 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090089 bio->bi_private = sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +080090
91 return bio;
92}
93
Jaegeuk Kim458e6192013-12-11 13:54:01 +090094static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090095{
Jaegeuk Kim458e6192013-12-11 13:54:01 +090096 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090097
98 if (!io->bio)
99 return;
100
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700101 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800102 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700103 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800104 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900105
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700106 submit_bio(fio->rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900107 io->bio = NULL;
108}
109
110void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900111 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900112{
113 enum page_type btype = PAGE_TYPE_OF_BIO(type);
114 struct f2fs_bio_info *io;
115
116 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
117
Chao Yudf0f8dc2014-03-22 14:57:23 +0800118 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900119
120 /* change META to META_FLUSH in the checkpoint procedure */
121 if (type >= META_FLUSH) {
122 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700123 if (test_opt(sbi, NOBARRIER))
124 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
125 else
126 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900127 }
128 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800129 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900130}
131
132/*
133 * Fill the locked page with data located in the block address.
134 * Return unlocked page.
135 */
136int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800137 struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900138{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900139 struct bio *bio;
140
Chao Yu2ace38e2014-12-24 16:08:14 +0800141 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -0800142 f2fs_trace_ios(page, fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900143
144 /* Allocate a new bio */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800145 bio = __bio_alloc(sbi, fio->blk_addr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900146
147 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
148 bio_put(bio);
149 f2fs_put_page(page, 1);
150 return -EFAULT;
151 }
152
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800153 submit_bio(fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900154 return 0;
155}
156
157void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800158 struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900159{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900160 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900161 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800162 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900163
Gu Zheng940a6d32013-12-20 17:39:59 +0800164 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900165
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800166 verify_block_addr(sbi, fio->blk_addr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900167
Chao Yudf0f8dc2014-03-22 14:57:23 +0800168 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900169
Gu Zheng940a6d32013-12-20 17:39:59 +0800170 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900171 inc_page_count(sbi, F2FS_WRITEBACK);
172
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800173 if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900174 io->fio.rw != fio->rw))
175 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900176alloc_new:
177 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700178 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800179
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800180 io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900181 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900182 }
183
184 if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
185 PAGE_CACHE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900186 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900187 goto alloc_new;
188 }
189
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800190 io->last_block_in_bio = fio->blk_addr;
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -0800191 f2fs_trace_ios(page, fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900192
Chao Yudf0f8dc2014-03-22 14:57:23 +0800193 up_write(&io->io_rwsem);
Chao Yu2ace38e2014-12-24 16:08:14 +0800194 trace_f2fs_submit_page_mbio(page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900195}
196
197/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900198 * Lock ordering for the change of data block address:
199 * ->data_page
200 * ->node_page
201 * update block addresses in the node page
202 */
Chao Yu216a6202015-03-19 19:23:32 +0800203void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900204{
205 struct f2fs_node *rn;
206 __le32 *addr_array;
207 struct page *node_page = dn->node_page;
208 unsigned int ofs_in_node = dn->ofs_in_node;
209
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000210 f2fs_wait_on_page_writeback(node_page, NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900211
Gu Zheng45590712013-07-15 17:57:38 +0800212 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900213
214 /* Get physical address of data block */
215 addr_array = blkaddr_in_node(rn);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800216 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900217 set_page_dirty(node_page);
218}
219
220int reserve_new_block(struct dnode_of_data *dn)
221{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700222 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900223
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900224 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900225 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800226 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900227 return -ENOSPC;
228
Namjae Jeonc01e2852013-04-23 17:00:52 +0900229 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
230
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900231 dn->data_blkaddr = NEW_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800232 set_data_blkaddr(dn);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900233 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900234 sync_inode_page(dn);
235 return 0;
236}
237
Huajun Lib6009652013-11-10 23:13:18 +0800238int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
239{
240 bool need_put = dn->inode_page ? false : true;
241 int err;
242
243 err = get_dnode_of_data(dn, index, ALLOC_NODE);
244 if (err)
245 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900246
Huajun Lib6009652013-11-10 23:13:18 +0800247 if (dn->data_blkaddr == NULL_ADDR)
248 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900249 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800250 f2fs_put_dnode(dn);
251 return err;
252}
253
Chao Yua2e7d1b2015-02-05 17:50:30 +0800254static void f2fs_map_bh(struct super_block *sb, pgoff_t pgofs,
255 struct extent_info *ei, struct buffer_head *bh_result)
256{
257 unsigned int blkbits = sb->s_blocksize_bits;
Chao Yu1b3e27a2015-03-24 13:08:05 +0800258 size_t max_size = bh_result->b_size;
259 size_t mapped_size;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800260
Jaegeuk Kim3402e872015-03-11 23:27:25 -0400261 clear_buffer_new(bh_result);
Chao Yua2e7d1b2015-02-05 17:50:30 +0800262 map_bh(bh_result, sb, ei->blk + pgofs - ei->fofs);
Chao Yu1b3e27a2015-03-24 13:08:05 +0800263 mapped_size = (ei->fofs + ei->len - pgofs) << blkbits;
264 bh_result->b_size = min(max_size, mapped_size);
Chao Yua2e7d1b2015-02-05 17:50:30 +0800265}
266
Chao Yu7e4dde72015-02-05 17:51:34 +0800267static bool lookup_extent_info(struct inode *inode, pgoff_t pgofs,
268 struct extent_info *ei)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900269{
270 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900271 pgoff_t start_fofs, end_fofs;
272 block_t start_blkaddr;
273
Chao Yu0c872e22015-02-05 17:46:29 +0800274 read_lock(&fi->ext_lock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900275 if (fi->ext.len == 0) {
Chao Yu0c872e22015-02-05 17:46:29 +0800276 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800277 return false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900278 }
279
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900280 stat_inc_total_hit(inode->i_sb);
281
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900282 start_fofs = fi->ext.fofs;
283 end_fofs = fi->ext.fofs + fi->ext.len - 1;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800284 start_blkaddr = fi->ext.blk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900285
286 if (pgofs >= start_fofs && pgofs <= end_fofs) {
Chao Yua2e7d1b2015-02-05 17:50:30 +0800287 *ei = fi->ext;
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900288 stat_inc_read_hit(inode->i_sb);
Chao Yu0c872e22015-02-05 17:46:29 +0800289 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800290 return true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900291 }
Chao Yu0c872e22015-02-05 17:46:29 +0800292 read_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800293 return false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900294}
295
Chao Yu7e4dde72015-02-05 17:51:34 +0800296static bool update_extent_info(struct inode *inode, pgoff_t fofs,
297 block_t blkaddr)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900298{
Chao Yu7e4dde72015-02-05 17:51:34 +0800299 struct f2fs_inode_info *fi = F2FS_I(inode);
300 pgoff_t start_fofs, end_fofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900301 block_t start_blkaddr, end_blkaddr;
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900302 int need_update = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900303
Chao Yu0c872e22015-02-05 17:46:29 +0800304 write_lock(&fi->ext_lock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900305
306 start_fofs = fi->ext.fofs;
307 end_fofs = fi->ext.fofs + fi->ext.len - 1;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800308 start_blkaddr = fi->ext.blk;
309 end_blkaddr = fi->ext.blk + fi->ext.len - 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900310
311 /* Drop and initialize the matched extent */
312 if (fi->ext.len == 1 && fofs == start_fofs)
313 fi->ext.len = 0;
314
315 /* Initial extent */
316 if (fi->ext.len == 0) {
Chao Yu7e4dde72015-02-05 17:51:34 +0800317 if (blkaddr != NULL_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900318 fi->ext.fofs = fofs;
Chao Yu7e4dde72015-02-05 17:51:34 +0800319 fi->ext.blk = blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900320 fi->ext.len = 1;
321 }
322 goto end_update;
323 }
324
Namjae Jeon6224da82013-04-06 14:44:32 +0900325 /* Front merge */
Chao Yu7e4dde72015-02-05 17:51:34 +0800326 if (fofs == start_fofs - 1 && blkaddr == start_blkaddr - 1) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900327 fi->ext.fofs--;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800328 fi->ext.blk--;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900329 fi->ext.len++;
330 goto end_update;
331 }
332
333 /* Back merge */
Chao Yu7e4dde72015-02-05 17:51:34 +0800334 if (fofs == end_fofs + 1 && blkaddr == end_blkaddr + 1) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900335 fi->ext.len++;
336 goto end_update;
337 }
338
339 /* Split the existing extent */
340 if (fi->ext.len > 1 &&
341 fofs >= start_fofs && fofs <= end_fofs) {
342 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
343 fi->ext.len = fofs - start_fofs;
344 } else {
345 fi->ext.fofs = fofs + 1;
Chao Yu4d0b0bd2015-02-05 17:47:25 +0800346 fi->ext.blk = start_blkaddr + fofs - start_fofs + 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900347 fi->ext.len -= fofs - start_fofs + 1;
348 }
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900349 } else {
350 need_update = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900351 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900352
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900353 /* Finally, if the extent is very fragmented, let's drop the cache. */
354 if (fi->ext.len < F2FS_MIN_EXTENT_LEN) {
355 fi->ext.len = 0;
356 set_inode_flag(fi, FI_NO_EXTENT);
357 need_update = true;
358 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900359end_update:
Chao Yu0c872e22015-02-05 17:46:29 +0800360 write_unlock(&fi->ext_lock);
Chao Yu7e4dde72015-02-05 17:51:34 +0800361 return need_update;
362}
363
Chao Yu429511c2015-02-05 17:54:31 +0800364static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
365 struct extent_tree *et, struct extent_info *ei,
366 struct rb_node *parent, struct rb_node **p)
367{
368 struct extent_node *en;
369
370 en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
371 if (!en)
372 return NULL;
373
374 en->ei = *ei;
375 INIT_LIST_HEAD(&en->list);
376
377 rb_link_node(&en->rb_node, parent, p);
378 rb_insert_color(&en->rb_node, &et->root);
379 et->count++;
380 atomic_inc(&sbi->total_ext_node);
381 return en;
382}
383
384static void __detach_extent_node(struct f2fs_sb_info *sbi,
385 struct extent_tree *et, struct extent_node *en)
386{
387 rb_erase(&en->rb_node, &et->root);
388 et->count--;
389 atomic_dec(&sbi->total_ext_node);
Chao Yu62c8af62015-02-05 18:01:39 +0800390
391 if (et->cached_en == en)
392 et->cached_en = NULL;
Chao Yu429511c2015-02-05 17:54:31 +0800393}
394
Chao Yu93dfc522015-03-19 19:24:59 +0800395static struct extent_tree *__find_extent_tree(struct f2fs_sb_info *sbi,
396 nid_t ino)
397{
398 struct extent_tree *et;
399
400 down_read(&sbi->extent_tree_lock);
401 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
402 if (!et) {
403 up_read(&sbi->extent_tree_lock);
404 return NULL;
405 }
406 atomic_inc(&et->refcount);
407 up_read(&sbi->extent_tree_lock);
408
409 return et;
410}
411
412static struct extent_tree *__grab_extent_tree(struct inode *inode)
413{
414 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
415 struct extent_tree *et;
416 nid_t ino = inode->i_ino;
417
418 down_write(&sbi->extent_tree_lock);
419 et = radix_tree_lookup(&sbi->extent_tree_root, ino);
420 if (!et) {
421 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
422 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
423 memset(et, 0, sizeof(struct extent_tree));
424 et->ino = ino;
425 et->root = RB_ROOT;
426 et->cached_en = NULL;
427 rwlock_init(&et->lock);
428 atomic_set(&et->refcount, 0);
429 et->count = 0;
430 sbi->total_ext_tree++;
431 }
432 atomic_inc(&et->refcount);
433 up_write(&sbi->extent_tree_lock);
434
435 return et;
436}
437
Chao Yu429511c2015-02-05 17:54:31 +0800438static struct extent_node *__lookup_extent_tree(struct extent_tree *et,
439 unsigned int fofs)
440{
441 struct rb_node *node = et->root.rb_node;
442 struct extent_node *en;
443
Chao Yu62c8af62015-02-05 18:01:39 +0800444 if (et->cached_en) {
445 struct extent_info *cei = &et->cached_en->ei;
446
447 if (cei->fofs <= fofs && cei->fofs + cei->len > fofs)
448 return et->cached_en;
449 }
450
Chao Yu429511c2015-02-05 17:54:31 +0800451 while (node) {
452 en = rb_entry(node, struct extent_node, rb_node);
453
Chao Yu62c8af62015-02-05 18:01:39 +0800454 if (fofs < en->ei.fofs) {
Chao Yu429511c2015-02-05 17:54:31 +0800455 node = node->rb_left;
Chao Yu62c8af62015-02-05 18:01:39 +0800456 } else if (fofs >= en->ei.fofs + en->ei.len) {
Chao Yu429511c2015-02-05 17:54:31 +0800457 node = node->rb_right;
Chao Yu62c8af62015-02-05 18:01:39 +0800458 } else {
459 et->cached_en = en;
Chao Yu429511c2015-02-05 17:54:31 +0800460 return en;
Chao Yu62c8af62015-02-05 18:01:39 +0800461 }
Chao Yu429511c2015-02-05 17:54:31 +0800462 }
463 return NULL;
464}
465
466static struct extent_node *__try_back_merge(struct f2fs_sb_info *sbi,
467 struct extent_tree *et, struct extent_node *en)
468{
469 struct extent_node *prev;
470 struct rb_node *node;
471
472 node = rb_prev(&en->rb_node);
473 if (!node)
474 return NULL;
475
476 prev = rb_entry(node, struct extent_node, rb_node);
477 if (__is_back_mergeable(&en->ei, &prev->ei)) {
478 en->ei.fofs = prev->ei.fofs;
479 en->ei.blk = prev->ei.blk;
480 en->ei.len += prev->ei.len;
481 __detach_extent_node(sbi, et, prev);
482 return prev;
483 }
484 return NULL;
485}
486
487static struct extent_node *__try_front_merge(struct f2fs_sb_info *sbi,
488 struct extent_tree *et, struct extent_node *en)
489{
490 struct extent_node *next;
491 struct rb_node *node;
492
493 node = rb_next(&en->rb_node);
494 if (!node)
495 return NULL;
496
497 next = rb_entry(node, struct extent_node, rb_node);
498 if (__is_front_mergeable(&en->ei, &next->ei)) {
499 en->ei.len += next->ei.len;
500 __detach_extent_node(sbi, et, next);
501 return next;
502 }
503 return NULL;
504}
505
506static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
507 struct extent_tree *et, struct extent_info *ei,
508 struct extent_node **den)
509{
510 struct rb_node **p = &et->root.rb_node;
511 struct rb_node *parent = NULL;
512 struct extent_node *en;
513
514 while (*p) {
515 parent = *p;
516 en = rb_entry(parent, struct extent_node, rb_node);
517
518 if (ei->fofs < en->ei.fofs) {
519 if (__is_front_mergeable(ei, &en->ei)) {
520 f2fs_bug_on(sbi, !den);
521 en->ei.fofs = ei->fofs;
522 en->ei.blk = ei->blk;
523 en->ei.len += ei->len;
524 *den = __try_back_merge(sbi, et, en);
525 return en;
526 }
527 p = &(*p)->rb_left;
528 } else if (ei->fofs >= en->ei.fofs + en->ei.len) {
529 if (__is_back_mergeable(ei, &en->ei)) {
530 f2fs_bug_on(sbi, !den);
531 en->ei.len += ei->len;
532 *den = __try_front_merge(sbi, et, en);
533 return en;
534 }
535 p = &(*p)->rb_right;
536 } else {
537 f2fs_bug_on(sbi, 1);
538 }
539 }
540
541 return __attach_extent_node(sbi, et, ei, parent, p);
542}
543
544static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
545 struct extent_tree *et, bool free_all)
546{
547 struct rb_node *node, *next;
548 struct extent_node *en;
549 unsigned int count = et->count;
550
551 node = rb_first(&et->root);
552 while (node) {
553 next = rb_next(node);
554 en = rb_entry(node, struct extent_node, rb_node);
555
556 if (free_all) {
557 spin_lock(&sbi->extent_lock);
558 if (!list_empty(&en->list))
559 list_del_init(&en->list);
560 spin_unlock(&sbi->extent_lock);
561 }
562
563 if (free_all || list_empty(&en->list)) {
564 __detach_extent_node(sbi, et, en);
565 kmem_cache_free(extent_node_slab, en);
566 }
567 node = next;
568 }
569
570 return count - et->count;
571}
572
Chao Yu028a41e2015-03-19 19:26:02 +0800573static void f2fs_init_extent_tree(struct inode *inode,
574 struct f2fs_extent *i_ext)
575{
576 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
577 struct extent_tree *et;
578 struct extent_node *en;
579 struct extent_info ei;
580
581 if (le32_to_cpu(i_ext->len) < F2FS_MIN_EXTENT_LEN)
582 return;
583
584 et = __grab_extent_tree(inode);
585
586 write_lock(&et->lock);
587 if (et->count)
588 goto out;
589
590 set_extent_info(&ei, le32_to_cpu(i_ext->fofs),
591 le32_to_cpu(i_ext->blk), le32_to_cpu(i_ext->len));
592
593 en = __insert_extent_tree(sbi, et, &ei, NULL);
594 if (en) {
595 et->cached_en = en;
596
597 spin_lock(&sbi->extent_lock);
598 list_add_tail(&en->list, &sbi->extent_list);
599 spin_unlock(&sbi->extent_lock);
600 }
601out:
602 write_unlock(&et->lock);
603 atomic_dec(&et->refcount);
604}
605
Chao Yu429511c2015-02-05 17:54:31 +0800606static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
607 struct extent_info *ei)
608{
609 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
610 struct extent_tree *et;
611 struct extent_node *en;
612
Chao Yu1ec46102015-02-05 17:59:59 +0800613 trace_f2fs_lookup_extent_tree_start(inode, pgofs);
614
Chao Yu93dfc522015-03-19 19:24:59 +0800615 et = __find_extent_tree(sbi, inode->i_ino);
616 if (!et)
Chao Yu429511c2015-02-05 17:54:31 +0800617 return false;
Chao Yu429511c2015-02-05 17:54:31 +0800618
619 read_lock(&et->lock);
620 en = __lookup_extent_tree(et, pgofs);
621 if (en) {
622 *ei = en->ei;
623 spin_lock(&sbi->extent_lock);
624 if (!list_empty(&en->list))
625 list_move_tail(&en->list, &sbi->extent_list);
626 spin_unlock(&sbi->extent_lock);
627 stat_inc_read_hit(sbi->sb);
628 }
629 stat_inc_total_hit(sbi->sb);
630 read_unlock(&et->lock);
631
Chao Yu1ec46102015-02-05 17:59:59 +0800632 trace_f2fs_lookup_extent_tree_end(inode, pgofs, en);
633
Chao Yu429511c2015-02-05 17:54:31 +0800634 atomic_dec(&et->refcount);
635 return en ? true : false;
636}
637
638static void f2fs_update_extent_tree(struct inode *inode, pgoff_t fofs,
639 block_t blkaddr)
640{
641 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu429511c2015-02-05 17:54:31 +0800642 struct extent_tree *et;
643 struct extent_node *en = NULL, *en1 = NULL, *en2 = NULL, *en3 = NULL;
644 struct extent_node *den = NULL;
645 struct extent_info ei, dei;
646 unsigned int endofs;
647
Chao Yu1ec46102015-02-05 17:59:59 +0800648 trace_f2fs_update_extent_tree(inode, fofs, blkaddr);
649
Chao Yu93dfc522015-03-19 19:24:59 +0800650 et = __grab_extent_tree(inode);
Chao Yu429511c2015-02-05 17:54:31 +0800651
652 write_lock(&et->lock);
653
654 /* 1. lookup and remove existing extent info in cache */
655 en = __lookup_extent_tree(et, fofs);
656 if (!en)
657 goto update_extent;
658
659 dei = en->ei;
660 __detach_extent_node(sbi, et, en);
661
662 /* 2. if extent can be split more, split and insert the left part */
663 if (dei.len > 1) {
664 /* insert left part of split extent into cache */
665 if (fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
666 set_extent_info(&ei, dei.fofs, dei.blk,
667 fofs - dei.fofs);
668 en1 = __insert_extent_tree(sbi, et, &ei, NULL);
669 }
670
671 /* insert right part of split extent into cache */
672 endofs = dei.fofs + dei.len - 1;
673 if (endofs - fofs >= F2FS_MIN_EXTENT_LEN) {
674 set_extent_info(&ei, fofs + 1,
675 fofs - dei.fofs + dei.blk, endofs - fofs);
676 en2 = __insert_extent_tree(sbi, et, &ei, NULL);
677 }
678 }
679
680update_extent:
681 /* 3. update extent in extent cache */
682 if (blkaddr) {
683 set_extent_info(&ei, fofs, blkaddr, 1);
684 en3 = __insert_extent_tree(sbi, et, &ei, &den);
685 }
686
687 /* 4. update in global extent list */
688 spin_lock(&sbi->extent_lock);
689 if (en && !list_empty(&en->list))
690 list_del(&en->list);
691 /*
692 * en1 and en2 split from en, they will become more and more smaller
693 * fragments after splitting several times. So if the length is smaller
694 * than F2FS_MIN_EXTENT_LEN, we will not add them into extent tree.
695 */
696 if (en1)
697 list_add_tail(&en1->list, &sbi->extent_list);
698 if (en2)
699 list_add_tail(&en2->list, &sbi->extent_list);
700 if (en3) {
701 if (list_empty(&en3->list))
702 list_add_tail(&en3->list, &sbi->extent_list);
703 else
704 list_move_tail(&en3->list, &sbi->extent_list);
705 }
706 if (den && !list_empty(&den->list))
707 list_del(&den->list);
708 spin_unlock(&sbi->extent_lock);
709
710 /* 5. release extent node */
711 if (en)
712 kmem_cache_free(extent_node_slab, en);
713 if (den)
714 kmem_cache_free(extent_node_slab, den);
715
716 write_unlock(&et->lock);
717 atomic_dec(&et->refcount);
718}
719
Chao Yu0bdee482015-03-19 19:27:51 +0800720void f2fs_preserve_extent_tree(struct inode *inode)
721{
722 struct extent_tree *et;
723 struct extent_info *ext = &F2FS_I(inode)->ext;
724 bool sync = false;
725
726 if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
727 return;
728
729 et = __find_extent_tree(F2FS_I_SB(inode), inode->i_ino);
730 if (!et) {
731 if (ext->len) {
732 ext->len = 0;
733 update_inode_page(inode);
734 }
735 return;
736 }
737
738 read_lock(&et->lock);
739 if (et->count) {
740 struct extent_node *en;
741
742 if (et->cached_en) {
743 en = et->cached_en;
744 } else {
745 struct rb_node *node = rb_first(&et->root);
746
747 if (!node)
748 node = rb_last(&et->root);
749 en = rb_entry(node, struct extent_node, rb_node);
750 }
751
752 if (__is_extent_same(ext, &en->ei))
753 goto out;
754
755 *ext = en->ei;
756 sync = true;
757 } else if (ext->len) {
758 ext->len = 0;
759 sync = true;
760 }
761out:
762 read_unlock(&et->lock);
763 atomic_dec(&et->refcount);
764
765 if (sync)
766 update_inode_page(inode);
767}
768
Chao Yu429511c2015-02-05 17:54:31 +0800769void f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
770{
771 struct extent_tree *treevec[EXT_TREE_VEC_SIZE];
772 struct extent_node *en, *tmp;
773 unsigned long ino = F2FS_ROOT_INO(sbi);
774 struct radix_tree_iter iter;
775 void **slot;
776 unsigned int found;
Chao Yu1ec46102015-02-05 17:59:59 +0800777 unsigned int node_cnt = 0, tree_cnt = 0;
Chao Yu429511c2015-02-05 17:54:31 +0800778
Chao Yu1dcc3362015-02-05 17:57:31 +0800779 if (!test_opt(sbi, EXTENT_CACHE))
780 return;
781
Chao Yu429511c2015-02-05 17:54:31 +0800782 if (available_free_memory(sbi, EXTENT_CACHE))
783 return;
784
785 spin_lock(&sbi->extent_lock);
786 list_for_each_entry_safe(en, tmp, &sbi->extent_list, list) {
787 if (!nr_shrink--)
788 break;
789 list_del_init(&en->list);
790 }
791 spin_unlock(&sbi->extent_lock);
792
793 down_read(&sbi->extent_tree_lock);
794 while ((found = radix_tree_gang_lookup(&sbi->extent_tree_root,
795 (void **)treevec, ino, EXT_TREE_VEC_SIZE))) {
796 unsigned i;
797
798 ino = treevec[found - 1]->ino + 1;
799 for (i = 0; i < found; i++) {
800 struct extent_tree *et = treevec[i];
801
802 atomic_inc(&et->refcount);
803 write_lock(&et->lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800804 node_cnt += __free_extent_tree(sbi, et, false);
Chao Yu429511c2015-02-05 17:54:31 +0800805 write_unlock(&et->lock);
806 atomic_dec(&et->refcount);
807 }
808 }
809 up_read(&sbi->extent_tree_lock);
810
811 down_write(&sbi->extent_tree_lock);
812 radix_tree_for_each_slot(slot, &sbi->extent_tree_root, &iter,
813 F2FS_ROOT_INO(sbi)) {
814 struct extent_tree *et = (struct extent_tree *)*slot;
815
816 if (!atomic_read(&et->refcount) && !et->count) {
817 radix_tree_delete(&sbi->extent_tree_root, et->ino);
818 kmem_cache_free(extent_tree_slab, et);
819 sbi->total_ext_tree--;
Chao Yu1ec46102015-02-05 17:59:59 +0800820 tree_cnt++;
Chao Yu429511c2015-02-05 17:54:31 +0800821 }
822 }
823 up_write(&sbi->extent_tree_lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800824
825 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
Chao Yu429511c2015-02-05 17:54:31 +0800826}
827
828void f2fs_destroy_extent_tree(struct inode *inode)
829{
830 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
831 struct extent_tree *et;
Chao Yu1ec46102015-02-05 17:59:59 +0800832 unsigned int node_cnt = 0;
Chao Yu429511c2015-02-05 17:54:31 +0800833
Chao Yu1dcc3362015-02-05 17:57:31 +0800834 if (!test_opt(sbi, EXTENT_CACHE))
835 return;
836
Chao Yu93dfc522015-03-19 19:24:59 +0800837 et = __find_extent_tree(sbi, inode->i_ino);
838 if (!et)
Chao Yu429511c2015-02-05 17:54:31 +0800839 goto out;
Chao Yu429511c2015-02-05 17:54:31 +0800840
841 /* free all extent info belong to this extent tree */
842 write_lock(&et->lock);
Chao Yu1ec46102015-02-05 17:59:59 +0800843 node_cnt = __free_extent_tree(sbi, et, true);
Chao Yu429511c2015-02-05 17:54:31 +0800844 write_unlock(&et->lock);
845
846 atomic_dec(&et->refcount);
847
848 /* try to find and delete extent tree entry in radix tree */
849 down_write(&sbi->extent_tree_lock);
850 et = radix_tree_lookup(&sbi->extent_tree_root, inode->i_ino);
851 if (!et) {
852 up_write(&sbi->extent_tree_lock);
853 goto out;
854 }
855 f2fs_bug_on(sbi, atomic_read(&et->refcount) || et->count);
856 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
857 kmem_cache_free(extent_tree_slab, et);
858 sbi->total_ext_tree--;
859 up_write(&sbi->extent_tree_lock);
860out:
Chao Yu1ec46102015-02-05 17:59:59 +0800861 trace_f2fs_destroy_extent_tree(inode, node_cnt);
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900862 return;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900863}
864
Chao Yu028a41e2015-03-19 19:26:02 +0800865void f2fs_init_extent_cache(struct inode *inode, struct f2fs_extent *i_ext)
866{
867 if (test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
868 f2fs_init_extent_tree(inode, i_ext);
869
870 write_lock(&F2FS_I(inode)->ext_lock);
871 get_extent_info(&F2FS_I(inode)->ext, *i_ext);
872 write_unlock(&F2FS_I(inode)->ext_lock);
873}
874
Chao Yu7e4dde72015-02-05 17:51:34 +0800875static bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
876 struct extent_info *ei)
877{
Chao Yu91c5d9b2015-02-05 18:02:44 +0800878 if (is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
879 return false;
880
Chao Yu1dcc3362015-02-05 17:57:31 +0800881 if (test_opt(F2FS_I_SB(inode), EXTENT_CACHE))
882 return f2fs_lookup_extent_tree(inode, pgofs, ei);
883
Chao Yu7e4dde72015-02-05 17:51:34 +0800884 return lookup_extent_info(inode, pgofs, ei);
885}
886
887void f2fs_update_extent_cache(struct dnode_of_data *dn)
888{
889 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
890 pgoff_t fofs;
891
892 f2fs_bug_on(F2FS_I_SB(dn->inode), dn->data_blkaddr == NEW_ADDR);
893
Chao Yu91c5d9b2015-02-05 18:02:44 +0800894 if (is_inode_flag_set(fi, FI_NO_EXTENT))
895 return;
896
Chao Yu7e4dde72015-02-05 17:51:34 +0800897 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
898 dn->ofs_in_node;
899
Chao Yu1dcc3362015-02-05 17:57:31 +0800900 if (test_opt(F2FS_I_SB(dn->inode), EXTENT_CACHE))
901 return f2fs_update_extent_tree(dn->inode, fofs,
902 dn->data_blkaddr);
903
Chao Yu7e4dde72015-02-05 17:51:34 +0800904 if (update_extent_info(dn->inode, fofs, dn->data_blkaddr))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900905 sync_inode_page(dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900906}
907
908struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
909{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900910 struct address_space *mapping = inode->i_mapping;
911 struct dnode_of_data dn;
912 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800913 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900914 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800915 struct f2fs_io_info fio = {
916 .type = DATA,
917 .rw = sync ? READ_SYNC : READA,
918 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900919
Jaegeuk Kimb7f204c2015-02-25 19:54:48 -0800920 /*
921 * If sync is false, it needs to check its block allocation.
922 * This is need and triggered by two flows:
923 * gc and truncate_partial_data_page.
924 */
925 if (!sync)
926 goto search;
927
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900928 page = find_get_page(mapping, index);
929 if (page && PageUptodate(page))
930 return page;
931 f2fs_put_page(page, 0);
Jaegeuk Kimb7f204c2015-02-25 19:54:48 -0800932search:
Chao Yucb3bc9e2015-02-05 18:03:40 +0800933 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
934 dn.data_blkaddr = ei.blk + index - ei.fofs;
935 goto got_it;
936 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900937
938 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900939 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900940 if (err)
941 return ERR_PTR(err);
942 f2fs_put_dnode(&dn);
943
944 if (dn.data_blkaddr == NULL_ADDR)
945 return ERR_PTR(-ENOENT);
946
947 /* By fallocate(), there is no cached page, but with NEW_ADDR */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900948 if (unlikely(dn.data_blkaddr == NEW_ADDR))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900949 return ERR_PTR(-EINVAL);
950
Chao Yucb3bc9e2015-02-05 18:03:40 +0800951got_it:
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900952 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900953 if (!page)
954 return ERR_PTR(-ENOMEM);
955
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900956 if (PageUptodate(page)) {
957 unlock_page(page);
958 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900959 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900960
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800961 fio.blk_addr = dn.data_blkaddr;
962 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Chao Yu1069bbf2013-11-28 15:43:43 +0800963 if (err)
964 return ERR_PTR(err);
965
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900966 if (sync) {
967 wait_on_page_locked(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900968 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900969 f2fs_put_page(page, 0);
970 return ERR_PTR(-EIO);
971 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900972 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900973 return page;
974}
975
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900976/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900977 * If it tries to access a hole, return an error.
978 * Because, the callers, functions in dir.c and GC, should be able to know
979 * whether this page exists or not.
980 */
981struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
982{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900983 struct address_space *mapping = inode->i_mapping;
984 struct dnode_of_data dn;
985 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800986 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900987 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800988 struct f2fs_io_info fio = {
989 .type = DATA,
990 .rw = READ_SYNC,
991 };
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900992repeat:
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900993 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900994 if (!page)
995 return ERR_PTR(-ENOMEM);
996
Chao Yucb3bc9e2015-02-05 18:03:40 +0800997 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
998 dn.data_blkaddr = ei.blk + index - ei.fofs;
999 goto got_it;
1000 }
1001
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001002 set_new_dnode(&dn, inode, NULL, NULL, 0);
1003 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
1004 if (err) {
1005 f2fs_put_page(page, 1);
1006 return ERR_PTR(err);
1007 }
1008 f2fs_put_dnode(&dn);
1009
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001010 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim650495d2013-05-13 08:38:35 +09001011 f2fs_put_page(page, 1);
1012 return ERR_PTR(-ENOENT);
1013 }
1014
Chao Yucb3bc9e2015-02-05 18:03:40 +08001015got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001016 if (PageUptodate(page))
1017 return page;
1018
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +09001019 /*
1020 * A new dentry page is allocated but not able to be written, since its
1021 * new inode page couldn't be allocated due to -ENOSPC.
1022 * In such the case, its blkaddr can be remained as NEW_ADDR.
1023 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
1024 */
1025 if (dn.data_blkaddr == NEW_ADDR) {
1026 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1027 SetPageUptodate(page);
1028 return page;
1029 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001030
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001031 fio.blk_addr = dn.data_blkaddr;
1032 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001033 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001034 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001035
1036 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001037 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001038 f2fs_put_page(page, 1);
1039 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001040 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001041 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001042 f2fs_put_page(page, 1);
1043 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001044 }
1045 return page;
1046}
1047
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001048/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001049 * Caller ensures that this data page is never allocated.
1050 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +09001051 *
Chao Yu4f4124d2013-12-21 18:02:14 +08001052 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
1053 * f2fs_unlock_op().
Jaegeuk Kima8865372013-12-27 17:04:17 +09001054 * Note that, ipage is set only by make_empty_dir.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001055 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +09001056struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +09001057 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001058{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001059 struct address_space *mapping = inode->i_mapping;
1060 struct page *page;
1061 struct dnode_of_data dn;
1062 int err;
1063
Jaegeuk Kima8865372013-12-27 17:04:17 +09001064 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +08001065 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001066 if (err)
1067 return ERR_PTR(err);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001068repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001069 page = grab_cache_page(mapping, index);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001070 if (!page) {
1071 err = -ENOMEM;
1072 goto put_err;
1073 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001074
1075 if (PageUptodate(page))
1076 return page;
1077
1078 if (dn.data_blkaddr == NEW_ADDR) {
1079 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001080 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001081 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001082 struct f2fs_io_info fio = {
1083 .type = DATA,
1084 .rw = READ_SYNC,
1085 .blk_addr = dn.data_blkaddr,
1086 };
1087 err = f2fs_submit_page_bio(F2FS_I_SB(inode), page, &fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001088 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +09001089 goto put_err;
1090
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001091 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001092 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001093 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +09001094 err = -EIO;
1095 goto put_err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001096 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001097 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001098 f2fs_put_page(page, 1);
1099 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001100 }
1101 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001102
1103 if (new_i_size &&
1104 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
1105 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +09001106 /* Only the directory inode sets new_i_size */
1107 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001108 }
1109 return page;
Jaegeuk Kima8865372013-12-27 17:04:17 +09001110
1111put_err:
1112 f2fs_put_dnode(&dn);
1113 return ERR_PTR(err);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001114}
1115
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001116static int __allocate_data_block(struct dnode_of_data *dn)
1117{
Jaegeuk Kim40813632014-09-02 15:31:18 -07001118 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001119 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001120 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001121 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001122 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001123 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001124
1125 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
1126 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +08001127
1128 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
1129 if (dn->data_blkaddr == NEW_ADDR)
1130 goto alloc;
1131
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001132 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
1133 return -ENOSPC;
1134
Chao Yudf6136e2015-03-23 10:33:37 +08001135alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001136 get_node_info(sbi, dn->nid, &ni);
1137 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
1138
Jaegeuk Kim38aa0882015-01-05 16:02:20 -08001139 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
1140 seg = CURSEG_DIRECT_IO;
1141
Chao Yudf6136e2015-03-23 10:33:37 +08001142 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
1143 &sum, seg);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001144
1145 /* direct IO doesn't use extent cache to maximize the performance */
Chao Yu216a6202015-03-19 19:23:32 +08001146 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001147
Jaegeuk Kim976e4c52014-09-15 19:32:16 -07001148 /* update i_size */
1149 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
1150 dn->ofs_in_node;
1151 if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT))
1152 i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT));
1153
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001154 return 0;
1155}
1156
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001157static void __allocate_data_blocks(struct inode *inode, loff_t offset,
1158 size_t count)
1159{
1160 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1161 struct dnode_of_data dn;
1162 u64 start = F2FS_BYTES_TO_BLK(offset);
1163 u64 len = F2FS_BYTES_TO_BLK(count);
1164 bool allocated;
1165 u64 end_offset;
1166
1167 while (len) {
1168 f2fs_balance_fs(sbi);
1169 f2fs_lock_op(sbi);
1170
1171 /* When reading holes, we need its node page */
1172 set_new_dnode(&dn, inode, NULL, NULL, 0);
1173 if (get_dnode_of_data(&dn, start, ALLOC_NODE))
1174 goto out;
1175
1176 allocated = false;
1177 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
1178
1179 while (dn.ofs_in_node < end_offset && len) {
Chao Yud6d4f1c2015-03-12 17:04:24 +08001180 block_t blkaddr;
1181
1182 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +08001183 if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR) {
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001184 if (__allocate_data_block(&dn))
1185 goto sync_out;
1186 allocated = true;
1187 }
1188 len--;
1189 start++;
1190 dn.ofs_in_node++;
1191 }
1192
1193 if (allocated)
1194 sync_inode_page(&dn);
1195
1196 f2fs_put_dnode(&dn);
1197 f2fs_unlock_op(sbi);
1198 }
1199 return;
1200
1201sync_out:
1202 if (allocated)
1203 sync_inode_page(&dn);
1204 f2fs_put_dnode(&dn);
1205out:
1206 f2fs_unlock_op(sbi);
1207 return;
1208}
1209
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001210/*
Chao Yu4f4124d2013-12-21 18:02:14 +08001211 * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
1212 * If original data blocks are allocated, then give them to blockdev.
1213 * Otherwise,
1214 * a. preallocate requested block addresses
1215 * b. do not use extent cache for better performance
1216 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001217 */
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001218static int __get_data_block(struct inode *inode, sector_t iblock,
1219 struct buffer_head *bh_result, int create, bool fiemap)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001220{
1221 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
1222 unsigned maxblocks = bh_result->b_size >> blkbits;
1223 struct dnode_of_data dn;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001224 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
1225 pgoff_t pgofs, end_offset;
1226 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001227 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001228 bool allocated = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001229
1230 /* Get the page offset from the block offset(iblock) */
1231 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
1232
Chao Yu7e4dde72015-02-05 17:51:34 +08001233 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Chao Yua2e7d1b2015-02-05 17:50:30 +08001234 f2fs_map_bh(inode->i_sb, pgofs, &ei, bh_result);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001235 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001236 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001237
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001238 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001239 f2fs_lock_op(F2FS_I_SB(inode));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001240
1241 /* When reading holes, we need its node page */
1242 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001243 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001244 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001245 if (err == -ENOENT)
1246 err = 0;
1247 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001248 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001249 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001250 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001251
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001252 if (dn.data_blkaddr != NULL_ADDR) {
Jaegeuk Kim3402e872015-03-11 23:27:25 -04001253 clear_buffer_new(bh_result);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001254 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001255 } else if (create) {
1256 err = __allocate_data_block(&dn);
1257 if (err)
1258 goto put_out;
1259 allocated = true;
Jaegeuk Kimda17eec2015-02-09 10:34:38 -08001260 set_buffer_new(bh_result);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001261 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
1262 } else {
1263 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001264 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001265
Chao Yu6403eb12014-04-26 19:59:52 +08001266 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001267 bh_result->b_size = (((size_t)1) << blkbits);
1268 dn.ofs_in_node++;
1269 pgofs++;
1270
1271get_next:
1272 if (dn.ofs_in_node >= end_offset) {
1273 if (allocated)
1274 sync_inode_page(&dn);
1275 allocated = false;
1276 f2fs_put_dnode(&dn);
1277
1278 set_new_dnode(&dn, inode, NULL, NULL, 0);
1279 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001280 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001281 if (err == -ENOENT)
1282 err = 0;
1283 goto unlock_out;
1284 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001285 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001286 goto put_out;
1287
Chao Yu6403eb12014-04-26 19:59:52 +08001288 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001289 }
1290
1291 if (maxblocks > (bh_result->b_size >> blkbits)) {
1292 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
1293 if (blkaddr == NULL_ADDR && create) {
1294 err = __allocate_data_block(&dn);
1295 if (err)
1296 goto sync_out;
1297 allocated = true;
Jaegeuk Kim3402e872015-03-11 23:27:25 -04001298 set_buffer_new(bh_result);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001299 blkaddr = dn.data_blkaddr;
1300 }
arter97e1c42042014-08-06 23:22:50 +09001301 /* Give more consecutive addresses for the readahead */
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001302 if (blkaddr == (bh_result->b_blocknr + ofs)) {
1303 ofs++;
1304 dn.ofs_in_node++;
1305 pgofs++;
1306 bh_result->b_size += (((size_t)1) << blkbits);
1307 goto get_next;
1308 }
1309 }
1310sync_out:
1311 if (allocated)
1312 sync_inode_page(&dn);
1313put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001314 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001315unlock_out:
1316 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -07001317 f2fs_unlock_op(F2FS_I_SB(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001318out:
1319 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
1320 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001321}
1322
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001323static int get_data_block(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, false);
1327}
1328
1329static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
1330 struct buffer_head *bh_result, int create)
1331{
1332 return __get_data_block(inode, iblock, bh_result, create, true);
1333}
1334
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001335int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1336 u64 start, u64 len)
1337{
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001338 return generic_block_fiemap(inode, fieinfo,
1339 start, len, get_data_block_fiemap);
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001340}
1341
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001342static int f2fs_read_data_page(struct file *file, struct page *page)
1343{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001344 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001345 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001346
Chao Yuc20e89c2014-05-06 16:53:08 +08001347 trace_f2fs_readpage(page, DATA);
1348
arter97e1c42042014-08-06 23:22:50 +09001349 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001350 if (f2fs_has_inline_data(inode))
1351 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001352 if (ret == -EAGAIN)
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001353 ret = mpage_readpage(page, get_data_block);
1354
1355 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001356}
1357
1358static int f2fs_read_data_pages(struct file *file,
1359 struct address_space *mapping,
1360 struct list_head *pages, unsigned nr_pages)
1361{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001362 struct inode *inode = file->f_mapping->host;
1363
1364 /* If the file has inline data, skip readpages */
1365 if (f2fs_has_inline_data(inode))
1366 return 0;
1367
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001368 return mpage_readpages(mapping, pages, nr_pages, get_data_block);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001369}
1370
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001371int do_write_data_page(struct page *page, struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001372{
1373 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001374 struct dnode_of_data dn;
1375 int err = 0;
1376
1377 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001378 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001379 if (err)
1380 return err;
1381
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001382 fio->blk_addr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001383
1384 /* This page is already truncated */
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001385 if (fio->blk_addr == NULL_ADDR) {
1386 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001387 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001388 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001389
1390 set_page_writeback(page);
1391
1392 /*
1393 * If current allocation needs SSR,
1394 * it had better in-place writes for updated data.
1395 */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001396 if (unlikely(fio->blk_addr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001397 !is_cold_data(page) &&
1398 need_inplace_update(inode))) {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001399 rewrite_data_page(page, fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001400 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001401 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001402 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001403 write_data_page(page, &dn, fio);
Chao Yu216a6202015-03-19 19:23:32 +08001404 set_data_blkaddr(&dn);
Chao Yu7e4dde72015-02-05 17:51:34 +08001405 f2fs_update_extent_cache(&dn);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001406 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001407 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001408 if (page->index == 0)
1409 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001410 }
1411out_writepage:
1412 f2fs_put_dnode(&dn);
1413 return err;
1414}
1415
1416static int f2fs_write_data_page(struct page *page,
1417 struct writeback_control *wbc)
1418{
1419 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001420 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001421 loff_t i_size = i_size_read(inode);
1422 const pgoff_t end_index = ((unsigned long long) i_size)
1423 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001424 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001425 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001426 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001427 struct f2fs_io_info fio = {
1428 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001429 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001430 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001431
Chao Yuecda0de2014-05-06 16:48:26 +08001432 trace_f2fs_writepage(page, DATA);
1433
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001434 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001435 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001436
1437 /*
1438 * If the offset is out-of-range of file size,
1439 * this page does not have to be written to disk.
1440 */
1441 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001442 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001443 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001444
1445 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001446write:
Chao Yucaf00472015-01-28 17:48:42 +08001447 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001448 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001449 if (f2fs_is_drop_cache(inode))
1450 goto out;
1451 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1452 available_free_memory(sbi, BASE_CHECK))
1453 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001454
Jaegeuk Kim39936832012-11-22 16:21:29 +09001455 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001456 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001457 if (unlikely(f2fs_cp_error(sbi)))
1458 goto redirty_out;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001459 err = do_write_data_page(page, &fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001460 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001461 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001462
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001463 /* we should bypass data pages to proceed the kworkder jobs */
1464 if (unlikely(f2fs_cp_error(sbi))) {
1465 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001466 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001467 }
1468
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001469 if (!wbc->for_reclaim)
1470 need_balance_fs = true;
1471 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001472 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001473
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001474 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001475 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001476 if (f2fs_has_inline_data(inode))
1477 err = f2fs_write_inline_data(inode, page);
1478 if (err == -EAGAIN)
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001479 err = do_write_data_page(page, &fio);
1480 f2fs_unlock_op(sbi);
1481done:
1482 if (err && err != -ENOENT)
1483 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001484
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001485 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001486out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001487 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001488 if (err)
1489 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001490 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001491 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001492 f2fs_balance_fs(sbi);
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +09001493 if (wbc->for_reclaim)
1494 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001495 return 0;
1496
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001497redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001498 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001499 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001500}
1501
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001502static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1503 void *data)
1504{
1505 struct address_space *mapping = data;
1506 int ret = mapping->a_ops->writepage(page, wbc);
1507 mapping_set_error(mapping, ret);
1508 return ret;
1509}
1510
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001511static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001512 struct writeback_control *wbc)
1513{
1514 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001515 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001516 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001517 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001518
Chao Yue5748432014-05-06 16:51:24 +08001519 trace_f2fs_writepages(mapping->host, wbc, DATA);
1520
P J Pcfb185a2013-04-03 11:38:00 +09001521 /* deal with chardevs and other special file */
1522 if (!mapping->a_ops->writepage)
1523 return 0;
1524
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001525 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001526 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001527 available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001528 goto skip_write;
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001529
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001530 /* during POR, we don't need to trigger writepage at all. */
1531 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1532 goto skip_write;
1533
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001534 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001535
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001536 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001537
1538 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001539
1540 remove_dirty_dir_inode(inode);
1541
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001542 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001543 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001544
1545skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001546 wbc->pages_skipped += get_dirty_pages(inode);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001547 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001548}
1549
Chao Yu3aab8f82014-07-02 13:25:04 +08001550static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1551{
1552 struct inode *inode = mapping->host;
1553
1554 if (to > inode->i_size) {
1555 truncate_pagecache(inode, inode->i_size);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -07001556 truncate_blocks(inode, inode->i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001557 }
1558}
1559
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001560static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1561 loff_t pos, unsigned len, unsigned flags,
1562 struct page **pagep, void **fsdata)
1563{
1564 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001565 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001566 struct page *page, *ipage;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001567 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
1568 struct dnode_of_data dn;
1569 int err = 0;
1570
Chao Yu62aed042014-05-06 16:46:04 +08001571 trace_f2fs_write_begin(inode, pos, len, flags);
1572
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001573 f2fs_balance_fs(sbi);
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001574
1575 /*
1576 * We should check this at this moment to avoid deadlock on inode page
1577 * and #0 page. The locking rule for inline_data conversion should be:
1578 * lock_page(page #0) -> lock_page(inode_page)
1579 */
1580 if (index != 0) {
1581 err = f2fs_convert_inline_inode(inode);
1582 if (err)
1583 goto fail;
1584 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001585repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001586 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001587 if (!page) {
1588 err = -ENOMEM;
1589 goto fail;
1590 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001591
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001592 *pagep = page;
1593
Gu Zhenge4795562013-09-27 18:08:30 +08001594 f2fs_lock_op(sbi);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001595
1596 /* check inline_data */
1597 ipage = get_node_page(sbi, inode->i_ino);
Chao Yucd34e292014-12-01 11:30:20 +08001598 if (IS_ERR(ipage)) {
1599 err = PTR_ERR(ipage);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001600 goto unlock_fail;
Chao Yucd34e292014-12-01 11:30:20 +08001601 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001602
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001603 set_new_dnode(&dn, inode, ipage, ipage, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001604
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001605 if (f2fs_has_inline_data(inode)) {
1606 if (pos + len <= MAX_INLINE_DATA) {
1607 read_inline_data(page, ipage);
1608 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1609 sync_inode_page(&dn);
1610 goto put_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001611 }
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001612 err = f2fs_convert_inline_page(&dn, page);
1613 if (err)
1614 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001615 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001616 err = f2fs_reserve_block(&dn, index);
1617 if (err)
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001618 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001619put_next:
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001620 f2fs_put_dnode(&dn);
1621 f2fs_unlock_op(sbi);
1622
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001623 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
1624 return 0;
1625
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001626 f2fs_wait_on_page_writeback(page, DATA);
1627
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001628 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1629 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1630 unsigned end = start + len;
1631
1632 /* Reading beyond i_size is simple: memset to zero */
1633 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001634 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001635 }
1636
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001637 if (dn.data_blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001638 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1639 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001640 struct f2fs_io_info fio = {
1641 .type = DATA,
1642 .rw = READ_SYNC,
1643 .blk_addr = dn.data_blkaddr,
1644 };
1645 err = f2fs_submit_page_bio(sbi, page, &fio);
Jan Kara9234f312014-10-22 15:21:47 +02001646 if (err)
1647 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001648
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001649 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001650 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001651 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001652 err = -EIO;
1653 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001654 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001655 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001656 f2fs_put_page(page, 1);
1657 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001658 }
1659 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001660out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001661 SetPageUptodate(page);
1662 clear_cold_data(page);
1663 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001664
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001665put_fail:
1666 f2fs_put_dnode(&dn);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001667unlock_fail:
1668 f2fs_unlock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001669 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001670fail:
1671 f2fs_write_failed(mapping, pos + len);
1672 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001673}
1674
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001675static int f2fs_write_end(struct file *file,
1676 struct address_space *mapping,
1677 loff_t pos, unsigned len, unsigned copied,
1678 struct page *page, void *fsdata)
1679{
1680 struct inode *inode = page->mapping->host;
1681
Chao Yudfb2bf32014-05-06 16:47:23 +08001682 trace_f2fs_write_end(inode, pos, len, copied);
1683
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001684 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001685
1686 if (pos + copied > i_size_read(inode)) {
1687 i_size_write(inode, pos + copied);
1688 mark_inode_dirty(inode);
1689 update_inode_page(inode);
1690 }
1691
Chao Yu75c3c8b2013-11-16 14:15:59 +08001692 f2fs_put_page(page, 1);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001693 return copied;
1694}
1695
Omar Sandoval6f673762015-03-16 04:33:52 -07001696static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1697 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001698{
1699 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001700
Omar Sandoval6f673762015-03-16 04:33:52 -07001701 if (iov_iter_rw(iter) == READ)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001702 return 0;
1703
1704 if (offset & blocksize_mask)
1705 return -EINVAL;
1706
Al Viro5b46f252014-03-16 18:07:34 -04001707 if (iov_iter_alignment(iter) & blocksize_mask)
1708 return -EINVAL;
1709
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001710 return 0;
1711}
1712
Omar Sandoval22c61862015-03-16 04:33:53 -07001713static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
1714 loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001715{
1716 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001717 struct address_space *mapping = file->f_mapping;
1718 struct inode *inode = mapping->host;
1719 size_t count = iov_iter_count(iter);
1720 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001721
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001722 /* we don't need to use inline_data strictly */
1723 if (f2fs_has_inline_data(inode)) {
1724 err = f2fs_convert_inline_inode(inode);
1725 if (err)
1726 return err;
1727 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001728
Omar Sandoval6f673762015-03-16 04:33:52 -07001729 if (check_direct_IO(inode, iter, offset))
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001730 return 0;
1731
Omar Sandoval6f673762015-03-16 04:33:52 -07001732 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001733
Omar Sandoval6f673762015-03-16 04:33:52 -07001734 if (iov_iter_rw(iter) == WRITE)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001735 __allocate_data_blocks(inode, offset, count);
1736
Omar Sandoval17f8c842015-03-16 04:33:50 -07001737 err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block);
Omar Sandoval6f673762015-03-16 04:33:52 -07001738 if (err < 0 && iov_iter_rw(iter) == WRITE)
Chao Yu3aab8f82014-07-02 13:25:04 +08001739 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001740
Omar Sandoval6f673762015-03-16 04:33:52 -07001741 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001742
Chao Yu3aab8f82014-07-02 13:25:04 +08001743 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001744}
1745
Chao Yu487261f2015-02-05 17:44:29 +08001746void f2fs_invalidate_page(struct page *page, unsigned int offset,
1747 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001748{
1749 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001750 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001751
Chao Yu487261f2015-02-05 17:44:29 +08001752 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1753 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001754 return;
1755
Chao Yu487261f2015-02-05 17:44:29 +08001756 if (PageDirty(page)) {
1757 if (inode->i_ino == F2FS_META_INO(sbi))
1758 dec_page_count(sbi, F2FS_DIRTY_META);
1759 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1760 dec_page_count(sbi, F2FS_DIRTY_NODES);
1761 else
1762 inode_dec_dirty_pages(inode);
1763 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001764 ClearPagePrivate(page);
1765}
1766
Chao Yu487261f2015-02-05 17:44:29 +08001767int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001768{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001769 /* If this is dirty page, keep PagePrivate */
1770 if (PageDirty(page))
1771 return 0;
1772
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001773 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001774 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001775}
1776
1777static int f2fs_set_data_page_dirty(struct page *page)
1778{
1779 struct address_space *mapping = page->mapping;
1780 struct inode *inode = mapping->host;
1781
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001782 trace_f2fs_set_page_dirty(page, DATA);
1783
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001784 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001785
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001786 if (f2fs_is_atomic_file(inode)) {
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001787 register_inmem_page(inode, page);
1788 return 1;
1789 }
1790
Jaegeuk Kima18ff062014-01-21 13:32:12 +09001791 mark_inode_dirty(inode);
1792
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001793 if (!PageDirty(page)) {
1794 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001795 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001796 return 1;
1797 }
1798 return 0;
1799}
1800
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001801static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1802{
Chao Yu454ae7e2014-04-22 13:34:01 +08001803 struct inode *inode = mapping->host;
1804
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001805 /* we don't need to use inline_data strictly */
1806 if (f2fs_has_inline_data(inode)) {
1807 int err = f2fs_convert_inline_inode(inode);
1808 if (err)
1809 return err;
1810 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001811 return generic_block_bmap(mapping, block, get_data_block);
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001812}
1813
Chao Yu429511c2015-02-05 17:54:31 +08001814void init_extent_cache_info(struct f2fs_sb_info *sbi)
1815{
1816 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
1817 init_rwsem(&sbi->extent_tree_lock);
1818 INIT_LIST_HEAD(&sbi->extent_list);
1819 spin_lock_init(&sbi->extent_lock);
1820 sbi->total_ext_tree = 0;
1821 atomic_set(&sbi->total_ext_node, 0);
1822}
1823
1824int __init create_extent_cache(void)
1825{
1826 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
1827 sizeof(struct extent_tree));
1828 if (!extent_tree_slab)
1829 return -ENOMEM;
1830 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
1831 sizeof(struct extent_node));
1832 if (!extent_node_slab) {
1833 kmem_cache_destroy(extent_tree_slab);
1834 return -ENOMEM;
1835 }
1836 return 0;
1837}
1838
1839void destroy_extent_cache(void)
1840{
1841 kmem_cache_destroy(extent_node_slab);
1842 kmem_cache_destroy(extent_tree_slab);
1843}
1844
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001845const struct address_space_operations f2fs_dblock_aops = {
1846 .readpage = f2fs_read_data_page,
1847 .readpages = f2fs_read_data_pages,
1848 .writepage = f2fs_write_data_page,
1849 .writepages = f2fs_write_data_pages,
1850 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001851 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001852 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001853 .invalidatepage = f2fs_invalidate_page,
1854 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001855 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001856 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001857};