blob: 05fb5c6077b8c83a9b0143f5cacbfc09502c86fa [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
42 rn = (struct f2fs_node *)page_address(node_page);
43
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);
71 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
72 pgoff_t start_fofs, end_fofs;
73 block_t start_blkaddr;
74
75 read_lock(&fi->ext.ext_lock);
76 if (fi->ext.len == 0) {
77 read_unlock(&fi->ext.ext_lock);
78 return 0;
79 }
80
81 sbi->total_hit_ext++;
82 start_fofs = fi->ext.fofs;
83 end_fofs = fi->ext.fofs + fi->ext.len - 1;
84 start_blkaddr = fi->ext.blk_addr;
85
86 if (pgofs >= start_fofs && pgofs <= end_fofs) {
87 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
88 size_t count;
89
90 clear_buffer_new(bh_result);
91 map_bh(bh_result, inode->i_sb,
92 start_blkaddr + pgofs - start_fofs);
93 count = end_fofs - pgofs + 1;
94 if (count < (UINT_MAX >> blkbits))
95 bh_result->b_size = (count << blkbits);
96 else
97 bh_result->b_size = UINT_MAX;
98
99 sbi->read_hit_ext++;
100 read_unlock(&fi->ext.ext_lock);
101 return 1;
102 }
103 read_unlock(&fi->ext.ext_lock);
104 return 0;
105}
106
107void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn)
108{
109 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
110 pgoff_t fofs, start_fofs, end_fofs;
111 block_t start_blkaddr, end_blkaddr;
112
113 BUG_ON(blk_addr == NEW_ADDR);
114 fofs = start_bidx_of_node(ofs_of_node(dn->node_page)) + dn->ofs_in_node;
115
116 /* Update the page address in the parent node */
117 __set_data_blkaddr(dn, blk_addr);
118
119 write_lock(&fi->ext.ext_lock);
120
121 start_fofs = fi->ext.fofs;
122 end_fofs = fi->ext.fofs + fi->ext.len - 1;
123 start_blkaddr = fi->ext.blk_addr;
124 end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1;
125
126 /* Drop and initialize the matched extent */
127 if (fi->ext.len == 1 && fofs == start_fofs)
128 fi->ext.len = 0;
129
130 /* Initial extent */
131 if (fi->ext.len == 0) {
132 if (blk_addr != NULL_ADDR) {
133 fi->ext.fofs = fofs;
134 fi->ext.blk_addr = blk_addr;
135 fi->ext.len = 1;
136 }
137 goto end_update;
138 }
139
Namjae Jeon6224da82013-04-06 14:44:32 +0900140 /* Front merge */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900141 if (fofs == start_fofs - 1 && blk_addr == start_blkaddr - 1) {
142 fi->ext.fofs--;
143 fi->ext.blk_addr--;
144 fi->ext.len++;
145 goto end_update;
146 }
147
148 /* Back merge */
149 if (fofs == end_fofs + 1 && blk_addr == end_blkaddr + 1) {
150 fi->ext.len++;
151 goto end_update;
152 }
153
154 /* Split the existing extent */
155 if (fi->ext.len > 1 &&
156 fofs >= start_fofs && fofs <= end_fofs) {
157 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
158 fi->ext.len = fofs - start_fofs;
159 } else {
160 fi->ext.fofs = fofs + 1;
161 fi->ext.blk_addr = start_blkaddr +
162 fofs - start_fofs + 1;
163 fi->ext.len -= fofs - start_fofs + 1;
164 }
165 goto end_update;
166 }
167 write_unlock(&fi->ext.ext_lock);
168 return;
169
170end_update:
171 write_unlock(&fi->ext.ext_lock);
172 sync_inode_page(dn);
173 return;
174}
175
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900176struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900177{
178 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
179 struct address_space *mapping = inode->i_mapping;
180 struct dnode_of_data dn;
181 struct page *page;
182 int err;
183
184 page = find_get_page(mapping, index);
185 if (page && PageUptodate(page))
186 return page;
187 f2fs_put_page(page, 0);
188
189 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900190 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900191 if (err)
192 return ERR_PTR(err);
193 f2fs_put_dnode(&dn);
194
195 if (dn.data_blkaddr == NULL_ADDR)
196 return ERR_PTR(-ENOENT);
197
198 /* By fallocate(), there is no cached page, but with NEW_ADDR */
199 if (dn.data_blkaddr == NEW_ADDR)
200 return ERR_PTR(-EINVAL);
201
202 page = grab_cache_page(mapping, index);
203 if (!page)
204 return ERR_PTR(-ENOMEM);
205
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900206 if (PageUptodate(page)) {
207 unlock_page(page);
208 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900209 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900210
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900211 err = f2fs_readpage(sbi, page, dn.data_blkaddr,
212 sync ? READ_SYNC : READA);
213 if (sync) {
214 wait_on_page_locked(page);
215 if (!PageUptodate(page)) {
216 f2fs_put_page(page, 0);
217 return ERR_PTR(-EIO);
218 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900219 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900220 return page;
221}
222
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900223/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900224 * If it tries to access a hole, return an error.
225 * Because, the callers, functions in dir.c and GC, should be able to know
226 * whether this page exists or not.
227 */
228struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
229{
230 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
231 struct address_space *mapping = inode->i_mapping;
232 struct dnode_of_data dn;
233 struct page *page;
234 int err;
235
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900236repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900237 page = grab_cache_page(mapping, index);
238 if (!page)
239 return ERR_PTR(-ENOMEM);
240
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900241 set_new_dnode(&dn, inode, NULL, NULL, 0);
242 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
243 if (err) {
244 f2fs_put_page(page, 1);
245 return ERR_PTR(err);
246 }
247 f2fs_put_dnode(&dn);
248
249 if (dn.data_blkaddr == NULL_ADDR) {
250 f2fs_put_page(page, 1);
251 return ERR_PTR(-ENOENT);
252 }
253
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900254 if (PageUptodate(page))
255 return page;
256
257 BUG_ON(dn.data_blkaddr == NEW_ADDR);
258 BUG_ON(dn.data_blkaddr == NULL_ADDR);
259
260 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900261 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900262 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900263
264 lock_page(page);
265 if (!PageUptodate(page)) {
266 f2fs_put_page(page, 1);
267 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900268 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900269 if (page->mapping != mapping) {
270 f2fs_put_page(page, 1);
271 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900272 }
273 return page;
274}
275
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900276/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900277 * Caller ensures that this data page is never allocated.
278 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900279 *
280 * Also, caller should grab and release a mutex by calling mutex_lock_op() and
281 * mutex_unlock_op().
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900282 */
283struct page *get_new_data_page(struct inode *inode, pgoff_t index,
284 bool new_i_size)
285{
286 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
287 struct address_space *mapping = inode->i_mapping;
288 struct page *page;
289 struct dnode_of_data dn;
290 int err;
291
292 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900293 err = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900294 if (err)
295 return ERR_PTR(err);
296
297 if (dn.data_blkaddr == NULL_ADDR) {
298 if (reserve_new_block(&dn)) {
299 f2fs_put_dnode(&dn);
300 return ERR_PTR(-ENOSPC);
301 }
302 }
303 f2fs_put_dnode(&dn);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900304repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900305 page = grab_cache_page(mapping, index);
306 if (!page)
307 return ERR_PTR(-ENOMEM);
308
309 if (PageUptodate(page))
310 return page;
311
312 if (dn.data_blkaddr == NEW_ADDR) {
313 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900314 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900315 } else {
316 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900317 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900318 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900319 lock_page(page);
320 if (!PageUptodate(page)) {
321 f2fs_put_page(page, 1);
322 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900323 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900324 if (page->mapping != mapping) {
325 f2fs_put_page(page, 1);
326 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900327 }
328 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900329
330 if (new_i_size &&
331 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
332 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
333 mark_inode_dirty_sync(inode);
334 }
335 return page;
336}
337
338static void read_end_io(struct bio *bio, int err)
339{
340 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
341 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
342
343 do {
344 struct page *page = bvec->bv_page;
345
346 if (--bvec >= bio->bi_io_vec)
347 prefetchw(&bvec->bv_page->flags);
348
349 if (uptodate) {
350 SetPageUptodate(page);
351 } else {
352 ClearPageUptodate(page);
353 SetPageError(page);
354 }
355 unlock_page(page);
356 } while (bvec >= bio->bi_io_vec);
357 kfree(bio->bi_private);
358 bio_put(bio);
359}
360
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900361/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900362 * Fill the locked page with data located in the block address.
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900363 * Return unlocked page.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900364 */
365int f2fs_readpage(struct f2fs_sb_info *sbi, struct page *page,
366 block_t blk_addr, int type)
367{
368 struct block_device *bdev = sbi->sb->s_bdev;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900369 struct bio *bio;
370
Namjae Jeon848753a2013-04-23 16:38:02 +0900371 trace_f2fs_readpage(page, blk_addr, type);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900372
373 down_read(&sbi->bio_sem);
374
375 /* Allocate a new bio */
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900376 bio = f2fs_bio_alloc(bdev, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900377
378 /* Initialize the bio */
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900379 bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900380 bio->bi_end_io = read_end_io;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900381
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900382 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
383 kfree(bio->bi_private);
384 bio_put(bio);
385 up_read(&sbi->bio_sem);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900386 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900387 return -EFAULT;
388 }
389
390 submit_bio(type, bio);
391 up_read(&sbi->bio_sem);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900392 return 0;
393}
394
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900395/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900396 * This function should be used by the data read flow only where it
397 * does not check the "create" flag that indicates block allocation.
398 * The reason for this special functionality is to exploit VFS readahead
399 * mechanism.
400 */
401static int get_data_block_ro(struct inode *inode, sector_t iblock,
402 struct buffer_head *bh_result, int create)
403{
404 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
405 unsigned maxblocks = bh_result->b_size >> blkbits;
406 struct dnode_of_data dn;
407 pgoff_t pgofs;
408 int err;
409
410 /* Get the page offset from the block offset(iblock) */
411 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
412
Namjae Jeon848753a2013-04-23 16:38:02 +0900413 if (check_extent_cache(inode, pgofs, bh_result)) {
414 trace_f2fs_get_data_block(inode, iblock, bh_result, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900415 return 0;
Namjae Jeon848753a2013-04-23 16:38:02 +0900416 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900417
418 /* When reading holes, we need its node page */
419 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900420 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
Namjae Jeon848753a2013-04-23 16:38:02 +0900421 if (err) {
422 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900423 return (err == -ENOENT) ? 0 : err;
Namjae Jeon848753a2013-04-23 16:38:02 +0900424 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900425
426 /* It does not support data allocation */
427 BUG_ON(create);
428
429 if (dn.data_blkaddr != NEW_ADDR && dn.data_blkaddr != NULL_ADDR) {
430 int i;
431 unsigned int end_offset;
432
433 end_offset = IS_INODE(dn.node_page) ?
434 ADDRS_PER_INODE :
435 ADDRS_PER_BLOCK;
436
437 clear_buffer_new(bh_result);
438
439 /* Give more consecutive addresses for the read ahead */
440 for (i = 0; i < end_offset - dn.ofs_in_node; i++)
441 if (((datablock_addr(dn.node_page,
442 dn.ofs_in_node + i))
443 != (dn.data_blkaddr + i)) || maxblocks == i)
444 break;
445 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
446 bh_result->b_size = (i << blkbits);
447 }
448 f2fs_put_dnode(&dn);
Namjae Jeon848753a2013-04-23 16:38:02 +0900449 trace_f2fs_get_data_block(inode, iblock, bh_result, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900450 return 0;
451}
452
453static int f2fs_read_data_page(struct file *file, struct page *page)
454{
455 return mpage_readpage(page, get_data_block_ro);
456}
457
458static int f2fs_read_data_pages(struct file *file,
459 struct address_space *mapping,
460 struct list_head *pages, unsigned nr_pages)
461{
462 return mpage_readpages(mapping, pages, nr_pages, get_data_block_ro);
463}
464
465int do_write_data_page(struct page *page)
466{
467 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900468 block_t old_blk_addr, new_blk_addr;
469 struct dnode_of_data dn;
470 int err = 0;
471
472 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900473 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900474 if (err)
475 return err;
476
477 old_blk_addr = dn.data_blkaddr;
478
479 /* This page is already truncated */
480 if (old_blk_addr == NULL_ADDR)
481 goto out_writepage;
482
483 set_page_writeback(page);
484
485 /*
486 * If current allocation needs SSR,
487 * it had better in-place writes for updated data.
488 */
489 if (old_blk_addr != NEW_ADDR && !is_cold_data(page) &&
490 need_inplace_update(inode)) {
491 rewrite_data_page(F2FS_SB(inode->i_sb), page,
492 old_blk_addr);
493 } else {
494 write_data_page(inode, page, &dn,
495 old_blk_addr, &new_blk_addr);
496 update_extent_cache(new_blk_addr, &dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900497 }
498out_writepage:
499 f2fs_put_dnode(&dn);
500 return err;
501}
502
503static int f2fs_write_data_page(struct page *page,
504 struct writeback_control *wbc)
505{
506 struct inode *inode = page->mapping->host;
507 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
508 loff_t i_size = i_size_read(inode);
509 const pgoff_t end_index = ((unsigned long long) i_size)
510 >> PAGE_CACHE_SHIFT;
511 unsigned offset;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900512 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900513 int err = 0;
514
515 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900516 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900517
518 /*
519 * If the offset is out-of-range of file size,
520 * this page does not have to be written to disk.
521 */
522 offset = i_size & (PAGE_CACHE_SIZE - 1);
523 if ((page->index >= end_index + 1) || !offset) {
524 if (S_ISDIR(inode->i_mode)) {
525 dec_page_count(sbi, F2FS_DIRTY_DENTS);
526 inode_dec_dirty_dents(inode);
527 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900528 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900529 }
530
531 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900532write:
533 if (sbi->por_doing) {
534 err = AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900535 goto redirty_out;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900536 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900537
Jaegeuk Kim39936832012-11-22 16:21:29 +0900538 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900539 if (S_ISDIR(inode->i_mode)) {
540 dec_page_count(sbi, F2FS_DIRTY_DENTS);
541 inode_dec_dirty_dents(inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900542 err = do_write_data_page(page);
543 } else {
544 int ilock = mutex_lock_op(sbi);
545 err = do_write_data_page(page);
546 mutex_unlock_op(sbi, ilock);
547 need_balance_fs = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900548 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900549 if (err == -ENOENT)
550 goto out;
551 else if (err)
552 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900553
554 if (wbc->for_reclaim)
555 f2fs_submit_bio(sbi, DATA, true);
556
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900557 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900558out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900559 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900560 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900561 f2fs_balance_fs(sbi);
562 return 0;
563
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900564redirty_out:
565 wbc->pages_skipped++;
566 set_page_dirty(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900567 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900568}
569
570#define MAX_DESIRED_PAGES_WP 4096
571
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900572static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
573 void *data)
574{
575 struct address_space *mapping = data;
576 int ret = mapping->a_ops->writepage(page, wbc);
577 mapping_set_error(mapping, ret);
578 return ret;
579}
580
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900581static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900582 struct writeback_control *wbc)
583{
584 struct inode *inode = mapping->host;
585 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900586 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900587 int ret;
588 long excess_nrtw = 0, desired_nrtw;
589
P J Pcfb185a2013-04-03 11:38:00 +0900590 /* deal with chardevs and other special file */
591 if (!mapping->a_ops->writepage)
592 return 0;
593
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900594 if (wbc->nr_to_write < MAX_DESIRED_PAGES_WP) {
595 desired_nrtw = MAX_DESIRED_PAGES_WP;
596 excess_nrtw = desired_nrtw - wbc->nr_to_write;
597 wbc->nr_to_write = desired_nrtw;
598 }
599
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900600 if (!S_ISDIR(inode->i_mode)) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900601 mutex_lock(&sbi->writepages);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900602 locked = true;
603 }
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900604 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900605 if (locked)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900606 mutex_unlock(&sbi->writepages);
607 f2fs_submit_bio(sbi, DATA, (wbc->sync_mode == WB_SYNC_ALL));
608
609 remove_dirty_dir_inode(inode);
610
611 wbc->nr_to_write -= excess_nrtw;
612 return ret;
613}
614
615static int f2fs_write_begin(struct file *file, struct address_space *mapping,
616 loff_t pos, unsigned len, unsigned flags,
617 struct page **pagep, void **fsdata)
618{
619 struct inode *inode = mapping->host;
620 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
621 struct page *page;
622 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
623 struct dnode_of_data dn;
624 int err = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900625 int ilock;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900626
627 /* for nobh_write_end */
628 *fsdata = NULL;
629
630 f2fs_balance_fs(sbi);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900631repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900632 page = grab_cache_page_write_begin(mapping, index, flags);
633 if (!page)
634 return -ENOMEM;
635 *pagep = page;
636
Jaegeuk Kim39936832012-11-22 16:21:29 +0900637 ilock = mutex_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900638
639 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900640 err = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900641 if (err)
642 goto err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900643
Jaegeuk Kim39936832012-11-22 16:21:29 +0900644 if (dn.data_blkaddr == NULL_ADDR)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900645 err = reserve_new_block(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900646
Jaegeuk Kim39936832012-11-22 16:21:29 +0900647 f2fs_put_dnode(&dn);
648 if (err)
649 goto err;
650
651 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900652
653 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
654 return 0;
655
656 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
657 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
658 unsigned end = start + len;
659
660 /* Reading beyond i_size is simple: memset to zero */
661 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900662 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900663 }
664
665 if (dn.data_blkaddr == NEW_ADDR) {
666 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
667 } else {
668 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900669 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900670 return err;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900671 lock_page(page);
672 if (!PageUptodate(page)) {
673 f2fs_put_page(page, 1);
674 return -EIO;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900675 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900676 if (page->mapping != mapping) {
677 f2fs_put_page(page, 1);
678 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900679 }
680 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900681out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900682 SetPageUptodate(page);
683 clear_cold_data(page);
684 return 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900685
686err:
687 mutex_unlock_op(sbi, ilock);
688 f2fs_put_page(page, 1);
689 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900690}
691
692static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
693 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
694{
695 struct file *file = iocb->ki_filp;
696 struct inode *inode = file->f_mapping->host;
697
698 if (rw == WRITE)
699 return 0;
700
701 /* Needs synchronization with the cleaner */
702 return blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
703 get_data_block_ro);
704}
705
706static void f2fs_invalidate_data_page(struct page *page, unsigned long offset)
707{
708 struct inode *inode = page->mapping->host;
709 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
710 if (S_ISDIR(inode->i_mode) && PageDirty(page)) {
711 dec_page_count(sbi, F2FS_DIRTY_DENTS);
712 inode_dec_dirty_dents(inode);
713 }
714 ClearPagePrivate(page);
715}
716
717static int f2fs_release_data_page(struct page *page, gfp_t wait)
718{
719 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +0900720 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900721}
722
723static int f2fs_set_data_page_dirty(struct page *page)
724{
725 struct address_space *mapping = page->mapping;
726 struct inode *inode = mapping->host;
727
728 SetPageUptodate(page);
729 if (!PageDirty(page)) {
730 __set_page_dirty_nobuffers(page);
731 set_dirty_dir_page(inode, page);
732 return 1;
733 }
734 return 0;
735}
736
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +0900737static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
738{
739 return generic_block_bmap(mapping, block, get_data_block_ro);
740}
741
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900742const struct address_space_operations f2fs_dblock_aops = {
743 .readpage = f2fs_read_data_page,
744 .readpages = f2fs_read_data_pages,
745 .writepage = f2fs_write_data_page,
746 .writepages = f2fs_write_data_pages,
747 .write_begin = f2fs_write_begin,
748 .write_end = nobh_write_end,
749 .set_page_dirty = f2fs_set_data_page_dirty,
750 .invalidatepage = f2fs_invalidate_data_page,
751 .releasepage = f2fs_release_data_page,
752 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +0900753 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900754};