blob: 5f3bc98a387d4448e6d9091f9c76cf093e12264f [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 Yu1ecc0c52016-09-23 21:30:09 +080058 if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_IO))
Chao Yu8b038c72016-09-18 23:30:07 +080059 bio->bi_error = -EIO;
60#endif
61
Chao Yu12377022015-05-25 18:03:38 +080062 if (f2fs_bio_encrypted(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020063 if (bio->bi_error) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070064 fscrypt_release_ctx(bio->bi_private);
Chao Yu12377022015-05-25 18:03:38 +080065 } else {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070066 fscrypt_decrypt_bio_pages(bio->bi_private, bio);
Chao Yu12377022015-05-25 18:03:38 +080067 return;
68 }
69 }
70
Linus Torvaldsf5688492014-01-30 11:19:05 -080071 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090072 struct page *page = bvec->bv_page;
73
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020074 if (!bio->bi_error) {
Jaegeuk Kim237c0792016-06-30 18:49:15 -070075 if (!PageUptodate(page))
76 SetPageUptodate(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080077 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090078 ClearPageUptodate(page);
79 SetPageError(page);
80 }
81 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080082 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090083 bio_put(bio);
84}
85
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020086static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090087{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090088 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080089 struct bio_vec *bvec;
90 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090091
Linus Torvaldsf5688492014-01-30 11:19:05 -080092 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090093 struct page *page = bvec->bv_page;
Chao Yu36951b32016-11-16 10:41:20 +080094 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090095
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -080096 if (IS_DUMMY_WRITTEN_PAGE(page)) {
97 set_page_private(page, (unsigned long)NULL);
98 ClearPagePrivate(page);
99 unlock_page(page);
100 mempool_free(page, sbi->write_io_dummy);
101
102 if (unlikely(bio->bi_error))
103 f2fs_stop_checkpoint(sbi, true);
104 continue;
105 }
106
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700107 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700108
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200109 if (unlikely(bio->bi_error)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700110 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700111 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900112 }
Chao Yu36951b32016-11-16 10:41:20 +0800113 dec_page_count(sbi, type);
114 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900115 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800116 }
Chao Yu36951b32016-11-16 10:41:20 +0800117 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700118 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900119 wake_up(&sbi->cp_wait);
120
121 bio_put(bio);
122}
123
Gu Zheng940a6d32013-12-20 17:39:59 +0800124/*
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700125 * Return true, if pre_bio's bdev is same as its target device.
126 */
127struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
128 block_t blk_addr, struct bio *bio)
129{
130 struct block_device *bdev = sbi->sb->s_bdev;
131 int i;
132
133 for (i = 0; i < sbi->s_ndevs; i++) {
134 if (FDEV(i).start_blk <= blk_addr &&
135 FDEV(i).end_blk >= blk_addr) {
136 blk_addr -= FDEV(i).start_blk;
137 bdev = FDEV(i).bdev;
138 break;
139 }
140 }
141 if (bio) {
142 bio->bi_bdev = bdev;
143 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
144 }
145 return bdev;
146}
147
148int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
149{
150 int i;
151
152 for (i = 0; i < sbi->s_ndevs; i++)
153 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
154 return i;
155 return 0;
156}
157
158static bool __same_bdev(struct f2fs_sb_info *sbi,
159 block_t blk_addr, struct bio *bio)
160{
161 return f2fs_target_device(sbi, blk_addr, NULL) == bio->bi_bdev;
162}
163
164/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800165 * Low-level block read/write IO operations.
166 */
167static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
168 int npages, bool is_read)
169{
170 struct bio *bio;
171
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700172 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +0800173
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700174 f2fs_target_device(sbi, blk_addr, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800175 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +0800176 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800177
178 return bio;
179}
180
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700181static inline void __submit_bio(struct f2fs_sb_info *sbi,
182 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700183{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700184 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800185 unsigned int start;
186
Damien Le Moal0bfd7a02016-10-28 17:45:00 +0900187 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700188 current->plug && (type == DATA || type == NODE))
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700189 blk_finish_plug(current->plug);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800190
191 if (type != DATA && type != NODE)
192 goto submit_io;
193
194 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
195 start %= F2FS_IO_SIZE(sbi);
196
197 if (start == 0)
198 goto submit_io;
199
200 /* fill dummy pages */
201 for (; start < F2FS_IO_SIZE(sbi); start++) {
202 struct page *page =
203 mempool_alloc(sbi->write_io_dummy,
204 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
205 f2fs_bug_on(sbi, !page);
206
207 SetPagePrivate(page);
208 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
209 lock_page(page);
210 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
211 f2fs_bug_on(sbi, 1);
212 }
213 /*
214 * In the NODE case, we lose next block address chain. So, we
215 * need to do checkpoint in f2fs_sync_file.
216 */
217 if (type == NODE)
218 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700219 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800220submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800221 if (is_read_io(bio_op(bio)))
222 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
223 else
224 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500225 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700226}
227
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900228static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900229{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900230 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900231
232 if (!io->bio)
233 return;
234
Mike Christie04d328d2016-06-05 14:31:55 -0500235 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
236
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800237 if (is_read_io(fio->op))
238 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
239 else
240 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
241
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700242 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900243 io->bio = NULL;
244}
245
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800246static bool __has_merged_page(struct f2fs_bio_info *io,
247 struct inode *inode, nid_t ino, pgoff_t idx)
Chao Yu0fd785e2016-01-18 18:24:59 +0800248{
Chao Yu0fd785e2016-01-18 18:24:59 +0800249 struct bio_vec *bvec;
250 struct page *target;
251 int i;
252
Chao Yu0c3a5792016-01-18 18:28:11 +0800253 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800254 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800255
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800256 if (!inode && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800257 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800258
259 bio_for_each_segment_all(bvec, io->bio, i) {
260
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700261 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800262 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700263 else
264 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800265
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800266 if (idx != target->index)
267 continue;
268
Chao Yu0c3a5792016-01-18 18:28:11 +0800269 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800270 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800271 if (ino && ino == ino_of_node(target))
272 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800273 }
274
Chao Yu0fd785e2016-01-18 18:24:59 +0800275 return false;
276}
277
Chao Yu0c3a5792016-01-18 18:28:11 +0800278static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800279 nid_t ino, pgoff_t idx, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800280{
281 enum page_type btype = PAGE_TYPE_OF_BIO(type);
282 struct f2fs_bio_info *io = &sbi->write_io[btype];
283 bool ret;
284
285 down_read(&io->io_rwsem);
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800286 ret = __has_merged_page(io, inode, ino, idx);
Chao Yu0c3a5792016-01-18 18:28:11 +0800287 up_read(&io->io_rwsem);
288 return ret;
289}
290
291static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800292 struct inode *inode, nid_t ino, pgoff_t idx,
293 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900294{
295 enum page_type btype = PAGE_TYPE_OF_BIO(type);
296 struct f2fs_bio_info *io;
297
298 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
299
Chao Yudf0f8dc2014-03-22 14:57:23 +0800300 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900301
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800302 if (!__has_merged_page(io, inode, ino, idx))
Chao Yu0c3a5792016-01-18 18:28:11 +0800303 goto out;
304
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900305 /* change META to META_FLUSH in the checkpoint procedure */
306 if (type >= META_FLUSH) {
307 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500308 io->fio.op = REQ_OP_WRITE;
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800309 io->fio.op_flags = REQ_META | REQ_PRIO;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600310 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800311 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900312 }
313 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800314out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800315 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900316}
317
Chao Yu0c3a5792016-01-18 18:28:11 +0800318void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
319 int rw)
320{
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800321 __f2fs_submit_merged_bio(sbi, NULL, 0, 0, type, rw);
Chao Yu0c3a5792016-01-18 18:28:11 +0800322}
323
324void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800325 struct inode *inode, nid_t ino, pgoff_t idx,
326 enum page_type type, int rw)
Chao Yu0c3a5792016-01-18 18:28:11 +0800327{
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800328 if (has_merged_page(sbi, inode, ino, idx, type))
329 __f2fs_submit_merged_bio(sbi, inode, ino, idx, type, rw);
Chao Yu0c3a5792016-01-18 18:28:11 +0800330}
331
Chao Yu406657d2016-02-24 17:17:55 +0800332void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
333{
334 f2fs_submit_merged_bio(sbi, DATA, WRITE);
335 f2fs_submit_merged_bio(sbi, NODE, WRITE);
336 f2fs_submit_merged_bio(sbi, META, WRITE);
337}
338
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900339/*
340 * Fill the locked page with data located in the block address.
341 * Return unlocked page.
342 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700343int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900344{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900345 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700346 struct page *page = fio->encrypted_page ?
347 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900348
Chao Yu2ace38e2014-12-24 16:08:14 +0800349 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700350 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900351
352 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500353 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900354
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300355 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900356 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900357 return -EFAULT;
358 }
Mike Christie04d328d2016-06-05 14:31:55 -0500359 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900360
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700361 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900362 return 0;
363}
364
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800365int f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900366{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700367 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900368 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900369 struct f2fs_bio_info *io;
Mike Christie04d328d2016-06-05 14:31:55 -0500370 bool is_read = is_read_io(fio->op);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700371 struct page *bio_page;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800372 int err = 0;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900373
Gu Zheng940a6d32013-12-20 17:39:59 +0800374 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900375
Chao Yu7a9d7542016-02-22 18:36:38 +0800376 if (fio->old_blkaddr != NEW_ADDR)
377 verify_block_addr(sbi, fio->old_blkaddr);
378 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900379
Chao Yu36951b32016-11-16 10:41:20 +0800380 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
381
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800382 /* set submitted = 1 as a return value */
383 fio->submitted = 1;
384
Chao Yu36951b32016-11-16 10:41:20 +0800385 if (!is_read)
386 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
387
Chao Yudf0f8dc2014-03-22 14:57:23 +0800388 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900389
Chao Yu7a9d7542016-02-22 18:36:38 +0800390 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700391 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
392 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900393 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900394alloc_new:
395 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800396 if ((fio->type == DATA || fio->type == NODE) &&
397 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
398 err = -EAGAIN;
399 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
400 goto out_fail;
401 }
Chao Yu7a9d7542016-02-22 18:36:38 +0800402 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
Jaegeuk Kim664ba972016-10-18 11:07:45 -0700403 BIO_MAX_PAGES, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900404 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900405 }
406
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300407 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
408 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900409 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900410 goto alloc_new;
411 }
412
Chao Yu7a9d7542016-02-22 18:36:38 +0800413 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700414 f2fs_trace_ios(fio, 0);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800415out_fail:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800416 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700417 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800418 return err;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900419}
420
Chao Yu46008c62016-05-09 19:56:30 +0800421static void __set_data_blkaddr(struct dnode_of_data *dn)
422{
423 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
424 __le32 *addr_array;
425
426 /* Get physical address of data block */
427 addr_array = blkaddr_in_node(rn);
428 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
429}
430
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900431/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900432 * Lock ordering for the change of data block address:
433 * ->data_page
434 * ->node_page
435 * update block addresses in the node page
436 */
Chao Yu216a6202015-03-19 19:23:32 +0800437void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900438{
Chao Yu46008c62016-05-09 19:56:30 +0800439 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
440 __set_data_blkaddr(dn);
441 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800442 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900443}
444
Chao Yuf28b3432016-02-24 17:16:47 +0800445void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
446{
447 dn->data_blkaddr = blkaddr;
448 set_data_blkaddr(dn);
449 f2fs_update_extent_cache(dn);
450}
451
Chao Yu46008c62016-05-09 19:56:30 +0800452/* dn->ofs_in_node will be returned with up-to-date last block pointer */
453int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900454{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700455 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900456
Chao Yu46008c62016-05-09 19:56:30 +0800457 if (!count)
458 return 0;
459
Jaegeuk Kim91942322016-05-20 10:13:22 -0700460 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900461 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800462 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900463 return -ENOSPC;
464
Chao Yu46008c62016-05-09 19:56:30 +0800465 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
466 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900467
Chao Yu46008c62016-05-09 19:56:30 +0800468 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
469
470 for (; count > 0; dn->ofs_in_node++) {
471 block_t blkaddr =
472 datablock_addr(dn->node_page, dn->ofs_in_node);
473 if (blkaddr == NULL_ADDR) {
474 dn->data_blkaddr = NEW_ADDR;
475 __set_data_blkaddr(dn);
476 count--;
477 }
478 }
479
480 if (set_page_dirty(dn->node_page))
481 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900482 return 0;
483}
484
Chao Yu46008c62016-05-09 19:56:30 +0800485/* Should keep dn->ofs_in_node unchanged */
486int reserve_new_block(struct dnode_of_data *dn)
487{
488 unsigned int ofs_in_node = dn->ofs_in_node;
489 int ret;
490
491 ret = reserve_new_blocks(dn, 1);
492 dn->ofs_in_node = ofs_in_node;
493 return ret;
494}
495
Huajun Lib6009652013-11-10 23:13:18 +0800496int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
497{
498 bool need_put = dn->inode_page ? false : true;
499 int err;
500
501 err = get_dnode_of_data(dn, index, ALLOC_NODE);
502 if (err)
503 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900504
Huajun Lib6009652013-11-10 23:13:18 +0800505 if (dn->data_blkaddr == NULL_ADDR)
506 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900507 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800508 f2fs_put_dnode(dn);
509 return err;
510}
511
Fan Li759af1c2015-08-05 15:52:16 +0800512int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900513{
Chao Yu028a41e2015-03-19 19:26:02 +0800514 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800515 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800516
Fan Li759af1c2015-08-05 15:52:16 +0800517 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
518 dn->data_blkaddr = ei.blk + index - ei.fofs;
519 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800520 }
521
Fan Li759af1c2015-08-05 15:52:16 +0800522 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900523}
524
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700525struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500526 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900527{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900528 struct address_space *mapping = inode->i_mapping;
529 struct dnode_of_data dn;
530 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800531 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900532 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800533 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700534 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800535 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500536 .op = REQ_OP_READ,
537 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700538 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800539 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900540
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700541 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
542 return read_mapping_page(mapping, index, NULL);
543
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700544 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900545 if (!page)
546 return ERR_PTR(-ENOMEM);
547
Chao Yucb3bc9e2015-02-05 18:03:40 +0800548 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
549 dn.data_blkaddr = ei.blk + index - ei.fofs;
550 goto got_it;
551 }
552
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900553 set_new_dnode(&dn, inode, NULL, NULL, 0);
554 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700555 if (err)
556 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900557 f2fs_put_dnode(&dn);
558
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900559 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700560 err = -ENOENT;
561 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900562 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800563got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700564 if (PageUptodate(page)) {
565 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900566 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700567 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900568
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900569 /*
570 * A new dentry page is allocated but not able to be written, since its
571 * new inode page couldn't be allocated due to -ENOSPC.
572 * In such the case, its blkaddr can be remained as NEW_ADDR.
573 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
574 */
575 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300576 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700577 if (!PageUptodate(page))
578 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700579 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900580 return page;
581 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900582
Chao Yu7a9d7542016-02-22 18:36:38 +0800583 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700584 fio.page = page;
585 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900586 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700587 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700588 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700589
590put_err:
591 f2fs_put_page(page, 1);
592 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700593}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900594
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700595struct page *find_data_page(struct inode *inode, pgoff_t index)
596{
597 struct address_space *mapping = inode->i_mapping;
598 struct page *page;
599
600 page = find_get_page(mapping, index);
601 if (page && PageUptodate(page))
602 return page;
603 f2fs_put_page(page, 0);
604
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600605 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700606 if (IS_ERR(page))
607 return page;
608
609 if (PageUptodate(page))
610 return page;
611
612 wait_on_page_locked(page);
613 if (unlikely(!PageUptodate(page))) {
614 f2fs_put_page(page, 0);
615 return ERR_PTR(-EIO);
616 }
617 return page;
618}
619
620/*
621 * If it tries to access a hole, return an error.
622 * Because, the callers, functions in dir.c and GC, should be able to know
623 * whether this page exists or not.
624 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700625struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
626 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700627{
628 struct address_space *mapping = inode->i_mapping;
629 struct page *page;
630repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600631 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700632 if (IS_ERR(page))
633 return page;
634
635 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900636 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900637 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900638 f2fs_put_page(page, 1);
639 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900640 }
Chao Yu1563ac72016-07-03 22:05:12 +0800641 if (unlikely(!PageUptodate(page))) {
642 f2fs_put_page(page, 1);
643 return ERR_PTR(-EIO);
644 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900645 return page;
646}
647
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900648/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900649 * Caller ensures that this data page is never allocated.
650 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900651 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800652 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
653 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800654 * Note that, ipage is set only by make_empty_dir, and if any error occur,
655 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900656 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900657struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900658 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900659{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900660 struct address_space *mapping = inode->i_mapping;
661 struct page *page;
662 struct dnode_of_data dn;
663 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800664
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700665 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800666 if (!page) {
667 /*
668 * before exiting, we should make sure ipage will be released
669 * if any error occur.
670 */
671 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700672 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800673 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900674
Jaegeuk Kima8865372013-12-27 17:04:17 +0900675 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800676 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700677 if (err) {
678 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900679 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900680 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700681 if (!ipage)
682 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900683
684 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700685 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900686
687 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300688 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700689 if (!PageUptodate(page))
690 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900691 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700692 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900693
Jaegeuk Kim76121182016-01-01 22:03:47 -0800694 /* if ipage exists, blkaddr should be NEW_ADDR */
695 f2fs_bug_on(F2FS_I_SB(inode), ipage);
696 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700697 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800698 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900699 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700700got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800701 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700702 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700703 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900704 return page;
705}
706
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900707static int __allocate_data_block(struct dnode_of_data *dn)
708{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700709 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900710 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900711 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700712 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800713 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900714
Jaegeuk Kim91942322016-05-20 10:13:22 -0700715 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900716 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800717
718 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
719 if (dn->data_blkaddr == NEW_ADDR)
720 goto alloc;
721
Chao Yu46008c62016-05-09 19:56:30 +0800722 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900723 return -ENOSPC;
724
Chao Yudf6136e2015-03-23 10:33:37 +0800725alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900726 get_node_info(sbi, dn->nid, &ni);
727 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
728
Chao Yudf6136e2015-03-23 10:33:37 +0800729 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -0800730 &sum, CURSEG_WARM_DATA);
Chao Yu216a6202015-03-19 19:23:32 +0800731 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900732
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700733 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800734 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700735 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300736 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700737 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300738 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900739 return 0;
740}
741
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800742static inline bool __force_buffered_io(struct inode *inode, int rw)
743{
744 return ((f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) ||
745 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
746 F2FS_I_SB(inode)->s_ndevs);
747}
748
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800749int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800750{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800751 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800752 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800753 int err = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800754
Jaegeuk Kimdc91de72017-01-13 13:12:29 -0800755 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
756 return 0;
757
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700758 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800759 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
760 if (map.m_len > map.m_lblk)
761 map.m_len -= map.m_lblk;
762 else
763 map.m_len = 0;
764
Chao Yuda859852016-01-26 15:42:58 +0800765 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800766
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800767 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800768 err = f2fs_convert_inline_inode(inode);
769 if (err)
770 return err;
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800771 return f2fs_map_blocks(inode, &map, 1,
772 __force_buffered_io(inode, WRITE) ?
773 F2FS_GET_BLOCK_PRE_AIO :
774 F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800775 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800776 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800777 err = f2fs_convert_inline_inode(inode);
778 if (err)
779 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800780 }
781 if (!f2fs_has_inline_data(inode))
782 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800783 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800784}
785
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900786/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700787 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
788 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800789 * If original data blocks are allocated, then give them to blockdev.
790 * Otherwise,
791 * a. preallocate requested block addresses
792 * b. do not use extent cache for better performance
793 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900794 */
Chao Yud323d002015-10-27 09:53:45 +0800795int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800796 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900797{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700798 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900799 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800800 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700801 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800802 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900803 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800804 unsigned int ofs_in_node, last_ofs_in_node;
805 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800806 struct extent_info ei;
Fan Li7df3a432015-12-17 13:20:59 +0800807 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900808
Chao Yudfd02e42016-08-20 15:12:01 +0800809 if (!maxblocks)
810 return 0;
811
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700812 map->m_len = 0;
813 map->m_flags = 0;
814
815 /* it only supports block size == page size */
816 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800817 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900818
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800819 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700820 map->m_pblk = ei.blk + pgofs - ei.fofs;
821 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
822 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900823 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800824 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900825
Chao Yu4fe71e82016-01-26 15:37:38 +0800826next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800827 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800828 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900829
830 /* When reading holes, we need its node page */
831 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900832 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900833 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800834 if (flag == F2FS_GET_BLOCK_BMAP)
835 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800836 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900837 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800838 if (map->m_next_pgofs)
839 *map->m_next_pgofs =
840 get_next_page_offset(&dn, pgofs);
841 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900842 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900843 }
Chao Yu973163f2015-09-18 16:51:51 +0800844
Chao Yu46008c62016-05-09 19:56:30 +0800845 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +0100846 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800847 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900848
Chao Yu4fe71e82016-01-26 15:37:38 +0800849next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800850 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800851
Fan Li7df3a432015-12-17 13:20:59 +0800852 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
853 if (create) {
854 if (unlikely(f2fs_cp_error(sbi))) {
855 err = -EIO;
856 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800857 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800858 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800859 if (blkaddr == NULL_ADDR) {
860 prealloc++;
861 last_ofs_in_node = dn.ofs_in_node;
862 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800863 } else {
864 err = __allocate_data_block(&dn);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800865 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700866 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800867 }
Fan Li7df3a432015-12-17 13:20:59 +0800868 if (err)
869 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800870 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800871 blkaddr = dn.data_blkaddr;
872 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800873 if (flag == F2FS_GET_BLOCK_BMAP) {
874 map->m_pblk = 0;
875 goto sync_out;
876 }
Chao Yuda859852016-01-26 15:42:58 +0800877 if (flag == F2FS_GET_BLOCK_FIEMAP &&
878 blkaddr == NULL_ADDR) {
879 if (map->m_next_pgofs)
880 *map->m_next_pgofs = pgofs + 1;
881 }
Fan Li7df3a432015-12-17 13:20:59 +0800882 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800883 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800884 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900885 }
886 }
Fan Li7df3a432015-12-17 13:20:59 +0800887
Chao Yu46008c62016-05-09 19:56:30 +0800888 if (flag == F2FS_GET_BLOCK_PRE_AIO)
889 goto skip;
890
Chao Yu4fe71e82016-01-26 15:37:38 +0800891 if (map->m_len == 0) {
892 /* preallocated unwritten block should be mapped for fiemap. */
893 if (blkaddr == NEW_ADDR)
894 map->m_flags |= F2FS_MAP_UNWRITTEN;
895 map->m_flags |= F2FS_MAP_MAPPED;
896
897 map->m_pblk = blkaddr;
898 map->m_len = 1;
899 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800900 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800901 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800902 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800903 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800904 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800905 } else {
906 goto sync_out;
907 }
908
Chao Yu46008c62016-05-09 19:56:30 +0800909skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800910 dn.ofs_in_node++;
911 pgofs++;
912
Chao Yu46008c62016-05-09 19:56:30 +0800913 /* preallocate blocks in batch for one dnode page */
914 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
915 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800916
Chao Yu46008c62016-05-09 19:56:30 +0800917 dn.ofs_in_node = ofs_in_node;
918 err = reserve_new_blocks(&dn, prealloc);
919 if (err)
920 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800921
Chao Yu46008c62016-05-09 19:56:30 +0800922 map->m_len += dn.ofs_in_node - ofs_in_node;
923 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
924 err = -ENOSPC;
925 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800926 }
Chao Yu46008c62016-05-09 19:56:30 +0800927 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800928 }
929
Chao Yu46008c62016-05-09 19:56:30 +0800930 if (pgofs >= end)
931 goto sync_out;
932 else if (dn.ofs_in_node < end_offset)
933 goto next_block;
934
Chao Yu46008c62016-05-09 19:56:30 +0800935 f2fs_put_dnode(&dn);
936
937 if (create) {
938 f2fs_unlock_op(sbi);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800939 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +0800940 }
Chao Yu46008c62016-05-09 19:56:30 +0800941 goto next_dnode;
942
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900943sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900944 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900945unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800946 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800947 f2fs_unlock_op(sbi);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800948 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800949 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900950out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700951 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900952 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900953}
954
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700955static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800956 struct buffer_head *bh, int create, int flag,
957 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700958{
959 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800960 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700961
962 map.m_lblk = iblock;
963 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800964 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700965
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800966 err = f2fs_map_blocks(inode, &map, create, flag);
967 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700968 map_bh(bh, inode->i_sb, map.m_pblk);
969 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +0800970 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700971 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800972 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700973}
974
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900975static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800976 struct buffer_head *bh_result, int create, int flag,
977 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900978{
Chao Yuda859852016-01-26 15:42:58 +0800979 return __get_data_block(inode, iblock, bh_result, create,
980 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900981}
982
Chao Yue2b4e2b2015-08-19 19:11:19 +0800983static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900984 struct buffer_head *bh_result, int create)
985{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800986 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800987 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800988}
989
990static int get_data_block_bmap(struct inode *inode, sector_t iblock,
991 struct buffer_head *bh_result, int create)
992{
Yunlei He179448b2015-12-28 21:48:32 +0800993 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800994 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800995 return -EFBIG;
996
Chao Yue2b4e2b2015-08-19 19:11:19 +0800997 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800998 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900999}
1000
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001001static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1002{
1003 return (offset >> inode->i_blkbits);
1004}
1005
1006static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1007{
1008 return (blk << inode->i_blkbits);
1009}
1010
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001011int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1012 u64 start, u64 len)
1013{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001014 struct buffer_head map_bh;
1015 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001016 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001017 u64 logical = 0, phys = 0, size = 0;
1018 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001019 int ret = 0;
1020
1021 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1022 if (ret)
1023 return ret;
1024
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001025 if (f2fs_has_inline_data(inode)) {
1026 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1027 if (ret != -EAGAIN)
1028 return ret;
1029 }
1030
Al Viro59551022016-01-22 15:40:57 -05001031 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +08001032
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001033 if (logical_to_blk(inode, len) == 0)
1034 len = blk_to_logical(inode, 1);
1035
1036 start_blk = logical_to_blk(inode, start);
1037 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001038
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001039next:
1040 memset(&map_bh, 0, sizeof(struct buffer_head));
1041 map_bh.b_size = len;
1042
Chao Yue2b4e2b2015-08-19 19:11:19 +08001043 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001044 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001045 if (ret)
1046 goto out;
1047
1048 /* HOLE */
1049 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001050 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001051
1052 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1053 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001054 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001055
Fan Li9a950d52015-12-26 18:07:41 +08001056 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001057 }
Fan Li9a950d52015-12-26 18:07:41 +08001058
Chao Yuda5af122016-01-08 20:19:27 +08001059 if (size) {
1060 if (f2fs_encrypted_inode(inode))
1061 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1062
Fan Li9a950d52015-12-26 18:07:41 +08001063 ret = fiemap_fill_next_extent(fieinfo, logical,
1064 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001065 }
Fan Li9a950d52015-12-26 18:07:41 +08001066
1067 if (start_blk > last_blk || ret)
1068 goto out;
1069
1070 logical = blk_to_logical(inode, start_blk);
1071 phys = blk_to_logical(inode, map_bh.b_blocknr);
1072 size = map_bh.b_size;
1073 flags = 0;
1074 if (buffer_unwritten(&map_bh))
1075 flags = FIEMAP_EXTENT_UNWRITTEN;
1076
1077 start_blk += logical_to_blk(inode, size);
1078
1079prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001080 cond_resched();
1081 if (fatal_signal_pending(current))
1082 ret = -EINTR;
1083 else
1084 goto next;
1085out:
1086 if (ret == 1)
1087 ret = 0;
1088
Al Viro59551022016-01-22 15:40:57 -05001089 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001090 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001091}
1092
Wei Yongjun6a7a3ae2016-08-23 15:23:59 +00001093static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
1094 unsigned nr_pages)
Chao Yu78682f72016-07-03 22:05:11 +08001095{
1096 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1097 struct fscrypt_ctx *ctx = NULL;
Chao Yu78682f72016-07-03 22:05:11 +08001098 struct bio *bio;
1099
1100 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1101 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
1102 if (IS_ERR(ctx))
1103 return ERR_CAST(ctx);
1104
1105 /* wait the page to be moved by cleaning */
1106 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
1107 }
1108
1109 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
1110 if (!bio) {
1111 if (ctx)
1112 fscrypt_release_ctx(ctx);
1113 return ERR_PTR(-ENOMEM);
1114 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001115 f2fs_target_device(sbi, blkaddr, bio);
Chao Yu78682f72016-07-03 22:05:11 +08001116 bio->bi_end_io = f2fs_read_end_io;
1117 bio->bi_private = ctx;
1118
1119 return bio;
1120}
1121
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001122/*
1123 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1124 * Major change was from block_size == page_size in f2fs by default.
1125 */
1126static int f2fs_mpage_readpages(struct address_space *mapping,
1127 struct list_head *pages, struct page *page,
1128 unsigned nr_pages)
1129{
1130 struct bio *bio = NULL;
1131 unsigned page_idx;
1132 sector_t last_block_in_bio = 0;
1133 struct inode *inode = mapping->host;
1134 const unsigned blkbits = inode->i_blkbits;
1135 const unsigned blocksize = 1 << blkbits;
1136 sector_t block_in_file;
1137 sector_t last_block;
1138 sector_t last_block_in_file;
1139 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001140 struct f2fs_map_blocks map;
1141
1142 map.m_pblk = 0;
1143 map.m_lblk = 0;
1144 map.m_len = 0;
1145 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001146 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001147
1148 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1149
1150 prefetchw(&page->flags);
1151 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001152 page = list_last_entry(pages, struct page, lru);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001153 list_del(&page->lru);
1154 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001155 page->index,
1156 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001157 goto next_page;
1158 }
1159
1160 block_in_file = (sector_t)page->index;
1161 last_block = block_in_file + nr_pages;
1162 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1163 blkbits;
1164 if (last_block > last_block_in_file)
1165 last_block = last_block_in_file;
1166
1167 /*
1168 * Map blocks using the previous result first.
1169 */
1170 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1171 block_in_file > map.m_lblk &&
1172 block_in_file < (map.m_lblk + map.m_len))
1173 goto got_it;
1174
1175 /*
1176 * Then do more f2fs_map_blocks() calls until we are
1177 * done with this page.
1178 */
1179 map.m_flags = 0;
1180
1181 if (block_in_file < last_block) {
1182 map.m_lblk = block_in_file;
1183 map.m_len = last_block - block_in_file;
1184
Chao Yu46c9e142015-09-18 16:54:16 +08001185 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001186 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001187 goto set_error_page;
1188 }
1189got_it:
1190 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1191 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1192 SetPageMappedToDisk(page);
1193
1194 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1195 SetPageUptodate(page);
1196 goto confused;
1197 }
1198 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001199 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001200 if (!PageUptodate(page))
1201 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001202 unlock_page(page);
1203 goto next_page;
1204 }
1205
1206 /*
1207 * This page will go to BIO. Do we need to send this
1208 * BIO off first?
1209 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001210 if (bio && (last_block_in_bio != block_nr - 1 ||
1211 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001212submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001213 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001214 bio = NULL;
1215 }
1216 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001217 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001218 if (IS_ERR(bio)) {
1219 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001220 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001221 }
Mike Christie04d328d2016-06-05 14:31:55 -05001222 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001223 }
1224
1225 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1226 goto submit_and_realloc;
1227
1228 last_block_in_bio = block_nr;
1229 goto next_page;
1230set_error_page:
1231 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001232 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001233 unlock_page(page);
1234 goto next_page;
1235confused:
1236 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001237 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001238 bio = NULL;
1239 }
1240 unlock_page(page);
1241next_page:
1242 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001243 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001244 }
1245 BUG_ON(pages && !list_empty(pages));
1246 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001247 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001248 return 0;
1249}
1250
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001251static int f2fs_read_data_page(struct file *file, struct page *page)
1252{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001253 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001254 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001255
Chao Yuc20e89c2014-05-06 16:53:08 +08001256 trace_f2fs_readpage(page, DATA);
1257
arter97e1c42042014-08-06 23:22:50 +09001258 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001259 if (f2fs_has_inline_data(inode))
1260 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001261 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001262 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001263 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001264}
1265
1266static int f2fs_read_data_pages(struct file *file,
1267 struct address_space *mapping,
1268 struct list_head *pages, unsigned nr_pages)
1269{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001270 struct inode *inode = file->f_mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001271 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001272
1273 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001274
1275 /* If the file has inline data, skip readpages */
1276 if (f2fs_has_inline_data(inode))
1277 return 0;
1278
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001279 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001280}
1281
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001282int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001283{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001284 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001285 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001286 struct dnode_of_data dn;
1287 int err = 0;
1288
1289 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001290 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001291 if (err)
1292 return err;
1293
Chao Yu28bc1062016-02-06 14:40:34 +08001294 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001295
1296 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001297 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001298 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001299 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001300 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001301
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001302 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001303 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001304
1305 /* wait for GCed encrypted page writeback */
1306 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001307 fio->old_blkaddr);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001308retry_encrypt:
1309 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
David Gstir9c4bb8a2016-11-13 22:20:48 +01001310 PAGE_SIZE, 0,
1311 fio->page->index,
1312 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001313 if (IS_ERR(fio->encrypted_page)) {
1314 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001315 if (err == -ENOMEM) {
1316 /* flush pending ios and wait for a while */
1317 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1318 congestion_wait(BLK_RW_ASYNC, HZ/50);
1319 gfp_flags |= __GFP_NOFAIL;
1320 err = 0;
1321 goto retry_encrypt;
1322 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001323 goto out_writepage;
1324 }
1325 }
1326
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001327 set_page_writeback(page);
1328
1329 /*
1330 * If current allocation needs SSR,
1331 * it had better in-place writes for updated data.
1332 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001333 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001334 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001335 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001336 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001337 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001338 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001339 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001340 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001341 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001342 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001343 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001344 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001345 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001346 }
1347out_writepage:
1348 f2fs_put_dnode(&dn);
1349 return err;
1350}
1351
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001352static int __write_data_page(struct page *page, bool *submitted,
1353 struct writeback_control *wbc)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001354{
1355 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001356 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001357 loff_t i_size = i_size_read(inode);
1358 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001359 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001360 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001361 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001362 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001363 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001364 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001365 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001366 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001367 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001368 .op_flags = wbc_to_write_flags(wbc),
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001369 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001370 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001371 .submitted = false,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001372 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001373
Chao Yuecda0de2014-05-06 16:48:26 +08001374 trace_f2fs_writepage(page, DATA);
1375
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001376 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001377 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001378
1379 /*
1380 * If the offset is out-of-range of file size,
1381 * this page does not have to be written to disk.
1382 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001383 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001384 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001385 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001386
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001387 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001388write:
Chao Yucaf00472015-01-28 17:48:42 +08001389 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001390 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001391 if (f2fs_is_drop_cache(inode))
1392 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001393 /* we should not write 0'th page having journal header */
1394 if (f2fs_is_volatile_file(inode) && (!page->index ||
1395 (!wbc->for_reclaim &&
1396 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001397 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001398
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001399 /* we should bypass data pages to proceed the kworkder jobs */
1400 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001401 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001402 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001403 }
1404
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001405 /* Dentry blocks are controlled by checkpoint */
1406 if (S_ISDIR(inode->i_mode)) {
1407 err = do_write_data_page(&fio);
1408 goto done;
1409 }
1410
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001411 if (!wbc->for_reclaim)
1412 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001413 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001414 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001415
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001416 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001417 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001418 if (f2fs_has_inline_data(inode))
1419 err = f2fs_write_inline_data(inode, page);
1420 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001421 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001422 if (F2FS_I(inode)->last_disk_size < psize)
1423 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001424 f2fs_unlock_op(sbi);
1425done:
1426 if (err && err != -ENOENT)
1427 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001428
Jaegeuk Kim39936832012-11-22 16:21:29 +09001429out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001430 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001431 if (err)
1432 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001433
1434 if (wbc->for_reclaim) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001435 f2fs_submit_merged_bio_cond(sbi, inode, 0, page->index,
1436 DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001437 remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001438 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001439 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001440
1441 unlock_page(page);
1442 f2fs_balance_fs(sbi, need_balance_fs);
1443
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001444 if (unlikely(f2fs_cp_error(sbi))) {
Chao Yu0c3a5792016-01-18 18:28:11 +08001445 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001446 submitted = NULL;
1447 }
1448
1449 if (submitted)
1450 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001451
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001452 return 0;
1453
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001454redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001455 redirty_page_for_writepage(wbc, page);
Chao Yu0002b612016-11-28 19:13:43 -08001456 if (!err)
1457 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001458 unlock_page(page);
1459 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001460}
1461
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001462static int f2fs_write_data_page(struct page *page,
1463 struct writeback_control *wbc)
1464{
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001465 return __write_data_page(page, NULL, wbc);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001466}
1467
Chao Yu8f46dca2015-07-14 18:56:10 +08001468/*
1469 * This function was copied from write_cche_pages from mm/page-writeback.c.
1470 * The major change is making write step of cold data page separately from
1471 * warm/hot data page.
1472 */
1473static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001474 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001475{
1476 int ret = 0;
1477 int done = 0;
1478 struct pagevec pvec;
1479 int nr_pages;
1480 pgoff_t uninitialized_var(writeback_index);
1481 pgoff_t index;
1482 pgoff_t end; /* Inclusive */
1483 pgoff_t done_index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001484 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001485 int cycled;
1486 int range_whole = 0;
1487 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001488
1489 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001490
Chao Yu8f46dca2015-07-14 18:56:10 +08001491 if (wbc->range_cyclic) {
1492 writeback_index = mapping->writeback_index; /* prev offset */
1493 index = writeback_index;
1494 if (index == 0)
1495 cycled = 1;
1496 else
1497 cycled = 0;
1498 end = -1;
1499 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001500 index = wbc->range_start >> PAGE_SHIFT;
1501 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001502 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1503 range_whole = 1;
1504 cycled = 1; /* ignore range_cyclic tests */
1505 }
1506 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1507 tag = PAGECACHE_TAG_TOWRITE;
1508 else
1509 tag = PAGECACHE_TAG_DIRTY;
1510retry:
1511 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1512 tag_pages_for_writeback(mapping, index, end);
1513 done_index = index;
1514 while (!done && (index <= end)) {
1515 int i;
1516
1517 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1518 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1519 if (nr_pages == 0)
1520 break;
1521
1522 for (i = 0; i < nr_pages; i++) {
1523 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001524 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001525
1526 if (page->index > end) {
1527 done = 1;
1528 break;
1529 }
1530
1531 done_index = page->index;
1532
1533 lock_page(page);
1534
1535 if (unlikely(page->mapping != mapping)) {
1536continue_unlock:
1537 unlock_page(page);
1538 continue;
1539 }
1540
1541 if (!PageDirty(page)) {
1542 /* someone wrote it for us */
1543 goto continue_unlock;
1544 }
1545
Chao Yu8f46dca2015-07-14 18:56:10 +08001546 if (PageWriteback(page)) {
1547 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001548 f2fs_wait_on_page_writeback(page,
1549 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001550 else
1551 goto continue_unlock;
1552 }
1553
1554 BUG_ON(PageWriteback(page));
1555 if (!clear_page_dirty_for_io(page))
1556 goto continue_unlock;
1557
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001558 ret = __write_data_page(page, &submitted, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001559 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08001560 /*
1561 * keep nr_to_write, since vfs uses this to
1562 * get # of written pages.
1563 */
1564 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1565 unlock_page(page);
1566 ret = 0;
1567 continue;
1568 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001569 done_index = page->index + 1;
1570 done = 1;
1571 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001572 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001573 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08001574 }
1575
1576 if (--wbc->nr_to_write <= 0 &&
1577 wbc->sync_mode == WB_SYNC_NONE) {
1578 done = 1;
1579 break;
1580 }
1581 }
1582 pagevec_release(&pvec);
1583 cond_resched();
1584 }
1585
Chao Yu8f46dca2015-07-14 18:56:10 +08001586 if (!cycled && !done) {
1587 cycled = 1;
1588 index = 0;
1589 end = writeback_index - 1;
1590 goto retry;
1591 }
1592 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1593 mapping->writeback_index = done_index;
1594
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001595 if (last_idx != ULONG_MAX)
Chao Yu6ca56ca2016-09-29 18:50:11 +08001596 f2fs_submit_merged_bio_cond(F2FS_M_SB(mapping), mapping->host,
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001597 0, last_idx, DATA, WRITE);
Chao Yu6ca56ca2016-09-29 18:50:11 +08001598
Chao Yu8f46dca2015-07-14 18:56:10 +08001599 return ret;
1600}
1601
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001602static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001603 struct writeback_control *wbc)
1604{
1605 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001606 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001607 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001608 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001609
P J Pcfb185a2013-04-03 11:38:00 +09001610 /* deal with chardevs and other special file */
1611 if (!mapping->a_ops->writepage)
1612 return 0;
1613
Chao Yu6a290542015-07-17 18:02:39 +08001614 /* skip writing if there is no dirty page in this inode */
1615 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1616 return 0;
1617
Jaegeuk Kima1257022015-10-08 10:40:07 -07001618 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1619 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1620 available_free_memory(sbi, DIRTY_DENTS))
1621 goto skip_write;
1622
Chao Yud323d002015-10-27 09:53:45 +08001623 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001624 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001625 goto skip_write;
1626
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001627 /* during POR, we don't need to trigger writepage at all. */
1628 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1629 goto skip_write;
1630
Yunlei Hed31c7c32016-02-04 16:14:00 +08001631 trace_f2fs_writepages(mapping->host, wbc, DATA);
1632
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001633 blk_start_plug(&plug);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001634 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001635 blk_finish_plug(&plug);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001636 /*
1637 * if some pages were truncated, we cannot guarantee its mapping->host
1638 * to detect pending bios.
1639 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001640
Chao Yuc227f912015-12-16 13:09:20 +08001641 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001642 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001643
1644skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001645 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001646 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001647 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001648}
1649
Chao Yu3aab8f82014-07-02 13:25:04 +08001650static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1651{
1652 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001653 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001654
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001655 if (to > i_size) {
1656 truncate_pagecache(inode, i_size);
1657 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001658 }
1659}
1660
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001661static int prepare_write_begin(struct f2fs_sb_info *sbi,
1662 struct page *page, loff_t pos, unsigned len,
1663 block_t *blk_addr, bool *node_changed)
1664{
1665 struct inode *inode = page->mapping->host;
1666 pgoff_t index = page->index;
1667 struct dnode_of_data dn;
1668 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001669 bool locked = false;
1670 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001671 int err = 0;
1672
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001673 /*
1674 * we already allocated all the blocks, so we don't need to get
1675 * the block addresses when there is no need to fill the page.
1676 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08001677 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
1678 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001679 return 0;
1680
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001681 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001682 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001683 f2fs_lock_op(sbi);
1684 locked = true;
1685 }
1686restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001687 /* check inline_data */
1688 ipage = get_node_page(sbi, inode->i_ino);
1689 if (IS_ERR(ipage)) {
1690 err = PTR_ERR(ipage);
1691 goto unlock_out;
1692 }
1693
1694 set_new_dnode(&dn, inode, ipage, ipage, 0);
1695
1696 if (f2fs_has_inline_data(inode)) {
1697 if (pos + len <= MAX_INLINE_DATA) {
1698 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001699 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001700 if (inode->i_nlink)
1701 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001702 } else {
1703 err = f2fs_convert_inline_page(&dn, page);
1704 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001705 goto out;
1706 if (dn.data_blkaddr == NULL_ADDR)
1707 err = f2fs_get_block(&dn, index);
1708 }
1709 } else if (locked) {
1710 err = f2fs_get_block(&dn, index);
1711 } else {
1712 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1713 dn.data_blkaddr = ei.blk + index - ei.fofs;
1714 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001715 /* hole case */
1716 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001717 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001718 f2fs_put_dnode(&dn);
1719 f2fs_lock_op(sbi);
1720 locked = true;
1721 goto restart;
1722 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001723 }
1724 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001725
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001726 /* convert_inline_page can make node_changed */
1727 *blk_addr = dn.data_blkaddr;
1728 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001729out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001730 f2fs_put_dnode(&dn);
1731unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001732 if (locked)
1733 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001734 return err;
1735}
1736
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001737static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1738 loff_t pos, unsigned len, unsigned flags,
1739 struct page **pagep, void **fsdata)
1740{
1741 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001742 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001743 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001744 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001745 bool need_balance = false;
1746 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001747 int err = 0;
1748
Chao Yu62aed042014-05-06 16:46:04 +08001749 trace_f2fs_write_begin(inode, pos, len, flags);
1750
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001751 /*
1752 * We should check this at this moment to avoid deadlock on inode page
1753 * and #0 page. The locking rule for inline_data conversion should be:
1754 * lock_page(page #0) -> lock_page(inode_page)
1755 */
1756 if (index != 0) {
1757 err = f2fs_convert_inline_inode(inode);
1758 if (err)
1759 goto fail;
1760 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001761repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08001762 /*
1763 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1764 * wait_for_stable_page. Will wait that below with our IO control.
1765 */
1766 page = pagecache_get_page(mapping, index,
1767 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08001768 if (!page) {
1769 err = -ENOMEM;
1770 goto fail;
1771 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001772
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001773 *pagep = page;
1774
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001775 err = prepare_write_begin(sbi, page, pos, len,
1776 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001777 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001778 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001779
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001780 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001781 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001782 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001783 lock_page(page);
1784 if (page->mapping != mapping) {
1785 /* The page got truncated from under us */
1786 f2fs_put_page(page, 1);
1787 goto repeat;
1788 }
1789 }
1790
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001791 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001792
Chao Yu08b39fb2015-10-08 13:27:34 +08001793 /* wait for GCed encrypted page writeback */
1794 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001795 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001796
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001797 if (len == PAGE_SIZE || PageUptodate(page))
1798 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001799
Yunlei He746e2402016-12-20 11:11:35 +08001800 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
1801 zero_user_segment(page, len, PAGE_SIZE);
1802 return 0;
1803 }
1804
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001805 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001806 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001807 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001808 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001809 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001810
Chao Yu78682f72016-07-03 22:05:11 +08001811 bio = f2fs_grab_bio(inode, blkaddr, 1);
1812 if (IS_ERR(bio)) {
1813 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001814 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001815 }
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001816 bio->bi_opf = REQ_OP_READ;
Chao Yu78682f72016-07-03 22:05:11 +08001817 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1818 bio_put(bio);
1819 err = -EFAULT;
1820 goto fail;
1821 }
1822
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001823 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001824
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001825 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001826 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001827 f2fs_put_page(page, 1);
1828 goto repeat;
1829 }
Chao Yu1563ac72016-07-03 22:05:12 +08001830 if (unlikely(!PageUptodate(page))) {
1831 err = -EIO;
1832 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001833 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001834 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001835 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001836
Chao Yu3aab8f82014-07-02 13:25:04 +08001837fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001838 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001839 f2fs_write_failed(mapping, pos + len);
1840 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001841}
1842
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001843static int f2fs_write_end(struct file *file,
1844 struct address_space *mapping,
1845 loff_t pos, unsigned len, unsigned copied,
1846 struct page *page, void *fsdata)
1847{
1848 struct inode *inode = page->mapping->host;
1849
Chao Yudfb2bf32014-05-06 16:47:23 +08001850 trace_f2fs_write_end(inode, pos, len, copied);
1851
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001852 /*
1853 * This should be come from len == PAGE_SIZE, and we expect copied
1854 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
1855 * let generic_perform_write() try to copy data again through copied=0.
1856 */
1857 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08001858 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001859 copied = 0;
1860 else
1861 SetPageUptodate(page);
1862 }
1863 if (!copied)
1864 goto unlock_out;
1865
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001866 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001867
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001868 if (pos + copied > i_size_read(inode))
1869 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001870unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08001871 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001872 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001873 return copied;
1874}
1875
Omar Sandoval6f673762015-03-16 04:33:52 -07001876static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1877 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001878{
1879 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001880
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001881 if (offset & blocksize_mask)
1882 return -EINVAL;
1883
Al Viro5b46f252014-03-16 18:07:34 -04001884 if (iov_iter_alignment(iter) & blocksize_mask)
1885 return -EINVAL;
1886
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001887 return 0;
1888}
1889
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001890static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001891{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001892 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001893 struct inode *inode = mapping->host;
1894 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001895 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08001896 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08001897 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001898
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001899 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001900 if (err)
1901 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001902
Jaegeuk Kimc040ff92016-11-11 16:46:40 -08001903 if (__force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001904 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001905
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001906 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08001907
Chao Yu82e0a5a2016-07-13 09:18:29 +08001908 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001909 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08001910 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
1911
1912 if (rw == WRITE) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001913 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001914 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001915 else if (err < 0)
1916 f2fs_write_failed(mapping, offset + count);
1917 }
Chao Yu70407fa2014-07-31 21:11:22 +08001918
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001919 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08001920
Chao Yu3aab8f82014-07-02 13:25:04 +08001921 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001922}
1923
Chao Yu487261f2015-02-05 17:44:29 +08001924void f2fs_invalidate_page(struct page *page, unsigned int offset,
1925 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001926{
1927 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001928 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001929
Chao Yu487261f2015-02-05 17:44:29 +08001930 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001931 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001932 return;
1933
Chao Yu487261f2015-02-05 17:44:29 +08001934 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08001935 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08001936 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08001937 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08001938 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08001939 } else {
Chao Yu487261f2015-02-05 17:44:29 +08001940 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08001941 remove_dirty_inode(inode);
1942 }
Chao Yu487261f2015-02-05 17:44:29 +08001943 }
Chao Yudecd36b2015-08-07 18:42:09 +08001944
1945 /* This is atomic written page, keep Private */
1946 if (IS_ATOMIC_WRITTEN_PAGE(page))
1947 return;
1948
Chao Yu23dc9742016-04-29 20:09:15 +08001949 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001950 ClearPagePrivate(page);
1951}
1952
Chao Yu487261f2015-02-05 17:44:29 +08001953int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001954{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001955 /* If this is dirty page, keep PagePrivate */
1956 if (PageDirty(page))
1957 return 0;
1958
Chao Yudecd36b2015-08-07 18:42:09 +08001959 /* This is atomic written page, keep Private */
1960 if (IS_ATOMIC_WRITTEN_PAGE(page))
1961 return 0;
1962
Chao Yu23dc9742016-04-29 20:09:15 +08001963 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001964 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001965 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001966}
1967
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001968/*
1969 * This was copied from __set_page_dirty_buffers which gives higher performance
1970 * in very high speed storages. (e.g., pmem)
1971 */
1972void f2fs_set_page_dirty_nobuffers(struct page *page)
1973{
1974 struct address_space *mapping = page->mapping;
1975 unsigned long flags;
1976
1977 if (unlikely(!mapping))
1978 return;
1979
1980 spin_lock(&mapping->private_lock);
1981 lock_page_memcg(page);
1982 SetPageDirty(page);
1983 spin_unlock(&mapping->private_lock);
1984
1985 spin_lock_irqsave(&mapping->tree_lock, flags);
1986 WARN_ON_ONCE(!PageUptodate(page));
1987 account_page_dirtied(page, mapping);
1988 radix_tree_tag_set(&mapping->page_tree,
1989 page_index(page), PAGECACHE_TAG_DIRTY);
1990 spin_unlock_irqrestore(&mapping->tree_lock, flags);
1991 unlock_page_memcg(page);
1992
1993 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1994 return;
1995}
1996
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001997static int f2fs_set_data_page_dirty(struct page *page)
1998{
1999 struct address_space *mapping = page->mapping;
2000 struct inode *inode = mapping->host;
2001
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002002 trace_f2fs_set_page_dirty(page, DATA);
2003
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002004 if (!PageUptodate(page))
2005 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002006
Chao Yu5fe45742017-01-07 18:50:26 +08002007 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002008 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2009 register_inmem_page(inode, page);
2010 return 1;
2011 }
2012 /*
2013 * Previously, this page has been registered, we just
2014 * return here.
2015 */
2016 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002017 }
2018
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002019 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002020 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002021 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002022 return 1;
2023 }
2024 return 0;
2025}
2026
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002027static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2028{
Chao Yu454ae7e2014-04-22 13:34:01 +08002029 struct inode *inode = mapping->host;
2030
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002031 if (f2fs_has_inline_data(inode))
2032 return 0;
2033
2034 /* make sure allocating whole blocks */
2035 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2036 filemap_write_and_wait(mapping);
2037
Chao Yue2b4e2b2015-08-19 19:11:19 +08002038 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002039}
2040
Weichao Guo5b7a4872016-09-20 05:03:27 +08002041#ifdef CONFIG_MIGRATION
2042#include <linux/migrate.h>
2043
2044int f2fs_migrate_page(struct address_space *mapping,
2045 struct page *newpage, struct page *page, enum migrate_mode mode)
2046{
2047 int rc, extra_count;
2048 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2049 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2050
2051 BUG_ON(PageWriteback(page));
2052
2053 /* migrating an atomic written page is safe with the inmem_lock hold */
2054 if (atomic_written && !mutex_trylock(&fi->inmem_lock))
2055 return -EAGAIN;
2056
2057 /*
2058 * A reference is expected if PagePrivate set when move mapping,
2059 * however F2FS breaks this for maintaining dirty page counts when
2060 * truncating pages. So here adjusting the 'extra_count' make it work.
2061 */
2062 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2063 rc = migrate_page_move_mapping(mapping, newpage,
2064 page, NULL, mode, extra_count);
2065 if (rc != MIGRATEPAGE_SUCCESS) {
2066 if (atomic_written)
2067 mutex_unlock(&fi->inmem_lock);
2068 return rc;
2069 }
2070
2071 if (atomic_written) {
2072 struct inmem_pages *cur;
2073 list_for_each_entry(cur, &fi->inmem_pages, list)
2074 if (cur->page == page) {
2075 cur->page = newpage;
2076 break;
2077 }
2078 mutex_unlock(&fi->inmem_lock);
2079 put_page(page);
2080 get_page(newpage);
2081 }
2082
2083 if (PagePrivate(page))
2084 SetPagePrivate(newpage);
2085 set_page_private(newpage, page_private(page));
2086
2087 migrate_page_copy(newpage, page);
2088
2089 return MIGRATEPAGE_SUCCESS;
2090}
2091#endif
2092
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002093const struct address_space_operations f2fs_dblock_aops = {
2094 .readpage = f2fs_read_data_page,
2095 .readpages = f2fs_read_data_pages,
2096 .writepage = f2fs_write_data_page,
2097 .writepages = f2fs_write_data_pages,
2098 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002099 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002100 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002101 .invalidatepage = f2fs_invalidate_page,
2102 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002103 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002104 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002105#ifdef CONFIG_MIGRATION
2106 .migratepage = f2fs_migrate_page,
2107#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002108};