blob: 7f51296fbbf6b357da97683257b82e0a1e18893f [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>
Chao Yu8f46dca2015-07-14 18:56:10 +080017#include <linux/pagevec.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090018#include <linux/blkdev.h>
19#include <linux/bio.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010020#include <linux/prefetch.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080021#include <linux/uio.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070022#include <linux/cleancache.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090023
24#include "f2fs.h"
25#include "node.h"
26#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080027#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090028#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090029
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090030static void f2fs_read_end_io(struct bio *bio, int err)
31{
Linus Torvaldsf5688492014-01-30 11:19:05 -080032 struct bio_vec *bvec;
33 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090034
Chao Yu12377022015-05-25 18:03:38 +080035 if (f2fs_bio_encrypted(bio)) {
36 if (err) {
37 f2fs_release_crypto_ctx(bio->bi_private);
38 } else {
39 f2fs_end_io_crypto_work(bio->bi_private, bio);
40 return;
41 }
42 }
43
Linus Torvaldsf5688492014-01-30 11:19:05 -080044 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090045 struct page *page = bvec->bv_page;
46
Linus Torvaldsf5688492014-01-30 11:19:05 -080047 if (!err) {
48 SetPageUptodate(page);
49 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090050 ClearPageUptodate(page);
51 SetPageError(page);
52 }
53 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080054 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090055 bio_put(bio);
56}
57
58static void f2fs_write_end_io(struct bio *bio, int err)
59{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090060 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080061 struct bio_vec *bvec;
62 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090063
Linus Torvaldsf5688492014-01-30 11:19:05 -080064 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090065 struct page *page = bvec->bv_page;
66
Jaegeuk Kim4375a332015-04-23 12:04:33 -070067 f2fs_restore_and_release_control_page(&page);
68
Linus Torvaldsf5688492014-01-30 11:19:05 -080069 if (unlikely(err)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -070070 set_page_dirty(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090071 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim744602c2014-01-24 09:42:16 +090072 f2fs_stop_checkpoint(sbi);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090073 }
74 end_page_writeback(page);
75 dec_page_count(sbi, F2FS_WRITEBACK);
Linus Torvaldsf5688492014-01-30 11:19:05 -080076 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090077
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090078 if (!get_pages(sbi, F2FS_WRITEBACK) &&
79 !list_empty(&sbi->cp_wait.task_list))
80 wake_up(&sbi->cp_wait);
81
82 bio_put(bio);
83}
84
Gu Zheng940a6d32013-12-20 17:39:59 +080085/*
86 * Low-level block read/write IO operations.
87 */
88static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
89 int npages, bool is_read)
90{
91 struct bio *bio;
92
93 /* No failure on bio allocation */
94 bio = bio_alloc(GFP_NOIO, npages);
95
96 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +080097 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +080098 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +080099 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800100
101 return bio;
102}
103
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900104static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900105{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900106 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900107
108 if (!io->bio)
109 return;
110
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700111 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800112 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700113 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800114 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900115
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700116 submit_bio(fio->rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900117 io->bio = NULL;
118}
119
120void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900121 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900122{
123 enum page_type btype = PAGE_TYPE_OF_BIO(type);
124 struct f2fs_bio_info *io;
125
126 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
127
Chao Yudf0f8dc2014-03-22 14:57:23 +0800128 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900129
130 /* change META to META_FLUSH in the checkpoint procedure */
131 if (type >= META_FLUSH) {
132 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700133 if (test_opt(sbi, NOBARRIER))
134 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
135 else
136 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900137 }
138 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800139 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900140}
141
142/*
143 * Fill the locked page with data located in the block address.
144 * Return unlocked page.
145 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700146int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900147{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900148 struct bio *bio;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700149 struct page *page = fio->encrypted_page ? fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900150
Chao Yu2ace38e2014-12-24 16:08:14 +0800151 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700152 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900153
154 /* Allocate a new bio */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700155 bio = __bio_alloc(fio->sbi, fio->blk_addr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900156
157 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
158 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900159 return -EFAULT;
160 }
161
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800162 submit_bio(fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900163 return 0;
164}
165
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700166void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900167{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700168 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900169 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900170 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800171 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700172 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900173
Gu Zheng940a6d32013-12-20 17:39:59 +0800174 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900175
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800176 verify_block_addr(sbi, fio->blk_addr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900177
Chao Yudf0f8dc2014-03-22 14:57:23 +0800178 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900179
Gu Zheng940a6d32013-12-20 17:39:59 +0800180 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900181 inc_page_count(sbi, F2FS_WRITEBACK);
182
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800183 if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900184 io->fio.rw != fio->rw))
185 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900186alloc_new:
187 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700188 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800189
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800190 io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900191 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900192 }
193
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700194 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
195
196 if (bio_add_page(io->bio, bio_page, PAGE_CACHE_SIZE, 0) <
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900197 PAGE_CACHE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900198 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900199 goto alloc_new;
200 }
201
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800202 io->last_block_in_bio = fio->blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700203 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900204
Chao Yudf0f8dc2014-03-22 14:57:23 +0800205 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700206 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900207}
208
209/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900210 * Lock ordering for the change of data block address:
211 * ->data_page
212 * ->node_page
213 * update block addresses in the node page
214 */
Chao Yu216a6202015-03-19 19:23:32 +0800215void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900216{
217 struct f2fs_node *rn;
218 __le32 *addr_array;
219 struct page *node_page = dn->node_page;
220 unsigned int ofs_in_node = dn->ofs_in_node;
221
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000222 f2fs_wait_on_page_writeback(node_page, NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900223
Gu Zheng45590712013-07-15 17:57:38 +0800224 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900225
226 /* Get physical address of data block */
227 addr_array = blkaddr_in_node(rn);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800228 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900229 set_page_dirty(node_page);
230}
231
232int reserve_new_block(struct dnode_of_data *dn)
233{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700234 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900235
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900236 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900237 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800238 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900239 return -ENOSPC;
240
Namjae Jeonc01e2852013-04-23 17:00:52 +0900241 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
242
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900243 dn->data_blkaddr = NEW_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800244 set_data_blkaddr(dn);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900245 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900246 sync_inode_page(dn);
247 return 0;
248}
249
Huajun Lib6009652013-11-10 23:13:18 +0800250int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
251{
252 bool need_put = dn->inode_page ? false : true;
253 int err;
254
255 err = get_dnode_of_data(dn, index, ALLOC_NODE);
256 if (err)
257 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900258
Huajun Lib6009652013-11-10 23:13:18 +0800259 if (dn->data_blkaddr == NULL_ADDR)
260 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900261 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800262 f2fs_put_dnode(dn);
263 return err;
264}
265
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700266struct page *get_read_data_page(struct inode *inode, pgoff_t index, int rw)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900267{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900268 struct address_space *mapping = inode->i_mapping;
269 struct dnode_of_data dn;
270 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800271 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900272 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800273 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700274 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800275 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700276 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700277 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800278 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900279
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700280 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
281 return read_mapping_page(mapping, index, NULL);
282
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900283 page = grab_cache_page(mapping, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900284 if (!page)
285 return ERR_PTR(-ENOMEM);
286
Chao Yucb3bc9e2015-02-05 18:03:40 +0800287 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
288 dn.data_blkaddr = ei.blk + index - ei.fofs;
289 goto got_it;
290 }
291
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900292 set_new_dnode(&dn, inode, NULL, NULL, 0);
293 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700294 if (err)
295 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900296 f2fs_put_dnode(&dn);
297
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900298 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700299 err = -ENOENT;
300 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900301 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800302got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700303 if (PageUptodate(page)) {
304 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900305 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700306 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900307
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900308 /*
309 * A new dentry page is allocated but not able to be written, since its
310 * new inode page couldn't be allocated due to -ENOSPC.
311 * In such the case, its blkaddr can be remained as NEW_ADDR.
312 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
313 */
314 if (dn.data_blkaddr == NEW_ADDR) {
315 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
316 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700317 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900318 return page;
319 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900320
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800321 fio.blk_addr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700322 fio.page = page;
323 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900324 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700325 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700326 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700327
328put_err:
329 f2fs_put_page(page, 1);
330 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700331}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900332
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700333struct page *find_data_page(struct inode *inode, pgoff_t index)
334{
335 struct address_space *mapping = inode->i_mapping;
336 struct page *page;
337
338 page = find_get_page(mapping, index);
339 if (page && PageUptodate(page))
340 return page;
341 f2fs_put_page(page, 0);
342
343 page = get_read_data_page(inode, index, READ_SYNC);
344 if (IS_ERR(page))
345 return page;
346
347 if (PageUptodate(page))
348 return page;
349
350 wait_on_page_locked(page);
351 if (unlikely(!PageUptodate(page))) {
352 f2fs_put_page(page, 0);
353 return ERR_PTR(-EIO);
354 }
355 return page;
356}
357
358/*
359 * If it tries to access a hole, return an error.
360 * Because, the callers, functions in dir.c and GC, should be able to know
361 * whether this page exists or not.
362 */
363struct page *get_lock_data_page(struct inode *inode, pgoff_t index)
364{
365 struct address_space *mapping = inode->i_mapping;
366 struct page *page;
367repeat:
368 page = get_read_data_page(inode, index, READ_SYNC);
369 if (IS_ERR(page))
370 return page;
371
372 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900373 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900374 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900375 f2fs_put_page(page, 1);
376 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900377 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900378 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900379 f2fs_put_page(page, 1);
380 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900381 }
382 return page;
383}
384
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900385/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900386 * Caller ensures that this data page is never allocated.
387 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900388 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800389 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
390 * f2fs_unlock_op().
Jaegeuk Kima8865372013-12-27 17:04:17 +0900391 * Note that, ipage is set only by make_empty_dir.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900392 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900393struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900394 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900395{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900396 struct address_space *mapping = inode->i_mapping;
397 struct page *page;
398 struct dnode_of_data dn;
399 int err;
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700400repeat:
401 page = grab_cache_page(mapping, index);
402 if (!page)
403 return ERR_PTR(-ENOMEM);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900404
Jaegeuk Kima8865372013-12-27 17:04:17 +0900405 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800406 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700407 if (err) {
408 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900409 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900410 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700411 if (!ipage)
412 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900413
414 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700415 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900416
417 if (dn.data_blkaddr == NEW_ADDR) {
418 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900419 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900420 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700421 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900422
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700423 page = get_read_data_page(inode, index, READ_SYNC);
424 if (IS_ERR(page))
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900425 goto repeat;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700426
427 /* wait for read completion */
428 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900429 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700430got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900431 if (new_i_size &&
432 i_size_read(inode) < ((index + 1) << PAGE_CACHE_SHIFT)) {
433 i_size_write(inode, ((index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900434 /* Only the directory inode sets new_i_size */
435 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900436 }
437 return page;
438}
439
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900440static int __allocate_data_block(struct dnode_of_data *dn)
441{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700442 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700443 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900444 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900445 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800446 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700447 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900448
449 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
450 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800451
452 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
453 if (dn->data_blkaddr == NEW_ADDR)
454 goto alloc;
455
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900456 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
457 return -ENOSPC;
458
Chao Yudf6136e2015-03-23 10:33:37 +0800459alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900460 get_node_info(sbi, dn->nid, &ni);
461 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
462
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800463 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
464 seg = CURSEG_DIRECT_IO;
465
Chao Yudf6136e2015-03-23 10:33:37 +0800466 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
467 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800468 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900469
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700470 /* update i_size */
471 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
472 dn->ofs_in_node;
473 if (i_size_read(dn->inode) < ((fofs + 1) << PAGE_CACHE_SHIFT))
474 i_size_write(dn->inode, ((fofs + 1) << PAGE_CACHE_SHIFT));
475
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700476 /* direct IO doesn't use extent cache to maximize the performance */
Chao Yua28ef1f2015-07-08 17:59:36 +0800477 f2fs_drop_largest_extent(dn->inode, fofs);
Jaegeuk Kim3e72f722015-06-19 17:53:26 -0700478
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900479 return 0;
480}
481
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800482static void __allocate_data_blocks(struct inode *inode, loff_t offset,
483 size_t count)
484{
485 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
486 struct dnode_of_data dn;
487 u64 start = F2FS_BYTES_TO_BLK(offset);
488 u64 len = F2FS_BYTES_TO_BLK(count);
489 bool allocated;
490 u64 end_offset;
491
492 while (len) {
493 f2fs_balance_fs(sbi);
494 f2fs_lock_op(sbi);
495
496 /* When reading holes, we need its node page */
497 set_new_dnode(&dn, inode, NULL, NULL, 0);
498 if (get_dnode_of_data(&dn, start, ALLOC_NODE))
499 goto out;
500
501 allocated = false;
502 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
503
504 while (dn.ofs_in_node < end_offset && len) {
Chao Yud6d4f1c2015-03-12 17:04:24 +0800505 block_t blkaddr;
506
507 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800508 if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR) {
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800509 if (__allocate_data_block(&dn))
510 goto sync_out;
511 allocated = true;
512 }
513 len--;
514 start++;
515 dn.ofs_in_node++;
516 }
517
518 if (allocated)
519 sync_inode_page(&dn);
520
521 f2fs_put_dnode(&dn);
522 f2fs_unlock_op(sbi);
523 }
524 return;
525
526sync_out:
527 if (allocated)
528 sync_inode_page(&dn);
529 f2fs_put_dnode(&dn);
530out:
531 f2fs_unlock_op(sbi);
532 return;
533}
534
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900535/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700536 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
537 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800538 * If original data blocks are allocated, then give them to blockdev.
539 * Otherwise,
540 * a. preallocate requested block addresses
541 * b. do not use extent cache for better performance
542 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900543 */
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700544static int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
545 int create, bool fiemap)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900546{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700547 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900548 struct dnode_of_data dn;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900549 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
550 pgoff_t pgofs, end_offset;
551 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800552 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900553 bool allocated = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900554
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700555 map->m_len = 0;
556 map->m_flags = 0;
557
558 /* it only supports block size == page size */
559 pgofs = (pgoff_t)map->m_lblk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900560
Chao Yu7e4dde72015-02-05 17:51:34 +0800561 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700562 map->m_pblk = ei.blk + pgofs - ei.fofs;
563 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
564 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900565 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800566 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900567
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800568 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -0700569 f2fs_lock_op(F2FS_I_SB(inode));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900570
571 /* When reading holes, we need its node page */
572 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900573 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900574 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900575 if (err == -ENOENT)
576 err = 0;
577 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900578 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900579 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900580 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900581
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900582 if (dn.data_blkaddr != NULL_ADDR) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700583 map->m_flags = F2FS_MAP_MAPPED;
584 map->m_pblk = dn.data_blkaddr;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700585 if (dn.data_blkaddr == NEW_ADDR)
586 map->m_flags |= F2FS_MAP_UNWRITTEN;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900587 } else if (create) {
588 err = __allocate_data_block(&dn);
589 if (err)
590 goto put_out;
591 allocated = true;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700592 map->m_flags = F2FS_MAP_NEW | F2FS_MAP_MAPPED;
593 map->m_pblk = dn.data_blkaddr;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900594 } else {
595 goto put_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900596 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900597
Chao Yu6403eb12014-04-26 19:59:52 +0800598 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700599 map->m_len = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900600 dn.ofs_in_node++;
601 pgofs++;
602
603get_next:
604 if (dn.ofs_in_node >= end_offset) {
605 if (allocated)
606 sync_inode_page(&dn);
607 allocated = false;
608 f2fs_put_dnode(&dn);
609
610 set_new_dnode(&dn, inode, NULL, NULL, 0);
611 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900612 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900613 if (err == -ENOENT)
614 err = 0;
615 goto unlock_out;
616 }
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900617 if (dn.data_blkaddr == NEW_ADDR && !fiemap)
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900618 goto put_out;
619
Chao Yu6403eb12014-04-26 19:59:52 +0800620 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900621 }
622
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700623 if (maxblocks > map->m_len) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900624 block_t blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
625 if (blkaddr == NULL_ADDR && create) {
626 err = __allocate_data_block(&dn);
627 if (err)
628 goto sync_out;
629 allocated = true;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700630 map->m_flags |= F2FS_MAP_NEW;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900631 blkaddr = dn.data_blkaddr;
632 }
arter97e1c42042014-08-06 23:22:50 +0900633 /* Give more consecutive addresses for the readahead */
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700634 if ((map->m_pblk != NEW_ADDR &&
635 blkaddr == (map->m_pblk + ofs)) ||
636 (map->m_pblk == NEW_ADDR &&
637 blkaddr == NEW_ADDR)) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900638 ofs++;
639 dn.ofs_in_node++;
640 pgofs++;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700641 map->m_len++;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900642 goto get_next;
643 }
644 }
645sync_out:
646 if (allocated)
647 sync_inode_page(&dn);
648put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900649 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900650unlock_out:
651 if (create)
Jaegeuk Kim40813632014-09-02 15:31:18 -0700652 f2fs_unlock_op(F2FS_I_SB(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900653out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700654 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900655 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900656}
657
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700658static int __get_data_block(struct inode *inode, sector_t iblock,
659 struct buffer_head *bh, int create, bool fiemap)
660{
661 struct f2fs_map_blocks map;
662 int ret;
663
664 map.m_lblk = iblock;
665 map.m_len = bh->b_size >> inode->i_blkbits;
666
667 ret = f2fs_map_blocks(inode, &map, create, fiemap);
668 if (!ret) {
669 map_bh(bh, inode->i_sb, map.m_pblk);
670 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
671 bh->b_size = map.m_len << inode->i_blkbits;
672 }
673 return ret;
674}
675
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900676static int get_data_block(struct inode *inode, sector_t iblock,
677 struct buffer_head *bh_result, int create)
678{
679 return __get_data_block(inode, iblock, bh_result, create, false);
680}
681
682static int get_data_block_fiemap(struct inode *inode, sector_t iblock,
683 struct buffer_head *bh_result, int create)
684{
685 return __get_data_block(inode, iblock, bh_result, create, true);
686}
687
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700688static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
689{
690 return (offset >> inode->i_blkbits);
691}
692
693static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
694{
695 return (blk << inode->i_blkbits);
696}
697
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900698int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
699 u64 start, u64 len)
700{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700701 struct buffer_head map_bh;
702 sector_t start_blk, last_blk;
703 loff_t isize = i_size_read(inode);
704 u64 logical = 0, phys = 0, size = 0;
705 u32 flags = 0;
706 bool past_eof = false, whole_file = false;
707 int ret = 0;
708
709 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
710 if (ret)
711 return ret;
712
713 mutex_lock(&inode->i_mutex);
714
715 if (len >= isize) {
716 whole_file = true;
717 len = isize;
718 }
719
720 if (logical_to_blk(inode, len) == 0)
721 len = blk_to_logical(inode, 1);
722
723 start_blk = logical_to_blk(inode, start);
724 last_blk = logical_to_blk(inode, start + len - 1);
725next:
726 memset(&map_bh, 0, sizeof(struct buffer_head));
727 map_bh.b_size = len;
728
729 ret = get_data_block_fiemap(inode, start_blk, &map_bh, 0);
730 if (ret)
731 goto out;
732
733 /* HOLE */
734 if (!buffer_mapped(&map_bh)) {
735 start_blk++;
736
737 if (!past_eof && blk_to_logical(inode, start_blk) >= isize)
738 past_eof = 1;
739
740 if (past_eof && size) {
741 flags |= FIEMAP_EXTENT_LAST;
742 ret = fiemap_fill_next_extent(fieinfo, logical,
743 phys, size, flags);
744 } else if (size) {
745 ret = fiemap_fill_next_extent(fieinfo, logical,
746 phys, size, flags);
747 size = 0;
748 }
749
750 /* if we have holes up to/past EOF then we're done */
751 if (start_blk > last_blk || past_eof || ret)
752 goto out;
753 } else {
754 if (start_blk > last_blk && !whole_file) {
755 ret = fiemap_fill_next_extent(fieinfo, logical,
756 phys, size, flags);
757 goto out;
758 }
759
760 /*
761 * if size != 0 then we know we already have an extent
762 * to add, so add it.
763 */
764 if (size) {
765 ret = fiemap_fill_next_extent(fieinfo, logical,
766 phys, size, flags);
767 if (ret)
768 goto out;
769 }
770
771 logical = blk_to_logical(inode, start_blk);
772 phys = blk_to_logical(inode, map_bh.b_blocknr);
773 size = map_bh.b_size;
774 flags = 0;
775 if (buffer_unwritten(&map_bh))
776 flags = FIEMAP_EXTENT_UNWRITTEN;
777
778 start_blk += logical_to_blk(inode, size);
779
780 /*
781 * If we are past the EOF, then we need to make sure as
782 * soon as we find a hole that the last extent we found
783 * is marked with FIEMAP_EXTENT_LAST
784 */
785 if (!past_eof && logical + size >= isize)
786 past_eof = true;
787 }
788 cond_resched();
789 if (fatal_signal_pending(current))
790 ret = -EINTR;
791 else
792 goto next;
793out:
794 if (ret == 1)
795 ret = 0;
796
797 mutex_unlock(&inode->i_mutex);
798 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900799}
800
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700801/*
802 * This function was originally taken from fs/mpage.c, and customized for f2fs.
803 * Major change was from block_size == page_size in f2fs by default.
804 */
805static int f2fs_mpage_readpages(struct address_space *mapping,
806 struct list_head *pages, struct page *page,
807 unsigned nr_pages)
808{
809 struct bio *bio = NULL;
810 unsigned page_idx;
811 sector_t last_block_in_bio = 0;
812 struct inode *inode = mapping->host;
813 const unsigned blkbits = inode->i_blkbits;
814 const unsigned blocksize = 1 << blkbits;
815 sector_t block_in_file;
816 sector_t last_block;
817 sector_t last_block_in_file;
818 sector_t block_nr;
819 struct block_device *bdev = inode->i_sb->s_bdev;
820 struct f2fs_map_blocks map;
821
822 map.m_pblk = 0;
823 map.m_lblk = 0;
824 map.m_len = 0;
825 map.m_flags = 0;
826
827 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
828
829 prefetchw(&page->flags);
830 if (pages) {
831 page = list_entry(pages->prev, struct page, lru);
832 list_del(&page->lru);
833 if (add_to_page_cache_lru(page, mapping,
834 page->index, GFP_KERNEL))
835 goto next_page;
836 }
837
838 block_in_file = (sector_t)page->index;
839 last_block = block_in_file + nr_pages;
840 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
841 blkbits;
842 if (last_block > last_block_in_file)
843 last_block = last_block_in_file;
844
845 /*
846 * Map blocks using the previous result first.
847 */
848 if ((map.m_flags & F2FS_MAP_MAPPED) &&
849 block_in_file > map.m_lblk &&
850 block_in_file < (map.m_lblk + map.m_len))
851 goto got_it;
852
853 /*
854 * Then do more f2fs_map_blocks() calls until we are
855 * done with this page.
856 */
857 map.m_flags = 0;
858
859 if (block_in_file < last_block) {
860 map.m_lblk = block_in_file;
861 map.m_len = last_block - block_in_file;
862
863 if (f2fs_map_blocks(inode, &map, 0, false))
864 goto set_error_page;
865 }
866got_it:
867 if ((map.m_flags & F2FS_MAP_MAPPED)) {
868 block_nr = map.m_pblk + block_in_file - map.m_lblk;
869 SetPageMappedToDisk(page);
870
871 if (!PageUptodate(page) && !cleancache_get_page(page)) {
872 SetPageUptodate(page);
873 goto confused;
874 }
875 } else {
876 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
877 SetPageUptodate(page);
878 unlock_page(page);
879 goto next_page;
880 }
881
882 /*
883 * This page will go to BIO. Do we need to send this
884 * BIO off first?
885 */
886 if (bio && (last_block_in_bio != block_nr - 1)) {
887submit_and_realloc:
888 submit_bio(READ, bio);
889 bio = NULL;
890 }
891 if (bio == NULL) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700892 struct f2fs_crypto_ctx *ctx = NULL;
893
894 if (f2fs_encrypted_inode(inode) &&
895 S_ISREG(inode->i_mode)) {
896 struct page *cpage;
897
898 ctx = f2fs_get_crypto_ctx(inode);
899 if (IS_ERR(ctx))
900 goto set_error_page;
901
902 /* wait the page to be moved by cleaning */
903 cpage = find_lock_page(
904 META_MAPPING(F2FS_I_SB(inode)),
905 block_nr);
906 if (cpage) {
907 f2fs_wait_on_page_writeback(cpage,
908 DATA);
909 f2fs_put_page(cpage, 1);
910 }
911 }
912
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700913 bio = bio_alloc(GFP_KERNEL,
914 min_t(int, nr_pages, bio_get_nr_vecs(bdev)));
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700915 if (!bio) {
916 if (ctx)
917 f2fs_release_crypto_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700918 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700919 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700920 bio->bi_bdev = bdev;
921 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +0800922 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700923 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700924 }
925
926 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
927 goto submit_and_realloc;
928
929 last_block_in_bio = block_nr;
930 goto next_page;
931set_error_page:
932 SetPageError(page);
933 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
934 unlock_page(page);
935 goto next_page;
936confused:
937 if (bio) {
938 submit_bio(READ, bio);
939 bio = NULL;
940 }
941 unlock_page(page);
942next_page:
943 if (pages)
944 page_cache_release(page);
945 }
946 BUG_ON(pages && !list_empty(pages));
947 if (bio)
948 submit_bio(READ, bio);
949 return 0;
950}
951
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900952static int f2fs_read_data_page(struct file *file, struct page *page)
953{
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800954 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700955 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800956
Chao Yuc20e89c2014-05-06 16:53:08 +0800957 trace_f2fs_readpage(page, DATA);
958
arter97e1c42042014-08-06 23:22:50 +0900959 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800960 if (f2fs_has_inline_data(inode))
961 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700962 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700963 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800964 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900965}
966
967static int f2fs_read_data_pages(struct file *file,
968 struct address_space *mapping,
969 struct list_head *pages, unsigned nr_pages)
970{
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800971 struct inode *inode = file->f_mapping->host;
972
973 /* If the file has inline data, skip readpages */
974 if (f2fs_has_inline_data(inode))
975 return 0;
976
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700977 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900978}
979
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700980int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900981{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700982 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900983 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900984 struct dnode_of_data dn;
985 int err = 0;
986
987 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900988 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900989 if (err)
990 return err;
991
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800992 fio->blk_addr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900993
994 /* This page is already truncated */
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -0800995 if (fio->blk_addr == NULL_ADDR) {
996 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900997 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -0800998 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900999
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001000 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1001 fio->encrypted_page = f2fs_encrypt(inode, fio->page);
1002 if (IS_ERR(fio->encrypted_page)) {
1003 err = PTR_ERR(fio->encrypted_page);
1004 goto out_writepage;
1005 }
1006 }
1007
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001008 set_page_writeback(page);
1009
1010 /*
1011 * If current allocation needs SSR,
1012 * it had better in-place writes for updated data.
1013 */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001014 if (unlikely(fio->blk_addr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001015 !is_cold_data(page) &&
1016 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001017 rewrite_data_page(fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001018 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001019 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001020 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001021 write_data_page(&dn, fio);
Chao Yu216a6202015-03-19 19:23:32 +08001022 set_data_blkaddr(&dn);
Chao Yu7e4dde72015-02-05 17:51:34 +08001023 f2fs_update_extent_cache(&dn);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001024 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001025 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001026 if (page->index == 0)
1027 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001028 }
1029out_writepage:
1030 f2fs_put_dnode(&dn);
1031 return err;
1032}
1033
1034static int f2fs_write_data_page(struct page *page,
1035 struct writeback_control *wbc)
1036{
1037 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001038 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001039 loff_t i_size = i_size_read(inode);
1040 const pgoff_t end_index = ((unsigned long long) i_size)
1041 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001042 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001043 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001044 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001045 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001046 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001047 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001048 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001049 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001050 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001051 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001052
Chao Yuecda0de2014-05-06 16:48:26 +08001053 trace_f2fs_writepage(page, DATA);
1054
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001055 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001056 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001057
1058 /*
1059 * If the offset is out-of-range of file size,
1060 * this page does not have to be written to disk.
1061 */
1062 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001063 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001064 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001065
1066 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001067write:
Chao Yucaf00472015-01-28 17:48:42 +08001068 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001069 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001070 if (f2fs_is_drop_cache(inode))
1071 goto out;
1072 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1073 available_free_memory(sbi, BASE_CHECK))
1074 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001075
Jaegeuk Kim39936832012-11-22 16:21:29 +09001076 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001077 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001078 if (unlikely(f2fs_cp_error(sbi)))
1079 goto redirty_out;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001080 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001081 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001082 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001083
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001084 /* we should bypass data pages to proceed the kworkder jobs */
1085 if (unlikely(f2fs_cp_error(sbi))) {
1086 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001087 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001088 }
1089
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001090 if (!wbc->for_reclaim)
1091 need_balance_fs = true;
1092 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001093 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001094
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001095 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001096 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001097 if (f2fs_has_inline_data(inode))
1098 err = f2fs_write_inline_data(inode, page);
1099 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001100 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001101 f2fs_unlock_op(sbi);
1102done:
1103 if (err && err != -ENOENT)
1104 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001105
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001106 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001107out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001108 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001109 if (err)
1110 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001111 unlock_page(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001112 if (need_balance_fs)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001113 f2fs_balance_fs(sbi);
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +09001114 if (wbc->for_reclaim)
1115 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001116 return 0;
1117
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001118redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001119 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001120 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001121}
1122
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001123static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1124 void *data)
1125{
1126 struct address_space *mapping = data;
1127 int ret = mapping->a_ops->writepage(page, wbc);
1128 mapping_set_error(mapping, ret);
1129 return ret;
1130}
1131
Chao Yu8f46dca2015-07-14 18:56:10 +08001132/*
1133 * This function was copied from write_cche_pages from mm/page-writeback.c.
1134 * The major change is making write step of cold data page separately from
1135 * warm/hot data page.
1136 */
1137static int f2fs_write_cache_pages(struct address_space *mapping,
1138 struct writeback_control *wbc, writepage_t writepage,
1139 void *data)
1140{
1141 int ret = 0;
1142 int done = 0;
1143 struct pagevec pvec;
1144 int nr_pages;
1145 pgoff_t uninitialized_var(writeback_index);
1146 pgoff_t index;
1147 pgoff_t end; /* Inclusive */
1148 pgoff_t done_index;
1149 int cycled;
1150 int range_whole = 0;
1151 int tag;
1152 int step = 0;
1153
1154 pagevec_init(&pvec, 0);
1155next:
1156 if (wbc->range_cyclic) {
1157 writeback_index = mapping->writeback_index; /* prev offset */
1158 index = writeback_index;
1159 if (index == 0)
1160 cycled = 1;
1161 else
1162 cycled = 0;
1163 end = -1;
1164 } else {
1165 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1166 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1167 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1168 range_whole = 1;
1169 cycled = 1; /* ignore range_cyclic tests */
1170 }
1171 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1172 tag = PAGECACHE_TAG_TOWRITE;
1173 else
1174 tag = PAGECACHE_TAG_DIRTY;
1175retry:
1176 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1177 tag_pages_for_writeback(mapping, index, end);
1178 done_index = index;
1179 while (!done && (index <= end)) {
1180 int i;
1181
1182 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1183 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1184 if (nr_pages == 0)
1185 break;
1186
1187 for (i = 0; i < nr_pages; i++) {
1188 struct page *page = pvec.pages[i];
1189
1190 if (page->index > end) {
1191 done = 1;
1192 break;
1193 }
1194
1195 done_index = page->index;
1196
1197 lock_page(page);
1198
1199 if (unlikely(page->mapping != mapping)) {
1200continue_unlock:
1201 unlock_page(page);
1202 continue;
1203 }
1204
1205 if (!PageDirty(page)) {
1206 /* someone wrote it for us */
1207 goto continue_unlock;
1208 }
1209
1210 if (step == 0 && !is_cold_data(page))
1211 goto continue_unlock;
1212 if (step == 1 && is_cold_data(page))
1213 goto continue_unlock;
1214
1215 if (PageWriteback(page)) {
1216 if (wbc->sync_mode != WB_SYNC_NONE)
1217 f2fs_wait_on_page_writeback(page, DATA);
1218 else
1219 goto continue_unlock;
1220 }
1221
1222 BUG_ON(PageWriteback(page));
1223 if (!clear_page_dirty_for_io(page))
1224 goto continue_unlock;
1225
1226 ret = (*writepage)(page, wbc, data);
1227 if (unlikely(ret)) {
1228 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1229 unlock_page(page);
1230 ret = 0;
1231 } else {
1232 done_index = page->index + 1;
1233 done = 1;
1234 break;
1235 }
1236 }
1237
1238 if (--wbc->nr_to_write <= 0 &&
1239 wbc->sync_mode == WB_SYNC_NONE) {
1240 done = 1;
1241 break;
1242 }
1243 }
1244 pagevec_release(&pvec);
1245 cond_resched();
1246 }
1247
1248 if (step < 1) {
1249 step++;
1250 goto next;
1251 }
1252
1253 if (!cycled && !done) {
1254 cycled = 1;
1255 index = 0;
1256 end = writeback_index - 1;
1257 goto retry;
1258 }
1259 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1260 mapping->writeback_index = done_index;
1261
1262 return ret;
1263}
1264
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001265static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001266 struct writeback_control *wbc)
1267{
1268 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001269 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001270 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001271 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001272 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001273
Chao Yue5748432014-05-06 16:51:24 +08001274 trace_f2fs_writepages(mapping->host, wbc, DATA);
1275
P J Pcfb185a2013-04-03 11:38:00 +09001276 /* deal with chardevs and other special file */
1277 if (!mapping->a_ops->writepage)
1278 return 0;
1279
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001280 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001281 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Jaegeuk Kim6fb03f32014-04-16 10:47:06 +09001282 available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001283 goto skip_write;
Jaegeuk Kim87d6f892014-03-18 12:40:49 +09001284
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001285 /* during POR, we don't need to trigger writepage at all. */
1286 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1287 goto skip_write;
1288
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001289 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001290
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001291 if (!S_ISDIR(inode->i_mode)) {
1292 mutex_lock(&sbi->writepages);
1293 locked = true;
1294 }
Chao Yu8f46dca2015-07-14 18:56:10 +08001295 ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Chao Yubb96a8d2015-07-06 20:31:49 +08001296 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001297 if (locked)
1298 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001299
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001300 remove_dirty_dir_inode(inode);
1301
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001302 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001303 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001304
1305skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001306 wbc->pages_skipped += get_dirty_pages(inode);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001307 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001308}
1309
Chao Yu3aab8f82014-07-02 13:25:04 +08001310static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1311{
1312 struct inode *inode = mapping->host;
1313
1314 if (to > inode->i_size) {
1315 truncate_pagecache(inode, inode->i_size);
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -07001316 truncate_blocks(inode, inode->i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001317 }
1318}
1319
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001320static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1321 loff_t pos, unsigned len, unsigned flags,
1322 struct page **pagep, void **fsdata)
1323{
1324 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001325 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001326 struct page *page = NULL;
1327 struct page *ipage;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001328 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
1329 struct dnode_of_data dn;
1330 int err = 0;
1331
Chao Yu62aed042014-05-06 16:46:04 +08001332 trace_f2fs_write_begin(inode, pos, len, flags);
1333
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001334 f2fs_balance_fs(sbi);
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001335
1336 /*
1337 * We should check this at this moment to avoid deadlock on inode page
1338 * and #0 page. The locking rule for inline_data conversion should be:
1339 * lock_page(page #0) -> lock_page(inode_page)
1340 */
1341 if (index != 0) {
1342 err = f2fs_convert_inline_inode(inode);
1343 if (err)
1344 goto fail;
1345 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001346repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001347 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001348 if (!page) {
1349 err = -ENOMEM;
1350 goto fail;
1351 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001352
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001353 *pagep = page;
1354
Gu Zhenge4795562013-09-27 18:08:30 +08001355 f2fs_lock_op(sbi);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001356
1357 /* check inline_data */
1358 ipage = get_node_page(sbi, inode->i_ino);
Chao Yucd34e292014-12-01 11:30:20 +08001359 if (IS_ERR(ipage)) {
1360 err = PTR_ERR(ipage);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001361 goto unlock_fail;
Chao Yucd34e292014-12-01 11:30:20 +08001362 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001363
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001364 set_new_dnode(&dn, inode, ipage, ipage, 0);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001365
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001366 if (f2fs_has_inline_data(inode)) {
1367 if (pos + len <= MAX_INLINE_DATA) {
1368 read_inline_data(page, ipage);
1369 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
1370 sync_inode_page(&dn);
1371 goto put_next;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001372 }
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001373 err = f2fs_convert_inline_page(&dn, page);
1374 if (err)
1375 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001376 }
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001377 err = f2fs_reserve_block(&dn, index);
1378 if (err)
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001379 goto put_fail;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001380put_next:
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001381 f2fs_put_dnode(&dn);
1382 f2fs_unlock_op(sbi);
1383
Chao Yu90d43882015-07-08 18:24:38 +08001384 if (len == PAGE_CACHE_SIZE)
1385 goto out_update;
1386 if (PageUptodate(page))
1387 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001388
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001389 f2fs_wait_on_page_writeback(page, DATA);
1390
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001391 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1392 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1393 unsigned end = start + len;
1394
1395 /* Reading beyond i_size is simple: memset to zero */
1396 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001397 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001398 }
1399
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001400 if (dn.data_blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001401 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1402 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001403 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001404 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001405 .type = DATA,
1406 .rw = READ_SYNC,
1407 .blk_addr = dn.data_blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001408 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001409 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001410 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001411 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001412 if (err)
1413 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001414
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001415 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001416 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001417 err = -EIO;
1418 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001419 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001420 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001421 f2fs_put_page(page, 1);
1422 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001423 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001424
1425 /* avoid symlink page */
1426 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1427 err = f2fs_decrypt_one(inode, page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001428 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001429 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001430 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001431 }
Chao Yu90d43882015-07-08 18:24:38 +08001432out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001433 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001434out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001435 clear_cold_data(page);
1436 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001437
Jaegeuk Kim8cdcb712014-11-17 16:14:11 -08001438put_fail:
1439 f2fs_put_dnode(&dn);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001440unlock_fail:
1441 f2fs_unlock_op(sbi);
Chao Yu3aab8f82014-07-02 13:25:04 +08001442fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001443 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001444 f2fs_write_failed(mapping, pos + len);
1445 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001446}
1447
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001448static int f2fs_write_end(struct file *file,
1449 struct address_space *mapping,
1450 loff_t pos, unsigned len, unsigned copied,
1451 struct page *page, void *fsdata)
1452{
1453 struct inode *inode = page->mapping->host;
1454
Chao Yudfb2bf32014-05-06 16:47:23 +08001455 trace_f2fs_write_end(inode, pos, len, copied);
1456
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001457 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001458
1459 if (pos + copied > i_size_read(inode)) {
1460 i_size_write(inode, pos + copied);
1461 mark_inode_dirty(inode);
1462 update_inode_page(inode);
1463 }
1464
Chao Yu75c3c8b2013-11-16 14:15:59 +08001465 f2fs_put_page(page, 1);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001466 return copied;
1467}
1468
Omar Sandoval6f673762015-03-16 04:33:52 -07001469static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1470 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001471{
1472 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001473
Omar Sandoval6f673762015-03-16 04:33:52 -07001474 if (iov_iter_rw(iter) == READ)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001475 return 0;
1476
1477 if (offset & blocksize_mask)
1478 return -EINVAL;
1479
Al Viro5b46f252014-03-16 18:07:34 -04001480 if (iov_iter_alignment(iter) & blocksize_mask)
1481 return -EINVAL;
1482
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001483 return 0;
1484}
1485
Omar Sandoval22c61862015-03-16 04:33:53 -07001486static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
1487 loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001488{
1489 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001490 struct address_space *mapping = file->f_mapping;
1491 struct inode *inode = mapping->host;
1492 size_t count = iov_iter_count(iter);
1493 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001494
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001495 /* we don't need to use inline_data strictly */
1496 if (f2fs_has_inline_data(inode)) {
1497 err = f2fs_convert_inline_inode(inode);
1498 if (err)
1499 return err;
1500 }
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001501
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001502 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1503 return 0;
1504
Omar Sandoval6f673762015-03-16 04:33:52 -07001505 if (check_direct_IO(inode, iter, offset))
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001506 return 0;
1507
Omar Sandoval6f673762015-03-16 04:33:52 -07001508 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001509
Omar Sandoval6f673762015-03-16 04:33:52 -07001510 if (iov_iter_rw(iter) == WRITE)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001511 __allocate_data_blocks(inode, offset, count);
1512
Omar Sandoval17f8c842015-03-16 04:33:50 -07001513 err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block);
Omar Sandoval6f673762015-03-16 04:33:52 -07001514 if (err < 0 && iov_iter_rw(iter) == WRITE)
Chao Yu3aab8f82014-07-02 13:25:04 +08001515 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001516
Omar Sandoval6f673762015-03-16 04:33:52 -07001517 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001518
Chao Yu3aab8f82014-07-02 13:25:04 +08001519 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001520}
1521
Chao Yu487261f2015-02-05 17:44:29 +08001522void f2fs_invalidate_page(struct page *page, unsigned int offset,
1523 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001524{
1525 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001526 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001527
Chao Yu487261f2015-02-05 17:44:29 +08001528 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1529 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001530 return;
1531
Chao Yu487261f2015-02-05 17:44:29 +08001532 if (PageDirty(page)) {
1533 if (inode->i_ino == F2FS_META_INO(sbi))
1534 dec_page_count(sbi, F2FS_DIRTY_META);
1535 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1536 dec_page_count(sbi, F2FS_DIRTY_NODES);
1537 else
1538 inode_dec_dirty_pages(inode);
1539 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001540 ClearPagePrivate(page);
1541}
1542
Chao Yu487261f2015-02-05 17:44:29 +08001543int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001544{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001545 /* If this is dirty page, keep PagePrivate */
1546 if (PageDirty(page))
1547 return 0;
1548
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001549 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001550 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001551}
1552
1553static int f2fs_set_data_page_dirty(struct page *page)
1554{
1555 struct address_space *mapping = page->mapping;
1556 struct inode *inode = mapping->host;
1557
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001558 trace_f2fs_set_page_dirty(page, DATA);
1559
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001560 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001561
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001562 if (f2fs_is_atomic_file(inode)) {
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001563 register_inmem_page(inode, page);
1564 return 1;
1565 }
1566
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001567 if (!PageDirty(page)) {
1568 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001569 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001570 return 1;
1571 }
1572 return 0;
1573}
1574
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001575static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1576{
Chao Yu454ae7e2014-04-22 13:34:01 +08001577 struct inode *inode = mapping->host;
1578
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001579 /* we don't need to use inline_data strictly */
1580 if (f2fs_has_inline_data(inode)) {
1581 int err = f2fs_convert_inline_inode(inode);
1582 if (err)
1583 return err;
1584 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001585 return generic_block_bmap(mapping, block, get_data_block);
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001586}
1587
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001588const struct address_space_operations f2fs_dblock_aops = {
1589 .readpage = f2fs_read_data_page,
1590 .readpages = f2fs_read_data_pages,
1591 .writepage = f2fs_write_data_page,
1592 .writepages = f2fs_write_data_pages,
1593 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001594 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001595 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001596 .invalidatepage = f2fs_invalidate_page,
1597 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001598 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001599 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001600};