blob: b2cccd4083b8d1864d739dd67737fcb0c0782d02 [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;
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 Yuaafb3712018-08-01 19:13:44 +0800243 if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr,
244 __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC))
245 return -EFAULT;
246
Chao Yu2ace38e2014-12-24 16:08:14 +0800247 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700248 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900249
250 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500251 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900252
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300253 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900254 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900255 return -EFAULT;
256 }
Mike Christie04d328d2016-06-05 14:31:55 -0500257 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900258
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700259 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900260 return 0;
261}
262
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700263void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900264{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700265 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900266 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900267 struct f2fs_bio_info *io;
Mike Christie04d328d2016-06-05 14:31:55 -0500268 bool is_read = is_read_io(fio->op);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700269 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900270
Gu Zheng940a6d32013-12-20 17:39:59 +0800271 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900272
Chao Yud4511882018-06-05 17:44:11 +0800273 if (__is_valid_data_blkaddr(fio->old_blkaddr))
Yunlei He89d78cf2018-03-08 16:29:13 +0800274 verify_block_addr(fio, fio->old_blkaddr);
275 verify_block_addr(fio, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900276
Chao Yudf0f8dc2014-03-22 14:57:23 +0800277 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900278
Chao Yu7a9d7542016-02-22 18:36:38 +0800279 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Mike Christie04d328d2016-06-05 14:31:55 -0500280 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900281 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900282alloc_new:
283 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700284 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800285
Chao Yu7a9d7542016-02-22 18:36:38 +0800286 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
287 bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900288 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900289 }
290
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700291 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
292
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300293 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
294 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900295 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900296 goto alloc_new;
297 }
298
Chao Yu7a9d7542016-02-22 18:36:38 +0800299 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700300 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900301
Chao Yudf0f8dc2014-03-22 14:57:23 +0800302 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700303 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900304}
305
Chao Yu46008c62016-05-09 19:56:30 +0800306static void __set_data_blkaddr(struct dnode_of_data *dn)
307{
308 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
309 __le32 *addr_array;
310
311 /* Get physical address of data block */
312 addr_array = blkaddr_in_node(rn);
313 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
314}
315
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900316/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900317 * Lock ordering for the change of data block address:
318 * ->data_page
319 * ->node_page
320 * update block addresses in the node page
321 */
Chao Yu216a6202015-03-19 19:23:32 +0800322void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900323{
Chao Yu46008c62016-05-09 19:56:30 +0800324 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
325 __set_data_blkaddr(dn);
326 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800327 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900328}
329
Chao Yuf28b3432016-02-24 17:16:47 +0800330void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
331{
332 dn->data_blkaddr = blkaddr;
333 set_data_blkaddr(dn);
334 f2fs_update_extent_cache(dn);
335}
336
Chao Yu46008c62016-05-09 19:56:30 +0800337/* dn->ofs_in_node will be returned with up-to-date last block pointer */
338int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900339{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700340 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900341
Chao Yu46008c62016-05-09 19:56:30 +0800342 if (!count)
343 return 0;
344
Jaegeuk Kim91942322016-05-20 10:13:22 -0700345 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900346 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800347 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900348 return -ENOSPC;
349
Chao Yu46008c62016-05-09 19:56:30 +0800350 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
351 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900352
Chao Yu46008c62016-05-09 19:56:30 +0800353 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
354
355 for (; count > 0; dn->ofs_in_node++) {
356 block_t blkaddr =
357 datablock_addr(dn->node_page, dn->ofs_in_node);
358 if (blkaddr == NULL_ADDR) {
359 dn->data_blkaddr = NEW_ADDR;
360 __set_data_blkaddr(dn);
361 count--;
362 }
363 }
364
365 if (set_page_dirty(dn->node_page))
366 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900367 return 0;
368}
369
Chao Yu46008c62016-05-09 19:56:30 +0800370/* Should keep dn->ofs_in_node unchanged */
371int reserve_new_block(struct dnode_of_data *dn)
372{
373 unsigned int ofs_in_node = dn->ofs_in_node;
374 int ret;
375
376 ret = reserve_new_blocks(dn, 1);
377 dn->ofs_in_node = ofs_in_node;
378 return ret;
379}
380
Huajun Lib6009652013-11-10 23:13:18 +0800381int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
382{
383 bool need_put = dn->inode_page ? false : true;
384 int err;
385
386 err = get_dnode_of_data(dn, index, ALLOC_NODE);
387 if (err)
388 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900389
Huajun Lib6009652013-11-10 23:13:18 +0800390 if (dn->data_blkaddr == NULL_ADDR)
391 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900392 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800393 f2fs_put_dnode(dn);
394 return err;
395}
396
Fan Li759af1c2015-08-05 15:52:16 +0800397int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900398{
Chao Yu028a41e2015-03-19 19:26:02 +0800399 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800400 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800401
Fan Li759af1c2015-08-05 15:52:16 +0800402 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
403 dn->data_blkaddr = ei.blk + index - ei.fofs;
404 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800405 }
406
Fan Li759af1c2015-08-05 15:52:16 +0800407 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900408}
409
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700410struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500411 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900412{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900413 struct address_space *mapping = inode->i_mapping;
414 struct dnode_of_data dn;
415 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800416 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900417 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800418 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700419 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800420 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500421 .op = REQ_OP_READ,
422 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700423 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800424 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900425
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700426 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
427 return read_mapping_page(mapping, index, NULL);
428
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700429 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900430 if (!page)
431 return ERR_PTR(-ENOMEM);
432
Chao Yucb3bc9e2015-02-05 18:03:40 +0800433 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
434 dn.data_blkaddr = ei.blk + index - ei.fofs;
435 goto got_it;
436 }
437
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900438 set_new_dnode(&dn, inode, NULL, NULL, 0);
439 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700440 if (err)
441 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900442 f2fs_put_dnode(&dn);
443
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900444 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700445 err = -ENOENT;
446 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900447 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800448got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700449 if (PageUptodate(page)) {
450 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900451 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700452 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900453
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900454 /*
455 * A new dentry page is allocated but not able to be written, since its
456 * new inode page couldn't be allocated due to -ENOSPC.
457 * In such the case, its blkaddr can be remained as NEW_ADDR.
458 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
459 */
460 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300461 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700462 if (!PageUptodate(page))
463 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700464 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900465 return page;
466 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900467
Chao Yu7a9d7542016-02-22 18:36:38 +0800468 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700469 fio.page = page;
470 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900471 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700472 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700473 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700474
475put_err:
476 f2fs_put_page(page, 1);
477 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700478}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900479
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700480struct page *find_data_page(struct inode *inode, pgoff_t index)
481{
482 struct address_space *mapping = inode->i_mapping;
483 struct page *page;
484
485 page = find_get_page(mapping, index);
486 if (page && PageUptodate(page))
487 return page;
488 f2fs_put_page(page, 0);
489
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700490 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700491 if (IS_ERR(page))
492 return page;
493
494 if (PageUptodate(page))
495 return page;
496
497 wait_on_page_locked(page);
498 if (unlikely(!PageUptodate(page))) {
499 f2fs_put_page(page, 0);
500 return ERR_PTR(-EIO);
501 }
502 return page;
503}
504
505/*
506 * If it tries to access a hole, return an error.
507 * Because, the callers, functions in dir.c and GC, should be able to know
508 * whether this page exists or not.
509 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700510struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
511 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700512{
513 struct address_space *mapping = inode->i_mapping;
514 struct page *page;
515repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700516 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700517 if (IS_ERR(page))
518 return page;
519
520 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900521 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900522 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900523 f2fs_put_page(page, 1);
524 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900525 }
Chao Yu1563ac72016-07-03 22:05:12 +0800526 if (unlikely(!PageUptodate(page))) {
527 f2fs_put_page(page, 1);
528 return ERR_PTR(-EIO);
529 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900530 return page;
531}
532
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900533/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900534 * Caller ensures that this data page is never allocated.
535 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900536 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800537 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
538 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800539 * Note that, ipage is set only by make_empty_dir, and if any error occur,
540 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900541 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900542struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900543 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900544{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900545 struct address_space *mapping = inode->i_mapping;
546 struct page *page;
547 struct dnode_of_data dn;
548 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800549
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700550 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800551 if (!page) {
552 /*
553 * before exiting, we should make sure ipage will be released
554 * if any error occur.
555 */
556 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700557 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800558 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900559
Jaegeuk Kima8865372013-12-27 17:04:17 +0900560 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800561 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700562 if (err) {
563 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900564 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900565 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700566 if (!ipage)
567 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900568
569 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700570 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900571
572 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300573 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700574 if (!PageUptodate(page))
575 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900576 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700577 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900578
Jaegeuk Kim76121182016-01-01 22:03:47 -0800579 /* if ipage exists, blkaddr should be NEW_ADDR */
580 f2fs_bug_on(F2FS_I_SB(inode), ipage);
581 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700582 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800583 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900584 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700585got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800586 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700587 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700588 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900589 return page;
590}
591
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900592static int __allocate_data_block(struct dnode_of_data *dn)
593{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700594 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900595 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900596 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800597 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700598 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800599 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900600
Jaegeuk Kim91942322016-05-20 10:13:22 -0700601 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900602 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800603
604 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
605 if (dn->data_blkaddr == NEW_ADDR)
606 goto alloc;
607
Chao Yu46008c62016-05-09 19:56:30 +0800608 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900609 return -ENOSPC;
610
Chao Yudf6136e2015-03-23 10:33:37 +0800611alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900612 get_node_info(sbi, dn->nid, &ni);
613 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
614
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800615 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
616 seg = CURSEG_DIRECT_IO;
617
Chao Yudf6136e2015-03-23 10:33:37 +0800618 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
619 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800620 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900621
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700622 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800623 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700624 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300625 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700626 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300627 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900628 return 0;
629}
630
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800631ssize_t f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800632{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800633 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800634 struct f2fs_map_blocks map;
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800635 ssize_t ret = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800636
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700637 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800638 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
639 if (map.m_len > map.m_lblk)
640 map.m_len -= map.m_lblk;
641 else
642 map.m_len = 0;
643
Chao Yuda859852016-01-26 15:42:58 +0800644 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800645
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800646 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800647 ret = f2fs_convert_inline_inode(inode);
648 if (ret)
649 return ret;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800650 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800651 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800652 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
653 ret = f2fs_convert_inline_inode(inode);
654 if (ret)
655 return ret;
656 }
657 if (!f2fs_has_inline_data(inode))
658 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800659 return ret;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800660}
661
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900662/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700663 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
664 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800665 * If original data blocks are allocated, then give them to blockdev.
666 * Otherwise,
667 * a. preallocate requested block addresses
668 * b. do not use extent cache for better performance
669 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900670 */
Chao Yud323d002015-10-27 09:53:45 +0800671int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800672 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900673{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700674 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900675 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800676 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700677 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800678 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900679 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800680 unsigned int ofs_in_node, last_ofs_in_node;
681 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800682 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900683 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800684 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900685
Chao Yudfd02e42016-08-20 15:12:01 +0800686 if (!maxblocks)
687 return 0;
688
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700689 map->m_len = 0;
690 map->m_flags = 0;
691
692 /* it only supports block size == page size */
693 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800694 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900695
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800696 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700697 map->m_pblk = ei.blk + pgofs - ei.fofs;
698 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
699 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900700 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800701 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900702
Chao Yu4fe71e82016-01-26 15:37:38 +0800703next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800704 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800705 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900706
707 /* When reading holes, we need its node page */
708 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900709 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900710 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800711 if (flag == F2FS_GET_BLOCK_BMAP)
712 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800713 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900714 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800715 if (map->m_next_pgofs)
716 *map->m_next_pgofs =
717 get_next_page_offset(&dn, pgofs);
718 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900719 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900720 }
Chao Yu973163f2015-09-18 16:51:51 +0800721
Chao Yu46008c62016-05-09 19:56:30 +0800722 prealloc = 0;
Arnd Bergmanna299abd2016-11-02 14:52:15 +0100723 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800724 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900725
Chao Yu4fe71e82016-01-26 15:37:38 +0800726next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800727 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800728
Chao Yuaafb3712018-08-01 19:13:44 +0800729 if (__is_valid_data_blkaddr(blkaddr) &&
730 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) {
731 err = -EFAULT;
732 goto sync_out;
733 }
734
Chao Yud4511882018-06-05 17:44:11 +0800735 if (!is_valid_data_blkaddr(sbi, blkaddr)) {
Fan Li7df3a432015-12-17 13:20:59 +0800736 if (create) {
737 if (unlikely(f2fs_cp_error(sbi))) {
738 err = -EIO;
739 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800740 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800741 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800742 if (blkaddr == NULL_ADDR) {
743 prealloc++;
744 last_ofs_in_node = dn.ofs_in_node;
745 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800746 } else {
747 err = __allocate_data_block(&dn);
Chao Yu46008c62016-05-09 19:56:30 +0800748 if (!err) {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700749 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yu46008c62016-05-09 19:56:30 +0800750 allocated = true;
751 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800752 }
Fan Li7df3a432015-12-17 13:20:59 +0800753 if (err)
754 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800755 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800756 blkaddr = dn.data_blkaddr;
757 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800758 if (flag == F2FS_GET_BLOCK_BMAP) {
759 map->m_pblk = 0;
760 goto sync_out;
761 }
Chao Yuda859852016-01-26 15:42:58 +0800762 if (flag == F2FS_GET_BLOCK_FIEMAP &&
763 blkaddr == NULL_ADDR) {
764 if (map->m_next_pgofs)
765 *map->m_next_pgofs = pgofs + 1;
766 }
Fan Li7df3a432015-12-17 13:20:59 +0800767 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800768 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800769 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900770 }
771 }
Fan Li7df3a432015-12-17 13:20:59 +0800772
Chao Yu46008c62016-05-09 19:56:30 +0800773 if (flag == F2FS_GET_BLOCK_PRE_AIO)
774 goto skip;
775
Chao Yu4fe71e82016-01-26 15:37:38 +0800776 if (map->m_len == 0) {
777 /* preallocated unwritten block should be mapped for fiemap. */
778 if (blkaddr == NEW_ADDR)
779 map->m_flags |= F2FS_MAP_UNWRITTEN;
780 map->m_flags |= F2FS_MAP_MAPPED;
781
782 map->m_pblk = blkaddr;
783 map->m_len = 1;
784 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800785 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800786 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800787 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800788 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800789 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800790 } else {
791 goto sync_out;
792 }
793
Chao Yu46008c62016-05-09 19:56:30 +0800794skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800795 dn.ofs_in_node++;
796 pgofs++;
797
Chao Yu46008c62016-05-09 19:56:30 +0800798 /* preallocate blocks in batch for one dnode page */
799 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
800 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800801
Chao Yu46008c62016-05-09 19:56:30 +0800802 dn.ofs_in_node = ofs_in_node;
803 err = reserve_new_blocks(&dn, prealloc);
804 if (err)
805 goto sync_out;
Chao Yu58383be2016-08-20 15:12:02 +0800806 allocated = dn.node_changed;
Chao Yu4fe71e82016-01-26 15:37:38 +0800807
Chao Yu46008c62016-05-09 19:56:30 +0800808 map->m_len += dn.ofs_in_node - ofs_in_node;
809 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
810 err = -ENOSPC;
811 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800812 }
Chao Yu46008c62016-05-09 19:56:30 +0800813 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800814 }
815
Chao Yu46008c62016-05-09 19:56:30 +0800816 if (pgofs >= end)
817 goto sync_out;
818 else if (dn.ofs_in_node < end_offset)
819 goto next_block;
820
Chao Yu46008c62016-05-09 19:56:30 +0800821 f2fs_put_dnode(&dn);
822
823 if (create) {
824 f2fs_unlock_op(sbi);
825 f2fs_balance_fs(sbi, allocated);
826 }
827 allocated = false;
828 goto next_dnode;
829
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900830sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900831 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900832unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800833 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800834 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800835 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800836 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900837out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700838 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900839 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900840}
841
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700842static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800843 struct buffer_head *bh, int create, int flag,
844 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700845{
846 struct f2fs_map_blocks map;
847 int ret;
848
849 map.m_lblk = iblock;
850 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800851 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700852
Chao Yue2b4e2b2015-08-19 19:11:19 +0800853 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700854 if (!ret) {
855 map_bh(bh, inode->i_sb, map.m_pblk);
856 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb8bf4b82017-01-22 12:21:02 +0800857 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700858 }
859 return ret;
860}
861
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900862static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800863 struct buffer_head *bh_result, int create, int flag,
864 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900865{
Chao Yuda859852016-01-26 15:42:58 +0800866 return __get_data_block(inode, iblock, bh_result, create,
867 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900868}
869
Chao Yue2b4e2b2015-08-19 19:11:19 +0800870static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900871 struct buffer_head *bh_result, int create)
872{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800873 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800874 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800875}
876
877static int get_data_block_bmap(struct inode *inode, sector_t iblock,
878 struct buffer_head *bh_result, int create)
879{
Yunlei He179448b2015-12-28 21:48:32 +0800880 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800881 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800882 return -EFBIG;
883
Chao Yue2b4e2b2015-08-19 19:11:19 +0800884 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800885 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900886}
887
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700888static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
889{
890 return (offset >> inode->i_blkbits);
891}
892
893static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
894{
895 return (blk << inode->i_blkbits);
896}
897
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900898int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
899 u64 start, u64 len)
900{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700901 struct buffer_head map_bh;
902 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800903 pgoff_t next_pgofs;
Fan Lide1475c2016-01-04 15:56:50 +0800904 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700905 u64 logical = 0, phys = 0, size = 0;
906 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700907 int ret = 0;
908
909 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
910 if (ret)
911 return ret;
912
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -0700913 if (f2fs_has_inline_data(inode)) {
914 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
915 if (ret != -EAGAIN)
916 return ret;
917 }
918
Al Viro59551022016-01-22 15:40:57 -0500919 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800920
921 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800922 if (start >= isize)
923 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700924
Fan Li9a950d52015-12-26 18:07:41 +0800925 if (start + len > isize)
926 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700927
928 if (logical_to_blk(inode, len) == 0)
929 len = blk_to_logical(inode, 1);
930
931 start_blk = logical_to_blk(inode, start);
932 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800933
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700934next:
935 memset(&map_bh, 0, sizeof(struct buffer_head));
936 map_bh.b_size = len;
937
Chao Yue2b4e2b2015-08-19 19:11:19 +0800938 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800939 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700940 if (ret)
941 goto out;
942
943 /* HOLE */
944 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800945 start_blk = next_pgofs;
Fan Li9a950d52015-12-26 18:07:41 +0800946 /* Go through holes util pass the EOF */
Chao Yuda859852016-01-26 15:42:58 +0800947 if (blk_to_logical(inode, start_blk) < isize)
Fan Li9a950d52015-12-26 18:07:41 +0800948 goto prep_next;
949 /* Found a hole beyond isize means no more extents.
950 * Note that the premise is that filesystems don't
951 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700952 */
Fan Li9a950d52015-12-26 18:07:41 +0800953 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700954 }
Fan Li9a950d52015-12-26 18:07:41 +0800955
Chao Yuda5af122016-01-08 20:19:27 +0800956 if (size) {
957 if (f2fs_encrypted_inode(inode))
958 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
959
Fan Li9a950d52015-12-26 18:07:41 +0800960 ret = fiemap_fill_next_extent(fieinfo, logical,
961 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800962 }
Fan Li9a950d52015-12-26 18:07:41 +0800963
964 if (start_blk > last_blk || ret)
965 goto out;
966
967 logical = blk_to_logical(inode, start_blk);
968 phys = blk_to_logical(inode, map_bh.b_blocknr);
969 size = map_bh.b_size;
970 flags = 0;
971 if (buffer_unwritten(&map_bh))
972 flags = FIEMAP_EXTENT_UNWRITTEN;
973
974 start_blk += logical_to_blk(inode, size);
975
976prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700977 cond_resched();
978 if (fatal_signal_pending(current))
979 ret = -EINTR;
980 else
981 goto next;
982out:
983 if (ret == 1)
984 ret = 0;
985
Al Viro59551022016-01-22 15:40:57 -0500986 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700987 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900988}
989
Wei Yongjun6a7a3ae2016-08-23 15:23:59 +0000990static struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
991 unsigned nr_pages)
Chao Yu78682f72016-07-03 22:05:11 +0800992{
993 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
994 struct fscrypt_ctx *ctx = NULL;
995 struct block_device *bdev = sbi->sb->s_bdev;
996 struct bio *bio;
997
Chao Yub10a6ac2018-07-10 23:01:45 +0800998 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
999 return ERR_PTR(-EFAULT);
1000
Chao Yu78682f72016-07-03 22:05:11 +08001001 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1002 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
1003 if (IS_ERR(ctx))
1004 return ERR_CAST(ctx);
1005
1006 /* wait the page to be moved by cleaning */
1007 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
1008 }
1009
1010 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
1011 if (!bio) {
1012 if (ctx)
1013 fscrypt_release_ctx(ctx);
1014 return ERR_PTR(-ENOMEM);
1015 }
1016 bio->bi_bdev = bdev;
1017 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blkaddr);
1018 bio->bi_end_io = f2fs_read_end_io;
1019 bio->bi_private = ctx;
1020
1021 return bio;
1022}
1023
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001024/*
1025 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1026 * Major change was from block_size == page_size in f2fs by default.
1027 */
1028static int f2fs_mpage_readpages(struct address_space *mapping,
1029 struct list_head *pages, struct page *page,
1030 unsigned nr_pages)
1031{
1032 struct bio *bio = NULL;
1033 unsigned page_idx;
1034 sector_t last_block_in_bio = 0;
1035 struct inode *inode = mapping->host;
1036 const unsigned blkbits = inode->i_blkbits;
1037 const unsigned blocksize = 1 << blkbits;
1038 sector_t block_in_file;
1039 sector_t last_block;
1040 sector_t last_block_in_file;
1041 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001042 struct f2fs_map_blocks map;
1043
1044 map.m_pblk = 0;
1045 map.m_lblk = 0;
1046 map.m_len = 0;
1047 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001048 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001049
1050 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1051
1052 prefetchw(&page->flags);
1053 if (pages) {
1054 page = list_entry(pages->prev, struct page, lru);
1055 list_del(&page->lru);
1056 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001057 page->index,
1058 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001059 goto next_page;
1060 }
1061
1062 block_in_file = (sector_t)page->index;
1063 last_block = block_in_file + nr_pages;
1064 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1065 blkbits;
1066 if (last_block > last_block_in_file)
1067 last_block = last_block_in_file;
1068
1069 /*
1070 * Map blocks using the previous result first.
1071 */
1072 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1073 block_in_file > map.m_lblk &&
1074 block_in_file < (map.m_lblk + map.m_len))
1075 goto got_it;
1076
1077 /*
1078 * Then do more f2fs_map_blocks() calls until we are
1079 * done with this page.
1080 */
1081 map.m_flags = 0;
1082
1083 if (block_in_file < last_block) {
1084 map.m_lblk = block_in_file;
1085 map.m_len = last_block - block_in_file;
1086
Chao Yu46c9e142015-09-18 16:54:16 +08001087 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001088 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001089 goto set_error_page;
1090 }
1091got_it:
1092 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1093 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1094 SetPageMappedToDisk(page);
1095
1096 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1097 SetPageUptodate(page);
1098 goto confused;
1099 }
Chao Yuaafb3712018-08-01 19:13:44 +08001100
1101 if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr,
1102 DATA_GENERIC))
1103 goto set_error_page;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001104 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001105 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001106 if (!PageUptodate(page))
1107 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001108 unlock_page(page);
1109 goto next_page;
1110 }
1111
1112 /*
1113 * This page will go to BIO. Do we need to send this
1114 * BIO off first?
1115 */
1116 if (bio && (last_block_in_bio != block_nr - 1)) {
1117submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001118 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001119 bio = NULL;
1120 }
1121 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001122 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001123 if (IS_ERR(bio)) {
1124 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001125 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001126 }
Mike Christie04d328d2016-06-05 14:31:55 -05001127 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001128 }
1129
1130 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1131 goto submit_and_realloc;
1132
1133 last_block_in_bio = block_nr;
1134 goto next_page;
1135set_error_page:
1136 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001137 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001138 unlock_page(page);
1139 goto next_page;
1140confused:
1141 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001142 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001143 bio = NULL;
1144 }
1145 unlock_page(page);
1146next_page:
1147 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001148 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001149 }
1150 BUG_ON(pages && !list_empty(pages));
1151 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001152 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001153 return 0;
1154}
1155
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001156static int f2fs_read_data_page(struct file *file, struct page *page)
1157{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001158 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001159 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001160
Chao Yuc20e89c2014-05-06 16:53:08 +08001161 trace_f2fs_readpage(page, DATA);
1162
arter97e1c42042014-08-06 23:22:50 +09001163 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001164 if (f2fs_has_inline_data(inode))
1165 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001166 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001167 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001168 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001169}
1170
1171static int f2fs_read_data_pages(struct file *file,
1172 struct address_space *mapping,
1173 struct list_head *pages, unsigned nr_pages)
1174{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001175 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001176 struct page *page = list_entry(pages->prev, struct page, lru);
1177
1178 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001179
1180 /* If the file has inline data, skip readpages */
1181 if (f2fs_has_inline_data(inode))
1182 return 0;
1183
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001184 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001185}
1186
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001187int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001188{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001189 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001190 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001191 struct dnode_of_data dn;
1192 int err = 0;
1193
1194 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001195 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001196 if (err)
1197 return err;
1198
Chao Yu28bc1062016-02-06 14:40:34 +08001199 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001200
1201 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001202 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001203 ClearPageUptodate(page);
Chao Yu02fe0b22018-07-27 18:15:16 +08001204 clear_cold_data(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001205 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001206 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001207
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001208 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001209 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001210
1211 /* wait for GCed encrypted page writeback */
1212 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001213 fio->old_blkaddr);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001214retry_encrypt:
1215 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1216 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001217 if (IS_ERR(fio->encrypted_page)) {
1218 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001219 if (err == -ENOMEM) {
1220 /* flush pending ios and wait for a while */
1221 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1222 congestion_wait(BLK_RW_ASYNC, HZ/50);
1223 gfp_flags |= __GFP_NOFAIL;
1224 err = 0;
1225 goto retry_encrypt;
1226 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001227 goto out_writepage;
1228 }
1229 }
1230
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001231 set_page_writeback(page);
1232
Chao Yuaafb3712018-08-01 19:13:44 +08001233 if (__is_valid_data_blkaddr(fio->old_blkaddr) &&
1234 !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr,
1235 DATA_GENERIC)) {
1236 err = -EFAULT;
1237 goto out_writepage;
1238 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001239 /*
1240 * If current allocation needs SSR,
1241 * it had better in-place writes for updated data.
1242 */
Chao Yud4511882018-06-05 17:44:11 +08001243 if (unlikely(is_valid_data_blkaddr(fio->sbi, fio->old_blkaddr) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001244 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001245 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001246 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001247 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001248 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001249 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001250 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001251 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001252 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001253 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001254 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001255 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001256 }
1257out_writepage:
1258 f2fs_put_dnode(&dn);
1259 return err;
1260}
1261
1262static int f2fs_write_data_page(struct page *page,
1263 struct writeback_control *wbc)
1264{
1265 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001266 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001267 loff_t i_size = i_size_read(inode);
1268 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001269 >> PAGE_SHIFT;
Chao Yu85319362019-11-07 17:29:00 +08001270 loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001271 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001272 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001273 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001274 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001275 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001276 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001277 .op = REQ_OP_WRITE,
1278 .op_flags = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : 0,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001279 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001280 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001281 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001282
Chao Yuecda0de2014-05-06 16:48:26 +08001283 trace_f2fs_writepage(page, DATA);
1284
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001285 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001286 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001287
1288 /*
1289 * If the offset is out-of-range of file size,
1290 * this page does not have to be written to disk.
1291 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001292 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001293 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001294 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001295
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001296 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001297write:
Chao Yucaf00472015-01-28 17:48:42 +08001298 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001299 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001300 if (f2fs_is_drop_cache(inode))
1301 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001302 /* we should not write 0'th page having journal header */
1303 if (f2fs_is_volatile_file(inode) && (!page->index ||
1304 (!wbc->for_reclaim &&
1305 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001306 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001307
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001308 /* we should bypass data pages to proceed the kworkder jobs */
1309 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001310 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001311 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001312 }
1313
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001314 /* Dentry blocks are controlled by checkpoint */
1315 if (S_ISDIR(inode->i_mode)) {
1316 err = do_write_data_page(&fio);
1317 goto done;
1318 }
1319
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001320 if (!wbc->for_reclaim)
1321 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001322 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001323 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001324
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001325 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001326 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001327 if (f2fs_has_inline_data(inode))
1328 err = f2fs_write_inline_data(inode, page);
1329 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001330 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001331 if (F2FS_I(inode)->last_disk_size < psize)
1332 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001333 f2fs_unlock_op(sbi);
1334done:
1335 if (err && err != -ENOENT)
1336 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001337
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001338 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001339out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001340 inode_dec_dirty_pages(inode);
Chao Yu02fe0b22018-07-27 18:15:16 +08001341 if (err) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001342 ClearPageUptodate(page);
Chao Yu02fe0b22018-07-27 18:15:16 +08001343 clear_cold_data(page);
1344 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001345
1346 if (wbc->for_reclaim) {
1347 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001348 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001349 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001350
1351 unlock_page(page);
1352 f2fs_balance_fs(sbi, need_balance_fs);
1353
1354 if (unlikely(f2fs_cp_error(sbi)))
1355 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1356
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001357 return 0;
1358
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001359redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001360 redirty_page_for_writepage(wbc, page);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001361 unlock_page(page);
1362 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001363}
1364
Chao Yu8f46dca2015-07-14 18:56:10 +08001365/*
1366 * This function was copied from write_cche_pages from mm/page-writeback.c.
1367 * The major change is making write step of cold data page separately from
1368 * warm/hot data page.
1369 */
1370static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001371 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001372{
1373 int ret = 0;
1374 int done = 0;
1375 struct pagevec pvec;
1376 int nr_pages;
1377 pgoff_t uninitialized_var(writeback_index);
1378 pgoff_t index;
1379 pgoff_t end; /* Inclusive */
1380 pgoff_t done_index;
1381 int cycled;
1382 int range_whole = 0;
1383 int tag;
Chao Yu6ca56ca2016-09-29 18:50:11 +08001384 int nwritten = 0;
Chao Yu8f46dca2015-07-14 18:56:10 +08001385
1386 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001387
Chao Yu8f46dca2015-07-14 18:56:10 +08001388 if (wbc->range_cyclic) {
1389 writeback_index = mapping->writeback_index; /* prev offset */
1390 index = writeback_index;
1391 if (index == 0)
1392 cycled = 1;
1393 else
1394 cycled = 0;
1395 end = -1;
1396 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001397 index = wbc->range_start >> PAGE_SHIFT;
1398 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001399 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1400 range_whole = 1;
1401 cycled = 1; /* ignore range_cyclic tests */
1402 }
1403 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1404 tag = PAGECACHE_TAG_TOWRITE;
1405 else
1406 tag = PAGECACHE_TAG_DIRTY;
1407retry:
1408 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1409 tag_pages_for_writeback(mapping, index, end);
1410 done_index = index;
1411 while (!done && (index <= end)) {
1412 int i;
1413
1414 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1415 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1416 if (nr_pages == 0)
1417 break;
1418
1419 for (i = 0; i < nr_pages; i++) {
1420 struct page *page = pvec.pages[i];
1421
1422 if (page->index > end) {
1423 done = 1;
1424 break;
1425 }
1426
1427 done_index = page->index;
1428
1429 lock_page(page);
1430
1431 if (unlikely(page->mapping != mapping)) {
1432continue_unlock:
1433 unlock_page(page);
1434 continue;
1435 }
1436
1437 if (!PageDirty(page)) {
1438 /* someone wrote it for us */
1439 goto continue_unlock;
1440 }
1441
Chao Yu8f46dca2015-07-14 18:56:10 +08001442 if (PageWriteback(page)) {
1443 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001444 f2fs_wait_on_page_writeback(page,
1445 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001446 else
1447 goto continue_unlock;
1448 }
1449
1450 BUG_ON(PageWriteback(page));
1451 if (!clear_page_dirty_for_io(page))
1452 goto continue_unlock;
1453
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001454 ret = mapping->a_ops->writepage(page, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001455 if (unlikely(ret)) {
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001456 done_index = page->index + 1;
1457 done = 1;
1458 break;
Chao Yu6ca56ca2016-09-29 18:50:11 +08001459 } else {
1460 nwritten++;
Chao Yu8f46dca2015-07-14 18:56:10 +08001461 }
1462
1463 if (--wbc->nr_to_write <= 0 &&
1464 wbc->sync_mode == WB_SYNC_NONE) {
1465 done = 1;
1466 break;
1467 }
1468 }
1469 pagevec_release(&pvec);
1470 cond_resched();
1471 }
1472
Chao Yu8f46dca2015-07-14 18:56:10 +08001473 if (!cycled && !done) {
1474 cycled = 1;
1475 index = 0;
1476 end = writeback_index - 1;
1477 goto retry;
1478 }
1479 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1480 mapping->writeback_index = done_index;
1481
Chao Yu6ca56ca2016-09-29 18:50:11 +08001482 if (nwritten)
1483 f2fs_submit_merged_bio_cond(F2FS_M_SB(mapping), mapping->host,
1484 NULL, 0, DATA, WRITE);
1485
Chao Yu8f46dca2015-07-14 18:56:10 +08001486 return ret;
1487}
1488
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001489static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001490 struct writeback_control *wbc)
1491{
1492 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001493 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001494 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001495 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001496
P J Pcfb185a2013-04-03 11:38:00 +09001497 /* deal with chardevs and other special file */
1498 if (!mapping->a_ops->writepage)
1499 return 0;
1500
Chao Yu6a290542015-07-17 18:02:39 +08001501 /* skip writing if there is no dirty page in this inode */
1502 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1503 return 0;
1504
Jaegeuk Kima1257022015-10-08 10:40:07 -07001505 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1506 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1507 available_free_memory(sbi, DIRTY_DENTS))
1508 goto skip_write;
1509
Chao Yud323d002015-10-27 09:53:45 +08001510 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001511 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001512 goto skip_write;
1513
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001514 /* during POR, we don't need to trigger writepage at all. */
1515 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1516 goto skip_write;
1517
Yunlei Hed31c7c32016-02-04 16:14:00 +08001518 trace_f2fs_writepages(mapping->host, wbc, DATA);
1519
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001520 blk_start_plug(&plug);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001521 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001522 blk_finish_plug(&plug);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001523 /*
1524 * if some pages were truncated, we cannot guarantee its mapping->host
1525 * to detect pending bios.
1526 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001527
Chao Yuc227f912015-12-16 13:09:20 +08001528 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001529 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001530
1531skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001532 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001533 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001534 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001535}
1536
Chao Yu3aab8f82014-07-02 13:25:04 +08001537static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1538{
1539 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001540 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001541
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001542 if (to > i_size) {
1543 truncate_pagecache(inode, i_size);
1544 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001545 }
1546}
1547
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001548static int prepare_write_begin(struct f2fs_sb_info *sbi,
1549 struct page *page, loff_t pos, unsigned len,
1550 block_t *blk_addr, bool *node_changed)
1551{
1552 struct inode *inode = page->mapping->host;
1553 pgoff_t index = page->index;
1554 struct dnode_of_data dn;
1555 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001556 bool locked = false;
1557 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001558 int err = 0;
1559
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001560 /*
1561 * we already allocated all the blocks, so we don't need to get
1562 * the block addresses when there is no need to fill the page.
1563 */
Yunlei He5d4c0af2016-09-18 08:16:56 +08001564 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001565 return 0;
1566
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001567 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001568 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001569 f2fs_lock_op(sbi);
1570 locked = true;
1571 }
1572restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001573 /* check inline_data */
1574 ipage = get_node_page(sbi, inode->i_ino);
1575 if (IS_ERR(ipage)) {
1576 err = PTR_ERR(ipage);
1577 goto unlock_out;
1578 }
1579
1580 set_new_dnode(&dn, inode, ipage, ipage, 0);
1581
1582 if (f2fs_has_inline_data(inode)) {
1583 if (pos + len <= MAX_INLINE_DATA) {
1584 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001585 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001586 if (inode->i_nlink)
1587 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001588 } else {
1589 err = f2fs_convert_inline_page(&dn, page);
1590 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001591 goto out;
1592 if (dn.data_blkaddr == NULL_ADDR)
1593 err = f2fs_get_block(&dn, index);
1594 }
1595 } else if (locked) {
1596 err = f2fs_get_block(&dn, index);
1597 } else {
1598 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1599 dn.data_blkaddr = ei.blk + index - ei.fofs;
1600 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001601 /* hole case */
1602 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001603 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001604 f2fs_put_dnode(&dn);
1605 f2fs_lock_op(sbi);
1606 locked = true;
1607 goto restart;
1608 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001609 }
1610 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001611
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001612 /* convert_inline_page can make node_changed */
1613 *blk_addr = dn.data_blkaddr;
1614 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001615out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001616 f2fs_put_dnode(&dn);
1617unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001618 if (locked)
1619 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001620 return err;
1621}
1622
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001623static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1624 loff_t pos, unsigned len, unsigned flags,
1625 struct page **pagep, void **fsdata)
1626{
1627 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001628 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001629 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001630 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001631 bool need_balance = false;
1632 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001633 int err = 0;
1634
Chao Yu62aed042014-05-06 16:46:04 +08001635 trace_f2fs_write_begin(inode, pos, len, flags);
1636
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001637 /*
1638 * We should check this at this moment to avoid deadlock on inode page
1639 * and #0 page. The locking rule for inline_data conversion should be:
1640 * lock_page(page #0) -> lock_page(inode_page)
1641 */
1642 if (index != 0) {
1643 err = f2fs_convert_inline_inode(inode);
1644 if (err)
1645 goto fail;
1646 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001647repeat:
Jaegeuk Kime9afe7c2017-02-17 09:55:55 -08001648 /*
1649 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1650 * wait_for_stable_page. Will wait that below with our IO control.
1651 */
1652 page = pagecache_get_page(mapping, index,
1653 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08001654 if (!page) {
1655 err = -ENOMEM;
1656 goto fail;
1657 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001658
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001659 *pagep = page;
1660
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001661 err = prepare_write_begin(sbi, page, pos, len,
1662 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001663 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001664 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001665
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001666 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001667 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001668 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001669 lock_page(page);
1670 if (page->mapping != mapping) {
1671 /* The page got truncated from under us */
1672 f2fs_put_page(page, 1);
1673 goto repeat;
1674 }
1675 }
1676
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001677 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001678
Chao Yu08b39fb2015-10-08 13:27:34 +08001679 /* wait for GCed encrypted page writeback */
1680 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001681 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001682
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001683 if (len == PAGE_SIZE || PageUptodate(page))
1684 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001685
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001686 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001687 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001688 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001689 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001690 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001691
Chao Yu78682f72016-07-03 22:05:11 +08001692 bio = f2fs_grab_bio(inode, blkaddr, 1);
1693 if (IS_ERR(bio)) {
1694 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001695 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001696 }
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001697 bio_set_op_attrs(bio, REQ_OP_READ, READ_SYNC);
Chao Yu78682f72016-07-03 22:05:11 +08001698 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1699 bio_put(bio);
1700 err = -EFAULT;
1701 goto fail;
1702 }
1703
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001704 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001705
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001706 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001707 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001708 f2fs_put_page(page, 1);
1709 goto repeat;
1710 }
Chao Yu1563ac72016-07-03 22:05:12 +08001711 if (unlikely(!PageUptodate(page))) {
1712 err = -EIO;
1713 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001714 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001715 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001716 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001717
Chao Yu3aab8f82014-07-02 13:25:04 +08001718fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001719 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001720 f2fs_write_failed(mapping, pos + len);
1721 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001722}
1723
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001724static int f2fs_write_end(struct file *file,
1725 struct address_space *mapping,
1726 loff_t pos, unsigned len, unsigned copied,
1727 struct page *page, void *fsdata)
1728{
1729 struct inode *inode = page->mapping->host;
1730
Chao Yudfb2bf32014-05-06 16:47:23 +08001731 trace_f2fs_write_end(inode, pos, len, copied);
1732
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001733 /*
1734 * This should be come from len == PAGE_SIZE, and we expect copied
1735 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
1736 * let generic_perform_write() try to copy data again through copied=0.
1737 */
1738 if (!PageUptodate(page)) {
1739 if (unlikely(copied != PAGE_SIZE))
1740 copied = 0;
1741 else
1742 SetPageUptodate(page);
1743 }
1744 if (!copied)
1745 goto unlock_out;
1746
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001747 set_page_dirty(page);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001748 clear_cold_data(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001749
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001750 if (pos + copied > i_size_read(inode))
1751 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07001752unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08001753 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001754 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001755 return copied;
1756}
1757
Omar Sandoval6f673762015-03-16 04:33:52 -07001758static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1759 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001760{
1761 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001762
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001763 if (offset & blocksize_mask)
1764 return -EINVAL;
1765
Al Viro5b46f252014-03-16 18:07:34 -04001766 if (iov_iter_alignment(iter) & blocksize_mask)
1767 return -EINVAL;
1768
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001769 return 0;
1770}
1771
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001772static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001773{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001774 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001775 struct inode *inode = mapping->host;
1776 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001777 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08001778 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08001779 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001780
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001781 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001782 if (err)
1783 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001784
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001785 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1786 return 0;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001787 if (test_opt(F2FS_I_SB(inode), LFS))
1788 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001789
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001790 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08001791
Chao Yu82e0a5a2016-07-13 09:18:29 +08001792 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001793 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08001794 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
1795
1796 if (rw == WRITE) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001797 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001798 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001799 else if (err < 0)
1800 f2fs_write_failed(mapping, offset + count);
1801 }
Chao Yu70407fa2014-07-31 21:11:22 +08001802
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001803 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08001804
Chao Yu3aab8f82014-07-02 13:25:04 +08001805 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001806}
1807
Chao Yu487261f2015-02-05 17:44:29 +08001808void f2fs_invalidate_page(struct page *page, unsigned int offset,
1809 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001810{
1811 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001812 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001813
Chao Yu487261f2015-02-05 17:44:29 +08001814 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001815 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001816 return;
1817
Chao Yu487261f2015-02-05 17:44:29 +08001818 if (PageDirty(page)) {
1819 if (inode->i_ino == F2FS_META_INO(sbi))
1820 dec_page_count(sbi, F2FS_DIRTY_META);
1821 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1822 dec_page_count(sbi, F2FS_DIRTY_NODES);
1823 else
1824 inode_dec_dirty_pages(inode);
1825 }
Chao Yudecd36b2015-08-07 18:42:09 +08001826
Chao Yu02fe0b22018-07-27 18:15:16 +08001827 clear_cold_data(page);
1828
Chao Yudecd36b2015-08-07 18:42:09 +08001829 /* This is atomic written page, keep Private */
1830 if (IS_ATOMIC_WRITTEN_PAGE(page))
1831 return;
1832
Chao Yu23dc9742016-04-29 20:09:15 +08001833 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001834 ClearPagePrivate(page);
1835}
1836
Chao Yu487261f2015-02-05 17:44:29 +08001837int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001838{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001839 /* If this is dirty page, keep PagePrivate */
1840 if (PageDirty(page))
1841 return 0;
1842
Chao Yudecd36b2015-08-07 18:42:09 +08001843 /* This is atomic written page, keep Private */
1844 if (IS_ATOMIC_WRITTEN_PAGE(page))
1845 return 0;
1846
Chao Yu02fe0b22018-07-27 18:15:16 +08001847 clear_cold_data(page);
Chao Yu23dc9742016-04-29 20:09:15 +08001848 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001849 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001850 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001851}
1852
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001853/*
1854 * This was copied from __set_page_dirty_buffers which gives higher performance
1855 * in very high speed storages. (e.g., pmem)
1856 */
1857void f2fs_set_page_dirty_nobuffers(struct page *page)
1858{
1859 struct address_space *mapping = page->mapping;
1860 unsigned long flags;
1861
1862 if (unlikely(!mapping))
1863 return;
1864
1865 spin_lock(&mapping->private_lock);
1866 lock_page_memcg(page);
1867 SetPageDirty(page);
1868 spin_unlock(&mapping->private_lock);
1869
1870 spin_lock_irqsave(&mapping->tree_lock, flags);
1871 WARN_ON_ONCE(!PageUptodate(page));
1872 account_page_dirtied(page, mapping);
1873 radix_tree_tag_set(&mapping->page_tree,
1874 page_index(page), PAGECACHE_TAG_DIRTY);
1875 spin_unlock_irqrestore(&mapping->tree_lock, flags);
1876 unlock_page_memcg(page);
1877
1878 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1879 return;
1880}
1881
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001882static int f2fs_set_data_page_dirty(struct page *page)
1883{
1884 struct address_space *mapping = page->mapping;
1885 struct inode *inode = mapping->host;
1886
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001887 trace_f2fs_set_page_dirty(page, DATA);
1888
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001889 if (!PageUptodate(page))
1890 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001891
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001892 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001893 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1894 register_inmem_page(inode, page);
1895 return 1;
1896 }
1897 /*
1898 * Previously, this page has been registered, we just
1899 * return here.
1900 */
1901 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001902 }
1903
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001904 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001905 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001906 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001907 return 1;
1908 }
1909 return 0;
1910}
1911
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001912static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1913{
Chao Yu454ae7e2014-04-22 13:34:01 +08001914 struct inode *inode = mapping->host;
1915
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001916 if (f2fs_has_inline_data(inode))
1917 return 0;
1918
1919 /* make sure allocating whole blocks */
1920 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1921 filemap_write_and_wait(mapping);
1922
Chao Yue2b4e2b2015-08-19 19:11:19 +08001923 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001924}
1925
Weichao Guo5b7a4872016-09-20 05:03:27 +08001926#ifdef CONFIG_MIGRATION
1927#include <linux/migrate.h>
1928
1929int f2fs_migrate_page(struct address_space *mapping,
1930 struct page *newpage, struct page *page, enum migrate_mode mode)
1931{
1932 int rc, extra_count;
1933 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
1934 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
1935
1936 BUG_ON(PageWriteback(page));
1937
1938 /* migrating an atomic written page is safe with the inmem_lock hold */
1939 if (atomic_written && !mutex_trylock(&fi->inmem_lock))
1940 return -EAGAIN;
1941
1942 /*
1943 * A reference is expected if PagePrivate set when move mapping,
1944 * however F2FS breaks this for maintaining dirty page counts when
1945 * truncating pages. So here adjusting the 'extra_count' make it work.
1946 */
1947 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
1948 rc = migrate_page_move_mapping(mapping, newpage,
1949 page, NULL, mode, extra_count);
1950 if (rc != MIGRATEPAGE_SUCCESS) {
1951 if (atomic_written)
1952 mutex_unlock(&fi->inmem_lock);
1953 return rc;
1954 }
1955
1956 if (atomic_written) {
1957 struct inmem_pages *cur;
1958 list_for_each_entry(cur, &fi->inmem_pages, list)
1959 if (cur->page == page) {
1960 cur->page = newpage;
1961 break;
1962 }
1963 mutex_unlock(&fi->inmem_lock);
1964 put_page(page);
1965 get_page(newpage);
1966 }
1967
1968 if (PagePrivate(page))
1969 SetPagePrivate(newpage);
1970 set_page_private(newpage, page_private(page));
1971
1972 migrate_page_copy(newpage, page);
1973
1974 return MIGRATEPAGE_SUCCESS;
1975}
1976#endif
1977
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001978const struct address_space_operations f2fs_dblock_aops = {
1979 .readpage = f2fs_read_data_page,
1980 .readpages = f2fs_read_data_pages,
1981 .writepage = f2fs_write_data_page,
1982 .writepages = f2fs_write_data_pages,
1983 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001984 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001985 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001986 .invalidatepage = f2fs_invalidate_page,
1987 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001988 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001989 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08001990#ifdef CONFIG_MIGRATION
1991 .migratepage = f2fs_migrate_page,
1992#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001993};