blob: 9ac262564fa6b5934b8ec4987425ea3205bba1cf [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 Kim0b81d072015-05-15 16:26:10 -070096 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -070097
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020098 if (unlikely(bio->bi_error)) {
Michal Hocko5114a972016-10-11 13:56:01 -070099 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700100 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900101 }
Chao Yu36951b32016-11-16 10:41:20 +0800102 dec_page_count(sbi, type);
103 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900104 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800105 }
Chao Yu36951b32016-11-16 10:41:20 +0800106 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700107 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900108 wake_up(&sbi->cp_wait);
109
110 bio_put(bio);
111}
112
Gu Zheng940a6d32013-12-20 17:39:59 +0800113/*
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700114 * Return true, if pre_bio's bdev is same as its target device.
115 */
116struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
117 block_t blk_addr, struct bio *bio)
118{
119 struct block_device *bdev = sbi->sb->s_bdev;
120 int i;
121
122 for (i = 0; i < sbi->s_ndevs; i++) {
123 if (FDEV(i).start_blk <= blk_addr &&
124 FDEV(i).end_blk >= blk_addr) {
125 blk_addr -= FDEV(i).start_blk;
126 bdev = FDEV(i).bdev;
127 break;
128 }
129 }
130 if (bio) {
131 bio->bi_bdev = bdev;
132 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
133 }
134 return bdev;
135}
136
137int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
138{
139 int i;
140
141 for (i = 0; i < sbi->s_ndevs; i++)
142 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
143 return i;
144 return 0;
145}
146
147static bool __same_bdev(struct f2fs_sb_info *sbi,
148 block_t blk_addr, struct bio *bio)
149{
150 return f2fs_target_device(sbi, blk_addr, NULL) == bio->bi_bdev;
151}
152
153/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800154 * Low-level block read/write IO operations.
155 */
156static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
157 int npages, bool is_read)
158{
159 struct bio *bio;
160
Jaegeuk Kim740432f2015-08-14 11:43:56 -0700161 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +0800162
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700163 f2fs_target_device(sbi, blk_addr, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800164 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +0800165 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800166
167 return bio;
168}
169
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700170static inline void __submit_bio(struct f2fs_sb_info *sbi,
171 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700172{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700173 if (!is_read_io(bio_op(bio))) {
Damien Le Moal0bfd7a02016-10-28 17:45:00 +0900174 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700175 current->plug && (type == DATA || type == NODE))
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700176 blk_finish_plug(current->plug);
177 }
Mike Christie4e49ea42016-06-05 14:31:41 -0500178 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700179}
180
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900181static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900182{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900183 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900184
185 if (!io->bio)
186 return;
187
Mike Christie04d328d2016-06-05 14:31:55 -0500188 if (is_read_io(fio->op))
Chao Yu2ace38e2014-12-24 16:08:14 +0800189 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700190 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800191 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900192
Mike Christie04d328d2016-06-05 14:31:55 -0500193 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
194
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700195 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900196 io->bio = NULL;
197}
198
Chao Yu0c3a5792016-01-18 18:28:11 +0800199static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
200 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800201{
Chao Yu0fd785e2016-01-18 18:24:59 +0800202 struct bio_vec *bvec;
203 struct page *target;
204 int i;
205
Chao Yu0c3a5792016-01-18 18:28:11 +0800206 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800207 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800208
209 if (!inode && !page && !ino)
210 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800211
212 bio_for_each_segment_all(bvec, io->bio, i) {
213
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700214 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800215 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700216 else
217 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800218
Chao Yu0c3a5792016-01-18 18:28:11 +0800219 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800220 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800221 if (page && page == target)
222 return true;
223 if (ino && ino == ino_of_node(target))
224 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800225 }
226
Chao Yu0fd785e2016-01-18 18:24:59 +0800227 return false;
228}
229
Chao Yu0c3a5792016-01-18 18:28:11 +0800230static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
231 struct page *page, nid_t ino,
232 enum page_type type)
233{
234 enum page_type btype = PAGE_TYPE_OF_BIO(type);
235 struct f2fs_bio_info *io = &sbi->write_io[btype];
236 bool ret;
237
238 down_read(&io->io_rwsem);
239 ret = __has_merged_page(io, inode, page, ino);
240 up_read(&io->io_rwsem);
241 return ret;
242}
243
244static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
245 struct inode *inode, struct page *page,
246 nid_t ino, enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900247{
248 enum page_type btype = PAGE_TYPE_OF_BIO(type);
249 struct f2fs_bio_info *io;
250
251 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
252
Chao Yudf0f8dc2014-03-22 14:57:23 +0800253 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900254
Chao Yu0c3a5792016-01-18 18:28:11 +0800255 if (!__has_merged_page(io, inode, page, ino))
256 goto out;
257
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900258 /* change META to META_FLUSH in the checkpoint procedure */
259 if (type >= META_FLUSH) {
260 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500261 io->fio.op = REQ_OP_WRITE;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600262 io->fio.op_flags = REQ_PREFLUSH | REQ_META | REQ_PRIO;
263 if (!test_opt(sbi, NOBARRIER))
264 io->fio.op_flags |= REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900265 }
266 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800267out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800268 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900269}
270
Chao Yu0c3a5792016-01-18 18:28:11 +0800271void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
272 int rw)
273{
274 __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw);
275}
276
277void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
278 struct inode *inode, struct page *page,
279 nid_t ino, enum page_type type, int rw)
280{
281 if (has_merged_page(sbi, inode, page, ino, type))
282 __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw);
283}
284
Chao Yu406657d2016-02-24 17:17:55 +0800285void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
286{
287 f2fs_submit_merged_bio(sbi, DATA, WRITE);
288 f2fs_submit_merged_bio(sbi, NODE, WRITE);
289 f2fs_submit_merged_bio(sbi, META, WRITE);
290}
291
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900292/*
293 * Fill the locked page with data located in the block address.
294 * Return unlocked page.
295 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700296int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900297{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900298 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700299 struct page *page = fio->encrypted_page ?
300 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900301
Chao Yu2ace38e2014-12-24 16:08:14 +0800302 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700303 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900304
305 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500306 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900307
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300308 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900309 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900310 return -EFAULT;
311 }
Mike Christie04d328d2016-06-05 14:31:55 -0500312 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900313
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700314 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900315 return 0;
316}
317
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700318void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900319{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700320 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900321 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900322 struct f2fs_bio_info *io;
Mike Christie04d328d2016-06-05 14:31:55 -0500323 bool is_read = is_read_io(fio->op);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700324 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900325
Gu Zheng940a6d32013-12-20 17:39:59 +0800326 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900327
Chao Yu7a9d7542016-02-22 18:36:38 +0800328 if (fio->old_blkaddr != NEW_ADDR)
329 verify_block_addr(sbi, fio->old_blkaddr);
330 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900331
Chao Yu36951b32016-11-16 10:41:20 +0800332 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
333
334 if (!is_read)
335 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
336
Chao Yudf0f8dc2014-03-22 14:57:23 +0800337 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900338
Chao Yu7a9d7542016-02-22 18:36:38 +0800339 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700340 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
341 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900342 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900343alloc_new:
344 if (io->bio == NULL) {
Chao Yu7a9d7542016-02-22 18:36:38 +0800345 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
Jaegeuk Kim664ba972016-10-18 11:07:45 -0700346 BIO_MAX_PAGES, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900347 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900348 }
349
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300350 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
351 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900352 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900353 goto alloc_new;
354 }
355
Chao Yu7a9d7542016-02-22 18:36:38 +0800356 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700357 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900358
Chao Yudf0f8dc2014-03-22 14:57:23 +0800359 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700360 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900361}
362
Chao Yu46008c62016-05-09 19:56:30 +0800363static void __set_data_blkaddr(struct dnode_of_data *dn)
364{
365 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
366 __le32 *addr_array;
367
368 /* Get physical address of data block */
369 addr_array = blkaddr_in_node(rn);
370 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
371}
372
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900373/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900374 * Lock ordering for the change of data block address:
375 * ->data_page
376 * ->node_page
377 * update block addresses in the node page
378 */
Chao Yu216a6202015-03-19 19:23:32 +0800379void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900380{
Chao Yu46008c62016-05-09 19:56:30 +0800381 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
382 __set_data_blkaddr(dn);
383 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800384 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900385}
386
Chao Yuf28b3432016-02-24 17:16:47 +0800387void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
388{
389 dn->data_blkaddr = blkaddr;
390 set_data_blkaddr(dn);
391 f2fs_update_extent_cache(dn);
392}
393
Chao Yu46008c62016-05-09 19:56:30 +0800394/* dn->ofs_in_node will be returned with up-to-date last block pointer */
395int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900396{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700397 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900398
Chao Yu46008c62016-05-09 19:56:30 +0800399 if (!count)
400 return 0;
401
Jaegeuk Kim91942322016-05-20 10:13:22 -0700402 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900403 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800404 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900405 return -ENOSPC;
406
Chao Yu46008c62016-05-09 19:56:30 +0800407 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
408 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900409
Chao Yu46008c62016-05-09 19:56:30 +0800410 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
411
412 for (; count > 0; dn->ofs_in_node++) {
413 block_t blkaddr =
414 datablock_addr(dn->node_page, dn->ofs_in_node);
415 if (blkaddr == NULL_ADDR) {
416 dn->data_blkaddr = NEW_ADDR;
417 __set_data_blkaddr(dn);
418 count--;
419 }
420 }
421
422 if (set_page_dirty(dn->node_page))
423 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900424 return 0;
425}
426
Chao Yu46008c62016-05-09 19:56:30 +0800427/* Should keep dn->ofs_in_node unchanged */
428int reserve_new_block(struct dnode_of_data *dn)
429{
430 unsigned int ofs_in_node = dn->ofs_in_node;
431 int ret;
432
433 ret = reserve_new_blocks(dn, 1);
434 dn->ofs_in_node = ofs_in_node;
435 return ret;
436}
437
Huajun Lib6009652013-11-10 23:13:18 +0800438int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
439{
440 bool need_put = dn->inode_page ? false : true;
441 int err;
442
443 err = get_dnode_of_data(dn, index, ALLOC_NODE);
444 if (err)
445 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900446
Huajun Lib6009652013-11-10 23:13:18 +0800447 if (dn->data_blkaddr == NULL_ADDR)
448 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900449 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800450 f2fs_put_dnode(dn);
451 return err;
452}
453
Fan Li759af1c2015-08-05 15:52:16 +0800454int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900455{
Chao Yu028a41e2015-03-19 19:26:02 +0800456 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800457 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800458
Fan Li759af1c2015-08-05 15:52:16 +0800459 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
460 dn->data_blkaddr = ei.blk + index - ei.fofs;
461 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800462 }
463
Fan Li759af1c2015-08-05 15:52:16 +0800464 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900465}
466
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700467struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500468 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900469{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900470 struct address_space *mapping = inode->i_mapping;
471 struct dnode_of_data dn;
472 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800473 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900474 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800475 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700476 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800477 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500478 .op = REQ_OP_READ,
479 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700480 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800481 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900482
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700483 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
484 return read_mapping_page(mapping, index, NULL);
485
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700486 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900487 if (!page)
488 return ERR_PTR(-ENOMEM);
489
Chao Yucb3bc9e2015-02-05 18:03:40 +0800490 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
491 dn.data_blkaddr = ei.blk + index - ei.fofs;
492 goto got_it;
493 }
494
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900495 set_new_dnode(&dn, inode, NULL, NULL, 0);
496 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700497 if (err)
498 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900499 f2fs_put_dnode(&dn);
500
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900501 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700502 err = -ENOENT;
503 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900504 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800505got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700506 if (PageUptodate(page)) {
507 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900508 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700509 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900510
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900511 /*
512 * A new dentry page is allocated but not able to be written, since its
513 * new inode page couldn't be allocated due to -ENOSPC.
514 * In such the case, its blkaddr can be remained as NEW_ADDR.
515 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
516 */
517 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300518 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700519 if (!PageUptodate(page))
520 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700521 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900522 return page;
523 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900524
Chao Yu7a9d7542016-02-22 18:36:38 +0800525 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700526 fio.page = page;
527 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900528 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700529 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700530 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700531
532put_err:
533 f2fs_put_page(page, 1);
534 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700535}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900536
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700537struct page *find_data_page(struct inode *inode, pgoff_t index)
538{
539 struct address_space *mapping = inode->i_mapping;
540 struct page *page;
541
542 page = find_get_page(mapping, index);
543 if (page && PageUptodate(page))
544 return page;
545 f2fs_put_page(page, 0);
546
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600547 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700548 if (IS_ERR(page))
549 return page;
550
551 if (PageUptodate(page))
552 return page;
553
554 wait_on_page_locked(page);
555 if (unlikely(!PageUptodate(page))) {
556 f2fs_put_page(page, 0);
557 return ERR_PTR(-EIO);
558 }
559 return page;
560}
561
562/*
563 * If it tries to access a hole, return an error.
564 * Because, the callers, functions in dir.c and GC, should be able to know
565 * whether this page exists or not.
566 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700567struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
568 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700569{
570 struct address_space *mapping = inode->i_mapping;
571 struct page *page;
572repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600573 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700574 if (IS_ERR(page))
575 return page;
576
577 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900578 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900579 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900580 f2fs_put_page(page, 1);
581 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900582 }
Chao Yu1563ac72016-07-03 22:05:12 +0800583 if (unlikely(!PageUptodate(page))) {
584 f2fs_put_page(page, 1);
585 return ERR_PTR(-EIO);
586 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900587 return page;
588}
589
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900590/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900591 * Caller ensures that this data page is never allocated.
592 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900593 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800594 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
595 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800596 * Note that, ipage is set only by make_empty_dir, and if any error occur,
597 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900598 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900599struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900600 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900601{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900602 struct address_space *mapping = inode->i_mapping;
603 struct page *page;
604 struct dnode_of_data dn;
605 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800606
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700607 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800608 if (!page) {
609 /*
610 * before exiting, we should make sure ipage will be released
611 * if any error occur.
612 */
613 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700614 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800615 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900616
Jaegeuk Kima8865372013-12-27 17:04:17 +0900617 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800618 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700619 if (err) {
620 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900621 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900622 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700623 if (!ipage)
624 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900625
626 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700627 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900628
629 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300630 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700631 if (!PageUptodate(page))
632 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900633 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700634 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900635
Jaegeuk Kim76121182016-01-01 22:03:47 -0800636 /* if ipage exists, blkaddr should be NEW_ADDR */
637 f2fs_bug_on(F2FS_I_SB(inode), ipage);
638 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700639 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800640 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900641 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700642got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800643 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700644 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700645 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900646 return page;
647}
648
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900649static int __allocate_data_block(struct dnode_of_data *dn)
650{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700651 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900652 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900653 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700654 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800655 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900656
Jaegeuk Kim91942322016-05-20 10:13:22 -0700657 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900658 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800659
660 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
661 if (dn->data_blkaddr == NEW_ADDR)
662 goto alloc;
663
Chao Yu46008c62016-05-09 19:56:30 +0800664 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900665 return -ENOSPC;
666
Chao Yudf6136e2015-03-23 10:33:37 +0800667alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900668 get_node_info(sbi, dn->nid, &ni);
669 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
670
Chao Yudf6136e2015-03-23 10:33:37 +0800671 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Jaegeuk Kim6ae1be12016-11-11 12:31:40 -0800672 &sum, CURSEG_WARM_DATA);
Chao Yu216a6202015-03-19 19:23:32 +0800673 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900674
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700675 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800676 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700677 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300678 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700679 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300680 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900681 return 0;
682}
683
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800684static inline bool __force_buffered_io(struct inode *inode, int rw)
685{
686 return ((f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) ||
687 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
688 F2FS_I_SB(inode)->s_ndevs);
689}
690
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800691int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800692{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800693 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800694 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800695 int err = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800696
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700697 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800698 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
699 if (map.m_len > map.m_lblk)
700 map.m_len -= map.m_lblk;
701 else
702 map.m_len = 0;
703
Chao Yuda859852016-01-26 15:42:58 +0800704 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800705
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800706 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800707 err = f2fs_convert_inline_inode(inode);
708 if (err)
709 return err;
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800710 return f2fs_map_blocks(inode, &map, 1,
711 __force_buffered_io(inode, WRITE) ?
712 F2FS_GET_BLOCK_PRE_AIO :
713 F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800714 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800715 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800716 err = f2fs_convert_inline_inode(inode);
717 if (err)
718 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800719 }
720 if (!f2fs_has_inline_data(inode))
721 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800722 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800723}
724
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900725/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700726 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
727 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800728 * If original data blocks are allocated, then give them to blockdev.
729 * Otherwise,
730 * a. preallocate requested block addresses
731 * b. do not use extent cache for better performance
732 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900733 */
Chao Yud323d002015-10-27 09:53:45 +0800734int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800735 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900736{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700737 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900738 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800739 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700740 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800741 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900742 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800743 unsigned int ofs_in_node, last_ofs_in_node;
744 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800745 struct extent_info ei;
Fan Li7df3a432015-12-17 13:20:59 +0800746 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900747
Chao Yudfd02e42016-08-20 15:12:01 +0800748 if (!maxblocks)
749 return 0;
750
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700751 map->m_len = 0;
752 map->m_flags = 0;
753
754 /* it only supports block size == page size */
755 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800756 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900757
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800758 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700759 map->m_pblk = ei.blk + pgofs - ei.fofs;
760 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
761 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900762 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800763 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900764
Chao Yu4fe71e82016-01-26 15:37:38 +0800765next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800766 if (create)
Chao Yu3104af352015-12-23 17:11:43 +0800767 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900768
769 /* When reading holes, we need its node page */
770 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900771 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900772 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800773 if (flag == F2FS_GET_BLOCK_BMAP)
774 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800775 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900776 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800777 if (map->m_next_pgofs)
778 *map->m_next_pgofs =
779 get_next_page_offset(&dn, pgofs);
780 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900781 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900782 }
Chao Yu973163f2015-09-18 16:51:51 +0800783
Chao Yu46008c62016-05-09 19:56:30 +0800784 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +0100785 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800786 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900787
Chao Yu4fe71e82016-01-26 15:37:38 +0800788next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800789 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800790
Fan Li7df3a432015-12-17 13:20:59 +0800791 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
792 if (create) {
793 if (unlikely(f2fs_cp_error(sbi))) {
794 err = -EIO;
795 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800796 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800797 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800798 if (blkaddr == NULL_ADDR) {
799 prealloc++;
800 last_ofs_in_node = dn.ofs_in_node;
801 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800802 } else {
803 err = __allocate_data_block(&dn);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800804 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700805 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800806 }
Fan Li7df3a432015-12-17 13:20:59 +0800807 if (err)
808 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800809 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800810 blkaddr = dn.data_blkaddr;
811 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800812 if (flag == F2FS_GET_BLOCK_BMAP) {
813 map->m_pblk = 0;
814 goto sync_out;
815 }
Chao Yuda859852016-01-26 15:42:58 +0800816 if (flag == F2FS_GET_BLOCK_FIEMAP &&
817 blkaddr == NULL_ADDR) {
818 if (map->m_next_pgofs)
819 *map->m_next_pgofs = pgofs + 1;
820 }
Fan Li7df3a432015-12-17 13:20:59 +0800821 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800822 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800823 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900824 }
825 }
Fan Li7df3a432015-12-17 13:20:59 +0800826
Chao Yu46008c62016-05-09 19:56:30 +0800827 if (flag == F2FS_GET_BLOCK_PRE_AIO)
828 goto skip;
829
Chao Yu4fe71e82016-01-26 15:37:38 +0800830 if (map->m_len == 0) {
831 /* preallocated unwritten block should be mapped for fiemap. */
832 if (blkaddr == NEW_ADDR)
833 map->m_flags |= F2FS_MAP_UNWRITTEN;
834 map->m_flags |= F2FS_MAP_MAPPED;
835
836 map->m_pblk = blkaddr;
837 map->m_len = 1;
838 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800839 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800840 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800841 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800842 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800843 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800844 } else {
845 goto sync_out;
846 }
847
Chao Yu46008c62016-05-09 19:56:30 +0800848skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800849 dn.ofs_in_node++;
850 pgofs++;
851
Chao Yu46008c62016-05-09 19:56:30 +0800852 /* preallocate blocks in batch for one dnode page */
853 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
854 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800855
Chao Yu46008c62016-05-09 19:56:30 +0800856 dn.ofs_in_node = ofs_in_node;
857 err = reserve_new_blocks(&dn, prealloc);
858 if (err)
859 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800860
Chao Yu46008c62016-05-09 19:56:30 +0800861 map->m_len += dn.ofs_in_node - ofs_in_node;
862 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
863 err = -ENOSPC;
864 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800865 }
Chao Yu46008c62016-05-09 19:56:30 +0800866 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800867 }
868
Chao Yu46008c62016-05-09 19:56:30 +0800869 if (pgofs >= end)
870 goto sync_out;
871 else if (dn.ofs_in_node < end_offset)
872 goto next_block;
873
Chao Yu46008c62016-05-09 19:56:30 +0800874 f2fs_put_dnode(&dn);
875
876 if (create) {
877 f2fs_unlock_op(sbi);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800878 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +0800879 }
Chao Yu46008c62016-05-09 19:56:30 +0800880 goto next_dnode;
881
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900882sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900883 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900884unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800885 if (create) {
Chao Yu3104af352015-12-23 17:11:43 +0800886 f2fs_unlock_op(sbi);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800887 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800888 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900889out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700890 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900891 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900892}
893
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700894static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800895 struct buffer_head *bh, int create, int flag,
896 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700897{
898 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800899 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700900
901 map.m_lblk = iblock;
902 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800903 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700904
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800905 err = f2fs_map_blocks(inode, &map, create, flag);
906 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700907 map_bh(bh, inode->i_sb, map.m_pblk);
908 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
909 bh->b_size = map.m_len << inode->i_blkbits;
910 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800911 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700912}
913
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900914static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800915 struct buffer_head *bh_result, int create, int flag,
916 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900917{
Chao Yuda859852016-01-26 15:42:58 +0800918 return __get_data_block(inode, iblock, bh_result, create,
919 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900920}
921
Chao Yue2b4e2b2015-08-19 19:11:19 +0800922static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900923 struct buffer_head *bh_result, int create)
924{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800925 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800926 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800927}
928
929static int get_data_block_bmap(struct inode *inode, sector_t iblock,
930 struct buffer_head *bh_result, int create)
931{
Yunlei He179448b2015-12-28 21:48:32 +0800932 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800933 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800934 return -EFBIG;
935
Chao Yue2b4e2b2015-08-19 19:11:19 +0800936 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800937 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900938}
939
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700940static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
941{
942 return (offset >> inode->i_blkbits);
943}
944
945static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
946{
947 return (blk << inode->i_blkbits);
948}
949
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900950int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
951 u64 start, u64 len)
952{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700953 struct buffer_head map_bh;
954 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800955 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700956 u64 logical = 0, phys = 0, size = 0;
957 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700958 int ret = 0;
959
960 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
961 if (ret)
962 return ret;
963
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700964 if (f2fs_has_inline_data(inode)) {
965 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
966 if (ret != -EAGAIN)
967 return ret;
968 }
969
Al Viro59551022016-01-22 15:40:57 -0500970 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800971
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700972 if (logical_to_blk(inode, len) == 0)
973 len = blk_to_logical(inode, 1);
974
975 start_blk = logical_to_blk(inode, start);
976 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800977
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700978next:
979 memset(&map_bh, 0, sizeof(struct buffer_head));
980 map_bh.b_size = len;
981
Chao Yue2b4e2b2015-08-19 19:11:19 +0800982 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800983 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700984 if (ret)
985 goto out;
986
987 /* HOLE */
988 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800989 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +0800990
991 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
992 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +0800993 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +0800994
Fan Li9a950d52015-12-26 18:07:41 +0800995 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700996 }
Fan Li9a950d52015-12-26 18:07:41 +0800997
Chao Yuda5af122016-01-08 20:19:27 +0800998 if (size) {
999 if (f2fs_encrypted_inode(inode))
1000 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1001
Fan Li9a950d52015-12-26 18:07:41 +08001002 ret = fiemap_fill_next_extent(fieinfo, logical,
1003 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001004 }
Fan Li9a950d52015-12-26 18:07:41 +08001005
1006 if (start_blk > last_blk || ret)
1007 goto out;
1008
1009 logical = blk_to_logical(inode, start_blk);
1010 phys = blk_to_logical(inode, map_bh.b_blocknr);
1011 size = map_bh.b_size;
1012 flags = 0;
1013 if (buffer_unwritten(&map_bh))
1014 flags = FIEMAP_EXTENT_UNWRITTEN;
1015
1016 start_blk += logical_to_blk(inode, size);
1017
1018prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001019 cond_resched();
1020 if (fatal_signal_pending(current))
1021 ret = -EINTR;
1022 else
1023 goto next;
1024out:
1025 if (ret == 1)
1026 ret = 0;
1027
Al Viro59551022016-01-22 15:40:57 -05001028 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001029 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +09001030}
1031
Wei Yongjun6a7a3ae2016-08-23 15:23:59 +00001032static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
1033 unsigned nr_pages)
Chao Yu78682f72016-07-03 22:05:11 +08001034{
1035 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1036 struct fscrypt_ctx *ctx = NULL;
Chao Yu78682f72016-07-03 22:05:11 +08001037 struct bio *bio;
1038
1039 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1040 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
1041 if (IS_ERR(ctx))
1042 return ERR_CAST(ctx);
1043
1044 /* wait the page to be moved by cleaning */
1045 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
1046 }
1047
1048 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
1049 if (!bio) {
1050 if (ctx)
1051 fscrypt_release_ctx(ctx);
1052 return ERR_PTR(-ENOMEM);
1053 }
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001054 f2fs_target_device(sbi, blkaddr, bio);
Chao Yu78682f72016-07-03 22:05:11 +08001055 bio->bi_end_io = f2fs_read_end_io;
1056 bio->bi_private = ctx;
1057
1058 return bio;
1059}
1060
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001061/*
1062 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1063 * Major change was from block_size == page_size in f2fs by default.
1064 */
1065static int f2fs_mpage_readpages(struct address_space *mapping,
1066 struct list_head *pages, struct page *page,
1067 unsigned nr_pages)
1068{
1069 struct bio *bio = NULL;
1070 unsigned page_idx;
1071 sector_t last_block_in_bio = 0;
1072 struct inode *inode = mapping->host;
1073 const unsigned blkbits = inode->i_blkbits;
1074 const unsigned blocksize = 1 << blkbits;
1075 sector_t block_in_file;
1076 sector_t last_block;
1077 sector_t last_block_in_file;
1078 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001079 struct f2fs_map_blocks map;
1080
1081 map.m_pblk = 0;
1082 map.m_lblk = 0;
1083 map.m_len = 0;
1084 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001085 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001086
1087 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1088
1089 prefetchw(&page->flags);
1090 if (pages) {
1091 page = list_entry(pages->prev, struct page, lru);
1092 list_del(&page->lru);
1093 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001094 page->index,
1095 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001096 goto next_page;
1097 }
1098
1099 block_in_file = (sector_t)page->index;
1100 last_block = block_in_file + nr_pages;
1101 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1102 blkbits;
1103 if (last_block > last_block_in_file)
1104 last_block = last_block_in_file;
1105
1106 /*
1107 * Map blocks using the previous result first.
1108 */
1109 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1110 block_in_file > map.m_lblk &&
1111 block_in_file < (map.m_lblk + map.m_len))
1112 goto got_it;
1113
1114 /*
1115 * Then do more f2fs_map_blocks() calls until we are
1116 * done with this page.
1117 */
1118 map.m_flags = 0;
1119
1120 if (block_in_file < last_block) {
1121 map.m_lblk = block_in_file;
1122 map.m_len = last_block - block_in_file;
1123
Chao Yu46c9e142015-09-18 16:54:16 +08001124 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001125 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001126 goto set_error_page;
1127 }
1128got_it:
1129 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1130 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1131 SetPageMappedToDisk(page);
1132
1133 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1134 SetPageUptodate(page);
1135 goto confused;
1136 }
1137 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001138 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001139 if (!PageUptodate(page))
1140 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001141 unlock_page(page);
1142 goto next_page;
1143 }
1144
1145 /*
1146 * This page will go to BIO. Do we need to send this
1147 * BIO off first?
1148 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001149 if (bio && (last_block_in_bio != block_nr - 1 ||
1150 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001151submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001152 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001153 bio = NULL;
1154 }
1155 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001156 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001157 if (IS_ERR(bio)) {
1158 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001159 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001160 }
Mike Christie04d328d2016-06-05 14:31:55 -05001161 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001162 }
1163
1164 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1165 goto submit_and_realloc;
1166
1167 last_block_in_bio = block_nr;
1168 goto next_page;
1169set_error_page:
1170 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001171 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001172 unlock_page(page);
1173 goto next_page;
1174confused:
1175 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001176 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001177 bio = NULL;
1178 }
1179 unlock_page(page);
1180next_page:
1181 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001182 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001183 }
1184 BUG_ON(pages && !list_empty(pages));
1185 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001186 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001187 return 0;
1188}
1189
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001190static int f2fs_read_data_page(struct file *file, struct page *page)
1191{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001192 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001193 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001194
Chao Yuc20e89c2014-05-06 16:53:08 +08001195 trace_f2fs_readpage(page, DATA);
1196
arter97e1c42042014-08-06 23:22:50 +09001197 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001198 if (f2fs_has_inline_data(inode))
1199 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001200 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001201 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001202 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001203}
1204
1205static int f2fs_read_data_pages(struct file *file,
1206 struct address_space *mapping,
1207 struct list_head *pages, unsigned nr_pages)
1208{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001209 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001210 struct page *page = list_entry(pages->prev, struct page, lru);
1211
1212 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001213
1214 /* If the file has inline data, skip readpages */
1215 if (f2fs_has_inline_data(inode))
1216 return 0;
1217
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001218 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001219}
1220
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001221int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001222{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001223 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001224 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001225 struct dnode_of_data dn;
1226 int err = 0;
1227
1228 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001229 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001230 if (err)
1231 return err;
1232
Chao Yu28bc1062016-02-06 14:40:34 +08001233 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001234
1235 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001236 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001237 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001238 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001239 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001240
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001241 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001242 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001243
1244 /* wait for GCed encrypted page writeback */
1245 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001246 fio->old_blkaddr);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001247retry_encrypt:
1248 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
David Gstir9c4bb8a2016-11-13 22:20:48 +01001249 PAGE_SIZE, 0,
1250 fio->page->index,
1251 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001252 if (IS_ERR(fio->encrypted_page)) {
1253 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001254 if (err == -ENOMEM) {
1255 /* flush pending ios and wait for a while */
1256 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1257 congestion_wait(BLK_RW_ASYNC, HZ/50);
1258 gfp_flags |= __GFP_NOFAIL;
1259 err = 0;
1260 goto retry_encrypt;
1261 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001262 goto out_writepage;
1263 }
1264 }
1265
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001266 set_page_writeback(page);
1267
1268 /*
1269 * If current allocation needs SSR,
1270 * it had better in-place writes for updated data.
1271 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001272 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001273 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001274 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001275 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001276 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001277 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001278 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001279 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001280 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001281 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001282 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001283 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001284 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001285 }
1286out_writepage:
1287 f2fs_put_dnode(&dn);
1288 return err;
1289}
1290
1291static int f2fs_write_data_page(struct page *page,
1292 struct writeback_control *wbc)
1293{
1294 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001295 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001296 loff_t i_size = i_size_read(inode);
1297 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001298 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001299 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001300 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001301 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001302 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001303 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001304 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001305 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001306 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001307 .op_flags = wbc_to_write_flags(wbc),
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001308 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001309 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001310 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001311
Chao Yuecda0de2014-05-06 16:48:26 +08001312 trace_f2fs_writepage(page, DATA);
1313
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001314 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001315 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001316
1317 /*
1318 * If the offset is out-of-range of file size,
1319 * this page does not have to be written to disk.
1320 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001321 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001322 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001323 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001324
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001325 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001326write:
Chao Yucaf00472015-01-28 17:48:42 +08001327 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001328 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001329 if (f2fs_is_drop_cache(inode))
1330 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001331 /* we should not write 0'th page having journal header */
1332 if (f2fs_is_volatile_file(inode) && (!page->index ||
1333 (!wbc->for_reclaim &&
1334 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001335 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001336
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001337 /* we should bypass data pages to proceed the kworkder jobs */
1338 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001339 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001340 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001341 }
1342
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001343 /* Dentry blocks are controlled by checkpoint */
1344 if (S_ISDIR(inode->i_mode)) {
1345 err = do_write_data_page(&fio);
1346 goto done;
1347 }
1348
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001349 if (!wbc->for_reclaim)
1350 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001351 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001352 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001353
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001354 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001355 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001356 if (f2fs_has_inline_data(inode))
1357 err = f2fs_write_inline_data(inode, page);
1358 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001359 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001360 if (F2FS_I(inode)->last_disk_size < psize)
1361 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001362 f2fs_unlock_op(sbi);
1363done:
1364 if (err && err != -ENOENT)
1365 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001366
Jaegeuk Kim39936832012-11-22 16:21:29 +09001367out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001368 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001369 if (err)
1370 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001371
1372 if (wbc->for_reclaim) {
1373 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001374 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001375 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001376
1377 unlock_page(page);
1378 f2fs_balance_fs(sbi, need_balance_fs);
1379
1380 if (unlikely(f2fs_cp_error(sbi)))
1381 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1382
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001383 return 0;
1384
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001385redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001386 redirty_page_for_writepage(wbc, page);
Chao Yu0002b612016-11-28 19:13:43 -08001387 if (!err)
1388 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001389 unlock_page(page);
1390 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001391}
1392
Chao Yu8f46dca2015-07-14 18:56:10 +08001393/*
1394 * This function was copied from write_cche_pages from mm/page-writeback.c.
1395 * The major change is making write step of cold data page separately from
1396 * warm/hot data page.
1397 */
1398static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001399 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001400{
1401 int ret = 0;
1402 int done = 0;
1403 struct pagevec pvec;
1404 int nr_pages;
1405 pgoff_t uninitialized_var(writeback_index);
1406 pgoff_t index;
1407 pgoff_t end; /* Inclusive */
1408 pgoff_t done_index;
1409 int cycled;
1410 int range_whole = 0;
1411 int tag;
Chao Yu6ca56ca2016-09-29 18:50:11 +08001412 int nwritten = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08001413
1414 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001415
Chao Yu8f46dca2015-07-14 18:56:10 +08001416 if (wbc->range_cyclic) {
1417 writeback_index = mapping->writeback_index; /* prev offset */
1418 index = writeback_index;
1419 if (index == 0)
1420 cycled = 1;
1421 else
1422 cycled = 0;
1423 end = -1;
1424 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001425 index = wbc->range_start >> PAGE_SHIFT;
1426 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001427 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1428 range_whole = 1;
1429 cycled = 1; /* ignore range_cyclic tests */
1430 }
1431 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1432 tag = PAGECACHE_TAG_TOWRITE;
1433 else
1434 tag = PAGECACHE_TAG_DIRTY;
1435retry:
1436 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1437 tag_pages_for_writeback(mapping, index, end);
1438 done_index = index;
1439 while (!done && (index <= end)) {
1440 int i;
1441
1442 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1443 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1444 if (nr_pages == 0)
1445 break;
1446
1447 for (i = 0; i < nr_pages; i++) {
1448 struct page *page = pvec.pages[i];
1449
1450 if (page->index > end) {
1451 done = 1;
1452 break;
1453 }
1454
1455 done_index = page->index;
1456
1457 lock_page(page);
1458
1459 if (unlikely(page->mapping != mapping)) {
1460continue_unlock:
1461 unlock_page(page);
1462 continue;
1463 }
1464
1465 if (!PageDirty(page)) {
1466 /* someone wrote it for us */
1467 goto continue_unlock;
1468 }
1469
Chao Yu8f46dca2015-07-14 18:56:10 +08001470 if (PageWriteback(page)) {
1471 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001472 f2fs_wait_on_page_writeback(page,
1473 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001474 else
1475 goto continue_unlock;
1476 }
1477
1478 BUG_ON(PageWriteback(page));
1479 if (!clear_page_dirty_for_io(page))
1480 goto continue_unlock;
1481
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001482 ret = mapping->a_ops->writepage(page, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001483 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08001484 /*
1485 * keep nr_to_write, since vfs uses this to
1486 * get # of written pages.
1487 */
1488 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1489 unlock_page(page);
1490 ret = 0;
1491 continue;
1492 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001493 done_index = page->index + 1;
1494 done = 1;
1495 break;
Chao Yu6ca56ca2016-09-29 18:50:11 +08001496 } else {
1497 nwritten++;
Chao Yu8f46dca2015-07-14 18:56:10 +08001498 }
1499
1500 if (--wbc->nr_to_write <= 0 &&
1501 wbc->sync_mode == WB_SYNC_NONE) {
1502 done = 1;
1503 break;
1504 }
1505 }
1506 pagevec_release(&pvec);
1507 cond_resched();
1508 }
1509
Chao Yu8f46dca2015-07-14 18:56:10 +08001510 if (!cycled && !done) {
1511 cycled = 1;
1512 index = 0;
1513 end = writeback_index - 1;
1514 goto retry;
1515 }
1516 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1517 mapping->writeback_index = done_index;
1518
Chao Yu6ca56ca2016-09-29 18:50:11 +08001519 if (nwritten)
1520 f2fs_submit_merged_bio_cond(F2FS_M_SB(mapping), mapping->host,
1521 NULL, 0, DATA, WRITE);
1522
Chao Yu8f46dca2015-07-14 18:56:10 +08001523 return ret;
1524}
1525
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001526static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001527 struct writeback_control *wbc)
1528{
1529 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001530 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001531 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001532 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001533
P J Pcfb185a2013-04-03 11:38:00 +09001534 /* deal with chardevs and other special file */
1535 if (!mapping->a_ops->writepage)
1536 return 0;
1537
Chao Yu6a290542015-07-17 18:02:39 +08001538 /* skip writing if there is no dirty page in this inode */
1539 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1540 return 0;
1541
Jaegeuk Kima1257022015-10-08 10:40:07 -07001542 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1543 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1544 available_free_memory(sbi, DIRTY_DENTS))
1545 goto skip_write;
1546
Chao Yud323d002015-10-27 09:53:45 +08001547 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001548 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001549 goto skip_write;
1550
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001551 /* during POR, we don't need to trigger writepage at all. */
1552 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1553 goto skip_write;
1554
Yunlei Hed31c7c32016-02-04 16:14:00 +08001555 trace_f2fs_writepages(mapping->host, wbc, DATA);
1556
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001557 blk_start_plug(&plug);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001558 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001559 blk_finish_plug(&plug);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001560 /*
1561 * if some pages were truncated, we cannot guarantee its mapping->host
1562 * to detect pending bios.
1563 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001564
Chao Yuc227f912015-12-16 13:09:20 +08001565 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001566 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001567
1568skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001569 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001570 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001571 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001572}
1573
Chao Yu3aab8f82014-07-02 13:25:04 +08001574static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1575{
1576 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001577 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001578
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001579 if (to > i_size) {
1580 truncate_pagecache(inode, i_size);
1581 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001582 }
1583}
1584
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001585static int prepare_write_begin(struct f2fs_sb_info *sbi,
1586 struct page *page, loff_t pos, unsigned len,
1587 block_t *blk_addr, bool *node_changed)
1588{
1589 struct inode *inode = page->mapping->host;
1590 pgoff_t index = page->index;
1591 struct dnode_of_data dn;
1592 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001593 bool locked = false;
1594 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001595 int err = 0;
1596
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001597 /*
1598 * we already allocated all the blocks, so we don't need to get
1599 * the block addresses when there is no need to fill the page.
1600 */
Yunlei He5d4c0af2016-09-18 08:16:56 +08001601 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001602 return 0;
1603
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001604 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001605 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001606 f2fs_lock_op(sbi);
1607 locked = true;
1608 }
1609restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001610 /* check inline_data */
1611 ipage = get_node_page(sbi, inode->i_ino);
1612 if (IS_ERR(ipage)) {
1613 err = PTR_ERR(ipage);
1614 goto unlock_out;
1615 }
1616
1617 set_new_dnode(&dn, inode, ipage, ipage, 0);
1618
1619 if (f2fs_has_inline_data(inode)) {
1620 if (pos + len <= MAX_INLINE_DATA) {
1621 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001622 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001623 if (inode->i_nlink)
1624 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001625 } else {
1626 err = f2fs_convert_inline_page(&dn, page);
1627 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001628 goto out;
1629 if (dn.data_blkaddr == NULL_ADDR)
1630 err = f2fs_get_block(&dn, index);
1631 }
1632 } else if (locked) {
1633 err = f2fs_get_block(&dn, index);
1634 } else {
1635 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1636 dn.data_blkaddr = ei.blk + index - ei.fofs;
1637 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001638 /* hole case */
1639 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001640 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001641 f2fs_put_dnode(&dn);
1642 f2fs_lock_op(sbi);
1643 locked = true;
1644 goto restart;
1645 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001646 }
1647 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001648
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001649 /* convert_inline_page can make node_changed */
1650 *blk_addr = dn.data_blkaddr;
1651 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001652out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001653 f2fs_put_dnode(&dn);
1654unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001655 if (locked)
1656 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001657 return err;
1658}
1659
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001660static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1661 loff_t pos, unsigned len, unsigned flags,
1662 struct page **pagep, void **fsdata)
1663{
1664 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001665 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001666 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001667 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001668 bool need_balance = false;
1669 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001670 int err = 0;
1671
Chao Yu62aed042014-05-06 16:46:04 +08001672 trace_f2fs_write_begin(inode, pos, len, flags);
1673
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001674 /*
1675 * We should check this at this moment to avoid deadlock on inode page
1676 * and #0 page. The locking rule for inline_data conversion should be:
1677 * lock_page(page #0) -> lock_page(inode_page)
1678 */
1679 if (index != 0) {
1680 err = f2fs_convert_inline_inode(inode);
1681 if (err)
1682 goto fail;
1683 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001684repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001685 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001686 if (!page) {
1687 err = -ENOMEM;
1688 goto fail;
1689 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001690
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001691 *pagep = page;
1692
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001693 err = prepare_write_begin(sbi, page, pos, len,
1694 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001695 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001696 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001697
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001698 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001699 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001700 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001701 lock_page(page);
1702 if (page->mapping != mapping) {
1703 /* The page got truncated from under us */
1704 f2fs_put_page(page, 1);
1705 goto repeat;
1706 }
1707 }
1708
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001709 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001710
Chao Yu08b39fb2015-10-08 13:27:34 +08001711 /* wait for GCed encrypted page writeback */
1712 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001713 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001714
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001715 if (len == PAGE_SIZE || PageUptodate(page))
1716 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001717
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001718 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001719 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001720 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001721 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001722 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001723
Chao Yu78682f72016-07-03 22:05:11 +08001724 bio = f2fs_grab_bio(inode, blkaddr, 1);
1725 if (IS_ERR(bio)) {
1726 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001727 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001728 }
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001729 bio->bi_opf = REQ_OP_READ;
Chao Yu78682f72016-07-03 22:05:11 +08001730 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1731 bio_put(bio);
1732 err = -EFAULT;
1733 goto fail;
1734 }
1735
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001736 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001737
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001738 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001739 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001740 f2fs_put_page(page, 1);
1741 goto repeat;
1742 }
Chao Yu1563ac72016-07-03 22:05:12 +08001743 if (unlikely(!PageUptodate(page))) {
1744 err = -EIO;
1745 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001746 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001747 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001748 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001749
Chao Yu3aab8f82014-07-02 13:25:04 +08001750fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001751 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001752 f2fs_write_failed(mapping, pos + len);
1753 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001754}
1755
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001756static int f2fs_write_end(struct file *file,
1757 struct address_space *mapping,
1758 loff_t pos, unsigned len, unsigned copied,
1759 struct page *page, void *fsdata)
1760{
1761 struct inode *inode = page->mapping->host;
1762
Chao Yudfb2bf32014-05-06 16:47:23 +08001763 trace_f2fs_write_end(inode, pos, len, copied);
1764
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001765 /*
1766 * This should be come from len == PAGE_SIZE, and we expect copied
1767 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
1768 * let generic_perform_write() try to copy data again through copied=0.
1769 */
1770 if (!PageUptodate(page)) {
1771 if (unlikely(copied != PAGE_SIZE))
1772 copied = 0;
1773 else
1774 SetPageUptodate(page);
1775 }
1776 if (!copied)
1777 goto unlock_out;
1778
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001779 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001780
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001781 if (pos + copied > i_size_read(inode))
1782 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001783unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08001784 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001785 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001786 return copied;
1787}
1788
Omar Sandoval6f673762015-03-16 04:33:52 -07001789static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1790 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001791{
1792 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001793
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001794 if (offset & blocksize_mask)
1795 return -EINVAL;
1796
Al Viro5b46f252014-03-16 18:07:34 -04001797 if (iov_iter_alignment(iter) & blocksize_mask)
1798 return -EINVAL;
1799
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001800 return 0;
1801}
1802
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001803static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001804{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001805 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001806 struct inode *inode = mapping->host;
1807 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001808 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08001809 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08001810 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001811
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001812 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001813 if (err)
1814 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001815
Jaegeuk Kimc040ff92016-11-11 16:46:40 -08001816 if (__force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001817 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001818
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001819 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08001820
Chao Yu82e0a5a2016-07-13 09:18:29 +08001821 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001822 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08001823 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
1824
1825 if (rw == WRITE) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001826 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001827 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001828 else if (err < 0)
1829 f2fs_write_failed(mapping, offset + count);
1830 }
Chao Yu70407fa2014-07-31 21:11:22 +08001831
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001832 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08001833
Chao Yu3aab8f82014-07-02 13:25:04 +08001834 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001835}
1836
Chao Yu487261f2015-02-05 17:44:29 +08001837void f2fs_invalidate_page(struct page *page, unsigned int offset,
1838 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001839{
1840 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001841 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001842
Chao Yu487261f2015-02-05 17:44:29 +08001843 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001844 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001845 return;
1846
Chao Yu487261f2015-02-05 17:44:29 +08001847 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08001848 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08001849 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08001850 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08001851 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08001852 } else {
Chao Yu487261f2015-02-05 17:44:29 +08001853 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08001854 remove_dirty_inode(inode);
1855 }
Chao Yu487261f2015-02-05 17:44:29 +08001856 }
Chao Yudecd36b2015-08-07 18:42:09 +08001857
1858 /* This is atomic written page, keep Private */
1859 if (IS_ATOMIC_WRITTEN_PAGE(page))
1860 return;
1861
Chao Yu23dc9742016-04-29 20:09:15 +08001862 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001863 ClearPagePrivate(page);
1864}
1865
Chao Yu487261f2015-02-05 17:44:29 +08001866int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001867{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001868 /* If this is dirty page, keep PagePrivate */
1869 if (PageDirty(page))
1870 return 0;
1871
Chao Yudecd36b2015-08-07 18:42:09 +08001872 /* This is atomic written page, keep Private */
1873 if (IS_ATOMIC_WRITTEN_PAGE(page))
1874 return 0;
1875
Chao Yu23dc9742016-04-29 20:09:15 +08001876 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001877 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001878 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001879}
1880
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001881/*
1882 * This was copied from __set_page_dirty_buffers which gives higher performance
1883 * in very high speed storages. (e.g., pmem)
1884 */
1885void f2fs_set_page_dirty_nobuffers(struct page *page)
1886{
1887 struct address_space *mapping = page->mapping;
1888 unsigned long flags;
1889
1890 if (unlikely(!mapping))
1891 return;
1892
1893 spin_lock(&mapping->private_lock);
1894 lock_page_memcg(page);
1895 SetPageDirty(page);
1896 spin_unlock(&mapping->private_lock);
1897
1898 spin_lock_irqsave(&mapping->tree_lock, flags);
1899 WARN_ON_ONCE(!PageUptodate(page));
1900 account_page_dirtied(page, mapping);
1901 radix_tree_tag_set(&mapping->page_tree,
1902 page_index(page), PAGECACHE_TAG_DIRTY);
1903 spin_unlock_irqrestore(&mapping->tree_lock, flags);
1904 unlock_page_memcg(page);
1905
1906 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1907 return;
1908}
1909
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001910static int f2fs_set_data_page_dirty(struct page *page)
1911{
1912 struct address_space *mapping = page->mapping;
1913 struct inode *inode = mapping->host;
1914
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001915 trace_f2fs_set_page_dirty(page, DATA);
1916
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001917 if (!PageUptodate(page))
1918 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001919
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001920 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001921 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1922 register_inmem_page(inode, page);
1923 return 1;
1924 }
1925 /*
1926 * Previously, this page has been registered, we just
1927 * return here.
1928 */
1929 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001930 }
1931
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001932 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001933 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001934 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001935 return 1;
1936 }
1937 return 0;
1938}
1939
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001940static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1941{
Chao Yu454ae7e2014-04-22 13:34:01 +08001942 struct inode *inode = mapping->host;
1943
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001944 if (f2fs_has_inline_data(inode))
1945 return 0;
1946
1947 /* make sure allocating whole blocks */
1948 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1949 filemap_write_and_wait(mapping);
1950
Chao Yue2b4e2b2015-08-19 19:11:19 +08001951 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001952}
1953
Weichao Guo5b7a4872016-09-20 05:03:27 +08001954#ifdef CONFIG_MIGRATION
1955#include <linux/migrate.h>
1956
1957int f2fs_migrate_page(struct address_space *mapping,
1958 struct page *newpage, struct page *page, enum migrate_mode mode)
1959{
1960 int rc, extra_count;
1961 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
1962 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
1963
1964 BUG_ON(PageWriteback(page));
1965
1966 /* migrating an atomic written page is safe with the inmem_lock hold */
1967 if (atomic_written && !mutex_trylock(&fi->inmem_lock))
1968 return -EAGAIN;
1969
1970 /*
1971 * A reference is expected if PagePrivate set when move mapping,
1972 * however F2FS breaks this for maintaining dirty page counts when
1973 * truncating pages. So here adjusting the 'extra_count' make it work.
1974 */
1975 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
1976 rc = migrate_page_move_mapping(mapping, newpage,
1977 page, NULL, mode, extra_count);
1978 if (rc != MIGRATEPAGE_SUCCESS) {
1979 if (atomic_written)
1980 mutex_unlock(&fi->inmem_lock);
1981 return rc;
1982 }
1983
1984 if (atomic_written) {
1985 struct inmem_pages *cur;
1986 list_for_each_entry(cur, &fi->inmem_pages, list)
1987 if (cur->page == page) {
1988 cur->page = newpage;
1989 break;
1990 }
1991 mutex_unlock(&fi->inmem_lock);
1992 put_page(page);
1993 get_page(newpage);
1994 }
1995
1996 if (PagePrivate(page))
1997 SetPagePrivate(newpage);
1998 set_page_private(newpage, page_private(page));
1999
2000 migrate_page_copy(newpage, page);
2001
2002 return MIGRATEPAGE_SUCCESS;
2003}
2004#endif
2005
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002006const struct address_space_operations f2fs_dblock_aops = {
2007 .readpage = f2fs_read_data_page,
2008 .readpages = f2fs_read_data_pages,
2009 .writepage = f2fs_write_data_page,
2010 .writepages = f2fs_write_data_pages,
2011 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002012 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002013 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002014 .invalidatepage = f2fs_invalidate_page,
2015 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002016 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002017 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002018#ifdef CONFIG_MIGRATION
2019 .migratepage = f2fs_migrate_page,
2020#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002021};