blob: 31c2edb217ec0124207b8f71423a6a39a313645c [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;
250 bio->bi_write_hint = io_type_to_rw_hint(sbi, type, temp);
251 }
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
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700465int 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 Kim0a595eb2016-12-14 10:12:56 -0800471 int err = 0;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900472
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700473 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900474
Chao Yufb830fc2017-05-19 23:37:01 +0800475 down_write(&io->io_rwsem);
476next:
477 if (fio->in_list) {
478 spin_lock(&io->io_lock);
479 if (list_empty(&io->io_list)) {
480 spin_unlock(&io->io_lock);
481 goto out_fail;
482 }
483 fio = list_first_entry(&io->io_list,
484 struct f2fs_io_info, list);
485 list_del(&fio->list);
486 spin_unlock(&io->io_lock);
487 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900488
Chao Yu7b525dd2018-05-23 22:25:08 +0800489 if (is_valid_blkaddr(fio->old_blkaddr))
Yunlei He08337212018-03-08 16:29:13 +0800490 verify_block_addr(fio, fio->old_blkaddr);
491 verify_block_addr(fio, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900492
Chao Yu36951b32016-11-16 10:41:20 +0800493 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
494
Thomas Meyerebf7c522017-10-07 16:02:21 +0200495 /* set submitted = true as a return value */
496 fio->submitted = true;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800497
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700498 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900499
Chao Yu7a9d7542016-02-22 18:36:38 +0800500 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700501 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
502 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900503 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900504alloc_new:
505 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800506 if ((fio->type == DATA || fio->type == NODE) &&
507 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
508 err = -EAGAIN;
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700509 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800510 goto out_fail;
511 }
Yufen Yu578c6472018-01-09 19:33:39 +0800512 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900513 BIO_MAX_PAGES, false,
514 fio->type, fio->temp);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900515 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900516 }
517
Jaegeuk Kima912b542017-05-10 11:18:25 -0700518 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900519 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900520 goto alloc_new;
521 }
522
Yufen Yu578c6472018-01-09 19:33:39 +0800523 if (fio->io_wbc)
524 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
525
Chao Yu7a9d7542016-02-22 18:36:38 +0800526 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700527 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800528
529 trace_f2fs_submit_page_write(fio->page, fio);
530
531 if (fio->in_list)
532 goto next;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800533out_fail:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800534 up_write(&io->io_rwsem);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800535 return err;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900536}
537
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700538static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
539 unsigned nr_pages)
540{
541 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700542 struct bio *bio;
Eric Biggers6dbb1792018-04-18 11:09:48 -0700543 struct bio_post_read_ctx *ctx;
544 unsigned int post_read_steps = 0;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700545
Eric Biggers6dbb1792018-04-18 11:09:48 -0700546 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
547 if (!bio)
548 return ERR_PTR(-ENOMEM);
549 f2fs_target_device(sbi, blkaddr, bio);
550 bio->bi_end_io = f2fs_read_end_io;
551 bio_set_op_attrs(bio, REQ_OP_READ, 0);
552
553 if (f2fs_encrypted_file(inode))
554 post_read_steps |= 1 << STEP_DECRYPT;
555 if (post_read_steps) {
556 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
557 if (!ctx) {
558 bio_put(bio);
559 return ERR_PTR(-ENOMEM);
560 }
561 ctx->bio = bio;
562 ctx->enabled_steps = post_read_steps;
563 bio->bi_private = ctx;
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700564
565 /* wait the page to be moved by cleaning */
566 f2fs_wait_on_block_writeback(sbi, blkaddr);
567 }
568
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700569 return bio;
570}
571
572/* This can handle encryption stuffs */
573static int f2fs_submit_page_read(struct inode *inode, struct page *page,
574 block_t blkaddr)
575{
576 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1);
577
578 if (IS_ERR(bio))
579 return PTR_ERR(bio);
580
581 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
582 bio_put(bio);
583 return -EFAULT;
584 }
585 __submit_bio(F2FS_I_SB(inode), bio, DATA);
586 return 0;
587}
588
Chao Yu46008c62016-05-09 19:56:30 +0800589static void __set_data_blkaddr(struct dnode_of_data *dn)
590{
591 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
592 __le32 *addr_array;
Chao Yu7a2af762017-07-19 00:19:06 +0800593 int base = 0;
594
595 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
596 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800597
598 /* Get physical address of data block */
599 addr_array = blkaddr_in_node(rn);
Chao Yu7a2af762017-07-19 00:19:06 +0800600 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800601}
602
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900603/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900604 * Lock ordering for the change of data block address:
605 * ->data_page
606 * ->node_page
607 * update block addresses in the node page
608 */
Chao Yu216a6202015-03-19 19:23:32 +0800609void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900610{
Chao Yu46008c62016-05-09 19:56:30 +0800611 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
612 __set_data_blkaddr(dn);
613 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800614 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900615}
616
Chao Yuf28b3432016-02-24 17:16:47 +0800617void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
618{
619 dn->data_blkaddr = blkaddr;
620 set_data_blkaddr(dn);
621 f2fs_update_extent_cache(dn);
622}
623
Chao Yu46008c62016-05-09 19:56:30 +0800624/* dn->ofs_in_node will be returned with up-to-date last block pointer */
625int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900626{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700627 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800628 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900629
Chao Yu46008c62016-05-09 19:56:30 +0800630 if (!count)
631 return 0;
632
Jaegeuk Kim91942322016-05-20 10:13:22 -0700633 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900634 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +0800635 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
636 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900637
Chao Yu46008c62016-05-09 19:56:30 +0800638 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
639 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900640
Chao Yu46008c62016-05-09 19:56:30 +0800641 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
642
643 for (; count > 0; dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800644 block_t blkaddr = datablock_addr(dn->inode,
645 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800646 if (blkaddr == NULL_ADDR) {
647 dn->data_blkaddr = NEW_ADDR;
648 __set_data_blkaddr(dn);
649 count--;
650 }
651 }
652
653 if (set_page_dirty(dn->node_page))
654 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900655 return 0;
656}
657
Chao Yu46008c62016-05-09 19:56:30 +0800658/* Should keep dn->ofs_in_node unchanged */
659int reserve_new_block(struct dnode_of_data *dn)
660{
661 unsigned int ofs_in_node = dn->ofs_in_node;
662 int ret;
663
664 ret = reserve_new_blocks(dn, 1);
665 dn->ofs_in_node = ofs_in_node;
666 return ret;
667}
668
Huajun Lib6009652013-11-10 23:13:18 +0800669int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
670{
671 bool need_put = dn->inode_page ? false : true;
672 int err;
673
674 err = get_dnode_of_data(dn, index, ALLOC_NODE);
675 if (err)
676 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900677
Huajun Lib6009652013-11-10 23:13:18 +0800678 if (dn->data_blkaddr == NULL_ADDR)
679 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900680 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800681 f2fs_put_dnode(dn);
682 return err;
683}
684
Fan Li759af1c2015-08-05 15:52:16 +0800685int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900686{
Hou Pengyange15882b2017-02-23 09:18:05 +0000687 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800688 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800689
Fan Li759af1c2015-08-05 15:52:16 +0800690 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
691 dn->data_blkaddr = ei.blk + index - ei.fofs;
692 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800693 }
694
Fan Li759af1c2015-08-05 15:52:16 +0800695 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900696}
697
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700698struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500699 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900700{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900701 struct address_space *mapping = inode->i_mapping;
702 struct dnode_of_data dn;
703 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +0000704 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900705 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700706
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700707 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900708 if (!page)
709 return ERR_PTR(-ENOMEM);
710
Chao Yucb3bc9e2015-02-05 18:03:40 +0800711 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
712 dn.data_blkaddr = ei.blk + index - ei.fofs;
713 goto got_it;
714 }
715
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900716 set_new_dnode(&dn, inode, NULL, NULL, 0);
717 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700718 if (err)
719 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900720 f2fs_put_dnode(&dn);
721
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900722 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700723 err = -ENOENT;
724 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900725 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800726got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700727 if (PageUptodate(page)) {
728 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900729 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700730 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900731
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900732 /*
733 * A new dentry page is allocated but not able to be written, since its
734 * new inode page couldn't be allocated due to -ENOSPC.
735 * In such the case, its blkaddr can be remained as NEW_ADDR.
736 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
737 */
738 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300739 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700740 if (!PageUptodate(page))
741 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700742 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900743 return page;
744 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900745
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700746 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900747 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700748 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700749 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700750
751put_err:
752 f2fs_put_page(page, 1);
753 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700754}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900755
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700756struct page *find_data_page(struct inode *inode, pgoff_t index)
757{
758 struct address_space *mapping = inode->i_mapping;
759 struct page *page;
760
761 page = find_get_page(mapping, index);
762 if (page && PageUptodate(page))
763 return page;
764 f2fs_put_page(page, 0);
765
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600766 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700767 if (IS_ERR(page))
768 return page;
769
770 if (PageUptodate(page))
771 return page;
772
773 wait_on_page_locked(page);
774 if (unlikely(!PageUptodate(page))) {
775 f2fs_put_page(page, 0);
776 return ERR_PTR(-EIO);
777 }
778 return page;
779}
780
781/*
782 * If it tries to access a hole, return an error.
783 * Because, the callers, functions in dir.c and GC, should be able to know
784 * whether this page exists or not.
785 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700786struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
787 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700788{
789 struct address_space *mapping = inode->i_mapping;
790 struct page *page;
791repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600792 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700793 if (IS_ERR(page))
794 return page;
795
796 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900797 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900798 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900799 f2fs_put_page(page, 1);
800 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900801 }
Chao Yu1563ac72016-07-03 22:05:12 +0800802 if (unlikely(!PageUptodate(page))) {
803 f2fs_put_page(page, 1);
804 return ERR_PTR(-EIO);
805 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900806 return page;
807}
808
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900809/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900810 * Caller ensures that this data page is never allocated.
811 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900812 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800813 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
814 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800815 * Note that, ipage is set only by make_empty_dir, and if any error occur,
816 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900817 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900818struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900819 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900820{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900821 struct address_space *mapping = inode->i_mapping;
822 struct page *page;
823 struct dnode_of_data dn;
824 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800825
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700826 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800827 if (!page) {
828 /*
829 * before exiting, we should make sure ipage will be released
830 * if any error occur.
831 */
832 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700833 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800834 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900835
Jaegeuk Kima8865372013-12-27 17:04:17 +0900836 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800837 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700838 if (err) {
839 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900840 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900841 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700842 if (!ipage)
843 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900844
845 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700846 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900847
848 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300849 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700850 if (!PageUptodate(page))
851 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900852 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700853 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900854
Jaegeuk Kim76121182016-01-01 22:03:47 -0800855 /* if ipage exists, blkaddr should be NEW_ADDR */
856 f2fs_bug_on(F2FS_I_SB(inode), ipage);
857 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700858 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800859 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900860 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700861got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800862 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700863 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700864 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900865 return page;
866}
867
Hyunchul Leed5097be2017-11-28 09:23:00 +0900868static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900869{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700870 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900871 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900872 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700873 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800874 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +0800875 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900876
Jaegeuk Kim91942322016-05-20 10:13:22 -0700877 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900878 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800879
Chao Yu7a2af762017-07-19 00:19:06 +0800880 dn->data_blkaddr = datablock_addr(dn->inode,
881 dn->node_page, dn->ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800882 if (dn->data_blkaddr == NEW_ADDR)
883 goto alloc;
884
Chao Yu0abd6752017-07-09 00:13:07 +0800885 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
886 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900887
Chao Yudf6136e2015-03-23 10:33:37 +0800888alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900889 get_node_info(sbi, dn->nid, &ni);
890 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
891
Chao Yudf6136e2015-03-23 10:33:37 +0800892 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Hyunchul Leed5097be2017-11-28 09:23:00 +0900893 &sum, seg_type, NULL, false);
Chao Yu216a6202015-03-19 19:23:32 +0800894 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900895
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700896 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800897 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700898 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300899 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700900 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300901 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900902 return 0;
903}
904
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800905int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800906{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800907 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800908 struct f2fs_map_blocks map;
Chao Yud6d478a12018-01-03 17:30:19 +0800909 int flag;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800910 int err = 0;
Chao Yud6d478a12018-01-03 17:30:19 +0800911 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800912
Weichao Guo71ad6822017-09-29 22:43:23 +0800913 /* convert inline data for Direct I/O*/
Chao Yud6d478a12018-01-03 17:30:19 +0800914 if (direct_io) {
Weichao Guo71ad6822017-09-29 22:43:23 +0800915 err = f2fs_convert_inline_inode(inode);
916 if (err)
917 return err;
918 }
919
Jaegeuk Kimdc91de72017-01-13 13:12:29 -0800920 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
921 return 0;
922
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700923 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800924 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
925 if (map.m_len > map.m_lblk)
926 map.m_len -= map.m_lblk;
927 else
928 map.m_len = 0;
929
Chao Yuda859852016-01-26 15:42:58 +0800930 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +0800931 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +0900932 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800933
Chao Yud6d478a12018-01-03 17:30:19 +0800934 if (direct_io) {
Hyunchul Leed5097be2017-11-28 09:23:00 +0900935 map.m_seg_type = rw_hint_to_seg_type(iocb->ki_hint);
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900936 flag = f2fs_force_buffered_io(inode, WRITE) ?
Chao Yud6d478a12018-01-03 17:30:19 +0800937 F2FS_GET_BLOCK_PRE_AIO :
938 F2FS_GET_BLOCK_PRE_DIO;
939 goto map_blocks;
Hyunchul Leed5097be2017-11-28 09:23:00 +0900940 }
Chao Yuf2470372017-07-19 00:19:05 +0800941 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800942 err = f2fs_convert_inline_inode(inode);
943 if (err)
944 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800945 }
Chao Yud6d478a12018-01-03 17:30:19 +0800946 if (f2fs_has_inline_data(inode))
Sheng Yong250066452017-11-22 18:23:39 +0800947 return err;
Chao Yud6d478a12018-01-03 17:30:19 +0800948
949 flag = F2FS_GET_BLOCK_PRE_AIO;
950
951map_blocks:
952 err = f2fs_map_blocks(inode, &map, 1, flag);
953 if (map.m_len > 0 && err == -ENOSPC) {
954 if (!direct_io)
955 set_inode_flag(inode, FI_NO_PREALLOC);
956 err = 0;
Sheng Yong250066452017-11-22 18:23:39 +0800957 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800958 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800959}
960
Yunlei He59c90812017-03-13 20:22:18 +0800961static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
962{
963 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
964 if (lock)
965 down_read(&sbi->node_change);
966 else
967 up_read(&sbi->node_change);
968 } else {
969 if (lock)
970 f2fs_lock_op(sbi);
971 else
972 f2fs_unlock_op(sbi);
973 }
974}
975
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900976/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700977 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
978 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800979 * If original data blocks are allocated, then give them to blockdev.
980 * Otherwise,
981 * a. preallocate requested block addresses
982 * b. do not use extent cache for better performance
983 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900984 */
Chao Yud323d002015-10-27 09:53:45 +0800985int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800986 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900987{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700988 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900989 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800990 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700991 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800992 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900993 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800994 unsigned int ofs_in_node, last_ofs_in_node;
995 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +0000996 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +0800997 block_t blkaddr;
Chao Yuc4020b22018-01-11 14:42:30 +0800998 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900999
Chao Yudfd02e42016-08-20 15:12:01 +08001000 if (!maxblocks)
1001 return 0;
1002
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001003 map->m_len = 0;
1004 map->m_flags = 0;
1005
1006 /* it only supports block size == page size */
1007 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +08001008 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001009
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001010 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001011 map->m_pblk = ei.blk + pgofs - ei.fofs;
1012 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1013 map->m_flags = F2FS_MAP_MAPPED;
Chao Yuc4020b22018-01-11 14:42:30 +08001014 if (map->m_next_extent)
1015 *map->m_next_extent = pgofs + map->m_len;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001016 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001017 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001018
Chao Yu4fe71e82016-01-26 15:37:38 +08001019next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001020 if (create)
Yunlei He59c90812017-03-13 20:22:18 +08001021 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001022
1023 /* When reading holes, we need its node page */
1024 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001025 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001026 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +08001027 if (flag == F2FS_GET_BLOCK_BMAP)
1028 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +08001029 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001030 err = 0;
Chao Yuda859852016-01-26 15:42:58 +08001031 if (map->m_next_pgofs)
1032 *map->m_next_pgofs =
1033 get_next_page_offset(&dn, pgofs);
Chao Yuc4020b22018-01-11 14:42:30 +08001034 if (map->m_next_extent)
1035 *map->m_next_extent =
1036 get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +08001037 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001038 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001039 }
Chao Yu973163f2015-09-18 16:51:51 +08001040
Chao Yuc4020b22018-01-11 14:42:30 +08001041 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +08001042 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +01001043 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +08001044 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001045
Chao Yu4fe71e82016-01-26 15:37:38 +08001046next_block:
Chao Yu7a2af762017-07-19 00:19:06 +08001047 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +08001048
Chao Yu7b525dd2018-05-23 22:25:08 +08001049 if (!is_valid_blkaddr(blkaddr)) {
Fan Li7df3a432015-12-17 13:20:59 +08001050 if (create) {
1051 if (unlikely(f2fs_cp_error(sbi))) {
1052 err = -EIO;
1053 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +08001054 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001055 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +08001056 if (blkaddr == NULL_ADDR) {
1057 prealloc++;
1058 last_ofs_in_node = dn.ofs_in_node;
1059 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001060 } else {
Hyunchul Leed5097be2017-11-28 09:23:00 +09001061 err = __allocate_data_block(&dn,
1062 map->m_seg_type);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001063 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001064 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001065 }
Fan Li7df3a432015-12-17 13:20:59 +08001066 if (err)
1067 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +08001068 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001069 blkaddr = dn.data_blkaddr;
1070 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001071 if (flag == F2FS_GET_BLOCK_BMAP) {
1072 map->m_pblk = 0;
1073 goto sync_out;
1074 }
Chao Yuc4020b22018-01-11 14:42:30 +08001075 if (flag == F2FS_GET_BLOCK_PRECACHE)
1076 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001077 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1078 blkaddr == NULL_ADDR) {
1079 if (map->m_next_pgofs)
1080 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001081 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001082 }
Chao Yuf3d98e72018-01-10 18:18:52 +08001083 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1084 /* for defragment case */
1085 if (map->m_next_pgofs)
1086 *map->m_next_pgofs = pgofs + 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001087 goto sync_out;
Chao Yuf3d98e72018-01-10 18:18:52 +08001088 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001089 }
1090 }
Fan Li7df3a432015-12-17 13:20:59 +08001091
Chao Yu46008c62016-05-09 19:56:30 +08001092 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1093 goto skip;
1094
Chao Yu4fe71e82016-01-26 15:37:38 +08001095 if (map->m_len == 0) {
1096 /* preallocated unwritten block should be mapped for fiemap. */
1097 if (blkaddr == NEW_ADDR)
1098 map->m_flags |= F2FS_MAP_UNWRITTEN;
1099 map->m_flags |= F2FS_MAP_MAPPED;
1100
1101 map->m_pblk = blkaddr;
1102 map->m_len = 1;
1103 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001104 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001105 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001106 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001107 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001108 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001109 } else {
1110 goto sync_out;
1111 }
1112
Chao Yu46008c62016-05-09 19:56:30 +08001113skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001114 dn.ofs_in_node++;
1115 pgofs++;
1116
Chao Yu46008c62016-05-09 19:56:30 +08001117 /* preallocate blocks in batch for one dnode page */
1118 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1119 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001120
Chao Yu46008c62016-05-09 19:56:30 +08001121 dn.ofs_in_node = ofs_in_node;
1122 err = reserve_new_blocks(&dn, prealloc);
1123 if (err)
1124 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001125
Chao Yu46008c62016-05-09 19:56:30 +08001126 map->m_len += dn.ofs_in_node - ofs_in_node;
1127 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1128 err = -ENOSPC;
1129 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001130 }
Chao Yu46008c62016-05-09 19:56:30 +08001131 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001132 }
1133
Chao Yu46008c62016-05-09 19:56:30 +08001134 if (pgofs >= end)
1135 goto sync_out;
1136 else if (dn.ofs_in_node < end_offset)
1137 goto next_block;
1138
Chao Yuc4020b22018-01-11 14:42:30 +08001139 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1140 if (map->m_flags & F2FS_MAP_MAPPED) {
1141 unsigned int ofs = start_pgofs - map->m_lblk;
1142
1143 f2fs_update_extent_cache_range(&dn,
1144 start_pgofs, map->m_pblk + ofs,
1145 map->m_len - ofs);
1146 }
1147 }
1148
Chao Yu46008c62016-05-09 19:56:30 +08001149 f2fs_put_dnode(&dn);
1150
1151 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001152 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001153 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001154 }
Chao Yu46008c62016-05-09 19:56:30 +08001155 goto next_dnode;
1156
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001157sync_out:
Chao Yuc4020b22018-01-11 14:42:30 +08001158 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1159 if (map->m_flags & F2FS_MAP_MAPPED) {
1160 unsigned int ofs = start_pgofs - map->m_lblk;
1161
1162 f2fs_update_extent_cache_range(&dn,
1163 start_pgofs, map->m_pblk + ofs,
1164 map->m_len - ofs);
1165 }
1166 if (map->m_next_extent)
1167 *map->m_next_extent = pgofs + 1;
1168 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001169 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001170unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001171 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001172 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001173 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001174 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001175out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001176 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001177 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001178}
1179
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001180bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1181{
1182 struct f2fs_map_blocks map;
1183 block_t last_lblk;
1184 int err;
1185
1186 if (pos + len > i_size_read(inode))
1187 return false;
1188
1189 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1190 map.m_next_pgofs = NULL;
1191 map.m_next_extent = NULL;
1192 map.m_seg_type = NO_CHECK_TYPE;
1193 last_lblk = F2FS_BLK_ALIGN(pos + len);
1194
1195 while (map.m_lblk < last_lblk) {
1196 map.m_len = last_lblk - map.m_lblk;
1197 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1198 if (err || map.m_len == 0)
1199 return false;
1200 map.m_lblk += map.m_len;
1201 }
1202 return true;
1203}
1204
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001205static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001206 struct buffer_head *bh, int create, int flag,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001207 pgoff_t *next_pgofs, int seg_type)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001208{
1209 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001210 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001211
1212 map.m_lblk = iblock;
1213 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001214 map.m_next_pgofs = next_pgofs;
Chao Yuc4020b22018-01-11 14:42:30 +08001215 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001216 map.m_seg_type = seg_type;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001217
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001218 err = f2fs_map_blocks(inode, &map, create, flag);
1219 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001220 map_bh(bh, inode->i_sb, map.m_pblk);
1221 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001222 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001223 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001224 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001225}
1226
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001227static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001228 struct buffer_head *bh_result, int create, int flag,
1229 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001230{
Chao Yuda859852016-01-26 15:42:58 +08001231 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001232 flag, next_pgofs,
1233 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001234}
1235
Chao Yue2b4e2b2015-08-19 19:11:19 +08001236static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001237 struct buffer_head *bh_result, int create)
1238{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001239 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001240 F2FS_GET_BLOCK_DEFAULT, NULL,
1241 rw_hint_to_seg_type(
1242 inode->i_write_hint));
Chao Yue2b4e2b2015-08-19 19:11:19 +08001243}
1244
1245static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1246 struct buffer_head *bh_result, int create)
1247{
Yunlei He179448b2015-12-28 21:48:32 +08001248 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001249 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001250 return -EFBIG;
1251
Chao Yue2b4e2b2015-08-19 19:11:19 +08001252 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001253 F2FS_GET_BLOCK_BMAP, NULL,
1254 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001255}
1256
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001257static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1258{
1259 return (offset >> inode->i_blkbits);
1260}
1261
1262static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1263{
1264 return (blk << inode->i_blkbits);
1265}
1266
Chao Yu442a9db2018-01-11 14:39:57 +08001267static int f2fs_xattr_fiemap(struct inode *inode,
1268 struct fiemap_extent_info *fieinfo)
1269{
1270 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1271 struct page *page;
1272 struct node_info ni;
1273 __u64 phys = 0, len;
1274 __u32 flags;
1275 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1276 int err = 0;
1277
1278 if (f2fs_has_inline_xattr(inode)) {
1279 int offset;
1280
1281 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1282 inode->i_ino, false);
1283 if (!page)
1284 return -ENOMEM;
1285
1286 get_node_info(sbi, inode->i_ino, &ni);
1287
1288 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1289 offset = offsetof(struct f2fs_inode, i_addr) +
1290 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yub323fd22018-01-17 16:31:36 +08001291 get_inline_xattr_addrs(inode));
Chao Yu442a9db2018-01-11 14:39:57 +08001292
1293 phys += offset;
1294 len = inline_xattr_size(inode);
1295
1296 f2fs_put_page(page, 1);
1297
1298 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1299
1300 if (!xnid)
1301 flags |= FIEMAP_EXTENT_LAST;
1302
1303 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1304 if (err || err == 1)
1305 return err;
1306 }
1307
1308 if (xnid) {
1309 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1310 if (!page)
1311 return -ENOMEM;
1312
1313 get_node_info(sbi, xnid, &ni);
1314
1315 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1316 len = inode->i_sb->s_blocksize;
1317
1318 f2fs_put_page(page, 1);
1319
1320 flags = FIEMAP_EXTENT_LAST;
1321 }
1322
1323 if (phys)
1324 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1325
1326 return (err < 0 ? err : 0);
1327}
1328
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001329int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1330 u64 start, u64 len)
1331{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001332 struct buffer_head map_bh;
1333 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001334 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001335 u64 logical = 0, phys = 0, size = 0;
1336 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001337 int ret = 0;
1338
Chao Yuc4020b22018-01-11 14:42:30 +08001339 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1340 ret = f2fs_precache_extents(inode);
1341 if (ret)
1342 return ret;
1343 }
1344
Chao Yu442a9db2018-01-11 14:39:57 +08001345 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001346 if (ret)
1347 return ret;
1348
Chao Yuf1b43d42018-01-11 14:37:35 +08001349 inode_lock(inode);
1350
Chao Yu442a9db2018-01-11 14:39:57 +08001351 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1352 ret = f2fs_xattr_fiemap(inode, fieinfo);
1353 goto out;
1354 }
1355
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001356 if (f2fs_has_inline_data(inode)) {
1357 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1358 if (ret != -EAGAIN)
Chao Yuf1b43d42018-01-11 14:37:35 +08001359 goto out;
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001360 }
1361
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001362 if (logical_to_blk(inode, len) == 0)
1363 len = blk_to_logical(inode, 1);
1364
1365 start_blk = logical_to_blk(inode, start);
1366 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001367
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001368next:
1369 memset(&map_bh, 0, sizeof(struct buffer_head));
1370 map_bh.b_size = len;
1371
Chao Yue2b4e2b2015-08-19 19:11:19 +08001372 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001373 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001374 if (ret)
1375 goto out;
1376
1377 /* HOLE */
1378 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001379 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001380
1381 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1382 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001383 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001384
Fan Li9a950d52015-12-26 18:07:41 +08001385 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001386 }
Fan Li9a950d52015-12-26 18:07:41 +08001387
Chao Yuda5af122016-01-08 20:19:27 +08001388 if (size) {
1389 if (f2fs_encrypted_inode(inode))
1390 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1391
Fan Li9a950d52015-12-26 18:07:41 +08001392 ret = fiemap_fill_next_extent(fieinfo, logical,
1393 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001394 }
Fan Li9a950d52015-12-26 18:07:41 +08001395
1396 if (start_blk > last_blk || ret)
1397 goto out;
1398
1399 logical = blk_to_logical(inode, start_blk);
1400 phys = blk_to_logical(inode, map_bh.b_blocknr);
1401 size = map_bh.b_size;
1402 flags = 0;
1403 if (buffer_unwritten(&map_bh))
1404 flags = FIEMAP_EXTENT_UNWRITTEN;
1405
1406 start_blk += logical_to_blk(inode, size);
1407
1408prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001409 cond_resched();
1410 if (fatal_signal_pending(current))
1411 ret = -EINTR;
1412 else
1413 goto next;
1414out:
1415 if (ret == 1)
1416 ret = 0;
1417
Al Viro59551022016-01-22 15:40:57 -05001418 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001419 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001420}
1421
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001422/*
1423 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1424 * Major change was from block_size == page_size in f2fs by default.
1425 */
1426static int f2fs_mpage_readpages(struct address_space *mapping,
1427 struct list_head *pages, struct page *page,
1428 unsigned nr_pages)
1429{
1430 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001431 sector_t last_block_in_bio = 0;
1432 struct inode *inode = mapping->host;
1433 const unsigned blkbits = inode->i_blkbits;
1434 const unsigned blocksize = 1 << blkbits;
1435 sector_t block_in_file;
1436 sector_t last_block;
1437 sector_t last_block_in_file;
1438 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001439 struct f2fs_map_blocks map;
1440
1441 map.m_pblk = 0;
1442 map.m_lblk = 0;
1443 map.m_len = 0;
1444 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001445 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08001446 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001447 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001448
LiFan736c0a72017-11-25 11:46:18 +08001449 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001450 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001451 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08001452
1453 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001454 list_del(&page->lru);
1455 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001456 page->index,
1457 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001458 goto next_page;
1459 }
1460
1461 block_in_file = (sector_t)page->index;
1462 last_block = block_in_file + nr_pages;
1463 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1464 blkbits;
1465 if (last_block > last_block_in_file)
1466 last_block = last_block_in_file;
1467
1468 /*
1469 * Map blocks using the previous result first.
1470 */
1471 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1472 block_in_file > map.m_lblk &&
1473 block_in_file < (map.m_lblk + map.m_len))
1474 goto got_it;
1475
1476 /*
1477 * Then do more f2fs_map_blocks() calls until we are
1478 * done with this page.
1479 */
1480 map.m_flags = 0;
1481
1482 if (block_in_file < last_block) {
1483 map.m_lblk = block_in_file;
1484 map.m_len = last_block - block_in_file;
1485
Chao Yu46c9e142015-09-18 16:54:16 +08001486 if (f2fs_map_blocks(inode, &map, 0,
Qiuyang Sunf2220c72017-08-09 17:27:30 +08001487 F2FS_GET_BLOCK_DEFAULT))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001488 goto set_error_page;
1489 }
1490got_it:
1491 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1492 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1493 SetPageMappedToDisk(page);
1494
1495 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1496 SetPageUptodate(page);
1497 goto confused;
1498 }
1499 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001500 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001501 if (!PageUptodate(page))
1502 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001503 unlock_page(page);
1504 goto next_page;
1505 }
1506
1507 /*
1508 * This page will go to BIO. Do we need to send this
1509 * BIO off first?
1510 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001511 if (bio && (last_block_in_bio != block_nr - 1 ||
1512 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001513submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001514 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001515 bio = NULL;
1516 }
1517 if (bio == NULL) {
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001518 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001519 if (IS_ERR(bio)) {
1520 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001521 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001522 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001523 }
1524
1525 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1526 goto submit_and_realloc;
1527
1528 last_block_in_bio = block_nr;
1529 goto next_page;
1530set_error_page:
1531 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001532 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001533 unlock_page(page);
1534 goto next_page;
1535confused:
1536 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001537 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001538 bio = NULL;
1539 }
1540 unlock_page(page);
1541next_page:
1542 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001543 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001544 }
1545 BUG_ON(pages && !list_empty(pages));
1546 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001547 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001548 return 0;
1549}
1550
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001551static int f2fs_read_data_page(struct file *file, struct page *page)
1552{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001553 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001554 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001555
Chao Yuc20e89c2014-05-06 16:53:08 +08001556 trace_f2fs_readpage(page, DATA);
1557
arter97e1c42042014-08-06 23:22:50 +09001558 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001559 if (f2fs_has_inline_data(inode))
1560 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001561 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001562 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001563 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001564}
1565
1566static int f2fs_read_data_pages(struct file *file,
1567 struct address_space *mapping,
1568 struct list_head *pages, unsigned nr_pages)
1569{
Hsiang Kao71cb4af2017-09-24 02:45:42 +08001570 struct inode *inode = mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001571 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001572
1573 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001574
1575 /* If the file has inline data, skip readpages */
1576 if (f2fs_has_inline_data(inode))
1577 return 0;
1578
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001579 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001580}
1581
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001582static int encrypt_one_page(struct f2fs_io_info *fio)
1583{
1584 struct inode *inode = fio->page->mapping->host;
1585 gfp_t gfp_flags = GFP_NOFS;
1586
Jaegeuk Kim19585932017-09-05 16:54:24 -07001587 if (!f2fs_encrypted_file(inode))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001588 return 0;
1589
Eric Biggers6dbb1792018-04-18 11:09:48 -07001590 /* wait for GCed page writeback via META_MAPPING */
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -07001591 f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001592
1593retry_encrypt:
1594 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1595 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1596 if (!IS_ERR(fio->encrypted_page))
1597 return 0;
1598
1599 /* flush pending IOs and wait for a while in the ENOMEM case */
1600 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001601 f2fs_flush_merged_writes(fio->sbi);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001602 congestion_wait(BLK_RW_ASYNC, HZ/50);
1603 gfp_flags |= __GFP_NOFAIL;
1604 goto retry_encrypt;
1605 }
1606 return PTR_ERR(fio->encrypted_page);
1607}
1608
Chao Yubb9e3bb2018-01-17 16:31:38 +08001609static inline bool check_inplace_update_policy(struct inode *inode,
1610 struct f2fs_io_info *fio)
1611{
1612 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1613 unsigned int policy = SM_I(sbi)->ipu_policy;
1614
1615 if (policy & (0x1 << F2FS_IPU_FORCE))
1616 return true;
1617 if (policy & (0x1 << F2FS_IPU_SSR) && need_SSR(sbi))
1618 return true;
1619 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1620 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1621 return true;
1622 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && need_SSR(sbi) &&
1623 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1624 return true;
1625
1626 /*
1627 * IPU for rewrite async pages
1628 */
1629 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1630 fio && fio->op == REQ_OP_WRITE &&
1631 !(fio->op_flags & REQ_SYNC) &&
1632 !f2fs_encrypted_inode(inode))
1633 return true;
1634
1635 /* this is only set during fdatasync */
1636 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1637 is_inode_flag_set(inode, FI_NEED_IPU))
1638 return true;
1639
1640 return false;
1641}
1642
1643bool should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
1644{
1645 if (f2fs_is_pinned_file(inode))
1646 return true;
1647
1648 /* if this is cold file, we should overwrite to avoid fragmentation */
1649 if (file_is_cold(inode))
1650 return true;
1651
1652 return check_inplace_update_policy(inode, fio);
1653}
1654
1655bool should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
1656{
1657 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1658
1659 if (test_opt(sbi, LFS))
1660 return true;
1661 if (S_ISDIR(inode->i_mode))
1662 return true;
1663 if (f2fs_is_atomic_file(inode))
1664 return true;
1665 if (fio) {
1666 if (is_cold_data(fio->page))
1667 return true;
1668 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1669 return true;
1670 }
1671 return false;
1672}
1673
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001674static inline bool need_inplace_update(struct f2fs_io_info *fio)
1675{
1676 struct inode *inode = fio->page->mapping->host;
1677
Chao Yubb9e3bb2018-01-17 16:31:38 +08001678 if (should_update_outplace(inode, fio))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001679 return false;
1680
Chao Yubb9e3bb2018-01-17 16:31:38 +08001681 return should_update_inplace(inode, fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001682}
1683
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001684int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001685{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001686 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001687 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001688 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001689 struct extent_info ei = {0,0,0};
1690 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001691 int err = 0;
1692
1693 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00001694 if (need_inplace_update(fio) &&
1695 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1696 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07001697
Chao Yu7b525dd2018-05-23 22:25:08 +08001698 if (is_valid_blkaddr(fio->old_blkaddr)) {
Hou Pengyange959c8f2017-04-25 12:45:13 +00001699 ipu_force = true;
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001700 fio->need_lock = LOCK_DONE;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001701 goto got_it;
1702 }
1703 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001704
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001705 /* Deadlock due to between page->lock and f2fs_lock_op */
1706 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1707 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001708
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001709 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001710 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001711 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001712
Chao Yu28bc1062016-02-06 14:40:34 +08001713 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001714
1715 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001716 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001717 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001718 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001719 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00001720got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001721 /*
1722 * If current allocation needs SSR,
1723 * it had better in-place writes for updated data.
1724 */
Chao Yu7b525dd2018-05-23 22:25:08 +08001725 if (ipu_force || (is_valid_blkaddr(fio->old_blkaddr) &&
1726 need_inplace_update(fio))) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001727 err = encrypt_one_page(fio);
1728 if (err)
1729 goto out_writepage;
1730
1731 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07001732 ClearPageError(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001733 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001734 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001735 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -07001736 err = rewrite_data_page(fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001737 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001738 set_inode_flag(inode, FI_UPDATE_WRITE);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001739 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001740 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001741
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001742 if (fio->need_lock == LOCK_RETRY) {
1743 if (!f2fs_trylock_op(fio->sbi)) {
1744 err = -EAGAIN;
1745 goto out_writepage;
1746 }
1747 fio->need_lock = LOCK_REQ;
1748 }
1749
1750 err = encrypt_one_page(fio);
1751 if (err)
1752 goto out_writepage;
1753
1754 set_page_writeback(page);
Jaegeuk Kim17c50032018-04-11 23:09:04 -07001755 ClearPageError(page);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001756
Hou Pengyang279d6df2017-04-27 00:17:21 +08001757 /* LFS mode write path */
1758 write_data_page(&dn, fio);
1759 trace_f2fs_do_write_data_page(page, OPU);
1760 set_inode_flag(inode, FI_APPEND_WRITE);
1761 if (page->index == 0)
1762 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001763out_writepage:
1764 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001765out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001766 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001767 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001768 return err;
1769}
1770
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001771static int __write_data_page(struct page *page, bool *submitted,
Chao Yub0af6d42017-08-02 23:21:48 +08001772 struct writeback_control *wbc,
1773 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001774{
1775 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001776 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001777 loff_t i_size = i_size_read(inode);
1778 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001779 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001780 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001781 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001782 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001783 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001784 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001785 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08001786 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001787 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001788 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001789 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00001790 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001791 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001792 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001793 .submitted = false,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001794 .need_lock = LOCK_RETRY,
Chao Yub0af6d42017-08-02 23:21:48 +08001795 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08001796 .io_wbc = wbc,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001797 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001798
Chao Yuecda0de2014-05-06 16:48:26 +08001799 trace_f2fs_writepage(page, DATA);
1800
Chao Yudb198ae2018-01-18 17:29:10 +08001801 /* we should bypass data pages to proceed the kworkder jobs */
1802 if (unlikely(f2fs_cp_error(sbi))) {
1803 mapping_set_error(page->mapping, -EIO);
Chao Yu1174abf2018-05-28 16:59:26 +08001804 /*
1805 * don't drop any dirty dentry pages for keeping lastest
1806 * directory structure.
1807 */
1808 if (S_ISDIR(inode->i_mode))
1809 goto redirty_out;
Chao Yudb198ae2018-01-18 17:29:10 +08001810 goto out;
1811 }
1812
Chao Yu0771fcc2017-06-29 23:20:45 +08001813 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1814 goto redirty_out;
1815
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001816 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001817 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001818
1819 /*
1820 * If the offset is out-of-range of file size,
1821 * this page does not have to be written to disk.
1822 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001823 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001824 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001825 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001826
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001827 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001828write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001829 if (f2fs_is_drop_cache(inode))
1830 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001831 /* we should not write 0'th page having journal header */
1832 if (f2fs_is_volatile_file(inode) && (!page->index ||
1833 (!wbc->for_reclaim &&
1834 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001835 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001836
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001837 /* Dentry blocks are controlled by checkpoint */
1838 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001839 fio.need_lock = LOCK_DONE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001840 err = do_write_data_page(&fio);
1841 goto done;
1842 }
1843
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001844 if (!wbc->for_reclaim)
1845 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001846 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001847 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001848 else
1849 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001850
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001851 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08001852 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001853 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08001854 if (!err)
1855 goto out;
1856 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001857
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001858 if (err == -EAGAIN) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001859 err = do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001860 if (err == -EAGAIN) {
1861 fio.need_lock = LOCK_REQ;
1862 err = do_write_data_page(&fio);
1863 }
1864 }
Chao Yua0d00fa2017-10-09 17:55:19 +08001865
Chao Yueb449792018-01-17 16:31:37 +08001866 if (err) {
1867 file_set_keep_isize(inode);
1868 } else {
1869 down_write(&F2FS_I(inode)->i_sem);
1870 if (F2FS_I(inode)->last_disk_size < psize)
1871 F2FS_I(inode)->last_disk_size = psize;
1872 up_write(&F2FS_I(inode)->i_sem);
1873 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001874
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001875done:
1876 if (err && err != -ENOENT)
1877 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001878
Jaegeuk Kim39936832012-11-22 16:21:29 +09001879out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001880 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001881 if (err)
1882 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001883
1884 if (wbc->for_reclaim) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001885 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001886 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yuc227f912015-12-16 13:09:20 +08001887 remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001888 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001889 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001890
1891 unlock_page(page);
Jaegeuk Kima7881892017-04-20 13:51:57 -07001892 if (!S_ISDIR(inode->i_mode))
1893 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001894
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001895 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001896 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001897 submitted = NULL;
1898 }
1899
1900 if (submitted)
1901 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001902
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001903 return 0;
1904
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001905redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001906 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim5b19d282018-05-03 23:26:02 -07001907 /*
1908 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
1909 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
1910 * file_write_and_wait_range() will see EIO error, which is critical
1911 * to return value of fsync() followed by atomic_write failure to user.
1912 */
1913 if (!err || wbc->for_reclaim)
Chao Yu0002b612016-11-28 19:13:43 -08001914 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001915 unlock_page(page);
1916 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001917}
1918
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001919static int f2fs_write_data_page(struct page *page,
1920 struct writeback_control *wbc)
1921{
Chao Yub0af6d42017-08-02 23:21:48 +08001922 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001923}
1924
Chao Yu8f46dca2015-07-14 18:56:10 +08001925/*
1926 * This function was copied from write_cche_pages from mm/page-writeback.c.
1927 * The major change is making write step of cold data page separately from
1928 * warm/hot data page.
1929 */
1930static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08001931 struct writeback_control *wbc,
1932 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08001933{
1934 int ret = 0;
1935 int done = 0;
1936 struct pagevec pvec;
1937 int nr_pages;
1938 pgoff_t uninitialized_var(writeback_index);
1939 pgoff_t index;
1940 pgoff_t end; /* Inclusive */
1941 pgoff_t done_index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001942 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001943 int cycled;
1944 int range_whole = 0;
1945 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001946
Mel Gorman86679822017-11-15 17:37:52 -08001947 pagevec_init(&pvec);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001948
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001949 if (get_dirty_pages(mapping->host) <=
1950 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1951 set_inode_flag(mapping->host, FI_HOT_DATA);
1952 else
1953 clear_inode_flag(mapping->host, FI_HOT_DATA);
1954
Chao Yu8f46dca2015-07-14 18:56:10 +08001955 if (wbc->range_cyclic) {
1956 writeback_index = mapping->writeback_index; /* prev offset */
1957 index = writeback_index;
1958 if (index == 0)
1959 cycled = 1;
1960 else
1961 cycled = 0;
1962 end = -1;
1963 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001964 index = wbc->range_start >> PAGE_SHIFT;
1965 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001966 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1967 range_whole = 1;
1968 cycled = 1; /* ignore range_cyclic tests */
1969 }
1970 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1971 tag = PAGECACHE_TAG_TOWRITE;
1972 else
1973 tag = PAGECACHE_TAG_DIRTY;
1974retry:
1975 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1976 tag_pages_for_writeback(mapping, index, end);
1977 done_index = index;
1978 while (!done && (index <= end)) {
1979 int i;
1980
Jan Kara69c4f352017-11-15 17:34:48 -08001981 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08001982 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08001983 if (nr_pages == 0)
1984 break;
1985
1986 for (i = 0; i < nr_pages; i++) {
1987 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001988 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001989
Chao Yuf8de4332018-05-23 22:25:09 +08001990 /* give a priority to WB_SYNC threads */
1991 if (atomic_read(&F2FS_M_SB(mapping)->wb_sync_req) &&
1992 wbc->sync_mode == WB_SYNC_NONE) {
1993 done = 1;
1994 break;
1995 }
1996
Chao Yu8f46dca2015-07-14 18:56:10 +08001997 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001998retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08001999 lock_page(page);
2000
2001 if (unlikely(page->mapping != mapping)) {
2002continue_unlock:
2003 unlock_page(page);
2004 continue;
2005 }
2006
2007 if (!PageDirty(page)) {
2008 /* someone wrote it for us */
2009 goto continue_unlock;
2010 }
2011
Chao Yu8f46dca2015-07-14 18:56:10 +08002012 if (PageWriteback(page)) {
2013 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002014 f2fs_wait_on_page_writeback(page,
2015 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08002016 else
2017 goto continue_unlock;
2018 }
2019
2020 BUG_ON(PageWriteback(page));
2021 if (!clear_page_dirty_for_io(page))
2022 goto continue_unlock;
2023
Chao Yub0af6d42017-08-02 23:21:48 +08002024 ret = __write_data_page(page, &submitted, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08002025 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08002026 /*
2027 * keep nr_to_write, since vfs uses this to
2028 * get # of written pages.
2029 */
2030 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2031 unlock_page(page);
2032 ret = 0;
2033 continue;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07002034 } else if (ret == -EAGAIN) {
2035 ret = 0;
2036 if (wbc->sync_mode == WB_SYNC_ALL) {
2037 cond_resched();
2038 congestion_wait(BLK_RW_ASYNC,
2039 HZ/50);
2040 goto retry_write;
2041 }
2042 continue;
Chao Yu0002b612016-11-28 19:13:43 -08002043 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002044 done_index = page->index + 1;
2045 done = 1;
2046 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08002047 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08002048 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08002049 }
2050
Chao Yuf8de4332018-05-23 22:25:09 +08002051 if (--wbc->nr_to_write <= 0 &&
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002052 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08002053 done = 1;
2054 break;
2055 }
2056 }
2057 pagevec_release(&pvec);
2058 cond_resched();
2059 }
2060
Chao Yu8f46dca2015-07-14 18:56:10 +08002061 if (!cycled && !done) {
2062 cycled = 1;
2063 index = 0;
2064 end = writeback_index - 1;
2065 goto retry;
2066 }
2067 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2068 mapping->writeback_index = done_index;
2069
Jaegeuk Kim942fd312017-02-01 16:51:22 -08002070 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07002071 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
2072 0, last_idx, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08002073
Chao Yu8f46dca2015-07-14 18:56:10 +08002074 return ret;
2075}
2076
Chao Yub0af6d42017-08-02 23:21:48 +08002077int __f2fs_write_data_pages(struct address_space *mapping,
2078 struct writeback_control *wbc,
2079 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002080{
2081 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002082 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002083 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002084 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002085
P J Pcfb185a2013-04-03 11:38:00 +09002086 /* deal with chardevs and other special file */
2087 if (!mapping->a_ops->writepage)
2088 return 0;
2089
Chao Yu6a290542015-07-17 18:02:39 +08002090 /* skip writing if there is no dirty page in this inode */
2091 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2092 return 0;
2093
Chao Yu0771fcc2017-06-29 23:20:45 +08002094 /* during POR, we don't need to trigger writepage at all. */
2095 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2096 goto skip_write;
2097
Jaegeuk Kima1257022015-10-08 10:40:07 -07002098 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
2099 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
2100 available_free_memory(sbi, DIRTY_DENTS))
2101 goto skip_write;
2102
Chao Yud323d002015-10-27 09:53:45 +08002103 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07002104 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08002105 goto skip_write;
2106
Yunlei Hed31c7c32016-02-04 16:14:00 +08002107 trace_f2fs_writepages(mapping->host, wbc, DATA);
2108
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002109 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2110 if (wbc->sync_mode == WB_SYNC_ALL)
2111 atomic_inc(&sbi->wb_sync_req);
2112 else if (atomic_read(&sbi->wb_sync_req))
2113 goto skip_write;
2114
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002115 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08002116 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002117 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002118
2119 if (wbc->sync_mode == WB_SYNC_ALL)
2120 atomic_dec(&sbi->wb_sync_req);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07002121 /*
2122 * if some pages were truncated, we cannot guarantee its mapping->host
2123 * to detect pending bios.
2124 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002125
Chao Yuc227f912015-12-16 13:09:20 +08002126 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002127 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002128
2129skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002130 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08002131 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002132 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002133}
2134
Chao Yub0af6d42017-08-02 23:21:48 +08002135static int f2fs_write_data_pages(struct address_space *mapping,
2136 struct writeback_control *wbc)
2137{
2138 struct inode *inode = mapping->host;
2139
2140 return __f2fs_write_data_pages(mapping, wbc,
2141 F2FS_I(inode)->cp_task == current ?
2142 FS_CP_DATA_IO : FS_DATA_IO);
2143}
2144
Chao Yu3aab8f82014-07-02 13:25:04 +08002145static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2146{
2147 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002148 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002149
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002150 if (to > i_size) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08002151 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002152 truncate_pagecache(inode, i_size);
2153 truncate_blocks(inode, i_size, true);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08002154 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3aab8f82014-07-02 13:25:04 +08002155 }
2156}
2157
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002158static int prepare_write_begin(struct f2fs_sb_info *sbi,
2159 struct page *page, loff_t pos, unsigned len,
2160 block_t *blk_addr, bool *node_changed)
2161{
2162 struct inode *inode = page->mapping->host;
2163 pgoff_t index = page->index;
2164 struct dnode_of_data dn;
2165 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002166 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00002167 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002168 int err = 0;
2169
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002170 /*
2171 * we already allocated all the blocks, so we don't need to get
2172 * the block addresses when there is no need to fill the page.
2173 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002174 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2175 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002176 return 0;
2177
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002178 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002179 (pos & PAGE_MASK) >= i_size_read(inode)) {
Yunlei He59c90812017-03-13 20:22:18 +08002180 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002181 locked = true;
2182 }
2183restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002184 /* check inline_data */
2185 ipage = get_node_page(sbi, inode->i_ino);
2186 if (IS_ERR(ipage)) {
2187 err = PTR_ERR(ipage);
2188 goto unlock_out;
2189 }
2190
2191 set_new_dnode(&dn, inode, ipage, ipage, 0);
2192
2193 if (f2fs_has_inline_data(inode)) {
Chao Yuf2470372017-07-19 00:19:05 +08002194 if (pos + len <= MAX_INLINE_DATA(inode)) {
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002195 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002196 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08002197 if (inode->i_nlink)
2198 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002199 } else {
2200 err = f2fs_convert_inline_page(&dn, page);
2201 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002202 goto out;
2203 if (dn.data_blkaddr == NULL_ADDR)
2204 err = f2fs_get_block(&dn, index);
2205 }
2206 } else if (locked) {
2207 err = f2fs_get_block(&dn, index);
2208 } else {
2209 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2210 dn.data_blkaddr = ei.blk + index - ei.fofs;
2211 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002212 /* hole case */
2213 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07002214 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002215 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08002216 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2217 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002218 locked = true;
2219 goto restart;
2220 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002221 }
2222 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002223
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002224 /* convert_inline_page can make node_changed */
2225 *blk_addr = dn.data_blkaddr;
2226 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002227out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002228 f2fs_put_dnode(&dn);
2229unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002230 if (locked)
Yunlei He59c90812017-03-13 20:22:18 +08002231 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002232 return err;
2233}
2234
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002235static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2236 loff_t pos, unsigned len, unsigned flags,
2237 struct page **pagep, void **fsdata)
2238{
2239 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002240 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002241 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002242 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yua2e2e762018-01-15 17:16:46 +08002243 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002244 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002245 int err = 0;
2246
Chao Yu62aed042014-05-06 16:46:04 +08002247 trace_f2fs_write_begin(inode, pos, len, flags);
2248
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002249 if (f2fs_is_atomic_file(inode) &&
2250 !available_free_memory(sbi, INMEM_PAGES)) {
2251 err = -ENOMEM;
Chao Yua2e2e762018-01-15 17:16:46 +08002252 drop_atomic = true;
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002253 goto fail;
2254 }
2255
Jaegeuk Kim5f727392014-11-25 10:59:45 -08002256 /*
2257 * We should check this at this moment to avoid deadlock on inode page
2258 * and #0 page. The locking rule for inline_data conversion should be:
2259 * lock_page(page #0) -> lock_page(inode_page)
2260 */
2261 if (index != 0) {
2262 err = f2fs_convert_inline_inode(inode);
2263 if (err)
2264 goto fail;
2265 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002266repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002267 /*
2268 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2269 * wait_for_stable_page. Will wait that below with our IO control.
2270 */
Chao Yu01eccef2017-10-28 16:52:30 +08002271 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002272 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08002273 if (!page) {
2274 err = -ENOMEM;
2275 goto fail;
2276 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09002277
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002278 *pagep = page;
2279
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002280 err = prepare_write_begin(sbi, page, pos, len,
2281 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002282 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002283 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002284
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002285 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002286 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002287 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002288 lock_page(page);
2289 if (page->mapping != mapping) {
2290 /* The page got truncated from under us */
2291 f2fs_put_page(page, 1);
2292 goto repeat;
2293 }
2294 }
2295
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002296 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002297
Eric Biggers6dbb1792018-04-18 11:09:48 -07002298 /* wait for GCed page writeback via META_MAPPING */
2299 if (f2fs_post_read_required(inode))
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -07002300 f2fs_wait_on_block_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08002301
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002302 if (len == PAGE_SIZE || PageUptodate(page))
2303 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002304
Yunlei He746e2402016-12-20 11:11:35 +08002305 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2306 zero_user_segment(page, len, PAGE_SIZE);
2307 return 0;
2308 }
2309
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002310 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002311 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002312 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002313 } else {
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07002314 err = f2fs_submit_page_read(inode, page, blkaddr);
2315 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08002316 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08002317
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002318 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002319 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002320 f2fs_put_page(page, 1);
2321 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002322 }
Chao Yu1563ac72016-07-03 22:05:12 +08002323 if (unlikely(!PageUptodate(page))) {
2324 err = -EIO;
2325 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002326 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002327 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002328 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002329
Chao Yu3aab8f82014-07-02 13:25:04 +08002330fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002331 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002332 f2fs_write_failed(mapping, pos + len);
Chao Yua2e2e762018-01-15 17:16:46 +08002333 if (drop_atomic)
Chao Yu2ef79ec2018-05-07 20:28:54 +08002334 drop_inmem_pages_all(sbi, false);
Chao Yu3aab8f82014-07-02 13:25:04 +08002335 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002336}
2337
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002338static int f2fs_write_end(struct file *file,
2339 struct address_space *mapping,
2340 loff_t pos, unsigned len, unsigned copied,
2341 struct page *page, void *fsdata)
2342{
2343 struct inode *inode = page->mapping->host;
2344
Chao Yudfb2bf32014-05-06 16:47:23 +08002345 trace_f2fs_write_end(inode, pos, len, copied);
2346
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002347 /*
2348 * This should be come from len == PAGE_SIZE, and we expect copied
2349 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2350 * let generic_perform_write() try to copy data again through copied=0.
2351 */
2352 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08002353 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002354 copied = 0;
2355 else
2356 SetPageUptodate(page);
2357 }
2358 if (!copied)
2359 goto unlock_out;
2360
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002361 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002362
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002363 if (pos + copied > i_size_read(inode))
2364 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002365unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002366 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002367 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002368 return copied;
2369}
2370
Omar Sandoval6f673762015-03-16 04:33:52 -07002371static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2372 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002373{
2374 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002375
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002376 if (offset & blocksize_mask)
2377 return -EINVAL;
2378
Al Viro5b46f252014-03-16 18:07:34 -04002379 if (iov_iter_alignment(iter) & blocksize_mask)
2380 return -EINVAL;
2381
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002382 return 0;
2383}
2384
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002385static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002386{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002387 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002388 struct inode *inode = mapping->host;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002389 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002390 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002391 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002392 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002393 int err;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002394 enum rw_hint hint = iocb->ki_hint;
Chao Yu63189b72018-03-08 14:22:56 +08002395 int whint_mode = F2FS_OPTION(sbi).whint_mode;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002396
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002397 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002398 if (err)
2399 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002400
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002401 if (f2fs_force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002402 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002403
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002404 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002405
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002406 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2407 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2408
Chao Yub2532c62018-04-24 10:55:28 +08002409 if (!down_read_trylock(&F2FS_I(inode)->i_gc_rwsem[rw])) {
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002410 if (iocb->ki_flags & IOCB_NOWAIT) {
2411 iocb->ki_hint = hint;
2412 err = -EAGAIN;
2413 goto out;
2414 }
Chao Yub2532c62018-04-24 10:55:28 +08002415 down_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002416 }
2417
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002418 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yub2532c62018-04-24 10:55:28 +08002419 up_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002420
2421 if (rw == WRITE) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002422 if (whint_mode == WHINT_MODE_OFF)
2423 iocb->ki_hint = hint;
Chao Yub0af6d42017-08-02 23:21:48 +08002424 if (err > 0) {
2425 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2426 err);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002427 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yub0af6d42017-08-02 23:21:48 +08002428 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002429 f2fs_write_failed(mapping, offset + count);
Chao Yub0af6d42017-08-02 23:21:48 +08002430 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002431 }
Chao Yu70407fa2014-07-31 21:11:22 +08002432
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002433out:
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002434 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002435
Chao Yu3aab8f82014-07-02 13:25:04 +08002436 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002437}
2438
Chao Yu487261f2015-02-05 17:44:29 +08002439void f2fs_invalidate_page(struct page *page, unsigned int offset,
2440 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002441{
2442 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002443 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002444
Chao Yu487261f2015-02-05 17:44:29 +08002445 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002446 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002447 return;
2448
Chao Yu487261f2015-02-05 17:44:29 +08002449 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08002450 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002451 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08002452 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002453 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08002454 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002455 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08002456 remove_dirty_inode(inode);
2457 }
Chao Yu487261f2015-02-05 17:44:29 +08002458 }
Chao Yudecd36b2015-08-07 18:42:09 +08002459
2460 /* This is atomic written page, keep Private */
2461 if (IS_ATOMIC_WRITTEN_PAGE(page))
Jaegeuk Kim8c242db2017-03-17 09:55:52 +08002462 return drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002463
Chao Yu23dc9742016-04-29 20:09:15 +08002464 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002465 ClearPagePrivate(page);
2466}
2467
Chao Yu487261f2015-02-05 17:44:29 +08002468int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002469{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002470 /* If this is dirty page, keep PagePrivate */
2471 if (PageDirty(page))
2472 return 0;
2473
Chao Yudecd36b2015-08-07 18:42:09 +08002474 /* This is atomic written page, keep Private */
2475 if (IS_ATOMIC_WRITTEN_PAGE(page))
2476 return 0;
2477
Chao Yu23dc9742016-04-29 20:09:15 +08002478 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002479 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002480 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002481}
2482
2483static int f2fs_set_data_page_dirty(struct page *page)
2484{
2485 struct address_space *mapping = page->mapping;
2486 struct inode *inode = mapping->host;
2487
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002488 trace_f2fs_set_page_dirty(page, DATA);
2489
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002490 if (!PageUptodate(page))
2491 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002492
Chao Yu5fe45742017-01-07 18:50:26 +08002493 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002494 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2495 register_inmem_page(inode, page);
2496 return 1;
2497 }
2498 /*
2499 * Previously, this page has been registered, we just
2500 * return here.
2501 */
2502 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002503 }
2504
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002505 if (!PageDirty(page)) {
Jaegeuk Kimb87078a2018-04-20 19:29:52 -07002506 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002507 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002508 return 1;
2509 }
2510 return 0;
2511}
2512
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002513static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2514{
Chao Yu454ae7e2014-04-22 13:34:01 +08002515 struct inode *inode = mapping->host;
2516
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002517 if (f2fs_has_inline_data(inode))
2518 return 0;
2519
2520 /* make sure allocating whole blocks */
2521 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2522 filemap_write_and_wait(mapping);
2523
Chao Yue2b4e2b2015-08-19 19:11:19 +08002524 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002525}
2526
Weichao Guo5b7a4872016-09-20 05:03:27 +08002527#ifdef CONFIG_MIGRATION
2528#include <linux/migrate.h>
2529
2530int f2fs_migrate_page(struct address_space *mapping,
2531 struct page *newpage, struct page *page, enum migrate_mode mode)
2532{
2533 int rc, extra_count;
2534 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2535 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2536
2537 BUG_ON(PageWriteback(page));
2538
2539 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07002540 if (atomic_written) {
2541 if (mode != MIGRATE_SYNC)
2542 return -EBUSY;
2543 if (!mutex_trylock(&fi->inmem_lock))
2544 return -EAGAIN;
2545 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002546
2547 /*
2548 * A reference is expected if PagePrivate set when move mapping,
2549 * however F2FS breaks this for maintaining dirty page counts when
2550 * truncating pages. So here adjusting the 'extra_count' make it work.
2551 */
2552 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2553 rc = migrate_page_move_mapping(mapping, newpage,
2554 page, NULL, mode, extra_count);
2555 if (rc != MIGRATEPAGE_SUCCESS) {
2556 if (atomic_written)
2557 mutex_unlock(&fi->inmem_lock);
2558 return rc;
2559 }
2560
2561 if (atomic_written) {
2562 struct inmem_pages *cur;
2563 list_for_each_entry(cur, &fi->inmem_pages, list)
2564 if (cur->page == page) {
2565 cur->page = newpage;
2566 break;
2567 }
2568 mutex_unlock(&fi->inmem_lock);
2569 put_page(page);
2570 get_page(newpage);
2571 }
2572
2573 if (PagePrivate(page))
2574 SetPagePrivate(newpage);
2575 set_page_private(newpage, page_private(page));
2576
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07002577 if (mode != MIGRATE_SYNC_NO_COPY)
2578 migrate_page_copy(newpage, page);
2579 else
2580 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002581
2582 return MIGRATEPAGE_SUCCESS;
2583}
2584#endif
2585
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002586const struct address_space_operations f2fs_dblock_aops = {
2587 .readpage = f2fs_read_data_page,
2588 .readpages = f2fs_read_data_pages,
2589 .writepage = f2fs_write_data_page,
2590 .writepages = f2fs_write_data_pages,
2591 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002592 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002593 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002594 .invalidatepage = f2fs_invalidate_page,
2595 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002596 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002597 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002598#ifdef CONFIG_MIGRATION
2599 .migratepage = f2fs_migrate_page,
2600#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002601};
Eric Biggers6dbb1792018-04-18 11:09:48 -07002602
Chao Yuaec2f722018-05-26 18:03:35 +08002603void clear_radix_tree_dirty_tag(struct page *page)
2604{
2605 struct address_space *mapping = page_mapping(page);
2606 unsigned long flags;
2607
2608 xa_lock_irqsave(&mapping->i_pages, flags);
2609 radix_tree_tag_clear(&mapping->i_pages, page_index(page),
2610 PAGECACHE_TAG_DIRTY);
2611 xa_unlock_irqrestore(&mapping->i_pages, flags);
2612}
2613
Eric Biggers6dbb1792018-04-18 11:09:48 -07002614int __init f2fs_init_post_read_processing(void)
2615{
2616 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0);
2617 if (!bio_post_read_ctx_cache)
2618 goto fail;
2619 bio_post_read_ctx_pool =
2620 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
2621 bio_post_read_ctx_cache);
2622 if (!bio_post_read_ctx_pool)
2623 goto fail_free_cache;
2624 return 0;
2625
2626fail_free_cache:
2627 kmem_cache_destroy(bio_post_read_ctx_cache);
2628fail:
2629 return -ENOMEM;
2630}
2631
2632void __exit f2fs_destroy_post_read_processing(void)
2633{
2634 mempool_destroy(bio_post_read_ctx_pool);
2635 kmem_cache_destroy(bio_post_read_ctx_cache);
2636}