blob: a7eb529257233f09e0b523f771d7d090a70a87e7 [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
Jin Xua5694692013-08-05 20:02:04 +080040 f2fs_wait_on_page_writeback(node_page, NODE, false);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090041
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);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900368 bio_put(bio);
369}
370
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900371/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900372 * Fill the locked page with data located in the block address.
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900373 * Return unlocked page.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900374 */
375int f2fs_readpage(struct f2fs_sb_info *sbi, struct page *page,
376 block_t blk_addr, int type)
377{
378 struct block_device *bdev = sbi->sb->s_bdev;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900379 struct bio *bio;
380
Namjae Jeon848753a2013-04-23 16:38:02 +0900381 trace_f2fs_readpage(page, blk_addr, type);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900382
383 down_read(&sbi->bio_sem);
384
385 /* Allocate a new bio */
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900386 bio = f2fs_bio_alloc(bdev, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900387
388 /* Initialize the bio */
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900389 bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900390 bio->bi_end_io = read_end_io;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900391
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900392 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900393 bio_put(bio);
394 up_read(&sbi->bio_sem);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900395 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900396 return -EFAULT;
397 }
398
399 submit_bio(type, bio);
400 up_read(&sbi->bio_sem);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900401 return 0;
402}
403
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900404/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900405 * This function should be used by the data read flow only where it
406 * does not check the "create" flag that indicates block allocation.
407 * The reason for this special functionality is to exploit VFS readahead
408 * mechanism.
409 */
410static int get_data_block_ro(struct inode *inode, sector_t iblock,
411 struct buffer_head *bh_result, int create)
412{
413 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
414 unsigned maxblocks = bh_result->b_size >> blkbits;
415 struct dnode_of_data dn;
416 pgoff_t pgofs;
417 int err;
418
419 /* Get the page offset from the block offset(iblock) */
420 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
421
Namjae Jeon848753a2013-04-23 16:38:02 +0900422 if (check_extent_cache(inode, pgofs, bh_result)) {
423 trace_f2fs_get_data_block(inode, iblock, bh_result, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900424 return 0;
Namjae Jeon848753a2013-04-23 16:38:02 +0900425 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900426
427 /* When reading holes, we need its node page */
428 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900429 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
Namjae Jeon848753a2013-04-23 16:38:02 +0900430 if (err) {
431 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900432 return (err == -ENOENT) ? 0 : err;
Namjae Jeon848753a2013-04-23 16:38:02 +0900433 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900434
435 /* It does not support data allocation */
436 BUG_ON(create);
437
438 if (dn.data_blkaddr != NEW_ADDR && dn.data_blkaddr != NULL_ADDR) {
439 int i;
440 unsigned int end_offset;
441
442 end_offset = IS_INODE(dn.node_page) ?
443 ADDRS_PER_INODE :
444 ADDRS_PER_BLOCK;
445
446 clear_buffer_new(bh_result);
447
448 /* Give more consecutive addresses for the read ahead */
449 for (i = 0; i < end_offset - dn.ofs_in_node; i++)
450 if (((datablock_addr(dn.node_page,
451 dn.ofs_in_node + i))
452 != (dn.data_blkaddr + i)) || maxblocks == i)
453 break;
454 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
455 bh_result->b_size = (i << blkbits);
456 }
457 f2fs_put_dnode(&dn);
Namjae Jeon848753a2013-04-23 16:38:02 +0900458 trace_f2fs_get_data_block(inode, iblock, bh_result, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900459 return 0;
460}
461
462static int f2fs_read_data_page(struct file *file, struct page *page)
463{
464 return mpage_readpage(page, get_data_block_ro);
465}
466
467static int f2fs_read_data_pages(struct file *file,
468 struct address_space *mapping,
469 struct list_head *pages, unsigned nr_pages)
470{
471 return mpage_readpages(mapping, pages, nr_pages, get_data_block_ro);
472}
473
474int do_write_data_page(struct page *page)
475{
476 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900477 block_t old_blk_addr, new_blk_addr;
478 struct dnode_of_data dn;
479 int err = 0;
480
481 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900482 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900483 if (err)
484 return err;
485
486 old_blk_addr = dn.data_blkaddr;
487
488 /* This page is already truncated */
489 if (old_blk_addr == NULL_ADDR)
490 goto out_writepage;
491
492 set_page_writeback(page);
493
494 /*
495 * If current allocation needs SSR,
496 * it had better in-place writes for updated data.
497 */
Haicheng Lib25958b2013-06-13 16:59:29 +0800498 if (unlikely(old_blk_addr != NEW_ADDR &&
499 !is_cold_data(page) &&
500 need_inplace_update(inode))) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900501 rewrite_data_page(F2FS_SB(inode->i_sb), page,
502 old_blk_addr);
503 } else {
504 write_data_page(inode, page, &dn,
505 old_blk_addr, &new_blk_addr);
506 update_extent_cache(new_blk_addr, &dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900507 }
508out_writepage:
509 f2fs_put_dnode(&dn);
510 return err;
511}
512
513static int f2fs_write_data_page(struct page *page,
514 struct writeback_control *wbc)
515{
516 struct inode *inode = page->mapping->host;
517 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
518 loff_t i_size = i_size_read(inode);
519 const pgoff_t end_index = ((unsigned long long) i_size)
520 >> PAGE_CACHE_SHIFT;
521 unsigned offset;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900522 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900523 int err = 0;
524
525 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900526 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900527
528 /*
529 * If the offset is out-of-range of file size,
530 * this page does not have to be written to disk.
531 */
532 offset = i_size & (PAGE_CACHE_SIZE - 1);
533 if ((page->index >= end_index + 1) || !offset) {
534 if (S_ISDIR(inode->i_mode)) {
535 dec_page_count(sbi, F2FS_DIRTY_DENTS);
536 inode_dec_dirty_dents(inode);
537 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900538 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900539 }
540
541 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900542write:
543 if (sbi->por_doing) {
544 err = AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900545 goto redirty_out;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900546 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900547
Jaegeuk Kim39936832012-11-22 16:21:29 +0900548 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900549 if (S_ISDIR(inode->i_mode)) {
550 dec_page_count(sbi, F2FS_DIRTY_DENTS);
551 inode_dec_dirty_dents(inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900552 err = do_write_data_page(page);
553 } else {
554 int ilock = mutex_lock_op(sbi);
555 err = do_write_data_page(page);
556 mutex_unlock_op(sbi, ilock);
557 need_balance_fs = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900558 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900559 if (err == -ENOENT)
560 goto out;
561 else if (err)
562 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900563
564 if (wbc->for_reclaim)
565 f2fs_submit_bio(sbi, DATA, true);
566
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900567 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900568out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900569 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900570 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900571 f2fs_balance_fs(sbi);
572 return 0;
573
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900574redirty_out:
575 wbc->pages_skipped++;
576 set_page_dirty(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900577 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900578}
579
580#define MAX_DESIRED_PAGES_WP 4096
581
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900582static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
583 void *data)
584{
585 struct address_space *mapping = data;
586 int ret = mapping->a_ops->writepage(page, wbc);
587 mapping_set_error(mapping, ret);
588 return ret;
589}
590
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900591static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900592 struct writeback_control *wbc)
593{
594 struct inode *inode = mapping->host;
595 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900596 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900597 int ret;
598 long excess_nrtw = 0, desired_nrtw;
599
P J Pcfb185a2013-04-03 11:38:00 +0900600 /* deal with chardevs and other special file */
601 if (!mapping->a_ops->writepage)
602 return 0;
603
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900604 if (wbc->nr_to_write < MAX_DESIRED_PAGES_WP) {
605 desired_nrtw = MAX_DESIRED_PAGES_WP;
606 excess_nrtw = desired_nrtw - wbc->nr_to_write;
607 wbc->nr_to_write = desired_nrtw;
608 }
609
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900610 if (!S_ISDIR(inode->i_mode)) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900611 mutex_lock(&sbi->writepages);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900612 locked = true;
613 }
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900614 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900615 if (locked)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900616 mutex_unlock(&sbi->writepages);
617 f2fs_submit_bio(sbi, DATA, (wbc->sync_mode == WB_SYNC_ALL));
618
619 remove_dirty_dir_inode(inode);
620
621 wbc->nr_to_write -= excess_nrtw;
622 return ret;
623}
624
625static int f2fs_write_begin(struct file *file, struct address_space *mapping,
626 loff_t pos, unsigned len, unsigned flags,
627 struct page **pagep, void **fsdata)
628{
629 struct inode *inode = mapping->host;
630 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
631 struct page *page;
632 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
633 struct dnode_of_data dn;
634 int err = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900635 int ilock;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900636
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900637 f2fs_balance_fs(sbi);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900638repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900639 page = grab_cache_page_write_begin(mapping, index, flags);
640 if (!page)
641 return -ENOMEM;
642 *pagep = page;
643
Jaegeuk Kim39936832012-11-22 16:21:29 +0900644 ilock = mutex_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900645
646 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900647 err = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900648 if (err)
649 goto err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900650
Jaegeuk Kim39936832012-11-22 16:21:29 +0900651 if (dn.data_blkaddr == NULL_ADDR)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900652 err = reserve_new_block(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900653
Jaegeuk Kim39936832012-11-22 16:21:29 +0900654 f2fs_put_dnode(&dn);
655 if (err)
656 goto err;
657
658 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900659
660 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
661 return 0;
662
663 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
664 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
665 unsigned end = start + len;
666
667 /* Reading beyond i_size is simple: memset to zero */
668 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900669 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900670 }
671
672 if (dn.data_blkaddr == NEW_ADDR) {
673 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
674 } else {
675 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900676 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900677 return err;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900678 lock_page(page);
679 if (!PageUptodate(page)) {
680 f2fs_put_page(page, 1);
681 return -EIO;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900682 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900683 if (page->mapping != mapping) {
684 f2fs_put_page(page, 1);
685 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900686 }
687 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900688out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900689 SetPageUptodate(page);
690 clear_cold_data(page);
691 return 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900692
693err:
694 mutex_unlock_op(sbi, ilock);
695 f2fs_put_page(page, 1);
696 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900697}
698
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +0900699static int f2fs_write_end(struct file *file,
700 struct address_space *mapping,
701 loff_t pos, unsigned len, unsigned copied,
702 struct page *page, void *fsdata)
703{
704 struct inode *inode = page->mapping->host;
705
706 SetPageUptodate(page);
707 set_page_dirty(page);
708
709 if (pos + copied > i_size_read(inode)) {
710 i_size_write(inode, pos + copied);
711 mark_inode_dirty(inode);
712 update_inode_page(inode);
713 }
714
715 unlock_page(page);
716 page_cache_release(page);
717 return copied;
718}
719
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900720static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
721 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
722{
723 struct file *file = iocb->ki_filp;
724 struct inode *inode = file->f_mapping->host;
725
726 if (rw == WRITE)
727 return 0;
728
729 /* Needs synchronization with the cleaner */
730 return blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
731 get_data_block_ro);
732}
733
Lukas Czernerd47992f2013-05-21 23:17:23 -0400734static void f2fs_invalidate_data_page(struct page *page, unsigned int offset,
735 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900736{
737 struct inode *inode = page->mapping->host;
738 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
739 if (S_ISDIR(inode->i_mode) && PageDirty(page)) {
740 dec_page_count(sbi, F2FS_DIRTY_DENTS);
741 inode_dec_dirty_dents(inode);
742 }
743 ClearPagePrivate(page);
744}
745
746static int f2fs_release_data_page(struct page *page, gfp_t wait)
747{
748 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +0900749 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900750}
751
752static int f2fs_set_data_page_dirty(struct page *page)
753{
754 struct address_space *mapping = page->mapping;
755 struct inode *inode = mapping->host;
756
757 SetPageUptodate(page);
758 if (!PageDirty(page)) {
759 __set_page_dirty_nobuffers(page);
760 set_dirty_dir_page(inode, page);
761 return 1;
762 }
763 return 0;
764}
765
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +0900766static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
767{
768 return generic_block_bmap(mapping, block, get_data_block_ro);
769}
770
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900771const struct address_space_operations f2fs_dblock_aops = {
772 .readpage = f2fs_read_data_page,
773 .readpages = f2fs_read_data_pages,
774 .writepage = f2fs_write_data_page,
775 .writepages = f2fs_write_data_pages,
776 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +0900777 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900778 .set_page_dirty = f2fs_set_data_page_dirty,
779 .invalidatepage = f2fs_invalidate_data_page,
780 .releasepage = f2fs_release_data_page,
781 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +0900782 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900783};