blob: b0a2e3faabb21d0bd607ea2b8ac23b448ac7d04c [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
Chao Yu36951b32016-11-16 10:41:20 +080032static 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
Chao Yu55523512017-02-25 11:08:28 +080058 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;
Chao Yu55523512017-02-25 11:08:28 +080061 }
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;
Chao Yu36951b32016-11-16 10:41:20 +080096 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090097
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -080098 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 }
108
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 }
Chao Yu36951b32016-11-16 10:41:20 +0800115 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 }
Chao Yu36951b32016-11-16 10:41:20 +0800119 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 Kim3c62be12016-10-06 19:02:05 -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 Kim3c62be12016-10-06 19:02:05 -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 Kim0a595eb2016-12-14 10:12:56 -0800187 unsigned int start;
188
Damien Le Moal0bfd7a02016-10-28 17:45:00 +0900189 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 Kim0a595eb2016-12-14 10:12:56 -0800192
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 Kim0a595eb2016-12-14 10:12:56 -0800222submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800223 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 Kim554b5122016-12-21 12:13:03 -0800239 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 Kim942fd312017-02-01 16:51:22 -0800248static 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 Kim942fd312017-02-01 16:51:22 -0800258 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 Kim942fd312017-02-01 16:51:22 -0800268 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 Kim942fd312017-02-01 16:51:22 -0800281 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);
284 struct f2fs_bio_info *io = &sbi->write_io[btype];
285 bool ret;
286
287 down_read(&io->io_rwsem);
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800288 ret = __has_merged_page(io, inode, ino, idx);
Chao Yu0c3a5792016-01-18 18:28:11 +0800289 up_read(&io->io_rwsem);
290 return ret;
291}
292
293static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800294 struct inode *inode, nid_t ino, pgoff_t idx,
295 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900296{
297 enum page_type btype = PAGE_TYPE_OF_BIO(type);
298 struct f2fs_bio_info *io;
299
300 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
301
Chao Yudf0f8dc2014-03-22 14:57:23 +0800302 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900303
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800304 if (!__has_merged_page(io, inode, ino, idx))
Chao Yu0c3a5792016-01-18 18:28:11 +0800305 goto out;
306
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900307 /* change META to META_FLUSH in the checkpoint procedure */
308 if (type >= META_FLUSH) {
309 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500310 io->fio.op = REQ_OP_WRITE;
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800311 io->fio.op_flags = REQ_META | REQ_PRIO;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600312 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800313 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900314 }
315 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800316out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800317 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900318}
319
Chao Yu0c3a5792016-01-18 18:28:11 +0800320void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
321 int rw)
322{
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800323 __f2fs_submit_merged_bio(sbi, NULL, 0, 0, type, rw);
Chao Yu0c3a5792016-01-18 18:28:11 +0800324}
325
326void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800327 struct inode *inode, nid_t ino, pgoff_t idx,
328 enum page_type type, int rw)
Chao Yu0c3a5792016-01-18 18:28:11 +0800329{
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800330 if (has_merged_page(sbi, inode, ino, idx, type))
331 __f2fs_submit_merged_bio(sbi, inode, ino, idx, type, rw);
Chao Yu0c3a5792016-01-18 18:28:11 +0800332}
333
Chao Yu406657d2016-02-24 17:17:55 +0800334void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
335{
336 f2fs_submit_merged_bio(sbi, DATA, WRITE);
337 f2fs_submit_merged_bio(sbi, NODE, WRITE);
338 f2fs_submit_merged_bio(sbi, META, WRITE);
339}
340
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900341/*
342 * Fill the locked page with data located in the block address.
343 * Return unlocked page.
344 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700345int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900346{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900347 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700348 struct page *page = fio->encrypted_page ?
349 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900350
Chao Yu2ace38e2014-12-24 16:08:14 +0800351 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700352 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900353
354 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500355 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900356
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300357 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900358 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900359 return -EFAULT;
360 }
Mike Christie04d328d2016-06-05 14:31:55 -0500361 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900362
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700363 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900364 return 0;
365}
366
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800367int f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900368{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700369 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900370 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900371 struct f2fs_bio_info *io;
Mike Christie04d328d2016-06-05 14:31:55 -0500372 bool is_read = is_read_io(fio->op);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700373 struct page *bio_page;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800374 int err = 0;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900375
Gu Zheng940a6d32013-12-20 17:39:59 +0800376 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900377
Chao Yu7a9d7542016-02-22 18:36:38 +0800378 if (fio->old_blkaddr != NEW_ADDR)
379 verify_block_addr(sbi, fio->old_blkaddr);
380 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900381
Chao Yu36951b32016-11-16 10:41:20 +0800382 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
383
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800384 /* set submitted = 1 as a return value */
385 fio->submitted = 1;
386
Chao Yu36951b32016-11-16 10:41:20 +0800387 if (!is_read)
388 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
389
Chao Yudf0f8dc2014-03-22 14:57:23 +0800390 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900391
Chao Yu7a9d7542016-02-22 18:36:38 +0800392 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700393 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
394 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900395 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900396alloc_new:
397 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800398 if ((fio->type == DATA || fio->type == NODE) &&
399 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
400 err = -EAGAIN;
401 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
402 goto out_fail;
403 }
Chao Yu7a9d7542016-02-22 18:36:38 +0800404 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
Jaegeuk Kim664ba972016-10-18 11:07:45 -0700405 BIO_MAX_PAGES, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900406 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900407 }
408
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300409 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
410 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900411 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900412 goto alloc_new;
413 }
414
Chao Yu7a9d7542016-02-22 18:36:38 +0800415 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700416 f2fs_trace_ios(fio, 0);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800417out_fail:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800418 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700419 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800420 return err;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900421}
422
Chao Yu46008c62016-05-09 19:56:30 +0800423static void __set_data_blkaddr(struct dnode_of_data *dn)
424{
425 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
426 __le32 *addr_array;
427
428 /* Get physical address of data block */
429 addr_array = blkaddr_in_node(rn);
430 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
431}
432
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900433/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900434 * Lock ordering for the change of data block address:
435 * ->data_page
436 * ->node_page
437 * update block addresses in the node page
438 */
Chao Yu216a6202015-03-19 19:23:32 +0800439void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900440{
Chao Yu46008c62016-05-09 19:56:30 +0800441 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
442 __set_data_blkaddr(dn);
443 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800444 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900445}
446
Chao Yuf28b3432016-02-24 17:16:47 +0800447void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
448{
449 dn->data_blkaddr = blkaddr;
450 set_data_blkaddr(dn);
451 f2fs_update_extent_cache(dn);
452}
453
Chao Yu46008c62016-05-09 19:56:30 +0800454/* dn->ofs_in_node will be returned with up-to-date last block pointer */
455int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900456{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700457 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900458
Chao Yu46008c62016-05-09 19:56:30 +0800459 if (!count)
460 return 0;
461
Jaegeuk Kim91942322016-05-20 10:13:22 -0700462 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900463 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800464 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900465 return -ENOSPC;
466
Chao Yu46008c62016-05-09 19:56:30 +0800467 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
468 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900469
Chao Yu46008c62016-05-09 19:56:30 +0800470 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
471
472 for (; count > 0; dn->ofs_in_node++) {
473 block_t blkaddr =
474 datablock_addr(dn->node_page, dn->ofs_in_node);
475 if (blkaddr == NULL_ADDR) {
476 dn->data_blkaddr = NEW_ADDR;
477 __set_data_blkaddr(dn);
478 count--;
479 }
480 }
481
482 if (set_page_dirty(dn->node_page))
483 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900484 return 0;
485}
486
Chao Yu46008c62016-05-09 19:56:30 +0800487/* Should keep dn->ofs_in_node unchanged */
488int reserve_new_block(struct dnode_of_data *dn)
489{
490 unsigned int ofs_in_node = dn->ofs_in_node;
491 int ret;
492
493 ret = reserve_new_blocks(dn, 1);
494 dn->ofs_in_node = ofs_in_node;
495 return ret;
496}
497
Huajun Lib6009652013-11-10 23:13:18 +0800498int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
499{
500 bool need_put = dn->inode_page ? false : true;
501 int err;
502
503 err = get_dnode_of_data(dn, index, ALLOC_NODE);
504 if (err)
505 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900506
Huajun Lib6009652013-11-10 23:13:18 +0800507 if (dn->data_blkaddr == NULL_ADDR)
508 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900509 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800510 f2fs_put_dnode(dn);
511 return err;
512}
513
Fan Li759af1c2015-08-05 15:52:16 +0800514int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900515{
Hou Pengyange15882b2017-02-23 09:18:05 +0000516 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800517 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800518
Fan Li759af1c2015-08-05 15:52:16 +0800519 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
520 dn->data_blkaddr = ei.blk + index - ei.fofs;
521 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800522 }
523
Fan Li759af1c2015-08-05 15:52:16 +0800524 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900525}
526
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700527struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500528 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900529{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900530 struct address_space *mapping = inode->i_mapping;
531 struct dnode_of_data dn;
532 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +0000533 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900534 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800535 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700536 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800537 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500538 .op = REQ_OP_READ,
539 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700540 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800541 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900542
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700543 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
544 return read_mapping_page(mapping, index, NULL);
545
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700546 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900547 if (!page)
548 return ERR_PTR(-ENOMEM);
549
Chao Yucb3bc9e2015-02-05 18:03:40 +0800550 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
551 dn.data_blkaddr = ei.blk + index - ei.fofs;
552 goto got_it;
553 }
554
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900555 set_new_dnode(&dn, inode, NULL, NULL, 0);
556 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700557 if (err)
558 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900559 f2fs_put_dnode(&dn);
560
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900561 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700562 err = -ENOENT;
563 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900564 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800565got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700566 if (PageUptodate(page)) {
567 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900568 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700569 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900570
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900571 /*
572 * A new dentry page is allocated but not able to be written, since its
573 * new inode page couldn't be allocated due to -ENOSPC.
574 * In such the case, its blkaddr can be remained as NEW_ADDR.
575 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
576 */
577 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300578 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700579 if (!PageUptodate(page))
580 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700581 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900582 return page;
583 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900584
Chao Yu7a9d7542016-02-22 18:36:38 +0800585 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700586 fio.page = page;
587 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900588 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700589 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700590 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700591
592put_err:
593 f2fs_put_page(page, 1);
594 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700595}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900596
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700597struct page *find_data_page(struct inode *inode, pgoff_t index)
598{
599 struct address_space *mapping = inode->i_mapping;
600 struct page *page;
601
602 page = find_get_page(mapping, index);
603 if (page && PageUptodate(page))
604 return page;
605 f2fs_put_page(page, 0);
606
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600607 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700608 if (IS_ERR(page))
609 return page;
610
611 if (PageUptodate(page))
612 return page;
613
614 wait_on_page_locked(page);
615 if (unlikely(!PageUptodate(page))) {
616 f2fs_put_page(page, 0);
617 return ERR_PTR(-EIO);
618 }
619 return page;
620}
621
622/*
623 * If it tries to access a hole, return an error.
624 * Because, the callers, functions in dir.c and GC, should be able to know
625 * whether this page exists or not.
626 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700627struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
628 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700629{
630 struct address_space *mapping = inode->i_mapping;
631 struct page *page;
632repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600633 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700634 if (IS_ERR(page))
635 return page;
636
637 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900638 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900639 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900640 f2fs_put_page(page, 1);
641 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900642 }
Chao Yu1563ac72016-07-03 22:05:12 +0800643 if (unlikely(!PageUptodate(page))) {
644 f2fs_put_page(page, 1);
645 return ERR_PTR(-EIO);
646 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900647 return page;
648}
649
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900650/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900651 * Caller ensures that this data page is never allocated.
652 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900653 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800654 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
655 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800656 * Note that, ipage is set only by make_empty_dir, and if any error occur,
657 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900658 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900659struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900660 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900661{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900662 struct address_space *mapping = inode->i_mapping;
663 struct page *page;
664 struct dnode_of_data dn;
665 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800666
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700667 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800668 if (!page) {
669 /*
670 * before exiting, we should make sure ipage will be released
671 * if any error occur.
672 */
673 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700674 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800675 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900676
Jaegeuk Kima8865372013-12-27 17:04:17 +0900677 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800678 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700679 if (err) {
680 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900681 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900682 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700683 if (!ipage)
684 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900685
686 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700687 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900688
689 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300690 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700691 if (!PageUptodate(page))
692 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900693 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700694 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900695
Jaegeuk Kim76121182016-01-01 22:03:47 -0800696 /* if ipage exists, blkaddr should be NEW_ADDR */
697 f2fs_bug_on(F2FS_I_SB(inode), ipage);
698 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700699 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800700 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900701 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700702got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800703 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700704 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700705 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900706 return page;
707}
708
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900709static int __allocate_data_block(struct dnode_of_data *dn)
710{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700711 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900712 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900713 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700714 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800715 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900716
Jaegeuk Kim91942322016-05-20 10:13:22 -0700717 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900718 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800719
720 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
721 if (dn->data_blkaddr == NEW_ADDR)
722 goto alloc;
723
Chao Yu46008c62016-05-09 19:56:30 +0800724 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900725 return -ENOSPC;
726
Chao Yudf6136e2015-03-23 10:33:37 +0800727alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900728 get_node_info(sbi, dn->nid, &ni);
729 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
730
Chao Yudf6136e2015-03-23 10:33:37 +0800731 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -0800732 &sum, CURSEG_WARM_DATA);
Chao Yu216a6202015-03-19 19:23:32 +0800733 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900734
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700735 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800736 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700737 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300738 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700739 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300740 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900741 return 0;
742}
743
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800744static inline bool __force_buffered_io(struct inode *inode, int rw)
745{
746 return ((f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) ||
747 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
748 F2FS_I_SB(inode)->s_ndevs);
749}
750
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800751int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800752{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800753 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800754 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800755 int err = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800756
Jaegeuk Kimdc91de72017-01-13 13:12:29 -0800757 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
758 return 0;
759
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700760 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800761 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
762 if (map.m_len > map.m_lblk)
763 map.m_len -= map.m_lblk;
764 else
765 map.m_len = 0;
766
Chao Yuda859852016-01-26 15:42:58 +0800767 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800768
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800769 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800770 err = f2fs_convert_inline_inode(inode);
771 if (err)
772 return err;
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800773 return f2fs_map_blocks(inode, &map, 1,
774 __force_buffered_io(inode, WRITE) ?
775 F2FS_GET_BLOCK_PRE_AIO :
776 F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800777 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800778 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800779 err = f2fs_convert_inline_inode(inode);
780 if (err)
781 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800782 }
783 if (!f2fs_has_inline_data(inode))
784 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800785 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800786}
787
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900788/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700789 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
790 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800791 * If original data blocks are allocated, then give them to blockdev.
792 * Otherwise,
793 * a. preallocate requested block addresses
794 * b. do not use extent cache for better performance
795 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900796 */
Chao Yud323d002015-10-27 09:53:45 +0800797int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800798 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900799{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700800 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900801 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800802 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700803 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800804 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900805 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800806 unsigned int ofs_in_node, last_ofs_in_node;
807 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +0000808 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +0800809 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900810
Chao Yudfd02e42016-08-20 15:12:01 +0800811 if (!maxblocks)
812 return 0;
813
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700814 map->m_len = 0;
815 map->m_flags = 0;
816
817 /* it only supports block size == page size */
818 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800819 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900820
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800821 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700822 map->m_pblk = ei.blk + pgofs - ei.fofs;
823 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
824 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900825 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800826 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900827
Chao Yu4fe71e82016-01-26 15:37:38 +0800828next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800829 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800830 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900831
832 /* When reading holes, we need its node page */
833 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900834 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900835 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800836 if (flag == F2FS_GET_BLOCK_BMAP)
837 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800838 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900839 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800840 if (map->m_next_pgofs)
841 *map->m_next_pgofs =
842 get_next_page_offset(&dn, pgofs);
843 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900844 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900845 }
Chao Yu973163f2015-09-18 16:51:51 +0800846
Chao Yu46008c62016-05-09 19:56:30 +0800847 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +0100848 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800849 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900850
Chao Yu4fe71e82016-01-26 15:37:38 +0800851next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800852 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800853
Fan Li7df3a432015-12-17 13:20:59 +0800854 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
855 if (create) {
856 if (unlikely(f2fs_cp_error(sbi))) {
857 err = -EIO;
858 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800859 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800860 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800861 if (blkaddr == NULL_ADDR) {
862 prealloc++;
863 last_ofs_in_node = dn.ofs_in_node;
864 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800865 } else {
866 err = __allocate_data_block(&dn);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800867 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700868 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800869 }
Fan Li7df3a432015-12-17 13:20:59 +0800870 if (err)
871 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +0800872 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800873 blkaddr = dn.data_blkaddr;
874 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800875 if (flag == F2FS_GET_BLOCK_BMAP) {
876 map->m_pblk = 0;
877 goto sync_out;
878 }
Chao Yuda859852016-01-26 15:42:58 +0800879 if (flag == F2FS_GET_BLOCK_FIEMAP &&
880 blkaddr == NULL_ADDR) {
881 if (map->m_next_pgofs)
882 *map->m_next_pgofs = pgofs + 1;
883 }
Fan Li7df3a432015-12-17 13:20:59 +0800884 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800885 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800886 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900887 }
888 }
Fan Li7df3a432015-12-17 13:20:59 +0800889
Chao Yu46008c62016-05-09 19:56:30 +0800890 if (flag == F2FS_GET_BLOCK_PRE_AIO)
891 goto skip;
892
Chao Yu4fe71e82016-01-26 15:37:38 +0800893 if (map->m_len == 0) {
894 /* preallocated unwritten block should be mapped for fiemap. */
895 if (blkaddr == NEW_ADDR)
896 map->m_flags |= F2FS_MAP_UNWRITTEN;
897 map->m_flags |= F2FS_MAP_MAPPED;
898
899 map->m_pblk = blkaddr;
900 map->m_len = 1;
901 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800902 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800903 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800904 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800905 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800906 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800907 } else {
908 goto sync_out;
909 }
910
Chao Yu46008c62016-05-09 19:56:30 +0800911skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800912 dn.ofs_in_node++;
913 pgofs++;
914
Chao Yu46008c62016-05-09 19:56:30 +0800915 /* preallocate blocks in batch for one dnode page */
916 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
917 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800918
Chao Yu46008c62016-05-09 19:56:30 +0800919 dn.ofs_in_node = ofs_in_node;
920 err = reserve_new_blocks(&dn, prealloc);
921 if (err)
922 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800923
Chao Yu46008c62016-05-09 19:56:30 +0800924 map->m_len += dn.ofs_in_node - ofs_in_node;
925 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
926 err = -ENOSPC;
927 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800928 }
Chao Yu46008c62016-05-09 19:56:30 +0800929 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800930 }
931
Chao Yu46008c62016-05-09 19:56:30 +0800932 if (pgofs >= end)
933 goto sync_out;
934 else if (dn.ofs_in_node < end_offset)
935 goto next_block;
936
Chao Yu46008c62016-05-09 19:56:30 +0800937 f2fs_put_dnode(&dn);
938
939 if (create) {
940 f2fs_unlock_op(sbi);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800941 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +0800942 }
Chao Yu46008c62016-05-09 19:56:30 +0800943 goto next_dnode;
944
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900945sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900946 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900947unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800948 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800949 f2fs_unlock_op(sbi);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800950 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800951 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900952out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700953 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900954 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900955}
956
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700957static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800958 struct buffer_head *bh, int create, int flag,
959 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700960{
961 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800962 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700963
964 map.m_lblk = iblock;
965 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800966 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700967
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800968 err = f2fs_map_blocks(inode, &map, create, flag);
969 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700970 map_bh(bh, inode->i_sb, map.m_pblk);
971 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +0800972 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700973 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800974 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700975}
976
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900977static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800978 struct buffer_head *bh_result, int create, int flag,
979 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900980{
Chao Yuda859852016-01-26 15:42:58 +0800981 return __get_data_block(inode, iblock, bh_result, create,
982 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900983}
984
Chao Yue2b4e2b2015-08-19 19:11:19 +0800985static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900986 struct buffer_head *bh_result, int create)
987{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800988 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800989 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800990}
991
992static int get_data_block_bmap(struct inode *inode, sector_t iblock,
993 struct buffer_head *bh_result, int create)
994{
Yunlei He179448b2015-12-28 21:48:32 +0800995 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800996 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800997 return -EFBIG;
998
Chao Yue2b4e2b2015-08-19 19:11:19 +0800999 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +08001000 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001001}
1002
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001003static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1004{
1005 return (offset >> inode->i_blkbits);
1006}
1007
1008static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1009{
1010 return (blk << inode->i_blkbits);
1011}
1012
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001013int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1014 u64 start, u64 len)
1015{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001016 struct buffer_head map_bh;
1017 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001018 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001019 u64 logical = 0, phys = 0, size = 0;
1020 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001021 int ret = 0;
1022
1023 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1024 if (ret)
1025 return ret;
1026
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001027 if (f2fs_has_inline_data(inode)) {
1028 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1029 if (ret != -EAGAIN)
1030 return ret;
1031 }
1032
Al Viro59551022016-01-22 15:40:57 -05001033 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +08001034
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001035 if (logical_to_blk(inode, len) == 0)
1036 len = blk_to_logical(inode, 1);
1037
1038 start_blk = logical_to_blk(inode, start);
1039 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001040
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001041next:
1042 memset(&map_bh, 0, sizeof(struct buffer_head));
1043 map_bh.b_size = len;
1044
Chao Yue2b4e2b2015-08-19 19:11:19 +08001045 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001046 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001047 if (ret)
1048 goto out;
1049
1050 /* HOLE */
1051 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001052 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001053
1054 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1055 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001056 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001057
Fan Li9a950d52015-12-26 18:07:41 +08001058 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001059 }
Fan Li9a950d52015-12-26 18:07:41 +08001060
Chao Yuda5af122016-01-08 20:19:27 +08001061 if (size) {
1062 if (f2fs_encrypted_inode(inode))
1063 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1064
Fan Li9a950d52015-12-26 18:07:41 +08001065 ret = fiemap_fill_next_extent(fieinfo, logical,
1066 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001067 }
Fan Li9a950d52015-12-26 18:07:41 +08001068
1069 if (start_blk > last_blk || ret)
1070 goto out;
1071
1072 logical = blk_to_logical(inode, start_blk);
1073 phys = blk_to_logical(inode, map_bh.b_blocknr);
1074 size = map_bh.b_size;
1075 flags = 0;
1076 if (buffer_unwritten(&map_bh))
1077 flags = FIEMAP_EXTENT_UNWRITTEN;
1078
1079 start_blk += logical_to_blk(inode, size);
1080
1081prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001082 cond_resched();
1083 if (fatal_signal_pending(current))
1084 ret = -EINTR;
1085 else
1086 goto next;
1087out:
1088 if (ret == 1)
1089 ret = 0;
1090
Al Viro59551022016-01-22 15:40:57 -05001091 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001092 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001093}
1094
Wei Yongjun6a7a3ae2016-08-23 15:23:59 +00001095static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
1096 unsigned nr_pages)
Chao Yu78682f72016-07-03 22:05:11 +08001097{
1098 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1099 struct fscrypt_ctx *ctx = NULL;
Chao Yu78682f72016-07-03 22:05:11 +08001100 struct bio *bio;
1101
1102 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1103 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
1104 if (IS_ERR(ctx))
1105 return ERR_CAST(ctx);
1106
1107 /* wait the page to be moved by cleaning */
1108 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
1109 }
1110
1111 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
1112 if (!bio) {
1113 if (ctx)
1114 fscrypt_release_ctx(ctx);
1115 return ERR_PTR(-ENOMEM);
1116 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001117 f2fs_target_device(sbi, blkaddr, bio);
Chao Yu78682f72016-07-03 22:05:11 +08001118 bio->bi_end_io = f2fs_read_end_io;
1119 bio->bi_private = ctx;
1120
1121 return bio;
1122}
1123
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001124/*
1125 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1126 * Major change was from block_size == page_size in f2fs by default.
1127 */
1128static int f2fs_mpage_readpages(struct address_space *mapping,
1129 struct list_head *pages, struct page *page,
1130 unsigned nr_pages)
1131{
1132 struct bio *bio = NULL;
1133 unsigned page_idx;
1134 sector_t last_block_in_bio = 0;
1135 struct inode *inode = mapping->host;
1136 const unsigned blkbits = inode->i_blkbits;
1137 const unsigned blocksize = 1 << blkbits;
1138 sector_t block_in_file;
1139 sector_t last_block;
1140 sector_t last_block_in_file;
1141 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001142 struct f2fs_map_blocks map;
1143
1144 map.m_pblk = 0;
1145 map.m_lblk = 0;
1146 map.m_len = 0;
1147 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001148 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001149
1150 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1151
1152 prefetchw(&page->flags);
1153 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001154 page = list_last_entry(pages, struct page, lru);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001155 list_del(&page->lru);
1156 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001157 page->index,
1158 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001159 goto next_page;
1160 }
1161
1162 block_in_file = (sector_t)page->index;
1163 last_block = block_in_file + nr_pages;
1164 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1165 blkbits;
1166 if (last_block > last_block_in_file)
1167 last_block = last_block_in_file;
1168
1169 /*
1170 * Map blocks using the previous result first.
1171 */
1172 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1173 block_in_file > map.m_lblk &&
1174 block_in_file < (map.m_lblk + map.m_len))
1175 goto got_it;
1176
1177 /*
1178 * Then do more f2fs_map_blocks() calls until we are
1179 * done with this page.
1180 */
1181 map.m_flags = 0;
1182
1183 if (block_in_file < last_block) {
1184 map.m_lblk = block_in_file;
1185 map.m_len = last_block - block_in_file;
1186
Chao Yu46c9e142015-09-18 16:54:16 +08001187 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001188 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001189 goto set_error_page;
1190 }
1191got_it:
1192 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1193 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1194 SetPageMappedToDisk(page);
1195
1196 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1197 SetPageUptodate(page);
1198 goto confused;
1199 }
1200 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001201 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001202 if (!PageUptodate(page))
1203 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001204 unlock_page(page);
1205 goto next_page;
1206 }
1207
1208 /*
1209 * This page will go to BIO. Do we need to send this
1210 * BIO off first?
1211 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001212 if (bio && (last_block_in_bio != block_nr - 1 ||
1213 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001214submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001215 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001216 bio = NULL;
1217 }
1218 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001219 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001220 if (IS_ERR(bio)) {
1221 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001222 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001223 }
Mike Christie04d328d2016-06-05 14:31:55 -05001224 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001225 }
1226
1227 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1228 goto submit_and_realloc;
1229
1230 last_block_in_bio = block_nr;
1231 goto next_page;
1232set_error_page:
1233 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001234 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001235 unlock_page(page);
1236 goto next_page;
1237confused:
1238 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001239 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001240 bio = NULL;
1241 }
1242 unlock_page(page);
1243next_page:
1244 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001245 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001246 }
1247 BUG_ON(pages && !list_empty(pages));
1248 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001249 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001250 return 0;
1251}
1252
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001253static int f2fs_read_data_page(struct file *file, struct page *page)
1254{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001255 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001256 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001257
Chao Yuc20e89c2014-05-06 16:53:08 +08001258 trace_f2fs_readpage(page, DATA);
1259
arter97e1c42042014-08-06 23:22:50 +09001260 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001261 if (f2fs_has_inline_data(inode))
1262 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001263 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001264 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001265 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001266}
1267
1268static int f2fs_read_data_pages(struct file *file,
1269 struct address_space *mapping,
1270 struct list_head *pages, unsigned nr_pages)
1271{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001272 struct inode *inode = file->f_mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001273 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001274
1275 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001276
1277 /* If the file has inline data, skip readpages */
1278 if (f2fs_has_inline_data(inode))
1279 return 0;
1280
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001281 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001282}
1283
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001284int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001285{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001286 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001287 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001288 struct dnode_of_data dn;
1289 int err = 0;
1290
1291 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001292 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001293 if (err)
1294 return err;
1295
Chao Yu28bc1062016-02-06 14:40:34 +08001296 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001297
1298 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001299 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001300 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001301 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001302 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001303
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001304 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001305 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001306
1307 /* wait for GCed encrypted page writeback */
1308 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001309 fio->old_blkaddr);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001310retry_encrypt:
1311 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
David Gstir9c4bb8a2016-11-13 22:20:48 +01001312 PAGE_SIZE, 0,
1313 fio->page->index,
1314 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001315 if (IS_ERR(fio->encrypted_page)) {
1316 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001317 if (err == -ENOMEM) {
1318 /* flush pending ios and wait for a while */
1319 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1320 congestion_wait(BLK_RW_ASYNC, HZ/50);
1321 gfp_flags |= __GFP_NOFAIL;
1322 err = 0;
1323 goto retry_encrypt;
1324 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001325 goto out_writepage;
1326 }
1327 }
1328
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001329 set_page_writeback(page);
1330
1331 /*
1332 * If current allocation needs SSR,
1333 * it had better in-place writes for updated data.
1334 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001335 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001336 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001337 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001338 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001339 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001340 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001341 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001342 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001343 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001344 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001345 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001346 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001347 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001348 }
1349out_writepage:
1350 f2fs_put_dnode(&dn);
1351 return err;
1352}
1353
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001354static int __write_data_page(struct page *page, bool *submitted,
1355 struct writeback_control *wbc)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001356{
1357 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001358 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001359 loff_t i_size = i_size_read(inode);
1360 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001361 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001362 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001363 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001364 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001365 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001366 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001367 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001368 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001369 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001370 .op_flags = wbc_to_write_flags(wbc),
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001371 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001372 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001373 .submitted = false,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001374 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001375
Chao Yuecda0de2014-05-06 16:48:26 +08001376 trace_f2fs_writepage(page, DATA);
1377
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001378 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001379 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001380
1381 /*
1382 * If the offset is out-of-range of file size,
1383 * this page does not have to be written to disk.
1384 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001385 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001386 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001387 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001388
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001389 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001390write:
Chao Yucaf00472015-01-28 17:48:42 +08001391 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001392 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001393 if (f2fs_is_drop_cache(inode))
1394 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001395 /* we should not write 0'th page having journal header */
1396 if (f2fs_is_volatile_file(inode) && (!page->index ||
1397 (!wbc->for_reclaim &&
1398 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001399 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001400
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001401 /* we should bypass data pages to proceed the kworkder jobs */
1402 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001403 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001404 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001405 }
1406
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001407 /* Dentry blocks are controlled by checkpoint */
1408 if (S_ISDIR(inode->i_mode)) {
1409 err = do_write_data_page(&fio);
1410 goto done;
1411 }
1412
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001413 if (!wbc->for_reclaim)
1414 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001415 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001416 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001417
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001418 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08001419 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001420 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08001421 if (!err)
1422 goto out;
1423 }
1424 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001425 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001426 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001427 if (F2FS_I(inode)->last_disk_size < psize)
1428 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001429 f2fs_unlock_op(sbi);
1430done:
1431 if (err && err != -ENOENT)
1432 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001433
Jaegeuk Kim39936832012-11-22 16:21:29 +09001434out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001435 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001436 if (err)
1437 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001438
1439 if (wbc->for_reclaim) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001440 f2fs_submit_merged_bio_cond(sbi, inode, 0, page->index,
1441 DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001442 remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001443 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001444 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001445
1446 unlock_page(page);
1447 f2fs_balance_fs(sbi, need_balance_fs);
1448
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001449 if (unlikely(f2fs_cp_error(sbi))) {
Chao Yu0c3a5792016-01-18 18:28:11 +08001450 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001451 submitted = NULL;
1452 }
1453
1454 if (submitted)
1455 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001456
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001457 return 0;
1458
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001459redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001460 redirty_page_for_writepage(wbc, page);
Chao Yu0002b612016-11-28 19:13:43 -08001461 if (!err)
1462 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001463 unlock_page(page);
1464 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001465}
1466
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001467static int f2fs_write_data_page(struct page *page,
1468 struct writeback_control *wbc)
1469{
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001470 return __write_data_page(page, NULL, wbc);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001471}
1472
Chao Yu8f46dca2015-07-14 18:56:10 +08001473/*
1474 * This function was copied from write_cche_pages from mm/page-writeback.c.
1475 * The major change is making write step of cold data page separately from
1476 * warm/hot data page.
1477 */
1478static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001479 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001480{
1481 int ret = 0;
1482 int done = 0;
1483 struct pagevec pvec;
1484 int nr_pages;
1485 pgoff_t uninitialized_var(writeback_index);
1486 pgoff_t index;
1487 pgoff_t end; /* Inclusive */
1488 pgoff_t done_index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001489 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001490 int cycled;
1491 int range_whole = 0;
1492 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001493
1494 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001495
Chao Yu8f46dca2015-07-14 18:56:10 +08001496 if (wbc->range_cyclic) {
1497 writeback_index = mapping->writeback_index; /* prev offset */
1498 index = writeback_index;
1499 if (index == 0)
1500 cycled = 1;
1501 else
1502 cycled = 0;
1503 end = -1;
1504 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001505 index = wbc->range_start >> PAGE_SHIFT;
1506 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001507 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1508 range_whole = 1;
1509 cycled = 1; /* ignore range_cyclic tests */
1510 }
1511 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1512 tag = PAGECACHE_TAG_TOWRITE;
1513 else
1514 tag = PAGECACHE_TAG_DIRTY;
1515retry:
1516 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1517 tag_pages_for_writeback(mapping, index, end);
1518 done_index = index;
1519 while (!done && (index <= end)) {
1520 int i;
1521
1522 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1523 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1524 if (nr_pages == 0)
1525 break;
1526
1527 for (i = 0; i < nr_pages; i++) {
1528 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001529 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001530
1531 if (page->index > end) {
1532 done = 1;
1533 break;
1534 }
1535
1536 done_index = page->index;
1537
1538 lock_page(page);
1539
1540 if (unlikely(page->mapping != mapping)) {
1541continue_unlock:
1542 unlock_page(page);
1543 continue;
1544 }
1545
1546 if (!PageDirty(page)) {
1547 /* someone wrote it for us */
1548 goto continue_unlock;
1549 }
1550
Chao Yu8f46dca2015-07-14 18:56:10 +08001551 if (PageWriteback(page)) {
1552 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001553 f2fs_wait_on_page_writeback(page,
1554 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001555 else
1556 goto continue_unlock;
1557 }
1558
1559 BUG_ON(PageWriteback(page));
1560 if (!clear_page_dirty_for_io(page))
1561 goto continue_unlock;
1562
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001563 ret = __write_data_page(page, &submitted, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001564 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08001565 /*
1566 * keep nr_to_write, since vfs uses this to
1567 * get # of written pages.
1568 */
1569 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1570 unlock_page(page);
1571 ret = 0;
1572 continue;
1573 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001574 done_index = page->index + 1;
1575 done = 1;
1576 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001577 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001578 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08001579 }
1580
1581 if (--wbc->nr_to_write <= 0 &&
1582 wbc->sync_mode == WB_SYNC_NONE) {
1583 done = 1;
1584 break;
1585 }
1586 }
1587 pagevec_release(&pvec);
1588 cond_resched();
1589 }
1590
Chao Yu8f46dca2015-07-14 18:56:10 +08001591 if (!cycled && !done) {
1592 cycled = 1;
1593 index = 0;
1594 end = writeback_index - 1;
1595 goto retry;
1596 }
1597 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1598 mapping->writeback_index = done_index;
1599
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001600 if (last_idx != ULONG_MAX)
Chao Yu6ca56ca2016-09-29 18:50:11 +08001601 f2fs_submit_merged_bio_cond(F2FS_M_SB(mapping), mapping->host,
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001602 0, last_idx, DATA, WRITE);
Chao Yu6ca56ca2016-09-29 18:50:11 +08001603
Chao Yu8f46dca2015-07-14 18:56:10 +08001604 return ret;
1605}
1606
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001607static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001608 struct writeback_control *wbc)
1609{
1610 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001611 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001612 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001613 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001614
P J Pcfb185a2013-04-03 11:38:00 +09001615 /* deal with chardevs and other special file */
1616 if (!mapping->a_ops->writepage)
1617 return 0;
1618
Chao Yu6a290542015-07-17 18:02:39 +08001619 /* skip writing if there is no dirty page in this inode */
1620 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1621 return 0;
1622
Jaegeuk Kima1257022015-10-08 10:40:07 -07001623 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1624 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1625 available_free_memory(sbi, DIRTY_DENTS))
1626 goto skip_write;
1627
Chao Yud323d002015-10-27 09:53:45 +08001628 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001629 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001630 goto skip_write;
1631
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001632 /* during POR, we don't need to trigger writepage at all. */
1633 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1634 goto skip_write;
1635
Yunlei Hed31c7c32016-02-04 16:14:00 +08001636 trace_f2fs_writepages(mapping->host, wbc, DATA);
1637
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001638 blk_start_plug(&plug);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001639 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001640 blk_finish_plug(&plug);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001641 /*
1642 * if some pages were truncated, we cannot guarantee its mapping->host
1643 * to detect pending bios.
1644 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001645
Chao Yuc227f912015-12-16 13:09:20 +08001646 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001647 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001648
1649skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001650 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001651 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001652 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001653}
1654
Chao Yu3aab8f82014-07-02 13:25:04 +08001655static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1656{
1657 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001658 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001659
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001660 if (to > i_size) {
1661 truncate_pagecache(inode, i_size);
1662 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001663 }
1664}
1665
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001666static int prepare_write_begin(struct f2fs_sb_info *sbi,
1667 struct page *page, loff_t pos, unsigned len,
1668 block_t *blk_addr, bool *node_changed)
1669{
1670 struct inode *inode = page->mapping->host;
1671 pgoff_t index = page->index;
1672 struct dnode_of_data dn;
1673 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001674 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00001675 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001676 int err = 0;
1677
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001678 /*
1679 * we already allocated all the blocks, so we don't need to get
1680 * the block addresses when there is no need to fill the page.
1681 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08001682 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
1683 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001684 return 0;
1685
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001686 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001687 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001688 f2fs_lock_op(sbi);
1689 locked = true;
1690 }
1691restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001692 /* check inline_data */
1693 ipage = get_node_page(sbi, inode->i_ino);
1694 if (IS_ERR(ipage)) {
1695 err = PTR_ERR(ipage);
1696 goto unlock_out;
1697 }
1698
1699 set_new_dnode(&dn, inode, ipage, ipage, 0);
1700
1701 if (f2fs_has_inline_data(inode)) {
1702 if (pos + len <= MAX_INLINE_DATA) {
1703 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001704 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001705 if (inode->i_nlink)
1706 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001707 } else {
1708 err = f2fs_convert_inline_page(&dn, page);
1709 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001710 goto out;
1711 if (dn.data_blkaddr == NULL_ADDR)
1712 err = f2fs_get_block(&dn, index);
1713 }
1714 } else if (locked) {
1715 err = f2fs_get_block(&dn, index);
1716 } else {
1717 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1718 dn.data_blkaddr = ei.blk + index - ei.fofs;
1719 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001720 /* hole case */
1721 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001722 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001723 f2fs_put_dnode(&dn);
1724 f2fs_lock_op(sbi);
1725 locked = true;
1726 goto restart;
1727 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001728 }
1729 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001730
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001731 /* convert_inline_page can make node_changed */
1732 *blk_addr = dn.data_blkaddr;
1733 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001734out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001735 f2fs_put_dnode(&dn);
1736unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001737 if (locked)
1738 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001739 return err;
1740}
1741
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001742static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1743 loff_t pos, unsigned len, unsigned flags,
1744 struct page **pagep, void **fsdata)
1745{
1746 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001747 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001748 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001749 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001750 bool need_balance = false;
1751 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001752 int err = 0;
1753
Chao Yu62aed042014-05-06 16:46:04 +08001754 trace_f2fs_write_begin(inode, pos, len, flags);
1755
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001756 /*
1757 * We should check this at this moment to avoid deadlock on inode page
1758 * and #0 page. The locking rule for inline_data conversion should be:
1759 * lock_page(page #0) -> lock_page(inode_page)
1760 */
1761 if (index != 0) {
1762 err = f2fs_convert_inline_inode(inode);
1763 if (err)
1764 goto fail;
1765 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001766repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08001767 /*
1768 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1769 * wait_for_stable_page. Will wait that below with our IO control.
1770 */
1771 page = pagecache_get_page(mapping, index,
1772 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08001773 if (!page) {
1774 err = -ENOMEM;
1775 goto fail;
1776 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001777
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001778 *pagep = page;
1779
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001780 err = prepare_write_begin(sbi, page, pos, len,
1781 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001782 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001783 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001784
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001785 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001786 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001787 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001788 lock_page(page);
1789 if (page->mapping != mapping) {
1790 /* The page got truncated from under us */
1791 f2fs_put_page(page, 1);
1792 goto repeat;
1793 }
1794 }
1795
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001796 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001797
Chao Yu08b39fb2015-10-08 13:27:34 +08001798 /* wait for GCed encrypted page writeback */
1799 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001800 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001801
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001802 if (len == PAGE_SIZE || PageUptodate(page))
1803 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001804
Yunlei He746e2402016-12-20 11:11:35 +08001805 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
1806 zero_user_segment(page, len, PAGE_SIZE);
1807 return 0;
1808 }
1809
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001810 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001811 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001812 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001813 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001814 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001815
Chao Yu78682f72016-07-03 22:05:11 +08001816 bio = f2fs_grab_bio(inode, blkaddr, 1);
1817 if (IS_ERR(bio)) {
1818 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001819 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001820 }
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001821 bio->bi_opf = REQ_OP_READ;
Chao Yu78682f72016-07-03 22:05:11 +08001822 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1823 bio_put(bio);
1824 err = -EFAULT;
1825 goto fail;
1826 }
1827
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001828 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001829
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001830 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001831 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001832 f2fs_put_page(page, 1);
1833 goto repeat;
1834 }
Chao Yu1563ac72016-07-03 22:05:12 +08001835 if (unlikely(!PageUptodate(page))) {
1836 err = -EIO;
1837 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001838 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001839 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001840 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001841
Chao Yu3aab8f82014-07-02 13:25:04 +08001842fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001843 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001844 f2fs_write_failed(mapping, pos + len);
1845 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001846}
1847
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001848static int f2fs_write_end(struct file *file,
1849 struct address_space *mapping,
1850 loff_t pos, unsigned len, unsigned copied,
1851 struct page *page, void *fsdata)
1852{
1853 struct inode *inode = page->mapping->host;
1854
Chao Yudfb2bf32014-05-06 16:47:23 +08001855 trace_f2fs_write_end(inode, pos, len, copied);
1856
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001857 /*
1858 * This should be come from len == PAGE_SIZE, and we expect copied
1859 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
1860 * let generic_perform_write() try to copy data again through copied=0.
1861 */
1862 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08001863 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001864 copied = 0;
1865 else
1866 SetPageUptodate(page);
1867 }
1868 if (!copied)
1869 goto unlock_out;
1870
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001871 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001872
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001873 if (pos + copied > i_size_read(inode))
1874 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001875unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08001876 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001877 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001878 return copied;
1879}
1880
Omar Sandoval6f673762015-03-16 04:33:52 -07001881static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1882 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001883{
1884 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001885
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001886 if (offset & blocksize_mask)
1887 return -EINVAL;
1888
Al Viro5b46f252014-03-16 18:07:34 -04001889 if (iov_iter_alignment(iter) & blocksize_mask)
1890 return -EINVAL;
1891
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001892 return 0;
1893}
1894
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001895static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001896{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001897 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001898 struct inode *inode = mapping->host;
1899 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001900 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08001901 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08001902 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001903
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001904 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001905 if (err)
1906 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001907
Jaegeuk Kimc040ff92016-11-11 16:46:40 -08001908 if (__force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001909 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001910
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001911 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fad2014-07-31 21:11:22 +08001912
Chao Yu82e0a5a2016-07-13 09:18:29 +08001913 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001914 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08001915 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
1916
1917 if (rw == WRITE) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001918 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001919 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001920 else if (err < 0)
1921 f2fs_write_failed(mapping, offset + count);
1922 }
Chao Yu70407fad2014-07-31 21:11:22 +08001923
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001924 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fad2014-07-31 21:11:22 +08001925
Chao Yu3aab8f82014-07-02 13:25:04 +08001926 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001927}
1928
Chao Yu487261f2015-02-05 17:44:29 +08001929void f2fs_invalidate_page(struct page *page, unsigned int offset,
1930 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001931{
1932 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001933 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001934
Chao Yu487261f2015-02-05 17:44:29 +08001935 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001936 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001937 return;
1938
Chao Yu487261f2015-02-05 17:44:29 +08001939 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08001940 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08001941 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08001942 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08001943 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08001944 } else {
Chao Yu487261f2015-02-05 17:44:29 +08001945 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08001946 remove_dirty_inode(inode);
1947 }
Chao Yu487261f2015-02-05 17:44:29 +08001948 }
Chao Yudecd36b2015-08-07 18:42:09 +08001949
1950 /* This is atomic written page, keep Private */
1951 if (IS_ATOMIC_WRITTEN_PAGE(page))
1952 return;
1953
Chao Yu23dc9742016-04-29 20:09:15 +08001954 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001955 ClearPagePrivate(page);
1956}
1957
Chao Yu487261f2015-02-05 17:44:29 +08001958int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001959{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001960 /* If this is dirty page, keep PagePrivate */
1961 if (PageDirty(page))
1962 return 0;
1963
Chao Yudecd36b2015-08-07 18:42:09 +08001964 /* This is atomic written page, keep Private */
1965 if (IS_ATOMIC_WRITTEN_PAGE(page))
1966 return 0;
1967
Chao Yu23dc9742016-04-29 20:09:15 +08001968 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001969 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001970 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001971}
1972
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001973/*
1974 * This was copied from __set_page_dirty_buffers which gives higher performance
1975 * in very high speed storages. (e.g., pmem)
1976 */
1977void f2fs_set_page_dirty_nobuffers(struct page *page)
1978{
1979 struct address_space *mapping = page->mapping;
1980 unsigned long flags;
1981
1982 if (unlikely(!mapping))
1983 return;
1984
1985 spin_lock(&mapping->private_lock);
1986 lock_page_memcg(page);
1987 SetPageDirty(page);
1988 spin_unlock(&mapping->private_lock);
1989
1990 spin_lock_irqsave(&mapping->tree_lock, flags);
1991 WARN_ON_ONCE(!PageUptodate(page));
1992 account_page_dirtied(page, mapping);
1993 radix_tree_tag_set(&mapping->page_tree,
1994 page_index(page), PAGECACHE_TAG_DIRTY);
1995 spin_unlock_irqrestore(&mapping->tree_lock, flags);
1996 unlock_page_memcg(page);
1997
1998 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1999 return;
2000}
2001
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002002static int f2fs_set_data_page_dirty(struct page *page)
2003{
2004 struct address_space *mapping = page->mapping;
2005 struct inode *inode = mapping->host;
2006
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002007 trace_f2fs_set_page_dirty(page, DATA);
2008
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002009 if (!PageUptodate(page))
2010 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002011
Chao Yu5fe45742017-01-07 18:50:26 +08002012 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002013 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2014 register_inmem_page(inode, page);
2015 return 1;
2016 }
2017 /*
2018 * Previously, this page has been registered, we just
2019 * return here.
2020 */
2021 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002022 }
2023
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002024 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002025 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002026 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002027 return 1;
2028 }
2029 return 0;
2030}
2031
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002032static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2033{
Chao Yu454ae7e2014-04-22 13:34:01 +08002034 struct inode *inode = mapping->host;
2035
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002036 if (f2fs_has_inline_data(inode))
2037 return 0;
2038
2039 /* make sure allocating whole blocks */
2040 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2041 filemap_write_and_wait(mapping);
2042
Chao Yue2b4e2b2015-08-19 19:11:19 +08002043 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002044}
2045
Weichao Guo5b7a4872016-09-20 05:03:27 +08002046#ifdef CONFIG_MIGRATION
2047#include <linux/migrate.h>
2048
2049int f2fs_migrate_page(struct address_space *mapping,
2050 struct page *newpage, struct page *page, enum migrate_mode mode)
2051{
2052 int rc, extra_count;
2053 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2054 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2055
2056 BUG_ON(PageWriteback(page));
2057
2058 /* migrating an atomic written page is safe with the inmem_lock hold */
2059 if (atomic_written && !mutex_trylock(&fi->inmem_lock))
2060 return -EAGAIN;
2061
2062 /*
2063 * A reference is expected if PagePrivate set when move mapping,
2064 * however F2FS breaks this for maintaining dirty page counts when
2065 * truncating pages. So here adjusting the 'extra_count' make it work.
2066 */
2067 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2068 rc = migrate_page_move_mapping(mapping, newpage,
2069 page, NULL, mode, extra_count);
2070 if (rc != MIGRATEPAGE_SUCCESS) {
2071 if (atomic_written)
2072 mutex_unlock(&fi->inmem_lock);
2073 return rc;
2074 }
2075
2076 if (atomic_written) {
2077 struct inmem_pages *cur;
2078 list_for_each_entry(cur, &fi->inmem_pages, list)
2079 if (cur->page == page) {
2080 cur->page = newpage;
2081 break;
2082 }
2083 mutex_unlock(&fi->inmem_lock);
2084 put_page(page);
2085 get_page(newpage);
2086 }
2087
2088 if (PagePrivate(page))
2089 SetPagePrivate(newpage);
2090 set_page_private(newpage, page_private(page));
2091
2092 migrate_page_copy(newpage, page);
2093
2094 return MIGRATEPAGE_SUCCESS;
2095}
2096#endif
2097
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002098const struct address_space_operations f2fs_dblock_aops = {
2099 .readpage = f2fs_read_data_page,
2100 .readpages = f2fs_read_data_pages,
2101 .writepage = f2fs_write_data_page,
2102 .writepages = f2fs_write_data_pages,
2103 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002104 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002105 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002106 .invalidatepage = f2fs_invalidate_page,
2107 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002108 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002109 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002110#ifdef CONFIG_MIGRATION
2111 .migratepage = f2fs_migrate_page,
2112#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002113};