blob: 28a3328f161700831680addc267a4ad18dedf6d8 [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 Kimfe76b792016-06-30 18:40:10 -070022#include <linux/mm.h>
23#include <linux/memcontrol.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070024#include <linux/cleancache.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090026
27#include "f2fs.h"
28#include "node.h"
29#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080030#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090031#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090032
Chao Yu36951b32016-11-16 10:41:20 +080033static bool __is_cp_guaranteed(struct page *page)
34{
35 struct address_space *mapping = page->mapping;
36 struct inode *inode;
37 struct f2fs_sb_info *sbi;
38
39 if (!mapping)
40 return false;
41
42 inode = mapping->host;
43 sbi = F2FS_I_SB(inode);
44
45 if (inode->i_ino == F2FS_META_INO(sbi) ||
46 inode->i_ino == F2FS_NODE_INO(sbi) ||
47 S_ISDIR(inode->i_mode) ||
48 is_cold_data(page))
49 return true;
50 return false;
51}
52
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020053static void f2fs_read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090054{
Linus Torvaldsf5688492014-01-30 11:19:05 -080055 struct bio_vec *bvec;
56 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090057
Chao Yu8b038c72016-09-18 23:30:07 +080058#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu55523512017-02-25 11:08:28 +080059 if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_IO)) {
60 f2fs_show_injection_info(FAULT_IO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020061 bio->bi_status = BLK_STS_IOERR;
Chao Yu55523512017-02-25 11:08:28 +080062 }
Chao Yu8b038c72016-09-18 23:30:07 +080063#endif
64
Chao Yu12377022015-05-25 18:03:38 +080065 if (f2fs_bio_encrypted(bio)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020066 if (bio->bi_status) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070067 fscrypt_release_ctx(bio->bi_private);
Chao Yu12377022015-05-25 18:03:38 +080068 } else {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070069 fscrypt_decrypt_bio_pages(bio->bi_private, bio);
Chao Yu12377022015-05-25 18:03:38 +080070 return;
71 }
72 }
73
Linus Torvaldsf5688492014-01-30 11:19:05 -080074 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090075 struct page *page = bvec->bv_page;
76
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020077 if (!bio->bi_status) {
Jaegeuk Kim237c0792016-06-30 18:49:15 -070078 if (!PageUptodate(page))
79 SetPageUptodate(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080080 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090081 ClearPageUptodate(page);
82 SetPageError(page);
83 }
84 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080085 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090086 bio_put(bio);
87}
88
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020089static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090090{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090091 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080092 struct bio_vec *bvec;
93 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090094
Linus Torvaldsf5688492014-01-30 11:19:05 -080095 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090096 struct page *page = bvec->bv_page;
Chao Yu36951b32016-11-16 10:41:20 +080097 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090098
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -080099 if (IS_DUMMY_WRITTEN_PAGE(page)) {
100 set_page_private(page, (unsigned long)NULL);
101 ClearPagePrivate(page);
102 unlock_page(page);
103 mempool_free(page, sbi->write_io_dummy);
104
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200105 if (unlikely(bio->bi_status))
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800106 f2fs_stop_checkpoint(sbi, true);
107 continue;
108 }
109
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700110 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700111
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200112 if (unlikely(bio->bi_status)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700113 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kimb1ca3212017-12-31 16:26:38 -0800114 if (type == F2FS_WB_CP_DATA)
115 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900116 }
Yunlei He7dff55d2018-01-11 14:19:32 +0800117
118 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
119 page->index != nid_of_node(page));
120
Chao Yu36951b32016-11-16 10:41:20 +0800121 dec_page_count(sbi, type);
122 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900123 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800124 }
Chao Yu36951b32016-11-16 10:41:20 +0800125 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700126 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900127 wake_up(&sbi->cp_wait);
128
129 bio_put(bio);
130}
131
Gu Zheng940a6d32013-12-20 17:39:59 +0800132/*
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700133 * Return true, if pre_bio's bdev is same as its target device.
134 */
135struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
136 block_t blk_addr, struct bio *bio)
137{
138 struct block_device *bdev = sbi->sb->s_bdev;
139 int i;
140
141 for (i = 0; i < sbi->s_ndevs; i++) {
142 if (FDEV(i).start_blk <= blk_addr &&
143 FDEV(i).end_blk >= blk_addr) {
144 blk_addr -= FDEV(i).start_blk;
145 bdev = FDEV(i).bdev;
146 break;
147 }
148 }
149 if (bio) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200150 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700151 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
152 }
153 return bdev;
154}
155
156int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
157{
158 int i;
159
160 for (i = 0; i < sbi->s_ndevs; i++)
161 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
162 return i;
163 return 0;
164}
165
166static bool __same_bdev(struct f2fs_sb_info *sbi,
167 block_t blk_addr, struct bio *bio)
168{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200169 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
170 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700171}
172
173/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800174 * Low-level block read/write IO operations.
175 */
176static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
Yufen Yu578c6472018-01-09 19:33:39 +0800177 struct writeback_control *wbc,
Gu Zheng940a6d32013-12-20 17:39:59 +0800178 int npages, bool is_read)
179{
180 struct bio *bio;
181
Chao Yud62fe972017-10-28 16:52:31 +0800182 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800183
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700184 f2fs_target_device(sbi, blk_addr, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800185 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +0800186 bio->bi_private = is_read ? NULL : sbi;
Yufen Yu578c6472018-01-09 19:33:39 +0800187 if (wbc)
188 wbc_init_bio(wbc, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800189
190 return bio;
191}
192
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700193static inline void __submit_bio(struct f2fs_sb_info *sbi,
194 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700195{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700196 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800197 unsigned int start;
198
Damien Le Moal0bfd7a02016-10-28 17:45:00 +0900199 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700200 current->plug && (type == DATA || type == NODE))
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700201 blk_finish_plug(current->plug);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800202
203 if (type != DATA && type != NODE)
204 goto submit_io;
205
206 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
207 start %= F2FS_IO_SIZE(sbi);
208
209 if (start == 0)
210 goto submit_io;
211
212 /* fill dummy pages */
213 for (; start < F2FS_IO_SIZE(sbi); start++) {
214 struct page *page =
215 mempool_alloc(sbi->write_io_dummy,
216 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
217 f2fs_bug_on(sbi, !page);
218
219 SetPagePrivate(page);
220 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
221 lock_page(page);
222 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
223 f2fs_bug_on(sbi, 1);
224 }
225 /*
226 * In the NODE case, we lose next block address chain. So, we
227 * need to do checkpoint in f2fs_sync_file.
228 */
229 if (type == NODE)
230 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700231 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800232submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800233 if (is_read_io(bio_op(bio)))
234 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
235 else
236 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500237 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700238}
239
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900240static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900241{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900242 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900243
244 if (!io->bio)
245 return;
246
Mike Christie04d328d2016-06-05 14:31:55 -0500247 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
248
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800249 if (is_read_io(fio->op))
250 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
251 else
252 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
253
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700254 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900255 io->bio = NULL;
256}
257
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800258static bool __has_merged_page(struct f2fs_bio_info *io,
259 struct inode *inode, nid_t ino, pgoff_t idx)
Chao Yu0fd785e2016-01-18 18:24:59 +0800260{
Chao Yu0fd785e2016-01-18 18:24:59 +0800261 struct bio_vec *bvec;
262 struct page *target;
263 int i;
264
Chao Yu0c3a5792016-01-18 18:28:11 +0800265 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800266 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800267
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800268 if (!inode && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800269 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800270
271 bio_for_each_segment_all(bvec, io->bio, i) {
272
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700273 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800274 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700275 else
276 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800277
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800278 if (idx != target->index)
279 continue;
280
Chao Yu0c3a5792016-01-18 18:28:11 +0800281 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800282 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800283 if (ino && ino == ino_of_node(target))
284 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800285 }
286
Chao Yu0fd785e2016-01-18 18:24:59 +0800287 return false;
288}
289
Chao Yu0c3a5792016-01-18 18:28:11 +0800290static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800291 nid_t ino, pgoff_t idx, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800292{
293 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700294 enum temp_type temp;
295 struct f2fs_bio_info *io;
296 bool ret = false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800297
Jaegeuk Kima912b542017-05-10 11:18:25 -0700298 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
299 io = sbi->write_io[btype] + temp;
300
301 down_read(&io->io_rwsem);
302 ret = __has_merged_page(io, inode, ino, idx);
303 up_read(&io->io_rwsem);
304
305 /* TODO: use HOT temp only for meta pages now. */
306 if (ret || btype == META)
307 break;
308 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800309 return ret;
310}
311
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700312static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700313 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900314{
315 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700316 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900317
Chao Yudf0f8dc2014-03-22 14:57:23 +0800318 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900319
320 /* change META to META_FLUSH in the checkpoint procedure */
321 if (type >= META_FLUSH) {
322 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500323 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200324 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600325 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800326 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900327 }
328 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800329 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900330}
331
Jaegeuk Kima912b542017-05-10 11:18:25 -0700332static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800333 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700334 enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800335{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700336 enum temp_type temp;
337
338 if (!force && !has_merged_page(sbi, inode, ino, idx, type))
339 return;
340
341 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
342
343 __f2fs_submit_merged_write(sbi, type, temp);
344
345 /* TODO: use HOT temp only for meta pages now. */
346 if (type >= META)
347 break;
348 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800349}
350
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700351void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800352{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700353 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900354}
355
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700356void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900357 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700358 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900359{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700360 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900361}
362
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700363void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800364{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700365 f2fs_submit_merged_write(sbi, DATA);
366 f2fs_submit_merged_write(sbi, NODE);
367 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800368}
369
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900370/*
371 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300372 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900373 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700374int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900375{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900376 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700377 struct page *page = fio->encrypted_page ?
378 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900379
Chao Yu2ace38e2014-12-24 16:08:14 +0800380 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700381 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900382
383 /* Allocate a new bio */
Yufen Yu578c6472018-01-09 19:33:39 +0800384 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
385 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900386
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300387 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900388 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900389 return -EFAULT;
390 }
Mike Christie04d328d2016-06-05 14:31:55 -0500391 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900392
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700393 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -0700394
395 if (!is_read_io(fio->op))
396 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900397 return 0;
398}
399
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700400int f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900401{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700402 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900403 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700404 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700405 struct page *bio_page;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800406 int err = 0;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900407
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700408 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900409
Chao Yufb830fc2017-05-19 23:37:01 +0800410 down_write(&io->io_rwsem);
411next:
412 if (fio->in_list) {
413 spin_lock(&io->io_lock);
414 if (list_empty(&io->io_list)) {
415 spin_unlock(&io->io_lock);
416 goto out_fail;
417 }
418 fio = list_first_entry(&io->io_list,
419 struct f2fs_io_info, list);
420 list_del(&fio->list);
421 spin_unlock(&io->io_lock);
422 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900423
Chao Yu7a9d7542016-02-22 18:36:38 +0800424 if (fio->old_blkaddr != NEW_ADDR)
425 verify_block_addr(sbi, fio->old_blkaddr);
426 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900427
Chao Yu36951b32016-11-16 10:41:20 +0800428 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
429
Thomas Meyerebf7c522017-10-07 16:02:21 +0200430 /* set submitted = true as a return value */
431 fio->submitted = true;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800432
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700433 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900434
Chao Yu7a9d7542016-02-22 18:36:38 +0800435 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700436 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
437 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900438 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900439alloc_new:
440 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800441 if ((fio->type == DATA || fio->type == NODE) &&
442 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
443 err = -EAGAIN;
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700444 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800445 goto out_fail;
446 }
Yufen Yu578c6472018-01-09 19:33:39 +0800447 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700448 BIO_MAX_PAGES, false);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900449 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900450 }
451
Jaegeuk Kima912b542017-05-10 11:18:25 -0700452 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900453 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900454 goto alloc_new;
455 }
456
Yufen Yu578c6472018-01-09 19:33:39 +0800457 if (fio->io_wbc)
458 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
459
Chao Yu7a9d7542016-02-22 18:36:38 +0800460 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700461 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800462
463 trace_f2fs_submit_page_write(fio->page, fio);
464
465 if (fio->in_list)
466 goto next;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800467out_fail:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800468 up_write(&io->io_rwsem);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800469 return err;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900470}
471
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700472static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
473 unsigned nr_pages)
474{
475 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
476 struct fscrypt_ctx *ctx = NULL;
477 struct bio *bio;
478
479 if (f2fs_encrypted_file(inode)) {
480 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
481 if (IS_ERR(ctx))
482 return ERR_CAST(ctx);
483
484 /* wait the page to be moved by cleaning */
485 f2fs_wait_on_block_writeback(sbi, blkaddr);
486 }
487
Chao Yud62fe972017-10-28 16:52:31 +0800488 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700489 if (!bio) {
490 if (ctx)
491 fscrypt_release_ctx(ctx);
492 return ERR_PTR(-ENOMEM);
493 }
494 f2fs_target_device(sbi, blkaddr, bio);
495 bio->bi_end_io = f2fs_read_end_io;
496 bio->bi_private = ctx;
497 bio_set_op_attrs(bio, REQ_OP_READ, 0);
498
499 return bio;
500}
501
502/* This can handle encryption stuffs */
503static int f2fs_submit_page_read(struct inode *inode, struct page *page,
504 block_t blkaddr)
505{
506 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1);
507
508 if (IS_ERR(bio))
509 return PTR_ERR(bio);
510
511 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
512 bio_put(bio);
513 return -EFAULT;
514 }
515 __submit_bio(F2FS_I_SB(inode), bio, DATA);
516 return 0;
517}
518
Chao Yu46008c62016-05-09 19:56:30 +0800519static void __set_data_blkaddr(struct dnode_of_data *dn)
520{
521 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
522 __le32 *addr_array;
Chao Yu7a2af762017-07-19 00:19:06 +0800523 int base = 0;
524
525 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
526 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800527
528 /* Get physical address of data block */
529 addr_array = blkaddr_in_node(rn);
Chao Yu7a2af762017-07-19 00:19:06 +0800530 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800531}
532
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900533/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900534 * Lock ordering for the change of data block address:
535 * ->data_page
536 * ->node_page
537 * update block addresses in the node page
538 */
Chao Yu216a6202015-03-19 19:23:32 +0800539void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900540{
Chao Yu46008c62016-05-09 19:56:30 +0800541 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
542 __set_data_blkaddr(dn);
543 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800544 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900545}
546
Chao Yuf28b3432016-02-24 17:16:47 +0800547void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
548{
549 dn->data_blkaddr = blkaddr;
550 set_data_blkaddr(dn);
551 f2fs_update_extent_cache(dn);
552}
553
Chao Yu46008c62016-05-09 19:56:30 +0800554/* dn->ofs_in_node will be returned with up-to-date last block pointer */
555int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900556{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700557 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800558 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900559
Chao Yu46008c62016-05-09 19:56:30 +0800560 if (!count)
561 return 0;
562
Jaegeuk Kim91942322016-05-20 10:13:22 -0700563 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900564 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +0800565 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
566 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900567
Chao Yu46008c62016-05-09 19:56:30 +0800568 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
569 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900570
Chao Yu46008c62016-05-09 19:56:30 +0800571 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
572
573 for (; count > 0; dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800574 block_t blkaddr = datablock_addr(dn->inode,
575 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800576 if (blkaddr == NULL_ADDR) {
577 dn->data_blkaddr = NEW_ADDR;
578 __set_data_blkaddr(dn);
579 count--;
580 }
581 }
582
583 if (set_page_dirty(dn->node_page))
584 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900585 return 0;
586}
587
Chao Yu46008c62016-05-09 19:56:30 +0800588/* Should keep dn->ofs_in_node unchanged */
589int reserve_new_block(struct dnode_of_data *dn)
590{
591 unsigned int ofs_in_node = dn->ofs_in_node;
592 int ret;
593
594 ret = reserve_new_blocks(dn, 1);
595 dn->ofs_in_node = ofs_in_node;
596 return ret;
597}
598
Huajun Lib6009652013-11-10 23:13:18 +0800599int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
600{
601 bool need_put = dn->inode_page ? false : true;
602 int err;
603
604 err = get_dnode_of_data(dn, index, ALLOC_NODE);
605 if (err)
606 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900607
Huajun Lib6009652013-11-10 23:13:18 +0800608 if (dn->data_blkaddr == NULL_ADDR)
609 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900610 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800611 f2fs_put_dnode(dn);
612 return err;
613}
614
Fan Li759af1c2015-08-05 15:52:16 +0800615int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900616{
Hou Pengyange15882b2017-02-23 09:18:05 +0000617 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800618 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800619
Fan Li759af1c2015-08-05 15:52:16 +0800620 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
621 dn->data_blkaddr = ei.blk + index - ei.fofs;
622 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800623 }
624
Fan Li759af1c2015-08-05 15:52:16 +0800625 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900626}
627
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700628struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500629 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900630{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900631 struct address_space *mapping = inode->i_mapping;
632 struct dnode_of_data dn;
633 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +0000634 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900635 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700636
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700637 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900638 if (!page)
639 return ERR_PTR(-ENOMEM);
640
Chao Yucb3bc9e2015-02-05 18:03:40 +0800641 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
642 dn.data_blkaddr = ei.blk + index - ei.fofs;
643 goto got_it;
644 }
645
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900646 set_new_dnode(&dn, inode, NULL, NULL, 0);
647 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700648 if (err)
649 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900650 f2fs_put_dnode(&dn);
651
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900652 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700653 err = -ENOENT;
654 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900655 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800656got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700657 if (PageUptodate(page)) {
658 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900659 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700660 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900661
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900662 /*
663 * A new dentry page is allocated but not able to be written, since its
664 * new inode page couldn't be allocated due to -ENOSPC.
665 * In such the case, its blkaddr can be remained as NEW_ADDR.
666 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
667 */
668 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300669 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700670 if (!PageUptodate(page))
671 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700672 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900673 return page;
674 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900675
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700676 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900677 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700678 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700679 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700680
681put_err:
682 f2fs_put_page(page, 1);
683 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700684}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900685
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700686struct page *find_data_page(struct inode *inode, pgoff_t index)
687{
688 struct address_space *mapping = inode->i_mapping;
689 struct page *page;
690
691 page = find_get_page(mapping, index);
692 if (page && PageUptodate(page))
693 return page;
694 f2fs_put_page(page, 0);
695
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600696 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700697 if (IS_ERR(page))
698 return page;
699
700 if (PageUptodate(page))
701 return page;
702
703 wait_on_page_locked(page);
704 if (unlikely(!PageUptodate(page))) {
705 f2fs_put_page(page, 0);
706 return ERR_PTR(-EIO);
707 }
708 return page;
709}
710
711/*
712 * If it tries to access a hole, return an error.
713 * Because, the callers, functions in dir.c and GC, should be able to know
714 * whether this page exists or not.
715 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700716struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
717 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700718{
719 struct address_space *mapping = inode->i_mapping;
720 struct page *page;
721repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600722 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700723 if (IS_ERR(page))
724 return page;
725
726 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900727 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900728 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900729 f2fs_put_page(page, 1);
730 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900731 }
Chao Yu1563ac72016-07-03 22:05:12 +0800732 if (unlikely(!PageUptodate(page))) {
733 f2fs_put_page(page, 1);
734 return ERR_PTR(-EIO);
735 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900736 return page;
737}
738
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900739/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900740 * Caller ensures that this data page is never allocated.
741 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900742 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800743 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
744 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800745 * Note that, ipage is set only by make_empty_dir, and if any error occur,
746 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900747 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900748struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900749 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900750{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900751 struct address_space *mapping = inode->i_mapping;
752 struct page *page;
753 struct dnode_of_data dn;
754 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800755
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700756 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800757 if (!page) {
758 /*
759 * before exiting, we should make sure ipage will be released
760 * if any error occur.
761 */
762 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700763 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800764 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900765
Jaegeuk Kima8865372013-12-27 17:04:17 +0900766 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800767 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700768 if (err) {
769 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900770 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900771 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700772 if (!ipage)
773 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900774
775 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700776 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900777
778 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300779 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700780 if (!PageUptodate(page))
781 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900782 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700783 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900784
Jaegeuk Kim76121182016-01-01 22:03:47 -0800785 /* if ipage exists, blkaddr should be NEW_ADDR */
786 f2fs_bug_on(F2FS_I_SB(inode), ipage);
787 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700788 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800789 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900790 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700791got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800792 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700793 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700794 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900795 return page;
796}
797
Hyunchul Leed5097be2017-11-28 09:23:00 +0900798static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900799{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700800 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900801 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900802 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700803 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800804 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +0800805 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900806
Jaegeuk Kim91942322016-05-20 10:13:22 -0700807 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900808 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800809
Chao Yu7a2af762017-07-19 00:19:06 +0800810 dn->data_blkaddr = datablock_addr(dn->inode,
811 dn->node_page, dn->ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800812 if (dn->data_blkaddr == NEW_ADDR)
813 goto alloc;
814
Chao Yu0abd6752017-07-09 00:13:07 +0800815 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
816 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900817
Chao Yudf6136e2015-03-23 10:33:37 +0800818alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900819 get_node_info(sbi, dn->nid, &ni);
820 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
821
Chao Yudf6136e2015-03-23 10:33:37 +0800822 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Hyunchul Leed5097be2017-11-28 09:23:00 +0900823 &sum, seg_type, NULL, false);
Chao Yu216a6202015-03-19 19:23:32 +0800824 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900825
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700826 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800827 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700828 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300829 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700830 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300831 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900832 return 0;
833}
834
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800835static inline bool __force_buffered_io(struct inode *inode, int rw)
836{
Jaegeuk Kim19585932017-09-05 16:54:24 -0700837 return (f2fs_encrypted_file(inode) ||
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800838 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
839 F2FS_I_SB(inode)->s_ndevs);
840}
841
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800842int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800843{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800844 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800845 struct f2fs_map_blocks map;
Chao Yud6d478a12018-01-03 17:30:19 +0800846 int flag;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800847 int err = 0;
Chao Yud6d478a12018-01-03 17:30:19 +0800848 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800849
Weichao Guo71ad6822017-09-29 22:43:23 +0800850 /* convert inline data for Direct I/O*/
Chao Yud6d478a12018-01-03 17:30:19 +0800851 if (direct_io) {
Weichao Guo71ad6822017-09-29 22:43:23 +0800852 err = f2fs_convert_inline_inode(inode);
853 if (err)
854 return err;
855 }
856
Jaegeuk Kimdc91de72017-01-13 13:12:29 -0800857 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
858 return 0;
859
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700860 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800861 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
862 if (map.m_len > map.m_lblk)
863 map.m_len -= map.m_lblk;
864 else
865 map.m_len = 0;
866
Chao Yuda859852016-01-26 15:42:58 +0800867 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +0800868 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +0900869 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800870
Chao Yud6d478a12018-01-03 17:30:19 +0800871 if (direct_io) {
Hyunchul Leed5097be2017-11-28 09:23:00 +0900872 map.m_seg_type = rw_hint_to_seg_type(iocb->ki_hint);
Chao Yud6d478a12018-01-03 17:30:19 +0800873 flag = __force_buffered_io(inode, WRITE) ?
874 F2FS_GET_BLOCK_PRE_AIO :
875 F2FS_GET_BLOCK_PRE_DIO;
876 goto map_blocks;
Hyunchul Leed5097be2017-11-28 09:23:00 +0900877 }
Chao Yuf2470372017-07-19 00:19:05 +0800878 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800879 err = f2fs_convert_inline_inode(inode);
880 if (err)
881 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800882 }
Chao Yud6d478a12018-01-03 17:30:19 +0800883 if (f2fs_has_inline_data(inode))
Sheng Yong250066452017-11-22 18:23:39 +0800884 return err;
Chao Yud6d478a12018-01-03 17:30:19 +0800885
886 flag = F2FS_GET_BLOCK_PRE_AIO;
887
888map_blocks:
889 err = f2fs_map_blocks(inode, &map, 1, flag);
890 if (map.m_len > 0 && err == -ENOSPC) {
891 if (!direct_io)
892 set_inode_flag(inode, FI_NO_PREALLOC);
893 err = 0;
Sheng Yong250066452017-11-22 18:23:39 +0800894 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800895 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800896}
897
Yunlei He59c90812017-03-13 20:22:18 +0800898static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
899{
900 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
901 if (lock)
902 down_read(&sbi->node_change);
903 else
904 up_read(&sbi->node_change);
905 } else {
906 if (lock)
907 f2fs_lock_op(sbi);
908 else
909 f2fs_unlock_op(sbi);
910 }
911}
912
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900913/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700914 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
915 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800916 * If original data blocks are allocated, then give them to blockdev.
917 * Otherwise,
918 * a. preallocate requested block addresses
919 * b. do not use extent cache for better performance
920 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900921 */
Chao Yud323d002015-10-27 09:53:45 +0800922int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800923 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900924{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700925 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900926 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800927 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700928 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800929 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900930 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800931 unsigned int ofs_in_node, last_ofs_in_node;
932 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +0000933 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +0800934 block_t blkaddr;
Chao Yuc4020b22018-01-11 14:42:30 +0800935 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900936
Chao Yudfd02e42016-08-20 15:12:01 +0800937 if (!maxblocks)
938 return 0;
939
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700940 map->m_len = 0;
941 map->m_flags = 0;
942
943 /* it only supports block size == page size */
944 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800945 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900946
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800947 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700948 map->m_pblk = ei.blk + pgofs - ei.fofs;
949 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
950 map->m_flags = F2FS_MAP_MAPPED;
Chao Yuc4020b22018-01-11 14:42:30 +0800951 if (map->m_next_extent)
952 *map->m_next_extent = pgofs + map->m_len;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900953 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800954 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900955
Chao Yu4fe71e82016-01-26 15:37:38 +0800956next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800957 if (create)
Yunlei He59c90812017-03-13 20:22:18 +0800958 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900959
960 /* When reading holes, we need its node page */
961 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900962 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900963 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800964 if (flag == F2FS_GET_BLOCK_BMAP)
965 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800966 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900967 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800968 if (map->m_next_pgofs)
969 *map->m_next_pgofs =
970 get_next_page_offset(&dn, pgofs);
Chao Yuc4020b22018-01-11 14:42:30 +0800971 if (map->m_next_extent)
972 *map->m_next_extent =
973 get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +0800974 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900975 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900976 }
Chao Yu973163f2015-09-18 16:51:51 +0800977
Chao Yuc4020b22018-01-11 14:42:30 +0800978 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +0800979 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +0100980 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800981 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900982
Chao Yu4fe71e82016-01-26 15:37:38 +0800983next_block:
Chao Yu7a2af762017-07-19 00:19:06 +0800984 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800985
Fan Li7df3a432015-12-17 13:20:59 +0800986 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
987 if (create) {
988 if (unlikely(f2fs_cp_error(sbi))) {
989 err = -EIO;
990 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800991 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800992 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800993 if (blkaddr == NULL_ADDR) {
994 prealloc++;
995 last_ofs_in_node = dn.ofs_in_node;
996 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800997 } else {
Hyunchul Leed5097be2017-11-28 09:23:00 +0900998 err = __allocate_data_block(&dn,
999 map->m_seg_type);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001000 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001001 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001002 }
Fan Li7df3a432015-12-17 13:20:59 +08001003 if (err)
1004 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +08001005 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001006 blkaddr = dn.data_blkaddr;
1007 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001008 if (flag == F2FS_GET_BLOCK_BMAP) {
1009 map->m_pblk = 0;
1010 goto sync_out;
1011 }
Chao Yuc4020b22018-01-11 14:42:30 +08001012 if (flag == F2FS_GET_BLOCK_PRECACHE)
1013 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001014 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1015 blkaddr == NULL_ADDR) {
1016 if (map->m_next_pgofs)
1017 *map->m_next_pgofs = pgofs + 1;
Chao Yu4c2ac6a2017-11-30 19:28:22 +08001018 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001019 }
Chao Yu4c2ac6a2017-11-30 19:28:22 +08001020 if (flag != F2FS_GET_BLOCK_FIEMAP)
Fan Li7df3a432015-12-17 13:20:59 +08001021 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001022 }
1023 }
Fan Li7df3a432015-12-17 13:20:59 +08001024
Chao Yu46008c62016-05-09 19:56:30 +08001025 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1026 goto skip;
1027
Chao Yu4fe71e82016-01-26 15:37:38 +08001028 if (map->m_len == 0) {
1029 /* preallocated unwritten block should be mapped for fiemap. */
1030 if (blkaddr == NEW_ADDR)
1031 map->m_flags |= F2FS_MAP_UNWRITTEN;
1032 map->m_flags |= F2FS_MAP_MAPPED;
1033
1034 map->m_pblk = blkaddr;
1035 map->m_len = 1;
1036 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001037 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001038 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001039 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001040 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001041 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001042 } else {
1043 goto sync_out;
1044 }
1045
Chao Yu46008c62016-05-09 19:56:30 +08001046skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001047 dn.ofs_in_node++;
1048 pgofs++;
1049
Chao Yu46008c62016-05-09 19:56:30 +08001050 /* preallocate blocks in batch for one dnode page */
1051 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1052 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001053
Chao Yu46008c62016-05-09 19:56:30 +08001054 dn.ofs_in_node = ofs_in_node;
1055 err = reserve_new_blocks(&dn, prealloc);
1056 if (err)
1057 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001058
Chao Yu46008c62016-05-09 19:56:30 +08001059 map->m_len += dn.ofs_in_node - ofs_in_node;
1060 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1061 err = -ENOSPC;
1062 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001063 }
Chao Yu46008c62016-05-09 19:56:30 +08001064 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001065 }
1066
Chao Yu46008c62016-05-09 19:56:30 +08001067 if (pgofs >= end)
1068 goto sync_out;
1069 else if (dn.ofs_in_node < end_offset)
1070 goto next_block;
1071
Chao Yuc4020b22018-01-11 14:42:30 +08001072 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1073 if (map->m_flags & F2FS_MAP_MAPPED) {
1074 unsigned int ofs = start_pgofs - map->m_lblk;
1075
1076 f2fs_update_extent_cache_range(&dn,
1077 start_pgofs, map->m_pblk + ofs,
1078 map->m_len - ofs);
1079 }
1080 }
1081
Chao Yu46008c62016-05-09 19:56:30 +08001082 f2fs_put_dnode(&dn);
1083
1084 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001085 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001086 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001087 }
Chao Yu46008c62016-05-09 19:56:30 +08001088 goto next_dnode;
1089
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001090sync_out:
Chao Yuc4020b22018-01-11 14:42:30 +08001091 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1092 if (map->m_flags & F2FS_MAP_MAPPED) {
1093 unsigned int ofs = start_pgofs - map->m_lblk;
1094
1095 f2fs_update_extent_cache_range(&dn,
1096 start_pgofs, map->m_pblk + ofs,
1097 map->m_len - ofs);
1098 }
1099 if (map->m_next_extent)
1100 *map->m_next_extent = pgofs + 1;
1101 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001102 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001103unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001104 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001105 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001106 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001107 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001108out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001109 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001110 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001111}
1112
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001113static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001114 struct buffer_head *bh, int create, int flag,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001115 pgoff_t *next_pgofs, int seg_type)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001116{
1117 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001118 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001119
1120 map.m_lblk = iblock;
1121 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001122 map.m_next_pgofs = next_pgofs;
Chao Yuc4020b22018-01-11 14:42:30 +08001123 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001124 map.m_seg_type = seg_type;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001125
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001126 err = f2fs_map_blocks(inode, &map, create, flag);
1127 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001128 map_bh(bh, inode->i_sb, map.m_pblk);
1129 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001130 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001131 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001132 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001133}
1134
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001135static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001136 struct buffer_head *bh_result, int create, int flag,
1137 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001138{
Chao Yuda859852016-01-26 15:42:58 +08001139 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001140 flag, next_pgofs,
1141 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001142}
1143
Chao Yue2b4e2b2015-08-19 19:11:19 +08001144static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001145 struct buffer_head *bh_result, int create)
1146{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001147 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001148 F2FS_GET_BLOCK_DEFAULT, NULL,
1149 rw_hint_to_seg_type(
1150 inode->i_write_hint));
Chao Yue2b4e2b2015-08-19 19:11:19 +08001151}
1152
1153static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1154 struct buffer_head *bh_result, int create)
1155{
Yunlei He179448b2015-12-28 21:48:32 +08001156 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001157 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001158 return -EFBIG;
1159
Chao Yue2b4e2b2015-08-19 19:11:19 +08001160 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001161 F2FS_GET_BLOCK_BMAP, NULL,
1162 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001163}
1164
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001165static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1166{
1167 return (offset >> inode->i_blkbits);
1168}
1169
1170static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1171{
1172 return (blk << inode->i_blkbits);
1173}
1174
Chao Yu442a9db2018-01-11 14:39:57 +08001175static int f2fs_xattr_fiemap(struct inode *inode,
1176 struct fiemap_extent_info *fieinfo)
1177{
1178 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1179 struct page *page;
1180 struct node_info ni;
1181 __u64 phys = 0, len;
1182 __u32 flags;
1183 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1184 int err = 0;
1185
1186 if (f2fs_has_inline_xattr(inode)) {
1187 int offset;
1188
1189 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1190 inode->i_ino, false);
1191 if (!page)
1192 return -ENOMEM;
1193
1194 get_node_info(sbi, inode->i_ino, &ni);
1195
1196 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1197 offset = offsetof(struct f2fs_inode, i_addr) +
1198 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
1199 F2FS_INLINE_XATTR_ADDRS(inode));
1200
1201 phys += offset;
1202 len = inline_xattr_size(inode);
1203
1204 f2fs_put_page(page, 1);
1205
1206 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1207
1208 if (!xnid)
1209 flags |= FIEMAP_EXTENT_LAST;
1210
1211 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1212 if (err || err == 1)
1213 return err;
1214 }
1215
1216 if (xnid) {
1217 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1218 if (!page)
1219 return -ENOMEM;
1220
1221 get_node_info(sbi, xnid, &ni);
1222
1223 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1224 len = inode->i_sb->s_blocksize;
1225
1226 f2fs_put_page(page, 1);
1227
1228 flags = FIEMAP_EXTENT_LAST;
1229 }
1230
1231 if (phys)
1232 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1233
1234 return (err < 0 ? err : 0);
1235}
1236
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001237int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1238 u64 start, u64 len)
1239{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001240 struct buffer_head map_bh;
1241 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001242 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001243 u64 logical = 0, phys = 0, size = 0;
1244 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001245 int ret = 0;
1246
Chao Yuc4020b22018-01-11 14:42:30 +08001247 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1248 ret = f2fs_precache_extents(inode);
1249 if (ret)
1250 return ret;
1251 }
1252
Chao Yu442a9db2018-01-11 14:39:57 +08001253 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001254 if (ret)
1255 return ret;
1256
Chao Yuf1b43d42018-01-11 14:37:35 +08001257 inode_lock(inode);
1258
Chao Yu442a9db2018-01-11 14:39:57 +08001259 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1260 ret = f2fs_xattr_fiemap(inode, fieinfo);
1261 goto out;
1262 }
1263
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001264 if (f2fs_has_inline_data(inode)) {
1265 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1266 if (ret != -EAGAIN)
Chao Yuf1b43d42018-01-11 14:37:35 +08001267 goto out;
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001268 }
1269
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001270 if (logical_to_blk(inode, len) == 0)
1271 len = blk_to_logical(inode, 1);
1272
1273 start_blk = logical_to_blk(inode, start);
1274 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001275
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001276next:
1277 memset(&map_bh, 0, sizeof(struct buffer_head));
1278 map_bh.b_size = len;
1279
Chao Yue2b4e2b2015-08-19 19:11:19 +08001280 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001281 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001282 if (ret)
1283 goto out;
1284
1285 /* HOLE */
1286 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001287 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001288
1289 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1290 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001291 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001292
Fan Li9a950d52015-12-26 18:07:41 +08001293 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001294 }
Fan Li9a950d52015-12-26 18:07:41 +08001295
Chao Yuda5af122016-01-08 20:19:27 +08001296 if (size) {
1297 if (f2fs_encrypted_inode(inode))
1298 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1299
Fan Li9a950d52015-12-26 18:07:41 +08001300 ret = fiemap_fill_next_extent(fieinfo, logical,
1301 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001302 }
Fan Li9a950d52015-12-26 18:07:41 +08001303
1304 if (start_blk > last_blk || ret)
1305 goto out;
1306
1307 logical = blk_to_logical(inode, start_blk);
1308 phys = blk_to_logical(inode, map_bh.b_blocknr);
1309 size = map_bh.b_size;
1310 flags = 0;
1311 if (buffer_unwritten(&map_bh))
1312 flags = FIEMAP_EXTENT_UNWRITTEN;
1313
1314 start_blk += logical_to_blk(inode, size);
1315
1316prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001317 cond_resched();
1318 if (fatal_signal_pending(current))
1319 ret = -EINTR;
1320 else
1321 goto next;
1322out:
1323 if (ret == 1)
1324 ret = 0;
1325
Al Viro59551022016-01-22 15:40:57 -05001326 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001327 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001328}
1329
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001330/*
1331 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1332 * Major change was from block_size == page_size in f2fs by default.
1333 */
1334static int f2fs_mpage_readpages(struct address_space *mapping,
1335 struct list_head *pages, struct page *page,
1336 unsigned nr_pages)
1337{
1338 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001339 sector_t last_block_in_bio = 0;
1340 struct inode *inode = mapping->host;
1341 const unsigned blkbits = inode->i_blkbits;
1342 const unsigned blocksize = 1 << blkbits;
1343 sector_t block_in_file;
1344 sector_t last_block;
1345 sector_t last_block_in_file;
1346 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001347 struct f2fs_map_blocks map;
1348
1349 map.m_pblk = 0;
1350 map.m_lblk = 0;
1351 map.m_len = 0;
1352 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001353 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08001354 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001355 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001356
LiFan736c0a72017-11-25 11:46:18 +08001357 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001358 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001359 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08001360
1361 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001362 list_del(&page->lru);
1363 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001364 page->index,
1365 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001366 goto next_page;
1367 }
1368
1369 block_in_file = (sector_t)page->index;
1370 last_block = block_in_file + nr_pages;
1371 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1372 blkbits;
1373 if (last_block > last_block_in_file)
1374 last_block = last_block_in_file;
1375
1376 /*
1377 * Map blocks using the previous result first.
1378 */
1379 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1380 block_in_file > map.m_lblk &&
1381 block_in_file < (map.m_lblk + map.m_len))
1382 goto got_it;
1383
1384 /*
1385 * Then do more f2fs_map_blocks() calls until we are
1386 * done with this page.
1387 */
1388 map.m_flags = 0;
1389
1390 if (block_in_file < last_block) {
1391 map.m_lblk = block_in_file;
1392 map.m_len = last_block - block_in_file;
1393
Chao Yu46c9e142015-09-18 16:54:16 +08001394 if (f2fs_map_blocks(inode, &map, 0,
Qiuyang Sunf2220c72017-08-09 17:27:30 +08001395 F2FS_GET_BLOCK_DEFAULT))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001396 goto set_error_page;
1397 }
1398got_it:
1399 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1400 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1401 SetPageMappedToDisk(page);
1402
1403 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1404 SetPageUptodate(page);
1405 goto confused;
1406 }
1407 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001408 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001409 if (!PageUptodate(page))
1410 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001411 unlock_page(page);
1412 goto next_page;
1413 }
1414
1415 /*
1416 * This page will go to BIO. Do we need to send this
1417 * BIO off first?
1418 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001419 if (bio && (last_block_in_bio != block_nr - 1 ||
1420 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001421submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001422 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001423 bio = NULL;
1424 }
1425 if (bio == NULL) {
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001426 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001427 if (IS_ERR(bio)) {
1428 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001429 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001430 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001431 }
1432
1433 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1434 goto submit_and_realloc;
1435
1436 last_block_in_bio = block_nr;
1437 goto next_page;
1438set_error_page:
1439 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001440 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001441 unlock_page(page);
1442 goto next_page;
1443confused:
1444 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001445 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001446 bio = NULL;
1447 }
1448 unlock_page(page);
1449next_page:
1450 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001451 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001452 }
1453 BUG_ON(pages && !list_empty(pages));
1454 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001455 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001456 return 0;
1457}
1458
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001459static int f2fs_read_data_page(struct file *file, struct page *page)
1460{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001461 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001462 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001463
Chao Yuc20e89c2014-05-06 16:53:08 +08001464 trace_f2fs_readpage(page, DATA);
1465
arter97e1c42042014-08-06 23:22:50 +09001466 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001467 if (f2fs_has_inline_data(inode))
1468 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001469 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001470 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001471 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001472}
1473
1474static int f2fs_read_data_pages(struct file *file,
1475 struct address_space *mapping,
1476 struct list_head *pages, unsigned nr_pages)
1477{
Hsiang Kao71cb4af2017-09-24 02:45:42 +08001478 struct inode *inode = mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001479 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001480
1481 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001482
1483 /* If the file has inline data, skip readpages */
1484 if (f2fs_has_inline_data(inode))
1485 return 0;
1486
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001487 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001488}
1489
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001490static int encrypt_one_page(struct f2fs_io_info *fio)
1491{
1492 struct inode *inode = fio->page->mapping->host;
1493 gfp_t gfp_flags = GFP_NOFS;
1494
Jaegeuk Kim19585932017-09-05 16:54:24 -07001495 if (!f2fs_encrypted_file(inode))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001496 return 0;
1497
1498 /* wait for GCed encrypted page writeback */
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -07001499 f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001500
1501retry_encrypt:
1502 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1503 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1504 if (!IS_ERR(fio->encrypted_page))
1505 return 0;
1506
1507 /* flush pending IOs and wait for a while in the ENOMEM case */
1508 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001509 f2fs_flush_merged_writes(fio->sbi);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001510 congestion_wait(BLK_RW_ASYNC, HZ/50);
1511 gfp_flags |= __GFP_NOFAIL;
1512 goto retry_encrypt;
1513 }
1514 return PTR_ERR(fio->encrypted_page);
1515}
1516
1517static inline bool need_inplace_update(struct f2fs_io_info *fio)
1518{
1519 struct inode *inode = fio->page->mapping->host;
1520
Jaegeuk Kim1ad71a22017-12-07 16:25:39 -08001521 if (f2fs_is_pinned_file(inode))
1522 return true;
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001523 if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode))
1524 return false;
1525 if (is_cold_data(fio->page))
1526 return false;
1527 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1528 return false;
1529
1530 return need_inplace_update_policy(inode, fio);
1531}
1532
Jaegeuk Kima8177372017-04-24 15:20:16 -07001533static inline bool valid_ipu_blkaddr(struct f2fs_io_info *fio)
1534{
1535 if (fio->old_blkaddr == NEW_ADDR)
1536 return false;
1537 if (fio->old_blkaddr == NULL_ADDR)
1538 return false;
1539 return true;
1540}
1541
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001542int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001543{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001544 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001545 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001546 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001547 struct extent_info ei = {0,0,0};
1548 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001549 int err = 0;
1550
1551 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00001552 if (need_inplace_update(fio) &&
1553 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1554 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07001555
1556 if (valid_ipu_blkaddr(fio)) {
Hou Pengyange959c8f2017-04-25 12:45:13 +00001557 ipu_force = true;
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001558 fio->need_lock = LOCK_DONE;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001559 goto got_it;
1560 }
1561 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001562
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001563 /* Deadlock due to between page->lock and f2fs_lock_op */
1564 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1565 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001566
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001567 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001568 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001569 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001570
Chao Yu28bc1062016-02-06 14:40:34 +08001571 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001572
1573 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001574 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001575 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001576 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001577 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00001578got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001579 /*
1580 * If current allocation needs SSR,
1581 * it had better in-place writes for updated data.
1582 */
Jaegeuk Kima8177372017-04-24 15:20:16 -07001583 if (ipu_force || (valid_ipu_blkaddr(fio) && need_inplace_update(fio))) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001584 err = encrypt_one_page(fio);
1585 if (err)
1586 goto out_writepage;
1587
1588 set_page_writeback(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001589 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001590 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001591 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -07001592 err = rewrite_data_page(fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001593 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001594 set_inode_flag(inode, FI_UPDATE_WRITE);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001595 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001596 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001597
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001598 if (fio->need_lock == LOCK_RETRY) {
1599 if (!f2fs_trylock_op(fio->sbi)) {
1600 err = -EAGAIN;
1601 goto out_writepage;
1602 }
1603 fio->need_lock = LOCK_REQ;
1604 }
1605
1606 err = encrypt_one_page(fio);
1607 if (err)
1608 goto out_writepage;
1609
1610 set_page_writeback(page);
1611
Hou Pengyang279d6df2017-04-27 00:17:21 +08001612 /* LFS mode write path */
1613 write_data_page(&dn, fio);
1614 trace_f2fs_do_write_data_page(page, OPU);
1615 set_inode_flag(inode, FI_APPEND_WRITE);
1616 if (page->index == 0)
1617 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001618out_writepage:
1619 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001620out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001621 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001622 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001623 return err;
1624}
1625
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001626static int __write_data_page(struct page *page, bool *submitted,
Chao Yub0af6d42017-08-02 23:21:48 +08001627 struct writeback_control *wbc,
1628 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001629{
1630 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001631 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001632 loff_t i_size = i_size_read(inode);
1633 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001634 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001635 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001636 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001637 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001638 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001639 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001640 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08001641 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001642 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001643 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001644 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00001645 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001646 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001647 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001648 .submitted = false,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001649 .need_lock = LOCK_RETRY,
Chao Yub0af6d42017-08-02 23:21:48 +08001650 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08001651 .io_wbc = wbc,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001652 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001653
Chao Yuecda0de2014-05-06 16:48:26 +08001654 trace_f2fs_writepage(page, DATA);
1655
Chao Yu0771fcc2017-06-29 23:20:45 +08001656 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1657 goto redirty_out;
1658
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001659 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001660 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001661
1662 /*
1663 * If the offset is out-of-range of file size,
1664 * this page does not have to be written to disk.
1665 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001666 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001667 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001668 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001669
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001670 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001671write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001672 if (f2fs_is_drop_cache(inode))
1673 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001674 /* we should not write 0'th page having journal header */
1675 if (f2fs_is_volatile_file(inode) && (!page->index ||
1676 (!wbc->for_reclaim &&
1677 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001678 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001679
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001680 /* we should bypass data pages to proceed the kworkder jobs */
1681 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001682 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001683 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001684 }
1685
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001686 /* Dentry blocks are controlled by checkpoint */
1687 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001688 fio.need_lock = LOCK_DONE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001689 err = do_write_data_page(&fio);
1690 goto done;
1691 }
1692
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001693 if (!wbc->for_reclaim)
1694 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001695 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001696 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001697 else
1698 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001699
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001700 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08001701 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001702 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08001703 if (!err)
1704 goto out;
1705 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001706
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001707 if (err == -EAGAIN) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001708 err = do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001709 if (err == -EAGAIN) {
1710 fio.need_lock = LOCK_REQ;
1711 err = do_write_data_page(&fio);
1712 }
1713 }
Chao Yua0d00fa2017-10-09 17:55:19 +08001714
1715 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001716 if (F2FS_I(inode)->last_disk_size < psize)
1717 F2FS_I(inode)->last_disk_size = psize;
Chao Yua0d00fa2017-10-09 17:55:19 +08001718 up_write(&F2FS_I(inode)->i_sem);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001719
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001720done:
1721 if (err && err != -ENOENT)
1722 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001723
Jaegeuk Kim39936832012-11-22 16:21:29 +09001724out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001725 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001726 if (err)
1727 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001728
1729 if (wbc->for_reclaim) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001730 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001731 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yuc227f912015-12-16 13:09:20 +08001732 remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001733 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001734 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001735
1736 unlock_page(page);
Jaegeuk Kima7881892017-04-20 13:51:57 -07001737 if (!S_ISDIR(inode->i_mode))
1738 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001739
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001740 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001741 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001742 submitted = NULL;
1743 }
1744
1745 if (submitted)
1746 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001747
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001748 return 0;
1749
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001750redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001751 redirty_page_for_writepage(wbc, page);
Chao Yu0002b612016-11-28 19:13:43 -08001752 if (!err)
1753 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001754 unlock_page(page);
1755 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001756}
1757
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001758static int f2fs_write_data_page(struct page *page,
1759 struct writeback_control *wbc)
1760{
Chao Yub0af6d42017-08-02 23:21:48 +08001761 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001762}
1763
Chao Yu8f46dca2015-07-14 18:56:10 +08001764/*
1765 * This function was copied from write_cche_pages from mm/page-writeback.c.
1766 * The major change is making write step of cold data page separately from
1767 * warm/hot data page.
1768 */
1769static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08001770 struct writeback_control *wbc,
1771 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08001772{
1773 int ret = 0;
1774 int done = 0;
1775 struct pagevec pvec;
1776 int nr_pages;
1777 pgoff_t uninitialized_var(writeback_index);
1778 pgoff_t index;
1779 pgoff_t end; /* Inclusive */
1780 pgoff_t done_index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001781 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001782 int cycled;
1783 int range_whole = 0;
1784 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001785
Mel Gorman86679822017-11-15 17:37:52 -08001786 pagevec_init(&pvec);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001787
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001788 if (get_dirty_pages(mapping->host) <=
1789 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1790 set_inode_flag(mapping->host, FI_HOT_DATA);
1791 else
1792 clear_inode_flag(mapping->host, FI_HOT_DATA);
1793
Chao Yu8f46dca2015-07-14 18:56:10 +08001794 if (wbc->range_cyclic) {
1795 writeback_index = mapping->writeback_index; /* prev offset */
1796 index = writeback_index;
1797 if (index == 0)
1798 cycled = 1;
1799 else
1800 cycled = 0;
1801 end = -1;
1802 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001803 index = wbc->range_start >> PAGE_SHIFT;
1804 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001805 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1806 range_whole = 1;
1807 cycled = 1; /* ignore range_cyclic tests */
1808 }
1809 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1810 tag = PAGECACHE_TAG_TOWRITE;
1811 else
1812 tag = PAGECACHE_TAG_DIRTY;
1813retry:
1814 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1815 tag_pages_for_writeback(mapping, index, end);
1816 done_index = index;
1817 while (!done && (index <= end)) {
1818 int i;
1819
Jan Kara69c4f352017-11-15 17:34:48 -08001820 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08001821 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08001822 if (nr_pages == 0)
1823 break;
1824
1825 for (i = 0; i < nr_pages; i++) {
1826 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001827 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001828
Chao Yu8f46dca2015-07-14 18:56:10 +08001829 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001830retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08001831 lock_page(page);
1832
1833 if (unlikely(page->mapping != mapping)) {
1834continue_unlock:
1835 unlock_page(page);
1836 continue;
1837 }
1838
1839 if (!PageDirty(page)) {
1840 /* someone wrote it for us */
1841 goto continue_unlock;
1842 }
1843
Chao Yu8f46dca2015-07-14 18:56:10 +08001844 if (PageWriteback(page)) {
1845 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001846 f2fs_wait_on_page_writeback(page,
1847 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001848 else
1849 goto continue_unlock;
1850 }
1851
1852 BUG_ON(PageWriteback(page));
1853 if (!clear_page_dirty_for_io(page))
1854 goto continue_unlock;
1855
Chao Yub0af6d42017-08-02 23:21:48 +08001856 ret = __write_data_page(page, &submitted, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08001857 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08001858 /*
1859 * keep nr_to_write, since vfs uses this to
1860 * get # of written pages.
1861 */
1862 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1863 unlock_page(page);
1864 ret = 0;
1865 continue;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001866 } else if (ret == -EAGAIN) {
1867 ret = 0;
1868 if (wbc->sync_mode == WB_SYNC_ALL) {
1869 cond_resched();
1870 congestion_wait(BLK_RW_ASYNC,
1871 HZ/50);
1872 goto retry_write;
1873 }
1874 continue;
Chao Yu0002b612016-11-28 19:13:43 -08001875 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001876 done_index = page->index + 1;
1877 done = 1;
1878 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001879 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001880 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08001881 }
1882
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001883 /* give a priority to WB_SYNC threads */
1884 if ((atomic_read(&F2FS_M_SB(mapping)->wb_sync_req) ||
1885 --wbc->nr_to_write <= 0) &&
1886 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08001887 done = 1;
1888 break;
1889 }
1890 }
1891 pagevec_release(&pvec);
1892 cond_resched();
1893 }
1894
Chao Yu8f46dca2015-07-14 18:56:10 +08001895 if (!cycled && !done) {
1896 cycled = 1;
1897 index = 0;
1898 end = writeback_index - 1;
1899 goto retry;
1900 }
1901 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1902 mapping->writeback_index = done_index;
1903
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001904 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001905 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
1906 0, last_idx, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08001907
Chao Yu8f46dca2015-07-14 18:56:10 +08001908 return ret;
1909}
1910
Chao Yub0af6d42017-08-02 23:21:48 +08001911int __f2fs_write_data_pages(struct address_space *mapping,
1912 struct writeback_control *wbc,
1913 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001914{
1915 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001916 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001917 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001918 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001919
P J Pcfb185a2013-04-03 11:38:00 +09001920 /* deal with chardevs and other special file */
1921 if (!mapping->a_ops->writepage)
1922 return 0;
1923
Chao Yu6a290542015-07-17 18:02:39 +08001924 /* skip writing if there is no dirty page in this inode */
1925 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1926 return 0;
1927
Chao Yu0771fcc2017-06-29 23:20:45 +08001928 /* during POR, we don't need to trigger writepage at all. */
1929 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1930 goto skip_write;
1931
Jaegeuk Kima1257022015-10-08 10:40:07 -07001932 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1933 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1934 available_free_memory(sbi, DIRTY_DENTS))
1935 goto skip_write;
1936
Chao Yud323d002015-10-27 09:53:45 +08001937 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001938 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001939 goto skip_write;
1940
Yunlei Hed31c7c32016-02-04 16:14:00 +08001941 trace_f2fs_writepages(mapping->host, wbc, DATA);
1942
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001943 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
1944 if (wbc->sync_mode == WB_SYNC_ALL)
1945 atomic_inc(&sbi->wb_sync_req);
1946 else if (atomic_read(&sbi->wb_sync_req))
1947 goto skip_write;
1948
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001949 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08001950 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001951 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001952
1953 if (wbc->sync_mode == WB_SYNC_ALL)
1954 atomic_dec(&sbi->wb_sync_req);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001955 /*
1956 * if some pages were truncated, we cannot guarantee its mapping->host
1957 * to detect pending bios.
1958 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001959
Chao Yuc227f912015-12-16 13:09:20 +08001960 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001961 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001962
1963skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001964 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001965 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001966 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001967}
1968
Chao Yub0af6d42017-08-02 23:21:48 +08001969static int f2fs_write_data_pages(struct address_space *mapping,
1970 struct writeback_control *wbc)
1971{
1972 struct inode *inode = mapping->host;
1973
1974 return __f2fs_write_data_pages(mapping, wbc,
1975 F2FS_I(inode)->cp_task == current ?
1976 FS_CP_DATA_IO : FS_DATA_IO);
1977}
1978
Chao Yu3aab8f82014-07-02 13:25:04 +08001979static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1980{
1981 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001982 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001983
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001984 if (to > i_size) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001985 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001986 truncate_pagecache(inode, i_size);
1987 truncate_blocks(inode, i_size, true);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001988 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3aab8f82014-07-02 13:25:04 +08001989 }
1990}
1991
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001992static int prepare_write_begin(struct f2fs_sb_info *sbi,
1993 struct page *page, loff_t pos, unsigned len,
1994 block_t *blk_addr, bool *node_changed)
1995{
1996 struct inode *inode = page->mapping->host;
1997 pgoff_t index = page->index;
1998 struct dnode_of_data dn;
1999 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002000 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00002001 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002002 int err = 0;
2003
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002004 /*
2005 * we already allocated all the blocks, so we don't need to get
2006 * the block addresses when there is no need to fill the page.
2007 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002008 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2009 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002010 return 0;
2011
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002012 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002013 (pos & PAGE_MASK) >= i_size_read(inode)) {
Yunlei He59c90812017-03-13 20:22:18 +08002014 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002015 locked = true;
2016 }
2017restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002018 /* check inline_data */
2019 ipage = get_node_page(sbi, inode->i_ino);
2020 if (IS_ERR(ipage)) {
2021 err = PTR_ERR(ipage);
2022 goto unlock_out;
2023 }
2024
2025 set_new_dnode(&dn, inode, ipage, ipage, 0);
2026
2027 if (f2fs_has_inline_data(inode)) {
Chao Yuf2470372017-07-19 00:19:05 +08002028 if (pos + len <= MAX_INLINE_DATA(inode)) {
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002029 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002030 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08002031 if (inode->i_nlink)
2032 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002033 } else {
2034 err = f2fs_convert_inline_page(&dn, page);
2035 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002036 goto out;
2037 if (dn.data_blkaddr == NULL_ADDR)
2038 err = f2fs_get_block(&dn, index);
2039 }
2040 } else if (locked) {
2041 err = f2fs_get_block(&dn, index);
2042 } else {
2043 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2044 dn.data_blkaddr = ei.blk + index - ei.fofs;
2045 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002046 /* hole case */
2047 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07002048 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002049 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08002050 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2051 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002052 locked = true;
2053 goto restart;
2054 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002055 }
2056 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002057
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002058 /* convert_inline_page can make node_changed */
2059 *blk_addr = dn.data_blkaddr;
2060 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002061out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002062 f2fs_put_dnode(&dn);
2063unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002064 if (locked)
Yunlei He59c90812017-03-13 20:22:18 +08002065 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002066 return err;
2067}
2068
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002069static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2070 loff_t pos, unsigned len, unsigned flags,
2071 struct page **pagep, void **fsdata)
2072{
2073 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002074 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002075 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002076 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002077 bool need_balance = false;
2078 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002079 int err = 0;
2080
Chao Yu62aed042014-05-06 16:46:04 +08002081 trace_f2fs_write_begin(inode, pos, len, flags);
2082
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002083 if (f2fs_is_atomic_file(inode) &&
2084 !available_free_memory(sbi, INMEM_PAGES)) {
2085 err = -ENOMEM;
2086 goto fail;
2087 }
2088
Jaegeuk Kim5f727392014-11-25 10:59:45 -08002089 /*
2090 * We should check this at this moment to avoid deadlock on inode page
2091 * and #0 page. The locking rule for inline_data conversion should be:
2092 * lock_page(page #0) -> lock_page(inode_page)
2093 */
2094 if (index != 0) {
2095 err = f2fs_convert_inline_inode(inode);
2096 if (err)
2097 goto fail;
2098 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002099repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002100 /*
2101 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2102 * wait_for_stable_page. Will wait that below with our IO control.
2103 */
Chao Yu01eccef2017-10-28 16:52:30 +08002104 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002105 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08002106 if (!page) {
2107 err = -ENOMEM;
2108 goto fail;
2109 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09002110
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002111 *pagep = page;
2112
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002113 err = prepare_write_begin(sbi, page, pos, len,
2114 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002115 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002116 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002117
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002118 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002119 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002120 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002121 lock_page(page);
2122 if (page->mapping != mapping) {
2123 /* The page got truncated from under us */
2124 f2fs_put_page(page, 1);
2125 goto repeat;
2126 }
2127 }
2128
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002129 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002130
Chao Yu08b39fb2015-10-08 13:27:34 +08002131 /* wait for GCed encrypted page writeback */
Jaegeuk Kim19585932017-09-05 16:54:24 -07002132 if (f2fs_encrypted_file(inode))
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -07002133 f2fs_wait_on_block_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08002134
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002135 if (len == PAGE_SIZE || PageUptodate(page))
2136 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002137
Yunlei He746e2402016-12-20 11:11:35 +08002138 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2139 zero_user_segment(page, len, PAGE_SIZE);
2140 return 0;
2141 }
2142
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002143 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002144 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002145 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002146 } else {
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07002147 err = f2fs_submit_page_read(inode, page, blkaddr);
2148 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08002149 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08002150
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002151 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002152 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002153 f2fs_put_page(page, 1);
2154 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002155 }
Chao Yu1563ac72016-07-03 22:05:12 +08002156 if (unlikely(!PageUptodate(page))) {
2157 err = -EIO;
2158 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002159 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002160 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002161 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002162
Chao Yu3aab8f82014-07-02 13:25:04 +08002163fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002164 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002165 f2fs_write_failed(mapping, pos + len);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002166 if (f2fs_is_atomic_file(inode))
2167 drop_inmem_pages_all(sbi);
Chao Yu3aab8f82014-07-02 13:25:04 +08002168 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002169}
2170
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002171static int f2fs_write_end(struct file *file,
2172 struct address_space *mapping,
2173 loff_t pos, unsigned len, unsigned copied,
2174 struct page *page, void *fsdata)
2175{
2176 struct inode *inode = page->mapping->host;
2177
Chao Yudfb2bf32014-05-06 16:47:23 +08002178 trace_f2fs_write_end(inode, pos, len, copied);
2179
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002180 /*
2181 * This should be come from len == PAGE_SIZE, and we expect copied
2182 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2183 * let generic_perform_write() try to copy data again through copied=0.
2184 */
2185 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08002186 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002187 copied = 0;
2188 else
2189 SetPageUptodate(page);
2190 }
2191 if (!copied)
2192 goto unlock_out;
2193
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002194 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002195
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002196 if (pos + copied > i_size_read(inode))
2197 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002198unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002199 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002200 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002201 return copied;
2202}
2203
Omar Sandoval6f673762015-03-16 04:33:52 -07002204static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2205 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002206{
2207 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002208
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002209 if (offset & blocksize_mask)
2210 return -EINVAL;
2211
Al Viro5b46f252014-03-16 18:07:34 -04002212 if (iov_iter_alignment(iter) & blocksize_mask)
2213 return -EINVAL;
2214
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002215 return 0;
2216}
2217
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002218static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002219{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002220 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002221 struct inode *inode = mapping->host;
2222 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002223 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002224 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002225 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002226
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002227 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002228 if (err)
2229 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002230
Jaegeuk Kimc040ff92016-11-11 16:46:40 -08002231 if (__force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002232 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002233
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002234 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002235
Chao Yu82e0a5a2016-07-13 09:18:29 +08002236 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002237 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002238 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
2239
2240 if (rw == WRITE) {
Chao Yub0af6d42017-08-02 23:21:48 +08002241 if (err > 0) {
2242 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2243 err);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002244 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yub0af6d42017-08-02 23:21:48 +08002245 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002246 f2fs_write_failed(mapping, offset + count);
Chao Yub0af6d42017-08-02 23:21:48 +08002247 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002248 }
Chao Yu70407fa2014-07-31 21:11:22 +08002249
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002250 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002251
Chao Yu3aab8f82014-07-02 13:25:04 +08002252 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002253}
2254
Chao Yu487261f2015-02-05 17:44:29 +08002255void f2fs_invalidate_page(struct page *page, unsigned int offset,
2256 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002257{
2258 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002259 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002260
Chao Yu487261f2015-02-05 17:44:29 +08002261 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002262 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002263 return;
2264
Chao Yu487261f2015-02-05 17:44:29 +08002265 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08002266 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002267 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08002268 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002269 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08002270 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002271 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08002272 remove_dirty_inode(inode);
2273 }
Chao Yu487261f2015-02-05 17:44:29 +08002274 }
Chao Yudecd36b2015-08-07 18:42:09 +08002275
2276 /* This is atomic written page, keep Private */
2277 if (IS_ATOMIC_WRITTEN_PAGE(page))
Jaegeuk Kim8c242db2017-03-17 09:55:52 +08002278 return drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002279
Chao Yu23dc9742016-04-29 20:09:15 +08002280 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002281 ClearPagePrivate(page);
2282}
2283
Chao Yu487261f2015-02-05 17:44:29 +08002284int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002285{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002286 /* If this is dirty page, keep PagePrivate */
2287 if (PageDirty(page))
2288 return 0;
2289
Chao Yudecd36b2015-08-07 18:42:09 +08002290 /* This is atomic written page, keep Private */
2291 if (IS_ATOMIC_WRITTEN_PAGE(page))
2292 return 0;
2293
Chao Yu23dc9742016-04-29 20:09:15 +08002294 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002295 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002296 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002297}
2298
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002299/*
2300 * This was copied from __set_page_dirty_buffers which gives higher performance
2301 * in very high speed storages. (e.g., pmem)
2302 */
2303void f2fs_set_page_dirty_nobuffers(struct page *page)
2304{
2305 struct address_space *mapping = page->mapping;
2306 unsigned long flags;
2307
2308 if (unlikely(!mapping))
2309 return;
2310
2311 spin_lock(&mapping->private_lock);
2312 lock_page_memcg(page);
2313 SetPageDirty(page);
2314 spin_unlock(&mapping->private_lock);
2315
2316 spin_lock_irqsave(&mapping->tree_lock, flags);
2317 WARN_ON_ONCE(!PageUptodate(page));
2318 account_page_dirtied(page, mapping);
2319 radix_tree_tag_set(&mapping->page_tree,
2320 page_index(page), PAGECACHE_TAG_DIRTY);
2321 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2322 unlock_page_memcg(page);
2323
2324 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
2325 return;
2326}
2327
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002328static int f2fs_set_data_page_dirty(struct page *page)
2329{
2330 struct address_space *mapping = page->mapping;
2331 struct inode *inode = mapping->host;
2332
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002333 trace_f2fs_set_page_dirty(page, DATA);
2334
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002335 if (!PageUptodate(page))
2336 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002337
Chao Yu5fe45742017-01-07 18:50:26 +08002338 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002339 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2340 register_inmem_page(inode, page);
2341 return 1;
2342 }
2343 /*
2344 * Previously, this page has been registered, we just
2345 * return here.
2346 */
2347 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002348 }
2349
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002350 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002351 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002352 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002353 return 1;
2354 }
2355 return 0;
2356}
2357
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002358static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2359{
Chao Yu454ae7e2014-04-22 13:34:01 +08002360 struct inode *inode = mapping->host;
2361
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002362 if (f2fs_has_inline_data(inode))
2363 return 0;
2364
2365 /* make sure allocating whole blocks */
2366 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2367 filemap_write_and_wait(mapping);
2368
Chao Yue2b4e2b2015-08-19 19:11:19 +08002369 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002370}
2371
Weichao Guo5b7a4872016-09-20 05:03:27 +08002372#ifdef CONFIG_MIGRATION
2373#include <linux/migrate.h>
2374
2375int f2fs_migrate_page(struct address_space *mapping,
2376 struct page *newpage, struct page *page, enum migrate_mode mode)
2377{
2378 int rc, extra_count;
2379 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2380 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2381
2382 BUG_ON(PageWriteback(page));
2383
2384 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07002385 if (atomic_written) {
2386 if (mode != MIGRATE_SYNC)
2387 return -EBUSY;
2388 if (!mutex_trylock(&fi->inmem_lock))
2389 return -EAGAIN;
2390 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002391
2392 /*
2393 * A reference is expected if PagePrivate set when move mapping,
2394 * however F2FS breaks this for maintaining dirty page counts when
2395 * truncating pages. So here adjusting the 'extra_count' make it work.
2396 */
2397 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2398 rc = migrate_page_move_mapping(mapping, newpage,
2399 page, NULL, mode, extra_count);
2400 if (rc != MIGRATEPAGE_SUCCESS) {
2401 if (atomic_written)
2402 mutex_unlock(&fi->inmem_lock);
2403 return rc;
2404 }
2405
2406 if (atomic_written) {
2407 struct inmem_pages *cur;
2408 list_for_each_entry(cur, &fi->inmem_pages, list)
2409 if (cur->page == page) {
2410 cur->page = newpage;
2411 break;
2412 }
2413 mutex_unlock(&fi->inmem_lock);
2414 put_page(page);
2415 get_page(newpage);
2416 }
2417
2418 if (PagePrivate(page))
2419 SetPagePrivate(newpage);
2420 set_page_private(newpage, page_private(page));
2421
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07002422 if (mode != MIGRATE_SYNC_NO_COPY)
2423 migrate_page_copy(newpage, page);
2424 else
2425 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002426
2427 return MIGRATEPAGE_SUCCESS;
2428}
2429#endif
2430
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002431const struct address_space_operations f2fs_dblock_aops = {
2432 .readpage = f2fs_read_data_page,
2433 .readpages = f2fs_read_data_pages,
2434 .writepage = f2fs_write_data_page,
2435 .writepages = f2fs_write_data_pages,
2436 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002437 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002438 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002439 .invalidatepage = f2fs_invalidate_page,
2440 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002441 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002442 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002443#ifdef CONFIG_MIGRATION
2444 .migratepage = f2fs_migrate_page,
2445#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002446};