blob: e5c762b372390b59fd0ceb011fea75ab19a41fe7 [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
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020030static void f2fs_read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090031{
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)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020036 if (bio->bi_error) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070037 fscrypt_release_ctx(bio->bi_private);
Chao Yu12377022015-05-25 18:03:38 +080038 } else {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070039 fscrypt_decrypt_bio_pages(bio->bi_private, bio);
Chao Yu12377022015-05-25 18:03:38 +080040 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
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020047 if (!bio->bi_error) {
Linus Torvaldsf5688492014-01-30 11:19:05 -080048 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
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020058static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090059{
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 Kim0b81d072015-05-15 16:26:10 -070067 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -070068
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020069 if (unlikely(bio->bi_error)) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090070 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim744602c2014-01-24 09:42:16 +090071 f2fs_stop_checkpoint(sbi);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090072 }
73 end_page_writeback(page);
74 dec_page_count(sbi, F2FS_WRITEBACK);
Linus Torvaldsf5688492014-01-30 11:19:05 -080075 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090076
Jaegeuk Kim7c506892016-01-26 11:55:35 -080077 if (!get_pages(sbi, F2FS_WRITEBACK) && wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090078 wake_up(&sbi->cp_wait);
79
80 bio_put(bio);
81}
82
Gu Zheng940a6d32013-12-20 17:39:59 +080083/*
84 * Low-level block read/write IO operations.
85 */
86static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
87 int npages, bool is_read)
88{
89 struct bio *bio;
90
Jaegeuk Kim740432f2015-08-14 11:43:56 -070091 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +080092
93 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +080094 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +080095 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +080096 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +080097
98 return bio;
99}
100
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900101static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900102{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900103 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900104
105 if (!io->bio)
106 return;
107
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700108 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800109 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700110 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800111 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900112
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700113 submit_bio(fio->rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900114 io->bio = NULL;
115}
116
Chao Yu0c3a5792016-01-18 18:28:11 +0800117static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
118 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800119{
Chao Yu0fd785e2016-01-18 18:24:59 +0800120 struct bio_vec *bvec;
121 struct page *target;
122 int i;
123
Chao Yu0c3a5792016-01-18 18:28:11 +0800124 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800125 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800126
127 if (!inode && !page && !ino)
128 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800129
130 bio_for_each_segment_all(bvec, io->bio, i) {
131
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700132 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800133 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700134 else
135 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800136
Chao Yu0c3a5792016-01-18 18:28:11 +0800137 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800138 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800139 if (page && page == target)
140 return true;
141 if (ino && ino == ino_of_node(target))
142 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800143 }
144
Chao Yu0fd785e2016-01-18 18:24:59 +0800145 return false;
146}
147
Chao Yu0c3a5792016-01-18 18:28:11 +0800148static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
149 struct page *page, nid_t ino,
150 enum page_type type)
151{
152 enum page_type btype = PAGE_TYPE_OF_BIO(type);
153 struct f2fs_bio_info *io = &sbi->write_io[btype];
154 bool ret;
155
156 down_read(&io->io_rwsem);
157 ret = __has_merged_page(io, inode, page, ino);
158 up_read(&io->io_rwsem);
159 return ret;
160}
161
162static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
163 struct inode *inode, struct page *page,
164 nid_t ino, enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900165{
166 enum page_type btype = PAGE_TYPE_OF_BIO(type);
167 struct f2fs_bio_info *io;
168
169 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
170
Chao Yudf0f8dc2014-03-22 14:57:23 +0800171 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900172
Chao Yu0c3a5792016-01-18 18:28:11 +0800173 if (!__has_merged_page(io, inode, page, ino))
174 goto out;
175
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900176 /* change META to META_FLUSH in the checkpoint procedure */
177 if (type >= META_FLUSH) {
178 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700179 if (test_opt(sbi, NOBARRIER))
180 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
181 else
182 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900183 }
184 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800185out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800186 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900187}
188
Chao Yu0c3a5792016-01-18 18:28:11 +0800189void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
190 int rw)
191{
192 __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw);
193}
194
195void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
196 struct inode *inode, struct page *page,
197 nid_t ino, enum page_type type, int rw)
198{
199 if (has_merged_page(sbi, inode, page, ino, type))
200 __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw);
201}
202
Chao Yu406657d2016-02-24 17:17:55 +0800203void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
204{
205 f2fs_submit_merged_bio(sbi, DATA, WRITE);
206 f2fs_submit_merged_bio(sbi, NODE, WRITE);
207 f2fs_submit_merged_bio(sbi, META, WRITE);
208}
209
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900210/*
211 * Fill the locked page with data located in the block address.
212 * Return unlocked page.
213 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700214int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900215{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900216 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700217 struct page *page = fio->encrypted_page ?
218 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900219
Chao Yu2ace38e2014-12-24 16:08:14 +0800220 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700221 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900222
223 /* Allocate a new bio */
Chao Yu7a9d7542016-02-22 18:36:38 +0800224 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900225
226 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
227 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900228 return -EFAULT;
229 }
230
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800231 submit_bio(fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900232 return 0;
233}
234
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700235void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900236{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700237 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900238 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900239 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800240 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700241 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900242
Gu Zheng940a6d32013-12-20 17:39:59 +0800243 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900244
Chao Yu7a9d7542016-02-22 18:36:38 +0800245 if (fio->old_blkaddr != NEW_ADDR)
246 verify_block_addr(sbi, fio->old_blkaddr);
247 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900248
Chao Yudf0f8dc2014-03-22 14:57:23 +0800249 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900250
Gu Zheng940a6d32013-12-20 17:39:59 +0800251 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900252 inc_page_count(sbi, F2FS_WRITEBACK);
253
Chao Yu7a9d7542016-02-22 18:36:38 +0800254 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900255 io->fio.rw != fio->rw))
256 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900257alloc_new:
258 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700259 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800260
Chao Yu7a9d7542016-02-22 18:36:38 +0800261 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
262 bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900263 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900264 }
265
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700266 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
267
268 if (bio_add_page(io->bio, bio_page, PAGE_CACHE_SIZE, 0) <
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900269 PAGE_CACHE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900270 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900271 goto alloc_new;
272 }
273
Chao Yu7a9d7542016-02-22 18:36:38 +0800274 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700275 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900276
Chao Yudf0f8dc2014-03-22 14:57:23 +0800277 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700278 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900279}
280
281/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900282 * Lock ordering for the change of data block address:
283 * ->data_page
284 * ->node_page
285 * update block addresses in the node page
286 */
Chao Yu216a6202015-03-19 19:23:32 +0800287void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900288{
289 struct f2fs_node *rn;
290 __le32 *addr_array;
291 struct page *node_page = dn->node_page;
292 unsigned int ofs_in_node = dn->ofs_in_node;
293
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800294 f2fs_wait_on_page_writeback(node_page, NODE, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900295
Gu Zheng45590712013-07-15 17:57:38 +0800296 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900297
298 /* Get physical address of data block */
299 addr_array = blkaddr_in_node(rn);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800300 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800301 if (set_page_dirty(node_page))
302 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900303}
304
Chao Yuf28b3432016-02-24 17:16:47 +0800305void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
306{
307 dn->data_blkaddr = blkaddr;
308 set_data_blkaddr(dn);
309 f2fs_update_extent_cache(dn);
310}
311
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900312int reserve_new_block(struct dnode_of_data *dn)
313{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700314 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900315
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900316 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900317 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800318 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900319 return -ENOSPC;
320
Namjae Jeonc01e2852013-04-23 17:00:52 +0900321 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
322
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900323 dn->data_blkaddr = NEW_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800324 set_data_blkaddr(dn);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900325 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900326 sync_inode_page(dn);
327 return 0;
328}
329
Huajun Lib6009652013-11-10 23:13:18 +0800330int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
331{
332 bool need_put = dn->inode_page ? false : true;
333 int err;
334
335 err = get_dnode_of_data(dn, index, ALLOC_NODE);
336 if (err)
337 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900338
Huajun Lib6009652013-11-10 23:13:18 +0800339 if (dn->data_blkaddr == NULL_ADDR)
340 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900341 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800342 f2fs_put_dnode(dn);
343 return err;
344}
345
Fan Li759af1c2015-08-05 15:52:16 +0800346int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900347{
Chao Yu028a41e2015-03-19 19:26:02 +0800348 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800349 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800350
Fan Li759af1c2015-08-05 15:52:16 +0800351 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
352 dn->data_blkaddr = ei.blk + index - ei.fofs;
353 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800354 }
355
Fan Li759af1c2015-08-05 15:52:16 +0800356 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900357}
358
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700359struct page *get_read_data_page(struct inode *inode, pgoff_t index,
360 int rw, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900361{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900362 struct address_space *mapping = inode->i_mapping;
363 struct dnode_of_data dn;
364 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800365 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900366 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800367 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700368 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800369 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700370 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700371 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800372 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900373
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700374 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
375 return read_mapping_page(mapping, index, NULL);
376
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700377 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900378 if (!page)
379 return ERR_PTR(-ENOMEM);
380
Chao Yucb3bc9e2015-02-05 18:03:40 +0800381 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
382 dn.data_blkaddr = ei.blk + index - ei.fofs;
383 goto got_it;
384 }
385
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900386 set_new_dnode(&dn, inode, NULL, NULL, 0);
387 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700388 if (err)
389 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900390 f2fs_put_dnode(&dn);
391
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900392 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700393 err = -ENOENT;
394 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900395 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800396got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700397 if (PageUptodate(page)) {
398 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900399 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700400 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900401
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900402 /*
403 * A new dentry page is allocated but not able to be written, since its
404 * new inode page couldn't be allocated due to -ENOSPC.
405 * In such the case, its blkaddr can be remained as NEW_ADDR.
406 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
407 */
408 if (dn.data_blkaddr == NEW_ADDR) {
409 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
410 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700411 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900412 return page;
413 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900414
Chao Yu7a9d7542016-02-22 18:36:38 +0800415 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700416 fio.page = page;
417 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900418 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700419 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700420 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700421
422put_err:
423 f2fs_put_page(page, 1);
424 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700425}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900426
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700427struct page *find_data_page(struct inode *inode, pgoff_t index)
428{
429 struct address_space *mapping = inode->i_mapping;
430 struct page *page;
431
432 page = find_get_page(mapping, index);
433 if (page && PageUptodate(page))
434 return page;
435 f2fs_put_page(page, 0);
436
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700437 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700438 if (IS_ERR(page))
439 return page;
440
441 if (PageUptodate(page))
442 return page;
443
444 wait_on_page_locked(page);
445 if (unlikely(!PageUptodate(page))) {
446 f2fs_put_page(page, 0);
447 return ERR_PTR(-EIO);
448 }
449 return page;
450}
451
452/*
453 * If it tries to access a hole, return an error.
454 * Because, the callers, functions in dir.c and GC, should be able to know
455 * whether this page exists or not.
456 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700457struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
458 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700459{
460 struct address_space *mapping = inode->i_mapping;
461 struct page *page;
462repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700463 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700464 if (IS_ERR(page))
465 return page;
466
467 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900468 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900469 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900470 f2fs_put_page(page, 1);
471 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900472 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900473 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900474 f2fs_put_page(page, 1);
475 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900476 }
477 return page;
478}
479
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900480/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900481 * Caller ensures that this data page is never allocated.
482 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900483 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800484 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
485 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800486 * Note that, ipage is set only by make_empty_dir, and if any error occur,
487 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900488 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900489struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900490 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900491{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900492 struct address_space *mapping = inode->i_mapping;
493 struct page *page;
494 struct dnode_of_data dn;
495 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800496
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700497 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800498 if (!page) {
499 /*
500 * before exiting, we should make sure ipage will be released
501 * if any error occur.
502 */
503 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700504 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800505 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900506
Jaegeuk Kima8865372013-12-27 17:04:17 +0900507 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800508 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700509 if (err) {
510 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900511 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900512 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700513 if (!ipage)
514 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900515
516 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700517 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900518
519 if (dn.data_blkaddr == NEW_ADDR) {
520 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900521 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900522 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700523 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900524
Jaegeuk Kim76121182016-01-01 22:03:47 -0800525 /* if ipage exists, blkaddr should be NEW_ADDR */
526 f2fs_bug_on(F2FS_I_SB(inode), ipage);
527 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700528 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800529 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900530 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700531got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800532 if (new_i_size && i_size_read(inode) <
533 ((loff_t)(index + 1) << PAGE_CACHE_SHIFT)) {
534 i_size_write(inode, ((loff_t)(index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900535 /* Only the directory inode sets new_i_size */
536 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900537 }
538 return page;
539}
540
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900541static int __allocate_data_block(struct dnode_of_data *dn)
542{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700543 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900544 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900545 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800546 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700547 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900548
549 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
550 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800551
552 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
553 if (dn->data_blkaddr == NEW_ADDR)
554 goto alloc;
555
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900556 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
557 return -ENOSPC;
558
Chao Yudf6136e2015-03-23 10:33:37 +0800559alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900560 get_node_info(sbi, dn->nid, &ni);
561 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
562
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800563 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
564 seg = CURSEG_DIRECT_IO;
565
Chao Yudf6136e2015-03-23 10:33:37 +0800566 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
567 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800568 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900569
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700570 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800571 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700572 dn->ofs_in_node;
Chao Yu9edcdab2015-09-11 14:43:52 +0800573 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT))
574 i_size_write(dn->inode,
575 ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900576 return 0;
577}
578
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800579ssize_t f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800580{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800581 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800582 struct f2fs_map_blocks map;
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800583 ssize_t ret = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800584
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800585 map.m_lblk = F2FS_BYTES_TO_BLK(iocb->ki_pos);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800586 map.m_len = F2FS_BLK_ALIGN(iov_iter_count(from));
Chao Yuda859852016-01-26 15:42:58 +0800587 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800588
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800589 if (f2fs_encrypted_inode(inode))
590 return 0;
591
592 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800593 ret = f2fs_convert_inline_inode(inode);
594 if (ret)
595 return ret;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800596 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800597 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800598 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
599 ret = f2fs_convert_inline_inode(inode);
600 if (ret)
601 return ret;
602 }
603 if (!f2fs_has_inline_data(inode))
604 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800605 return ret;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800606}
607
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900608/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700609 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
610 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800611 * If original data blocks are allocated, then give them to blockdev.
612 * Otherwise,
613 * a. preallocate requested block addresses
614 * b. do not use extent cache for better performance
615 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900616 */
Chao Yud323d002015-10-27 09:53:45 +0800617int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800618 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900619{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700620 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900621 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800622 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900623 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
624 pgoff_t pgofs, end_offset;
625 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800626 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900627 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800628 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900629
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700630 map->m_len = 0;
631 map->m_flags = 0;
632
633 /* it only supports block size == page size */
634 pgofs = (pgoff_t)map->m_lblk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900635
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800636 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700637 map->m_pblk = ei.blk + pgofs - ei.fofs;
638 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
639 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900640 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800641 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900642
Chao Yu4fe71e82016-01-26 15:37:38 +0800643next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800644 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800645 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900646
647 /* When reading holes, we need its node page */
648 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900649 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900650 if (err) {
Chao Yuda859852016-01-26 15:42:58 +0800651 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900652 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800653 if (map->m_next_pgofs)
654 *map->m_next_pgofs =
655 get_next_page_offset(&dn, pgofs);
656 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900657 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900658 }
Chao Yu973163f2015-09-18 16:51:51 +0800659
Chao Yu81ca7352016-01-26 15:39:35 +0800660 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900661
Chao Yu4fe71e82016-01-26 15:37:38 +0800662next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800663 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800664
Fan Li7df3a432015-12-17 13:20:59 +0800665 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
666 if (create) {
667 if (unlikely(f2fs_cp_error(sbi))) {
668 err = -EIO;
669 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800670 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800671 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
672 if (blkaddr == NULL_ADDR)
673 err = reserve_new_block(&dn);
674 } else {
675 err = __allocate_data_block(&dn);
676 }
Fan Li7df3a432015-12-17 13:20:59 +0800677 if (err)
678 goto sync_out;
679 allocated = true;
Chao Yu4fe71e82016-01-26 15:37:38 +0800680 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800681 blkaddr = dn.data_blkaddr;
682 } else {
Chao Yuda859852016-01-26 15:42:58 +0800683 if (flag == F2FS_GET_BLOCK_FIEMAP &&
684 blkaddr == NULL_ADDR) {
685 if (map->m_next_pgofs)
686 *map->m_next_pgofs = pgofs + 1;
687 }
Fan Li7df3a432015-12-17 13:20:59 +0800688 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu4fe71e82016-01-26 15:37:38 +0800689 blkaddr != NEW_ADDR) {
690 if (flag == F2FS_GET_BLOCK_BMAP)
691 err = -ENOENT;
Fan Li7df3a432015-12-17 13:20:59 +0800692 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800693 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900694 }
695 }
Fan Li7df3a432015-12-17 13:20:59 +0800696
Chao Yu4fe71e82016-01-26 15:37:38 +0800697 if (map->m_len == 0) {
698 /* preallocated unwritten block should be mapped for fiemap. */
699 if (blkaddr == NEW_ADDR)
700 map->m_flags |= F2FS_MAP_UNWRITTEN;
701 map->m_flags |= F2FS_MAP_MAPPED;
702
703 map->m_pblk = blkaddr;
704 map->m_len = 1;
705 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800706 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800707 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800708 flag == F2FS_GET_BLOCK_PRE_DIO ||
709 flag == F2FS_GET_BLOCK_PRE_AIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800710 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800711 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800712 } else {
713 goto sync_out;
714 }
715
716 dn.ofs_in_node++;
717 pgofs++;
718
719 if (map->m_len < maxblocks) {
720 if (dn.ofs_in_node < end_offset)
721 goto next_block;
722
723 if (allocated)
724 sync_inode_page(&dn);
725 f2fs_put_dnode(&dn);
726
727 if (create) {
728 f2fs_unlock_op(sbi);
729 f2fs_balance_fs(sbi, allocated);
730 }
731 allocated = false;
732 goto next_dnode;
Fan Li7df3a432015-12-17 13:20:59 +0800733 }
734
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900735sync_out:
736 if (allocated)
737 sync_inode_page(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900738 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900739unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800740 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800741 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800742 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800743 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900744out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700745 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900746 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900747}
748
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700749static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800750 struct buffer_head *bh, int create, int flag,
751 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700752{
753 struct f2fs_map_blocks map;
754 int ret;
755
756 map.m_lblk = iblock;
757 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800758 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700759
Chao Yue2b4e2b2015-08-19 19:11:19 +0800760 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700761 if (!ret) {
762 map_bh(bh, inode->i_sb, map.m_pblk);
763 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
764 bh->b_size = map.m_len << inode->i_blkbits;
765 }
766 return ret;
767}
768
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900769static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800770 struct buffer_head *bh_result, int create, int flag,
771 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900772{
Chao Yuda859852016-01-26 15:42:58 +0800773 return __get_data_block(inode, iblock, bh_result, create,
774 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900775}
776
Chao Yue2b4e2b2015-08-19 19:11:19 +0800777static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900778 struct buffer_head *bh_result, int create)
779{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800780 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800781 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800782}
783
784static int get_data_block_bmap(struct inode *inode, sector_t iblock,
785 struct buffer_head *bh_result, int create)
786{
Yunlei He179448b2015-12-28 21:48:32 +0800787 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800788 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800789 return -EFBIG;
790
Chao Yue2b4e2b2015-08-19 19:11:19 +0800791 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800792 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900793}
794
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700795static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
796{
797 return (offset >> inode->i_blkbits);
798}
799
800static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
801{
802 return (blk << inode->i_blkbits);
803}
804
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900805int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
806 u64 start, u64 len)
807{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700808 struct buffer_head map_bh;
809 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800810 pgoff_t next_pgofs;
Fan Lide1475c2016-01-04 15:56:50 +0800811 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700812 u64 logical = 0, phys = 0, size = 0;
813 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700814 int ret = 0;
815
816 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
817 if (ret)
818 return ret;
819
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -0700820 if (f2fs_has_inline_data(inode)) {
821 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
822 if (ret != -EAGAIN)
823 return ret;
824 }
825
Al Viro59551022016-01-22 15:40:57 -0500826 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800827
828 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800829 if (start >= isize)
830 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700831
Fan Li9a950d52015-12-26 18:07:41 +0800832 if (start + len > isize)
833 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700834
835 if (logical_to_blk(inode, len) == 0)
836 len = blk_to_logical(inode, 1);
837
838 start_blk = logical_to_blk(inode, start);
839 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800840
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700841next:
842 memset(&map_bh, 0, sizeof(struct buffer_head));
843 map_bh.b_size = len;
844
Chao Yue2b4e2b2015-08-19 19:11:19 +0800845 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800846 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700847 if (ret)
848 goto out;
849
850 /* HOLE */
851 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800852 start_blk = next_pgofs;
Fan Li9a950d52015-12-26 18:07:41 +0800853 /* Go through holes util pass the EOF */
Chao Yuda859852016-01-26 15:42:58 +0800854 if (blk_to_logical(inode, start_blk) < isize)
Fan Li9a950d52015-12-26 18:07:41 +0800855 goto prep_next;
856 /* Found a hole beyond isize means no more extents.
857 * Note that the premise is that filesystems don't
858 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700859 */
Fan Li9a950d52015-12-26 18:07:41 +0800860 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700861 }
Fan Li9a950d52015-12-26 18:07:41 +0800862
Chao Yuda5af122016-01-08 20:19:27 +0800863 if (size) {
864 if (f2fs_encrypted_inode(inode))
865 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
866
Fan Li9a950d52015-12-26 18:07:41 +0800867 ret = fiemap_fill_next_extent(fieinfo, logical,
868 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800869 }
Fan Li9a950d52015-12-26 18:07:41 +0800870
871 if (start_blk > last_blk || ret)
872 goto out;
873
874 logical = blk_to_logical(inode, start_blk);
875 phys = blk_to_logical(inode, map_bh.b_blocknr);
876 size = map_bh.b_size;
877 flags = 0;
878 if (buffer_unwritten(&map_bh))
879 flags = FIEMAP_EXTENT_UNWRITTEN;
880
881 start_blk += logical_to_blk(inode, size);
882
883prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700884 cond_resched();
885 if (fatal_signal_pending(current))
886 ret = -EINTR;
887 else
888 goto next;
889out:
890 if (ret == 1)
891 ret = 0;
892
Al Viro59551022016-01-22 15:40:57 -0500893 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700894 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900895}
896
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700897/*
898 * This function was originally taken from fs/mpage.c, and customized for f2fs.
899 * Major change was from block_size == page_size in f2fs by default.
900 */
901static int f2fs_mpage_readpages(struct address_space *mapping,
902 struct list_head *pages, struct page *page,
903 unsigned nr_pages)
904{
905 struct bio *bio = NULL;
906 unsigned page_idx;
907 sector_t last_block_in_bio = 0;
908 struct inode *inode = mapping->host;
909 const unsigned blkbits = inode->i_blkbits;
910 const unsigned blocksize = 1 << blkbits;
911 sector_t block_in_file;
912 sector_t last_block;
913 sector_t last_block_in_file;
914 sector_t block_nr;
915 struct block_device *bdev = inode->i_sb->s_bdev;
916 struct f2fs_map_blocks map;
917
918 map.m_pblk = 0;
919 map.m_lblk = 0;
920 map.m_len = 0;
921 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +0800922 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700923
924 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
925
926 prefetchw(&page->flags);
927 if (pages) {
928 page = list_entry(pages->prev, struct page, lru);
929 list_del(&page->lru);
930 if (add_to_page_cache_lru(page, mapping,
931 page->index, GFP_KERNEL))
932 goto next_page;
933 }
934
935 block_in_file = (sector_t)page->index;
936 last_block = block_in_file + nr_pages;
937 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
938 blkbits;
939 if (last_block > last_block_in_file)
940 last_block = last_block_in_file;
941
942 /*
943 * Map blocks using the previous result first.
944 */
945 if ((map.m_flags & F2FS_MAP_MAPPED) &&
946 block_in_file > map.m_lblk &&
947 block_in_file < (map.m_lblk + map.m_len))
948 goto got_it;
949
950 /*
951 * Then do more f2fs_map_blocks() calls until we are
952 * done with this page.
953 */
954 map.m_flags = 0;
955
956 if (block_in_file < last_block) {
957 map.m_lblk = block_in_file;
958 map.m_len = last_block - block_in_file;
959
Chao Yu46c9e142015-09-18 16:54:16 +0800960 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +0800961 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700962 goto set_error_page;
963 }
964got_it:
965 if ((map.m_flags & F2FS_MAP_MAPPED)) {
966 block_nr = map.m_pblk + block_in_file - map.m_lblk;
967 SetPageMappedToDisk(page);
968
969 if (!PageUptodate(page) && !cleancache_get_page(page)) {
970 SetPageUptodate(page);
971 goto confused;
972 }
973 } else {
974 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
975 SetPageUptodate(page);
976 unlock_page(page);
977 goto next_page;
978 }
979
980 /*
981 * This page will go to BIO. Do we need to send this
982 * BIO off first?
983 */
984 if (bio && (last_block_in_bio != block_nr - 1)) {
985submit_and_realloc:
986 submit_bio(READ, bio);
987 bio = NULL;
988 }
989 if (bio == NULL) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700990 struct fscrypt_ctx *ctx = NULL;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700991
992 if (f2fs_encrypted_inode(inode) &&
993 S_ISREG(inode->i_mode)) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700994
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700995 ctx = fscrypt_get_ctx(inode);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700996 if (IS_ERR(ctx))
997 goto set_error_page;
998
999 /* wait the page to be moved by cleaning */
Chao Yu08b39fb2015-10-08 13:27:34 +08001000 f2fs_wait_on_encrypted_page_writeback(
1001 F2FS_I_SB(inode), block_nr);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001002 }
1003
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001004 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +02001005 min_t(int, nr_pages, BIO_MAX_PAGES));
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001006 if (!bio) {
1007 if (ctx)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001008 fscrypt_release_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001009 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001010 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001011 bio->bi_bdev = bdev;
1012 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +08001013 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001014 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001015 }
1016
1017 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1018 goto submit_and_realloc;
1019
1020 last_block_in_bio = block_nr;
1021 goto next_page;
1022set_error_page:
1023 SetPageError(page);
1024 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1025 unlock_page(page);
1026 goto next_page;
1027confused:
1028 if (bio) {
1029 submit_bio(READ, bio);
1030 bio = NULL;
1031 }
1032 unlock_page(page);
1033next_page:
1034 if (pages)
1035 page_cache_release(page);
1036 }
1037 BUG_ON(pages && !list_empty(pages));
1038 if (bio)
1039 submit_bio(READ, bio);
1040 return 0;
1041}
1042
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001043static int f2fs_read_data_page(struct file *file, struct page *page)
1044{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001045 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001046 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001047
Chao Yuc20e89c2014-05-06 16:53:08 +08001048 trace_f2fs_readpage(page, DATA);
1049
arter97e1c42042014-08-06 23:22:50 +09001050 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001051 if (f2fs_has_inline_data(inode))
1052 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001053 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001054 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001055 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001056}
1057
1058static int f2fs_read_data_pages(struct file *file,
1059 struct address_space *mapping,
1060 struct list_head *pages, unsigned nr_pages)
1061{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001062 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001063 struct page *page = list_entry(pages->prev, struct page, lru);
1064
1065 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001066
1067 /* If the file has inline data, skip readpages */
1068 if (f2fs_has_inline_data(inode))
1069 return 0;
1070
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001071 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001072}
1073
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001074int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001075{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001076 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001077 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001078 struct dnode_of_data dn;
1079 int err = 0;
1080
1081 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001082 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001083 if (err)
1084 return err;
1085
Chao Yu28bc1062016-02-06 14:40:34 +08001086 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001087
1088 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001089 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001090 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001091 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001092 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001093
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001094 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Chao Yu08b39fb2015-10-08 13:27:34 +08001095
1096 /* wait for GCed encrypted page writeback */
1097 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001098 fio->old_blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001099
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001100 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001101 if (IS_ERR(fio->encrypted_page)) {
1102 err = PTR_ERR(fio->encrypted_page);
1103 goto out_writepage;
1104 }
1105 }
1106
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001107 set_page_writeback(page);
1108
1109 /*
1110 * If current allocation needs SSR,
1111 * it had better in-place writes for updated data.
1112 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001113 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001114 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001115 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001116 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001117 rewrite_data_page(fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001118 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001119 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001120 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001121 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001122 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001123 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001124 if (page->index == 0)
1125 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001126 }
1127out_writepage:
1128 f2fs_put_dnode(&dn);
1129 return err;
1130}
1131
1132static int f2fs_write_data_page(struct page *page,
1133 struct writeback_control *wbc)
1134{
1135 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001136 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001137 loff_t i_size = i_size_read(inode);
1138 const pgoff_t end_index = ((unsigned long long) i_size)
1139 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001140 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001141 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001142 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001143 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001144 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001145 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001146 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001147 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001148 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001149 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001150
Chao Yuecda0de2014-05-06 16:48:26 +08001151 trace_f2fs_writepage(page, DATA);
1152
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001153 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001154 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001155
1156 /*
1157 * If the offset is out-of-range of file size,
1158 * this page does not have to be written to disk.
1159 */
1160 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001161 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001162 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001163
1164 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001165write:
Chao Yucaf00472015-01-28 17:48:42 +08001166 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001167 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001168 if (f2fs_is_drop_cache(inode))
1169 goto out;
1170 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1171 available_free_memory(sbi, BASE_CHECK))
1172 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001173
Jaegeuk Kim39936832012-11-22 16:21:29 +09001174 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001175 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001176 if (unlikely(f2fs_cp_error(sbi)))
1177 goto redirty_out;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001178 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001179 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001180 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001181
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001182 /* we should bypass data pages to proceed the kworkder jobs */
1183 if (unlikely(f2fs_cp_error(sbi))) {
1184 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001185 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001186 }
1187
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001188 if (!wbc->for_reclaim)
1189 need_balance_fs = true;
1190 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001191 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001192
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001193 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001194 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001195 if (f2fs_has_inline_data(inode))
1196 err = f2fs_write_inline_data(inode, page);
1197 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001198 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001199 f2fs_unlock_op(sbi);
1200done:
1201 if (err && err != -ENOENT)
1202 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001203
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001204 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001205out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001206 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001207 if (err)
1208 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001209
1210 if (wbc->for_reclaim) {
1211 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001212 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001213 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001214
1215 unlock_page(page);
1216 f2fs_balance_fs(sbi, need_balance_fs);
1217
1218 if (unlikely(f2fs_cp_error(sbi)))
1219 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1220
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001221 return 0;
1222
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001223redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001224 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001225 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001226}
1227
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001228static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1229 void *data)
1230{
1231 struct address_space *mapping = data;
1232 int ret = mapping->a_ops->writepage(page, wbc);
1233 mapping_set_error(mapping, ret);
1234 return ret;
1235}
1236
Chao Yu8f46dca2015-07-14 18:56:10 +08001237/*
1238 * This function was copied from write_cche_pages from mm/page-writeback.c.
1239 * The major change is making write step of cold data page separately from
1240 * warm/hot data page.
1241 */
1242static int f2fs_write_cache_pages(struct address_space *mapping,
1243 struct writeback_control *wbc, writepage_t writepage,
1244 void *data)
1245{
1246 int ret = 0;
1247 int done = 0;
1248 struct pagevec pvec;
1249 int nr_pages;
1250 pgoff_t uninitialized_var(writeback_index);
1251 pgoff_t index;
1252 pgoff_t end; /* Inclusive */
1253 pgoff_t done_index;
1254 int cycled;
1255 int range_whole = 0;
1256 int tag;
1257 int step = 0;
1258
1259 pagevec_init(&pvec, 0);
1260next:
1261 if (wbc->range_cyclic) {
1262 writeback_index = mapping->writeback_index; /* prev offset */
1263 index = writeback_index;
1264 if (index == 0)
1265 cycled = 1;
1266 else
1267 cycled = 0;
1268 end = -1;
1269 } else {
1270 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1271 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1272 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1273 range_whole = 1;
1274 cycled = 1; /* ignore range_cyclic tests */
1275 }
1276 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1277 tag = PAGECACHE_TAG_TOWRITE;
1278 else
1279 tag = PAGECACHE_TAG_DIRTY;
1280retry:
1281 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1282 tag_pages_for_writeback(mapping, index, end);
1283 done_index = index;
1284 while (!done && (index <= end)) {
1285 int i;
1286
1287 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1288 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1289 if (nr_pages == 0)
1290 break;
1291
1292 for (i = 0; i < nr_pages; i++) {
1293 struct page *page = pvec.pages[i];
1294
1295 if (page->index > end) {
1296 done = 1;
1297 break;
1298 }
1299
1300 done_index = page->index;
1301
1302 lock_page(page);
1303
1304 if (unlikely(page->mapping != mapping)) {
1305continue_unlock:
1306 unlock_page(page);
1307 continue;
1308 }
1309
1310 if (!PageDirty(page)) {
1311 /* someone wrote it for us */
1312 goto continue_unlock;
1313 }
1314
Tiezhu Yang737f1892015-07-17 12:56:00 +08001315 if (step == is_cold_data(page))
Chao Yu8f46dca2015-07-14 18:56:10 +08001316 goto continue_unlock;
1317
1318 if (PageWriteback(page)) {
1319 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001320 f2fs_wait_on_page_writeback(page,
1321 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001322 else
1323 goto continue_unlock;
1324 }
1325
1326 BUG_ON(PageWriteback(page));
1327 if (!clear_page_dirty_for_io(page))
1328 goto continue_unlock;
1329
1330 ret = (*writepage)(page, wbc, data);
1331 if (unlikely(ret)) {
1332 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1333 unlock_page(page);
1334 ret = 0;
1335 } else {
1336 done_index = page->index + 1;
1337 done = 1;
1338 break;
1339 }
1340 }
1341
1342 if (--wbc->nr_to_write <= 0 &&
1343 wbc->sync_mode == WB_SYNC_NONE) {
1344 done = 1;
1345 break;
1346 }
1347 }
1348 pagevec_release(&pvec);
1349 cond_resched();
1350 }
1351
1352 if (step < 1) {
1353 step++;
1354 goto next;
1355 }
1356
1357 if (!cycled && !done) {
1358 cycled = 1;
1359 index = 0;
1360 end = writeback_index - 1;
1361 goto retry;
1362 }
1363 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1364 mapping->writeback_index = done_index;
1365
1366 return ret;
1367}
1368
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001369static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001370 struct writeback_control *wbc)
1371{
1372 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001373 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001374 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001375 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001376 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001377
P J Pcfb185a2013-04-03 11:38:00 +09001378 /* deal with chardevs and other special file */
1379 if (!mapping->a_ops->writepage)
1380 return 0;
1381
Chao Yu6a290542015-07-17 18:02:39 +08001382 /* skip writing if there is no dirty page in this inode */
1383 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1384 return 0;
1385
Jaegeuk Kima1257022015-10-08 10:40:07 -07001386 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1387 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1388 available_free_memory(sbi, DIRTY_DENTS))
1389 goto skip_write;
1390
Chao Yud323d002015-10-27 09:53:45 +08001391 /* skip writing during file defragment */
1392 if (is_inode_flag_set(F2FS_I(inode), FI_DO_DEFRAG))
1393 goto skip_write;
1394
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001395 /* during POR, we don't need to trigger writepage at all. */
1396 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1397 goto skip_write;
1398
Yunlei Hed31c7c32016-02-04 16:14:00 +08001399 trace_f2fs_writepages(mapping->host, wbc, DATA);
1400
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001401 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001402
Jaegeuk Kim25c13552016-01-20 23:46:05 +08001403 if (!S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_ALL) {
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001404 mutex_lock(&sbi->writepages);
1405 locked = true;
1406 }
Chao Yu8f46dca2015-07-14 18:56:10 +08001407 ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Chao Yu0c3a5792016-01-18 18:28:11 +08001408 f2fs_submit_merged_bio_cond(sbi, inode, NULL, 0, DATA, WRITE);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001409 if (locked)
1410 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001411
Chao Yuc227f912015-12-16 13:09:20 +08001412 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001413
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001414 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001415 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001416
1417skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001418 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001419 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001420 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001421}
1422
Chao Yu3aab8f82014-07-02 13:25:04 +08001423static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1424{
1425 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001426 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001427
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001428 if (to > i_size) {
1429 truncate_pagecache(inode, i_size);
1430 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001431 }
1432}
1433
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001434static int prepare_write_begin(struct f2fs_sb_info *sbi,
1435 struct page *page, loff_t pos, unsigned len,
1436 block_t *blk_addr, bool *node_changed)
1437{
1438 struct inode *inode = page->mapping->host;
1439 pgoff_t index = page->index;
1440 struct dnode_of_data dn;
1441 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001442 bool locked = false;
1443 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001444 int err = 0;
1445
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001446 /*
1447 * we already allocated all the blocks, so we don't need to get
1448 * the block addresses when there is no need to fill the page.
1449 */
1450 if (!f2fs_has_inline_data(inode) && !f2fs_encrypted_inode(inode) &&
1451 len == PAGE_CACHE_SIZE)
1452 return 0;
1453
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001454 if (f2fs_has_inline_data(inode) ||
1455 (pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1456 f2fs_lock_op(sbi);
1457 locked = true;
1458 }
1459restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001460 /* check inline_data */
1461 ipage = get_node_page(sbi, inode->i_ino);
1462 if (IS_ERR(ipage)) {
1463 err = PTR_ERR(ipage);
1464 goto unlock_out;
1465 }
1466
1467 set_new_dnode(&dn, inode, ipage, ipage, 0);
1468
1469 if (f2fs_has_inline_data(inode)) {
1470 if (pos + len <= MAX_INLINE_DATA) {
1471 read_inline_data(page, ipage);
1472 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001473 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001474 } else {
1475 err = f2fs_convert_inline_page(&dn, page);
1476 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001477 goto out;
1478 if (dn.data_blkaddr == NULL_ADDR)
1479 err = f2fs_get_block(&dn, index);
1480 }
1481 } else if (locked) {
1482 err = f2fs_get_block(&dn, index);
1483 } else {
1484 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1485 dn.data_blkaddr = ei.blk + index - ei.fofs;
1486 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001487 /* hole case */
1488 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimb483fad2016-01-20 08:27:37 +08001489 if (err || (!err && dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001490 f2fs_put_dnode(&dn);
1491 f2fs_lock_op(sbi);
1492 locked = true;
1493 goto restart;
1494 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001495 }
1496 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001497
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001498 /* convert_inline_page can make node_changed */
1499 *blk_addr = dn.data_blkaddr;
1500 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001501out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001502 f2fs_put_dnode(&dn);
1503unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001504 if (locked)
1505 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001506 return err;
1507}
1508
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001509static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1510 loff_t pos, unsigned len, unsigned flags,
1511 struct page **pagep, void **fsdata)
1512{
1513 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001514 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001515 struct page *page = NULL;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001516 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001517 bool need_balance = false;
1518 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001519 int err = 0;
1520
Chao Yu62aed042014-05-06 16:46:04 +08001521 trace_f2fs_write_begin(inode, pos, len, flags);
1522
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001523 /*
1524 * We should check this at this moment to avoid deadlock on inode page
1525 * and #0 page. The locking rule for inline_data conversion should be:
1526 * lock_page(page #0) -> lock_page(inode_page)
1527 */
1528 if (index != 0) {
1529 err = f2fs_convert_inline_inode(inode);
1530 if (err)
1531 goto fail;
1532 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001533repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001534 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001535 if (!page) {
1536 err = -ENOMEM;
1537 goto fail;
1538 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001539
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001540 *pagep = page;
1541
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001542 err = prepare_write_begin(sbi, page, pos, len,
1543 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001544 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001545 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001546
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001547 if (need_balance && has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001548 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001549 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001550 lock_page(page);
1551 if (page->mapping != mapping) {
1552 /* The page got truncated from under us */
1553 f2fs_put_page(page, 1);
1554 goto repeat;
1555 }
1556 }
1557
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001558 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001559
Chao Yu08b39fb2015-10-08 13:27:34 +08001560 /* wait for GCed encrypted page writeback */
1561 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001562 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001563
Chao Yu90d43882015-07-08 18:24:38 +08001564 if (len == PAGE_CACHE_SIZE)
1565 goto out_update;
1566 if (PageUptodate(page))
1567 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001568
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001569 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1570 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1571 unsigned end = start + len;
1572
1573 /* Reading beyond i_size is simple: memset to zero */
1574 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001575 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001576 }
1577
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001578 if (blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001579 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1580 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001581 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001582 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001583 .type = DATA,
1584 .rw = READ_SYNC,
Chao Yu7a9d7542016-02-22 18:36:38 +08001585 .old_blkaddr = blkaddr,
1586 .new_blkaddr = blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001587 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001588 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001589 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001590 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001591 if (err)
1592 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001593
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001594 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001595 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001596 err = -EIO;
1597 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001598 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001599 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001600 f2fs_put_page(page, 1);
1601 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001602 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001603
1604 /* avoid symlink page */
1605 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001606 err = fscrypt_decrypt_page(page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001607 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001608 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001609 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001610 }
Chao Yu90d43882015-07-08 18:24:38 +08001611out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001612 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001613out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001614 clear_cold_data(page);
1615 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001616
Chao Yu3aab8f82014-07-02 13:25:04 +08001617fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001618 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001619 f2fs_write_failed(mapping, pos + len);
1620 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001621}
1622
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001623static int f2fs_write_end(struct file *file,
1624 struct address_space *mapping,
1625 loff_t pos, unsigned len, unsigned copied,
1626 struct page *page, void *fsdata)
1627{
1628 struct inode *inode = page->mapping->host;
1629
Chao Yudfb2bf32014-05-06 16:47:23 +08001630 trace_f2fs_write_end(inode, pos, len, copied);
1631
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001632 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001633
1634 if (pos + copied > i_size_read(inode)) {
1635 i_size_write(inode, pos + copied);
1636 mark_inode_dirty(inode);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001637 }
1638
Chao Yu75c3c8b2013-11-16 14:15:59 +08001639 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001640 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001641 return copied;
1642}
1643
Omar Sandoval6f673762015-03-16 04:33:52 -07001644static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1645 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001646{
1647 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001648
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001649 if (offset & blocksize_mask)
1650 return -EINVAL;
1651
Al Viro5b46f252014-03-16 18:07:34 -04001652 if (iov_iter_alignment(iter) & blocksize_mask)
1653 return -EINVAL;
1654
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001655 return 0;
1656}
1657
Omar Sandoval22c61862015-03-16 04:33:53 -07001658static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
1659 loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001660{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001661 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001662 struct inode *inode = mapping->host;
1663 size_t count = iov_iter_count(iter);
1664 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001665
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001666 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001667 if (err)
1668 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001669
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001670 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1671 return 0;
1672
Omar Sandoval6f673762015-03-16 04:33:52 -07001673 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001674
Chao Yue2b4e2b2015-08-19 19:11:19 +08001675 err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block_dio);
Omar Sandoval6f673762015-03-16 04:33:52 -07001676 if (err < 0 && iov_iter_rw(iter) == WRITE)
Chao Yu3aab8f82014-07-02 13:25:04 +08001677 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001678
Omar Sandoval6f673762015-03-16 04:33:52 -07001679 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001680
Chao Yu3aab8f82014-07-02 13:25:04 +08001681 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001682}
1683
Chao Yu487261f2015-02-05 17:44:29 +08001684void f2fs_invalidate_page(struct page *page, unsigned int offset,
1685 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001686{
1687 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001688 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001689
Chao Yu487261f2015-02-05 17:44:29 +08001690 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1691 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001692 return;
1693
Chao Yu487261f2015-02-05 17:44:29 +08001694 if (PageDirty(page)) {
1695 if (inode->i_ino == F2FS_META_INO(sbi))
1696 dec_page_count(sbi, F2FS_DIRTY_META);
1697 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1698 dec_page_count(sbi, F2FS_DIRTY_NODES);
1699 else
1700 inode_dec_dirty_pages(inode);
1701 }
Chao Yudecd36b2015-08-07 18:42:09 +08001702
1703 /* This is atomic written page, keep Private */
1704 if (IS_ATOMIC_WRITTEN_PAGE(page))
1705 return;
1706
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001707 ClearPagePrivate(page);
1708}
1709
Chao Yu487261f2015-02-05 17:44:29 +08001710int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001711{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001712 /* If this is dirty page, keep PagePrivate */
1713 if (PageDirty(page))
1714 return 0;
1715
Chao Yudecd36b2015-08-07 18:42:09 +08001716 /* This is atomic written page, keep Private */
1717 if (IS_ATOMIC_WRITTEN_PAGE(page))
1718 return 0;
1719
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001720 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001721 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001722}
1723
1724static int f2fs_set_data_page_dirty(struct page *page)
1725{
1726 struct address_space *mapping = page->mapping;
1727 struct inode *inode = mapping->host;
1728
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001729 trace_f2fs_set_page_dirty(page, DATA);
1730
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001731 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001732
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001733 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001734 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1735 register_inmem_page(inode, page);
1736 return 1;
1737 }
1738 /*
1739 * Previously, this page has been registered, we just
1740 * return here.
1741 */
1742 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001743 }
1744
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001745 if (!PageDirty(page)) {
1746 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001747 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001748 return 1;
1749 }
1750 return 0;
1751}
1752
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001753static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1754{
Chao Yu454ae7e2014-04-22 13:34:01 +08001755 struct inode *inode = mapping->host;
1756
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001757 if (f2fs_has_inline_data(inode))
1758 return 0;
1759
1760 /* make sure allocating whole blocks */
1761 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1762 filemap_write_and_wait(mapping);
1763
Chao Yue2b4e2b2015-08-19 19:11:19 +08001764 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001765}
1766
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001767const struct address_space_operations f2fs_dblock_aops = {
1768 .readpage = f2fs_read_data_page,
1769 .readpages = f2fs_read_data_pages,
1770 .writepage = f2fs_write_data_page,
1771 .writepages = f2fs_write_data_pages,
1772 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001773 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001774 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001775 .invalidatepage = f2fs_invalidate_page,
1776 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001777 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001778 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001779};