blob: f1c37d695051eee1a0946f5da2119a12864e4a5f [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>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090025
26#include "f2fs.h"
27#include "node.h"
28#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080029#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090030#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090031
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070032static bool __is_cp_guaranteed(struct page *page)
33{
34 struct address_space *mapping = page->mapping;
35 struct inode *inode;
36 struct f2fs_sb_info *sbi;
37
38 if (!mapping)
39 return false;
40
41 inode = mapping->host;
42 sbi = F2FS_I_SB(inode);
43
44 if (inode->i_ino == F2FS_META_INO(sbi) ||
45 inode->i_ino == F2FS_NODE_INO(sbi) ||
46 S_ISDIR(inode->i_mode) ||
47 is_cold_data(page))
48 return true;
49 return false;
50}
51
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020052static void f2fs_read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090053{
Linus Torvaldsf5688492014-01-30 11:19:05 -080054 struct bio_vec *bvec;
55 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090056
Chao Yu8b038c72016-09-18 23:30:07 +080057#ifdef CONFIG_F2FS_FAULT_INJECTION
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070058 if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_IO)) {
59 f2fs_show_injection_info(FAULT_IO);
Chao Yu8b038c72016-09-18 23:30:07 +080060 bio->bi_error = -EIO;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070061 }
Chao Yu8b038c72016-09-18 23:30:07 +080062#endif
63
Chao Yu12377022015-05-25 18:03:38 +080064 if (f2fs_bio_encrypted(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020065 if (bio->bi_error) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070066 fscrypt_release_ctx(bio->bi_private);
Chao Yu12377022015-05-25 18:03:38 +080067 } else {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070068 fscrypt_decrypt_bio_pages(bio->bi_private, bio);
Chao Yu12377022015-05-25 18:03:38 +080069 return;
70 }
71 }
72
Linus Torvaldsf5688492014-01-30 11:19:05 -080073 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090074 struct page *page = bvec->bv_page;
75
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020076 if (!bio->bi_error) {
Jaegeuk Kim237c0792016-06-30 18:49:15 -070077 if (!PageUptodate(page))
78 SetPageUptodate(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080079 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090080 ClearPageUptodate(page);
81 SetPageError(page);
82 }
83 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080084 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090085 bio_put(bio);
86}
87
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020088static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090089{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090090 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080091 struct bio_vec *bvec;
92 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090093
Linus Torvaldsf5688492014-01-30 11:19:05 -080094 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090095 struct page *page = bvec->bv_page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070096 enum count_type type = WB_DATA_TYPE(page);
97
98 if (IS_DUMMY_WRITTEN_PAGE(page)) {
99 set_page_private(page, (unsigned long)NULL);
100 ClearPagePrivate(page);
101 unlock_page(page);
102 mempool_free(page, sbi->write_io_dummy);
103
104 if (unlikely(bio->bi_error))
105 f2fs_stop_checkpoint(sbi, true);
106 continue;
107 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900108
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700109 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700110
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200111 if (unlikely(bio->bi_error)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700112 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700113 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900114 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700115 dec_page_count(sbi, type);
116 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900117 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800118 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700119 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700120 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900121 wake_up(&sbi->cp_wait);
122
123 bio_put(bio);
124}
125
Gu Zheng940a6d32013-12-20 17:39:59 +0800126/*
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700127 * Return true, if pre_bio's bdev is same as its target device.
128 */
129struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
130 block_t blk_addr, struct bio *bio)
131{
132 struct block_device *bdev = sbi->sb->s_bdev;
133 int i;
134
135 for (i = 0; i < sbi->s_ndevs; i++) {
136 if (FDEV(i).start_blk <= blk_addr &&
137 FDEV(i).end_blk >= blk_addr) {
138 blk_addr -= FDEV(i).start_blk;
139 bdev = FDEV(i).bdev;
140 break;
141 }
142 }
143 if (bio) {
144 bio->bi_bdev = bdev;
145 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
146 }
147 return bdev;
148}
149
150int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
151{
152 int i;
153
154 for (i = 0; i < sbi->s_ndevs; i++)
155 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
156 return i;
157 return 0;
158}
159
160static bool __same_bdev(struct f2fs_sb_info *sbi,
161 block_t blk_addr, struct bio *bio)
162{
163 return f2fs_target_device(sbi, blk_addr, NULL) == bio->bi_bdev;
164}
165
166/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800167 * Low-level block read/write IO operations.
168 */
169static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
170 int npages, bool is_read)
171{
172 struct bio *bio;
173
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700174 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +0800175
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700176 f2fs_target_device(sbi, blk_addr, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800177 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +0800178 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800179
180 return bio;
181}
182
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700183static inline void __submit_bio(struct f2fs_sb_info *sbi,
184 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700185{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700186 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700187 unsigned int start;
188
189 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700190 current->plug && (type == DATA || type == NODE))
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700191 blk_finish_plug(current->plug);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700192
193 if (type != DATA && type != NODE)
194 goto submit_io;
195
196 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
197 start %= F2FS_IO_SIZE(sbi);
198
199 if (start == 0)
200 goto submit_io;
201
202 /* fill dummy pages */
203 for (; start < F2FS_IO_SIZE(sbi); start++) {
204 struct page *page =
205 mempool_alloc(sbi->write_io_dummy,
206 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
207 f2fs_bug_on(sbi, !page);
208
209 SetPagePrivate(page);
210 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
211 lock_page(page);
212 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
213 f2fs_bug_on(sbi, 1);
214 }
215 /*
216 * In the NODE case, we lose next block address chain. So, we
217 * need to do checkpoint in f2fs_sync_file.
218 */
219 if (type == NODE)
220 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700221 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700222submit_io:
223 if (is_read_io(bio_op(bio)))
224 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
225 else
226 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500227 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700228}
229
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900230static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900231{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900232 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900233
234 if (!io->bio)
235 return;
236
Mike Christie04d328d2016-06-05 14:31:55 -0500237 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
238
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700239 if (is_read_io(fio->op))
240 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
241 else
242 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
243
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700244 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900245 io->bio = NULL;
246}
247
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700248static bool __has_merged_page(struct f2fs_bio_info *io,
249 struct inode *inode, nid_t ino, pgoff_t idx)
Chao Yu0fd785e2016-01-18 18:24:59 +0800250{
Chao Yu0fd785e2016-01-18 18:24:59 +0800251 struct bio_vec *bvec;
252 struct page *target;
253 int i;
254
Chao Yu0c3a5792016-01-18 18:28:11 +0800255 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800256 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800257
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700258 if (!inode && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800259 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800260
261 bio_for_each_segment_all(bvec, io->bio, i) {
262
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700263 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800264 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700265 else
266 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800267
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700268 if (idx != target->index)
269 continue;
270
Chao Yu0c3a5792016-01-18 18:28:11 +0800271 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800272 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800273 if (ino && ino == ino_of_node(target))
274 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800275 }
276
Chao Yu0fd785e2016-01-18 18:24:59 +0800277 return false;
278}
279
Chao Yu0c3a5792016-01-18 18:28:11 +0800280static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700281 nid_t ino, pgoff_t idx, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800282{
283 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700284 enum temp_type temp;
285 struct f2fs_bio_info *io;
286 bool ret = false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800287
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700288 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
289 io = sbi->write_io[btype] + temp;
290
291 down_read(&io->io_rwsem);
292 ret = __has_merged_page(io, inode, ino, idx);
293 up_read(&io->io_rwsem);
294
295 /* TODO: use HOT temp only for meta pages now. */
296 if (ret || btype == META)
297 break;
298 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800299 return ret;
300}
301
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700302static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700303 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900304{
305 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700306 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900307
Chao Yudf0f8dc2014-03-22 14:57:23 +0800308 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900309
310 /* change META to META_FLUSH in the checkpoint procedure */
311 if (type >= META_FLUSH) {
312 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500313 io->fio.op = REQ_OP_WRITE;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700314 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
315 if (!test_opt(sbi, NOBARRIER))
316 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900317 }
318 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800319 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900320}
321
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700322static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
323 struct inode *inode, nid_t ino, pgoff_t idx,
324 enum page_type type, bool force)
325{
326 enum temp_type temp;
327
328 if (!force && !has_merged_page(sbi, inode, ino, idx, type))
329 return;
330
331 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
332
333 __f2fs_submit_merged_write(sbi, type, temp);
334
335 /* TODO: use HOT temp only for meta pages now. */
336 if (type >= META)
337 break;
338 }
339}
340
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700341void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800342{
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700343 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
Chao Yu0c3a5792016-01-18 18:28:11 +0800344}
345
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700346void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700347 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700348 enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800349{
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700350 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
Chao Yu0c3a5792016-01-18 18:28:11 +0800351}
352
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700353void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800354{
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700355 f2fs_submit_merged_write(sbi, DATA);
356 f2fs_submit_merged_write(sbi, NODE);
357 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800358}
359
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900360/*
361 * Fill the locked page with data located in the block address.
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700362 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900363 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700364int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900365{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900366 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700367 struct page *page = fio->encrypted_page ?
368 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900369
Chao Yu2ace38e2014-12-24 16:08:14 +0800370 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700371 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900372
373 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500374 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900375
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300376 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900377 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900378 return -EFAULT;
379 }
Mike Christie04d328d2016-06-05 14:31:55 -0500380 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900381
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700382 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700383
384 if (!is_read_io(fio->op))
385 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900386 return 0;
387}
388
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700389int f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900390{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700391 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900392 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700393 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700394 struct page *bio_page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700395 int err = 0;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900396
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700397 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900398
Chao Yuc52dc0f2017-05-19 23:37:01 +0800399 down_write(&io->io_rwsem);
400next:
401 if (fio->in_list) {
402 spin_lock(&io->io_lock);
403 if (list_empty(&io->io_list)) {
404 spin_unlock(&io->io_lock);
405 goto out_fail;
406 }
407 fio = list_first_entry(&io->io_list,
408 struct f2fs_io_info, list);
409 list_del(&fio->list);
410 spin_unlock(&io->io_lock);
411 }
412
Chao Yu7a9d7542016-02-22 18:36:38 +0800413 if (fio->old_blkaddr != NEW_ADDR)
414 verify_block_addr(sbi, fio->old_blkaddr);
415 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900416
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700417 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
418
Thomas Meyer59504672017-10-07 16:02:21 +0200419 /* set submitted = true as a return value */
420 fio->submitted = true;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700421
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700422 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700423
Chao Yu7a9d7542016-02-22 18:36:38 +0800424 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700425 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
426 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900427 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900428alloc_new:
429 if (io->bio == NULL) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700430 if ((fio->type == DATA || fio->type == NODE) &&
431 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
432 err = -EAGAIN;
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700433 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700434 goto out_fail;
435 }
Chao Yu7a9d7542016-02-22 18:36:38 +0800436 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -0700437 BIO_MAX_PAGES, false);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900438 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900439 }
440
Jaegeuk Kimc4127262017-05-10 11:18:25 -0700441 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900442 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900443 goto alloc_new;
444 }
445
Chao Yu7a9d7542016-02-22 18:36:38 +0800446 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700447 f2fs_trace_ios(fio, 0);
Chao Yuc52dc0f2017-05-19 23:37:01 +0800448
449 trace_f2fs_submit_page_write(fio->page, fio);
450
451 if (fio->in_list)
452 goto next;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700453out_fail:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800454 up_write(&io->io_rwsem);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700455 return err;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900456}
457
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700458static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
459 unsigned nr_pages)
460{
461 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
462 struct fscrypt_ctx *ctx = NULL;
463 struct bio *bio;
464
465 if (f2fs_encrypted_file(inode)) {
466 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
467 if (IS_ERR(ctx))
468 return ERR_CAST(ctx);
469
470 /* wait the page to be moved by cleaning */
471 f2fs_wait_on_block_writeback(sbi, blkaddr);
472 }
473
474 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
475 if (!bio) {
476 if (ctx)
477 fscrypt_release_ctx(ctx);
478 return ERR_PTR(-ENOMEM);
479 }
480 f2fs_target_device(sbi, blkaddr, bio);
481 bio->bi_end_io = f2fs_read_end_io;
482 bio->bi_private = ctx;
483 bio_set_op_attrs(bio, REQ_OP_READ, 0);
484
485 return bio;
486}
487
488/* This can handle encryption stuffs */
489static int f2fs_submit_page_read(struct inode *inode, struct page *page,
490 block_t blkaddr)
491{
492 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1);
493
494 if (IS_ERR(bio))
495 return PTR_ERR(bio);
496
497 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
498 bio_put(bio);
499 return -EFAULT;
500 }
501 __submit_bio(F2FS_I_SB(inode), bio, DATA);
502 return 0;
503}
504
Chao Yu46008c62016-05-09 19:56:30 +0800505static void __set_data_blkaddr(struct dnode_of_data *dn)
506{
507 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
508 __le32 *addr_array;
Chao Yufbcf9312017-07-19 00:19:06 +0800509 int base = 0;
510
511 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
512 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800513
514 /* Get physical address of data block */
515 addr_array = blkaddr_in_node(rn);
Chao Yufbcf9312017-07-19 00:19:06 +0800516 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800517}
518
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900519/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900520 * Lock ordering for the change of data block address:
521 * ->data_page
522 * ->node_page
523 * update block addresses in the node page
524 */
Chao Yu216a6202015-03-19 19:23:32 +0800525void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900526{
Chao Yu46008c62016-05-09 19:56:30 +0800527 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
528 __set_data_blkaddr(dn);
529 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800530 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900531}
532
Chao Yuf28b3432016-02-24 17:16:47 +0800533void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
534{
535 dn->data_blkaddr = blkaddr;
536 set_data_blkaddr(dn);
537 f2fs_update_extent_cache(dn);
538}
539
Chao Yu46008c62016-05-09 19:56:30 +0800540/* dn->ofs_in_node will be returned with up-to-date last block pointer */
541int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900542{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700543 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu09c3a722017-07-09 00:13:07 +0800544 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900545
Chao Yu46008c62016-05-09 19:56:30 +0800546 if (!count)
547 return 0;
548
Jaegeuk Kim91942322016-05-20 10:13:22 -0700549 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900550 return -EPERM;
Chao Yu09c3a722017-07-09 00:13:07 +0800551 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
552 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900553
Chao Yu46008c62016-05-09 19:56:30 +0800554 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
555 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900556
Chao Yu46008c62016-05-09 19:56:30 +0800557 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
558
559 for (; count > 0; dn->ofs_in_node++) {
Chao Yufbcf9312017-07-19 00:19:06 +0800560 block_t blkaddr = datablock_addr(dn->inode,
561 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800562 if (blkaddr == NULL_ADDR) {
563 dn->data_blkaddr = NEW_ADDR;
564 __set_data_blkaddr(dn);
565 count--;
566 }
567 }
568
569 if (set_page_dirty(dn->node_page))
570 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900571 return 0;
572}
573
Chao Yu46008c62016-05-09 19:56:30 +0800574/* Should keep dn->ofs_in_node unchanged */
575int reserve_new_block(struct dnode_of_data *dn)
576{
577 unsigned int ofs_in_node = dn->ofs_in_node;
578 int ret;
579
580 ret = reserve_new_blocks(dn, 1);
581 dn->ofs_in_node = ofs_in_node;
582 return ret;
583}
584
Huajun Lib6009652013-11-10 23:13:18 +0800585int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
586{
587 bool need_put = dn->inode_page ? false : true;
588 int err;
589
590 err = get_dnode_of_data(dn, index, ALLOC_NODE);
591 if (err)
592 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900593
Huajun Lib6009652013-11-10 23:13:18 +0800594 if (dn->data_blkaddr == NULL_ADDR)
595 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900596 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800597 f2fs_put_dnode(dn);
598 return err;
599}
600
Fan Li759af1c2015-08-05 15:52:16 +0800601int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900602{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700603 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800604 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800605
Fan Li759af1c2015-08-05 15:52:16 +0800606 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
607 dn->data_blkaddr = ei.blk + index - ei.fofs;
608 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800609 }
610
Fan Li759af1c2015-08-05 15:52:16 +0800611 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900612}
613
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700614struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500615 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900616{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900617 struct address_space *mapping = inode->i_mapping;
618 struct dnode_of_data dn;
619 struct page *page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700620 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900621 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700622
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700623 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900624 if (!page)
625 return ERR_PTR(-ENOMEM);
626
Chao Yucb3bc9e2015-02-05 18:03:40 +0800627 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
628 dn.data_blkaddr = ei.blk + index - ei.fofs;
629 goto got_it;
630 }
631
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900632 set_new_dnode(&dn, inode, NULL, NULL, 0);
633 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700634 if (err)
635 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900636 f2fs_put_dnode(&dn);
637
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900638 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700639 err = -ENOENT;
640 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900641 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800642got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700643 if (PageUptodate(page)) {
644 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900645 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700646 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900647
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900648 /*
649 * A new dentry page is allocated but not able to be written, since its
650 * new inode page couldn't be allocated due to -ENOSPC.
651 * In such the case, its blkaddr can be remained as NEW_ADDR.
652 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
653 */
654 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300655 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700656 if (!PageUptodate(page))
657 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700658 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900659 return page;
660 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900661
Jaegeuk Kim5a690672017-09-06 21:04:44 -0700662 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900663 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700664 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700665 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700666
667put_err:
668 f2fs_put_page(page, 1);
669 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700670}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900671
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700672struct page *find_data_page(struct inode *inode, pgoff_t index)
673{
674 struct address_space *mapping = inode->i_mapping;
675 struct page *page;
676
677 page = find_get_page(mapping, index);
678 if (page && PageUptodate(page))
679 return page;
680 f2fs_put_page(page, 0);
681
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700682 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700683 if (IS_ERR(page))
684 return page;
685
686 if (PageUptodate(page))
687 return page;
688
689 wait_on_page_locked(page);
690 if (unlikely(!PageUptodate(page))) {
691 f2fs_put_page(page, 0);
692 return ERR_PTR(-EIO);
693 }
694 return page;
695}
696
697/*
698 * If it tries to access a hole, return an error.
699 * Because, the callers, functions in dir.c and GC, should be able to know
700 * whether this page exists or not.
701 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700702struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
703 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700704{
705 struct address_space *mapping = inode->i_mapping;
706 struct page *page;
707repeat:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700708 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700709 if (IS_ERR(page))
710 return page;
711
712 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900713 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900714 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900715 f2fs_put_page(page, 1);
716 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900717 }
Chao Yu1563ac72016-07-03 22:05:12 +0800718 if (unlikely(!PageUptodate(page))) {
719 f2fs_put_page(page, 1);
720 return ERR_PTR(-EIO);
721 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900722 return page;
723}
724
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900725/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900726 * Caller ensures that this data page is never allocated.
727 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900728 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800729 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
730 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800731 * Note that, ipage is set only by make_empty_dir, and if any error occur,
732 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900733 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900734struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900735 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900736{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900737 struct address_space *mapping = inode->i_mapping;
738 struct page *page;
739 struct dnode_of_data dn;
740 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800741
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700742 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800743 if (!page) {
744 /*
745 * before exiting, we should make sure ipage will be released
746 * if any error occur.
747 */
748 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700749 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800750 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900751
Jaegeuk Kima8865372013-12-27 17:04:17 +0900752 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800753 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700754 if (err) {
755 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900756 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900757 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700758 if (!ipage)
759 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900760
761 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700762 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900763
764 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300765 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700766 if (!PageUptodate(page))
767 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900768 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700769 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900770
Jaegeuk Kim76121182016-01-01 22:03:47 -0800771 /* if ipage exists, blkaddr should be NEW_ADDR */
772 f2fs_bug_on(F2FS_I_SB(inode), ipage);
773 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700774 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800775 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900776 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700777got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800778 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700779 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700780 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900781 return page;
782}
783
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900784static int __allocate_data_block(struct dnode_of_data *dn)
785{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700786 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900787 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900788 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700789 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800790 blkcnt_t count = 1;
Chao Yu09c3a722017-07-09 00:13:07 +0800791 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900792
Jaegeuk Kim91942322016-05-20 10:13:22 -0700793 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900794 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800795
Chao Yufbcf9312017-07-19 00:19:06 +0800796 dn->data_blkaddr = datablock_addr(dn->inode,
797 dn->node_page, dn->ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800798 if (dn->data_blkaddr == NEW_ADDR)
799 goto alloc;
800
Chao Yu09c3a722017-07-09 00:13:07 +0800801 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
802 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900803
Chao Yudf6136e2015-03-23 10:33:37 +0800804alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900805 get_node_info(sbi, dn->nid, &ni);
806 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
807
Chao Yudf6136e2015-03-23 10:33:37 +0800808 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Chao Yuc52dc0f2017-05-19 23:37:01 +0800809 &sum, CURSEG_WARM_DATA, NULL, false);
Chao Yu216a6202015-03-19 19:23:32 +0800810 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900811
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700812 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800813 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700814 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300815 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700816 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300817 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900818 return 0;
819}
820
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700821static inline bool __force_buffered_io(struct inode *inode, int rw)
822{
Jaegeuk Kima4fb1892017-09-05 16:54:24 -0700823 return (f2fs_encrypted_file(inode) ||
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700824 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
825 F2FS_I_SB(inode)->s_ndevs);
826}
827
828int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800829{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800830 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800831 struct f2fs_map_blocks map;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700832 int err = 0;
833
Weichao Guo8456d342017-09-29 22:43:23 +0800834 /* convert inline data for Direct I/O*/
835 if (iocb->ki_flags & IOCB_DIRECT) {
836 err = f2fs_convert_inline_inode(inode);
837 if (err)
838 return err;
839 }
840
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700841 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
842 return 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800843
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700844 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800845 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
846 if (map.m_len > map.m_lblk)
847 map.m_len -= map.m_lblk;
848 else
849 map.m_len = 0;
850
Chao Yuda859852016-01-26 15:42:58 +0800851 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800852
Weichao Guo8456d342017-09-29 22:43:23 +0800853 if (iocb->ki_flags & IOCB_DIRECT)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700854 return f2fs_map_blocks(inode, &map, 1,
855 __force_buffered_io(inode, WRITE) ?
856 F2FS_GET_BLOCK_PRE_AIO :
857 F2FS_GET_BLOCK_PRE_DIO);
Chao Yue84f88e2017-07-19 00:19:05 +0800858 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700859 err = f2fs_convert_inline_inode(inode);
860 if (err)
861 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800862 }
863 if (!f2fs_has_inline_data(inode))
864 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700865 return err;
866}
867
868static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
869{
870 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
871 if (lock)
872 down_read(&sbi->node_change);
873 else
874 up_read(&sbi->node_change);
875 } else {
876 if (lock)
877 f2fs_lock_op(sbi);
878 else
879 f2fs_unlock_op(sbi);
880 }
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800881}
882
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900883/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700884 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
885 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800886 * If original data blocks are allocated, then give them to blockdev.
887 * Otherwise,
888 * a. preallocate requested block addresses
889 * b. do not use extent cache for better performance
890 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900891 */
Chao Yud323d002015-10-27 09:53:45 +0800892int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800893 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900894{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700895 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900896 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800897 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700898 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800899 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900900 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800901 unsigned int ofs_in_node, last_ofs_in_node;
902 blkcnt_t prealloc;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700903 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +0800904 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900905
Chao Yudfd02e42016-08-20 15:12:01 +0800906 if (!maxblocks)
907 return 0;
908
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700909 map->m_len = 0;
910 map->m_flags = 0;
911
912 /* it only supports block size == page size */
913 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800914 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900915
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800916 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700917 map->m_pblk = ei.blk + pgofs - ei.fofs;
918 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
919 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900920 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800921 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900922
Chao Yu4fe71e82016-01-26 15:37:38 +0800923next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800924 if (create)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700925 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900926
927 /* When reading holes, we need its node page */
928 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900929 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900930 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800931 if (flag == F2FS_GET_BLOCK_BMAP)
932 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800933 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900934 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800935 if (map->m_next_pgofs)
936 *map->m_next_pgofs =
937 get_next_page_offset(&dn, pgofs);
938 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900939 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900940 }
Chao Yu973163f2015-09-18 16:51:51 +0800941
Chao Yu46008c62016-05-09 19:56:30 +0800942 prealloc = 0;
Arnd Bergmanna299abd2016-11-02 14:52:15 +0100943 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800944 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900945
Chao Yu4fe71e82016-01-26 15:37:38 +0800946next_block:
Chao Yufbcf9312017-07-19 00:19:06 +0800947 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800948
Fan Li7df3a432015-12-17 13:20:59 +0800949 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
950 if (create) {
951 if (unlikely(f2fs_cp_error(sbi))) {
952 err = -EIO;
953 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800954 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800955 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800956 if (blkaddr == NULL_ADDR) {
957 prealloc++;
958 last_ofs_in_node = dn.ofs_in_node;
959 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800960 } else {
961 err = __allocate_data_block(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700962 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700963 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800964 }
Fan Li7df3a432015-12-17 13:20:59 +0800965 if (err)
966 goto sync_out;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700967 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800968 blkaddr = dn.data_blkaddr;
969 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800970 if (flag == F2FS_GET_BLOCK_BMAP) {
971 map->m_pblk = 0;
972 goto sync_out;
973 }
Chao Yuda859852016-01-26 15:42:58 +0800974 if (flag == F2FS_GET_BLOCK_FIEMAP &&
975 blkaddr == NULL_ADDR) {
976 if (map->m_next_pgofs)
977 *map->m_next_pgofs = pgofs + 1;
978 }
Fan Li7df3a432015-12-17 13:20:59 +0800979 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800980 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800981 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900982 }
983 }
Fan Li7df3a432015-12-17 13:20:59 +0800984
Chao Yu46008c62016-05-09 19:56:30 +0800985 if (flag == F2FS_GET_BLOCK_PRE_AIO)
986 goto skip;
987
Chao Yu4fe71e82016-01-26 15:37:38 +0800988 if (map->m_len == 0) {
989 /* preallocated unwritten block should be mapped for fiemap. */
990 if (blkaddr == NEW_ADDR)
991 map->m_flags |= F2FS_MAP_UNWRITTEN;
992 map->m_flags |= F2FS_MAP_MAPPED;
993
994 map->m_pblk = blkaddr;
995 map->m_len = 1;
996 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800997 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800998 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800999 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001000 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001001 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001002 } else {
1003 goto sync_out;
1004 }
1005
Chao Yu46008c62016-05-09 19:56:30 +08001006skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001007 dn.ofs_in_node++;
1008 pgofs++;
1009
Chao Yu46008c62016-05-09 19:56:30 +08001010 /* preallocate blocks in batch for one dnode page */
1011 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1012 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001013
Chao Yu46008c62016-05-09 19:56:30 +08001014 dn.ofs_in_node = ofs_in_node;
1015 err = reserve_new_blocks(&dn, prealloc);
1016 if (err)
1017 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001018
Chao Yu46008c62016-05-09 19:56:30 +08001019 map->m_len += dn.ofs_in_node - ofs_in_node;
1020 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1021 err = -ENOSPC;
1022 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001023 }
Chao Yu46008c62016-05-09 19:56:30 +08001024 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001025 }
1026
Chao Yu46008c62016-05-09 19:56:30 +08001027 if (pgofs >= end)
1028 goto sync_out;
1029 else if (dn.ofs_in_node < end_offset)
1030 goto next_block;
1031
Chao Yu46008c62016-05-09 19:56:30 +08001032 f2fs_put_dnode(&dn);
1033
1034 if (create) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001035 __do_map_lock(sbi, flag, false);
1036 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001037 }
Chao Yu46008c62016-05-09 19:56:30 +08001038 goto next_dnode;
1039
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001040sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001041 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001042unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001043 if (create) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001044 __do_map_lock(sbi, flag, false);
1045 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001046 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001047out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001048 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001049 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001050}
1051
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001052static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001053 struct buffer_head *bh, int create, int flag,
1054 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001055{
1056 struct f2fs_map_blocks map;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001057 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001058
1059 map.m_lblk = iblock;
1060 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001061 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001062
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001063 err = f2fs_map_blocks(inode, &map, create, flag);
1064 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001065 map_bh(bh, inode->i_sb, map.m_pblk);
1066 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001067 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001068 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001069 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001070}
1071
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001072static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001073 struct buffer_head *bh_result, int create, int flag,
1074 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001075{
Chao Yuda859852016-01-26 15:42:58 +08001076 return __get_data_block(inode, iblock, bh_result, create,
1077 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001078}
1079
Chao Yue2b4e2b2015-08-19 19:11:19 +08001080static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001081 struct buffer_head *bh_result, int create)
1082{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001083 return __get_data_block(inode, iblock, bh_result, create,
Qiuyang Sun9afa1592017-08-09 17:27:30 +08001084 F2FS_GET_BLOCK_DEFAULT, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +08001085}
1086
1087static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1088 struct buffer_head *bh_result, int create)
1089{
Yunlei He179448b2015-12-28 21:48:32 +08001090 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001091 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001092 return -EFBIG;
1093
Chao Yue2b4e2b2015-08-19 19:11:19 +08001094 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +08001095 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001096}
1097
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001098static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1099{
1100 return (offset >> inode->i_blkbits);
1101}
1102
1103static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1104{
1105 return (blk << inode->i_blkbits);
1106}
1107
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001108int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1109 u64 start, u64 len)
1110{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001111 struct buffer_head map_bh;
1112 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001113 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001114 u64 logical = 0, phys = 0, size = 0;
1115 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001116 int ret = 0;
1117
1118 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1119 if (ret)
1120 return ret;
1121
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -07001122 if (f2fs_has_inline_data(inode)) {
1123 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1124 if (ret != -EAGAIN)
1125 return ret;
1126 }
1127
Al Viro59551022016-01-22 15:40:57 -05001128 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +08001129
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001130 if (logical_to_blk(inode, len) == 0)
1131 len = blk_to_logical(inode, 1);
1132
1133 start_blk = logical_to_blk(inode, start);
1134 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001135
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001136next:
1137 memset(&map_bh, 0, sizeof(struct buffer_head));
1138 map_bh.b_size = len;
1139
Chao Yue2b4e2b2015-08-19 19:11:19 +08001140 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001141 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001142 if (ret)
1143 goto out;
1144
1145 /* HOLE */
1146 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001147 start_blk = next_pgofs;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001148
1149 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1150 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001151 goto prep_next;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001152
Fan Li9a950d52015-12-26 18:07:41 +08001153 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001154 }
Fan Li9a950d52015-12-26 18:07:41 +08001155
Chao Yuda5af122016-01-08 20:19:27 +08001156 if (size) {
1157 if (f2fs_encrypted_inode(inode))
1158 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1159
Fan Li9a950d52015-12-26 18:07:41 +08001160 ret = fiemap_fill_next_extent(fieinfo, logical,
1161 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001162 }
Fan Li9a950d52015-12-26 18:07:41 +08001163
1164 if (start_blk > last_blk || ret)
1165 goto out;
1166
1167 logical = blk_to_logical(inode, start_blk);
1168 phys = blk_to_logical(inode, map_bh.b_blocknr);
1169 size = map_bh.b_size;
1170 flags = 0;
1171 if (buffer_unwritten(&map_bh))
1172 flags = FIEMAP_EXTENT_UNWRITTEN;
1173
1174 start_blk += logical_to_blk(inode, size);
1175
1176prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001177 cond_resched();
1178 if (fatal_signal_pending(current))
1179 ret = -EINTR;
1180 else
1181 goto next;
1182out:
1183 if (ret == 1)
1184 ret = 0;
1185
Al Viro59551022016-01-22 15:40:57 -05001186 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001187 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001188}
1189
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001190/*
1191 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1192 * Major change was from block_size == page_size in f2fs by default.
1193 */
1194static int f2fs_mpage_readpages(struct address_space *mapping,
1195 struct list_head *pages, struct page *page,
1196 unsigned nr_pages)
1197{
1198 struct bio *bio = NULL;
1199 unsigned page_idx;
1200 sector_t last_block_in_bio = 0;
1201 struct inode *inode = mapping->host;
1202 const unsigned blkbits = inode->i_blkbits;
1203 const unsigned blocksize = 1 << blkbits;
1204 sector_t block_in_file;
1205 sector_t last_block;
1206 sector_t last_block_in_file;
1207 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001208 struct f2fs_map_blocks map;
1209
1210 map.m_pblk = 0;
1211 map.m_lblk = 0;
1212 map.m_len = 0;
1213 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001214 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001215
1216 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1217
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001218 if (pages) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001219 page = list_last_entry(pages, struct page, lru);
1220
1221 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001222 list_del(&page->lru);
1223 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001224 page->index,
1225 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001226 goto next_page;
1227 }
1228
1229 block_in_file = (sector_t)page->index;
1230 last_block = block_in_file + nr_pages;
1231 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1232 blkbits;
1233 if (last_block > last_block_in_file)
1234 last_block = last_block_in_file;
1235
1236 /*
1237 * Map blocks using the previous result first.
1238 */
1239 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1240 block_in_file > map.m_lblk &&
1241 block_in_file < (map.m_lblk + map.m_len))
1242 goto got_it;
1243
1244 /*
1245 * Then do more f2fs_map_blocks() calls until we are
1246 * done with this page.
1247 */
1248 map.m_flags = 0;
1249
1250 if (block_in_file < last_block) {
1251 map.m_lblk = block_in_file;
1252 map.m_len = last_block - block_in_file;
1253
Chao Yu46c9e142015-09-18 16:54:16 +08001254 if (f2fs_map_blocks(inode, &map, 0,
Qiuyang Sun9afa1592017-08-09 17:27:30 +08001255 F2FS_GET_BLOCK_DEFAULT))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001256 goto set_error_page;
1257 }
1258got_it:
1259 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1260 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1261 SetPageMappedToDisk(page);
1262
1263 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1264 SetPageUptodate(page);
1265 goto confused;
1266 }
1267 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001268 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001269 if (!PageUptodate(page))
1270 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001271 unlock_page(page);
1272 goto next_page;
1273 }
1274
1275 /*
1276 * This page will go to BIO. Do we need to send this
1277 * BIO off first?
1278 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001279 if (bio && (last_block_in_bio != block_nr - 1 ||
1280 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001281submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001282 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001283 bio = NULL;
1284 }
1285 if (bio == NULL) {
Jaegeuk Kim5a690672017-09-06 21:04:44 -07001286 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001287 if (IS_ERR(bio)) {
1288 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001289 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001290 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001291 }
1292
1293 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1294 goto submit_and_realloc;
1295
1296 last_block_in_bio = block_nr;
1297 goto next_page;
1298set_error_page:
1299 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001300 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001301 unlock_page(page);
1302 goto next_page;
1303confused:
1304 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001305 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001306 bio = NULL;
1307 }
1308 unlock_page(page);
1309next_page:
1310 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001311 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001312 }
1313 BUG_ON(pages && !list_empty(pages));
1314 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001315 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001316 return 0;
1317}
1318
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001319static int f2fs_read_data_page(struct file *file, struct page *page)
1320{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001321 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001322 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001323
Chao Yuc20e89c2014-05-06 16:53:08 +08001324 trace_f2fs_readpage(page, DATA);
1325
arter97e1c42042014-08-06 23:22:50 +09001326 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001327 if (f2fs_has_inline_data(inode))
1328 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001329 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001330 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001331 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001332}
1333
1334static int f2fs_read_data_pages(struct file *file,
1335 struct address_space *mapping,
1336 struct list_head *pages, unsigned nr_pages)
1337{
Hsiang Kao3f01af72017-09-24 02:45:42 +08001338 struct inode *inode = mapping->host;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001339 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001340
1341 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001342
1343 /* If the file has inline data, skip readpages */
1344 if (f2fs_has_inline_data(inode))
1345 return 0;
1346
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001347 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001348}
1349
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001350static int encrypt_one_page(struct f2fs_io_info *fio)
1351{
1352 struct inode *inode = fio->page->mapping->host;
1353 gfp_t gfp_flags = GFP_NOFS;
1354
Jaegeuk Kima4fb1892017-09-05 16:54:24 -07001355 if (!f2fs_encrypted_file(inode))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001356 return 0;
1357
1358 /* wait for GCed encrypted page writeback */
Jaegeuk Kimfb605d02017-09-05 17:04:35 -07001359 f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001360
1361retry_encrypt:
1362 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1363 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1364 if (!IS_ERR(fio->encrypted_page))
1365 return 0;
1366
1367 /* flush pending IOs and wait for a while in the ENOMEM case */
1368 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001369 f2fs_flush_merged_writes(fio->sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001370 congestion_wait(BLK_RW_ASYNC, HZ/50);
1371 gfp_flags |= __GFP_NOFAIL;
1372 goto retry_encrypt;
1373 }
1374 return PTR_ERR(fio->encrypted_page);
1375}
1376
1377static inline bool need_inplace_update(struct f2fs_io_info *fio)
1378{
1379 struct inode *inode = fio->page->mapping->host;
1380
1381 if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode))
1382 return false;
1383 if (is_cold_data(fio->page))
1384 return false;
1385 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1386 return false;
1387
1388 return need_inplace_update_policy(inode, fio);
1389}
1390
1391static inline bool valid_ipu_blkaddr(struct f2fs_io_info *fio)
1392{
1393 if (fio->old_blkaddr == NEW_ADDR)
1394 return false;
1395 if (fio->old_blkaddr == NULL_ADDR)
1396 return false;
1397 return true;
1398}
1399
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001400int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001401{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001402 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001403 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001404 struct dnode_of_data dn;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001405 struct extent_info ei = {0,0,0};
1406 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001407 int err = 0;
1408
1409 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001410 if (need_inplace_update(fio) &&
1411 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1412 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
1413
1414 if (valid_ipu_blkaddr(fio)) {
1415 ipu_force = true;
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001416 fio->need_lock = LOCK_DONE;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001417 goto got_it;
1418 }
1419 }
1420
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07001421 /* Deadlock due to between page->lock and f2fs_lock_op */
1422 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1423 return -EAGAIN;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001424
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001425 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001426 if (err)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001427 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001428
Chao Yu28bc1062016-02-06 14:40:34 +08001429 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001430
1431 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001432 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001433 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001434 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001435 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001436got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001437 /*
1438 * If current allocation needs SSR,
1439 * it had better in-place writes for updated data.
1440 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001441 if (ipu_force || (valid_ipu_blkaddr(fio) && need_inplace_update(fio))) {
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001442 err = encrypt_one_page(fio);
1443 if (err)
1444 goto out_writepage;
1445
1446 set_page_writeback(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001447 f2fs_put_dnode(&dn);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001448 if (fio->need_lock == LOCK_REQ)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001449 f2fs_unlock_op(fio->sbi);
1450 err = rewrite_data_page(fio);
1451 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001452 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001453 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001454 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001455
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001456 if (fio->need_lock == LOCK_RETRY) {
1457 if (!f2fs_trylock_op(fio->sbi)) {
1458 err = -EAGAIN;
1459 goto out_writepage;
1460 }
1461 fio->need_lock = LOCK_REQ;
1462 }
1463
1464 err = encrypt_one_page(fio);
1465 if (err)
1466 goto out_writepage;
1467
1468 set_page_writeback(page);
1469
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001470 /* LFS mode write path */
1471 write_data_page(&dn, fio);
1472 trace_f2fs_do_write_data_page(page, OPU);
1473 set_inode_flag(inode, FI_APPEND_WRITE);
1474 if (page->index == 0)
1475 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001476out_writepage:
1477 f2fs_put_dnode(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001478out:
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001479 if (fio->need_lock == LOCK_REQ)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001480 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001481 return err;
1482}
1483
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001484static int __write_data_page(struct page *page, bool *submitted,
Chao Yuc0fe4882017-08-02 23:21:48 +08001485 struct writeback_control *wbc,
1486 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001487{
1488 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001489 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001490 loff_t i_size = i_size_read(inode);
1491 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001492 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001493 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001494 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001495 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001496 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001497 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001498 .sbi = sbi,
Chao Yuf014be82017-09-29 13:59:38 +08001499 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001500 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001501 .op = REQ_OP_WRITE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001502 .op_flags = wbc_to_write_flags(wbc),
1503 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001504 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001505 .encrypted_page = NULL,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001506 .submitted = false,
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001507 .need_lock = LOCK_RETRY,
Chao Yuc0fe4882017-08-02 23:21:48 +08001508 .io_type = io_type,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001509 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001510
Chao Yuecda0de2014-05-06 16:48:26 +08001511 trace_f2fs_writepage(page, DATA);
1512
Chao Yu00cc7ed2017-06-29 23:20:45 +08001513 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1514 goto redirty_out;
1515
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001516 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001517 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001518
1519 /*
1520 * If the offset is out-of-range of file size,
1521 * this page does not have to be written to disk.
1522 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001523 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001524 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001525 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001526
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001527 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001528write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001529 if (f2fs_is_drop_cache(inode))
1530 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001531 /* we should not write 0'th page having journal header */
1532 if (f2fs_is_volatile_file(inode) && (!page->index ||
1533 (!wbc->for_reclaim &&
1534 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001535 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001536
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001537 /* we should bypass data pages to proceed the kworkder jobs */
1538 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001539 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001540 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001541 }
1542
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001543 /* Dentry blocks are controlled by checkpoint */
1544 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001545 fio.need_lock = LOCK_DONE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001546 err = do_write_data_page(&fio);
1547 goto done;
1548 }
1549
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001550 if (!wbc->for_reclaim)
1551 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001552 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001553 goto redirty_out;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001554 else
1555 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001556
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001557 err = -EAGAIN;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001558 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001559 err = f2fs_write_inline_data(inode, page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001560 if (!err)
1561 goto out;
1562 }
1563
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001564 if (err == -EAGAIN) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001565 err = do_write_data_page(&fio);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001566 if (err == -EAGAIN) {
1567 fio.need_lock = LOCK_REQ;
1568 err = do_write_data_page(&fio);
1569 }
1570 }
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001571 if (F2FS_I(inode)->last_disk_size < psize)
1572 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001573
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001574done:
1575 if (err && err != -ENOENT)
1576 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001577
Jaegeuk Kim39936832012-11-22 16:21:29 +09001578out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001579 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001580 if (err)
1581 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001582
1583 if (wbc->for_reclaim) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001584 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001585 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yuc227f912015-12-16 13:09:20 +08001586 remove_dirty_inode(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001587 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001588 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001589
1590 unlock_page(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001591 if (!S_ISDIR(inode->i_mode))
1592 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001593
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001594 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001595 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001596 submitted = NULL;
1597 }
1598
1599 if (submitted)
1600 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001601
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001602 return 0;
1603
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001604redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001605 redirty_page_for_writepage(wbc, page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001606 if (!err)
1607 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001608 unlock_page(page);
1609 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001610}
1611
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001612static int f2fs_write_data_page(struct page *page,
1613 struct writeback_control *wbc)
1614{
Chao Yuc0fe4882017-08-02 23:21:48 +08001615 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001616}
1617
Chao Yu8f46dca2015-07-14 18:56:10 +08001618/*
1619 * This function was copied from write_cche_pages from mm/page-writeback.c.
1620 * The major change is making write step of cold data page separately from
1621 * warm/hot data page.
1622 */
1623static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yuc0fe4882017-08-02 23:21:48 +08001624 struct writeback_control *wbc,
1625 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08001626{
1627 int ret = 0;
1628 int done = 0;
1629 struct pagevec pvec;
1630 int nr_pages;
1631 pgoff_t uninitialized_var(writeback_index);
1632 pgoff_t index;
1633 pgoff_t end; /* Inclusive */
1634 pgoff_t done_index;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001635 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001636 int cycled;
1637 int range_whole = 0;
1638 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001639
1640 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001641
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001642 if (get_dirty_pages(mapping->host) <=
1643 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1644 set_inode_flag(mapping->host, FI_HOT_DATA);
1645 else
1646 clear_inode_flag(mapping->host, FI_HOT_DATA);
1647
Chao Yu8f46dca2015-07-14 18:56:10 +08001648 if (wbc->range_cyclic) {
1649 writeback_index = mapping->writeback_index; /* prev offset */
1650 index = writeback_index;
1651 if (index == 0)
1652 cycled = 1;
1653 else
1654 cycled = 0;
1655 end = -1;
1656 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001657 index = wbc->range_start >> PAGE_SHIFT;
1658 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001659 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1660 range_whole = 1;
1661 cycled = 1; /* ignore range_cyclic tests */
1662 }
1663 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1664 tag = PAGECACHE_TAG_TOWRITE;
1665 else
1666 tag = PAGECACHE_TAG_DIRTY;
1667retry:
1668 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1669 tag_pages_for_writeback(mapping, index, end);
1670 done_index = index;
1671 while (!done && (index <= end)) {
1672 int i;
1673
1674 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1675 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1676 if (nr_pages == 0)
1677 break;
1678
1679 for (i = 0; i < nr_pages; i++) {
1680 struct page *page = pvec.pages[i];
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001681 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001682
1683 if (page->index > end) {
1684 done = 1;
1685 break;
1686 }
1687
1688 done_index = page->index;
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07001689retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08001690 lock_page(page);
1691
1692 if (unlikely(page->mapping != mapping)) {
1693continue_unlock:
1694 unlock_page(page);
1695 continue;
1696 }
1697
1698 if (!PageDirty(page)) {
1699 /* someone wrote it for us */
1700 goto continue_unlock;
1701 }
1702
Chao Yu8f46dca2015-07-14 18:56:10 +08001703 if (PageWriteback(page)) {
1704 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001705 f2fs_wait_on_page_writeback(page,
1706 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001707 else
1708 goto continue_unlock;
1709 }
1710
1711 BUG_ON(PageWriteback(page));
1712 if (!clear_page_dirty_for_io(page))
1713 goto continue_unlock;
1714
Chao Yuc0fe4882017-08-02 23:21:48 +08001715 ret = __write_data_page(page, &submitted, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08001716 if (unlikely(ret)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001717 /*
1718 * keep nr_to_write, since vfs uses this to
1719 * get # of written pages.
1720 */
1721 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1722 unlock_page(page);
1723 ret = 0;
1724 continue;
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07001725 } else if (ret == -EAGAIN) {
1726 ret = 0;
1727 if (wbc->sync_mode == WB_SYNC_ALL) {
1728 cond_resched();
1729 congestion_wait(BLK_RW_ASYNC,
1730 HZ/50);
1731 goto retry_write;
1732 }
1733 continue;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001734 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001735 done_index = page->index + 1;
1736 done = 1;
1737 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001738 } else if (submitted) {
1739 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08001740 }
1741
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001742 /* give a priority to WB_SYNC threads */
1743 if ((atomic_read(&F2FS_M_SB(mapping)->wb_sync_req) ||
1744 --wbc->nr_to_write <= 0) &&
1745 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08001746 done = 1;
1747 break;
1748 }
1749 }
1750 pagevec_release(&pvec);
1751 cond_resched();
1752 }
1753
Chao Yu8f46dca2015-07-14 18:56:10 +08001754 if (!cycled && !done) {
1755 cycled = 1;
1756 index = 0;
1757 end = writeback_index - 1;
1758 goto retry;
1759 }
1760 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1761 mapping->writeback_index = done_index;
1762
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001763 if (last_idx != ULONG_MAX)
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001764 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
1765 0, last_idx, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08001766
Chao Yu8f46dca2015-07-14 18:56:10 +08001767 return ret;
1768}
1769
Chao Yuc0fe4882017-08-02 23:21:48 +08001770int __f2fs_write_data_pages(struct address_space *mapping,
1771 struct writeback_control *wbc,
1772 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001773{
1774 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001775 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001776 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001777 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001778
P J Pcfb185a2013-04-03 11:38:00 +09001779 /* deal with chardevs and other special file */
1780 if (!mapping->a_ops->writepage)
1781 return 0;
1782
Chao Yu6a290542015-07-17 18:02:39 +08001783 /* skip writing if there is no dirty page in this inode */
1784 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1785 return 0;
1786
Chao Yu00cc7ed2017-06-29 23:20:45 +08001787 /* during POR, we don't need to trigger writepage at all. */
1788 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1789 goto skip_write;
1790
Jaegeuk Kima1257022015-10-08 10:40:07 -07001791 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1792 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1793 available_free_memory(sbi, DIRTY_DENTS))
1794 goto skip_write;
1795
Chao Yud323d002015-10-27 09:53:45 +08001796 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001797 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001798 goto skip_write;
1799
Yunlei Hed31c7c32016-02-04 16:14:00 +08001800 trace_f2fs_writepages(mapping->host, wbc, DATA);
1801
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001802 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
1803 if (wbc->sync_mode == WB_SYNC_ALL)
1804 atomic_inc(&sbi->wb_sync_req);
1805 else if (atomic_read(&sbi->wb_sync_req))
1806 goto skip_write;
1807
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001808 blk_start_plug(&plug);
Chao Yuc0fe4882017-08-02 23:21:48 +08001809 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001810 blk_finish_plug(&plug);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001811
1812 if (wbc->sync_mode == WB_SYNC_ALL)
1813 atomic_dec(&sbi->wb_sync_req);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001814 /*
1815 * if some pages were truncated, we cannot guarantee its mapping->host
1816 * to detect pending bios.
1817 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001818
Chao Yuc227f912015-12-16 13:09:20 +08001819 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001820 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001821
1822skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001823 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001824 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001825 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001826}
1827
Chao Yuc0fe4882017-08-02 23:21:48 +08001828static int f2fs_write_data_pages(struct address_space *mapping,
1829 struct writeback_control *wbc)
1830{
1831 struct inode *inode = mapping->host;
1832
1833 return __f2fs_write_data_pages(mapping, wbc,
1834 F2FS_I(inode)->cp_task == current ?
1835 FS_CP_DATA_IO : FS_DATA_IO);
1836}
1837
Chao Yu3aab8f82014-07-02 13:25:04 +08001838static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1839{
1840 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001841 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001842
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001843 if (to > i_size) {
Qiuyang Sun7b23ea12017-05-18 11:06:45 +08001844 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001845 truncate_pagecache(inode, i_size);
1846 truncate_blocks(inode, i_size, true);
Qiuyang Sun7b23ea12017-05-18 11:06:45 +08001847 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3aab8f82014-07-02 13:25:04 +08001848 }
1849}
1850
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001851static int prepare_write_begin(struct f2fs_sb_info *sbi,
1852 struct page *page, loff_t pos, unsigned len,
1853 block_t *blk_addr, bool *node_changed)
1854{
1855 struct inode *inode = page->mapping->host;
1856 pgoff_t index = page->index;
1857 struct dnode_of_data dn;
1858 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001859 bool locked = false;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001860 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001861 int err = 0;
1862
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001863 /*
1864 * we already allocated all the blocks, so we don't need to get
1865 * the block addresses when there is no need to fill the page.
1866 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001867 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
1868 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001869 return 0;
1870
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001871 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001872 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001873 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001874 locked = true;
1875 }
1876restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001877 /* check inline_data */
1878 ipage = get_node_page(sbi, inode->i_ino);
1879 if (IS_ERR(ipage)) {
1880 err = PTR_ERR(ipage);
1881 goto unlock_out;
1882 }
1883
1884 set_new_dnode(&dn, inode, ipage, ipage, 0);
1885
1886 if (f2fs_has_inline_data(inode)) {
Chao Yue84f88e2017-07-19 00:19:05 +08001887 if (pos + len <= MAX_INLINE_DATA(inode)) {
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001888 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001889 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001890 if (inode->i_nlink)
1891 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001892 } else {
1893 err = f2fs_convert_inline_page(&dn, page);
1894 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001895 goto out;
1896 if (dn.data_blkaddr == NULL_ADDR)
1897 err = f2fs_get_block(&dn, index);
1898 }
1899 } else if (locked) {
1900 err = f2fs_get_block(&dn, index);
1901 } else {
1902 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1903 dn.data_blkaddr = ei.blk + index - ei.fofs;
1904 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001905 /* hole case */
1906 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001907 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001908 f2fs_put_dnode(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001909 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
1910 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001911 locked = true;
1912 goto restart;
1913 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001914 }
1915 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001916
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001917 /* convert_inline_page can make node_changed */
1918 *blk_addr = dn.data_blkaddr;
1919 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001920out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001921 f2fs_put_dnode(&dn);
1922unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001923 if (locked)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001924 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001925 return err;
1926}
1927
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001928static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1929 loff_t pos, unsigned len, unsigned flags,
1930 struct page **pagep, void **fsdata)
1931{
1932 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001933 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001934 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001935 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001936 bool need_balance = false;
1937 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001938 int err = 0;
1939
Chao Yu62aed042014-05-06 16:46:04 +08001940 trace_f2fs_write_begin(inode, pos, len, flags);
1941
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001942 /*
1943 * We should check this at this moment to avoid deadlock on inode page
1944 * and #0 page. The locking rule for inline_data conversion should be:
1945 * lock_page(page #0) -> lock_page(inode_page)
1946 */
1947 if (index != 0) {
1948 err = f2fs_convert_inline_inode(inode);
1949 if (err)
1950 goto fail;
1951 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001952repeat:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001953 /*
1954 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1955 * wait_for_stable_page. Will wait that below with our IO control.
1956 */
1957 page = pagecache_get_page(mapping, index,
1958 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08001959 if (!page) {
1960 err = -ENOMEM;
1961 goto fail;
1962 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001963
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001964 *pagep = page;
1965
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001966 err = prepare_write_begin(sbi, page, pos, len,
1967 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001968 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001969 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001970
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001971 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001972 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001973 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001974 lock_page(page);
1975 if (page->mapping != mapping) {
1976 /* The page got truncated from under us */
1977 f2fs_put_page(page, 1);
1978 goto repeat;
1979 }
1980 }
1981
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001982 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001983
Chao Yu08b39fb2015-10-08 13:27:34 +08001984 /* wait for GCed encrypted page writeback */
Jaegeuk Kima4fb1892017-09-05 16:54:24 -07001985 if (f2fs_encrypted_file(inode))
Jaegeuk Kimfb605d02017-09-05 17:04:35 -07001986 f2fs_wait_on_block_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001987
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001988 if (len == PAGE_SIZE || PageUptodate(page))
1989 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001990
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001991 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
1992 zero_user_segment(page, len, PAGE_SIZE);
1993 return 0;
1994 }
1995
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001996 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001997 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001998 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001999 } else {
Jaegeuk Kim5a690672017-09-06 21:04:44 -07002000 err = f2fs_submit_page_read(inode, page, blkaddr);
2001 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08002002 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08002003
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002004 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002005 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002006 f2fs_put_page(page, 1);
2007 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002008 }
Chao Yu1563ac72016-07-03 22:05:12 +08002009 if (unlikely(!PageUptodate(page))) {
2010 err = -EIO;
2011 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002012 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002013 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002014 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002015
Chao Yu3aab8f82014-07-02 13:25:04 +08002016fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002017 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002018 f2fs_write_failed(mapping, pos + len);
2019 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002020}
2021
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002022static int f2fs_write_end(struct file *file,
2023 struct address_space *mapping,
2024 loff_t pos, unsigned len, unsigned copied,
2025 struct page *page, void *fsdata)
2026{
2027 struct inode *inode = page->mapping->host;
2028
Chao Yudfb2bf32014-05-06 16:47:23 +08002029 trace_f2fs_write_end(inode, pos, len, copied);
2030
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002031 /*
2032 * This should be come from len == PAGE_SIZE, and we expect copied
2033 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2034 * let generic_perform_write() try to copy data again through copied=0.
2035 */
2036 if (!PageUptodate(page)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002037 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002038 copied = 0;
2039 else
2040 SetPageUptodate(page);
2041 }
2042 if (!copied)
2043 goto unlock_out;
2044
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002045 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002046
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002047 if (pos + copied > i_size_read(inode))
2048 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002049unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002050 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002051 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002052 return copied;
2053}
2054
Omar Sandoval6f673762015-03-16 04:33:52 -07002055static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2056 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002057{
2058 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002059
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002060 if (offset & blocksize_mask)
2061 return -EINVAL;
2062
Al Viro5b46f252014-03-16 18:07:34 -04002063 if (iov_iter_alignment(iter) & blocksize_mask)
2064 return -EINVAL;
2065
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002066 return 0;
2067}
2068
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002069static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002070{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002071 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002072 struct inode *inode = mapping->host;
2073 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002074 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002075 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002076 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002077
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002078 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002079 if (err)
2080 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002081
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002082 if (__force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002083 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002084
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002085 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002086
Chao Yu82e0a5a2016-07-13 09:18:29 +08002087 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002088 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002089 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
2090
2091 if (rw == WRITE) {
Chao Yuc0fe4882017-08-02 23:21:48 +08002092 if (err > 0) {
2093 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2094 err);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002095 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yuc0fe4882017-08-02 23:21:48 +08002096 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002097 f2fs_write_failed(mapping, offset + count);
Chao Yuc0fe4882017-08-02 23:21:48 +08002098 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002099 }
Chao Yu70407fa2014-07-31 21:11:22 +08002100
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002101 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002102
Chao Yu3aab8f82014-07-02 13:25:04 +08002103 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002104}
2105
Chao Yu487261f2015-02-05 17:44:29 +08002106void f2fs_invalidate_page(struct page *page, unsigned int offset,
2107 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002108{
2109 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002110 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002111
Chao Yu487261f2015-02-05 17:44:29 +08002112 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002113 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002114 return;
2115
Chao Yu487261f2015-02-05 17:44:29 +08002116 if (PageDirty(page)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002117 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002118 dec_page_count(sbi, F2FS_DIRTY_META);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002119 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002120 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002121 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002122 inode_dec_dirty_pages(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002123 remove_dirty_inode(inode);
2124 }
Chao Yu487261f2015-02-05 17:44:29 +08002125 }
Chao Yudecd36b2015-08-07 18:42:09 +08002126
2127 /* This is atomic written page, keep Private */
2128 if (IS_ATOMIC_WRITTEN_PAGE(page))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002129 return drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002130
Chao Yu23dc9742016-04-29 20:09:15 +08002131 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002132 ClearPagePrivate(page);
2133}
2134
Chao Yu487261f2015-02-05 17:44:29 +08002135int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002136{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002137 /* If this is dirty page, keep PagePrivate */
2138 if (PageDirty(page))
2139 return 0;
2140
Chao Yudecd36b2015-08-07 18:42:09 +08002141 /* This is atomic written page, keep Private */
2142 if (IS_ATOMIC_WRITTEN_PAGE(page))
2143 return 0;
2144
Chao Yu23dc9742016-04-29 20:09:15 +08002145 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002146 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002147 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002148}
2149
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002150/*
2151 * This was copied from __set_page_dirty_buffers which gives higher performance
2152 * in very high speed storages. (e.g., pmem)
2153 */
2154void f2fs_set_page_dirty_nobuffers(struct page *page)
2155{
2156 struct address_space *mapping = page->mapping;
2157 unsigned long flags;
2158
2159 if (unlikely(!mapping))
2160 return;
2161
2162 spin_lock(&mapping->private_lock);
2163 lock_page_memcg(page);
2164 SetPageDirty(page);
2165 spin_unlock(&mapping->private_lock);
2166
2167 spin_lock_irqsave(&mapping->tree_lock, flags);
2168 WARN_ON_ONCE(!PageUptodate(page));
2169 account_page_dirtied(page, mapping);
2170 radix_tree_tag_set(&mapping->page_tree,
2171 page_index(page), PAGECACHE_TAG_DIRTY);
2172 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2173 unlock_page_memcg(page);
2174
2175 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
2176 return;
2177}
2178
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002179static int f2fs_set_data_page_dirty(struct page *page)
2180{
2181 struct address_space *mapping = page->mapping;
2182 struct inode *inode = mapping->host;
2183
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002184 trace_f2fs_set_page_dirty(page, DATA);
2185
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002186 if (!PageUptodate(page))
2187 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002188
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002189 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002190 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2191 register_inmem_page(inode, page);
2192 return 1;
2193 }
2194 /*
2195 * Previously, this page has been registered, we just
2196 * return here.
2197 */
2198 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002199 }
2200
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002201 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002202 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002203 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002204 return 1;
2205 }
2206 return 0;
2207}
2208
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002209static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2210{
Chao Yu454ae7e2014-04-22 13:34:01 +08002211 struct inode *inode = mapping->host;
2212
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002213 if (f2fs_has_inline_data(inode))
2214 return 0;
2215
2216 /* make sure allocating whole blocks */
2217 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2218 filemap_write_and_wait(mapping);
2219
Chao Yue2b4e2b2015-08-19 19:11:19 +08002220 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002221}
2222
Weichao Guo5b7a4872016-09-20 05:03:27 +08002223#ifdef CONFIG_MIGRATION
2224#include <linux/migrate.h>
2225
2226int f2fs_migrate_page(struct address_space *mapping,
2227 struct page *newpage, struct page *page, enum migrate_mode mode)
2228{
2229 int rc, extra_count;
2230 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2231 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2232
2233 BUG_ON(PageWriteback(page));
2234
2235 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kim025258e2017-07-06 14:46:01 -07002236 if (atomic_written) {
2237 if (mode != MIGRATE_SYNC)
2238 return -EBUSY;
2239 if (!mutex_trylock(&fi->inmem_lock))
2240 return -EAGAIN;
2241 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002242
2243 /*
2244 * A reference is expected if PagePrivate set when move mapping,
2245 * however F2FS breaks this for maintaining dirty page counts when
2246 * truncating pages. So here adjusting the 'extra_count' make it work.
2247 */
2248 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2249 rc = migrate_page_move_mapping(mapping, newpage,
2250 page, NULL, mode, extra_count);
2251 if (rc != MIGRATEPAGE_SUCCESS) {
2252 if (atomic_written)
2253 mutex_unlock(&fi->inmem_lock);
2254 return rc;
2255 }
2256
2257 if (atomic_written) {
2258 struct inmem_pages *cur;
2259 list_for_each_entry(cur, &fi->inmem_pages, list)
2260 if (cur->page == page) {
2261 cur->page = newpage;
2262 break;
2263 }
2264 mutex_unlock(&fi->inmem_lock);
2265 put_page(page);
2266 get_page(newpage);
2267 }
2268
2269 if (PagePrivate(page))
2270 SetPagePrivate(newpage);
2271 set_page_private(newpage, page_private(page));
2272
2273 migrate_page_copy(newpage, page);
2274
2275 return MIGRATEPAGE_SUCCESS;
2276}
2277#endif
2278
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002279const struct address_space_operations f2fs_dblock_aops = {
2280 .readpage = f2fs_read_data_page,
2281 .readpages = f2fs_read_data_pages,
2282 .writepage = f2fs_write_data_page,
2283 .writepages = f2fs_write_data_pages,
2284 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002285 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002286 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002287 .invalidatepage = f2fs_invalidate_page,
2288 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002289 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002290 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002291#ifdef CONFIG_MIGRATION
2292 .migratepage = f2fs_migrate_page,
2293#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002294};