blob: c73c394c3d8ca5a0929bb388fbcaeff5ae5f7990 [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 Kim0a8165d2012-11-29 13:28:09 +090027/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090028 * Lock ordering for the change of data block address:
29 * ->data_page
30 * ->node_page
31 * update block addresses in the node page
32 */
33static void __set_data_blkaddr(struct dnode_of_data *dn, block_t new_addr)
34{
35 struct f2fs_node *rn;
36 __le32 *addr_array;
37 struct page *node_page = dn->node_page;
38 unsigned int ofs_in_node = dn->ofs_in_node;
39
40 wait_on_page_writeback(node_page);
41
Gu Zheng45590712013-07-15 17:57:38 +080042 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090043
44 /* Get physical address of data block */
45 addr_array = blkaddr_in_node(rn);
46 addr_array[ofs_in_node] = cpu_to_le32(new_addr);
47 set_page_dirty(node_page);
48}
49
50int reserve_new_block(struct dnode_of_data *dn)
51{
52 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
53
54 if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))
55 return -EPERM;
56 if (!inc_valid_block_count(sbi, dn->inode, 1))
57 return -ENOSPC;
58
Namjae Jeonc01e2852013-04-23 17:00:52 +090059 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
60
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090061 __set_data_blkaddr(dn, NEW_ADDR);
62 dn->data_blkaddr = NEW_ADDR;
63 sync_inode_page(dn);
64 return 0;
65}
66
67static int check_extent_cache(struct inode *inode, pgoff_t pgofs,
68 struct buffer_head *bh_result)
69{
70 struct f2fs_inode_info *fi = F2FS_I(inode);
Namjae Jeon35b09d82013-05-23 22:57:53 +090071#ifdef CONFIG_F2FS_STAT_FS
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090072 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Namjae Jeon35b09d82013-05-23 22:57:53 +090073#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090074 pgoff_t start_fofs, end_fofs;
75 block_t start_blkaddr;
76
77 read_lock(&fi->ext.ext_lock);
78 if (fi->ext.len == 0) {
79 read_unlock(&fi->ext.ext_lock);
80 return 0;
81 }
82
Namjae Jeon35b09d82013-05-23 22:57:53 +090083#ifdef CONFIG_F2FS_STAT_FS
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090084 sbi->total_hit_ext++;
Namjae Jeon35b09d82013-05-23 22:57:53 +090085#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090086 start_fofs = fi->ext.fofs;
87 end_fofs = fi->ext.fofs + fi->ext.len - 1;
88 start_blkaddr = fi->ext.blk_addr;
89
90 if (pgofs >= start_fofs && pgofs <= end_fofs) {
91 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
92 size_t count;
93
94 clear_buffer_new(bh_result);
95 map_bh(bh_result, inode->i_sb,
96 start_blkaddr + pgofs - start_fofs);
97 count = end_fofs - pgofs + 1;
98 if (count < (UINT_MAX >> blkbits))
99 bh_result->b_size = (count << blkbits);
100 else
101 bh_result->b_size = UINT_MAX;
102
Namjae Jeon35b09d82013-05-23 22:57:53 +0900103#ifdef CONFIG_F2FS_STAT_FS
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900104 sbi->read_hit_ext++;
Namjae Jeon35b09d82013-05-23 22:57:53 +0900105#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900106 read_unlock(&fi->ext.ext_lock);
107 return 1;
108 }
109 read_unlock(&fi->ext.ext_lock);
110 return 0;
111}
112
113void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn)
114{
115 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
116 pgoff_t fofs, start_fofs, end_fofs;
117 block_t start_blkaddr, end_blkaddr;
118
119 BUG_ON(blk_addr == NEW_ADDR);
120 fofs = start_bidx_of_node(ofs_of_node(dn->node_page)) + dn->ofs_in_node;
121
122 /* Update the page address in the parent node */
123 __set_data_blkaddr(dn, blk_addr);
124
125 write_lock(&fi->ext.ext_lock);
126
127 start_fofs = fi->ext.fofs;
128 end_fofs = fi->ext.fofs + fi->ext.len - 1;
129 start_blkaddr = fi->ext.blk_addr;
130 end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1;
131
132 /* Drop and initialize the matched extent */
133 if (fi->ext.len == 1 && fofs == start_fofs)
134 fi->ext.len = 0;
135
136 /* Initial extent */
137 if (fi->ext.len == 0) {
138 if (blk_addr != NULL_ADDR) {
139 fi->ext.fofs = fofs;
140 fi->ext.blk_addr = blk_addr;
141 fi->ext.len = 1;
142 }
143 goto end_update;
144 }
145
Namjae Jeon6224da82013-04-06 14:44:32 +0900146 /* Front merge */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900147 if (fofs == start_fofs - 1 && blk_addr == start_blkaddr - 1) {
148 fi->ext.fofs--;
149 fi->ext.blk_addr--;
150 fi->ext.len++;
151 goto end_update;
152 }
153
154 /* Back merge */
155 if (fofs == end_fofs + 1 && blk_addr == end_blkaddr + 1) {
156 fi->ext.len++;
157 goto end_update;
158 }
159
160 /* Split the existing extent */
161 if (fi->ext.len > 1 &&
162 fofs >= start_fofs && fofs <= end_fofs) {
163 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
164 fi->ext.len = fofs - start_fofs;
165 } else {
166 fi->ext.fofs = fofs + 1;
167 fi->ext.blk_addr = start_blkaddr +
168 fofs - start_fofs + 1;
169 fi->ext.len -= fofs - start_fofs + 1;
170 }
171 goto end_update;
172 }
173 write_unlock(&fi->ext.ext_lock);
174 return;
175
176end_update:
177 write_unlock(&fi->ext.ext_lock);
178 sync_inode_page(dn);
179 return;
180}
181
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900182struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900183{
184 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
185 struct address_space *mapping = inode->i_mapping;
186 struct dnode_of_data dn;
187 struct page *page;
188 int err;
189
190 page = find_get_page(mapping, index);
191 if (page && PageUptodate(page))
192 return page;
193 f2fs_put_page(page, 0);
194
195 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900196 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900197 if (err)
198 return ERR_PTR(err);
199 f2fs_put_dnode(&dn);
200
201 if (dn.data_blkaddr == NULL_ADDR)
202 return ERR_PTR(-ENOENT);
203
204 /* By fallocate(), there is no cached page, but with NEW_ADDR */
205 if (dn.data_blkaddr == NEW_ADDR)
206 return ERR_PTR(-EINVAL);
207
Jaegeuk Kim6f85b352013-05-20 16:15:22 +0900208 page = grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900209 if (!page)
210 return ERR_PTR(-ENOMEM);
211
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900212 if (PageUptodate(page)) {
213 unlock_page(page);
214 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900215 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900216
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900217 err = f2fs_readpage(sbi, page, dn.data_blkaddr,
218 sync ? READ_SYNC : READA);
219 if (sync) {
220 wait_on_page_locked(page);
221 if (!PageUptodate(page)) {
222 f2fs_put_page(page, 0);
223 return ERR_PTR(-EIO);
224 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900225 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900226 return page;
227}
228
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900229/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900230 * If it tries to access a hole, return an error.
231 * Because, the callers, functions in dir.c and GC, should be able to know
232 * whether this page exists or not.
233 */
234struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
235{
236 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
237 struct address_space *mapping = inode->i_mapping;
238 struct dnode_of_data dn;
239 struct page *page;
240 int err;
241
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900242repeat:
Jaegeuk Kim6f85b352013-05-20 16:15:22 +0900243 page = grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900244 if (!page)
245 return ERR_PTR(-ENOMEM);
246
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900247 set_new_dnode(&dn, inode, NULL, NULL, 0);
248 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
249 if (err) {
250 f2fs_put_page(page, 1);
251 return ERR_PTR(err);
252 }
253 f2fs_put_dnode(&dn);
254
255 if (dn.data_blkaddr == NULL_ADDR) {
256 f2fs_put_page(page, 1);
257 return ERR_PTR(-ENOENT);
258 }
259
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900260 if (PageUptodate(page))
261 return page;
262
263 BUG_ON(dn.data_blkaddr == NEW_ADDR);
264 BUG_ON(dn.data_blkaddr == NULL_ADDR);
265
266 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900267 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900268 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900269
270 lock_page(page);
271 if (!PageUptodate(page)) {
272 f2fs_put_page(page, 1);
273 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900274 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900275 if (page->mapping != mapping) {
276 f2fs_put_page(page, 1);
277 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900278 }
279 return page;
280}
281
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900282/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900283 * Caller ensures that this data page is never allocated.
284 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900285 *
286 * Also, caller should grab and release a mutex by calling mutex_lock_op() and
287 * mutex_unlock_op().
Jaegeuk Kim44a83ff2013-05-20 10:10:29 +0900288 * Note that, npage is set only by make_empty_dir.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900289 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900290struct page *get_new_data_page(struct inode *inode,
291 struct page *npage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900292{
293 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
294 struct address_space *mapping = inode->i_mapping;
295 struct page *page;
296 struct dnode_of_data dn;
297 int err;
298
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900299 set_new_dnode(&dn, inode, npage, npage, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900300 err = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900301 if (err)
302 return ERR_PTR(err);
303
304 if (dn.data_blkaddr == NULL_ADDR) {
305 if (reserve_new_block(&dn)) {
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900306 if (!npage)
307 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900308 return ERR_PTR(-ENOSPC);
309 }
310 }
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900311 if (!npage)
312 f2fs_put_dnode(&dn);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900313repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900314 page = grab_cache_page(mapping, index);
315 if (!page)
316 return ERR_PTR(-ENOMEM);
317
318 if (PageUptodate(page))
319 return page;
320
321 if (dn.data_blkaddr == NEW_ADDR) {
322 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900323 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900324 } else {
325 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900326 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900327 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900328 lock_page(page);
329 if (!PageUptodate(page)) {
330 f2fs_put_page(page, 1);
331 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900332 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900333 if (page->mapping != mapping) {
334 f2fs_put_page(page, 1);
335 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900336 }
337 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900338
339 if (new_i_size &&
340 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
341 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900342 /* Only the directory inode sets new_i_size */
343 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900344 mark_inode_dirty_sync(inode);
345 }
346 return page;
347}
348
349static void read_end_io(struct bio *bio, int err)
350{
351 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
352 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
353
354 do {
355 struct page *page = bvec->bv_page;
356
357 if (--bvec >= bio->bi_io_vec)
358 prefetchw(&bvec->bv_page->flags);
359
360 if (uptodate) {
361 SetPageUptodate(page);
362 } else {
363 ClearPageUptodate(page);
364 SetPageError(page);
365 }
366 unlock_page(page);
367 } while (bvec >= bio->bi_io_vec);
368 kfree(bio->bi_private);
369 bio_put(bio);
370}
371
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900372/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900373 * Fill the locked page with data located in the block address.
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900374 * Return unlocked page.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900375 */
376int f2fs_readpage(struct f2fs_sb_info *sbi, struct page *page,
377 block_t blk_addr, int type)
378{
379 struct block_device *bdev = sbi->sb->s_bdev;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900380 struct bio *bio;
381
Namjae Jeon848753a2013-04-23 16:38:02 +0900382 trace_f2fs_readpage(page, blk_addr, type);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900383
384 down_read(&sbi->bio_sem);
385
386 /* Allocate a new bio */
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900387 bio = f2fs_bio_alloc(bdev, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900388
389 /* Initialize the bio */
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900390 bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900391 bio->bi_end_io = read_end_io;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900392
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900393 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
394 kfree(bio->bi_private);
395 bio_put(bio);
396 up_read(&sbi->bio_sem);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900397 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900398 return -EFAULT;
399 }
400
401 submit_bio(type, bio);
402 up_read(&sbi->bio_sem);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900403 return 0;
404}
405
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900406/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900407 * This function should be used by the data read flow only where it
408 * does not check the "create" flag that indicates block allocation.
409 * The reason for this special functionality is to exploit VFS readahead
410 * mechanism.
411 */
412static int get_data_block_ro(struct inode *inode, sector_t iblock,
413 struct buffer_head *bh_result, int create)
414{
415 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
416 unsigned maxblocks = bh_result->b_size >> blkbits;
417 struct dnode_of_data dn;
418 pgoff_t pgofs;
419 int err;
420
421 /* Get the page offset from the block offset(iblock) */
422 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
423
Namjae Jeon848753a2013-04-23 16:38:02 +0900424 if (check_extent_cache(inode, pgofs, bh_result)) {
425 trace_f2fs_get_data_block(inode, iblock, bh_result, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900426 return 0;
Namjae Jeon848753a2013-04-23 16:38:02 +0900427 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900428
429 /* When reading holes, we need its node page */
430 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900431 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
Namjae Jeon848753a2013-04-23 16:38:02 +0900432 if (err) {
433 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900434 return (err == -ENOENT) ? 0 : err;
Namjae Jeon848753a2013-04-23 16:38:02 +0900435 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900436
437 /* It does not support data allocation */
438 BUG_ON(create);
439
440 if (dn.data_blkaddr != NEW_ADDR && dn.data_blkaddr != NULL_ADDR) {
441 int i;
442 unsigned int end_offset;
443
444 end_offset = IS_INODE(dn.node_page) ?
445 ADDRS_PER_INODE :
446 ADDRS_PER_BLOCK;
447
448 clear_buffer_new(bh_result);
449
450 /* Give more consecutive addresses for the read ahead */
451 for (i = 0; i < end_offset - dn.ofs_in_node; i++)
452 if (((datablock_addr(dn.node_page,
453 dn.ofs_in_node + i))
454 != (dn.data_blkaddr + i)) || maxblocks == i)
455 break;
456 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
457 bh_result->b_size = (i << blkbits);
458 }
459 f2fs_put_dnode(&dn);
Namjae Jeon848753a2013-04-23 16:38:02 +0900460 trace_f2fs_get_data_block(inode, iblock, bh_result, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900461 return 0;
462}
463
464static int f2fs_read_data_page(struct file *file, struct page *page)
465{
466 return mpage_readpage(page, get_data_block_ro);
467}
468
469static int f2fs_read_data_pages(struct file *file,
470 struct address_space *mapping,
471 struct list_head *pages, unsigned nr_pages)
472{
473 return mpage_readpages(mapping, pages, nr_pages, get_data_block_ro);
474}
475
476int do_write_data_page(struct page *page)
477{
478 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900479 block_t old_blk_addr, new_blk_addr;
480 struct dnode_of_data dn;
481 int err = 0;
482
483 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900484 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900485 if (err)
486 return err;
487
488 old_blk_addr = dn.data_blkaddr;
489
490 /* This page is already truncated */
491 if (old_blk_addr == NULL_ADDR)
492 goto out_writepage;
493
494 set_page_writeback(page);
495
496 /*
497 * If current allocation needs SSR,
498 * it had better in-place writes for updated data.
499 */
Haicheng Lib25958b2013-06-13 16:59:29 +0800500 if (unlikely(old_blk_addr != NEW_ADDR &&
501 !is_cold_data(page) &&
502 need_inplace_update(inode))) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900503 rewrite_data_page(F2FS_SB(inode->i_sb), page,
504 old_blk_addr);
505 } else {
506 write_data_page(inode, page, &dn,
507 old_blk_addr, &new_blk_addr);
508 update_extent_cache(new_blk_addr, &dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900509 }
510out_writepage:
511 f2fs_put_dnode(&dn);
512 return err;
513}
514
515static int f2fs_write_data_page(struct page *page,
516 struct writeback_control *wbc)
517{
518 struct inode *inode = page->mapping->host;
519 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
520 loff_t i_size = i_size_read(inode);
521 const pgoff_t end_index = ((unsigned long long) i_size)
522 >> PAGE_CACHE_SHIFT;
523 unsigned offset;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900524 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900525 int err = 0;
526
527 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900528 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900529
530 /*
531 * If the offset is out-of-range of file size,
532 * this page does not have to be written to disk.
533 */
534 offset = i_size & (PAGE_CACHE_SIZE - 1);
535 if ((page->index >= end_index + 1) || !offset) {
536 if (S_ISDIR(inode->i_mode)) {
537 dec_page_count(sbi, F2FS_DIRTY_DENTS);
538 inode_dec_dirty_dents(inode);
539 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900540 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900541 }
542
543 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900544write:
545 if (sbi->por_doing) {
546 err = AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900547 goto redirty_out;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900548 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900549
Jaegeuk Kim39936832012-11-22 16:21:29 +0900550 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900551 if (S_ISDIR(inode->i_mode)) {
552 dec_page_count(sbi, F2FS_DIRTY_DENTS);
553 inode_dec_dirty_dents(inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900554 err = do_write_data_page(page);
555 } else {
556 int ilock = mutex_lock_op(sbi);
557 err = do_write_data_page(page);
558 mutex_unlock_op(sbi, ilock);
559 need_balance_fs = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900560 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900561 if (err == -ENOENT)
562 goto out;
563 else if (err)
564 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900565
566 if (wbc->for_reclaim)
567 f2fs_submit_bio(sbi, DATA, true);
568
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900569 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900570out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900571 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900572 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900573 f2fs_balance_fs(sbi);
574 return 0;
575
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900576redirty_out:
577 wbc->pages_skipped++;
578 set_page_dirty(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900579 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900580}
581
582#define MAX_DESIRED_PAGES_WP 4096
583
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900584static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
585 void *data)
586{
587 struct address_space *mapping = data;
588 int ret = mapping->a_ops->writepage(page, wbc);
589 mapping_set_error(mapping, ret);
590 return ret;
591}
592
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900593static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900594 struct writeback_control *wbc)
595{
596 struct inode *inode = mapping->host;
597 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900598 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900599 int ret;
600 long excess_nrtw = 0, desired_nrtw;
601
P J Pcfb185a2013-04-03 11:38:00 +0900602 /* deal with chardevs and other special file */
603 if (!mapping->a_ops->writepage)
604 return 0;
605
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900606 if (wbc->nr_to_write < MAX_DESIRED_PAGES_WP) {
607 desired_nrtw = MAX_DESIRED_PAGES_WP;
608 excess_nrtw = desired_nrtw - wbc->nr_to_write;
609 wbc->nr_to_write = desired_nrtw;
610 }
611
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900612 if (!S_ISDIR(inode->i_mode)) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900613 mutex_lock(&sbi->writepages);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900614 locked = true;
615 }
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900616 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900617 if (locked)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900618 mutex_unlock(&sbi->writepages);
619 f2fs_submit_bio(sbi, DATA, (wbc->sync_mode == WB_SYNC_ALL));
620
621 remove_dirty_dir_inode(inode);
622
623 wbc->nr_to_write -= excess_nrtw;
624 return ret;
625}
626
627static int f2fs_write_begin(struct file *file, struct address_space *mapping,
628 loff_t pos, unsigned len, unsigned flags,
629 struct page **pagep, void **fsdata)
630{
631 struct inode *inode = mapping->host;
632 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
633 struct page *page;
634 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
635 struct dnode_of_data dn;
636 int err = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900637 int ilock;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900638
639 /* for nobh_write_end */
640 *fsdata = NULL;
641
642 f2fs_balance_fs(sbi);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900643repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900644 page = grab_cache_page_write_begin(mapping, index, flags);
645 if (!page)
646 return -ENOMEM;
647 *pagep = page;
648
Jaegeuk Kim39936832012-11-22 16:21:29 +0900649 ilock = mutex_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900650
651 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900652 err = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900653 if (err)
654 goto err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900655
Jaegeuk Kim39936832012-11-22 16:21:29 +0900656 if (dn.data_blkaddr == NULL_ADDR)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900657 err = reserve_new_block(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900658
Jaegeuk Kim39936832012-11-22 16:21:29 +0900659 f2fs_put_dnode(&dn);
660 if (err)
661 goto err;
662
663 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900664
665 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
666 return 0;
667
668 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
669 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
670 unsigned end = start + len;
671
672 /* Reading beyond i_size is simple: memset to zero */
673 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900674 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900675 }
676
677 if (dn.data_blkaddr == NEW_ADDR) {
678 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
679 } else {
680 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900681 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900682 return err;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900683 lock_page(page);
684 if (!PageUptodate(page)) {
685 f2fs_put_page(page, 1);
686 return -EIO;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900687 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900688 if (page->mapping != mapping) {
689 f2fs_put_page(page, 1);
690 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900691 }
692 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900693out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900694 SetPageUptodate(page);
695 clear_cold_data(page);
696 return 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900697
698err:
699 mutex_unlock_op(sbi, ilock);
700 f2fs_put_page(page, 1);
701 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900702}
703
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +0900704static int f2fs_write_end(struct file *file,
705 struct address_space *mapping,
706 loff_t pos, unsigned len, unsigned copied,
707 struct page *page, void *fsdata)
708{
709 struct inode *inode = page->mapping->host;
710
711 SetPageUptodate(page);
712 set_page_dirty(page);
713
714 if (pos + copied > i_size_read(inode)) {
715 i_size_write(inode, pos + copied);
716 mark_inode_dirty(inode);
717 update_inode_page(inode);
718 }
719
720 unlock_page(page);
721 page_cache_release(page);
722 return copied;
723}
724
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900725static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
726 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
727{
728 struct file *file = iocb->ki_filp;
729 struct inode *inode = file->f_mapping->host;
730
731 if (rw == WRITE)
732 return 0;
733
734 /* Needs synchronization with the cleaner */
735 return blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
736 get_data_block_ro);
737}
738
Lukas Czernerd47992f2013-05-21 23:17:23 -0400739static void f2fs_invalidate_data_page(struct page *page, unsigned int offset,
740 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900741{
742 struct inode *inode = page->mapping->host;
743 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
744 if (S_ISDIR(inode->i_mode) && PageDirty(page)) {
745 dec_page_count(sbi, F2FS_DIRTY_DENTS);
746 inode_dec_dirty_dents(inode);
747 }
748 ClearPagePrivate(page);
749}
750
751static int f2fs_release_data_page(struct page *page, gfp_t wait)
752{
753 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +0900754 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900755}
756
757static int f2fs_set_data_page_dirty(struct page *page)
758{
759 struct address_space *mapping = page->mapping;
760 struct inode *inode = mapping->host;
761
762 SetPageUptodate(page);
763 if (!PageDirty(page)) {
764 __set_page_dirty_nobuffers(page);
765 set_dirty_dir_page(inode, page);
766 return 1;
767 }
768 return 0;
769}
770
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +0900771static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
772{
773 return generic_block_bmap(mapping, block, get_data_block_ro);
774}
775
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900776const struct address_space_operations f2fs_dblock_aops = {
777 .readpage = f2fs_read_data_page,
778 .readpages = f2fs_read_data_pages,
779 .writepage = f2fs_write_data_page,
780 .writepages = f2fs_write_data_pages,
781 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +0900782 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900783 .set_page_dirty = f2fs_set_data_page_dirty,
784 .invalidatepage = f2fs_invalidate_data_page,
785 .releasepage = f2fs_release_data_page,
786 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +0900787 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900788};