blob: 72a1b30f720ac3501ea7c6bf88fe92a665334afc [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002 * fs/f2fs/data.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/buffer_head.h>
14#include <linux/mpage.h>
15#include <linux/writeback.h>
16#include <linux/backing-dev.h>
17#include <linux/blkdev.h>
18#include <linux/bio.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010019#include <linux/prefetch.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090020
21#include "f2fs.h"
22#include "node.h"
23#include "segment.h"
24
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +090025/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090026 * Lock ordering for the change of data block address:
27 * ->data_page
28 * ->node_page
29 * update block addresses in the node page
30 */
31static void __set_data_blkaddr(struct dnode_of_data *dn, block_t new_addr)
32{
33 struct f2fs_node *rn;
34 __le32 *addr_array;
35 struct page *node_page = dn->node_page;
36 unsigned int ofs_in_node = dn->ofs_in_node;
37
38 wait_on_page_writeback(node_page);
39
40 rn = (struct f2fs_node *)page_address(node_page);
41
42 /* Get physical address of data block */
43 addr_array = blkaddr_in_node(rn);
44 addr_array[ofs_in_node] = cpu_to_le32(new_addr);
45 set_page_dirty(node_page);
46}
47
48int reserve_new_block(struct dnode_of_data *dn)
49{
50 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
51
52 if (is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC))
53 return -EPERM;
54 if (!inc_valid_block_count(sbi, dn->inode, 1))
55 return -ENOSPC;
56
57 __set_data_blkaddr(dn, NEW_ADDR);
58 dn->data_blkaddr = NEW_ADDR;
59 sync_inode_page(dn);
60 return 0;
61}
62
63static int check_extent_cache(struct inode *inode, pgoff_t pgofs,
64 struct buffer_head *bh_result)
65{
66 struct f2fs_inode_info *fi = F2FS_I(inode);
67 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
68 pgoff_t start_fofs, end_fofs;
69 block_t start_blkaddr;
70
71 read_lock(&fi->ext.ext_lock);
72 if (fi->ext.len == 0) {
73 read_unlock(&fi->ext.ext_lock);
74 return 0;
75 }
76
77 sbi->total_hit_ext++;
78 start_fofs = fi->ext.fofs;
79 end_fofs = fi->ext.fofs + fi->ext.len - 1;
80 start_blkaddr = fi->ext.blk_addr;
81
82 if (pgofs >= start_fofs && pgofs <= end_fofs) {
83 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
84 size_t count;
85
86 clear_buffer_new(bh_result);
87 map_bh(bh_result, inode->i_sb,
88 start_blkaddr + pgofs - start_fofs);
89 count = end_fofs - pgofs + 1;
90 if (count < (UINT_MAX >> blkbits))
91 bh_result->b_size = (count << blkbits);
92 else
93 bh_result->b_size = UINT_MAX;
94
95 sbi->read_hit_ext++;
96 read_unlock(&fi->ext.ext_lock);
97 return 1;
98 }
99 read_unlock(&fi->ext.ext_lock);
100 return 0;
101}
102
103void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn)
104{
105 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
106 pgoff_t fofs, start_fofs, end_fofs;
107 block_t start_blkaddr, end_blkaddr;
108
109 BUG_ON(blk_addr == NEW_ADDR);
110 fofs = start_bidx_of_node(ofs_of_node(dn->node_page)) + dn->ofs_in_node;
111
112 /* Update the page address in the parent node */
113 __set_data_blkaddr(dn, blk_addr);
114
115 write_lock(&fi->ext.ext_lock);
116
117 start_fofs = fi->ext.fofs;
118 end_fofs = fi->ext.fofs + fi->ext.len - 1;
119 start_blkaddr = fi->ext.blk_addr;
120 end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1;
121
122 /* Drop and initialize the matched extent */
123 if (fi->ext.len == 1 && fofs == start_fofs)
124 fi->ext.len = 0;
125
126 /* Initial extent */
127 if (fi->ext.len == 0) {
128 if (blk_addr != NULL_ADDR) {
129 fi->ext.fofs = fofs;
130 fi->ext.blk_addr = blk_addr;
131 fi->ext.len = 1;
132 }
133 goto end_update;
134 }
135
136 /* Frone merge */
137 if (fofs == start_fofs - 1 && blk_addr == start_blkaddr - 1) {
138 fi->ext.fofs--;
139 fi->ext.blk_addr--;
140 fi->ext.len++;
141 goto end_update;
142 }
143
144 /* Back merge */
145 if (fofs == end_fofs + 1 && blk_addr == end_blkaddr + 1) {
146 fi->ext.len++;
147 goto end_update;
148 }
149
150 /* Split the existing extent */
151 if (fi->ext.len > 1 &&
152 fofs >= start_fofs && fofs <= end_fofs) {
153 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
154 fi->ext.len = fofs - start_fofs;
155 } else {
156 fi->ext.fofs = fofs + 1;
157 fi->ext.blk_addr = start_blkaddr +
158 fofs - start_fofs + 1;
159 fi->ext.len -= fofs - start_fofs + 1;
160 }
161 goto end_update;
162 }
163 write_unlock(&fi->ext.ext_lock);
164 return;
165
166end_update:
167 write_unlock(&fi->ext.ext_lock);
168 sync_inode_page(dn);
169 return;
170}
171
172struct page *find_data_page(struct inode *inode, pgoff_t index)
173{
174 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
175 struct address_space *mapping = inode->i_mapping;
176 struct dnode_of_data dn;
177 struct page *page;
178 int err;
179
180 page = find_get_page(mapping, index);
181 if (page && PageUptodate(page))
182 return page;
183 f2fs_put_page(page, 0);
184
185 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900186 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900187 if (err)
188 return ERR_PTR(err);
189 f2fs_put_dnode(&dn);
190
191 if (dn.data_blkaddr == NULL_ADDR)
192 return ERR_PTR(-ENOENT);
193
194 /* By fallocate(), there is no cached page, but with NEW_ADDR */
195 if (dn.data_blkaddr == NEW_ADDR)
196 return ERR_PTR(-EINVAL);
197
198 page = grab_cache_page(mapping, index);
199 if (!page)
200 return ERR_PTR(-ENOMEM);
201
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900202 if (PageUptodate(page)) {
203 unlock_page(page);
204 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900205 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900206
207 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
208 wait_on_page_locked(page);
209 if (!PageUptodate(page)) {
210 f2fs_put_page(page, 0);
211 return ERR_PTR(-EIO);
212 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900213 return page;
214}
215
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900216/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900217 * If it tries to access a hole, return an error.
218 * Because, the callers, functions in dir.c and GC, should be able to know
219 * whether this page exists or not.
220 */
221struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
222{
223 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
224 struct address_space *mapping = inode->i_mapping;
225 struct dnode_of_data dn;
226 struct page *page;
227 int err;
228
229 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900230 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900231 if (err)
232 return ERR_PTR(err);
233 f2fs_put_dnode(&dn);
234
235 if (dn.data_blkaddr == NULL_ADDR)
236 return ERR_PTR(-ENOENT);
237
238 page = grab_cache_page(mapping, index);
239 if (!page)
240 return ERR_PTR(-ENOMEM);
241
242 if (PageUptodate(page))
243 return page;
244
245 BUG_ON(dn.data_blkaddr == NEW_ADDR);
246 BUG_ON(dn.data_blkaddr == NULL_ADDR);
247
248 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900249 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900250 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900251
252 lock_page(page);
253 if (!PageUptodate(page)) {
254 f2fs_put_page(page, 1);
255 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900256 }
257 return page;
258}
259
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900260/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900261 * Caller ensures that this data page is never allocated.
262 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900263 *
264 * Also, caller should grab and release a mutex by calling mutex_lock_op() and
265 * mutex_unlock_op().
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900266 */
267struct page *get_new_data_page(struct inode *inode, pgoff_t index,
268 bool new_i_size)
269{
270 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
271 struct address_space *mapping = inode->i_mapping;
272 struct page *page;
273 struct dnode_of_data dn;
274 int err;
275
276 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900277 err = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900278 if (err)
279 return ERR_PTR(err);
280
281 if (dn.data_blkaddr == NULL_ADDR) {
282 if (reserve_new_block(&dn)) {
283 f2fs_put_dnode(&dn);
284 return ERR_PTR(-ENOSPC);
285 }
286 }
287 f2fs_put_dnode(&dn);
288
289 page = grab_cache_page(mapping, index);
290 if (!page)
291 return ERR_PTR(-ENOMEM);
292
293 if (PageUptodate(page))
294 return page;
295
296 if (dn.data_blkaddr == NEW_ADDR) {
297 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900298 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900299 } else {
300 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900301 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900302 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900303 lock_page(page);
304 if (!PageUptodate(page)) {
305 f2fs_put_page(page, 1);
306 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900307 }
308 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900309
310 if (new_i_size &&
311 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
312 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
313 mark_inode_dirty_sync(inode);
314 }
315 return page;
316}
317
318static void read_end_io(struct bio *bio, int err)
319{
320 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
321 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
322
323 do {
324 struct page *page = bvec->bv_page;
325
326 if (--bvec >= bio->bi_io_vec)
327 prefetchw(&bvec->bv_page->flags);
328
329 if (uptodate) {
330 SetPageUptodate(page);
331 } else {
332 ClearPageUptodate(page);
333 SetPageError(page);
334 }
335 unlock_page(page);
336 } while (bvec >= bio->bi_io_vec);
337 kfree(bio->bi_private);
338 bio_put(bio);
339}
340
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900341/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900342 * Fill the locked page with data located in the block address.
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900343 * Return unlocked page.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900344 */
345int f2fs_readpage(struct f2fs_sb_info *sbi, struct page *page,
346 block_t blk_addr, int type)
347{
348 struct block_device *bdev = sbi->sb->s_bdev;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900349 struct bio *bio;
350
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900351 down_read(&sbi->bio_sem);
352
353 /* Allocate a new bio */
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900354 bio = f2fs_bio_alloc(bdev, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900355
356 /* Initialize the bio */
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900357 bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900358 bio->bi_end_io = read_end_io;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900359
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900360 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
361 kfree(bio->bi_private);
362 bio_put(bio);
363 up_read(&sbi->bio_sem);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900364 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900365 return -EFAULT;
366 }
367
368 submit_bio(type, bio);
369 up_read(&sbi->bio_sem);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900370 return 0;
371}
372
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900373/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900374 * This function should be used by the data read flow only where it
375 * does not check the "create" flag that indicates block allocation.
376 * The reason for this special functionality is to exploit VFS readahead
377 * mechanism.
378 */
379static int get_data_block_ro(struct inode *inode, sector_t iblock,
380 struct buffer_head *bh_result, int create)
381{
382 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
383 unsigned maxblocks = bh_result->b_size >> blkbits;
384 struct dnode_of_data dn;
385 pgoff_t pgofs;
386 int err;
387
388 /* Get the page offset from the block offset(iblock) */
389 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
390
391 if (check_extent_cache(inode, pgofs, bh_result))
392 return 0;
393
394 /* When reading holes, we need its node page */
395 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900396 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900397 if (err)
398 return (err == -ENOENT) ? 0 : err;
399
400 /* It does not support data allocation */
401 BUG_ON(create);
402
403 if (dn.data_blkaddr != NEW_ADDR && dn.data_blkaddr != NULL_ADDR) {
404 int i;
405 unsigned int end_offset;
406
407 end_offset = IS_INODE(dn.node_page) ?
408 ADDRS_PER_INODE :
409 ADDRS_PER_BLOCK;
410
411 clear_buffer_new(bh_result);
412
413 /* Give more consecutive addresses for the read ahead */
414 for (i = 0; i < end_offset - dn.ofs_in_node; i++)
415 if (((datablock_addr(dn.node_page,
416 dn.ofs_in_node + i))
417 != (dn.data_blkaddr + i)) || maxblocks == i)
418 break;
419 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
420 bh_result->b_size = (i << blkbits);
421 }
422 f2fs_put_dnode(&dn);
423 return 0;
424}
425
426static int f2fs_read_data_page(struct file *file, struct page *page)
427{
428 return mpage_readpage(page, get_data_block_ro);
429}
430
431static int f2fs_read_data_pages(struct file *file,
432 struct address_space *mapping,
433 struct list_head *pages, unsigned nr_pages)
434{
435 return mpage_readpages(mapping, pages, nr_pages, get_data_block_ro);
436}
437
438int do_write_data_page(struct page *page)
439{
440 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900441 block_t old_blk_addr, new_blk_addr;
442 struct dnode_of_data dn;
443 int err = 0;
444
445 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900446 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900447 if (err)
448 return err;
449
450 old_blk_addr = dn.data_blkaddr;
451
452 /* This page is already truncated */
453 if (old_blk_addr == NULL_ADDR)
454 goto out_writepage;
455
456 set_page_writeback(page);
457
458 /*
459 * If current allocation needs SSR,
460 * it had better in-place writes for updated data.
461 */
462 if (old_blk_addr != NEW_ADDR && !is_cold_data(page) &&
463 need_inplace_update(inode)) {
464 rewrite_data_page(F2FS_SB(inode->i_sb), page,
465 old_blk_addr);
466 } else {
467 write_data_page(inode, page, &dn,
468 old_blk_addr, &new_blk_addr);
469 update_extent_cache(new_blk_addr, &dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900470 }
471out_writepage:
472 f2fs_put_dnode(&dn);
473 return err;
474}
475
476static int f2fs_write_data_page(struct page *page,
477 struct writeback_control *wbc)
478{
479 struct inode *inode = page->mapping->host;
480 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
481 loff_t i_size = i_size_read(inode);
482 const pgoff_t end_index = ((unsigned long long) i_size)
483 >> PAGE_CACHE_SHIFT;
484 unsigned offset;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900485 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900486 int err = 0;
487
488 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900489 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900490
491 /*
492 * If the offset is out-of-range of file size,
493 * this page does not have to be written to disk.
494 */
495 offset = i_size & (PAGE_CACHE_SIZE - 1);
496 if ((page->index >= end_index + 1) || !offset) {
497 if (S_ISDIR(inode->i_mode)) {
498 dec_page_count(sbi, F2FS_DIRTY_DENTS);
499 inode_dec_dirty_dents(inode);
500 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900501 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900502 }
503
504 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900505write:
506 if (sbi->por_doing) {
507 err = AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900508 goto redirty_out;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900509 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900510
Jaegeuk Kim39936832012-11-22 16:21:29 +0900511 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900512 if (S_ISDIR(inode->i_mode)) {
513 dec_page_count(sbi, F2FS_DIRTY_DENTS);
514 inode_dec_dirty_dents(inode);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900515 err = do_write_data_page(page);
516 } else {
517 int ilock = mutex_lock_op(sbi);
518 err = do_write_data_page(page);
519 mutex_unlock_op(sbi, ilock);
520 need_balance_fs = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900521 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900522 if (err == -ENOENT)
523 goto out;
524 else if (err)
525 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900526
527 if (wbc->for_reclaim)
528 f2fs_submit_bio(sbi, DATA, true);
529
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900530 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900531out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900532 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900533 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900534 f2fs_balance_fs(sbi);
535 return 0;
536
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900537redirty_out:
538 wbc->pages_skipped++;
539 set_page_dirty(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900540 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900541}
542
543#define MAX_DESIRED_PAGES_WP 4096
544
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900545static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
546 void *data)
547{
548 struct address_space *mapping = data;
549 int ret = mapping->a_ops->writepage(page, wbc);
550 mapping_set_error(mapping, ret);
551 return ret;
552}
553
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900554static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900555 struct writeback_control *wbc)
556{
557 struct inode *inode = mapping->host;
558 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
559 int ret;
560 long excess_nrtw = 0, desired_nrtw;
561
P J Pcfb185a2013-04-03 11:38:00 +0900562 /* deal with chardevs and other special file */
563 if (!mapping->a_ops->writepage)
564 return 0;
565
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900566 if (wbc->nr_to_write < MAX_DESIRED_PAGES_WP) {
567 desired_nrtw = MAX_DESIRED_PAGES_WP;
568 excess_nrtw = desired_nrtw - wbc->nr_to_write;
569 wbc->nr_to_write = desired_nrtw;
570 }
571
572 if (!S_ISDIR(inode->i_mode))
573 mutex_lock(&sbi->writepages);
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900574 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900575 if (!S_ISDIR(inode->i_mode))
576 mutex_unlock(&sbi->writepages);
577 f2fs_submit_bio(sbi, DATA, (wbc->sync_mode == WB_SYNC_ALL));
578
579 remove_dirty_dir_inode(inode);
580
581 wbc->nr_to_write -= excess_nrtw;
582 return ret;
583}
584
585static int f2fs_write_begin(struct file *file, struct address_space *mapping,
586 loff_t pos, unsigned len, unsigned flags,
587 struct page **pagep, void **fsdata)
588{
589 struct inode *inode = mapping->host;
590 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
591 struct page *page;
592 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
593 struct dnode_of_data dn;
594 int err = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900595 int ilock;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900596
597 /* for nobh_write_end */
598 *fsdata = NULL;
599
600 f2fs_balance_fs(sbi);
601
602 page = grab_cache_page_write_begin(mapping, index, flags);
603 if (!page)
604 return -ENOMEM;
605 *pagep = page;
606
Jaegeuk Kim39936832012-11-22 16:21:29 +0900607 ilock = mutex_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900608
609 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900610 err = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900611 if (err)
612 goto err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900613
Jaegeuk Kim39936832012-11-22 16:21:29 +0900614 if (dn.data_blkaddr == NULL_ADDR)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900615 err = reserve_new_block(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900616
Jaegeuk Kim39936832012-11-22 16:21:29 +0900617 f2fs_put_dnode(&dn);
618 if (err)
619 goto err;
620
621 mutex_unlock_op(sbi, ilock);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900622
623 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
624 return 0;
625
626 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
627 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
628 unsigned end = start + len;
629
630 /* Reading beyond i_size is simple: memset to zero */
631 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900632 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900633 }
634
635 if (dn.data_blkaddr == NEW_ADDR) {
636 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
637 } else {
638 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900639 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900640 return err;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900641 lock_page(page);
642 if (!PageUptodate(page)) {
643 f2fs_put_page(page, 1);
644 return -EIO;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900645 }
646 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900647out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900648 SetPageUptodate(page);
649 clear_cold_data(page);
650 return 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900651
652err:
653 mutex_unlock_op(sbi, ilock);
654 f2fs_put_page(page, 1);
655 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900656}
657
658static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
659 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
660{
661 struct file *file = iocb->ki_filp;
662 struct inode *inode = file->f_mapping->host;
663
664 if (rw == WRITE)
665 return 0;
666
667 /* Needs synchronization with the cleaner */
668 return blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
669 get_data_block_ro);
670}
671
672static void f2fs_invalidate_data_page(struct page *page, unsigned long offset)
673{
674 struct inode *inode = page->mapping->host;
675 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
676 if (S_ISDIR(inode->i_mode) && PageDirty(page)) {
677 dec_page_count(sbi, F2FS_DIRTY_DENTS);
678 inode_dec_dirty_dents(inode);
679 }
680 ClearPagePrivate(page);
681}
682
683static int f2fs_release_data_page(struct page *page, gfp_t wait)
684{
685 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +0900686 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900687}
688
689static int f2fs_set_data_page_dirty(struct page *page)
690{
691 struct address_space *mapping = page->mapping;
692 struct inode *inode = mapping->host;
693
694 SetPageUptodate(page);
695 if (!PageDirty(page)) {
696 __set_page_dirty_nobuffers(page);
697 set_dirty_dir_page(inode, page);
698 return 1;
699 }
700 return 0;
701}
702
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +0900703static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
704{
705 return generic_block_bmap(mapping, block, get_data_block_ro);
706}
707
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900708const struct address_space_operations f2fs_dblock_aops = {
709 .readpage = f2fs_read_data_page,
710 .readpages = f2fs_read_data_pages,
711 .writepage = f2fs_write_data_page,
712 .writepages = f2fs_write_data_pages,
713 .write_begin = f2fs_write_begin,
714 .write_end = nobh_write_end,
715 .set_page_dirty = f2fs_set_data_page_dirty,
716 .invalidatepage = f2fs_invalidate_data_page,
717 .releasepage = f2fs_release_data_page,
718 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +0900719 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900720};