blob: e57bde02e37f662fc37253a82d07617d41fd6e7a [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 Kim93dfe2a2013-11-30 12:51:14 +090027static void f2fs_read_end_io(struct bio *bio, int err)
28{
29 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
30 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
31
32 do {
33 struct page *page = bvec->bv_page;
34
35 if (--bvec >= bio->bi_io_vec)
36 prefetchw(&bvec->bv_page->flags);
37
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090038 if (unlikely(!uptodate)) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090039 ClearPageUptodate(page);
40 SetPageError(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090041 } else {
42 SetPageUptodate(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090043 }
44 unlock_page(page);
45 } while (bvec >= bio->bi_io_vec);
46
47 bio_put(bio);
48}
49
50static void f2fs_write_end_io(struct bio *bio, int err)
51{
52 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
53 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
54 struct f2fs_sb_info *sbi = F2FS_SB(bvec->bv_page->mapping->host->i_sb);
55
56 do {
57 struct page *page = bvec->bv_page;
58
59 if (--bvec >= bio->bi_io_vec)
60 prefetchw(&bvec->bv_page->flags);
61
Jaegeuk Kim6bacf522013-12-06 15:00:58 +090062 if (unlikely(!uptodate)) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090063 SetPageError(page);
64 set_bit(AS_EIO, &page->mapping->flags);
65 set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
66 sbi->sb->s_flags |= MS_RDONLY;
67 }
68 end_page_writeback(page);
69 dec_page_count(sbi, F2FS_WRITEBACK);
70 } while (bvec >= bio->bi_io_vec);
71
72 if (bio->bi_private)
73 complete(bio->bi_private);
74
75 if (!get_pages(sbi, F2FS_WRITEBACK) &&
76 !list_empty(&sbi->cp_wait.task_list))
77 wake_up(&sbi->cp_wait);
78
79 bio_put(bio);
80}
81
Gu Zheng940a6d32013-12-20 17:39:59 +080082/*
83 * Low-level block read/write IO operations.
84 */
85static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
86 int npages, bool is_read)
87{
88 struct bio *bio;
89
90 /* No failure on bio allocation */
91 bio = bio_alloc(GFP_NOIO, npages);
92
93 bio->bi_bdev = sbi->sb->s_bdev;
94 bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
95 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
96
97 return bio;
98}
99
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900100static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900101{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900102 struct f2fs_io_info *fio = &io->fio;
103 int rw;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900104
105 if (!io->bio)
106 return;
107
Gu Zheng7e8f2302013-12-20 18:17:49 +0800108 rw = fio->rw;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900109
110 if (is_read_io(rw)) {
Gu Zheng940a6d32013-12-20 17:39:59 +0800111 trace_f2fs_submit_read_bio(io->sbi->sb, rw,
112 fio->type, io->bio);
Jaegeuk Kim76130cc2013-12-11 14:29:39 +0900113 submit_bio(rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900114 } else {
Gu Zheng940a6d32013-12-20 17:39:59 +0800115 trace_f2fs_submit_write_bio(io->sbi->sb, rw,
116 fio->type, io->bio);
117 /*
118 * META_FLUSH is only from the checkpoint procedure, and we
119 * should wait this metadata bio for FS consistency.
120 */
121 if (fio->type == META_FLUSH) {
122 DECLARE_COMPLETION_ONSTACK(wait);
123 io->bio->bi_private = &wait;
124 submit_bio(rw, io->bio);
125 wait_for_completion(&wait);
126 } else {
127 submit_bio(rw, io->bio);
128 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900129 }
Gu Zheng940a6d32013-12-20 17:39:59 +0800130
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900131 io->bio = NULL;
132}
133
134void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900135 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900136{
137 enum page_type btype = PAGE_TYPE_OF_BIO(type);
138 struct f2fs_bio_info *io;
139
140 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
141
142 mutex_lock(&io->io_mutex);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900143
144 /* change META to META_FLUSH in the checkpoint procedure */
145 if (type >= META_FLUSH) {
146 io->fio.type = META_FLUSH;
147 io->fio.rw = WRITE_FLUSH_FUA;
148 }
149 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900150 mutex_unlock(&io->io_mutex);
151}
152
153/*
154 * Fill the locked page with data located in the block address.
155 * Return unlocked page.
156 */
157int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page,
158 block_t blk_addr, int rw)
159{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900160 struct bio *bio;
161
162 trace_f2fs_submit_page_bio(page, blk_addr, rw);
163
164 /* Allocate a new bio */
Gu Zheng940a6d32013-12-20 17:39:59 +0800165 bio = __bio_alloc(sbi, blk_addr, 1, is_read_io(rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900166
167 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
168 bio_put(bio);
169 f2fs_put_page(page, 1);
170 return -EFAULT;
171 }
172
173 submit_bio(rw, bio);
174 return 0;
175}
176
177void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900178 block_t blk_addr, struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900179{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900180 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900181 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800182 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900183
Gu Zheng940a6d32013-12-20 17:39:59 +0800184 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900185
186 verify_block_addr(sbi, blk_addr);
187
188 mutex_lock(&io->io_mutex);
189
Gu Zheng940a6d32013-12-20 17:39:59 +0800190 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900191 inc_page_count(sbi, F2FS_WRITEBACK);
192
Fan Li63a0b7cb2013-12-09 16:09:00 +0800193 if (io->bio && (io->last_block_in_bio != blk_addr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900194 io->fio.rw != fio->rw))
195 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900196alloc_new:
197 if (io->bio == NULL) {
Gu Zheng940a6d32013-12-20 17:39:59 +0800198 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
199
200 io->bio = __bio_alloc(sbi, blk_addr, bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900201 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900202 }
203
204 if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
205 PAGE_CACHE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900206 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900207 goto alloc_new;
208 }
209
210 io->last_block_in_bio = blk_addr;
211
212 mutex_unlock(&io->io_mutex);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900213 trace_f2fs_submit_page_mbio(page, fio->rw, fio->type, blk_addr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900214}
215
216/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900217 * Lock ordering for the change of data block address:
218 * ->data_page
219 * ->node_page
220 * update block addresses in the node page
221 */
222static void __set_data_blkaddr(struct dnode_of_data *dn, block_t new_addr)
223{
224 struct f2fs_node *rn;
225 __le32 *addr_array;
226 struct page *node_page = dn->node_page;
227 unsigned int ofs_in_node = dn->ofs_in_node;
228
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000229 f2fs_wait_on_page_writeback(node_page, NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900230
Gu Zheng45590712013-07-15 17:57:38 +0800231 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900232
233 /* Get physical address of data block */
234 addr_array = blkaddr_in_node(rn);
235 addr_array[ofs_in_node] = cpu_to_le32(new_addr);
236 set_page_dirty(node_page);
237}
238
239int reserve_new_block(struct dnode_of_data *dn)
240{
241 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
242
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900243 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900244 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800245 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900246 return -ENOSPC;
247
Namjae Jeonc01e2852013-04-23 17:00:52 +0900248 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
249
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900250 __set_data_blkaddr(dn, NEW_ADDR);
251 dn->data_blkaddr = NEW_ADDR;
252 sync_inode_page(dn);
253 return 0;
254}
255
Huajun Lib6009652013-11-10 23:13:18 +0800256int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
257{
258 bool need_put = dn->inode_page ? false : true;
259 int err;
260
Jaegeuk Kima8865372013-12-27 17:04:17 +0900261 /* if inode_page exists, index should be zero */
262 f2fs_bug_on(!need_put && index);
263
Huajun Lib6009652013-11-10 23:13:18 +0800264 err = get_dnode_of_data(dn, index, ALLOC_NODE);
265 if (err)
266 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900267
Huajun Lib6009652013-11-10 23:13:18 +0800268 if (dn->data_blkaddr == NULL_ADDR)
269 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900270 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800271 f2fs_put_dnode(dn);
272 return err;
273}
274
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900275static int check_extent_cache(struct inode *inode, pgoff_t pgofs,
276 struct buffer_head *bh_result)
277{
278 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900279 pgoff_t start_fofs, end_fofs;
280 block_t start_blkaddr;
281
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900282 if (is_inode_flag_set(fi, FI_NO_EXTENT))
283 return 0;
284
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900285 read_lock(&fi->ext.ext_lock);
286 if (fi->ext.len == 0) {
287 read_unlock(&fi->ext.ext_lock);
288 return 0;
289 }
290
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900291 stat_inc_total_hit(inode->i_sb);
292
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900293 start_fofs = fi->ext.fofs;
294 end_fofs = fi->ext.fofs + fi->ext.len - 1;
295 start_blkaddr = fi->ext.blk_addr;
296
297 if (pgofs >= start_fofs && pgofs <= end_fofs) {
298 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
299 size_t count;
300
301 clear_buffer_new(bh_result);
302 map_bh(bh_result, inode->i_sb,
303 start_blkaddr + pgofs - start_fofs);
304 count = end_fofs - pgofs + 1;
305 if (count < (UINT_MAX >> blkbits))
306 bh_result->b_size = (count << blkbits);
307 else
308 bh_result->b_size = UINT_MAX;
309
Jaegeuk Kimdcdfff62013-10-22 20:56:10 +0900310 stat_inc_read_hit(inode->i_sb);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900311 read_unlock(&fi->ext.ext_lock);
312 return 1;
313 }
314 read_unlock(&fi->ext.ext_lock);
315 return 0;
316}
317
318void update_extent_cache(block_t blk_addr, struct dnode_of_data *dn)
319{
320 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
321 pgoff_t fofs, start_fofs, end_fofs;
322 block_t start_blkaddr, end_blkaddr;
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900323 int need_update = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900324
Jaegeuk Kim5d56b672013-10-29 15:14:54 +0900325 f2fs_bug_on(blk_addr == NEW_ADDR);
Jaegeuk Kimde936532013-08-12 21:08:03 +0900326 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
327 dn->ofs_in_node;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900328
329 /* Update the page address in the parent node */
330 __set_data_blkaddr(dn, blk_addr);
331
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900332 if (is_inode_flag_set(fi, FI_NO_EXTENT))
333 return;
334
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900335 write_lock(&fi->ext.ext_lock);
336
337 start_fofs = fi->ext.fofs;
338 end_fofs = fi->ext.fofs + fi->ext.len - 1;
339 start_blkaddr = fi->ext.blk_addr;
340 end_blkaddr = fi->ext.blk_addr + fi->ext.len - 1;
341
342 /* Drop and initialize the matched extent */
343 if (fi->ext.len == 1 && fofs == start_fofs)
344 fi->ext.len = 0;
345
346 /* Initial extent */
347 if (fi->ext.len == 0) {
348 if (blk_addr != NULL_ADDR) {
349 fi->ext.fofs = fofs;
350 fi->ext.blk_addr = blk_addr;
351 fi->ext.len = 1;
352 }
353 goto end_update;
354 }
355
Namjae Jeon6224da82013-04-06 14:44:32 +0900356 /* Front merge */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900357 if (fofs == start_fofs - 1 && blk_addr == start_blkaddr - 1) {
358 fi->ext.fofs--;
359 fi->ext.blk_addr--;
360 fi->ext.len++;
361 goto end_update;
362 }
363
364 /* Back merge */
365 if (fofs == end_fofs + 1 && blk_addr == end_blkaddr + 1) {
366 fi->ext.len++;
367 goto end_update;
368 }
369
370 /* Split the existing extent */
371 if (fi->ext.len > 1 &&
372 fofs >= start_fofs && fofs <= end_fofs) {
373 if ((end_fofs - fofs) < (fi->ext.len >> 1)) {
374 fi->ext.len = fofs - start_fofs;
375 } else {
376 fi->ext.fofs = fofs + 1;
377 fi->ext.blk_addr = start_blkaddr +
378 fofs - start_fofs + 1;
379 fi->ext.len -= fofs - start_fofs + 1;
380 }
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900381 } else {
382 need_update = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900383 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900384
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900385 /* Finally, if the extent is very fragmented, let's drop the cache. */
386 if (fi->ext.len < F2FS_MIN_EXTENT_LEN) {
387 fi->ext.len = 0;
388 set_inode_flag(fi, FI_NO_EXTENT);
389 need_update = true;
390 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900391end_update:
392 write_unlock(&fi->ext.ext_lock);
Jaegeuk Kimc11abd12013-11-19 10:41:54 +0900393 if (need_update)
394 sync_inode_page(dn);
395 return;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900396}
397
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900398struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900399{
400 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
401 struct address_space *mapping = inode->i_mapping;
402 struct dnode_of_data dn;
403 struct page *page;
404 int err;
405
406 page = find_get_page(mapping, index);
407 if (page && PageUptodate(page))
408 return page;
409 f2fs_put_page(page, 0);
410
411 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900412 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900413 if (err)
414 return ERR_PTR(err);
415 f2fs_put_dnode(&dn);
416
417 if (dn.data_blkaddr == NULL_ADDR)
418 return ERR_PTR(-ENOENT);
419
420 /* By fallocate(), there is no cached page, but with NEW_ADDR */
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900421 if (unlikely(dn.data_blkaddr == NEW_ADDR))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900422 return ERR_PTR(-EINVAL);
423
Jaegeuk Kim6f85b352013-05-20 16:15:22 +0900424 page = grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900425 if (!page)
426 return ERR_PTR(-ENOMEM);
427
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900428 if (PageUptodate(page)) {
429 unlock_page(page);
430 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900431 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900432
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900433 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr,
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900434 sync ? READ_SYNC : READA);
Chao Yu1069bbf2013-11-28 15:43:43 +0800435 if (err)
436 return ERR_PTR(err);
437
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900438 if (sync) {
439 wait_on_page_locked(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900440 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kimc718379b2013-04-24 13:19:56 +0900441 f2fs_put_page(page, 0);
442 return ERR_PTR(-EIO);
443 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900444 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900445 return page;
446}
447
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900448/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900449 * If it tries to access a hole, return an error.
450 * Because, the callers, functions in dir.c and GC, should be able to know
451 * whether this page exists or not.
452 */
453struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
454{
455 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
456 struct address_space *mapping = inode->i_mapping;
457 struct dnode_of_data dn;
458 struct page *page;
459 int err;
460
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900461repeat:
Jaegeuk Kim6f85b352013-05-20 16:15:22 +0900462 page = grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900463 if (!page)
464 return ERR_PTR(-ENOMEM);
465
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900466 set_new_dnode(&dn, inode, NULL, NULL, 0);
467 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
468 if (err) {
469 f2fs_put_page(page, 1);
470 return ERR_PTR(err);
471 }
472 f2fs_put_dnode(&dn);
473
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900474 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900475 f2fs_put_page(page, 1);
476 return ERR_PTR(-ENOENT);
477 }
478
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900479 if (PageUptodate(page))
480 return page;
481
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900482 /*
483 * A new dentry page is allocated but not able to be written, since its
484 * new inode page couldn't be allocated due to -ENOSPC.
485 * In such the case, its blkaddr can be remained as NEW_ADDR.
486 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
487 */
488 if (dn.data_blkaddr == NEW_ADDR) {
489 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
490 SetPageUptodate(page);
491 return page;
492 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900493
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900494 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr, READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900495 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900496 return ERR_PTR(err);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900497
498 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900499 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900500 f2fs_put_page(page, 1);
501 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900502 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900503 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900504 f2fs_put_page(page, 1);
505 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900506 }
507 return page;
508}
509
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900510/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900511 * Caller ensures that this data page is never allocated.
512 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900513 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800514 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
515 * f2fs_unlock_op().
Jaegeuk Kima8865372013-12-27 17:04:17 +0900516 * Note that, ipage is set only by make_empty_dir.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900517 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900518struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900519 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900520{
521 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
522 struct address_space *mapping = inode->i_mapping;
523 struct page *page;
524 struct dnode_of_data dn;
525 int err;
526
Jaegeuk Kima8865372013-12-27 17:04:17 +0900527 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800528 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900529 if (err)
530 return ERR_PTR(err);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900531repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900532 page = grab_cache_page(mapping, index);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900533 if (!page) {
534 err = -ENOMEM;
535 goto put_err;
536 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900537
538 if (PageUptodate(page))
539 return page;
540
541 if (dn.data_blkaddr == NEW_ADDR) {
542 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900543 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900544 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900545 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr,
546 READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900547 if (err)
Jaegeuk Kima8865372013-12-27 17:04:17 +0900548 goto put_err;
549
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900550 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900551 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900552 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900553 err = -EIO;
554 goto put_err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900555 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900556 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900557 f2fs_put_page(page, 1);
558 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900559 }
560 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900561
562 if (new_i_size &&
563 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
564 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900565 /* Only the directory inode sets new_i_size */
566 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900567 mark_inode_dirty_sync(inode);
568 }
569 return page;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900570
571put_err:
572 f2fs_put_dnode(&dn);
573 return ERR_PTR(err);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900574}
575
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900576static int __allocate_data_block(struct dnode_of_data *dn)
577{
578 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
579 struct f2fs_summary sum;
580 block_t new_blkaddr;
581 struct node_info ni;
582 int type;
583
584 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
585 return -EPERM;
586 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
587 return -ENOSPC;
588
589 __set_data_blkaddr(dn, NEW_ADDR);
590 dn->data_blkaddr = NEW_ADDR;
591
592 get_node_info(sbi, dn->nid, &ni);
593 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
594
595 type = CURSEG_WARM_DATA;
596
597 allocate_data_block(sbi, NULL, NULL_ADDR, &new_blkaddr, &sum, type);
598
599 /* direct IO doesn't use extent cache to maximize the performance */
600 set_inode_flag(F2FS_I(dn->inode), FI_NO_EXTENT);
601 update_extent_cache(new_blkaddr, dn);
602 clear_inode_flag(F2FS_I(dn->inode), FI_NO_EXTENT);
603
604 dn->data_blkaddr = new_blkaddr;
605 return 0;
606}
607
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900608/*
Chao Yu4f4124d2013-12-21 18:02:14 +0800609 * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
610 * If original data blocks are allocated, then give them to blockdev.
611 * Otherwise,
612 * a. preallocate requested block addresses
613 * b. do not use extent cache for better performance
614 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900615 */
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900616static int get_data_block(struct inode *inode, sector_t iblock,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900617 struct buffer_head *bh_result, int create)
618{
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900619 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900620 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
621 unsigned maxblocks = bh_result->b_size >> blkbits;
622 struct dnode_of_data dn;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900623 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
624 pgoff_t pgofs, end_offset;
625 int err = 0, ofs = 1;
626 bool allocated = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900627
628 /* Get the page offset from the block offset(iblock) */
629 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
630
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900631 if (check_extent_cache(inode, pgofs, bh_result))
632 goto out;
633
634 if (create)
635 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900636
637 /* When reading holes, we need its node page */
638 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900639 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900640 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900641 if (err == -ENOENT)
642 err = 0;
643 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900644 }
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900645 if (dn.data_blkaddr == NEW_ADDR)
646 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900647
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900648 if (dn.data_blkaddr != NULL_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900649 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900650 } else if (create) {
651 err = __allocate_data_block(&dn);
652 if (err)
653 goto put_out;
654 allocated = true;
655 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
656 } else {
657 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900658 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900659
660 end_offset = IS_INODE(dn.node_page) ?
661 ADDRS_PER_INODE(F2FS_I(inode)) : ADDRS_PER_BLOCK;
662 bh_result->b_size = (((size_t)1) << blkbits);
663 dn.ofs_in_node++;
664 pgofs++;
665
666get_next:
667 if (dn.ofs_in_node >= end_offset) {
668 if (allocated)
669 sync_inode_page(&dn);
670 allocated = false;
671 f2fs_put_dnode(&dn);
672
673 set_new_dnode(&dn, inode, NULL, NULL, 0);
674 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900675 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900676 if (err == -ENOENT)
677 err = 0;
678 goto unlock_out;
679 }
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900680 if (dn.data_blkaddr == NEW_ADDR)
681 goto put_out;
682
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900683 end_offset = IS_INODE(dn.node_page) ?
684 ADDRS_PER_INODE(F2FS_I(inode)) : ADDRS_PER_BLOCK;
685 }
686
687 if (maxblocks > (bh_result->b_size >> blkbits)) {
688 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
689 if (blkaddr == NULL_ADDR && create) {
690 err = __allocate_data_block(&dn);
691 if (err)
692 goto sync_out;
693 allocated = true;
694 blkaddr = dn.data_blkaddr;
695 }
696 /* Give more consecutive addresses for the read ahead */
697 if (blkaddr == (bh_result->b_blocknr + ofs)) {
698 ofs++;
699 dn.ofs_in_node++;
700 pgofs++;
701 bh_result->b_size += (((size_t)1) << blkbits);
702 goto get_next;
703 }
704 }
705sync_out:
706 if (allocated)
707 sync_inode_page(&dn);
708put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900709 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900710unlock_out:
711 if (create)
712 f2fs_unlock_op(sbi);
713out:
714 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
715 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900716}
717
718static int f2fs_read_data_page(struct file *file, struct page *page)
719{
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800720 struct inode *inode = page->mapping->host;
721 int ret;
722
723 /* If the file has inline data, try to read it directlly */
724 if (f2fs_has_inline_data(inode))
725 ret = f2fs_read_inline_data(inode, page);
726 else
727 ret = mpage_readpage(page, get_data_block);
728
729 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900730}
731
732static int f2fs_read_data_pages(struct file *file,
733 struct address_space *mapping,
734 struct list_head *pages, unsigned nr_pages)
735{
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800736 struct inode *inode = file->f_mapping->host;
737
738 /* If the file has inline data, skip readpages */
739 if (f2fs_has_inline_data(inode))
740 return 0;
741
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900742 return mpage_readpages(mapping, pages, nr_pages, get_data_block);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900743}
744
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900745int do_write_data_page(struct page *page, struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900746{
747 struct inode *inode = page->mapping->host;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900748 block_t old_blkaddr, new_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900749 struct dnode_of_data dn;
750 int err = 0;
751
752 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900753 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900754 if (err)
755 return err;
756
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900757 old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900758
759 /* This page is already truncated */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900760 if (old_blkaddr == NULL_ADDR)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900761 goto out_writepage;
762
763 set_page_writeback(page);
764
765 /*
766 * If current allocation needs SSR,
767 * it had better in-place writes for updated data.
768 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900769 if (unlikely(old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +0800770 !is_cold_data(page) &&
771 need_inplace_update(inode))) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900772 rewrite_data_page(page, old_blkaddr, fio);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900773 } else {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900774 write_data_page(page, &dn, &new_blkaddr, fio);
775 update_extent_cache(new_blkaddr, &dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900776 }
777out_writepage:
778 f2fs_put_dnode(&dn);
779 return err;
780}
781
782static int f2fs_write_data_page(struct page *page,
783 struct writeback_control *wbc)
784{
785 struct inode *inode = page->mapping->host;
786 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
787 loff_t i_size = i_size_read(inode);
788 const pgoff_t end_index = ((unsigned long long) i_size)
789 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800790 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900791 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900792 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900793 struct f2fs_io_info fio = {
794 .type = DATA,
795 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC: WRITE,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900796 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900797
798 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +0900799 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900800
801 /*
802 * If the offset is out-of-range of file size,
803 * this page does not have to be written to disk.
804 */
805 offset = i_size & (PAGE_CACHE_SIZE - 1);
806 if ((page->index >= end_index + 1) || !offset) {
807 if (S_ISDIR(inode->i_mode)) {
808 dec_page_count(sbi, F2FS_DIRTY_DENTS);
809 inode_dec_dirty_dents(inode);
810 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900811 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900812 }
813
814 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900815write:
Chao Yucfb271d2013-12-05 17:15:22 +0800816 if (unlikely(sbi->por_doing)) {
Jaegeuk Kim39936832012-11-22 16:21:29 +0900817 err = AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900818 goto redirty_out;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900819 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900820
Jaegeuk Kim39936832012-11-22 16:21:29 +0900821 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900822 if (S_ISDIR(inode->i_mode)) {
823 dec_page_count(sbi, F2FS_DIRTY_DENTS);
824 inode_dec_dirty_dents(inode);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900825 err = do_write_data_page(page, &fio);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900826 } else {
Gu Zhenge4795562013-09-27 18:08:30 +0800827 f2fs_lock_op(sbi);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800828
829 if (f2fs_has_inline_data(inode) || f2fs_may_inline(inode)) {
830 err = f2fs_write_inline_data(inode, page, offset);
831 f2fs_unlock_op(sbi);
832 goto out;
833 } else {
834 err = do_write_data_page(page, &fio);
835 }
836
Gu Zhenge4795562013-09-27 18:08:30 +0800837 f2fs_unlock_op(sbi);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900838 need_balance_fs = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900839 }
Jaegeuk Kim39936832012-11-22 16:21:29 +0900840 if (err == -ENOENT)
841 goto out;
842 else if (err)
843 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900844
Jaegeuk Kimc33ec322014-01-16 16:20:40 +0900845 if (wbc->for_reclaim) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900846 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimc33ec322014-01-16 16:20:40 +0900847 need_balance_fs = false;
848 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900849
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900850 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900851out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900852 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900853 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900854 f2fs_balance_fs(sbi);
855 return 0;
856
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900857redirty_out:
858 wbc->pages_skipped++;
859 set_page_dirty(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +0900860 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900861}
862
863#define MAX_DESIRED_PAGES_WP 4096
864
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900865static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
866 void *data)
867{
868 struct address_space *mapping = data;
869 int ret = mapping->a_ops->writepage(page, wbc);
870 mapping_set_error(mapping, ret);
871 return ret;
872}
873
Jaegeuk Kim25ca9232012-11-28 16:12:41 +0900874static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900875 struct writeback_control *wbc)
876{
877 struct inode *inode = mapping->host;
878 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900879 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900880 int ret;
881 long excess_nrtw = 0, desired_nrtw;
882
P J Pcfb185a2013-04-03 11:38:00 +0900883 /* deal with chardevs and other special file */
884 if (!mapping->a_ops->writepage)
885 return 0;
886
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900887 if (wbc->nr_to_write < MAX_DESIRED_PAGES_WP) {
888 desired_nrtw = MAX_DESIRED_PAGES_WP;
889 excess_nrtw = desired_nrtw - wbc->nr_to_write;
890 wbc->nr_to_write = desired_nrtw;
891 }
892
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900893 if (!S_ISDIR(inode->i_mode)) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900894 mutex_lock(&sbi->writepages);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900895 locked = true;
896 }
Namjae Jeonfa9150a2013-01-15 16:45:24 +0900897 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Jaegeuk Kim531ad7d2013-04-30 11:33:27 +0900898 if (locked)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900899 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900900
901 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900902
903 remove_dirty_dir_inode(inode);
904
905 wbc->nr_to_write -= excess_nrtw;
906 return ret;
907}
908
909static int f2fs_write_begin(struct file *file, struct address_space *mapping,
910 loff_t pos, unsigned len, unsigned flags,
911 struct page **pagep, void **fsdata)
912{
913 struct inode *inode = mapping->host;
914 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
915 struct page *page;
916 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
917 struct dnode_of_data dn;
918 int err = 0;
919
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900920 f2fs_balance_fs(sbi);
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900921repeat:
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900922 err = f2fs_convert_inline_data(inode, pos + len);
923 if (err)
924 return err;
925
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900926 page = grab_cache_page_write_begin(mapping, index, flags);
927 if (!page)
928 return -ENOMEM;
929 *pagep = page;
930
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900931 if (f2fs_has_inline_data(inode) && (pos + len) <= MAX_INLINE_DATA)
932 goto inline_data;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800933
Gu Zhenge4795562013-09-27 18:08:30 +0800934 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900935 set_new_dnode(&dn, inode, NULL, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800936 err = f2fs_reserve_block(&dn, index);
Gu Zhenge4795562013-09-27 18:08:30 +0800937 f2fs_unlock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900938
Huajun Lib6009652013-11-10 23:13:18 +0800939 if (err) {
940 f2fs_put_page(page, 1);
941 return err;
942 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800943inline_data:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900944 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
945 return 0;
946
947 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
948 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
949 unsigned end = start + len;
950
951 /* Reading beyond i_size is simple: memset to zero */
952 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900953 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900954 }
955
956 if (dn.data_blkaddr == NEW_ADDR) {
957 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
958 } else {
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800959 if (f2fs_has_inline_data(inode))
960 err = f2fs_read_inline_data(inode, page);
961 else
962 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr,
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900963 READ_SYNC);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900964 if (err)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900965 return err;
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900966 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900967 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900968 f2fs_put_page(page, 1);
969 return -EIO;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900970 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900971 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900972 f2fs_put_page(page, 1);
973 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900974 }
975 }
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900976out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900977 SetPageUptodate(page);
978 clear_cold_data(page);
979 return 0;
980}
981
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +0900982static int f2fs_write_end(struct file *file,
983 struct address_space *mapping,
984 loff_t pos, unsigned len, unsigned copied,
985 struct page *page, void *fsdata)
986{
987 struct inode *inode = page->mapping->host;
988
989 SetPageUptodate(page);
990 set_page_dirty(page);
991
992 if (pos + copied > i_size_read(inode)) {
993 i_size_write(inode, pos + copied);
994 mark_inode_dirty(inode);
995 update_inode_page(inode);
996 }
997
Chao Yu75c3c8b2013-11-16 14:15:59 +0800998 f2fs_put_page(page, 1);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +0900999 return copied;
1000}
1001
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001002static int check_direct_IO(struct inode *inode, int rw,
1003 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
1004{
1005 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
1006 int i;
1007
1008 if (rw == READ)
1009 return 0;
1010
1011 if (offset & blocksize_mask)
1012 return -EINVAL;
1013
1014 for (i = 0; i < nr_segs; i++)
1015 if (iov[i].iov_len & blocksize_mask)
1016 return -EINVAL;
1017 return 0;
1018}
1019
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001020static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
1021 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
1022{
1023 struct file *file = iocb->ki_filp;
1024 struct inode *inode = file->f_mapping->host;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001025
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001026 /* Let buffer I/O handle the inline data case. */
1027 if (f2fs_has_inline_data(inode))
1028 return 0;
1029
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001030 if (check_direct_IO(inode, rw, iov, offset, nr_segs))
1031 return 0;
1032
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001033 return blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001034 get_data_block);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001035}
1036
Lukas Czernerd47992f2013-05-21 23:17:23 -04001037static void f2fs_invalidate_data_page(struct page *page, unsigned int offset,
1038 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001039{
1040 struct inode *inode = page->mapping->host;
1041 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1042 if (S_ISDIR(inode->i_mode) && PageDirty(page)) {
1043 dec_page_count(sbi, F2FS_DIRTY_DENTS);
1044 inode_dec_dirty_dents(inode);
1045 }
1046 ClearPagePrivate(page);
1047}
1048
1049static int f2fs_release_data_page(struct page *page, gfp_t wait)
1050{
1051 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001052 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001053}
1054
1055static int f2fs_set_data_page_dirty(struct page *page)
1056{
1057 struct address_space *mapping = page->mapping;
1058 struct inode *inode = mapping->host;
1059
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001060 trace_f2fs_set_page_dirty(page, DATA);
1061
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001062 SetPageUptodate(page);
1063 if (!PageDirty(page)) {
1064 __set_page_dirty_nobuffers(page);
1065 set_dirty_dir_page(inode, page);
1066 return 1;
1067 }
1068 return 0;
1069}
1070
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001071static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1072{
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001073 return generic_block_bmap(mapping, block, get_data_block);
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001074}
1075
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001076const struct address_space_operations f2fs_dblock_aops = {
1077 .readpage = f2fs_read_data_page,
1078 .readpages = f2fs_read_data_pages,
1079 .writepage = f2fs_write_data_page,
1080 .writepages = f2fs_write_data_pages,
1081 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001082 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001083 .set_page_dirty = f2fs_set_data_page_dirty,
1084 .invalidatepage = f2fs_invalidate_data_page,
1085 .releasepage = f2fs_release_data_page,
1086 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001087 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001088};