blob: 0cce322646e6dff134beab61d929c63096425b52 [file] [log] [blame]
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001/*
2 * 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>
Evan McClainf3f030d2014-07-17 21:16:35 -040015#include <linux/aio.h>
Linus Torvalds8005ecc2012-12-20 13:54:51 -080016#include <linux/writeback.h>
17#include <linux/backing-dev.h>
18#include <linux/blkdev.h>
19#include <linux/bio.h>
20#include <linux/prefetch.h>
21
22#include "f2fs.h"
23#include "node.h"
24#include "segment.h"
25#include <trace/events/f2fs.h>
26
Changman Leeb1a94e82013-11-15 10:42:51 +090027static void f2fs_read_end_io(struct bio *bio, int err)
28{
Evan McClainf3f030d2014-07-17 21:16:35 -040029 struct bio_vec *bvec;
30 int i;
Changman Leeb1a94e82013-11-15 10:42:51 +090031
Evan McClainf3f030d2014-07-17 21:16:35 -040032 __bio_for_each_segment(bvec, bio, i, 0) {
Changman Leeb1a94e82013-11-15 10:42:51 +090033 struct page *page = bvec->bv_page;
34
Evan McClainf3f030d2014-07-17 21:16:35 -040035 if (!err) {
36 SetPageUptodate(page);
37 } else {
Changman Leeb1a94e82013-11-15 10:42:51 +090038 ClearPageUptodate(page);
39 SetPageError(page);
Changman Leeb1a94e82013-11-15 10:42:51 +090040 }
41 unlock_page(page);
Evan McClainf3f030d2014-07-17 21:16:35 -040042 }
Changman Leeb1a94e82013-11-15 10:42:51 +090043 bio_put(bio);
44}
45
46static void f2fs_write_end_io(struct bio *bio, int err)
47{
Jaegeuk Kim080c4632014-02-03 10:50:22 +090048 struct f2fs_sb_info *sbi = bio->bi_private;
Evan McClainf3f030d2014-07-17 21:16:35 -040049 struct bio_vec *bvec;
50 int i;
Changman Leeb1a94e82013-11-15 10:42:51 +090051
Evan McClainf3f030d2014-07-17 21:16:35 -040052 __bio_for_each_segment(bvec, bio, i, 0) {
Changman Leeb1a94e82013-11-15 10:42:51 +090053 struct page *page = bvec->bv_page;
54
Evan McClainf3f030d2014-07-17 21:16:35 -040055 if (unlikely(err)) {
Jaegeuk Kim5da13f32014-08-11 18:37:46 -070056 set_page_dirty(page);
Changman Leeb1a94e82013-11-15 10:42:51 +090057 set_bit(AS_EIO, &page->mapping->flags);
58 set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
59 sbi->sb->s_flags |= MS_RDONLY;
60 }
61 end_page_writeback(page);
62 dec_page_count(sbi, F2FS_WRITEBACK);
Evan McClainf3f030d2014-07-17 21:16:35 -040063 }
Changman Leeb1a94e82013-11-15 10:42:51 +090064
Jaegeuk Kim080c4632014-02-03 10:50:22 +090065 if (sbi->wait_io) {
66 complete(sbi->wait_io);
67 sbi->wait_io = NULL;
68 }
Changman Leeb1a94e82013-11-15 10:42:51 +090069
70 if (!get_pages(sbi, F2FS_WRITEBACK) &&
71 !list_empty(&sbi->cp_wait.task_list))
72 wake_up(&sbi->cp_wait);
73
74 bio_put(bio);
75}
76
77/*
78 * Low-level block read/write IO operations.
79 */
80static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
81 int npages, bool is_read)
82{
83 struct bio *bio;
84
85 /* No failure on bio allocation */
86 bio = bio_alloc(GFP_NOIO, npages);
87
88 bio->bi_bdev = sbi->sb->s_bdev;
89 bio->bi_sector = SECTOR_FROM_BLOCK(sbi, blk_addr);
90 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Jaegeuk Kim080c4632014-02-03 10:50:22 +090091 bio->bi_private = sbi;
Changman Leeb1a94e82013-11-15 10:42:51 +090092
93 return bio;
94}
95
96static void __submit_merged_bio(struct f2fs_bio_info *io)
97{
98 struct f2fs_io_info *fio = &io->fio;
99 int rw;
100
101 if (!io->bio)
102 return;
103
104 rw = fio->rw;
105
106 if (is_read_io(rw)) {
107 trace_f2fs_submit_read_bio(io->sbi->sb, rw,
108 fio->type, io->bio);
109 submit_bio(rw, io->bio);
110 } else {
111 trace_f2fs_submit_write_bio(io->sbi->sb, rw,
112 fio->type, io->bio);
113 /*
114 * META_FLUSH is only from the checkpoint procedure, and we
115 * should wait this metadata bio for FS consistency.
116 */
117 if (fio->type == META_FLUSH) {
118 DECLARE_COMPLETION_ONSTACK(wait);
Jaegeuk Kim080c4632014-02-03 10:50:22 +0900119 io->sbi->wait_io = &wait;
Changman Leeb1a94e82013-11-15 10:42:51 +0900120 submit_bio(rw, io->bio);
121 wait_for_completion(&wait);
122 } else {
123 submit_bio(rw, io->bio);
124 }
125 }
126
127 io->bio = NULL;
128}
129
130void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
131 enum page_type type, int rw)
132{
133 enum page_type btype = PAGE_TYPE_OF_BIO(type);
134 struct f2fs_bio_info *io;
135
136 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
137
Evan McClainf3f030d2014-07-17 21:16:35 -0400138 down_write(&io->io_rwsem);
Changman Leeb1a94e82013-11-15 10:42:51 +0900139
140 /* change META to META_FLUSH in the checkpoint procedure */
141 if (type >= META_FLUSH) {
142 io->fio.type = META_FLUSH;
Jaegeuk Kim6f6541b2014-07-23 09:57:31 -0700143 if (test_opt(sbi, NOBARRIER))
144 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
145 else
146 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Changman Leeb1a94e82013-11-15 10:42:51 +0900147 }
148 __submit_merged_bio(io);
Evan McClainf3f030d2014-07-17 21:16:35 -0400149 up_write(&io->io_rwsem);
Changman Leeb1a94e82013-11-15 10:42:51 +0900150}
151
152/*
153 * Fill the locked page with data located in the block address.
154 * Return unlocked page.
155 */
156int f2fs_submit_page_bio(struct f2fs_sb_info *sbi, struct page *page,
157 block_t blk_addr, int rw)
158{
159 struct bio *bio;
160
161 trace_f2fs_submit_page_bio(page, blk_addr, rw);
162
163 /* Allocate a new bio */
164 bio = __bio_alloc(sbi, blk_addr, 1, is_read_io(rw));
165
166 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
167 bio_put(bio);
168 f2fs_put_page(page, 1);
169 return -EFAULT;
170 }
171
172 submit_bio(rw, bio);
173 return 0;
174}
175
176void f2fs_submit_page_mbio(struct f2fs_sb_info *sbi, struct page *page,
177 block_t blk_addr, struct f2fs_io_info *fio)
178{
179 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
180 struct f2fs_bio_info *io;
181 bool is_read = is_read_io(fio->rw);
182
183 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
184
185 verify_block_addr(sbi, blk_addr);
186
Evan McClainf3f030d2014-07-17 21:16:35 -0400187 down_write(&io->io_rwsem);
Changman Leeb1a94e82013-11-15 10:42:51 +0900188
189 if (!is_read)
190 inc_page_count(sbi, F2FS_WRITEBACK);
191
192 if (io->bio && (io->last_block_in_bio != blk_addr - 1 ||
193 io->fio.rw != fio->rw))
194 __submit_merged_bio(io);
195alloc_new:
196 if (io->bio == NULL) {
197 int bio_blocks = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
198
199 io->bio = __bio_alloc(sbi, blk_addr, bio_blocks, is_read);
200 io->fio = *fio;
201 }
202
203 if (bio_add_page(io->bio, page, PAGE_CACHE_SIZE, 0) <
204 PAGE_CACHE_SIZE) {
205 __submit_merged_bio(io);
206 goto alloc_new;
207 }
208
209 io->last_block_in_bio = blk_addr;
210
Evan McClainf3f030d2014-07-17 21:16:35 -0400211 up_write(&io->io_rwsem);
Changman Leeb1a94e82013-11-15 10:42:51 +0900212 trace_f2fs_submit_page_mbio(page, fio->rw, fio->type, blk_addr);
213}
214
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800215/*
216 * Lock ordering for the change of data block address:
217 * ->data_page
218 * ->node_page
219 * update block addresses in the node page
220 */
221static void __set_data_blkaddr(struct dnode_of_data *dn, block_t new_addr)
222{
223 struct f2fs_node *rn;
224 __le32 *addr_array;
225 struct page *node_page = dn->node_page;
226 unsigned int ofs_in_node = dn->ofs_in_node;
227
Changman Leeb1a94e82013-11-15 10:42:51 +0900228 f2fs_wait_on_page_writeback(node_page, NODE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800229
230 rn = F2FS_NODE(node_page);
231
232 /* Get physical address of data block */
233 addr_array = blkaddr_in_node(rn);
234 addr_array[ofs_in_node] = cpu_to_le32(new_addr);
235 set_page_dirty(node_page);
236}
237
238int reserve_new_block(struct dnode_of_data *dn)
239{
240 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
241
Changman Leeb1a94e82013-11-15 10:42:51 +0900242 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800243 return -EPERM;
Changman Leeb1a94e82013-11-15 10:42:51 +0900244 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800245 return -ENOSPC;
246
247 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
248
249 __set_data_blkaddr(dn, NEW_ADDR);
250 dn->data_blkaddr = NEW_ADDR;
Changman Leeb1a94e82013-11-15 10:42:51 +0900251 mark_inode_dirty(dn->inode);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800252 sync_inode_page(dn);
253 return 0;
254}
255
Changman Leeb1a94e82013-11-15 10:42:51 +0900256int 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
261 /* if inode_page exists, index should be zero */
262 f2fs_bug_on(!need_put && index);
263
264 err = get_dnode_of_data(dn, index, ALLOC_NODE);
265 if (err)
266 return err;
267
268 if (dn->data_blkaddr == NULL_ADDR)
269 err = reserve_new_block(dn);
270 if (err || need_put)
271 f2fs_put_dnode(dn);
272 return err;
273}
274
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800275static 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);
279 pgoff_t start_fofs, end_fofs;
280 block_t start_blkaddr;
281
Changman Leeb1a94e82013-11-15 10:42:51 +0900282 if (is_inode_flag_set(fi, FI_NO_EXTENT))
283 return 0;
284
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800285 read_lock(&fi->ext.ext_lock);
286 if (fi->ext.len == 0) {
287 read_unlock(&fi->ext.ext_lock);
288 return 0;
289 }
290
291 stat_inc_total_hit(inode->i_sb);
292
293 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
310 stat_inc_read_hit(inode->i_sb);
311 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;
Changman Leeb1a94e82013-11-15 10:42:51 +0900323 int need_update = true;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800324
325 f2fs_bug_on(blk_addr == NEW_ADDR);
326 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
327 dn->ofs_in_node;
328
329 /* Update the page address in the parent node */
330 __set_data_blkaddr(dn, blk_addr);
331
Changman Leeb1a94e82013-11-15 10:42:51 +0900332 if (is_inode_flag_set(fi, FI_NO_EXTENT))
333 return;
334
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800335 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
356 /* Front merge */
357 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 }
Changman Leeb1a94e82013-11-15 10:42:51 +0900381 } else {
382 need_update = false;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800383 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800384
Changman Leeb1a94e82013-11-15 10:42:51 +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 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800391end_update:
392 write_unlock(&fi->ext.ext_lock);
Changman Leeb1a94e82013-11-15 10:42:51 +0900393 if (need_update)
394 sync_inode_page(dn);
395 return;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800396}
397
398struct page *find_data_page(struct inode *inode, pgoff_t index, bool sync)
399{
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);
412 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
413 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 */
Changman Leeb1a94e82013-11-15 10:42:51 +0900421 if (unlikely(dn.data_blkaddr == NEW_ADDR))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800422 return ERR_PTR(-EINVAL);
423
Jaegeuk Kim767fa502014-04-29 17:35:10 +0900424 page = grab_cache_page(mapping, index);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800425 if (!page)
426 return ERR_PTR(-ENOMEM);
427
428 if (PageUptodate(page)) {
429 unlock_page(page);
430 return page;
431 }
432
Changman Leeb1a94e82013-11-15 10:42:51 +0900433 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr,
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800434 sync ? READ_SYNC : READA);
Changman Leeb1a94e82013-11-15 10:42:51 +0900435 if (err)
436 return ERR_PTR(err);
437
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800438 if (sync) {
439 wait_on_page_locked(page);
Changman Leeb1a94e82013-11-15 10:42:51 +0900440 if (unlikely(!PageUptodate(page))) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800441 f2fs_put_page(page, 0);
442 return ERR_PTR(-EIO);
443 }
444 }
445 return page;
446}
447
448/*
449 * 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
461repeat:
Jaegeuk Kim767fa502014-04-29 17:35:10 +0900462 page = grab_cache_page(mapping, index);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800463 if (!page)
464 return ERR_PTR(-ENOMEM);
465
466 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
Changman Leeb1a94e82013-11-15 10:42:51 +0900474 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800475 f2fs_put_page(page, 1);
476 return ERR_PTR(-ENOENT);
477 }
478
479 if (PageUptodate(page))
480 return page;
481
482 /*
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 }
493
Changman Leeb1a94e82013-11-15 10:42:51 +0900494 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr, READ_SYNC);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800495 if (err)
496 return ERR_PTR(err);
497
498 lock_page(page);
Changman Leeb1a94e82013-11-15 10:42:51 +0900499 if (unlikely(!PageUptodate(page))) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800500 f2fs_put_page(page, 1);
501 return ERR_PTR(-EIO);
502 }
Changman Leeb1a94e82013-11-15 10:42:51 +0900503 if (unlikely(page->mapping != mapping)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800504 f2fs_put_page(page, 1);
505 goto repeat;
506 }
507 return page;
508}
509
510/*
511 * Caller ensures that this data page is never allocated.
512 * A new zero-filled data page is allocated in the page cache.
513 *
Changman Leeb1a94e82013-11-15 10:42:51 +0900514 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
515 * f2fs_unlock_op().
516 * Note that, ipage is set only by make_empty_dir.
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800517 */
518struct page *get_new_data_page(struct inode *inode,
Changman Leeb1a94e82013-11-15 10:42:51 +0900519 struct page *ipage, pgoff_t index, bool new_i_size)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800520{
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
Changman Leeb1a94e82013-11-15 10:42:51 +0900527 set_new_dnode(&dn, inode, ipage, NULL, 0);
528 err = f2fs_reserve_block(&dn, index);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800529 if (err)
530 return ERR_PTR(err);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800531repeat:
532 page = grab_cache_page(mapping, index);
Changman Leeb1a94e82013-11-15 10:42:51 +0900533 if (!page) {
534 err = -ENOMEM;
535 goto put_err;
536 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800537
538 if (PageUptodate(page))
539 return page;
540
541 if (dn.data_blkaddr == NEW_ADDR) {
542 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
543 SetPageUptodate(page);
544 } else {
Changman Leeb1a94e82013-11-15 10:42:51 +0900545 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr,
546 READ_SYNC);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800547 if (err)
Changman Leeb1a94e82013-11-15 10:42:51 +0900548 goto put_err;
549
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800550 lock_page(page);
Changman Leeb1a94e82013-11-15 10:42:51 +0900551 if (unlikely(!PageUptodate(page))) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800552 f2fs_put_page(page, 1);
Changman Leeb1a94e82013-11-15 10:42:51 +0900553 err = -EIO;
554 goto put_err;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800555 }
Changman Leeb1a94e82013-11-15 10:42:51 +0900556 if (unlikely(page->mapping != mapping)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800557 f2fs_put_page(page, 1);
558 goto repeat;
559 }
560 }
561
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));
565 /* Only the directory inode sets new_i_size */
566 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800567 }
568 return page;
Changman Leeb1a94e82013-11-15 10:42:51 +0900569
570put_err:
571 f2fs_put_dnode(&dn);
572 return ERR_PTR(err);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800573}
574
Changman Leeb1a94e82013-11-15 10:42:51 +0900575static int __allocate_data_block(struct dnode_of_data *dn)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800576{
Changman Leeb1a94e82013-11-15 10:42:51 +0900577 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
578 struct f2fs_summary sum;
579 block_t new_blkaddr;
580 struct node_info ni;
581 int type;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800582
Changman Leeb1a94e82013-11-15 10:42:51 +0900583 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
584 return -EPERM;
585 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
586 return -ENOSPC;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800587
Changman Leeb1a94e82013-11-15 10:42:51 +0900588 __set_data_blkaddr(dn, NEW_ADDR);
589 dn->data_blkaddr = NEW_ADDR;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800590
Changman Leeb1a94e82013-11-15 10:42:51 +0900591 get_node_info(sbi, dn->nid, &ni);
592 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800593
Changman Leeb1a94e82013-11-15 10:42:51 +0900594 type = CURSEG_WARM_DATA;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800595
Changman Leeb1a94e82013-11-15 10:42:51 +0900596 allocate_data_block(sbi, NULL, NULL_ADDR, &new_blkaddr, &sum, type);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800597
Changman Leeb1a94e82013-11-15 10:42:51 +0900598 /* direct IO doesn't use extent cache to maximize the performance */
599 set_inode_flag(F2FS_I(dn->inode), FI_NO_EXTENT);
600 update_extent_cache(new_blkaddr, dn);
601 clear_inode_flag(F2FS_I(dn->inode), FI_NO_EXTENT);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800602
Changman Leeb1a94e82013-11-15 10:42:51 +0900603 dn->data_blkaddr = new_blkaddr;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800604 return 0;
605}
606
607/*
Changman Leeb1a94e82013-11-15 10:42:51 +0900608 * get_data_block() now supported readahead/bmap/rw direct_IO with mapped bh.
609 * If original data blocks are allocated, then give them to blockdev.
610 * Otherwise,
611 * a. preallocate requested block addresses
612 * b. do not use extent cache for better performance
613 * c. give the block addresses to blockdev
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800614 */
Jaegeuk Kim8728de52014-06-13 13:02:11 +0900615static int __get_data_block(struct inode *inode, sector_t iblock,
616 struct buffer_head *bh_result, int create, bool fiemap)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800617{
Changman Leeb1a94e82013-11-15 10:42:51 +0900618 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800619 unsigned int blkbits = inode->i_sb->s_blocksize_bits;
620 unsigned maxblocks = bh_result->b_size >> blkbits;
621 struct dnode_of_data dn;
Changman Leeb1a94e82013-11-15 10:42:51 +0900622 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
623 pgoff_t pgofs, end_offset;
624 int err = 0, ofs = 1;
625 bool allocated = false;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800626
627 /* Get the page offset from the block offset(iblock) */
628 pgofs = (pgoff_t)(iblock >> (PAGE_CACHE_SHIFT - blkbits));
629
Changman Leeb1a94e82013-11-15 10:42:51 +0900630 if (check_extent_cache(inode, pgofs, bh_result))
631 goto out;
632
Huang Ying3a8e4f52014-07-12 20:10:00 +0800633 if (create) {
634 f2fs_balance_fs(sbi);
Changman Leeb1a94e82013-11-15 10:42:51 +0900635 f2fs_lock_op(sbi);
Huang Ying3a8e4f52014-07-12 20:10:00 +0800636 }
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800637
638 /* When reading holes, we need its node page */
639 set_new_dnode(&dn, inode, NULL, NULL, 0);
Changman Leeb1a94e82013-11-15 10:42:51 +0900640 err = get_dnode_of_data(&dn, pgofs, mode);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800641 if (err) {
Changman Leeb1a94e82013-11-15 10:42:51 +0900642 if (err == -ENOENT)
643 err = 0;
644 goto unlock_out;
645 }
Jaegeuk Kim8728de52014-06-13 13:02:11 +0900646 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Changman Leeb1a94e82013-11-15 10:42:51 +0900647 goto put_out;
648
649 if (dn.data_blkaddr != NULL_ADDR) {
650 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
651 } else if (create) {
652 err = __allocate_data_block(&dn);
653 if (err)
654 goto put_out;
655 allocated = true;
656 map_bh(bh_result, inode->i_sb, dn.data_blkaddr);
657 } else {
658 goto put_out;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800659 }
660
Chao Yu90e712d2014-04-26 19:59:52 +0800661 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Changman Leeb1a94e82013-11-15 10:42:51 +0900662 bh_result->b_size = (((size_t)1) << blkbits);
663 dn.ofs_in_node++;
664 pgofs++;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800665
Changman Leeb1a94e82013-11-15 10:42:51 +0900666get_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);
675 if (err) {
676 if (err == -ENOENT)
677 err = 0;
678 goto unlock_out;
679 }
Jaegeuk Kim8728de52014-06-13 13:02:11 +0900680 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Changman Leeb1a94e82013-11-15 10:42:51 +0900681 goto put_out;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800682
Chao Yu90e712d2014-04-26 19:59:52 +0800683 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800684 }
Changman Leeb1a94e82013-11-15 10:42:51 +0900685
686 if (maxblocks > (bh_result->b_size >> blkbits)) {
687 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
688 if (blkaddr == NULL_ADDR && create) {
689 err = __allocate_data_block(&dn);
690 if (err)
691 goto sync_out;
692 allocated = true;
693 blkaddr = dn.data_blkaddr;
694 }
arter97f4081402014-08-06 23:22:50 +0900695 /* Give more consecutive addresses for the readahead */
Changman Leeb1a94e82013-11-15 10:42:51 +0900696 if (blkaddr == (bh_result->b_blocknr + ofs)) {
697 ofs++;
698 dn.ofs_in_node++;
699 pgofs++;
700 bh_result->b_size += (((size_t)1) << blkbits);
701 goto get_next;
702 }
703 }
704sync_out:
705 if (allocated)
706 sync_inode_page(&dn);
707put_out:
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800708 f2fs_put_dnode(&dn);
Changman Leeb1a94e82013-11-15 10:42:51 +0900709unlock_out:
710 if (create)
711 f2fs_unlock_op(sbi);
712out:
713 trace_f2fs_get_data_block(inode, iblock, bh_result, err);
714 return err;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800715}
716
Jaegeuk Kim8728de52014-06-13 13:02:11 +0900717static int get_data_block(struct inode *inode, sector_t iblock,
718 struct buffer_head *bh_result, int create)
719{
720 return __get_data_block(inode, iblock, bh_result, create, false);
721}
722
723static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
724 struct buffer_head *bh_result, int create)
725{
726 return __get_data_block(inode, iblock, bh_result, create, true);
727}
728
Jaegeuk Kim8ff1d522014-06-08 04:30:14 +0900729int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
730 u64 start, u64 len)
731{
Jaegeuk Kim8728de52014-06-13 13:02:11 +0900732 return generic_block_fiemap(inode, fieinfo,
733 start, len, get_data_block_fiemap);
Jaegeuk Kim8ff1d522014-06-08 04:30:14 +0900734}
735
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800736static int f2fs_read_data_page(struct file *file, struct page *page)
737{
Changman Leeb1a94e82013-11-15 10:42:51 +0900738 struct inode *inode = page->mapping->host;
739 int ret;
740
Chao Yu12d38fc2014-05-06 16:53:08 +0800741 trace_f2fs_readpage(page, DATA);
742
arter97f4081402014-08-06 23:22:50 +0900743 /* If the file has inline data, try to read it directly */
Changman Leeb1a94e82013-11-15 10:42:51 +0900744 if (f2fs_has_inline_data(inode))
745 ret = f2fs_read_inline_data(inode, page);
746 else
747 ret = mpage_readpage(page, get_data_block);
748
749 return ret;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800750}
751
752static int f2fs_read_data_pages(struct file *file,
753 struct address_space *mapping,
754 struct list_head *pages, unsigned nr_pages)
755{
Changman Leeb1a94e82013-11-15 10:42:51 +0900756 struct inode *inode = file->f_mapping->host;
757
758 /* If the file has inline data, skip readpages */
759 if (f2fs_has_inline_data(inode))
760 return 0;
761
762 return mpage_readpages(mapping, pages, nr_pages, get_data_block);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800763}
764
Changman Leeb1a94e82013-11-15 10:42:51 +0900765int do_write_data_page(struct page *page, struct f2fs_io_info *fio)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800766{
767 struct inode *inode = page->mapping->host;
Changman Leeb1a94e82013-11-15 10:42:51 +0900768 block_t old_blkaddr, new_blkaddr;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800769 struct dnode_of_data dn;
770 int err = 0;
771
772 set_new_dnode(&dn, inode, NULL, NULL, 0);
773 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
774 if (err)
775 return err;
776
Changman Leeb1a94e82013-11-15 10:42:51 +0900777 old_blkaddr = dn.data_blkaddr;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800778
779 /* This page is already truncated */
Changman Leeb1a94e82013-11-15 10:42:51 +0900780 if (old_blkaddr == NULL_ADDR)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800781 goto out_writepage;
782
783 set_page_writeback(page);
784
785 /*
786 * If current allocation needs SSR,
787 * it had better in-place writes for updated data.
788 */
Changman Leeb1a94e82013-11-15 10:42:51 +0900789 if (unlikely(old_blkaddr != NEW_ADDR &&
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800790 !is_cold_data(page) &&
791 need_inplace_update(inode))) {
Changman Leeb1a94e82013-11-15 10:42:51 +0900792 rewrite_data_page(page, old_blkaddr, fio);
Jaegeuk Kim7ea36952014-07-25 07:40:59 -0700793 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800794 } else {
Changman Leeb1a94e82013-11-15 10:42:51 +0900795 write_data_page(page, &dn, &new_blkaddr, fio);
796 update_extent_cache(new_blkaddr, &dn);
Jaegeuk Kim7ea36952014-07-25 07:40:59 -0700797 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800798 }
799out_writepage:
800 f2fs_put_dnode(&dn);
801 return err;
802}
803
804static int f2fs_write_data_page(struct page *page,
805 struct writeback_control *wbc)
806{
807 struct inode *inode = page->mapping->host;
808 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
809 loff_t i_size = i_size_read(inode);
810 const pgoff_t end_index = ((unsigned long long) i_size)
811 >> PAGE_CACHE_SHIFT;
Changman Leeb1a94e82013-11-15 10:42:51 +0900812 unsigned offset = 0;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800813 bool need_balance_fs = false;
814 int err = 0;
Changman Leeb1a94e82013-11-15 10:42:51 +0900815 struct f2fs_io_info fio = {
816 .type = DATA,
817 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
818 };
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800819
Chao Yu327cb6d2014-05-06 16:48:26 +0800820 trace_f2fs_writepage(page, DATA);
821
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800822 if (page->index < end_index)
823 goto write;
824
825 /*
826 * If the offset is out-of-range of file size,
827 * this page does not have to be written to disk.
828 */
829 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kimdc8c2462014-04-15 16:04:15 +0900830 if ((page->index >= end_index + 1) || !offset)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800831 goto out;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800832
833 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
834write:
Jaegeuk Kim78ae57e2014-02-17 19:29:27 +0900835 if (unlikely(sbi->por_doing))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800836 goto redirty_out;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800837
838 /* Dentry blocks are controlled by checkpoint */
839 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim5da13f32014-08-11 18:37:46 -0700840 if (unlikely(f2fs_cp_error(sbi)))
841 goto redirty_out;
Changman Leeb1a94e82013-11-15 10:42:51 +0900842 err = do_write_data_page(page, &fio);
Jaegeuk Kim78ae57e2014-02-17 19:29:27 +0900843 goto done;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800844 }
Jaegeuk Kim78ae57e2014-02-17 19:29:27 +0900845
Jaegeuk Kim5da13f32014-08-11 18:37:46 -0700846 /* we should bypass data pages to proceed the kworkder jobs */
847 if (unlikely(f2fs_cp_error(sbi))) {
848 SetPageError(page);
849 unlock_page(page);
850 return 0;
851 }
852
Jaegeuk Kim78ae57e2014-02-17 19:29:27 +0900853 if (!wbc->for_reclaim)
854 need_balance_fs = true;
855 else if (has_not_enough_free_secs(sbi, 0))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800856 goto redirty_out;
857
Jaegeuk Kim78ae57e2014-02-17 19:29:27 +0900858 f2fs_lock_op(sbi);
859 if (f2fs_has_inline_data(inode) || f2fs_may_inline(inode))
860 err = f2fs_write_inline_data(inode, page, offset);
861 else
862 err = do_write_data_page(page, &fio);
863 f2fs_unlock_op(sbi);
864done:
865 if (err && err != -ENOENT)
866 goto redirty_out;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800867
868 clear_cold_data(page);
869out:
Jaegeuk Kimdc8c2462014-04-15 16:04:15 +0900870 inode_dec_dirty_dents(inode);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800871 unlock_page(page);
872 if (need_balance_fs)
873 f2fs_balance_fs(sbi);
Jaegeuk Kim8edabc72014-04-24 09:49:52 +0900874 if (wbc->for_reclaim)
875 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800876 return 0;
877
878redirty_out:
Jaegeuk Kimdc8c2462014-04-15 16:04:15 +0900879 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim78ae57e2014-02-17 19:29:27 +0900880 return AOP_WRITEPAGE_ACTIVATE;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800881}
882
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800883static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
884 void *data)
885{
886 struct address_space *mapping = data;
887 int ret = mapping->a_ops->writepage(page, wbc);
888 mapping_set_error(mapping, ret);
889 return ret;
890}
891
892static int f2fs_write_data_pages(struct address_space *mapping,
893 struct writeback_control *wbc)
894{
895 struct inode *inode = mapping->host;
896 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
897 bool locked = false;
898 int ret;
Jaegeuk Kim1f1eaf42014-03-18 13:47:11 +0900899 long diff;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800900
Chao Yub4d85492014-05-06 16:51:24 +0800901 trace_f2fs_writepages(mapping->host, wbc, DATA);
902
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800903 /* deal with chardevs and other special file */
904 if (!mapping->a_ops->writepage)
905 return 0;
906
Jaegeuk Kimc7f9f432014-03-18 12:40:49 +0900907 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kim250c7692014-04-16 10:47:06 +0900908 get_dirty_dents(inode) < nr_pages_to_skip(sbi, DATA) &&
909 available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kim823d59f2014-03-18 13:43:05 +0900910 goto skip_write;
Jaegeuk Kimc7f9f432014-03-18 12:40:49 +0900911
Jaegeuk Kim1f1eaf42014-03-18 13:47:11 +0900912 diff = nr_pages_to_write(sbi, DATA, wbc);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800913
914 if (!S_ISDIR(inode->i_mode)) {
915 mutex_lock(&sbi->writepages);
916 locked = true;
917 }
918 ret = write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
919 if (locked)
920 mutex_unlock(&sbi->writepages);
Changman Leeb1a94e82013-11-15 10:42:51 +0900921
922 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800923
924 remove_dirty_dir_inode(inode);
925
Jaegeuk Kim1f1eaf42014-03-18 13:47:11 +0900926 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800927 return ret;
Jaegeuk Kim823d59f2014-03-18 13:43:05 +0900928
929skip_write:
930 wbc->pages_skipped += get_dirty_dents(inode);
931 return 0;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800932}
933
Evan McClainf3f030d2014-07-17 21:16:35 -0400934static void f2fs_write_failed(struct address_space *mapping, loff_t to)
935{
936 struct inode *inode = mapping->host;
937
938 if (to > inode->i_size) {
939 truncate_pagecache(inode, 0, inode->i_size);
940 truncate_blocks(inode, inode->i_size);
941 }
942}
943
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800944static int f2fs_write_begin(struct file *file, struct address_space *mapping,
945 loff_t pos, unsigned len, unsigned flags,
946 struct page **pagep, void **fsdata)
947{
948 struct inode *inode = mapping->host;
949 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
950 struct page *page;
951 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
952 struct dnode_of_data dn;
953 int err = 0;
954
Chao Yudf7a5962014-05-06 16:46:04 +0800955 trace_f2fs_write_begin(inode, pos, len, flags);
956
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800957 f2fs_balance_fs(sbi);
958repeat:
Jaegeuk Kim80fd22a2014-08-07 16:32:25 -0700959 err = f2fs_convert_inline_data(inode, pos + len, NULL);
Changman Leeb1a94e82013-11-15 10:42:51 +0900960 if (err)
Evan McClainf3f030d2014-07-17 21:16:35 -0400961 goto fail;
Changman Leeb1a94e82013-11-15 10:42:51 +0900962
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800963 page = grab_cache_page_write_begin(mapping, index, flags);
Evan McClainf3f030d2014-07-17 21:16:35 -0400964 if (!page) {
965 err = -ENOMEM;
966 goto fail;
967 }
Jaegeuk Kimb3052cf2014-04-30 09:22:45 +0900968
969 /* to avoid latency during memory pressure */
970 unlock_page(page);
971
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800972 *pagep = page;
973
Changman Leeb1a94e82013-11-15 10:42:51 +0900974 if (f2fs_has_inline_data(inode) && (pos + len) <= MAX_INLINE_DATA)
975 goto inline_data;
976
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800977 f2fs_lock_op(sbi);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800978 set_new_dnode(&dn, inode, NULL, NULL, 0);
Changman Leeb1a94e82013-11-15 10:42:51 +0900979 err = f2fs_reserve_block(&dn, index);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800980 f2fs_unlock_op(sbi);
Changman Leeb1a94e82013-11-15 10:42:51 +0900981 if (err) {
Jaegeuk Kimb3052cf2014-04-30 09:22:45 +0900982 f2fs_put_page(page, 0);
Evan McClainf3f030d2014-07-17 21:16:35 -0400983 goto fail;
Changman Leeb1a94e82013-11-15 10:42:51 +0900984 }
985inline_data:
Jaegeuk Kimb3052cf2014-04-30 09:22:45 +0900986 lock_page(page);
987 if (unlikely(page->mapping != mapping)) {
988 f2fs_put_page(page, 1);
989 goto repeat;
990 }
991
992 f2fs_wait_on_page_writeback(page, DATA);
993
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800994 if ((len == PAGE_CACHE_SIZE) || PageUptodate(page))
995 return 0;
996
997 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
998 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
999 unsigned end = start + len;
1000
1001 /* Reading beyond i_size is simple: memset to zero */
1002 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
1003 goto out;
1004 }
1005
1006 if (dn.data_blkaddr == NEW_ADDR) {
1007 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1008 } else {
Chao Yu3d1d6d92014-03-29 15:30:40 +08001009 if (f2fs_has_inline_data(inode)) {
Changman Leeb1a94e82013-11-15 10:42:51 +09001010 err = f2fs_read_inline_data(inode, page);
Chao Yu3d1d6d92014-03-29 15:30:40 +08001011 if (err) {
1012 page_cache_release(page);
Evan McClainf3f030d2014-07-17 21:16:35 -04001013 goto fail;
Chao Yu3d1d6d92014-03-29 15:30:40 +08001014 }
1015 } else {
Changman Leeb1a94e82013-11-15 10:42:51 +09001016 err = f2fs_submit_page_bio(sbi, page, dn.data_blkaddr,
1017 READ_SYNC);
Chao Yu3d1d6d92014-03-29 15:30:40 +08001018 if (err)
Evan McClainf3f030d2014-07-17 21:16:35 -04001019 goto fail;
Chao Yu3d1d6d92014-03-29 15:30:40 +08001020 }
1021
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001022 lock_page(page);
Changman Leeb1a94e82013-11-15 10:42:51 +09001023 if (unlikely(!PageUptodate(page))) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001024 f2fs_put_page(page, 1);
Evan McClainf3f030d2014-07-17 21:16:35 -04001025 err = -EIO;
1026 goto fail;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001027 }
Changman Leeb1a94e82013-11-15 10:42:51 +09001028 if (unlikely(page->mapping != mapping)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001029 f2fs_put_page(page, 1);
1030 goto repeat;
1031 }
1032 }
1033out:
1034 SetPageUptodate(page);
1035 clear_cold_data(page);
1036 return 0;
Evan McClainf3f030d2014-07-17 21:16:35 -04001037fail:
1038 f2fs_write_failed(mapping, pos + len);
1039 return err;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001040}
1041
1042static int f2fs_write_end(struct file *file,
1043 struct address_space *mapping,
1044 loff_t pos, unsigned len, unsigned copied,
1045 struct page *page, void *fsdata)
1046{
1047 struct inode *inode = page->mapping->host;
1048
Chao Yu3f1ac6d2014-05-06 16:47:23 +08001049 trace_f2fs_write_end(inode, pos, len, copied);
1050
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001051 set_page_dirty(page);
1052
1053 if (pos + copied > i_size_read(inode)) {
1054 i_size_write(inode, pos + copied);
1055 mark_inode_dirty(inode);
1056 update_inode_page(inode);
1057 }
1058
Changman Leeb1a94e82013-11-15 10:42:51 +09001059 f2fs_put_page(page, 1);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001060 return copied;
1061}
1062
Changman Leeb1a94e82013-11-15 10:42:51 +09001063static int check_direct_IO(struct inode *inode, int rw,
1064 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
1065{
1066 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
1067 int i;
1068
1069 if (rw == READ)
1070 return 0;
1071
1072 if (offset & blocksize_mask)
1073 return -EINVAL;
1074
Evan McClainf3f030d2014-07-17 21:16:35 -04001075 for (i = 0; i < nr_segs; i++)
1076 if (iov[i].iov_len & blocksize_mask)
1077 return -EINVAL;
1078
Changman Leeb1a94e82013-11-15 10:42:51 +09001079 return 0;
1080}
1081
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001082static ssize_t f2fs_direct_IO(int rw, struct kiocb *iocb,
Evan McClainf3f030d2014-07-17 21:16:35 -04001083 const struct iovec *iov, loff_t offset,
1084 unsigned long nr_segs)
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001085{
1086 struct file *file = iocb->ki_filp;
Evan McClainf3f030d2014-07-17 21:16:35 -04001087 struct address_space *mapping = file->f_mapping;
1088 struct inode *inode = mapping->host;
1089 size_t count = iov_length(iov, nr_segs);
1090 int err;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001091
Changman Leeb1a94e82013-11-15 10:42:51 +09001092 /* Let buffer I/O handle the inline data case. */
1093 if (f2fs_has_inline_data(inode))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001094 return 0;
1095
Changman Leeb1a94e82013-11-15 10:42:51 +09001096 if (check_direct_IO(inode, rw, iov, offset, nr_segs))
1097 return 0;
1098
Jaegeuk Kim44f7a3b2014-06-04 00:39:42 +09001099 /* clear fsync mark to recover these blocks */
1100 fsync_mark_clear(F2FS_SB(inode->i_sb), inode->i_ino);
1101
Evan McClainf3f030d2014-07-17 21:16:35 -04001102 err = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
Changman Leeb1a94e82013-11-15 10:42:51 +09001103 get_data_block);
Chao Yu9f64de72014-07-31 21:11:22 +08001104
1105 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
1106
Evan McClainf3f030d2014-07-17 21:16:35 -04001107 if (err < 0 && (rw & WRITE))
1108 f2fs_write_failed(mapping, offset + count);
Chao Yu9f64de72014-07-31 21:11:22 +08001109
1110 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
1111
Evan McClainf3f030d2014-07-17 21:16:35 -04001112 return err;
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001113}
1114
1115static void f2fs_invalidate_data_page(struct page *page, unsigned long offset)
1116{
1117 struct inode *inode = page->mapping->host;
Jaegeuk Kim9694e662014-02-07 10:00:06 +09001118 if (PageDirty(page))
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001119 inode_dec_dirty_dents(inode);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001120 ClearPagePrivate(page);
1121}
1122
1123static int f2fs_release_data_page(struct page *page, gfp_t wait)
1124{
1125 ClearPagePrivate(page);
1126 return 1;
1127}
1128
1129static int f2fs_set_data_page_dirty(struct page *page)
1130{
1131 struct address_space *mapping = page->mapping;
1132 struct inode *inode = mapping->host;
1133
1134 trace_f2fs_set_page_dirty(page, DATA);
1135
1136 SetPageUptodate(page);
Changman Leeb1a94e82013-11-15 10:42:51 +09001137 mark_inode_dirty(inode);
1138
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001139 if (!PageDirty(page)) {
1140 __set_page_dirty_nobuffers(page);
1141 set_dirty_dir_page(inode, page);
1142 return 1;
1143 }
1144 return 0;
1145}
1146
1147static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1148{
Chao Yu11237352014-04-22 13:34:01 +08001149 struct inode *inode = mapping->host;
1150
1151 if (f2fs_has_inline_data(inode))
1152 return 0;
1153
Changman Leeb1a94e82013-11-15 10:42:51 +09001154 return generic_block_bmap(mapping, block, get_data_block);
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001155}
1156
1157const struct address_space_operations f2fs_dblock_aops = {
1158 .readpage = f2fs_read_data_page,
1159 .readpages = f2fs_read_data_pages,
1160 .writepage = f2fs_write_data_page,
1161 .writepages = f2fs_write_data_pages,
1162 .write_begin = f2fs_write_begin,
1163 .write_end = f2fs_write_end,
1164 .set_page_dirty = f2fs_set_data_page_dirty,
1165 .invalidatepage = f2fs_invalidate_data_page,
1166 .releasepage = f2fs_release_data_page,
1167 .direct_IO = f2fs_direct_IO,
1168 .bmap = f2fs_bmap,
1169};