blob: dce1f2ac9703e827911135ed9fc1a2a13d5df927 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002 * fs/f2fs/data.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/buffer_head.h>
14#include <linux/mpage.h>
15#include <linux/writeback.h>
16#include <linux/backing-dev.h>
Chao Yu8f46dca2015-07-14 18:56:10 +080017#include <linux/pagevec.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090018#include <linux/blkdev.h>
19#include <linux/bio.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010020#include <linux/prefetch.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080021#include <linux/uio.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070022#include <linux/cleancache.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090023
24#include "f2fs.h"
25#include "node.h"
26#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080027#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090028#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090029
Eric Biggersf69e8142018-04-18 11:09:48 -070030#define NUM_PREALLOC_POST_READ_CTXS 128
31
32static struct kmem_cache *bio_post_read_ctx_cache;
33static mempool_t *bio_post_read_ctx_pool;
34
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070035static bool __is_cp_guaranteed(struct page *page)
36{
37 struct address_space *mapping = page->mapping;
38 struct inode *inode;
39 struct f2fs_sb_info *sbi;
40
41 if (!mapping)
42 return false;
43
44 inode = mapping->host;
45 sbi = F2FS_I_SB(inode);
46
47 if (inode->i_ino == F2FS_META_INO(sbi) ||
48 inode->i_ino == F2FS_NODE_INO(sbi) ||
49 S_ISDIR(inode->i_mode) ||
Chao Yua301e762018-05-08 14:06:03 +080050 (S_ISREG(inode->i_mode) &&
51 is_inode_flag_set(inode, FI_ATOMIC_FILE)) ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070052 is_cold_data(page))
53 return true;
54 return false;
55}
56
Eric Biggersf69e8142018-04-18 11:09:48 -070057/* postprocessing steps for read bios */
58enum bio_post_read_step {
59 STEP_INITIAL = 0,
60 STEP_DECRYPT,
61};
62
63struct bio_post_read_ctx {
64 struct bio *bio;
65 struct work_struct work;
66 unsigned int cur_step;
67 unsigned int enabled_steps;
68};
69
70static void __read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090071{
Eric Biggersf69e8142018-04-18 11:09:48 -070072 struct page *page;
73 struct bio_vec *bv;
Linus Torvaldsf5688492014-01-30 11:19:05 -080074 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090075
Eric Biggersf69e8142018-04-18 11:09:48 -070076 bio_for_each_segment_all(bv, bio, i) {
77 page = bv->bv_page;
78
79 /* PG_error was set if any post_read step failed */
80 if (bio->bi_error || PageError(page)) {
81 ClearPageUptodate(page);
82 SetPageError(page);
83 } else {
84 SetPageUptodate(page);
85 }
86 unlock_page(page);
87 }
88 if (bio->bi_private)
89 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
90 bio_put(bio);
91}
92
93static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
94
95static void decrypt_work(struct work_struct *work)
96{
97 struct bio_post_read_ctx *ctx =
98 container_of(work, struct bio_post_read_ctx, work);
99
100 fscrypt_decrypt_bio(ctx->bio);
101
102 bio_post_read_processing(ctx);
103}
104
105static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
106{
107 switch (++ctx->cur_step) {
108 case STEP_DECRYPT:
109 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
110 INIT_WORK(&ctx->work, decrypt_work);
111 fscrypt_enqueue_decrypt_work(&ctx->work);
112 return;
113 }
114 ctx->cur_step++;
115 /* fall-through */
116 default:
117 __read_end_io(ctx->bio);
118 }
119}
120
121static bool f2fs_bio_post_read_required(struct bio *bio)
122{
123 return bio->bi_private && !bio->bi_error;
124}
125
126static void f2fs_read_end_io(struct bio *bio)
127{
Chao Yu8b038c72016-09-18 23:30:07 +0800128#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700129 if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_IO)) {
130 f2fs_show_injection_info(FAULT_IO);
Chao Yu8b038c72016-09-18 23:30:07 +0800131 bio->bi_error = -EIO;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700132 }
Chao Yu8b038c72016-09-18 23:30:07 +0800133#endif
134
Eric Biggersf69e8142018-04-18 11:09:48 -0700135 if (f2fs_bio_post_read_required(bio)) {
136 struct bio_post_read_ctx *ctx = bio->bi_private;
137
138 ctx->cur_step = STEP_INITIAL;
139 bio_post_read_processing(ctx);
140 return;
Chao Yu12377022015-05-25 18:03:38 +0800141 }
142
Eric Biggersf69e8142018-04-18 11:09:48 -0700143 __read_end_io(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900144}
145
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200146static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900147{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +0900148 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -0800149 struct bio_vec *bvec;
150 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900151
Linus Torvaldsf5688492014-01-30 11:19:05 -0800152 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900153 struct page *page = bvec->bv_page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700154 enum count_type type = WB_DATA_TYPE(page);
155
156 if (IS_DUMMY_WRITTEN_PAGE(page)) {
157 set_page_private(page, (unsigned long)NULL);
158 ClearPagePrivate(page);
159 unlock_page(page);
160 mempool_free(page, sbi->write_io_dummy);
161
162 if (unlikely(bio->bi_error))
163 f2fs_stop_checkpoint(sbi, true);
164 continue;
165 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900166
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700167 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700168
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200169 if (unlikely(bio->bi_error)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700170 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kime2709762017-12-31 16:26:38 -0800171 if (type == F2FS_WB_CP_DATA)
172 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900173 }
Yunlei He92b8f9c2018-01-11 14:19:32 +0800174
175 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
176 page->index != nid_of_node(page));
177
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700178 dec_page_count(sbi, type);
179 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900180 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800181 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700182 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700183 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900184 wake_up(&sbi->cp_wait);
185
186 bio_put(bio);
187}
188
Gu Zheng940a6d32013-12-20 17:39:59 +0800189/*
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700190 * Return true, if pre_bio's bdev is same as its target device.
191 */
192struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
193 block_t blk_addr, struct bio *bio)
194{
195 struct block_device *bdev = sbi->sb->s_bdev;
196 int i;
197
198 for (i = 0; i < sbi->s_ndevs; i++) {
199 if (FDEV(i).start_blk <= blk_addr &&
200 FDEV(i).end_blk >= blk_addr) {
201 blk_addr -= FDEV(i).start_blk;
202 bdev = FDEV(i).bdev;
203 break;
204 }
205 }
206 if (bio) {
207 bio->bi_bdev = bdev;
208 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
209 }
210 return bdev;
211}
212
213int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
214{
215 int i;
216
217 for (i = 0; i < sbi->s_ndevs; i++)
218 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
219 return i;
220 return 0;
221}
222
223static bool __same_bdev(struct f2fs_sb_info *sbi,
224 block_t blk_addr, struct bio *bio)
225{
226 return f2fs_target_device(sbi, blk_addr, NULL) == bio->bi_bdev;
227}
228
229/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800230 * Low-level block read/write IO operations.
231 */
232static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800233 struct writeback_control *wbc,
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900234 int npages, bool is_read,
235 enum page_type type, enum temp_type temp)
Gu Zheng940a6d32013-12-20 17:39:59 +0800236{
237 struct bio *bio;
238
Chao Yub5db2de2017-10-28 16:52:31 +0800239 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800240
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700241 f2fs_target_device(sbi, blk_addr, bio);
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900242 if (is_read) {
243 bio->bi_end_io = f2fs_read_end_io;
244 bio->bi_private = NULL;
245 } else {
246 bio->bi_end_io = f2fs_write_end_io;
247 bio->bi_private = sbi;
Chao Yu6b4d6a82018-05-30 00:20:41 +0800248 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, type, temp);
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900249 }
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800250 if (wbc)
251 wbc_init_bio(wbc, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800252
253 return bio;
254}
255
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700256static inline void __submit_bio(struct f2fs_sb_info *sbi,
257 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700258{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700259 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700260 unsigned int start;
261
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700262 if (type != DATA && type != NODE)
263 goto submit_io;
264
Yunlong Song94e1c6a2018-07-12 23:09:28 +0800265 if (test_opt(sbi, LFS) && current->plug)
Tiezhu Yang4d8e4a82018-02-06 08:21:45 +0800266 blk_finish_plug(current->plug);
267
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700268 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
269 start %= F2FS_IO_SIZE(sbi);
270
271 if (start == 0)
272 goto submit_io;
273
274 /* fill dummy pages */
275 for (; start < F2FS_IO_SIZE(sbi); start++) {
276 struct page *page =
277 mempool_alloc(sbi->write_io_dummy,
278 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
279 f2fs_bug_on(sbi, !page);
280
281 SetPagePrivate(page);
282 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
283 lock_page(page);
284 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
285 f2fs_bug_on(sbi, 1);
286 }
287 /*
288 * In the NODE case, we lose next block address chain. So, we
289 * need to do checkpoint in f2fs_sync_file.
290 */
291 if (type == NODE)
292 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700293 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700294submit_io:
295 if (is_read_io(bio_op(bio)))
296 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
297 else
298 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500299 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700300}
301
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900302static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900303{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900304 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900305
306 if (!io->bio)
307 return;
308
Mike Christie04d328d2016-06-05 14:31:55 -0500309 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
310
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700311 if (is_read_io(fio->op))
312 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
313 else
314 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
315
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700316 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900317 io->bio = NULL;
318}
319
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700320static bool __has_merged_page(struct f2fs_bio_info *io,
321 struct inode *inode, nid_t ino, pgoff_t idx)
Chao Yu0fd785e2016-01-18 18:24:59 +0800322{
Chao Yu0fd785e2016-01-18 18:24:59 +0800323 struct bio_vec *bvec;
324 struct page *target;
325 int i;
326
Chao Yu0c3a5792016-01-18 18:28:11 +0800327 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800328 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800329
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700330 if (!inode && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800331 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800332
333 bio_for_each_segment_all(bvec, io->bio, i) {
334
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700335 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800336 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700337 else
338 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800339
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700340 if (idx != target->index)
341 continue;
342
Chao Yu0c3a5792016-01-18 18:28:11 +0800343 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800344 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800345 if (ino && ino == ino_of_node(target))
346 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800347 }
348
Chao Yu0fd785e2016-01-18 18:24:59 +0800349 return false;
350}
351
Chao Yu0c3a5792016-01-18 18:28:11 +0800352static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700353 nid_t ino, pgoff_t idx, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800354{
355 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700356 enum temp_type temp;
357 struct f2fs_bio_info *io;
358 bool ret = false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800359
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700360 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
361 io = sbi->write_io[btype] + temp;
362
363 down_read(&io->io_rwsem);
364 ret = __has_merged_page(io, inode, ino, idx);
365 up_read(&io->io_rwsem);
366
367 /* TODO: use HOT temp only for meta pages now. */
368 if (ret || btype == META)
369 break;
370 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800371 return ret;
372}
373
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700374static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700375 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900376{
377 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700378 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900379
Chao Yudf0f8dc2014-03-22 14:57:23 +0800380 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900381
382 /* change META to META_FLUSH in the checkpoint procedure */
383 if (type >= META_FLUSH) {
384 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500385 io->fio.op = REQ_OP_WRITE;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700386 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
387 if (!test_opt(sbi, NOBARRIER))
388 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900389 }
390 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800391 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900392}
393
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700394static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
395 struct inode *inode, nid_t ino, pgoff_t idx,
396 enum page_type type, bool force)
397{
398 enum temp_type temp;
399
400 if (!force && !has_merged_page(sbi, inode, ino, idx, type))
401 return;
402
403 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
404
405 __f2fs_submit_merged_write(sbi, type, temp);
406
407 /* TODO: use HOT temp only for meta pages now. */
408 if (type >= META)
409 break;
410 }
411}
412
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700413void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800414{
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700415 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
Chao Yu0c3a5792016-01-18 18:28:11 +0800416}
417
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700418void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700419 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700420 enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800421{
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700422 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
Chao Yu0c3a5792016-01-18 18:28:11 +0800423}
424
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700425void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800426{
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700427 f2fs_submit_merged_write(sbi, DATA);
428 f2fs_submit_merged_write(sbi, NODE);
429 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800430}
431
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900432/*
433 * Fill the locked page with data located in the block address.
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700434 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900435 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700436int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900437{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900438 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700439 struct page *page = fio->encrypted_page ?
440 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900441
Chao Yub3c869a2018-08-01 19:13:44 +0800442 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
443 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
444 return -EFAULT;
445
Chao Yu2ace38e2014-12-24 16:08:14 +0800446 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700447 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900448
449 /* Allocate a new bio */
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800450 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900451 1, is_read_io(fio->op), fio->type, fio->temp);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900452
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300453 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900454 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900455 return -EFAULT;
456 }
Mike Christie04d328d2016-06-05 14:31:55 -0500457 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900458
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700459 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700460
461 if (!is_read_io(fio->op))
462 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900463 return 0;
464}
465
Chao Yu34880e02018-05-28 23:47:18 +0800466void f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900467{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700468 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900469 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700470 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700471 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900472
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700473 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900474
Chao Yuc52dc0f2017-05-19 23:37:01 +0800475 down_write(&io->io_rwsem);
476next:
477 if (fio->in_list) {
478 spin_lock(&io->io_lock);
479 if (list_empty(&io->io_list)) {
480 spin_unlock(&io->io_lock);
Chao Yu34880e02018-05-28 23:47:18 +0800481 goto out;
Chao Yuc52dc0f2017-05-19 23:37:01 +0800482 }
483 fio = list_first_entry(&io->io_list,
484 struct f2fs_io_info, list);
485 list_del(&fio->list);
486 spin_unlock(&io->io_lock);
487 }
488
Chao Yud05e8712018-06-05 17:44:11 +0800489 if (__is_valid_data_blkaddr(fio->old_blkaddr))
Yunlei Hea3f8ec82018-03-08 16:29:13 +0800490 verify_block_addr(fio, fio->old_blkaddr);
491 verify_block_addr(fio, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900492
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700493 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
494
Thomas Meyer59504672017-10-07 16:02:21 +0200495 /* set submitted = true as a return value */
496 fio->submitted = true;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700497
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700498 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700499
Chao Yu7a9d7542016-02-22 18:36:38 +0800500 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700501 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
502 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900503 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900504alloc_new:
505 if (io->bio == NULL) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700506 if ((fio->type == DATA || fio->type == NODE) &&
507 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700508 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Chao Yu34880e02018-05-28 23:47:18 +0800509 fio->retry = true;
510 goto skip;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700511 }
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800512 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900513 BIO_MAX_PAGES, false,
514 fio->type, fio->temp);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900515 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900516 }
517
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700518 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900519 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900520 goto alloc_new;
521 }
522
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800523 if (fio->io_wbc)
524 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
525
Chao Yu7a9d7542016-02-22 18:36:38 +0800526 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700527 f2fs_trace_ios(fio, 0);
Chao Yuc52dc0f2017-05-19 23:37:01 +0800528
529 trace_f2fs_submit_page_write(fio->page, fio);
Chao Yu34880e02018-05-28 23:47:18 +0800530skip:
Chao Yuc52dc0f2017-05-19 23:37:01 +0800531 if (fio->in_list)
532 goto next;
Chao Yu34880e02018-05-28 23:47:18 +0800533out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800534 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900535}
536
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700537static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -0700538 unsigned nr_pages, unsigned op_flag)
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700539{
540 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700541 struct bio *bio;
Eric Biggersf69e8142018-04-18 11:09:48 -0700542 struct bio_post_read_ctx *ctx;
543 unsigned int post_read_steps = 0;
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700544
Eric Biggersf69e8142018-04-18 11:09:48 -0700545 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
546 if (!bio)
547 return ERR_PTR(-ENOMEM);
548 f2fs_target_device(sbi, blkaddr, bio);
549 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -0700550 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
Eric Biggersf69e8142018-04-18 11:09:48 -0700551
552 if (f2fs_encrypted_file(inode))
553 post_read_steps |= 1 << STEP_DECRYPT;
554 if (post_read_steps) {
555 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
556 if (!ctx) {
557 bio_put(bio);
558 return ERR_PTR(-ENOMEM);
559 }
560 ctx->bio = bio;
561 ctx->enabled_steps = post_read_steps;
562 bio->bi_private = ctx;
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700563
564 /* wait the page to be moved by cleaning */
565 f2fs_wait_on_block_writeback(sbi, blkaddr);
566 }
567
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700568 return bio;
569}
570
571/* This can handle encryption stuffs */
572static int f2fs_submit_page_read(struct inode *inode, struct page *page,
573 block_t blkaddr)
574{
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -0700575 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0);
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700576
577 if (IS_ERR(bio))
578 return PTR_ERR(bio);
579
580 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
581 bio_put(bio);
582 return -EFAULT;
583 }
584 __submit_bio(F2FS_I_SB(inode), bio, DATA);
585 return 0;
586}
587
Chao Yu46008c62016-05-09 19:56:30 +0800588static void __set_data_blkaddr(struct dnode_of_data *dn)
589{
590 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
591 __le32 *addr_array;
Chao Yufbcf9312017-07-19 00:19:06 +0800592 int base = 0;
593
594 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
595 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800596
597 /* Get physical address of data block */
598 addr_array = blkaddr_in_node(rn);
Chao Yufbcf9312017-07-19 00:19:06 +0800599 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800600}
601
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900602/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900603 * Lock ordering for the change of data block address:
604 * ->data_page
605 * ->node_page
606 * update block addresses in the node page
607 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800608void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900609{
Chao Yu46008c62016-05-09 19:56:30 +0800610 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
611 __set_data_blkaddr(dn);
612 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800613 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900614}
615
Chao Yuf28b3432016-02-24 17:16:47 +0800616void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
617{
618 dn->data_blkaddr = blkaddr;
Chao Yu6b4d6a82018-05-30 00:20:41 +0800619 f2fs_set_data_blkaddr(dn);
Chao Yuf28b3432016-02-24 17:16:47 +0800620 f2fs_update_extent_cache(dn);
621}
622
Chao Yu46008c62016-05-09 19:56:30 +0800623/* dn->ofs_in_node will be returned with up-to-date last block pointer */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800624int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900625{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700626 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu09c3a722017-07-09 00:13:07 +0800627 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900628
Chao Yu46008c62016-05-09 19:56:30 +0800629 if (!count)
630 return 0;
631
Jaegeuk Kim91942322016-05-20 10:13:22 -0700632 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900633 return -EPERM;
Chao Yu09c3a722017-07-09 00:13:07 +0800634 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
635 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900636
Chao Yu46008c62016-05-09 19:56:30 +0800637 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
638 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900639
Chao Yu46008c62016-05-09 19:56:30 +0800640 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
641
642 for (; count > 0; dn->ofs_in_node++) {
Chao Yufbcf9312017-07-19 00:19:06 +0800643 block_t blkaddr = datablock_addr(dn->inode,
644 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800645 if (blkaddr == NULL_ADDR) {
646 dn->data_blkaddr = NEW_ADDR;
647 __set_data_blkaddr(dn);
648 count--;
649 }
650 }
651
652 if (set_page_dirty(dn->node_page))
653 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900654 return 0;
655}
656
Chao Yu46008c62016-05-09 19:56:30 +0800657/* Should keep dn->ofs_in_node unchanged */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800658int f2fs_reserve_new_block(struct dnode_of_data *dn)
Chao Yu46008c62016-05-09 19:56:30 +0800659{
660 unsigned int ofs_in_node = dn->ofs_in_node;
661 int ret;
662
Chao Yu6b4d6a82018-05-30 00:20:41 +0800663 ret = f2fs_reserve_new_blocks(dn, 1);
Chao Yu46008c62016-05-09 19:56:30 +0800664 dn->ofs_in_node = ofs_in_node;
665 return ret;
666}
667
Huajun Lib6009652013-11-10 23:13:18 +0800668int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
669{
670 bool need_put = dn->inode_page ? false : true;
671 int err;
672
Chao Yu6b4d6a82018-05-30 00:20:41 +0800673 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
Huajun Lib6009652013-11-10 23:13:18 +0800674 if (err)
675 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900676
Huajun Lib6009652013-11-10 23:13:18 +0800677 if (dn->data_blkaddr == NULL_ADDR)
Chao Yu6b4d6a82018-05-30 00:20:41 +0800678 err = f2fs_reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900679 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800680 f2fs_put_dnode(dn);
681 return err;
682}
683
Fan Li759af1c2015-08-05 15:52:16 +0800684int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900685{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700686 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800687 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800688
Fan Li759af1c2015-08-05 15:52:16 +0800689 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
690 dn->data_blkaddr = ei.blk + index - ei.fofs;
691 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800692 }
693
Fan Li759af1c2015-08-05 15:52:16 +0800694 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900695}
696
Chao Yu6b4d6a82018-05-30 00:20:41 +0800697struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500698 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900699{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900700 struct address_space *mapping = inode->i_mapping;
701 struct dnode_of_data dn;
702 struct page *page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700703 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900704 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700705
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700706 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900707 if (!page)
708 return ERR_PTR(-ENOMEM);
709
Chao Yucb3bc9e2015-02-05 18:03:40 +0800710 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
711 dn.data_blkaddr = ei.blk + index - ei.fofs;
712 goto got_it;
713 }
714
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900715 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800716 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700717 if (err)
718 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900719 f2fs_put_dnode(&dn);
720
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900721 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700722 err = -ENOENT;
723 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900724 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800725got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700726 if (PageUptodate(page)) {
727 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900728 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700729 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900730
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900731 /*
732 * A new dentry page is allocated but not able to be written, since its
733 * new inode page couldn't be allocated due to -ENOSPC.
734 * In such the case, its blkaddr can be remained as NEW_ADDR.
Chao Yu6b4d6a82018-05-30 00:20:41 +0800735 * see, f2fs_add_link -> f2fs_get_new_data_page ->
736 * f2fs_init_inode_metadata.
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900737 */
738 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300739 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700740 if (!PageUptodate(page))
741 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700742 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900743 return page;
744 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900745
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700746 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900747 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700748 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700749 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700750
751put_err:
752 f2fs_put_page(page, 1);
753 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700754}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900755
Chao Yu6b4d6a82018-05-30 00:20:41 +0800756struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700757{
758 struct address_space *mapping = inode->i_mapping;
759 struct page *page;
760
761 page = find_get_page(mapping, index);
762 if (page && PageUptodate(page))
763 return page;
764 f2fs_put_page(page, 0);
765
Chao Yu6b4d6a82018-05-30 00:20:41 +0800766 page = f2fs_get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700767 if (IS_ERR(page))
768 return page;
769
770 if (PageUptodate(page))
771 return page;
772
773 wait_on_page_locked(page);
774 if (unlikely(!PageUptodate(page))) {
775 f2fs_put_page(page, 0);
776 return ERR_PTR(-EIO);
777 }
778 return page;
779}
780
781/*
782 * If it tries to access a hole, return an error.
783 * Because, the callers, functions in dir.c and GC, should be able to know
784 * whether this page exists or not.
785 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800786struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700787 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700788{
789 struct address_space *mapping = inode->i_mapping;
790 struct page *page;
791repeat:
Chao Yu6b4d6a82018-05-30 00:20:41 +0800792 page = f2fs_get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700793 if (IS_ERR(page))
794 return page;
795
796 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900797 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900798 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900799 f2fs_put_page(page, 1);
800 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900801 }
Chao Yu1563ac72016-07-03 22:05:12 +0800802 if (unlikely(!PageUptodate(page))) {
803 f2fs_put_page(page, 1);
804 return ERR_PTR(-EIO);
805 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900806 return page;
807}
808
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900809/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900810 * Caller ensures that this data page is never allocated.
811 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900812 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800813 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
814 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800815 * Note that, ipage is set only by make_empty_dir, and if any error occur,
816 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900817 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800818struct page *f2fs_get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900819 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900820{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900821 struct address_space *mapping = inode->i_mapping;
822 struct page *page;
823 struct dnode_of_data dn;
824 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800825
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700826 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800827 if (!page) {
828 /*
829 * before exiting, we should make sure ipage will be released
830 * if any error occur.
831 */
832 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700833 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800834 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900835
Jaegeuk Kima8865372013-12-27 17:04:17 +0900836 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800837 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700838 if (err) {
839 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900840 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900841 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700842 if (!ipage)
843 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900844
845 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700846 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900847
848 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300849 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700850 if (!PageUptodate(page))
851 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900852 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700853 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900854
Jaegeuk Kim76121182016-01-01 22:03:47 -0800855 /* if ipage exists, blkaddr should be NEW_ADDR */
856 f2fs_bug_on(F2FS_I_SB(inode), ipage);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800857 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700858 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800859 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900860 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700861got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800862 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700863 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700864 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900865 return page;
866}
867
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900868static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900869{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700870 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900871 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900872 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700873 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800874 blkcnt_t count = 1;
Chao Yu09c3a722017-07-09 00:13:07 +0800875 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900876
Jaegeuk Kim91942322016-05-20 10:13:22 -0700877 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900878 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800879
Chao Yu3c743262018-07-17 00:02:17 +0800880 err = f2fs_get_node_info(sbi, dn->nid, &ni);
881 if (err)
882 return err;
883
Chao Yufbcf9312017-07-19 00:19:06 +0800884 dn->data_blkaddr = datablock_addr(dn->inode,
885 dn->node_page, dn->ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800886 if (dn->data_blkaddr == NEW_ADDR)
887 goto alloc;
888
Chao Yu09c3a722017-07-09 00:13:07 +0800889 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
890 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900891
Chao Yudf6136e2015-03-23 10:33:37 +0800892alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900893 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
894
Chao Yu6b4d6a82018-05-30 00:20:41 +0800895 f2fs_allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900896 &sum, seg_type, NULL, false);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800897 f2fs_set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900898
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700899 /* update i_size */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800900 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700901 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300902 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700903 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300904 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900905 return 0;
906}
907
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700908int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800909{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800910 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800911 struct f2fs_map_blocks map;
Chao Yu8fc74462018-01-03 17:30:19 +0800912 int flag;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700913 int err = 0;
Chao Yu8fc74462018-01-03 17:30:19 +0800914 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700915
Weichao Guo8456d342017-09-29 22:43:23 +0800916 /* convert inline data for Direct I/O*/
Chao Yu8fc74462018-01-03 17:30:19 +0800917 if (direct_io) {
Weichao Guo8456d342017-09-29 22:43:23 +0800918 err = f2fs_convert_inline_inode(inode);
919 if (err)
920 return err;
921 }
922
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700923 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
924 return 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800925
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700926 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800927 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
928 if (map.m_len > map.m_lblk)
929 map.m_len -= map.m_lblk;
930 else
931 map.m_len = 0;
932
Chao Yuda859852016-01-26 15:42:58 +0800933 map.m_next_pgofs = NULL;
Chao Yu6bce9632018-01-11 14:42:30 +0800934 map.m_next_extent = NULL;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900935 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800936
Chao Yu8fc74462018-01-03 17:30:19 +0800937 if (direct_io) {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800938 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
Hyunchul Lee355d2342018-03-08 19:34:38 +0900939 flag = f2fs_force_buffered_io(inode, WRITE) ?
Chao Yu8fc74462018-01-03 17:30:19 +0800940 F2FS_GET_BLOCK_PRE_AIO :
941 F2FS_GET_BLOCK_PRE_DIO;
942 goto map_blocks;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900943 }
Chao Yue84f88e2017-07-19 00:19:05 +0800944 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700945 err = f2fs_convert_inline_inode(inode);
946 if (err)
947 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800948 }
Chao Yu8fc74462018-01-03 17:30:19 +0800949 if (f2fs_has_inline_data(inode))
Sheng Yong76b6e8e2017-11-22 18:23:39 +0800950 return err;
Chao Yu8fc74462018-01-03 17:30:19 +0800951
952 flag = F2FS_GET_BLOCK_PRE_AIO;
953
954map_blocks:
955 err = f2fs_map_blocks(inode, &map, 1, flag);
956 if (map.m_len > 0 && err == -ENOSPC) {
957 if (!direct_io)
958 set_inode_flag(inode, FI_NO_PREALLOC);
959 err = 0;
Sheng Yong76b6e8e2017-11-22 18:23:39 +0800960 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700961 return err;
962}
963
964static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
965{
966 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
967 if (lock)
968 down_read(&sbi->node_change);
969 else
970 up_read(&sbi->node_change);
971 } else {
972 if (lock)
973 f2fs_lock_op(sbi);
974 else
975 f2fs_unlock_op(sbi);
976 }
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800977}
978
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900979/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700980 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
981 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800982 * If original data blocks are allocated, then give them to blockdev.
983 * Otherwise,
984 * a. preallocate requested block addresses
985 * b. do not use extent cache for better performance
986 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900987 */
Chao Yud323d002015-10-27 09:53:45 +0800988int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800989 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900990{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700991 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900992 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800993 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700994 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800995 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900996 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800997 unsigned int ofs_in_node, last_ofs_in_node;
998 blkcnt_t prealloc;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700999 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +08001000 block_t blkaddr;
Chao Yu6bce9632018-01-11 14:42:30 +08001001 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001002
Chao Yudfd02e42016-08-20 15:12:01 +08001003 if (!maxblocks)
1004 return 0;
1005
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001006 map->m_len = 0;
1007 map->m_flags = 0;
1008
1009 /* it only supports block size == page size */
1010 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +08001011 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001012
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001013 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001014 map->m_pblk = ei.blk + pgofs - ei.fofs;
1015 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1016 map->m_flags = F2FS_MAP_MAPPED;
Chao Yu6bce9632018-01-11 14:42:30 +08001017 if (map->m_next_extent)
1018 *map->m_next_extent = pgofs + map->m_len;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001019 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001020 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001021
Chao Yu4fe71e82016-01-26 15:37:38 +08001022next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001023 if (create)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001024 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001025
1026 /* When reading holes, we need its node page */
1027 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001028 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001029 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +08001030 if (flag == F2FS_GET_BLOCK_BMAP)
1031 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +08001032 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001033 err = 0;
Chao Yuda859852016-01-26 15:42:58 +08001034 if (map->m_next_pgofs)
1035 *map->m_next_pgofs =
Chao Yu6b4d6a82018-05-30 00:20:41 +08001036 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu6bce9632018-01-11 14:42:30 +08001037 if (map->m_next_extent)
1038 *map->m_next_extent =
Chao Yu6b4d6a82018-05-30 00:20:41 +08001039 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +08001040 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001041 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001042 }
Chao Yu973163f2015-09-18 16:51:51 +08001043
Chao Yu6bce9632018-01-11 14:42:30 +08001044 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +08001045 prealloc = 0;
Arnd Bergmanna299abd2016-11-02 14:52:15 +01001046 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +08001047 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001048
Chao Yu4fe71e82016-01-26 15:37:38 +08001049next_block:
Chao Yufbcf9312017-07-19 00:19:06 +08001050 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +08001051
Chao Yub3c869a2018-08-01 19:13:44 +08001052 if (__is_valid_data_blkaddr(blkaddr) &&
1053 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
1054 err = -EFAULT;
1055 goto sync_out;
1056 }
1057
Chao Yud05e8712018-06-05 17:44:11 +08001058 if (!is_valid_data_blkaddr(sbi, blkaddr)) {
Fan Li7df3a432015-12-17 13:20:59 +08001059 if (create) {
1060 if (unlikely(f2fs_cp_error(sbi))) {
1061 err = -EIO;
1062 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +08001063 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001064 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +08001065 if (blkaddr == NULL_ADDR) {
1066 prealloc++;
1067 last_ofs_in_node = dn.ofs_in_node;
1068 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001069 } else {
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001070 err = __allocate_data_block(&dn,
1071 map->m_seg_type);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001072 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001073 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001074 }
Fan Li7df3a432015-12-17 13:20:59 +08001075 if (err)
1076 goto sync_out;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001077 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001078 blkaddr = dn.data_blkaddr;
1079 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001080 if (flag == F2FS_GET_BLOCK_BMAP) {
1081 map->m_pblk = 0;
1082 goto sync_out;
1083 }
Chao Yu6bce9632018-01-11 14:42:30 +08001084 if (flag == F2FS_GET_BLOCK_PRECACHE)
1085 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001086 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1087 blkaddr == NULL_ADDR) {
1088 if (map->m_next_pgofs)
1089 *map->m_next_pgofs = pgofs + 1;
Chao Yu987892c2017-11-30 19:28:22 +08001090 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001091 }
Chao Yufd214422018-01-10 18:18:52 +08001092 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1093 /* for defragment case */
1094 if (map->m_next_pgofs)
1095 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001096 goto sync_out;
Chao Yufd214422018-01-10 18:18:52 +08001097 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001098 }
1099 }
Fan Li7df3a432015-12-17 13:20:59 +08001100
Chao Yu46008c62016-05-09 19:56:30 +08001101 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1102 goto skip;
1103
Chao Yu4fe71e82016-01-26 15:37:38 +08001104 if (map->m_len == 0) {
1105 /* preallocated unwritten block should be mapped for fiemap. */
1106 if (blkaddr == NEW_ADDR)
1107 map->m_flags |= F2FS_MAP_UNWRITTEN;
1108 map->m_flags |= F2FS_MAP_MAPPED;
1109
1110 map->m_pblk = blkaddr;
1111 map->m_len = 1;
1112 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001113 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001114 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001115 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001116 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001117 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001118 } else {
1119 goto sync_out;
1120 }
1121
Chao Yu46008c62016-05-09 19:56:30 +08001122skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001123 dn.ofs_in_node++;
1124 pgofs++;
1125
Chao Yu46008c62016-05-09 19:56:30 +08001126 /* preallocate blocks in batch for one dnode page */
1127 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1128 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001129
Chao Yu46008c62016-05-09 19:56:30 +08001130 dn.ofs_in_node = ofs_in_node;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001131 err = f2fs_reserve_new_blocks(&dn, prealloc);
Chao Yu46008c62016-05-09 19:56:30 +08001132 if (err)
1133 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001134
Chao Yu46008c62016-05-09 19:56:30 +08001135 map->m_len += dn.ofs_in_node - ofs_in_node;
1136 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1137 err = -ENOSPC;
1138 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001139 }
Chao Yu46008c62016-05-09 19:56:30 +08001140 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001141 }
1142
Chao Yu46008c62016-05-09 19:56:30 +08001143 if (pgofs >= end)
1144 goto sync_out;
1145 else if (dn.ofs_in_node < end_offset)
1146 goto next_block;
1147
Chao Yu6bce9632018-01-11 14:42:30 +08001148 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1149 if (map->m_flags & F2FS_MAP_MAPPED) {
1150 unsigned int ofs = start_pgofs - map->m_lblk;
1151
1152 f2fs_update_extent_cache_range(&dn,
1153 start_pgofs, map->m_pblk + ofs,
1154 map->m_len - ofs);
1155 }
1156 }
1157
Chao Yu46008c62016-05-09 19:56:30 +08001158 f2fs_put_dnode(&dn);
1159
1160 if (create) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001161 __do_map_lock(sbi, flag, false);
1162 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001163 }
Chao Yu46008c62016-05-09 19:56:30 +08001164 goto next_dnode;
1165
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001166sync_out:
Chao Yu6bce9632018-01-11 14:42:30 +08001167 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1168 if (map->m_flags & F2FS_MAP_MAPPED) {
1169 unsigned int ofs = start_pgofs - map->m_lblk;
1170
1171 f2fs_update_extent_cache_range(&dn,
1172 start_pgofs, map->m_pblk + ofs,
1173 map->m_len - ofs);
1174 }
1175 if (map->m_next_extent)
1176 *map->m_next_extent = pgofs + 1;
1177 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001178 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001179unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001180 if (create) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001181 __do_map_lock(sbi, flag, false);
1182 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001183 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001184out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001185 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001186 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001187}
1188
Hyunchul Lee355d2342018-03-08 19:34:38 +09001189bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1190{
1191 struct f2fs_map_blocks map;
1192 block_t last_lblk;
1193 int err;
1194
1195 if (pos + len > i_size_read(inode))
1196 return false;
1197
1198 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1199 map.m_next_pgofs = NULL;
1200 map.m_next_extent = NULL;
1201 map.m_seg_type = NO_CHECK_TYPE;
1202 last_lblk = F2FS_BLK_ALIGN(pos + len);
1203
1204 while (map.m_lblk < last_lblk) {
1205 map.m_len = last_lblk - map.m_lblk;
1206 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1207 if (err || map.m_len == 0)
1208 return false;
1209 map.m_lblk += map.m_len;
1210 }
1211 return true;
1212}
1213
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001214static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001215 struct buffer_head *bh, int create, int flag,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001216 pgoff_t *next_pgofs, int seg_type)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001217{
1218 struct f2fs_map_blocks map;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001219 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001220
1221 map.m_lblk = iblock;
1222 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001223 map.m_next_pgofs = next_pgofs;
Chao Yu6bce9632018-01-11 14:42:30 +08001224 map.m_next_extent = NULL;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001225 map.m_seg_type = seg_type;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001226
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001227 err = f2fs_map_blocks(inode, &map, create, flag);
1228 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001229 map_bh(bh, inode->i_sb, map.m_pblk);
1230 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001231 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001232 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001233 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001234}
1235
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001236static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001237 struct buffer_head *bh_result, int create, int flag,
1238 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001239{
Chao Yuda859852016-01-26 15:42:58 +08001240 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001241 flag, next_pgofs,
1242 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001243}
1244
Chao Yue2b4e2b2015-08-19 19:11:19 +08001245static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001246 struct buffer_head *bh_result, int create)
1247{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001248 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001249 F2FS_GET_BLOCK_DEFAULT, NULL,
Chao Yu6b4d6a82018-05-30 00:20:41 +08001250 f2fs_rw_hint_to_seg_type(
Hyunchul Leebcdc5712018-01-31 11:36:57 +09001251 inode->i_write_hint));
Chao Yue2b4e2b2015-08-19 19:11:19 +08001252}
1253
1254static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1255 struct buffer_head *bh_result, int create)
1256{
Yunlei He179448b2015-12-28 21:48:32 +08001257 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001258 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001259 return -EFBIG;
1260
Chao Yue2b4e2b2015-08-19 19:11:19 +08001261 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001262 F2FS_GET_BLOCK_BMAP, NULL,
1263 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001264}
1265
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001266static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1267{
1268 return (offset >> inode->i_blkbits);
1269}
1270
1271static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1272{
1273 return (blk << inode->i_blkbits);
1274}
1275
Chao Yu08ddb192018-01-11 14:39:57 +08001276static int f2fs_xattr_fiemap(struct inode *inode,
1277 struct fiemap_extent_info *fieinfo)
1278{
1279 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1280 struct page *page;
1281 struct node_info ni;
1282 __u64 phys = 0, len;
1283 __u32 flags;
1284 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1285 int err = 0;
1286
1287 if (f2fs_has_inline_xattr(inode)) {
1288 int offset;
1289
1290 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1291 inode->i_ino, false);
1292 if (!page)
1293 return -ENOMEM;
1294
Chao Yu3c743262018-07-17 00:02:17 +08001295 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1296 if (err) {
1297 f2fs_put_page(page, 1);
1298 return err;
1299 }
Chao Yu08ddb192018-01-11 14:39:57 +08001300
1301 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1302 offset = offsetof(struct f2fs_inode, i_addr) +
1303 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yu3aa46e22018-01-17 16:31:36 +08001304 get_inline_xattr_addrs(inode));
Chao Yu08ddb192018-01-11 14:39:57 +08001305
1306 phys += offset;
1307 len = inline_xattr_size(inode);
1308
1309 f2fs_put_page(page, 1);
1310
1311 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1312
1313 if (!xnid)
1314 flags |= FIEMAP_EXTENT_LAST;
1315
1316 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1317 if (err || err == 1)
1318 return err;
1319 }
1320
1321 if (xnid) {
1322 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1323 if (!page)
1324 return -ENOMEM;
1325
Chao Yu3c743262018-07-17 00:02:17 +08001326 err = f2fs_get_node_info(sbi, xnid, &ni);
1327 if (err) {
1328 f2fs_put_page(page, 1);
1329 return err;
1330 }
Chao Yu08ddb192018-01-11 14:39:57 +08001331
1332 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1333 len = inode->i_sb->s_blocksize;
1334
1335 f2fs_put_page(page, 1);
1336
1337 flags = FIEMAP_EXTENT_LAST;
1338 }
1339
1340 if (phys)
1341 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1342
1343 return (err < 0 ? err : 0);
1344}
1345
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001346int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1347 u64 start, u64 len)
1348{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001349 struct buffer_head map_bh;
1350 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001351 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001352 u64 logical = 0, phys = 0, size = 0;
1353 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001354 int ret = 0;
1355
Chao Yu6bce9632018-01-11 14:42:30 +08001356 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1357 ret = f2fs_precache_extents(inode);
1358 if (ret)
1359 return ret;
1360 }
1361
Chao Yu08ddb192018-01-11 14:39:57 +08001362 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001363 if (ret)
1364 return ret;
1365
Chao Yuaa9c1c12018-01-11 14:37:35 +08001366 inode_lock(inode);
1367
Chao Yu08ddb192018-01-11 14:39:57 +08001368 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1369 ret = f2fs_xattr_fiemap(inode, fieinfo);
1370 goto out;
1371 }
1372
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -07001373 if (f2fs_has_inline_data(inode)) {
1374 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1375 if (ret != -EAGAIN)
Chao Yuaa9c1c12018-01-11 14:37:35 +08001376 goto out;
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -07001377 }
1378
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001379 if (logical_to_blk(inode, len) == 0)
1380 len = blk_to_logical(inode, 1);
1381
1382 start_blk = logical_to_blk(inode, start);
1383 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001384
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001385next:
1386 memset(&map_bh, 0, sizeof(struct buffer_head));
1387 map_bh.b_size = len;
1388
Chao Yue2b4e2b2015-08-19 19:11:19 +08001389 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001390 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001391 if (ret)
1392 goto out;
1393
1394 /* HOLE */
1395 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001396 start_blk = next_pgofs;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001397
1398 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1399 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001400 goto prep_next;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001401
Fan Li9a950d52015-12-26 18:07:41 +08001402 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001403 }
Fan Li9a950d52015-12-26 18:07:41 +08001404
Chao Yuda5af122016-01-08 20:19:27 +08001405 if (size) {
1406 if (f2fs_encrypted_inode(inode))
1407 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1408
Fan Li9a950d52015-12-26 18:07:41 +08001409 ret = fiemap_fill_next_extent(fieinfo, logical,
1410 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001411 }
Fan Li9a950d52015-12-26 18:07:41 +08001412
1413 if (start_blk > last_blk || ret)
1414 goto out;
1415
1416 logical = blk_to_logical(inode, start_blk);
1417 phys = blk_to_logical(inode, map_bh.b_blocknr);
1418 size = map_bh.b_size;
1419 flags = 0;
1420 if (buffer_unwritten(&map_bh))
1421 flags = FIEMAP_EXTENT_UNWRITTEN;
1422
1423 start_blk += logical_to_blk(inode, size);
1424
1425prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001426 cond_resched();
1427 if (fatal_signal_pending(current))
1428 ret = -EINTR;
1429 else
1430 goto next;
1431out:
1432 if (ret == 1)
1433 ret = 0;
1434
Al Viro59551022016-01-22 15:40:57 -05001435 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001436 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001437}
1438
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001439/*
1440 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1441 * Major change was from block_size == page_size in f2fs by default.
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001442 *
1443 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1444 * this function ever deviates from doing just read-ahead, it should either
1445 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1446 * from read-ahead.
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001447 */
1448static int f2fs_mpage_readpages(struct address_space *mapping,
1449 struct list_head *pages, struct page *page,
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001450 unsigned nr_pages, bool is_readahead)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001451{
1452 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001453 sector_t last_block_in_bio = 0;
1454 struct inode *inode = mapping->host;
1455 const unsigned blkbits = inode->i_blkbits;
1456 const unsigned blocksize = 1 << blkbits;
1457 sector_t block_in_file;
1458 sector_t last_block;
1459 sector_t last_block_in_file;
1460 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001461 struct f2fs_map_blocks map;
1462
1463 map.m_pblk = 0;
1464 map.m_lblk = 0;
1465 map.m_len = 0;
1466 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001467 map.m_next_pgofs = NULL;
Chao Yu6bce9632018-01-11 14:42:30 +08001468 map.m_next_extent = NULL;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001469 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001470
LiFanba4556c2017-11-25 11:46:18 +08001471 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001472 if (pages) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001473 page = list_last_entry(pages, struct page, lru);
1474
1475 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001476 list_del(&page->lru);
1477 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001478 page->index,
1479 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001480 goto next_page;
1481 }
1482
1483 block_in_file = (sector_t)page->index;
1484 last_block = block_in_file + nr_pages;
1485 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1486 blkbits;
1487 if (last_block > last_block_in_file)
1488 last_block = last_block_in_file;
1489
1490 /*
1491 * Map blocks using the previous result first.
1492 */
1493 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1494 block_in_file > map.m_lblk &&
1495 block_in_file < (map.m_lblk + map.m_len))
1496 goto got_it;
1497
1498 /*
1499 * Then do more f2fs_map_blocks() calls until we are
1500 * done with this page.
1501 */
1502 map.m_flags = 0;
1503
1504 if (block_in_file < last_block) {
1505 map.m_lblk = block_in_file;
1506 map.m_len = last_block - block_in_file;
1507
Chao Yu46c9e142015-09-18 16:54:16 +08001508 if (f2fs_map_blocks(inode, &map, 0,
Qiuyang Sun9afa1592017-08-09 17:27:30 +08001509 F2FS_GET_BLOCK_DEFAULT))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001510 goto set_error_page;
1511 }
1512got_it:
1513 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1514 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1515 SetPageMappedToDisk(page);
1516
1517 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1518 SetPageUptodate(page);
1519 goto confused;
1520 }
Chao Yub3c869a2018-08-01 19:13:44 +08001521
1522 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1523 DATA_GENERIC))
1524 goto set_error_page;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001525 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001526 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001527 if (!PageUptodate(page))
1528 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001529 unlock_page(page);
1530 goto next_page;
1531 }
1532
1533 /*
1534 * This page will go to BIO. Do we need to send this
1535 * BIO off first?
1536 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001537 if (bio && (last_block_in_bio != block_nr - 1 ||
1538 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001539submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001540 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001541 bio = NULL;
1542 }
1543 if (bio == NULL) {
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001544 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
1545 is_readahead ? REQ_RAHEAD : 0);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001546 if (IS_ERR(bio)) {
1547 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001548 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001549 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001550 }
1551
1552 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1553 goto submit_and_realloc;
1554
1555 last_block_in_bio = block_nr;
1556 goto next_page;
1557set_error_page:
1558 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001559 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001560 unlock_page(page);
1561 goto next_page;
1562confused:
1563 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001564 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001565 bio = NULL;
1566 }
1567 unlock_page(page);
1568next_page:
1569 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001570 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001571 }
1572 BUG_ON(pages && !list_empty(pages));
1573 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001574 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001575 return 0;
1576}
1577
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001578static int f2fs_read_data_page(struct file *file, struct page *page)
1579{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001580 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001581 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001582
Chao Yuc20e89c2014-05-06 16:53:08 +08001583 trace_f2fs_readpage(page, DATA);
1584
arter97e1c42042014-08-06 23:22:50 +09001585 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001586 if (f2fs_has_inline_data(inode))
1587 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001588 if (ret == -EAGAIN)
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001589 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1, false);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001590 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001591}
1592
1593static int f2fs_read_data_pages(struct file *file,
1594 struct address_space *mapping,
1595 struct list_head *pages, unsigned nr_pages)
1596{
Hsiang Kao3f01af72017-09-24 02:45:42 +08001597 struct inode *inode = mapping->host;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001598 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001599
1600 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001601
1602 /* If the file has inline data, skip readpages */
1603 if (f2fs_has_inline_data(inode))
1604 return 0;
1605
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001606 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001607}
1608
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001609static int encrypt_one_page(struct f2fs_io_info *fio)
1610{
1611 struct inode *inode = fio->page->mapping->host;
1612 gfp_t gfp_flags = GFP_NOFS;
1613
Jaegeuk Kima4fb1892017-09-05 16:54:24 -07001614 if (!f2fs_encrypted_file(inode))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001615 return 0;
1616
Eric Biggersf69e8142018-04-18 11:09:48 -07001617 /* wait for GCed page writeback via META_MAPPING */
Jaegeuk Kimfb605d02017-09-05 17:04:35 -07001618 f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001619
1620retry_encrypt:
1621 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1622 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1623 if (!IS_ERR(fio->encrypted_page))
1624 return 0;
1625
1626 /* flush pending IOs and wait for a while in the ENOMEM case */
1627 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001628 f2fs_flush_merged_writes(fio->sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001629 congestion_wait(BLK_RW_ASYNC, HZ/50);
1630 gfp_flags |= __GFP_NOFAIL;
1631 goto retry_encrypt;
1632 }
1633 return PTR_ERR(fio->encrypted_page);
1634}
1635
Chao Yu66356ee2018-01-17 16:31:38 +08001636static inline bool check_inplace_update_policy(struct inode *inode,
1637 struct f2fs_io_info *fio)
1638{
1639 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1640 unsigned int policy = SM_I(sbi)->ipu_policy;
1641
1642 if (policy & (0x1 << F2FS_IPU_FORCE))
1643 return true;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001644 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
Chao Yu66356ee2018-01-17 16:31:38 +08001645 return true;
1646 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1647 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1648 return true;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001649 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
Chao Yu66356ee2018-01-17 16:31:38 +08001650 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1651 return true;
1652
1653 /*
1654 * IPU for rewrite async pages
1655 */
1656 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1657 fio && fio->op == REQ_OP_WRITE &&
1658 !(fio->op_flags & REQ_SYNC) &&
1659 !f2fs_encrypted_inode(inode))
1660 return true;
1661
1662 /* this is only set during fdatasync */
1663 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1664 is_inode_flag_set(inode, FI_NEED_IPU))
1665 return true;
1666
1667 return false;
1668}
1669
Chao Yu6b4d6a82018-05-30 00:20:41 +08001670bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yu66356ee2018-01-17 16:31:38 +08001671{
1672 if (f2fs_is_pinned_file(inode))
1673 return true;
1674
1675 /* if this is cold file, we should overwrite to avoid fragmentation */
1676 if (file_is_cold(inode))
1677 return true;
1678
1679 return check_inplace_update_policy(inode, fio);
1680}
1681
Chao Yu6b4d6a82018-05-30 00:20:41 +08001682bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yu66356ee2018-01-17 16:31:38 +08001683{
1684 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1685
1686 if (test_opt(sbi, LFS))
1687 return true;
1688 if (S_ISDIR(inode->i_mode))
1689 return true;
1690 if (f2fs_is_atomic_file(inode))
1691 return true;
1692 if (fio) {
1693 if (is_cold_data(fio->page))
1694 return true;
1695 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1696 return true;
1697 }
1698 return false;
1699}
1700
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001701static inline bool need_inplace_update(struct f2fs_io_info *fio)
1702{
1703 struct inode *inode = fio->page->mapping->host;
1704
Chao Yu6b4d6a82018-05-30 00:20:41 +08001705 if (f2fs_should_update_outplace(inode, fio))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001706 return false;
1707
Chao Yu6b4d6a82018-05-30 00:20:41 +08001708 return f2fs_should_update_inplace(inode, fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001709}
1710
Chao Yu6b4d6a82018-05-30 00:20:41 +08001711int f2fs_do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001712{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001713 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001714 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001715 struct dnode_of_data dn;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001716 struct extent_info ei = {0,0,0};
Chao Yu3c743262018-07-17 00:02:17 +08001717 struct node_info ni;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001718 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001719 int err = 0;
1720
1721 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001722 if (need_inplace_update(fio) &&
1723 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1724 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
1725
Chao Yub3c869a2018-08-01 19:13:44 +08001726 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1727 DATA_GENERIC))
1728 return -EFAULT;
1729
1730 ipu_force = true;
1731 fio->need_lock = LOCK_DONE;
1732 goto got_it;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001733 }
1734
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07001735 /* Deadlock due to between page->lock and f2fs_lock_op */
1736 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1737 return -EAGAIN;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001738
Chao Yu6b4d6a82018-05-30 00:20:41 +08001739 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001740 if (err)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001741 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001742
Chao Yu28bc1062016-02-06 14:40:34 +08001743 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001744
1745 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001746 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001747 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001748 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001749 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001750got_it:
Chao Yub3c869a2018-08-01 19:13:44 +08001751 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
1752 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1753 DATA_GENERIC)) {
1754 err = -EFAULT;
1755 goto out_writepage;
1756 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001757 /*
1758 * If current allocation needs SSR,
1759 * it had better in-place writes for updated data.
1760 */
Chao Yud05e8712018-06-05 17:44:11 +08001761 if (ipu_force || (is_valid_data_blkaddr(fio->sbi, fio->old_blkaddr) &&
Chao Yu158d9bb2018-05-23 22:25:08 +08001762 need_inplace_update(fio))) {
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001763 err = encrypt_one_page(fio);
1764 if (err)
1765 goto out_writepage;
1766
1767 set_page_writeback(page);
Jaegeuk Kim926b3702018-04-11 23:09:04 -07001768 ClearPageError(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001769 f2fs_put_dnode(&dn);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001770 if (fio->need_lock == LOCK_REQ)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001771 f2fs_unlock_op(fio->sbi);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001772 err = f2fs_inplace_write_data(fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001773 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001774 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001775 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001776 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001777
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001778 if (fio->need_lock == LOCK_RETRY) {
1779 if (!f2fs_trylock_op(fio->sbi)) {
1780 err = -EAGAIN;
1781 goto out_writepage;
1782 }
1783 fio->need_lock = LOCK_REQ;
1784 }
1785
Chao Yu3c743262018-07-17 00:02:17 +08001786 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
1787 if (err)
1788 goto out_writepage;
1789
1790 fio->version = ni.version;
1791
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001792 err = encrypt_one_page(fio);
1793 if (err)
1794 goto out_writepage;
1795
1796 set_page_writeback(page);
Jaegeuk Kim926b3702018-04-11 23:09:04 -07001797 ClearPageError(page);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001798
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001799 /* LFS mode write path */
Chao Yu6b4d6a82018-05-30 00:20:41 +08001800 f2fs_outplace_write_data(&dn, fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001801 trace_f2fs_do_write_data_page(page, OPU);
1802 set_inode_flag(inode, FI_APPEND_WRITE);
1803 if (page->index == 0)
1804 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001805out_writepage:
1806 f2fs_put_dnode(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001807out:
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001808 if (fio->need_lock == LOCK_REQ)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001809 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001810 return err;
1811}
1812
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001813static int __write_data_page(struct page *page, bool *submitted,
Chao Yuc0fe4882017-08-02 23:21:48 +08001814 struct writeback_control *wbc,
1815 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001816{
1817 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001818 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001819 loff_t i_size = i_size_read(inode);
1820 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001821 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001822 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001823 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001824 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001825 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001826 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001827 .sbi = sbi,
Chao Yuf014be82017-09-29 13:59:38 +08001828 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001829 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001830 .op = REQ_OP_WRITE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001831 .op_flags = wbc_to_write_flags(wbc),
1832 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001833 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001834 .encrypted_page = NULL,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001835 .submitted = false,
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001836 .need_lock = LOCK_RETRY,
Chao Yuc0fe4882017-08-02 23:21:48 +08001837 .io_type = io_type,
Yufen Yucbd5e5a2018-01-09 19:33:39 +08001838 .io_wbc = wbc,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001839 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001840
Chao Yuecda0de2014-05-06 16:48:26 +08001841 trace_f2fs_writepage(page, DATA);
1842
Chao Yu86f78c12018-01-18 17:29:10 +08001843 /* we should bypass data pages to proceed the kworkder jobs */
1844 if (unlikely(f2fs_cp_error(sbi))) {
1845 mapping_set_error(page->mapping, -EIO);
Chao Yu9780d682018-05-28 16:59:26 +08001846 /*
1847 * don't drop any dirty dentry pages for keeping lastest
1848 * directory structure.
1849 */
1850 if (S_ISDIR(inode->i_mode))
1851 goto redirty_out;
Chao Yu86f78c12018-01-18 17:29:10 +08001852 goto out;
1853 }
1854
Chao Yu00cc7ed2017-06-29 23:20:45 +08001855 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1856 goto redirty_out;
1857
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001858 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001859 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001860
1861 /*
1862 * If the offset is out-of-range of file size,
1863 * this page does not have to be written to disk.
1864 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001865 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001866 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001867 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001868
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001869 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001870write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001871 if (f2fs_is_drop_cache(inode))
1872 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001873 /* we should not write 0'th page having journal header */
1874 if (f2fs_is_volatile_file(inode) && (!page->index ||
1875 (!wbc->for_reclaim &&
Chao Yu6b4d6a82018-05-30 00:20:41 +08001876 f2fs_available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001877 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001878
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001879 /* Dentry blocks are controlled by checkpoint */
1880 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001881 fio.need_lock = LOCK_DONE;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001882 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001883 goto done;
1884 }
1885
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001886 if (!wbc->for_reclaim)
1887 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001888 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001889 goto redirty_out;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001890 else
1891 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001892
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001893 err = -EAGAIN;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001894 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001895 err = f2fs_write_inline_data(inode, page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001896 if (!err)
1897 goto out;
1898 }
1899
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001900 if (err == -EAGAIN) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08001901 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001902 if (err == -EAGAIN) {
1903 fio.need_lock = LOCK_REQ;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001904 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001905 }
1906 }
Chao Yu33fdebb2017-10-09 17:55:19 +08001907
Chao Yu5912fba2018-01-17 16:31:37 +08001908 if (err) {
1909 file_set_keep_isize(inode);
1910 } else {
1911 down_write(&F2FS_I(inode)->i_sem);
1912 if (F2FS_I(inode)->last_disk_size < psize)
1913 F2FS_I(inode)->last_disk_size = psize;
1914 up_write(&F2FS_I(inode)->i_sem);
1915 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001916
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001917done:
1918 if (err && err != -ENOENT)
1919 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001920
Jaegeuk Kim39936832012-11-22 16:21:29 +09001921out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001922 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001923 if (err)
1924 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001925
1926 if (wbc->for_reclaim) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001927 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001928 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001929 f2fs_remove_dirty_inode(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001930 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001931 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001932
1933 unlock_page(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001934 if (!S_ISDIR(inode->i_mode))
1935 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001936
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001937 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001938 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001939 submitted = NULL;
1940 }
1941
1942 if (submitted)
1943 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001944
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001945 return 0;
1946
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001947redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001948 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim5b37ebb2018-05-03 23:26:02 -07001949 /*
1950 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
1951 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
1952 * file_write_and_wait_range() will see EIO error, which is critical
1953 * to return value of fsync() followed by atomic_write failure to user.
1954 */
1955 if (!err || wbc->for_reclaim)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001956 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001957 unlock_page(page);
1958 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001959}
1960
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001961static int f2fs_write_data_page(struct page *page,
1962 struct writeback_control *wbc)
1963{
Chao Yuc0fe4882017-08-02 23:21:48 +08001964 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001965}
1966
Chao Yu8f46dca2015-07-14 18:56:10 +08001967/*
1968 * This function was copied from write_cche_pages from mm/page-writeback.c.
1969 * The major change is making write step of cold data page separately from
1970 * warm/hot data page.
1971 */
1972static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yuc0fe4882017-08-02 23:21:48 +08001973 struct writeback_control *wbc,
1974 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08001975{
1976 int ret = 0;
1977 int done = 0;
1978 struct pagevec pvec;
Chao Yudeb78d42018-06-04 23:20:36 +08001979 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Chao Yu8f46dca2015-07-14 18:56:10 +08001980 int nr_pages;
1981 pgoff_t uninitialized_var(writeback_index);
1982 pgoff_t index;
1983 pgoff_t end; /* Inclusive */
1984 pgoff_t done_index;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001985 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001986 int cycled;
1987 int range_whole = 0;
1988 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001989
1990 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001991
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001992 if (get_dirty_pages(mapping->host) <=
1993 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1994 set_inode_flag(mapping->host, FI_HOT_DATA);
1995 else
1996 clear_inode_flag(mapping->host, FI_HOT_DATA);
1997
Chao Yu8f46dca2015-07-14 18:56:10 +08001998 if (wbc->range_cyclic) {
1999 writeback_index = mapping->writeback_index; /* prev offset */
2000 index = writeback_index;
2001 if (index == 0)
2002 cycled = 1;
2003 else
2004 cycled = 0;
2005 end = -1;
2006 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002007 index = wbc->range_start >> PAGE_SHIFT;
2008 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08002009 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2010 range_whole = 1;
2011 cycled = 1; /* ignore range_cyclic tests */
2012 }
2013 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2014 tag = PAGECACHE_TAG_TOWRITE;
2015 else
2016 tag = PAGECACHE_TAG_DIRTY;
2017retry:
2018 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2019 tag_pages_for_writeback(mapping, index, end);
2020 done_index = index;
2021 while (!done && (index <= end)) {
2022 int i;
2023
Jan Karae0673652017-11-15 17:34:48 -08002024 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara4d93a432017-11-15 17:35:19 -08002025 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08002026 if (nr_pages == 0)
2027 break;
2028
2029 for (i = 0; i < nr_pages; i++) {
2030 struct page *page = pvec.pages[i];
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002031 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08002032
Chao Yu458e47f2018-05-23 22:25:09 +08002033 /* give a priority to WB_SYNC threads */
Chao Yudeb78d42018-06-04 23:20:36 +08002034 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
Chao Yu458e47f2018-05-23 22:25:09 +08002035 wbc->sync_mode == WB_SYNC_NONE) {
2036 done = 1;
2037 break;
2038 }
2039
Chao Yu8f46dca2015-07-14 18:56:10 +08002040 done_index = page->index;
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07002041retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08002042 lock_page(page);
2043
2044 if (unlikely(page->mapping != mapping)) {
2045continue_unlock:
2046 unlock_page(page);
2047 continue;
2048 }
2049
2050 if (!PageDirty(page)) {
2051 /* someone wrote it for us */
2052 goto continue_unlock;
2053 }
2054
Chao Yu8f46dca2015-07-14 18:56:10 +08002055 if (PageWriteback(page)) {
2056 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002057 f2fs_wait_on_page_writeback(page,
2058 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08002059 else
2060 goto continue_unlock;
2061 }
2062
2063 BUG_ON(PageWriteback(page));
2064 if (!clear_page_dirty_for_io(page))
2065 goto continue_unlock;
2066
Chao Yuc0fe4882017-08-02 23:21:48 +08002067 ret = __write_data_page(page, &submitted, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08002068 if (unlikely(ret)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002069 /*
2070 * keep nr_to_write, since vfs uses this to
2071 * get # of written pages.
2072 */
2073 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2074 unlock_page(page);
2075 ret = 0;
2076 continue;
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07002077 } else if (ret == -EAGAIN) {
2078 ret = 0;
2079 if (wbc->sync_mode == WB_SYNC_ALL) {
2080 cond_resched();
2081 congestion_wait(BLK_RW_ASYNC,
2082 HZ/50);
2083 goto retry_write;
2084 }
2085 continue;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002086 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002087 done_index = page->index + 1;
2088 done = 1;
2089 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002090 } else if (submitted) {
2091 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08002092 }
2093
Chao Yu458e47f2018-05-23 22:25:09 +08002094 if (--wbc->nr_to_write <= 0 &&
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002095 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08002096 done = 1;
2097 break;
2098 }
2099 }
2100 pagevec_release(&pvec);
2101 cond_resched();
2102 }
2103
Chao Yu8f46dca2015-07-14 18:56:10 +08002104 if (!cycled && !done) {
2105 cycled = 1;
2106 index = 0;
2107 end = writeback_index - 1;
2108 goto retry;
2109 }
2110 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2111 mapping->writeback_index = done_index;
2112
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002113 if (last_idx != ULONG_MAX)
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002114 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
2115 0, last_idx, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08002116
Chao Yu8f46dca2015-07-14 18:56:10 +08002117 return ret;
2118}
2119
Chao Yufdd569a2018-05-30 00:20:39 +08002120static int __f2fs_write_data_pages(struct address_space *mapping,
Chao Yuc0fe4882017-08-02 23:21:48 +08002121 struct writeback_control *wbc,
2122 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002123{
2124 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002125 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002126 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002127 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002128
P J Pcfb185a2013-04-03 11:38:00 +09002129 /* deal with chardevs and other special file */
2130 if (!mapping->a_ops->writepage)
2131 return 0;
2132
Chao Yu6a290542015-07-17 18:02:39 +08002133 /* skip writing if there is no dirty page in this inode */
2134 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2135 return 0;
2136
Chao Yu00cc7ed2017-06-29 23:20:45 +08002137 /* during POR, we don't need to trigger writepage at all. */
2138 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2139 goto skip_write;
2140
Jaegeuk Kima1257022015-10-08 10:40:07 -07002141 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
2142 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Chao Yu6b4d6a82018-05-30 00:20:41 +08002143 f2fs_available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kima1257022015-10-08 10:40:07 -07002144 goto skip_write;
2145
Chao Yud323d002015-10-27 09:53:45 +08002146 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07002147 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08002148 goto skip_write;
2149
Yunlei Hed31c7c32016-02-04 16:14:00 +08002150 trace_f2fs_writepages(mapping->host, wbc, DATA);
2151
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002152 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2153 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yudeb78d42018-06-04 23:20:36 +08002154 atomic_inc(&sbi->wb_sync_req[DATA]);
2155 else if (atomic_read(&sbi->wb_sync_req[DATA]))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002156 goto skip_write;
2157
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002158 blk_start_plug(&plug);
Chao Yuc0fe4882017-08-02 23:21:48 +08002159 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002160 blk_finish_plug(&plug);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002161
2162 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yudeb78d42018-06-04 23:20:36 +08002163 atomic_dec(&sbi->wb_sync_req[DATA]);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07002164 /*
2165 * if some pages were truncated, we cannot guarantee its mapping->host
2166 * to detect pending bios.
2167 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002168
Chao Yu6b4d6a82018-05-30 00:20:41 +08002169 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002170 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002171
2172skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002173 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08002174 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002175 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002176}
2177
Chao Yuc0fe4882017-08-02 23:21:48 +08002178static int f2fs_write_data_pages(struct address_space *mapping,
2179 struct writeback_control *wbc)
2180{
2181 struct inode *inode = mapping->host;
2182
2183 return __f2fs_write_data_pages(mapping, wbc,
2184 F2FS_I(inode)->cp_task == current ?
2185 FS_CP_DATA_IO : FS_DATA_IO);
2186}
2187
Chao Yu3aab8f82014-07-02 13:25:04 +08002188static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2189{
2190 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002191 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002192
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002193 if (to > i_size) {
Qiuyang Sun7b23ea12017-05-18 11:06:45 +08002194 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002195 truncate_pagecache(inode, i_size);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002196 f2fs_truncate_blocks(inode, i_size, true);
Qiuyang Sun7b23ea12017-05-18 11:06:45 +08002197 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3aab8f82014-07-02 13:25:04 +08002198 }
2199}
2200
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002201static int prepare_write_begin(struct f2fs_sb_info *sbi,
2202 struct page *page, loff_t pos, unsigned len,
2203 block_t *blk_addr, bool *node_changed)
2204{
2205 struct inode *inode = page->mapping->host;
2206 pgoff_t index = page->index;
2207 struct dnode_of_data dn;
2208 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002209 bool locked = false;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002210 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002211 int err = 0;
2212
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002213 /*
2214 * we already allocated all the blocks, so we don't need to get
2215 * the block addresses when there is no need to fill the page.
2216 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002217 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2218 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002219 return 0;
2220
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002221 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002222 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002223 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002224 locked = true;
2225 }
2226restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002227 /* check inline_data */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002228 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002229 if (IS_ERR(ipage)) {
2230 err = PTR_ERR(ipage);
2231 goto unlock_out;
2232 }
2233
2234 set_new_dnode(&dn, inode, ipage, ipage, 0);
2235
2236 if (f2fs_has_inline_data(inode)) {
Chao Yue84f88e2017-07-19 00:19:05 +08002237 if (pos + len <= MAX_INLINE_DATA(inode)) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08002238 f2fs_do_read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002239 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08002240 if (inode->i_nlink)
2241 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002242 } else {
2243 err = f2fs_convert_inline_page(&dn, page);
2244 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002245 goto out;
2246 if (dn.data_blkaddr == NULL_ADDR)
2247 err = f2fs_get_block(&dn, index);
2248 }
2249 } else if (locked) {
2250 err = f2fs_get_block(&dn, index);
2251 } else {
2252 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2253 dn.data_blkaddr = ei.blk + index - ei.fofs;
2254 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002255 /* hole case */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002256 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07002257 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002258 f2fs_put_dnode(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002259 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2260 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002261 locked = true;
2262 goto restart;
2263 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002264 }
2265 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002266
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002267 /* convert_inline_page can make node_changed */
2268 *blk_addr = dn.data_blkaddr;
2269 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002270out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002271 f2fs_put_dnode(&dn);
2272unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002273 if (locked)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002274 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002275 return err;
2276}
2277
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002278static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2279 loff_t pos, unsigned len, unsigned flags,
2280 struct page **pagep, void **fsdata)
2281{
2282 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002283 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002284 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002285 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yu2a6489c2018-01-15 17:16:46 +08002286 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002287 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002288 int err = 0;
2289
Chao Yu62aed042014-05-06 16:46:04 +08002290 trace_f2fs_write_begin(inode, pos, len, flags);
2291
Jaegeuk Kim35e13ca2017-10-18 19:05:57 -07002292 if (f2fs_is_atomic_file(inode) &&
Chao Yu6b4d6a82018-05-30 00:20:41 +08002293 !f2fs_available_free_memory(sbi, INMEM_PAGES)) {
Jaegeuk Kim35e13ca2017-10-18 19:05:57 -07002294 err = -ENOMEM;
Chao Yu2a6489c2018-01-15 17:16:46 +08002295 drop_atomic = true;
Jaegeuk Kim35e13ca2017-10-18 19:05:57 -07002296 goto fail;
2297 }
2298
Jaegeuk Kim5f727392014-11-25 10:59:45 -08002299 /*
2300 * We should check this at this moment to avoid deadlock on inode page
2301 * and #0 page. The locking rule for inline_data conversion should be:
2302 * lock_page(page #0) -> lock_page(inode_page)
2303 */
2304 if (index != 0) {
2305 err = f2fs_convert_inline_inode(inode);
2306 if (err)
2307 goto fail;
2308 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002309repeat:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002310 /*
2311 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2312 * wait_for_stable_page. Will wait that below with our IO control.
2313 */
Chao Yu58ddec82017-10-28 16:52:30 +08002314 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002315 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08002316 if (!page) {
2317 err = -ENOMEM;
2318 goto fail;
2319 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09002320
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002321 *pagep = page;
2322
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002323 err = prepare_write_begin(sbi, page, pos, len,
2324 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002325 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002326 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002327
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002328 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002329 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002330 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002331 lock_page(page);
2332 if (page->mapping != mapping) {
2333 /* The page got truncated from under us */
2334 f2fs_put_page(page, 1);
2335 goto repeat;
2336 }
2337 }
2338
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002339 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002340
Eric Biggersf69e8142018-04-18 11:09:48 -07002341 /* wait for GCed page writeback via META_MAPPING */
2342 if (f2fs_post_read_required(inode))
Jaegeuk Kimfb605d02017-09-05 17:04:35 -07002343 f2fs_wait_on_block_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08002344
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002345 if (len == PAGE_SIZE || PageUptodate(page))
2346 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002347
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002348 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2349 zero_user_segment(page, len, PAGE_SIZE);
2350 return 0;
2351 }
2352
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002353 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002354 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002355 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002356 } else {
Jaegeuk Kim5a690672017-09-06 21:04:44 -07002357 err = f2fs_submit_page_read(inode, page, blkaddr);
2358 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08002359 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08002360
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002361 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002362 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002363 f2fs_put_page(page, 1);
2364 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002365 }
Chao Yu1563ac72016-07-03 22:05:12 +08002366 if (unlikely(!PageUptodate(page))) {
2367 err = -EIO;
2368 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002369 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002370 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002371 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002372
Chao Yu3aab8f82014-07-02 13:25:04 +08002373fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002374 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002375 f2fs_write_failed(mapping, pos + len);
Chao Yu2a6489c2018-01-15 17:16:46 +08002376 if (drop_atomic)
Chao Yu6b4d6a82018-05-30 00:20:41 +08002377 f2fs_drop_inmem_pages_all(sbi, false);
Chao Yu3aab8f82014-07-02 13:25:04 +08002378 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002379}
2380
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002381static int f2fs_write_end(struct file *file,
2382 struct address_space *mapping,
2383 loff_t pos, unsigned len, unsigned copied,
2384 struct page *page, void *fsdata)
2385{
2386 struct inode *inode = page->mapping->host;
2387
Chao Yudfb2bf32014-05-06 16:47:23 +08002388 trace_f2fs_write_end(inode, pos, len, copied);
2389
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002390 /*
2391 * This should be come from len == PAGE_SIZE, and we expect copied
2392 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2393 * let generic_perform_write() try to copy data again through copied=0.
2394 */
2395 if (!PageUptodate(page)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002396 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002397 copied = 0;
2398 else
2399 SetPageUptodate(page);
2400 }
2401 if (!copied)
2402 goto unlock_out;
2403
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002404 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002405
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002406 if (pos + copied > i_size_read(inode))
2407 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002408unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002409 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002410 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002411 return copied;
2412}
2413
Omar Sandoval6f673762015-03-16 04:33:52 -07002414static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2415 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002416{
Jaegeuk Kim28475252018-06-29 18:55:12 -07002417 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2418 unsigned blkbits = i_blkbits;
2419 unsigned blocksize_mask = (1 << blkbits) - 1;
2420 unsigned long align = offset | iov_iter_alignment(iter);
2421 struct block_device *bdev = inode->i_sb->s_bdev;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002422
Jaegeuk Kim28475252018-06-29 18:55:12 -07002423 if (align & blocksize_mask) {
2424 if (bdev)
2425 blkbits = blksize_bits(bdev_logical_block_size(bdev));
2426 blocksize_mask = (1 << blkbits) - 1;
2427 if (align & blocksize_mask)
2428 return -EINVAL;
2429 return 1;
2430 }
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002431 return 0;
2432}
2433
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002434static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002435{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002436 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002437 struct inode *inode = mapping->host;
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002438 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002439 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002440 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002441 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002442 int err;
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002443 enum rw_hint hint = iocb->ki_hint;
Chao Yue9a50e62018-03-08 14:22:56 +08002444 int whint_mode = F2FS_OPTION(sbi).whint_mode;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002445
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002446 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002447 if (err)
Jaegeuk Kim28475252018-06-29 18:55:12 -07002448 return err < 0 ? err : 0;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002449
Hyunchul Lee355d2342018-03-08 19:34:38 +09002450 if (f2fs_force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002451 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002452
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002453 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002454
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002455 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2456 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2457
Chao Yuf6bd7d42018-04-24 10:55:28 +08002458 if (!down_read_trylock(&F2FS_I(inode)->i_gc_rwsem[rw])) {
Hyunchul Lee355d2342018-03-08 19:34:38 +09002459 if (iocb->ki_flags & IOCB_NOWAIT) {
2460 iocb->ki_hint = hint;
2461 err = -EAGAIN;
2462 goto out;
2463 }
Chao Yuf6bd7d42018-04-24 10:55:28 +08002464 down_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
Hyunchul Lee355d2342018-03-08 19:34:38 +09002465 }
2466
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002467 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yuf6bd7d42018-04-24 10:55:28 +08002468 up_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002469
2470 if (rw == WRITE) {
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002471 if (whint_mode == WHINT_MODE_OFF)
2472 iocb->ki_hint = hint;
Chao Yuc0fe4882017-08-02 23:21:48 +08002473 if (err > 0) {
2474 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2475 err);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002476 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yuc0fe4882017-08-02 23:21:48 +08002477 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002478 f2fs_write_failed(mapping, offset + count);
Chao Yuc0fe4882017-08-02 23:21:48 +08002479 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002480 }
Chao Yu70407fa2014-07-31 21:11:22 +08002481
Hyunchul Lee355d2342018-03-08 19:34:38 +09002482out:
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002483 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002484
Chao Yu3aab8f82014-07-02 13:25:04 +08002485 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002486}
2487
Chao Yu487261f2015-02-05 17:44:29 +08002488void f2fs_invalidate_page(struct page *page, unsigned int offset,
2489 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002490{
2491 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002492 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002493
Chao Yu487261f2015-02-05 17:44:29 +08002494 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002495 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002496 return;
2497
Chao Yu487261f2015-02-05 17:44:29 +08002498 if (PageDirty(page)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002499 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002500 dec_page_count(sbi, F2FS_DIRTY_META);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002501 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002502 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002503 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002504 inode_dec_dirty_pages(inode);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002505 f2fs_remove_dirty_inode(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002506 }
Chao Yu487261f2015-02-05 17:44:29 +08002507 }
Chao Yudecd36b2015-08-07 18:42:09 +08002508
2509 /* This is atomic written page, keep Private */
2510 if (IS_ATOMIC_WRITTEN_PAGE(page))
Chao Yu6b4d6a82018-05-30 00:20:41 +08002511 return f2fs_drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002512
Chao Yu23dc9742016-04-29 20:09:15 +08002513 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002514 ClearPagePrivate(page);
2515}
2516
Chao Yu487261f2015-02-05 17:44:29 +08002517int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002518{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002519 /* If this is dirty page, keep PagePrivate */
2520 if (PageDirty(page))
2521 return 0;
2522
Chao Yudecd36b2015-08-07 18:42:09 +08002523 /* This is atomic written page, keep Private */
2524 if (IS_ATOMIC_WRITTEN_PAGE(page))
2525 return 0;
2526
Chao Yu23dc9742016-04-29 20:09:15 +08002527 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002528 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002529 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002530}
2531
2532static int f2fs_set_data_page_dirty(struct page *page)
2533{
2534 struct address_space *mapping = page->mapping;
2535 struct inode *inode = mapping->host;
2536
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002537 trace_f2fs_set_page_dirty(page, DATA);
2538
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002539 if (!PageUptodate(page))
2540 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002541
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002542 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002543 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08002544 f2fs_register_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002545 return 1;
2546 }
2547 /*
2548 * Previously, this page has been registered, we just
2549 * return here.
2550 */
2551 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002552 }
2553
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002554 if (!PageDirty(page)) {
Jaegeuk Kim95459e92018-04-20 19:29:52 -07002555 __set_page_dirty_nobuffers(page);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002556 f2fs_update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002557 return 1;
2558 }
2559 return 0;
2560}
2561
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002562static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2563{
Chao Yu454ae7e2014-04-22 13:34:01 +08002564 struct inode *inode = mapping->host;
2565
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002566 if (f2fs_has_inline_data(inode))
2567 return 0;
2568
2569 /* make sure allocating whole blocks */
2570 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2571 filemap_write_and_wait(mapping);
2572
Chao Yue2b4e2b2015-08-19 19:11:19 +08002573 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002574}
2575
Weichao Guo5b7a4872016-09-20 05:03:27 +08002576#ifdef CONFIG_MIGRATION
2577#include <linux/migrate.h>
2578
2579int f2fs_migrate_page(struct address_space *mapping,
2580 struct page *newpage, struct page *page, enum migrate_mode mode)
2581{
2582 int rc, extra_count;
2583 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2584 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2585
2586 BUG_ON(PageWriteback(page));
2587
2588 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kim025258e2017-07-06 14:46:01 -07002589 if (atomic_written) {
2590 if (mode != MIGRATE_SYNC)
2591 return -EBUSY;
2592 if (!mutex_trylock(&fi->inmem_lock))
2593 return -EAGAIN;
2594 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002595
2596 /*
2597 * A reference is expected if PagePrivate set when move mapping,
2598 * however F2FS breaks this for maintaining dirty page counts when
2599 * truncating pages. So here adjusting the 'extra_count' make it work.
2600 */
2601 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2602 rc = migrate_page_move_mapping(mapping, newpage,
2603 page, NULL, mode, extra_count);
2604 if (rc != MIGRATEPAGE_SUCCESS) {
2605 if (atomic_written)
2606 mutex_unlock(&fi->inmem_lock);
2607 return rc;
2608 }
2609
2610 if (atomic_written) {
2611 struct inmem_pages *cur;
2612 list_for_each_entry(cur, &fi->inmem_pages, list)
2613 if (cur->page == page) {
2614 cur->page = newpage;
2615 break;
2616 }
2617 mutex_unlock(&fi->inmem_lock);
2618 put_page(page);
2619 get_page(newpage);
2620 }
2621
2622 if (PagePrivate(page))
2623 SetPagePrivate(newpage);
2624 set_page_private(newpage, page_private(page));
2625
2626 migrate_page_copy(newpage, page);
2627
2628 return MIGRATEPAGE_SUCCESS;
2629}
2630#endif
2631
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002632const struct address_space_operations f2fs_dblock_aops = {
2633 .readpage = f2fs_read_data_page,
2634 .readpages = f2fs_read_data_pages,
2635 .writepage = f2fs_write_data_page,
2636 .writepages = f2fs_write_data_pages,
2637 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002638 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002639 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002640 .invalidatepage = f2fs_invalidate_page,
2641 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002642 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002643 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002644#ifdef CONFIG_MIGRATION
2645 .migratepage = f2fs_migrate_page,
2646#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002647};
Eric Biggersf69e8142018-04-18 11:09:48 -07002648
Chao Yu6b4d6a82018-05-30 00:20:41 +08002649void f2fs_clear_radix_tree_dirty_tag(struct page *page)
Chao Yubcbcda42018-05-26 18:03:35 +08002650{
2651 struct address_space *mapping = page_mapping(page);
2652 unsigned long flags;
2653
2654 spin_lock_irqsave(&mapping->tree_lock, flags);
2655 radix_tree_tag_clear(&mapping->page_tree, page_index(page),
2656 PAGECACHE_TAG_DIRTY);
2657 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2658}
2659
Eric Biggersf69e8142018-04-18 11:09:48 -07002660int __init f2fs_init_post_read_processing(void)
2661{
2662 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0);
2663 if (!bio_post_read_ctx_cache)
2664 goto fail;
2665 bio_post_read_ctx_pool =
2666 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
2667 bio_post_read_ctx_cache);
2668 if (!bio_post_read_ctx_pool)
2669 goto fail_free_cache;
2670 return 0;
2671
2672fail_free_cache:
2673 kmem_cache_destroy(bio_post_read_ctx_cache);
2674fail:
2675 return -ENOMEM;
2676}
2677
2678void __exit f2fs_destroy_post_read_processing(void)
2679{
2680 mempool_destroy(bio_post_read_ctx_pool);
2681 kmem_cache_destroy(bio_post_read_ctx_cache);
2682}