blob: 7f7a75d588f1bf5605b1eaa77c4a31fc5bd5377c [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002 * fs/f2fs/data.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/buffer_head.h>
14#include <linux/mpage.h>
15#include <linux/writeback.h>
16#include <linux/backing-dev.h>
Chao Yu8f46dca2015-07-14 18:56:10 +080017#include <linux/pagevec.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090018#include <linux/blkdev.h>
19#include <linux/bio.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010020#include <linux/prefetch.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080021#include <linux/uio.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070022#include <linux/cleancache.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090023
24#include "f2fs.h"
25#include "node.h"
26#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080027#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090028#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090029
Eric Biggersf69e8142018-04-18 11:09:48 -070030#define NUM_PREALLOC_POST_READ_CTXS 128
31
32static struct kmem_cache *bio_post_read_ctx_cache;
33static mempool_t *bio_post_read_ctx_pool;
34
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070035static bool __is_cp_guaranteed(struct page *page)
36{
37 struct address_space *mapping = page->mapping;
38 struct inode *inode;
39 struct f2fs_sb_info *sbi;
40
41 if (!mapping)
42 return false;
43
44 inode = mapping->host;
45 sbi = F2FS_I_SB(inode);
46
47 if (inode->i_ino == F2FS_META_INO(sbi) ||
48 inode->i_ino == F2FS_NODE_INO(sbi) ||
49 S_ISDIR(inode->i_mode) ||
Chao Yua301e762018-05-08 14:06:03 +080050 (S_ISREG(inode->i_mode) &&
51 is_inode_flag_set(inode, FI_ATOMIC_FILE)) ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070052 is_cold_data(page))
53 return true;
54 return false;
55}
56
Eric Biggersf69e8142018-04-18 11:09:48 -070057/* postprocessing steps for read bios */
58enum bio_post_read_step {
59 STEP_INITIAL = 0,
60 STEP_DECRYPT,
61};
62
63struct bio_post_read_ctx {
64 struct bio *bio;
65 struct work_struct work;
66 unsigned int cur_step;
67 unsigned int enabled_steps;
68};
69
70static void __read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090071{
Eric Biggersf69e8142018-04-18 11:09:48 -070072 struct page *page;
73 struct bio_vec *bv;
Linus Torvaldsf5688492014-01-30 11:19:05 -080074 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090075
Eric Biggersf69e8142018-04-18 11:09:48 -070076 bio_for_each_segment_all(bv, bio, i) {
77 page = bv->bv_page;
78
79 /* PG_error was set if any post_read step failed */
80 if (bio->bi_error || PageError(page)) {
81 ClearPageUptodate(page);
82 SetPageError(page);
83 } else {
84 SetPageUptodate(page);
85 }
86 unlock_page(page);
87 }
88 if (bio->bi_private)
89 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
90 bio_put(bio);
91}
92
93static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
94
95static void decrypt_work(struct work_struct *work)
96{
97 struct bio_post_read_ctx *ctx =
98 container_of(work, struct bio_post_read_ctx, work);
99
100 fscrypt_decrypt_bio(ctx->bio);
101
102 bio_post_read_processing(ctx);
103}
104
105static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
106{
107 switch (++ctx->cur_step) {
108 case STEP_DECRYPT:
109 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
110 INIT_WORK(&ctx->work, decrypt_work);
111 fscrypt_enqueue_decrypt_work(&ctx->work);
112 return;
113 }
114 ctx->cur_step++;
115 /* fall-through */
116 default:
117 __read_end_io(ctx->bio);
118 }
119}
120
121static bool f2fs_bio_post_read_required(struct bio *bio)
122{
123 return bio->bi_private && !bio->bi_error;
124}
125
126static void f2fs_read_end_io(struct bio *bio)
127{
Chao Yu8b038c72016-09-18 23:30:07 +0800128#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700129 if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_IO)) {
130 f2fs_show_injection_info(FAULT_IO);
Chao Yu8b038c72016-09-18 23:30:07 +0800131 bio->bi_error = -EIO;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700132 }
Chao Yu8b038c72016-09-18 23:30:07 +0800133#endif
134
Eric Biggersf69e8142018-04-18 11:09:48 -0700135 if (f2fs_bio_post_read_required(bio)) {
136 struct bio_post_read_ctx *ctx = bio->bi_private;
137
138 ctx->cur_step = STEP_INITIAL;
139 bio_post_read_processing(ctx);
140 return;
Chao Yu12377022015-05-25 18:03:38 +0800141 }
142
Eric Biggersf69e8142018-04-18 11:09:48 -0700143 __read_end_io(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900144}
145
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200146static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900147{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +0900148 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -0800149 struct bio_vec *bvec;
150 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900151
Linus Torvaldsf5688492014-01-30 11:19:05 -0800152 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900153 struct page *page = bvec->bv_page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700154 enum count_type type = WB_DATA_TYPE(page);
155
156 if (IS_DUMMY_WRITTEN_PAGE(page)) {
157 set_page_private(page, (unsigned long)NULL);
158 ClearPagePrivate(page);
159 unlock_page(page);
160 mempool_free(page, sbi->write_io_dummy);
161
162 if (unlikely(bio->bi_error))
163 f2fs_stop_checkpoint(sbi, true);
164 continue;
165 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900166
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700167 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700168
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200169 if (unlikely(bio->bi_error)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700170 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kime2709762017-12-31 16:26:38 -0800171 if (type == F2FS_WB_CP_DATA)
172 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900173 }
Yunlei He92b8f9c2018-01-11 14:19:32 +0800174
175 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
176 page->index != nid_of_node(page));
177
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700178 dec_page_count(sbi, type);
Chao Yu3d5c54a2018-08-02 23:03:19 +0800179 if (f2fs_in_warm_node_list(sbi, page))
180 f2fs_del_fsync_node_entry(sbi, page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700181 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900182 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800183 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700184 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700185 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900186 wake_up(&sbi->cp_wait);
187
188 bio_put(bio);
189}
190
Gu Zheng940a6d32013-12-20 17:39:59 +0800191/*
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700192 * Return true, if pre_bio's bdev is same as its target device.
193 */
194struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
195 block_t blk_addr, struct bio *bio)
196{
197 struct block_device *bdev = sbi->sb->s_bdev;
198 int i;
199
200 for (i = 0; i < sbi->s_ndevs; i++) {
201 if (FDEV(i).start_blk <= blk_addr &&
202 FDEV(i).end_blk >= blk_addr) {
203 blk_addr -= FDEV(i).start_blk;
204 bdev = FDEV(i).bdev;
205 break;
206 }
207 }
208 if (bio) {
209 bio->bi_bdev = bdev;
210 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
211 }
212 return bdev;
213}
214
215int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
216{
217 int i;
218
219 for (i = 0; i < sbi->s_ndevs; i++)
220 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
221 return i;
222 return 0;
223}
224
225static bool __same_bdev(struct f2fs_sb_info *sbi,
226 block_t blk_addr, struct bio *bio)
227{
228 return f2fs_target_device(sbi, blk_addr, NULL) == bio->bi_bdev;
229}
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 Yucbd5e5a2018-01-09 19:33:39 +0800235 struct writeback_control *wbc,
Hyunchul Leebcdc5712018-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 Yub5db2de2017-10-28 16:52:31 +0800241 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800242
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700243 f2fs_target_device(sbi, blk_addr, bio);
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900244 if (is_read) {
245 bio->bi_end_io = f2fs_read_end_io;
246 bio->bi_private = NULL;
247 } else {
248 bio->bi_end_io = f2fs_write_end_io;
249 bio->bi_private = sbi;
Chao Yu6b4d6a82018-05-30 00:20:41 +0800250 bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, type, temp);
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900251 }
Yufen Yucbd5e5a2018-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 Kime6b120d2017-07-10 12:55:09 -0700262 unsigned int start;
263
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700264 if (type != DATA && type != NODE)
265 goto submit_io;
266
Yunlong Song94e1c6a2018-07-12 23:09:28 +0800267 if (test_opt(sbi, LFS) && current->plug)
Tiezhu Yang4d8e4a82018-02-06 08:21:45 +0800268 blk_finish_plug(current->plug);
269
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700270 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 Kime6b120d2017-07-10 12:55:09 -0700296submit_io:
297 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 Kime6b120d2017-07-10 12:55:09 -0700313 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 Kime6b120d2017-07-10 12:55:09 -0700322static 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 Kime6b120d2017-07-10 12:55:09 -0700332 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 Kime6b120d2017-07-10 12:55:09 -0700342 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 Kime6b120d2017-07-10 12:55:09 -0700355 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 Kimc4127262017-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 Kimc4127262017-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 Kim9bc576a2017-05-10 11:28:38 -0700376static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kimc4127262017-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 Kimc4127262017-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;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700388 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
389 if (!test_opt(sbi, NOBARRIER))
390 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 Kimc4127262017-05-10 11:18:25 -0700396static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
397 struct inode *inode, nid_t ino, pgoff_t idx,
398 enum page_type type, bool force)
399{
400 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 }
413}
414
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700415void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800416{
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700417 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
Chao Yu0c3a5792016-01-18 18:28:11 +0800418}
419
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700420void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700421 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700422 enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800423{
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700424 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
Chao Yu0c3a5792016-01-18 18:28:11 +0800425}
426
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700427void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800428{
Jaegeuk Kim9bc576a2017-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.
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700436 * 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
Chao Yub3c869a2018-08-01 19:13:44 +0800444 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
445 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
446 return -EFAULT;
447
Chao Yu2ace38e2014-12-24 16:08:14 +0800448 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700449 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900450
451 /* Allocate a new bio */
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800452 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900453 1, is_read_io(fio->op), fio->type, fio->temp);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900454
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300455 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900456 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900457 return -EFAULT;
458 }
Mike Christie04d328d2016-06-05 14:31:55 -0500459 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900460
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700461 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700462
463 if (!is_read_io(fio->op))
464 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900465 return 0;
466}
467
Chao Yu34880e02018-05-28 23:47:18 +0800468void f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900469{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700470 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900471 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700472 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700473 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900474
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700475 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900476
Chao Yuc52dc0f2017-05-19 23:37:01 +0800477 down_write(&io->io_rwsem);
478next:
479 if (fio->in_list) {
480 spin_lock(&io->io_lock);
481 if (list_empty(&io->io_list)) {
482 spin_unlock(&io->io_lock);
Chao Yu34880e02018-05-28 23:47:18 +0800483 goto out;
Chao Yuc52dc0f2017-05-19 23:37:01 +0800484 }
485 fio = list_first_entry(&io->io_list,
486 struct f2fs_io_info, list);
487 list_del(&fio->list);
488 spin_unlock(&io->io_lock);
489 }
490
Chao Yud05e8712018-06-05 17:44:11 +0800491 if (__is_valid_data_blkaddr(fio->old_blkaddr))
Yunlei Hea3f8ec82018-03-08 16:29:13 +0800492 verify_block_addr(fio, fio->old_blkaddr);
493 verify_block_addr(fio, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900494
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700495 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
496
Thomas Meyer59504672017-10-07 16:02:21 +0200497 /* set submitted = true as a return value */
498 fio->submitted = true;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700499
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700500 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700501
Chao Yu7a9d7542016-02-22 18:36:38 +0800502 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700503 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
504 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900505 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900506alloc_new:
507 if (io->bio == NULL) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700508 if ((fio->type == DATA || fio->type == NODE) &&
509 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700510 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Chao Yu34880e02018-05-28 23:47:18 +0800511 fio->retry = true;
512 goto skip;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700513 }
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800514 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Leebcdc5712018-01-31 11:36:57 +0900515 BIO_MAX_PAGES, false,
516 fio->type, fio->temp);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900517 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900518 }
519
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700520 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900521 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900522 goto alloc_new;
523 }
524
Yufen Yucbd5e5a2018-01-09 19:33:39 +0800525 if (fio->io_wbc)
526 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
527
Chao Yu7a9d7542016-02-22 18:36:38 +0800528 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700529 f2fs_trace_ios(fio, 0);
Chao Yuc52dc0f2017-05-19 23:37:01 +0800530
531 trace_f2fs_submit_page_write(fio->page, fio);
Chao Yu34880e02018-05-28 23:47:18 +0800532skip:
Chao Yuc52dc0f2017-05-19 23:37:01 +0800533 if (fio->in_list)
534 goto next;
Chao Yu34880e02018-05-28 23:47:18 +0800535out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800536 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900537}
538
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700539static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -0700540 unsigned nr_pages, unsigned op_flag)
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700541{
542 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700543 struct bio *bio;
Eric Biggersf69e8142018-04-18 11:09:48 -0700544 struct bio_post_read_ctx *ctx;
545 unsigned int post_read_steps = 0;
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700546
Chao Yu10d67032018-07-10 23:01:45 +0800547 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
548 return ERR_PTR(-EFAULT);
549
Eric Biggersf69e8142018-04-18 11:09:48 -0700550 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
551 if (!bio)
552 return ERR_PTR(-ENOMEM);
553 f2fs_target_device(sbi, blkaddr, bio);
554 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -0700555 bio_set_op_attrs(bio, REQ_OP_READ, op_flag);
Eric Biggersf69e8142018-04-18 11:09:48 -0700556
557 if (f2fs_encrypted_file(inode))
558 post_read_steps |= 1 << STEP_DECRYPT;
559 if (post_read_steps) {
560 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
561 if (!ctx) {
562 bio_put(bio);
563 return ERR_PTR(-ENOMEM);
564 }
565 ctx->bio = bio;
566 ctx->enabled_steps = post_read_steps;
567 bio->bi_private = ctx;
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700568
569 /* wait the page to be moved by cleaning */
570 f2fs_wait_on_block_writeback(sbi, blkaddr);
571 }
572
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700573 return bio;
574}
575
576/* This can handle encryption stuffs */
577static int f2fs_submit_page_read(struct inode *inode, struct page *page,
578 block_t blkaddr)
579{
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -0700580 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1, 0);
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700581
582 if (IS_ERR(bio))
583 return PTR_ERR(bio);
584
585 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
586 bio_put(bio);
587 return -EFAULT;
588 }
589 __submit_bio(F2FS_I_SB(inode), bio, DATA);
590 return 0;
591}
592
Chao Yu46008c62016-05-09 19:56:30 +0800593static void __set_data_blkaddr(struct dnode_of_data *dn)
594{
595 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
596 __le32 *addr_array;
Chao Yufbcf9312017-07-19 00:19:06 +0800597 int base = 0;
598
599 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
600 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800601
602 /* Get physical address of data block */
603 addr_array = blkaddr_in_node(rn);
Chao Yufbcf9312017-07-19 00:19:06 +0800604 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800605}
606
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900607/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900608 * Lock ordering for the change of data block address:
609 * ->data_page
610 * ->node_page
611 * update block addresses in the node page
612 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800613void f2fs_set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900614{
Chao Yu46008c62016-05-09 19:56:30 +0800615 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
616 __set_data_blkaddr(dn);
617 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800618 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900619}
620
Chao Yuf28b3432016-02-24 17:16:47 +0800621void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
622{
623 dn->data_blkaddr = blkaddr;
Chao Yu6b4d6a82018-05-30 00:20:41 +0800624 f2fs_set_data_blkaddr(dn);
Chao Yuf28b3432016-02-24 17:16:47 +0800625 f2fs_update_extent_cache(dn);
626}
627
Chao Yu46008c62016-05-09 19:56:30 +0800628/* dn->ofs_in_node will be returned with up-to-date last block pointer */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800629int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900630{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700631 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu09c3a722017-07-09 00:13:07 +0800632 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900633
Chao Yu46008c62016-05-09 19:56:30 +0800634 if (!count)
635 return 0;
636
Jaegeuk Kim91942322016-05-20 10:13:22 -0700637 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900638 return -EPERM;
Chao Yu09c3a722017-07-09 00:13:07 +0800639 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
640 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900641
Chao Yu46008c62016-05-09 19:56:30 +0800642 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
643 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900644
Chao Yu46008c62016-05-09 19:56:30 +0800645 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
646
647 for (; count > 0; dn->ofs_in_node++) {
Chao Yufbcf9312017-07-19 00:19:06 +0800648 block_t blkaddr = datablock_addr(dn->inode,
649 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800650 if (blkaddr == NULL_ADDR) {
651 dn->data_blkaddr = NEW_ADDR;
652 __set_data_blkaddr(dn);
653 count--;
654 }
655 }
656
657 if (set_page_dirty(dn->node_page))
658 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900659 return 0;
660}
661
Chao Yu46008c62016-05-09 19:56:30 +0800662/* Should keep dn->ofs_in_node unchanged */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800663int f2fs_reserve_new_block(struct dnode_of_data *dn)
Chao Yu46008c62016-05-09 19:56:30 +0800664{
665 unsigned int ofs_in_node = dn->ofs_in_node;
666 int ret;
667
Chao Yu6b4d6a82018-05-30 00:20:41 +0800668 ret = f2fs_reserve_new_blocks(dn, 1);
Chao Yu46008c62016-05-09 19:56:30 +0800669 dn->ofs_in_node = ofs_in_node;
670 return ret;
671}
672
Huajun Lib6009652013-11-10 23:13:18 +0800673int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
674{
675 bool need_put = dn->inode_page ? false : true;
676 int err;
677
Chao Yu6b4d6a82018-05-30 00:20:41 +0800678 err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE);
Huajun Lib6009652013-11-10 23:13:18 +0800679 if (err)
680 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900681
Huajun Lib6009652013-11-10 23:13:18 +0800682 if (dn->data_blkaddr == NULL_ADDR)
Chao Yu6b4d6a82018-05-30 00:20:41 +0800683 err = f2fs_reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900684 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800685 f2fs_put_dnode(dn);
686 return err;
687}
688
Fan Li759af1c2015-08-05 15:52:16 +0800689int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900690{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700691 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800692 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800693
Fan Li759af1c2015-08-05 15:52:16 +0800694 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
695 dn->data_blkaddr = ei.blk + index - ei.fofs;
696 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800697 }
698
Fan Li759af1c2015-08-05 15:52:16 +0800699 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900700}
701
Chao Yu6b4d6a82018-05-30 00:20:41 +0800702struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500703 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900704{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900705 struct address_space *mapping = inode->i_mapping;
706 struct dnode_of_data dn;
707 struct page *page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700708 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900709 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700710
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700711 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900712 if (!page)
713 return ERR_PTR(-ENOMEM);
714
Chao Yucb3bc9e2015-02-05 18:03:40 +0800715 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
716 dn.data_blkaddr = ei.blk + index - ei.fofs;
717 goto got_it;
718 }
719
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900720 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800721 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700722 if (err)
723 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900724 f2fs_put_dnode(&dn);
725
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900726 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700727 err = -ENOENT;
728 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900729 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800730got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700731 if (PageUptodate(page)) {
732 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900733 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700734 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900735
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900736 /*
737 * A new dentry page is allocated but not able to be written, since its
738 * new inode page couldn't be allocated due to -ENOSPC.
739 * In such the case, its blkaddr can be remained as NEW_ADDR.
Chao Yu6b4d6a82018-05-30 00:20:41 +0800740 * see, f2fs_add_link -> f2fs_get_new_data_page ->
741 * f2fs_init_inode_metadata.
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900742 */
743 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300744 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700745 if (!PageUptodate(page))
746 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700747 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900748 return page;
749 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900750
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700751 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900752 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700753 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700754 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700755
756put_err:
757 f2fs_put_page(page, 1);
758 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700759}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900760
Chao Yu6b4d6a82018-05-30 00:20:41 +0800761struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700762{
763 struct address_space *mapping = inode->i_mapping;
764 struct page *page;
765
766 page = find_get_page(mapping, index);
767 if (page && PageUptodate(page))
768 return page;
769 f2fs_put_page(page, 0);
770
Chao Yu6b4d6a82018-05-30 00:20:41 +0800771 page = f2fs_get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700772 if (IS_ERR(page))
773 return page;
774
775 if (PageUptodate(page))
776 return page;
777
778 wait_on_page_locked(page);
779 if (unlikely(!PageUptodate(page))) {
780 f2fs_put_page(page, 0);
781 return ERR_PTR(-EIO);
782 }
783 return page;
784}
785
786/*
787 * If it tries to access a hole, return an error.
788 * Because, the callers, functions in dir.c and GC, should be able to know
789 * whether this page exists or not.
790 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800791struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700792 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700793{
794 struct address_space *mapping = inode->i_mapping;
795 struct page *page;
796repeat:
Chao Yu6b4d6a82018-05-30 00:20:41 +0800797 page = f2fs_get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700798 if (IS_ERR(page))
799 return page;
800
801 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900802 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900803 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900804 f2fs_put_page(page, 1);
805 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900806 }
Chao Yu1563ac72016-07-03 22:05:12 +0800807 if (unlikely(!PageUptodate(page))) {
808 f2fs_put_page(page, 1);
809 return ERR_PTR(-EIO);
810 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900811 return page;
812}
813
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900814/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900815 * Caller ensures that this data page is never allocated.
816 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900817 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800818 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
819 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800820 * Note that, ipage is set only by make_empty_dir, and if any error occur,
821 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900822 */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800823struct page *f2fs_get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900824 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900825{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900826 struct address_space *mapping = inode->i_mapping;
827 struct page *page;
828 struct dnode_of_data dn;
829 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800830
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700831 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800832 if (!page) {
833 /*
834 * before exiting, we should make sure ipage will be released
835 * if any error occur.
836 */
837 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700838 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800839 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900840
Jaegeuk Kima8865372013-12-27 17:04:17 +0900841 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800842 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700843 if (err) {
844 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900845 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900846 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700847 if (!ipage)
848 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900849
850 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700851 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900852
853 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300854 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700855 if (!PageUptodate(page))
856 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900857 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700858 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900859
Jaegeuk Kim76121182016-01-01 22:03:47 -0800860 /* if ipage exists, blkaddr should be NEW_ADDR */
861 f2fs_bug_on(F2FS_I_SB(inode), ipage);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800862 page = f2fs_get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700863 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800864 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900865 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700866got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800867 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700868 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700869 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900870 return page;
871}
872
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900873static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900874{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700875 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900876 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900877 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700878 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800879 blkcnt_t count = 1;
Chao Yu09c3a722017-07-09 00:13:07 +0800880 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900881
Jaegeuk Kim91942322016-05-20 10:13:22 -0700882 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900883 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800884
Chao Yu3c743262018-07-17 00:02:17 +0800885 err = f2fs_get_node_info(sbi, dn->nid, &ni);
886 if (err)
887 return err;
888
Chao Yufbcf9312017-07-19 00:19:06 +0800889 dn->data_blkaddr = datablock_addr(dn->inode,
890 dn->node_page, dn->ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800891 if (dn->data_blkaddr == NEW_ADDR)
892 goto alloc;
893
Chao Yu09c3a722017-07-09 00:13:07 +0800894 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
895 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900896
Chao Yudf6136e2015-03-23 10:33:37 +0800897alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900898 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
899
Chao Yu6b4d6a82018-05-30 00:20:41 +0800900 f2fs_allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900901 &sum, seg_type, NULL, false);
Chao Yu6b4d6a82018-05-30 00:20:41 +0800902 f2fs_set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900903
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700904 /* update i_size */
Chao Yu6b4d6a82018-05-30 00:20:41 +0800905 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700906 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300907 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700908 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300909 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900910 return 0;
911}
912
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700913int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800914{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800915 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800916 struct f2fs_map_blocks map;
Chao Yu8fc74462018-01-03 17:30:19 +0800917 int flag;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700918 int err = 0;
Chao Yu8fc74462018-01-03 17:30:19 +0800919 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700920
Weichao Guo8456d342017-09-29 22:43:23 +0800921 /* convert inline data for Direct I/O*/
Chao Yu8fc74462018-01-03 17:30:19 +0800922 if (direct_io) {
Weichao Guo8456d342017-09-29 22:43:23 +0800923 err = f2fs_convert_inline_inode(inode);
924 if (err)
925 return err;
926 }
927
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700928 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
929 return 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800930
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700931 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800932 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
933 if (map.m_len > map.m_lblk)
934 map.m_len -= map.m_lblk;
935 else
936 map.m_len = 0;
937
Chao Yuda859852016-01-26 15:42:58 +0800938 map.m_next_pgofs = NULL;
Chao Yu6bce9632018-01-11 14:42:30 +0800939 map.m_next_extent = NULL;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900940 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800941
Chao Yu8fc74462018-01-03 17:30:19 +0800942 if (direct_io) {
Chao Yu6b4d6a82018-05-30 00:20:41 +0800943 map.m_seg_type = f2fs_rw_hint_to_seg_type(iocb->ki_hint);
Hyunchul Lee355d2342018-03-08 19:34:38 +0900944 flag = f2fs_force_buffered_io(inode, WRITE) ?
Chao Yu8fc74462018-01-03 17:30:19 +0800945 F2FS_GET_BLOCK_PRE_AIO :
946 F2FS_GET_BLOCK_PRE_DIO;
947 goto map_blocks;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +0900948 }
Chao Yue84f88e2017-07-19 00:19:05 +0800949 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700950 err = f2fs_convert_inline_inode(inode);
951 if (err)
952 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800953 }
Chao Yu8fc74462018-01-03 17:30:19 +0800954 if (f2fs_has_inline_data(inode))
Sheng Yong76b6e8e2017-11-22 18:23:39 +0800955 return err;
Chao Yu8fc74462018-01-03 17:30:19 +0800956
957 flag = F2FS_GET_BLOCK_PRE_AIO;
958
959map_blocks:
960 err = f2fs_map_blocks(inode, &map, 1, flag);
961 if (map.m_len > 0 && err == -ENOSPC) {
962 if (!direct_io)
963 set_inode_flag(inode, FI_NO_PREALLOC);
964 err = 0;
Sheng Yong76b6e8e2017-11-22 18:23:39 +0800965 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700966 return err;
967}
968
969static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
970{
971 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
972 if (lock)
973 down_read(&sbi->node_change);
974 else
975 up_read(&sbi->node_change);
976 } else {
977 if (lock)
978 f2fs_lock_op(sbi);
979 else
980 f2fs_unlock_op(sbi);
981 }
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800982}
983
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900984/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700985 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
986 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800987 * If original data blocks are allocated, then give them to blockdev.
988 * Otherwise,
989 * a. preallocate requested block addresses
990 * b. do not use extent cache for better performance
991 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900992 */
Chao Yud323d002015-10-27 09:53:45 +0800993int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800994 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900995{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700996 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900997 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800998 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700999 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +08001000 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001001 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +08001002 unsigned int ofs_in_node, last_ofs_in_node;
1003 blkcnt_t prealloc;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001004 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +08001005 block_t blkaddr;
Chao Yu6bce9632018-01-11 14:42:30 +08001006 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001007
Chao Yudfd02e42016-08-20 15:12:01 +08001008 if (!maxblocks)
1009 return 0;
1010
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001011 map->m_len = 0;
1012 map->m_flags = 0;
1013
1014 /* it only supports block size == page size */
1015 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +08001016 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001017
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001018 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001019 map->m_pblk = ei.blk + pgofs - ei.fofs;
1020 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
1021 map->m_flags = F2FS_MAP_MAPPED;
Chao Yu6bce9632018-01-11 14:42:30 +08001022 if (map->m_next_extent)
1023 *map->m_next_extent = pgofs + map->m_len;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001024 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +08001025 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001026
Chao Yu4fe71e82016-01-26 15:37:38 +08001027next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001028 if (create)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001029 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001030
1031 /* When reading holes, we need its node page */
1032 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001033 err = f2fs_get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +09001034 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +08001035 if (flag == F2FS_GET_BLOCK_BMAP)
1036 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +08001037 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001038 err = 0;
Chao Yuda859852016-01-26 15:42:58 +08001039 if (map->m_next_pgofs)
1040 *map->m_next_pgofs =
Chao Yu6b4d6a82018-05-30 00:20:41 +08001041 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yu6bce9632018-01-11 14:42:30 +08001042 if (map->m_next_extent)
1043 *map->m_next_extent =
Chao Yu6b4d6a82018-05-30 00:20:41 +08001044 f2fs_get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +08001045 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001046 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +09001047 }
Chao Yu973163f2015-09-18 16:51:51 +08001048
Chao Yu6bce9632018-01-11 14:42:30 +08001049 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +08001050 prealloc = 0;
Arnd Bergmanna299abd2016-11-02 14:52:15 +01001051 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +08001052 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001053
Chao Yu4fe71e82016-01-26 15:37:38 +08001054next_block:
Chao Yufbcf9312017-07-19 00:19:06 +08001055 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +08001056
Chao Yub3c869a2018-08-01 19:13:44 +08001057 if (__is_valid_data_blkaddr(blkaddr) &&
1058 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
1059 err = -EFAULT;
1060 goto sync_out;
1061 }
1062
Chao Yud05e8712018-06-05 17:44:11 +08001063 if (!is_valid_data_blkaddr(sbi, blkaddr)) {
Fan Li7df3a432015-12-17 13:20:59 +08001064 if (create) {
1065 if (unlikely(f2fs_cp_error(sbi))) {
1066 err = -EIO;
1067 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +08001068 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001069 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +08001070 if (blkaddr == NULL_ADDR) {
1071 prealloc++;
1072 last_ofs_in_node = dn.ofs_in_node;
1073 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001074 } else {
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001075 err = __allocate_data_block(&dn,
1076 map->m_seg_type);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001077 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001078 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001079 }
Fan Li7df3a432015-12-17 13:20:59 +08001080 if (err)
1081 goto sync_out;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001082 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001083 blkaddr = dn.data_blkaddr;
1084 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001085 if (flag == F2FS_GET_BLOCK_BMAP) {
1086 map->m_pblk = 0;
1087 goto sync_out;
1088 }
Chao Yu6bce9632018-01-11 14:42:30 +08001089 if (flag == F2FS_GET_BLOCK_PRECACHE)
1090 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001091 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1092 blkaddr == NULL_ADDR) {
1093 if (map->m_next_pgofs)
1094 *map->m_next_pgofs = pgofs + 1;
Chao Yu987892c2017-11-30 19:28:22 +08001095 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001096 }
Chao Yufd214422018-01-10 18:18:52 +08001097 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1098 /* for defragment case */
1099 if (map->m_next_pgofs)
1100 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001101 goto sync_out;
Chao Yufd214422018-01-10 18:18:52 +08001102 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001103 }
1104 }
Fan Li7df3a432015-12-17 13:20:59 +08001105
Chao Yu46008c62016-05-09 19:56:30 +08001106 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1107 goto skip;
1108
Chao Yu4fe71e82016-01-26 15:37:38 +08001109 if (map->m_len == 0) {
1110 /* preallocated unwritten block should be mapped for fiemap. */
1111 if (blkaddr == NEW_ADDR)
1112 map->m_flags |= F2FS_MAP_UNWRITTEN;
1113 map->m_flags |= F2FS_MAP_MAPPED;
1114
1115 map->m_pblk = blkaddr;
1116 map->m_len = 1;
1117 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001118 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001119 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001120 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001121 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001122 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001123 } else {
1124 goto sync_out;
1125 }
1126
Chao Yu46008c62016-05-09 19:56:30 +08001127skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001128 dn.ofs_in_node++;
1129 pgofs++;
1130
Chao Yu46008c62016-05-09 19:56:30 +08001131 /* preallocate blocks in batch for one dnode page */
1132 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1133 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001134
Chao Yu46008c62016-05-09 19:56:30 +08001135 dn.ofs_in_node = ofs_in_node;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001136 err = f2fs_reserve_new_blocks(&dn, prealloc);
Chao Yu46008c62016-05-09 19:56:30 +08001137 if (err)
1138 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001139
Chao Yu46008c62016-05-09 19:56:30 +08001140 map->m_len += dn.ofs_in_node - ofs_in_node;
1141 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1142 err = -ENOSPC;
1143 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001144 }
Chao Yu46008c62016-05-09 19:56:30 +08001145 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001146 }
1147
Chao Yu46008c62016-05-09 19:56:30 +08001148 if (pgofs >= end)
1149 goto sync_out;
1150 else if (dn.ofs_in_node < end_offset)
1151 goto next_block;
1152
Chao Yu6bce9632018-01-11 14:42:30 +08001153 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1154 if (map->m_flags & F2FS_MAP_MAPPED) {
1155 unsigned int ofs = start_pgofs - map->m_lblk;
1156
1157 f2fs_update_extent_cache_range(&dn,
1158 start_pgofs, map->m_pblk + ofs,
1159 map->m_len - ofs);
1160 }
1161 }
1162
Chao Yu46008c62016-05-09 19:56:30 +08001163 f2fs_put_dnode(&dn);
1164
1165 if (create) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001166 __do_map_lock(sbi, flag, false);
1167 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001168 }
Chao Yu46008c62016-05-09 19:56:30 +08001169 goto next_dnode;
1170
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001171sync_out:
Chao Yu6bce9632018-01-11 14:42:30 +08001172 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1173 if (map->m_flags & F2FS_MAP_MAPPED) {
1174 unsigned int ofs = start_pgofs - map->m_lblk;
1175
1176 f2fs_update_extent_cache_range(&dn,
1177 start_pgofs, map->m_pblk + ofs,
1178 map->m_len - ofs);
1179 }
1180 if (map->m_next_extent)
1181 *map->m_next_extent = pgofs + 1;
1182 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001183 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001184unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001185 if (create) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001186 __do_map_lock(sbi, flag, false);
1187 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001188 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001189out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001190 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001191 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001192}
1193
Hyunchul Lee355d2342018-03-08 19:34:38 +09001194bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1195{
1196 struct f2fs_map_blocks map;
1197 block_t last_lblk;
1198 int err;
1199
1200 if (pos + len > i_size_read(inode))
1201 return false;
1202
1203 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1204 map.m_next_pgofs = NULL;
1205 map.m_next_extent = NULL;
1206 map.m_seg_type = NO_CHECK_TYPE;
1207 last_lblk = F2FS_BLK_ALIGN(pos + len);
1208
1209 while (map.m_lblk < last_lblk) {
1210 map.m_len = last_lblk - map.m_lblk;
1211 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1212 if (err || map.m_len == 0)
1213 return false;
1214 map.m_lblk += map.m_len;
1215 }
1216 return true;
1217}
1218
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001219static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001220 struct buffer_head *bh, int create, int flag,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001221 pgoff_t *next_pgofs, int seg_type)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001222{
1223 struct f2fs_map_blocks map;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001224 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001225
1226 map.m_lblk = iblock;
1227 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001228 map.m_next_pgofs = next_pgofs;
Chao Yu6bce9632018-01-11 14:42:30 +08001229 map.m_next_extent = NULL;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001230 map.m_seg_type = seg_type;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001231
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001232 err = f2fs_map_blocks(inode, &map, create, flag);
1233 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001234 map_bh(bh, inode->i_sb, map.m_pblk);
1235 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001236 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001237 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001238 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001239}
1240
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001241static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001242 struct buffer_head *bh_result, int create, int flag,
1243 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001244{
Chao Yuda859852016-01-26 15:42:58 +08001245 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001246 flag, next_pgofs,
1247 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001248}
1249
Chao Yue2b4e2b2015-08-19 19:11:19 +08001250static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001251 struct buffer_head *bh_result, int create)
1252{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001253 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001254 F2FS_GET_BLOCK_DEFAULT, NULL,
Chao Yu6b4d6a82018-05-30 00:20:41 +08001255 f2fs_rw_hint_to_seg_type(
Hyunchul Leebcdc5712018-01-31 11:36:57 +09001256 inode->i_write_hint));
Chao Yue2b4e2b2015-08-19 19:11:19 +08001257}
1258
1259static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1260 struct buffer_head *bh_result, int create)
1261{
Yunlei He179448b2015-12-28 21:48:32 +08001262 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001263 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001264 return -EFBIG;
1265
Chao Yue2b4e2b2015-08-19 19:11:19 +08001266 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001267 F2FS_GET_BLOCK_BMAP, NULL,
1268 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001269}
1270
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001271static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1272{
1273 return (offset >> inode->i_blkbits);
1274}
1275
1276static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1277{
1278 return (blk << inode->i_blkbits);
1279}
1280
Chao Yu08ddb192018-01-11 14:39:57 +08001281static int f2fs_xattr_fiemap(struct inode *inode,
1282 struct fiemap_extent_info *fieinfo)
1283{
1284 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1285 struct page *page;
1286 struct node_info ni;
1287 __u64 phys = 0, len;
1288 __u32 flags;
1289 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1290 int err = 0;
1291
1292 if (f2fs_has_inline_xattr(inode)) {
1293 int offset;
1294
1295 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1296 inode->i_ino, false);
1297 if (!page)
1298 return -ENOMEM;
1299
Chao Yu3c743262018-07-17 00:02:17 +08001300 err = f2fs_get_node_info(sbi, inode->i_ino, &ni);
1301 if (err) {
1302 f2fs_put_page(page, 1);
1303 return err;
1304 }
Chao Yu08ddb192018-01-11 14:39:57 +08001305
1306 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1307 offset = offsetof(struct f2fs_inode, i_addr) +
1308 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yu3aa46e22018-01-17 16:31:36 +08001309 get_inline_xattr_addrs(inode));
Chao Yu08ddb192018-01-11 14:39:57 +08001310
1311 phys += offset;
1312 len = inline_xattr_size(inode);
1313
1314 f2fs_put_page(page, 1);
1315
1316 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1317
1318 if (!xnid)
1319 flags |= FIEMAP_EXTENT_LAST;
1320
1321 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1322 if (err || err == 1)
1323 return err;
1324 }
1325
1326 if (xnid) {
1327 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1328 if (!page)
1329 return -ENOMEM;
1330
Chao Yu3c743262018-07-17 00:02:17 +08001331 err = f2fs_get_node_info(sbi, xnid, &ni);
1332 if (err) {
1333 f2fs_put_page(page, 1);
1334 return err;
1335 }
Chao Yu08ddb192018-01-11 14:39:57 +08001336
1337 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1338 len = inode->i_sb->s_blocksize;
1339
1340 f2fs_put_page(page, 1);
1341
1342 flags = FIEMAP_EXTENT_LAST;
1343 }
1344
1345 if (phys)
1346 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1347
1348 return (err < 0 ? err : 0);
1349}
1350
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001351int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1352 u64 start, u64 len)
1353{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001354 struct buffer_head map_bh;
1355 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001356 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001357 u64 logical = 0, phys = 0, size = 0;
1358 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001359 int ret = 0;
1360
Chao Yu6bce9632018-01-11 14:42:30 +08001361 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1362 ret = f2fs_precache_extents(inode);
1363 if (ret)
1364 return ret;
1365 }
1366
Chao Yu08ddb192018-01-11 14:39:57 +08001367 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001368 if (ret)
1369 return ret;
1370
Chao Yuaa9c1c12018-01-11 14:37:35 +08001371 inode_lock(inode);
1372
Chao Yu08ddb192018-01-11 14:39:57 +08001373 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1374 ret = f2fs_xattr_fiemap(inode, fieinfo);
1375 goto out;
1376 }
1377
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -07001378 if (f2fs_has_inline_data(inode)) {
1379 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1380 if (ret != -EAGAIN)
Chao Yuaa9c1c12018-01-11 14:37:35 +08001381 goto out;
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -07001382 }
1383
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001384 if (logical_to_blk(inode, len) == 0)
1385 len = blk_to_logical(inode, 1);
1386
1387 start_blk = logical_to_blk(inode, start);
1388 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001389
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001390next:
1391 memset(&map_bh, 0, sizeof(struct buffer_head));
1392 map_bh.b_size = len;
1393
Chao Yue2b4e2b2015-08-19 19:11:19 +08001394 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001395 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001396 if (ret)
1397 goto out;
1398
1399 /* HOLE */
1400 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001401 start_blk = next_pgofs;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001402
1403 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1404 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001405 goto prep_next;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001406
Fan Li9a950d52015-12-26 18:07:41 +08001407 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001408 }
Fan Li9a950d52015-12-26 18:07:41 +08001409
Chao Yuda5af122016-01-08 20:19:27 +08001410 if (size) {
1411 if (f2fs_encrypted_inode(inode))
1412 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1413
Fan Li9a950d52015-12-26 18:07:41 +08001414 ret = fiemap_fill_next_extent(fieinfo, logical,
1415 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001416 }
Fan Li9a950d52015-12-26 18:07:41 +08001417
1418 if (start_blk > last_blk || ret)
1419 goto out;
1420
1421 logical = blk_to_logical(inode, start_blk);
1422 phys = blk_to_logical(inode, map_bh.b_blocknr);
1423 size = map_bh.b_size;
1424 flags = 0;
1425 if (buffer_unwritten(&map_bh))
1426 flags = FIEMAP_EXTENT_UNWRITTEN;
1427
1428 start_blk += logical_to_blk(inode, size);
1429
1430prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001431 cond_resched();
1432 if (fatal_signal_pending(current))
1433 ret = -EINTR;
1434 else
1435 goto next;
1436out:
1437 if (ret == 1)
1438 ret = 0;
1439
Al Viro59551022016-01-22 15:40:57 -05001440 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001441 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001442}
1443
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001444/*
1445 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1446 * Major change was from block_size == page_size in f2fs by default.
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001447 *
1448 * Note that the aops->readpages() function is ONLY used for read-ahead. If
1449 * this function ever deviates from doing just read-ahead, it should either
1450 * use ->readpage() or do the necessary surgery to decouple ->readpages()
1451 * from read-ahead.
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001452 */
1453static int f2fs_mpage_readpages(struct address_space *mapping,
1454 struct list_head *pages, struct page *page,
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001455 unsigned nr_pages, bool is_readahead)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001456{
1457 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001458 sector_t last_block_in_bio = 0;
1459 struct inode *inode = mapping->host;
1460 const unsigned blkbits = inode->i_blkbits;
1461 const unsigned blocksize = 1 << blkbits;
1462 sector_t block_in_file;
1463 sector_t last_block;
1464 sector_t last_block_in_file;
1465 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001466 struct f2fs_map_blocks map;
1467
1468 map.m_pblk = 0;
1469 map.m_lblk = 0;
1470 map.m_len = 0;
1471 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001472 map.m_next_pgofs = NULL;
Chao Yu6bce9632018-01-11 14:42:30 +08001473 map.m_next_extent = NULL;
Hyunchul Lee1a9d6a92017-11-28 09:23:00 +09001474 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001475
LiFanba4556c2017-11-25 11:46:18 +08001476 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001477 if (pages) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001478 page = list_last_entry(pages, struct page, lru);
1479
1480 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001481 list_del(&page->lru);
1482 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001483 page->index,
1484 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001485 goto next_page;
1486 }
1487
1488 block_in_file = (sector_t)page->index;
1489 last_block = block_in_file + nr_pages;
1490 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1491 blkbits;
1492 if (last_block > last_block_in_file)
1493 last_block = last_block_in_file;
1494
1495 /*
1496 * Map blocks using the previous result first.
1497 */
1498 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1499 block_in_file > map.m_lblk &&
1500 block_in_file < (map.m_lblk + map.m_len))
1501 goto got_it;
1502
1503 /*
1504 * Then do more f2fs_map_blocks() calls until we are
1505 * done with this page.
1506 */
1507 map.m_flags = 0;
1508
1509 if (block_in_file < last_block) {
1510 map.m_lblk = block_in_file;
1511 map.m_len = last_block - block_in_file;
1512
Chao Yu46c9e142015-09-18 16:54:16 +08001513 if (f2fs_map_blocks(inode, &map, 0,
Qiuyang Sun9afa1592017-08-09 17:27:30 +08001514 F2FS_GET_BLOCK_DEFAULT))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001515 goto set_error_page;
1516 }
1517got_it:
1518 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1519 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1520 SetPageMappedToDisk(page);
1521
1522 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1523 SetPageUptodate(page);
1524 goto confused;
1525 }
Chao Yub3c869a2018-08-01 19:13:44 +08001526
1527 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1528 DATA_GENERIC))
1529 goto set_error_page;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001530 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001531 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001532 if (!PageUptodate(page))
1533 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001534 unlock_page(page);
1535 goto next_page;
1536 }
1537
1538 /*
1539 * This page will go to BIO. Do we need to send this
1540 * BIO off first?
1541 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001542 if (bio && (last_block_in_bio != block_nr - 1 ||
1543 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001544submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001545 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001546 bio = NULL;
1547 }
1548 if (bio == NULL) {
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001549 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages,
1550 is_readahead ? REQ_RAHEAD : 0);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001551 if (IS_ERR(bio)) {
1552 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001553 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001554 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001555 }
1556
1557 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1558 goto submit_and_realloc;
1559
1560 last_block_in_bio = block_nr;
1561 goto next_page;
1562set_error_page:
1563 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001564 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001565 unlock_page(page);
1566 goto next_page;
1567confused:
1568 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001569 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001570 bio = NULL;
1571 }
1572 unlock_page(page);
1573next_page:
1574 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001575 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001576 }
1577 BUG_ON(pages && !list_empty(pages));
1578 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001579 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001580 return 0;
1581}
1582
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001583static int f2fs_read_data_page(struct file *file, struct page *page)
1584{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001585 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001586 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001587
Chao Yuc20e89c2014-05-06 16:53:08 +08001588 trace_f2fs_readpage(page, DATA);
1589
arter97e1c42042014-08-06 23:22:50 +09001590 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001591 if (f2fs_has_inline_data(inode))
1592 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001593 if (ret == -EAGAIN)
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001594 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1, false);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001595 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001596}
1597
1598static int f2fs_read_data_pages(struct file *file,
1599 struct address_space *mapping,
1600 struct list_head *pages, unsigned nr_pages)
1601{
Hsiang Kao3f01af72017-09-24 02:45:42 +08001602 struct inode *inode = mapping->host;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001603 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001604
1605 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001606
1607 /* If the file has inline data, skip readpages */
1608 if (f2fs_has_inline_data(inode))
1609 return 0;
1610
Jaegeuk Kimf9d75d62018-06-21 11:29:43 -07001611 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001612}
1613
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001614static int encrypt_one_page(struct f2fs_io_info *fio)
1615{
1616 struct inode *inode = fio->page->mapping->host;
1617 gfp_t gfp_flags = GFP_NOFS;
1618
Jaegeuk Kima4fb1892017-09-05 16:54:24 -07001619 if (!f2fs_encrypted_file(inode))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001620 return 0;
1621
Eric Biggersf69e8142018-04-18 11:09:48 -07001622 /* wait for GCed page writeback via META_MAPPING */
Jaegeuk Kimfb605d02017-09-05 17:04:35 -07001623 f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001624
1625retry_encrypt:
1626 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1627 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1628 if (!IS_ERR(fio->encrypted_page))
1629 return 0;
1630
1631 /* flush pending IOs and wait for a while in the ENOMEM case */
1632 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001633 f2fs_flush_merged_writes(fio->sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001634 congestion_wait(BLK_RW_ASYNC, HZ/50);
1635 gfp_flags |= __GFP_NOFAIL;
1636 goto retry_encrypt;
1637 }
1638 return PTR_ERR(fio->encrypted_page);
1639}
1640
Chao Yu66356ee2018-01-17 16:31:38 +08001641static inline bool check_inplace_update_policy(struct inode *inode,
1642 struct f2fs_io_info *fio)
1643{
1644 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1645 unsigned int policy = SM_I(sbi)->ipu_policy;
1646
1647 if (policy & (0x1 << F2FS_IPU_FORCE))
1648 return true;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001649 if (policy & (0x1 << F2FS_IPU_SSR) && f2fs_need_SSR(sbi))
Chao Yu66356ee2018-01-17 16:31:38 +08001650 return true;
1651 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1652 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1653 return true;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001654 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && f2fs_need_SSR(sbi) &&
Chao Yu66356ee2018-01-17 16:31:38 +08001655 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1656 return true;
1657
1658 /*
1659 * IPU for rewrite async pages
1660 */
1661 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1662 fio && fio->op == REQ_OP_WRITE &&
1663 !(fio->op_flags & REQ_SYNC) &&
1664 !f2fs_encrypted_inode(inode))
1665 return true;
1666
1667 /* this is only set during fdatasync */
1668 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1669 is_inode_flag_set(inode, FI_NEED_IPU))
1670 return true;
1671
1672 return false;
1673}
1674
Chao Yu6b4d6a82018-05-30 00:20:41 +08001675bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yu66356ee2018-01-17 16:31:38 +08001676{
1677 if (f2fs_is_pinned_file(inode))
1678 return true;
1679
1680 /* if this is cold file, we should overwrite to avoid fragmentation */
1681 if (file_is_cold(inode))
1682 return true;
1683
1684 return check_inplace_update_policy(inode, fio);
1685}
1686
Chao Yu6b4d6a82018-05-30 00:20:41 +08001687bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
Chao Yu66356ee2018-01-17 16:31:38 +08001688{
1689 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1690
1691 if (test_opt(sbi, LFS))
1692 return true;
1693 if (S_ISDIR(inode->i_mode))
1694 return true;
1695 if (f2fs_is_atomic_file(inode))
1696 return true;
1697 if (fio) {
1698 if (is_cold_data(fio->page))
1699 return true;
1700 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1701 return true;
1702 }
1703 return false;
1704}
1705
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001706static inline bool need_inplace_update(struct f2fs_io_info *fio)
1707{
1708 struct inode *inode = fio->page->mapping->host;
1709
Chao Yu6b4d6a82018-05-30 00:20:41 +08001710 if (f2fs_should_update_outplace(inode, fio))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001711 return false;
1712
Chao Yu6b4d6a82018-05-30 00:20:41 +08001713 return f2fs_should_update_inplace(inode, fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001714}
1715
Chao Yu6b4d6a82018-05-30 00:20:41 +08001716int f2fs_do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001717{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001718 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001719 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001720 struct dnode_of_data dn;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001721 struct extent_info ei = {0,0,0};
Chao Yu3c743262018-07-17 00:02:17 +08001722 struct node_info ni;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001723 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001724 int err = 0;
1725
1726 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001727 if (need_inplace_update(fio) &&
1728 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1729 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
1730
Chao Yub3c869a2018-08-01 19:13:44 +08001731 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1732 DATA_GENERIC))
1733 return -EFAULT;
1734
1735 ipu_force = true;
1736 fio->need_lock = LOCK_DONE;
1737 goto got_it;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001738 }
1739
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07001740 /* Deadlock due to between page->lock and f2fs_lock_op */
1741 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1742 return -EAGAIN;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001743
Chao Yu6b4d6a82018-05-30 00:20:41 +08001744 err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001745 if (err)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001746 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001747
Chao Yu28bc1062016-02-06 14:40:34 +08001748 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001749
1750 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001751 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001752 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001753 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001754 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001755got_it:
Chao Yub3c869a2018-08-01 19:13:44 +08001756 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
1757 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1758 DATA_GENERIC)) {
1759 err = -EFAULT;
1760 goto out_writepage;
1761 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001762 /*
1763 * If current allocation needs SSR,
1764 * it had better in-place writes for updated data.
1765 */
Chao Yud05e8712018-06-05 17:44:11 +08001766 if (ipu_force || (is_valid_data_blkaddr(fio->sbi, fio->old_blkaddr) &&
Chao Yu158d9bb2018-05-23 22:25:08 +08001767 need_inplace_update(fio))) {
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001768 err = encrypt_one_page(fio);
1769 if (err)
1770 goto out_writepage;
1771
1772 set_page_writeback(page);
Jaegeuk Kim926b3702018-04-11 23:09:04 -07001773 ClearPageError(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001774 f2fs_put_dnode(&dn);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001775 if (fio->need_lock == LOCK_REQ)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001776 f2fs_unlock_op(fio->sbi);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001777 err = f2fs_inplace_write_data(fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001778 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001779 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001780 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001781 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001782
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001783 if (fio->need_lock == LOCK_RETRY) {
1784 if (!f2fs_trylock_op(fio->sbi)) {
1785 err = -EAGAIN;
1786 goto out_writepage;
1787 }
1788 fio->need_lock = LOCK_REQ;
1789 }
1790
Chao Yu3c743262018-07-17 00:02:17 +08001791 err = f2fs_get_node_info(fio->sbi, dn.nid, &ni);
1792 if (err)
1793 goto out_writepage;
1794
1795 fio->version = ni.version;
1796
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001797 err = encrypt_one_page(fio);
1798 if (err)
1799 goto out_writepage;
1800
1801 set_page_writeback(page);
Jaegeuk Kim926b3702018-04-11 23:09:04 -07001802 ClearPageError(page);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001803
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001804 /* LFS mode write path */
Chao Yu6b4d6a82018-05-30 00:20:41 +08001805 f2fs_outplace_write_data(&dn, fio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001806 trace_f2fs_do_write_data_page(page, OPU);
1807 set_inode_flag(inode, FI_APPEND_WRITE);
1808 if (page->index == 0)
1809 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001810out_writepage:
1811 f2fs_put_dnode(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001812out:
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001813 if (fio->need_lock == LOCK_REQ)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001814 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001815 return err;
1816}
1817
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001818static int __write_data_page(struct page *page, bool *submitted,
Chao Yuc0fe4882017-08-02 23:21:48 +08001819 struct writeback_control *wbc,
1820 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001821{
1822 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001823 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001824 loff_t i_size = i_size_read(inode);
1825 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001826 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001827 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001828 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001829 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001830 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001831 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001832 .sbi = sbi,
Chao Yuf014be82017-09-29 13:59:38 +08001833 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001834 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001835 .op = REQ_OP_WRITE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001836 .op_flags = wbc_to_write_flags(wbc),
1837 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001838 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001839 .encrypted_page = NULL,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001840 .submitted = false,
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001841 .need_lock = LOCK_RETRY,
Chao Yuc0fe4882017-08-02 23:21:48 +08001842 .io_type = io_type,
Yufen Yucbd5e5a2018-01-09 19:33:39 +08001843 .io_wbc = wbc,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001844 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001845
Chao Yuecda0de2014-05-06 16:48:26 +08001846 trace_f2fs_writepage(page, DATA);
1847
Chao Yu86f78c12018-01-18 17:29:10 +08001848 /* we should bypass data pages to proceed the kworkder jobs */
1849 if (unlikely(f2fs_cp_error(sbi))) {
1850 mapping_set_error(page->mapping, -EIO);
Chao Yu9780d682018-05-28 16:59:26 +08001851 /*
1852 * don't drop any dirty dentry pages for keeping lastest
1853 * directory structure.
1854 */
1855 if (S_ISDIR(inode->i_mode))
1856 goto redirty_out;
Chao Yu86f78c12018-01-18 17:29:10 +08001857 goto out;
1858 }
1859
Chao Yu00cc7ed2017-06-29 23:20:45 +08001860 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1861 goto redirty_out;
1862
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001863 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001864 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001865
1866 /*
1867 * If the offset is out-of-range of file size,
1868 * this page does not have to be written to disk.
1869 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001870 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001871 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001872 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001873
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001874 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001875write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001876 if (f2fs_is_drop_cache(inode))
1877 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001878 /* we should not write 0'th page having journal header */
1879 if (f2fs_is_volatile_file(inode) && (!page->index ||
1880 (!wbc->for_reclaim &&
Chao Yu6b4d6a82018-05-30 00:20:41 +08001881 f2fs_available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001882 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001883
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001884 /* Dentry blocks are controlled by checkpoint */
1885 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001886 fio.need_lock = LOCK_DONE;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001887 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001888 goto done;
1889 }
1890
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001891 if (!wbc->for_reclaim)
1892 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001893 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001894 goto redirty_out;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001895 else
1896 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001897
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001898 err = -EAGAIN;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001899 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001900 err = f2fs_write_inline_data(inode, page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001901 if (!err)
1902 goto out;
1903 }
1904
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001905 if (err == -EAGAIN) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08001906 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001907 if (err == -EAGAIN) {
1908 fio.need_lock = LOCK_REQ;
Chao Yu6b4d6a82018-05-30 00:20:41 +08001909 err = f2fs_do_write_data_page(&fio);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001910 }
1911 }
Chao Yu33fdebb2017-10-09 17:55:19 +08001912
Chao Yu5912fba2018-01-17 16:31:37 +08001913 if (err) {
1914 file_set_keep_isize(inode);
1915 } else {
1916 down_write(&F2FS_I(inode)->i_sem);
1917 if (F2FS_I(inode)->last_disk_size < psize)
1918 F2FS_I(inode)->last_disk_size = psize;
1919 up_write(&F2FS_I(inode)->i_sem);
1920 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001921
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001922done:
1923 if (err && err != -ENOENT)
1924 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001925
Jaegeuk Kim39936832012-11-22 16:21:29 +09001926out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001927 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001928 if (err)
1929 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001930
1931 if (wbc->for_reclaim) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001932 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001933 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yu6b4d6a82018-05-30 00:20:41 +08001934 f2fs_remove_dirty_inode(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001935 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001936 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001937
1938 unlock_page(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001939 if (!S_ISDIR(inode->i_mode))
1940 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001941
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001942 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001943 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001944 submitted = NULL;
1945 }
1946
1947 if (submitted)
1948 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001949
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001950 return 0;
1951
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001952redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001953 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim5b37ebb2018-05-03 23:26:02 -07001954 /*
1955 * pageout() in MM traslates EAGAIN, so calls handle_write_error()
1956 * -> mapping_set_error() -> set_bit(AS_EIO, ...).
1957 * file_write_and_wait_range() will see EIO error, which is critical
1958 * to return value of fsync() followed by atomic_write failure to user.
1959 */
1960 if (!err || wbc->for_reclaim)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001961 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001962 unlock_page(page);
1963 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001964}
1965
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001966static int f2fs_write_data_page(struct page *page,
1967 struct writeback_control *wbc)
1968{
Chao Yuc0fe4882017-08-02 23:21:48 +08001969 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001970}
1971
Chao Yu8f46dca2015-07-14 18:56:10 +08001972/*
1973 * This function was copied from write_cche_pages from mm/page-writeback.c.
1974 * The major change is making write step of cold data page separately from
1975 * warm/hot data page.
1976 */
1977static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yuc0fe4882017-08-02 23:21:48 +08001978 struct writeback_control *wbc,
1979 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08001980{
1981 int ret = 0;
1982 int done = 0;
1983 struct pagevec pvec;
Chao Yudeb78d42018-06-04 23:20:36 +08001984 struct f2fs_sb_info *sbi = F2FS_M_SB(mapping);
Chao Yu8f46dca2015-07-14 18:56:10 +08001985 int nr_pages;
1986 pgoff_t uninitialized_var(writeback_index);
1987 pgoff_t index;
1988 pgoff_t end; /* Inclusive */
1989 pgoff_t done_index;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001990 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001991 int cycled;
1992 int range_whole = 0;
1993 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001994
1995 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001996
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001997 if (get_dirty_pages(mapping->host) <=
1998 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1999 set_inode_flag(mapping->host, FI_HOT_DATA);
2000 else
2001 clear_inode_flag(mapping->host, FI_HOT_DATA);
2002
Chao Yu8f46dca2015-07-14 18:56:10 +08002003 if (wbc->range_cyclic) {
2004 writeback_index = mapping->writeback_index; /* prev offset */
2005 index = writeback_index;
2006 if (index == 0)
2007 cycled = 1;
2008 else
2009 cycled = 0;
2010 end = -1;
2011 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002012 index = wbc->range_start >> PAGE_SHIFT;
2013 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08002014 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2015 range_whole = 1;
2016 cycled = 1; /* ignore range_cyclic tests */
2017 }
2018 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2019 tag = PAGECACHE_TAG_TOWRITE;
2020 else
2021 tag = PAGECACHE_TAG_DIRTY;
2022retry:
2023 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2024 tag_pages_for_writeback(mapping, index, end);
2025 done_index = index;
2026 while (!done && (index <= end)) {
2027 int i;
2028
Jan Karae0673652017-11-15 17:34:48 -08002029 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara4d93a432017-11-15 17:35:19 -08002030 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08002031 if (nr_pages == 0)
2032 break;
2033
2034 for (i = 0; i < nr_pages; i++) {
2035 struct page *page = pvec.pages[i];
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002036 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08002037
Chao Yu458e47f2018-05-23 22:25:09 +08002038 /* give a priority to WB_SYNC threads */
Chao Yudeb78d42018-06-04 23:20:36 +08002039 if (atomic_read(&sbi->wb_sync_req[DATA]) &&
Chao Yu458e47f2018-05-23 22:25:09 +08002040 wbc->sync_mode == WB_SYNC_NONE) {
2041 done = 1;
2042 break;
2043 }
2044
Chao Yu8f46dca2015-07-14 18:56:10 +08002045 done_index = page->index;
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07002046retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08002047 lock_page(page);
2048
2049 if (unlikely(page->mapping != mapping)) {
2050continue_unlock:
2051 unlock_page(page);
2052 continue;
2053 }
2054
2055 if (!PageDirty(page)) {
2056 /* someone wrote it for us */
2057 goto continue_unlock;
2058 }
2059
Chao Yu8f46dca2015-07-14 18:56:10 +08002060 if (PageWriteback(page)) {
2061 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002062 f2fs_wait_on_page_writeback(page,
2063 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08002064 else
2065 goto continue_unlock;
2066 }
2067
2068 BUG_ON(PageWriteback(page));
2069 if (!clear_page_dirty_for_io(page))
2070 goto continue_unlock;
2071
Chao Yuc0fe4882017-08-02 23:21:48 +08002072 ret = __write_data_page(page, &submitted, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08002073 if (unlikely(ret)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002074 /*
2075 * keep nr_to_write, since vfs uses this to
2076 * get # of written pages.
2077 */
2078 if (ret == AOP_WRITEPAGE_ACTIVATE) {
2079 unlock_page(page);
2080 ret = 0;
2081 continue;
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07002082 } else if (ret == -EAGAIN) {
2083 ret = 0;
2084 if (wbc->sync_mode == WB_SYNC_ALL) {
2085 cond_resched();
2086 congestion_wait(BLK_RW_ASYNC,
2087 HZ/50);
2088 goto retry_write;
2089 }
2090 continue;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002091 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07002092 done_index = page->index + 1;
2093 done = 1;
2094 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002095 } else if (submitted) {
2096 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08002097 }
2098
Chao Yu458e47f2018-05-23 22:25:09 +08002099 if (--wbc->nr_to_write <= 0 &&
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002100 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08002101 done = 1;
2102 break;
2103 }
2104 }
2105 pagevec_release(&pvec);
2106 cond_resched();
2107 }
2108
Chao Yu8f46dca2015-07-14 18:56:10 +08002109 if (!cycled && !done) {
2110 cycled = 1;
2111 index = 0;
2112 end = writeback_index - 1;
2113 goto retry;
2114 }
2115 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2116 mapping->writeback_index = done_index;
2117
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002118 if (last_idx != ULONG_MAX)
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07002119 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
2120 0, last_idx, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08002121
Chao Yu8f46dca2015-07-14 18:56:10 +08002122 return ret;
2123}
2124
Chao Yufdd569a2018-05-30 00:20:39 +08002125static int __f2fs_write_data_pages(struct address_space *mapping,
Chao Yuc0fe4882017-08-02 23:21:48 +08002126 struct writeback_control *wbc,
2127 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002128{
2129 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002130 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002131 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002132 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002133
P J Pcfb185a2013-04-03 11:38:00 +09002134 /* deal with chardevs and other special file */
2135 if (!mapping->a_ops->writepage)
2136 return 0;
2137
Chao Yu6a290542015-07-17 18:02:39 +08002138 /* skip writing if there is no dirty page in this inode */
2139 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2140 return 0;
2141
Chao Yu00cc7ed2017-06-29 23:20:45 +08002142 /* during POR, we don't need to trigger writepage at all. */
2143 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2144 goto skip_write;
2145
Jaegeuk Kima1257022015-10-08 10:40:07 -07002146 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
2147 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
Chao Yu6b4d6a82018-05-30 00:20:41 +08002148 f2fs_available_free_memory(sbi, DIRTY_DENTS))
Jaegeuk Kima1257022015-10-08 10:40:07 -07002149 goto skip_write;
2150
Chao Yud323d002015-10-27 09:53:45 +08002151 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07002152 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08002153 goto skip_write;
2154
Yunlei Hed31c7c32016-02-04 16:14:00 +08002155 trace_f2fs_writepages(mapping->host, wbc, DATA);
2156
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002157 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2158 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yudeb78d42018-06-04 23:20:36 +08002159 atomic_inc(&sbi->wb_sync_req[DATA]);
2160 else if (atomic_read(&sbi->wb_sync_req[DATA]))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002161 goto skip_write;
2162
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002163 blk_start_plug(&plug);
Chao Yuc0fe4882017-08-02 23:21:48 +08002164 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002165 blk_finish_plug(&plug);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002166
2167 if (wbc->sync_mode == WB_SYNC_ALL)
Chao Yudeb78d42018-06-04 23:20:36 +08002168 atomic_dec(&sbi->wb_sync_req[DATA]);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07002169 /*
2170 * if some pages were truncated, we cannot guarantee its mapping->host
2171 * to detect pending bios.
2172 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002173
Chao Yu6b4d6a82018-05-30 00:20:41 +08002174 f2fs_remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002175 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002176
2177skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002178 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08002179 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002180 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002181}
2182
Chao Yuc0fe4882017-08-02 23:21:48 +08002183static int f2fs_write_data_pages(struct address_space *mapping,
2184 struct writeback_control *wbc)
2185{
2186 struct inode *inode = mapping->host;
2187
2188 return __f2fs_write_data_pages(mapping, wbc,
2189 F2FS_I(inode)->cp_task == current ?
2190 FS_CP_DATA_IO : FS_DATA_IO);
2191}
2192
Chao Yu3aab8f82014-07-02 13:25:04 +08002193static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2194{
2195 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002196 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002197
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002198 if (to > i_size) {
Qiuyang Sun7b23ea12017-05-18 11:06:45 +08002199 down_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu1d8aac32018-08-05 23:04:25 +08002200 down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
2201
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002202 truncate_pagecache(inode, i_size);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002203 f2fs_truncate_blocks(inode, i_size, true);
Chao Yu1d8aac32018-08-05 23:04:25 +08002204
2205 up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
Qiuyang Sun7b23ea12017-05-18 11:06:45 +08002206 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3aab8f82014-07-02 13:25:04 +08002207 }
2208}
2209
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002210static int prepare_write_begin(struct f2fs_sb_info *sbi,
2211 struct page *page, loff_t pos, unsigned len,
2212 block_t *blk_addr, bool *node_changed)
2213{
2214 struct inode *inode = page->mapping->host;
2215 pgoff_t index = page->index;
2216 struct dnode_of_data dn;
2217 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002218 bool locked = false;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002219 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002220 int err = 0;
2221
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002222 /*
2223 * we already allocated all the blocks, so we don't need to get
2224 * the block addresses when there is no need to fill the page.
2225 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002226 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2227 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002228 return 0;
2229
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002230 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002231 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002232 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002233 locked = true;
2234 }
2235restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002236 /* check inline_data */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002237 ipage = f2fs_get_node_page(sbi, inode->i_ino);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002238 if (IS_ERR(ipage)) {
2239 err = PTR_ERR(ipage);
2240 goto unlock_out;
2241 }
2242
2243 set_new_dnode(&dn, inode, ipage, ipage, 0);
2244
2245 if (f2fs_has_inline_data(inode)) {
Chao Yue84f88e2017-07-19 00:19:05 +08002246 if (pos + len <= MAX_INLINE_DATA(inode)) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08002247 f2fs_do_read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002248 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08002249 if (inode->i_nlink)
2250 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002251 } else {
2252 err = f2fs_convert_inline_page(&dn, page);
2253 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002254 goto out;
2255 if (dn.data_blkaddr == NULL_ADDR)
2256 err = f2fs_get_block(&dn, index);
2257 }
2258 } else if (locked) {
2259 err = f2fs_get_block(&dn, index);
2260 } else {
2261 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2262 dn.data_blkaddr = ei.blk + index - ei.fofs;
2263 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002264 /* hole case */
Chao Yu6b4d6a82018-05-30 00:20:41 +08002265 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07002266 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002267 f2fs_put_dnode(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002268 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2269 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002270 locked = true;
2271 goto restart;
2272 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002273 }
2274 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002275
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002276 /* convert_inline_page can make node_changed */
2277 *blk_addr = dn.data_blkaddr;
2278 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002279out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002280 f2fs_put_dnode(&dn);
2281unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002282 if (locked)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002283 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002284 return err;
2285}
2286
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002287static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2288 loff_t pos, unsigned len, unsigned flags,
2289 struct page **pagep, void **fsdata)
2290{
2291 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002292 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002293 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002294 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yu2a6489c2018-01-15 17:16:46 +08002295 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002296 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002297 int err = 0;
2298
Chao Yu62aed042014-05-06 16:46:04 +08002299 trace_f2fs_write_begin(inode, pos, len, flags);
2300
Jaegeuk Kim77d13322018-07-27 18:15:11 +09002301 if ((f2fs_is_atomic_file(inode) &&
2302 !f2fs_available_free_memory(sbi, INMEM_PAGES)) ||
2303 is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) {
Jaegeuk Kim35e13ca2017-10-18 19:05:57 -07002304 err = -ENOMEM;
Chao Yu2a6489c2018-01-15 17:16:46 +08002305 drop_atomic = true;
Jaegeuk Kim35e13ca2017-10-18 19:05:57 -07002306 goto fail;
2307 }
2308
Jaegeuk Kim5f727392014-11-25 10:59:45 -08002309 /*
2310 * We should check this at this moment to avoid deadlock on inode page
2311 * and #0 page. The locking rule for inline_data conversion should be:
2312 * lock_page(page #0) -> lock_page(inode_page)
2313 */
2314 if (index != 0) {
2315 err = f2fs_convert_inline_inode(inode);
2316 if (err)
2317 goto fail;
2318 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002319repeat:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002320 /*
2321 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2322 * wait_for_stable_page. Will wait that below with our IO control.
2323 */
Chao Yu58ddec82017-10-28 16:52:30 +08002324 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002325 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08002326 if (!page) {
2327 err = -ENOMEM;
2328 goto fail;
2329 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09002330
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002331 *pagep = page;
2332
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002333 err = prepare_write_begin(sbi, page, pos, len,
2334 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002335 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002336 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002337
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002338 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002339 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002340 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002341 lock_page(page);
2342 if (page->mapping != mapping) {
2343 /* The page got truncated from under us */
2344 f2fs_put_page(page, 1);
2345 goto repeat;
2346 }
2347 }
2348
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002349 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002350
Eric Biggersf69e8142018-04-18 11:09:48 -07002351 /* wait for GCed page writeback via META_MAPPING */
2352 if (f2fs_post_read_required(inode))
Jaegeuk Kimfb605d02017-09-05 17:04:35 -07002353 f2fs_wait_on_block_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08002354
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002355 if (len == PAGE_SIZE || PageUptodate(page))
2356 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002357
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002358 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2359 zero_user_segment(page, len, PAGE_SIZE);
2360 return 0;
2361 }
2362
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002363 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002364 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002365 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002366 } else {
Jaegeuk Kim5a690672017-09-06 21:04:44 -07002367 err = f2fs_submit_page_read(inode, page, blkaddr);
2368 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08002369 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08002370
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002371 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002372 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002373 f2fs_put_page(page, 1);
2374 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002375 }
Chao Yu1563ac72016-07-03 22:05:12 +08002376 if (unlikely(!PageUptodate(page))) {
2377 err = -EIO;
2378 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002379 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002380 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002381 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002382
Chao Yu3aab8f82014-07-02 13:25:04 +08002383fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002384 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002385 f2fs_write_failed(mapping, pos + len);
Chao Yu2a6489c2018-01-15 17:16:46 +08002386 if (drop_atomic)
Chao Yu6b4d6a82018-05-30 00:20:41 +08002387 f2fs_drop_inmem_pages_all(sbi, false);
Chao Yu3aab8f82014-07-02 13:25:04 +08002388 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002389}
2390
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002391static int f2fs_write_end(struct file *file,
2392 struct address_space *mapping,
2393 loff_t pos, unsigned len, unsigned copied,
2394 struct page *page, void *fsdata)
2395{
2396 struct inode *inode = page->mapping->host;
2397
Chao Yudfb2bf32014-05-06 16:47:23 +08002398 trace_f2fs_write_end(inode, pos, len, copied);
2399
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002400 /*
2401 * This should be come from len == PAGE_SIZE, and we expect copied
2402 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2403 * let generic_perform_write() try to copy data again through copied=0.
2404 */
2405 if (!PageUptodate(page)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002406 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002407 copied = 0;
2408 else
2409 SetPageUptodate(page);
2410 }
2411 if (!copied)
2412 goto unlock_out;
2413
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002414 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002415
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002416 if (pos + copied > i_size_read(inode))
2417 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002418unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002419 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002420 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002421 return copied;
2422}
2423
Omar Sandoval6f673762015-03-16 04:33:52 -07002424static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2425 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002426{
Jaegeuk Kim28475252018-06-29 18:55:12 -07002427 unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
2428 unsigned blkbits = i_blkbits;
2429 unsigned blocksize_mask = (1 << blkbits) - 1;
2430 unsigned long align = offset | iov_iter_alignment(iter);
2431 struct block_device *bdev = inode->i_sb->s_bdev;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002432
Jaegeuk Kim28475252018-06-29 18:55:12 -07002433 if (align & blocksize_mask) {
2434 if (bdev)
2435 blkbits = blksize_bits(bdev_logical_block_size(bdev));
2436 blocksize_mask = (1 << blkbits) - 1;
2437 if (align & blocksize_mask)
2438 return -EINVAL;
2439 return 1;
2440 }
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002441 return 0;
2442}
2443
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002444static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002445{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002446 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002447 struct inode *inode = mapping->host;
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002448 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002449 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002450 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002451 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002452 int err;
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002453 enum rw_hint hint = iocb->ki_hint;
Chao Yue9a50e62018-03-08 14:22:56 +08002454 int whint_mode = F2FS_OPTION(sbi).whint_mode;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002455
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002456 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002457 if (err)
Jaegeuk Kim28475252018-06-29 18:55:12 -07002458 return err < 0 ? err : 0;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002459
Hyunchul Lee355d2342018-03-08 19:34:38 +09002460 if (f2fs_force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002461 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002462
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002463 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002464
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002465 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2466 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2467
Chao Yuf6bd7d42018-04-24 10:55:28 +08002468 if (!down_read_trylock(&F2FS_I(inode)->i_gc_rwsem[rw])) {
Hyunchul Lee355d2342018-03-08 19:34:38 +09002469 if (iocb->ki_flags & IOCB_NOWAIT) {
2470 iocb->ki_hint = hint;
2471 err = -EAGAIN;
2472 goto out;
2473 }
Chao Yuf6bd7d42018-04-24 10:55:28 +08002474 down_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
Hyunchul Lee355d2342018-03-08 19:34:38 +09002475 }
2476
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002477 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yuf6bd7d42018-04-24 10:55:28 +08002478 up_read(&F2FS_I(inode)->i_gc_rwsem[rw]);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002479
2480 if (rw == WRITE) {
Hyunchul Leebcdc5712018-01-31 11:36:57 +09002481 if (whint_mode == WHINT_MODE_OFF)
2482 iocb->ki_hint = hint;
Chao Yuc0fe4882017-08-02 23:21:48 +08002483 if (err > 0) {
2484 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2485 err);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002486 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yuc0fe4882017-08-02 23:21:48 +08002487 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002488 f2fs_write_failed(mapping, offset + count);
Chao Yuc0fe4882017-08-02 23:21:48 +08002489 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002490 }
Chao Yu70407fa2014-07-31 21:11:22 +08002491
Hyunchul Lee355d2342018-03-08 19:34:38 +09002492out:
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002493 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002494
Chao Yu3aab8f82014-07-02 13:25:04 +08002495 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002496}
2497
Chao Yu487261f2015-02-05 17:44:29 +08002498void f2fs_invalidate_page(struct page *page, unsigned int offset,
2499 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002500{
2501 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002502 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002503
Chao Yu487261f2015-02-05 17:44:29 +08002504 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002505 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002506 return;
2507
Chao Yu487261f2015-02-05 17:44:29 +08002508 if (PageDirty(page)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002509 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002510 dec_page_count(sbi, F2FS_DIRTY_META);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002511 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002512 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002513 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002514 inode_dec_dirty_pages(inode);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002515 f2fs_remove_dirty_inode(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002516 }
Chao Yu487261f2015-02-05 17:44:29 +08002517 }
Chao Yudecd36b2015-08-07 18:42:09 +08002518
2519 /* This is atomic written page, keep Private */
2520 if (IS_ATOMIC_WRITTEN_PAGE(page))
Chao Yu6b4d6a82018-05-30 00:20:41 +08002521 return f2fs_drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002522
Chao Yu23dc9742016-04-29 20:09:15 +08002523 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002524 ClearPagePrivate(page);
2525}
2526
Chao Yu487261f2015-02-05 17:44:29 +08002527int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002528{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002529 /* If this is dirty page, keep PagePrivate */
2530 if (PageDirty(page))
2531 return 0;
2532
Chao Yudecd36b2015-08-07 18:42:09 +08002533 /* This is atomic written page, keep Private */
2534 if (IS_ATOMIC_WRITTEN_PAGE(page))
2535 return 0;
2536
Chao Yu23dc9742016-04-29 20:09:15 +08002537 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002538 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002539 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002540}
2541
2542static int f2fs_set_data_page_dirty(struct page *page)
2543{
2544 struct address_space *mapping = page->mapping;
2545 struct inode *inode = mapping->host;
2546
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002547 trace_f2fs_set_page_dirty(page, DATA);
2548
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002549 if (!PageUptodate(page))
2550 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002551
Chao Yu1069e812018-07-29 12:16:59 +08002552 /* don't remain PG_checked flag which was set during GC */
2553 if (is_cold_data(page))
2554 clear_cold_data(page);
2555
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002556 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002557 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
Chao Yu6b4d6a82018-05-30 00:20:41 +08002558 f2fs_register_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002559 return 1;
2560 }
2561 /*
2562 * Previously, this page has been registered, we just
2563 * return here.
2564 */
2565 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002566 }
2567
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002568 if (!PageDirty(page)) {
Jaegeuk Kim95459e92018-04-20 19:29:52 -07002569 __set_page_dirty_nobuffers(page);
Chao Yu6b4d6a82018-05-30 00:20:41 +08002570 f2fs_update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002571 return 1;
2572 }
2573 return 0;
2574}
2575
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002576static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2577{
Chao Yu454ae7e2014-04-22 13:34:01 +08002578 struct inode *inode = mapping->host;
2579
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002580 if (f2fs_has_inline_data(inode))
2581 return 0;
2582
2583 /* make sure allocating whole blocks */
2584 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2585 filemap_write_and_wait(mapping);
2586
Chao Yue2b4e2b2015-08-19 19:11:19 +08002587 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002588}
2589
Weichao Guo5b7a4872016-09-20 05:03:27 +08002590#ifdef CONFIG_MIGRATION
2591#include <linux/migrate.h>
2592
2593int f2fs_migrate_page(struct address_space *mapping,
2594 struct page *newpage, struct page *page, enum migrate_mode mode)
2595{
2596 int rc, extra_count;
2597 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2598 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2599
2600 BUG_ON(PageWriteback(page));
2601
2602 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kim025258e2017-07-06 14:46:01 -07002603 if (atomic_written) {
2604 if (mode != MIGRATE_SYNC)
2605 return -EBUSY;
2606 if (!mutex_trylock(&fi->inmem_lock))
2607 return -EAGAIN;
2608 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002609
2610 /*
2611 * A reference is expected if PagePrivate set when move mapping,
2612 * however F2FS breaks this for maintaining dirty page counts when
2613 * truncating pages. So here adjusting the 'extra_count' make it work.
2614 */
2615 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2616 rc = migrate_page_move_mapping(mapping, newpage,
2617 page, NULL, mode, extra_count);
2618 if (rc != MIGRATEPAGE_SUCCESS) {
2619 if (atomic_written)
2620 mutex_unlock(&fi->inmem_lock);
2621 return rc;
2622 }
2623
2624 if (atomic_written) {
2625 struct inmem_pages *cur;
2626 list_for_each_entry(cur, &fi->inmem_pages, list)
2627 if (cur->page == page) {
2628 cur->page = newpage;
2629 break;
2630 }
2631 mutex_unlock(&fi->inmem_lock);
2632 put_page(page);
2633 get_page(newpage);
2634 }
2635
2636 if (PagePrivate(page))
2637 SetPagePrivate(newpage);
2638 set_page_private(newpage, page_private(page));
2639
2640 migrate_page_copy(newpage, page);
2641
2642 return MIGRATEPAGE_SUCCESS;
2643}
2644#endif
2645
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002646const struct address_space_operations f2fs_dblock_aops = {
2647 .readpage = f2fs_read_data_page,
2648 .readpages = f2fs_read_data_pages,
2649 .writepage = f2fs_write_data_page,
2650 .writepages = f2fs_write_data_pages,
2651 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002652 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002653 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002654 .invalidatepage = f2fs_invalidate_page,
2655 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002656 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002657 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002658#ifdef CONFIG_MIGRATION
2659 .migratepage = f2fs_migrate_page,
2660#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002661};
Eric Biggersf69e8142018-04-18 11:09:48 -07002662
Chao Yu6b4d6a82018-05-30 00:20:41 +08002663void f2fs_clear_radix_tree_dirty_tag(struct page *page)
Chao Yubcbcda42018-05-26 18:03:35 +08002664{
2665 struct address_space *mapping = page_mapping(page);
2666 unsigned long flags;
2667
2668 spin_lock_irqsave(&mapping->tree_lock, flags);
2669 radix_tree_tag_clear(&mapping->page_tree, page_index(page),
2670 PAGECACHE_TAG_DIRTY);
2671 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2672}
2673
Eric Biggersf69e8142018-04-18 11:09:48 -07002674int __init f2fs_init_post_read_processing(void)
2675{
2676 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, 0);
2677 if (!bio_post_read_ctx_cache)
2678 goto fail;
2679 bio_post_read_ctx_pool =
2680 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
2681 bio_post_read_ctx_cache);
2682 if (!bio_post_read_ctx_pool)
2683 goto fail_free_cache;
2684 return 0;
2685
2686fail_free_cache:
2687 kmem_cache_destroy(bio_post_read_ctx_cache);
2688fail:
2689 return -ENOMEM;
2690}
2691
2692void __exit f2fs_destroy_post_read_processing(void)
2693{
2694 mempool_destroy(bio_post_read_ctx_pool);
2695 kmem_cache_destroy(bio_post_read_ctx_cache);
2696}