blob: 8b9a1dc151b2fb4d5309231ccd37bf82dd03b9d5 [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)) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090078 set_bit(AS_EIO, &page->mapping->flags);
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;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700201 if (test_opt(sbi, NOBARRIER))
Mike Christie04d328d2016-06-05 14:31:55 -0500202 io->fio.op_flags = WRITE_FLUSH | REQ_META | REQ_PRIO;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700203 else
Mike Christie04d328d2016-06-05 14:31:55 -0500204 io->fio.op_flags = WRITE_FLUSH_FUA | REQ_META |
205 REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900206 }
207 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800208out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800209 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900210}
211
Chao Yu0c3a5792016-01-18 18:28:11 +0800212void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
213 int rw)
214{
215 __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw);
216}
217
218void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
219 struct inode *inode, struct page *page,
220 nid_t ino, enum page_type type, int rw)
221{
222 if (has_merged_page(sbi, inode, page, ino, type))
223 __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw);
224}
225
Chao Yu406657d2016-02-24 17:17:55 +0800226void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
227{
228 f2fs_submit_merged_bio(sbi, DATA, WRITE);
229 f2fs_submit_merged_bio(sbi, NODE, WRITE);
230 f2fs_submit_merged_bio(sbi, META, WRITE);
231}
232
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900233/*
234 * Fill the locked page with data located in the block address.
235 * Return unlocked page.
236 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700237int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900238{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900239 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700240 struct page *page = fio->encrypted_page ?
241 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900242
Chao Yu2ace38e2014-12-24 16:08:14 +0800243 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700244 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900245
246 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500247 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900248
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300249 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900250 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900251 return -EFAULT;
252 }
Mike Christie04d328d2016-06-05 14:31:55 -0500253 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900254
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700255 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900256 return 0;
257}
258
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700259void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900260{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700261 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900262 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900263 struct f2fs_bio_info *io;
Mike Christie04d328d2016-06-05 14:31:55 -0500264 bool is_read = is_read_io(fio->op);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700265 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900266
Gu Zheng940a6d32013-12-20 17:39:59 +0800267 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900268
Chao Yu7a9d7542016-02-22 18:36:38 +0800269 if (fio->old_blkaddr != NEW_ADDR)
270 verify_block_addr(sbi, fio->old_blkaddr);
271 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900272
Chao Yudf0f8dc2014-03-22 14:57:23 +0800273 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900274
Chao Yu7a9d7542016-02-22 18:36:38 +0800275 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Mike Christie04d328d2016-06-05 14:31:55 -0500276 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900277 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900278alloc_new:
279 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700280 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800281
Chao Yu7a9d7542016-02-22 18:36:38 +0800282 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
283 bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900284 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900285 }
286
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700287 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
288
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300289 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
290 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900291 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900292 goto alloc_new;
293 }
294
Chao Yu7a9d7542016-02-22 18:36:38 +0800295 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700296 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900297
Chao Yudf0f8dc2014-03-22 14:57:23 +0800298 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700299 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900300}
301
Chao Yu46008c62016-05-09 19:56:30 +0800302static void __set_data_blkaddr(struct dnode_of_data *dn)
303{
304 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
305 __le32 *addr_array;
306
307 /* Get physical address of data block */
308 addr_array = blkaddr_in_node(rn);
309 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
310}
311
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900312/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900313 * Lock ordering for the change of data block address:
314 * ->data_page
315 * ->node_page
316 * update block addresses in the node page
317 */
Chao Yu216a6202015-03-19 19:23:32 +0800318void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900319{
Chao Yu46008c62016-05-09 19:56:30 +0800320 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
321 __set_data_blkaddr(dn);
322 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800323 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900324}
325
Chao Yuf28b3432016-02-24 17:16:47 +0800326void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
327{
328 dn->data_blkaddr = blkaddr;
329 set_data_blkaddr(dn);
330 f2fs_update_extent_cache(dn);
331}
332
Chao Yu46008c62016-05-09 19:56:30 +0800333/* dn->ofs_in_node will be returned with up-to-date last block pointer */
334int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900335{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700336 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900337
Chao Yu46008c62016-05-09 19:56:30 +0800338 if (!count)
339 return 0;
340
Jaegeuk Kim91942322016-05-20 10:13:22 -0700341 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900342 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800343 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900344 return -ENOSPC;
345
Chao Yu46008c62016-05-09 19:56:30 +0800346 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
347 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900348
Chao Yu46008c62016-05-09 19:56:30 +0800349 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
350
351 for (; count > 0; dn->ofs_in_node++) {
352 block_t blkaddr =
353 datablock_addr(dn->node_page, dn->ofs_in_node);
354 if (blkaddr == NULL_ADDR) {
355 dn->data_blkaddr = NEW_ADDR;
356 __set_data_blkaddr(dn);
357 count--;
358 }
359 }
360
361 if (set_page_dirty(dn->node_page))
362 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900363 return 0;
364}
365
Chao Yu46008c62016-05-09 19:56:30 +0800366/* Should keep dn->ofs_in_node unchanged */
367int reserve_new_block(struct dnode_of_data *dn)
368{
369 unsigned int ofs_in_node = dn->ofs_in_node;
370 int ret;
371
372 ret = reserve_new_blocks(dn, 1);
373 dn->ofs_in_node = ofs_in_node;
374 return ret;
375}
376
Huajun Lib6009652013-11-10 23:13:18 +0800377int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
378{
379 bool need_put = dn->inode_page ? false : true;
380 int err;
381
382 err = get_dnode_of_data(dn, index, ALLOC_NODE);
383 if (err)
384 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900385
Huajun Lib6009652013-11-10 23:13:18 +0800386 if (dn->data_blkaddr == NULL_ADDR)
387 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900388 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800389 f2fs_put_dnode(dn);
390 return err;
391}
392
Fan Li759af1c2015-08-05 15:52:16 +0800393int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900394{
Chao Yu028a41e2015-03-19 19:26:02 +0800395 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800396 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800397
Fan Li759af1c2015-08-05 15:52:16 +0800398 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
399 dn->data_blkaddr = ei.blk + index - ei.fofs;
400 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800401 }
402
Fan Li759af1c2015-08-05 15:52:16 +0800403 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900404}
405
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700406struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500407 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900408{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900409 struct address_space *mapping = inode->i_mapping;
410 struct dnode_of_data dn;
411 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800412 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900413 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800414 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700415 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800416 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500417 .op = REQ_OP_READ,
418 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700419 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800420 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900421
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700422 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
423 return read_mapping_page(mapping, index, NULL);
424
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700425 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900426 if (!page)
427 return ERR_PTR(-ENOMEM);
428
Chao Yucb3bc9e2015-02-05 18:03:40 +0800429 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
430 dn.data_blkaddr = ei.blk + index - ei.fofs;
431 goto got_it;
432 }
433
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900434 set_new_dnode(&dn, inode, NULL, NULL, 0);
435 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700436 if (err)
437 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900438 f2fs_put_dnode(&dn);
439
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900440 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700441 err = -ENOENT;
442 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900443 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800444got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700445 if (PageUptodate(page)) {
446 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900447 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700448 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900449
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900450 /*
451 * A new dentry page is allocated but not able to be written, since its
452 * new inode page couldn't be allocated due to -ENOSPC.
453 * In such the case, its blkaddr can be remained as NEW_ADDR.
454 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
455 */
456 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300457 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700458 if (!PageUptodate(page))
459 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700460 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900461 return page;
462 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900463
Chao Yu7a9d7542016-02-22 18:36:38 +0800464 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700465 fio.page = page;
466 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900467 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700468 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700469 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700470
471put_err:
472 f2fs_put_page(page, 1);
473 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700474}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900475
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700476struct page *find_data_page(struct inode *inode, pgoff_t index)
477{
478 struct address_space *mapping = inode->i_mapping;
479 struct page *page;
480
481 page = find_get_page(mapping, index);
482 if (page && PageUptodate(page))
483 return page;
484 f2fs_put_page(page, 0);
485
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700486 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700487 if (IS_ERR(page))
488 return page;
489
490 if (PageUptodate(page))
491 return page;
492
493 wait_on_page_locked(page);
494 if (unlikely(!PageUptodate(page))) {
495 f2fs_put_page(page, 0);
496 return ERR_PTR(-EIO);
497 }
498 return page;
499}
500
501/*
502 * If it tries to access a hole, return an error.
503 * Because, the callers, functions in dir.c and GC, should be able to know
504 * whether this page exists or not.
505 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700506struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
507 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700508{
509 struct address_space *mapping = inode->i_mapping;
510 struct page *page;
511repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700512 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700513 if (IS_ERR(page))
514 return page;
515
516 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900517 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900518 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900519 f2fs_put_page(page, 1);
520 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900521 }
Chao Yu1563ac72016-07-03 22:05:12 +0800522 if (unlikely(!PageUptodate(page))) {
523 f2fs_put_page(page, 1);
524 return ERR_PTR(-EIO);
525 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900526 return page;
527}
528
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900529/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900530 * Caller ensures that this data page is never allocated.
531 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900532 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800533 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
534 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800535 * Note that, ipage is set only by make_empty_dir, and if any error occur,
536 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900537 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900538struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900539 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900540{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900541 struct address_space *mapping = inode->i_mapping;
542 struct page *page;
543 struct dnode_of_data dn;
544 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800545
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700546 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800547 if (!page) {
548 /*
549 * before exiting, we should make sure ipage will be released
550 * if any error occur.
551 */
552 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700553 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800554 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900555
Jaegeuk Kima8865372013-12-27 17:04:17 +0900556 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800557 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700558 if (err) {
559 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900560 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900561 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700562 if (!ipage)
563 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900564
565 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700566 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900567
568 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300569 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700570 if (!PageUptodate(page))
571 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900572 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700573 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900574
Jaegeuk Kim76121182016-01-01 22:03:47 -0800575 /* if ipage exists, blkaddr should be NEW_ADDR */
576 f2fs_bug_on(F2FS_I_SB(inode), ipage);
577 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700578 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800579 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900580 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700581got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800582 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700583 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700584 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900585 return page;
586}
587
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900588static int __allocate_data_block(struct dnode_of_data *dn)
589{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700590 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900591 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900592 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800593 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700594 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800595 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900596
Jaegeuk Kim91942322016-05-20 10:13:22 -0700597 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900598 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800599
600 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
601 if (dn->data_blkaddr == NEW_ADDR)
602 goto alloc;
603
Chao Yu46008c62016-05-09 19:56:30 +0800604 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900605 return -ENOSPC;
606
Chao Yudf6136e2015-03-23 10:33:37 +0800607alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900608 get_node_info(sbi, dn->nid, &ni);
609 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
610
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800611 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
612 seg = CURSEG_DIRECT_IO;
613
Chao Yudf6136e2015-03-23 10:33:37 +0800614 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
615 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800616 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900617
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700618 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800619 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700620 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300621 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700622 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300623 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900624 return 0;
625}
626
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800627ssize_t f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800628{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800629 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800630 struct f2fs_map_blocks map;
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800631 ssize_t ret = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800632
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700633 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800634 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
635 if (map.m_len > map.m_lblk)
636 map.m_len -= map.m_lblk;
637 else
638 map.m_len = 0;
639
Chao Yuda859852016-01-26 15:42:58 +0800640 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800641
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800642 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800643 ret = f2fs_convert_inline_inode(inode);
644 if (ret)
645 return ret;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800646 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800647 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800648 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
649 ret = f2fs_convert_inline_inode(inode);
650 if (ret)
651 return ret;
652 }
653 if (!f2fs_has_inline_data(inode))
654 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800655 return ret;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800656}
657
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900658/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700659 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
660 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800661 * If original data blocks are allocated, then give them to blockdev.
662 * Otherwise,
663 * a. preallocate requested block addresses
664 * b. do not use extent cache for better performance
665 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900666 */
Chao Yud323d002015-10-27 09:53:45 +0800667int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800668 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900669{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700670 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900671 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800672 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700673 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800674 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900675 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800676 unsigned int ofs_in_node, last_ofs_in_node;
677 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800678 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900679 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800680 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900681
Chao Yudfd02e42016-08-20 15:12:01 +0800682 if (!maxblocks)
683 return 0;
684
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700685 map->m_len = 0;
686 map->m_flags = 0;
687
688 /* it only supports block size == page size */
689 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800690 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900691
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800692 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700693 map->m_pblk = ei.blk + pgofs - ei.fofs;
694 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
695 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900696 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800697 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900698
Chao Yu4fe71e82016-01-26 15:37:38 +0800699next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800700 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800701 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900702
703 /* When reading holes, we need its node page */
704 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900705 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900706 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800707 if (flag == F2FS_GET_BLOCK_BMAP)
708 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800709 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900710 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800711 if (map->m_next_pgofs)
712 *map->m_next_pgofs =
713 get_next_page_offset(&dn, pgofs);
714 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900715 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900716 }
Chao Yu973163f2015-09-18 16:51:51 +0800717
Chao Yu46008c62016-05-09 19:56:30 +0800718 prealloc = 0;
719 ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800720 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900721
Chao Yu4fe71e82016-01-26 15:37:38 +0800722next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800723 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800724
Fan Li7df3a432015-12-17 13:20:59 +0800725 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
726 if (create) {
727 if (unlikely(f2fs_cp_error(sbi))) {
728 err = -EIO;
729 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800730 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800731 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800732 if (blkaddr == NULL_ADDR) {
733 prealloc++;
734 last_ofs_in_node = dn.ofs_in_node;
735 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800736 } else {
737 err = __allocate_data_block(&dn);
Chao Yu46008c62016-05-09 19:56:30 +0800738 if (!err) {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700739 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yu46008c62016-05-09 19:56:30 +0800740 allocated = true;
741 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800742 }
Fan Li7df3a432015-12-17 13:20:59 +0800743 if (err)
744 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800745 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800746 blkaddr = dn.data_blkaddr;
747 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800748 if (flag == F2FS_GET_BLOCK_BMAP) {
749 map->m_pblk = 0;
750 goto sync_out;
751 }
Chao Yuda859852016-01-26 15:42:58 +0800752 if (flag == F2FS_GET_BLOCK_FIEMAP &&
753 blkaddr == NULL_ADDR) {
754 if (map->m_next_pgofs)
755 *map->m_next_pgofs = pgofs + 1;
756 }
Fan Li7df3a432015-12-17 13:20:59 +0800757 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800758 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800759 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900760 }
761 }
Fan Li7df3a432015-12-17 13:20:59 +0800762
Chao Yu46008c62016-05-09 19:56:30 +0800763 if (flag == F2FS_GET_BLOCK_PRE_AIO)
764 goto skip;
765
Chao Yu4fe71e82016-01-26 15:37:38 +0800766 if (map->m_len == 0) {
767 /* preallocated unwritten block should be mapped for fiemap. */
768 if (blkaddr == NEW_ADDR)
769 map->m_flags |= F2FS_MAP_UNWRITTEN;
770 map->m_flags |= F2FS_MAP_MAPPED;
771
772 map->m_pblk = blkaddr;
773 map->m_len = 1;
774 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800775 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800776 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800777 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800778 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800779 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800780 } else {
781 goto sync_out;
782 }
783
Chao Yu46008c62016-05-09 19:56:30 +0800784skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800785 dn.ofs_in_node++;
786 pgofs++;
787
Chao Yu46008c62016-05-09 19:56:30 +0800788 /* preallocate blocks in batch for one dnode page */
789 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
790 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800791
Chao Yu46008c62016-05-09 19:56:30 +0800792 dn.ofs_in_node = ofs_in_node;
793 err = reserve_new_blocks(&dn, prealloc);
794 if (err)
795 goto sync_out;
Chao Yu58383be2016-08-20 15:12:02 +0800796 allocated = dn.node_changed;
Chao Yu4fe71e82016-01-26 15:37:38 +0800797
Chao Yu46008c62016-05-09 19:56:30 +0800798 map->m_len += dn.ofs_in_node - ofs_in_node;
799 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
800 err = -ENOSPC;
801 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800802 }
Chao Yu46008c62016-05-09 19:56:30 +0800803 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800804 }
805
Chao Yu46008c62016-05-09 19:56:30 +0800806 if (pgofs >= end)
807 goto sync_out;
808 else if (dn.ofs_in_node < end_offset)
809 goto next_block;
810
Chao Yu46008c62016-05-09 19:56:30 +0800811 f2fs_put_dnode(&dn);
812
813 if (create) {
814 f2fs_unlock_op(sbi);
815 f2fs_balance_fs(sbi, allocated);
816 }
817 allocated = false;
818 goto next_dnode;
819
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900820sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900821 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900822unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800823 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800824 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800825 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800826 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900827out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700828 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900829 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900830}
831
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700832static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800833 struct buffer_head *bh, int create, int flag,
834 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700835{
836 struct f2fs_map_blocks map;
837 int ret;
838
839 map.m_lblk = iblock;
840 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800841 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700842
Chao Yue2b4e2b2015-08-19 19:11:19 +0800843 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700844 if (!ret) {
845 map_bh(bh, inode->i_sb, map.m_pblk);
846 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
847 bh->b_size = map.m_len << inode->i_blkbits;
848 }
849 return ret;
850}
851
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900852static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800853 struct buffer_head *bh_result, int create, int flag,
854 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900855{
Chao Yuda859852016-01-26 15:42:58 +0800856 return __get_data_block(inode, iblock, bh_result, create,
857 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900858}
859
Chao Yue2b4e2b2015-08-19 19:11:19 +0800860static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900861 struct buffer_head *bh_result, int create)
862{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800863 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800864 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800865}
866
867static int get_data_block_bmap(struct inode *inode, sector_t iblock,
868 struct buffer_head *bh_result, int create)
869{
Yunlei He179448b2015-12-28 21:48:32 +0800870 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800871 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800872 return -EFBIG;
873
Chao Yue2b4e2b2015-08-19 19:11:19 +0800874 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800875 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900876}
877
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700878static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
879{
880 return (offset >> inode->i_blkbits);
881}
882
883static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
884{
885 return (blk << inode->i_blkbits);
886}
887
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900888int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
889 u64 start, u64 len)
890{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700891 struct buffer_head map_bh;
892 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800893 pgoff_t next_pgofs;
Fan Lide1475c2016-01-04 15:56:50 +0800894 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700895 u64 logical = 0, phys = 0, size = 0;
896 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700897 int ret = 0;
898
899 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
900 if (ret)
901 return ret;
902
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700903 if (f2fs_has_inline_data(inode)) {
904 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
905 if (ret != -EAGAIN)
906 return ret;
907 }
908
Al Viro59551022016-01-22 15:40:57 -0500909 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800910
911 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800912 if (start >= isize)
913 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700914
Fan Li9a950d52015-12-26 18:07:41 +0800915 if (start + len > isize)
916 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700917
918 if (logical_to_blk(inode, len) == 0)
919 len = blk_to_logical(inode, 1);
920
921 start_blk = logical_to_blk(inode, start);
922 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800923
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700924next:
925 memset(&map_bh, 0, sizeof(struct buffer_head));
926 map_bh.b_size = len;
927
Chao Yue2b4e2b2015-08-19 19:11:19 +0800928 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800929 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700930 if (ret)
931 goto out;
932
933 /* HOLE */
934 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800935 start_blk = next_pgofs;
Fan Li9a950d52015-12-26 18:07:41 +0800936 /* Go through holes util pass the EOF */
Chao Yuda859852016-01-26 15:42:58 +0800937 if (blk_to_logical(inode, start_blk) < isize)
Fan Li9a950d52015-12-26 18:07:41 +0800938 goto prep_next;
939 /* Found a hole beyond isize means no more extents.
940 * Note that the premise is that filesystems don't
941 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700942 */
Fan Li9a950d52015-12-26 18:07:41 +0800943 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700944 }
Fan Li9a950d52015-12-26 18:07:41 +0800945
Chao Yuda5af122016-01-08 20:19:27 +0800946 if (size) {
947 if (f2fs_encrypted_inode(inode))
948 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
949
Fan Li9a950d52015-12-26 18:07:41 +0800950 ret = fiemap_fill_next_extent(fieinfo, logical,
951 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800952 }
Fan Li9a950d52015-12-26 18:07:41 +0800953
954 if (start_blk > last_blk || ret)
955 goto out;
956
957 logical = blk_to_logical(inode, start_blk);
958 phys = blk_to_logical(inode, map_bh.b_blocknr);
959 size = map_bh.b_size;
960 flags = 0;
961 if (buffer_unwritten(&map_bh))
962 flags = FIEMAP_EXTENT_UNWRITTEN;
963
964 start_blk += logical_to_blk(inode, size);
965
966prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700967 cond_resched();
968 if (fatal_signal_pending(current))
969 ret = -EINTR;
970 else
971 goto next;
972out:
973 if (ret == 1)
974 ret = 0;
975
Al Viro59551022016-01-22 15:40:57 -0500976 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700977 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900978}
979
Wei Yongjun6a7a3ae2016-08-23 15:23:59 +0000980static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
981 unsigned nr_pages)
Chao Yu78682f72016-07-03 22:05:11 +0800982{
983 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
984 struct fscrypt_ctx *ctx = NULL;
985 struct block_device *bdev = sbi->sb->s_bdev;
986 struct bio *bio;
987
988 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
989 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
990 if (IS_ERR(ctx))
991 return ERR_CAST(ctx);
992
993 /* wait the page to be moved by cleaning */
994 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
995 }
996
997 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
998 if (!bio) {
999 if (ctx)
1000 fscrypt_release_ctx(ctx);
1001 return ERR_PTR(-ENOMEM);
1002 }
1003 bio->bi_bdev = bdev;
1004 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blkaddr);
1005 bio->bi_end_io = f2fs_read_end_io;
1006 bio->bi_private = ctx;
1007
1008 return bio;
1009}
1010
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001011/*
1012 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1013 * Major change was from block_size == page_size in f2fs by default.
1014 */
1015static int f2fs_mpage_readpages(struct address_space *mapping,
1016 struct list_head *pages, struct page *page,
1017 unsigned nr_pages)
1018{
1019 struct bio *bio = NULL;
1020 unsigned page_idx;
1021 sector_t last_block_in_bio = 0;
1022 struct inode *inode = mapping->host;
1023 const unsigned blkbits = inode->i_blkbits;
1024 const unsigned blocksize = 1 << blkbits;
1025 sector_t block_in_file;
1026 sector_t last_block;
1027 sector_t last_block_in_file;
1028 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001029 struct f2fs_map_blocks map;
1030
1031 map.m_pblk = 0;
1032 map.m_lblk = 0;
1033 map.m_len = 0;
1034 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001035 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001036
1037 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1038
1039 prefetchw(&page->flags);
1040 if (pages) {
1041 page = list_entry(pages->prev, struct page, lru);
1042 list_del(&page->lru);
1043 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001044 page->index,
1045 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001046 goto next_page;
1047 }
1048
1049 block_in_file = (sector_t)page->index;
1050 last_block = block_in_file + nr_pages;
1051 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1052 blkbits;
1053 if (last_block > last_block_in_file)
1054 last_block = last_block_in_file;
1055
1056 /*
1057 * Map blocks using the previous result first.
1058 */
1059 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1060 block_in_file > map.m_lblk &&
1061 block_in_file < (map.m_lblk + map.m_len))
1062 goto got_it;
1063
1064 /*
1065 * Then do more f2fs_map_blocks() calls until we are
1066 * done with this page.
1067 */
1068 map.m_flags = 0;
1069
1070 if (block_in_file < last_block) {
1071 map.m_lblk = block_in_file;
1072 map.m_len = last_block - block_in_file;
1073
Chao Yu46c9e142015-09-18 16:54:16 +08001074 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001075 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001076 goto set_error_page;
1077 }
1078got_it:
1079 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1080 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1081 SetPageMappedToDisk(page);
1082
1083 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1084 SetPageUptodate(page);
1085 goto confused;
1086 }
1087 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001088 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001089 if (!PageUptodate(page))
1090 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001091 unlock_page(page);
1092 goto next_page;
1093 }
1094
1095 /*
1096 * This page will go to BIO. Do we need to send this
1097 * BIO off first?
1098 */
1099 if (bio && (last_block_in_bio != block_nr - 1)) {
1100submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001101 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001102 bio = NULL;
1103 }
1104 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001105 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001106 if (IS_ERR(bio)) {
1107 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001108 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001109 }
Mike Christie04d328d2016-06-05 14:31:55 -05001110 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001111 }
1112
1113 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1114 goto submit_and_realloc;
1115
1116 last_block_in_bio = block_nr;
1117 goto next_page;
1118set_error_page:
1119 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001120 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001121 unlock_page(page);
1122 goto next_page;
1123confused:
1124 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001125 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001126 bio = NULL;
1127 }
1128 unlock_page(page);
1129next_page:
1130 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001131 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001132 }
1133 BUG_ON(pages && !list_empty(pages));
1134 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001135 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001136 return 0;
1137}
1138
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001139static int f2fs_read_data_page(struct file *file, struct page *page)
1140{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001141 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001142 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001143
Chao Yuc20e89c2014-05-06 16:53:08 +08001144 trace_f2fs_readpage(page, DATA);
1145
arter97e1c42042014-08-06 23:22:50 +09001146 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001147 if (f2fs_has_inline_data(inode))
1148 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001149 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001150 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001151 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001152}
1153
1154static int f2fs_read_data_pages(struct file *file,
1155 struct address_space *mapping,
1156 struct list_head *pages, unsigned nr_pages)
1157{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001158 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001159 struct page *page = list_entry(pages->prev, struct page, lru);
1160
1161 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001162
1163 /* If the file has inline data, skip readpages */
1164 if (f2fs_has_inline_data(inode))
1165 return 0;
1166
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001167 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001168}
1169
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001170int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001171{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001172 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001173 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001174 struct dnode_of_data dn;
1175 int err = 0;
1176
1177 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001178 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001179 if (err)
1180 return err;
1181
Chao Yu28bc1062016-02-06 14:40:34 +08001182 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001183
1184 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001185 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001186 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001187 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001188 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001189
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001190 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001191 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001192
1193 /* wait for GCed encrypted page writeback */
1194 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001195 fio->old_blkaddr);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001196retry_encrypt:
1197 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1198 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001199 if (IS_ERR(fio->encrypted_page)) {
1200 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001201 if (err == -ENOMEM) {
1202 /* flush pending ios and wait for a while */
1203 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1204 congestion_wait(BLK_RW_ASYNC, HZ/50);
1205 gfp_flags |= __GFP_NOFAIL;
1206 err = 0;
1207 goto retry_encrypt;
1208 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001209 goto out_writepage;
1210 }
1211 }
1212
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001213 set_page_writeback(page);
1214
1215 /*
1216 * If current allocation needs SSR,
1217 * it had better in-place writes for updated data.
1218 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001219 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001220 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001221 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001222 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001223 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001224 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001225 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001226 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001227 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001228 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001229 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001230 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001231 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001232 }
1233out_writepage:
1234 f2fs_put_dnode(&dn);
1235 return err;
1236}
1237
1238static int f2fs_write_data_page(struct page *page,
1239 struct writeback_control *wbc)
1240{
1241 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001242 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001243 loff_t i_size = i_size_read(inode);
1244 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001245 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001246 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001247 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001248 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001249 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001250 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001251 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001252 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001253 .op = REQ_OP_WRITE,
1254 .op_flags = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : 0,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001255 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001256 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001257 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001258
Chao Yuecda0de2014-05-06 16:48:26 +08001259 trace_f2fs_writepage(page, DATA);
1260
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001261 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001262 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001263
1264 /*
1265 * If the offset is out-of-range of file size,
1266 * this page does not have to be written to disk.
1267 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001268 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001269 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001270 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001271
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001272 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001273write:
Chao Yucaf00472015-01-28 17:48:42 +08001274 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001275 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001276 if (f2fs_is_drop_cache(inode))
1277 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001278 /* we should not write 0'th page having journal header */
1279 if (f2fs_is_volatile_file(inode) && (!page->index ||
1280 (!wbc->for_reclaim &&
1281 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001282 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001283
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001284 /* we should bypass data pages to proceed the kworkder jobs */
1285 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001286 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001287 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001288 }
1289
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001290 /* Dentry blocks are controlled by checkpoint */
1291 if (S_ISDIR(inode->i_mode)) {
1292 err = do_write_data_page(&fio);
1293 goto done;
1294 }
1295
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001296 if (!wbc->for_reclaim)
1297 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001298 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001299 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001300
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001301 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001302 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001303 if (f2fs_has_inline_data(inode))
1304 err = f2fs_write_inline_data(inode, page);
1305 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001306 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001307 if (F2FS_I(inode)->last_disk_size < psize)
1308 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001309 f2fs_unlock_op(sbi);
1310done:
1311 if (err && err != -ENOENT)
1312 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001313
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001314 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001315out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001316 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001317 if (err)
1318 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001319
1320 if (wbc->for_reclaim) {
1321 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001322 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001323 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001324
1325 unlock_page(page);
1326 f2fs_balance_fs(sbi, need_balance_fs);
1327
1328 if (unlikely(f2fs_cp_error(sbi)))
1329 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1330
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001331 return 0;
1332
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001333redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001334 redirty_page_for_writepage(wbc, page);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001335 unlock_page(page);
1336 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001337}
1338
Chao Yu8f46dca2015-07-14 18:56:10 +08001339/*
1340 * This function was copied from write_cche_pages from mm/page-writeback.c.
1341 * The major change is making write step of cold data page separately from
1342 * warm/hot data page.
1343 */
1344static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001345 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001346{
1347 int ret = 0;
1348 int done = 0;
1349 struct pagevec pvec;
1350 int nr_pages;
1351 pgoff_t uninitialized_var(writeback_index);
1352 pgoff_t index;
1353 pgoff_t end; /* Inclusive */
1354 pgoff_t done_index;
1355 int cycled;
1356 int range_whole = 0;
1357 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001358
1359 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001360
Chao Yu8f46dca2015-07-14 18:56:10 +08001361 if (wbc->range_cyclic) {
1362 writeback_index = mapping->writeback_index; /* prev offset */
1363 index = writeback_index;
1364 if (index == 0)
1365 cycled = 1;
1366 else
1367 cycled = 0;
1368 end = -1;
1369 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001370 index = wbc->range_start >> PAGE_SHIFT;
1371 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001372 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1373 range_whole = 1;
1374 cycled = 1; /* ignore range_cyclic tests */
1375 }
1376 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1377 tag = PAGECACHE_TAG_TOWRITE;
1378 else
1379 tag = PAGECACHE_TAG_DIRTY;
1380retry:
1381 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1382 tag_pages_for_writeback(mapping, index, end);
1383 done_index = index;
1384 while (!done && (index <= end)) {
1385 int i;
1386
1387 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1388 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1389 if (nr_pages == 0)
1390 break;
1391
1392 for (i = 0; i < nr_pages; i++) {
1393 struct page *page = pvec.pages[i];
1394
1395 if (page->index > end) {
1396 done = 1;
1397 break;
1398 }
1399
1400 done_index = page->index;
1401
1402 lock_page(page);
1403
1404 if (unlikely(page->mapping != mapping)) {
1405continue_unlock:
1406 unlock_page(page);
1407 continue;
1408 }
1409
1410 if (!PageDirty(page)) {
1411 /* someone wrote it for us */
1412 goto continue_unlock;
1413 }
1414
Chao Yu8f46dca2015-07-14 18:56:10 +08001415 if (PageWriteback(page)) {
1416 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001417 f2fs_wait_on_page_writeback(page,
1418 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001419 else
1420 goto continue_unlock;
1421 }
1422
1423 BUG_ON(PageWriteback(page));
1424 if (!clear_page_dirty_for_io(page))
1425 goto continue_unlock;
1426
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001427 ret = mapping->a_ops->writepage(page, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001428 if (unlikely(ret)) {
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001429 done_index = page->index + 1;
1430 done = 1;
1431 break;
Chao Yu8f46dca2015-07-14 18:56:10 +08001432 }
1433
1434 if (--wbc->nr_to_write <= 0 &&
1435 wbc->sync_mode == WB_SYNC_NONE) {
1436 done = 1;
1437 break;
1438 }
1439 }
1440 pagevec_release(&pvec);
1441 cond_resched();
1442 }
1443
Chao Yu8f46dca2015-07-14 18:56:10 +08001444 if (!cycled && !done) {
1445 cycled = 1;
1446 index = 0;
1447 end = writeback_index - 1;
1448 goto retry;
1449 }
1450 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1451 mapping->writeback_index = done_index;
1452
1453 return ret;
1454}
1455
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001456static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001457 struct writeback_control *wbc)
1458{
1459 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001460 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001461 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001462 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001463
P J Pcfb185a2013-04-03 11:38:00 +09001464 /* deal with chardevs and other special file */
1465 if (!mapping->a_ops->writepage)
1466 return 0;
1467
Chao Yu6a290542015-07-17 18:02:39 +08001468 /* skip writing if there is no dirty page in this inode */
1469 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1470 return 0;
1471
Jaegeuk Kima1257022015-10-08 10:40:07 -07001472 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1473 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1474 available_free_memory(sbi, DIRTY_DENTS))
1475 goto skip_write;
1476
Chao Yud323d002015-10-27 09:53:45 +08001477 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001478 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001479 goto skip_write;
1480
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001481 /* during POR, we don't need to trigger writepage at all. */
1482 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1483 goto skip_write;
1484
Yunlei Hed31c7c32016-02-04 16:14:00 +08001485 trace_f2fs_writepages(mapping->host, wbc, DATA);
1486
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001487 blk_start_plug(&plug);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001488 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001489 blk_finish_plug(&plug);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001490 /*
1491 * if some pages were truncated, we cannot guarantee its mapping->host
1492 * to detect pending bios.
1493 */
1494 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001495
Chao Yuc227f912015-12-16 13:09:20 +08001496 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001497 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001498
1499skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001500 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001501 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001502 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001503}
1504
Chao Yu3aab8f82014-07-02 13:25:04 +08001505static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1506{
1507 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001508 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001509
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001510 if (to > i_size) {
1511 truncate_pagecache(inode, i_size);
1512 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001513 }
1514}
1515
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001516static int prepare_write_begin(struct f2fs_sb_info *sbi,
1517 struct page *page, loff_t pos, unsigned len,
1518 block_t *blk_addr, bool *node_changed)
1519{
1520 struct inode *inode = page->mapping->host;
1521 pgoff_t index = page->index;
1522 struct dnode_of_data dn;
1523 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001524 bool locked = false;
1525 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001526 int err = 0;
1527
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001528 /*
1529 * we already allocated all the blocks, so we don't need to get
1530 * the block addresses when there is no need to fill the page.
1531 */
Yunlei He5d4c0af2016-09-18 08:16:56 +08001532 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001533 return 0;
1534
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001535 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001536 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001537 f2fs_lock_op(sbi);
1538 locked = true;
1539 }
1540restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001541 /* check inline_data */
1542 ipage = get_node_page(sbi, inode->i_ino);
1543 if (IS_ERR(ipage)) {
1544 err = PTR_ERR(ipage);
1545 goto unlock_out;
1546 }
1547
1548 set_new_dnode(&dn, inode, ipage, ipage, 0);
1549
1550 if (f2fs_has_inline_data(inode)) {
1551 if (pos + len <= MAX_INLINE_DATA) {
1552 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001553 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001554 if (inode->i_nlink)
1555 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001556 } else {
1557 err = f2fs_convert_inline_page(&dn, page);
1558 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001559 goto out;
1560 if (dn.data_blkaddr == NULL_ADDR)
1561 err = f2fs_get_block(&dn, index);
1562 }
1563 } else if (locked) {
1564 err = f2fs_get_block(&dn, index);
1565 } else {
1566 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1567 dn.data_blkaddr = ei.blk + index - ei.fofs;
1568 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001569 /* hole case */
1570 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001571 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001572 f2fs_put_dnode(&dn);
1573 f2fs_lock_op(sbi);
1574 locked = true;
1575 goto restart;
1576 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001577 }
1578 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001579
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001580 /* convert_inline_page can make node_changed */
1581 *blk_addr = dn.data_blkaddr;
1582 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001583out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001584 f2fs_put_dnode(&dn);
1585unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001586 if (locked)
1587 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001588 return err;
1589}
1590
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001591static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1592 loff_t pos, unsigned len, unsigned flags,
1593 struct page **pagep, void **fsdata)
1594{
1595 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001596 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001597 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001598 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001599 bool need_balance = false;
1600 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001601 int err = 0;
1602
Chao Yu62aed042014-05-06 16:46:04 +08001603 trace_f2fs_write_begin(inode, pos, len, flags);
1604
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001605 /*
1606 * We should check this at this moment to avoid deadlock on inode page
1607 * and #0 page. The locking rule for inline_data conversion should be:
1608 * lock_page(page #0) -> lock_page(inode_page)
1609 */
1610 if (index != 0) {
1611 err = f2fs_convert_inline_inode(inode);
1612 if (err)
1613 goto fail;
1614 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001615repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001616 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001617 if (!page) {
1618 err = -ENOMEM;
1619 goto fail;
1620 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001621
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001622 *pagep = page;
1623
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001624 err = prepare_write_begin(sbi, page, pos, len,
1625 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001626 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001627 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001628
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001629 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001630 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001631 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001632 lock_page(page);
1633 if (page->mapping != mapping) {
1634 /* The page got truncated from under us */
1635 f2fs_put_page(page, 1);
1636 goto repeat;
1637 }
1638 }
1639
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001640 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001641
Chao Yu08b39fb2015-10-08 13:27:34 +08001642 /* wait for GCed encrypted page writeback */
1643 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001644 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001645
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001646 if (len == PAGE_SIZE || PageUptodate(page))
1647 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001648
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001649 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001650 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001651 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001652 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001653 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001654
Chao Yu78682f72016-07-03 22:05:11 +08001655 bio = f2fs_grab_bio(inode, blkaddr, 1);
1656 if (IS_ERR(bio)) {
1657 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001658 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001659 }
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001660 bio_set_op_attrs(bio, REQ_OP_READ, READ_SYNC);
Chao Yu78682f72016-07-03 22:05:11 +08001661 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1662 bio_put(bio);
1663 err = -EFAULT;
1664 goto fail;
1665 }
1666
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001667 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001668
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001669 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001670 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001671 f2fs_put_page(page, 1);
1672 goto repeat;
1673 }
Chao Yu1563ac72016-07-03 22:05:12 +08001674 if (unlikely(!PageUptodate(page))) {
1675 err = -EIO;
1676 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001677 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001678 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001679 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001680
Chao Yu3aab8f82014-07-02 13:25:04 +08001681fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001682 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001683 f2fs_write_failed(mapping, pos + len);
1684 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001685}
1686
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001687static int f2fs_write_end(struct file *file,
1688 struct address_space *mapping,
1689 loff_t pos, unsigned len, unsigned copied,
1690 struct page *page, void *fsdata)
1691{
1692 struct inode *inode = page->mapping->host;
1693
Chao Yudfb2bf32014-05-06 16:47:23 +08001694 trace_f2fs_write_end(inode, pos, len, copied);
1695
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001696 /*
1697 * This should be come from len == PAGE_SIZE, and we expect copied
1698 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
1699 * let generic_perform_write() try to copy data again through copied=0.
1700 */
1701 if (!PageUptodate(page)) {
1702 if (unlikely(copied != PAGE_SIZE))
1703 copied = 0;
1704 else
1705 SetPageUptodate(page);
1706 }
1707 if (!copied)
1708 goto unlock_out;
1709
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001710 set_page_dirty(page);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001711 clear_cold_data(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001712
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001713 if (pos + copied > i_size_read(inode))
1714 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001715unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08001716 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001717 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001718 return copied;
1719}
1720
Omar Sandoval6f673762015-03-16 04:33:52 -07001721static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1722 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001723{
1724 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001725
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001726 if (offset & blocksize_mask)
1727 return -EINVAL;
1728
Al Viro5b46f252014-03-16 18:07:34 -04001729 if (iov_iter_alignment(iter) & blocksize_mask)
1730 return -EINVAL;
1731
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001732 return 0;
1733}
1734
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001735static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001736{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001737 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001738 struct inode *inode = mapping->host;
1739 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001740 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08001741 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08001742 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001743
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001744 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001745 if (err)
1746 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001747
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001748 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1749 return 0;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001750 if (test_opt(F2FS_I_SB(inode), LFS))
1751 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001752
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001753 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fad2014-07-31 21:11:22 +08001754
Chao Yu82e0a5a2016-07-13 09:18:29 +08001755 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001756 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08001757 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
1758
1759 if (rw == WRITE) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001760 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001761 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001762 else if (err < 0)
1763 f2fs_write_failed(mapping, offset + count);
1764 }
Chao Yu70407fad2014-07-31 21:11:22 +08001765
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001766 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fad2014-07-31 21:11:22 +08001767
Chao Yu3aab8f82014-07-02 13:25:04 +08001768 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001769}
1770
Chao Yu487261f2015-02-05 17:44:29 +08001771void f2fs_invalidate_page(struct page *page, unsigned int offset,
1772 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001773{
1774 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001775 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001776
Chao Yu487261f2015-02-05 17:44:29 +08001777 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001778 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001779 return;
1780
Chao Yu487261f2015-02-05 17:44:29 +08001781 if (PageDirty(page)) {
1782 if (inode->i_ino == F2FS_META_INO(sbi))
1783 dec_page_count(sbi, F2FS_DIRTY_META);
1784 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1785 dec_page_count(sbi, F2FS_DIRTY_NODES);
1786 else
1787 inode_dec_dirty_pages(inode);
1788 }
Chao Yudecd36b2015-08-07 18:42:09 +08001789
1790 /* This is atomic written page, keep Private */
1791 if (IS_ATOMIC_WRITTEN_PAGE(page))
1792 return;
1793
Chao Yu23dc9742016-04-29 20:09:15 +08001794 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001795 ClearPagePrivate(page);
1796}
1797
Chao Yu487261f2015-02-05 17:44:29 +08001798int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001799{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001800 /* If this is dirty page, keep PagePrivate */
1801 if (PageDirty(page))
1802 return 0;
1803
Chao Yudecd36b2015-08-07 18:42:09 +08001804 /* This is atomic written page, keep Private */
1805 if (IS_ATOMIC_WRITTEN_PAGE(page))
1806 return 0;
1807
Chao Yu23dc9742016-04-29 20:09:15 +08001808 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001809 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001810 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001811}
1812
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001813/*
1814 * This was copied from __set_page_dirty_buffers which gives higher performance
1815 * in very high speed storages. (e.g., pmem)
1816 */
1817void f2fs_set_page_dirty_nobuffers(struct page *page)
1818{
1819 struct address_space *mapping = page->mapping;
1820 unsigned long flags;
1821
1822 if (unlikely(!mapping))
1823 return;
1824
1825 spin_lock(&mapping->private_lock);
1826 lock_page_memcg(page);
1827 SetPageDirty(page);
1828 spin_unlock(&mapping->private_lock);
1829
1830 spin_lock_irqsave(&mapping->tree_lock, flags);
1831 WARN_ON_ONCE(!PageUptodate(page));
1832 account_page_dirtied(page, mapping);
1833 radix_tree_tag_set(&mapping->page_tree,
1834 page_index(page), PAGECACHE_TAG_DIRTY);
1835 spin_unlock_irqrestore(&mapping->tree_lock, flags);
1836 unlock_page_memcg(page);
1837
1838 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1839 return;
1840}
1841
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001842static int f2fs_set_data_page_dirty(struct page *page)
1843{
1844 struct address_space *mapping = page->mapping;
1845 struct inode *inode = mapping->host;
1846
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001847 trace_f2fs_set_page_dirty(page, DATA);
1848
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001849 if (!PageUptodate(page))
1850 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001851
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001852 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001853 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1854 register_inmem_page(inode, page);
1855 return 1;
1856 }
1857 /*
1858 * Previously, this page has been registered, we just
1859 * return here.
1860 */
1861 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001862 }
1863
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001864 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001865 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001866 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001867 return 1;
1868 }
1869 return 0;
1870}
1871
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001872static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1873{
Chao Yu454ae7e2014-04-22 13:34:01 +08001874 struct inode *inode = mapping->host;
1875
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001876 if (f2fs_has_inline_data(inode))
1877 return 0;
1878
1879 /* make sure allocating whole blocks */
1880 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1881 filemap_write_and_wait(mapping);
1882
Chao Yue2b4e2b2015-08-19 19:11:19 +08001883 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001884}
1885
Weichao Guo5b7a4872016-09-20 05:03:27 +08001886#ifdef CONFIG_MIGRATION
1887#include <linux/migrate.h>
1888
1889int f2fs_migrate_page(struct address_space *mapping,
1890 struct page *newpage, struct page *page, enum migrate_mode mode)
1891{
1892 int rc, extra_count;
1893 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
1894 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
1895
1896 BUG_ON(PageWriteback(page));
1897
1898 /* migrating an atomic written page is safe with the inmem_lock hold */
1899 if (atomic_written && !mutex_trylock(&fi->inmem_lock))
1900 return -EAGAIN;
1901
1902 /*
1903 * A reference is expected if PagePrivate set when move mapping,
1904 * however F2FS breaks this for maintaining dirty page counts when
1905 * truncating pages. So here adjusting the 'extra_count' make it work.
1906 */
1907 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
1908 rc = migrate_page_move_mapping(mapping, newpage,
1909 page, NULL, mode, extra_count);
1910 if (rc != MIGRATEPAGE_SUCCESS) {
1911 if (atomic_written)
1912 mutex_unlock(&fi->inmem_lock);
1913 return rc;
1914 }
1915
1916 if (atomic_written) {
1917 struct inmem_pages *cur;
1918 list_for_each_entry(cur, &fi->inmem_pages, list)
1919 if (cur->page == page) {
1920 cur->page = newpage;
1921 break;
1922 }
1923 mutex_unlock(&fi->inmem_lock);
1924 put_page(page);
1925 get_page(newpage);
1926 }
1927
1928 if (PagePrivate(page))
1929 SetPagePrivate(newpage);
1930 set_page_private(newpage, page_private(page));
1931
1932 migrate_page_copy(newpage, page);
1933
1934 return MIGRATEPAGE_SUCCESS;
1935}
1936#endif
1937
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001938const struct address_space_operations f2fs_dblock_aops = {
1939 .readpage = f2fs_read_data_page,
1940 .readpages = f2fs_read_data_pages,
1941 .writepage = f2fs_write_data_page,
1942 .writepages = f2fs_write_data_pages,
1943 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001944 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001945 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001946 .invalidatepage = f2fs_invalidate_page,
1947 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001948 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001949 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08001950#ifdef CONFIG_MIGRATION
1951 .migratepage = f2fs_migrate_page,
1952#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001953};