blob: b8f1c9647ec4e6352f0a77e029bc521a8a1b8706 [file] [log] [blame]
Chao Yu6d1a8322018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09002/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09003 * fs/f2fs/data.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/buffer_head.h>
11#include <linux/mpage.h>
12#include <linux/writeback.h>
13#include <linux/backing-dev.h>
Chao Yu8f46dca2015-07-14 18:56:10 +080014#include <linux/pagevec.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090015#include <linux/blkdev.h>
16#include <linux/bio.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010017#include <linux/prefetch.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080018#include <linux/uio.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070019#include <linux/cleancache.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090020
21#include "f2fs.h"
22#include "node.h"
23#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080024#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090025#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090026
Eric Biggersf69e8142018-04-18 11:09:48 -070027#define NUM_PREALLOC_POST_READ_CTXS 128
28
29static struct kmem_cache *bio_post_read_ctx_cache;
30static mempool_t *bio_post_read_ctx_pool;
31
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070032static bool __is_cp_guaranteed(struct page *page)
33{
34 struct address_space *mapping = page->mapping;
35 struct inode *inode;
36 struct f2fs_sb_info *sbi;
37
38 if (!mapping)
39 return false;
40
41 inode = mapping->host;
42 sbi = F2FS_I_SB(inode);
43
44 if (inode->i_ino == F2FS_META_INO(sbi) ||
45 inode->i_ino == F2FS_NODE_INO(sbi) ||
46 S_ISDIR(inode->i_mode) ||
Chao Yua301e762018-05-08 14:06:03 +080047 (S_ISREG(inode->i_mode) &&
Chao Yud8312722018-09-20 20:05:00 +080048 (f2fs_is_atomic_file(inode) || IS_NOQUOTA(inode))) ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070049 is_cold_data(page))
50 return true;
51 return false;
52}
53
Jaegeuk Kim8025d722018-10-16 10:20:53 -070054static enum count_type __read_io_type(struct page *page)
55{
56 struct address_space *mapping = page->mapping;
57
58 if (mapping) {
59 struct inode *inode = mapping->host;
60 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
61
62 if (inode->i_ino == F2FS_META_INO(sbi))
63 return F2FS_RD_META;
64
65 if (inode->i_ino == F2FS_NODE_INO(sbi))
66 return F2FS_RD_NODE;
67 }
68 return F2FS_RD_DATA;
69}
70
Eric Biggersf69e8142018-04-18 11:09:48 -070071/* postprocessing steps for read bios */
72enum bio_post_read_step {
73 STEP_INITIAL = 0,
74 STEP_DECRYPT,
75};
76
77struct bio_post_read_ctx {
78 struct bio *bio;
79 struct work_struct work;
80 unsigned int cur_step;
81 unsigned int enabled_steps;
82};
83
84static void __read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090085{
Eric Biggersf69e8142018-04-18 11:09:48 -070086 struct page *page;
87 struct bio_vec *bv;
Linus Torvaldsf5688492014-01-30 11:19:05 -080088 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090089
Eric Biggersf69e8142018-04-18 11:09:48 -070090 bio_for_each_segment_all(bv, bio, i) {
91 page = bv->bv_page;
92
93 /* PG_error was set if any post_read step failed */
94 if (bio->bi_error || PageError(page)) {
95 ClearPageUptodate(page);
Jaegeuk Kim4edba232018-09-25 13:54:33 -070096 /* will re-read again later */
97 ClearPageError(page);
Eric Biggersf69e8142018-04-18 11:09:48 -070098 } else {
99 SetPageUptodate(page);
100 }
Jaegeuk Kim8025d722018-10-16 10:20:53 -0700101 dec_page_count(F2FS_P_SB(page), __read_io_type(page));
Eric Biggersf69e8142018-04-18 11:09:48 -0700102 unlock_page(page);
103 }
104 if (bio->bi_private)
105 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
106 bio_put(bio);
107}
108
109static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
110
111static void decrypt_work(struct work_struct *work)
112{
113 struct bio_post_read_ctx *ctx =
114 container_of(work, struct bio_post_read_ctx, work);
115
116 fscrypt_decrypt_bio(ctx->bio);
117
118 bio_post_read_processing(ctx);
119}
120
121static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
122{
123 switch (++ctx->cur_step) {
124 case STEP_DECRYPT:
125 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
126 INIT_WORK(&ctx->work, decrypt_work);
127 fscrypt_enqueue_decrypt_work(&ctx->work);
128 return;
129 }
130 ctx->cur_step++;
131 /* fall-through */
132 default:
133 __read_end_io(ctx->bio);
134 }
135}
136
137static bool f2fs_bio_post_read_required(struct bio *bio)
138{
139 return bio->bi_private && !bio->bi_error;
140}
141
142static void f2fs_read_end_io(struct bio *bio)
143{
Chao Yu40292b92018-09-12 09:22:29 +0800144 if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_READ_IO)) {
145 f2fs_show_injection_info(FAULT_READ_IO);
Chao Yu8b038c72016-09-18 23:30:07 +0800146 bio->bi_error = -EIO;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700147 }
Chao Yu8b038c72016-09-18 23:30:07 +0800148
Eric Biggersf69e8142018-04-18 11:09:48 -0700149 if (f2fs_bio_post_read_required(bio)) {
150 struct bio_post_read_ctx *ctx = bio->bi_private;
151
152 ctx->cur_step = STEP_INITIAL;
153 bio_post_read_processing(ctx);
154 return;
Chao Yu12377022015-05-25 18:03:38 +0800155 }
156
Eric Biggersf69e8142018-04-18 11:09:48 -0700157 __read_end_io(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900158}
159
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200160static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900161{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +0900162 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -0800163 struct bio_vec *bvec;
164 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900165
Chao Yu40292b92018-09-12 09:22:29 +0800166 if (time_to_inject(sbi, FAULT_WRITE_IO)) {
167 f2fs_show_injection_info(FAULT_WRITE_IO);
168 bio->bi_error = -EIO;
169 }
170
Linus Torvaldsf5688492014-01-30 11:19:05 -0800171 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900172 struct page *page = bvec->bv_page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700173 enum count_type type = WB_DATA_TYPE(page);
174
175 if (IS_DUMMY_WRITTEN_PAGE(page)) {
176 set_page_private(page, (unsigned long)NULL);
177 ClearPagePrivate(page);
178 unlock_page(page);
179 mempool_free(page, sbi->write_io_dummy);
180
181 if (unlikely(bio->bi_error))
182 f2fs_stop_checkpoint(sbi, true);
183 continue;
184 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900185
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700186 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700187
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200188 if (unlikely(bio->bi_error)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700189 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kime2709762017-12-31 16:26:38 -0800190 if (type == F2FS_WB_CP_DATA)
191 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900192 }
Yunlei He92b8f9c2018-01-11 14:19:32 +0800193
194 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
195 page->index != nid_of_node(page));
196
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700197 dec_page_count(sbi, type);
Chao Yu3d5c54a2018-08-02 23:03:19 +0800198 if (f2fs_in_warm_node_list(sbi, page))
199 f2fs_del_fsync_node_entry(sbi, page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700200 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900201 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800202 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700203 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700204 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900205 wake_up(&sbi->cp_wait);
206
207 bio_put(bio);
208}
209
Gu Zheng940a6d32013-12-20 17:39:59 +0800210/*
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700211 * Return true, if pre_bio's bdev is same as its target device.
212 */
213struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
214 block_t blk_addr, struct bio *bio)
215{
216 struct block_device *bdev = sbi->sb->s_bdev;
217 int i;
218
219 for (i = 0; i < sbi->s_ndevs; i++) {
220 if (FDEV(i).start_blk <= blk_addr &&
221 FDEV(i).end_blk >= blk_addr) {
222 blk_addr -= FDEV(i).start_blk;
223 bdev = FDEV(i).bdev;
224 break;
225 }
226 }
227 if (bio) {
228 bio->bi_bdev = bdev;
229 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
230 }
231 return bdev;
232}
233
234int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
235{
236 int i;
237
238 for (i = 0; i < sbi->s_ndevs; i++)
239 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
240 return i;
241 return 0;
242}
243
244static bool __same_bdev(struct f2fs_sb_info *sbi,
245 block_t blk_addr, struct bio *bio)
246{
247 return f2fs_target_device(sbi, blk_addr, NULL) == bio->bi_bdev;
248}
249
250/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800251 * Low-level block read/write IO operations.
252 */
253static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800254 struct writeback_control *wbc,
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900255 int npages, bool is_read,
256 enum page_type type, enum temp_type temp)
Gu Zheng940a6d32013-12-20 17:39:59 +0800257{
258 struct bio *bio;
259
Chao Yub5db2de2017-10-28 16:52:31 +0800260 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800261
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700262 f2fs_target_device(sbi, blk_addr, bio);
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900263 if (is_read) {
264 bio->bi_end_io = f2fs_read_end_io;
265 bio->bi_private = NULL;
266 } else {
267 bio->bi_end_io = f2fs_write_end_io;
268 bio->bi_private = sbi;
Chao Yu6b4d6a82018-05-30 00:20:41 +0800269 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, type, temp);
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900270 }
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800271 if (wbc)
272 wbc_init_bio(wbc, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800273
274 return bio;
275}
276
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700277static inline void __submit_bio(struct f2fs_sb_info *sbi,
278 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700279{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700280 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700281 unsigned int start;
282
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700283 if (type != DATA && type != NODE)
284 goto submit_io;
285
Yunlong Song94e1c6a2018-07-12 23:09:28 +0800286 if (test_opt(sbi, LFS) && current->plug)
Tiezhu Yang4d8e4a82018-02-06 08:21:45 +0800287 blk_finish_plug(current->plug);
288
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700289 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
290 start %= F2FS_IO_SIZE(sbi);
291
292 if (start == 0)
293 goto submit_io;
294
295 /* fill dummy pages */
296 for (; start < F2FS_IO_SIZE(sbi); start++) {
297 struct page *page =
298 mempool_alloc(sbi->write_io_dummy,
Gao Xiang07894d22019-02-19 10:31:52 +0800299 GFP_NOIO | __GFP_NOFAIL);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700300 f2fs_bug_on(sbi, !page);
301
Gao Xiang07894d22019-02-19 10:31:52 +0800302 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700303 SetPagePrivate(page);
304 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
305 lock_page(page);
306 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
307 f2fs_bug_on(sbi, 1);
308 }
309 /*
310 * In the NODE case, we lose next block address chain. So, we
311 * need to do checkpoint in f2fs_sync_file.
312 */
313 if (type == NODE)
314 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700315 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700316submit_io:
317 if (is_read_io(bio_op(bio)))
318 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
319 else
320 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500321 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700322}
323
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900324static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900325{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900326 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900327
328 if (!io->bio)
329 return;
330
Mike Christie04d328d2016-06-05 14:31:55 -0500331 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
332
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700333 if (is_read_io(fio->op))
334 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
335 else
336 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
337
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700338 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900339 io->bio = NULL;
340}
341
Chao Yu39373df2018-09-27 23:41:16 +0800342static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
343 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800344{
Chao Yu0fd785e2016-01-18 18:24:59 +0800345 struct bio_vec *bvec;
346 struct page *target;
347 int i;
348
Chao Yu0c3a5792016-01-18 18:28:11 +0800349 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800350 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800351
Chao Yu39373df2018-09-27 23:41:16 +0800352 if (!inode && !page && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800353 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800354
355 bio_for_each_segment_all(bvec, io->bio, i) {
356
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700357 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800358 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700359 else
360 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800361
Chao Yu0c3a5792016-01-18 18:28:11 +0800362 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800363 return true;
Chao Yu39373df2018-09-27 23:41:16 +0800364 if (page && page == target)
365 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800366 if (ino && ino == ino_of_node(target))
367 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800368 }
369
Chao Yu0fd785e2016-01-18 18:24:59 +0800370 return false;
371}
372
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700373static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700374 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900375{
376 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700377 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900378
Chao Yudf0f8dc2014-03-22 14:57:23 +0800379 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900380
381 /* change META to META_FLUSH in the checkpoint procedure */
382 if (type >= META_FLUSH) {
383 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500384 io->fio.op = REQ_OP_WRITE;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700385 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
386 if (!test_opt(sbi, NOBARRIER))
387 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900388 }
389 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800390 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900391}
392
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700393static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yu39373df2018-09-27 23:41:16 +0800394 struct inode *inode, struct page *page,
395 nid_t ino, enum page_type type, bool force)
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700396{
397 enum temp_type temp;
Yunlong Songa151f362018-11-13 11:57:32 +0800398 bool ret = true;
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700399
400 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
Yunlong Songa151f362018-11-13 11:57:32 +0800401 if (!force) {
402 enum page_type btype = PAGE_TYPE_OF_BIO(type);
403 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700404
Yunlong Songa151f362018-11-13 11:57:32 +0800405 down_read(&io->io_rwsem);
406 ret = __has_merged_page(io, inode, page, ino);
407 up_read(&io->io_rwsem);
408 }
409 if (ret)
410 __f2fs_submit_merged_write(sbi, type, temp);
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700411
412 /* TODO: use HOT temp only for meta pages now. */
413 if (type >= META)
414 break;
415 }
416}
417
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700418void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800419{
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700420 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
Chao Yu0c3a5792016-01-18 18:28:11 +0800421}
422
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700423void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Chao Yu39373df2018-09-27 23:41:16 +0800424 struct inode *inode, struct page *page,
425 nid_t ino, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800426{
Chao Yu39373df2018-09-27 23:41:16 +0800427 __submit_merged_write_cond(sbi, inode, page, ino, type, false);
Chao Yu0c3a5792016-01-18 18:28:11 +0800428}
429
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700430void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800431{
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700432 f2fs_submit_merged_write(sbi, DATA);
433 f2fs_submit_merged_write(sbi, NODE);
434 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800435}
436
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900437/*
438 * Fill the locked page with data located in the block address.
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700439 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900440 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700441int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900442{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900443 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700444 struct page *page = fio->encrypted_page ?
445 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900446
Chao Yub3c869a2018-08-01 19:13:44 +0800447 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
448 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
449 return -EFAULT;
450
Chao Yu2ace38e2014-12-24 16:08:14 +0800451 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700452 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900453
454 /* Allocate a new bio */
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800455 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900456 1, is_read_io(fio->op), fio->type, fio->temp);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900457
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300458 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900459 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900460 return -EFAULT;
461 }
Chao Yub8cc7a92018-10-22 23:24:28 +0800462
463 if (fio->io_wbc && !is_read_io(fio->op))
464 wbc_account_io(fio->io_wbc, page, PAGE_SIZE);
465
Mike Christie04d328d2016-06-05 14:31:55 -0500466 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900467
Jaegeuk Kim8025d722018-10-16 10:20:53 -0700468 inc_page_count(fio->sbi, is_read_io(fio->op) ?
469 __read_io_type(page): WB_DATA_TYPE(fio->page));
Chao Yuf44ba512018-10-22 09:12:51 +0800470
471 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900472 return 0;
473}
474
Chao Yu34880e02018-05-28 23:47:18 +0800475void f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900476{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700477 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900478 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700479 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700480 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900481
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700482 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900483
Chao Yuc52dc0f2017-05-19 23:37:01 +0800484 down_write(&io->io_rwsem);
485next:
486 if (fio->in_list) {
487 spin_lock(&io->io_lock);
488 if (list_empty(&io->io_list)) {
489 spin_unlock(&io->io_lock);
Chao Yu34880e02018-05-28 23:47:18 +0800490 goto out;
Chao Yuc52dc0f2017-05-19 23:37:01 +0800491 }
492 fio = list_first_entry(&io->io_list,
493 struct f2fs_io_info, list);
494 list_del(&fio->list);
495 spin_unlock(&io->io_lock);
496 }
497
Chao Yud05e8712018-06-05 17:44:11 +0800498 if (__is_valid_data_blkaddr(fio->old_blkaddr))
Yunlei Hea3f8ec82018-03-08 16:29:13 +0800499 verify_block_addr(fio, fio->old_blkaddr);
500 verify_block_addr(fio, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900501
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700502 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
503
Thomas Meyer59504672017-10-07 16:02:21 +0200504 /* set submitted = true as a return value */
505 fio->submitted = true;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700506
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700507 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700508
Chao Yu7a9d7542016-02-22 18:36:38 +0800509 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700510 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
511 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900512 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900513alloc_new:
514 if (io->bio == NULL) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700515 if ((fio->type == DATA || fio->type == NODE) &&
516 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700517 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Chao Yu34880e02018-05-28 23:47:18 +0800518 fio->retry = true;
519 goto skip;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700520 }
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800521 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900522 BIO_MAX_PAGES, false,
523 fio->type, fio->temp);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900524 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900525 }
526
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700527 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900528 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900529 goto alloc_new;
530 }
531
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800532 if (fio->io_wbc)
533 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
534
Chao Yu7a9d7542016-02-22 18:36:38 +0800535 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700536 f2fs_trace_ios(fio, 0);
Chao Yuc52dc0f2017-05-19 23:37:01 +0800537
538 trace_f2fs_submit_page_write(fio->page, fio);
Chao Yu34880e02018-05-28 23:47:18 +0800539skip:
Chao Yuc52dc0f2017-05-19 23:37:01 +0800540 if (fio->in_list)
541 goto next;
Chao Yu34880e02018-05-28 23:47:18 +0800542out:
Daniel Rosenbergf22f93a2018-08-20 19:21:43 -0700543 if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) ||
544 f2fs_is_checkpoint_ready(sbi))
Jaegeuk Kim60208e22018-09-06 11:40:12 -0700545 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800546 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900547}
548
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700549static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -0700550 unsigned nr_pages, unsigned op_flag)
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700551{
552 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700553 struct bio *bio;
Eric Biggersf69e8142018-04-18 11:09:48 -0700554 struct bio_post_read_ctx *ctx;
555 unsigned int post_read_steps = 0;
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700556
Chao Yu10d67032018-07-10 23:01:45 +0800557 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
558 return ERR_PTR(-EFAULT);
559
Eric Biggersf69e8142018-04-18 11:09:48 -0700560 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
561 if (!bio)
562 return ERR_PTR(-ENOMEM);
563 f2fs_target_device(sbi, blkaddr, bio);
564 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -0700565 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
Eric Biggersf69e8142018-04-18 11:09:48 -0700566
567 if (f2fs_encrypted_file(inode))
568 post_read_steps |= 1 << STEP_DECRYPT;
569 if (post_read_steps) {
570 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
571 if (!ctx) {
572 bio_put(bio);
573 return ERR_PTR(-ENOMEM);
574 }
575 ctx->bio = bio;
576 ctx->enabled_steps = post_read_steps;
577 bio->bi_private = ctx;
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700578 }
579
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700580 return bio;
581}
582
583/* This can handle encryption stuffs */
584static int f2fs_submit_page_read(struct inode *inode, struct page *page,
585 block_t blkaddr)
586{
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -0700587 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0);
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700588
589 if (IS_ERR(bio))
590 return PTR_ERR(bio);
591
Jaegeuk Kim0d51cea2018-08-22 21:18:00 -0700592 /* wait for GCed page writeback via META_MAPPING */
593 f2fs_wait_on_block_writeback(inode, blkaddr);
594
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700595 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
596 bio_put(bio);
597 return -EFAULT;
598 }
Jaegeuk Kim4edba232018-09-25 13:54:33 -0700599 ClearPageError(page);
Jaegeuk Kim8025d722018-10-16 10:20:53 -0700600 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700601 __submit_bio(F2FS_I_SB(inode), bio, DATA);
602 return 0;
603}
604
Chao Yu46008c62016-05-09 19:56:30 +0800605static void __set_data_blkaddr(struct dnode_of_data *dn)
606{
607 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
608 __le32 *addr_array;
Chao Yufbcf9312017-07-19 00:19:06 +0800609 int base = 0;
610
611 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
612 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800613
614 /* Get physical address of data block */
615 addr_array = blkaddr_in_node(rn);
Chao Yufbcf9312017-07-19 00:19:06 +0800616 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800617}
618
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900619/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900620 * Lock ordering for the change of data block address:
621 * ->data_page
622 * ->node_page
623 * update block addresses in the node page
624 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800625void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900626{
Chao Yud9b3df52018-12-25 17:43:42 +0800627 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +0800628 __set_data_blkaddr(dn);
629 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800630 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900631}
632
Chao Yuf28b3432016-02-24 17:16:47 +0800633void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
634{
635 dn->data_blkaddr = blkaddr;
Chao Yu6b4d6a82018-05-30 00:20:41 +0800636 f2fs_set_data_blkaddr(dn);
Chao Yuf28b3432016-02-24 17:16:47 +0800637 f2fs_update_extent_cache(dn);
638}
639
Chao Yu46008c62016-05-09 19:56:30 +0800640/* dn->ofs_in_node will be returned with up-to-date last block pointer */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800641int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900642{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700643 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu09c3a722017-07-09 00:13:07 +0800644 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900645
Chao Yu46008c62016-05-09 19:56:30 +0800646 if (!count)
647 return 0;
648
Jaegeuk Kim91942322016-05-20 10:13:22 -0700649 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900650 return -EPERM;
Chao Yu09c3a722017-07-09 00:13:07 +0800651 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
652 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900653
Chao Yu46008c62016-05-09 19:56:30 +0800654 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
655 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900656
Chao Yud9b3df52018-12-25 17:43:42 +0800657 f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true);
Chao Yu46008c62016-05-09 19:56:30 +0800658
659 for (; count > 0; dn->ofs_in_node++) {
Chao Yufbcf9312017-07-19 00:19:06 +0800660 block_t blkaddr = datablock_addr(dn->inode,
661 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800662 if (blkaddr == NULL_ADDR) {
663 dn->data_blkaddr = NEW_ADDR;
664 __set_data_blkaddr(dn);
665 count--;
666 }
667 }
668
669 if (set_page_dirty(dn->node_page))
670 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900671 return 0;
672}
673
Chao Yu46008c62016-05-09 19:56:30 +0800674/* Should keep dn->ofs_in_node unchanged */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800675int f2fs_reserve_new_block(struct dnode_of_data *dn)
Chao Yu46008c62016-05-09 19:56:30 +0800676{
677 unsigned int ofs_in_node = dn->ofs_in_node;
678 int ret;
679
Chao Yu6b4d6a82018-05-30 00:20:41 +0800680 ret = f2fs_reserve_new_blocks(dn, 1);
Chao Yu46008c62016-05-09 19:56:30 +0800681 dn->ofs_in_node = ofs_in_node;
682 return ret;
683}
684
Huajun Lib6009652013-11-10 23:13:18 +0800685int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
686{
687 bool need_put = dn->inode_page ? false : true;
688 int err;
689
Chao Yu6b4d6a82018-05-30 00:20:41 +0800690 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
Huajun Lib6009652013-11-10 23:13:18 +0800691 if (err)
692 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900693
Huajun Lib6009652013-11-10 23:13:18 +0800694 if (dn->data_blkaddr == NULL_ADDR)
Chao Yu6b4d6a82018-05-30 00:20:41 +0800695 err = f2fs_reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900696 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800697 f2fs_put_dnode(dn);
698 return err;
699}
700
Fan Li759af1c2015-08-05 15:52:16 +0800701int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900702{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700703 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800704 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800705
Fan Li759af1c2015-08-05 15:52:16 +0800706 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
707 dn->data_blkaddr = ei.blk + index - ei.fofs;
708 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800709 }
710
Fan Li759af1c2015-08-05 15:52:16 +0800711 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900712}
713
Chao Yu6b4d6a82018-05-30 00:20:41 +0800714struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500715 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900716{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900717 struct address_space *mapping = inode->i_mapping;
718 struct dnode_of_data dn;
719 struct page *page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700720 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900721 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700722
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700723 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900724 if (!page)
725 return ERR_PTR(-ENOMEM);
726
Chao Yucb3bc9e2015-02-05 18:03:40 +0800727 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
728 dn.data_blkaddr = ei.blk + index - ei.fofs;
729 goto got_it;
730 }
731
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900732 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800733 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700734 if (err)
735 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900736 f2fs_put_dnode(&dn);
737
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900738 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700739 err = -ENOENT;
740 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900741 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800742got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700743 if (PageUptodate(page)) {
744 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900745 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700746 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900747
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900748 /*
749 * A new dentry page is allocated but not able to be written, since its
750 * new inode page couldn't be allocated due to -ENOSPC.
751 * In such the case, its blkaddr can be remained as NEW_ADDR.
Chao Yu6b4d6a82018-05-30 00:20:41 +0800752 * see, f2fs_add_link -> f2fs_get_new_data_page ->
753 * f2fs_init_inode_metadata.
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900754 */
755 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300756 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700757 if (!PageUptodate(page))
758 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700759 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900760 return page;
761 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900762
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700763 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900764 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700765 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700766 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700767
768put_err:
769 f2fs_put_page(page, 1);
770 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700771}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900772
Chao Yu6b4d6a82018-05-30 00:20:41 +0800773struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700774{
775 struct address_space *mapping = inode->i_mapping;
776 struct page *page;
777
778 page = find_get_page(mapping, index);
779 if (page && PageUptodate(page))
780 return page;
781 f2fs_put_page(page, 0);
782
Chao Yu6b4d6a82018-05-30 00:20:41 +0800783 page = f2fs_get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700784 if (IS_ERR(page))
785 return page;
786
787 if (PageUptodate(page))
788 return page;
789
790 wait_on_page_locked(page);
791 if (unlikely(!PageUptodate(page))) {
792 f2fs_put_page(page, 0);
793 return ERR_PTR(-EIO);
794 }
795 return page;
796}
797
798/*
799 * If it tries to access a hole, return an error.
800 * Because, the callers, functions in dir.c and GC, should be able to know
801 * whether this page exists or not.
802 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800803struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700804 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700805{
806 struct address_space *mapping = inode->i_mapping;
807 struct page *page;
808repeat:
Chao Yu6b4d6a82018-05-30 00:20:41 +0800809 page = f2fs_get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700810 if (IS_ERR(page))
811 return page;
812
813 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900814 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900815 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900816 f2fs_put_page(page, 1);
817 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900818 }
Chao Yu1563ac72016-07-03 22:05:12 +0800819 if (unlikely(!PageUptodate(page))) {
820 f2fs_put_page(page, 1);
821 return ERR_PTR(-EIO);
822 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900823 return page;
824}
825
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900826/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900827 * Caller ensures that this data page is never allocated.
828 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900829 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800830 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
831 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800832 * Note that, ipage is set only by make_empty_dir, and if any error occur,
833 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900834 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800835struct page *f2fs_get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900836 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900837{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900838 struct address_space *mapping = inode->i_mapping;
839 struct page *page;
840 struct dnode_of_data dn;
841 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800842
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700843 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800844 if (!page) {
845 /*
846 * before exiting, we should make sure ipage will be released
847 * if any error occur.
848 */
849 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700850 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800851 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900852
Jaegeuk Kima8865372013-12-27 17:04:17 +0900853 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800854 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700855 if (err) {
856 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900857 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900858 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700859 if (!ipage)
860 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900861
862 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700863 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900864
865 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300866 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700867 if (!PageUptodate(page))
868 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900869 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700870 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900871
Jaegeuk Kim76121182016-01-01 22:03:47 -0800872 /* if ipage exists, blkaddr should be NEW_ADDR */
873 f2fs_bug_on(F2FS_I_SB(inode), ipage);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800874 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700875 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800876 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900877 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700878got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800879 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700880 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700881 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900882 return page;
883}
884
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900885static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900886{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700887 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900888 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900889 struct node_info ni;
Chao Yu08386a62018-08-14 22:37:25 +0800890 block_t old_blkaddr;
Chao Yu46008c62016-05-09 19:56:30 +0800891 blkcnt_t count = 1;
Chao Yu09c3a722017-07-09 00:13:07 +0800892 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900893
Jaegeuk Kim91942322016-05-20 10:13:22 -0700894 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900895 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800896
Chao Yu3c743262018-07-17 00:02:17 +0800897 err = f2fs_get_node_info(sbi, dn->nid, &ni);
898 if (err)
899 return err;
900
Chao Yufbcf9312017-07-19 00:19:06 +0800901 dn->data_blkaddr = datablock_addr(dn->inode,
902 dn->node_page, dn->ofs_in_node);
Chao Yub84f9a72018-09-27 18:34:52 +0800903 if (dn->data_blkaddr != NULL_ADDR)
Chao Yudf6136e2015-03-23 10:33:37 +0800904 goto alloc;
905
Chao Yu09c3a722017-07-09 00:13:07 +0800906 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
907 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900908
Chao Yudf6136e2015-03-23 10:33:37 +0800909alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900910 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
Chao Yu08386a62018-08-14 22:37:25 +0800911 old_blkaddr = dn->data_blkaddr;
912 f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900913 &sum, seg_type, NULL, false);
Chao Yu08386a62018-08-14 22:37:25 +0800914 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
915 invalidate_mapping_pages(META_MAPPING(sbi),
916 old_blkaddr, old_blkaddr);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800917 f2fs_set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900918
Jaegeuk Kim0328f882018-09-19 15:28:40 -0700919 /*
920 * i_size will be updated by direct_IO. Otherwise, we'll get stale
921 * data from unwritten block via dio_read.
922 */
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900923 return 0;
924}
925
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700926int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800927{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800928 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800929 struct f2fs_map_blocks map;
Chao Yu8fc74462018-01-03 17:30:19 +0800930 int flag;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700931 int err = 0;
Chao Yu8fc74462018-01-03 17:30:19 +0800932 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700933
Weichao Guo8456d342017-09-29 22:43:23 +0800934 /* convert inline data for Direct I/O*/
Chao Yu8fc74462018-01-03 17:30:19 +0800935 if (direct_io) {
Weichao Guo8456d342017-09-29 22:43:23 +0800936 err = f2fs_convert_inline_inode(inode);
937 if (err)
938 return err;
939 }
940
Chao Yu5b5162d2018-11-13 14:33:45 +0800941 if (direct_io && allow_outplace_dio(inode, iocb, from))
942 return 0;
943
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700944 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
945 return 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800946
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700947 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800948 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
949 if (map.m_len > map.m_lblk)
950 map.m_len -= map.m_lblk;
951 else
952 map.m_len = 0;
953
Chao Yuda859852016-01-26 15:42:58 +0800954 map.m_next_pgofs = NULL;
Chao Yu6bce9632018-01-11 14:42:30 +0800955 map.m_next_extent = NULL;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900956 map.m_seg_type = NO_CHECK_TYPE;
Chao Yu5b5162d2018-11-13 14:33:45 +0800957 map.m_may_create = true;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800958
Chao Yu8fc74462018-01-03 17:30:19 +0800959 if (direct_io) {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800960 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
Chao Yub84f9a72018-09-27 18:34:52 +0800961 flag = f2fs_force_buffered_io(inode, iocb, from) ?
Chao Yu8fc74462018-01-03 17:30:19 +0800962 F2FS_GET_BLOCK_PRE_AIO :
963 F2FS_GET_BLOCK_PRE_DIO;
964 goto map_blocks;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900965 }
Chao Yue84f88e2017-07-19 00:19:05 +0800966 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700967 err = f2fs_convert_inline_inode(inode);
968 if (err)
969 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800970 }
Chao Yu8fc74462018-01-03 17:30:19 +0800971 if (f2fs_has_inline_data(inode))
Sheng Yong76b6e8e2017-11-22 18:23:39 +0800972 return err;
Chao Yu8fc74462018-01-03 17:30:19 +0800973
974 flag = F2FS_GET_BLOCK_PRE_AIO;
975
976map_blocks:
977 err = f2fs_map_blocks(inode, &map, 1, flag);
978 if (map.m_len > 0 && err == -ENOSPC) {
979 if (!direct_io)
980 set_inode_flag(inode, FI_NO_PREALLOC);
981 err = 0;
Sheng Yong76b6e8e2017-11-22 18:23:39 +0800982 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700983 return err;
984}
985
Chao Yu6923cd12018-09-27 18:33:18 +0800986void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700987{
988 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
989 if (lock)
990 down_read(&sbi->node_change);
991 else
992 up_read(&sbi->node_change);
993 } else {
994 if (lock)
995 f2fs_lock_op(sbi);
996 else
997 f2fs_unlock_op(sbi);
998 }
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800999}
1000
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001001/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001002 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
1003 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +08001004 * If original data blocks are allocated, then give them to blockdev.
1005 * Otherwise,
1006 * a. preallocate requested block addresses
1007 * b. do not use extent cache for better performance
1008 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001009 */
Chao Yud323d002015-10-27 09:53:45 +08001010int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +08001011 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001012{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001013 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001014 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +08001015 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu5b5162d2018-11-13 14:33:45 +08001016 int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +08001017 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001018 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +08001019 unsigned int ofs_in_node, last_ofs_in_node;
1020 blkcnt_t prealloc;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001021 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +08001022 block_t blkaddr;
Chao Yu6bce9632018-01-11 14:42:30 +08001023 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001024
Chao Yudfd02e42016-08-20 15:12:01 +08001025 if (!maxblocks)
1026 return 0;
1027
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001028 map->m_len = 0;
1029 map->m_flags = 0;
1030
1031 /* it only supports block size == page size */
1032 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +08001033 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001034
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001035 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jia Zhue3faef12018-11-20 04:29:35 +08001036 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1037 map->m_may_create)
1038 goto next_dnode;
1039
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001040 map->m_pblk = ei.blk + pgofs - ei.fofs;
1041 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1042 map->m_flags = F2FS_MAP_MAPPED;
Chao Yu6bce9632018-01-11 14:42:30 +08001043 if (map->m_next_extent)
1044 *map->m_next_extent = pgofs + map->m_len;
Sahitya Tummala3d571472018-10-10 10:56:22 +05301045
1046 /* for hardware encryption, but to avoid potential issue in future */
1047 if (flag == F2FS_GET_BLOCK_DIO)
1048 f2fs_wait_on_block_writeback_range(inode,
1049 map->m_pblk, map->m_len);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001050 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001051 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001052
Chao Yu4fe71e82016-01-26 15:37:38 +08001053next_dnode:
Chao Yu5b5162d2018-11-13 14:33:45 +08001054 if (map->m_may_create)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001055 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001056
1057 /* When reading holes, we need its node page */
1058 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001059 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001060 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +08001061 if (flag == F2FS_GET_BLOCK_BMAP)
1062 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +08001063 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001064 err = 0;
Chao Yuda859852016-01-26 15:42:58 +08001065 if (map->m_next_pgofs)
1066 *map->m_next_pgofs =
Chao Yu6b4d6a82018-05-30 00:20:41 +08001067 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu6bce9632018-01-11 14:42:30 +08001068 if (map->m_next_extent)
1069 *map->m_next_extent =
Chao Yu6b4d6a82018-05-30 00:20:41 +08001070 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +08001071 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001072 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001073 }
Chao Yu973163f2015-09-18 16:51:51 +08001074
Chao Yu6bce9632018-01-11 14:42:30 +08001075 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +08001076 prealloc = 0;
Arnd Bergmanna299abd2016-11-02 14:52:15 +01001077 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +08001078 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001079
Chao Yu4fe71e82016-01-26 15:37:38 +08001080next_block:
Chao Yufbcf9312017-07-19 00:19:06 +08001081 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +08001082
Chao Yub3c869a2018-08-01 19:13:44 +08001083 if (__is_valid_data_blkaddr(blkaddr) &&
1084 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
1085 err = -EFAULT;
1086 goto sync_out;
1087 }
1088
Chao Yub84f9a72018-09-27 18:34:52 +08001089 if (is_valid_data_blkaddr(sbi, blkaddr)) {
1090 /* use out-place-update for driect IO under LFS mode */
Chao Yu5b5162d2018-11-13 14:33:45 +08001091 if (test_opt(sbi, LFS) && flag == F2FS_GET_BLOCK_DIO &&
1092 map->m_may_create) {
Chao Yub84f9a72018-09-27 18:34:52 +08001093 err = __allocate_data_block(&dn, map->m_seg_type);
Jia Zhu35304bb2018-11-27 02:32:32 +08001094 if (!err) {
1095 blkaddr = dn.data_blkaddr;
Chao Yub84f9a72018-09-27 18:34:52 +08001096 set_inode_flag(inode, FI_APPEND_WRITE);
Jia Zhu35304bb2018-11-27 02:32:32 +08001097 }
Chao Yub84f9a72018-09-27 18:34:52 +08001098 }
1099 } else {
Fan Li7df3a432015-12-17 13:20:59 +08001100 if (create) {
1101 if (unlikely(f2fs_cp_error(sbi))) {
1102 err = -EIO;
1103 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +08001104 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001105 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +08001106 if (blkaddr == NULL_ADDR) {
1107 prealloc++;
1108 last_ofs_in_node = dn.ofs_in_node;
1109 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001110 } else {
Jaegeuk Kim0328f882018-09-19 15:28:40 -07001111 WARN_ON(flag != F2FS_GET_BLOCK_PRE_DIO &&
1112 flag != F2FS_GET_BLOCK_DIO);
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001113 err = __allocate_data_block(&dn,
1114 map->m_seg_type);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001115 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001116 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001117 }
Fan Li7df3a432015-12-17 13:20:59 +08001118 if (err)
1119 goto sync_out;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001120 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001121 blkaddr = dn.data_blkaddr;
1122 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001123 if (flag == F2FS_GET_BLOCK_BMAP) {
1124 map->m_pblk = 0;
1125 goto sync_out;
1126 }
Chao Yu6bce9632018-01-11 14:42:30 +08001127 if (flag == F2FS_GET_BLOCK_PRECACHE)
1128 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001129 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1130 blkaddr == NULL_ADDR) {
1131 if (map->m_next_pgofs)
1132 *map->m_next_pgofs = pgofs + 1;
Chao Yu987892c2017-11-30 19:28:22 +08001133 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001134 }
Chao Yufd214422018-01-10 18:18:52 +08001135 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1136 /* for defragment case */
1137 if (map->m_next_pgofs)
1138 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001139 goto sync_out;
Chao Yufd214422018-01-10 18:18:52 +08001140 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001141 }
1142 }
Fan Li7df3a432015-12-17 13:20:59 +08001143
Chao Yu46008c62016-05-09 19:56:30 +08001144 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1145 goto skip;
1146
Chao Yu4fe71e82016-01-26 15:37:38 +08001147 if (map->m_len == 0) {
1148 /* preallocated unwritten block should be mapped for fiemap. */
1149 if (blkaddr == NEW_ADDR)
1150 map->m_flags |= F2FS_MAP_UNWRITTEN;
1151 map->m_flags |= F2FS_MAP_MAPPED;
1152
1153 map->m_pblk = blkaddr;
1154 map->m_len = 1;
1155 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001156 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001157 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001158 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001159 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001160 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001161 } else {
1162 goto sync_out;
1163 }
1164
Chao Yu46008c62016-05-09 19:56:30 +08001165skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001166 dn.ofs_in_node++;
1167 pgofs++;
1168
Chao Yu46008c62016-05-09 19:56:30 +08001169 /* preallocate blocks in batch for one dnode page */
1170 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1171 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001172
Chao Yu46008c62016-05-09 19:56:30 +08001173 dn.ofs_in_node = ofs_in_node;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001174 err = f2fs_reserve_new_blocks(&dn, prealloc);
Chao Yu46008c62016-05-09 19:56:30 +08001175 if (err)
1176 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001177
Chao Yu46008c62016-05-09 19:56:30 +08001178 map->m_len += dn.ofs_in_node - ofs_in_node;
1179 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1180 err = -ENOSPC;
1181 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001182 }
Chao Yu46008c62016-05-09 19:56:30 +08001183 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001184 }
1185
Chao Yu46008c62016-05-09 19:56:30 +08001186 if (pgofs >= end)
1187 goto sync_out;
1188 else if (dn.ofs_in_node < end_offset)
1189 goto next_block;
1190
Chao Yu6bce9632018-01-11 14:42:30 +08001191 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1192 if (map->m_flags & F2FS_MAP_MAPPED) {
1193 unsigned int ofs = start_pgofs - map->m_lblk;
1194
1195 f2fs_update_extent_cache_range(&dn,
1196 start_pgofs, map->m_pblk + ofs,
1197 map->m_len - ofs);
1198 }
1199 }
1200
Chao Yu46008c62016-05-09 19:56:30 +08001201 f2fs_put_dnode(&dn);
1202
Chao Yu5b5162d2018-11-13 14:33:45 +08001203 if (map->m_may_create) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001204 __do_map_lock(sbi, flag, false);
1205 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001206 }
Chao Yu46008c62016-05-09 19:56:30 +08001207 goto next_dnode;
1208
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001209sync_out:
Sahitya Tummala3d571472018-10-10 10:56:22 +05301210
1211 /* for hardware encryption, but to avoid potential issue in future */
1212 if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
1213 f2fs_wait_on_block_writeback_range(inode,
1214 map->m_pblk, map->m_len);
1215
Chao Yu6bce9632018-01-11 14:42:30 +08001216 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1217 if (map->m_flags & F2FS_MAP_MAPPED) {
1218 unsigned int ofs = start_pgofs - map->m_lblk;
1219
1220 f2fs_update_extent_cache_range(&dn,
1221 start_pgofs, map->m_pblk + ofs,
1222 map->m_len - ofs);
1223 }
1224 if (map->m_next_extent)
1225 *map->m_next_extent = pgofs + 1;
1226 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001227 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001228unlock_out:
Chao Yu5b5162d2018-11-13 14:33:45 +08001229 if (map->m_may_create) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001230 __do_map_lock(sbi, flag, false);
1231 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001232 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001233out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001234 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001235 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001236}
1237
Hyunchul Lee355d2342018-03-08 19:34:38 +09001238bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1239{
1240 struct f2fs_map_blocks map;
1241 block_t last_lblk;
1242 int err;
1243
1244 if (pos + len > i_size_read(inode))
1245 return false;
1246
1247 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1248 map.m_next_pgofs = NULL;
1249 map.m_next_extent = NULL;
1250 map.m_seg_type = NO_CHECK_TYPE;
Jia Zhue3faef12018-11-20 04:29:35 +08001251 map.m_may_create = false;
Hyunchul Lee355d2342018-03-08 19:34:38 +09001252 last_lblk = F2FS_BLK_ALIGN(pos + len);
1253
1254 while (map.m_lblk < last_lblk) {
1255 map.m_len = last_lblk - map.m_lblk;
1256 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1257 if (err || map.m_len == 0)
1258 return false;
1259 map.m_lblk += map.m_len;
1260 }
1261 return true;
1262}
1263
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001264static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001265 struct buffer_head *bh, int create, int flag,
Chao Yu5b5162d2018-11-13 14:33:45 +08001266 pgoff_t *next_pgofs, int seg_type, bool may_write)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001267{
1268 struct f2fs_map_blocks map;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001269 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001270
1271 map.m_lblk = iblock;
1272 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001273 map.m_next_pgofs = next_pgofs;
Chao Yu6bce9632018-01-11 14:42:30 +08001274 map.m_next_extent = NULL;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001275 map.m_seg_type = seg_type;
Chao Yu5b5162d2018-11-13 14:33:45 +08001276 map.m_may_create = may_write;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001277
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001278 err = f2fs_map_blocks(inode, &map, create, flag);
1279 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001280 map_bh(bh, inode->i_sb, map.m_pblk);
1281 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001282 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001283 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001284 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001285}
1286
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001287static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001288 struct buffer_head *bh_result, int create, int flag,
1289 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001290{
Chao Yuda859852016-01-26 15:42:58 +08001291 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001292 flag, next_pgofs,
Chao Yu5b5162d2018-11-13 14:33:45 +08001293 NO_CHECK_TYPE, create);
1294}
1295
1296static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1297 struct buffer_head *bh_result, int create)
1298{
1299 return __get_data_block(inode, iblock, bh_result, create,
1300 F2FS_GET_BLOCK_DIO, NULL,
1301 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1302 true);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001303}
1304
Chao Yue2b4e2b2015-08-19 19:11:19 +08001305static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001306 struct buffer_head *bh_result, int create)
1307{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001308 return __get_data_block(inode, iblock, bh_result, create,
Chao Yu5b5162d2018-11-13 14:33:45 +08001309 F2FS_GET_BLOCK_DIO, NULL,
1310 f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1311 false);
Chao Yue2b4e2b2015-08-19 19:11:19 +08001312}
1313
1314static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1315 struct buffer_head *bh_result, int create)
1316{
Yunlei He179448b2015-12-28 21:48:32 +08001317 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001318 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001319 return -EFBIG;
1320
Chao Yue2b4e2b2015-08-19 19:11:19 +08001321 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001322 F2FS_GET_BLOCK_BMAP, NULL,
Chao Yu5b5162d2018-11-13 14:33:45 +08001323 NO_CHECK_TYPE, create);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001324}
1325
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001326static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1327{
1328 return (offset >> inode->i_blkbits);
1329}
1330
1331static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1332{
1333 return (blk << inode->i_blkbits);
1334}
1335
Chao Yu08ddb192018-01-11 14:39:57 +08001336static int f2fs_xattr_fiemap(struct inode *inode,
1337 struct fiemap_extent_info *fieinfo)
1338{
1339 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1340 struct page *page;
1341 struct node_info ni;
1342 __u64 phys = 0, len;
1343 __u32 flags;
1344 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1345 int err = 0;
1346
1347 if (f2fs_has_inline_xattr(inode)) {
1348 int offset;
1349
1350 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1351 inode->i_ino, false);
1352 if (!page)
1353 return -ENOMEM;
1354
Chao Yu3c743262018-07-17 00:02:17 +08001355 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1356 if (err) {
1357 f2fs_put_page(page, 1);
1358 return err;
1359 }
Chao Yu08ddb192018-01-11 14:39:57 +08001360
1361 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1362 offset = offsetof(struct f2fs_inode, i_addr) +
1363 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yu3aa46e22018-01-17 16:31:36 +08001364 get_inline_xattr_addrs(inode));
Chao Yu08ddb192018-01-11 14:39:57 +08001365
1366 phys += offset;
1367 len = inline_xattr_size(inode);
1368
1369 f2fs_put_page(page, 1);
1370
1371 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1372
1373 if (!xnid)
1374 flags |= FIEMAP_EXTENT_LAST;
1375
1376 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1377 if (err || err == 1)
1378 return err;
1379 }
1380
1381 if (xnid) {
1382 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1383 if (!page)
1384 return -ENOMEM;
1385
Chao Yu3c743262018-07-17 00:02:17 +08001386 err = f2fs_get_node_info(sbi, xnid, &ni);
1387 if (err) {
1388 f2fs_put_page(page, 1);
1389 return err;
1390 }
Chao Yu08ddb192018-01-11 14:39:57 +08001391
1392 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1393 len = inode->i_sb->s_blocksize;
1394
1395 f2fs_put_page(page, 1);
1396
1397 flags = FIEMAP_EXTENT_LAST;
1398 }
1399
1400 if (phys)
1401 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1402
1403 return (err < 0 ? err : 0);
1404}
1405
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001406int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1407 u64 start, u64 len)
1408{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001409 struct buffer_head map_bh;
1410 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001411 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001412 u64 logical = 0, phys = 0, size = 0;
1413 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001414 int ret = 0;
1415
Chao Yu6bce9632018-01-11 14:42:30 +08001416 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1417 ret = f2fs_precache_extents(inode);
1418 if (ret)
1419 return ret;
1420 }
1421
Chao Yu08ddb192018-01-11 14:39:57 +08001422 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001423 if (ret)
1424 return ret;
1425
Chao Yuaa9c1c12018-01-11 14:37:35 +08001426 inode_lock(inode);
1427
Chao Yu08ddb192018-01-11 14:39:57 +08001428 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1429 ret = f2fs_xattr_fiemap(inode, fieinfo);
1430 goto out;
1431 }
1432
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -07001433 if (f2fs_has_inline_data(inode)) {
1434 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1435 if (ret != -EAGAIN)
Chao Yuaa9c1c12018-01-11 14:37:35 +08001436 goto out;
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -07001437 }
1438
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001439 if (logical_to_blk(inode, len) == 0)
1440 len = blk_to_logical(inode, 1);
1441
1442 start_blk = logical_to_blk(inode, start);
1443 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001444
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001445next:
1446 memset(&map_bh, 0, sizeof(struct buffer_head));
1447 map_bh.b_size = len;
1448
Chao Yue2b4e2b2015-08-19 19:11:19 +08001449 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001450 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001451 if (ret)
1452 goto out;
1453
1454 /* HOLE */
1455 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001456 start_blk = next_pgofs;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001457
1458 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1459 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001460 goto prep_next;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001461
Fan Li9a950d52015-12-26 18:07:41 +08001462 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001463 }
Fan Li9a950d52015-12-26 18:07:41 +08001464
Chao Yuda5af122016-01-08 20:19:27 +08001465 if (size) {
1466 if (f2fs_encrypted_inode(inode))
1467 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1468
Fan Li9a950d52015-12-26 18:07:41 +08001469 ret = fiemap_fill_next_extent(fieinfo, logical,
1470 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001471 }
Fan Li9a950d52015-12-26 18:07:41 +08001472
1473 if (start_blk > last_blk || ret)
1474 goto out;
1475
1476 logical = blk_to_logical(inode, start_blk);
1477 phys = blk_to_logical(inode, map_bh.b_blocknr);
1478 size = map_bh.b_size;
1479 flags = 0;
1480 if (buffer_unwritten(&map_bh))
1481 flags = FIEMAP_EXTENT_UNWRITTEN;
1482
1483 start_blk += logical_to_blk(inode, size);
1484
1485prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001486 cond_resched();
1487 if (fatal_signal_pending(current))
1488 ret = -EINTR;
1489 else
1490 goto next;
1491out:
1492 if (ret == 1)
1493 ret = 0;
1494
Al Viro59551022016-01-22 15:40:57 -05001495 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001496 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001497}
1498
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001499/*
1500 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1501 * Major change was from block_size == page_size in f2fs by default.
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001502 *
1503 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1504 * this function ever deviates from doing just read-ahead, it should either
1505 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1506 * from read-ahead.
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001507 */
1508static int f2fs_mpage_readpages(struct address_space *mapping,
1509 struct list_head *pages, struct page *page,
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001510 unsigned nr_pages, bool is_readahead)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001511{
1512 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001513 sector_t last_block_in_bio = 0;
1514 struct inode *inode = mapping->host;
1515 const unsigned blkbits = inode->i_blkbits;
1516 const unsigned blocksize = 1 << blkbits;
1517 sector_t block_in_file;
1518 sector_t last_block;
1519 sector_t last_block_in_file;
1520 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001521 struct f2fs_map_blocks map;
1522
1523 map.m_pblk = 0;
1524 map.m_lblk = 0;
1525 map.m_len = 0;
1526 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001527 map.m_next_pgofs = NULL;
Chao Yu6bce9632018-01-11 14:42:30 +08001528 map.m_next_extent = NULL;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001529 map.m_seg_type = NO_CHECK_TYPE;
Chao Yu5b5162d2018-11-13 14:33:45 +08001530 map.m_may_create = false;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001531
LiFanba4556c2017-11-25 11:46:18 +08001532 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001533 if (pages) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001534 page = list_last_entry(pages, struct page, lru);
1535
1536 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001537 list_del(&page->lru);
1538 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001539 page->index,
1540 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001541 goto next_page;
1542 }
1543
1544 block_in_file = (sector_t)page->index;
1545 last_block = block_in_file + nr_pages;
1546 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1547 blkbits;
1548 if (last_block > last_block_in_file)
1549 last_block = last_block_in_file;
1550
Chao Yub50924a2019-03-07 17:31:30 +08001551 /* just zeroing out page which is beyond EOF */
1552 if (block_in_file >= last_block)
1553 goto zero_out;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001554 /*
1555 * Map blocks using the previous result first.
1556 */
1557 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1558 block_in_file > map.m_lblk &&
1559 block_in_file < (map.m_lblk + map.m_len))
1560 goto got_it;
1561
1562 /*
1563 * Then do more f2fs_map_blocks() calls until we are
1564 * done with this page.
1565 */
Chao Yub50924a2019-03-07 17:31:30 +08001566 map.m_lblk = block_in_file;
1567 map.m_len = last_block - block_in_file;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001568
Chao Yub50924a2019-03-07 17:31:30 +08001569 if (f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT))
1570 goto set_error_page;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001571got_it:
1572 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1573 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1574 SetPageMappedToDisk(page);
1575
1576 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1577 SetPageUptodate(page);
1578 goto confused;
1579 }
Chao Yub3c869a2018-08-01 19:13:44 +08001580
1581 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1582 DATA_GENERIC))
1583 goto set_error_page;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001584 } else {
Chao Yub50924a2019-03-07 17:31:30 +08001585zero_out:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001586 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001587 if (!PageUptodate(page))
1588 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001589 unlock_page(page);
1590 goto next_page;
1591 }
1592
1593 /*
1594 * This page will go to BIO. Do we need to send this
1595 * BIO off first?
1596 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001597 if (bio && (last_block_in_bio != block_nr - 1 ||
1598 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001599submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001600 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001601 bio = NULL;
1602 }
1603 if (bio == NULL) {
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001604 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
1605 is_readahead ? REQ_RAHEAD : 0);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001606 if (IS_ERR(bio)) {
1607 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001608 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001609 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001610 }
1611
Jaegeuk Kim0d51cea2018-08-22 21:18:00 -07001612 /*
1613 * If the page is under writeback, we need to wait for
1614 * its completion to see the correct decrypted data.
1615 */
1616 f2fs_wait_on_block_writeback(inode, block_nr);
1617
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001618 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1619 goto submit_and_realloc;
1620
Jaegeuk Kim8025d722018-10-16 10:20:53 -07001621 inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA);
Jaegeuk Kim4edba232018-09-25 13:54:33 -07001622 ClearPageError(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001623 last_block_in_bio = block_nr;
1624 goto next_page;
1625set_error_page:
1626 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001627 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001628 unlock_page(page);
1629 goto next_page;
1630confused:
1631 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001632 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001633 bio = NULL;
1634 }
1635 unlock_page(page);
1636next_page:
1637 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001638 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001639 }
1640 BUG_ON(pages && !list_empty(pages));
1641 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001642 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001643 return 0;
1644}
1645
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001646static int f2fs_read_data_page(struct file *file, struct page *page)
1647{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001648 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001649 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001650
Chao Yuc20e89c2014-05-06 16:53:08 +08001651 trace_f2fs_readpage(page, DATA);
1652
arter97e1c42042014-08-06 23:22:50 +09001653 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001654 if (f2fs_has_inline_data(inode))
1655 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001656 if (ret == -EAGAIN)
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001657 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1, false);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001658 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001659}
1660
1661static int f2fs_read_data_pages(struct file *file,
1662 struct address_space *mapping,
1663 struct list_head *pages, unsigned nr_pages)
1664{
Hsiang Kao3f01af72017-09-24 02:45:42 +08001665 struct inode *inode = mapping->host;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001666 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001667
1668 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001669
1670 /* If the file has inline data, skip readpages */
1671 if (f2fs_has_inline_data(inode))
1672 return 0;
1673
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001674 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001675}
1676
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001677static int encrypt_one_page(struct f2fs_io_info *fio)
1678{
1679 struct inode *inode = fio->page->mapping->host;
Chao Yu08386a62018-08-14 22:37:25 +08001680 struct page *mpage;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001681 gfp_t gfp_flags = GFP_NOFS;
1682
Jaegeuk Kima4fb1892017-09-05 16:54:24 -07001683 if (!f2fs_encrypted_file(inode))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001684 return 0;
1685
Eric Biggersf69e8142018-04-18 11:09:48 -07001686 /* wait for GCed page writeback via META_MAPPING */
Jaegeuk Kim0d51cea2018-08-22 21:18:00 -07001687 f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001688
1689retry_encrypt:
1690 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1691 PAGE_SIZE, 0, fio->page->index, gfp_flags);
Chao Yu08386a62018-08-14 22:37:25 +08001692 if (IS_ERR(fio->encrypted_page)) {
1693 /* flush pending IOs and wait for a while in the ENOMEM case */
1694 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
1695 f2fs_flush_merged_writes(fio->sbi);
1696 congestion_wait(BLK_RW_ASYNC, HZ/50);
1697 gfp_flags |= __GFP_NOFAIL;
1698 goto retry_encrypt;
1699 }
1700 return PTR_ERR(fio->encrypted_page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001701 }
Chao Yu08386a62018-08-14 22:37:25 +08001702
1703 mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr);
1704 if (mpage) {
1705 if (PageUptodate(mpage))
1706 memcpy(page_address(mpage),
1707 page_address(fio->encrypted_page), PAGE_SIZE);
1708 f2fs_put_page(mpage, 1);
1709 }
1710 return 0;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001711}
1712
Chao Yu66356ee2018-01-17 16:31:38 +08001713static inline bool check_inplace_update_policy(struct inode *inode,
1714 struct f2fs_io_info *fio)
1715{
1716 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1717 unsigned int policy = SM_I(sbi)->ipu_policy;
1718
1719 if (policy & (0x1 << F2FS_IPU_FORCE))
1720 return true;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001721 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
Chao Yu66356ee2018-01-17 16:31:38 +08001722 return true;
1723 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1724 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1725 return true;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001726 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
Chao Yu66356ee2018-01-17 16:31:38 +08001727 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1728 return true;
1729
1730 /*
1731 * IPU for rewrite async pages
1732 */
1733 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1734 fio && fio->op == REQ_OP_WRITE &&
1735 !(fio->op_flags & REQ_SYNC) &&
1736 !f2fs_encrypted_inode(inode))
1737 return true;
1738
1739 /* this is only set during fdatasync */
1740 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1741 is_inode_flag_set(inode, FI_NEED_IPU))
1742 return true;
1743
Daniel Rosenbergf22f93a2018-08-20 19:21:43 -07001744 if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1745 !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1746 return true;
1747
Chao Yu66356ee2018-01-17 16:31:38 +08001748 return false;
1749}
1750
Chao Yu6b4d6a82018-05-30 00:20:41 +08001751bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yu66356ee2018-01-17 16:31:38 +08001752{
1753 if (f2fs_is_pinned_file(inode))
1754 return true;
1755
1756 /* if this is cold file, we should overwrite to avoid fragmentation */
1757 if (file_is_cold(inode))
1758 return true;
1759
1760 return check_inplace_update_policy(inode, fio);
1761}
1762
Chao Yu6b4d6a82018-05-30 00:20:41 +08001763bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yu66356ee2018-01-17 16:31:38 +08001764{
1765 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1766
1767 if (test_opt(sbi, LFS))
1768 return true;
1769 if (S_ISDIR(inode->i_mode))
1770 return true;
Chao Yud8312722018-09-20 20:05:00 +08001771 if (IS_NOQUOTA(inode))
1772 return true;
Chao Yu66356ee2018-01-17 16:31:38 +08001773 if (f2fs_is_atomic_file(inode))
1774 return true;
1775 if (fio) {
1776 if (is_cold_data(fio->page))
1777 return true;
1778 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1779 return true;
Daniel Rosenbergf22f93a2018-08-20 19:21:43 -07001780 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) &&
1781 f2fs_is_checkpointed_data(sbi, fio->old_blkaddr)))
1782 return true;
Chao Yu66356ee2018-01-17 16:31:38 +08001783 }
1784 return false;
1785}
1786
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001787static inline bool need_inplace_update(struct f2fs_io_info *fio)
1788{
1789 struct inode *inode = fio->page->mapping->host;
1790
Chao Yu6b4d6a82018-05-30 00:20:41 +08001791 if (f2fs_should_update_outplace(inode, fio))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001792 return false;
1793
Chao Yu6b4d6a82018-05-30 00:20:41 +08001794 return f2fs_should_update_inplace(inode, fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001795}
1796
Chao Yu6b4d6a82018-05-30 00:20:41 +08001797int f2fs_do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001798{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001799 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001800 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001801 struct dnode_of_data dn;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001802 struct extent_info ei = {0,0,0};
Chao Yu3c743262018-07-17 00:02:17 +08001803 struct node_info ni;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001804 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001805 int err = 0;
1806
1807 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001808 if (need_inplace_update(fio) &&
1809 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1810 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
1811
Chao Yub3c869a2018-08-01 19:13:44 +08001812 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1813 DATA_GENERIC))
1814 return -EFAULT;
1815
1816 ipu_force = true;
1817 fio->need_lock = LOCK_DONE;
1818 goto got_it;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001819 }
1820
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07001821 /* Deadlock due to between page->lock and f2fs_lock_op */
1822 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1823 return -EAGAIN;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001824
Chao Yu6b4d6a82018-05-30 00:20:41 +08001825 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001826 if (err)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001827 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001828
Chao Yu28bc1062016-02-06 14:40:34 +08001829 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001830
1831 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001832 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001833 ClearPageUptodate(page);
Chao Yu408bca02018-07-27 18:15:16 +08001834 clear_cold_data(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001835 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001836 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001837got_it:
Chao Yub3c869a2018-08-01 19:13:44 +08001838 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
1839 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1840 DATA_GENERIC)) {
1841 err = -EFAULT;
1842 goto out_writepage;
1843 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001844 /*
1845 * If current allocation needs SSR,
1846 * it had better in-place writes for updated data.
1847 */
Chao Yud05e8712018-06-05 17:44:11 +08001848 if (ipu_force || (is_valid_data_blkaddr(fio->sbi, fio->old_blkaddr) &&
Chao Yu158d9bb2018-05-23 22:25:08 +08001849 need_inplace_update(fio))) {
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001850 err = encrypt_one_page(fio);
1851 if (err)
1852 goto out_writepage;
1853
1854 set_page_writeback(page);
Jaegeuk Kim926b3702018-04-11 23:09:04 -07001855 ClearPageError(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001856 f2fs_put_dnode(&dn);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001857 if (fio->need_lock == LOCK_REQ)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001858 f2fs_unlock_op(fio->sbi);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001859 err = f2fs_inplace_write_data(fio);
Chao Yub4b34532019-02-21 20:37:14 +08001860 if (err) {
1861 if (f2fs_encrypted_file(inode))
1862 fscrypt_pullback_bio_page(&fio->encrypted_page,
1863 true);
1864 if (PageWriteback(page))
1865 end_page_writeback(page);
1866 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001867 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001868 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001869 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001870 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001871
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001872 if (fio->need_lock == LOCK_RETRY) {
1873 if (!f2fs_trylock_op(fio->sbi)) {
1874 err = -EAGAIN;
1875 goto out_writepage;
1876 }
1877 fio->need_lock = LOCK_REQ;
1878 }
1879
Chao Yu3c743262018-07-17 00:02:17 +08001880 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
1881 if (err)
1882 goto out_writepage;
1883
1884 fio->version = ni.version;
1885
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001886 err = encrypt_one_page(fio);
1887 if (err)
1888 goto out_writepage;
1889
1890 set_page_writeback(page);
Jaegeuk Kim926b3702018-04-11 23:09:04 -07001891 ClearPageError(page);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001892
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001893 /* LFS mode write path */
Chao Yu6b4d6a82018-05-30 00:20:41 +08001894 f2fs_outplace_write_data(&dn, fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001895 trace_f2fs_do_write_data_page(page, OPU);
1896 set_inode_flag(inode, FI_APPEND_WRITE);
1897 if (page->index == 0)
1898 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001899out_writepage:
1900 f2fs_put_dnode(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001901out:
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001902 if (fio->need_lock == LOCK_REQ)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001903 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001904 return err;
1905}
1906
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001907static int __write_data_page(struct page *page, bool *submitted,
Chao Yuc0fe4882017-08-02 23:21:48 +08001908 struct writeback_control *wbc,
1909 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001910{
1911 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001912 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001913 loff_t i_size = i_size_read(inode);
1914 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001915 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001916 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001917 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001918 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001919 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001920 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001921 .sbi = sbi,
Chao Yuf014be82017-09-29 13:59:38 +08001922 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001923 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001924 .op = REQ_OP_WRITE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001925 .op_flags = wbc_to_write_flags(wbc),
1926 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001927 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001928 .encrypted_page = NULL,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001929 .submitted = false,
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001930 .need_lock = LOCK_RETRY,
Chao Yuc0fe4882017-08-02 23:21:48 +08001931 .io_type = io_type,
Yufen Yucbd5e5a2018-01-09 19:33:39 +08001932 .io_wbc = wbc,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001933 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001934
Chao Yuecda0de2014-05-06 16:48:26 +08001935 trace_f2fs_writepage(page, DATA);
1936
Chao Yu86f78c12018-01-18 17:29:10 +08001937 /* we should bypass data pages to proceed the kworkder jobs */
1938 if (unlikely(f2fs_cp_error(sbi))) {
1939 mapping_set_error(page->mapping, -EIO);
Chao Yu9780d682018-05-28 16:59:26 +08001940 /*
1941 * don't drop any dirty dentry pages for keeping lastest
1942 * directory structure.
1943 */
1944 if (S_ISDIR(inode->i_mode))
1945 goto redirty_out;
Chao Yu86f78c12018-01-18 17:29:10 +08001946 goto out;
1947 }
1948
Chao Yu00cc7ed2017-06-29 23:20:45 +08001949 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1950 goto redirty_out;
1951
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001952 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001953 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001954
1955 /*
1956 * If the offset is out-of-range of file size,
1957 * this page does not have to be written to disk.
1958 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001959 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001960 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001961 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001962
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001963 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001964write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001965 if (f2fs_is_drop_cache(inode))
1966 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001967 /* we should not write 0'th page having journal header */
1968 if (f2fs_is_volatile_file(inode) && (!page->index ||
1969 (!wbc->for_reclaim &&
Chao Yu6b4d6a82018-05-30 00:20:41 +08001970 f2fs_available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001971 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001972
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001973 /* Dentry blocks are controlled by checkpoint */
1974 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001975 fio.need_lock = LOCK_DONE;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001976 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001977 goto done;
1978 }
1979
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001980 if (!wbc->for_reclaim)
1981 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001982 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001983 goto redirty_out;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001984 else
1985 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001986
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001987 err = -EAGAIN;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001988 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001989 err = f2fs_write_inline_data(inode, page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001990 if (!err)
1991 goto out;
1992 }
1993
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001994 if (err == -EAGAIN) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08001995 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001996 if (err == -EAGAIN) {
1997 fio.need_lock = LOCK_REQ;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001998 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001999 }
2000 }
Chao Yu33fdebb2017-10-09 17:55:19 +08002001
Chao Yu5912fba2018-01-17 16:31:37 +08002002 if (err) {
2003 file_set_keep_isize(inode);
2004 } else {
2005 down_write(&F2FS_I(inode)->i_sem);
2006 if (F2FS_I(inode)->last_disk_size < psize)
2007 F2FS_I(inode)->last_disk_size = psize;
2008 up_write(&F2FS_I(inode)->i_sem);
2009 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002010
Jaegeuk Kim8618b882014-02-17 19:29:27 +09002011done:
2012 if (err && err != -ENOENT)
2013 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002014
Jaegeuk Kim39936832012-11-22 16:21:29 +09002015out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002016 inode_dec_dirty_pages(inode);
Chao Yu408bca02018-07-27 18:15:16 +08002017 if (err) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08002018 ClearPageUptodate(page);
Chao Yu408bca02018-07-27 18:15:16 +08002019 clear_cold_data(page);
2020 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002021
2022 if (wbc->for_reclaim) {
Chao Yu39373df2018-09-27 23:41:16 +08002023 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002024 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002025 f2fs_remove_dirty_inode(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002026 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08002027 }
Chao Yu0c3a5792016-01-18 18:28:11 +08002028
2029 unlock_page(page);
Chao Yud8312722018-09-20 20:05:00 +08002030 if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002031 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08002032
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002033 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002034 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002035 submitted = NULL;
2036 }
2037
2038 if (submitted)
2039 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08002040
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002041 return 0;
2042
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002043redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09002044 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim5b37ebb2018-05-03 23:26:02 -07002045 /*
2046 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
2047 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
2048 * file_write_and_wait_range() will see EIO error, which is critical
2049 * to return value of fsync() followed by atomic_write failure to user.
2050 */
2051 if (!err || wbc->for_reclaim)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002052 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002053 unlock_page(page);
2054 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09002055}
2056
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002057static int f2fs_write_data_page(struct page *page,
2058 struct writeback_control *wbc)
2059{
Chao Yuc0fe4882017-08-02 23:21:48 +08002060 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002061}
2062
Chao Yu8f46dca2015-07-14 18:56:10 +08002063/*
2064 * This function was copied from write_cche_pages from mm/page-writeback.c.
2065 * The major change is making write step of cold data page separately from
2066 * warm/hot data page.
2067 */
2068static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yuc0fe4882017-08-02 23:21:48 +08002069 struct writeback_control *wbc,
2070 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08002071{
2072 int ret = 0;
2073 int done = 0;
2074 struct pagevec pvec;
Chao Yudeb78d42018-06-04 23:20:36 +08002075 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Chao Yu8f46dca2015-07-14 18:56:10 +08002076 int nr_pages;
2077 pgoff_t uninitialized_var(writeback_index);
2078 pgoff_t index;
2079 pgoff_t end; /* Inclusive */
2080 pgoff_t done_index;
2081 int cycled;
2082 int range_whole = 0;
2083 int tag;
Chao Yu39373df2018-09-27 23:41:16 +08002084 int nwritten = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08002085
2086 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07002087
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002088 if (get_dirty_pages(mapping->host) <=
2089 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
2090 set_inode_flag(mapping->host, FI_HOT_DATA);
2091 else
2092 clear_inode_flag(mapping->host, FI_HOT_DATA);
2093
Chao Yu8f46dca2015-07-14 18:56:10 +08002094 if (wbc->range_cyclic) {
2095 writeback_index = mapping->writeback_index; /* prev offset */
2096 index = writeback_index;
2097 if (index == 0)
2098 cycled = 1;
2099 else
2100 cycled = 0;
2101 end = -1;
2102 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002103 index = wbc->range_start >> PAGE_SHIFT;
2104 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08002105 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2106 range_whole = 1;
2107 cycled = 1; /* ignore range_cyclic tests */
2108 }
2109 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2110 tag = PAGECACHE_TAG_TOWRITE;
2111 else
2112 tag = PAGECACHE_TAG_DIRTY;
2113retry:
2114 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2115 tag_pages_for_writeback(mapping, index, end);
2116 done_index = index;
2117 while (!done && (index <= end)) {
2118 int i;
2119
Jan Karae0673652017-11-15 17:34:48 -08002120 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara4d93a432017-11-15 17:35:19 -08002121 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08002122 if (nr_pages == 0)
2123 break;
2124
2125 for (i = 0; i < nr_pages; i++) {
2126 struct page *page = pvec.pages[i];
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002127 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08002128
Chao Yu458e47f2018-05-23 22:25:09 +08002129 /* give a priority to WB_SYNC threads */
Chao Yudeb78d42018-06-04 23:20:36 +08002130 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
Chao Yu458e47f2018-05-23 22:25:09 +08002131 wbc->sync_mode == WB_SYNC_NONE) {
2132 done = 1;
2133 break;
2134 }
2135
Chao Yu8f46dca2015-07-14 18:56:10 +08002136 done_index = page->index;
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07002137retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08002138 lock_page(page);
2139
2140 if (unlikely(page->mapping != mapping)) {
2141continue_unlock:
2142 unlock_page(page);
2143 continue;
2144 }
2145
2146 if (!PageDirty(page)) {
2147 /* someone wrote it for us */
2148 goto continue_unlock;
2149 }
2150
Chao Yu8f46dca2015-07-14 18:56:10 +08002151 if (PageWriteback(page)) {
2152 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002153 f2fs_wait_on_page_writeback(page,
Chao Yud9b3df52018-12-25 17:43:42 +08002154 DATA, true, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08002155 else
2156 goto continue_unlock;
2157 }
2158
Chao Yu8f46dca2015-07-14 18:56:10 +08002159 if (!clear_page_dirty_for_io(page))
2160 goto continue_unlock;
2161
Chao Yuc0fe4882017-08-02 23:21:48 +08002162 ret = __write_data_page(page, &submitted, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08002163 if (unlikely(ret)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002164 /*
2165 * keep nr_to_write, since vfs uses this to
2166 * get # of written pages.
2167 */
2168 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2169 unlock_page(page);
2170 ret = 0;
2171 continue;
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07002172 } else if (ret == -EAGAIN) {
2173 ret = 0;
2174 if (wbc->sync_mode == WB_SYNC_ALL) {
2175 cond_resched();
2176 congestion_wait(BLK_RW_ASYNC,
2177 HZ/50);
2178 goto retry_write;
2179 }
2180 continue;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002181 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002182 done_index = page->index + 1;
2183 done = 1;
2184 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002185 } else if (submitted) {
Chao Yu39373df2018-09-27 23:41:16 +08002186 nwritten++;
Chao Yu8f46dca2015-07-14 18:56:10 +08002187 }
2188
Chao Yu458e47f2018-05-23 22:25:09 +08002189 if (--wbc->nr_to_write <= 0 &&
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002190 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08002191 done = 1;
2192 break;
2193 }
2194 }
2195 pagevec_release(&pvec);
2196 cond_resched();
2197 }
2198
Chao Yu8f46dca2015-07-14 18:56:10 +08002199 if (!cycled && !done) {
2200 cycled = 1;
2201 index = 0;
2202 end = writeback_index - 1;
2203 goto retry;
2204 }
2205 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2206 mapping->writeback_index = done_index;
2207
Chao Yu39373df2018-09-27 23:41:16 +08002208 if (nwritten)
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002209 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
Chao Yu39373df2018-09-27 23:41:16 +08002210 NULL, 0, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08002211
Chao Yu8f46dca2015-07-14 18:56:10 +08002212 return ret;
2213}
2214
Jaegeuk Kim6d598452018-08-09 17:53:34 -07002215static inline bool __should_serialize_io(struct inode *inode,
2216 struct writeback_control *wbc)
2217{
2218 if (!S_ISREG(inode->i_mode))
2219 return false;
Chao Yud8312722018-09-20 20:05:00 +08002220 if (IS_NOQUOTA(inode))
2221 return false;
Jaegeuk Kim6d598452018-08-09 17:53:34 -07002222 if (wbc->sync_mode != WB_SYNC_ALL)
2223 return true;
2224 if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks)
2225 return true;
2226 return false;
2227}
2228
Chao Yufdd569a2018-05-30 00:20:39 +08002229static int __f2fs_write_data_pages(struct address_space *mapping,
Chao Yuc0fe4882017-08-02 23:21:48 +08002230 struct writeback_control *wbc,
2231 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002232{
2233 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002234 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002235 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002236 int ret;
Jaegeuk Kim6d598452018-08-09 17:53:34 -07002237 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002238
P J Pcfb185a2013-04-03 11:38:00 +09002239 /* deal with chardevs and other special file */
2240 if (!mapping->a_ops->writepage)
2241 return 0;
2242
Chao Yu6a290542015-07-17 18:02:39 +08002243 /* skip writing if there is no dirty page in this inode */
2244 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2245 return 0;
2246
Chao Yu00cc7ed2017-06-29 23:20:45 +08002247 /* during POR, we don't need to trigger writepage at all. */
2248 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2249 goto skip_write;
2250
Chao Yud8312722018-09-20 20:05:00 +08002251 if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) &&
2252 wbc->sync_mode == WB_SYNC_NONE &&
Jaegeuk Kima1257022015-10-08 10:40:07 -07002253 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Chao Yu6b4d6a82018-05-30 00:20:41 +08002254 f2fs_available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kima1257022015-10-08 10:40:07 -07002255 goto skip_write;
2256
Chao Yud323d002015-10-27 09:53:45 +08002257 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07002258 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08002259 goto skip_write;
2260
Yunlei Hed31c7c32016-02-04 16:14:00 +08002261 trace_f2fs_writepages(mapping->host, wbc, DATA);
2262
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002263 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2264 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yudeb78d42018-06-04 23:20:36 +08002265 atomic_inc(&sbi->wb_sync_req[DATA]);
2266 else if (atomic_read(&sbi->wb_sync_req[DATA]))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002267 goto skip_write;
2268
Jaegeuk Kim6d598452018-08-09 17:53:34 -07002269 if (__should_serialize_io(inode, wbc)) {
2270 mutex_lock(&sbi->writepages);
2271 locked = true;
2272 }
2273
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002274 blk_start_plug(&plug);
Chao Yuc0fe4882017-08-02 23:21:48 +08002275 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002276 blk_finish_plug(&plug);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002277
Jaegeuk Kim6d598452018-08-09 17:53:34 -07002278 if (locked)
2279 mutex_unlock(&sbi->writepages);
2280
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002281 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yudeb78d42018-06-04 23:20:36 +08002282 atomic_dec(&sbi->wb_sync_req[DATA]);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07002283 /*
2284 * if some pages were truncated, we cannot guarantee its mapping->host
2285 * to detect pending bios.
2286 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002287
Chao Yu6b4d6a82018-05-30 00:20:41 +08002288 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002289 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002290
2291skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002292 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08002293 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002294 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002295}
2296
Chao Yuc0fe4882017-08-02 23:21:48 +08002297static int f2fs_write_data_pages(struct address_space *mapping,
2298 struct writeback_control *wbc)
2299{
2300 struct inode *inode = mapping->host;
2301
2302 return __f2fs_write_data_pages(mapping, wbc,
2303 F2FS_I(inode)->cp_task == current ?
2304 FS_CP_DATA_IO : FS_DATA_IO);
2305}
2306
Chao Yu3aab8f82014-07-02 13:25:04 +08002307static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2308{
2309 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002310 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002311
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002312 if (to > i_size) {
Chao Yu1d8aac32018-08-05 23:04:25 +08002313 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Jaegeuk Kimf64b4d72018-07-25 12:11:56 +09002314 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu1d8aac32018-08-05 23:04:25 +08002315
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002316 truncate_pagecache(inode, i_size);
Chao Yuc6f78232019-02-02 17:33:01 +08002317 if (!IS_NOQUOTA(inode))
2318 f2fs_truncate_blocks(inode, i_size, true);
Chao Yu1d8aac32018-08-05 23:04:25 +08002319
Qiuyang Sun7b23ea12017-05-18 11:06:45 +08002320 up_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kimf64b4d72018-07-25 12:11:56 +09002321 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Chao Yu3aab8f82014-07-02 13:25:04 +08002322 }
2323}
2324
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002325static int prepare_write_begin(struct f2fs_sb_info *sbi,
2326 struct page *page, loff_t pos, unsigned len,
2327 block_t *blk_addr, bool *node_changed)
2328{
2329 struct inode *inode = page->mapping->host;
2330 pgoff_t index = page->index;
2331 struct dnode_of_data dn;
2332 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002333 bool locked = false;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002334 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002335 int err = 0;
Sheng Yongbcabd032018-11-14 19:34:28 +08002336 int flag;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002337
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002338 /*
2339 * we already allocated all the blocks, so we don't need to get
2340 * the block addresses when there is no need to fill the page.
2341 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002342 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2343 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002344 return 0;
2345
Sheng Yongbcabd032018-11-14 19:34:28 +08002346 /* f2fs_lock_op avoids race between write CP and convert_inline_page */
2347 if (f2fs_has_inline_data(inode) && pos + len > MAX_INLINE_DATA(inode))
2348 flag = F2FS_GET_BLOCK_DEFAULT;
2349 else
2350 flag = F2FS_GET_BLOCK_PRE_AIO;
2351
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002352 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002353 (pos & PAGE_MASK) >= i_size_read(inode)) {
Sheng Yongbcabd032018-11-14 19:34:28 +08002354 __do_map_lock(sbi, flag, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002355 locked = true;
2356 }
2357restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002358 /* check inline_data */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002359 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002360 if (IS_ERR(ipage)) {
2361 err = PTR_ERR(ipage);
2362 goto unlock_out;
2363 }
2364
2365 set_new_dnode(&dn, inode, ipage, ipage, 0);
2366
2367 if (f2fs_has_inline_data(inode)) {
Chao Yue84f88e2017-07-19 00:19:05 +08002368 if (pos + len <= MAX_INLINE_DATA(inode)) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08002369 f2fs_do_read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002370 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08002371 if (inode->i_nlink)
2372 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002373 } else {
2374 err = f2fs_convert_inline_page(&dn, page);
2375 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002376 goto out;
2377 if (dn.data_blkaddr == NULL_ADDR)
2378 err = f2fs_get_block(&dn, index);
2379 }
2380 } else if (locked) {
2381 err = f2fs_get_block(&dn, index);
2382 } else {
2383 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2384 dn.data_blkaddr = ei.blk + index - ei.fofs;
2385 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002386 /* hole case */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002387 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07002388 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002389 f2fs_put_dnode(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002390 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2391 true);
Sheng Yongbcabd032018-11-14 19:34:28 +08002392 WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002393 locked = true;
2394 goto restart;
2395 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002396 }
2397 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002398
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002399 /* convert_inline_page can make node_changed */
2400 *blk_addr = dn.data_blkaddr;
2401 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002402out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002403 f2fs_put_dnode(&dn);
2404unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002405 if (locked)
Sheng Yongbcabd032018-11-14 19:34:28 +08002406 __do_map_lock(sbi, flag, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002407 return err;
2408}
2409
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002410static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2411 loff_t pos, unsigned len, unsigned flags,
2412 struct page **pagep, void **fsdata)
2413{
2414 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002415 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002416 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002417 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yu2a6489c2018-01-15 17:16:46 +08002418 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002419 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002420 int err = 0;
2421
Chao Yu62aed042014-05-06 16:46:04 +08002422 trace_f2fs_write_begin(inode, pos, len, flags);
2423
Daniel Rosenbergf22f93a2018-08-20 19:21:43 -07002424 err = f2fs_is_checkpoint_ready(sbi);
2425 if (err)
2426 goto fail;
2427
Jaegeuk Kim77d13322018-07-27 18:15:11 +09002428 if ((f2fs_is_atomic_file(inode) &&
2429 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
2430 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
Jaegeuk Kim35e13ca2017-10-18 19:05:57 -07002431 err = -ENOMEM;
Chao Yu2a6489c2018-01-15 17:16:46 +08002432 drop_atomic = true;
Jaegeuk Kim35e13ca2017-10-18 19:05:57 -07002433 goto fail;
2434 }
2435
Jaegeuk Kim5f727392014-11-25 10:59:45 -08002436 /*
2437 * We should check this at this moment to avoid deadlock on inode page
2438 * and #0 page. The locking rule for inline_data conversion should be:
2439 * lock_page(page #0) -> lock_page(inode_page)
2440 */
2441 if (index != 0) {
2442 err = f2fs_convert_inline_inode(inode);
2443 if (err)
2444 goto fail;
2445 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002446repeat:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002447 /*
2448 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2449 * wait_for_stable_page. Will wait that below with our IO control.
2450 */
Chao Yu58ddec82017-10-28 16:52:30 +08002451 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002452 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08002453 if (!page) {
2454 err = -ENOMEM;
2455 goto fail;
2456 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09002457
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002458 *pagep = page;
2459
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002460 err = prepare_write_begin(sbi, page, pos, len,
2461 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002462 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002463 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002464
Chao Yud8312722018-09-20 20:05:00 +08002465 if (need_balance && !IS_NOQUOTA(inode) &&
2466 has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002467 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002468 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002469 lock_page(page);
2470 if (page->mapping != mapping) {
2471 /* The page got truncated from under us */
2472 f2fs_put_page(page, 1);
2473 goto repeat;
2474 }
2475 }
2476
Chao Yud9b3df52018-12-25 17:43:42 +08002477 f2fs_wait_on_page_writeback(page, DATA, false, true);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002478
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002479 if (len == PAGE_SIZE || PageUptodate(page))
2480 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002481
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002482 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2483 zero_user_segment(page, len, PAGE_SIZE);
2484 return 0;
2485 }
2486
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002487 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002488 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002489 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002490 } else {
Jaegeuk Kim5a690672017-09-06 21:04:44 -07002491 err = f2fs_submit_page_read(inode, page, blkaddr);
2492 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08002493 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08002494
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002495 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002496 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002497 f2fs_put_page(page, 1);
2498 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002499 }
Chao Yu1563ac72016-07-03 22:05:12 +08002500 if (unlikely(!PageUptodate(page))) {
2501 err = -EIO;
2502 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002503 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002504 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002505 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002506
Chao Yu3aab8f82014-07-02 13:25:04 +08002507fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002508 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002509 f2fs_write_failed(mapping, pos + len);
Chao Yu2a6489c2018-01-15 17:16:46 +08002510 if (drop_atomic)
Chao Yu6b4d6a82018-05-30 00:20:41 +08002511 f2fs_drop_inmem_pages_all(sbi, false);
Chao Yu3aab8f82014-07-02 13:25:04 +08002512 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002513}
2514
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002515static int f2fs_write_end(struct file *file,
2516 struct address_space *mapping,
2517 loff_t pos, unsigned len, unsigned copied,
2518 struct page *page, void *fsdata)
2519{
2520 struct inode *inode = page->mapping->host;
2521
Chao Yudfb2bf32014-05-06 16:47:23 +08002522 trace_f2fs_write_end(inode, pos, len, copied);
2523
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002524 /*
2525 * This should be come from len == PAGE_SIZE, and we expect copied
2526 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2527 * let generic_perform_write() try to copy data again through copied=0.
2528 */
2529 if (!PageUptodate(page)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002530 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002531 copied = 0;
2532 else
2533 SetPageUptodate(page);
2534 }
2535 if (!copied)
2536 goto unlock_out;
2537
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002538 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002539
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002540 if (pos + copied > i_size_read(inode))
2541 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002542unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002543 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002544 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002545 return copied;
2546}
2547
Omar Sandoval6f673762015-03-16 04:33:52 -07002548static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2549 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002550{
Jaegeuk Kim28475252018-06-29 18:55:12 -07002551 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2552 unsigned blkbits = i_blkbits;
2553 unsigned blocksize_mask = (1 << blkbits) - 1;
2554 unsigned long align = offset | iov_iter_alignment(iter);
2555 struct block_device *bdev = inode->i_sb->s_bdev;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002556
Jaegeuk Kim28475252018-06-29 18:55:12 -07002557 if (align & blocksize_mask) {
2558 if (bdev)
2559 blkbits = blksize_bits(bdev_logical_block_size(bdev));
2560 blocksize_mask = (1 << blkbits) - 1;
2561 if (align & blocksize_mask)
2562 return -EINVAL;
2563 return 1;
2564 }
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002565 return 0;
2566}
2567
Chao Yucc32cf32018-11-12 00:46:46 +08002568static void f2fs_dio_end_io(struct bio *bio)
2569{
2570 struct f2fs_private_dio *dio = bio->bi_private;
2571
2572 dec_page_count(F2FS_I_SB(dio->inode),
2573 dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2574
2575 bio->bi_private = dio->orig_private;
2576 bio->bi_end_io = dio->orig_end_io;
2577
Jaegeuk Kim9248dba2018-12-13 18:38:33 -08002578 kvfree(dio);
Chao Yucc32cf32018-11-12 00:46:46 +08002579
2580 bio_endio(bio);
2581}
2582
2583static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
2584 loff_t file_offset)
2585{
2586 struct f2fs_private_dio *dio;
2587 bool write = (bio_op(bio) == REQ_OP_WRITE);
Chao Yucc32cf32018-11-12 00:46:46 +08002588
2589 dio = f2fs_kzalloc(F2FS_I_SB(inode),
2590 sizeof(struct f2fs_private_dio), GFP_NOFS);
YueHaibingc476cbd2019-01-04 01:38:29 +00002591 if (!dio)
Chao Yucc32cf32018-11-12 00:46:46 +08002592 goto out;
Chao Yucc32cf32018-11-12 00:46:46 +08002593
2594 dio->inode = inode;
2595 dio->orig_end_io = bio->bi_end_io;
2596 dio->orig_private = bio->bi_private;
2597 dio->write = write;
2598
2599 bio->bi_end_io = f2fs_dio_end_io;
2600 bio->bi_private = dio;
2601
2602 inc_page_count(F2FS_I_SB(inode),
2603 write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
2604
2605 submit_bio(bio);
2606 return;
2607out:
2608 bio->bi_error = -EIO;
2609 bio_endio(bio);
2610}
2611
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002612static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002613{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002614 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002615 struct inode *inode = mapping->host;
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002616 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yub84f9a72018-09-27 18:34:52 +08002617 struct f2fs_inode_info *fi = F2FS_I(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002618 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002619 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002620 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002621 int err;
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002622 enum rw_hint hint = iocb->ki_hint;
Chao Yue9a50e62018-03-08 14:22:56 +08002623 int whint_mode = F2FS_OPTION(sbi).whint_mode;
Chao Yub84f9a72018-09-27 18:34:52 +08002624 bool do_opu;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002625
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002626 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002627 if (err)
Jaegeuk Kim28475252018-06-29 18:55:12 -07002628 return err < 0 ? err : 0;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002629
Chao Yub84f9a72018-09-27 18:34:52 +08002630 if (f2fs_force_buffered_io(inode, iocb, iter))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002631 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002632
Chao Yub84f9a72018-09-27 18:34:52 +08002633 do_opu = allow_outplace_dio(inode, iocb, iter);
2634
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002635 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002636
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002637 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2638 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2639
Chao Yub84f9a72018-09-27 18:34:52 +08002640 if (iocb->ki_flags & IOCB_NOWAIT) {
2641 if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
Hyunchul Lee355d2342018-03-08 19:34:38 +09002642 iocb->ki_hint = hint;
2643 err = -EAGAIN;
2644 goto out;
2645 }
Chao Yub84f9a72018-09-27 18:34:52 +08002646 if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
2647 up_read(&fi->i_gc_rwsem[rw]);
2648 iocb->ki_hint = hint;
2649 err = -EAGAIN;
2650 goto out;
2651 }
2652 } else {
2653 down_read(&fi->i_gc_rwsem[rw]);
2654 if (do_opu)
2655 down_read(&fi->i_gc_rwsem[READ]);
Hyunchul Lee355d2342018-03-08 19:34:38 +09002656 }
2657
Chao Yucc32cf32018-11-12 00:46:46 +08002658 err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
Chao Yu5b5162d2018-11-13 14:33:45 +08002659 iter, rw == WRITE ? get_data_block_dio_write :
2660 get_data_block_dio, NULL, f2fs_dio_submit_bio,
Chao Yucc32cf32018-11-12 00:46:46 +08002661 DIO_LOCKING | DIO_SKIP_HOLES);
Chao Yub84f9a72018-09-27 18:34:52 +08002662
2663 if (do_opu)
2664 up_read(&fi->i_gc_rwsem[READ]);
2665
2666 up_read(&fi->i_gc_rwsem[rw]);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002667
2668 if (rw == WRITE) {
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002669 if (whint_mode == WHINT_MODE_OFF)
2670 iocb->ki_hint = hint;
Chao Yuc0fe4882017-08-02 23:21:48 +08002671 if (err > 0) {
2672 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2673 err);
Chao Yub84f9a72018-09-27 18:34:52 +08002674 if (!do_opu)
2675 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yuc0fe4882017-08-02 23:21:48 +08002676 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002677 f2fs_write_failed(mapping, offset + count);
Chao Yuc0fe4882017-08-02 23:21:48 +08002678 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002679 }
Chao Yu70407fa2014-07-31 21:11:22 +08002680
Hyunchul Lee355d2342018-03-08 19:34:38 +09002681out:
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002682 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002683
Chao Yu3aab8f82014-07-02 13:25:04 +08002684 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002685}
2686
Chao Yu487261f2015-02-05 17:44:29 +08002687void f2fs_invalidate_page(struct page *page, unsigned int offset,
2688 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002689{
2690 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002691 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002692
Chao Yu487261f2015-02-05 17:44:29 +08002693 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002694 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002695 return;
2696
Chao Yu487261f2015-02-05 17:44:29 +08002697 if (PageDirty(page)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002698 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002699 dec_page_count(sbi, F2FS_DIRTY_META);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002700 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002701 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002702 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002703 inode_dec_dirty_pages(inode);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002704 f2fs_remove_dirty_inode(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002705 }
Chao Yu487261f2015-02-05 17:44:29 +08002706 }
Chao Yudecd36b2015-08-07 18:42:09 +08002707
Chao Yu408bca02018-07-27 18:15:16 +08002708 clear_cold_data(page);
2709
Chao Yudecd36b2015-08-07 18:42:09 +08002710 if (IS_ATOMIC_WRITTEN_PAGE(page))
Chao Yu6b4d6a82018-05-30 00:20:41 +08002711 return f2fs_drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002712
Chao Yuc4e07602019-03-06 17:30:59 +08002713 f2fs_clear_page_private(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002714}
2715
Chao Yu487261f2015-02-05 17:44:29 +08002716int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002717{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002718 /* If this is dirty page, keep PagePrivate */
2719 if (PageDirty(page))
2720 return 0;
2721
Chao Yudecd36b2015-08-07 18:42:09 +08002722 /* This is atomic written page, keep Private */
2723 if (IS_ATOMIC_WRITTEN_PAGE(page))
2724 return 0;
2725
Chao Yu408bca02018-07-27 18:15:16 +08002726 clear_cold_data(page);
Chao Yuc4e07602019-03-06 17:30:59 +08002727 f2fs_clear_page_private(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002728 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002729}
2730
2731static int f2fs_set_data_page_dirty(struct page *page)
2732{
2733 struct address_space *mapping = page->mapping;
2734 struct inode *inode = mapping->host;
2735
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002736 trace_f2fs_set_page_dirty(page, DATA);
2737
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002738 if (!PageUptodate(page))
2739 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002740
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002741 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002742 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08002743 f2fs_register_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002744 return 1;
2745 }
2746 /*
2747 * Previously, this page has been registered, we just
2748 * return here.
2749 */
2750 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002751 }
2752
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002753 if (!PageDirty(page)) {
Jaegeuk Kim95459e92018-04-20 19:29:52 -07002754 __set_page_dirty_nobuffers(page);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002755 f2fs_update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002756 return 1;
2757 }
2758 return 0;
2759}
2760
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002761static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2762{
Chao Yu454ae7e2014-04-22 13:34:01 +08002763 struct inode *inode = mapping->host;
2764
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002765 if (f2fs_has_inline_data(inode))
2766 return 0;
2767
2768 /* make sure allocating whole blocks */
2769 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2770 filemap_write_and_wait(mapping);
2771
Chao Yue2b4e2b2015-08-19 19:11:19 +08002772 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002773}
2774
Weichao Guo5b7a4872016-09-20 05:03:27 +08002775#ifdef CONFIG_MIGRATION
2776#include <linux/migrate.h>
2777
2778int f2fs_migrate_page(struct address_space *mapping,
2779 struct page *newpage, struct page *page, enum migrate_mode mode)
2780{
2781 int rc, extra_count;
2782 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2783 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2784
2785 BUG_ON(PageWriteback(page));
2786
2787 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kim025258e2017-07-06 14:46:01 -07002788 if (atomic_written) {
2789 if (mode != MIGRATE_SYNC)
2790 return -EBUSY;
2791 if (!mutex_trylock(&fi->inmem_lock))
2792 return -EAGAIN;
2793 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002794
Chao Yuc4e07602019-03-06 17:30:59 +08002795 /* one extra reference was held for atomic_write page */
2796 extra_count = atomic_written ? 1 : 0;
Weichao Guo5b7a4872016-09-20 05:03:27 +08002797 rc = migrate_page_move_mapping(mapping, newpage,
2798 page, NULL, mode, extra_count);
2799 if (rc != MIGRATEPAGE_SUCCESS) {
2800 if (atomic_written)
2801 mutex_unlock(&fi->inmem_lock);
2802 return rc;
2803 }
2804
2805 if (atomic_written) {
2806 struct inmem_pages *cur;
2807 list_for_each_entry(cur, &fi->inmem_pages, list)
2808 if (cur->page == page) {
2809 cur->page = newpage;
2810 break;
2811 }
2812 mutex_unlock(&fi->inmem_lock);
2813 put_page(page);
2814 get_page(newpage);
2815 }
2816
Chao Yuc4e07602019-03-06 17:30:59 +08002817 if (PagePrivate(page)) {
2818 f2fs_set_page_private(newpage, page_private(page));
2819 f2fs_clear_page_private(page);
2820 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002821
2822 migrate_page_copy(newpage, page);
2823
2824 return MIGRATEPAGE_SUCCESS;
2825}
2826#endif
2827
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002828const struct address_space_operations f2fs_dblock_aops = {
2829 .readpage = f2fs_read_data_page,
2830 .readpages = f2fs_read_data_pages,
2831 .writepage = f2fs_write_data_page,
2832 .writepages = f2fs_write_data_pages,
2833 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002834 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002835 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002836 .invalidatepage = f2fs_invalidate_page,
2837 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002838 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002839 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002840#ifdef CONFIG_MIGRATION
2841 .migratepage = f2fs_migrate_page,
2842#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002843};
Eric Biggersf69e8142018-04-18 11:09:48 -07002844
Chao Yu6b4d6a82018-05-30 00:20:41 +08002845void f2fs_clear_radix_tree_dirty_tag(struct page *page)
Chao Yubcbcda42018-05-26 18:03:35 +08002846{
2847 struct address_space *mapping = page_mapping(page);
2848 unsigned long flags;
2849
2850 spin_lock_irqsave(&mapping->tree_lock, flags);
2851 radix_tree_tag_clear(&mapping->page_tree, page_index(page),
2852 PAGECACHE_TAG_DIRTY);
2853 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2854}
2855
Eric Biggersf69e8142018-04-18 11:09:48 -07002856int __init f2fs_init_post_read_processing(void)
2857{
2858 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0);
2859 if (!bio_post_read_ctx_cache)
2860 goto fail;
2861 bio_post_read_ctx_pool =
2862 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
2863 bio_post_read_ctx_cache);
2864 if (!bio_post_read_ctx_pool)
2865 goto fail_free_cache;
2866 return 0;
2867
2868fail_free_cache:
2869 kmem_cache_destroy(bio_post_read_ctx_cache);
2870fail:
2871 return -ENOMEM;
2872}
2873
2874void __exit f2fs_destroy_post_read_processing(void)
2875{
2876 mempool_destroy(bio_post_read_ctx_pool);
2877 kmem_cache_destroy(bio_post_read_ctx_cache);
2878}