blob: 3b5f1d14cab370df1362f9b302cf558d59405f67 [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
Chao Yu0c3a5792016-01-18 18:28:11 +0800246static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
247 struct page *page, nid_t ino)
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
256 if (!inode && !page && !ino)
257 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
Chao Yu0c3a5792016-01-18 18:28:11 +0800266 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800267 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800268 if (page && page == target)
269 return true;
270 if (ino && ino == ino_of_node(target))
271 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800272 }
273
Chao Yu0fd785e2016-01-18 18:24:59 +0800274 return false;
275}
276
Chao Yu0c3a5792016-01-18 18:28:11 +0800277static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
278 struct page *page, nid_t ino,
279 enum page_type type)
280{
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);
286 ret = __has_merged_page(io, inode, page, ino);
287 up_read(&io->io_rwsem);
288 return ret;
289}
290
291static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
292 struct inode *inode, struct page *page,
293 nid_t ino, 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
Chao Yu0c3a5792016-01-18 18:28:11 +0800302 if (!__has_merged_page(io, inode, page, ino))
303 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;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600309 io->fio.op_flags = REQ_PREFLUSH | REQ_META | REQ_PRIO;
310 if (!test_opt(sbi, NOBARRIER))
311 io->fio.op_flags |= 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{
321 __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw);
322}
323
324void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
325 struct inode *inode, struct page *page,
326 nid_t ino, enum page_type type, int rw)
327{
328 if (has_merged_page(sbi, inode, page, ino, type))
329 __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw);
330}
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
382 if (!is_read)
383 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
384
Chao Yudf0f8dc2014-03-22 14:57:23 +0800385 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900386
Chao Yu7a9d7542016-02-22 18:36:38 +0800387 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700388 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
389 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900390 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900391alloc_new:
392 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800393 if ((fio->type == DATA || fio->type == NODE) &&
394 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
395 err = -EAGAIN;
396 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
397 goto out_fail;
398 }
Chao Yu7a9d7542016-02-22 18:36:38 +0800399 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
Jaegeuk Kim664ba972016-10-18 11:07:45 -0700400 BIO_MAX_PAGES, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900401 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900402 }
403
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300404 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
405 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900406 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900407 goto alloc_new;
408 }
409
Chao Yu7a9d7542016-02-22 18:36:38 +0800410 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700411 f2fs_trace_ios(fio, 0);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800412out_fail:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800413 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700414 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800415 return err;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900416}
417
Chao Yu46008c62016-05-09 19:56:30 +0800418static void __set_data_blkaddr(struct dnode_of_data *dn)
419{
420 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
421 __le32 *addr_array;
422
423 /* Get physical address of data block */
424 addr_array = blkaddr_in_node(rn);
425 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
426}
427
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900428/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900429 * Lock ordering for the change of data block address:
430 * ->data_page
431 * ->node_page
432 * update block addresses in the node page
433 */
Chao Yu216a6202015-03-19 19:23:32 +0800434void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900435{
Chao Yu46008c62016-05-09 19:56:30 +0800436 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
437 __set_data_blkaddr(dn);
438 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800439 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900440}
441
Chao Yuf28b3432016-02-24 17:16:47 +0800442void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
443{
444 dn->data_blkaddr = blkaddr;
445 set_data_blkaddr(dn);
446 f2fs_update_extent_cache(dn);
447}
448
Chao Yu46008c62016-05-09 19:56:30 +0800449/* dn->ofs_in_node will be returned with up-to-date last block pointer */
450int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900451{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700452 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900453
Chao Yu46008c62016-05-09 19:56:30 +0800454 if (!count)
455 return 0;
456
Jaegeuk Kim91942322016-05-20 10:13:22 -0700457 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900458 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800459 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900460 return -ENOSPC;
461
Chao Yu46008c62016-05-09 19:56:30 +0800462 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
463 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900464
Chao Yu46008c62016-05-09 19:56:30 +0800465 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
466
467 for (; count > 0; dn->ofs_in_node++) {
468 block_t blkaddr =
469 datablock_addr(dn->node_page, dn->ofs_in_node);
470 if (blkaddr == NULL_ADDR) {
471 dn->data_blkaddr = NEW_ADDR;
472 __set_data_blkaddr(dn);
473 count--;
474 }
475 }
476
477 if (set_page_dirty(dn->node_page))
478 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900479 return 0;
480}
481
Chao Yu46008c62016-05-09 19:56:30 +0800482/* Should keep dn->ofs_in_node unchanged */
483int reserve_new_block(struct dnode_of_data *dn)
484{
485 unsigned int ofs_in_node = dn->ofs_in_node;
486 int ret;
487
488 ret = reserve_new_blocks(dn, 1);
489 dn->ofs_in_node = ofs_in_node;
490 return ret;
491}
492
Huajun Lib6009652013-11-10 23:13:18 +0800493int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
494{
495 bool need_put = dn->inode_page ? false : true;
496 int err;
497
498 err = get_dnode_of_data(dn, index, ALLOC_NODE);
499 if (err)
500 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900501
Huajun Lib6009652013-11-10 23:13:18 +0800502 if (dn->data_blkaddr == NULL_ADDR)
503 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900504 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800505 f2fs_put_dnode(dn);
506 return err;
507}
508
Fan Li759af1c2015-08-05 15:52:16 +0800509int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900510{
Chao Yu028a41e2015-03-19 19:26:02 +0800511 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800512 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800513
Fan Li759af1c2015-08-05 15:52:16 +0800514 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
515 dn->data_blkaddr = ei.blk + index - ei.fofs;
516 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800517 }
518
Fan Li759af1c2015-08-05 15:52:16 +0800519 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900520}
521
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700522struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500523 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900524{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900525 struct address_space *mapping = inode->i_mapping;
526 struct dnode_of_data dn;
527 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800528 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900529 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800530 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700531 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800532 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500533 .op = REQ_OP_READ,
534 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700535 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800536 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900537
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700538 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
539 return read_mapping_page(mapping, index, NULL);
540
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700541 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900542 if (!page)
543 return ERR_PTR(-ENOMEM);
544
Chao Yucb3bc9e2015-02-05 18:03:40 +0800545 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
546 dn.data_blkaddr = ei.blk + index - ei.fofs;
547 goto got_it;
548 }
549
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900550 set_new_dnode(&dn, inode, NULL, NULL, 0);
551 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700552 if (err)
553 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900554 f2fs_put_dnode(&dn);
555
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900556 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700557 err = -ENOENT;
558 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900559 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800560got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700561 if (PageUptodate(page)) {
562 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900563 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700564 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900565
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900566 /*
567 * A new dentry page is allocated but not able to be written, since its
568 * new inode page couldn't be allocated due to -ENOSPC.
569 * In such the case, its blkaddr can be remained as NEW_ADDR.
570 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
571 */
572 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300573 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700574 if (!PageUptodate(page))
575 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700576 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900577 return page;
578 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900579
Chao Yu7a9d7542016-02-22 18:36:38 +0800580 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700581 fio.page = page;
582 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900583 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700584 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700585 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700586
587put_err:
588 f2fs_put_page(page, 1);
589 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700590}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900591
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700592struct page *find_data_page(struct inode *inode, pgoff_t index)
593{
594 struct address_space *mapping = inode->i_mapping;
595 struct page *page;
596
597 page = find_get_page(mapping, index);
598 if (page && PageUptodate(page))
599 return page;
600 f2fs_put_page(page, 0);
601
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600602 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700603 if (IS_ERR(page))
604 return page;
605
606 if (PageUptodate(page))
607 return page;
608
609 wait_on_page_locked(page);
610 if (unlikely(!PageUptodate(page))) {
611 f2fs_put_page(page, 0);
612 return ERR_PTR(-EIO);
613 }
614 return page;
615}
616
617/*
618 * If it tries to access a hole, return an error.
619 * Because, the callers, functions in dir.c and GC, should be able to know
620 * whether this page exists or not.
621 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700622struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
623 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700624{
625 struct address_space *mapping = inode->i_mapping;
626 struct page *page;
627repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600628 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700629 if (IS_ERR(page))
630 return page;
631
632 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900633 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900634 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900635 f2fs_put_page(page, 1);
636 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900637 }
Chao Yu1563ac72016-07-03 22:05:12 +0800638 if (unlikely(!PageUptodate(page))) {
639 f2fs_put_page(page, 1);
640 return ERR_PTR(-EIO);
641 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900642 return page;
643}
644
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900645/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900646 * Caller ensures that this data page is never allocated.
647 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900648 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800649 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
650 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800651 * Note that, ipage is set only by make_empty_dir, and if any error occur,
652 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900653 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900654struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900655 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900656{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900657 struct address_space *mapping = inode->i_mapping;
658 struct page *page;
659 struct dnode_of_data dn;
660 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800661
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700662 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800663 if (!page) {
664 /*
665 * before exiting, we should make sure ipage will be released
666 * if any error occur.
667 */
668 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700669 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800670 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900671
Jaegeuk Kima8865372013-12-27 17:04:17 +0900672 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800673 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700674 if (err) {
675 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900676 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900677 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700678 if (!ipage)
679 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900680
681 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700682 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900683
684 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300685 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700686 if (!PageUptodate(page))
687 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900688 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700689 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900690
Jaegeuk Kim76121182016-01-01 22:03:47 -0800691 /* if ipage exists, blkaddr should be NEW_ADDR */
692 f2fs_bug_on(F2FS_I_SB(inode), ipage);
693 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700694 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800695 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900696 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700697got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800698 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700699 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700700 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900701 return page;
702}
703
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900704static int __allocate_data_block(struct dnode_of_data *dn)
705{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700706 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900707 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900708 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700709 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800710 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900711
Jaegeuk Kim91942322016-05-20 10:13:22 -0700712 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900713 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800714
715 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
716 if (dn->data_blkaddr == NEW_ADDR)
717 goto alloc;
718
Chao Yu46008c62016-05-09 19:56:30 +0800719 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900720 return -ENOSPC;
721
Chao Yudf6136e2015-03-23 10:33:37 +0800722alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900723 get_node_info(sbi, dn->nid, &ni);
724 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
725
Chao Yudf6136e2015-03-23 10:33:37 +0800726 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -0800727 &sum, CURSEG_WARM_DATA);
Chao Yu216a6202015-03-19 19:23:32 +0800728 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900729
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700730 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800731 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700732 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300733 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700734 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300735 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900736 return 0;
737}
738
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800739static inline bool __force_buffered_io(struct inode *inode, int rw)
740{
741 return ((f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) ||
742 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
743 F2FS_I_SB(inode)->s_ndevs);
744}
745
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800746int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800747{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800748 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800749 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800750 int err = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800751
Jaegeuk Kimdc91de72017-01-13 13:12:29 -0800752 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
753 return 0;
754
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700755 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800756 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
757 if (map.m_len > map.m_lblk)
758 map.m_len -= map.m_lblk;
759 else
760 map.m_len = 0;
761
Chao Yuda859852016-01-26 15:42:58 +0800762 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800763
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800764 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800765 err = f2fs_convert_inline_inode(inode);
766 if (err)
767 return err;
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800768 return f2fs_map_blocks(inode, &map, 1,
769 __force_buffered_io(inode, WRITE) ?
770 F2FS_GET_BLOCK_PRE_AIO :
771 F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800772 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800773 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800774 err = f2fs_convert_inline_inode(inode);
775 if (err)
776 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800777 }
778 if (!f2fs_has_inline_data(inode))
779 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800780 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800781}
782
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900783/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700784 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
785 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800786 * If original data blocks are allocated, then give them to blockdev.
787 * Otherwise,
788 * a. preallocate requested block addresses
789 * b. do not use extent cache for better performance
790 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900791 */
Chao Yud323d002015-10-27 09:53:45 +0800792int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800793 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900794{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700795 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900796 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800797 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700798 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800799 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900800 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800801 unsigned int ofs_in_node, last_ofs_in_node;
802 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800803 struct extent_info ei;
Fan Li7df3a432015-12-17 13:20:59 +0800804 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900805
Chao Yudfd02e42016-08-20 15:12:01 +0800806 if (!maxblocks)
807 return 0;
808
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700809 map->m_len = 0;
810 map->m_flags = 0;
811
812 /* it only supports block size == page size */
813 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800814 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900815
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800816 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700817 map->m_pblk = ei.blk + pgofs - ei.fofs;
818 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
819 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900820 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800821 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900822
Chao Yu4fe71e82016-01-26 15:37:38 +0800823next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800824 if (create)
Chao Yu3104af352015-12-23 17:11:43 +0800825 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900826
827 /* When reading holes, we need its node page */
828 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900829 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900830 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800831 if (flag == F2FS_GET_BLOCK_BMAP)
832 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800833 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900834 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800835 if (map->m_next_pgofs)
836 *map->m_next_pgofs =
837 get_next_page_offset(&dn, pgofs);
838 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900839 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900840 }
Chao Yu973163f2015-09-18 16:51:51 +0800841
Chao Yu46008c62016-05-09 19:56:30 +0800842 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +0100843 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800844 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900845
Chao Yu4fe71e82016-01-26 15:37:38 +0800846next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800847 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800848
Fan Li7df3a432015-12-17 13:20:59 +0800849 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
850 if (create) {
851 if (unlikely(f2fs_cp_error(sbi))) {
852 err = -EIO;
853 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800854 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800855 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800856 if (blkaddr == NULL_ADDR) {
857 prealloc++;
858 last_ofs_in_node = dn.ofs_in_node;
859 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800860 } else {
861 err = __allocate_data_block(&dn);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800862 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700863 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800864 }
Fan Li7df3a432015-12-17 13:20:59 +0800865 if (err)
866 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800867 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800868 blkaddr = dn.data_blkaddr;
869 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800870 if (flag == F2FS_GET_BLOCK_BMAP) {
871 map->m_pblk = 0;
872 goto sync_out;
873 }
Chao Yuda859852016-01-26 15:42:58 +0800874 if (flag == F2FS_GET_BLOCK_FIEMAP &&
875 blkaddr == NULL_ADDR) {
876 if (map->m_next_pgofs)
877 *map->m_next_pgofs = pgofs + 1;
878 }
Fan Li7df3a432015-12-17 13:20:59 +0800879 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800880 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800881 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900882 }
883 }
Fan Li7df3a432015-12-17 13:20:59 +0800884
Chao Yu46008c62016-05-09 19:56:30 +0800885 if (flag == F2FS_GET_BLOCK_PRE_AIO)
886 goto skip;
887
Chao Yu4fe71e82016-01-26 15:37:38 +0800888 if (map->m_len == 0) {
889 /* preallocated unwritten block should be mapped for fiemap. */
890 if (blkaddr == NEW_ADDR)
891 map->m_flags |= F2FS_MAP_UNWRITTEN;
892 map->m_flags |= F2FS_MAP_MAPPED;
893
894 map->m_pblk = blkaddr;
895 map->m_len = 1;
896 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800897 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800898 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800899 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800900 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800901 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800902 } else {
903 goto sync_out;
904 }
905
Chao Yu46008c62016-05-09 19:56:30 +0800906skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800907 dn.ofs_in_node++;
908 pgofs++;
909
Chao Yu46008c62016-05-09 19:56:30 +0800910 /* preallocate blocks in batch for one dnode page */
911 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
912 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800913
Chao Yu46008c62016-05-09 19:56:30 +0800914 dn.ofs_in_node = ofs_in_node;
915 err = reserve_new_blocks(&dn, prealloc);
916 if (err)
917 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800918
Chao Yu46008c62016-05-09 19:56:30 +0800919 map->m_len += dn.ofs_in_node - ofs_in_node;
920 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
921 err = -ENOSPC;
922 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800923 }
Chao Yu46008c62016-05-09 19:56:30 +0800924 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800925 }
926
Chao Yu46008c62016-05-09 19:56:30 +0800927 if (pgofs >= end)
928 goto sync_out;
929 else if (dn.ofs_in_node < end_offset)
930 goto next_block;
931
Chao Yu46008c62016-05-09 19:56:30 +0800932 f2fs_put_dnode(&dn);
933
934 if (create) {
935 f2fs_unlock_op(sbi);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800936 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +0800937 }
Chao Yu46008c62016-05-09 19:56:30 +0800938 goto next_dnode;
939
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900940sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900941 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900942unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800943 if (create) {
Chao Yu3104af352015-12-23 17:11:43 +0800944 f2fs_unlock_op(sbi);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800945 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800946 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900947out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700948 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900949 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900950}
951
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700952static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800953 struct buffer_head *bh, int create, int flag,
954 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700955{
956 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800957 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700958
959 map.m_lblk = iblock;
960 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800961 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700962
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800963 err = f2fs_map_blocks(inode, &map, create, flag);
964 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700965 map_bh(bh, inode->i_sb, map.m_pblk);
966 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +0800967 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700968 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800969 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700970}
971
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900972static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800973 struct buffer_head *bh_result, int create, int flag,
974 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900975{
Chao Yuda859852016-01-26 15:42:58 +0800976 return __get_data_block(inode, iblock, bh_result, create,
977 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900978}
979
Chao Yue2b4e2b2015-08-19 19:11:19 +0800980static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900981 struct buffer_head *bh_result, int create)
982{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800983 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800984 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800985}
986
987static int get_data_block_bmap(struct inode *inode, sector_t iblock,
988 struct buffer_head *bh_result, int create)
989{
Yunlei He179448b2015-12-28 21:48:32 +0800990 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800991 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800992 return -EFBIG;
993
Chao Yue2b4e2b2015-08-19 19:11:19 +0800994 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800995 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900996}
997
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700998static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
999{
1000 return (offset >> inode->i_blkbits);
1001}
1002
1003static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1004{
1005 return (blk << inode->i_blkbits);
1006}
1007
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001008int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1009 u64 start, u64 len)
1010{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001011 struct buffer_head map_bh;
1012 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001013 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001014 u64 logical = 0, phys = 0, size = 0;
1015 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001016 int ret = 0;
1017
1018 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1019 if (ret)
1020 return ret;
1021
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001022 if (f2fs_has_inline_data(inode)) {
1023 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1024 if (ret != -EAGAIN)
1025 return ret;
1026 }
1027
Al Viro59551022016-01-22 15:40:57 -05001028 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +08001029
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001030 if (logical_to_blk(inode, len) == 0)
1031 len = blk_to_logical(inode, 1);
1032
1033 start_blk = logical_to_blk(inode, start);
1034 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001035
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001036next:
1037 memset(&map_bh, 0, sizeof(struct buffer_head));
1038 map_bh.b_size = len;
1039
Chao Yue2b4e2b2015-08-19 19:11:19 +08001040 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001041 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001042 if (ret)
1043 goto out;
1044
1045 /* HOLE */
1046 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001047 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001048
1049 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1050 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001051 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001052
Fan Li9a950d52015-12-26 18:07:41 +08001053 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001054 }
Fan Li9a950d52015-12-26 18:07:41 +08001055
Chao Yuda5af122016-01-08 20:19:27 +08001056 if (size) {
1057 if (f2fs_encrypted_inode(inode))
1058 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1059
Fan Li9a950d52015-12-26 18:07:41 +08001060 ret = fiemap_fill_next_extent(fieinfo, logical,
1061 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001062 }
Fan Li9a950d52015-12-26 18:07:41 +08001063
1064 if (start_blk > last_blk || ret)
1065 goto out;
1066
1067 logical = blk_to_logical(inode, start_blk);
1068 phys = blk_to_logical(inode, map_bh.b_blocknr);
1069 size = map_bh.b_size;
1070 flags = 0;
1071 if (buffer_unwritten(&map_bh))
1072 flags = FIEMAP_EXTENT_UNWRITTEN;
1073
1074 start_blk += logical_to_blk(inode, size);
1075
1076prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001077 cond_resched();
1078 if (fatal_signal_pending(current))
1079 ret = -EINTR;
1080 else
1081 goto next;
1082out:
1083 if (ret == 1)
1084 ret = 0;
1085
Al Viro59551022016-01-22 15:40:57 -05001086 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001087 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001088}
1089
Wei Yongjun6a7a3ae2016-08-23 15:23:59 +00001090static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
1091 unsigned nr_pages)
Chao Yu78682f72016-07-03 22:05:11 +08001092{
1093 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1094 struct fscrypt_ctx *ctx = NULL;
Chao Yu78682f72016-07-03 22:05:11 +08001095 struct bio *bio;
1096
1097 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1098 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
1099 if (IS_ERR(ctx))
1100 return ERR_CAST(ctx);
1101
1102 /* wait the page to be moved by cleaning */
1103 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
1104 }
1105
1106 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
1107 if (!bio) {
1108 if (ctx)
1109 fscrypt_release_ctx(ctx);
1110 return ERR_PTR(-ENOMEM);
1111 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001112 f2fs_target_device(sbi, blkaddr, bio);
Chao Yu78682f72016-07-03 22:05:11 +08001113 bio->bi_end_io = f2fs_read_end_io;
1114 bio->bi_private = ctx;
1115
1116 return bio;
1117}
1118
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001119/*
1120 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1121 * Major change was from block_size == page_size in f2fs by default.
1122 */
1123static int f2fs_mpage_readpages(struct address_space *mapping,
1124 struct list_head *pages, struct page *page,
1125 unsigned nr_pages)
1126{
1127 struct bio *bio = NULL;
1128 unsigned page_idx;
1129 sector_t last_block_in_bio = 0;
1130 struct inode *inode = mapping->host;
1131 const unsigned blkbits = inode->i_blkbits;
1132 const unsigned blocksize = 1 << blkbits;
1133 sector_t block_in_file;
1134 sector_t last_block;
1135 sector_t last_block_in_file;
1136 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001137 struct f2fs_map_blocks map;
1138
1139 map.m_pblk = 0;
1140 map.m_lblk = 0;
1141 map.m_len = 0;
1142 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001143 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001144
1145 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1146
1147 prefetchw(&page->flags);
1148 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001149 page = list_last_entry(pages, struct page, lru);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001150 list_del(&page->lru);
1151 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001152 page->index,
1153 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001154 goto next_page;
1155 }
1156
1157 block_in_file = (sector_t)page->index;
1158 last_block = block_in_file + nr_pages;
1159 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1160 blkbits;
1161 if (last_block > last_block_in_file)
1162 last_block = last_block_in_file;
1163
1164 /*
1165 * Map blocks using the previous result first.
1166 */
1167 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1168 block_in_file > map.m_lblk &&
1169 block_in_file < (map.m_lblk + map.m_len))
1170 goto got_it;
1171
1172 /*
1173 * Then do more f2fs_map_blocks() calls until we are
1174 * done with this page.
1175 */
1176 map.m_flags = 0;
1177
1178 if (block_in_file < last_block) {
1179 map.m_lblk = block_in_file;
1180 map.m_len = last_block - block_in_file;
1181
Chao Yu46c9e142015-09-18 16:54:16 +08001182 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001183 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001184 goto set_error_page;
1185 }
1186got_it:
1187 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1188 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1189 SetPageMappedToDisk(page);
1190
1191 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1192 SetPageUptodate(page);
1193 goto confused;
1194 }
1195 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001196 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001197 if (!PageUptodate(page))
1198 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001199 unlock_page(page);
1200 goto next_page;
1201 }
1202
1203 /*
1204 * This page will go to BIO. Do we need to send this
1205 * BIO off first?
1206 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001207 if (bio && (last_block_in_bio != block_nr - 1 ||
1208 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001209submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001210 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001211 bio = NULL;
1212 }
1213 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001214 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001215 if (IS_ERR(bio)) {
1216 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001217 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001218 }
Mike Christie04d328d2016-06-05 14:31:55 -05001219 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001220 }
1221
1222 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1223 goto submit_and_realloc;
1224
1225 last_block_in_bio = block_nr;
1226 goto next_page;
1227set_error_page:
1228 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001229 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001230 unlock_page(page);
1231 goto next_page;
1232confused:
1233 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001234 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001235 bio = NULL;
1236 }
1237 unlock_page(page);
1238next_page:
1239 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001240 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001241 }
1242 BUG_ON(pages && !list_empty(pages));
1243 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001244 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001245 return 0;
1246}
1247
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001248static int f2fs_read_data_page(struct file *file, struct page *page)
1249{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001250 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001251 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001252
Chao Yuc20e89c2014-05-06 16:53:08 +08001253 trace_f2fs_readpage(page, DATA);
1254
arter97e1c42042014-08-06 23:22:50 +09001255 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001256 if (f2fs_has_inline_data(inode))
1257 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001258 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001259 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001260 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001261}
1262
1263static int f2fs_read_data_pages(struct file *file,
1264 struct address_space *mapping,
1265 struct list_head *pages, unsigned nr_pages)
1266{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001267 struct inode *inode = file->f_mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001268 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001269
1270 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001271
1272 /* If the file has inline data, skip readpages */
1273 if (f2fs_has_inline_data(inode))
1274 return 0;
1275
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001276 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001277}
1278
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001279int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001280{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001281 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001282 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001283 struct dnode_of_data dn;
1284 int err = 0;
1285
1286 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001287 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001288 if (err)
1289 return err;
1290
Chao Yu28bc1062016-02-06 14:40:34 +08001291 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001292
1293 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001294 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001295 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001296 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001297 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001298
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001299 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001300 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001301
1302 /* wait for GCed encrypted page writeback */
1303 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001304 fio->old_blkaddr);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001305retry_encrypt:
1306 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
David Gstir9c4bb8a2016-11-13 22:20:48 +01001307 PAGE_SIZE, 0,
1308 fio->page->index,
1309 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001310 if (IS_ERR(fio->encrypted_page)) {
1311 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001312 if (err == -ENOMEM) {
1313 /* flush pending ios and wait for a while */
1314 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1315 congestion_wait(BLK_RW_ASYNC, HZ/50);
1316 gfp_flags |= __GFP_NOFAIL;
1317 err = 0;
1318 goto retry_encrypt;
1319 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001320 goto out_writepage;
1321 }
1322 }
1323
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001324 set_page_writeback(page);
1325
1326 /*
1327 * If current allocation needs SSR,
1328 * it had better in-place writes for updated data.
1329 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001330 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001331 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001332 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001333 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001334 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001335 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001336 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001337 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001338 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001339 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001340 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001341 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001342 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001343 }
1344out_writepage:
1345 f2fs_put_dnode(&dn);
1346 return err;
1347}
1348
1349static int f2fs_write_data_page(struct page *page,
1350 struct writeback_control *wbc)
1351{
1352 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001353 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001354 loff_t i_size = i_size_read(inode);
1355 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001356 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001357 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001358 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001359 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001360 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001361 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001362 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001363 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001364 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001365 .op_flags = wbc_to_write_flags(wbc),
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001366 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001367 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001368 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001369
Chao Yuecda0de2014-05-06 16:48:26 +08001370 trace_f2fs_writepage(page, DATA);
1371
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001372 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001373 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001374
1375 /*
1376 * If the offset is out-of-range of file size,
1377 * this page does not have to be written to disk.
1378 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001379 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001380 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001381 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001382
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001383 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001384write:
Chao Yucaf00472015-01-28 17:48:42 +08001385 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001386 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001387 if (f2fs_is_drop_cache(inode))
1388 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001389 /* we should not write 0'th page having journal header */
1390 if (f2fs_is_volatile_file(inode) && (!page->index ||
1391 (!wbc->for_reclaim &&
1392 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001393 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001394
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001395 /* we should bypass data pages to proceed the kworkder jobs */
1396 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001397 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001398 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001399 }
1400
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001401 /* Dentry blocks are controlled by checkpoint */
1402 if (S_ISDIR(inode->i_mode)) {
1403 err = do_write_data_page(&fio);
1404 goto done;
1405 }
1406
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001407 if (!wbc->for_reclaim)
1408 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001409 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001410 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001411
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001412 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001413 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001414 if (f2fs_has_inline_data(inode))
1415 err = f2fs_write_inline_data(inode, page);
1416 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001417 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001418 if (F2FS_I(inode)->last_disk_size < psize)
1419 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001420 f2fs_unlock_op(sbi);
1421done:
1422 if (err && err != -ENOENT)
1423 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001424
Jaegeuk Kim39936832012-11-22 16:21:29 +09001425out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001426 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001427 if (err)
1428 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001429
1430 if (wbc->for_reclaim) {
1431 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001432 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001433 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001434
1435 unlock_page(page);
1436 f2fs_balance_fs(sbi, need_balance_fs);
1437
1438 if (unlikely(f2fs_cp_error(sbi)))
1439 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1440
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001441 return 0;
1442
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001443redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001444 redirty_page_for_writepage(wbc, page);
Chao Yu0002b612016-11-28 19:13:43 -08001445 if (!err)
1446 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001447 unlock_page(page);
1448 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001449}
1450
Chao Yu8f46dca2015-07-14 18:56:10 +08001451/*
1452 * This function was copied from write_cche_pages from mm/page-writeback.c.
1453 * The major change is making write step of cold data page separately from
1454 * warm/hot data page.
1455 */
1456static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001457 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001458{
1459 int ret = 0;
1460 int done = 0;
1461 struct pagevec pvec;
1462 int nr_pages;
1463 pgoff_t uninitialized_var(writeback_index);
1464 pgoff_t index;
1465 pgoff_t end; /* Inclusive */
1466 pgoff_t done_index;
1467 int cycled;
1468 int range_whole = 0;
1469 int tag;
Chao Yu6ca56ca2016-09-29 18:50:11 +08001470 int nwritten = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08001471
1472 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001473
Chao Yu8f46dca2015-07-14 18:56:10 +08001474 if (wbc->range_cyclic) {
1475 writeback_index = mapping->writeback_index; /* prev offset */
1476 index = writeback_index;
1477 if (index == 0)
1478 cycled = 1;
1479 else
1480 cycled = 0;
1481 end = -1;
1482 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001483 index = wbc->range_start >> PAGE_SHIFT;
1484 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001485 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1486 range_whole = 1;
1487 cycled = 1; /* ignore range_cyclic tests */
1488 }
1489 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1490 tag = PAGECACHE_TAG_TOWRITE;
1491 else
1492 tag = PAGECACHE_TAG_DIRTY;
1493retry:
1494 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1495 tag_pages_for_writeback(mapping, index, end);
1496 done_index = index;
1497 while (!done && (index <= end)) {
1498 int i;
1499
1500 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1501 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1502 if (nr_pages == 0)
1503 break;
1504
1505 for (i = 0; i < nr_pages; i++) {
1506 struct page *page = pvec.pages[i];
1507
1508 if (page->index > end) {
1509 done = 1;
1510 break;
1511 }
1512
1513 done_index = page->index;
1514
1515 lock_page(page);
1516
1517 if (unlikely(page->mapping != mapping)) {
1518continue_unlock:
1519 unlock_page(page);
1520 continue;
1521 }
1522
1523 if (!PageDirty(page)) {
1524 /* someone wrote it for us */
1525 goto continue_unlock;
1526 }
1527
Chao Yu8f46dca2015-07-14 18:56:10 +08001528 if (PageWriteback(page)) {
1529 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001530 f2fs_wait_on_page_writeback(page,
1531 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001532 else
1533 goto continue_unlock;
1534 }
1535
1536 BUG_ON(PageWriteback(page));
1537 if (!clear_page_dirty_for_io(page))
1538 goto continue_unlock;
1539
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001540 ret = mapping->a_ops->writepage(page, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001541 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08001542 /*
1543 * keep nr_to_write, since vfs uses this to
1544 * get # of written pages.
1545 */
1546 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1547 unlock_page(page);
1548 ret = 0;
1549 continue;
1550 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001551 done_index = page->index + 1;
1552 done = 1;
1553 break;
Chao Yu6ca56ca2016-09-29 18:50:11 +08001554 } else {
1555 nwritten++;
Chao Yu8f46dca2015-07-14 18:56:10 +08001556 }
1557
1558 if (--wbc->nr_to_write <= 0 &&
1559 wbc->sync_mode == WB_SYNC_NONE) {
1560 done = 1;
1561 break;
1562 }
1563 }
1564 pagevec_release(&pvec);
1565 cond_resched();
1566 }
1567
Chao Yu8f46dca2015-07-14 18:56:10 +08001568 if (!cycled && !done) {
1569 cycled = 1;
1570 index = 0;
1571 end = writeback_index - 1;
1572 goto retry;
1573 }
1574 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1575 mapping->writeback_index = done_index;
1576
Chao Yu6ca56ca2016-09-29 18:50:11 +08001577 if (nwritten)
1578 f2fs_submit_merged_bio_cond(F2FS_M_SB(mapping), mapping->host,
1579 NULL, 0, DATA, WRITE);
1580
Chao Yu8f46dca2015-07-14 18:56:10 +08001581 return ret;
1582}
1583
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001584static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001585 struct writeback_control *wbc)
1586{
1587 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001588 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001589 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001590 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001591
P J Pcfb185a2013-04-03 11:38:00 +09001592 /* deal with chardevs and other special file */
1593 if (!mapping->a_ops->writepage)
1594 return 0;
1595
Chao Yu6a290542015-07-17 18:02:39 +08001596 /* skip writing if there is no dirty page in this inode */
1597 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1598 return 0;
1599
Jaegeuk Kima1257022015-10-08 10:40:07 -07001600 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1601 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1602 available_free_memory(sbi, DIRTY_DENTS))
1603 goto skip_write;
1604
Chao Yud323d002015-10-27 09:53:45 +08001605 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001606 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001607 goto skip_write;
1608
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001609 /* during POR, we don't need to trigger writepage at all. */
1610 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1611 goto skip_write;
1612
Yunlei Hed31c7c32016-02-04 16:14:00 +08001613 trace_f2fs_writepages(mapping->host, wbc, DATA);
1614
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001615 blk_start_plug(&plug);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001616 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001617 blk_finish_plug(&plug);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001618 /*
1619 * if some pages were truncated, we cannot guarantee its mapping->host
1620 * to detect pending bios.
1621 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001622
Chao Yuc227f912015-12-16 13:09:20 +08001623 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001624 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001625
1626skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001627 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001628 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001629 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001630}
1631
Chao Yu3aab8f82014-07-02 13:25:04 +08001632static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1633{
1634 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001635 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001636
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001637 if (to > i_size) {
1638 truncate_pagecache(inode, i_size);
1639 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001640 }
1641}
1642
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001643static int prepare_write_begin(struct f2fs_sb_info *sbi,
1644 struct page *page, loff_t pos, unsigned len,
1645 block_t *blk_addr, bool *node_changed)
1646{
1647 struct inode *inode = page->mapping->host;
1648 pgoff_t index = page->index;
1649 struct dnode_of_data dn;
1650 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001651 bool locked = false;
1652 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001653 int err = 0;
1654
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001655 /*
1656 * we already allocated all the blocks, so we don't need to get
1657 * the block addresses when there is no need to fill the page.
1658 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08001659 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
1660 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001661 return 0;
1662
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001663 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001664 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001665 f2fs_lock_op(sbi);
1666 locked = true;
1667 }
1668restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001669 /* check inline_data */
1670 ipage = get_node_page(sbi, inode->i_ino);
1671 if (IS_ERR(ipage)) {
1672 err = PTR_ERR(ipage);
1673 goto unlock_out;
1674 }
1675
1676 set_new_dnode(&dn, inode, ipage, ipage, 0);
1677
1678 if (f2fs_has_inline_data(inode)) {
1679 if (pos + len <= MAX_INLINE_DATA) {
1680 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001681 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001682 if (inode->i_nlink)
1683 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001684 } else {
1685 err = f2fs_convert_inline_page(&dn, page);
1686 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001687 goto out;
1688 if (dn.data_blkaddr == NULL_ADDR)
1689 err = f2fs_get_block(&dn, index);
1690 }
1691 } else if (locked) {
1692 err = f2fs_get_block(&dn, index);
1693 } else {
1694 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1695 dn.data_blkaddr = ei.blk + index - ei.fofs;
1696 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001697 /* hole case */
1698 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001699 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001700 f2fs_put_dnode(&dn);
1701 f2fs_lock_op(sbi);
1702 locked = true;
1703 goto restart;
1704 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001705 }
1706 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001707
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001708 /* convert_inline_page can make node_changed */
1709 *blk_addr = dn.data_blkaddr;
1710 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001711out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001712 f2fs_put_dnode(&dn);
1713unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001714 if (locked)
1715 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001716 return err;
1717}
1718
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001719static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1720 loff_t pos, unsigned len, unsigned flags,
1721 struct page **pagep, void **fsdata)
1722{
1723 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001724 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001725 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001726 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001727 bool need_balance = false;
1728 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001729 int err = 0;
1730
Chao Yu62aed042014-05-06 16:46:04 +08001731 trace_f2fs_write_begin(inode, pos, len, flags);
1732
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001733 /*
1734 * We should check this at this moment to avoid deadlock on inode page
1735 * and #0 page. The locking rule for inline_data conversion should be:
1736 * lock_page(page #0) -> lock_page(inode_page)
1737 */
1738 if (index != 0) {
1739 err = f2fs_convert_inline_inode(inode);
1740 if (err)
1741 goto fail;
1742 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001743repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001744 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001745 if (!page) {
1746 err = -ENOMEM;
1747 goto fail;
1748 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001749
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001750 *pagep = page;
1751
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001752 err = prepare_write_begin(sbi, page, pos, len,
1753 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001754 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001755 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001756
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001757 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001758 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001759 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001760 lock_page(page);
1761 if (page->mapping != mapping) {
1762 /* The page got truncated from under us */
1763 f2fs_put_page(page, 1);
1764 goto repeat;
1765 }
1766 }
1767
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001768 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001769
Chao Yu08b39fb2015-10-08 13:27:34 +08001770 /* wait for GCed encrypted page writeback */
1771 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001772 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001773
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001774 if (len == PAGE_SIZE || PageUptodate(page))
1775 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001776
Yunlei He746e2402016-12-20 11:11:35 +08001777 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
1778 zero_user_segment(page, len, PAGE_SIZE);
1779 return 0;
1780 }
1781
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001782 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001783 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001784 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001785 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001786 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001787
Chao Yu78682f72016-07-03 22:05:11 +08001788 bio = f2fs_grab_bio(inode, blkaddr, 1);
1789 if (IS_ERR(bio)) {
1790 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001791 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001792 }
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001793 bio->bi_opf = REQ_OP_READ;
Chao Yu78682f72016-07-03 22:05:11 +08001794 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1795 bio_put(bio);
1796 err = -EFAULT;
1797 goto fail;
1798 }
1799
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001800 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001801
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001802 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001803 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001804 f2fs_put_page(page, 1);
1805 goto repeat;
1806 }
Chao Yu1563ac72016-07-03 22:05:12 +08001807 if (unlikely(!PageUptodate(page))) {
1808 err = -EIO;
1809 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001810 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001811 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001812 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001813
Chao Yu3aab8f82014-07-02 13:25:04 +08001814fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001815 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001816 f2fs_write_failed(mapping, pos + len);
1817 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001818}
1819
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001820static int f2fs_write_end(struct file *file,
1821 struct address_space *mapping,
1822 loff_t pos, unsigned len, unsigned copied,
1823 struct page *page, void *fsdata)
1824{
1825 struct inode *inode = page->mapping->host;
1826
Chao Yudfb2bf32014-05-06 16:47:23 +08001827 trace_f2fs_write_end(inode, pos, len, copied);
1828
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001829 /*
1830 * This should be come from len == PAGE_SIZE, and we expect copied
1831 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
1832 * let generic_perform_write() try to copy data again through copied=0.
1833 */
1834 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08001835 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001836 copied = 0;
1837 else
1838 SetPageUptodate(page);
1839 }
1840 if (!copied)
1841 goto unlock_out;
1842
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001843 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001844
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001845 if (pos + copied > i_size_read(inode))
1846 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001847unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08001848 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001849 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001850 return copied;
1851}
1852
Omar Sandoval6f673762015-03-16 04:33:52 -07001853static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1854 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001855{
1856 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001857
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001858 if (offset & blocksize_mask)
1859 return -EINVAL;
1860
Al Viro5b46f252014-03-16 18:07:34 -04001861 if (iov_iter_alignment(iter) & blocksize_mask)
1862 return -EINVAL;
1863
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001864 return 0;
1865}
1866
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001867static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001868{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001869 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001870 struct inode *inode = mapping->host;
1871 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001872 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08001873 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08001874 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001875
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001876 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001877 if (err)
1878 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001879
Jaegeuk Kimc040ff92016-11-11 16:46:40 -08001880 if (__force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001881 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001882
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001883 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08001884
Chao Yu82e0a5a2016-07-13 09:18:29 +08001885 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001886 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08001887 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
1888
1889 if (rw == WRITE) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001890 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001891 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001892 else if (err < 0)
1893 f2fs_write_failed(mapping, offset + count);
1894 }
Chao Yu70407fa2014-07-31 21:11:22 +08001895
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001896 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08001897
Chao Yu3aab8f82014-07-02 13:25:04 +08001898 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001899}
1900
Chao Yu487261f2015-02-05 17:44:29 +08001901void f2fs_invalidate_page(struct page *page, unsigned int offset,
1902 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001903{
1904 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001905 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001906
Chao Yu487261f2015-02-05 17:44:29 +08001907 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001908 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001909 return;
1910
Chao Yu487261f2015-02-05 17:44:29 +08001911 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08001912 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08001913 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08001914 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08001915 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08001916 } else {
Chao Yu487261f2015-02-05 17:44:29 +08001917 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08001918 remove_dirty_inode(inode);
1919 }
Chao Yu487261f2015-02-05 17:44:29 +08001920 }
Chao Yudecd36b2015-08-07 18:42:09 +08001921
1922 /* This is atomic written page, keep Private */
1923 if (IS_ATOMIC_WRITTEN_PAGE(page))
1924 return;
1925
Chao Yu23dc9742016-04-29 20:09:15 +08001926 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001927 ClearPagePrivate(page);
1928}
1929
Chao Yu487261f2015-02-05 17:44:29 +08001930int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001931{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001932 /* If this is dirty page, keep PagePrivate */
1933 if (PageDirty(page))
1934 return 0;
1935
Chao Yudecd36b2015-08-07 18:42:09 +08001936 /* This is atomic written page, keep Private */
1937 if (IS_ATOMIC_WRITTEN_PAGE(page))
1938 return 0;
1939
Chao Yu23dc9742016-04-29 20:09:15 +08001940 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001941 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001942 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001943}
1944
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001945/*
1946 * This was copied from __set_page_dirty_buffers which gives higher performance
1947 * in very high speed storages. (e.g., pmem)
1948 */
1949void f2fs_set_page_dirty_nobuffers(struct page *page)
1950{
1951 struct address_space *mapping = page->mapping;
1952 unsigned long flags;
1953
1954 if (unlikely(!mapping))
1955 return;
1956
1957 spin_lock(&mapping->private_lock);
1958 lock_page_memcg(page);
1959 SetPageDirty(page);
1960 spin_unlock(&mapping->private_lock);
1961
1962 spin_lock_irqsave(&mapping->tree_lock, flags);
1963 WARN_ON_ONCE(!PageUptodate(page));
1964 account_page_dirtied(page, mapping);
1965 radix_tree_tag_set(&mapping->page_tree,
1966 page_index(page), PAGECACHE_TAG_DIRTY);
1967 spin_unlock_irqrestore(&mapping->tree_lock, flags);
1968 unlock_page_memcg(page);
1969
1970 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1971 return;
1972}
1973
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001974static int f2fs_set_data_page_dirty(struct page *page)
1975{
1976 struct address_space *mapping = page->mapping;
1977 struct inode *inode = mapping->host;
1978
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001979 trace_f2fs_set_page_dirty(page, DATA);
1980
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001981 if (!PageUptodate(page))
1982 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001983
Chao Yu5fe45742017-01-07 18:50:26 +08001984 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001985 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1986 register_inmem_page(inode, page);
1987 return 1;
1988 }
1989 /*
1990 * Previously, this page has been registered, we just
1991 * return here.
1992 */
1993 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001994 }
1995
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001996 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001997 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001998 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001999 return 1;
2000 }
2001 return 0;
2002}
2003
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002004static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2005{
Chao Yu454ae7e2014-04-22 13:34:01 +08002006 struct inode *inode = mapping->host;
2007
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002008 if (f2fs_has_inline_data(inode))
2009 return 0;
2010
2011 /* make sure allocating whole blocks */
2012 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2013 filemap_write_and_wait(mapping);
2014
Chao Yue2b4e2b2015-08-19 19:11:19 +08002015 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002016}
2017
Weichao Guo5b7a4872016-09-20 05:03:27 +08002018#ifdef CONFIG_MIGRATION
2019#include <linux/migrate.h>
2020
2021int f2fs_migrate_page(struct address_space *mapping,
2022 struct page *newpage, struct page *page, enum migrate_mode mode)
2023{
2024 int rc, extra_count;
2025 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2026 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2027
2028 BUG_ON(PageWriteback(page));
2029
2030 /* migrating an atomic written page is safe with the inmem_lock hold */
2031 if (atomic_written && !mutex_trylock(&fi->inmem_lock))
2032 return -EAGAIN;
2033
2034 /*
2035 * A reference is expected if PagePrivate set when move mapping,
2036 * however F2FS breaks this for maintaining dirty page counts when
2037 * truncating pages. So here adjusting the 'extra_count' make it work.
2038 */
2039 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2040 rc = migrate_page_move_mapping(mapping, newpage,
2041 page, NULL, mode, extra_count);
2042 if (rc != MIGRATEPAGE_SUCCESS) {
2043 if (atomic_written)
2044 mutex_unlock(&fi->inmem_lock);
2045 return rc;
2046 }
2047
2048 if (atomic_written) {
2049 struct inmem_pages *cur;
2050 list_for_each_entry(cur, &fi->inmem_pages, list)
2051 if (cur->page == page) {
2052 cur->page = newpage;
2053 break;
2054 }
2055 mutex_unlock(&fi->inmem_lock);
2056 put_page(page);
2057 get_page(newpage);
2058 }
2059
2060 if (PagePrivate(page))
2061 SetPagePrivate(newpage);
2062 set_page_private(newpage, page_private(page));
2063
2064 migrate_page_copy(newpage, page);
2065
2066 return MIGRATEPAGE_SUCCESS;
2067}
2068#endif
2069
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002070const struct address_space_operations f2fs_dblock_aops = {
2071 .readpage = f2fs_read_data_page,
2072 .readpages = f2fs_read_data_pages,
2073 .writepage = f2fs_write_data_page,
2074 .writepages = f2fs_write_data_pages,
2075 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002076 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002077 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002078 .invalidatepage = f2fs_invalidate_page,
2079 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002080 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002081 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002082#ifdef CONFIG_MIGRATION
2083 .migratepage = f2fs_migrate_page,
2084#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002085};