blob: a2e30c403ef2b1789f7ae939b7189378f1ed383d [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010023#include <linux/sched/signal.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090024
25#include "f2fs.h"
26#include "node.h"
27#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080028#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090029#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090030
Eric Biggers6dbb1792018-04-18 11:09:48 -070031#define NUM_PREALLOC_POST_READ_CTXS 128
32
33static struct kmem_cache *bio_post_read_ctx_cache;
34static mempool_t *bio_post_read_ctx_pool;
35
Chao Yu36951b32016-11-16 10:41:20 +080036static bool __is_cp_guaranteed(struct page *page)
37{
38 struct address_space *mapping = page->mapping;
39 struct inode *inode;
40 struct f2fs_sb_info *sbi;
41
42 if (!mapping)
43 return false;
44
45 inode = mapping->host;
46 sbi = F2FS_I_SB(inode);
47
48 if (inode->i_ino == F2FS_META_INO(sbi) ||
49 inode->i_ino == F2FS_NODE_INO(sbi) ||
50 S_ISDIR(inode->i_mode) ||
Chao Yue7a4feb2018-05-08 14:06:03 +080051 (S_ISREG(inode->i_mode) &&
52 is_inode_flag_set(inode, FI_ATOMIC_FILE)) ||
Chao Yu36951b32016-11-16 10:41:20 +080053 is_cold_data(page))
54 return true;
55 return false;
56}
57
Eric Biggers6dbb1792018-04-18 11:09:48 -070058/* postprocessing steps for read bios */
59enum bio_post_read_step {
60 STEP_INITIAL = 0,
61 STEP_DECRYPT,
62};
63
64struct bio_post_read_ctx {
65 struct bio *bio;
66 struct work_struct work;
67 unsigned int cur_step;
68 unsigned int enabled_steps;
69};
70
71static void __read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090072{
Eric Biggers6dbb1792018-04-18 11:09:48 -070073 struct page *page;
74 struct bio_vec *bv;
Linus Torvaldsf5688492014-01-30 11:19:05 -080075 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090076
Eric Biggers6dbb1792018-04-18 11:09:48 -070077 bio_for_each_segment_all(bv, bio, i) {
78 page = bv->bv_page;
79
80 /* PG_error was set if any post_read step failed */
81 if (bio->bi_status || PageError(page)) {
82 ClearPageUptodate(page);
83 SetPageError(page);
84 } else {
85 SetPageUptodate(page);
86 }
87 unlock_page(page);
88 }
89 if (bio->bi_private)
90 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
91 bio_put(bio);
92}
93
94static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
95
96static void decrypt_work(struct work_struct *work)
97{
98 struct bio_post_read_ctx *ctx =
99 container_of(work, struct bio_post_read_ctx, work);
100
101 fscrypt_decrypt_bio(ctx->bio);
102
103 bio_post_read_processing(ctx);
104}
105
106static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
107{
108 switch (++ctx->cur_step) {
109 case STEP_DECRYPT:
110 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
111 INIT_WORK(&ctx->work, decrypt_work);
112 fscrypt_enqueue_decrypt_work(&ctx->work);
113 return;
114 }
115 ctx->cur_step++;
116 /* fall-through */
117 default:
118 __read_end_io(ctx->bio);
119 }
120}
121
122static bool f2fs_bio_post_read_required(struct bio *bio)
123{
124 return bio->bi_private && !bio->bi_status;
125}
126
127static void f2fs_read_end_io(struct bio *bio)
128{
Chao Yu8b038c72016-09-18 23:30:07 +0800129#ifdef CONFIG_F2FS_FAULT_INJECTION
Ming Lei263663c2017-12-18 20:22:04 +0800130 if (time_to_inject(F2FS_P_SB(bio_first_page_all(bio)), FAULT_IO)) {
Chao Yu55523512017-02-25 11:08:28 +0800131 f2fs_show_injection_info(FAULT_IO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200132 bio->bi_status = BLK_STS_IOERR;
Chao Yu55523512017-02-25 11:08:28 +0800133 }
Chao Yu8b038c72016-09-18 23:30:07 +0800134#endif
135
Eric Biggers6dbb1792018-04-18 11:09:48 -0700136 if (f2fs_bio_post_read_required(bio)) {
137 struct bio_post_read_ctx *ctx = bio->bi_private;
138
139 ctx->cur_step = STEP_INITIAL;
140 bio_post_read_processing(ctx);
141 return;
Chao Yu12377022015-05-25 18:03:38 +0800142 }
143
Eric Biggers6dbb1792018-04-18 11:09:48 -0700144 __read_end_io(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900145}
146
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200147static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900148{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +0900149 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -0800150 struct bio_vec *bvec;
151 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900152
Linus Torvaldsf5688492014-01-30 11:19:05 -0800153 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900154 struct page *page = bvec->bv_page;
Chao Yu36951b32016-11-16 10:41:20 +0800155 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900156
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800157 if (IS_DUMMY_WRITTEN_PAGE(page)) {
158 set_page_private(page, (unsigned long)NULL);
159 ClearPagePrivate(page);
160 unlock_page(page);
161 mempool_free(page, sbi->write_io_dummy);
162
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200163 if (unlikely(bio->bi_status))
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800164 f2fs_stop_checkpoint(sbi, true);
165 continue;
166 }
167
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700168 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700169
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200170 if (unlikely(bio->bi_status)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700171 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kimb1ca3212017-12-31 16:26:38 -0800172 if (type == F2FS_WB_CP_DATA)
173 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900174 }
Yunlei He7dff55d2018-01-11 14:19:32 +0800175
176 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
177 page->index != nid_of_node(page));
178
Chao Yu36951b32016-11-16 10:41:20 +0800179 dec_page_count(sbi, type);
180 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900181 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800182 }
Chao Yu36951b32016-11-16 10:41:20 +0800183 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700184 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900185 wake_up(&sbi->cp_wait);
186
187 bio_put(bio);
188}
189
Gu Zheng940a6d32013-12-20 17:39:59 +0800190/*
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700191 * Return true, if pre_bio's bdev is same as its target device.
192 */
193struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
194 block_t blk_addr, struct bio *bio)
195{
196 struct block_device *bdev = sbi->sb->s_bdev;
197 int i;
198
199 for (i = 0; i < sbi->s_ndevs; i++) {
200 if (FDEV(i).start_blk <= blk_addr &&
201 FDEV(i).end_blk >= blk_addr) {
202 blk_addr -= FDEV(i).start_blk;
203 bdev = FDEV(i).bdev;
204 break;
205 }
206 }
207 if (bio) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200208 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700209 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
210 }
211 return bdev;
212}
213
214int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
215{
216 int i;
217
218 for (i = 0; i < sbi->s_ndevs; i++)
219 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
220 return i;
221 return 0;
222}
223
224static bool __same_bdev(struct f2fs_sb_info *sbi,
225 block_t blk_addr, struct bio *bio)
226{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200227 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
228 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700229}
230
231/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800232 * Low-level block read/write IO operations.
233 */
234static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
Yufen Yu578c6472018-01-09 19:33:39 +0800235 struct writeback_control *wbc,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900236 int npages, bool is_read,
237 enum page_type type, enum temp_type temp)
Gu Zheng940a6d32013-12-20 17:39:59 +0800238{
239 struct bio *bio;
240
Chao Yud62fe972017-10-28 16:52:31 +0800241 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800242
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700243 f2fs_target_device(sbi, blk_addr, bio);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900244 if (is_read) {
245 bio->bi_end_io = f2fs_read_end_io;
246 bio->bi_private = NULL;
247 } else {
248 bio->bi_end_io = f2fs_write_end_io;
249 bio->bi_private = sbi;
Chao Yu4d57b862018-05-30 00:20:41 +0800250 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, type, temp);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900251 }
Yufen Yu578c6472018-01-09 19:33:39 +0800252 if (wbc)
253 wbc_init_bio(wbc, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800254
255 return bio;
256}
257
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700258static inline void __submit_bio(struct f2fs_sb_info *sbi,
259 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700260{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700261 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800262 unsigned int start;
263
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800264 if (type != DATA && type != NODE)
265 goto submit_io;
266
Sheng Yongccd31cb2018-02-06 12:31:17 +0800267 if (f2fs_sb_has_blkzoned(sbi->sb) && current->plug)
Tiezhu Yang3bb09a02018-02-06 08:21:45 +0800268 blk_finish_plug(current->plug);
269
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800270 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
271 start %= F2FS_IO_SIZE(sbi);
272
273 if (start == 0)
274 goto submit_io;
275
276 /* fill dummy pages */
277 for (; start < F2FS_IO_SIZE(sbi); start++) {
278 struct page *page =
279 mempool_alloc(sbi->write_io_dummy,
280 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
281 f2fs_bug_on(sbi, !page);
282
283 SetPagePrivate(page);
284 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
285 lock_page(page);
286 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
287 f2fs_bug_on(sbi, 1);
288 }
289 /*
290 * In the NODE case, we lose next block address chain. So, we
291 * need to do checkpoint in f2fs_sync_file.
292 */
293 if (type == NODE)
294 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700295 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800296submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800297 if (is_read_io(bio_op(bio)))
298 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
299 else
300 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500301 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700302}
303
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900304static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900305{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900306 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900307
308 if (!io->bio)
309 return;
310
Mike Christie04d328d2016-06-05 14:31:55 -0500311 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
312
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800313 if (is_read_io(fio->op))
314 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
315 else
316 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
317
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700318 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900319 io->bio = NULL;
320}
321
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800322static bool __has_merged_page(struct f2fs_bio_info *io,
323 struct inode *inode, nid_t ino, pgoff_t idx)
Chao Yu0fd785e2016-01-18 18:24:59 +0800324{
Chao Yu0fd785e2016-01-18 18:24:59 +0800325 struct bio_vec *bvec;
326 struct page *target;
327 int i;
328
Chao Yu0c3a5792016-01-18 18:28:11 +0800329 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800330 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800331
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800332 if (!inode && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800333 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800334
335 bio_for_each_segment_all(bvec, io->bio, i) {
336
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700337 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800338 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700339 else
340 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800341
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800342 if (idx != target->index)
343 continue;
344
Chao Yu0c3a5792016-01-18 18:28:11 +0800345 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800346 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800347 if (ino && ino == ino_of_node(target))
348 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800349 }
350
Chao Yu0fd785e2016-01-18 18:24:59 +0800351 return false;
352}
353
Chao Yu0c3a5792016-01-18 18:28:11 +0800354static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800355 nid_t ino, pgoff_t idx, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800356{
357 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700358 enum temp_type temp;
359 struct f2fs_bio_info *io;
360 bool ret = false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800361
Jaegeuk Kima912b542017-05-10 11:18:25 -0700362 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
363 io = sbi->write_io[btype] + temp;
364
365 down_read(&io->io_rwsem);
366 ret = __has_merged_page(io, inode, ino, idx);
367 up_read(&io->io_rwsem);
368
369 /* TODO: use HOT temp only for meta pages now. */
370 if (ret || btype == META)
371 break;
372 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800373 return ret;
374}
375
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700376static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700377 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900378{
379 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700380 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900381
Chao Yudf0f8dc2014-03-22 14:57:23 +0800382 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900383
384 /* change META to META_FLUSH in the checkpoint procedure */
385 if (type >= META_FLUSH) {
386 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500387 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200388 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600389 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800390 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900391 }
392 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800393 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900394}
395
Jaegeuk Kima912b542017-05-10 11:18:25 -0700396static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800397 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700398 enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800399{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700400 enum temp_type temp;
401
402 if (!force && !has_merged_page(sbi, inode, ino, idx, type))
403 return;
404
405 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
406
407 __f2fs_submit_merged_write(sbi, type, temp);
408
409 /* TODO: use HOT temp only for meta pages now. */
410 if (type >= META)
411 break;
412 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800413}
414
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700415void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800416{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700417 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900418}
419
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700420void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900421 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700422 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900423{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700424 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900425}
426
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700427void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800428{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700429 f2fs_submit_merged_write(sbi, DATA);
430 f2fs_submit_merged_write(sbi, NODE);
431 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800432}
433
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900434/*
435 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300436 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900437 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700438int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900439{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900440 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700441 struct page *page = fio->encrypted_page ?
442 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900443
Yunlei He08337212018-03-08 16:29:13 +0800444 verify_block_addr(fio, fio->new_blkaddr);
Chao Yu2ace38e2014-12-24 16:08:14 +0800445 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700446 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900447
448 /* Allocate a new bio */
Yufen Yu578c6472018-01-09 19:33:39 +0800449 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900450 1, is_read_io(fio->op), fio->type, fio->temp);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900451
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300452 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900453 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900454 return -EFAULT;
455 }
Mike Christie04d328d2016-06-05 14:31:55 -0500456 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900457
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700458 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -0700459
460 if (!is_read_io(fio->op))
461 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900462 return 0;
463}
464
Chao Yufe16efe2018-05-28 23:47:18 +0800465void f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900466{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700467 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900468 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700469 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700470 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900471
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700472 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900473
Chao Yufb830fc2017-05-19 23:37:01 +0800474 down_write(&io->io_rwsem);
475next:
476 if (fio->in_list) {
477 spin_lock(&io->io_lock);
478 if (list_empty(&io->io_list)) {
479 spin_unlock(&io->io_lock);
Chao Yufe16efe2018-05-28 23:47:18 +0800480 goto out;
Chao Yufb830fc2017-05-19 23:37:01 +0800481 }
482 fio = list_first_entry(&io->io_list,
483 struct f2fs_io_info, list);
484 list_del(&fio->list);
485 spin_unlock(&io->io_lock);
486 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900487
Chao Yu7b525dd2018-05-23 22:25:08 +0800488 if (is_valid_blkaddr(fio->old_blkaddr))
Yunlei He08337212018-03-08 16:29:13 +0800489 verify_block_addr(fio, fio->old_blkaddr);
490 verify_block_addr(fio, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900491
Chao Yu36951b32016-11-16 10:41:20 +0800492 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
493
Thomas Meyerebf7c522017-10-07 16:02:21 +0200494 /* set submitted = true as a return value */
495 fio->submitted = true;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800496
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700497 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900498
Chao Yu7a9d7542016-02-22 18:36:38 +0800499 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700500 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
501 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900502 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900503alloc_new:
504 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800505 if ((fio->type == DATA || fio->type == NODE) &&
506 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700507 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Chao Yufe16efe2018-05-28 23:47:18 +0800508 fio->retry = true;
509 goto skip;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800510 }
Yufen Yu578c6472018-01-09 19:33:39 +0800511 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900512 BIO_MAX_PAGES, false,
513 fio->type, fio->temp);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900514 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900515 }
516
Jaegeuk Kima912b542017-05-10 11:18:25 -0700517 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900518 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900519 goto alloc_new;
520 }
521
Yufen Yu578c6472018-01-09 19:33:39 +0800522 if (fio->io_wbc)
523 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
524
Chao Yu7a9d7542016-02-22 18:36:38 +0800525 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700526 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800527
528 trace_f2fs_submit_page_write(fio->page, fio);
Chao Yufe16efe2018-05-28 23:47:18 +0800529skip:
Chao Yufb830fc2017-05-19 23:37:01 +0800530 if (fio->in_list)
531 goto next;
Chao Yufe16efe2018-05-28 23:47:18 +0800532out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800533 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900534}
535
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700536static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
537 unsigned nr_pages)
538{
539 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700540 struct bio *bio;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700541 struct bio_post_read_ctx *ctx;
542 unsigned int post_read_steps = 0;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700543
Eric Biggers6dbb1792018-04-18 11:09:48 -0700544 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
545 if (!bio)
546 return ERR_PTR(-ENOMEM);
547 f2fs_target_device(sbi, blkaddr, bio);
548 bio->bi_end_io = f2fs_read_end_io;
549 bio_set_op_attrs(bio, REQ_OP_READ, 0);
550
551 if (f2fs_encrypted_file(inode))
552 post_read_steps |= 1 << STEP_DECRYPT;
553 if (post_read_steps) {
554 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
555 if (!ctx) {
556 bio_put(bio);
557 return ERR_PTR(-ENOMEM);
558 }
559 ctx->bio = bio;
560 ctx->enabled_steps = post_read_steps;
561 bio->bi_private = ctx;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700562
563 /* wait the page to be moved by cleaning */
564 f2fs_wait_on_block_writeback(sbi, blkaddr);
565 }
566
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700567 return bio;
568}
569
570/* This can handle encryption stuffs */
571static int f2fs_submit_page_read(struct inode *inode, struct page *page,
572 block_t blkaddr)
573{
574 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1);
575
576 if (IS_ERR(bio))
577 return PTR_ERR(bio);
578
579 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
580 bio_put(bio);
581 return -EFAULT;
582 }
583 __submit_bio(F2FS_I_SB(inode), bio, DATA);
584 return 0;
585}
586
Chao Yu46008c62016-05-09 19:56:30 +0800587static void __set_data_blkaddr(struct dnode_of_data *dn)
588{
589 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
590 __le32 *addr_array;
Chao Yu7a2af762017-07-19 00:19:06 +0800591 int base = 0;
592
593 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
594 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800595
596 /* Get physical address of data block */
597 addr_array = blkaddr_in_node(rn);
Chao Yu7a2af762017-07-19 00:19:06 +0800598 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800599}
600
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900601/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900602 * Lock ordering for the change of data block address:
603 * ->data_page
604 * ->node_page
605 * update block addresses in the node page
606 */
Chao Yu4d57b862018-05-30 00:20:41 +0800607void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900608{
Chao Yu46008c62016-05-09 19:56:30 +0800609 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
610 __set_data_blkaddr(dn);
611 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800612 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900613}
614
Chao Yuf28b3432016-02-24 17:16:47 +0800615void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
616{
617 dn->data_blkaddr = blkaddr;
Chao Yu4d57b862018-05-30 00:20:41 +0800618 f2fs_set_data_blkaddr(dn);
Chao Yuf28b3432016-02-24 17:16:47 +0800619 f2fs_update_extent_cache(dn);
620}
621
Chao Yu46008c62016-05-09 19:56:30 +0800622/* dn->ofs_in_node will be returned with up-to-date last block pointer */
Chao Yu4d57b862018-05-30 00:20:41 +0800623int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900624{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700625 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800626 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900627
Chao Yu46008c62016-05-09 19:56:30 +0800628 if (!count)
629 return 0;
630
Jaegeuk Kim91942322016-05-20 10:13:22 -0700631 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900632 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +0800633 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
634 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900635
Chao Yu46008c62016-05-09 19:56:30 +0800636 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
637 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900638
Chao Yu46008c62016-05-09 19:56:30 +0800639 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
640
641 for (; count > 0; dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800642 block_t blkaddr = datablock_addr(dn->inode,
643 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800644 if (blkaddr == NULL_ADDR) {
645 dn->data_blkaddr = NEW_ADDR;
646 __set_data_blkaddr(dn);
647 count--;
648 }
649 }
650
651 if (set_page_dirty(dn->node_page))
652 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900653 return 0;
654}
655
Chao Yu46008c62016-05-09 19:56:30 +0800656/* Should keep dn->ofs_in_node unchanged */
Chao Yu4d57b862018-05-30 00:20:41 +0800657int f2fs_reserve_new_block(struct dnode_of_data *dn)
Chao Yu46008c62016-05-09 19:56:30 +0800658{
659 unsigned int ofs_in_node = dn->ofs_in_node;
660 int ret;
661
Chao Yu4d57b862018-05-30 00:20:41 +0800662 ret = f2fs_reserve_new_blocks(dn, 1);
Chao Yu46008c62016-05-09 19:56:30 +0800663 dn->ofs_in_node = ofs_in_node;
664 return ret;
665}
666
Huajun Lib6009652013-11-10 23:13:18 +0800667int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
668{
669 bool need_put = dn->inode_page ? false : true;
670 int err;
671
Chao Yu4d57b862018-05-30 00:20:41 +0800672 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
Huajun Lib6009652013-11-10 23:13:18 +0800673 if (err)
674 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900675
Huajun Lib6009652013-11-10 23:13:18 +0800676 if (dn->data_blkaddr == NULL_ADDR)
Chao Yu4d57b862018-05-30 00:20:41 +0800677 err = f2fs_reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900678 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800679 f2fs_put_dnode(dn);
680 return err;
681}
682
Fan Li759af1c2015-08-05 15:52:16 +0800683int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900684{
Hou Pengyange15882b2017-02-23 09:18:05 +0000685 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800686 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800687
Fan Li759af1c2015-08-05 15:52:16 +0800688 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
689 dn->data_blkaddr = ei.blk + index - ei.fofs;
690 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800691 }
692
Fan Li759af1c2015-08-05 15:52:16 +0800693 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900694}
695
Chao Yu4d57b862018-05-30 00:20:41 +0800696struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500697 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900698{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900699 struct address_space *mapping = inode->i_mapping;
700 struct dnode_of_data dn;
701 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +0000702 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900703 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700704
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700705 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900706 if (!page)
707 return ERR_PTR(-ENOMEM);
708
Chao Yucb3bc9e2015-02-05 18:03:40 +0800709 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
710 dn.data_blkaddr = ei.blk + index - ei.fofs;
711 goto got_it;
712 }
713
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900714 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +0800715 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700716 if (err)
717 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900718 f2fs_put_dnode(&dn);
719
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900720 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700721 err = -ENOENT;
722 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900723 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800724got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700725 if (PageUptodate(page)) {
726 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900727 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700728 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900729
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900730 /*
731 * A new dentry page is allocated but not able to be written, since its
732 * new inode page couldn't be allocated due to -ENOSPC.
733 * In such the case, its blkaddr can be remained as NEW_ADDR.
Chao Yu4d57b862018-05-30 00:20:41 +0800734 * see, f2fs_add_link -> f2fs_get_new_data_page ->
735 * f2fs_init_inode_metadata.
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900736 */
737 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300738 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700739 if (!PageUptodate(page))
740 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700741 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900742 return page;
743 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900744
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700745 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900746 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700747 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700748 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700749
750put_err:
751 f2fs_put_page(page, 1);
752 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700753}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900754
Chao Yu4d57b862018-05-30 00:20:41 +0800755struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700756{
757 struct address_space *mapping = inode->i_mapping;
758 struct page *page;
759
760 page = find_get_page(mapping, index);
761 if (page && PageUptodate(page))
762 return page;
763 f2fs_put_page(page, 0);
764
Chao Yu4d57b862018-05-30 00:20:41 +0800765 page = f2fs_get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700766 if (IS_ERR(page))
767 return page;
768
769 if (PageUptodate(page))
770 return page;
771
772 wait_on_page_locked(page);
773 if (unlikely(!PageUptodate(page))) {
774 f2fs_put_page(page, 0);
775 return ERR_PTR(-EIO);
776 }
777 return page;
778}
779
780/*
781 * If it tries to access a hole, return an error.
782 * Because, the callers, functions in dir.c and GC, should be able to know
783 * whether this page exists or not.
784 */
Chao Yu4d57b862018-05-30 00:20:41 +0800785struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700786 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700787{
788 struct address_space *mapping = inode->i_mapping;
789 struct page *page;
790repeat:
Chao Yu4d57b862018-05-30 00:20:41 +0800791 page = f2fs_get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700792 if (IS_ERR(page))
793 return page;
794
795 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900796 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900797 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900798 f2fs_put_page(page, 1);
799 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900800 }
Chao Yu1563ac72016-07-03 22:05:12 +0800801 if (unlikely(!PageUptodate(page))) {
802 f2fs_put_page(page, 1);
803 return ERR_PTR(-EIO);
804 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900805 return page;
806}
807
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900808/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900809 * Caller ensures that this data page is never allocated.
810 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900811 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800812 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
813 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800814 * Note that, ipage is set only by make_empty_dir, and if any error occur,
815 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900816 */
Chao Yu4d57b862018-05-30 00:20:41 +0800817struct page *f2fs_get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900818 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900819{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900820 struct address_space *mapping = inode->i_mapping;
821 struct page *page;
822 struct dnode_of_data dn;
823 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800824
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700825 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800826 if (!page) {
827 /*
828 * before exiting, we should make sure ipage will be released
829 * if any error occur.
830 */
831 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700832 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800833 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900834
Jaegeuk Kima8865372013-12-27 17:04:17 +0900835 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800836 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700837 if (err) {
838 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900839 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900840 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700841 if (!ipage)
842 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900843
844 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700845 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900846
847 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300848 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700849 if (!PageUptodate(page))
850 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900851 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700852 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900853
Jaegeuk Kim76121182016-01-01 22:03:47 -0800854 /* if ipage exists, blkaddr should be NEW_ADDR */
855 f2fs_bug_on(F2FS_I_SB(inode), ipage);
Chao Yu4d57b862018-05-30 00:20:41 +0800856 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700857 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800858 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900859 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700860got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800861 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700862 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700863 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900864 return page;
865}
866
Hyunchul Leed5097be2017-11-28 09:23:00 +0900867static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900868{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700869 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900870 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900871 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700872 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800873 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +0800874 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900875
Jaegeuk Kim91942322016-05-20 10:13:22 -0700876 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900877 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800878
Chao Yu7a2af762017-07-19 00:19:06 +0800879 dn->data_blkaddr = datablock_addr(dn->inode,
880 dn->node_page, dn->ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800881 if (dn->data_blkaddr == NEW_ADDR)
882 goto alloc;
883
Chao Yu0abd6752017-07-09 00:13:07 +0800884 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
885 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900886
Chao Yudf6136e2015-03-23 10:33:37 +0800887alloc:
Chao Yu4d57b862018-05-30 00:20:41 +0800888 f2fs_get_node_info(sbi, dn->nid, &ni);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900889 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
890
Chao Yu4d57b862018-05-30 00:20:41 +0800891 f2fs_allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Hyunchul Leed5097be2017-11-28 09:23:00 +0900892 &sum, seg_type, NULL, false);
Chao Yu4d57b862018-05-30 00:20:41 +0800893 f2fs_set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900894
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700895 /* update i_size */
Chao Yu4d57b862018-05-30 00:20:41 +0800896 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700897 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300898 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700899 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300900 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900901 return 0;
902}
903
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800904int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800905{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800906 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800907 struct f2fs_map_blocks map;
Chao Yud6d478a12018-01-03 17:30:19 +0800908 int flag;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800909 int err = 0;
Chao Yud6d478a12018-01-03 17:30:19 +0800910 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800911
Weichao Guo71ad6822017-09-29 22:43:23 +0800912 /* convert inline data for Direct I/O*/
Chao Yud6d478a12018-01-03 17:30:19 +0800913 if (direct_io) {
Weichao Guo71ad6822017-09-29 22:43:23 +0800914 err = f2fs_convert_inline_inode(inode);
915 if (err)
916 return err;
917 }
918
Jaegeuk Kimdc91de72017-01-13 13:12:29 -0800919 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
920 return 0;
921
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700922 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800923 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
924 if (map.m_len > map.m_lblk)
925 map.m_len -= map.m_lblk;
926 else
927 map.m_len = 0;
928
Chao Yuda859852016-01-26 15:42:58 +0800929 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +0800930 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +0900931 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800932
Chao Yud6d478a12018-01-03 17:30:19 +0800933 if (direct_io) {
Chao Yu4d57b862018-05-30 00:20:41 +0800934 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900935 flag = f2fs_force_buffered_io(inode, WRITE) ?
Chao Yud6d478a12018-01-03 17:30:19 +0800936 F2FS_GET_BLOCK_PRE_AIO :
937 F2FS_GET_BLOCK_PRE_DIO;
938 goto map_blocks;
Hyunchul Leed5097be2017-11-28 09:23:00 +0900939 }
Chao Yuf2470372017-07-19 00:19:05 +0800940 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800941 err = f2fs_convert_inline_inode(inode);
942 if (err)
943 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800944 }
Chao Yud6d478a12018-01-03 17:30:19 +0800945 if (f2fs_has_inline_data(inode))
Sheng Yong250066452017-11-22 18:23:39 +0800946 return err;
Chao Yud6d478a12018-01-03 17:30:19 +0800947
948 flag = F2FS_GET_BLOCK_PRE_AIO;
949
950map_blocks:
951 err = f2fs_map_blocks(inode, &map, 1, flag);
952 if (map.m_len > 0 && err == -ENOSPC) {
953 if (!direct_io)
954 set_inode_flag(inode, FI_NO_PREALLOC);
955 err = 0;
Sheng Yong250066452017-11-22 18:23:39 +0800956 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800957 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800958}
959
Yunlei He59c90812017-03-13 20:22:18 +0800960static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
961{
962 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
963 if (lock)
964 down_read(&sbi->node_change);
965 else
966 up_read(&sbi->node_change);
967 } else {
968 if (lock)
969 f2fs_lock_op(sbi);
970 else
971 f2fs_unlock_op(sbi);
972 }
973}
974
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900975/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700976 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
977 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800978 * If original data blocks are allocated, then give them to blockdev.
979 * Otherwise,
980 * a. preallocate requested block addresses
981 * b. do not use extent cache for better performance
982 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900983 */
Chao Yud323d002015-10-27 09:53:45 +0800984int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800985 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900986{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700987 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900988 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800989 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700990 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800991 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900992 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800993 unsigned int ofs_in_node, last_ofs_in_node;
994 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +0000995 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +0800996 block_t blkaddr;
Chao Yuc4020b22018-01-11 14:42:30 +0800997 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900998
Chao Yudfd02e42016-08-20 15:12:01 +0800999 if (!maxblocks)
1000 return 0;
1001
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001002 map->m_len = 0;
1003 map->m_flags = 0;
1004
1005 /* it only supports block size == page size */
1006 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +08001007 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001008
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001009 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001010 map->m_pblk = ei.blk + pgofs - ei.fofs;
1011 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1012 map->m_flags = F2FS_MAP_MAPPED;
Chao Yuc4020b22018-01-11 14:42:30 +08001013 if (map->m_next_extent)
1014 *map->m_next_extent = pgofs + map->m_len;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001015 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001016 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001017
Chao Yu4fe71e82016-01-26 15:37:38 +08001018next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001019 if (create)
Yunlei He59c90812017-03-13 20:22:18 +08001020 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001021
1022 /* When reading holes, we need its node page */
1023 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu4d57b862018-05-30 00:20:41 +08001024 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001025 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +08001026 if (flag == F2FS_GET_BLOCK_BMAP)
1027 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +08001028 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001029 err = 0;
Chao Yuda859852016-01-26 15:42:58 +08001030 if (map->m_next_pgofs)
1031 *map->m_next_pgofs =
Chao Yu4d57b862018-05-30 00:20:41 +08001032 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuc4020b22018-01-11 14:42:30 +08001033 if (map->m_next_extent)
1034 *map->m_next_extent =
Chao Yu4d57b862018-05-30 00:20:41 +08001035 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +08001036 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001037 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001038 }
Chao Yu973163f2015-09-18 16:51:51 +08001039
Chao Yuc4020b22018-01-11 14:42:30 +08001040 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +08001041 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +01001042 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +08001043 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001044
Chao Yu4fe71e82016-01-26 15:37:38 +08001045next_block:
Chao Yu7a2af762017-07-19 00:19:06 +08001046 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +08001047
Chao Yu7b525dd2018-05-23 22:25:08 +08001048 if (!is_valid_blkaddr(blkaddr)) {
Fan Li7df3a432015-12-17 13:20:59 +08001049 if (create) {
1050 if (unlikely(f2fs_cp_error(sbi))) {
1051 err = -EIO;
1052 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +08001053 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001054 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +08001055 if (blkaddr == NULL_ADDR) {
1056 prealloc++;
1057 last_ofs_in_node = dn.ofs_in_node;
1058 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001059 } else {
Hyunchul Leed5097be2017-11-28 09:23:00 +09001060 err = __allocate_data_block(&dn,
1061 map->m_seg_type);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001062 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001063 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001064 }
Fan Li7df3a432015-12-17 13:20:59 +08001065 if (err)
1066 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +08001067 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001068 blkaddr = dn.data_blkaddr;
1069 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001070 if (flag == F2FS_GET_BLOCK_BMAP) {
1071 map->m_pblk = 0;
1072 goto sync_out;
1073 }
Chao Yuc4020b22018-01-11 14:42:30 +08001074 if (flag == F2FS_GET_BLOCK_PRECACHE)
1075 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001076 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1077 blkaddr == NULL_ADDR) {
1078 if (map->m_next_pgofs)
1079 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001080 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001081 }
Chao Yuf3d98e72018-01-10 18:18:52 +08001082 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1083 /* for defragment case */
1084 if (map->m_next_pgofs)
1085 *map->m_next_pgofs = pgofs + 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001086 goto sync_out;
Chao Yuf3d98e72018-01-10 18:18:52 +08001087 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001088 }
1089 }
Fan Li7df3a432015-12-17 13:20:59 +08001090
Chao Yu46008c62016-05-09 19:56:30 +08001091 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1092 goto skip;
1093
Chao Yu4fe71e82016-01-26 15:37:38 +08001094 if (map->m_len == 0) {
1095 /* preallocated unwritten block should be mapped for fiemap. */
1096 if (blkaddr == NEW_ADDR)
1097 map->m_flags |= F2FS_MAP_UNWRITTEN;
1098 map->m_flags |= F2FS_MAP_MAPPED;
1099
1100 map->m_pblk = blkaddr;
1101 map->m_len = 1;
1102 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001103 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001104 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001105 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001106 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001107 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001108 } else {
1109 goto sync_out;
1110 }
1111
Chao Yu46008c62016-05-09 19:56:30 +08001112skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001113 dn.ofs_in_node++;
1114 pgofs++;
1115
Chao Yu46008c62016-05-09 19:56:30 +08001116 /* preallocate blocks in batch for one dnode page */
1117 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1118 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001119
Chao Yu46008c62016-05-09 19:56:30 +08001120 dn.ofs_in_node = ofs_in_node;
Chao Yu4d57b862018-05-30 00:20:41 +08001121 err = f2fs_reserve_new_blocks(&dn, prealloc);
Chao Yu46008c62016-05-09 19:56:30 +08001122 if (err)
1123 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001124
Chao Yu46008c62016-05-09 19:56:30 +08001125 map->m_len += dn.ofs_in_node - ofs_in_node;
1126 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1127 err = -ENOSPC;
1128 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001129 }
Chao Yu46008c62016-05-09 19:56:30 +08001130 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001131 }
1132
Chao Yu46008c62016-05-09 19:56:30 +08001133 if (pgofs >= end)
1134 goto sync_out;
1135 else if (dn.ofs_in_node < end_offset)
1136 goto next_block;
1137
Chao Yuc4020b22018-01-11 14:42:30 +08001138 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1139 if (map->m_flags & F2FS_MAP_MAPPED) {
1140 unsigned int ofs = start_pgofs - map->m_lblk;
1141
1142 f2fs_update_extent_cache_range(&dn,
1143 start_pgofs, map->m_pblk + ofs,
1144 map->m_len - ofs);
1145 }
1146 }
1147
Chao Yu46008c62016-05-09 19:56:30 +08001148 f2fs_put_dnode(&dn);
1149
1150 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001151 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001152 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001153 }
Chao Yu46008c62016-05-09 19:56:30 +08001154 goto next_dnode;
1155
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001156sync_out:
Chao Yuc4020b22018-01-11 14:42:30 +08001157 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1158 if (map->m_flags & F2FS_MAP_MAPPED) {
1159 unsigned int ofs = start_pgofs - map->m_lblk;
1160
1161 f2fs_update_extent_cache_range(&dn,
1162 start_pgofs, map->m_pblk + ofs,
1163 map->m_len - ofs);
1164 }
1165 if (map->m_next_extent)
1166 *map->m_next_extent = pgofs + 1;
1167 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001168 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001169unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001170 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001171 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001172 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001173 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001174out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001175 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001176 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001177}
1178
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001179bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1180{
1181 struct f2fs_map_blocks map;
1182 block_t last_lblk;
1183 int err;
1184
1185 if (pos + len > i_size_read(inode))
1186 return false;
1187
1188 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1189 map.m_next_pgofs = NULL;
1190 map.m_next_extent = NULL;
1191 map.m_seg_type = NO_CHECK_TYPE;
1192 last_lblk = F2FS_BLK_ALIGN(pos + len);
1193
1194 while (map.m_lblk < last_lblk) {
1195 map.m_len = last_lblk - map.m_lblk;
1196 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1197 if (err || map.m_len == 0)
1198 return false;
1199 map.m_lblk += map.m_len;
1200 }
1201 return true;
1202}
1203
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001204static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001205 struct buffer_head *bh, int create, int flag,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001206 pgoff_t *next_pgofs, int seg_type)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001207{
1208 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001209 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001210
1211 map.m_lblk = iblock;
1212 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001213 map.m_next_pgofs = next_pgofs;
Chao Yuc4020b22018-01-11 14:42:30 +08001214 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001215 map.m_seg_type = seg_type;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001216
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001217 err = f2fs_map_blocks(inode, &map, create, flag);
1218 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001219 map_bh(bh, inode->i_sb, map.m_pblk);
1220 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001221 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001222 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001223 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001224}
1225
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001226static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001227 struct buffer_head *bh_result, int create, int flag,
1228 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001229{
Chao Yuda859852016-01-26 15:42:58 +08001230 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001231 flag, next_pgofs,
1232 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001233}
1234
Chao Yue2b4e2b2015-08-19 19:11:19 +08001235static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001236 struct buffer_head *bh_result, int create)
1237{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001238 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001239 F2FS_GET_BLOCK_DEFAULT, NULL,
Chao Yu4d57b862018-05-30 00:20:41 +08001240 f2fs_rw_hint_to_seg_type(
Hyunchul Leed5097be2017-11-28 09:23:00 +09001241 inode->i_write_hint));
Chao Yue2b4e2b2015-08-19 19:11:19 +08001242}
1243
1244static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1245 struct buffer_head *bh_result, int create)
1246{
Yunlei He179448b2015-12-28 21:48:32 +08001247 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001248 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001249 return -EFBIG;
1250
Chao Yue2b4e2b2015-08-19 19:11:19 +08001251 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001252 F2FS_GET_BLOCK_BMAP, NULL,
1253 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001254}
1255
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001256static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1257{
1258 return (offset >> inode->i_blkbits);
1259}
1260
1261static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1262{
1263 return (blk << inode->i_blkbits);
1264}
1265
Chao Yu442a9db2018-01-11 14:39:57 +08001266static int f2fs_xattr_fiemap(struct inode *inode,
1267 struct fiemap_extent_info *fieinfo)
1268{
1269 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1270 struct page *page;
1271 struct node_info ni;
1272 __u64 phys = 0, len;
1273 __u32 flags;
1274 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1275 int err = 0;
1276
1277 if (f2fs_has_inline_xattr(inode)) {
1278 int offset;
1279
1280 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1281 inode->i_ino, false);
1282 if (!page)
1283 return -ENOMEM;
1284
Chao Yu4d57b862018-05-30 00:20:41 +08001285 f2fs_get_node_info(sbi, inode->i_ino, &ni);
Chao Yu442a9db2018-01-11 14:39:57 +08001286
1287 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1288 offset = offsetof(struct f2fs_inode, i_addr) +
1289 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yub323fd22018-01-17 16:31:36 +08001290 get_inline_xattr_addrs(inode));
Chao Yu442a9db2018-01-11 14:39:57 +08001291
1292 phys += offset;
1293 len = inline_xattr_size(inode);
1294
1295 f2fs_put_page(page, 1);
1296
1297 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1298
1299 if (!xnid)
1300 flags |= FIEMAP_EXTENT_LAST;
1301
1302 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1303 if (err || err == 1)
1304 return err;
1305 }
1306
1307 if (xnid) {
1308 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1309 if (!page)
1310 return -ENOMEM;
1311
Chao Yu4d57b862018-05-30 00:20:41 +08001312 f2fs_get_node_info(sbi, xnid, &ni);
Chao Yu442a9db2018-01-11 14:39:57 +08001313
1314 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1315 len = inode->i_sb->s_blocksize;
1316
1317 f2fs_put_page(page, 1);
1318
1319 flags = FIEMAP_EXTENT_LAST;
1320 }
1321
1322 if (phys)
1323 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1324
1325 return (err < 0 ? err : 0);
1326}
1327
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001328int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1329 u64 start, u64 len)
1330{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001331 struct buffer_head map_bh;
1332 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001333 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001334 u64 logical = 0, phys = 0, size = 0;
1335 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001336 int ret = 0;
1337
Chao Yuc4020b22018-01-11 14:42:30 +08001338 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1339 ret = f2fs_precache_extents(inode);
1340 if (ret)
1341 return ret;
1342 }
1343
Chao Yu442a9db2018-01-11 14:39:57 +08001344 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001345 if (ret)
1346 return ret;
1347
Chao Yuf1b43d42018-01-11 14:37:35 +08001348 inode_lock(inode);
1349
Chao Yu442a9db2018-01-11 14:39:57 +08001350 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1351 ret = f2fs_xattr_fiemap(inode, fieinfo);
1352 goto out;
1353 }
1354
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001355 if (f2fs_has_inline_data(inode)) {
1356 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1357 if (ret != -EAGAIN)
Chao Yuf1b43d42018-01-11 14:37:35 +08001358 goto out;
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001359 }
1360
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001361 if (logical_to_blk(inode, len) == 0)
1362 len = blk_to_logical(inode, 1);
1363
1364 start_blk = logical_to_blk(inode, start);
1365 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001366
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001367next:
1368 memset(&map_bh, 0, sizeof(struct buffer_head));
1369 map_bh.b_size = len;
1370
Chao Yue2b4e2b2015-08-19 19:11:19 +08001371 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001372 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001373 if (ret)
1374 goto out;
1375
1376 /* HOLE */
1377 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001378 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001379
1380 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1381 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001382 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001383
Fan Li9a950d52015-12-26 18:07:41 +08001384 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001385 }
Fan Li9a950d52015-12-26 18:07:41 +08001386
Chao Yuda5af122016-01-08 20:19:27 +08001387 if (size) {
1388 if (f2fs_encrypted_inode(inode))
1389 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1390
Fan Li9a950d52015-12-26 18:07:41 +08001391 ret = fiemap_fill_next_extent(fieinfo, logical,
1392 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001393 }
Fan Li9a950d52015-12-26 18:07:41 +08001394
1395 if (start_blk > last_blk || ret)
1396 goto out;
1397
1398 logical = blk_to_logical(inode, start_blk);
1399 phys = blk_to_logical(inode, map_bh.b_blocknr);
1400 size = map_bh.b_size;
1401 flags = 0;
1402 if (buffer_unwritten(&map_bh))
1403 flags = FIEMAP_EXTENT_UNWRITTEN;
1404
1405 start_blk += logical_to_blk(inode, size);
1406
1407prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001408 cond_resched();
1409 if (fatal_signal_pending(current))
1410 ret = -EINTR;
1411 else
1412 goto next;
1413out:
1414 if (ret == 1)
1415 ret = 0;
1416
Al Viro59551022016-01-22 15:40:57 -05001417 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001418 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001419}
1420
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001421/*
1422 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1423 * Major change was from block_size == page_size in f2fs by default.
1424 */
1425static int f2fs_mpage_readpages(struct address_space *mapping,
1426 struct list_head *pages, struct page *page,
1427 unsigned nr_pages)
1428{
1429 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001430 sector_t last_block_in_bio = 0;
1431 struct inode *inode = mapping->host;
1432 const unsigned blkbits = inode->i_blkbits;
1433 const unsigned blocksize = 1 << blkbits;
1434 sector_t block_in_file;
1435 sector_t last_block;
1436 sector_t last_block_in_file;
1437 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001438 struct f2fs_map_blocks map;
1439
1440 map.m_pblk = 0;
1441 map.m_lblk = 0;
1442 map.m_len = 0;
1443 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001444 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08001445 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001446 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001447
LiFan736c0a72017-11-25 11:46:18 +08001448 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001449 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001450 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08001451
1452 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001453 list_del(&page->lru);
1454 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001455 page->index,
1456 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001457 goto next_page;
1458 }
1459
1460 block_in_file = (sector_t)page->index;
1461 last_block = block_in_file + nr_pages;
1462 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1463 blkbits;
1464 if (last_block > last_block_in_file)
1465 last_block = last_block_in_file;
1466
1467 /*
1468 * Map blocks using the previous result first.
1469 */
1470 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1471 block_in_file > map.m_lblk &&
1472 block_in_file < (map.m_lblk + map.m_len))
1473 goto got_it;
1474
1475 /*
1476 * Then do more f2fs_map_blocks() calls until we are
1477 * done with this page.
1478 */
1479 map.m_flags = 0;
1480
1481 if (block_in_file < last_block) {
1482 map.m_lblk = block_in_file;
1483 map.m_len = last_block - block_in_file;
1484
Chao Yu46c9e142015-09-18 16:54:16 +08001485 if (f2fs_map_blocks(inode, &map, 0,
Qiuyang Sunf2220c72017-08-09 17:27:30 +08001486 F2FS_GET_BLOCK_DEFAULT))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001487 goto set_error_page;
1488 }
1489got_it:
1490 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1491 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1492 SetPageMappedToDisk(page);
1493
1494 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1495 SetPageUptodate(page);
1496 goto confused;
1497 }
1498 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001499 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001500 if (!PageUptodate(page))
1501 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001502 unlock_page(page);
1503 goto next_page;
1504 }
1505
1506 /*
1507 * This page will go to BIO. Do we need to send this
1508 * BIO off first?
1509 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001510 if (bio && (last_block_in_bio != block_nr - 1 ||
1511 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001512submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001513 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001514 bio = NULL;
1515 }
1516 if (bio == NULL) {
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001517 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001518 if (IS_ERR(bio)) {
1519 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001520 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001521 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001522 }
1523
1524 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1525 goto submit_and_realloc;
1526
1527 last_block_in_bio = block_nr;
1528 goto next_page;
1529set_error_page:
1530 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001531 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001532 unlock_page(page);
1533 goto next_page;
1534confused:
1535 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001536 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001537 bio = NULL;
1538 }
1539 unlock_page(page);
1540next_page:
1541 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001542 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001543 }
1544 BUG_ON(pages && !list_empty(pages));
1545 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001546 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001547 return 0;
1548}
1549
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001550static int f2fs_read_data_page(struct file *file, struct page *page)
1551{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001552 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001553 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001554
Chao Yuc20e89c2014-05-06 16:53:08 +08001555 trace_f2fs_readpage(page, DATA);
1556
arter97e1c42042014-08-06 23:22:50 +09001557 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001558 if (f2fs_has_inline_data(inode))
1559 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001560 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001561 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001562 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001563}
1564
1565static int f2fs_read_data_pages(struct file *file,
1566 struct address_space *mapping,
1567 struct list_head *pages, unsigned nr_pages)
1568{
Hsiang Kao71cb4af2017-09-24 02:45:42 +08001569 struct inode *inode = mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001570 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001571
1572 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001573
1574 /* If the file has inline data, skip readpages */
1575 if (f2fs_has_inline_data(inode))
1576 return 0;
1577
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001578 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001579}
1580
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001581static int encrypt_one_page(struct f2fs_io_info *fio)
1582{
1583 struct inode *inode = fio->page->mapping->host;
1584 gfp_t gfp_flags = GFP_NOFS;
1585
Jaegeuk Kim19585932017-09-05 16:54:24 -07001586 if (!f2fs_encrypted_file(inode))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001587 return 0;
1588
Eric Biggers6dbb1792018-04-18 11:09:48 -07001589 /* wait for GCed page writeback via META_MAPPING */
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -07001590 f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001591
1592retry_encrypt:
1593 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1594 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1595 if (!IS_ERR(fio->encrypted_page))
1596 return 0;
1597
1598 /* flush pending IOs and wait for a while in the ENOMEM case */
1599 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001600 f2fs_flush_merged_writes(fio->sbi);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001601 congestion_wait(BLK_RW_ASYNC, HZ/50);
1602 gfp_flags |= __GFP_NOFAIL;
1603 goto retry_encrypt;
1604 }
1605 return PTR_ERR(fio->encrypted_page);
1606}
1607
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001608static inline bool check_inplace_update_policy(struct inode *inode,
1609 struct f2fs_io_info *fio)
1610{
1611 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1612 unsigned int policy = SM_I(sbi)->ipu_policy;
1613
1614 if (policy & (0x1 << F2FS_IPU_FORCE))
1615 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08001616 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001617 return true;
1618 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1619 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1620 return true;
Chao Yu4d57b862018-05-30 00:20:41 +08001621 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001622 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1623 return true;
1624
1625 /*
1626 * IPU for rewrite async pages
1627 */
1628 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1629 fio && fio->op == REQ_OP_WRITE &&
1630 !(fio->op_flags & REQ_SYNC) &&
1631 !f2fs_encrypted_inode(inode))
1632 return true;
1633
1634 /* this is only set during fdatasync */
1635 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1636 is_inode_flag_set(inode, FI_NEED_IPU))
1637 return true;
1638
1639 return false;
1640}
1641
Chao Yu4d57b862018-05-30 00:20:41 +08001642bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001643{
1644 if (f2fs_is_pinned_file(inode))
1645 return true;
1646
1647 /* if this is cold file, we should overwrite to avoid fragmentation */
1648 if (file_is_cold(inode))
1649 return true;
1650
1651 return check_inplace_update_policy(inode, fio);
1652}
1653
Chao Yu4d57b862018-05-30 00:20:41 +08001654bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yubb9e3bb8d2018-01-17 16:31:38 +08001655{
1656 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1657
1658 if (test_opt(sbi, LFS))
1659 return true;
1660 if (S_ISDIR(inode->i_mode))
1661 return true;
1662 if (f2fs_is_atomic_file(inode))
1663 return true;
1664 if (fio) {
1665 if (is_cold_data(fio->page))
1666 return true;
1667 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1668 return true;
1669 }
1670 return false;
1671}
1672
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001673static inline bool need_inplace_update(struct f2fs_io_info *fio)
1674{
1675 struct inode *inode = fio->page->mapping->host;
1676
Chao Yu4d57b862018-05-30 00:20:41 +08001677 if (f2fs_should_update_outplace(inode, fio))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001678 return false;
1679
Chao Yu4d57b862018-05-30 00:20:41 +08001680 return f2fs_should_update_inplace(inode, fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001681}
1682
Chao Yu4d57b862018-05-30 00:20:41 +08001683int f2fs_do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001684{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001685 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001686 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001687 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001688 struct extent_info ei = {0,0,0};
1689 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001690 int err = 0;
1691
1692 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00001693 if (need_inplace_update(fio) &&
1694 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1695 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07001696
Chao Yu7b525dd2018-05-23 22:25:08 +08001697 if (is_valid_blkaddr(fio->old_blkaddr)) {
Hou Pengyange959c8f2017-04-25 12:45:13 +00001698 ipu_force = true;
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001699 fio->need_lock = LOCK_DONE;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001700 goto got_it;
1701 }
1702 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001703
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001704 /* Deadlock due to between page->lock and f2fs_lock_op */
1705 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1706 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001707
Chao Yu4d57b862018-05-30 00:20:41 +08001708 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001709 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001710 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001711
Chao Yu28bc1062016-02-06 14:40:34 +08001712 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001713
1714 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001715 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001716 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001717 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001718 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00001719got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001720 /*
1721 * If current allocation needs SSR,
1722 * it had better in-place writes for updated data.
1723 */
Chao Yu7b525dd2018-05-23 22:25:08 +08001724 if (ipu_force || (is_valid_blkaddr(fio->old_blkaddr) &&
1725 need_inplace_update(fio))) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001726 err = encrypt_one_page(fio);
1727 if (err)
1728 goto out_writepage;
1729
1730 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07001731 ClearPageError(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001732 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001733 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001734 f2fs_unlock_op(fio->sbi);
Chao Yu4d57b862018-05-30 00:20:41 +08001735 err = f2fs_inplace_write_data(fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001736 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001737 set_inode_flag(inode, FI_UPDATE_WRITE);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001738 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001739 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001740
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001741 if (fio->need_lock == LOCK_RETRY) {
1742 if (!f2fs_trylock_op(fio->sbi)) {
1743 err = -EAGAIN;
1744 goto out_writepage;
1745 }
1746 fio->need_lock = LOCK_REQ;
1747 }
1748
1749 err = encrypt_one_page(fio);
1750 if (err)
1751 goto out_writepage;
1752
1753 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07001754 ClearPageError(page);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001755
Hou Pengyang279d6df2017-04-27 00:17:21 +08001756 /* LFS mode write path */
Chao Yu4d57b862018-05-30 00:20:41 +08001757 f2fs_outplace_write_data(&dn, fio);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001758 trace_f2fs_do_write_data_page(page, OPU);
1759 set_inode_flag(inode, FI_APPEND_WRITE);
1760 if (page->index == 0)
1761 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001762out_writepage:
1763 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001764out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001765 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001766 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001767 return err;
1768}
1769
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001770static int __write_data_page(struct page *page, bool *submitted,
Chao Yub0af6d42017-08-02 23:21:48 +08001771 struct writeback_control *wbc,
1772 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001773{
1774 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001775 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001776 loff_t i_size = i_size_read(inode);
1777 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001778 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001779 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001780 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001781 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001782 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001783 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001784 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08001785 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001786 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001787 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001788 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00001789 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001790 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001791 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001792 .submitted = false,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001793 .need_lock = LOCK_RETRY,
Chao Yub0af6d42017-08-02 23:21:48 +08001794 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08001795 .io_wbc = wbc,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001796 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001797
Chao Yuecda0de2014-05-06 16:48:26 +08001798 trace_f2fs_writepage(page, DATA);
1799
Chao Yudb198ae2018-01-18 17:29:10 +08001800 /* we should bypass data pages to proceed the kworkder jobs */
1801 if (unlikely(f2fs_cp_error(sbi))) {
1802 mapping_set_error(page->mapping, -EIO);
Chao Yu1174abf2018-05-28 16:59:26 +08001803 /*
1804 * don't drop any dirty dentry pages for keeping lastest
1805 * directory structure.
1806 */
1807 if (S_ISDIR(inode->i_mode))
1808 goto redirty_out;
Chao Yudb198ae2018-01-18 17:29:10 +08001809 goto out;
1810 }
1811
Chao Yu0771fcc2017-06-29 23:20:45 +08001812 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1813 goto redirty_out;
1814
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001815 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001816 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001817
1818 /*
1819 * If the offset is out-of-range of file size,
1820 * this page does not have to be written to disk.
1821 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001822 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001823 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001824 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001825
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001826 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001827write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001828 if (f2fs_is_drop_cache(inode))
1829 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001830 /* we should not write 0'th page having journal header */
1831 if (f2fs_is_volatile_file(inode) && (!page->index ||
1832 (!wbc->for_reclaim &&
Chao Yu4d57b862018-05-30 00:20:41 +08001833 f2fs_available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001834 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001835
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001836 /* Dentry blocks are controlled by checkpoint */
1837 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001838 fio.need_lock = LOCK_DONE;
Chao Yu4d57b862018-05-30 00:20:41 +08001839 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001840 goto done;
1841 }
1842
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001843 if (!wbc->for_reclaim)
1844 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001845 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001846 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001847 else
1848 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001849
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001850 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08001851 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001852 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08001853 if (!err)
1854 goto out;
1855 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001856
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001857 if (err == -EAGAIN) {
Chao Yu4d57b862018-05-30 00:20:41 +08001858 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001859 if (err == -EAGAIN) {
1860 fio.need_lock = LOCK_REQ;
Chao Yu4d57b862018-05-30 00:20:41 +08001861 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001862 }
1863 }
Chao Yua0d00fa2017-10-09 17:55:19 +08001864
Chao Yueb449792018-01-17 16:31:37 +08001865 if (err) {
1866 file_set_keep_isize(inode);
1867 } else {
1868 down_write(&F2FS_I(inode)->i_sem);
1869 if (F2FS_I(inode)->last_disk_size < psize)
1870 F2FS_I(inode)->last_disk_size = psize;
1871 up_write(&F2FS_I(inode)->i_sem);
1872 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001873
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001874done:
1875 if (err && err != -ENOENT)
1876 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001877
Jaegeuk Kim39936832012-11-22 16:21:29 +09001878out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001879 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001880 if (err)
1881 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001882
1883 if (wbc->for_reclaim) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001884 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001885 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu4d57b862018-05-30 00:20:41 +08001886 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001887 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001888 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001889
1890 unlock_page(page);
Jaegeuk Kima7881892017-04-20 13:51:57 -07001891 if (!S_ISDIR(inode->i_mode))
1892 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001893
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001894 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001895 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001896 submitted = NULL;
1897 }
1898
1899 if (submitted)
1900 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001901
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001902 return 0;
1903
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001904redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001905 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim5b19d282018-05-03 23:26:02 -07001906 /*
1907 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
1908 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
1909 * file_write_and_wait_range() will see EIO error, which is critical
1910 * to return value of fsync() followed by atomic_write failure to user.
1911 */
1912 if (!err || wbc->for_reclaim)
Chao Yu0002b612016-11-28 19:13:43 -08001913 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001914 unlock_page(page);
1915 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001916}
1917
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001918static int f2fs_write_data_page(struct page *page,
1919 struct writeback_control *wbc)
1920{
Chao Yub0af6d42017-08-02 23:21:48 +08001921 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001922}
1923
Chao Yu8f46dca2015-07-14 18:56:10 +08001924/*
1925 * This function was copied from write_cche_pages from mm/page-writeback.c.
1926 * The major change is making write step of cold data page separately from
1927 * warm/hot data page.
1928 */
1929static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08001930 struct writeback_control *wbc,
1931 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08001932{
1933 int ret = 0;
1934 int done = 0;
1935 struct pagevec pvec;
1936 int nr_pages;
1937 pgoff_t uninitialized_var(writeback_index);
1938 pgoff_t index;
1939 pgoff_t end; /* Inclusive */
1940 pgoff_t done_index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001941 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001942 int cycled;
1943 int range_whole = 0;
1944 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001945
Mel Gorman86679822017-11-15 17:37:52 -08001946 pagevec_init(&pvec);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001947
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001948 if (get_dirty_pages(mapping->host) <=
1949 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1950 set_inode_flag(mapping->host, FI_HOT_DATA);
1951 else
1952 clear_inode_flag(mapping->host, FI_HOT_DATA);
1953
Chao Yu8f46dca2015-07-14 18:56:10 +08001954 if (wbc->range_cyclic) {
1955 writeback_index = mapping->writeback_index; /* prev offset */
1956 index = writeback_index;
1957 if (index == 0)
1958 cycled = 1;
1959 else
1960 cycled = 0;
1961 end = -1;
1962 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001963 index = wbc->range_start >> PAGE_SHIFT;
1964 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001965 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1966 range_whole = 1;
1967 cycled = 1; /* ignore range_cyclic tests */
1968 }
1969 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1970 tag = PAGECACHE_TAG_TOWRITE;
1971 else
1972 tag = PAGECACHE_TAG_DIRTY;
1973retry:
1974 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1975 tag_pages_for_writeback(mapping, index, end);
1976 done_index = index;
1977 while (!done && (index <= end)) {
1978 int i;
1979
Jan Kara69c4f352017-11-15 17:34:48 -08001980 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08001981 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08001982 if (nr_pages == 0)
1983 break;
1984
1985 for (i = 0; i < nr_pages; i++) {
1986 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001987 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001988
Chao Yuf8de4332018-05-23 22:25:09 +08001989 /* give a priority to WB_SYNC threads */
1990 if (atomic_read(&F2FS_M_SB(mapping)->wb_sync_req) &&
1991 wbc->sync_mode == WB_SYNC_NONE) {
1992 done = 1;
1993 break;
1994 }
1995
Chao Yu8f46dca2015-07-14 18:56:10 +08001996 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001997retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08001998 lock_page(page);
1999
2000 if (unlikely(page->mapping != mapping)) {
2001continue_unlock:
2002 unlock_page(page);
2003 continue;
2004 }
2005
2006 if (!PageDirty(page)) {
2007 /* someone wrote it for us */
2008 goto continue_unlock;
2009 }
2010
Chao Yu8f46dca2015-07-14 18:56:10 +08002011 if (PageWriteback(page)) {
2012 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002013 f2fs_wait_on_page_writeback(page,
2014 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08002015 else
2016 goto continue_unlock;
2017 }
2018
2019 BUG_ON(PageWriteback(page));
2020 if (!clear_page_dirty_for_io(page))
2021 goto continue_unlock;
2022
Chao Yub0af6d42017-08-02 23:21:48 +08002023 ret = __write_data_page(page, &submitted, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08002024 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08002025 /*
2026 * keep nr_to_write, since vfs uses this to
2027 * get # of written pages.
2028 */
2029 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2030 unlock_page(page);
2031 ret = 0;
2032 continue;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002033 } else if (ret == -EAGAIN) {
2034 ret = 0;
2035 if (wbc->sync_mode == WB_SYNC_ALL) {
2036 cond_resched();
2037 congestion_wait(BLK_RW_ASYNC,
2038 HZ/50);
2039 goto retry_write;
2040 }
2041 continue;
Chao Yu0002b612016-11-28 19:13:43 -08002042 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002043 done_index = page->index + 1;
2044 done = 1;
2045 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002046 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08002047 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08002048 }
2049
Chao Yuf8de4332018-05-23 22:25:09 +08002050 if (--wbc->nr_to_write <= 0 &&
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002051 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08002052 done = 1;
2053 break;
2054 }
2055 }
2056 pagevec_release(&pvec);
2057 cond_resched();
2058 }
2059
Chao Yu8f46dca2015-07-14 18:56:10 +08002060 if (!cycled && !done) {
2061 cycled = 1;
2062 index = 0;
2063 end = writeback_index - 1;
2064 goto retry;
2065 }
2066 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2067 mapping->writeback_index = done_index;
2068
Jaegeuk Kim942fd312017-02-01 16:51:22 -08002069 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002070 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
2071 0, last_idx, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08002072
Chao Yu8f46dca2015-07-14 18:56:10 +08002073 return ret;
2074}
2075
Chao Yufc99fe22018-05-30 00:20:39 +08002076static int __f2fs_write_data_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08002077 struct writeback_control *wbc,
2078 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002079{
2080 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002081 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002082 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002083 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002084
P J Pcfb185a2013-04-03 11:38:00 +09002085 /* deal with chardevs and other special file */
2086 if (!mapping->a_ops->writepage)
2087 return 0;
2088
Chao Yu6a290542015-07-17 18:02:39 +08002089 /* skip writing if there is no dirty page in this inode */
2090 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2091 return 0;
2092
Chao Yu0771fcc2017-06-29 23:20:45 +08002093 /* during POR, we don't need to trigger writepage at all. */
2094 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2095 goto skip_write;
2096
Jaegeuk Kima1257022015-10-08 10:40:07 -07002097 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
2098 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Chao Yu4d57b862018-05-30 00:20:41 +08002099 f2fs_available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kima1257022015-10-08 10:40:07 -07002100 goto skip_write;
2101
Chao Yud323d002015-10-27 09:53:45 +08002102 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07002103 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08002104 goto skip_write;
2105
Yunlei Hed31c7c32016-02-04 16:14:00 +08002106 trace_f2fs_writepages(mapping->host, wbc, DATA);
2107
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002108 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2109 if (wbc->sync_mode == WB_SYNC_ALL)
2110 atomic_inc(&sbi->wb_sync_req);
2111 else if (atomic_read(&sbi->wb_sync_req))
2112 goto skip_write;
2113
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002114 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08002115 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002116 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002117
2118 if (wbc->sync_mode == WB_SYNC_ALL)
2119 atomic_dec(&sbi->wb_sync_req);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07002120 /*
2121 * if some pages were truncated, we cannot guarantee its mapping->host
2122 * to detect pending bios.
2123 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002124
Chao Yu4d57b862018-05-30 00:20:41 +08002125 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002126 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002127
2128skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002129 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08002130 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002131 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002132}
2133
Chao Yub0af6d42017-08-02 23:21:48 +08002134static int f2fs_write_data_pages(struct address_space *mapping,
2135 struct writeback_control *wbc)
2136{
2137 struct inode *inode = mapping->host;
2138
2139 return __f2fs_write_data_pages(mapping, wbc,
2140 F2FS_I(inode)->cp_task == current ?
2141 FS_CP_DATA_IO : FS_DATA_IO);
2142}
2143
Chao Yu3aab8f82014-07-02 13:25:04 +08002144static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2145{
2146 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002147 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002148
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002149 if (to > i_size) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08002150 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002151 truncate_pagecache(inode, i_size);
Chao Yu4d57b862018-05-30 00:20:41 +08002152 f2fs_truncate_blocks(inode, i_size, true);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08002153 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3aab8f82014-07-02 13:25:04 +08002154 }
2155}
2156
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002157static int prepare_write_begin(struct f2fs_sb_info *sbi,
2158 struct page *page, loff_t pos, unsigned len,
2159 block_t *blk_addr, bool *node_changed)
2160{
2161 struct inode *inode = page->mapping->host;
2162 pgoff_t index = page->index;
2163 struct dnode_of_data dn;
2164 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002165 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00002166 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002167 int err = 0;
2168
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002169 /*
2170 * we already allocated all the blocks, so we don't need to get
2171 * the block addresses when there is no need to fill the page.
2172 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002173 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2174 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002175 return 0;
2176
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002177 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002178 (pos & PAGE_MASK) >= i_size_read(inode)) {
Yunlei He59c90812017-03-13 20:22:18 +08002179 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002180 locked = true;
2181 }
2182restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002183 /* check inline_data */
Chao Yu4d57b862018-05-30 00:20:41 +08002184 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002185 if (IS_ERR(ipage)) {
2186 err = PTR_ERR(ipage);
2187 goto unlock_out;
2188 }
2189
2190 set_new_dnode(&dn, inode, ipage, ipage, 0);
2191
2192 if (f2fs_has_inline_data(inode)) {
Chao Yuf2470372017-07-19 00:19:05 +08002193 if (pos + len <= MAX_INLINE_DATA(inode)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002194 f2fs_do_read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002195 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08002196 if (inode->i_nlink)
2197 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002198 } else {
2199 err = f2fs_convert_inline_page(&dn, page);
2200 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002201 goto out;
2202 if (dn.data_blkaddr == NULL_ADDR)
2203 err = f2fs_get_block(&dn, index);
2204 }
2205 } else if (locked) {
2206 err = f2fs_get_block(&dn, index);
2207 } else {
2208 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2209 dn.data_blkaddr = ei.blk + index - ei.fofs;
2210 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002211 /* hole case */
Chao Yu4d57b862018-05-30 00:20:41 +08002212 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07002213 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002214 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08002215 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2216 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002217 locked = true;
2218 goto restart;
2219 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002220 }
2221 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002222
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002223 /* convert_inline_page can make node_changed */
2224 *blk_addr = dn.data_blkaddr;
2225 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002226out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002227 f2fs_put_dnode(&dn);
2228unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002229 if (locked)
Yunlei He59c90812017-03-13 20:22:18 +08002230 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002231 return err;
2232}
2233
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002234static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2235 loff_t pos, unsigned len, unsigned flags,
2236 struct page **pagep, void **fsdata)
2237{
2238 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002239 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002240 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002241 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yua2e2e762018-01-15 17:16:46 +08002242 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002243 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002244 int err = 0;
2245
Chao Yu62aed042014-05-06 16:46:04 +08002246 trace_f2fs_write_begin(inode, pos, len, flags);
2247
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002248 if (f2fs_is_atomic_file(inode) &&
Chao Yu4d57b862018-05-30 00:20:41 +08002249 !f2fs_available_free_memory(sbi, INMEM_PAGES)) {
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002250 err = -ENOMEM;
Chao Yua2e2e762018-01-15 17:16:46 +08002251 drop_atomic = true;
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002252 goto fail;
2253 }
2254
Jaegeuk Kim5f727392014-11-25 10:59:45 -08002255 /*
2256 * We should check this at this moment to avoid deadlock on inode page
2257 * and #0 page. The locking rule for inline_data conversion should be:
2258 * lock_page(page #0) -> lock_page(inode_page)
2259 */
2260 if (index != 0) {
2261 err = f2fs_convert_inline_inode(inode);
2262 if (err)
2263 goto fail;
2264 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002265repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002266 /*
2267 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2268 * wait_for_stable_page. Will wait that below with our IO control.
2269 */
Chao Yu01eccef2017-10-28 16:52:30 +08002270 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002271 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08002272 if (!page) {
2273 err = -ENOMEM;
2274 goto fail;
2275 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09002276
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002277 *pagep = page;
2278
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002279 err = prepare_write_begin(sbi, page, pos, len,
2280 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002281 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002282 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002283
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002284 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002285 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002286 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002287 lock_page(page);
2288 if (page->mapping != mapping) {
2289 /* The page got truncated from under us */
2290 f2fs_put_page(page, 1);
2291 goto repeat;
2292 }
2293 }
2294
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002295 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002296
Eric Biggers6dbb1792018-04-18 11:09:48 -07002297 /* wait for GCed page writeback via META_MAPPING */
2298 if (f2fs_post_read_required(inode))
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -07002299 f2fs_wait_on_block_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08002300
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002301 if (len == PAGE_SIZE || PageUptodate(page))
2302 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002303
Yunlei He746e2402016-12-20 11:11:35 +08002304 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2305 zero_user_segment(page, len, PAGE_SIZE);
2306 return 0;
2307 }
2308
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002309 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002310 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002311 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002312 } else {
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07002313 err = f2fs_submit_page_read(inode, page, blkaddr);
2314 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08002315 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08002316
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002317 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002318 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002319 f2fs_put_page(page, 1);
2320 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002321 }
Chao Yu1563ac72016-07-03 22:05:12 +08002322 if (unlikely(!PageUptodate(page))) {
2323 err = -EIO;
2324 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002325 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002326 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002327 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002328
Chao Yu3aab8f82014-07-02 13:25:04 +08002329fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002330 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002331 f2fs_write_failed(mapping, pos + len);
Chao Yua2e2e762018-01-15 17:16:46 +08002332 if (drop_atomic)
Chao Yu4d57b862018-05-30 00:20:41 +08002333 f2fs_drop_inmem_pages_all(sbi, false);
Chao Yu3aab8f82014-07-02 13:25:04 +08002334 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002335}
2336
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002337static int f2fs_write_end(struct file *file,
2338 struct address_space *mapping,
2339 loff_t pos, unsigned len, unsigned copied,
2340 struct page *page, void *fsdata)
2341{
2342 struct inode *inode = page->mapping->host;
2343
Chao Yudfb2bf32014-05-06 16:47:23 +08002344 trace_f2fs_write_end(inode, pos, len, copied);
2345
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002346 /*
2347 * This should be come from len == PAGE_SIZE, and we expect copied
2348 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2349 * let generic_perform_write() try to copy data again through copied=0.
2350 */
2351 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08002352 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002353 copied = 0;
2354 else
2355 SetPageUptodate(page);
2356 }
2357 if (!copied)
2358 goto unlock_out;
2359
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002360 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002361
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002362 if (pos + copied > i_size_read(inode))
2363 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002364unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002365 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002366 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002367 return copied;
2368}
2369
Omar Sandoval6f673762015-03-16 04:33:52 -07002370static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2371 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002372{
2373 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002374
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002375 if (offset & blocksize_mask)
2376 return -EINVAL;
2377
Al Viro5b46f252014-03-16 18:07:34 -04002378 if (iov_iter_alignment(iter) & blocksize_mask)
2379 return -EINVAL;
2380
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002381 return 0;
2382}
2383
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002384static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002385{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002386 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002387 struct inode *inode = mapping->host;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002388 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002389 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002390 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002391 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002392 int err;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002393 enum rw_hint hint = iocb->ki_hint;
Chao Yu63189b72018-03-08 14:22:56 +08002394 int whint_mode = F2FS_OPTION(sbi).whint_mode;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002395
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002396 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002397 if (err)
2398 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002399
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002400 if (f2fs_force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002401 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002402
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002403 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002404
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002405 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2406 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2407
Chao Yub2532c62018-04-24 10:55:28 +08002408 if (!down_read_trylock(&F2FS_I(inode)->i_gc_rwsem[rw])) {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002409 if (iocb->ki_flags & IOCB_NOWAIT) {
2410 iocb->ki_hint = hint;
2411 err = -EAGAIN;
2412 goto out;
2413 }
Chao Yub2532c62018-04-24 10:55:28 +08002414 down_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002415 }
2416
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002417 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yub2532c62018-04-24 10:55:28 +08002418 up_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002419
2420 if (rw == WRITE) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002421 if (whint_mode == WHINT_MODE_OFF)
2422 iocb->ki_hint = hint;
Chao Yub0af6d42017-08-02 23:21:48 +08002423 if (err > 0) {
2424 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2425 err);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002426 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yub0af6d42017-08-02 23:21:48 +08002427 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002428 f2fs_write_failed(mapping, offset + count);
Chao Yub0af6d42017-08-02 23:21:48 +08002429 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002430 }
Chao Yu70407fa2014-07-31 21:11:22 +08002431
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002432out:
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002433 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002434
Chao Yu3aab8f82014-07-02 13:25:04 +08002435 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002436}
2437
Chao Yu487261f2015-02-05 17:44:29 +08002438void f2fs_invalidate_page(struct page *page, unsigned int offset,
2439 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002440{
2441 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002442 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002443
Chao Yu487261f2015-02-05 17:44:29 +08002444 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002445 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002446 return;
2447
Chao Yu487261f2015-02-05 17:44:29 +08002448 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08002449 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002450 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08002451 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002452 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08002453 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002454 inode_dec_dirty_pages(inode);
Chao Yu4d57b862018-05-30 00:20:41 +08002455 f2fs_remove_dirty_inode(inode);
Chao Yu933439c2016-10-11 22:57:01 +08002456 }
Chao Yu487261f2015-02-05 17:44:29 +08002457 }
Chao Yudecd36b2015-08-07 18:42:09 +08002458
2459 /* This is atomic written page, keep Private */
2460 if (IS_ATOMIC_WRITTEN_PAGE(page))
Chao Yu4d57b862018-05-30 00:20:41 +08002461 return f2fs_drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002462
Chao Yu23dc9742016-04-29 20:09:15 +08002463 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002464 ClearPagePrivate(page);
2465}
2466
Chao Yu487261f2015-02-05 17:44:29 +08002467int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002468{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002469 /* If this is dirty page, keep PagePrivate */
2470 if (PageDirty(page))
2471 return 0;
2472
Chao Yudecd36b2015-08-07 18:42:09 +08002473 /* This is atomic written page, keep Private */
2474 if (IS_ATOMIC_WRITTEN_PAGE(page))
2475 return 0;
2476
Chao Yu23dc9742016-04-29 20:09:15 +08002477 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002478 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002479 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002480}
2481
2482static int f2fs_set_data_page_dirty(struct page *page)
2483{
2484 struct address_space *mapping = page->mapping;
2485 struct inode *inode = mapping->host;
2486
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002487 trace_f2fs_set_page_dirty(page, DATA);
2488
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002489 if (!PageUptodate(page))
2490 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002491
Chao Yu5fe45742017-01-07 18:50:26 +08002492 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002493 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
Chao Yu4d57b862018-05-30 00:20:41 +08002494 f2fs_register_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002495 return 1;
2496 }
2497 /*
2498 * Previously, this page has been registered, we just
2499 * return here.
2500 */
2501 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002502 }
2503
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002504 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07002505 __set_page_dirty_nobuffers(page);
Chao Yu4d57b862018-05-30 00:20:41 +08002506 f2fs_update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002507 return 1;
2508 }
2509 return 0;
2510}
2511
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002512static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2513{
Chao Yu454ae7e2014-04-22 13:34:01 +08002514 struct inode *inode = mapping->host;
2515
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002516 if (f2fs_has_inline_data(inode))
2517 return 0;
2518
2519 /* make sure allocating whole blocks */
2520 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2521 filemap_write_and_wait(mapping);
2522
Chao Yue2b4e2b2015-08-19 19:11:19 +08002523 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002524}
2525
Weichao Guo5b7a4872016-09-20 05:03:27 +08002526#ifdef CONFIG_MIGRATION
2527#include <linux/migrate.h>
2528
2529int f2fs_migrate_page(struct address_space *mapping,
2530 struct page *newpage, struct page *page, enum migrate_mode mode)
2531{
2532 int rc, extra_count;
2533 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2534 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2535
2536 BUG_ON(PageWriteback(page));
2537
2538 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07002539 if (atomic_written) {
2540 if (mode != MIGRATE_SYNC)
2541 return -EBUSY;
2542 if (!mutex_trylock(&fi->inmem_lock))
2543 return -EAGAIN;
2544 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002545
2546 /*
2547 * A reference is expected if PagePrivate set when move mapping,
2548 * however F2FS breaks this for maintaining dirty page counts when
2549 * truncating pages. So here adjusting the 'extra_count' make it work.
2550 */
2551 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2552 rc = migrate_page_move_mapping(mapping, newpage,
2553 page, NULL, mode, extra_count);
2554 if (rc != MIGRATEPAGE_SUCCESS) {
2555 if (atomic_written)
2556 mutex_unlock(&fi->inmem_lock);
2557 return rc;
2558 }
2559
2560 if (atomic_written) {
2561 struct inmem_pages *cur;
2562 list_for_each_entry(cur, &fi->inmem_pages, list)
2563 if (cur->page == page) {
2564 cur->page = newpage;
2565 break;
2566 }
2567 mutex_unlock(&fi->inmem_lock);
2568 put_page(page);
2569 get_page(newpage);
2570 }
2571
2572 if (PagePrivate(page))
2573 SetPagePrivate(newpage);
2574 set_page_private(newpage, page_private(page));
2575
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07002576 if (mode != MIGRATE_SYNC_NO_COPY)
2577 migrate_page_copy(newpage, page);
2578 else
2579 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002580
2581 return MIGRATEPAGE_SUCCESS;
2582}
2583#endif
2584
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002585const struct address_space_operations f2fs_dblock_aops = {
2586 .readpage = f2fs_read_data_page,
2587 .readpages = f2fs_read_data_pages,
2588 .writepage = f2fs_write_data_page,
2589 .writepages = f2fs_write_data_pages,
2590 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002591 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002592 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002593 .invalidatepage = f2fs_invalidate_page,
2594 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002595 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002596 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002597#ifdef CONFIG_MIGRATION
2598 .migratepage = f2fs_migrate_page,
2599#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002600};
Eric Biggers6dbb1792018-04-18 11:09:48 -07002601
Chao Yu4d57b862018-05-30 00:20:41 +08002602void f2fs_clear_radix_tree_dirty_tag(struct page *page)
Chao Yuaec2f722018-05-26 18:03:35 +08002603{
2604 struct address_space *mapping = page_mapping(page);
2605 unsigned long flags;
2606
2607 xa_lock_irqsave(&mapping->i_pages, flags);
2608 radix_tree_tag_clear(&mapping->i_pages, page_index(page),
2609 PAGECACHE_TAG_DIRTY);
2610 xa_unlock_irqrestore(&mapping->i_pages, flags);
2611}
2612
Eric Biggers6dbb1792018-04-18 11:09:48 -07002613int __init f2fs_init_post_read_processing(void)
2614{
2615 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0);
2616 if (!bio_post_read_ctx_cache)
2617 goto fail;
2618 bio_post_read_ctx_pool =
2619 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
2620 bio_post_read_ctx_cache);
2621 if (!bio_post_read_ctx_pool)
2622 goto fail_free_cache;
2623 return 0;
2624
2625fail_free_cache:
2626 kmem_cache_destroy(bio_post_read_ctx_cache);
2627fail:
2628 return -ENOMEM;
2629}
2630
2631void __exit f2fs_destroy_post_read_processing(void)
2632{
2633 mempool_destroy(bio_post_read_ctx_pool);
2634 kmem_cache_destroy(bio_post_read_ctx_cache);
2635}