blob: 7d618a969524399842e4be668fc9b0d4777324d7 [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 Yu12377022015-05-25 18:03:38 +080037 if (f2fs_bio_encrypted(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020038 if (bio->bi_error) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070039 fscrypt_release_ctx(bio->bi_private);
Chao Yu12377022015-05-25 18:03:38 +080040 } else {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070041 fscrypt_decrypt_bio_pages(bio->bi_private, bio);
Chao Yu12377022015-05-25 18:03:38 +080042 return;
43 }
44 }
45
Linus Torvaldsf5688492014-01-30 11:19:05 -080046 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090047 struct page *page = bvec->bv_page;
48
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020049 if (!bio->bi_error) {
Jaegeuk Kim237c0792016-06-30 18:49:15 -070050 if (!PageUptodate(page))
51 SetPageUptodate(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080052 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090053 ClearPageUptodate(page);
54 SetPageError(page);
55 }
56 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080057 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090058 bio_put(bio);
59}
60
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020061static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090062{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090063 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080064 struct bio_vec *bvec;
65 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090066
Linus Torvaldsf5688492014-01-30 11:19:05 -080067 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090068 struct page *page = bvec->bv_page;
69
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070070 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -070071
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020072 if (unlikely(bio->bi_error)) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090073 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -070074 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090075 }
76 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080077 }
Jaegeuk Kimf5730182016-05-17 16:23:36 -070078 if (atomic_dec_and_test(&sbi->nr_wb_bios) &&
79 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090080 wake_up(&sbi->cp_wait);
81
82 bio_put(bio);
83}
84
Gu Zheng940a6d32013-12-20 17:39:59 +080085/*
86 * Low-level block read/write IO operations.
87 */
88static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
89 int npages, bool is_read)
90{
91 struct bio *bio;
92
Jaegeuk Kim740432f2015-08-14 11:43:56 -070093 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +080094
95 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +080096 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +080097 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +080098 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +080099
100 return bio;
101}
102
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700103static inline void __submit_bio(struct f2fs_sb_info *sbi,
104 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700105{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700106 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700107 atomic_inc(&sbi->nr_wb_bios);
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700108 if (f2fs_sb_mounted_hmsmr(sbi->sb) &&
109 current->plug && (type == DATA || type == NODE))
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700110 blk_finish_plug(current->plug);
111 }
Mike Christie4e49ea42016-06-05 14:31:41 -0500112 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700113}
114
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900115static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900116{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900117 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900118
119 if (!io->bio)
120 return;
121
Mike Christie04d328d2016-06-05 14:31:55 -0500122 if (is_read_io(fio->op))
Chao Yu2ace38e2014-12-24 16:08:14 +0800123 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700124 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800125 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900126
Mike Christie04d328d2016-06-05 14:31:55 -0500127 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
128
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700129 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900130 io->bio = NULL;
131}
132
Chao Yu0c3a5792016-01-18 18:28:11 +0800133static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
134 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800135{
Chao Yu0fd785e2016-01-18 18:24:59 +0800136 struct bio_vec *bvec;
137 struct page *target;
138 int i;
139
Chao Yu0c3a5792016-01-18 18:28:11 +0800140 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800141 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800142
143 if (!inode && !page && !ino)
144 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800145
146 bio_for_each_segment_all(bvec, io->bio, i) {
147
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700148 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800149 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700150 else
151 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800152
Chao Yu0c3a5792016-01-18 18:28:11 +0800153 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800154 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800155 if (page && page == target)
156 return true;
157 if (ino && ino == ino_of_node(target))
158 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800159 }
160
Chao Yu0fd785e2016-01-18 18:24:59 +0800161 return false;
162}
163
Chao Yu0c3a5792016-01-18 18:28:11 +0800164static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
165 struct page *page, nid_t ino,
166 enum page_type type)
167{
168 enum page_type btype = PAGE_TYPE_OF_BIO(type);
169 struct f2fs_bio_info *io = &sbi->write_io[btype];
170 bool ret;
171
172 down_read(&io->io_rwsem);
173 ret = __has_merged_page(io, inode, page, ino);
174 up_read(&io->io_rwsem);
175 return ret;
176}
177
178static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
179 struct inode *inode, struct page *page,
180 nid_t ino, enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900181{
182 enum page_type btype = PAGE_TYPE_OF_BIO(type);
183 struct f2fs_bio_info *io;
184
185 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
186
Chao Yudf0f8dc2014-03-22 14:57:23 +0800187 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900188
Chao Yu0c3a5792016-01-18 18:28:11 +0800189 if (!__has_merged_page(io, inode, page, ino))
190 goto out;
191
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900192 /* change META to META_FLUSH in the checkpoint procedure */
193 if (type >= META_FLUSH) {
194 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500195 io->fio.op = REQ_OP_WRITE;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700196 if (test_opt(sbi, NOBARRIER))
Mike Christie04d328d2016-06-05 14:31:55 -0500197 io->fio.op_flags = WRITE_FLUSH | REQ_META | REQ_PRIO;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700198 else
Mike Christie04d328d2016-06-05 14:31:55 -0500199 io->fio.op_flags = WRITE_FLUSH_FUA | REQ_META |
200 REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900201 }
202 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800203out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800204 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900205}
206
Chao Yu0c3a5792016-01-18 18:28:11 +0800207void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
208 int rw)
209{
210 __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw);
211}
212
213void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
214 struct inode *inode, struct page *page,
215 nid_t ino, enum page_type type, int rw)
216{
217 if (has_merged_page(sbi, inode, page, ino, type))
218 __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw);
219}
220
Chao Yu406657d2016-02-24 17:17:55 +0800221void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
222{
223 f2fs_submit_merged_bio(sbi, DATA, WRITE);
224 f2fs_submit_merged_bio(sbi, NODE, WRITE);
225 f2fs_submit_merged_bio(sbi, META, WRITE);
226}
227
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900228/*
229 * Fill the locked page with data located in the block address.
230 * Return unlocked page.
231 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700232int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900233{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900234 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700235 struct page *page = fio->encrypted_page ?
236 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900237
Chao Yu2ace38e2014-12-24 16:08:14 +0800238 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700239 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900240
241 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500242 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900243
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300244 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900245 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900246 return -EFAULT;
247 }
Mike Christie04d328d2016-06-05 14:31:55 -0500248 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900249
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700250 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900251 return 0;
252}
253
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700254void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900255{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700256 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900257 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900258 struct f2fs_bio_info *io;
Mike Christie04d328d2016-06-05 14:31:55 -0500259 bool is_read = is_read_io(fio->op);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700260 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900261
Gu Zheng940a6d32013-12-20 17:39:59 +0800262 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900263
Chao Yu7a9d7542016-02-22 18:36:38 +0800264 if (fio->old_blkaddr != NEW_ADDR)
265 verify_block_addr(sbi, fio->old_blkaddr);
266 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900267
Chao Yudf0f8dc2014-03-22 14:57:23 +0800268 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900269
Chao Yu7a9d7542016-02-22 18:36:38 +0800270 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Mike Christie04d328d2016-06-05 14:31:55 -0500271 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900272 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900273alloc_new:
274 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700275 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800276
Chao Yu7a9d7542016-02-22 18:36:38 +0800277 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
278 bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900279 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900280 }
281
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700282 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
283
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300284 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
285 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900286 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900287 goto alloc_new;
288 }
289
Chao Yu7a9d7542016-02-22 18:36:38 +0800290 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700291 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900292
Chao Yudf0f8dc2014-03-22 14:57:23 +0800293 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700294 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900295}
296
Chao Yu46008c62016-05-09 19:56:30 +0800297static void __set_data_blkaddr(struct dnode_of_data *dn)
298{
299 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
300 __le32 *addr_array;
301
302 /* Get physical address of data block */
303 addr_array = blkaddr_in_node(rn);
304 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
305}
306
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900307/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900308 * Lock ordering for the change of data block address:
309 * ->data_page
310 * ->node_page
311 * update block addresses in the node page
312 */
Chao Yu216a6202015-03-19 19:23:32 +0800313void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900314{
Chao Yu46008c62016-05-09 19:56:30 +0800315 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
316 __set_data_blkaddr(dn);
317 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800318 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900319}
320
Chao Yuf28b3432016-02-24 17:16:47 +0800321void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
322{
323 dn->data_blkaddr = blkaddr;
324 set_data_blkaddr(dn);
325 f2fs_update_extent_cache(dn);
326}
327
Chao Yu46008c62016-05-09 19:56:30 +0800328/* dn->ofs_in_node will be returned with up-to-date last block pointer */
329int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900330{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700331 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900332
Chao Yu46008c62016-05-09 19:56:30 +0800333 if (!count)
334 return 0;
335
Jaegeuk Kim91942322016-05-20 10:13:22 -0700336 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900337 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800338 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900339 return -ENOSPC;
340
Chao Yu46008c62016-05-09 19:56:30 +0800341 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
342 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900343
Chao Yu46008c62016-05-09 19:56:30 +0800344 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
345
346 for (; count > 0; dn->ofs_in_node++) {
347 block_t blkaddr =
348 datablock_addr(dn->node_page, dn->ofs_in_node);
349 if (blkaddr == NULL_ADDR) {
350 dn->data_blkaddr = NEW_ADDR;
351 __set_data_blkaddr(dn);
352 count--;
353 }
354 }
355
356 if (set_page_dirty(dn->node_page))
357 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900358 return 0;
359}
360
Chao Yu46008c62016-05-09 19:56:30 +0800361/* Should keep dn->ofs_in_node unchanged */
362int reserve_new_block(struct dnode_of_data *dn)
363{
364 unsigned int ofs_in_node = dn->ofs_in_node;
365 int ret;
366
367 ret = reserve_new_blocks(dn, 1);
368 dn->ofs_in_node = ofs_in_node;
369 return ret;
370}
371
Huajun Lib6009652013-11-10 23:13:18 +0800372int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
373{
374 bool need_put = dn->inode_page ? false : true;
375 int err;
376
377 err = get_dnode_of_data(dn, index, ALLOC_NODE);
378 if (err)
379 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900380
Huajun Lib6009652013-11-10 23:13:18 +0800381 if (dn->data_blkaddr == NULL_ADDR)
382 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900383 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800384 f2fs_put_dnode(dn);
385 return err;
386}
387
Fan Li759af1c2015-08-05 15:52:16 +0800388int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900389{
Chao Yu028a41e2015-03-19 19:26:02 +0800390 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800391 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800392
Fan Li759af1c2015-08-05 15:52:16 +0800393 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
394 dn->data_blkaddr = ei.blk + index - ei.fofs;
395 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800396 }
397
Fan Li759af1c2015-08-05 15:52:16 +0800398 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900399}
400
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700401struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500402 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900403{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900404 struct address_space *mapping = inode->i_mapping;
405 struct dnode_of_data dn;
406 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800407 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900408 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800409 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700410 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800411 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -0500412 .op = REQ_OP_READ,
413 .op_flags = op_flags,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700414 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800415 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900416
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700417 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
418 return read_mapping_page(mapping, index, NULL);
419
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700420 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900421 if (!page)
422 return ERR_PTR(-ENOMEM);
423
Chao Yucb3bc9e2015-02-05 18:03:40 +0800424 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
425 dn.data_blkaddr = ei.blk + index - ei.fofs;
426 goto got_it;
427 }
428
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900429 set_new_dnode(&dn, inode, NULL, NULL, 0);
430 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700431 if (err)
432 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900433 f2fs_put_dnode(&dn);
434
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900435 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700436 err = -ENOENT;
437 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900438 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800439got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700440 if (PageUptodate(page)) {
441 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900442 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700443 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900444
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900445 /*
446 * A new dentry page is allocated but not able to be written, since its
447 * new inode page couldn't be allocated due to -ENOSPC.
448 * In such the case, its blkaddr can be remained as NEW_ADDR.
449 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
450 */
451 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300452 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700453 if (!PageUptodate(page))
454 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700455 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900456 return page;
457 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900458
Chao Yu7a9d7542016-02-22 18:36:38 +0800459 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700460 fio.page = page;
461 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900462 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700463 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700464 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700465
466put_err:
467 f2fs_put_page(page, 1);
468 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700469}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900470
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700471struct page *find_data_page(struct inode *inode, pgoff_t index)
472{
473 struct address_space *mapping = inode->i_mapping;
474 struct page *page;
475
476 page = find_get_page(mapping, index);
477 if (page && PageUptodate(page))
478 return page;
479 f2fs_put_page(page, 0);
480
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700481 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700482 if (IS_ERR(page))
483 return page;
484
485 if (PageUptodate(page))
486 return page;
487
488 wait_on_page_locked(page);
489 if (unlikely(!PageUptodate(page))) {
490 f2fs_put_page(page, 0);
491 return ERR_PTR(-EIO);
492 }
493 return page;
494}
495
496/*
497 * If it tries to access a hole, return an error.
498 * Because, the callers, functions in dir.c and GC, should be able to know
499 * whether this page exists or not.
500 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700501struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
502 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700503{
504 struct address_space *mapping = inode->i_mapping;
505 struct page *page;
506repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700507 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700508 if (IS_ERR(page))
509 return page;
510
511 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900512 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900513 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900514 f2fs_put_page(page, 1);
515 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900516 }
Chao Yu1563ac72016-07-03 22:05:12 +0800517 if (unlikely(!PageUptodate(page))) {
518 f2fs_put_page(page, 1);
519 return ERR_PTR(-EIO);
520 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900521 return page;
522}
523
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900524/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900525 * Caller ensures that this data page is never allocated.
526 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900527 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800528 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
529 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800530 * Note that, ipage is set only by make_empty_dir, and if any error occur,
531 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900532 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900533struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900534 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900535{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900536 struct address_space *mapping = inode->i_mapping;
537 struct page *page;
538 struct dnode_of_data dn;
539 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800540
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700541 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800542 if (!page) {
543 /*
544 * before exiting, we should make sure ipage will be released
545 * if any error occur.
546 */
547 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700548 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800549 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900550
Jaegeuk Kima8865372013-12-27 17:04:17 +0900551 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800552 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700553 if (err) {
554 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900555 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900556 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700557 if (!ipage)
558 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900559
560 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700561 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900562
563 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300564 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700565 if (!PageUptodate(page))
566 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900567 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700568 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900569
Jaegeuk Kim76121182016-01-01 22:03:47 -0800570 /* if ipage exists, blkaddr should be NEW_ADDR */
571 f2fs_bug_on(F2FS_I_SB(inode), ipage);
572 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700573 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800574 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900575 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700576got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800577 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700578 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700579 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900580 return page;
581}
582
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900583static int __allocate_data_block(struct dnode_of_data *dn)
584{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700585 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900586 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900587 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800588 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700589 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800590 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900591
Jaegeuk Kim91942322016-05-20 10:13:22 -0700592 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900593 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800594
595 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
596 if (dn->data_blkaddr == NEW_ADDR)
597 goto alloc;
598
Chao Yu46008c62016-05-09 19:56:30 +0800599 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900600 return -ENOSPC;
601
Chao Yudf6136e2015-03-23 10:33:37 +0800602alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900603 get_node_info(sbi, dn->nid, &ni);
604 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
605
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800606 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
607 seg = CURSEG_DIRECT_IO;
608
Chao Yudf6136e2015-03-23 10:33:37 +0800609 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
610 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800611 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900612
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700613 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800614 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700615 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300616 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700617 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300618 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900619 return 0;
620}
621
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800622ssize_t f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800623{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800624 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800625 struct f2fs_map_blocks map;
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800626 ssize_t ret = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800627
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700628 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
629 map.m_len = F2FS_BYTES_TO_BLK(iov_iter_count(from));
Chao Yuda859852016-01-26 15:42:58 +0800630 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800631
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800632 if (f2fs_encrypted_inode(inode))
633 return 0;
634
635 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800636 ret = f2fs_convert_inline_inode(inode);
637 if (ret)
638 return ret;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800639 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800640 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800641 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
642 ret = f2fs_convert_inline_inode(inode);
643 if (ret)
644 return ret;
645 }
646 if (!f2fs_has_inline_data(inode))
647 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800648 return ret;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800649}
650
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900651/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700652 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
653 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800654 * If original data blocks are allocated, then give them to blockdev.
655 * Otherwise,
656 * a. preallocate requested block addresses
657 * b. do not use extent cache for better performance
658 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900659 */
Chao Yud323d002015-10-27 09:53:45 +0800660int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800661 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900662{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700663 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900664 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800665 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700666 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800667 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900668 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800669 unsigned int ofs_in_node, last_ofs_in_node;
670 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800671 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900672 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800673 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900674
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700675 map->m_len = 0;
676 map->m_flags = 0;
677
678 /* it only supports block size == page size */
679 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800680 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900681
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800682 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700683 map->m_pblk = ei.blk + pgofs - ei.fofs;
684 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
685 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900686 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800687 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900688
Chao Yu4fe71e82016-01-26 15:37:38 +0800689next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800690 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800691 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900692
693 /* When reading holes, we need its node page */
694 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900695 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900696 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800697 if (flag == F2FS_GET_BLOCK_BMAP)
698 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800699 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900700 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800701 if (map->m_next_pgofs)
702 *map->m_next_pgofs =
703 get_next_page_offset(&dn, pgofs);
704 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900705 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900706 }
Chao Yu973163f2015-09-18 16:51:51 +0800707
Chao Yu46008c62016-05-09 19:56:30 +0800708 prealloc = 0;
709 ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800710 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900711
Chao Yu4fe71e82016-01-26 15:37:38 +0800712next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800713 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800714
Fan Li7df3a432015-12-17 13:20:59 +0800715 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
716 if (create) {
717 if (unlikely(f2fs_cp_error(sbi))) {
718 err = -EIO;
719 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800720 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800721 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800722 if (blkaddr == NULL_ADDR) {
723 prealloc++;
724 last_ofs_in_node = dn.ofs_in_node;
725 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800726 } else {
727 err = __allocate_data_block(&dn);
Chao Yu46008c62016-05-09 19:56:30 +0800728 if (!err) {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700729 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yu46008c62016-05-09 19:56:30 +0800730 allocated = true;
731 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800732 }
Fan Li7df3a432015-12-17 13:20:59 +0800733 if (err)
734 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800735 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800736 blkaddr = dn.data_blkaddr;
737 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800738 if (flag == F2FS_GET_BLOCK_BMAP) {
739 map->m_pblk = 0;
740 goto sync_out;
741 }
Chao Yuda859852016-01-26 15:42:58 +0800742 if (flag == F2FS_GET_BLOCK_FIEMAP &&
743 blkaddr == NULL_ADDR) {
744 if (map->m_next_pgofs)
745 *map->m_next_pgofs = pgofs + 1;
746 }
Fan Li7df3a432015-12-17 13:20:59 +0800747 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800748 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800749 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900750 }
751 }
Fan Li7df3a432015-12-17 13:20:59 +0800752
Chao Yu46008c62016-05-09 19:56:30 +0800753 if (flag == F2FS_GET_BLOCK_PRE_AIO)
754 goto skip;
755
Chao Yu4fe71e82016-01-26 15:37:38 +0800756 if (map->m_len == 0) {
757 /* preallocated unwritten block should be mapped for fiemap. */
758 if (blkaddr == NEW_ADDR)
759 map->m_flags |= F2FS_MAP_UNWRITTEN;
760 map->m_flags |= F2FS_MAP_MAPPED;
761
762 map->m_pblk = blkaddr;
763 map->m_len = 1;
764 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800765 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800766 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800767 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800768 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800769 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800770 } else {
771 goto sync_out;
772 }
773
Chao Yu46008c62016-05-09 19:56:30 +0800774skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800775 dn.ofs_in_node++;
776 pgofs++;
777
Chao Yu46008c62016-05-09 19:56:30 +0800778 /* preallocate blocks in batch for one dnode page */
779 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
780 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800781
Chao Yu46008c62016-05-09 19:56:30 +0800782 dn.ofs_in_node = ofs_in_node;
783 err = reserve_new_blocks(&dn, prealloc);
784 if (err)
785 goto sync_out;
Chao Yu58383be2016-08-20 15:12:02 +0800786 allocated = dn.node_changed;
Chao Yu4fe71e82016-01-26 15:37:38 +0800787
Chao Yu46008c62016-05-09 19:56:30 +0800788 map->m_len += dn.ofs_in_node - ofs_in_node;
789 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
790 err = -ENOSPC;
791 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800792 }
Chao Yu46008c62016-05-09 19:56:30 +0800793 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800794 }
795
Chao Yu46008c62016-05-09 19:56:30 +0800796 if (pgofs >= end)
797 goto sync_out;
798 else if (dn.ofs_in_node < end_offset)
799 goto next_block;
800
Chao Yu46008c62016-05-09 19:56:30 +0800801 f2fs_put_dnode(&dn);
802
803 if (create) {
804 f2fs_unlock_op(sbi);
805 f2fs_balance_fs(sbi, allocated);
806 }
807 allocated = false;
808 goto next_dnode;
809
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900810sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900811 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900812unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800813 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800814 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800815 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800816 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900817out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700818 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900819 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900820}
821
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700822static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800823 struct buffer_head *bh, int create, int flag,
824 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700825{
826 struct f2fs_map_blocks map;
827 int ret;
828
829 map.m_lblk = iblock;
830 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800831 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700832
Chao Yue2b4e2b2015-08-19 19:11:19 +0800833 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700834 if (!ret) {
835 map_bh(bh, inode->i_sb, map.m_pblk);
836 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
837 bh->b_size = map.m_len << inode->i_blkbits;
838 }
839 return ret;
840}
841
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900842static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800843 struct buffer_head *bh_result, int create, int flag,
844 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900845{
Chao Yuda859852016-01-26 15:42:58 +0800846 return __get_data_block(inode, iblock, bh_result, create,
847 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900848}
849
Chao Yue2b4e2b2015-08-19 19:11:19 +0800850static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900851 struct buffer_head *bh_result, int create)
852{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800853 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800854 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800855}
856
857static int get_data_block_bmap(struct inode *inode, sector_t iblock,
858 struct buffer_head *bh_result, int create)
859{
Yunlei He179448b2015-12-28 21:48:32 +0800860 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800861 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800862 return -EFBIG;
863
Chao Yue2b4e2b2015-08-19 19:11:19 +0800864 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800865 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900866}
867
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700868static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
869{
870 return (offset >> inode->i_blkbits);
871}
872
873static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
874{
875 return (blk << inode->i_blkbits);
876}
877
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900878int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
879 u64 start, u64 len)
880{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700881 struct buffer_head map_bh;
882 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800883 pgoff_t next_pgofs;
Fan Lide1475c2016-01-04 15:56:50 +0800884 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700885 u64 logical = 0, phys = 0, size = 0;
886 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700887 int ret = 0;
888
889 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
890 if (ret)
891 return ret;
892
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700893 if (f2fs_has_inline_data(inode)) {
894 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
895 if (ret != -EAGAIN)
896 return ret;
897 }
898
Al Viro59551022016-01-22 15:40:57 -0500899 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800900
901 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800902 if (start >= isize)
903 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700904
Fan Li9a950d52015-12-26 18:07:41 +0800905 if (start + len > isize)
906 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700907
908 if (logical_to_blk(inode, len) == 0)
909 len = blk_to_logical(inode, 1);
910
911 start_blk = logical_to_blk(inode, start);
912 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800913
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700914next:
915 memset(&map_bh, 0, sizeof(struct buffer_head));
916 map_bh.b_size = len;
917
Chao Yue2b4e2b2015-08-19 19:11:19 +0800918 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800919 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700920 if (ret)
921 goto out;
922
923 /* HOLE */
924 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800925 start_blk = next_pgofs;
Fan Li9a950d52015-12-26 18:07:41 +0800926 /* Go through holes util pass the EOF */
Chao Yuda859852016-01-26 15:42:58 +0800927 if (blk_to_logical(inode, start_blk) < isize)
Fan Li9a950d52015-12-26 18:07:41 +0800928 goto prep_next;
929 /* Found a hole beyond isize means no more extents.
930 * Note that the premise is that filesystems don't
931 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700932 */
Fan Li9a950d52015-12-26 18:07:41 +0800933 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700934 }
Fan Li9a950d52015-12-26 18:07:41 +0800935
Chao Yuda5af122016-01-08 20:19:27 +0800936 if (size) {
937 if (f2fs_encrypted_inode(inode))
938 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
939
Fan Li9a950d52015-12-26 18:07:41 +0800940 ret = fiemap_fill_next_extent(fieinfo, logical,
941 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800942 }
Fan Li9a950d52015-12-26 18:07:41 +0800943
944 if (start_blk > last_blk || ret)
945 goto out;
946
947 logical = blk_to_logical(inode, start_blk);
948 phys = blk_to_logical(inode, map_bh.b_blocknr);
949 size = map_bh.b_size;
950 flags = 0;
951 if (buffer_unwritten(&map_bh))
952 flags = FIEMAP_EXTENT_UNWRITTEN;
953
954 start_blk += logical_to_blk(inode, size);
955
956prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700957 cond_resched();
958 if (fatal_signal_pending(current))
959 ret = -EINTR;
960 else
961 goto next;
962out:
963 if (ret == 1)
964 ret = 0;
965
Al Viro59551022016-01-22 15:40:57 -0500966 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700967 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900968}
969
Chao Yu78682f72016-07-03 22:05:11 +0800970struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
971 unsigned nr_pages)
972{
973 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
974 struct fscrypt_ctx *ctx = NULL;
975 struct block_device *bdev = sbi->sb->s_bdev;
976 struct bio *bio;
977
978 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
979 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
980 if (IS_ERR(ctx))
981 return ERR_CAST(ctx);
982
983 /* wait the page to be moved by cleaning */
984 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
985 }
986
987 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
988 if (!bio) {
989 if (ctx)
990 fscrypt_release_ctx(ctx);
991 return ERR_PTR(-ENOMEM);
992 }
993 bio->bi_bdev = bdev;
994 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blkaddr);
995 bio->bi_end_io = f2fs_read_end_io;
996 bio->bi_private = ctx;
997
998 return bio;
999}
1000
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001001/*
1002 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1003 * Major change was from block_size == page_size in f2fs by default.
1004 */
1005static int f2fs_mpage_readpages(struct address_space *mapping,
1006 struct list_head *pages, struct page *page,
1007 unsigned nr_pages)
1008{
1009 struct bio *bio = NULL;
1010 unsigned page_idx;
1011 sector_t last_block_in_bio = 0;
1012 struct inode *inode = mapping->host;
1013 const unsigned blkbits = inode->i_blkbits;
1014 const unsigned blocksize = 1 << blkbits;
1015 sector_t block_in_file;
1016 sector_t last_block;
1017 sector_t last_block_in_file;
1018 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001019 struct f2fs_map_blocks map;
1020
1021 map.m_pblk = 0;
1022 map.m_lblk = 0;
1023 map.m_len = 0;
1024 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001025 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001026
1027 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1028
1029 prefetchw(&page->flags);
1030 if (pages) {
1031 page = list_entry(pages->prev, struct page, lru);
1032 list_del(&page->lru);
1033 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001034 page->index,
1035 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001036 goto next_page;
1037 }
1038
1039 block_in_file = (sector_t)page->index;
1040 last_block = block_in_file + nr_pages;
1041 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1042 blkbits;
1043 if (last_block > last_block_in_file)
1044 last_block = last_block_in_file;
1045
1046 /*
1047 * Map blocks using the previous result first.
1048 */
1049 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1050 block_in_file > map.m_lblk &&
1051 block_in_file < (map.m_lblk + map.m_len))
1052 goto got_it;
1053
1054 /*
1055 * Then do more f2fs_map_blocks() calls until we are
1056 * done with this page.
1057 */
1058 map.m_flags = 0;
1059
1060 if (block_in_file < last_block) {
1061 map.m_lblk = block_in_file;
1062 map.m_len = last_block - block_in_file;
1063
Chao Yu46c9e142015-09-18 16:54:16 +08001064 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001065 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001066 goto set_error_page;
1067 }
1068got_it:
1069 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1070 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1071 SetPageMappedToDisk(page);
1072
1073 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1074 SetPageUptodate(page);
1075 goto confused;
1076 }
1077 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001078 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001079 if (!PageUptodate(page))
1080 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001081 unlock_page(page);
1082 goto next_page;
1083 }
1084
1085 /*
1086 * This page will go to BIO. Do we need to send this
1087 * BIO off first?
1088 */
1089 if (bio && (last_block_in_bio != block_nr - 1)) {
1090submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001091 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001092 bio = NULL;
1093 }
1094 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001095 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001096 if (IS_ERR(bio)) {
1097 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001098 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001099 }
Mike Christie04d328d2016-06-05 14:31:55 -05001100 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001101 }
1102
1103 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1104 goto submit_and_realloc;
1105
1106 last_block_in_bio = block_nr;
1107 goto next_page;
1108set_error_page:
1109 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001110 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001111 unlock_page(page);
1112 goto next_page;
1113confused:
1114 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001115 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001116 bio = NULL;
1117 }
1118 unlock_page(page);
1119next_page:
1120 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001121 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001122 }
1123 BUG_ON(pages && !list_empty(pages));
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 return 0;
1127}
1128
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001129static int f2fs_read_data_page(struct file *file, struct page *page)
1130{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001131 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001132 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001133
Chao Yuc20e89c2014-05-06 16:53:08 +08001134 trace_f2fs_readpage(page, DATA);
1135
arter97e1c42042014-08-06 23:22:50 +09001136 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001137 if (f2fs_has_inline_data(inode))
1138 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001139 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001140 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001141 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001142}
1143
1144static int f2fs_read_data_pages(struct file *file,
1145 struct address_space *mapping,
1146 struct list_head *pages, unsigned nr_pages)
1147{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001148 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001149 struct page *page = list_entry(pages->prev, struct page, lru);
1150
1151 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001152
1153 /* If the file has inline data, skip readpages */
1154 if (f2fs_has_inline_data(inode))
1155 return 0;
1156
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001157 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001158}
1159
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001160int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001161{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001162 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001163 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001164 struct dnode_of_data dn;
1165 int err = 0;
1166
1167 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001168 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001169 if (err)
1170 return err;
1171
Chao Yu28bc1062016-02-06 14:40:34 +08001172 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001173
1174 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001175 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001176 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001177 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001178 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001179
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001180 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001181 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001182
1183 /* wait for GCed encrypted page writeback */
1184 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001185 fio->old_blkaddr);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001186retry_encrypt:
1187 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1188 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001189 if (IS_ERR(fio->encrypted_page)) {
1190 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001191 if (err == -ENOMEM) {
1192 /* flush pending ios and wait for a while */
1193 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1194 congestion_wait(BLK_RW_ASYNC, HZ/50);
1195 gfp_flags |= __GFP_NOFAIL;
1196 err = 0;
1197 goto retry_encrypt;
1198 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001199 goto out_writepage;
1200 }
1201 }
1202
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001203 set_page_writeback(page);
1204
1205 /*
1206 * If current allocation needs SSR,
1207 * it had better in-place writes for updated data.
1208 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001209 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001210 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001211 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001212 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001213 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001214 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001215 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001216 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001217 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001218 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001219 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001220 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001221 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001222 }
1223out_writepage:
1224 f2fs_put_dnode(&dn);
1225 return err;
1226}
1227
1228static int f2fs_write_data_page(struct page *page,
1229 struct writeback_control *wbc)
1230{
1231 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001232 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001233 loff_t i_size = i_size_read(inode);
1234 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001235 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001236 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001237 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001238 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001239 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001240 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001241 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001242 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001243 .op = REQ_OP_WRITE,
1244 .op_flags = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : 0,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001245 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001246 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001247 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001248
Chao Yuecda0de2014-05-06 16:48:26 +08001249 trace_f2fs_writepage(page, DATA);
1250
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001251 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001252 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001253
1254 /*
1255 * If the offset is out-of-range of file size,
1256 * this page does not have to be written to disk.
1257 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001258 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001259 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001260 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001261
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001262 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001263write:
Chao Yucaf00472015-01-28 17:48:42 +08001264 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001265 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001266 if (f2fs_is_drop_cache(inode))
1267 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001268 /* we should not write 0'th page having journal header */
1269 if (f2fs_is_volatile_file(inode) && (!page->index ||
1270 (!wbc->for_reclaim &&
1271 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001272 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001273
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001274 /* we should bypass data pages to proceed the kworkder jobs */
1275 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001276 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001277 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001278 }
1279
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001280 /* Dentry blocks are controlled by checkpoint */
1281 if (S_ISDIR(inode->i_mode)) {
1282 err = do_write_data_page(&fio);
1283 goto done;
1284 }
1285
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001286 if (!wbc->for_reclaim)
1287 need_balance_fs = true;
1288 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001289 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001290
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001291 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001292 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001293 if (f2fs_has_inline_data(inode))
1294 err = f2fs_write_inline_data(inode, page);
1295 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001296 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001297 if (F2FS_I(inode)->last_disk_size < psize)
1298 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001299 f2fs_unlock_op(sbi);
1300done:
1301 if (err && err != -ENOENT)
1302 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001303
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001304 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001305out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001306 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001307 if (err)
1308 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001309
1310 if (wbc->for_reclaim) {
1311 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001312 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001313 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001314
1315 unlock_page(page);
1316 f2fs_balance_fs(sbi, need_balance_fs);
1317
1318 if (unlikely(f2fs_cp_error(sbi)))
1319 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1320
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001321 return 0;
1322
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001323redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001324 redirty_page_for_writepage(wbc, page);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001325 unlock_page(page);
1326 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001327}
1328
Chao Yu8f46dca2015-07-14 18:56:10 +08001329/*
1330 * This function was copied from write_cche_pages from mm/page-writeback.c.
1331 * The major change is making write step of cold data page separately from
1332 * warm/hot data page.
1333 */
1334static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001335 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001336{
1337 int ret = 0;
1338 int done = 0;
1339 struct pagevec pvec;
1340 int nr_pages;
1341 pgoff_t uninitialized_var(writeback_index);
1342 pgoff_t index;
1343 pgoff_t end; /* Inclusive */
1344 pgoff_t done_index;
1345 int cycled;
1346 int range_whole = 0;
1347 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001348
1349 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001350
Chao Yu8f46dca2015-07-14 18:56:10 +08001351 if (wbc->range_cyclic) {
1352 writeback_index = mapping->writeback_index; /* prev offset */
1353 index = writeback_index;
1354 if (index == 0)
1355 cycled = 1;
1356 else
1357 cycled = 0;
1358 end = -1;
1359 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001360 index = wbc->range_start >> PAGE_SHIFT;
1361 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001362 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1363 range_whole = 1;
1364 cycled = 1; /* ignore range_cyclic tests */
1365 }
1366 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1367 tag = PAGECACHE_TAG_TOWRITE;
1368 else
1369 tag = PAGECACHE_TAG_DIRTY;
1370retry:
1371 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1372 tag_pages_for_writeback(mapping, index, end);
1373 done_index = index;
1374 while (!done && (index <= end)) {
1375 int i;
1376
1377 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1378 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1379 if (nr_pages == 0)
1380 break;
1381
1382 for (i = 0; i < nr_pages; i++) {
1383 struct page *page = pvec.pages[i];
1384
1385 if (page->index > end) {
1386 done = 1;
1387 break;
1388 }
1389
1390 done_index = page->index;
1391
1392 lock_page(page);
1393
1394 if (unlikely(page->mapping != mapping)) {
1395continue_unlock:
1396 unlock_page(page);
1397 continue;
1398 }
1399
1400 if (!PageDirty(page)) {
1401 /* someone wrote it for us */
1402 goto continue_unlock;
1403 }
1404
Chao Yu8f46dca2015-07-14 18:56:10 +08001405 if (PageWriteback(page)) {
1406 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001407 f2fs_wait_on_page_writeback(page,
1408 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001409 else
1410 goto continue_unlock;
1411 }
1412
1413 BUG_ON(PageWriteback(page));
1414 if (!clear_page_dirty_for_io(page))
1415 goto continue_unlock;
1416
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001417 ret = mapping->a_ops->writepage(page, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001418 if (unlikely(ret)) {
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001419 done_index = page->index + 1;
1420 done = 1;
1421 break;
Chao Yu8f46dca2015-07-14 18:56:10 +08001422 }
1423
1424 if (--wbc->nr_to_write <= 0 &&
1425 wbc->sync_mode == WB_SYNC_NONE) {
1426 done = 1;
1427 break;
1428 }
1429 }
1430 pagevec_release(&pvec);
1431 cond_resched();
1432 }
1433
Chao Yu8f46dca2015-07-14 18:56:10 +08001434 if (!cycled && !done) {
1435 cycled = 1;
1436 index = 0;
1437 end = writeback_index - 1;
1438 goto retry;
1439 }
1440 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1441 mapping->writeback_index = done_index;
1442
1443 return ret;
1444}
1445
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001446static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001447 struct writeback_control *wbc)
1448{
1449 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001450 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001451 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001452 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001453
P J Pcfb185a2013-04-03 11:38:00 +09001454 /* deal with chardevs and other special file */
1455 if (!mapping->a_ops->writepage)
1456 return 0;
1457
Chao Yu6a290542015-07-17 18:02:39 +08001458 /* skip writing if there is no dirty page in this inode */
1459 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1460 return 0;
1461
Jaegeuk Kima1257022015-10-08 10:40:07 -07001462 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1463 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1464 available_free_memory(sbi, DIRTY_DENTS))
1465 goto skip_write;
1466
Chao Yud323d002015-10-27 09:53:45 +08001467 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001468 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001469 goto skip_write;
1470
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001471 /* during POR, we don't need to trigger writepage at all. */
1472 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1473 goto skip_write;
1474
Yunlei Hed31c7c32016-02-04 16:14:00 +08001475 trace_f2fs_writepages(mapping->host, wbc, DATA);
1476
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001477 blk_start_plug(&plug);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001478 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001479 blk_finish_plug(&plug);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001480 /*
1481 * if some pages were truncated, we cannot guarantee its mapping->host
1482 * to detect pending bios.
1483 */
1484 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001485
Chao Yuc227f912015-12-16 13:09:20 +08001486 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001487 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001488
1489skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001490 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001491 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001492 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001493}
1494
Chao Yu3aab8f82014-07-02 13:25:04 +08001495static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1496{
1497 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001498 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001499
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001500 if (to > i_size) {
1501 truncate_pagecache(inode, i_size);
1502 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001503 }
1504}
1505
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001506static int prepare_write_begin(struct f2fs_sb_info *sbi,
1507 struct page *page, loff_t pos, unsigned len,
1508 block_t *blk_addr, bool *node_changed)
1509{
1510 struct inode *inode = page->mapping->host;
1511 pgoff_t index = page->index;
1512 struct dnode_of_data dn;
1513 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001514 bool locked = false;
1515 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001516 int err = 0;
1517
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001518 /*
1519 * we already allocated all the blocks, so we don't need to get
1520 * the block addresses when there is no need to fill the page.
1521 */
1522 if (!f2fs_has_inline_data(inode) && !f2fs_encrypted_inode(inode) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001523 len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001524 return 0;
1525
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001526 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001527 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001528 f2fs_lock_op(sbi);
1529 locked = true;
1530 }
1531restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001532 /* check inline_data */
1533 ipage = get_node_page(sbi, inode->i_ino);
1534 if (IS_ERR(ipage)) {
1535 err = PTR_ERR(ipage);
1536 goto unlock_out;
1537 }
1538
1539 set_new_dnode(&dn, inode, ipage, ipage, 0);
1540
1541 if (f2fs_has_inline_data(inode)) {
1542 if (pos + len <= MAX_INLINE_DATA) {
1543 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001544 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001545 if (inode->i_nlink)
1546 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001547 } else {
1548 err = f2fs_convert_inline_page(&dn, page);
1549 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001550 goto out;
1551 if (dn.data_blkaddr == NULL_ADDR)
1552 err = f2fs_get_block(&dn, index);
1553 }
1554 } else if (locked) {
1555 err = f2fs_get_block(&dn, index);
1556 } else {
1557 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1558 dn.data_blkaddr = ei.blk + index - ei.fofs;
1559 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001560 /* hole case */
1561 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001562 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001563 f2fs_put_dnode(&dn);
1564 f2fs_lock_op(sbi);
1565 locked = true;
1566 goto restart;
1567 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001568 }
1569 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001570
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001571 /* convert_inline_page can make node_changed */
1572 *blk_addr = dn.data_blkaddr;
1573 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001574out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001575 f2fs_put_dnode(&dn);
1576unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001577 if (locked)
1578 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001579 return err;
1580}
1581
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001582static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1583 loff_t pos, unsigned len, unsigned flags,
1584 struct page **pagep, void **fsdata)
1585{
1586 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001587 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001588 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001589 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001590 bool need_balance = false;
1591 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001592 int err = 0;
1593
Chao Yu62aed042014-05-06 16:46:04 +08001594 trace_f2fs_write_begin(inode, pos, len, flags);
1595
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001596 /*
1597 * We should check this at this moment to avoid deadlock on inode page
1598 * and #0 page. The locking rule for inline_data conversion should be:
1599 * lock_page(page #0) -> lock_page(inode_page)
1600 */
1601 if (index != 0) {
1602 err = f2fs_convert_inline_inode(inode);
1603 if (err)
1604 goto fail;
1605 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001606repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001607 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001608 if (!page) {
1609 err = -ENOMEM;
1610 goto fail;
1611 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001612
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001613 *pagep = page;
1614
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001615 err = prepare_write_begin(sbi, page, pos, len,
1616 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001617 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001618 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001619
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001620 if (need_balance && has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001621 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001622 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001623 lock_page(page);
1624 if (page->mapping != mapping) {
1625 /* The page got truncated from under us */
1626 f2fs_put_page(page, 1);
1627 goto repeat;
1628 }
1629 }
1630
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001631 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001632
Chao Yu08b39fb2015-10-08 13:27:34 +08001633 /* wait for GCed encrypted page writeback */
1634 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001635 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001636
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001637 if (len == PAGE_SIZE)
Chao Yu90d43882015-07-08 18:24:38 +08001638 goto out_update;
1639 if (PageUptodate(page))
1640 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001641
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001642 if ((pos & PAGE_MASK) >= i_size_read(inode)) {
1643 unsigned start = pos & (PAGE_SIZE - 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001644 unsigned end = start + len;
1645
1646 /* Reading beyond i_size is simple: memset to zero */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001647 zero_user_segments(page, 0, start, end, PAGE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001648 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001649 }
1650
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001651 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001652 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001653 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001654 struct bio *bio;
Chao Yud54c7952014-03-29 15:30:40 +08001655
Chao Yu78682f72016-07-03 22:05:11 +08001656 bio = f2fs_grab_bio(inode, blkaddr, 1);
1657 if (IS_ERR(bio)) {
1658 err = PTR_ERR(bio);
Chao Yu3aab8f82014-07-02 13:25:04 +08001659 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001660 }
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001661 bio_set_op_attrs(bio, REQ_OP_READ, READ_SYNC);
Chao Yu78682f72016-07-03 22:05:11 +08001662 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1663 bio_put(bio);
1664 err = -EFAULT;
1665 goto fail;
1666 }
1667
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001668 __submit_bio(sbi, bio, DATA);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001669
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001670 lock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001671 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001672 f2fs_put_page(page, 1);
1673 goto repeat;
1674 }
Chao Yu1563ac72016-07-03 22:05:12 +08001675 if (unlikely(!PageUptodate(page))) {
1676 err = -EIO;
1677 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001678 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001679 }
Chao Yu90d43882015-07-08 18:24:38 +08001680out_update:
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001681 if (!PageUptodate(page))
1682 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001683out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001684 clear_cold_data(page);
1685 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001686
Chao Yu3aab8f82014-07-02 13:25:04 +08001687fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001688 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001689 f2fs_write_failed(mapping, pos + len);
1690 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001691}
1692
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001693static int f2fs_write_end(struct file *file,
1694 struct address_space *mapping,
1695 loff_t pos, unsigned len, unsigned copied,
1696 struct page *page, void *fsdata)
1697{
1698 struct inode *inode = page->mapping->host;
1699
Chao Yudfb2bf32014-05-06 16:47:23 +08001700 trace_f2fs_write_end(inode, pos, len, copied);
1701
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001702 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001703
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001704 if (pos + copied > i_size_read(inode))
1705 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001706
Chao Yu3024c9a2016-08-06 21:09:41 +08001707 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001708 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001709 return copied;
1710}
1711
Omar Sandoval6f673762015-03-16 04:33:52 -07001712static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1713 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001714{
1715 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001716
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001717 if (offset & blocksize_mask)
1718 return -EINVAL;
1719
Al Viro5b46f252014-03-16 18:07:34 -04001720 if (iov_iter_alignment(iter) & blocksize_mask)
1721 return -EINVAL;
1722
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001723 return 0;
1724}
1725
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001726static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001727{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001728 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001729 struct inode *inode = mapping->host;
1730 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001731 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08001732 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08001733 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001734
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001735 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001736 if (err)
1737 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001738
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001739 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1740 return 0;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001741 if (test_opt(F2FS_I_SB(inode), LFS))
1742 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001743
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001744 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fad2014-07-31 21:11:22 +08001745
Chao Yu82e0a5a2016-07-13 09:18:29 +08001746 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001747 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08001748 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
1749
1750 if (rw == WRITE) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001751 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001752 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001753 else if (err < 0)
1754 f2fs_write_failed(mapping, offset + count);
1755 }
Chao Yu70407fad2014-07-31 21:11:22 +08001756
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07001757 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fad2014-07-31 21:11:22 +08001758
Chao Yu3aab8f82014-07-02 13:25:04 +08001759 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001760}
1761
Chao Yu487261f2015-02-05 17:44:29 +08001762void f2fs_invalidate_page(struct page *page, unsigned int offset,
1763 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001764{
1765 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001766 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001767
Chao Yu487261f2015-02-05 17:44:29 +08001768 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001769 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001770 return;
1771
Chao Yu487261f2015-02-05 17:44:29 +08001772 if (PageDirty(page)) {
1773 if (inode->i_ino == F2FS_META_INO(sbi))
1774 dec_page_count(sbi, F2FS_DIRTY_META);
1775 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1776 dec_page_count(sbi, F2FS_DIRTY_NODES);
1777 else
1778 inode_dec_dirty_pages(inode);
1779 }
Chao Yudecd36b2015-08-07 18:42:09 +08001780
1781 /* This is atomic written page, keep Private */
1782 if (IS_ATOMIC_WRITTEN_PAGE(page))
1783 return;
1784
Chao Yu23dc9742016-04-29 20:09:15 +08001785 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001786 ClearPagePrivate(page);
1787}
1788
Chao Yu487261f2015-02-05 17:44:29 +08001789int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001790{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001791 /* If this is dirty page, keep PagePrivate */
1792 if (PageDirty(page))
1793 return 0;
1794
Chao Yudecd36b2015-08-07 18:42:09 +08001795 /* This is atomic written page, keep Private */
1796 if (IS_ATOMIC_WRITTEN_PAGE(page))
1797 return 0;
1798
Chao Yu23dc9742016-04-29 20:09:15 +08001799 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001800 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001801 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001802}
1803
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001804/*
1805 * This was copied from __set_page_dirty_buffers which gives higher performance
1806 * in very high speed storages. (e.g., pmem)
1807 */
1808void f2fs_set_page_dirty_nobuffers(struct page *page)
1809{
1810 struct address_space *mapping = page->mapping;
1811 unsigned long flags;
1812
1813 if (unlikely(!mapping))
1814 return;
1815
1816 spin_lock(&mapping->private_lock);
1817 lock_page_memcg(page);
1818 SetPageDirty(page);
1819 spin_unlock(&mapping->private_lock);
1820
1821 spin_lock_irqsave(&mapping->tree_lock, flags);
1822 WARN_ON_ONCE(!PageUptodate(page));
1823 account_page_dirtied(page, mapping);
1824 radix_tree_tag_set(&mapping->page_tree,
1825 page_index(page), PAGECACHE_TAG_DIRTY);
1826 spin_unlock_irqrestore(&mapping->tree_lock, flags);
1827 unlock_page_memcg(page);
1828
1829 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1830 return;
1831}
1832
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001833static int f2fs_set_data_page_dirty(struct page *page)
1834{
1835 struct address_space *mapping = page->mapping;
1836 struct inode *inode = mapping->host;
1837
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001838 trace_f2fs_set_page_dirty(page, DATA);
1839
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001840 if (!PageUptodate(page))
1841 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001842
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001843 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001844 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1845 register_inmem_page(inode, page);
1846 return 1;
1847 }
1848 /*
1849 * Previously, this page has been registered, we just
1850 * return here.
1851 */
1852 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001853 }
1854
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001855 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07001856 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001857 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001858 return 1;
1859 }
1860 return 0;
1861}
1862
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001863static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1864{
Chao Yu454ae7e2014-04-22 13:34:01 +08001865 struct inode *inode = mapping->host;
1866
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001867 if (f2fs_has_inline_data(inode))
1868 return 0;
1869
1870 /* make sure allocating whole blocks */
1871 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1872 filemap_write_and_wait(mapping);
1873
Chao Yue2b4e2b2015-08-19 19:11:19 +08001874 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001875}
1876
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001877const struct address_space_operations f2fs_dblock_aops = {
1878 .readpage = f2fs_read_data_page,
1879 .readpages = f2fs_read_data_pages,
1880 .writepage = f2fs_write_data_page,
1881 .writepages = f2fs_write_data_pages,
1882 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001883 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001884 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001885 .invalidatepage = f2fs_invalidate_page,
1886 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001887 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001888 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001889};