blob: cf9ff5f7613467c6c626e932c971b0149c312ea3 [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.
263 */
264struct page *get_new_data_page(struct inode *inode, pgoff_t index,
265 bool new_i_size)
266{
267 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
268 struct address_space *mapping = inode->i_mapping;
269 struct page *page;
270 struct dnode_of_data dn;
271 int err;
272
273 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900274 err = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900275 if (err)
276 return ERR_PTR(err);
277
278 if (dn.data_blkaddr == NULL_ADDR) {
279 if (reserve_new_block(&dn)) {
280 f2fs_put_dnode(&dn);
281 return ERR_PTR(-ENOSPC);
282 }
283 }
284 f2fs_put_dnode(&dn);
285
286 page = grab_cache_page(mapping, index);
287 if (!page)
288 return ERR_PTR(-ENOMEM);
289
290 if (PageUptodate(page))
291 return page;
292
293 if (dn.data_blkaddr == NEW_ADDR) {
294 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900295 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900296 } else {
297 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900298 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900299 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900300 lock_page(page);
301 if (!PageUptodate(page)) {
302 f2fs_put_page(page, 1);
303 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900304 }
305 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900306
307 if (new_i_size &&
308 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
309 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
310 mark_inode_dirty_sync(inode);
311 }
312 return page;
313}
314
315static void read_end_io(struct bio *bio, int err)
316{
317 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
318 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
319
320 do {
321 struct page *page = bvec->bv_page;
322
323 if (--bvec >= bio->bi_io_vec)
324 prefetchw(&bvec->bv_page->flags);
325
326 if (uptodate) {
327 SetPageUptodate(page);
328 } else {
329 ClearPageUptodate(page);
330 SetPageError(page);
331 }
332 unlock_page(page);
333 } while (bvec >= bio->bi_io_vec);
334 kfree(bio->bi_private);
335 bio_put(bio);
336}
337
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900338/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900339 * Fill the locked page with data located in the block address.
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900340 * Return unlocked page.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900341 */
342int f2fs_readpage(struct f2fs_sb_info *sbi, struct page *page,
343 block_t blk_addr, int type)
344{
345 struct block_device *bdev = sbi->sb->s_bdev;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900346 struct bio *bio;
347
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900348 down_read(&sbi->bio_sem);
349
350 /* Allocate a new bio */
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900351 bio = f2fs_bio_alloc(bdev, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900352
353 /* Initialize the bio */
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900354 bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900355 bio->bi_end_io = read_end_io;
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900356
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900357 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
358 kfree(bio->bi_private);
359 bio_put(bio);
360 up_read(&sbi->bio_sem);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900361 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900362 return -EFAULT;
363 }
364
365 submit_bio(type, bio);
366 up_read(&sbi->bio_sem);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900367 return 0;
368}
369
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900370/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900371 * This function should be used by the data read flow only where it
372 * does not check the "create" flag that indicates block allocation.
373 * The reason for this special functionality is to exploit VFS readahead
374 * mechanism.
375 */
376static int get_data_block_ro(struct inode *inode, sector_t iblock,
377 struct buffer_head *bh_result, int create)
378{
379 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
380 unsigned maxblocks = bh_result->b_size >> blkbits;
381 struct dnode_of_data dn;
382 pgoff_t pgofs;
383 int err;
384
385 /* Get the page offset from the block offset(iblock) */
386 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
387
388 if (check_extent_cache(inode, pgofs, bh_result))
389 return 0;
390
391 /* When reading holes, we need its node page */
392 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900393 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900394 if (err)
395 return (err == -ENOENT) ? 0 : err;
396
397 /* It does not support data allocation */
398 BUG_ON(create);
399
400 if (dn.data_blkaddr != NEW_ADDR && dn.data_blkaddr != NULL_ADDR) {
401 int i;
402 unsigned int end_offset;
403
404 end_offset = IS_INODE(dn.node_page) ?
405 ADDRS_PER_INODE :
406 ADDRS_PER_BLOCK;
407
408 clear_buffer_new(bh_result);
409
410 /* Give more consecutive addresses for the read ahead */
411 for (i = 0; i < end_offset - dn.ofs_in_node; i++)
412 if (((datablock_addr(dn.node_page,
413 dn.ofs_in_node + i))
414 != (dn.data_blkaddr + i)) || maxblocks == i)
415 break;
416 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
417 bh_result->b_size = (i << blkbits);
418 }
419 f2fs_put_dnode(&dn);
420 return 0;
421}
422
423static int f2fs_read_data_page(struct file *file, struct page *page)
424{
425 return mpage_readpage(page, get_data_block_ro);
426}
427
428static int f2fs_read_data_pages(struct file *file,
429 struct address_space *mapping,
430 struct list_head *pages, unsigned nr_pages)
431{
432 return mpage_readpages(mapping, pages, nr_pages, get_data_block_ro);
433}
434
435int do_write_data_page(struct page *page)
436{
437 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900438 block_t old_blk_addr, new_blk_addr;
439 struct dnode_of_data dn;
440 int err = 0;
441
442 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900443 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900444 if (err)
445 return err;
446
447 old_blk_addr = dn.data_blkaddr;
448
449 /* This page is already truncated */
450 if (old_blk_addr == NULL_ADDR)
451 goto out_writepage;
452
453 set_page_writeback(page);
454
455 /*
456 * If current allocation needs SSR,
457 * it had better in-place writes for updated data.
458 */
459 if (old_blk_addr != NEW_ADDR && !is_cold_data(page) &&
460 need_inplace_update(inode)) {
461 rewrite_data_page(F2FS_SB(inode->i_sb), page,
462 old_blk_addr);
463 } else {
464 write_data_page(inode, page, &dn,
465 old_blk_addr, &new_blk_addr);
466 update_extent_cache(new_blk_addr, &dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900467 }
468out_writepage:
469 f2fs_put_dnode(&dn);
470 return err;
471}
472
473static int f2fs_write_data_page(struct page *page,
474 struct writeback_control *wbc)
475{
476 struct inode *inode = page->mapping->host;
477 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
478 loff_t i_size = i_size_read(inode);
479 const pgoff_t end_index = ((unsigned long long) i_size)
480 >> PAGE_CACHE_SHIFT;
481 unsigned offset;
482 int err = 0;
483
484 if (page->index < end_index)
485 goto out;
486
487 /*
488 * If the offset is out-of-range of file size,
489 * this page does not have to be written to disk.
490 */
491 offset = i_size & (PAGE_CACHE_SIZE - 1);
492 if ((page->index >= end_index + 1) || !offset) {
493 if (S_ISDIR(inode->i_mode)) {
494 dec_page_count(sbi, F2FS_DIRTY_DENTS);
495 inode_dec_dirty_dents(inode);
496 }
497 goto unlock_out;
498 }
499
500 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
501out:
502 if (sbi->por_doing)
503 goto redirty_out;
504
505 if (wbc->for_reclaim && !S_ISDIR(inode->i_mode) && !is_cold_data(page))
506 goto redirty_out;
507
508 mutex_lock_op(sbi, DATA_WRITE);
509 if (S_ISDIR(inode->i_mode)) {
510 dec_page_count(sbi, F2FS_DIRTY_DENTS);
511 inode_dec_dirty_dents(inode);
512 }
513 err = do_write_data_page(page);
514 if (err && err != -ENOENT) {
515 wbc->pages_skipped++;
516 set_page_dirty(page);
517 }
518 mutex_unlock_op(sbi, DATA_WRITE);
519
520 if (wbc->for_reclaim)
521 f2fs_submit_bio(sbi, DATA, true);
522
523 if (err == -ENOENT)
524 goto unlock_out;
525
526 clear_cold_data(page);
527 unlock_page(page);
528
529 if (!wbc->for_reclaim && !S_ISDIR(inode->i_mode))
530 f2fs_balance_fs(sbi);
531 return 0;
532
533unlock_out:
534 unlock_page(page);
535 return (err == -ENOENT) ? 0 : err;
536
537redirty_out:
538 wbc->pages_skipped++;
539 set_page_dirty(page);
540 return AOP_WRITEPAGE_ACTIVATE;
541}
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;
595
596 /* for nobh_write_end */
597 *fsdata = NULL;
598
599 f2fs_balance_fs(sbi);
600
601 page = grab_cache_page_write_begin(mapping, index, flags);
602 if (!page)
603 return -ENOMEM;
604 *pagep = page;
605
606 mutex_lock_op(sbi, DATA_NEW);
607
608 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900609 err = get_dnode_of_data(&dn, index, ALLOC_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900610 if (err) {
611 mutex_unlock_op(sbi, DATA_NEW);
612 f2fs_put_page(page, 1);
613 return err;
614 }
615
616 if (dn.data_blkaddr == NULL_ADDR) {
617 err = reserve_new_block(&dn);
618 if (err) {
619 f2fs_put_dnode(&dn);
620 mutex_unlock_op(sbi, DATA_NEW);
621 f2fs_put_page(page, 1);
622 return err;
623 }
624 }
625 f2fs_put_dnode(&dn);
626
627 mutex_unlock_op(sbi, DATA_NEW);
628
629 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
630 return 0;
631
632 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
633 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
634 unsigned end = start + len;
635
636 /* Reading beyond i_size is simple: memset to zero */
637 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900638 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900639 }
640
641 if (dn.data_blkaddr == NEW_ADDR) {
642 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
643 } else {
644 err = f2fs_readpage(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900645 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900646 return err;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900647 lock_page(page);
648 if (!PageUptodate(page)) {
649 f2fs_put_page(page, 1);
650 return -EIO;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900651 }
652 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900653out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900654 SetPageUptodate(page);
655 clear_cold_data(page);
656 return 0;
657}
658
659static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
660 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
661{
662 struct file *file = iocb->ki_filp;
663 struct inode *inode = file->f_mapping->host;
664
665 if (rw == WRITE)
666 return 0;
667
668 /* Needs synchronization with the cleaner */
669 return blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
670 get_data_block_ro);
671}
672
673static void f2fs_invalidate_data_page(struct page *page, unsigned long offset)
674{
675 struct inode *inode = page->mapping->host;
676 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
677 if (S_ISDIR(inode->i_mode) && PageDirty(page)) {
678 dec_page_count(sbi, F2FS_DIRTY_DENTS);
679 inode_dec_dirty_dents(inode);
680 }
681 ClearPagePrivate(page);
682}
683
684static int f2fs_release_data_page(struct page *page, gfp_t wait)
685{
686 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +0900687 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900688}
689
690static int f2fs_set_data_page_dirty(struct page *page)
691{
692 struct address_space *mapping = page->mapping;
693 struct inode *inode = mapping->host;
694
695 SetPageUptodate(page);
696 if (!PageDirty(page)) {
697 __set_page_dirty_nobuffers(page);
698 set_dirty_dir_page(inode, page);
699 return 1;
700 }
701 return 0;
702}
703
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +0900704static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
705{
706 return generic_block_bmap(mapping, block, get_data_block_ro);
707}
708
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900709const struct address_space_operations f2fs_dblock_aops = {
710 .readpage = f2fs_read_data_page,
711 .readpages = f2fs_read_data_pages,
712 .writepage = f2fs_write_data_page,
713 .writepages = f2fs_write_data_pages,
714 .write_begin = f2fs_write_begin,
715 .write_end = nobh_write_end,
716 .set_page_dirty = f2fs_set_data_page_dirty,
717 .invalidatepage = f2fs_invalidate_data_page,
718 .releasepage = f2fs_release_data_page,
719 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +0900720 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900721};