blob: 03313099c51c44b957f84fb6ec4a6ab698eee970 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002 * fs/f2fs/data.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/buffer_head.h>
14#include <linux/mpage.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070015#include <linux/aio.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090016#include <linux/writeback.h>
17#include <linux/backing-dev.h>
18#include <linux/blkdev.h>
19#include <linux/bio.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010020#include <linux/prefetch.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090021
22#include "f2fs.h"
23#include "node.h"
24#include "segment.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090025#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090026
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090027static void f2fs_read_end_io(struct bio *bio, int err)
28{
Linus Torvaldsf5688492014-01-30 11:19:05 -080029 struct bio_vec *bvec;
30 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090031
Linus Torvaldsf5688492014-01-30 11:19:05 -080032 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090033 struct page *page = bvec->bv_page;
34
Linus Torvaldsf5688492014-01-30 11:19:05 -080035 if (!err) {
36 SetPageUptodate(page);
37 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090038 ClearPageUptodate(page);
39 SetPageError(page);
40 }
41 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080042 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090043 bio_put(bio);
44}
45
46static void f2fs_write_end_io(struct bio *bio, int err)
47{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090048 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080049 struct bio_vec *bvec;
50 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090051
Linus Torvaldsf5688492014-01-30 11:19:05 -080052 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090053 struct page *page = bvec->bv_page;
54
Linus Torvaldsf5688492014-01-30 11:19:05 -080055 if (unlikely(err)) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090056 SetPageError(page);
57 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim744602c2014-01-24 09:42:16 +090058 f2fs_stop_checkpoint(sbi);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090059 }
60 end_page_writeback(page);
61 dec_page_count(sbi, F2FS_WRITEBACK);
Linus Torvaldsf5688492014-01-30 11:19:05 -080062 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090063
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090064 if (sbi->wait_io) {
65 complete(sbi->wait_io);
66 sbi->wait_io = NULL;
67 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090068
69 if (!get_pages(sbi, F2FS_WRITEBACK) &&
70 !list_empty(&sbi->cp_wait.task_list))
71 wake_up(&sbi->cp_wait);
72
73 bio_put(bio);
74}
75
Gu Zheng940a6d32013-12-20 17:39:59 +080076/*
77 * Low-level block read/write IO operations.
78 */
79static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
80 int npages, bool is_read)
81{
82 struct bio *bio;
83
84 /* No failure on bio allocation */
85 bio = bio_alloc(GFP_NOIO, npages);
86
87 bio->bi_bdev = sbi->sb->s_bdev;
Linus Torvaldsf5688492014-01-30 11:19:05 -080088 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +080089 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090090 bio->bi_private = sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +080091
92 return bio;
93}
94
Jaegeuk Kim458e6192013-12-11 13:54:01 +090095static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090096{
Jaegeuk Kim458e6192013-12-11 13:54:01 +090097 struct f2fs_io_info *fio = &io->fio;
98 int rw;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090099
100 if (!io->bio)
101 return;
102
Gu Zheng7e8f2302013-12-20 18:17:49 +0800103 rw = fio->rw;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900104
105 if (is_read_io(rw)) {
Gu Zheng940a6d32013-12-20 17:39:59 +0800106 trace_f2fs_submit_read_bio(io->sbi->sb, rw,
107 fio->type, io->bio);
Jaegeuk Kim76130cc2013-12-11 14:29:39 +0900108 submit_bio(rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900109 } else {
Gu Zheng940a6d32013-12-20 17:39:59 +0800110 trace_f2fs_submit_write_bio(io->sbi->sb, rw,
111 fio->type, io->bio);
112 /*
113 * META_FLUSH is only from the checkpoint procedure, and we
114 * should wait this metadata bio for FS consistency.
115 */
116 if (fio->type == META_FLUSH) {
117 DECLARE_COMPLETION_ONSTACK(wait);
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +0900118 io->sbi->wait_io = &wait;
Gu Zheng940a6d32013-12-20 17:39:59 +0800119 submit_bio(rw, io->bio);
120 wait_for_completion(&wait);
121 } else {
122 submit_bio(rw, io->bio);
123 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900124 }
Gu Zheng940a6d32013-12-20 17:39:59 +0800125
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900126 io->bio = NULL;
127}
128
129void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900130 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900131{
132 enum page_type btype = PAGE_TYPE_OF_BIO(type);
133 struct f2fs_bio_info *io;
134
135 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
136
Chao Yudf0f8dc2014-03-22 14:57:23 +0800137 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900138
139 /* change META to META_FLUSH in the checkpoint procedure */
140 if (type >= META_FLUSH) {
141 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700142 if (test_opt(sbi, NOBARRIER))
143 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
144 else
145 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900146 }
147 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800148 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900149}
150
151/*
152 * Fill the locked page with data located in the block address.
153 * Return unlocked page.
154 */
155int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page,
156 block_t blk_addr, int rw)
157{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900158 struct bio *bio;
159
160 trace_f2fs_submit_page_bio(page, blk_addr, rw);
161
162 /* Allocate a new bio */
Gu Zheng940a6d32013-12-20 17:39:59 +0800163 bio = __bio_alloc(sbi, blk_addr, 1, is_read_io(rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900164
165 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
166 bio_put(bio);
167 f2fs_put_page(page, 1);
168 return -EFAULT;
169 }
170
171 submit_bio(rw, bio);
172 return 0;
173}
174
175void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900176 block_t blk_addr, struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900177{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900178 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900179 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800180 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900181
Gu Zheng940a6d32013-12-20 17:39:59 +0800182 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900183
184 verify_block_addr(sbi, blk_addr);
185
Chao Yudf0f8dc2014-03-22 14:57:23 +0800186 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900187
Gu Zheng940a6d32013-12-20 17:39:59 +0800188 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900189 inc_page_count(sbi, F2FS_WRITEBACK);
190
Fan Li63a0b7cb2013-12-09 16:09:00 +0800191 if (io->bio && (io->last_block_in_bio != blk_addr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900192 io->fio.rw != fio->rw))
193 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900194alloc_new:
195 if (io->bio == NULL) {
Gu Zheng940a6d32013-12-20 17:39:59 +0800196 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
197
198 io->bio = __bio_alloc(sbi, blk_addr, bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900199 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900200 }
201
202 if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
203 PAGE_CACHE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900204 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900205 goto alloc_new;
206 }
207
208 io->last_block_in_bio = blk_addr;
209
Chao Yudf0f8dc2014-03-22 14:57:23 +0800210 up_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900211 trace_f2fs_submit_page_mbio(page, fio->rw, fio->type, blk_addr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900212}
213
214/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900215 * Lock ordering for the change of data block address:
216 * ->data_page
217 * ->node_page
218 * update block addresses in the node page
219 */
220static void __set_data_blkaddr(struct dnode_of_data *dn, block_t new_addr)
221{
222 struct f2fs_node *rn;
223 __le32 *addr_array;
224 struct page *node_page = dn->node_page;
225 unsigned int ofs_in_node = dn->ofs_in_node;
226
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000227 f2fs_wait_on_page_writeback(node_page, NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900228
Gu Zheng45590712013-07-15 17:57:38 +0800229 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900230
231 /* Get physical address of data block */
232 addr_array = blkaddr_in_node(rn);
233 addr_array[ofs_in_node] = cpu_to_le32(new_addr);
234 set_page_dirty(node_page);
235}
236
237int reserve_new_block(struct dnode_of_data *dn)
238{
239 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
240
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900241 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900242 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800243 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900244 return -ENOSPC;
245
Namjae Jeonc01e2852013-04-23 17:00:52 +0900246 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
247
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900248 __set_data_blkaddr(dn, NEW_ADDR);
249 dn->data_blkaddr = NEW_ADDR;
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900250 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900251 sync_inode_page(dn);
252 return 0;
253}
254
Huajun Lib6009652013-11-10 23:13:18 +0800255int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
256{
257 bool need_put = dn->inode_page ? false : true;
258 int err;
259
Jaegeuk Kima8865372013-12-27 17:04:17 +0900260 /* if inode_page exists, index should be zero */
261 f2fs_bug_on(!need_put && index);
262
Huajun Lib6009652013-11-10 23:13:18 +0800263 err = get_dnode_of_data(dn, index, ALLOC_NODE);
264 if (err)
265 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900266
Huajun Lib6009652013-11-10 23:13:18 +0800267 if (dn->data_blkaddr == NULL_ADDR)
268 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900269 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800270 f2fs_put_dnode(dn);
271 return err;
272}
273
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900274static int check_extent_cache(struct inode *inode, pgoff_t pgofs,
275 struct buffer_head *bh_result)
276{
277 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900278 pgoff_t start_fofs, end_fofs;
279 block_t start_blkaddr;
280
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900281 if (is_inode_flag_set(fi, FI_NO_EXTENT))
282 return 0;
283
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900284 read_lock(&fi->ext.ext_lock);
285 if (fi->ext.len == 0) {
286 read_unlock(&fi->ext.ext_lock);
287 return 0;
288 }
289
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900290 stat_inc_total_hit(inode->i_sb);
291
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900292 start_fofs = fi->ext.fofs;
293 end_fofs = fi->ext.fofs + fi->ext.len - 1;
294 start_blkaddr = fi->ext.blk_addr;
295
296 if (pgofs >= start_fofs && pgofs <= end_fofs) {
297 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
298 size_t count;
299
300 clear_buffer_new(bh_result);
301 map_bh(bh_result, inode->i_sb,
302 start_blkaddr + pgofs - start_fofs);
303 count = end_fofs - pgofs + 1;
304 if (count < (UINT_MAX >> blkbits))
305 bh_result->b_size = (count << blkbits);
306 else
307 bh_result->b_size = UINT_MAX;
308
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900309 stat_inc_read_hit(inode->i_sb);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900310 read_unlock(&fi->ext.ext_lock);
311 return 1;
312 }
313 read_unlock(&fi->ext.ext_lock);
314 return 0;
315}
316
317void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn)
318{
319 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
320 pgoff_t fofs, start_fofs, end_fofs;
321 block_t start_blkaddr, end_blkaddr;
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900322 int need_update = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900323
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900324 f2fs_bug_on(blk_addr == NEW_ADDR);
Jaegeuk Kimde936532013-08-12 21:08:03 +0900325 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
326 dn->ofs_in_node;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900327
328 /* Update the page address in the parent node */
329 __set_data_blkaddr(dn, blk_addr);
330
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900331 if (is_inode_flag_set(fi, FI_NO_EXTENT))
332 return;
333
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900334 write_lock(&fi->ext.ext_lock);
335
336 start_fofs = fi->ext.fofs;
337 end_fofs = fi->ext.fofs + fi->ext.len - 1;
338 start_blkaddr = fi->ext.blk_addr;
339 end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1;
340
341 /* Drop and initialize the matched extent */
342 if (fi->ext.len == 1 && fofs == start_fofs)
343 fi->ext.len = 0;
344
345 /* Initial extent */
346 if (fi->ext.len == 0) {
347 if (blk_addr != NULL_ADDR) {
348 fi->ext.fofs = fofs;
349 fi->ext.blk_addr = blk_addr;
350 fi->ext.len = 1;
351 }
352 goto end_update;
353 }
354
Namjae Jeon6224da82013-04-06 14:44:32 +0900355 /* Front merge */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900356 if (fofs == start_fofs - 1 && blk_addr == start_blkaddr - 1) {
357 fi->ext.fofs--;
358 fi->ext.blk_addr--;
359 fi->ext.len++;
360 goto end_update;
361 }
362
363 /* Back merge */
364 if (fofs == end_fofs + 1 && blk_addr == end_blkaddr + 1) {
365 fi->ext.len++;
366 goto end_update;
367 }
368
369 /* Split the existing extent */
370 if (fi->ext.len > 1 &&
371 fofs >= start_fofs && fofs <= end_fofs) {
372 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
373 fi->ext.len = fofs - start_fofs;
374 } else {
375 fi->ext.fofs = fofs + 1;
376 fi->ext.blk_addr = start_blkaddr +
377 fofs - start_fofs + 1;
378 fi->ext.len -= fofs - start_fofs + 1;
379 }
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900380 } else {
381 need_update = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900382 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900383
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900384 /* Finally, if the extent is very fragmented, let's drop the cache. */
385 if (fi->ext.len < F2FS_MIN_EXTENT_LEN) {
386 fi->ext.len = 0;
387 set_inode_flag(fi, FI_NO_EXTENT);
388 need_update = true;
389 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900390end_update:
391 write_unlock(&fi->ext.ext_lock);
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900392 if (need_update)
393 sync_inode_page(dn);
394 return;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900395}
396
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900397struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900398{
399 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
400 struct address_space *mapping = inode->i_mapping;
401 struct dnode_of_data dn;
402 struct page *page;
403 int err;
404
405 page = find_get_page(mapping, index);
406 if (page && PageUptodate(page))
407 return page;
408 f2fs_put_page(page, 0);
409
410 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900411 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900412 if (err)
413 return ERR_PTR(err);
414 f2fs_put_dnode(&dn);
415
416 if (dn.data_blkaddr == NULL_ADDR)
417 return ERR_PTR(-ENOENT);
418
419 /* By fallocate(), there is no cached page, but with NEW_ADDR */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900420 if (unlikely(dn.data_blkaddr == NEW_ADDR))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900421 return ERR_PTR(-EINVAL);
422
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900423 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900424 if (!page)
425 return ERR_PTR(-ENOMEM);
426
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900427 if (PageUptodate(page)) {
428 unlock_page(page);
429 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900430 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900431
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900432 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr,
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900433 sync ? READ_SYNC : READA);
Chao Yu1069bbf2013-11-28 15:43:43 +0800434 if (err)
435 return ERR_PTR(err);
436
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900437 if (sync) {
438 wait_on_page_locked(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900439 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900440 f2fs_put_page(page, 0);
441 return ERR_PTR(-EIO);
442 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900443 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900444 return page;
445}
446
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900447/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900448 * If it tries to access a hole, return an error.
449 * Because, the callers, functions in dir.c and GC, should be able to know
450 * whether this page exists or not.
451 */
452struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
453{
454 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
455 struct address_space *mapping = inode->i_mapping;
456 struct dnode_of_data dn;
457 struct page *page;
458 int err;
459
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900460repeat:
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900461 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900462 if (!page)
463 return ERR_PTR(-ENOMEM);
464
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900465 set_new_dnode(&dn, inode, NULL, NULL, 0);
466 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
467 if (err) {
468 f2fs_put_page(page, 1);
469 return ERR_PTR(err);
470 }
471 f2fs_put_dnode(&dn);
472
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900473 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900474 f2fs_put_page(page, 1);
475 return ERR_PTR(-ENOENT);
476 }
477
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900478 if (PageUptodate(page))
479 return page;
480
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900481 /*
482 * A new dentry page is allocated but not able to be written, since its
483 * new inode page couldn't be allocated due to -ENOSPC.
484 * In such the case, its blkaddr can be remained as NEW_ADDR.
485 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
486 */
487 if (dn.data_blkaddr == NEW_ADDR) {
488 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
489 SetPageUptodate(page);
490 return page;
491 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900492
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900493 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900494 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900495 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900496
497 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900498 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900499 f2fs_put_page(page, 1);
500 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900501 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900502 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900503 f2fs_put_page(page, 1);
504 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900505 }
506 return page;
507}
508
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900509/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900510 * Caller ensures that this data page is never allocated.
511 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900512 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800513 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
514 * f2fs_unlock_op().
Jaegeuk Kima8865372013-12-27 17:04:17 +0900515 * Note that, ipage is set only by make_empty_dir.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900516 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900517struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900518 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900519{
520 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
521 struct address_space *mapping = inode->i_mapping;
522 struct page *page;
523 struct dnode_of_data dn;
524 int err;
525
Jaegeuk Kima8865372013-12-27 17:04:17 +0900526 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800527 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900528 if (err)
529 return ERR_PTR(err);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900530repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900531 page = grab_cache_page(mapping, index);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900532 if (!page) {
533 err = -ENOMEM;
534 goto put_err;
535 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900536
537 if (PageUptodate(page))
538 return page;
539
540 if (dn.data_blkaddr == NEW_ADDR) {
541 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900542 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900543 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900544 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr,
545 READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900546 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900547 goto put_err;
548
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900549 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900550 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900551 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900552 err = -EIO;
553 goto put_err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900554 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900555 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900556 f2fs_put_page(page, 1);
557 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900558 }
559 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900560
561 if (new_i_size &&
562 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
563 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900564 /* Only the directory inode sets new_i_size */
565 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900566 }
567 return page;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900568
569put_err:
570 f2fs_put_dnode(&dn);
571 return ERR_PTR(err);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900572}
573
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900574static int __allocate_data_block(struct dnode_of_data *dn)
575{
576 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
577 struct f2fs_summary sum;
578 block_t new_blkaddr;
579 struct node_info ni;
580 int type;
581
582 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
583 return -EPERM;
584 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
585 return -ENOSPC;
586
587 __set_data_blkaddr(dn, NEW_ADDR);
588 dn->data_blkaddr = NEW_ADDR;
589
590 get_node_info(sbi, dn->nid, &ni);
591 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
592
593 type = CURSEG_WARM_DATA;
594
595 allocate_data_block(sbi, NULL, NULL_ADDR, &new_blkaddr, &sum, type);
596
597 /* direct IO doesn't use extent cache to maximize the performance */
598 set_inode_flag(F2FS_I(dn->inode), FI_NO_EXTENT);
599 update_extent_cache(new_blkaddr, dn);
600 clear_inode_flag(F2FS_I(dn->inode), FI_NO_EXTENT);
601
602 dn->data_blkaddr = new_blkaddr;
603 return 0;
604}
605
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900606/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800607 * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
608 * If original data blocks are allocated, then give them to blockdev.
609 * Otherwise,
610 * a. preallocate requested block addresses
611 * b. do not use extent cache for better performance
612 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900613 */
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900614static int __get_data_block(struct inode *inode, sector_t iblock,
615 struct buffer_head *bh_result, int create, bool fiemap)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900616{
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900617 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900618 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
619 unsigned maxblocks = bh_result->b_size >> blkbits;
620 struct dnode_of_data dn;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900621 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
622 pgoff_t pgofs, end_offset;
623 int err = 0, ofs = 1;
624 bool allocated = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900625
626 /* Get the page offset from the block offset(iblock) */
627 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
628
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900629 if (check_extent_cache(inode, pgofs, bh_result))
630 goto out;
631
Huang Ying79e35dc2014-07-12 20:10:00 +0800632 if (create) {
633 f2fs_balance_fs(sbi);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900634 f2fs_lock_op(sbi);
Huang Ying79e35dc2014-07-12 20:10:00 +0800635 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900636
637 /* When reading holes, we need its node page */
638 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900639 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900640 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900641 if (err == -ENOENT)
642 err = 0;
643 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900644 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900645 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900646 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900647
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900648 if (dn.data_blkaddr != NULL_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900649 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900650 } else if (create) {
651 err = __allocate_data_block(&dn);
652 if (err)
653 goto put_out;
654 allocated = true;
655 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
656 } else {
657 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900658 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900659
Chao Yu6403eb12014-04-26 19:59:52 +0800660 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900661 bh_result->b_size = (((size_t)1) << blkbits);
662 dn.ofs_in_node++;
663 pgofs++;
664
665get_next:
666 if (dn.ofs_in_node >= end_offset) {
667 if (allocated)
668 sync_inode_page(&dn);
669 allocated = false;
670 f2fs_put_dnode(&dn);
671
672 set_new_dnode(&dn, inode, NULL, NULL, 0);
673 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900674 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900675 if (err == -ENOENT)
676 err = 0;
677 goto unlock_out;
678 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900679 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900680 goto put_out;
681
Chao Yu6403eb12014-04-26 19:59:52 +0800682 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900683 }
684
685 if (maxblocks > (bh_result->b_size >> blkbits)) {
686 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
687 if (blkaddr == NULL_ADDR && create) {
688 err = __allocate_data_block(&dn);
689 if (err)
690 goto sync_out;
691 allocated = true;
692 blkaddr = dn.data_blkaddr;
693 }
694 /* Give more consecutive addresses for the read ahead */
695 if (blkaddr == (bh_result->b_blocknr + ofs)) {
696 ofs++;
697 dn.ofs_in_node++;
698 pgofs++;
699 bh_result->b_size += (((size_t)1) << blkbits);
700 goto get_next;
701 }
702 }
703sync_out:
704 if (allocated)
705 sync_inode_page(&dn);
706put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900707 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900708unlock_out:
709 if (create)
710 f2fs_unlock_op(sbi);
711out:
712 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
713 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900714}
715
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900716static int get_data_block(struct inode *inode, sector_t iblock,
717 struct buffer_head *bh_result, int create)
718{
719 return __get_data_block(inode, iblock, bh_result, create, false);
720}
721
722static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
723 struct buffer_head *bh_result, int create)
724{
725 return __get_data_block(inode, iblock, bh_result, create, true);
726}
727
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900728int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
729 u64 start, u64 len)
730{
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900731 return generic_block_fiemap(inode, fieinfo,
732 start, len, get_data_block_fiemap);
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900733}
734
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900735static int f2fs_read_data_page(struct file *file, struct page *page)
736{
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800737 struct inode *inode = page->mapping->host;
738 int ret;
739
Chao Yuc20e89c2014-05-06 16:53:08 +0800740 trace_f2fs_readpage(page, DATA);
741
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800742 /* If the file has inline data, try to read it directlly */
743 if (f2fs_has_inline_data(inode))
744 ret = f2fs_read_inline_data(inode, page);
745 else
746 ret = mpage_readpage(page, get_data_block);
747
748 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900749}
750
751static int f2fs_read_data_pages(struct file *file,
752 struct address_space *mapping,
753 struct list_head *pages, unsigned nr_pages)
754{
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800755 struct inode *inode = file->f_mapping->host;
756
757 /* If the file has inline data, skip readpages */
758 if (f2fs_has_inline_data(inode))
759 return 0;
760
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900761 return mpage_readpages(mapping, pages, nr_pages, get_data_block);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900762}
763
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900764int do_write_data_page(struct page *page, struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900765{
766 struct inode *inode = page->mapping->host;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900767 block_t old_blkaddr, new_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900768 struct dnode_of_data dn;
769 int err = 0;
770
771 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900772 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900773 if (err)
774 return err;
775
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900776 old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900777
778 /* This page is already truncated */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900779 if (old_blkaddr == NULL_ADDR)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900780 goto out_writepage;
781
782 set_page_writeback(page);
783
784 /*
785 * If current allocation needs SSR,
786 * it had better in-place writes for updated data.
787 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900788 if (unlikely(old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +0800789 !is_cold_data(page) &&
790 need_inplace_update(inode))) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900791 rewrite_data_page(page, old_blkaddr, fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700792 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900793 } else {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900794 write_data_page(page, &dn, &new_blkaddr, fio);
795 update_extent_cache(new_blkaddr, &dn);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700796 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900797 }
798out_writepage:
799 f2fs_put_dnode(&dn);
800 return err;
801}
802
803static int f2fs_write_data_page(struct page *page,
804 struct writeback_control *wbc)
805{
806 struct inode *inode = page->mapping->host;
807 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
808 loff_t i_size = i_size_read(inode);
809 const pgoff_t end_index = ((unsigned long long) i_size)
810 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800811 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900812 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900813 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900814 struct f2fs_io_info fio = {
815 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -0600816 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900817 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900818
Chao Yuecda0de2014-05-06 16:48:26 +0800819 trace_f2fs_writepage(page, DATA);
820
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900821 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900822 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900823
824 /*
825 * If the offset is out-of-range of file size,
826 * this page does not have to be written to disk.
827 */
828 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +0900829 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900830 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900831
832 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900833write:
Jaegeuk Kim8618b882014-02-17 19:29:27 +0900834 if (unlikely(sbi->por_doing))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900835 goto redirty_out;
836
Jaegeuk Kim39936832012-11-22 16:21:29 +0900837 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900838 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900839 err = do_write_data_page(page, &fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +0900840 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900841 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +0900842
843 if (!wbc->for_reclaim)
844 need_balance_fs = true;
845 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +0900846 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900847
Jaegeuk Kim8618b882014-02-17 19:29:27 +0900848 f2fs_lock_op(sbi);
849 if (f2fs_has_inline_data(inode) || f2fs_may_inline(inode))
850 err = f2fs_write_inline_data(inode, page, offset);
851 else
852 err = do_write_data_page(page, &fio);
853 f2fs_unlock_op(sbi);
854done:
855 if (err && err != -ENOENT)
856 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900857
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900858 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900859out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +0900860 inode_dec_dirty_dents(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900861 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900862 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900863 f2fs_balance_fs(sbi);
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +0900864 if (wbc->for_reclaim)
865 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900866 return 0;
867
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900868redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +0900869 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +0900870 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900871}
872
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900873static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
874 void *data)
875{
876 struct address_space *mapping = data;
877 int ret = mapping->a_ops->writepage(page, wbc);
878 mapping_set_error(mapping, ret);
879 return ret;
880}
881
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900882static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900883 struct writeback_control *wbc)
884{
885 struct inode *inode = mapping->host;
886 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900887 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900888 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +0900889 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900890
Chao Yue5748432014-05-06 16:51:24 +0800891 trace_f2fs_writepages(mapping->host, wbc, DATA);
892
P J Pcfb185a2013-04-03 11:38:00 +0900893 /* deal with chardevs and other special file */
894 if (!mapping->a_ops->writepage)
895 return 0;
896
Jaegeuk Kim87d6f892014-03-18 12:40:49 +0900897 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +0900898 get_dirty_dents(inode) < nr_pages_to_skip(sbi, DATA) &&
899 available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +0900900 goto skip_write;
Jaegeuk Kim87d6f892014-03-18 12:40:49 +0900901
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +0900902 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900903
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900904 if (!S_ISDIR(inode->i_mode)) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900905 mutex_lock(&sbi->writepages);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900906 locked = true;
907 }
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900908 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900909 if (locked)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900910 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900911
912 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900913
914 remove_dirty_dir_inode(inode);
915
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +0900916 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900917 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +0900918
919skip_write:
920 wbc->pages_skipped += get_dirty_dents(inode);
921 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900922}
923
Chao Yu3aab8f82014-07-02 13:25:04 +0800924static void f2fs_write_failed(struct address_space *mapping, loff_t to)
925{
926 struct inode *inode = mapping->host;
927
928 if (to > inode->i_size) {
929 truncate_pagecache(inode, inode->i_size);
930 truncate_blocks(inode, inode->i_size);
931 }
932}
933
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900934static int f2fs_write_begin(struct file *file, struct address_space *mapping,
935 loff_t pos, unsigned len, unsigned flags,
936 struct page **pagep, void **fsdata)
937{
938 struct inode *inode = mapping->host;
939 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
940 struct page *page;
941 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
942 struct dnode_of_data dn;
943 int err = 0;
944
Chao Yu62aed042014-05-06 16:46:04 +0800945 trace_f2fs_write_begin(inode, pos, len, flags);
946
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900947 f2fs_balance_fs(sbi);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900948repeat:
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900949 err = f2fs_convert_inline_data(inode, pos + len);
950 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +0800951 goto fail;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900952
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900953 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +0800954 if (!page) {
955 err = -ENOMEM;
956 goto fail;
957 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +0900958
959 /* to avoid latency during memory pressure */
960 unlock_page(page);
961
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900962 *pagep = page;
963
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900964 if (f2fs_has_inline_data(inode) && (pos + len) <= MAX_INLINE_DATA)
965 goto inline_data;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800966
Gu Zhenge4795562013-09-27 18:08:30 +0800967 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900968 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800969 err = f2fs_reserve_block(&dn, index);
Gu Zhenge4795562013-09-27 18:08:30 +0800970 f2fs_unlock_op(sbi);
Huajun Lib6009652013-11-10 23:13:18 +0800971 if (err) {
Jaegeuk Kimd5f66992014-04-30 09:22:45 +0900972 f2fs_put_page(page, 0);
Chao Yu3aab8f82014-07-02 13:25:04 +0800973 goto fail;
Huajun Lib6009652013-11-10 23:13:18 +0800974 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800975inline_data:
Jaegeuk Kimd5f66992014-04-30 09:22:45 +0900976 lock_page(page);
977 if (unlikely(page->mapping != mapping)) {
978 f2fs_put_page(page, 1);
979 goto repeat;
980 }
981
982 f2fs_wait_on_page_writeback(page, DATA);
983
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900984 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
985 return 0;
986
987 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
988 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
989 unsigned end = start + len;
990
991 /* Reading beyond i_size is simple: memset to zero */
992 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900993 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900994 }
995
996 if (dn.data_blkaddr == NEW_ADDR) {
997 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
998 } else {
Chao Yud54c7952014-03-29 15:30:40 +0800999 if (f2fs_has_inline_data(inode)) {
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001000 err = f2fs_read_inline_data(inode, page);
Chao Yud54c7952014-03-29 15:30:40 +08001001 if (err) {
1002 page_cache_release(page);
Chao Yu3aab8f82014-07-02 13:25:04 +08001003 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001004 }
1005 } else {
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001006 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr,
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +09001007 READ_SYNC);
Chao Yud54c7952014-03-29 15:30:40 +08001008 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08001009 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001010 }
1011
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001012 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001013 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001014 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001015 err = -EIO;
1016 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001017 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001018 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001019 f2fs_put_page(page, 1);
1020 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001021 }
1022 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001023out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001024 SetPageUptodate(page);
1025 clear_cold_data(page);
1026 return 0;
Chao Yu3aab8f82014-07-02 13:25:04 +08001027fail:
1028 f2fs_write_failed(mapping, pos + len);
1029 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001030}
1031
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001032static int f2fs_write_end(struct file *file,
1033 struct address_space *mapping,
1034 loff_t pos, unsigned len, unsigned copied,
1035 struct page *page, void *fsdata)
1036{
1037 struct inode *inode = page->mapping->host;
1038
Chao Yudfb2bf32014-05-06 16:47:23 +08001039 trace_f2fs_write_end(inode, pos, len, copied);
1040
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001041 set_page_dirty(page);
1042
1043 if (pos + copied > i_size_read(inode)) {
1044 i_size_write(inode, pos + copied);
1045 mark_inode_dirty(inode);
1046 update_inode_page(inode);
1047 }
1048
Chao Yu75c3c8b2013-11-16 14:15:59 +08001049 f2fs_put_page(page, 1);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001050 return copied;
1051}
1052
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001053static int check_direct_IO(struct inode *inode, int rw,
Al Viro5b46f252014-03-16 18:07:34 -04001054 struct iov_iter *iter, loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001055{
1056 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001057
1058 if (rw == READ)
1059 return 0;
1060
1061 if (offset & blocksize_mask)
1062 return -EINVAL;
1063
Al Viro5b46f252014-03-16 18:07:34 -04001064 if (iov_iter_alignment(iter) & blocksize_mask)
1065 return -EINVAL;
1066
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001067 return 0;
1068}
1069
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001070static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05001071 struct iov_iter *iter, loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001072{
1073 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001074 struct address_space *mapping = file->f_mapping;
1075 struct inode *inode = mapping->host;
1076 size_t count = iov_iter_count(iter);
1077 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001078
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001079 /* Let buffer I/O handle the inline data case. */
1080 if (f2fs_has_inline_data(inode))
1081 return 0;
1082
Al Viro5b46f252014-03-16 18:07:34 -04001083 if (check_direct_IO(inode, rw, iter, offset))
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001084 return 0;
1085
Jaegeuk Kimb6fe5872014-06-04 00:39:42 +09001086 /* clear fsync mark to recover these blocks */
1087 fsync_mark_clear(F2FS_SB(inode->i_sb), inode->i_ino);
1088
Chao Yu70407fa2014-07-31 21:11:22 +08001089 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
1090
Chao Yu3aab8f82014-07-02 13:25:04 +08001091 err = blockdev_direct_IO(rw, iocb, inode, iter, offset, get_data_block);
1092 if (err < 0 && (rw & WRITE))
1093 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001094
1095 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
1096
Chao Yu3aab8f82014-07-02 13:25:04 +08001097 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001098}
1099
Lukas Czernerd47992f2013-05-21 23:17:23 -04001100static void f2fs_invalidate_data_page(struct page *page, unsigned int offset,
1101 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001102{
1103 struct inode *inode = page->mapping->host;
Jaegeuk Kim1fe54f92014-02-07 10:00:06 +09001104 if (PageDirty(page))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001105 inode_dec_dirty_dents(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001106 ClearPagePrivate(page);
1107}
1108
1109static int f2fs_release_data_page(struct page *page, gfp_t wait)
1110{
1111 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001112 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001113}
1114
1115static int f2fs_set_data_page_dirty(struct page *page)
1116{
1117 struct address_space *mapping = page->mapping;
1118 struct inode *inode = mapping->host;
1119
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001120 trace_f2fs_set_page_dirty(page, DATA);
1121
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001122 SetPageUptodate(page);
Jaegeuk Kima18ff062014-01-21 13:32:12 +09001123 mark_inode_dirty(inode);
1124
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001125 if (!PageDirty(page)) {
1126 __set_page_dirty_nobuffers(page);
1127 set_dirty_dir_page(inode, page);
1128 return 1;
1129 }
1130 return 0;
1131}
1132
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001133static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1134{
Chao Yu454ae7e2014-04-22 13:34:01 +08001135 struct inode *inode = mapping->host;
1136
1137 if (f2fs_has_inline_data(inode))
1138 return 0;
1139
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001140 return generic_block_bmap(mapping, block, get_data_block);
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001141}
1142
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001143const struct address_space_operations f2fs_dblock_aops = {
1144 .readpage = f2fs_read_data_page,
1145 .readpages = f2fs_read_data_pages,
1146 .writepage = f2fs_write_data_page,
1147 .writepages = f2fs_write_data_pages,
1148 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001149 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001150 .set_page_dirty = f2fs_set_data_page_dirty,
1151 .invalidatepage = f2fs_invalidate_data_page,
1152 .releasepage = f2fs_release_data_page,
1153 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001154 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001155};