blob: b80fc885a08dfba82ab1aa54576a943ca9b3277d [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
419 /* set submitted = 1 as a return value */
420 fio->submitted = 1;
421
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
Chao Yu46008c62016-05-09 19:56:30 +0800458static void __set_data_blkaddr(struct dnode_of_data *dn)
459{
460 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
461 __le32 *addr_array;
Chao Yufbcf9312017-07-19 00:19:06 +0800462 int base = 0;
463
464 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
465 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800466
467 /* Get physical address of data block */
468 addr_array = blkaddr_in_node(rn);
Chao Yufbcf9312017-07-19 00:19:06 +0800469 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800470}
471
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900472/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900473 * Lock ordering for the change of data block address:
474 * ->data_page
475 * ->node_page
476 * update block addresses in the node page
477 */
Chao Yu216a6202015-03-19 19:23:32 +0800478void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900479{
Chao Yu46008c62016-05-09 19:56:30 +0800480 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
481 __set_data_blkaddr(dn);
482 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800483 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900484}
485
Chao Yuf28b3432016-02-24 17:16:47 +0800486void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
487{
488 dn->data_blkaddr = blkaddr;
489 set_data_blkaddr(dn);
490 f2fs_update_extent_cache(dn);
491}
492
Chao Yu46008c62016-05-09 19:56:30 +0800493/* dn->ofs_in_node will be returned with up-to-date last block pointer */
494int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900495{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700496 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu09c3a722017-07-09 00:13:07 +0800497 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900498
Chao Yu46008c62016-05-09 19:56:30 +0800499 if (!count)
500 return 0;
501
Jaegeuk Kim91942322016-05-20 10:13:22 -0700502 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900503 return -EPERM;
Chao Yu09c3a722017-07-09 00:13:07 +0800504 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
505 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900506
Chao Yu46008c62016-05-09 19:56:30 +0800507 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
508 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900509
Chao Yu46008c62016-05-09 19:56:30 +0800510 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
511
512 for (; count > 0; dn->ofs_in_node++) {
Chao Yufbcf9312017-07-19 00:19:06 +0800513 block_t blkaddr = datablock_addr(dn->inode,
514 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800515 if (blkaddr == NULL_ADDR) {
516 dn->data_blkaddr = NEW_ADDR;
517 __set_data_blkaddr(dn);
518 count--;
519 }
520 }
521
522 if (set_page_dirty(dn->node_page))
523 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900524 return 0;
525}
526
Chao Yu46008c62016-05-09 19:56:30 +0800527/* Should keep dn->ofs_in_node unchanged */
528int reserve_new_block(struct dnode_of_data *dn)
529{
530 unsigned int ofs_in_node = dn->ofs_in_node;
531 int ret;
532
533 ret = reserve_new_blocks(dn, 1);
534 dn->ofs_in_node = ofs_in_node;
535 return ret;
536}
537
Huajun Lib6009652013-11-10 23:13:18 +0800538int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
539{
540 bool need_put = dn->inode_page ? false : true;
541 int err;
542
543 err = get_dnode_of_data(dn, index, ALLOC_NODE);
544 if (err)
545 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900546
Huajun Lib6009652013-11-10 23:13:18 +0800547 if (dn->data_blkaddr == NULL_ADDR)
548 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900549 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800550 f2fs_put_dnode(dn);
551 return err;
552}
553
Fan Li759af1c2015-08-05 15:52:16 +0800554int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900555{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700556 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800557 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800558
Fan Li759af1c2015-08-05 15:52:16 +0800559 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
560 dn->data_blkaddr = ei.blk + index - ei.fofs;
561 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800562 }
563
Fan Li759af1c2015-08-05 15:52:16 +0800564 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900565}
566
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700567struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500568 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900569{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900570 struct address_space *mapping = inode->i_mapping;
571 struct dnode_of_data dn;
572 struct page *page;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700573 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900574 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800575 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700576 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800577 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500578 .op = REQ_OP_READ,
579 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700580 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800581 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900582
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700583 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
584 return read_mapping_page(mapping, index, NULL);
585
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700586 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900587 if (!page)
588 return ERR_PTR(-ENOMEM);
589
Chao Yucb3bc9e2015-02-05 18:03:40 +0800590 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
591 dn.data_blkaddr = ei.blk + index - ei.fofs;
592 goto got_it;
593 }
594
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900595 set_new_dnode(&dn, inode, NULL, NULL, 0);
596 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700597 if (err)
598 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900599 f2fs_put_dnode(&dn);
600
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900601 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700602 err = -ENOENT;
603 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900604 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800605got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700606 if (PageUptodate(page)) {
607 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900608 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700609 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900610
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900611 /*
612 * A new dentry page is allocated but not able to be written, since its
613 * new inode page couldn't be allocated due to -ENOSPC.
614 * In such the case, its blkaddr can be remained as NEW_ADDR.
615 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
616 */
617 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300618 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700619 if (!PageUptodate(page))
620 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700621 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900622 return page;
623 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900624
Chao Yu7a9d7542016-02-22 18:36:38 +0800625 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700626 fio.page = page;
627 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900628 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700629 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700630 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700631
632put_err:
633 f2fs_put_page(page, 1);
634 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700635}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900636
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700637struct page *find_data_page(struct inode *inode, pgoff_t index)
638{
639 struct address_space *mapping = inode->i_mapping;
640 struct page *page;
641
642 page = find_get_page(mapping, index);
643 if (page && PageUptodate(page))
644 return page;
645 f2fs_put_page(page, 0);
646
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700647 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700648 if (IS_ERR(page))
649 return page;
650
651 if (PageUptodate(page))
652 return page;
653
654 wait_on_page_locked(page);
655 if (unlikely(!PageUptodate(page))) {
656 f2fs_put_page(page, 0);
657 return ERR_PTR(-EIO);
658 }
659 return page;
660}
661
662/*
663 * If it tries to access a hole, return an error.
664 * Because, the callers, functions in dir.c and GC, should be able to know
665 * whether this page exists or not.
666 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700667struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
668 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700669{
670 struct address_space *mapping = inode->i_mapping;
671 struct page *page;
672repeat:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700673 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700674 if (IS_ERR(page))
675 return page;
676
677 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900678 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900679 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900680 f2fs_put_page(page, 1);
681 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900682 }
Chao Yu1563ac72016-07-03 22:05:12 +0800683 if (unlikely(!PageUptodate(page))) {
684 f2fs_put_page(page, 1);
685 return ERR_PTR(-EIO);
686 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900687 return page;
688}
689
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900690/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900691 * Caller ensures that this data page is never allocated.
692 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900693 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800694 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
695 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800696 * Note that, ipage is set only by make_empty_dir, and if any error occur,
697 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900698 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900699struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900700 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900701{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900702 struct address_space *mapping = inode->i_mapping;
703 struct page *page;
704 struct dnode_of_data dn;
705 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800706
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700707 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800708 if (!page) {
709 /*
710 * before exiting, we should make sure ipage will be released
711 * if any error occur.
712 */
713 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700714 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800715 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900716
Jaegeuk Kima8865372013-12-27 17:04:17 +0900717 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800718 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700719 if (err) {
720 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900721 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900722 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700723 if (!ipage)
724 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900725
726 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700727 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900728
729 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300730 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700731 if (!PageUptodate(page))
732 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900733 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700734 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900735
Jaegeuk Kim76121182016-01-01 22:03:47 -0800736 /* if ipage exists, blkaddr should be NEW_ADDR */
737 f2fs_bug_on(F2FS_I_SB(inode), ipage);
738 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700739 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800740 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900741 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700742got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800743 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700744 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700745 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900746 return page;
747}
748
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900749static int __allocate_data_block(struct dnode_of_data *dn)
750{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700751 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900752 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900753 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700754 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800755 blkcnt_t count = 1;
Chao Yu09c3a722017-07-09 00:13:07 +0800756 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900757
Jaegeuk Kim91942322016-05-20 10:13:22 -0700758 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900759 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800760
Chao Yufbcf9312017-07-19 00:19:06 +0800761 dn->data_blkaddr = datablock_addr(dn->inode,
762 dn->node_page, dn->ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800763 if (dn->data_blkaddr == NEW_ADDR)
764 goto alloc;
765
Chao Yu09c3a722017-07-09 00:13:07 +0800766 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
767 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900768
Chao Yudf6136e2015-03-23 10:33:37 +0800769alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900770 get_node_info(sbi, dn->nid, &ni);
771 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
772
Chao Yudf6136e2015-03-23 10:33:37 +0800773 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Chao Yuc52dc0f2017-05-19 23:37:01 +0800774 &sum, CURSEG_WARM_DATA, NULL, false);
Chao Yu216a6202015-03-19 19:23:32 +0800775 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900776
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700777 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800778 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700779 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300780 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700781 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300782 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900783 return 0;
784}
785
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700786static inline bool __force_buffered_io(struct inode *inode, int rw)
787{
788 return ((f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) ||
789 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
790 F2FS_I_SB(inode)->s_ndevs);
791}
792
793int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800794{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800795 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800796 struct f2fs_map_blocks map;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700797 int err = 0;
798
799 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
800 return 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800801
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700802 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800803 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
804 if (map.m_len > map.m_lblk)
805 map.m_len -= map.m_lblk;
806 else
807 map.m_len = 0;
808
Chao Yuda859852016-01-26 15:42:58 +0800809 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800810
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800811 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700812 err = f2fs_convert_inline_inode(inode);
813 if (err)
814 return err;
815 return f2fs_map_blocks(inode, &map, 1,
816 __force_buffered_io(inode, WRITE) ?
817 F2FS_GET_BLOCK_PRE_AIO :
818 F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800819 }
Chao Yue84f88e2017-07-19 00:19:05 +0800820 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700821 err = f2fs_convert_inline_inode(inode);
822 if (err)
823 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800824 }
825 if (!f2fs_has_inline_data(inode))
826 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700827 return err;
828}
829
830static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
831{
832 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
833 if (lock)
834 down_read(&sbi->node_change);
835 else
836 up_read(&sbi->node_change);
837 } else {
838 if (lock)
839 f2fs_lock_op(sbi);
840 else
841 f2fs_unlock_op(sbi);
842 }
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800843}
844
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900845/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700846 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
847 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800848 * If original data blocks are allocated, then give them to blockdev.
849 * Otherwise,
850 * a. preallocate requested block addresses
851 * b. do not use extent cache for better performance
852 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900853 */
Chao Yud323d002015-10-27 09:53:45 +0800854int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800855 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900856{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700857 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900858 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800859 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700860 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800861 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900862 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800863 unsigned int ofs_in_node, last_ofs_in_node;
864 blkcnt_t prealloc;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700865 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +0800866 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900867
Chao Yudfd02e42016-08-20 15:12:01 +0800868 if (!maxblocks)
869 return 0;
870
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700871 map->m_len = 0;
872 map->m_flags = 0;
873
874 /* it only supports block size == page size */
875 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800876 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900877
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800878 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700879 map->m_pblk = ei.blk + pgofs - ei.fofs;
880 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
881 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900882 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800883 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900884
Chao Yu4fe71e82016-01-26 15:37:38 +0800885next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800886 if (create)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700887 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900888
889 /* When reading holes, we need its node page */
890 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900891 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900892 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800893 if (flag == F2FS_GET_BLOCK_BMAP)
894 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800895 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900896 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800897 if (map->m_next_pgofs)
898 *map->m_next_pgofs =
899 get_next_page_offset(&dn, pgofs);
900 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900901 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900902 }
Chao Yu973163f2015-09-18 16:51:51 +0800903
Chao Yu46008c62016-05-09 19:56:30 +0800904 prealloc = 0;
Arnd Bergmanna299abd2016-11-02 14:52:15 +0100905 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800906 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900907
Chao Yu4fe71e82016-01-26 15:37:38 +0800908next_block:
Chao Yufbcf9312017-07-19 00:19:06 +0800909 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800910
Fan Li7df3a432015-12-17 13:20:59 +0800911 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
912 if (create) {
913 if (unlikely(f2fs_cp_error(sbi))) {
914 err = -EIO;
915 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800916 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800917 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800918 if (blkaddr == NULL_ADDR) {
919 prealloc++;
920 last_ofs_in_node = dn.ofs_in_node;
921 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800922 } else {
923 err = __allocate_data_block(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700924 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700925 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800926 }
Fan Li7df3a432015-12-17 13:20:59 +0800927 if (err)
928 goto sync_out;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700929 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800930 blkaddr = dn.data_blkaddr;
931 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800932 if (flag == F2FS_GET_BLOCK_BMAP) {
933 map->m_pblk = 0;
934 goto sync_out;
935 }
Chao Yuda859852016-01-26 15:42:58 +0800936 if (flag == F2FS_GET_BLOCK_FIEMAP &&
937 blkaddr == NULL_ADDR) {
938 if (map->m_next_pgofs)
939 *map->m_next_pgofs = pgofs + 1;
940 }
Fan Li7df3a432015-12-17 13:20:59 +0800941 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800942 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800943 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900944 }
945 }
Fan Li7df3a432015-12-17 13:20:59 +0800946
Chao Yu46008c62016-05-09 19:56:30 +0800947 if (flag == F2FS_GET_BLOCK_PRE_AIO)
948 goto skip;
949
Chao Yu4fe71e82016-01-26 15:37:38 +0800950 if (map->m_len == 0) {
951 /* preallocated unwritten block should be mapped for fiemap. */
952 if (blkaddr == NEW_ADDR)
953 map->m_flags |= F2FS_MAP_UNWRITTEN;
954 map->m_flags |= F2FS_MAP_MAPPED;
955
956 map->m_pblk = blkaddr;
957 map->m_len = 1;
958 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800959 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800960 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800961 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800962 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800963 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800964 } else {
965 goto sync_out;
966 }
967
Chao Yu46008c62016-05-09 19:56:30 +0800968skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800969 dn.ofs_in_node++;
970 pgofs++;
971
Chao Yu46008c62016-05-09 19:56:30 +0800972 /* preallocate blocks in batch for one dnode page */
973 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
974 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800975
Chao Yu46008c62016-05-09 19:56:30 +0800976 dn.ofs_in_node = ofs_in_node;
977 err = reserve_new_blocks(&dn, prealloc);
978 if (err)
979 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800980
Chao Yu46008c62016-05-09 19:56:30 +0800981 map->m_len += dn.ofs_in_node - ofs_in_node;
982 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
983 err = -ENOSPC;
984 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800985 }
Chao Yu46008c62016-05-09 19:56:30 +0800986 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800987 }
988
Chao Yu46008c62016-05-09 19:56:30 +0800989 if (pgofs >= end)
990 goto sync_out;
991 else if (dn.ofs_in_node < end_offset)
992 goto next_block;
993
Chao Yu46008c62016-05-09 19:56:30 +0800994 f2fs_put_dnode(&dn);
995
996 if (create) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700997 __do_map_lock(sbi, flag, false);
998 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +0800999 }
Chao Yu46008c62016-05-09 19:56:30 +08001000 goto next_dnode;
1001
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001002sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001003 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001004unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001005 if (create) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001006 __do_map_lock(sbi, flag, false);
1007 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001008 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001009out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001010 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001011 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001012}
1013
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001014static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001015 struct buffer_head *bh, int create, int flag,
1016 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001017{
1018 struct f2fs_map_blocks map;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001019 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001020
1021 map.m_lblk = iblock;
1022 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001023 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001024
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001025 err = f2fs_map_blocks(inode, &map, create, flag);
1026 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001027 map_bh(bh, inode->i_sb, map.m_pblk);
1028 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001029 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001030 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001031 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001032}
1033
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001034static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001035 struct buffer_head *bh_result, int create, int flag,
1036 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001037{
Chao Yuda859852016-01-26 15:42:58 +08001038 return __get_data_block(inode, iblock, bh_result, create,
1039 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001040}
1041
Chao Yue2b4e2b2015-08-19 19:11:19 +08001042static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001043 struct buffer_head *bh_result, int create)
1044{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001045 return __get_data_block(inode, iblock, bh_result, create,
Qiuyang Sun9afa1592017-08-09 17:27:30 +08001046 F2FS_GET_BLOCK_DEFAULT, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +08001047}
1048
1049static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1050 struct buffer_head *bh_result, int create)
1051{
Yunlei He179448b2015-12-28 21:48:32 +08001052 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001053 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001054 return -EFBIG;
1055
Chao Yue2b4e2b2015-08-19 19:11:19 +08001056 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +08001057 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001058}
1059
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001060static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1061{
1062 return (offset >> inode->i_blkbits);
1063}
1064
1065static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1066{
1067 return (blk << inode->i_blkbits);
1068}
1069
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001070int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1071 u64 start, u64 len)
1072{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001073 struct buffer_head map_bh;
1074 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001075 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001076 u64 logical = 0, phys = 0, size = 0;
1077 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001078 int ret = 0;
1079
1080 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1081 if (ret)
1082 return ret;
1083
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -07001084 if (f2fs_has_inline_data(inode)) {
1085 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1086 if (ret != -EAGAIN)
1087 return ret;
1088 }
1089
Al Viro59551022016-01-22 15:40:57 -05001090 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +08001091
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001092 if (logical_to_blk(inode, len) == 0)
1093 len = blk_to_logical(inode, 1);
1094
1095 start_blk = logical_to_blk(inode, start);
1096 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001097
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001098next:
1099 memset(&map_bh, 0, sizeof(struct buffer_head));
1100 map_bh.b_size = len;
1101
Chao Yue2b4e2b2015-08-19 19:11:19 +08001102 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001103 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001104 if (ret)
1105 goto out;
1106
1107 /* HOLE */
1108 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001109 start_blk = next_pgofs;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001110
1111 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1112 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001113 goto prep_next;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001114
Fan Li9a950d52015-12-26 18:07:41 +08001115 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001116 }
Fan Li9a950d52015-12-26 18:07:41 +08001117
Chao Yuda5af122016-01-08 20:19:27 +08001118 if (size) {
1119 if (f2fs_encrypted_inode(inode))
1120 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1121
Fan Li9a950d52015-12-26 18:07:41 +08001122 ret = fiemap_fill_next_extent(fieinfo, logical,
1123 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001124 }
Fan Li9a950d52015-12-26 18:07:41 +08001125
1126 if (start_blk > last_blk || ret)
1127 goto out;
1128
1129 logical = blk_to_logical(inode, start_blk);
1130 phys = blk_to_logical(inode, map_bh.b_blocknr);
1131 size = map_bh.b_size;
1132 flags = 0;
1133 if (buffer_unwritten(&map_bh))
1134 flags = FIEMAP_EXTENT_UNWRITTEN;
1135
1136 start_blk += logical_to_blk(inode, size);
1137
1138prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001139 cond_resched();
1140 if (fatal_signal_pending(current))
1141 ret = -EINTR;
1142 else
1143 goto next;
1144out:
1145 if (ret == 1)
1146 ret = 0;
1147
Al Viro59551022016-01-22 15:40:57 -05001148 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001149 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001150}
1151
Wei Yongjun6a7a3ae2016-08-23 15:23:59 +00001152static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
1153 unsigned nr_pages)
Chao Yu78682f72016-07-03 22:05:11 +08001154{
1155 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1156 struct fscrypt_ctx *ctx = NULL;
Chao Yu78682f72016-07-03 22:05:11 +08001157 struct bio *bio;
1158
1159 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1160 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
1161 if (IS_ERR(ctx))
1162 return ERR_CAST(ctx);
1163
1164 /* wait the page to be moved by cleaning */
1165 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
1166 }
1167
1168 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
1169 if (!bio) {
1170 if (ctx)
1171 fscrypt_release_ctx(ctx);
1172 return ERR_PTR(-ENOMEM);
1173 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001174 f2fs_target_device(sbi, blkaddr, bio);
Chao Yu78682f72016-07-03 22:05:11 +08001175 bio->bi_end_io = f2fs_read_end_io;
1176 bio->bi_private = ctx;
1177
1178 return bio;
1179}
1180
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001181/*
1182 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1183 * Major change was from block_size == page_size in f2fs by default.
1184 */
1185static int f2fs_mpage_readpages(struct address_space *mapping,
1186 struct list_head *pages, struct page *page,
1187 unsigned nr_pages)
1188{
1189 struct bio *bio = NULL;
1190 unsigned page_idx;
1191 sector_t last_block_in_bio = 0;
1192 struct inode *inode = mapping->host;
1193 const unsigned blkbits = inode->i_blkbits;
1194 const unsigned blocksize = 1 << blkbits;
1195 sector_t block_in_file;
1196 sector_t last_block;
1197 sector_t last_block_in_file;
1198 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001199 struct f2fs_map_blocks map;
1200
1201 map.m_pblk = 0;
1202 map.m_lblk = 0;
1203 map.m_len = 0;
1204 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001205 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001206
1207 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1208
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001209 if (pages) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001210 page = list_last_entry(pages, struct page, lru);
1211
1212 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001213 list_del(&page->lru);
1214 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001215 page->index,
1216 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001217 goto next_page;
1218 }
1219
1220 block_in_file = (sector_t)page->index;
1221 last_block = block_in_file + nr_pages;
1222 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1223 blkbits;
1224 if (last_block > last_block_in_file)
1225 last_block = last_block_in_file;
1226
1227 /*
1228 * Map blocks using the previous result first.
1229 */
1230 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1231 block_in_file > map.m_lblk &&
1232 block_in_file < (map.m_lblk + map.m_len))
1233 goto got_it;
1234
1235 /*
1236 * Then do more f2fs_map_blocks() calls until we are
1237 * done with this page.
1238 */
1239 map.m_flags = 0;
1240
1241 if (block_in_file < last_block) {
1242 map.m_lblk = block_in_file;
1243 map.m_len = last_block - block_in_file;
1244
Chao Yu46c9e142015-09-18 16:54:16 +08001245 if (f2fs_map_blocks(inode, &map, 0,
Qiuyang Sun9afa1592017-08-09 17:27:30 +08001246 F2FS_GET_BLOCK_DEFAULT))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001247 goto set_error_page;
1248 }
1249got_it:
1250 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1251 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1252 SetPageMappedToDisk(page);
1253
1254 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1255 SetPageUptodate(page);
1256 goto confused;
1257 }
1258 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001259 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001260 if (!PageUptodate(page))
1261 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001262 unlock_page(page);
1263 goto next_page;
1264 }
1265
1266 /*
1267 * This page will go to BIO. Do we need to send this
1268 * BIO off first?
1269 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001270 if (bio && (last_block_in_bio != block_nr - 1 ||
1271 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001272submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001273 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001274 bio = NULL;
1275 }
1276 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001277 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001278 if (IS_ERR(bio)) {
1279 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001280 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001281 }
Mike Christie04d328d2016-06-05 14:31:55 -05001282 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001283 }
1284
1285 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1286 goto submit_and_realloc;
1287
1288 last_block_in_bio = block_nr;
1289 goto next_page;
1290set_error_page:
1291 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001292 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001293 unlock_page(page);
1294 goto next_page;
1295confused:
1296 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001297 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001298 bio = NULL;
1299 }
1300 unlock_page(page);
1301next_page:
1302 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001303 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001304 }
1305 BUG_ON(pages && !list_empty(pages));
1306 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001307 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001308 return 0;
1309}
1310
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001311static int f2fs_read_data_page(struct file *file, struct page *page)
1312{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001313 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001314 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001315
Chao Yuc20e89c2014-05-06 16:53:08 +08001316 trace_f2fs_readpage(page, DATA);
1317
arter97e1c42042014-08-06 23:22:50 +09001318 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001319 if (f2fs_has_inline_data(inode))
1320 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001321 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001322 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001323 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001324}
1325
1326static int f2fs_read_data_pages(struct file *file,
1327 struct address_space *mapping,
1328 struct list_head *pages, unsigned nr_pages)
1329{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001330 struct inode *inode = file->f_mapping->host;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001331 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001332
1333 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001334
1335 /* If the file has inline data, skip readpages */
1336 if (f2fs_has_inline_data(inode))
1337 return 0;
1338
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001339 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001340}
1341
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001342static int encrypt_one_page(struct f2fs_io_info *fio)
1343{
1344 struct inode *inode = fio->page->mapping->host;
1345 gfp_t gfp_flags = GFP_NOFS;
1346
1347 if (!f2fs_encrypted_inode(inode) || !S_ISREG(inode->i_mode))
1348 return 0;
1349
1350 /* wait for GCed encrypted page writeback */
1351 f2fs_wait_on_encrypted_page_writeback(fio->sbi, fio->old_blkaddr);
1352
1353retry_encrypt:
1354 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1355 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1356 if (!IS_ERR(fio->encrypted_page))
1357 return 0;
1358
1359 /* flush pending IOs and wait for a while in the ENOMEM case */
1360 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001361 f2fs_flush_merged_writes(fio->sbi);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001362 congestion_wait(BLK_RW_ASYNC, HZ/50);
1363 gfp_flags |= __GFP_NOFAIL;
1364 goto retry_encrypt;
1365 }
1366 return PTR_ERR(fio->encrypted_page);
1367}
1368
1369static inline bool need_inplace_update(struct f2fs_io_info *fio)
1370{
1371 struct inode *inode = fio->page->mapping->host;
1372
1373 if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode))
1374 return false;
1375 if (is_cold_data(fio->page))
1376 return false;
1377 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1378 return false;
1379
1380 return need_inplace_update_policy(inode, fio);
1381}
1382
1383static inline bool valid_ipu_blkaddr(struct f2fs_io_info *fio)
1384{
1385 if (fio->old_blkaddr == NEW_ADDR)
1386 return false;
1387 if (fio->old_blkaddr == NULL_ADDR)
1388 return false;
1389 return true;
1390}
1391
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001392int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001393{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001394 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001395 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001396 struct dnode_of_data dn;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001397 struct extent_info ei = {0,0,0};
1398 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001399 int err = 0;
1400
1401 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001402 if (need_inplace_update(fio) &&
1403 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1404 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
1405
1406 if (valid_ipu_blkaddr(fio)) {
1407 ipu_force = true;
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001408 fio->need_lock = LOCK_DONE;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001409 goto got_it;
1410 }
1411 }
1412
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07001413 /* Deadlock due to between page->lock and f2fs_lock_op */
1414 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1415 return -EAGAIN;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001416
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001417 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001418 if (err)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001419 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001420
Chao Yu28bc1062016-02-06 14:40:34 +08001421 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001422
1423 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001424 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001425 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001426 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001427 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001428got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001429 /*
1430 * If current allocation needs SSR,
1431 * it had better in-place writes for updated data.
1432 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001433 if (ipu_force || (valid_ipu_blkaddr(fio) && need_inplace_update(fio))) {
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001434 err = encrypt_one_page(fio);
1435 if (err)
1436 goto out_writepage;
1437
1438 set_page_writeback(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001439 f2fs_put_dnode(&dn);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001440 if (fio->need_lock == LOCK_REQ)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001441 f2fs_unlock_op(fio->sbi);
1442 err = rewrite_data_page(fio);
1443 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001444 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001445 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001446 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001447
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001448 if (fio->need_lock == LOCK_RETRY) {
1449 if (!f2fs_trylock_op(fio->sbi)) {
1450 err = -EAGAIN;
1451 goto out_writepage;
1452 }
1453 fio->need_lock = LOCK_REQ;
1454 }
1455
1456 err = encrypt_one_page(fio);
1457 if (err)
1458 goto out_writepage;
1459
1460 set_page_writeback(page);
1461
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001462 /* LFS mode write path */
1463 write_data_page(&dn, fio);
1464 trace_f2fs_do_write_data_page(page, OPU);
1465 set_inode_flag(inode, FI_APPEND_WRITE);
1466 if (page->index == 0)
1467 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001468out_writepage:
1469 f2fs_put_dnode(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001470out:
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001471 if (fio->need_lock == LOCK_REQ)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001472 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001473 return err;
1474}
1475
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001476static int __write_data_page(struct page *page, bool *submitted,
Chao Yuc0fe4882017-08-02 23:21:48 +08001477 struct writeback_control *wbc,
1478 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001479{
1480 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001481 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001482 loff_t i_size = i_size_read(inode);
1483 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001484 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001485 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001486 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001487 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001488 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001489 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001490 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001491 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001492 .op = REQ_OP_WRITE,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001493 .op_flags = wbc_to_write_flags(wbc),
1494 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001495 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001496 .encrypted_page = NULL,
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001497 .submitted = false,
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001498 .need_lock = LOCK_RETRY,
Chao Yuc0fe4882017-08-02 23:21:48 +08001499 .io_type = io_type,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001500 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001501
Chao Yuecda0de2014-05-06 16:48:26 +08001502 trace_f2fs_writepage(page, DATA);
1503
Chao Yu00cc7ed2017-06-29 23:20:45 +08001504 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1505 goto redirty_out;
1506
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001507 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001508 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001509
1510 /*
1511 * If the offset is out-of-range of file size,
1512 * this page does not have to be written to disk.
1513 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001514 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001515 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001516 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001517
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001518 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001519write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001520 if (f2fs_is_drop_cache(inode))
1521 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001522 /* we should not write 0'th page having journal header */
1523 if (f2fs_is_volatile_file(inode) && (!page->index ||
1524 (!wbc->for_reclaim &&
1525 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001526 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001527
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001528 /* we should bypass data pages to proceed the kworkder jobs */
1529 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001530 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001531 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001532 }
1533
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001534 /* Dentry blocks are controlled by checkpoint */
1535 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001536 fio.need_lock = LOCK_DONE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001537 err = do_write_data_page(&fio);
1538 goto done;
1539 }
1540
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001541 if (!wbc->for_reclaim)
1542 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001543 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001544 goto redirty_out;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001545 else
1546 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001547
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001548 err = -EAGAIN;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001549 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001550 err = f2fs_write_inline_data(inode, page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001551 if (!err)
1552 goto out;
1553 }
1554
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001555 if (err == -EAGAIN) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001556 err = do_write_data_page(&fio);
Jaegeuk Kim1b48ed62017-05-12 13:51:34 -07001557 if (err == -EAGAIN) {
1558 fio.need_lock = LOCK_REQ;
1559 err = do_write_data_page(&fio);
1560 }
1561 }
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001562 if (F2FS_I(inode)->last_disk_size < psize)
1563 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001564
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001565done:
1566 if (err && err != -ENOENT)
1567 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001568
Jaegeuk Kim39936832012-11-22 16:21:29 +09001569out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001570 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001571 if (err)
1572 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001573
1574 if (wbc->for_reclaim) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001575 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001576 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yuc227f912015-12-16 13:09:20 +08001577 remove_dirty_inode(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001578 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001579 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001580
1581 unlock_page(page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001582 if (!S_ISDIR(inode->i_mode))
1583 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001584
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001585 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001586 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001587 submitted = NULL;
1588 }
1589
1590 if (submitted)
1591 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001592
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001593 return 0;
1594
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001595redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001596 redirty_page_for_writepage(wbc, page);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001597 if (!err)
1598 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001599 unlock_page(page);
1600 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001601}
1602
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001603static int f2fs_write_data_page(struct page *page,
1604 struct writeback_control *wbc)
1605{
Chao Yuc0fe4882017-08-02 23:21:48 +08001606 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001607}
1608
Chao Yu8f46dca2015-07-14 18:56:10 +08001609/*
1610 * This function was copied from write_cche_pages from mm/page-writeback.c.
1611 * The major change is making write step of cold data page separately from
1612 * warm/hot data page.
1613 */
1614static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yuc0fe4882017-08-02 23:21:48 +08001615 struct writeback_control *wbc,
1616 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08001617{
1618 int ret = 0;
1619 int done = 0;
1620 struct pagevec pvec;
1621 int nr_pages;
1622 pgoff_t uninitialized_var(writeback_index);
1623 pgoff_t index;
1624 pgoff_t end; /* Inclusive */
1625 pgoff_t done_index;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001626 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001627 int cycled;
1628 int range_whole = 0;
1629 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001630
1631 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001632
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001633 if (get_dirty_pages(mapping->host) <=
1634 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1635 set_inode_flag(mapping->host, FI_HOT_DATA);
1636 else
1637 clear_inode_flag(mapping->host, FI_HOT_DATA);
1638
Chao Yu8f46dca2015-07-14 18:56:10 +08001639 if (wbc->range_cyclic) {
1640 writeback_index = mapping->writeback_index; /* prev offset */
1641 index = writeback_index;
1642 if (index == 0)
1643 cycled = 1;
1644 else
1645 cycled = 0;
1646 end = -1;
1647 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001648 index = wbc->range_start >> PAGE_SHIFT;
1649 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001650 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1651 range_whole = 1;
1652 cycled = 1; /* ignore range_cyclic tests */
1653 }
1654 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1655 tag = PAGECACHE_TAG_TOWRITE;
1656 else
1657 tag = PAGECACHE_TAG_DIRTY;
1658retry:
1659 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1660 tag_pages_for_writeback(mapping, index, end);
1661 done_index = index;
1662 while (!done && (index <= end)) {
1663 int i;
1664
1665 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1666 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1667 if (nr_pages == 0)
1668 break;
1669
1670 for (i = 0; i < nr_pages; i++) {
1671 struct page *page = pvec.pages[i];
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001672 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001673
1674 if (page->index > end) {
1675 done = 1;
1676 break;
1677 }
1678
1679 done_index = page->index;
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07001680retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08001681 lock_page(page);
1682
1683 if (unlikely(page->mapping != mapping)) {
1684continue_unlock:
1685 unlock_page(page);
1686 continue;
1687 }
1688
1689 if (!PageDirty(page)) {
1690 /* someone wrote it for us */
1691 goto continue_unlock;
1692 }
1693
Chao Yu8f46dca2015-07-14 18:56:10 +08001694 if (PageWriteback(page)) {
1695 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001696 f2fs_wait_on_page_writeback(page,
1697 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001698 else
1699 goto continue_unlock;
1700 }
1701
1702 BUG_ON(PageWriteback(page));
1703 if (!clear_page_dirty_for_io(page))
1704 goto continue_unlock;
1705
Chao Yuc0fe4882017-08-02 23:21:48 +08001706 ret = __write_data_page(page, &submitted, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08001707 if (unlikely(ret)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001708 /*
1709 * keep nr_to_write, since vfs uses this to
1710 * get # of written pages.
1711 */
1712 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1713 unlock_page(page);
1714 ret = 0;
1715 continue;
Jaegeuk Kim0f4dc162017-06-21 17:52:39 -07001716 } else if (ret == -EAGAIN) {
1717 ret = 0;
1718 if (wbc->sync_mode == WB_SYNC_ALL) {
1719 cond_resched();
1720 congestion_wait(BLK_RW_ASYNC,
1721 HZ/50);
1722 goto retry_write;
1723 }
1724 continue;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001725 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001726 done_index = page->index + 1;
1727 done = 1;
1728 break;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001729 } else if (submitted) {
1730 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08001731 }
1732
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001733 /* give a priority to WB_SYNC threads */
1734 if ((atomic_read(&F2FS_M_SB(mapping)->wb_sync_req) ||
1735 --wbc->nr_to_write <= 0) &&
1736 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08001737 done = 1;
1738 break;
1739 }
1740 }
1741 pagevec_release(&pvec);
1742 cond_resched();
1743 }
1744
Chao Yu8f46dca2015-07-14 18:56:10 +08001745 if (!cycled && !done) {
1746 cycled = 1;
1747 index = 0;
1748 end = writeback_index - 1;
1749 goto retry;
1750 }
1751 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1752 mapping->writeback_index = done_index;
1753
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001754 if (last_idx != ULONG_MAX)
Jaegeuk Kim9bc576a2017-05-10 11:28:38 -07001755 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
1756 0, last_idx, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08001757
Chao Yu8f46dca2015-07-14 18:56:10 +08001758 return ret;
1759}
1760
Chao Yuc0fe4882017-08-02 23:21:48 +08001761int __f2fs_write_data_pages(struct address_space *mapping,
1762 struct writeback_control *wbc,
1763 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001764{
1765 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001766 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001767 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001768 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001769
P J Pcfb185a2013-04-03 11:38:00 +09001770 /* deal with chardevs and other special file */
1771 if (!mapping->a_ops->writepage)
1772 return 0;
1773
Chao Yu6a290542015-07-17 18:02:39 +08001774 /* skip writing if there is no dirty page in this inode */
1775 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1776 return 0;
1777
Chao Yu00cc7ed2017-06-29 23:20:45 +08001778 /* during POR, we don't need to trigger writepage at all. */
1779 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1780 goto skip_write;
1781
Jaegeuk Kima1257022015-10-08 10:40:07 -07001782 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1783 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1784 available_free_memory(sbi, DIRTY_DENTS))
1785 goto skip_write;
1786
Chao Yud323d002015-10-27 09:53:45 +08001787 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001788 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001789 goto skip_write;
1790
Yunlei Hed31c7c32016-02-04 16:14:00 +08001791 trace_f2fs_writepages(mapping->host, wbc, DATA);
1792
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001793 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
1794 if (wbc->sync_mode == WB_SYNC_ALL)
1795 atomic_inc(&sbi->wb_sync_req);
1796 else if (atomic_read(&sbi->wb_sync_req))
1797 goto skip_write;
1798
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001799 blk_start_plug(&plug);
Chao Yuc0fe4882017-08-02 23:21:48 +08001800 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001801 blk_finish_plug(&plug);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001802
1803 if (wbc->sync_mode == WB_SYNC_ALL)
1804 atomic_dec(&sbi->wb_sync_req);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001805 /*
1806 * if some pages were truncated, we cannot guarantee its mapping->host
1807 * to detect pending bios.
1808 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001809
Chao Yuc227f912015-12-16 13:09:20 +08001810 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001811 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001812
1813skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001814 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001815 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001816 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001817}
1818
Chao Yuc0fe4882017-08-02 23:21:48 +08001819static int f2fs_write_data_pages(struct address_space *mapping,
1820 struct writeback_control *wbc)
1821{
1822 struct inode *inode = mapping->host;
1823
1824 return __f2fs_write_data_pages(mapping, wbc,
1825 F2FS_I(inode)->cp_task == current ?
1826 FS_CP_DATA_IO : FS_DATA_IO);
1827}
1828
Chao Yu3aab8f82014-07-02 13:25:04 +08001829static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1830{
1831 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001832 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001833
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001834 if (to > i_size) {
Qiuyang Sun7b23ea12017-05-18 11:06:45 +08001835 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001836 truncate_pagecache(inode, i_size);
1837 truncate_blocks(inode, i_size, true);
Qiuyang Sun7b23ea12017-05-18 11:06:45 +08001838 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3aab8f82014-07-02 13:25:04 +08001839 }
1840}
1841
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001842static int prepare_write_begin(struct f2fs_sb_info *sbi,
1843 struct page *page, loff_t pos, unsigned len,
1844 block_t *blk_addr, bool *node_changed)
1845{
1846 struct inode *inode = page->mapping->host;
1847 pgoff_t index = page->index;
1848 struct dnode_of_data dn;
1849 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001850 bool locked = false;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001851 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001852 int err = 0;
1853
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001854 /*
1855 * we already allocated all the blocks, so we don't need to get
1856 * the block addresses when there is no need to fill the page.
1857 */
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001858 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
1859 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001860 return 0;
1861
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001862 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001863 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001864 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001865 locked = true;
1866 }
1867restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001868 /* check inline_data */
1869 ipage = get_node_page(sbi, inode->i_ino);
1870 if (IS_ERR(ipage)) {
1871 err = PTR_ERR(ipage);
1872 goto unlock_out;
1873 }
1874
1875 set_new_dnode(&dn, inode, ipage, ipage, 0);
1876
1877 if (f2fs_has_inline_data(inode)) {
Chao Yue84f88e2017-07-19 00:19:05 +08001878 if (pos + len <= MAX_INLINE_DATA(inode)) {
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001879 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001880 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001881 if (inode->i_nlink)
1882 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001883 } else {
1884 err = f2fs_convert_inline_page(&dn, page);
1885 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001886 goto out;
1887 if (dn.data_blkaddr == NULL_ADDR)
1888 err = f2fs_get_block(&dn, index);
1889 }
1890 } else if (locked) {
1891 err = f2fs_get_block(&dn, index);
1892 } else {
1893 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1894 dn.data_blkaddr = ei.blk + index - ei.fofs;
1895 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001896 /* hole case */
1897 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001898 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001899 f2fs_put_dnode(&dn);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001900 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
1901 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001902 locked = true;
1903 goto restart;
1904 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001905 }
1906 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001907
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001908 /* convert_inline_page can make node_changed */
1909 *blk_addr = dn.data_blkaddr;
1910 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001911out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001912 f2fs_put_dnode(&dn);
1913unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001914 if (locked)
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001915 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001916 return err;
1917}
1918
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001919static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1920 loff_t pos, unsigned len, unsigned flags,
1921 struct page **pagep, void **fsdata)
1922{
1923 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001924 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001925 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001926 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001927 bool need_balance = false;
1928 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001929 int err = 0;
1930
Chao Yu62aed042014-05-06 16:46:04 +08001931 trace_f2fs_write_begin(inode, pos, len, flags);
1932
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001933 /*
1934 * We should check this at this moment to avoid deadlock on inode page
1935 * and #0 page. The locking rule for inline_data conversion should be:
1936 * lock_page(page #0) -> lock_page(inode_page)
1937 */
1938 if (index != 0) {
1939 err = f2fs_convert_inline_inode(inode);
1940 if (err)
1941 goto fail;
1942 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001943repeat:
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001944 /*
1945 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1946 * wait_for_stable_page. Will wait that below with our IO control.
1947 */
1948 page = pagecache_get_page(mapping, index,
1949 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08001950 if (!page) {
1951 err = -ENOMEM;
1952 goto fail;
1953 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001954
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001955 *pagep = page;
1956
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001957 err = prepare_write_begin(sbi, page, pos, len,
1958 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001959 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001960 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001961
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001962 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001963 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001964 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001965 lock_page(page);
1966 if (page->mapping != mapping) {
1967 /* The page got truncated from under us */
1968 f2fs_put_page(page, 1);
1969 goto repeat;
1970 }
1971 }
1972
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001973 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001974
Chao Yu08b39fb2015-10-08 13:27:34 +08001975 /* wait for GCed encrypted page writeback */
1976 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001977 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001978
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001979 if (len == PAGE_SIZE || PageUptodate(page))
1980 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001981
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001982 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
1983 zero_user_segment(page, len, PAGE_SIZE);
1984 return 0;
1985 }
1986
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001987 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001988 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001989 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001990 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001991 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001992
Chao Yu78682f72016-07-03 22:05:11 +08001993 bio = f2fs_grab_bio(inode, blkaddr, 1);
1994 if (IS_ERR(bio)) {
1995 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001996 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001997 }
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001998 bio->bi_opf = REQ_OP_READ;
Chao Yu78682f72016-07-03 22:05:11 +08001999 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
2000 bio_put(bio);
2001 err = -EFAULT;
2002 goto fail;
2003 }
2004
Linus Torvalds4fc29c12016-07-27 10:36:31 -07002005 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002006
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002007 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002008 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002009 f2fs_put_page(page, 1);
2010 goto repeat;
2011 }
Chao Yu1563ac72016-07-03 22:05:12 +08002012 if (unlikely(!PageUptodate(page))) {
2013 err = -EIO;
2014 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002015 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002016 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002017 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002018
Chao Yu3aab8f82014-07-02 13:25:04 +08002019fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002020 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002021 f2fs_write_failed(mapping, pos + len);
2022 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002023}
2024
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002025static int f2fs_write_end(struct file *file,
2026 struct address_space *mapping,
2027 loff_t pos, unsigned len, unsigned copied,
2028 struct page *page, void *fsdata)
2029{
2030 struct inode *inode = page->mapping->host;
2031
Chao Yudfb2bf32014-05-06 16:47:23 +08002032 trace_f2fs_write_end(inode, pos, len, copied);
2033
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002034 /*
2035 * This should be come from len == PAGE_SIZE, and we expect copied
2036 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2037 * let generic_perform_write() try to copy data again through copied=0.
2038 */
2039 if (!PageUptodate(page)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002040 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002041 copied = 0;
2042 else
2043 SetPageUptodate(page);
2044 }
2045 if (!copied)
2046 goto unlock_out;
2047
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002048 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002049
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002050 if (pos + copied > i_size_read(inode))
2051 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002052unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002053 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002054 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002055 return copied;
2056}
2057
Omar Sandoval6f673762015-03-16 04:33:52 -07002058static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2059 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002060{
2061 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002062
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002063 if (offset & blocksize_mask)
2064 return -EINVAL;
2065
Al Viro5b46f252014-03-16 18:07:34 -04002066 if (iov_iter_alignment(iter) & blocksize_mask)
2067 return -EINVAL;
2068
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002069 return 0;
2070}
2071
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002072static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002073{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002074 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002075 struct inode *inode = mapping->host;
2076 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002077 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002078 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002079 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002080
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002081 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002082 if (err)
2083 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002084
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002085 if (__force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002086 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002087
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002088 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002089
Chao Yu82e0a5a2016-07-13 09:18:29 +08002090 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002091 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002092 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
2093
2094 if (rw == WRITE) {
Chao Yuc0fe4882017-08-02 23:21:48 +08002095 if (err > 0) {
2096 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2097 err);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002098 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yuc0fe4882017-08-02 23:21:48 +08002099 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002100 f2fs_write_failed(mapping, offset + count);
Chao Yuc0fe4882017-08-02 23:21:48 +08002101 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002102 }
Chao Yu70407fa2014-07-31 21:11:22 +08002103
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002104 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002105
Chao Yu3aab8f82014-07-02 13:25:04 +08002106 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002107}
2108
Chao Yu487261f2015-02-05 17:44:29 +08002109void f2fs_invalidate_page(struct page *page, unsigned int offset,
2110 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002111{
2112 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002113 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002114
Chao Yu487261f2015-02-05 17:44:29 +08002115 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002116 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002117 return;
2118
Chao Yu487261f2015-02-05 17:44:29 +08002119 if (PageDirty(page)) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002120 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002121 dec_page_count(sbi, F2FS_DIRTY_META);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002122 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002123 dec_page_count(sbi, F2FS_DIRTY_NODES);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002124 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002125 inode_dec_dirty_pages(inode);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002126 remove_dirty_inode(inode);
2127 }
Chao Yu487261f2015-02-05 17:44:29 +08002128 }
Chao Yudecd36b2015-08-07 18:42:09 +08002129
2130 /* This is atomic written page, keep Private */
2131 if (IS_ATOMIC_WRITTEN_PAGE(page))
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002132 return drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002133
Chao Yu23dc9742016-04-29 20:09:15 +08002134 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002135 ClearPagePrivate(page);
2136}
2137
Chao Yu487261f2015-02-05 17:44:29 +08002138int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002139{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002140 /* If this is dirty page, keep PagePrivate */
2141 if (PageDirty(page))
2142 return 0;
2143
Chao Yudecd36b2015-08-07 18:42:09 +08002144 /* This is atomic written page, keep Private */
2145 if (IS_ATOMIC_WRITTEN_PAGE(page))
2146 return 0;
2147
Chao Yu23dc9742016-04-29 20:09:15 +08002148 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002149 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002150 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002151}
2152
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002153/*
2154 * This was copied from __set_page_dirty_buffers which gives higher performance
2155 * in very high speed storages. (e.g., pmem)
2156 */
2157void f2fs_set_page_dirty_nobuffers(struct page *page)
2158{
2159 struct address_space *mapping = page->mapping;
2160 unsigned long flags;
2161
2162 if (unlikely(!mapping))
2163 return;
2164
2165 spin_lock(&mapping->private_lock);
2166 lock_page_memcg(page);
2167 SetPageDirty(page);
2168 spin_unlock(&mapping->private_lock);
2169
2170 spin_lock_irqsave(&mapping->tree_lock, flags);
2171 WARN_ON_ONCE(!PageUptodate(page));
2172 account_page_dirtied(page, mapping);
2173 radix_tree_tag_set(&mapping->page_tree,
2174 page_index(page), PAGECACHE_TAG_DIRTY);
2175 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2176 unlock_page_memcg(page);
2177
2178 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
2179 return;
2180}
2181
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002182static int f2fs_set_data_page_dirty(struct page *page)
2183{
2184 struct address_space *mapping = page->mapping;
2185 struct inode *inode = mapping->host;
2186
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002187 trace_f2fs_set_page_dirty(page, DATA);
2188
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002189 if (!PageUptodate(page))
2190 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002191
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07002192 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002193 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2194 register_inmem_page(inode, page);
2195 return 1;
2196 }
2197 /*
2198 * Previously, this page has been registered, we just
2199 * return here.
2200 */
2201 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002202 }
2203
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002204 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002205 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002206 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002207 return 1;
2208 }
2209 return 0;
2210}
2211
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002212static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2213{
Chao Yu454ae7e2014-04-22 13:34:01 +08002214 struct inode *inode = mapping->host;
2215
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002216 if (f2fs_has_inline_data(inode))
2217 return 0;
2218
2219 /* make sure allocating whole blocks */
2220 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2221 filemap_write_and_wait(mapping);
2222
Chao Yue2b4e2b2015-08-19 19:11:19 +08002223 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002224}
2225
Weichao Guo5b7a4872016-09-20 05:03:27 +08002226#ifdef CONFIG_MIGRATION
2227#include <linux/migrate.h>
2228
2229int f2fs_migrate_page(struct address_space *mapping,
2230 struct page *newpage, struct page *page, enum migrate_mode mode)
2231{
2232 int rc, extra_count;
2233 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2234 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2235
2236 BUG_ON(PageWriteback(page));
2237
2238 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kim025258e2017-07-06 14:46:01 -07002239 if (atomic_written) {
2240 if (mode != MIGRATE_SYNC)
2241 return -EBUSY;
2242 if (!mutex_trylock(&fi->inmem_lock))
2243 return -EAGAIN;
2244 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002245
2246 /*
2247 * A reference is expected if PagePrivate set when move mapping,
2248 * however F2FS breaks this for maintaining dirty page counts when
2249 * truncating pages. So here adjusting the 'extra_count' make it work.
2250 */
2251 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2252 rc = migrate_page_move_mapping(mapping, newpage,
2253 page, NULL, mode, extra_count);
2254 if (rc != MIGRATEPAGE_SUCCESS) {
2255 if (atomic_written)
2256 mutex_unlock(&fi->inmem_lock);
2257 return rc;
2258 }
2259
2260 if (atomic_written) {
2261 struct inmem_pages *cur;
2262 list_for_each_entry(cur, &fi->inmem_pages, list)
2263 if (cur->page == page) {
2264 cur->page = newpage;
2265 break;
2266 }
2267 mutex_unlock(&fi->inmem_lock);
2268 put_page(page);
2269 get_page(newpage);
2270 }
2271
2272 if (PagePrivate(page))
2273 SetPagePrivate(newpage);
2274 set_page_private(newpage, page_private(page));
2275
2276 migrate_page_copy(newpage, page);
2277
2278 return MIGRATEPAGE_SUCCESS;
2279}
2280#endif
2281
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002282const struct address_space_operations f2fs_dblock_aops = {
2283 .readpage = f2fs_read_data_page,
2284 .readpages = f2fs_read_data_pages,
2285 .writepage = f2fs_write_data_page,
2286 .writepages = f2fs_write_data_pages,
2287 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002288 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002289 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002290 .invalidatepage = f2fs_invalidate_page,
2291 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002292 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002293 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002294#ifdef CONFIG_MIGRATION
2295 .migratepage = f2fs_migrate_page,
2296#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002297};