blob: 9e5561fa4cb638219d44052ca8d3f7ff248f0695 [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
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020032static void f2fs_read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090033{
Linus Torvaldsf5688492014-01-30 11:19:05 -080034 struct bio_vec *bvec;
35 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090036
Chao Yu8b038c72016-09-18 23:30:07 +080037#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu1ecc0c52016-09-23 21:30:09 +080038 if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_IO))
Chao Yu8b038c72016-09-18 23:30:07 +080039 bio->bi_error = -EIO;
40#endif
41
Chao Yu12377022015-05-25 18:03:38 +080042 if (f2fs_bio_encrypted(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020043 if (bio->bi_error) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070044 fscrypt_release_ctx(bio->bi_private);
Chao Yu12377022015-05-25 18:03:38 +080045 } else {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070046 fscrypt_decrypt_bio_pages(bio->bi_private, bio);
Chao Yu12377022015-05-25 18:03:38 +080047 return;
48 }
49 }
50
Linus Torvaldsf5688492014-01-30 11:19:05 -080051 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090052 struct page *page = bvec->bv_page;
53
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020054 if (!bio->bi_error) {
Jaegeuk Kim237c0792016-06-30 18:49:15 -070055 if (!PageUptodate(page))
56 SetPageUptodate(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080057 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090058 ClearPageUptodate(page);
59 SetPageError(page);
60 }
61 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080062 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090063 bio_put(bio);
64}
65
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020066static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090067{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090068 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080069 struct bio_vec *bvec;
70 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090071
Linus Torvaldsf5688492014-01-30 11:19:05 -080072 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090073 struct page *page = bvec->bv_page;
74
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070075 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -070076
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020077 if (unlikely(bio->bi_error)) {
Michal Hocko5114a972016-10-11 13:56:01 -070078 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -070079 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090080 }
81 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080082 }
Jaegeuk Kimf5730182016-05-17 16:23:36 -070083 if (atomic_dec_and_test(&sbi->nr_wb_bios) &&
84 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090085 wake_up(&sbi->cp_wait);
86
87 bio_put(bio);
88}
89
Gu Zheng940a6d32013-12-20 17:39:59 +080090/*
91 * Low-level block read/write IO operations.
92 */
93static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
94 int npages, bool is_read)
95{
96 struct bio *bio;
97
Jaegeuk Kim740432f2015-08-14 11:43:56 -070098 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +080099
100 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +0800101 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +0800102 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +0800103 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800104
105 return bio;
106}
107
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700108static inline void __submit_bio(struct f2fs_sb_info *sbi,
109 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700110{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700111 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700112 atomic_inc(&sbi->nr_wb_bios);
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700113 if (f2fs_sb_mounted_hmsmr(sbi->sb) &&
114 current->plug && (type == DATA || type == NODE))
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700115 blk_finish_plug(current->plug);
116 }
Mike Christie4e49ea42016-06-05 14:31:41 -0500117 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700118}
119
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900120static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900121{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900122 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900123
124 if (!io->bio)
125 return;
126
Mike Christie04d328d2016-06-05 14:31:55 -0500127 if (is_read_io(fio->op))
Chao Yu2ace38e2014-12-24 16:08:14 +0800128 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700129 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800130 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900131
Mike Christie04d328d2016-06-05 14:31:55 -0500132 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
133
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700134 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900135 io->bio = NULL;
136}
137
Chao Yu0c3a5792016-01-18 18:28:11 +0800138static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
139 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800140{
Chao Yu0fd785e2016-01-18 18:24:59 +0800141 struct bio_vec *bvec;
142 struct page *target;
143 int i;
144
Chao Yu0c3a5792016-01-18 18:28:11 +0800145 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800146 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800147
148 if (!inode && !page && !ino)
149 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800150
151 bio_for_each_segment_all(bvec, io->bio, i) {
152
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700153 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800154 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700155 else
156 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800157
Chao Yu0c3a5792016-01-18 18:28:11 +0800158 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800159 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800160 if (page && page == target)
161 return true;
162 if (ino && ino == ino_of_node(target))
163 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800164 }
165
Chao Yu0fd785e2016-01-18 18:24:59 +0800166 return false;
167}
168
Chao Yu0c3a5792016-01-18 18:28:11 +0800169static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
170 struct page *page, nid_t ino,
171 enum page_type type)
172{
173 enum page_type btype = PAGE_TYPE_OF_BIO(type);
174 struct f2fs_bio_info *io = &sbi->write_io[btype];
175 bool ret;
176
177 down_read(&io->io_rwsem);
178 ret = __has_merged_page(io, inode, page, ino);
179 up_read(&io->io_rwsem);
180 return ret;
181}
182
183static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
184 struct inode *inode, struct page *page,
185 nid_t ino, enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900186{
187 enum page_type btype = PAGE_TYPE_OF_BIO(type);
188 struct f2fs_bio_info *io;
189
190 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
191
Chao Yudf0f8dc2014-03-22 14:57:23 +0800192 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900193
Chao Yu0c3a5792016-01-18 18:28:11 +0800194 if (!__has_merged_page(io, inode, page, ino))
195 goto out;
196
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900197 /* change META to META_FLUSH in the checkpoint procedure */
198 if (type >= META_FLUSH) {
199 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500200 io->fio.op = REQ_OP_WRITE;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600201 io->fio.op_flags = REQ_PREFLUSH | REQ_META | REQ_PRIO;
202 if (!test_opt(sbi, NOBARRIER))
203 io->fio.op_flags |= REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900204 }
205 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800206out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800207 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900208}
209
Chao Yu0c3a5792016-01-18 18:28:11 +0800210void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
211 int rw)
212{
213 __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw);
214}
215
216void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
217 struct inode *inode, struct page *page,
218 nid_t ino, enum page_type type, int rw)
219{
220 if (has_merged_page(sbi, inode, page, ino, type))
221 __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw);
222}
223
Chao Yu406657d2016-02-24 17:17:55 +0800224void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
225{
226 f2fs_submit_merged_bio(sbi, DATA, WRITE);
227 f2fs_submit_merged_bio(sbi, NODE, WRITE);
228 f2fs_submit_merged_bio(sbi, META, WRITE);
229}
230
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900231/*
232 * Fill the locked page with data located in the block address.
233 * Return unlocked page.
234 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700235int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900236{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900237 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700238 struct page *page = fio->encrypted_page ?
239 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900240
Chao Yu2ace38e2014-12-24 16:08:14 +0800241 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700242 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900243
244 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500245 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900246
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300247 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900248 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900249 return -EFAULT;
250 }
Mike Christie04d328d2016-06-05 14:31:55 -0500251 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900252
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700253 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900254 return 0;
255}
256
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700257void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900258{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700259 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900260 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900261 struct f2fs_bio_info *io;
Mike Christie04d328d2016-06-05 14:31:55 -0500262 bool is_read = is_read_io(fio->op);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700263 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900264
Gu Zheng940a6d32013-12-20 17:39:59 +0800265 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900266
Chao Yu7a9d7542016-02-22 18:36:38 +0800267 if (fio->old_blkaddr != NEW_ADDR)
268 verify_block_addr(sbi, fio->old_blkaddr);
269 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900270
Chao Yudf0f8dc2014-03-22 14:57:23 +0800271 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900272
Chao Yu7a9d7542016-02-22 18:36:38 +0800273 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Mike Christie04d328d2016-06-05 14:31:55 -0500274 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900275 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900276alloc_new:
277 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700278 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800279
Chao Yu7a9d7542016-02-22 18:36:38 +0800280 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
281 bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900282 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900283 }
284
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700285 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
286
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300287 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
288 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900289 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900290 goto alloc_new;
291 }
292
Chao Yu7a9d7542016-02-22 18:36:38 +0800293 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700294 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900295
Chao Yudf0f8dc2014-03-22 14:57:23 +0800296 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700297 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900298}
299
Chao Yu46008c62016-05-09 19:56:30 +0800300static void __set_data_blkaddr(struct dnode_of_data *dn)
301{
302 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
303 __le32 *addr_array;
304
305 /* Get physical address of data block */
306 addr_array = blkaddr_in_node(rn);
307 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
308}
309
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900310/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900311 * Lock ordering for the change of data block address:
312 * ->data_page
313 * ->node_page
314 * update block addresses in the node page
315 */
Chao Yu216a6202015-03-19 19:23:32 +0800316void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900317{
Chao Yu46008c62016-05-09 19:56:30 +0800318 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
319 __set_data_blkaddr(dn);
320 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800321 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900322}
323
Chao Yuf28b3432016-02-24 17:16:47 +0800324void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
325{
326 dn->data_blkaddr = blkaddr;
327 set_data_blkaddr(dn);
328 f2fs_update_extent_cache(dn);
329}
330
Chao Yu46008c62016-05-09 19:56:30 +0800331/* dn->ofs_in_node will be returned with up-to-date last block pointer */
332int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900333{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700334 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900335
Chao Yu46008c62016-05-09 19:56:30 +0800336 if (!count)
337 return 0;
338
Jaegeuk Kim91942322016-05-20 10:13:22 -0700339 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900340 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800341 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900342 return -ENOSPC;
343
Chao Yu46008c62016-05-09 19:56:30 +0800344 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
345 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900346
Chao Yu46008c62016-05-09 19:56:30 +0800347 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
348
349 for (; count > 0; dn->ofs_in_node++) {
350 block_t blkaddr =
351 datablock_addr(dn->node_page, dn->ofs_in_node);
352 if (blkaddr == NULL_ADDR) {
353 dn->data_blkaddr = NEW_ADDR;
354 __set_data_blkaddr(dn);
355 count--;
356 }
357 }
358
359 if (set_page_dirty(dn->node_page))
360 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900361 return 0;
362}
363
Chao Yu46008c62016-05-09 19:56:30 +0800364/* Should keep dn->ofs_in_node unchanged */
365int reserve_new_block(struct dnode_of_data *dn)
366{
367 unsigned int ofs_in_node = dn->ofs_in_node;
368 int ret;
369
370 ret = reserve_new_blocks(dn, 1);
371 dn->ofs_in_node = ofs_in_node;
372 return ret;
373}
374
Huajun Lib6009652013-11-10 23:13:18 +0800375int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
376{
377 bool need_put = dn->inode_page ? false : true;
378 int err;
379
380 err = get_dnode_of_data(dn, index, ALLOC_NODE);
381 if (err)
382 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900383
Huajun Lib6009652013-11-10 23:13:18 +0800384 if (dn->data_blkaddr == NULL_ADDR)
385 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900386 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800387 f2fs_put_dnode(dn);
388 return err;
389}
390
Fan Li759af1c2015-08-05 15:52:16 +0800391int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900392{
Chao Yu028a41e2015-03-19 19:26:02 +0800393 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800394 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800395
Fan Li759af1c2015-08-05 15:52:16 +0800396 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
397 dn->data_blkaddr = ei.blk + index - ei.fofs;
398 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800399 }
400
Fan Li759af1c2015-08-05 15:52:16 +0800401 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900402}
403
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700404struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500405 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900406{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900407 struct address_space *mapping = inode->i_mapping;
408 struct dnode_of_data dn;
409 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800410 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900411 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800412 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700413 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800414 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500415 .op = REQ_OP_READ,
416 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700417 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800418 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900419
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700420 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
421 return read_mapping_page(mapping, index, NULL);
422
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700423 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900424 if (!page)
425 return ERR_PTR(-ENOMEM);
426
Chao Yucb3bc9e2015-02-05 18:03:40 +0800427 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
428 dn.data_blkaddr = ei.blk + index - ei.fofs;
429 goto got_it;
430 }
431
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900432 set_new_dnode(&dn, inode, NULL, NULL, 0);
433 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700434 if (err)
435 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900436 f2fs_put_dnode(&dn);
437
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900438 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700439 err = -ENOENT;
440 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900441 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800442got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700443 if (PageUptodate(page)) {
444 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900445 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700446 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900447
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900448 /*
449 * A new dentry page is allocated but not able to be written, since its
450 * new inode page couldn't be allocated due to -ENOSPC.
451 * In such the case, its blkaddr can be remained as NEW_ADDR.
452 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
453 */
454 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300455 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700456 if (!PageUptodate(page))
457 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700458 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900459 return page;
460 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900461
Chao Yu7a9d7542016-02-22 18:36:38 +0800462 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700463 fio.page = page;
464 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900465 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700466 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700467 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700468
469put_err:
470 f2fs_put_page(page, 1);
471 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700472}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900473
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700474struct page *find_data_page(struct inode *inode, pgoff_t index)
475{
476 struct address_space *mapping = inode->i_mapping;
477 struct page *page;
478
479 page = find_get_page(mapping, index);
480 if (page && PageUptodate(page))
481 return page;
482 f2fs_put_page(page, 0);
483
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600484 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700485 if (IS_ERR(page))
486 return page;
487
488 if (PageUptodate(page))
489 return page;
490
491 wait_on_page_locked(page);
492 if (unlikely(!PageUptodate(page))) {
493 f2fs_put_page(page, 0);
494 return ERR_PTR(-EIO);
495 }
496 return page;
497}
498
499/*
500 * If it tries to access a hole, return an error.
501 * Because, the callers, functions in dir.c and GC, should be able to know
502 * whether this page exists or not.
503 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700504struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
505 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700506{
507 struct address_space *mapping = inode->i_mapping;
508 struct page *page;
509repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600510 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700511 if (IS_ERR(page))
512 return page;
513
514 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900515 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900516 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900517 f2fs_put_page(page, 1);
518 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900519 }
Chao Yu1563ac72016-07-03 22:05:12 +0800520 if (unlikely(!PageUptodate(page))) {
521 f2fs_put_page(page, 1);
522 return ERR_PTR(-EIO);
523 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900524 return page;
525}
526
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900527/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900528 * Caller ensures that this data page is never allocated.
529 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900530 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800531 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
532 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800533 * Note that, ipage is set only by make_empty_dir, and if any error occur,
534 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900535 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900536struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900537 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900538{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900539 struct address_space *mapping = inode->i_mapping;
540 struct page *page;
541 struct dnode_of_data dn;
542 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800543
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700544 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800545 if (!page) {
546 /*
547 * before exiting, we should make sure ipage will be released
548 * if any error occur.
549 */
550 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700551 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800552 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900553
Jaegeuk Kima8865372013-12-27 17:04:17 +0900554 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800555 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700556 if (err) {
557 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900558 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900559 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700560 if (!ipage)
561 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900562
563 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700564 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900565
566 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300567 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700568 if (!PageUptodate(page))
569 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900570 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700571 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900572
Jaegeuk Kim76121182016-01-01 22:03:47 -0800573 /* if ipage exists, blkaddr should be NEW_ADDR */
574 f2fs_bug_on(F2FS_I_SB(inode), ipage);
575 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700576 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800577 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900578 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700579got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800580 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700581 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700582 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900583 return page;
584}
585
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900586static int __allocate_data_block(struct dnode_of_data *dn)
587{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700588 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900589 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900590 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800591 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700592 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800593 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900594
Jaegeuk Kim91942322016-05-20 10:13:22 -0700595 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900596 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800597
598 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
599 if (dn->data_blkaddr == NEW_ADDR)
600 goto alloc;
601
Chao Yu46008c62016-05-09 19:56:30 +0800602 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900603 return -ENOSPC;
604
Chao Yudf6136e2015-03-23 10:33:37 +0800605alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900606 get_node_info(sbi, dn->nid, &ni);
607 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
608
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800609 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
610 seg = CURSEG_DIRECT_IO;
611
Chao Yudf6136e2015-03-23 10:33:37 +0800612 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
613 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800614 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900615
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700616 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800617 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700618 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300619 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700620 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300621 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900622 return 0;
623}
624
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800625ssize_t f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800626{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800627 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800628 struct f2fs_map_blocks map;
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800629 ssize_t ret = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800630
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700631 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800632 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
633 if (map.m_len > map.m_lblk)
634 map.m_len -= map.m_lblk;
635 else
636 map.m_len = 0;
637
Chao Yuda859852016-01-26 15:42:58 +0800638 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800639
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800640 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800641 ret = f2fs_convert_inline_inode(inode);
642 if (ret)
643 return ret;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800644 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800645 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800646 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
647 ret = f2fs_convert_inline_inode(inode);
648 if (ret)
649 return ret;
650 }
651 if (!f2fs_has_inline_data(inode))
652 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800653 return ret;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800654}
655
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900656/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700657 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
658 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800659 * If original data blocks are allocated, then give them to blockdev.
660 * Otherwise,
661 * a. preallocate requested block addresses
662 * b. do not use extent cache for better performance
663 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900664 */
Chao Yud323d002015-10-27 09:53:45 +0800665int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800666 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900667{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700668 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900669 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800670 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700671 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800672 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900673 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800674 unsigned int ofs_in_node, last_ofs_in_node;
675 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800676 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900677 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800678 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900679
Chao Yudfd02e42016-08-20 15:12:01 +0800680 if (!maxblocks)
681 return 0;
682
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700683 map->m_len = 0;
684 map->m_flags = 0;
685
686 /* it only supports block size == page size */
687 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800688 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900689
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800690 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700691 map->m_pblk = ei.blk + pgofs - ei.fofs;
692 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
693 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900694 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800695 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900696
Chao Yu4fe71e82016-01-26 15:37:38 +0800697next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800698 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800699 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900700
701 /* When reading holes, we need its node page */
702 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900703 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900704 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800705 if (flag == F2FS_GET_BLOCK_BMAP)
706 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800707 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900708 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800709 if (map->m_next_pgofs)
710 *map->m_next_pgofs =
711 get_next_page_offset(&dn, pgofs);
712 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900713 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900714 }
Chao Yu973163f2015-09-18 16:51:51 +0800715
Chao Yu46008c62016-05-09 19:56:30 +0800716 prealloc = 0;
717 ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800718 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900719
Chao Yu4fe71e82016-01-26 15:37:38 +0800720next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800721 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800722
Fan Li7df3a432015-12-17 13:20:59 +0800723 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
724 if (create) {
725 if (unlikely(f2fs_cp_error(sbi))) {
726 err = -EIO;
727 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800728 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800729 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800730 if (blkaddr == NULL_ADDR) {
731 prealloc++;
732 last_ofs_in_node = dn.ofs_in_node;
733 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800734 } else {
735 err = __allocate_data_block(&dn);
Chao Yu46008c62016-05-09 19:56:30 +0800736 if (!err) {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700737 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yu46008c62016-05-09 19:56:30 +0800738 allocated = true;
739 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800740 }
Fan Li7df3a432015-12-17 13:20:59 +0800741 if (err)
742 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800743 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800744 blkaddr = dn.data_blkaddr;
745 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800746 if (flag == F2FS_GET_BLOCK_BMAP) {
747 map->m_pblk = 0;
748 goto sync_out;
749 }
Chao Yuda859852016-01-26 15:42:58 +0800750 if (flag == F2FS_GET_BLOCK_FIEMAP &&
751 blkaddr == NULL_ADDR) {
752 if (map->m_next_pgofs)
753 *map->m_next_pgofs = pgofs + 1;
754 }
Fan Li7df3a432015-12-17 13:20:59 +0800755 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800756 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800757 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900758 }
759 }
Fan Li7df3a432015-12-17 13:20:59 +0800760
Chao Yu46008c62016-05-09 19:56:30 +0800761 if (flag == F2FS_GET_BLOCK_PRE_AIO)
762 goto skip;
763
Chao Yu4fe71e82016-01-26 15:37:38 +0800764 if (map->m_len == 0) {
765 /* preallocated unwritten block should be mapped for fiemap. */
766 if (blkaddr == NEW_ADDR)
767 map->m_flags |= F2FS_MAP_UNWRITTEN;
768 map->m_flags |= F2FS_MAP_MAPPED;
769
770 map->m_pblk = blkaddr;
771 map->m_len = 1;
772 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800773 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800774 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800775 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800776 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800777 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800778 } else {
779 goto sync_out;
780 }
781
Chao Yu46008c62016-05-09 19:56:30 +0800782skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800783 dn.ofs_in_node++;
784 pgofs++;
785
Chao Yu46008c62016-05-09 19:56:30 +0800786 /* preallocate blocks in batch for one dnode page */
787 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
788 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800789
Chao Yu46008c62016-05-09 19:56:30 +0800790 dn.ofs_in_node = ofs_in_node;
791 err = reserve_new_blocks(&dn, prealloc);
792 if (err)
793 goto sync_out;
Chao Yu58383be2016-08-20 15:12:02 +0800794 allocated = dn.node_changed;
Chao Yu4fe71e82016-01-26 15:37:38 +0800795
Chao Yu46008c62016-05-09 19:56:30 +0800796 map->m_len += dn.ofs_in_node - ofs_in_node;
797 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
798 err = -ENOSPC;
799 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800800 }
Chao Yu46008c62016-05-09 19:56:30 +0800801 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800802 }
803
Chao Yu46008c62016-05-09 19:56:30 +0800804 if (pgofs >= end)
805 goto sync_out;
806 else if (dn.ofs_in_node < end_offset)
807 goto next_block;
808
Chao Yu46008c62016-05-09 19:56:30 +0800809 f2fs_put_dnode(&dn);
810
811 if (create) {
812 f2fs_unlock_op(sbi);
813 f2fs_balance_fs(sbi, allocated);
814 }
815 allocated = false;
816 goto next_dnode;
817
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900818sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900819 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900820unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800821 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800822 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800823 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800824 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900825out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700826 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900827 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900828}
829
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700830static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800831 struct buffer_head *bh, int create, int flag,
832 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700833{
834 struct f2fs_map_blocks map;
835 int ret;
836
837 map.m_lblk = iblock;
838 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800839 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700840
Chao Yue2b4e2b2015-08-19 19:11:19 +0800841 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700842 if (!ret) {
843 map_bh(bh, inode->i_sb, map.m_pblk);
844 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
845 bh->b_size = map.m_len << inode->i_blkbits;
846 }
847 return ret;
848}
849
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900850static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800851 struct buffer_head *bh_result, int create, int flag,
852 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900853{
Chao Yuda859852016-01-26 15:42:58 +0800854 return __get_data_block(inode, iblock, bh_result, create,
855 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900856}
857
Chao Yue2b4e2b2015-08-19 19:11:19 +0800858static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900859 struct buffer_head *bh_result, int create)
860{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800861 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800862 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800863}
864
865static int get_data_block_bmap(struct inode *inode, sector_t iblock,
866 struct buffer_head *bh_result, int create)
867{
Yunlei He179448b2015-12-28 21:48:32 +0800868 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800869 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800870 return -EFBIG;
871
Chao Yue2b4e2b2015-08-19 19:11:19 +0800872 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800873 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900874}
875
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700876static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
877{
878 return (offset >> inode->i_blkbits);
879}
880
881static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
882{
883 return (blk << inode->i_blkbits);
884}
885
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900886int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
887 u64 start, u64 len)
888{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700889 struct buffer_head map_bh;
890 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800891 pgoff_t next_pgofs;
Fan Lide1475c2016-01-04 15:56:50 +0800892 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700893 u64 logical = 0, phys = 0, size = 0;
894 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700895 int ret = 0;
896
897 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
898 if (ret)
899 return ret;
900
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700901 if (f2fs_has_inline_data(inode)) {
902 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
903 if (ret != -EAGAIN)
904 return ret;
905 }
906
Al Viro59551022016-01-22 15:40:57 -0500907 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800908
909 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800910 if (start >= isize)
911 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700912
Fan Li9a950d52015-12-26 18:07:41 +0800913 if (start + len > isize)
914 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700915
916 if (logical_to_blk(inode, len) == 0)
917 len = blk_to_logical(inode, 1);
918
919 start_blk = logical_to_blk(inode, start);
920 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800921
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700922next:
923 memset(&map_bh, 0, sizeof(struct buffer_head));
924 map_bh.b_size = len;
925
Chao Yue2b4e2b2015-08-19 19:11:19 +0800926 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800927 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700928 if (ret)
929 goto out;
930
931 /* HOLE */
932 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800933 start_blk = next_pgofs;
Fan Li9a950d52015-12-26 18:07:41 +0800934 /* Go through holes util pass the EOF */
Chao Yuda859852016-01-26 15:42:58 +0800935 if (blk_to_logical(inode, start_blk) < isize)
Fan Li9a950d52015-12-26 18:07:41 +0800936 goto prep_next;
937 /* Found a hole beyond isize means no more extents.
938 * Note that the premise is that filesystems don't
939 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700940 */
Fan Li9a950d52015-12-26 18:07:41 +0800941 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700942 }
Fan Li9a950d52015-12-26 18:07:41 +0800943
Chao Yuda5af122016-01-08 20:19:27 +0800944 if (size) {
945 if (f2fs_encrypted_inode(inode))
946 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
947
Fan Li9a950d52015-12-26 18:07:41 +0800948 ret = fiemap_fill_next_extent(fieinfo, logical,
949 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800950 }
Fan Li9a950d52015-12-26 18:07:41 +0800951
952 if (start_blk > last_blk || ret)
953 goto out;
954
955 logical = blk_to_logical(inode, start_blk);
956 phys = blk_to_logical(inode, map_bh.b_blocknr);
957 size = map_bh.b_size;
958 flags = 0;
959 if (buffer_unwritten(&map_bh))
960 flags = FIEMAP_EXTENT_UNWRITTEN;
961
962 start_blk += logical_to_blk(inode, size);
963
964prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700965 cond_resched();
966 if (fatal_signal_pending(current))
967 ret = -EINTR;
968 else
969 goto next;
970out:
971 if (ret == 1)
972 ret = 0;
973
Al Viro59551022016-01-22 15:40:57 -0500974 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700975 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900976}
977
Wei Yongjun6a7a3ae2016-08-23 15:23:59 +0000978static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
979 unsigned nr_pages)
Chao Yu78682f72016-07-03 22:05:11 +0800980{
981 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
982 struct fscrypt_ctx *ctx = NULL;
983 struct block_device *bdev = sbi->sb->s_bdev;
984 struct bio *bio;
985
986 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
987 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
988 if (IS_ERR(ctx))
989 return ERR_CAST(ctx);
990
991 /* wait the page to be moved by cleaning */
992 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
993 }
994
995 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
996 if (!bio) {
997 if (ctx)
998 fscrypt_release_ctx(ctx);
999 return ERR_PTR(-ENOMEM);
1000 }
1001 bio->bi_bdev = bdev;
1002 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blkaddr);
1003 bio->bi_end_io = f2fs_read_end_io;
1004 bio->bi_private = ctx;
1005
1006 return bio;
1007}
1008
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001009/*
1010 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1011 * Major change was from block_size == page_size in f2fs by default.
1012 */
1013static int f2fs_mpage_readpages(struct address_space *mapping,
1014 struct list_head *pages, struct page *page,
1015 unsigned nr_pages)
1016{
1017 struct bio *bio = NULL;
1018 unsigned page_idx;
1019 sector_t last_block_in_bio = 0;
1020 struct inode *inode = mapping->host;
1021 const unsigned blkbits = inode->i_blkbits;
1022 const unsigned blocksize = 1 << blkbits;
1023 sector_t block_in_file;
1024 sector_t last_block;
1025 sector_t last_block_in_file;
1026 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001027 struct f2fs_map_blocks map;
1028
1029 map.m_pblk = 0;
1030 map.m_lblk = 0;
1031 map.m_len = 0;
1032 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001033 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001034
1035 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1036
1037 prefetchw(&page->flags);
1038 if (pages) {
1039 page = list_entry(pages->prev, struct page, lru);
1040 list_del(&page->lru);
1041 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001042 page->index,
1043 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001044 goto next_page;
1045 }
1046
1047 block_in_file = (sector_t)page->index;
1048 last_block = block_in_file + nr_pages;
1049 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1050 blkbits;
1051 if (last_block > last_block_in_file)
1052 last_block = last_block_in_file;
1053
1054 /*
1055 * Map blocks using the previous result first.
1056 */
1057 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1058 block_in_file > map.m_lblk &&
1059 block_in_file < (map.m_lblk + map.m_len))
1060 goto got_it;
1061
1062 /*
1063 * Then do more f2fs_map_blocks() calls until we are
1064 * done with this page.
1065 */
1066 map.m_flags = 0;
1067
1068 if (block_in_file < last_block) {
1069 map.m_lblk = block_in_file;
1070 map.m_len = last_block - block_in_file;
1071
Chao Yu46c9e142015-09-18 16:54:16 +08001072 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001073 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001074 goto set_error_page;
1075 }
1076got_it:
1077 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1078 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1079 SetPageMappedToDisk(page);
1080
1081 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1082 SetPageUptodate(page);
1083 goto confused;
1084 }
1085 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001086 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001087 if (!PageUptodate(page))
1088 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001089 unlock_page(page);
1090 goto next_page;
1091 }
1092
1093 /*
1094 * This page will go to BIO. Do we need to send this
1095 * BIO off first?
1096 */
1097 if (bio && (last_block_in_bio != block_nr - 1)) {
1098submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001099 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001100 bio = NULL;
1101 }
1102 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001103 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001104 if (IS_ERR(bio)) {
1105 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001106 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001107 }
Mike Christie04d328d2016-06-05 14:31:55 -05001108 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001109 }
1110
1111 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1112 goto submit_and_realloc;
1113
1114 last_block_in_bio = block_nr;
1115 goto next_page;
1116set_error_page:
1117 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001118 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001119 unlock_page(page);
1120 goto next_page;
1121confused:
1122 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001123 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001124 bio = NULL;
1125 }
1126 unlock_page(page);
1127next_page:
1128 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001129 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001130 }
1131 BUG_ON(pages && !list_empty(pages));
1132 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001133 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001134 return 0;
1135}
1136
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001137static int f2fs_read_data_page(struct file *file, struct page *page)
1138{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001139 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001140 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001141
Chao Yuc20e89c2014-05-06 16:53:08 +08001142 trace_f2fs_readpage(page, DATA);
1143
arter97e1c42042014-08-06 23:22:50 +09001144 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001145 if (f2fs_has_inline_data(inode))
1146 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001147 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001148 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001149 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001150}
1151
1152static int f2fs_read_data_pages(struct file *file,
1153 struct address_space *mapping,
1154 struct list_head *pages, unsigned nr_pages)
1155{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001156 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001157 struct page *page = list_entry(pages->prev, struct page, lru);
1158
1159 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001160
1161 /* If the file has inline data, skip readpages */
1162 if (f2fs_has_inline_data(inode))
1163 return 0;
1164
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001165 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001166}
1167
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001168int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001169{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001170 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001171 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001172 struct dnode_of_data dn;
1173 int err = 0;
1174
1175 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001176 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001177 if (err)
1178 return err;
1179
Chao Yu28bc1062016-02-06 14:40:34 +08001180 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001181
1182 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001183 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001184 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001185 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001186 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001187
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001188 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001189 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001190
1191 /* wait for GCed encrypted page writeback */
1192 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001193 fio->old_blkaddr);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001194retry_encrypt:
1195 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1196 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001197 if (IS_ERR(fio->encrypted_page)) {
1198 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001199 if (err == -ENOMEM) {
1200 /* flush pending ios and wait for a while */
1201 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1202 congestion_wait(BLK_RW_ASYNC, HZ/50);
1203 gfp_flags |= __GFP_NOFAIL;
1204 err = 0;
1205 goto retry_encrypt;
1206 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001207 goto out_writepage;
1208 }
1209 }
1210
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001211 set_page_writeback(page);
1212
1213 /*
1214 * If current allocation needs SSR,
1215 * it had better in-place writes for updated data.
1216 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001217 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001218 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001219 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001220 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001221 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001222 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001223 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001224 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001225 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001226 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001227 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001228 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001229 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001230 }
1231out_writepage:
1232 f2fs_put_dnode(&dn);
1233 return err;
1234}
1235
1236static int f2fs_write_data_page(struct page *page,
1237 struct writeback_control *wbc)
1238{
1239 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001240 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001241 loff_t i_size = i_size_read(inode);
1242 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001243 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001244 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001245 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001246 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001247 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001248 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001249 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001250 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001251 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001252 .op_flags = wbc_to_write_flags(wbc),
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001253 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001254 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001255 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001256
Chao Yuecda0de2014-05-06 16:48:26 +08001257 trace_f2fs_writepage(page, DATA);
1258
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001259 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001260 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001261
1262 /*
1263 * If the offset is out-of-range of file size,
1264 * this page does not have to be written to disk.
1265 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001266 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001267 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001268 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001269
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001270 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001271write:
Chao Yucaf00472015-01-28 17:48:42 +08001272 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001273 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001274 if (f2fs_is_drop_cache(inode))
1275 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001276 /* we should not write 0'th page having journal header */
1277 if (f2fs_is_volatile_file(inode) && (!page->index ||
1278 (!wbc->for_reclaim &&
1279 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001280 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001281
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001282 /* we should bypass data pages to proceed the kworkder jobs */
1283 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001284 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001285 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001286 }
1287
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001288 /* Dentry blocks are controlled by checkpoint */
1289 if (S_ISDIR(inode->i_mode)) {
1290 err = do_write_data_page(&fio);
1291 goto done;
1292 }
1293
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001294 if (!wbc->for_reclaim)
1295 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001296 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001297 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001298
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001299 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001300 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001301 if (f2fs_has_inline_data(inode))
1302 err = f2fs_write_inline_data(inode, page);
1303 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001304 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001305 if (F2FS_I(inode)->last_disk_size < psize)
1306 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001307 f2fs_unlock_op(sbi);
1308done:
1309 if (err && err != -ENOENT)
1310 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001311
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001312 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001313out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001314 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001315 if (err)
1316 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001317
1318 if (wbc->for_reclaim) {
1319 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001320 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001321 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001322
1323 unlock_page(page);
1324 f2fs_balance_fs(sbi, need_balance_fs);
1325
1326 if (unlikely(f2fs_cp_error(sbi)))
1327 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1328
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001329 return 0;
1330
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001331redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001332 redirty_page_for_writepage(wbc, page);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001333 unlock_page(page);
1334 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001335}
1336
Chao Yu8f46dca2015-07-14 18:56:10 +08001337/*
1338 * This function was copied from write_cche_pages from mm/page-writeback.c.
1339 * The major change is making write step of cold data page separately from
1340 * warm/hot data page.
1341 */
1342static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001343 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001344{
1345 int ret = 0;
1346 int done = 0;
1347 struct pagevec pvec;
1348 int nr_pages;
1349 pgoff_t uninitialized_var(writeback_index);
1350 pgoff_t index;
1351 pgoff_t end; /* Inclusive */
1352 pgoff_t done_index;
1353 int cycled;
1354 int range_whole = 0;
1355 int tag;
Chao Yu6ca56ca2016-09-29 18:50:11 +08001356 int nwritten = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08001357
1358 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001359
Chao Yu8f46dca2015-07-14 18:56:10 +08001360 if (wbc->range_cyclic) {
1361 writeback_index = mapping->writeback_index; /* prev offset */
1362 index = writeback_index;
1363 if (index == 0)
1364 cycled = 1;
1365 else
1366 cycled = 0;
1367 end = -1;
1368 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001369 index = wbc->range_start >> PAGE_SHIFT;
1370 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001371 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1372 range_whole = 1;
1373 cycled = 1; /* ignore range_cyclic tests */
1374 }
1375 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1376 tag = PAGECACHE_TAG_TOWRITE;
1377 else
1378 tag = PAGECACHE_TAG_DIRTY;
1379retry:
1380 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1381 tag_pages_for_writeback(mapping, index, end);
1382 done_index = index;
1383 while (!done && (index <= end)) {
1384 int i;
1385
1386 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1387 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1388 if (nr_pages == 0)
1389 break;
1390
1391 for (i = 0; i < nr_pages; i++) {
1392 struct page *page = pvec.pages[i];
1393
1394 if (page->index > end) {
1395 done = 1;
1396 break;
1397 }
1398
1399 done_index = page->index;
1400
1401 lock_page(page);
1402
1403 if (unlikely(page->mapping != mapping)) {
1404continue_unlock:
1405 unlock_page(page);
1406 continue;
1407 }
1408
1409 if (!PageDirty(page)) {
1410 /* someone wrote it for us */
1411 goto continue_unlock;
1412 }
1413
Chao Yu8f46dca2015-07-14 18:56:10 +08001414 if (PageWriteback(page)) {
1415 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001416 f2fs_wait_on_page_writeback(page,
1417 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001418 else
1419 goto continue_unlock;
1420 }
1421
1422 BUG_ON(PageWriteback(page));
1423 if (!clear_page_dirty_for_io(page))
1424 goto continue_unlock;
1425
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001426 ret = mapping->a_ops->writepage(page, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001427 if (unlikely(ret)) {
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001428 done_index = page->index + 1;
1429 done = 1;
1430 break;
Chao Yu6ca56ca2016-09-29 18:50:11 +08001431 } else {
1432 nwritten++;
Chao Yu8f46dca2015-07-14 18:56:10 +08001433 }
1434
1435 if (--wbc->nr_to_write <= 0 &&
1436 wbc->sync_mode == WB_SYNC_NONE) {
1437 done = 1;
1438 break;
1439 }
1440 }
1441 pagevec_release(&pvec);
1442 cond_resched();
1443 }
1444
Chao Yu8f46dca2015-07-14 18:56:10 +08001445 if (!cycled && !done) {
1446 cycled = 1;
1447 index = 0;
1448 end = writeback_index - 1;
1449 goto retry;
1450 }
1451 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1452 mapping->writeback_index = done_index;
1453
Chao Yu6ca56ca2016-09-29 18:50:11 +08001454 if (nwritten)
1455 f2fs_submit_merged_bio_cond(F2FS_M_SB(mapping), mapping->host,
1456 NULL, 0, DATA, WRITE);
1457
Chao Yu8f46dca2015-07-14 18:56:10 +08001458 return ret;
1459}
1460
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001461static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001462 struct writeback_control *wbc)
1463{
1464 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001465 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001466 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001467 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001468
P J Pcfb185a2013-04-03 11:38:00 +09001469 /* deal with chardevs and other special file */
1470 if (!mapping->a_ops->writepage)
1471 return 0;
1472
Chao Yu6a290542015-07-17 18:02:39 +08001473 /* skip writing if there is no dirty page in this inode */
1474 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1475 return 0;
1476
Jaegeuk Kima1257022015-10-08 10:40:07 -07001477 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1478 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1479 available_free_memory(sbi, DIRTY_DENTS))
1480 goto skip_write;
1481
Chao Yud323d002015-10-27 09:53:45 +08001482 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001483 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001484 goto skip_write;
1485
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001486 /* during POR, we don't need to trigger writepage at all. */
1487 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1488 goto skip_write;
1489
Yunlei Hed31c7c32016-02-04 16:14:00 +08001490 trace_f2fs_writepages(mapping->host, wbc, DATA);
1491
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001492 blk_start_plug(&plug);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001493 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001494 blk_finish_plug(&plug);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001495 /*
1496 * if some pages were truncated, we cannot guarantee its mapping->host
1497 * to detect pending bios.
1498 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001499
Chao Yuc227f912015-12-16 13:09:20 +08001500 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001501 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001502
1503skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001504 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001505 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001506 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001507}
1508
Chao Yu3aab8f82014-07-02 13:25:04 +08001509static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1510{
1511 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001512 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001513
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001514 if (to > i_size) {
1515 truncate_pagecache(inode, i_size);
1516 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001517 }
1518}
1519
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001520static int prepare_write_begin(struct f2fs_sb_info *sbi,
1521 struct page *page, loff_t pos, unsigned len,
1522 block_t *blk_addr, bool *node_changed)
1523{
1524 struct inode *inode = page->mapping->host;
1525 pgoff_t index = page->index;
1526 struct dnode_of_data dn;
1527 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001528 bool locked = false;
1529 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001530 int err = 0;
1531
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001532 /*
1533 * we already allocated all the blocks, so we don't need to get
1534 * the block addresses when there is no need to fill the page.
1535 */
Yunlei He5d4c0af2016-09-18 08:16:56 +08001536 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001537 return 0;
1538
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001539 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001540 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001541 f2fs_lock_op(sbi);
1542 locked = true;
1543 }
1544restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001545 /* check inline_data */
1546 ipage = get_node_page(sbi, inode->i_ino);
1547 if (IS_ERR(ipage)) {
1548 err = PTR_ERR(ipage);
1549 goto unlock_out;
1550 }
1551
1552 set_new_dnode(&dn, inode, ipage, ipage, 0);
1553
1554 if (f2fs_has_inline_data(inode)) {
1555 if (pos + len <= MAX_INLINE_DATA) {
1556 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001557 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001558 if (inode->i_nlink)
1559 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001560 } else {
1561 err = f2fs_convert_inline_page(&dn, page);
1562 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001563 goto out;
1564 if (dn.data_blkaddr == NULL_ADDR)
1565 err = f2fs_get_block(&dn, index);
1566 }
1567 } else if (locked) {
1568 err = f2fs_get_block(&dn, index);
1569 } else {
1570 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1571 dn.data_blkaddr = ei.blk + index - ei.fofs;
1572 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001573 /* hole case */
1574 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001575 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001576 f2fs_put_dnode(&dn);
1577 f2fs_lock_op(sbi);
1578 locked = true;
1579 goto restart;
1580 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001581 }
1582 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001583
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001584 /* convert_inline_page can make node_changed */
1585 *blk_addr = dn.data_blkaddr;
1586 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001587out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001588 f2fs_put_dnode(&dn);
1589unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001590 if (locked)
1591 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001592 return err;
1593}
1594
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001595static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1596 loff_t pos, unsigned len, unsigned flags,
1597 struct page **pagep, void **fsdata)
1598{
1599 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001600 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001601 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001602 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001603 bool need_balance = false;
1604 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001605 int err = 0;
1606
Chao Yu62aed042014-05-06 16:46:04 +08001607 trace_f2fs_write_begin(inode, pos, len, flags);
1608
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001609 /*
1610 * We should check this at this moment to avoid deadlock on inode page
1611 * and #0 page. The locking rule for inline_data conversion should be:
1612 * lock_page(page #0) -> lock_page(inode_page)
1613 */
1614 if (index != 0) {
1615 err = f2fs_convert_inline_inode(inode);
1616 if (err)
1617 goto fail;
1618 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001619repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001620 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001621 if (!page) {
1622 err = -ENOMEM;
1623 goto fail;
1624 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001625
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001626 *pagep = page;
1627
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001628 err = prepare_write_begin(sbi, page, pos, len,
1629 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001630 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001631 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001632
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001633 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001634 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001635 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001636 lock_page(page);
1637 if (page->mapping != mapping) {
1638 /* The page got truncated from under us */
1639 f2fs_put_page(page, 1);
1640 goto repeat;
1641 }
1642 }
1643
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001644 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001645
Chao Yu08b39fb2015-10-08 13:27:34 +08001646 /* wait for GCed encrypted page writeback */
1647 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001648 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001649
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001650 if (len == PAGE_SIZE || PageUptodate(page))
1651 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001652
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001653 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001654 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001655 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001656 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001657 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001658
Chao Yu78682f72016-07-03 22:05:11 +08001659 bio = f2fs_grab_bio(inode, blkaddr, 1);
1660 if (IS_ERR(bio)) {
1661 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001662 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001663 }
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001664 bio->bi_opf = REQ_OP_READ;
Chao Yu78682f72016-07-03 22:05:11 +08001665 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1666 bio_put(bio);
1667 err = -EFAULT;
1668 goto fail;
1669 }
1670
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001671 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001672
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001673 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001674 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001675 f2fs_put_page(page, 1);
1676 goto repeat;
1677 }
Chao Yu1563ac72016-07-03 22:05:12 +08001678 if (unlikely(!PageUptodate(page))) {
1679 err = -EIO;
1680 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001681 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001682 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001683 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001684
Chao Yu3aab8f82014-07-02 13:25:04 +08001685fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001686 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001687 f2fs_write_failed(mapping, pos + len);
1688 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001689}
1690
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001691static int f2fs_write_end(struct file *file,
1692 struct address_space *mapping,
1693 loff_t pos, unsigned len, unsigned copied,
1694 struct page *page, void *fsdata)
1695{
1696 struct inode *inode = page->mapping->host;
1697
Chao Yudfb2bf32014-05-06 16:47:23 +08001698 trace_f2fs_write_end(inode, pos, len, copied);
1699
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001700 /*
1701 * This should be come from len == PAGE_SIZE, and we expect copied
1702 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
1703 * let generic_perform_write() try to copy data again through copied=0.
1704 */
1705 if (!PageUptodate(page)) {
1706 if (unlikely(copied != PAGE_SIZE))
1707 copied = 0;
1708 else
1709 SetPageUptodate(page);
1710 }
1711 if (!copied)
1712 goto unlock_out;
1713
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001714 set_page_dirty(page);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001715 clear_cold_data(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001716
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001717 if (pos + copied > i_size_read(inode))
1718 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001719unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08001720 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001721 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001722 return copied;
1723}
1724
Omar Sandoval6f673762015-03-16 04:33:52 -07001725static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1726 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001727{
1728 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001729
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001730 if (offset & blocksize_mask)
1731 return -EINVAL;
1732
Al Viro5b46f252014-03-16 18:07:34 -04001733 if (iov_iter_alignment(iter) & blocksize_mask)
1734 return -EINVAL;
1735
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001736 return 0;
1737}
1738
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001739static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001740{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001741 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001742 struct inode *inode = mapping->host;
1743 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001744 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08001745 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08001746 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001747
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001748 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001749 if (err)
1750 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001751
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001752 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1753 return 0;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001754 if (test_opt(F2FS_I_SB(inode), LFS))
1755 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001756
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001757 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08001758
Chao Yu82e0a5a2016-07-13 09:18:29 +08001759 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001760 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08001761 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
1762
1763 if (rw == WRITE) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001764 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001765 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001766 else if (err < 0)
1767 f2fs_write_failed(mapping, offset + count);
1768 }
Chao Yu70407fa2014-07-31 21:11:22 +08001769
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001770 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08001771
Chao Yu3aab8f82014-07-02 13:25:04 +08001772 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001773}
1774
Chao Yu487261f2015-02-05 17:44:29 +08001775void f2fs_invalidate_page(struct page *page, unsigned int offset,
1776 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001777{
1778 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001779 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001780
Chao Yu487261f2015-02-05 17:44:29 +08001781 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001782 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001783 return;
1784
Chao Yu487261f2015-02-05 17:44:29 +08001785 if (PageDirty(page)) {
1786 if (inode->i_ino == F2FS_META_INO(sbi))
1787 dec_page_count(sbi, F2FS_DIRTY_META);
1788 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1789 dec_page_count(sbi, F2FS_DIRTY_NODES);
1790 else
1791 inode_dec_dirty_pages(inode);
1792 }
Chao Yudecd36b2015-08-07 18:42:09 +08001793
1794 /* This is atomic written page, keep Private */
1795 if (IS_ATOMIC_WRITTEN_PAGE(page))
1796 return;
1797
Chao Yu23dc9742016-04-29 20:09:15 +08001798 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001799 ClearPagePrivate(page);
1800}
1801
Chao Yu487261f2015-02-05 17:44:29 +08001802int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001803{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001804 /* If this is dirty page, keep PagePrivate */
1805 if (PageDirty(page))
1806 return 0;
1807
Chao Yudecd36b2015-08-07 18:42:09 +08001808 /* This is atomic written page, keep Private */
1809 if (IS_ATOMIC_WRITTEN_PAGE(page))
1810 return 0;
1811
Chao Yu23dc9742016-04-29 20:09:15 +08001812 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001813 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001814 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001815}
1816
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001817/*
1818 * This was copied from __set_page_dirty_buffers which gives higher performance
1819 * in very high speed storages. (e.g., pmem)
1820 */
1821void f2fs_set_page_dirty_nobuffers(struct page *page)
1822{
1823 struct address_space *mapping = page->mapping;
1824 unsigned long flags;
1825
1826 if (unlikely(!mapping))
1827 return;
1828
1829 spin_lock(&mapping->private_lock);
1830 lock_page_memcg(page);
1831 SetPageDirty(page);
1832 spin_unlock(&mapping->private_lock);
1833
1834 spin_lock_irqsave(&mapping->tree_lock, flags);
1835 WARN_ON_ONCE(!PageUptodate(page));
1836 account_page_dirtied(page, mapping);
1837 radix_tree_tag_set(&mapping->page_tree,
1838 page_index(page), PAGECACHE_TAG_DIRTY);
1839 spin_unlock_irqrestore(&mapping->tree_lock, flags);
1840 unlock_page_memcg(page);
1841
1842 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1843 return;
1844}
1845
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001846static int f2fs_set_data_page_dirty(struct page *page)
1847{
1848 struct address_space *mapping = page->mapping;
1849 struct inode *inode = mapping->host;
1850
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001851 trace_f2fs_set_page_dirty(page, DATA);
1852
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001853 if (!PageUptodate(page))
1854 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001855
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001856 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001857 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1858 register_inmem_page(inode, page);
1859 return 1;
1860 }
1861 /*
1862 * Previously, this page has been registered, we just
1863 * return here.
1864 */
1865 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001866 }
1867
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001868 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001869 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001870 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001871 return 1;
1872 }
1873 return 0;
1874}
1875
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001876static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1877{
Chao Yu454ae7e2014-04-22 13:34:01 +08001878 struct inode *inode = mapping->host;
1879
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001880 if (f2fs_has_inline_data(inode))
1881 return 0;
1882
1883 /* make sure allocating whole blocks */
1884 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1885 filemap_write_and_wait(mapping);
1886
Chao Yue2b4e2b2015-08-19 19:11:19 +08001887 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001888}
1889
Weichao Guo5b7a4872016-09-20 05:03:27 +08001890#ifdef CONFIG_MIGRATION
1891#include <linux/migrate.h>
1892
1893int f2fs_migrate_page(struct address_space *mapping,
1894 struct page *newpage, struct page *page, enum migrate_mode mode)
1895{
1896 int rc, extra_count;
1897 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
1898 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
1899
1900 BUG_ON(PageWriteback(page));
1901
1902 /* migrating an atomic written page is safe with the inmem_lock hold */
1903 if (atomic_written && !mutex_trylock(&fi->inmem_lock))
1904 return -EAGAIN;
1905
1906 /*
1907 * A reference is expected if PagePrivate set when move mapping,
1908 * however F2FS breaks this for maintaining dirty page counts when
1909 * truncating pages. So here adjusting the 'extra_count' make it work.
1910 */
1911 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
1912 rc = migrate_page_move_mapping(mapping, newpage,
1913 page, NULL, mode, extra_count);
1914 if (rc != MIGRATEPAGE_SUCCESS) {
1915 if (atomic_written)
1916 mutex_unlock(&fi->inmem_lock);
1917 return rc;
1918 }
1919
1920 if (atomic_written) {
1921 struct inmem_pages *cur;
1922 list_for_each_entry(cur, &fi->inmem_pages, list)
1923 if (cur->page == page) {
1924 cur->page = newpage;
1925 break;
1926 }
1927 mutex_unlock(&fi->inmem_lock);
1928 put_page(page);
1929 get_page(newpage);
1930 }
1931
1932 if (PagePrivate(page))
1933 SetPagePrivate(newpage);
1934 set_page_private(newpage, page_private(page));
1935
1936 migrate_page_copy(newpage, page);
1937
1938 return MIGRATEPAGE_SUCCESS;
1939}
1940#endif
1941
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001942const struct address_space_operations f2fs_dblock_aops = {
1943 .readpage = f2fs_read_data_page,
1944 .readpages = f2fs_read_data_pages,
1945 .writepage = f2fs_write_data_page,
1946 .writepages = f2fs_write_data_pages,
1947 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001948 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001949 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001950 .invalidatepage = f2fs_invalidate_page,
1951 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001952 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001953 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08001954#ifdef CONFIG_MIGRATION
1955 .migratepage = f2fs_migrate_page,
1956#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001957};