blob: 87c1f4150c645c83c64cd7f90243d347121f8886 [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) {
145 bio->bi_bdev = bdev;
146 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{
164 return f2fs_target_device(sbi, blk_addr, NULL) == bio->bi_bdev;
165}
166
167/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800168 * Low-level block read/write IO operations.
169 */
170static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
171 int npages, bool is_read)
172{
173 struct bio *bio;
174
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700175 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +0800176
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700177 f2fs_target_device(sbi, blk_addr, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800178 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +0800179 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800180
181 return bio;
182}
183
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700184static inline void __submit_bio(struct f2fs_sb_info *sbi,
185 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700186{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700187 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800188 unsigned int start;
189
Damien Le Moal0bfd7a02016-10-28 17:45:00 +0900190 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700191 current->plug && (type == DATA || type == NODE))
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700192 blk_finish_plug(current->plug);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800193
194 if (type != DATA && type != NODE)
195 goto submit_io;
196
197 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
198 start %= F2FS_IO_SIZE(sbi);
199
200 if (start == 0)
201 goto submit_io;
202
203 /* fill dummy pages */
204 for (; start < F2FS_IO_SIZE(sbi); start++) {
205 struct page *page =
206 mempool_alloc(sbi->write_io_dummy,
207 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
208 f2fs_bug_on(sbi, !page);
209
210 SetPagePrivate(page);
211 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
212 lock_page(page);
213 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
214 f2fs_bug_on(sbi, 1);
215 }
216 /*
217 * In the NODE case, we lose next block address chain. So, we
218 * need to do checkpoint in f2fs_sync_file.
219 */
220 if (type == NODE)
221 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700222 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800223submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800224 if (is_read_io(bio_op(bio)))
225 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
226 else
227 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500228 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700229}
230
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900231static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900232{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900233 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900234
235 if (!io->bio)
236 return;
237
Mike Christie04d328d2016-06-05 14:31:55 -0500238 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
239
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800240 if (is_read_io(fio->op))
241 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
242 else
243 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
244
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700245 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900246 io->bio = NULL;
247}
248
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800249static bool __has_merged_page(struct f2fs_bio_info *io,
250 struct inode *inode, nid_t ino, pgoff_t idx)
Chao Yu0fd785e2016-01-18 18:24:59 +0800251{
Chao Yu0fd785e2016-01-18 18:24:59 +0800252 struct bio_vec *bvec;
253 struct page *target;
254 int i;
255
Chao Yu0c3a5792016-01-18 18:28:11 +0800256 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800257 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800258
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800259 if (!inode && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800260 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800261
262 bio_for_each_segment_all(bvec, io->bio, i) {
263
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700264 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800265 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700266 else
267 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800268
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800269 if (idx != target->index)
270 continue;
271
Chao Yu0c3a5792016-01-18 18:28:11 +0800272 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800273 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800274 if (ino && ino == ino_of_node(target))
275 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800276 }
277
Chao Yu0fd785e2016-01-18 18:24:59 +0800278 return false;
279}
280
Chao Yu0c3a5792016-01-18 18:28:11 +0800281static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800282 nid_t ino, pgoff_t idx, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800283{
284 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700285 enum temp_type temp;
286 struct f2fs_bio_info *io;
287 bool ret = false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800288
Jaegeuk Kima912b542017-05-10 11:18:25 -0700289 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
290 io = sbi->write_io[btype] + temp;
291
292 down_read(&io->io_rwsem);
293 ret = __has_merged_page(io, inode, ino, idx);
294 up_read(&io->io_rwsem);
295
296 /* TODO: use HOT temp only for meta pages now. */
297 if (ret || btype == META)
298 break;
299 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800300 return ret;
301}
302
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700303static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700304 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900305{
306 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700307 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900308
Chao Yudf0f8dc2014-03-22 14:57:23 +0800309 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900310
311 /* change META to META_FLUSH in the checkpoint procedure */
312 if (type >= META_FLUSH) {
313 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500314 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200315 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600316 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800317 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900318 }
319 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800320 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900321}
322
Jaegeuk Kima912b542017-05-10 11:18:25 -0700323static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800324 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700325 enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800326{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700327 enum temp_type temp;
328
329 if (!force && !has_merged_page(sbi, inode, ino, idx, type))
330 return;
331
332 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
333
334 __f2fs_submit_merged_write(sbi, type, temp);
335
336 /* TODO: use HOT temp only for meta pages now. */
337 if (type >= META)
338 break;
339 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800340}
341
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700342void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800343{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700344 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900345}
346
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700347void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900348 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700349 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900350{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700351 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900352}
353
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700354void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800355{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700356 f2fs_submit_merged_write(sbi, DATA);
357 f2fs_submit_merged_write(sbi, NODE);
358 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800359}
360
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900361/*
362 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300363 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900364 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700365int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900366{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900367 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700368 struct page *page = fio->encrypted_page ?
369 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900370
Chao Yu2ace38e2014-12-24 16:08:14 +0800371 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700372 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900373
374 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500375 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900376
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300377 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900378 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900379 return -EFAULT;
380 }
Mike Christie04d328d2016-06-05 14:31:55 -0500381 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900382
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700383 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -0700384
385 if (!is_read_io(fio->op))
386 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900387 return 0;
388}
389
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700390int f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900391{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700392 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900393 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700394 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700395 struct page *bio_page;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800396 int err = 0;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900397
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700398 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900399
Chao Yufb830fc2017-05-19 23:37:01 +0800400 down_write(&io->io_rwsem);
401next:
402 if (fio->in_list) {
403 spin_lock(&io->io_lock);
404 if (list_empty(&io->io_list)) {
405 spin_unlock(&io->io_lock);
406 goto out_fail;
407 }
408 fio = list_first_entry(&io->io_list,
409 struct f2fs_io_info, list);
410 list_del(&fio->list);
411 spin_unlock(&io->io_lock);
412 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900413
Chao Yu7a9d7542016-02-22 18:36:38 +0800414 if (fio->old_blkaddr != NEW_ADDR)
415 verify_block_addr(sbi, fio->old_blkaddr);
416 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900417
Chao Yu36951b32016-11-16 10:41:20 +0800418 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
419
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800420 /* set submitted = 1 as a return value */
421 fio->submitted = 1;
422
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700423 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900424
Chao Yu7a9d7542016-02-22 18:36:38 +0800425 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700426 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
427 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900428 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900429alloc_new:
430 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800431 if ((fio->type == DATA || fio->type == NODE) &&
432 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
433 err = -EAGAIN;
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700434 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800435 goto out_fail;
436 }
Chao Yu7a9d7542016-02-22 18:36:38 +0800437 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700438 BIO_MAX_PAGES, false);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900439 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900440 }
441
Jaegeuk Kima912b542017-05-10 11:18:25 -0700442 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900443 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900444 goto alloc_new;
445 }
446
Chao Yu7a9d7542016-02-22 18:36:38 +0800447 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700448 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800449
450 trace_f2fs_submit_page_write(fio->page, fio);
451
452 if (fio->in_list)
453 goto next;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800454out_fail:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800455 up_write(&io->io_rwsem);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800456 return err;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900457}
458
Chao Yu46008c62016-05-09 19:56:30 +0800459static void __set_data_blkaddr(struct dnode_of_data *dn)
460{
461 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
462 __le32 *addr_array;
463
464 /* Get physical address of data block */
465 addr_array = blkaddr_in_node(rn);
466 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
467}
468
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900469/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900470 * Lock ordering for the change of data block address:
471 * ->data_page
472 * ->node_page
473 * update block addresses in the node page
474 */
Chao Yu216a6202015-03-19 19:23:32 +0800475void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900476{
Chao Yu46008c62016-05-09 19:56:30 +0800477 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
478 __set_data_blkaddr(dn);
479 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800480 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900481}
482
Chao Yuf28b3432016-02-24 17:16:47 +0800483void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
484{
485 dn->data_blkaddr = blkaddr;
486 set_data_blkaddr(dn);
487 f2fs_update_extent_cache(dn);
488}
489
Chao Yu46008c62016-05-09 19:56:30 +0800490/* dn->ofs_in_node will be returned with up-to-date last block pointer */
491int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900492{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700493 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800494 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900495
Chao Yu46008c62016-05-09 19:56:30 +0800496 if (!count)
497 return 0;
498
Jaegeuk Kim91942322016-05-20 10:13:22 -0700499 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900500 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +0800501 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
502 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900503
Chao Yu46008c62016-05-09 19:56:30 +0800504 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
505 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900506
Chao Yu46008c62016-05-09 19:56:30 +0800507 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
508
509 for (; count > 0; dn->ofs_in_node++) {
510 block_t blkaddr =
511 datablock_addr(dn->node_page, dn->ofs_in_node);
512 if (blkaddr == NULL_ADDR) {
513 dn->data_blkaddr = NEW_ADDR;
514 __set_data_blkaddr(dn);
515 count--;
516 }
517 }
518
519 if (set_page_dirty(dn->node_page))
520 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900521 return 0;
522}
523
Chao Yu46008c62016-05-09 19:56:30 +0800524/* Should keep dn->ofs_in_node unchanged */
525int reserve_new_block(struct dnode_of_data *dn)
526{
527 unsigned int ofs_in_node = dn->ofs_in_node;
528 int ret;
529
530 ret = reserve_new_blocks(dn, 1);
531 dn->ofs_in_node = ofs_in_node;
532 return ret;
533}
534
Huajun Lib6009652013-11-10 23:13:18 +0800535int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
536{
537 bool need_put = dn->inode_page ? false : true;
538 int err;
539
540 err = get_dnode_of_data(dn, index, ALLOC_NODE);
541 if (err)
542 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900543
Huajun Lib6009652013-11-10 23:13:18 +0800544 if (dn->data_blkaddr == NULL_ADDR)
545 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900546 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800547 f2fs_put_dnode(dn);
548 return err;
549}
550
Fan Li759af1c2015-08-05 15:52:16 +0800551int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900552{
Hou Pengyange15882b2017-02-23 09:18:05 +0000553 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800554 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800555
Fan Li759af1c2015-08-05 15:52:16 +0800556 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
557 dn->data_blkaddr = ei.blk + index - ei.fofs;
558 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800559 }
560
Fan Li759af1c2015-08-05 15:52:16 +0800561 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900562}
563
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700564struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500565 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900566{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900567 struct address_space *mapping = inode->i_mapping;
568 struct dnode_of_data dn;
569 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +0000570 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900571 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800572 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700573 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800574 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500575 .op = REQ_OP_READ,
576 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700577 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800578 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900579
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700580 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
581 return read_mapping_page(mapping, index, NULL);
582
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700583 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900584 if (!page)
585 return ERR_PTR(-ENOMEM);
586
Chao Yucb3bc9e2015-02-05 18:03:40 +0800587 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
588 dn.data_blkaddr = ei.blk + index - ei.fofs;
589 goto got_it;
590 }
591
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900592 set_new_dnode(&dn, inode, NULL, NULL, 0);
593 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700594 if (err)
595 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900596 f2fs_put_dnode(&dn);
597
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900598 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700599 err = -ENOENT;
600 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900601 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800602got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700603 if (PageUptodate(page)) {
604 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900605 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700606 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900607
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900608 /*
609 * A new dentry page is allocated but not able to be written, since its
610 * new inode page couldn't be allocated due to -ENOSPC.
611 * In such the case, its blkaddr can be remained as NEW_ADDR.
612 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
613 */
614 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300615 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700616 if (!PageUptodate(page))
617 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700618 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900619 return page;
620 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900621
Chao Yu7a9d7542016-02-22 18:36:38 +0800622 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700623 fio.page = page;
624 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900625 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700626 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700627 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700628
629put_err:
630 f2fs_put_page(page, 1);
631 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700632}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900633
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700634struct page *find_data_page(struct inode *inode, pgoff_t index)
635{
636 struct address_space *mapping = inode->i_mapping;
637 struct page *page;
638
639 page = find_get_page(mapping, index);
640 if (page && PageUptodate(page))
641 return page;
642 f2fs_put_page(page, 0);
643
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600644 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700645 if (IS_ERR(page))
646 return page;
647
648 if (PageUptodate(page))
649 return page;
650
651 wait_on_page_locked(page);
652 if (unlikely(!PageUptodate(page))) {
653 f2fs_put_page(page, 0);
654 return ERR_PTR(-EIO);
655 }
656 return page;
657}
658
659/*
660 * If it tries to access a hole, return an error.
661 * Because, the callers, functions in dir.c and GC, should be able to know
662 * whether this page exists or not.
663 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700664struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
665 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700666{
667 struct address_space *mapping = inode->i_mapping;
668 struct page *page;
669repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600670 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700671 if (IS_ERR(page))
672 return page;
673
674 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900675 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900676 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900677 f2fs_put_page(page, 1);
678 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900679 }
Chao Yu1563ac72016-07-03 22:05:12 +0800680 if (unlikely(!PageUptodate(page))) {
681 f2fs_put_page(page, 1);
682 return ERR_PTR(-EIO);
683 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900684 return page;
685}
686
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900687/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900688 * Caller ensures that this data page is never allocated.
689 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900690 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800691 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
692 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800693 * Note that, ipage is set only by make_empty_dir, and if any error occur,
694 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900695 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900696struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900697 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900698{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900699 struct address_space *mapping = inode->i_mapping;
700 struct page *page;
701 struct dnode_of_data dn;
702 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800703
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700704 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800705 if (!page) {
706 /*
707 * before exiting, we should make sure ipage will be released
708 * if any error occur.
709 */
710 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700711 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800712 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900713
Jaegeuk Kima8865372013-12-27 17:04:17 +0900714 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800715 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700716 if (err) {
717 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900718 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900719 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700720 if (!ipage)
721 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900722
723 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700724 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900725
726 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300727 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700728 if (!PageUptodate(page))
729 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900730 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700731 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900732
Jaegeuk Kim76121182016-01-01 22:03:47 -0800733 /* if ipage exists, blkaddr should be NEW_ADDR */
734 f2fs_bug_on(F2FS_I_SB(inode), ipage);
735 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700736 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800737 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900738 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700739got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800740 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700741 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700742 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900743 return page;
744}
745
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900746static int __allocate_data_block(struct dnode_of_data *dn)
747{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700748 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900749 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900750 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700751 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800752 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +0800753 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900754
Jaegeuk Kim91942322016-05-20 10:13:22 -0700755 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900756 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800757
758 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
759 if (dn->data_blkaddr == NEW_ADDR)
760 goto alloc;
761
Chao Yu0abd6752017-07-09 00:13:07 +0800762 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
763 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900764
Chao Yudf6136e2015-03-23 10:33:37 +0800765alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900766 get_node_info(sbi, dn->nid, &ni);
767 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
768
Chao Yudf6136e2015-03-23 10:33:37 +0800769 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Chao Yufb830fc2017-05-19 23:37:01 +0800770 &sum, CURSEG_WARM_DATA, NULL, false);
Chao Yu216a6202015-03-19 19:23:32 +0800771 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900772
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700773 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800774 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700775 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300776 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700777 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300778 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900779 return 0;
780}
781
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800782static inline bool __force_buffered_io(struct inode *inode, int rw)
783{
784 return ((f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) ||
785 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
786 F2FS_I_SB(inode)->s_ndevs);
787}
788
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800789int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800790{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800791 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800792 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800793 int err = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800794
Jaegeuk Kimdc91de72017-01-13 13:12:29 -0800795 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
796 return 0;
797
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700798 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800799 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
800 if (map.m_len > map.m_lblk)
801 map.m_len -= map.m_lblk;
802 else
803 map.m_len = 0;
804
Chao Yuda859852016-01-26 15:42:58 +0800805 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800806
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800807 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800808 err = f2fs_convert_inline_inode(inode);
809 if (err)
810 return err;
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800811 return f2fs_map_blocks(inode, &map, 1,
812 __force_buffered_io(inode, WRITE) ?
813 F2FS_GET_BLOCK_PRE_AIO :
814 F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800815 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800816 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800817 err = f2fs_convert_inline_inode(inode);
818 if (err)
819 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800820 }
821 if (!f2fs_has_inline_data(inode))
822 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800823 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800824}
825
Yunlei He59c90812017-03-13 20:22:18 +0800826static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
827{
828 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
829 if (lock)
830 down_read(&sbi->node_change);
831 else
832 up_read(&sbi->node_change);
833 } else {
834 if (lock)
835 f2fs_lock_op(sbi);
836 else
837 f2fs_unlock_op(sbi);
838 }
839}
840
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900841/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700842 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
843 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800844 * If original data blocks are allocated, then give them to blockdev.
845 * Otherwise,
846 * a. preallocate requested block addresses
847 * b. do not use extent cache for better performance
848 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900849 */
Chao Yud323d002015-10-27 09:53:45 +0800850int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800851 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900852{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700853 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900854 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800855 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700856 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800857 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900858 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800859 unsigned int ofs_in_node, last_ofs_in_node;
860 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +0000861 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +0800862 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900863
Chao Yudfd02e42016-08-20 15:12:01 +0800864 if (!maxblocks)
865 return 0;
866
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700867 map->m_len = 0;
868 map->m_flags = 0;
869
870 /* it only supports block size == page size */
871 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800872 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900873
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800874 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700875 map->m_pblk = ei.blk + pgofs - ei.fofs;
876 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
877 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900878 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800879 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900880
Chao Yu4fe71e82016-01-26 15:37:38 +0800881next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800882 if (create)
Yunlei He59c90812017-03-13 20:22:18 +0800883 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900884
885 /* When reading holes, we need its node page */
886 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900887 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900888 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800889 if (flag == F2FS_GET_BLOCK_BMAP)
890 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800891 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900892 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800893 if (map->m_next_pgofs)
894 *map->m_next_pgofs =
895 get_next_page_offset(&dn, pgofs);
896 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900897 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900898 }
Chao Yu973163f2015-09-18 16:51:51 +0800899
Chao Yu46008c62016-05-09 19:56:30 +0800900 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +0100901 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800902 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900903
Chao Yu4fe71e82016-01-26 15:37:38 +0800904next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800905 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800906
Fan Li7df3a432015-12-17 13:20:59 +0800907 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
908 if (create) {
909 if (unlikely(f2fs_cp_error(sbi))) {
910 err = -EIO;
911 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800912 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800913 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800914 if (blkaddr == NULL_ADDR) {
915 prealloc++;
916 last_ofs_in_node = dn.ofs_in_node;
917 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800918 } else {
919 err = __allocate_data_block(&dn);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800920 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700921 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800922 }
Fan Li7df3a432015-12-17 13:20:59 +0800923 if (err)
924 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +0800925 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800926 blkaddr = dn.data_blkaddr;
927 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800928 if (flag == F2FS_GET_BLOCK_BMAP) {
929 map->m_pblk = 0;
930 goto sync_out;
931 }
Chao Yuda859852016-01-26 15:42:58 +0800932 if (flag == F2FS_GET_BLOCK_FIEMAP &&
933 blkaddr == NULL_ADDR) {
934 if (map->m_next_pgofs)
935 *map->m_next_pgofs = pgofs + 1;
936 }
Fan Li7df3a432015-12-17 13:20:59 +0800937 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800938 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800939 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900940 }
941 }
Fan Li7df3a432015-12-17 13:20:59 +0800942
Chao Yu46008c62016-05-09 19:56:30 +0800943 if (flag == F2FS_GET_BLOCK_PRE_AIO)
944 goto skip;
945
Chao Yu4fe71e82016-01-26 15:37:38 +0800946 if (map->m_len == 0) {
947 /* preallocated unwritten block should be mapped for fiemap. */
948 if (blkaddr == NEW_ADDR)
949 map->m_flags |= F2FS_MAP_UNWRITTEN;
950 map->m_flags |= F2FS_MAP_MAPPED;
951
952 map->m_pblk = blkaddr;
953 map->m_len = 1;
954 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800955 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800956 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800957 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800958 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800959 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800960 } else {
961 goto sync_out;
962 }
963
Chao Yu46008c62016-05-09 19:56:30 +0800964skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800965 dn.ofs_in_node++;
966 pgofs++;
967
Chao Yu46008c62016-05-09 19:56:30 +0800968 /* preallocate blocks in batch for one dnode page */
969 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
970 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800971
Chao Yu46008c62016-05-09 19:56:30 +0800972 dn.ofs_in_node = ofs_in_node;
973 err = reserve_new_blocks(&dn, prealloc);
974 if (err)
975 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800976
Chao Yu46008c62016-05-09 19:56:30 +0800977 map->m_len += dn.ofs_in_node - ofs_in_node;
978 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
979 err = -ENOSPC;
980 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800981 }
Chao Yu46008c62016-05-09 19:56:30 +0800982 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800983 }
984
Chao Yu46008c62016-05-09 19:56:30 +0800985 if (pgofs >= end)
986 goto sync_out;
987 else if (dn.ofs_in_node < end_offset)
988 goto next_block;
989
Chao Yu46008c62016-05-09 19:56:30 +0800990 f2fs_put_dnode(&dn);
991
992 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +0800993 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800994 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +0800995 }
Chao Yu46008c62016-05-09 19:56:30 +0800996 goto next_dnode;
997
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900998sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900999 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001000unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001001 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001002 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001003 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001004 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001005out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001006 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001007 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001008}
1009
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001010static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001011 struct buffer_head *bh, int create, int flag,
1012 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001013{
1014 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001015 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001016
1017 map.m_lblk = iblock;
1018 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001019 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001020
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001021 err = f2fs_map_blocks(inode, &map, create, flag);
1022 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001023 map_bh(bh, inode->i_sb, map.m_pblk);
1024 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001025 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001026 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001027 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001028}
1029
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001030static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001031 struct buffer_head *bh_result, int create, int flag,
1032 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001033{
Chao Yuda859852016-01-26 15:42:58 +08001034 return __get_data_block(inode, iblock, bh_result, create,
1035 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001036}
1037
Chao Yue2b4e2b2015-08-19 19:11:19 +08001038static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001039 struct buffer_head *bh_result, int create)
1040{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001041 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +08001042 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +08001043}
1044
1045static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1046 struct buffer_head *bh_result, int create)
1047{
Yunlei He179448b2015-12-28 21:48:32 +08001048 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001049 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001050 return -EFBIG;
1051
Chao Yue2b4e2b2015-08-19 19:11:19 +08001052 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +08001053 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001054}
1055
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001056static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1057{
1058 return (offset >> inode->i_blkbits);
1059}
1060
1061static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1062{
1063 return (blk << inode->i_blkbits);
1064}
1065
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001066int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1067 u64 start, u64 len)
1068{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001069 struct buffer_head map_bh;
1070 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001071 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001072 u64 logical = 0, phys = 0, size = 0;
1073 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001074 int ret = 0;
1075
1076 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1077 if (ret)
1078 return ret;
1079
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001080 if (f2fs_has_inline_data(inode)) {
1081 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1082 if (ret != -EAGAIN)
1083 return ret;
1084 }
1085
Al Viro59551022016-01-22 15:40:57 -05001086 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +08001087
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001088 if (logical_to_blk(inode, len) == 0)
1089 len = blk_to_logical(inode, 1);
1090
1091 start_blk = logical_to_blk(inode, start);
1092 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001093
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001094next:
1095 memset(&map_bh, 0, sizeof(struct buffer_head));
1096 map_bh.b_size = len;
1097
Chao Yue2b4e2b2015-08-19 19:11:19 +08001098 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001099 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001100 if (ret)
1101 goto out;
1102
1103 /* HOLE */
1104 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001105 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001106
1107 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1108 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001109 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001110
Fan Li9a950d52015-12-26 18:07:41 +08001111 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001112 }
Fan Li9a950d52015-12-26 18:07:41 +08001113
Chao Yuda5af122016-01-08 20:19:27 +08001114 if (size) {
1115 if (f2fs_encrypted_inode(inode))
1116 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1117
Fan Li9a950d52015-12-26 18:07:41 +08001118 ret = fiemap_fill_next_extent(fieinfo, logical,
1119 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001120 }
Fan Li9a950d52015-12-26 18:07:41 +08001121
1122 if (start_blk > last_blk || ret)
1123 goto out;
1124
1125 logical = blk_to_logical(inode, start_blk);
1126 phys = blk_to_logical(inode, map_bh.b_blocknr);
1127 size = map_bh.b_size;
1128 flags = 0;
1129 if (buffer_unwritten(&map_bh))
1130 flags = FIEMAP_EXTENT_UNWRITTEN;
1131
1132 start_blk += logical_to_blk(inode, size);
1133
1134prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001135 cond_resched();
1136 if (fatal_signal_pending(current))
1137 ret = -EINTR;
1138 else
1139 goto next;
1140out:
1141 if (ret == 1)
1142 ret = 0;
1143
Al Viro59551022016-01-22 15:40:57 -05001144 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001145 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001146}
1147
Wei Yongjun6a7a3ae2016-08-23 15:23:59 +00001148static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
1149 unsigned nr_pages)
Chao Yu78682f72016-07-03 22:05:11 +08001150{
1151 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1152 struct fscrypt_ctx *ctx = NULL;
Chao Yu78682f72016-07-03 22:05:11 +08001153 struct bio *bio;
1154
1155 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1156 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
1157 if (IS_ERR(ctx))
1158 return ERR_CAST(ctx);
1159
1160 /* wait the page to be moved by cleaning */
1161 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
1162 }
1163
1164 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
1165 if (!bio) {
1166 if (ctx)
1167 fscrypt_release_ctx(ctx);
1168 return ERR_PTR(-ENOMEM);
1169 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001170 f2fs_target_device(sbi, blkaddr, bio);
Chao Yu78682f72016-07-03 22:05:11 +08001171 bio->bi_end_io = f2fs_read_end_io;
1172 bio->bi_private = ctx;
1173
1174 return bio;
1175}
1176
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001177/*
1178 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1179 * Major change was from block_size == page_size in f2fs by default.
1180 */
1181static int f2fs_mpage_readpages(struct address_space *mapping,
1182 struct list_head *pages, struct page *page,
1183 unsigned nr_pages)
1184{
1185 struct bio *bio = NULL;
1186 unsigned page_idx;
1187 sector_t last_block_in_bio = 0;
1188 struct inode *inode = mapping->host;
1189 const unsigned blkbits = inode->i_blkbits;
1190 const unsigned blocksize = 1 << blkbits;
1191 sector_t block_in_file;
1192 sector_t last_block;
1193 sector_t last_block_in_file;
1194 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001195 struct f2fs_map_blocks map;
1196
1197 map.m_pblk = 0;
1198 map.m_lblk = 0;
1199 map.m_len = 0;
1200 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001201 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001202
1203 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1204
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001205 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001206 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08001207
1208 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001209 list_del(&page->lru);
1210 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001211 page->index,
1212 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001213 goto next_page;
1214 }
1215
1216 block_in_file = (sector_t)page->index;
1217 last_block = block_in_file + nr_pages;
1218 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1219 blkbits;
1220 if (last_block > last_block_in_file)
1221 last_block = last_block_in_file;
1222
1223 /*
1224 * Map blocks using the previous result first.
1225 */
1226 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1227 block_in_file > map.m_lblk &&
1228 block_in_file < (map.m_lblk + map.m_len))
1229 goto got_it;
1230
1231 /*
1232 * Then do more f2fs_map_blocks() calls until we are
1233 * done with this page.
1234 */
1235 map.m_flags = 0;
1236
1237 if (block_in_file < last_block) {
1238 map.m_lblk = block_in_file;
1239 map.m_len = last_block - block_in_file;
1240
Chao Yu46c9e142015-09-18 16:54:16 +08001241 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001242 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001243 goto set_error_page;
1244 }
1245got_it:
1246 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1247 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1248 SetPageMappedToDisk(page);
1249
1250 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1251 SetPageUptodate(page);
1252 goto confused;
1253 }
1254 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001255 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001256 if (!PageUptodate(page))
1257 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001258 unlock_page(page);
1259 goto next_page;
1260 }
1261
1262 /*
1263 * This page will go to BIO. Do we need to send this
1264 * BIO off first?
1265 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001266 if (bio && (last_block_in_bio != block_nr - 1 ||
1267 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001268submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001269 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001270 bio = NULL;
1271 }
1272 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001273 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001274 if (IS_ERR(bio)) {
1275 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001276 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001277 }
Mike Christie04d328d2016-06-05 14:31:55 -05001278 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001279 }
1280
1281 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1282 goto submit_and_realloc;
1283
1284 last_block_in_bio = block_nr;
1285 goto next_page;
1286set_error_page:
1287 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001288 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001289 unlock_page(page);
1290 goto next_page;
1291confused:
1292 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001293 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001294 bio = NULL;
1295 }
1296 unlock_page(page);
1297next_page:
1298 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001299 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001300 }
1301 BUG_ON(pages && !list_empty(pages));
1302 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001303 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001304 return 0;
1305}
1306
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001307static int f2fs_read_data_page(struct file *file, struct page *page)
1308{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001309 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001310 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001311
Chao Yuc20e89c2014-05-06 16:53:08 +08001312 trace_f2fs_readpage(page, DATA);
1313
arter97e1c42042014-08-06 23:22:50 +09001314 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001315 if (f2fs_has_inline_data(inode))
1316 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001317 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001318 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001319 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001320}
1321
1322static int f2fs_read_data_pages(struct file *file,
1323 struct address_space *mapping,
1324 struct list_head *pages, unsigned nr_pages)
1325{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001326 struct inode *inode = file->f_mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001327 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001328
1329 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001330
1331 /* If the file has inline data, skip readpages */
1332 if (f2fs_has_inline_data(inode))
1333 return 0;
1334
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001335 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001336}
1337
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001338static int encrypt_one_page(struct f2fs_io_info *fio)
1339{
1340 struct inode *inode = fio->page->mapping->host;
1341 gfp_t gfp_flags = GFP_NOFS;
1342
1343 if (!f2fs_encrypted_inode(inode) || !S_ISREG(inode->i_mode))
1344 return 0;
1345
1346 /* wait for GCed encrypted page writeback */
1347 f2fs_wait_on_encrypted_page_writeback(fio->sbi, fio->old_blkaddr);
1348
1349retry_encrypt:
1350 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1351 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1352 if (!IS_ERR(fio->encrypted_page))
1353 return 0;
1354
1355 /* flush pending IOs and wait for a while in the ENOMEM case */
1356 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001357 f2fs_flush_merged_writes(fio->sbi);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001358 congestion_wait(BLK_RW_ASYNC, HZ/50);
1359 gfp_flags |= __GFP_NOFAIL;
1360 goto retry_encrypt;
1361 }
1362 return PTR_ERR(fio->encrypted_page);
1363}
1364
1365static inline bool need_inplace_update(struct f2fs_io_info *fio)
1366{
1367 struct inode *inode = fio->page->mapping->host;
1368
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001369 if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode))
1370 return false;
1371 if (is_cold_data(fio->page))
1372 return false;
1373 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1374 return false;
1375
1376 return need_inplace_update_policy(inode, fio);
1377}
1378
Jaegeuk Kima8177372017-04-24 15:20:16 -07001379static inline bool valid_ipu_blkaddr(struct f2fs_io_info *fio)
1380{
1381 if (fio->old_blkaddr == NEW_ADDR)
1382 return false;
1383 if (fio->old_blkaddr == NULL_ADDR)
1384 return false;
1385 return true;
1386}
1387
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001388int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001389{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001390 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001391 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001392 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001393 struct extent_info ei = {0,0,0};
1394 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001395 int err = 0;
1396
1397 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00001398 if (need_inplace_update(fio) &&
1399 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1400 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07001401
1402 if (valid_ipu_blkaddr(fio)) {
Hou Pengyange959c8f2017-04-25 12:45:13 +00001403 ipu_force = true;
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001404 fio->need_lock = LOCK_DONE;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001405 goto got_it;
1406 }
1407 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001408
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001409 /* Deadlock due to between page->lock and f2fs_lock_op */
1410 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1411 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001412
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001413 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001414 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001415 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001416
Chao Yu28bc1062016-02-06 14:40:34 +08001417 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001418
1419 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001420 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001421 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001422 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001423 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00001424got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001425 /*
1426 * If current allocation needs SSR,
1427 * it had better in-place writes for updated data.
1428 */
Jaegeuk Kima8177372017-04-24 15:20:16 -07001429 if (ipu_force || (valid_ipu_blkaddr(fio) && need_inplace_update(fio))) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001430 err = encrypt_one_page(fio);
1431 if (err)
1432 goto out_writepage;
1433
1434 set_page_writeback(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001435 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001436 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001437 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -07001438 err = rewrite_data_page(fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001439 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001440 set_inode_flag(inode, FI_UPDATE_WRITE);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001441 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001442 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001443
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001444 if (fio->need_lock == LOCK_RETRY) {
1445 if (!f2fs_trylock_op(fio->sbi)) {
1446 err = -EAGAIN;
1447 goto out_writepage;
1448 }
1449 fio->need_lock = LOCK_REQ;
1450 }
1451
1452 err = encrypt_one_page(fio);
1453 if (err)
1454 goto out_writepage;
1455
1456 set_page_writeback(page);
1457
Hou Pengyang279d6df2017-04-27 00:17:21 +08001458 /* LFS mode write path */
1459 write_data_page(&dn, fio);
1460 trace_f2fs_do_write_data_page(page, OPU);
1461 set_inode_flag(inode, FI_APPEND_WRITE);
1462 if (page->index == 0)
1463 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001464out_writepage:
1465 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001466out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001467 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001468 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001469 return err;
1470}
1471
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001472static int __write_data_page(struct page *page, bool *submitted,
1473 struct writeback_control *wbc)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001474{
1475 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001476 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001477 loff_t i_size = i_size_read(inode);
1478 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001479 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001480 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001481 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001482 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001483 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001484 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001485 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001486 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001487 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001488 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00001489 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001490 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001491 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001492 .submitted = false,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001493 .need_lock = LOCK_RETRY,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001494 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001495
Chao Yuecda0de2014-05-06 16:48:26 +08001496 trace_f2fs_writepage(page, DATA);
1497
Chao Yu0771fcc2017-06-29 23:20:45 +08001498 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1499 goto redirty_out;
1500
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001501 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001502 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001503
1504 /*
1505 * If the offset is out-of-range of file size,
1506 * this page does not have to be written to disk.
1507 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001508 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001509 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001510 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001511
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001512 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001513write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001514 if (f2fs_is_drop_cache(inode))
1515 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001516 /* we should not write 0'th page having journal header */
1517 if (f2fs_is_volatile_file(inode) && (!page->index ||
1518 (!wbc->for_reclaim &&
1519 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001520 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001521
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001522 /* we should bypass data pages to proceed the kworkder jobs */
1523 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001524 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001525 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001526 }
1527
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001528 /* Dentry blocks are controlled by checkpoint */
1529 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001530 fio.need_lock = LOCK_DONE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001531 err = do_write_data_page(&fio);
1532 goto done;
1533 }
1534
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001535 if (!wbc->for_reclaim)
1536 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001537 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001538 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001539 else
1540 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001541
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001542 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08001543 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001544 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08001545 if (!err)
1546 goto out;
1547 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001548
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001549 if (err == -EAGAIN) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001550 err = do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001551 if (err == -EAGAIN) {
1552 fio.need_lock = LOCK_REQ;
1553 err = do_write_data_page(&fio);
1554 }
1555 }
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001556 if (F2FS_I(inode)->last_disk_size < psize)
1557 F2FS_I(inode)->last_disk_size = psize;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001558
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001559done:
1560 if (err && err != -ENOENT)
1561 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001562
Jaegeuk Kim39936832012-11-22 16:21:29 +09001563out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001564 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001565 if (err)
1566 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001567
1568 if (wbc->for_reclaim) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001569 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001570 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yuc227f912015-12-16 13:09:20 +08001571 remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001572 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001573 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001574
1575 unlock_page(page);
Jaegeuk Kima7881892017-04-20 13:51:57 -07001576 if (!S_ISDIR(inode->i_mode))
1577 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001578
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001579 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001580 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001581 submitted = NULL;
1582 }
1583
1584 if (submitted)
1585 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001586
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001587 return 0;
1588
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001589redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001590 redirty_page_for_writepage(wbc, page);
Chao Yu0002b612016-11-28 19:13:43 -08001591 if (!err)
1592 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001593 unlock_page(page);
1594 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001595}
1596
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001597static int f2fs_write_data_page(struct page *page,
1598 struct writeback_control *wbc)
1599{
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001600 return __write_data_page(page, NULL, wbc);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001601}
1602
Chao Yu8f46dca2015-07-14 18:56:10 +08001603/*
1604 * This function was copied from write_cche_pages from mm/page-writeback.c.
1605 * The major change is making write step of cold data page separately from
1606 * warm/hot data page.
1607 */
1608static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001609 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001610{
1611 int ret = 0;
1612 int done = 0;
1613 struct pagevec pvec;
1614 int nr_pages;
1615 pgoff_t uninitialized_var(writeback_index);
1616 pgoff_t index;
1617 pgoff_t end; /* Inclusive */
1618 pgoff_t done_index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001619 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001620 int cycled;
1621 int range_whole = 0;
1622 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001623
1624 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001625
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001626 if (get_dirty_pages(mapping->host) <=
1627 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1628 set_inode_flag(mapping->host, FI_HOT_DATA);
1629 else
1630 clear_inode_flag(mapping->host, FI_HOT_DATA);
1631
Chao Yu8f46dca2015-07-14 18:56:10 +08001632 if (wbc->range_cyclic) {
1633 writeback_index = mapping->writeback_index; /* prev offset */
1634 index = writeback_index;
1635 if (index == 0)
1636 cycled = 1;
1637 else
1638 cycled = 0;
1639 end = -1;
1640 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001641 index = wbc->range_start >> PAGE_SHIFT;
1642 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001643 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1644 range_whole = 1;
1645 cycled = 1; /* ignore range_cyclic tests */
1646 }
1647 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1648 tag = PAGECACHE_TAG_TOWRITE;
1649 else
1650 tag = PAGECACHE_TAG_DIRTY;
1651retry:
1652 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1653 tag_pages_for_writeback(mapping, index, end);
1654 done_index = index;
1655 while (!done && (index <= end)) {
1656 int i;
1657
1658 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1659 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1660 if (nr_pages == 0)
1661 break;
1662
1663 for (i = 0; i < nr_pages; i++) {
1664 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001665 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001666
1667 if (page->index > end) {
1668 done = 1;
1669 break;
1670 }
1671
1672 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001673retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08001674 lock_page(page);
1675
1676 if (unlikely(page->mapping != mapping)) {
1677continue_unlock:
1678 unlock_page(page);
1679 continue;
1680 }
1681
1682 if (!PageDirty(page)) {
1683 /* someone wrote it for us */
1684 goto continue_unlock;
1685 }
1686
Chao Yu8f46dca2015-07-14 18:56:10 +08001687 if (PageWriteback(page)) {
1688 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001689 f2fs_wait_on_page_writeback(page,
1690 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001691 else
1692 goto continue_unlock;
1693 }
1694
1695 BUG_ON(PageWriteback(page));
1696 if (!clear_page_dirty_for_io(page))
1697 goto continue_unlock;
1698
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001699 ret = __write_data_page(page, &submitted, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001700 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08001701 /*
1702 * keep nr_to_write, since vfs uses this to
1703 * get # of written pages.
1704 */
1705 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1706 unlock_page(page);
1707 ret = 0;
1708 continue;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001709 } else if (ret == -EAGAIN) {
1710 ret = 0;
1711 if (wbc->sync_mode == WB_SYNC_ALL) {
1712 cond_resched();
1713 congestion_wait(BLK_RW_ASYNC,
1714 HZ/50);
1715 goto retry_write;
1716 }
1717 continue;
Chao Yu0002b612016-11-28 19:13:43 -08001718 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001719 done_index = page->index + 1;
1720 done = 1;
1721 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001722 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001723 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08001724 }
1725
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001726 /* give a priority to WB_SYNC threads */
1727 if ((atomic_read(&F2FS_M_SB(mapping)->wb_sync_req) ||
1728 --wbc->nr_to_write <= 0) &&
1729 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08001730 done = 1;
1731 break;
1732 }
1733 }
1734 pagevec_release(&pvec);
1735 cond_resched();
1736 }
1737
Chao Yu8f46dca2015-07-14 18:56:10 +08001738 if (!cycled && !done) {
1739 cycled = 1;
1740 index = 0;
1741 end = writeback_index - 1;
1742 goto retry;
1743 }
1744 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1745 mapping->writeback_index = done_index;
1746
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001747 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001748 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
1749 0, last_idx, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08001750
Chao Yu8f46dca2015-07-14 18:56:10 +08001751 return ret;
1752}
1753
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001754static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001755 struct writeback_control *wbc)
1756{
1757 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001758 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001759 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001760 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001761
P J Pcfb185a2013-04-03 11:38:00 +09001762 /* deal with chardevs and other special file */
1763 if (!mapping->a_ops->writepage)
1764 return 0;
1765
Chao Yu6a290542015-07-17 18:02:39 +08001766 /* skip writing if there is no dirty page in this inode */
1767 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1768 return 0;
1769
Chao Yu0771fcc2017-06-29 23:20:45 +08001770 /* during POR, we don't need to trigger writepage at all. */
1771 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1772 goto skip_write;
1773
Jaegeuk Kima1257022015-10-08 10:40:07 -07001774 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1775 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1776 available_free_memory(sbi, DIRTY_DENTS))
1777 goto skip_write;
1778
Chao Yud323d002015-10-27 09:53:45 +08001779 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001780 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001781 goto skip_write;
1782
Yunlei Hed31c7c32016-02-04 16:14:00 +08001783 trace_f2fs_writepages(mapping->host, wbc, DATA);
1784
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001785 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
1786 if (wbc->sync_mode == WB_SYNC_ALL)
1787 atomic_inc(&sbi->wb_sync_req);
1788 else if (atomic_read(&sbi->wb_sync_req))
1789 goto skip_write;
1790
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001791 blk_start_plug(&plug);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001792 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001793 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001794
1795 if (wbc->sync_mode == WB_SYNC_ALL)
1796 atomic_dec(&sbi->wb_sync_req);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001797 /*
1798 * if some pages were truncated, we cannot guarantee its mapping->host
1799 * to detect pending bios.
1800 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001801
Chao Yuc227f912015-12-16 13:09:20 +08001802 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001803 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001804
1805skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001806 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001807 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001808 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001809}
1810
Chao Yu3aab8f82014-07-02 13:25:04 +08001811static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1812{
1813 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001814 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001815
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001816 if (to > i_size) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001817 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001818 truncate_pagecache(inode, i_size);
1819 truncate_blocks(inode, i_size, true);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001820 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3aab8f82014-07-02 13:25:04 +08001821 }
1822}
1823
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001824static int prepare_write_begin(struct f2fs_sb_info *sbi,
1825 struct page *page, loff_t pos, unsigned len,
1826 block_t *blk_addr, bool *node_changed)
1827{
1828 struct inode *inode = page->mapping->host;
1829 pgoff_t index = page->index;
1830 struct dnode_of_data dn;
1831 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001832 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00001833 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001834 int err = 0;
1835
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001836 /*
1837 * we already allocated all the blocks, so we don't need to get
1838 * the block addresses when there is no need to fill the page.
1839 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08001840 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
1841 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001842 return 0;
1843
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001844 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001845 (pos & PAGE_MASK) >= i_size_read(inode)) {
Yunlei He59c90812017-03-13 20:22:18 +08001846 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001847 locked = true;
1848 }
1849restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001850 /* check inline_data */
1851 ipage = get_node_page(sbi, inode->i_ino);
1852 if (IS_ERR(ipage)) {
1853 err = PTR_ERR(ipage);
1854 goto unlock_out;
1855 }
1856
1857 set_new_dnode(&dn, inode, ipage, ipage, 0);
1858
1859 if (f2fs_has_inline_data(inode)) {
1860 if (pos + len <= MAX_INLINE_DATA) {
1861 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001862 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001863 if (inode->i_nlink)
1864 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001865 } else {
1866 err = f2fs_convert_inline_page(&dn, page);
1867 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001868 goto out;
1869 if (dn.data_blkaddr == NULL_ADDR)
1870 err = f2fs_get_block(&dn, index);
1871 }
1872 } else if (locked) {
1873 err = f2fs_get_block(&dn, index);
1874 } else {
1875 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1876 dn.data_blkaddr = ei.blk + index - ei.fofs;
1877 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001878 /* hole case */
1879 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001880 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001881 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08001882 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
1883 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001884 locked = true;
1885 goto restart;
1886 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001887 }
1888 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001889
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001890 /* convert_inline_page can make node_changed */
1891 *blk_addr = dn.data_blkaddr;
1892 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001893out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001894 f2fs_put_dnode(&dn);
1895unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001896 if (locked)
Yunlei He59c90812017-03-13 20:22:18 +08001897 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001898 return err;
1899}
1900
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001901static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1902 loff_t pos, unsigned len, unsigned flags,
1903 struct page **pagep, void **fsdata)
1904{
1905 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001906 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001907 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001908 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001909 bool need_balance = false;
1910 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001911 int err = 0;
1912
Chao Yu62aed042014-05-06 16:46:04 +08001913 trace_f2fs_write_begin(inode, pos, len, flags);
1914
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001915 /*
1916 * We should check this at this moment to avoid deadlock on inode page
1917 * and #0 page. The locking rule for inline_data conversion should be:
1918 * lock_page(page #0) -> lock_page(inode_page)
1919 */
1920 if (index != 0) {
1921 err = f2fs_convert_inline_inode(inode);
1922 if (err)
1923 goto fail;
1924 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001925repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08001926 /*
1927 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1928 * wait_for_stable_page. Will wait that below with our IO control.
1929 */
1930 page = pagecache_get_page(mapping, index,
1931 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08001932 if (!page) {
1933 err = -ENOMEM;
1934 goto fail;
1935 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001936
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001937 *pagep = page;
1938
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001939 err = prepare_write_begin(sbi, page, pos, len,
1940 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001941 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001942 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001943
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001944 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001945 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001946 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001947 lock_page(page);
1948 if (page->mapping != mapping) {
1949 /* The page got truncated from under us */
1950 f2fs_put_page(page, 1);
1951 goto repeat;
1952 }
1953 }
1954
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001955 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001956
Chao Yu08b39fb2015-10-08 13:27:34 +08001957 /* wait for GCed encrypted page writeback */
1958 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001959 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001960
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001961 if (len == PAGE_SIZE || PageUptodate(page))
1962 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001963
Yunlei He746e2402016-12-20 11:11:35 +08001964 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
1965 zero_user_segment(page, len, PAGE_SIZE);
1966 return 0;
1967 }
1968
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001969 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001970 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001971 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001972 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001973 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001974
Chao Yu78682f72016-07-03 22:05:11 +08001975 bio = f2fs_grab_bio(inode, blkaddr, 1);
1976 if (IS_ERR(bio)) {
1977 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001978 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001979 }
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001980 bio->bi_opf = REQ_OP_READ;
Chao Yu78682f72016-07-03 22:05:11 +08001981 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1982 bio_put(bio);
1983 err = -EFAULT;
1984 goto fail;
1985 }
1986
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001987 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001988
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001989 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001990 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001991 f2fs_put_page(page, 1);
1992 goto repeat;
1993 }
Chao Yu1563ac72016-07-03 22:05:12 +08001994 if (unlikely(!PageUptodate(page))) {
1995 err = -EIO;
1996 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001997 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001998 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001999 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002000
Chao Yu3aab8f82014-07-02 13:25:04 +08002001fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002002 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002003 f2fs_write_failed(mapping, pos + len);
2004 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002005}
2006
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002007static int f2fs_write_end(struct file *file,
2008 struct address_space *mapping,
2009 loff_t pos, unsigned len, unsigned copied,
2010 struct page *page, void *fsdata)
2011{
2012 struct inode *inode = page->mapping->host;
2013
Chao Yudfb2bf32014-05-06 16:47:23 +08002014 trace_f2fs_write_end(inode, pos, len, copied);
2015
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002016 /*
2017 * This should be come from len == PAGE_SIZE, and we expect copied
2018 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2019 * let generic_perform_write() try to copy data again through copied=0.
2020 */
2021 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08002022 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002023 copied = 0;
2024 else
2025 SetPageUptodate(page);
2026 }
2027 if (!copied)
2028 goto unlock_out;
2029
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002030 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002031
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002032 if (pos + copied > i_size_read(inode))
2033 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002034unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002035 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002036 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002037 return copied;
2038}
2039
Omar Sandoval6f673762015-03-16 04:33:52 -07002040static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2041 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002042{
2043 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002044
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002045 if (offset & blocksize_mask)
2046 return -EINVAL;
2047
Al Viro5b46f252014-03-16 18:07:34 -04002048 if (iov_iter_alignment(iter) & blocksize_mask)
2049 return -EINVAL;
2050
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002051 return 0;
2052}
2053
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002054static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002055{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002056 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002057 struct inode *inode = mapping->host;
2058 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002059 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002060 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002061 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002062
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002063 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002064 if (err)
2065 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002066
Jaegeuk Kimc040ff92016-11-11 16:46:40 -08002067 if (__force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002068 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002069
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002070 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fad2014-07-31 21:11:22 +08002071
Chao Yu82e0a5a2016-07-13 09:18:29 +08002072 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002073 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002074 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
2075
2076 if (rw == WRITE) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002077 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07002078 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002079 else if (err < 0)
2080 f2fs_write_failed(mapping, offset + count);
2081 }
Chao Yu70407fad2014-07-31 21:11:22 +08002082
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002083 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fad2014-07-31 21:11:22 +08002084
Chao Yu3aab8f82014-07-02 13:25:04 +08002085 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002086}
2087
Chao Yu487261f2015-02-05 17:44:29 +08002088void f2fs_invalidate_page(struct page *page, unsigned int offset,
2089 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002090{
2091 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002092 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002093
Chao Yu487261f2015-02-05 17:44:29 +08002094 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002095 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002096 return;
2097
Chao Yu487261f2015-02-05 17:44:29 +08002098 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08002099 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002100 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08002101 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002102 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08002103 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002104 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08002105 remove_dirty_inode(inode);
2106 }
Chao Yu487261f2015-02-05 17:44:29 +08002107 }
Chao Yudecd36b2015-08-07 18:42:09 +08002108
2109 /* This is atomic written page, keep Private */
2110 if (IS_ATOMIC_WRITTEN_PAGE(page))
Jaegeuk Kim8c242db2017-03-17 09:55:52 +08002111 return drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002112
Chao Yu23dc9742016-04-29 20:09:15 +08002113 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002114 ClearPagePrivate(page);
2115}
2116
Chao Yu487261f2015-02-05 17:44:29 +08002117int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002118{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002119 /* If this is dirty page, keep PagePrivate */
2120 if (PageDirty(page))
2121 return 0;
2122
Chao Yudecd36b2015-08-07 18:42:09 +08002123 /* This is atomic written page, keep Private */
2124 if (IS_ATOMIC_WRITTEN_PAGE(page))
2125 return 0;
2126
Chao Yu23dc9742016-04-29 20:09:15 +08002127 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002128 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002129 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002130}
2131
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002132/*
2133 * This was copied from __set_page_dirty_buffers which gives higher performance
2134 * in very high speed storages. (e.g., pmem)
2135 */
2136void f2fs_set_page_dirty_nobuffers(struct page *page)
2137{
2138 struct address_space *mapping = page->mapping;
2139 unsigned long flags;
2140
2141 if (unlikely(!mapping))
2142 return;
2143
2144 spin_lock(&mapping->private_lock);
2145 lock_page_memcg(page);
2146 SetPageDirty(page);
2147 spin_unlock(&mapping->private_lock);
2148
2149 spin_lock_irqsave(&mapping->tree_lock, flags);
2150 WARN_ON_ONCE(!PageUptodate(page));
2151 account_page_dirtied(page, mapping);
2152 radix_tree_tag_set(&mapping->page_tree,
2153 page_index(page), PAGECACHE_TAG_DIRTY);
2154 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2155 unlock_page_memcg(page);
2156
2157 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
2158 return;
2159}
2160
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002161static int f2fs_set_data_page_dirty(struct page *page)
2162{
2163 struct address_space *mapping = page->mapping;
2164 struct inode *inode = mapping->host;
2165
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002166 trace_f2fs_set_page_dirty(page, DATA);
2167
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002168 if (!PageUptodate(page))
2169 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002170
Chao Yu5fe45742017-01-07 18:50:26 +08002171 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002172 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2173 register_inmem_page(inode, page);
2174 return 1;
2175 }
2176 /*
2177 * Previously, this page has been registered, we just
2178 * return here.
2179 */
2180 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002181 }
2182
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002183 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002184 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002185 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002186 return 1;
2187 }
2188 return 0;
2189}
2190
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002191static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2192{
Chao Yu454ae7e2014-04-22 13:34:01 +08002193 struct inode *inode = mapping->host;
2194
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002195 if (f2fs_has_inline_data(inode))
2196 return 0;
2197
2198 /* make sure allocating whole blocks */
2199 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2200 filemap_write_and_wait(mapping);
2201
Chao Yue2b4e2b2015-08-19 19:11:19 +08002202 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002203}
2204
Weichao Guo5b7a4872016-09-20 05:03:27 +08002205#ifdef CONFIG_MIGRATION
2206#include <linux/migrate.h>
2207
2208int f2fs_migrate_page(struct address_space *mapping,
2209 struct page *newpage, struct page *page, enum migrate_mode mode)
2210{
2211 int rc, extra_count;
2212 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2213 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2214
2215 BUG_ON(PageWriteback(page));
2216
2217 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07002218 if (atomic_written) {
2219 if (mode != MIGRATE_SYNC)
2220 return -EBUSY;
2221 if (!mutex_trylock(&fi->inmem_lock))
2222 return -EAGAIN;
2223 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002224
2225 /*
2226 * A reference is expected if PagePrivate set when move mapping,
2227 * however F2FS breaks this for maintaining dirty page counts when
2228 * truncating pages. So here adjusting the 'extra_count' make it work.
2229 */
2230 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2231 rc = migrate_page_move_mapping(mapping, newpage,
2232 page, NULL, mode, extra_count);
2233 if (rc != MIGRATEPAGE_SUCCESS) {
2234 if (atomic_written)
2235 mutex_unlock(&fi->inmem_lock);
2236 return rc;
2237 }
2238
2239 if (atomic_written) {
2240 struct inmem_pages *cur;
2241 list_for_each_entry(cur, &fi->inmem_pages, list)
2242 if (cur->page == page) {
2243 cur->page = newpage;
2244 break;
2245 }
2246 mutex_unlock(&fi->inmem_lock);
2247 put_page(page);
2248 get_page(newpage);
2249 }
2250
2251 if (PagePrivate(page))
2252 SetPagePrivate(newpage);
2253 set_page_private(newpage, page_private(page));
2254
2255 migrate_page_copy(newpage, page);
2256
2257 return MIGRATEPAGE_SUCCESS;
2258}
2259#endif
2260
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002261const struct address_space_operations f2fs_dblock_aops = {
2262 .readpage = f2fs_read_data_page,
2263 .readpages = f2fs_read_data_pages,
2264 .writepage = f2fs_write_data_page,
2265 .writepages = f2fs_write_data_pages,
2266 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002267 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002268 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002269 .invalidatepage = f2fs_invalidate_page,
2270 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002271 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002272 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002273#ifdef CONFIG_MIGRATION
2274 .migratepage = f2fs_migrate_page,
2275#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002276};