blob: fb96bb71da003046bf00b9698aaa7da7046d0530 [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 Kim38f91ca2016-05-18 14:07:56 -0700114 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900115 }
Chao Yu36951b32016-11-16 10:41:20 +0800116 dec_page_count(sbi, type);
117 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900118 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800119 }
Chao Yu36951b32016-11-16 10:41:20 +0800120 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700121 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900122 wake_up(&sbi->cp_wait);
123
124 bio_put(bio);
125}
126
Gu Zheng940a6d32013-12-20 17:39:59 +0800127/*
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700128 * Return true, if pre_bio's bdev is same as its target device.
129 */
130struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
131 block_t blk_addr, struct bio *bio)
132{
133 struct block_device *bdev = sbi->sb->s_bdev;
134 int i;
135
136 for (i = 0; i < sbi->s_ndevs; i++) {
137 if (FDEV(i).start_blk <= blk_addr &&
138 FDEV(i).end_blk >= blk_addr) {
139 blk_addr -= FDEV(i).start_blk;
140 bdev = FDEV(i).bdev;
141 break;
142 }
143 }
144 if (bio) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200145 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700146 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
147 }
148 return bdev;
149}
150
151int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
152{
153 int i;
154
155 for (i = 0; i < sbi->s_ndevs; i++)
156 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
157 return i;
158 return 0;
159}
160
161static bool __same_bdev(struct f2fs_sb_info *sbi,
162 block_t blk_addr, struct bio *bio)
163{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200164 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
165 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700166}
167
168/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800169 * Low-level block read/write IO operations.
170 */
171static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
172 int npages, bool is_read)
173{
174 struct bio *bio;
175
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700176 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +0800177
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700178 f2fs_target_device(sbi, blk_addr, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800179 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +0800180 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800181
182 return bio;
183}
184
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700185static inline void __submit_bio(struct f2fs_sb_info *sbi,
186 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700187{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700188 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800189 unsigned int start;
190
Damien Le Moal0bfd7a02016-10-28 17:45:00 +0900191 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700192 current->plug && (type == DATA || type == NODE))
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700193 blk_finish_plug(current->plug);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800194
195 if (type != DATA && type != NODE)
196 goto submit_io;
197
198 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
199 start %= F2FS_IO_SIZE(sbi);
200
201 if (start == 0)
202 goto submit_io;
203
204 /* fill dummy pages */
205 for (; start < F2FS_IO_SIZE(sbi); start++) {
206 struct page *page =
207 mempool_alloc(sbi->write_io_dummy,
208 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
209 f2fs_bug_on(sbi, !page);
210
211 SetPagePrivate(page);
212 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
213 lock_page(page);
214 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
215 f2fs_bug_on(sbi, 1);
216 }
217 /*
218 * In the NODE case, we lose next block address chain. So, we
219 * need to do checkpoint in f2fs_sync_file.
220 */
221 if (type == NODE)
222 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700223 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800224submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800225 if (is_read_io(bio_op(bio)))
226 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
227 else
228 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500229 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700230}
231
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900232static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900233{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900234 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900235
236 if (!io->bio)
237 return;
238
Mike Christie04d328d2016-06-05 14:31:55 -0500239 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
240
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800241 if (is_read_io(fio->op))
242 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
243 else
244 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
245
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700246 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900247 io->bio = NULL;
248}
249
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800250static bool __has_merged_page(struct f2fs_bio_info *io,
251 struct inode *inode, nid_t ino, pgoff_t idx)
Chao Yu0fd785e2016-01-18 18:24:59 +0800252{
Chao Yu0fd785e2016-01-18 18:24:59 +0800253 struct bio_vec *bvec;
254 struct page *target;
255 int i;
256
Chao Yu0c3a5792016-01-18 18:28:11 +0800257 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800258 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800259
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800260 if (!inode && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800261 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800262
263 bio_for_each_segment_all(bvec, io->bio, i) {
264
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700265 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800266 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700267 else
268 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800269
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800270 if (idx != target->index)
271 continue;
272
Chao Yu0c3a5792016-01-18 18:28:11 +0800273 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800274 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800275 if (ino && ino == ino_of_node(target))
276 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800277 }
278
Chao Yu0fd785e2016-01-18 18:24:59 +0800279 return false;
280}
281
Chao Yu0c3a5792016-01-18 18:28:11 +0800282static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800283 nid_t ino, pgoff_t idx, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800284{
285 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700286 enum temp_type temp;
287 struct f2fs_bio_info *io;
288 bool ret = false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800289
Jaegeuk Kima912b542017-05-10 11:18:25 -0700290 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
291 io = sbi->write_io[btype] + temp;
292
293 down_read(&io->io_rwsem);
294 ret = __has_merged_page(io, inode, ino, idx);
295 up_read(&io->io_rwsem);
296
297 /* TODO: use HOT temp only for meta pages now. */
298 if (ret || btype == META)
299 break;
300 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800301 return ret;
302}
303
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700304static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700305 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900306{
307 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700308 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900309
Chao Yudf0f8dc2014-03-22 14:57:23 +0800310 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900311
312 /* change META to META_FLUSH in the checkpoint procedure */
313 if (type >= META_FLUSH) {
314 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500315 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200316 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600317 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800318 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900319 }
320 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800321 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900322}
323
Jaegeuk Kima912b542017-05-10 11:18:25 -0700324static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800325 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700326 enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800327{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700328 enum temp_type temp;
329
330 if (!force && !has_merged_page(sbi, inode, ino, idx, type))
331 return;
332
333 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
334
335 __f2fs_submit_merged_write(sbi, type, temp);
336
337 /* TODO: use HOT temp only for meta pages now. */
338 if (type >= META)
339 break;
340 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800341}
342
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700343void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800344{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700345 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900346}
347
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700348void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900349 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700350 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900351{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700352 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900353}
354
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700355void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800356{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700357 f2fs_submit_merged_write(sbi, DATA);
358 f2fs_submit_merged_write(sbi, NODE);
359 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800360}
361
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900362/*
363 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300364 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900365 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700366int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900367{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900368 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700369 struct page *page = fio->encrypted_page ?
370 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900371
Chao Yu2ace38e2014-12-24 16:08:14 +0800372 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700373 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900374
375 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500376 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900377
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300378 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900379 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900380 return -EFAULT;
381 }
Mike Christie04d328d2016-06-05 14:31:55 -0500382 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900383
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700384 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -0700385
386 if (!is_read_io(fio->op))
387 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900388 return 0;
389}
390
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700391int f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900392{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700393 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900394 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700395 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700396 struct page *bio_page;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800397 int err = 0;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900398
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700399 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900400
Chao Yufb830fc2017-05-19 23:37:01 +0800401 down_write(&io->io_rwsem);
402next:
403 if (fio->in_list) {
404 spin_lock(&io->io_lock);
405 if (list_empty(&io->io_list)) {
406 spin_unlock(&io->io_lock);
407 goto out_fail;
408 }
409 fio = list_first_entry(&io->io_list,
410 struct f2fs_io_info, list);
411 list_del(&fio->list);
412 spin_unlock(&io->io_lock);
413 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900414
Chao Yu7a9d7542016-02-22 18:36:38 +0800415 if (fio->old_blkaddr != NEW_ADDR)
416 verify_block_addr(sbi, fio->old_blkaddr);
417 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900418
Chao Yu36951b32016-11-16 10:41:20 +0800419 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
420
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800421 /* set submitted = 1 as a return value */
422 fio->submitted = 1;
423
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700424 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900425
Chao Yu7a9d7542016-02-22 18:36:38 +0800426 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700427 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
428 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900429 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900430alloc_new:
431 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800432 if ((fio->type == DATA || fio->type == NODE) &&
433 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
434 err = -EAGAIN;
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700435 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800436 goto out_fail;
437 }
Chao Yu7a9d7542016-02-22 18:36:38 +0800438 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700439 BIO_MAX_PAGES, false);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900440 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900441 }
442
Jaegeuk Kima912b542017-05-10 11:18:25 -0700443 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900444 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900445 goto alloc_new;
446 }
447
Chao Yu7a9d7542016-02-22 18:36:38 +0800448 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700449 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800450
451 trace_f2fs_submit_page_write(fio->page, fio);
452
453 if (fio->in_list)
454 goto next;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800455out_fail:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800456 up_write(&io->io_rwsem);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800457 return err;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900458}
459
Chao Yu46008c62016-05-09 19:56:30 +0800460static void __set_data_blkaddr(struct dnode_of_data *dn)
461{
462 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
463 __le32 *addr_array;
464
465 /* Get physical address of data block */
466 addr_array = blkaddr_in_node(rn);
467 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
468}
469
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900470/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900471 * Lock ordering for the change of data block address:
472 * ->data_page
473 * ->node_page
474 * update block addresses in the node page
475 */
Chao Yu216a6202015-03-19 19:23:32 +0800476void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900477{
Chao Yu46008c62016-05-09 19:56:30 +0800478 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
479 __set_data_blkaddr(dn);
480 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800481 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900482}
483
Chao Yuf28b3432016-02-24 17:16:47 +0800484void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
485{
486 dn->data_blkaddr = blkaddr;
487 set_data_blkaddr(dn);
488 f2fs_update_extent_cache(dn);
489}
490
Chao Yu46008c62016-05-09 19:56:30 +0800491/* dn->ofs_in_node will be returned with up-to-date last block pointer */
492int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900493{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700494 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800495 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900496
Chao Yu46008c62016-05-09 19:56:30 +0800497 if (!count)
498 return 0;
499
Jaegeuk Kim91942322016-05-20 10:13:22 -0700500 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900501 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +0800502 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
503 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900504
Chao Yu46008c62016-05-09 19:56:30 +0800505 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
506 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900507
Chao Yu46008c62016-05-09 19:56:30 +0800508 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
509
510 for (; count > 0; dn->ofs_in_node++) {
511 block_t blkaddr =
512 datablock_addr(dn->node_page, dn->ofs_in_node);
513 if (blkaddr == NULL_ADDR) {
514 dn->data_blkaddr = NEW_ADDR;
515 __set_data_blkaddr(dn);
516 count--;
517 }
518 }
519
520 if (set_page_dirty(dn->node_page))
521 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900522 return 0;
523}
524
Chao Yu46008c62016-05-09 19:56:30 +0800525/* Should keep dn->ofs_in_node unchanged */
526int reserve_new_block(struct dnode_of_data *dn)
527{
528 unsigned int ofs_in_node = dn->ofs_in_node;
529 int ret;
530
531 ret = reserve_new_blocks(dn, 1);
532 dn->ofs_in_node = ofs_in_node;
533 return ret;
534}
535
Huajun Lib6009652013-11-10 23:13:18 +0800536int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
537{
538 bool need_put = dn->inode_page ? false : true;
539 int err;
540
541 err = get_dnode_of_data(dn, index, ALLOC_NODE);
542 if (err)
543 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900544
Huajun Lib6009652013-11-10 23:13:18 +0800545 if (dn->data_blkaddr == NULL_ADDR)
546 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900547 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800548 f2fs_put_dnode(dn);
549 return err;
550}
551
Fan Li759af1c2015-08-05 15:52:16 +0800552int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900553{
Hou Pengyange15882b2017-02-23 09:18:05 +0000554 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800555 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800556
Fan Li759af1c2015-08-05 15:52:16 +0800557 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
558 dn->data_blkaddr = ei.blk + index - ei.fofs;
559 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800560 }
561
Fan Li759af1c2015-08-05 15:52:16 +0800562 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900563}
564
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700565struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500566 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900567{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900568 struct address_space *mapping = inode->i_mapping;
569 struct dnode_of_data dn;
570 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +0000571 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900572 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800573 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700574 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800575 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500576 .op = REQ_OP_READ,
577 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700578 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800579 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900580
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700581 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
582 return read_mapping_page(mapping, index, NULL);
583
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700584 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900585 if (!page)
586 return ERR_PTR(-ENOMEM);
587
Chao Yucb3bc9e2015-02-05 18:03:40 +0800588 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
589 dn.data_blkaddr = ei.blk + index - ei.fofs;
590 goto got_it;
591 }
592
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900593 set_new_dnode(&dn, inode, NULL, NULL, 0);
594 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700595 if (err)
596 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900597 f2fs_put_dnode(&dn);
598
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900599 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700600 err = -ENOENT;
601 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900602 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800603got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700604 if (PageUptodate(page)) {
605 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900606 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700607 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900608
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900609 /*
610 * A new dentry page is allocated but not able to be written, since its
611 * new inode page couldn't be allocated due to -ENOSPC.
612 * In such the case, its blkaddr can be remained as NEW_ADDR.
613 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
614 */
615 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300616 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700617 if (!PageUptodate(page))
618 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700619 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900620 return page;
621 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900622
Chao Yu7a9d7542016-02-22 18:36:38 +0800623 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700624 fio.page = page;
625 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900626 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700627 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700628 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700629
630put_err:
631 f2fs_put_page(page, 1);
632 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700633}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900634
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700635struct page *find_data_page(struct inode *inode, pgoff_t index)
636{
637 struct address_space *mapping = inode->i_mapping;
638 struct page *page;
639
640 page = find_get_page(mapping, index);
641 if (page && PageUptodate(page))
642 return page;
643 f2fs_put_page(page, 0);
644
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600645 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700646 if (IS_ERR(page))
647 return page;
648
649 if (PageUptodate(page))
650 return page;
651
652 wait_on_page_locked(page);
653 if (unlikely(!PageUptodate(page))) {
654 f2fs_put_page(page, 0);
655 return ERR_PTR(-EIO);
656 }
657 return page;
658}
659
660/*
661 * If it tries to access a hole, return an error.
662 * Because, the callers, functions in dir.c and GC, should be able to know
663 * whether this page exists or not.
664 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700665struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
666 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700667{
668 struct address_space *mapping = inode->i_mapping;
669 struct page *page;
670repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600671 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700672 if (IS_ERR(page))
673 return page;
674
675 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900676 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900677 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900678 f2fs_put_page(page, 1);
679 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900680 }
Chao Yu1563ac72016-07-03 22:05:12 +0800681 if (unlikely(!PageUptodate(page))) {
682 f2fs_put_page(page, 1);
683 return ERR_PTR(-EIO);
684 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900685 return page;
686}
687
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900688/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900689 * Caller ensures that this data page is never allocated.
690 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900691 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800692 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
693 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800694 * Note that, ipage is set only by make_empty_dir, and if any error occur,
695 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900696 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900697struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900698 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900699{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900700 struct address_space *mapping = inode->i_mapping;
701 struct page *page;
702 struct dnode_of_data dn;
703 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800704
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700705 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800706 if (!page) {
707 /*
708 * before exiting, we should make sure ipage will be released
709 * if any error occur.
710 */
711 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700712 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800713 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900714
Jaegeuk Kima8865372013-12-27 17:04:17 +0900715 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800716 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700717 if (err) {
718 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900719 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900720 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700721 if (!ipage)
722 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900723
724 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700725 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900726
727 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300728 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700729 if (!PageUptodate(page))
730 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900731 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700732 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900733
Jaegeuk Kim76121182016-01-01 22:03:47 -0800734 /* if ipage exists, blkaddr should be NEW_ADDR */
735 f2fs_bug_on(F2FS_I_SB(inode), ipage);
736 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700737 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800738 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900739 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700740got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800741 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700742 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700743 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900744 return page;
745}
746
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900747static int __allocate_data_block(struct dnode_of_data *dn)
748{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700749 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900750 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900751 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700752 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800753 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +0800754 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900755
Jaegeuk Kim91942322016-05-20 10:13:22 -0700756 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900757 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800758
759 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
760 if (dn->data_blkaddr == NEW_ADDR)
761 goto alloc;
762
Chao Yu0abd6752017-07-09 00:13:07 +0800763 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
764 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900765
Chao Yudf6136e2015-03-23 10:33:37 +0800766alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900767 get_node_info(sbi, dn->nid, &ni);
768 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
769
Chao Yudf6136e2015-03-23 10:33:37 +0800770 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +0800771 &sum, CURSEG_WARM_DATA, NULL, false);
Chao Yu216a6202015-03-19 19:23:32 +0800772 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900773
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700774 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800775 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700776 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300777 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700778 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300779 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900780 return 0;
781}
782
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800783static inline bool __force_buffered_io(struct inode *inode, int rw)
784{
785 return ((f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) ||
786 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
787 F2FS_I_SB(inode)->s_ndevs);
788}
789
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800790int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800791{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800792 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800793 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800794 int err = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800795
Jaegeuk Kimdc91de72017-01-13 13:12:29 -0800796 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
797 return 0;
798
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700799 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800800 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
801 if (map.m_len > map.m_lblk)
802 map.m_len -= map.m_lblk;
803 else
804 map.m_len = 0;
805
Chao Yuda859852016-01-26 15:42:58 +0800806 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800807
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800808 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800809 err = f2fs_convert_inline_inode(inode);
810 if (err)
811 return err;
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800812 return f2fs_map_blocks(inode, &map, 1,
813 __force_buffered_io(inode, WRITE) ?
814 F2FS_GET_BLOCK_PRE_AIO :
815 F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800816 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800817 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800818 err = f2fs_convert_inline_inode(inode);
819 if (err)
820 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800821 }
822 if (!f2fs_has_inline_data(inode))
823 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800824 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800825}
826
Yunlei He59c90812017-03-13 20:22:18 +0800827static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
828{
829 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
830 if (lock)
831 down_read(&sbi->node_change);
832 else
833 up_read(&sbi->node_change);
834 } else {
835 if (lock)
836 f2fs_lock_op(sbi);
837 else
838 f2fs_unlock_op(sbi);
839 }
840}
841
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900842/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700843 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
844 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800845 * If original data blocks are allocated, then give them to blockdev.
846 * Otherwise,
847 * a. preallocate requested block addresses
848 * b. do not use extent cache for better performance
849 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900850 */
Chao Yud323d002015-10-27 09:53:45 +0800851int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800852 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900853{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700854 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900855 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800856 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700857 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800858 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900859 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800860 unsigned int ofs_in_node, last_ofs_in_node;
861 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +0000862 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +0800863 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900864
Chao Yudfd02e42016-08-20 15:12:01 +0800865 if (!maxblocks)
866 return 0;
867
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700868 map->m_len = 0;
869 map->m_flags = 0;
870
871 /* it only supports block size == page size */
872 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800873 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900874
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800875 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700876 map->m_pblk = ei.blk + pgofs - ei.fofs;
877 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
878 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900879 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800880 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900881
Chao Yu4fe71e82016-01-26 15:37:38 +0800882next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800883 if (create)
Yunlei He59c90812017-03-13 20:22:18 +0800884 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900885
886 /* When reading holes, we need its node page */
887 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900888 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900889 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800890 if (flag == F2FS_GET_BLOCK_BMAP)
891 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800892 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900893 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800894 if (map->m_next_pgofs)
895 *map->m_next_pgofs =
896 get_next_page_offset(&dn, pgofs);
897 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900898 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900899 }
Chao Yu973163f2015-09-18 16:51:51 +0800900
Chao Yu46008c62016-05-09 19:56:30 +0800901 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +0100902 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800903 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900904
Chao Yu4fe71e82016-01-26 15:37:38 +0800905next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800906 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800907
Fan Li7df3a432015-12-17 13:20:59 +0800908 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
909 if (create) {
910 if (unlikely(f2fs_cp_error(sbi))) {
911 err = -EIO;
912 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800913 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800914 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800915 if (blkaddr == NULL_ADDR) {
916 prealloc++;
917 last_ofs_in_node = dn.ofs_in_node;
918 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800919 } else {
920 err = __allocate_data_block(&dn);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800921 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700922 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800923 }
Fan Li7df3a432015-12-17 13:20:59 +0800924 if (err)
925 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +0800926 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800927 blkaddr = dn.data_blkaddr;
928 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800929 if (flag == F2FS_GET_BLOCK_BMAP) {
930 map->m_pblk = 0;
931 goto sync_out;
932 }
Chao Yuda859852016-01-26 15:42:58 +0800933 if (flag == F2FS_GET_BLOCK_FIEMAP &&
934 blkaddr == NULL_ADDR) {
935 if (map->m_next_pgofs)
936 *map->m_next_pgofs = pgofs + 1;
937 }
Fan Li7df3a432015-12-17 13:20:59 +0800938 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800939 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800940 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900941 }
942 }
Fan Li7df3a432015-12-17 13:20:59 +0800943
Chao Yu46008c62016-05-09 19:56:30 +0800944 if (flag == F2FS_GET_BLOCK_PRE_AIO)
945 goto skip;
946
Chao Yu4fe71e82016-01-26 15:37:38 +0800947 if (map->m_len == 0) {
948 /* preallocated unwritten block should be mapped for fiemap. */
949 if (blkaddr == NEW_ADDR)
950 map->m_flags |= F2FS_MAP_UNWRITTEN;
951 map->m_flags |= F2FS_MAP_MAPPED;
952
953 map->m_pblk = blkaddr;
954 map->m_len = 1;
955 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800956 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800957 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800958 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800959 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800960 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800961 } else {
962 goto sync_out;
963 }
964
Chao Yu46008c62016-05-09 19:56:30 +0800965skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800966 dn.ofs_in_node++;
967 pgofs++;
968
Chao Yu46008c62016-05-09 19:56:30 +0800969 /* preallocate blocks in batch for one dnode page */
970 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
971 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800972
Chao Yu46008c62016-05-09 19:56:30 +0800973 dn.ofs_in_node = ofs_in_node;
974 err = reserve_new_blocks(&dn, prealloc);
975 if (err)
976 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800977
Chao Yu46008c62016-05-09 19:56:30 +0800978 map->m_len += dn.ofs_in_node - ofs_in_node;
979 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
980 err = -ENOSPC;
981 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800982 }
Chao Yu46008c62016-05-09 19:56:30 +0800983 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800984 }
985
Chao Yu46008c62016-05-09 19:56:30 +0800986 if (pgofs >= end)
987 goto sync_out;
988 else if (dn.ofs_in_node < end_offset)
989 goto next_block;
990
Chao Yu46008c62016-05-09 19:56:30 +0800991 f2fs_put_dnode(&dn);
992
993 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +0800994 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800995 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +0800996 }
Chao Yu46008c62016-05-09 19:56:30 +0800997 goto next_dnode;
998
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900999sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001000 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001001unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001002 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001003 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001004 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001005 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001006out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001007 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001008 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001009}
1010
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001011static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001012 struct buffer_head *bh, int create, int flag,
1013 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001014{
1015 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001016 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001017
1018 map.m_lblk = iblock;
1019 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001020 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001021
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001022 err = f2fs_map_blocks(inode, &map, create, flag);
1023 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001024 map_bh(bh, inode->i_sb, map.m_pblk);
1025 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001026 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001027 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001028 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001029}
1030
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001031static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001032 struct buffer_head *bh_result, int create, int flag,
1033 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001034{
Chao Yuda859852016-01-26 15:42:58 +08001035 return __get_data_block(inode, iblock, bh_result, create,
1036 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001037}
1038
Chao Yue2b4e2b2015-08-19 19:11:19 +08001039static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001040 struct buffer_head *bh_result, int create)
1041{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001042 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +08001043 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +08001044}
1045
1046static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1047 struct buffer_head *bh_result, int create)
1048{
Yunlei He179448b2015-12-28 21:48:32 +08001049 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001050 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001051 return -EFBIG;
1052
Chao Yue2b4e2b2015-08-19 19:11:19 +08001053 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +08001054 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001055}
1056
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001057static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1058{
1059 return (offset >> inode->i_blkbits);
1060}
1061
1062static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1063{
1064 return (blk << inode->i_blkbits);
1065}
1066
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001067int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1068 u64 start, u64 len)
1069{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001070 struct buffer_head map_bh;
1071 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001072 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001073 u64 logical = 0, phys = 0, size = 0;
1074 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001075 int ret = 0;
1076
1077 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1078 if (ret)
1079 return ret;
1080
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001081 if (f2fs_has_inline_data(inode)) {
1082 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1083 if (ret != -EAGAIN)
1084 return ret;
1085 }
1086
Al Viro59551022016-01-22 15:40:57 -05001087 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +08001088
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001089 if (logical_to_blk(inode, len) == 0)
1090 len = blk_to_logical(inode, 1);
1091
1092 start_blk = logical_to_blk(inode, start);
1093 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001094
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001095next:
1096 memset(&map_bh, 0, sizeof(struct buffer_head));
1097 map_bh.b_size = len;
1098
Chao Yue2b4e2b2015-08-19 19:11:19 +08001099 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001100 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001101 if (ret)
1102 goto out;
1103
1104 /* HOLE */
1105 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001106 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001107
1108 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1109 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001110 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001111
Fan Li9a950d52015-12-26 18:07:41 +08001112 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001113 }
Fan Li9a950d52015-12-26 18:07:41 +08001114
Chao Yuda5af122016-01-08 20:19:27 +08001115 if (size) {
1116 if (f2fs_encrypted_inode(inode))
1117 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1118
Fan Li9a950d52015-12-26 18:07:41 +08001119 ret = fiemap_fill_next_extent(fieinfo, logical,
1120 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001121 }
Fan Li9a950d52015-12-26 18:07:41 +08001122
1123 if (start_blk > last_blk || ret)
1124 goto out;
1125
1126 logical = blk_to_logical(inode, start_blk);
1127 phys = blk_to_logical(inode, map_bh.b_blocknr);
1128 size = map_bh.b_size;
1129 flags = 0;
1130 if (buffer_unwritten(&map_bh))
1131 flags = FIEMAP_EXTENT_UNWRITTEN;
1132
1133 start_blk += logical_to_blk(inode, size);
1134
1135prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001136 cond_resched();
1137 if (fatal_signal_pending(current))
1138 ret = -EINTR;
1139 else
1140 goto next;
1141out:
1142 if (ret == 1)
1143 ret = 0;
1144
Al Viro59551022016-01-22 15:40:57 -05001145 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001146 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001147}
1148
Wei Yongjun6a7a3ae2016-08-23 15:23:59 +00001149static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
1150 unsigned nr_pages)
Chao Yu78682f72016-07-03 22:05:11 +08001151{
1152 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1153 struct fscrypt_ctx *ctx = NULL;
Chao Yu78682f72016-07-03 22:05:11 +08001154 struct bio *bio;
1155
1156 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1157 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
1158 if (IS_ERR(ctx))
1159 return ERR_CAST(ctx);
1160
1161 /* wait the page to be moved by cleaning */
1162 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
1163 }
1164
1165 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
1166 if (!bio) {
1167 if (ctx)
1168 fscrypt_release_ctx(ctx);
1169 return ERR_PTR(-ENOMEM);
1170 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001171 f2fs_target_device(sbi, blkaddr, bio);
Chao Yu78682f72016-07-03 22:05:11 +08001172 bio->bi_end_io = f2fs_read_end_io;
1173 bio->bi_private = ctx;
1174
1175 return bio;
1176}
1177
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001178/*
1179 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1180 * Major change was from block_size == page_size in f2fs by default.
1181 */
1182static int f2fs_mpage_readpages(struct address_space *mapping,
1183 struct list_head *pages, struct page *page,
1184 unsigned nr_pages)
1185{
1186 struct bio *bio = NULL;
1187 unsigned page_idx;
1188 sector_t last_block_in_bio = 0;
1189 struct inode *inode = mapping->host;
1190 const unsigned blkbits = inode->i_blkbits;
1191 const unsigned blocksize = 1 << blkbits;
1192 sector_t block_in_file;
1193 sector_t last_block;
1194 sector_t last_block_in_file;
1195 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001196 struct f2fs_map_blocks map;
1197
1198 map.m_pblk = 0;
1199 map.m_lblk = 0;
1200 map.m_len = 0;
1201 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001202 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001203
1204 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1205
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001206 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001207 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08001208
1209 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001210 list_del(&page->lru);
1211 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001212 page->index,
1213 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001214 goto next_page;
1215 }
1216
1217 block_in_file = (sector_t)page->index;
1218 last_block = block_in_file + nr_pages;
1219 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1220 blkbits;
1221 if (last_block > last_block_in_file)
1222 last_block = last_block_in_file;
1223
1224 /*
1225 * Map blocks using the previous result first.
1226 */
1227 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1228 block_in_file > map.m_lblk &&
1229 block_in_file < (map.m_lblk + map.m_len))
1230 goto got_it;
1231
1232 /*
1233 * Then do more f2fs_map_blocks() calls until we are
1234 * done with this page.
1235 */
1236 map.m_flags = 0;
1237
1238 if (block_in_file < last_block) {
1239 map.m_lblk = block_in_file;
1240 map.m_len = last_block - block_in_file;
1241
Chao Yu46c9e142015-09-18 16:54:16 +08001242 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001243 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001244 goto set_error_page;
1245 }
1246got_it:
1247 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1248 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1249 SetPageMappedToDisk(page);
1250
1251 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1252 SetPageUptodate(page);
1253 goto confused;
1254 }
1255 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001256 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001257 if (!PageUptodate(page))
1258 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001259 unlock_page(page);
1260 goto next_page;
1261 }
1262
1263 /*
1264 * This page will go to BIO. Do we need to send this
1265 * BIO off first?
1266 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001267 if (bio && (last_block_in_bio != block_nr - 1 ||
1268 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001269submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001270 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001271 bio = NULL;
1272 }
1273 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001274 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001275 if (IS_ERR(bio)) {
1276 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001277 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001278 }
Mike Christie04d328d2016-06-05 14:31:55 -05001279 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001280 }
1281
1282 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1283 goto submit_and_realloc;
1284
1285 last_block_in_bio = block_nr;
1286 goto next_page;
1287set_error_page:
1288 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001289 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001290 unlock_page(page);
1291 goto next_page;
1292confused:
1293 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001294 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001295 bio = NULL;
1296 }
1297 unlock_page(page);
1298next_page:
1299 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001300 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001301 }
1302 BUG_ON(pages && !list_empty(pages));
1303 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001304 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001305 return 0;
1306}
1307
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001308static int f2fs_read_data_page(struct file *file, struct page *page)
1309{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001310 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001311 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001312
Chao Yuc20e89c2014-05-06 16:53:08 +08001313 trace_f2fs_readpage(page, DATA);
1314
arter97e1c42042014-08-06 23:22:50 +09001315 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001316 if (f2fs_has_inline_data(inode))
1317 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001318 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001319 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001320 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001321}
1322
1323static int f2fs_read_data_pages(struct file *file,
1324 struct address_space *mapping,
1325 struct list_head *pages, unsigned nr_pages)
1326{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001327 struct inode *inode = file->f_mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001328 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001329
1330 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001331
1332 /* If the file has inline data, skip readpages */
1333 if (f2fs_has_inline_data(inode))
1334 return 0;
1335
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001336 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001337}
1338
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001339static int encrypt_one_page(struct f2fs_io_info *fio)
1340{
1341 struct inode *inode = fio->page->mapping->host;
1342 gfp_t gfp_flags = GFP_NOFS;
1343
1344 if (!f2fs_encrypted_inode(inode) || !S_ISREG(inode->i_mode))
1345 return 0;
1346
1347 /* wait for GCed encrypted page writeback */
1348 f2fs_wait_on_encrypted_page_writeback(fio->sbi, fio->old_blkaddr);
1349
1350retry_encrypt:
1351 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1352 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1353 if (!IS_ERR(fio->encrypted_page))
1354 return 0;
1355
1356 /* flush pending IOs and wait for a while in the ENOMEM case */
1357 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001358 f2fs_flush_merged_writes(fio->sbi);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001359 congestion_wait(BLK_RW_ASYNC, HZ/50);
1360 gfp_flags |= __GFP_NOFAIL;
1361 goto retry_encrypt;
1362 }
1363 return PTR_ERR(fio->encrypted_page);
1364}
1365
1366static inline bool need_inplace_update(struct f2fs_io_info *fio)
1367{
1368 struct inode *inode = fio->page->mapping->host;
1369
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001370 if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode))
1371 return false;
1372 if (is_cold_data(fio->page))
1373 return false;
1374 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1375 return false;
1376
1377 return need_inplace_update_policy(inode, fio);
1378}
1379
Jaegeuk Kima8177372017-04-24 15:20:16 -07001380static inline bool valid_ipu_blkaddr(struct f2fs_io_info *fio)
1381{
1382 if (fio->old_blkaddr == NEW_ADDR)
1383 return false;
1384 if (fio->old_blkaddr == NULL_ADDR)
1385 return false;
1386 return true;
1387}
1388
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001389int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001390{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001391 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001392 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001393 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001394 struct extent_info ei = {0,0,0};
1395 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001396 int err = 0;
1397
1398 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00001399 if (need_inplace_update(fio) &&
1400 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1401 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07001402
1403 if (valid_ipu_blkaddr(fio)) {
Hou Pengyange959c8f2017-04-25 12:45:13 +00001404 ipu_force = true;
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001405 fio->need_lock = LOCK_DONE;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001406 goto got_it;
1407 }
1408 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001409
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001410 /* Deadlock due to between page->lock and f2fs_lock_op */
1411 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1412 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001413
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001414 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001415 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001416 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001417
Chao Yu28bc1062016-02-06 14:40:34 +08001418 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001419
1420 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001421 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001422 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001423 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001424 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00001425got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001426 /*
1427 * If current allocation needs SSR,
1428 * it had better in-place writes for updated data.
1429 */
Jaegeuk Kima8177372017-04-24 15:20:16 -07001430 if (ipu_force || (valid_ipu_blkaddr(fio) && need_inplace_update(fio))) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001431 err = encrypt_one_page(fio);
1432 if (err)
1433 goto out_writepage;
1434
1435 set_page_writeback(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001436 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001437 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001438 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -07001439 err = rewrite_data_page(fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001440 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001441 set_inode_flag(inode, FI_UPDATE_WRITE);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001442 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001443 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001444
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001445 if (fio->need_lock == LOCK_RETRY) {
1446 if (!f2fs_trylock_op(fio->sbi)) {
1447 err = -EAGAIN;
1448 goto out_writepage;
1449 }
1450 fio->need_lock = LOCK_REQ;
1451 }
1452
1453 err = encrypt_one_page(fio);
1454 if (err)
1455 goto out_writepage;
1456
1457 set_page_writeback(page);
1458
Hou Pengyang279d6df2017-04-27 00:17:21 +08001459 /* LFS mode write path */
1460 write_data_page(&dn, fio);
1461 trace_f2fs_do_write_data_page(page, OPU);
1462 set_inode_flag(inode, FI_APPEND_WRITE);
1463 if (page->index == 0)
1464 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001465out_writepage:
1466 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001467out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001468 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001469 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001470 return err;
1471}
1472
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001473static int __write_data_page(struct page *page, bool *submitted,
1474 struct writeback_control *wbc)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001475{
1476 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001477 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001478 loff_t i_size = i_size_read(inode);
1479 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001480 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001481 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001482 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001483 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001484 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001485 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001486 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001487 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001488 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001489 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00001490 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001491 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001492 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001493 .submitted = false,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001494 .need_lock = LOCK_RETRY,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001495 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001496
Chao Yuecda0de2014-05-06 16:48:26 +08001497 trace_f2fs_writepage(page, DATA);
1498
Chao Yu0771fcc2017-06-29 23:20:45 +08001499 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1500 goto redirty_out;
1501
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001502 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001503 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001504
1505 /*
1506 * If the offset is out-of-range of file size,
1507 * this page does not have to be written to disk.
1508 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001509 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001510 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001511 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001512
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001513 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001514write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001515 if (f2fs_is_drop_cache(inode))
1516 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001517 /* we should not write 0'th page having journal header */
1518 if (f2fs_is_volatile_file(inode) && (!page->index ||
1519 (!wbc->for_reclaim &&
1520 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001521 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001522
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001523 /* we should bypass data pages to proceed the kworkder jobs */
1524 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001525 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001526 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001527 }
1528
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001529 /* Dentry blocks are controlled by checkpoint */
1530 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001531 fio.need_lock = LOCK_DONE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001532 err = do_write_data_page(&fio);
1533 goto done;
1534 }
1535
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001536 if (!wbc->for_reclaim)
1537 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001538 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001539 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001540 else
1541 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001542
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001543 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08001544 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001545 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08001546 if (!err)
1547 goto out;
1548 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001549
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001550 if (err == -EAGAIN) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001551 err = do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001552 if (err == -EAGAIN) {
1553 fio.need_lock = LOCK_REQ;
1554 err = do_write_data_page(&fio);
1555 }
1556 }
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001557 if (F2FS_I(inode)->last_disk_size < psize)
1558 F2FS_I(inode)->last_disk_size = psize;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001559
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001560done:
1561 if (err && err != -ENOENT)
1562 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001563
Jaegeuk Kim39936832012-11-22 16:21:29 +09001564out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001565 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001566 if (err)
1567 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001568
1569 if (wbc->for_reclaim) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001570 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001571 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yuc227f912015-12-16 13:09:20 +08001572 remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001573 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001574 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001575
1576 unlock_page(page);
Jaegeuk Kima7881892017-04-20 13:51:57 -07001577 if (!S_ISDIR(inode->i_mode))
1578 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001579
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001580 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001581 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001582 submitted = NULL;
1583 }
1584
1585 if (submitted)
1586 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001587
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001588 return 0;
1589
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001590redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001591 redirty_page_for_writepage(wbc, page);
Chao Yu0002b612016-11-28 19:13:43 -08001592 if (!err)
1593 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001594 unlock_page(page);
1595 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001596}
1597
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001598static int f2fs_write_data_page(struct page *page,
1599 struct writeback_control *wbc)
1600{
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001601 return __write_data_page(page, NULL, wbc);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001602}
1603
Chao Yu8f46dca2015-07-14 18:56:10 +08001604/*
1605 * This function was copied from write_cche_pages from mm/page-writeback.c.
1606 * The major change is making write step of cold data page separately from
1607 * warm/hot data page.
1608 */
1609static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001610 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001611{
1612 int ret = 0;
1613 int done = 0;
1614 struct pagevec pvec;
1615 int nr_pages;
1616 pgoff_t uninitialized_var(writeback_index);
1617 pgoff_t index;
1618 pgoff_t end; /* Inclusive */
1619 pgoff_t done_index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001620 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001621 int cycled;
1622 int range_whole = 0;
1623 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001624
1625 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001626
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001627 if (get_dirty_pages(mapping->host) <=
1628 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1629 set_inode_flag(mapping->host, FI_HOT_DATA);
1630 else
1631 clear_inode_flag(mapping->host, FI_HOT_DATA);
1632
Chao Yu8f46dca2015-07-14 18:56:10 +08001633 if (wbc->range_cyclic) {
1634 writeback_index = mapping->writeback_index; /* prev offset */
1635 index = writeback_index;
1636 if (index == 0)
1637 cycled = 1;
1638 else
1639 cycled = 0;
1640 end = -1;
1641 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001642 index = wbc->range_start >> PAGE_SHIFT;
1643 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001644 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1645 range_whole = 1;
1646 cycled = 1; /* ignore range_cyclic tests */
1647 }
1648 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1649 tag = PAGECACHE_TAG_TOWRITE;
1650 else
1651 tag = PAGECACHE_TAG_DIRTY;
1652retry:
1653 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1654 tag_pages_for_writeback(mapping, index, end);
1655 done_index = index;
1656 while (!done && (index <= end)) {
1657 int i;
1658
1659 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1660 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1661 if (nr_pages == 0)
1662 break;
1663
1664 for (i = 0; i < nr_pages; i++) {
1665 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001666 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001667
1668 if (page->index > end) {
1669 done = 1;
1670 break;
1671 }
1672
1673 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001674retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08001675 lock_page(page);
1676
1677 if (unlikely(page->mapping != mapping)) {
1678continue_unlock:
1679 unlock_page(page);
1680 continue;
1681 }
1682
1683 if (!PageDirty(page)) {
1684 /* someone wrote it for us */
1685 goto continue_unlock;
1686 }
1687
Chao Yu8f46dca2015-07-14 18:56:10 +08001688 if (PageWriteback(page)) {
1689 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001690 f2fs_wait_on_page_writeback(page,
1691 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001692 else
1693 goto continue_unlock;
1694 }
1695
1696 BUG_ON(PageWriteback(page));
1697 if (!clear_page_dirty_for_io(page))
1698 goto continue_unlock;
1699
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001700 ret = __write_data_page(page, &submitted, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001701 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08001702 /*
1703 * keep nr_to_write, since vfs uses this to
1704 * get # of written pages.
1705 */
1706 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1707 unlock_page(page);
1708 ret = 0;
1709 continue;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001710 } else if (ret == -EAGAIN) {
1711 ret = 0;
1712 if (wbc->sync_mode == WB_SYNC_ALL) {
1713 cond_resched();
1714 congestion_wait(BLK_RW_ASYNC,
1715 HZ/50);
1716 goto retry_write;
1717 }
1718 continue;
Chao Yu0002b612016-11-28 19:13:43 -08001719 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001720 done_index = page->index + 1;
1721 done = 1;
1722 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001723 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001724 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08001725 }
1726
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001727 /* give a priority to WB_SYNC threads */
1728 if ((atomic_read(&F2FS_M_SB(mapping)->wb_sync_req) ||
1729 --wbc->nr_to_write <= 0) &&
1730 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08001731 done = 1;
1732 break;
1733 }
1734 }
1735 pagevec_release(&pvec);
1736 cond_resched();
1737 }
1738
Chao Yu8f46dca2015-07-14 18:56:10 +08001739 if (!cycled && !done) {
1740 cycled = 1;
1741 index = 0;
1742 end = writeback_index - 1;
1743 goto retry;
1744 }
1745 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1746 mapping->writeback_index = done_index;
1747
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001748 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001749 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
1750 0, last_idx, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08001751
Chao Yu8f46dca2015-07-14 18:56:10 +08001752 return ret;
1753}
1754
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001755static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001756 struct writeback_control *wbc)
1757{
1758 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001759 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001760 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001761 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001762
P J Pcfb185a2013-04-03 11:38:00 +09001763 /* deal with chardevs and other special file */
1764 if (!mapping->a_ops->writepage)
1765 return 0;
1766
Chao Yu6a290542015-07-17 18:02:39 +08001767 /* skip writing if there is no dirty page in this inode */
1768 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1769 return 0;
1770
Chao Yu0771fcc2017-06-29 23:20:45 +08001771 /* during POR, we don't need to trigger writepage at all. */
1772 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1773 goto skip_write;
1774
Jaegeuk Kima1257022015-10-08 10:40:07 -07001775 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1776 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1777 available_free_memory(sbi, DIRTY_DENTS))
1778 goto skip_write;
1779
Chao Yud323d002015-10-27 09:53:45 +08001780 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001781 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001782 goto skip_write;
1783
Yunlei Hed31c7c32016-02-04 16:14:00 +08001784 trace_f2fs_writepages(mapping->host, wbc, DATA);
1785
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001786 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
1787 if (wbc->sync_mode == WB_SYNC_ALL)
1788 atomic_inc(&sbi->wb_sync_req);
1789 else if (atomic_read(&sbi->wb_sync_req))
1790 goto skip_write;
1791
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001792 blk_start_plug(&plug);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001793 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001794 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001795
1796 if (wbc->sync_mode == WB_SYNC_ALL)
1797 atomic_dec(&sbi->wb_sync_req);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001798 /*
1799 * if some pages were truncated, we cannot guarantee its mapping->host
1800 * to detect pending bios.
1801 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001802
Chao Yuc227f912015-12-16 13:09:20 +08001803 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001804 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001805
1806skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001807 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001808 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001809 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001810}
1811
Chao Yu3aab8f82014-07-02 13:25:04 +08001812static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1813{
1814 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001815 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001816
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001817 if (to > i_size) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001818 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001819 truncate_pagecache(inode, i_size);
1820 truncate_blocks(inode, i_size, true);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001821 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3aab8f82014-07-02 13:25:04 +08001822 }
1823}
1824
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001825static int prepare_write_begin(struct f2fs_sb_info *sbi,
1826 struct page *page, loff_t pos, unsigned len,
1827 block_t *blk_addr, bool *node_changed)
1828{
1829 struct inode *inode = page->mapping->host;
1830 pgoff_t index = page->index;
1831 struct dnode_of_data dn;
1832 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001833 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00001834 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001835 int err = 0;
1836
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001837 /*
1838 * we already allocated all the blocks, so we don't need to get
1839 * the block addresses when there is no need to fill the page.
1840 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08001841 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
1842 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001843 return 0;
1844
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001845 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001846 (pos & PAGE_MASK) >= i_size_read(inode)) {
Yunlei He59c90812017-03-13 20:22:18 +08001847 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001848 locked = true;
1849 }
1850restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001851 /* check inline_data */
1852 ipage = get_node_page(sbi, inode->i_ino);
1853 if (IS_ERR(ipage)) {
1854 err = PTR_ERR(ipage);
1855 goto unlock_out;
1856 }
1857
1858 set_new_dnode(&dn, inode, ipage, ipage, 0);
1859
1860 if (f2fs_has_inline_data(inode)) {
1861 if (pos + len <= MAX_INLINE_DATA) {
1862 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001863 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001864 if (inode->i_nlink)
1865 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001866 } else {
1867 err = f2fs_convert_inline_page(&dn, page);
1868 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001869 goto out;
1870 if (dn.data_blkaddr == NULL_ADDR)
1871 err = f2fs_get_block(&dn, index);
1872 }
1873 } else if (locked) {
1874 err = f2fs_get_block(&dn, index);
1875 } else {
1876 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1877 dn.data_blkaddr = ei.blk + index - ei.fofs;
1878 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001879 /* hole case */
1880 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001881 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001882 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08001883 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
1884 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001885 locked = true;
1886 goto restart;
1887 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001888 }
1889 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001890
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001891 /* convert_inline_page can make node_changed */
1892 *blk_addr = dn.data_blkaddr;
1893 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001894out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001895 f2fs_put_dnode(&dn);
1896unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001897 if (locked)
Yunlei He59c90812017-03-13 20:22:18 +08001898 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001899 return err;
1900}
1901
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001902static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1903 loff_t pos, unsigned len, unsigned flags,
1904 struct page **pagep, void **fsdata)
1905{
1906 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001907 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001908 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001909 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001910 bool need_balance = false;
1911 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001912 int err = 0;
1913
Chao Yu62aed042014-05-06 16:46:04 +08001914 trace_f2fs_write_begin(inode, pos, len, flags);
1915
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001916 /*
1917 * We should check this at this moment to avoid deadlock on inode page
1918 * and #0 page. The locking rule for inline_data conversion should be:
1919 * lock_page(page #0) -> lock_page(inode_page)
1920 */
1921 if (index != 0) {
1922 err = f2fs_convert_inline_inode(inode);
1923 if (err)
1924 goto fail;
1925 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001926repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08001927 /*
1928 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1929 * wait_for_stable_page. Will wait that below with our IO control.
1930 */
1931 page = pagecache_get_page(mapping, index,
1932 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08001933 if (!page) {
1934 err = -ENOMEM;
1935 goto fail;
1936 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001937
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001938 *pagep = page;
1939
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001940 err = prepare_write_begin(sbi, page, pos, len,
1941 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001942 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001943 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001944
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001945 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001946 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001947 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001948 lock_page(page);
1949 if (page->mapping != mapping) {
1950 /* The page got truncated from under us */
1951 f2fs_put_page(page, 1);
1952 goto repeat;
1953 }
1954 }
1955
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001956 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001957
Chao Yu08b39fb2015-10-08 13:27:34 +08001958 /* wait for GCed encrypted page writeback */
1959 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001960 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001961
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001962 if (len == PAGE_SIZE || PageUptodate(page))
1963 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001964
Yunlei He746e2402016-12-20 11:11:35 +08001965 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
1966 zero_user_segment(page, len, PAGE_SIZE);
1967 return 0;
1968 }
1969
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001970 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001971 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001972 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001973 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001974 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001975
Chao Yu78682f72016-07-03 22:05:11 +08001976 bio = f2fs_grab_bio(inode, blkaddr, 1);
1977 if (IS_ERR(bio)) {
1978 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001979 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001980 }
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001981 bio->bi_opf = REQ_OP_READ;
Chao Yu78682f72016-07-03 22:05:11 +08001982 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1983 bio_put(bio);
1984 err = -EFAULT;
1985 goto fail;
1986 }
1987
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001988 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001989
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001990 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001991 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001992 f2fs_put_page(page, 1);
1993 goto repeat;
1994 }
Chao Yu1563ac72016-07-03 22:05:12 +08001995 if (unlikely(!PageUptodate(page))) {
1996 err = -EIO;
1997 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001998 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001999 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002000 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002001
Chao Yu3aab8f82014-07-02 13:25:04 +08002002fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002003 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002004 f2fs_write_failed(mapping, pos + len);
2005 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002006}
2007
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002008static int f2fs_write_end(struct file *file,
2009 struct address_space *mapping,
2010 loff_t pos, unsigned len, unsigned copied,
2011 struct page *page, void *fsdata)
2012{
2013 struct inode *inode = page->mapping->host;
2014
Chao Yudfb2bf32014-05-06 16:47:23 +08002015 trace_f2fs_write_end(inode, pos, len, copied);
2016
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002017 /*
2018 * This should be come from len == PAGE_SIZE, and we expect copied
2019 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2020 * let generic_perform_write() try to copy data again through copied=0.
2021 */
2022 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08002023 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002024 copied = 0;
2025 else
2026 SetPageUptodate(page);
2027 }
2028 if (!copied)
2029 goto unlock_out;
2030
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002031 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002032
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002033 if (pos + copied > i_size_read(inode))
2034 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002035unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002036 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002037 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002038 return copied;
2039}
2040
Omar Sandoval6f673762015-03-16 04:33:52 -07002041static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2042 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002043{
2044 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002045
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002046 if (offset & blocksize_mask)
2047 return -EINVAL;
2048
Al Viro5b46f252014-03-16 18:07:34 -04002049 if (iov_iter_alignment(iter) & blocksize_mask)
2050 return -EINVAL;
2051
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002052 return 0;
2053}
2054
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002055static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002056{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002057 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002058 struct inode *inode = mapping->host;
2059 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002060 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002061 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002062 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002063
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002064 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002065 if (err)
2066 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002067
Jaegeuk Kimc040ff92016-11-11 16:46:40 -08002068 if (__force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002069 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002070
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002071 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002072
Chao Yu82e0a5a2016-07-13 09:18:29 +08002073 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002074 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002075 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
2076
2077 if (rw == WRITE) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002078 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07002079 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002080 else if (err < 0)
2081 f2fs_write_failed(mapping, offset + count);
2082 }
Chao Yu70407fa2014-07-31 21:11:22 +08002083
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002084 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002085
Chao Yu3aab8f82014-07-02 13:25:04 +08002086 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002087}
2088
Chao Yu487261f2015-02-05 17:44:29 +08002089void f2fs_invalidate_page(struct page *page, unsigned int offset,
2090 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002091{
2092 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002093 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002094
Chao Yu487261f2015-02-05 17:44:29 +08002095 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002096 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002097 return;
2098
Chao Yu487261f2015-02-05 17:44:29 +08002099 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08002100 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002101 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08002102 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002103 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08002104 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002105 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08002106 remove_dirty_inode(inode);
2107 }
Chao Yu487261f2015-02-05 17:44:29 +08002108 }
Chao Yudecd36b2015-08-07 18:42:09 +08002109
2110 /* This is atomic written page, keep Private */
2111 if (IS_ATOMIC_WRITTEN_PAGE(page))
Jaegeuk Kim8c242db2017-03-17 09:55:52 +08002112 return drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002113
Chao Yu23dc9742016-04-29 20:09:15 +08002114 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002115 ClearPagePrivate(page);
2116}
2117
Chao Yu487261f2015-02-05 17:44:29 +08002118int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002119{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002120 /* If this is dirty page, keep PagePrivate */
2121 if (PageDirty(page))
2122 return 0;
2123
Chao Yudecd36b2015-08-07 18:42:09 +08002124 /* This is atomic written page, keep Private */
2125 if (IS_ATOMIC_WRITTEN_PAGE(page))
2126 return 0;
2127
Chao Yu23dc9742016-04-29 20:09:15 +08002128 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002129 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002130 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002131}
2132
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002133/*
2134 * This was copied from __set_page_dirty_buffers which gives higher performance
2135 * in very high speed storages. (e.g., pmem)
2136 */
2137void f2fs_set_page_dirty_nobuffers(struct page *page)
2138{
2139 struct address_space *mapping = page->mapping;
2140 unsigned long flags;
2141
2142 if (unlikely(!mapping))
2143 return;
2144
2145 spin_lock(&mapping->private_lock);
2146 lock_page_memcg(page);
2147 SetPageDirty(page);
2148 spin_unlock(&mapping->private_lock);
2149
2150 spin_lock_irqsave(&mapping->tree_lock, flags);
2151 WARN_ON_ONCE(!PageUptodate(page));
2152 account_page_dirtied(page, mapping);
2153 radix_tree_tag_set(&mapping->page_tree,
2154 page_index(page), PAGECACHE_TAG_DIRTY);
2155 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2156 unlock_page_memcg(page);
2157
2158 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
2159 return;
2160}
2161
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002162static int f2fs_set_data_page_dirty(struct page *page)
2163{
2164 struct address_space *mapping = page->mapping;
2165 struct inode *inode = mapping->host;
2166
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002167 trace_f2fs_set_page_dirty(page, DATA);
2168
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002169 if (!PageUptodate(page))
2170 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002171
Chao Yu5fe45742017-01-07 18:50:26 +08002172 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002173 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2174 register_inmem_page(inode, page);
2175 return 1;
2176 }
2177 /*
2178 * Previously, this page has been registered, we just
2179 * return here.
2180 */
2181 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002182 }
2183
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002184 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002185 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002186 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002187 return 1;
2188 }
2189 return 0;
2190}
2191
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002192static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2193{
Chao Yu454ae7e2014-04-22 13:34:01 +08002194 struct inode *inode = mapping->host;
2195
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002196 if (f2fs_has_inline_data(inode))
2197 return 0;
2198
2199 /* make sure allocating whole blocks */
2200 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2201 filemap_write_and_wait(mapping);
2202
Chao Yue2b4e2b2015-08-19 19:11:19 +08002203 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002204}
2205
Weichao Guo5b7a4872016-09-20 05:03:27 +08002206#ifdef CONFIG_MIGRATION
2207#include <linux/migrate.h>
2208
2209int f2fs_migrate_page(struct address_space *mapping,
2210 struct page *newpage, struct page *page, enum migrate_mode mode)
2211{
2212 int rc, extra_count;
2213 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2214 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2215
2216 BUG_ON(PageWriteback(page));
2217
2218 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07002219 if (atomic_written) {
2220 if (mode != MIGRATE_SYNC)
2221 return -EBUSY;
2222 if (!mutex_trylock(&fi->inmem_lock))
2223 return -EAGAIN;
2224 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002225
2226 /*
2227 * A reference is expected if PagePrivate set when move mapping,
2228 * however F2FS breaks this for maintaining dirty page counts when
2229 * truncating pages. So here adjusting the 'extra_count' make it work.
2230 */
2231 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2232 rc = migrate_page_move_mapping(mapping, newpage,
2233 page, NULL, mode, extra_count);
2234 if (rc != MIGRATEPAGE_SUCCESS) {
2235 if (atomic_written)
2236 mutex_unlock(&fi->inmem_lock);
2237 return rc;
2238 }
2239
2240 if (atomic_written) {
2241 struct inmem_pages *cur;
2242 list_for_each_entry(cur, &fi->inmem_pages, list)
2243 if (cur->page == page) {
2244 cur->page = newpage;
2245 break;
2246 }
2247 mutex_unlock(&fi->inmem_lock);
2248 put_page(page);
2249 get_page(newpage);
2250 }
2251
2252 if (PagePrivate(page))
2253 SetPagePrivate(newpage);
2254 set_page_private(newpage, page_private(page));
2255
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07002256 if (mode != MIGRATE_SYNC_NO_COPY)
2257 migrate_page_copy(newpage, page);
2258 else
2259 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002260
2261 return MIGRATEPAGE_SUCCESS;
2262}
2263#endif
2264
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002265const struct address_space_operations f2fs_dblock_aops = {
2266 .readpage = f2fs_read_data_page,
2267 .readpages = f2fs_read_data_pages,
2268 .writepage = f2fs_write_data_page,
2269 .writepages = f2fs_write_data_pages,
2270 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002271 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002272 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002273 .invalidatepage = f2fs_invalidate_page,
2274 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002275 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002276 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002277#ifdef CONFIG_MIGRATION
2278 .migratepage = f2fs_migrate_page,
2279#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002280};