blob: 7c0f6bdf817d4370b74b36de9096fad73c75fbd4 [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 Kimfe76b792016-06-30 18:40:10 -070022#include <linux/mm.h>
23#include <linux/memcontrol.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070024#include <linux/cleancache.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090026
27#include "f2fs.h"
28#include "node.h"
29#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080030#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090031#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090032
Chao Yu36951b32016-11-16 10:41:20 +080033static bool __is_cp_guaranteed(struct page *page)
34{
35 struct address_space *mapping = page->mapping;
36 struct inode *inode;
37 struct f2fs_sb_info *sbi;
38
39 if (!mapping)
40 return false;
41
42 inode = mapping->host;
43 sbi = F2FS_I_SB(inode);
44
45 if (inode->i_ino == F2FS_META_INO(sbi) ||
46 inode->i_ino == F2FS_NODE_INO(sbi) ||
47 S_ISDIR(inode->i_mode) ||
48 is_cold_data(page))
49 return true;
50 return false;
51}
52
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020053static void f2fs_read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090054{
Linus Torvaldsf5688492014-01-30 11:19:05 -080055 struct bio_vec *bvec;
56 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090057
Chao Yu8b038c72016-09-18 23:30:07 +080058#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu55523512017-02-25 11:08:28 +080059 if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_IO)) {
60 f2fs_show_injection_info(FAULT_IO);
Chao Yu8b038c72016-09-18 23:30:07 +080061 bio->bi_error = -EIO;
Chao Yu55523512017-02-25 11:08:28 +080062 }
Chao Yu8b038c72016-09-18 23:30:07 +080063#endif
64
Chao Yu12377022015-05-25 18:03:38 +080065 if (f2fs_bio_encrypted(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020066 if (bio->bi_error) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070067 fscrypt_release_ctx(bio->bi_private);
Chao Yu12377022015-05-25 18:03:38 +080068 } else {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070069 fscrypt_decrypt_bio_pages(bio->bi_private, bio);
Chao Yu12377022015-05-25 18:03:38 +080070 return;
71 }
72 }
73
Linus Torvaldsf5688492014-01-30 11:19:05 -080074 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090075 struct page *page = bvec->bv_page;
76
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020077 if (!bio->bi_error) {
Jaegeuk Kim237c0792016-06-30 18:49:15 -070078 if (!PageUptodate(page))
79 SetPageUptodate(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080080 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090081 ClearPageUptodate(page);
82 SetPageError(page);
83 }
84 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080085 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090086 bio_put(bio);
87}
88
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020089static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090090{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090091 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080092 struct bio_vec *bvec;
93 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090094
Linus Torvaldsf5688492014-01-30 11:19:05 -080095 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090096 struct page *page = bvec->bv_page;
Chao Yu36951b32016-11-16 10:41:20 +080097 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090098
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -080099 if (IS_DUMMY_WRITTEN_PAGE(page)) {
100 set_page_private(page, (unsigned long)NULL);
101 ClearPagePrivate(page);
102 unlock_page(page);
103 mempool_free(page, sbi->write_io_dummy);
104
105 if (unlikely(bio->bi_error))
106 f2fs_stop_checkpoint(sbi, true);
107 continue;
108 }
109
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700110 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700111
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200112 if (unlikely(bio->bi_error)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700113 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700114 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900115 }
Chao Yu36951b32016-11-16 10:41:20 +0800116 dec_page_count(sbi, type);
117 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900118 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800119 }
Chao Yu36951b32016-11-16 10:41:20 +0800120 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700121 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900122 wake_up(&sbi->cp_wait);
123
124 bio_put(bio);
125}
126
Gu Zheng940a6d32013-12-20 17:39:59 +0800127/*
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700128 * Return true, if pre_bio's bdev is same as its target device.
129 */
130struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
131 block_t blk_addr, struct bio *bio)
132{
133 struct block_device *bdev = sbi->sb->s_bdev;
134 int i;
135
136 for (i = 0; i < sbi->s_ndevs; i++) {
137 if (FDEV(i).start_blk <= blk_addr &&
138 FDEV(i).end_blk >= blk_addr) {
139 blk_addr -= FDEV(i).start_blk;
140 bdev = FDEV(i).bdev;
141 break;
142 }
143 }
144 if (bio) {
145 bio->bi_bdev = bdev;
146 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
147 }
148 return bdev;
149}
150
151int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
152{
153 int i;
154
155 for (i = 0; i < sbi->s_ndevs; i++)
156 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
157 return i;
158 return 0;
159}
160
161static bool __same_bdev(struct f2fs_sb_info *sbi,
162 block_t blk_addr, struct bio *bio)
163{
164 return f2fs_target_device(sbi, blk_addr, NULL) == bio->bi_bdev;
165}
166
167/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800168 * Low-level block read/write IO operations.
169 */
170static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
171 int npages, bool is_read)
172{
173 struct bio *bio;
174
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700175 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +0800176
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700177 f2fs_target_device(sbi, blk_addr, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800178 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +0800179 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800180
181 return bio;
182}
183
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700184static inline void __submit_bio(struct f2fs_sb_info *sbi,
185 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700186{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700187 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800188 unsigned int start;
189
Damien Le Moal0bfd7a02016-10-28 17:45:00 +0900190 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700191 current->plug && (type == DATA || type == NODE))
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700192 blk_finish_plug(current->plug);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800193
194 if (type != DATA && type != NODE)
195 goto submit_io;
196
197 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
198 start %= F2FS_IO_SIZE(sbi);
199
200 if (start == 0)
201 goto submit_io;
202
203 /* fill dummy pages */
204 for (; start < F2FS_IO_SIZE(sbi); start++) {
205 struct page *page =
206 mempool_alloc(sbi->write_io_dummy,
207 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
208 f2fs_bug_on(sbi, !page);
209
210 SetPagePrivate(page);
211 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
212 lock_page(page);
213 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
214 f2fs_bug_on(sbi, 1);
215 }
216 /*
217 * In the NODE case, we lose next block address chain. So, we
218 * need to do checkpoint in f2fs_sync_file.
219 */
220 if (type == NODE)
221 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700222 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800223submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800224 if (is_read_io(bio_op(bio)))
225 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
226 else
227 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500228 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700229}
230
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900231static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900232{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900233 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900234
235 if (!io->bio)
236 return;
237
Mike Christie04d328d2016-06-05 14:31:55 -0500238 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
239
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800240 if (is_read_io(fio->op))
241 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
242 else
243 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
244
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700245 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900246 io->bio = NULL;
247}
248
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800249static bool __has_merged_page(struct f2fs_bio_info *io,
250 struct inode *inode, nid_t ino, pgoff_t idx)
Chao Yu0fd785e2016-01-18 18:24:59 +0800251{
Chao Yu0fd785e2016-01-18 18:24:59 +0800252 struct bio_vec *bvec;
253 struct page *target;
254 int i;
255
Chao Yu0c3a5792016-01-18 18:28:11 +0800256 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800257 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800258
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800259 if (!inode && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800260 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800261
262 bio_for_each_segment_all(bvec, io->bio, i) {
263
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700264 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800265 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700266 else
267 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800268
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800269 if (idx != target->index)
270 continue;
271
Chao Yu0c3a5792016-01-18 18:28:11 +0800272 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800273 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800274 if (ino && ino == ino_of_node(target))
275 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800276 }
277
Chao Yu0fd785e2016-01-18 18:24:59 +0800278 return false;
279}
280
Chao Yu0c3a5792016-01-18 18:28:11 +0800281static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800282 nid_t ino, pgoff_t idx, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800283{
284 enum page_type btype = PAGE_TYPE_OF_BIO(type);
285 struct f2fs_bio_info *io = &sbi->write_io[btype];
286 bool ret;
287
288 down_read(&io->io_rwsem);
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800289 ret = __has_merged_page(io, inode, ino, idx);
Chao Yu0c3a5792016-01-18 18:28:11 +0800290 up_read(&io->io_rwsem);
291 return ret;
292}
293
294static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800295 struct inode *inode, nid_t ino, pgoff_t idx,
296 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900297{
298 enum page_type btype = PAGE_TYPE_OF_BIO(type);
299 struct f2fs_bio_info *io;
300
301 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
302
Chao Yudf0f8dc2014-03-22 14:57:23 +0800303 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900304
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800305 if (!__has_merged_page(io, inode, ino, idx))
Chao Yu0c3a5792016-01-18 18:28:11 +0800306 goto out;
307
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900308 /* change META to META_FLUSH in the checkpoint procedure */
309 if (type >= META_FLUSH) {
310 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500311 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200312 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600313 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800314 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900315 }
316 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800317out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800318 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900319}
320
Chao Yu0c3a5792016-01-18 18:28:11 +0800321void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
322 int rw)
323{
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800324 __f2fs_submit_merged_bio(sbi, NULL, 0, 0, type, rw);
Chao Yu0c3a5792016-01-18 18:28:11 +0800325}
326
327void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800328 struct inode *inode, nid_t ino, pgoff_t idx,
329 enum page_type type, int rw)
Chao Yu0c3a5792016-01-18 18:28:11 +0800330{
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800331 if (has_merged_page(sbi, inode, ino, idx, type))
332 __f2fs_submit_merged_bio(sbi, inode, ino, idx, type, rw);
Chao Yu0c3a5792016-01-18 18:28:11 +0800333}
334
Chao Yu406657d2016-02-24 17:17:55 +0800335void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
336{
337 f2fs_submit_merged_bio(sbi, DATA, WRITE);
338 f2fs_submit_merged_bio(sbi, NODE, WRITE);
339 f2fs_submit_merged_bio(sbi, META, WRITE);
340}
341
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900342/*
343 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300344 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900345 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700346int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900347{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900348 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700349 struct page *page = fio->encrypted_page ?
350 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900351
Chao Yu2ace38e2014-12-24 16:08:14 +0800352 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700353 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900354
355 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500356 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900357
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300358 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900359 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900360 return -EFAULT;
361 }
Mike Christie04d328d2016-06-05 14:31:55 -0500362 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900363
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700364 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -0700365
366 if (!is_read_io(fio->op))
367 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900368 return 0;
369}
370
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800371int f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900372{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700373 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900374 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900375 struct f2fs_bio_info *io;
Mike Christie04d328d2016-06-05 14:31:55 -0500376 bool is_read = is_read_io(fio->op);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700377 struct page *bio_page;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800378 int err = 0;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900379
Gu Zheng940a6d32013-12-20 17:39:59 +0800380 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900381
Chao Yu7a9d7542016-02-22 18:36:38 +0800382 if (fio->old_blkaddr != NEW_ADDR)
383 verify_block_addr(sbi, fio->old_blkaddr);
384 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900385
Chao Yu36951b32016-11-16 10:41:20 +0800386 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
387
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800388 /* set submitted = 1 as a return value */
389 fio->submitted = 1;
390
Chao Yu36951b32016-11-16 10:41:20 +0800391 if (!is_read)
392 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
393
Chao Yudf0f8dc2014-03-22 14:57:23 +0800394 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900395
Chao Yu7a9d7542016-02-22 18:36:38 +0800396 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700397 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
398 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900399 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900400alloc_new:
401 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800402 if ((fio->type == DATA || fio->type == NODE) &&
403 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
404 err = -EAGAIN;
Chao Yu540faed2017-02-27 17:10:45 +0800405 if (!is_read)
406 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800407 goto out_fail;
408 }
Chao Yu7a9d7542016-02-22 18:36:38 +0800409 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
Jaegeuk Kim664ba972016-10-18 11:07:45 -0700410 BIO_MAX_PAGES, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900411 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900412 }
413
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300414 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
415 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900416 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900417 goto alloc_new;
418 }
419
Chao Yu7a9d7542016-02-22 18:36:38 +0800420 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700421 f2fs_trace_ios(fio, 0);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800422out_fail:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800423 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700424 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800425 return err;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900426}
427
Chao Yu46008c62016-05-09 19:56:30 +0800428static void __set_data_blkaddr(struct dnode_of_data *dn)
429{
430 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
431 __le32 *addr_array;
432
433 /* Get physical address of data block */
434 addr_array = blkaddr_in_node(rn);
435 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
436}
437
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900438/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900439 * Lock ordering for the change of data block address:
440 * ->data_page
441 * ->node_page
442 * update block addresses in the node page
443 */
Chao Yu216a6202015-03-19 19:23:32 +0800444void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900445{
Chao Yu46008c62016-05-09 19:56:30 +0800446 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
447 __set_data_blkaddr(dn);
448 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800449 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900450}
451
Chao Yuf28b3432016-02-24 17:16:47 +0800452void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
453{
454 dn->data_blkaddr = blkaddr;
455 set_data_blkaddr(dn);
456 f2fs_update_extent_cache(dn);
457}
458
Chao Yu46008c62016-05-09 19:56:30 +0800459/* dn->ofs_in_node will be returned with up-to-date last block pointer */
460int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900461{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700462 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900463
Chao Yu46008c62016-05-09 19:56:30 +0800464 if (!count)
465 return 0;
466
Jaegeuk Kim91942322016-05-20 10:13:22 -0700467 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900468 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800469 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900470 return -ENOSPC;
471
Chao Yu46008c62016-05-09 19:56:30 +0800472 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
473 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900474
Chao Yu46008c62016-05-09 19:56:30 +0800475 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
476
477 for (; count > 0; dn->ofs_in_node++) {
478 block_t blkaddr =
479 datablock_addr(dn->node_page, dn->ofs_in_node);
480 if (blkaddr == NULL_ADDR) {
481 dn->data_blkaddr = NEW_ADDR;
482 __set_data_blkaddr(dn);
483 count--;
484 }
485 }
486
487 if (set_page_dirty(dn->node_page))
488 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900489 return 0;
490}
491
Chao Yu46008c62016-05-09 19:56:30 +0800492/* Should keep dn->ofs_in_node unchanged */
493int reserve_new_block(struct dnode_of_data *dn)
494{
495 unsigned int ofs_in_node = dn->ofs_in_node;
496 int ret;
497
498 ret = reserve_new_blocks(dn, 1);
499 dn->ofs_in_node = ofs_in_node;
500 return ret;
501}
502
Huajun Lib6009652013-11-10 23:13:18 +0800503int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
504{
505 bool need_put = dn->inode_page ? false : true;
506 int err;
507
508 err = get_dnode_of_data(dn, index, ALLOC_NODE);
509 if (err)
510 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900511
Huajun Lib6009652013-11-10 23:13:18 +0800512 if (dn->data_blkaddr == NULL_ADDR)
513 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900514 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800515 f2fs_put_dnode(dn);
516 return err;
517}
518
Fan Li759af1c2015-08-05 15:52:16 +0800519int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900520{
Hou Pengyange15882b2017-02-23 09:18:05 +0000521 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800522 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800523
Fan Li759af1c2015-08-05 15:52:16 +0800524 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
525 dn->data_blkaddr = ei.blk + index - ei.fofs;
526 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800527 }
528
Fan Li759af1c2015-08-05 15:52:16 +0800529 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900530}
531
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700532struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500533 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900534{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900535 struct address_space *mapping = inode->i_mapping;
536 struct dnode_of_data dn;
537 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +0000538 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900539 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800540 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700541 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800542 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500543 .op = REQ_OP_READ,
544 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700545 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800546 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900547
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700548 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
549 return read_mapping_page(mapping, index, NULL);
550
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700551 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900552 if (!page)
553 return ERR_PTR(-ENOMEM);
554
Chao Yucb3bc9e2015-02-05 18:03:40 +0800555 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
556 dn.data_blkaddr = ei.blk + index - ei.fofs;
557 goto got_it;
558 }
559
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900560 set_new_dnode(&dn, inode, NULL, NULL, 0);
561 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700562 if (err)
563 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900564 f2fs_put_dnode(&dn);
565
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900566 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700567 err = -ENOENT;
568 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900569 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800570got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700571 if (PageUptodate(page)) {
572 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900573 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700574 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900575
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900576 /*
577 * A new dentry page is allocated but not able to be written, since its
578 * new inode page couldn't be allocated due to -ENOSPC.
579 * In such the case, its blkaddr can be remained as NEW_ADDR.
580 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
581 */
582 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300583 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700584 if (!PageUptodate(page))
585 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700586 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900587 return page;
588 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900589
Chao Yu7a9d7542016-02-22 18:36:38 +0800590 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700591 fio.page = page;
592 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900593 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700594 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700595 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700596
597put_err:
598 f2fs_put_page(page, 1);
599 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700600}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900601
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700602struct page *find_data_page(struct inode *inode, pgoff_t index)
603{
604 struct address_space *mapping = inode->i_mapping;
605 struct page *page;
606
607 page = find_get_page(mapping, index);
608 if (page && PageUptodate(page))
609 return page;
610 f2fs_put_page(page, 0);
611
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600612 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700613 if (IS_ERR(page))
614 return page;
615
616 if (PageUptodate(page))
617 return page;
618
619 wait_on_page_locked(page);
620 if (unlikely(!PageUptodate(page))) {
621 f2fs_put_page(page, 0);
622 return ERR_PTR(-EIO);
623 }
624 return page;
625}
626
627/*
628 * If it tries to access a hole, return an error.
629 * Because, the callers, functions in dir.c and GC, should be able to know
630 * whether this page exists or not.
631 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700632struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
633 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700634{
635 struct address_space *mapping = inode->i_mapping;
636 struct page *page;
637repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600638 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700639 if (IS_ERR(page))
640 return page;
641
642 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900643 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900644 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900645 f2fs_put_page(page, 1);
646 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900647 }
Chao Yu1563ac72016-07-03 22:05:12 +0800648 if (unlikely(!PageUptodate(page))) {
649 f2fs_put_page(page, 1);
650 return ERR_PTR(-EIO);
651 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900652 return page;
653}
654
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900655/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900656 * Caller ensures that this data page is never allocated.
657 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900658 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800659 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
660 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800661 * Note that, ipage is set only by make_empty_dir, and if any error occur,
662 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900663 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900664struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900665 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900666{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900667 struct address_space *mapping = inode->i_mapping;
668 struct page *page;
669 struct dnode_of_data dn;
670 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800671
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700672 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800673 if (!page) {
674 /*
675 * before exiting, we should make sure ipage will be released
676 * if any error occur.
677 */
678 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700679 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800680 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900681
Jaegeuk Kima8865372013-12-27 17:04:17 +0900682 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800683 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700684 if (err) {
685 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900686 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900687 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700688 if (!ipage)
689 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900690
691 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700692 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900693
694 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300695 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700696 if (!PageUptodate(page))
697 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900698 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700699 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900700
Jaegeuk Kim76121182016-01-01 22:03:47 -0800701 /* if ipage exists, blkaddr should be NEW_ADDR */
702 f2fs_bug_on(F2FS_I_SB(inode), ipage);
703 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700704 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800705 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900706 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700707got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800708 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700709 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700710 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900711 return page;
712}
713
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900714static int __allocate_data_block(struct dnode_of_data *dn)
715{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700716 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900717 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900718 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700719 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800720 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900721
Jaegeuk Kim91942322016-05-20 10:13:22 -0700722 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900723 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800724
725 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
726 if (dn->data_blkaddr == NEW_ADDR)
727 goto alloc;
728
Chao Yu46008c62016-05-09 19:56:30 +0800729 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900730 return -ENOSPC;
731
Chao Yudf6136e2015-03-23 10:33:37 +0800732alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900733 get_node_info(sbi, dn->nid, &ni);
734 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
735
Chao Yudf6136e2015-03-23 10:33:37 +0800736 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -0800737 &sum, CURSEG_WARM_DATA);
Chao Yu216a6202015-03-19 19:23:32 +0800738 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900739
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700740 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800741 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700742 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300743 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700744 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300745 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900746 return 0;
747}
748
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800749static inline bool __force_buffered_io(struct inode *inode, int rw)
750{
751 return ((f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) ||
752 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
753 F2FS_I_SB(inode)->s_ndevs);
754}
755
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800756int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800757{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800758 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800759 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800760 int err = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800761
Jaegeuk Kimdc91de72017-01-13 13:12:29 -0800762 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
763 return 0;
764
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700765 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800766 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
767 if (map.m_len > map.m_lblk)
768 map.m_len -= map.m_lblk;
769 else
770 map.m_len = 0;
771
Chao Yuda859852016-01-26 15:42:58 +0800772 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800773
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800774 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800775 err = f2fs_convert_inline_inode(inode);
776 if (err)
777 return err;
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800778 return f2fs_map_blocks(inode, &map, 1,
779 __force_buffered_io(inode, WRITE) ?
780 F2FS_GET_BLOCK_PRE_AIO :
781 F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800782 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800783 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800784 err = f2fs_convert_inline_inode(inode);
785 if (err)
786 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800787 }
788 if (!f2fs_has_inline_data(inode))
789 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800790 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800791}
792
Yunlei He59c90812017-03-13 20:22:18 +0800793static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
794{
795 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
796 if (lock)
797 down_read(&sbi->node_change);
798 else
799 up_read(&sbi->node_change);
800 } else {
801 if (lock)
802 f2fs_lock_op(sbi);
803 else
804 f2fs_unlock_op(sbi);
805 }
806}
807
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900808/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700809 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
810 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800811 * If original data blocks are allocated, then give them to blockdev.
812 * Otherwise,
813 * a. preallocate requested block addresses
814 * b. do not use extent cache for better performance
815 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900816 */
Chao Yud323d002015-10-27 09:53:45 +0800817int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800818 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900819{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700820 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900821 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800822 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700823 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800824 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900825 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800826 unsigned int ofs_in_node, last_ofs_in_node;
827 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +0000828 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +0800829 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900830
Chao Yudfd02e42016-08-20 15:12:01 +0800831 if (!maxblocks)
832 return 0;
833
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700834 map->m_len = 0;
835 map->m_flags = 0;
836
837 /* it only supports block size == page size */
838 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800839 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900840
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800841 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700842 map->m_pblk = ei.blk + pgofs - ei.fofs;
843 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
844 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900845 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800846 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900847
Chao Yu4fe71e82016-01-26 15:37:38 +0800848next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800849 if (create)
Yunlei He59c90812017-03-13 20:22:18 +0800850 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900851
852 /* When reading holes, we need its node page */
853 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900854 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900855 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800856 if (flag == F2FS_GET_BLOCK_BMAP)
857 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800858 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900859 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800860 if (map->m_next_pgofs)
861 *map->m_next_pgofs =
862 get_next_page_offset(&dn, pgofs);
863 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900864 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900865 }
Chao Yu973163f2015-09-18 16:51:51 +0800866
Chao Yu46008c62016-05-09 19:56:30 +0800867 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +0100868 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800869 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900870
Chao Yu4fe71e82016-01-26 15:37:38 +0800871next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800872 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800873
Fan Li7df3a432015-12-17 13:20:59 +0800874 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
875 if (create) {
876 if (unlikely(f2fs_cp_error(sbi))) {
877 err = -EIO;
878 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800879 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800880 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800881 if (blkaddr == NULL_ADDR) {
882 prealloc++;
883 last_ofs_in_node = dn.ofs_in_node;
884 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800885 } else {
886 err = __allocate_data_block(&dn);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800887 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700888 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800889 }
Fan Li7df3a432015-12-17 13:20:59 +0800890 if (err)
891 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +0800892 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800893 blkaddr = dn.data_blkaddr;
894 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800895 if (flag == F2FS_GET_BLOCK_BMAP) {
896 map->m_pblk = 0;
897 goto sync_out;
898 }
Chao Yuda859852016-01-26 15:42:58 +0800899 if (flag == F2FS_GET_BLOCK_FIEMAP &&
900 blkaddr == NULL_ADDR) {
901 if (map->m_next_pgofs)
902 *map->m_next_pgofs = pgofs + 1;
903 }
Fan Li7df3a432015-12-17 13:20:59 +0800904 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800905 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800906 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900907 }
908 }
Fan Li7df3a432015-12-17 13:20:59 +0800909
Chao Yu46008c62016-05-09 19:56:30 +0800910 if (flag == F2FS_GET_BLOCK_PRE_AIO)
911 goto skip;
912
Chao Yu4fe71e82016-01-26 15:37:38 +0800913 if (map->m_len == 0) {
914 /* preallocated unwritten block should be mapped for fiemap. */
915 if (blkaddr == NEW_ADDR)
916 map->m_flags |= F2FS_MAP_UNWRITTEN;
917 map->m_flags |= F2FS_MAP_MAPPED;
918
919 map->m_pblk = blkaddr;
920 map->m_len = 1;
921 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800922 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800923 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800924 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800925 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800926 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800927 } else {
928 goto sync_out;
929 }
930
Chao Yu46008c62016-05-09 19:56:30 +0800931skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800932 dn.ofs_in_node++;
933 pgofs++;
934
Chao Yu46008c62016-05-09 19:56:30 +0800935 /* preallocate blocks in batch for one dnode page */
936 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
937 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800938
Chao Yu46008c62016-05-09 19:56:30 +0800939 dn.ofs_in_node = ofs_in_node;
940 err = reserve_new_blocks(&dn, prealloc);
941 if (err)
942 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800943
Chao Yu46008c62016-05-09 19:56:30 +0800944 map->m_len += dn.ofs_in_node - ofs_in_node;
945 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
946 err = -ENOSPC;
947 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800948 }
Chao Yu46008c62016-05-09 19:56:30 +0800949 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800950 }
951
Chao Yu46008c62016-05-09 19:56:30 +0800952 if (pgofs >= end)
953 goto sync_out;
954 else if (dn.ofs_in_node < end_offset)
955 goto next_block;
956
Chao Yu46008c62016-05-09 19:56:30 +0800957 f2fs_put_dnode(&dn);
958
959 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +0800960 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800961 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +0800962 }
Chao Yu46008c62016-05-09 19:56:30 +0800963 goto next_dnode;
964
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900965sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900966 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900967unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800968 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +0800969 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800970 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800971 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900972out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700973 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900974 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900975}
976
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700977static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800978 struct buffer_head *bh, int create, int flag,
979 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700980{
981 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800982 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700983
984 map.m_lblk = iblock;
985 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800986 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700987
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800988 err = f2fs_map_blocks(inode, &map, create, flag);
989 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700990 map_bh(bh, inode->i_sb, map.m_pblk);
991 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +0800992 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700993 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800994 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700995}
996
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900997static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800998 struct buffer_head *bh_result, int create, int flag,
999 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001000{
Chao Yuda859852016-01-26 15:42:58 +08001001 return __get_data_block(inode, iblock, bh_result, create,
1002 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001003}
1004
Chao Yue2b4e2b2015-08-19 19:11:19 +08001005static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001006 struct buffer_head *bh_result, int create)
1007{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001008 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +08001009 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +08001010}
1011
1012static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1013 struct buffer_head *bh_result, int create)
1014{
Yunlei He179448b2015-12-28 21:48:32 +08001015 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001016 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001017 return -EFBIG;
1018
Chao Yue2b4e2b2015-08-19 19:11:19 +08001019 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +08001020 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001021}
1022
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001023static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1024{
1025 return (offset >> inode->i_blkbits);
1026}
1027
1028static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1029{
1030 return (blk << inode->i_blkbits);
1031}
1032
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001033int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1034 u64 start, u64 len)
1035{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001036 struct buffer_head map_bh;
1037 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001038 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001039 u64 logical = 0, phys = 0, size = 0;
1040 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001041 int ret = 0;
1042
1043 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1044 if (ret)
1045 return ret;
1046
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001047 if (f2fs_has_inline_data(inode)) {
1048 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1049 if (ret != -EAGAIN)
1050 return ret;
1051 }
1052
Al Viro59551022016-01-22 15:40:57 -05001053 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +08001054
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001055 if (logical_to_blk(inode, len) == 0)
1056 len = blk_to_logical(inode, 1);
1057
1058 start_blk = logical_to_blk(inode, start);
1059 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001060
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001061next:
1062 memset(&map_bh, 0, sizeof(struct buffer_head));
1063 map_bh.b_size = len;
1064
Chao Yue2b4e2b2015-08-19 19:11:19 +08001065 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001066 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001067 if (ret)
1068 goto out;
1069
1070 /* HOLE */
1071 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001072 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001073
1074 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1075 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001076 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001077
Fan Li9a950d52015-12-26 18:07:41 +08001078 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001079 }
Fan Li9a950d52015-12-26 18:07:41 +08001080
Chao Yuda5af122016-01-08 20:19:27 +08001081 if (size) {
1082 if (f2fs_encrypted_inode(inode))
1083 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1084
Fan Li9a950d52015-12-26 18:07:41 +08001085 ret = fiemap_fill_next_extent(fieinfo, logical,
1086 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001087 }
Fan Li9a950d52015-12-26 18:07:41 +08001088
1089 if (start_blk > last_blk || ret)
1090 goto out;
1091
1092 logical = blk_to_logical(inode, start_blk);
1093 phys = blk_to_logical(inode, map_bh.b_blocknr);
1094 size = map_bh.b_size;
1095 flags = 0;
1096 if (buffer_unwritten(&map_bh))
1097 flags = FIEMAP_EXTENT_UNWRITTEN;
1098
1099 start_blk += logical_to_blk(inode, size);
1100
1101prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001102 cond_resched();
1103 if (fatal_signal_pending(current))
1104 ret = -EINTR;
1105 else
1106 goto next;
1107out:
1108 if (ret == 1)
1109 ret = 0;
1110
Al Viro59551022016-01-22 15:40:57 -05001111 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001112 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001113}
1114
Wei Yongjun6a7a3ae2016-08-23 15:23:59 +00001115static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
1116 unsigned nr_pages)
Chao Yu78682f72016-07-03 22:05:11 +08001117{
1118 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1119 struct fscrypt_ctx *ctx = NULL;
Chao Yu78682f72016-07-03 22:05:11 +08001120 struct bio *bio;
1121
1122 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1123 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
1124 if (IS_ERR(ctx))
1125 return ERR_CAST(ctx);
1126
1127 /* wait the page to be moved by cleaning */
1128 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
1129 }
1130
1131 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
1132 if (!bio) {
1133 if (ctx)
1134 fscrypt_release_ctx(ctx);
1135 return ERR_PTR(-ENOMEM);
1136 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001137 f2fs_target_device(sbi, blkaddr, bio);
Chao Yu78682f72016-07-03 22:05:11 +08001138 bio->bi_end_io = f2fs_read_end_io;
1139 bio->bi_private = ctx;
1140
1141 return bio;
1142}
1143
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001144/*
1145 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1146 * Major change was from block_size == page_size in f2fs by default.
1147 */
1148static int f2fs_mpage_readpages(struct address_space *mapping,
1149 struct list_head *pages, struct page *page,
1150 unsigned nr_pages)
1151{
1152 struct bio *bio = NULL;
1153 unsigned page_idx;
1154 sector_t last_block_in_bio = 0;
1155 struct inode *inode = mapping->host;
1156 const unsigned blkbits = inode->i_blkbits;
1157 const unsigned blocksize = 1 << blkbits;
1158 sector_t block_in_file;
1159 sector_t last_block;
1160 sector_t last_block_in_file;
1161 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001162 struct f2fs_map_blocks map;
1163
1164 map.m_pblk = 0;
1165 map.m_lblk = 0;
1166 map.m_len = 0;
1167 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001168 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001169
1170 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1171
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001172 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001173 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08001174
1175 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001176 list_del(&page->lru);
1177 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001178 page->index,
1179 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001180 goto next_page;
1181 }
1182
1183 block_in_file = (sector_t)page->index;
1184 last_block = block_in_file + nr_pages;
1185 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1186 blkbits;
1187 if (last_block > last_block_in_file)
1188 last_block = last_block_in_file;
1189
1190 /*
1191 * Map blocks using the previous result first.
1192 */
1193 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1194 block_in_file > map.m_lblk &&
1195 block_in_file < (map.m_lblk + map.m_len))
1196 goto got_it;
1197
1198 /*
1199 * Then do more f2fs_map_blocks() calls until we are
1200 * done with this page.
1201 */
1202 map.m_flags = 0;
1203
1204 if (block_in_file < last_block) {
1205 map.m_lblk = block_in_file;
1206 map.m_len = last_block - block_in_file;
1207
Chao Yu46c9e142015-09-18 16:54:16 +08001208 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001209 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001210 goto set_error_page;
1211 }
1212got_it:
1213 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1214 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1215 SetPageMappedToDisk(page);
1216
1217 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1218 SetPageUptodate(page);
1219 goto confused;
1220 }
1221 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001222 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001223 if (!PageUptodate(page))
1224 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001225 unlock_page(page);
1226 goto next_page;
1227 }
1228
1229 /*
1230 * This page will go to BIO. Do we need to send this
1231 * BIO off first?
1232 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001233 if (bio && (last_block_in_bio != block_nr - 1 ||
1234 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001235submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001236 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001237 bio = NULL;
1238 }
1239 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001240 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001241 if (IS_ERR(bio)) {
1242 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001243 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001244 }
Mike Christie04d328d2016-06-05 14:31:55 -05001245 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001246 }
1247
1248 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1249 goto submit_and_realloc;
1250
1251 last_block_in_bio = block_nr;
1252 goto next_page;
1253set_error_page:
1254 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001255 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001256 unlock_page(page);
1257 goto next_page;
1258confused:
1259 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001260 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001261 bio = NULL;
1262 }
1263 unlock_page(page);
1264next_page:
1265 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001266 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001267 }
1268 BUG_ON(pages && !list_empty(pages));
1269 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001270 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001271 return 0;
1272}
1273
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001274static int f2fs_read_data_page(struct file *file, struct page *page)
1275{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001276 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001277 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001278
Chao Yuc20e89c2014-05-06 16:53:08 +08001279 trace_f2fs_readpage(page, DATA);
1280
arter97e1c42042014-08-06 23:22:50 +09001281 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001282 if (f2fs_has_inline_data(inode))
1283 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001284 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001285 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001286 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001287}
1288
1289static int f2fs_read_data_pages(struct file *file,
1290 struct address_space *mapping,
1291 struct list_head *pages, unsigned nr_pages)
1292{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001293 struct inode *inode = file->f_mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001294 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001295
1296 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001297
1298 /* If the file has inline data, skip readpages */
1299 if (f2fs_has_inline_data(inode))
1300 return 0;
1301
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001302 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001303}
1304
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001305static int encrypt_one_page(struct f2fs_io_info *fio)
1306{
1307 struct inode *inode = fio->page->mapping->host;
1308 gfp_t gfp_flags = GFP_NOFS;
1309
1310 if (!f2fs_encrypted_inode(inode) || !S_ISREG(inode->i_mode))
1311 return 0;
1312
1313 /* wait for GCed encrypted page writeback */
1314 f2fs_wait_on_encrypted_page_writeback(fio->sbi, fio->old_blkaddr);
1315
1316retry_encrypt:
1317 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1318 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1319 if (!IS_ERR(fio->encrypted_page))
1320 return 0;
1321
1322 /* flush pending IOs and wait for a while in the ENOMEM case */
1323 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
1324 f2fs_flush_merged_bios(fio->sbi);
1325 congestion_wait(BLK_RW_ASYNC, HZ/50);
1326 gfp_flags |= __GFP_NOFAIL;
1327 goto retry_encrypt;
1328 }
1329 return PTR_ERR(fio->encrypted_page);
1330}
1331
1332static inline bool need_inplace_update(struct f2fs_io_info *fio)
1333{
1334 struct inode *inode = fio->page->mapping->host;
1335
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001336 if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode))
1337 return false;
1338 if (is_cold_data(fio->page))
1339 return false;
1340 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1341 return false;
1342
1343 return need_inplace_update_policy(inode, fio);
1344}
1345
Jaegeuk Kima8177372017-04-24 15:20:16 -07001346static inline bool valid_ipu_blkaddr(struct f2fs_io_info *fio)
1347{
1348 if (fio->old_blkaddr == NEW_ADDR)
1349 return false;
1350 if (fio->old_blkaddr == NULL_ADDR)
1351 return false;
1352 return true;
1353}
1354
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001355int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001356{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001357 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001358 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001359 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001360 struct extent_info ei = {0,0,0};
1361 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001362 int err = 0;
1363
1364 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00001365 if (need_inplace_update(fio) &&
1366 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1367 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07001368
1369 if (valid_ipu_blkaddr(fio)) {
Hou Pengyange959c8f2017-04-25 12:45:13 +00001370 ipu_force = true;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001371 fio->need_lock = false;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001372 goto got_it;
1373 }
1374 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001375
1376 if (fio->need_lock)
1377 f2fs_lock_op(fio->sbi);
1378
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001379 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001380 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001381 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001382
Chao Yu28bc1062016-02-06 14:40:34 +08001383 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001384
1385 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001386 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001387 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001388 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001389 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00001390got_it:
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001391 err = encrypt_one_page(fio);
1392 if (err)
1393 goto out_writepage;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001394
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001395 set_page_writeback(page);
1396
1397 /*
1398 * If current allocation needs SSR,
1399 * it had better in-place writes for updated data.
1400 */
Jaegeuk Kima8177372017-04-24 15:20:16 -07001401 if (ipu_force || (valid_ipu_blkaddr(fio) && need_inplace_update(fio))) {
Hou Pengyang279d6df2017-04-27 00:17:21 +08001402 f2fs_put_dnode(&dn);
1403 if (fio->need_lock)
1404 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -07001405 err = rewrite_data_page(fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001406 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001407 set_inode_flag(inode, FI_UPDATE_WRITE);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001408 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001409 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001410
1411 /* LFS mode write path */
1412 write_data_page(&dn, fio);
1413 trace_f2fs_do_write_data_page(page, OPU);
1414 set_inode_flag(inode, FI_APPEND_WRITE);
1415 if (page->index == 0)
1416 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001417out_writepage:
1418 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001419out:
1420 if (fio->need_lock)
1421 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001422 return err;
1423}
1424
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001425static int __write_data_page(struct page *page, bool *submitted,
1426 struct writeback_control *wbc)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001427{
1428 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001429 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001430 loff_t i_size = i_size_read(inode);
1431 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001432 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001433 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001434 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001435 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001436 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001437 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001438 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001439 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001440 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001441 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00001442 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001443 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001444 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001445 .submitted = false,
Hou Pengyang279d6df2017-04-27 00:17:21 +08001446 .need_lock = true,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001447 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001448
Chao Yuecda0de2014-05-06 16:48:26 +08001449 trace_f2fs_writepage(page, DATA);
1450
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001451 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001452 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001453
1454 /*
1455 * If the offset is out-of-range of file size,
1456 * this page does not have to be written to disk.
1457 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001458 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001459 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001460 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001461
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001462 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001463write:
Chao Yucaf00472015-01-28 17:48:42 +08001464 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001465 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001466 if (f2fs_is_drop_cache(inode))
1467 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001468 /* we should not write 0'th page having journal header */
1469 if (f2fs_is_volatile_file(inode) && (!page->index ||
1470 (!wbc->for_reclaim &&
1471 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001472 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001473
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001474 /* we should bypass data pages to proceed the kworkder jobs */
1475 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001476 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001477 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001478 }
1479
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001480 /* Dentry blocks are controlled by checkpoint */
1481 if (S_ISDIR(inode->i_mode)) {
Hou Pengyang279d6df2017-04-27 00:17:21 +08001482 fio.need_lock = false;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001483 err = do_write_data_page(&fio);
1484 goto done;
1485 }
1486
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001487 if (!wbc->for_reclaim)
1488 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001489 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001490 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001491 else
1492 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001493
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001494 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08001495 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001496 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08001497 if (!err)
1498 goto out;
1499 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001500
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001501 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001502 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001503 if (F2FS_I(inode)->last_disk_size < psize)
1504 F2FS_I(inode)->last_disk_size = psize;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001505
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001506done:
1507 if (err && err != -ENOENT)
1508 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001509
Jaegeuk Kim39936832012-11-22 16:21:29 +09001510out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001511 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001512 if (err)
1513 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001514
1515 if (wbc->for_reclaim) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001516 f2fs_submit_merged_bio_cond(sbi, inode, 0, page->index,
1517 DATA, WRITE);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001518 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yuc227f912015-12-16 13:09:20 +08001519 remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001520 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001521 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001522
1523 unlock_page(page);
Jaegeuk Kima7881892017-04-20 13:51:57 -07001524 if (!S_ISDIR(inode->i_mode))
1525 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001526
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001527 if (unlikely(f2fs_cp_error(sbi))) {
Chao Yu0c3a5792016-01-18 18:28:11 +08001528 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001529 submitted = NULL;
1530 }
1531
1532 if (submitted)
1533 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001534
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001535 return 0;
1536
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001537redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001538 redirty_page_for_writepage(wbc, page);
Chao Yu0002b612016-11-28 19:13:43 -08001539 if (!err)
1540 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001541 unlock_page(page);
1542 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001543}
1544
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001545static int f2fs_write_data_page(struct page *page,
1546 struct writeback_control *wbc)
1547{
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001548 return __write_data_page(page, NULL, wbc);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001549}
1550
Chao Yu8f46dca2015-07-14 18:56:10 +08001551/*
1552 * This function was copied from write_cche_pages from mm/page-writeback.c.
1553 * The major change is making write step of cold data page separately from
1554 * warm/hot data page.
1555 */
1556static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001557 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001558{
1559 int ret = 0;
1560 int done = 0;
1561 struct pagevec pvec;
1562 int nr_pages;
1563 pgoff_t uninitialized_var(writeback_index);
1564 pgoff_t index;
1565 pgoff_t end; /* Inclusive */
1566 pgoff_t done_index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001567 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001568 int cycled;
1569 int range_whole = 0;
1570 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001571
1572 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001573
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001574 if (get_dirty_pages(mapping->host) <=
1575 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1576 set_inode_flag(mapping->host, FI_HOT_DATA);
1577 else
1578 clear_inode_flag(mapping->host, FI_HOT_DATA);
1579
Chao Yu8f46dca2015-07-14 18:56:10 +08001580 if (wbc->range_cyclic) {
1581 writeback_index = mapping->writeback_index; /* prev offset */
1582 index = writeback_index;
1583 if (index == 0)
1584 cycled = 1;
1585 else
1586 cycled = 0;
1587 end = -1;
1588 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001589 index = wbc->range_start >> PAGE_SHIFT;
1590 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001591 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1592 range_whole = 1;
1593 cycled = 1; /* ignore range_cyclic tests */
1594 }
1595 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1596 tag = PAGECACHE_TAG_TOWRITE;
1597 else
1598 tag = PAGECACHE_TAG_DIRTY;
1599retry:
1600 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1601 tag_pages_for_writeback(mapping, index, end);
1602 done_index = index;
1603 while (!done && (index <= end)) {
1604 int i;
1605
1606 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1607 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1608 if (nr_pages == 0)
1609 break;
1610
1611 for (i = 0; i < nr_pages; i++) {
1612 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001613 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001614
1615 if (page->index > end) {
1616 done = 1;
1617 break;
1618 }
1619
1620 done_index = page->index;
1621
1622 lock_page(page);
1623
1624 if (unlikely(page->mapping != mapping)) {
1625continue_unlock:
1626 unlock_page(page);
1627 continue;
1628 }
1629
1630 if (!PageDirty(page)) {
1631 /* someone wrote it for us */
1632 goto continue_unlock;
1633 }
1634
Chao Yu8f46dca2015-07-14 18:56:10 +08001635 if (PageWriteback(page)) {
1636 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001637 f2fs_wait_on_page_writeback(page,
1638 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001639 else
1640 goto continue_unlock;
1641 }
1642
1643 BUG_ON(PageWriteback(page));
1644 if (!clear_page_dirty_for_io(page))
1645 goto continue_unlock;
1646
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001647 ret = __write_data_page(page, &submitted, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001648 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08001649 /*
1650 * keep nr_to_write, since vfs uses this to
1651 * get # of written pages.
1652 */
1653 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1654 unlock_page(page);
1655 ret = 0;
1656 continue;
1657 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001658 done_index = page->index + 1;
1659 done = 1;
1660 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001661 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001662 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08001663 }
1664
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001665 /* give a priority to WB_SYNC threads */
1666 if ((atomic_read(&F2FS_M_SB(mapping)->wb_sync_req) ||
1667 --wbc->nr_to_write <= 0) &&
1668 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08001669 done = 1;
1670 break;
1671 }
1672 }
1673 pagevec_release(&pvec);
1674 cond_resched();
1675 }
1676
Chao Yu8f46dca2015-07-14 18:56:10 +08001677 if (!cycled && !done) {
1678 cycled = 1;
1679 index = 0;
1680 end = writeback_index - 1;
1681 goto retry;
1682 }
1683 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1684 mapping->writeback_index = done_index;
1685
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001686 if (last_idx != ULONG_MAX)
Chao Yu6ca56ca2016-09-29 18:50:11 +08001687 f2fs_submit_merged_bio_cond(F2FS_M_SB(mapping), mapping->host,
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001688 0, last_idx, DATA, WRITE);
Chao Yu6ca56ca2016-09-29 18:50:11 +08001689
Chao Yu8f46dca2015-07-14 18:56:10 +08001690 return ret;
1691}
1692
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001693static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001694 struct writeback_control *wbc)
1695{
1696 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001697 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001698 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001699 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001700
P J Pcfb185a2013-04-03 11:38:00 +09001701 /* deal with chardevs and other special file */
1702 if (!mapping->a_ops->writepage)
1703 return 0;
1704
Chao Yu6a290542015-07-17 18:02:39 +08001705 /* skip writing if there is no dirty page in this inode */
1706 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1707 return 0;
1708
Jaegeuk Kima1257022015-10-08 10:40:07 -07001709 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1710 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1711 available_free_memory(sbi, DIRTY_DENTS))
1712 goto skip_write;
1713
Chao Yud323d002015-10-27 09:53:45 +08001714 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001715 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001716 goto skip_write;
1717
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001718 /* during POR, we don't need to trigger writepage at all. */
1719 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1720 goto skip_write;
1721
Yunlei Hed31c7c32016-02-04 16:14:00 +08001722 trace_f2fs_writepages(mapping->host, wbc, DATA);
1723
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001724 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
1725 if (wbc->sync_mode == WB_SYNC_ALL)
1726 atomic_inc(&sbi->wb_sync_req);
1727 else if (atomic_read(&sbi->wb_sync_req))
1728 goto skip_write;
1729
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001730 blk_start_plug(&plug);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001731 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001732 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001733
1734 if (wbc->sync_mode == WB_SYNC_ALL)
1735 atomic_dec(&sbi->wb_sync_req);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001736 /*
1737 * if some pages were truncated, we cannot guarantee its mapping->host
1738 * to detect pending bios.
1739 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001740
Chao Yuc227f912015-12-16 13:09:20 +08001741 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001742 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001743
1744skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001745 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001746 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001747 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001748}
1749
Chao Yu3aab8f82014-07-02 13:25:04 +08001750static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1751{
1752 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001753 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001754
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001755 if (to > i_size) {
1756 truncate_pagecache(inode, i_size);
1757 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001758 }
1759}
1760
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001761static int prepare_write_begin(struct f2fs_sb_info *sbi,
1762 struct page *page, loff_t pos, unsigned len,
1763 block_t *blk_addr, bool *node_changed)
1764{
1765 struct inode *inode = page->mapping->host;
1766 pgoff_t index = page->index;
1767 struct dnode_of_data dn;
1768 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001769 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00001770 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001771 int err = 0;
1772
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001773 /*
1774 * we already allocated all the blocks, so we don't need to get
1775 * the block addresses when there is no need to fill the page.
1776 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08001777 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
1778 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001779 return 0;
1780
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001781 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001782 (pos & PAGE_MASK) >= i_size_read(inode)) {
Yunlei He59c90812017-03-13 20:22:18 +08001783 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001784 locked = true;
1785 }
1786restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001787 /* check inline_data */
1788 ipage = get_node_page(sbi, inode->i_ino);
1789 if (IS_ERR(ipage)) {
1790 err = PTR_ERR(ipage);
1791 goto unlock_out;
1792 }
1793
1794 set_new_dnode(&dn, inode, ipage, ipage, 0);
1795
1796 if (f2fs_has_inline_data(inode)) {
1797 if (pos + len <= MAX_INLINE_DATA) {
1798 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001799 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001800 if (inode->i_nlink)
1801 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001802 } else {
1803 err = f2fs_convert_inline_page(&dn, page);
1804 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001805 goto out;
1806 if (dn.data_blkaddr == NULL_ADDR)
1807 err = f2fs_get_block(&dn, index);
1808 }
1809 } else if (locked) {
1810 err = f2fs_get_block(&dn, index);
1811 } else {
1812 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1813 dn.data_blkaddr = ei.blk + index - ei.fofs;
1814 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001815 /* hole case */
1816 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001817 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001818 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08001819 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
1820 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001821 locked = true;
1822 goto restart;
1823 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001824 }
1825 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001826
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001827 /* convert_inline_page can make node_changed */
1828 *blk_addr = dn.data_blkaddr;
1829 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001830out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001831 f2fs_put_dnode(&dn);
1832unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001833 if (locked)
Yunlei He59c90812017-03-13 20:22:18 +08001834 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001835 return err;
1836}
1837
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001838static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1839 loff_t pos, unsigned len, unsigned flags,
1840 struct page **pagep, void **fsdata)
1841{
1842 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001843 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001844 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001845 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001846 bool need_balance = false;
1847 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001848 int err = 0;
1849
Chao Yu62aed042014-05-06 16:46:04 +08001850 trace_f2fs_write_begin(inode, pos, len, flags);
1851
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001852 /*
1853 * We should check this at this moment to avoid deadlock on inode page
1854 * and #0 page. The locking rule for inline_data conversion should be:
1855 * lock_page(page #0) -> lock_page(inode_page)
1856 */
1857 if (index != 0) {
1858 err = f2fs_convert_inline_inode(inode);
1859 if (err)
1860 goto fail;
1861 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001862repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08001863 /*
1864 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1865 * wait_for_stable_page. Will wait that below with our IO control.
1866 */
1867 page = pagecache_get_page(mapping, index,
1868 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08001869 if (!page) {
1870 err = -ENOMEM;
1871 goto fail;
1872 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001873
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001874 *pagep = page;
1875
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001876 err = prepare_write_begin(sbi, page, pos, len,
1877 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001878 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001879 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001880
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001881 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001882 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001883 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001884 lock_page(page);
1885 if (page->mapping != mapping) {
1886 /* The page got truncated from under us */
1887 f2fs_put_page(page, 1);
1888 goto repeat;
1889 }
1890 }
1891
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001892 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001893
Chao Yu08b39fb2015-10-08 13:27:34 +08001894 /* wait for GCed encrypted page writeback */
1895 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001896 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001897
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001898 if (len == PAGE_SIZE || PageUptodate(page))
1899 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001900
Yunlei He746e2402016-12-20 11:11:35 +08001901 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
1902 zero_user_segment(page, len, PAGE_SIZE);
1903 return 0;
1904 }
1905
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001906 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001907 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001908 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001909 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001910 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001911
Chao Yu78682f72016-07-03 22:05:11 +08001912 bio = f2fs_grab_bio(inode, blkaddr, 1);
1913 if (IS_ERR(bio)) {
1914 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001915 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001916 }
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001917 bio->bi_opf = REQ_OP_READ;
Chao Yu78682f72016-07-03 22:05:11 +08001918 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1919 bio_put(bio);
1920 err = -EFAULT;
1921 goto fail;
1922 }
1923
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001924 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001925
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001926 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001927 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001928 f2fs_put_page(page, 1);
1929 goto repeat;
1930 }
Chao Yu1563ac72016-07-03 22:05:12 +08001931 if (unlikely(!PageUptodate(page))) {
1932 err = -EIO;
1933 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001934 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001935 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001936 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001937
Chao Yu3aab8f82014-07-02 13:25:04 +08001938fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001939 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001940 f2fs_write_failed(mapping, pos + len);
1941 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001942}
1943
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001944static int f2fs_write_end(struct file *file,
1945 struct address_space *mapping,
1946 loff_t pos, unsigned len, unsigned copied,
1947 struct page *page, void *fsdata)
1948{
1949 struct inode *inode = page->mapping->host;
1950
Chao Yudfb2bf32014-05-06 16:47:23 +08001951 trace_f2fs_write_end(inode, pos, len, copied);
1952
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001953 /*
1954 * This should be come from len == PAGE_SIZE, and we expect copied
1955 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
1956 * let generic_perform_write() try to copy data again through copied=0.
1957 */
1958 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08001959 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001960 copied = 0;
1961 else
1962 SetPageUptodate(page);
1963 }
1964 if (!copied)
1965 goto unlock_out;
1966
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001967 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001968
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001969 if (pos + copied > i_size_read(inode))
1970 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001971unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08001972 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001973 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001974 return copied;
1975}
1976
Omar Sandoval6f673762015-03-16 04:33:52 -07001977static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1978 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001979{
1980 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001981
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001982 if (offset & blocksize_mask)
1983 return -EINVAL;
1984
Al Viro5b46f252014-03-16 18:07:34 -04001985 if (iov_iter_alignment(iter) & blocksize_mask)
1986 return -EINVAL;
1987
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001988 return 0;
1989}
1990
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001991static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001992{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001993 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001994 struct inode *inode = mapping->host;
1995 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001996 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08001997 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08001998 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001999
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002000 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002001 if (err)
2002 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002003
Jaegeuk Kimc040ff92016-11-11 16:46:40 -08002004 if (__force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002005 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002006
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002007 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fad2014-07-31 21:11:22 +08002008
Chao Yu82e0a5a2016-07-13 09:18:29 +08002009 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002010 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002011 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
2012
2013 if (rw == WRITE) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002014 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07002015 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002016 else if (err < 0)
2017 f2fs_write_failed(mapping, offset + count);
2018 }
Chao Yu70407fad2014-07-31 21:11:22 +08002019
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002020 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fad2014-07-31 21:11:22 +08002021
Chao Yu3aab8f82014-07-02 13:25:04 +08002022 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002023}
2024
Chao Yu487261f2015-02-05 17:44:29 +08002025void f2fs_invalidate_page(struct page *page, unsigned int offset,
2026 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002027{
2028 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002029 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002030
Chao Yu487261f2015-02-05 17:44:29 +08002031 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002032 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002033 return;
2034
Chao Yu487261f2015-02-05 17:44:29 +08002035 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08002036 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002037 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08002038 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002039 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08002040 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002041 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08002042 remove_dirty_inode(inode);
2043 }
Chao Yu487261f2015-02-05 17:44:29 +08002044 }
Chao Yudecd36b2015-08-07 18:42:09 +08002045
2046 /* This is atomic written page, keep Private */
2047 if (IS_ATOMIC_WRITTEN_PAGE(page))
Jaegeuk Kim8c242db2017-03-17 09:55:52 +08002048 return drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002049
Chao Yu23dc9742016-04-29 20:09:15 +08002050 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002051 ClearPagePrivate(page);
2052}
2053
Chao Yu487261f2015-02-05 17:44:29 +08002054int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002055{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002056 /* If this is dirty page, keep PagePrivate */
2057 if (PageDirty(page))
2058 return 0;
2059
Chao Yudecd36b2015-08-07 18:42:09 +08002060 /* This is atomic written page, keep Private */
2061 if (IS_ATOMIC_WRITTEN_PAGE(page))
2062 return 0;
2063
Chao Yu23dc9742016-04-29 20:09:15 +08002064 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002065 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002066 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002067}
2068
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002069/*
2070 * This was copied from __set_page_dirty_buffers which gives higher performance
2071 * in very high speed storages. (e.g., pmem)
2072 */
2073void f2fs_set_page_dirty_nobuffers(struct page *page)
2074{
2075 struct address_space *mapping = page->mapping;
2076 unsigned long flags;
2077
2078 if (unlikely(!mapping))
2079 return;
2080
2081 spin_lock(&mapping->private_lock);
2082 lock_page_memcg(page);
2083 SetPageDirty(page);
2084 spin_unlock(&mapping->private_lock);
2085
2086 spin_lock_irqsave(&mapping->tree_lock, flags);
2087 WARN_ON_ONCE(!PageUptodate(page));
2088 account_page_dirtied(page, mapping);
2089 radix_tree_tag_set(&mapping->page_tree,
2090 page_index(page), PAGECACHE_TAG_DIRTY);
2091 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2092 unlock_page_memcg(page);
2093
2094 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
2095 return;
2096}
2097
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002098static int f2fs_set_data_page_dirty(struct page *page)
2099{
2100 struct address_space *mapping = page->mapping;
2101 struct inode *inode = mapping->host;
2102
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002103 trace_f2fs_set_page_dirty(page, DATA);
2104
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002105 if (!PageUptodate(page))
2106 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002107
Chao Yu5fe45742017-01-07 18:50:26 +08002108 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002109 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2110 register_inmem_page(inode, page);
2111 return 1;
2112 }
2113 /*
2114 * Previously, this page has been registered, we just
2115 * return here.
2116 */
2117 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002118 }
2119
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002120 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002121 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002122 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002123 return 1;
2124 }
2125 return 0;
2126}
2127
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002128static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2129{
Chao Yu454ae7e2014-04-22 13:34:01 +08002130 struct inode *inode = mapping->host;
2131
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002132 if (f2fs_has_inline_data(inode))
2133 return 0;
2134
2135 /* make sure allocating whole blocks */
2136 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2137 filemap_write_and_wait(mapping);
2138
Chao Yue2b4e2b2015-08-19 19:11:19 +08002139 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002140}
2141
Weichao Guo5b7a4872016-09-20 05:03:27 +08002142#ifdef CONFIG_MIGRATION
2143#include <linux/migrate.h>
2144
2145int f2fs_migrate_page(struct address_space *mapping,
2146 struct page *newpage, struct page *page, enum migrate_mode mode)
2147{
2148 int rc, extra_count;
2149 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2150 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2151
2152 BUG_ON(PageWriteback(page));
2153
2154 /* migrating an atomic written page is safe with the inmem_lock hold */
2155 if (atomic_written && !mutex_trylock(&fi->inmem_lock))
2156 return -EAGAIN;
2157
2158 /*
2159 * A reference is expected if PagePrivate set when move mapping,
2160 * however F2FS breaks this for maintaining dirty page counts when
2161 * truncating pages. So here adjusting the 'extra_count' make it work.
2162 */
2163 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2164 rc = migrate_page_move_mapping(mapping, newpage,
2165 page, NULL, mode, extra_count);
2166 if (rc != MIGRATEPAGE_SUCCESS) {
2167 if (atomic_written)
2168 mutex_unlock(&fi->inmem_lock);
2169 return rc;
2170 }
2171
2172 if (atomic_written) {
2173 struct inmem_pages *cur;
2174 list_for_each_entry(cur, &fi->inmem_pages, list)
2175 if (cur->page == page) {
2176 cur->page = newpage;
2177 break;
2178 }
2179 mutex_unlock(&fi->inmem_lock);
2180 put_page(page);
2181 get_page(newpage);
2182 }
2183
2184 if (PagePrivate(page))
2185 SetPagePrivate(newpage);
2186 set_page_private(newpage, page_private(page));
2187
2188 migrate_page_copy(newpage, page);
2189
2190 return MIGRATEPAGE_SUCCESS;
2191}
2192#endif
2193
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002194const struct address_space_operations f2fs_dblock_aops = {
2195 .readpage = f2fs_read_data_page,
2196 .readpages = f2fs_read_data_pages,
2197 .writepage = f2fs_write_data_page,
2198 .writepages = f2fs_write_data_pages,
2199 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002200 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002201 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002202 .invalidatepage = f2fs_invalidate_page,
2203 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002204 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002205 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002206#ifdef CONFIG_MIGRATION
2207 .migratepage = f2fs_migrate_page,
2208#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002209};