blob: 607ef43973307bb46dbb6fe66d8fe056d1971bd3 [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 Kimf1e88662015-04-09 11:20:42 -070022#include <linux/cleancache.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090023
24#include "f2fs.h"
25#include "node.h"
26#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080027#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090028#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090029
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020030static void f2fs_read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090031{
Linus Torvaldsf5688492014-01-30 11:19:05 -080032 struct bio_vec *bvec;
33 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090034
Chao Yu12377022015-05-25 18:03:38 +080035 if (f2fs_bio_encrypted(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020036 if (bio->bi_error) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070037 fscrypt_release_ctx(bio->bi_private);
Chao Yu12377022015-05-25 18:03:38 +080038 } else {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070039 fscrypt_decrypt_bio_pages(bio->bi_private, bio);
Chao Yu12377022015-05-25 18:03:38 +080040 return;
41 }
42 }
43
Linus Torvaldsf5688492014-01-30 11:19:05 -080044 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090045 struct page *page = bvec->bv_page;
46
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020047 if (!bio->bi_error) {
Linus Torvaldsf5688492014-01-30 11:19:05 -080048 SetPageUptodate(page);
49 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090050 ClearPageUptodate(page);
51 SetPageError(page);
52 }
53 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080054 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090055 bio_put(bio);
56}
57
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020058static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090059{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090060 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080061 struct bio_vec *bvec;
62 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090063
Linus Torvaldsf5688492014-01-30 11:19:05 -080064 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090065 struct page *page = bvec->bv_page;
66
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070067 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -070068
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020069 if (unlikely(bio->bi_error)) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090070 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -070071 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090072 }
73 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080074 }
Jaegeuk Kimf5730182016-05-17 16:23:36 -070075 if (atomic_dec_and_test(&sbi->nr_wb_bios) &&
76 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090077 wake_up(&sbi->cp_wait);
78
79 bio_put(bio);
80}
81
Gu Zheng940a6d32013-12-20 17:39:59 +080082/*
83 * Low-level block read/write IO operations.
84 */
85static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
86 int npages, bool is_read)
87{
88 struct bio *bio;
89
Jaegeuk Kim740432f2015-08-14 11:43:56 -070090 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +080091
92 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +080093 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +080094 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +080095 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +080096
97 return bio;
98}
99
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700100static inline void __submit_bio(struct f2fs_sb_info *sbi, int rw,
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700101 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700102{
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700103 if (!is_read_io(rw)) {
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700104 atomic_inc(&sbi->nr_wb_bios);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700105 if (current->plug && (type == DATA || type == NODE))
106 blk_finish_plug(current->plug);
107 }
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700108 submit_bio(rw, bio);
109}
110
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900111static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900112{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900113 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900114
115 if (!io->bio)
116 return;
117
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700118 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800119 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700120 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800121 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900122
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700123 __submit_bio(io->sbi, fio->rw, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900124 io->bio = NULL;
125}
126
Chao Yu0c3a5792016-01-18 18:28:11 +0800127static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
128 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800129{
Chao Yu0fd785e2016-01-18 18:24:59 +0800130 struct bio_vec *bvec;
131 struct page *target;
132 int i;
133
Chao Yu0c3a5792016-01-18 18:28:11 +0800134 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800135 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800136
137 if (!inode && !page && !ino)
138 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800139
140 bio_for_each_segment_all(bvec, io->bio, i) {
141
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700142 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800143 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700144 else
145 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800146
Chao Yu0c3a5792016-01-18 18:28:11 +0800147 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800148 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800149 if (page && page == target)
150 return true;
151 if (ino && ino == ino_of_node(target))
152 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800153 }
154
Chao Yu0fd785e2016-01-18 18:24:59 +0800155 return false;
156}
157
Chao Yu0c3a5792016-01-18 18:28:11 +0800158static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
159 struct page *page, nid_t ino,
160 enum page_type type)
161{
162 enum page_type btype = PAGE_TYPE_OF_BIO(type);
163 struct f2fs_bio_info *io = &sbi->write_io[btype];
164 bool ret;
165
166 down_read(&io->io_rwsem);
167 ret = __has_merged_page(io, inode, page, ino);
168 up_read(&io->io_rwsem);
169 return ret;
170}
171
172static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
173 struct inode *inode, struct page *page,
174 nid_t ino, enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900175{
176 enum page_type btype = PAGE_TYPE_OF_BIO(type);
177 struct f2fs_bio_info *io;
178
179 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
180
Chao Yudf0f8dc2014-03-22 14:57:23 +0800181 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900182
Chao Yu0c3a5792016-01-18 18:28:11 +0800183 if (!__has_merged_page(io, inode, page, ino))
184 goto out;
185
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900186 /* change META to META_FLUSH in the checkpoint procedure */
187 if (type >= META_FLUSH) {
188 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700189 if (test_opt(sbi, NOBARRIER))
190 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
191 else
192 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900193 }
194 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800195out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800196 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900197}
198
Chao Yu0c3a5792016-01-18 18:28:11 +0800199void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
200 int rw)
201{
202 __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw);
203}
204
205void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
206 struct inode *inode, struct page *page,
207 nid_t ino, enum page_type type, int rw)
208{
209 if (has_merged_page(sbi, inode, page, ino, type))
210 __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw);
211}
212
Chao Yu406657d2016-02-24 17:17:55 +0800213void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
214{
215 f2fs_submit_merged_bio(sbi, DATA, WRITE);
216 f2fs_submit_merged_bio(sbi, NODE, WRITE);
217 f2fs_submit_merged_bio(sbi, META, WRITE);
218}
219
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900220/*
221 * Fill the locked page with data located in the block address.
222 * Return unlocked page.
223 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700224int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900225{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900226 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700227 struct page *page = fio->encrypted_page ?
228 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900229
Chao Yu2ace38e2014-12-24 16:08:14 +0800230 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700231 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900232
233 /* Allocate a new bio */
Chao Yu7a9d7542016-02-22 18:36:38 +0800234 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900235
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300236 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900237 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900238 return -EFAULT;
239 }
240
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700241 __submit_bio(fio->sbi, fio->rw, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900242 return 0;
243}
244
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700245void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900246{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700247 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900248 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900249 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800250 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700251 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900252
Gu Zheng940a6d32013-12-20 17:39:59 +0800253 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900254
Chao Yu7a9d7542016-02-22 18:36:38 +0800255 if (fio->old_blkaddr != NEW_ADDR)
256 verify_block_addr(sbi, fio->old_blkaddr);
257 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900258
Chao Yudf0f8dc2014-03-22 14:57:23 +0800259 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900260
Chao Yu7a9d7542016-02-22 18:36:38 +0800261 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900262 io->fio.rw != fio->rw))
263 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900264alloc_new:
265 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700266 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800267
Chao Yu7a9d7542016-02-22 18:36:38 +0800268 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
269 bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900270 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900271 }
272
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700273 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
274
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300275 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
276 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900277 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900278 goto alloc_new;
279 }
280
Chao Yu7a9d7542016-02-22 18:36:38 +0800281 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700282 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900283
Chao Yudf0f8dc2014-03-22 14:57:23 +0800284 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700285 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900286}
287
Chao Yu46008c62016-05-09 19:56:30 +0800288static void __set_data_blkaddr(struct dnode_of_data *dn)
289{
290 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
291 __le32 *addr_array;
292
293 /* Get physical address of data block */
294 addr_array = blkaddr_in_node(rn);
295 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
296}
297
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900298/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900299 * Lock ordering for the change of data block address:
300 * ->data_page
301 * ->node_page
302 * update block addresses in the node page
303 */
Chao Yu216a6202015-03-19 19:23:32 +0800304void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900305{
Chao Yu46008c62016-05-09 19:56:30 +0800306 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
307 __set_data_blkaddr(dn);
308 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800309 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900310}
311
Chao Yuf28b3432016-02-24 17:16:47 +0800312void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
313{
314 dn->data_blkaddr = blkaddr;
315 set_data_blkaddr(dn);
316 f2fs_update_extent_cache(dn);
317}
318
Chao Yu46008c62016-05-09 19:56:30 +0800319/* dn->ofs_in_node will be returned with up-to-date last block pointer */
320int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900321{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700322 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900323
Chao Yu46008c62016-05-09 19:56:30 +0800324 if (!count)
325 return 0;
326
Jaegeuk Kim91942322016-05-20 10:13:22 -0700327 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900328 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800329 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900330 return -ENOSPC;
331
Chao Yu46008c62016-05-09 19:56:30 +0800332 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
333 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900334
Chao Yu46008c62016-05-09 19:56:30 +0800335 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
336
337 for (; count > 0; dn->ofs_in_node++) {
338 block_t blkaddr =
339 datablock_addr(dn->node_page, dn->ofs_in_node);
340 if (blkaddr == NULL_ADDR) {
341 dn->data_blkaddr = NEW_ADDR;
342 __set_data_blkaddr(dn);
343 count--;
344 }
345 }
346
347 if (set_page_dirty(dn->node_page))
348 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900349 return 0;
350}
351
Chao Yu46008c62016-05-09 19:56:30 +0800352/* Should keep dn->ofs_in_node unchanged */
353int reserve_new_block(struct dnode_of_data *dn)
354{
355 unsigned int ofs_in_node = dn->ofs_in_node;
356 int ret;
357
358 ret = reserve_new_blocks(dn, 1);
359 dn->ofs_in_node = ofs_in_node;
360 return ret;
361}
362
Huajun Lib6009652013-11-10 23:13:18 +0800363int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
364{
365 bool need_put = dn->inode_page ? false : true;
366 int err;
367
368 err = get_dnode_of_data(dn, index, ALLOC_NODE);
369 if (err)
370 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900371
Huajun Lib6009652013-11-10 23:13:18 +0800372 if (dn->data_blkaddr == NULL_ADDR)
373 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900374 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800375 f2fs_put_dnode(dn);
376 return err;
377}
378
Fan Li759af1c2015-08-05 15:52:16 +0800379int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900380{
Chao Yu028a41e2015-03-19 19:26:02 +0800381 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800382 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800383
Fan Li759af1c2015-08-05 15:52:16 +0800384 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
385 dn->data_blkaddr = ei.blk + index - ei.fofs;
386 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800387 }
388
Fan Li759af1c2015-08-05 15:52:16 +0800389 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900390}
391
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700392struct page *get_read_data_page(struct inode *inode, pgoff_t index,
393 int rw, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900394{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900395 struct address_space *mapping = inode->i_mapping;
396 struct dnode_of_data dn;
397 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800398 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900399 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800400 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700401 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800402 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700403 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700404 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800405 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900406
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700407 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
408 return read_mapping_page(mapping, index, NULL);
409
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700410 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900411 if (!page)
412 return ERR_PTR(-ENOMEM);
413
Chao Yucb3bc9e2015-02-05 18:03:40 +0800414 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
415 dn.data_blkaddr = ei.blk + index - ei.fofs;
416 goto got_it;
417 }
418
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900419 set_new_dnode(&dn, inode, NULL, NULL, 0);
420 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700421 if (err)
422 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900423 f2fs_put_dnode(&dn);
424
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900425 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700426 err = -ENOENT;
427 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900428 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800429got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700430 if (PageUptodate(page)) {
431 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900432 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700433 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900434
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900435 /*
436 * A new dentry page is allocated but not able to be written, since its
437 * new inode page couldn't be allocated due to -ENOSPC.
438 * In such the case, its blkaddr can be remained as NEW_ADDR.
439 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
440 */
441 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300442 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900443 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700444 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900445 return page;
446 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900447
Chao Yu7a9d7542016-02-22 18:36:38 +0800448 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700449 fio.page = page;
450 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900451 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700452 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700453 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700454
455put_err:
456 f2fs_put_page(page, 1);
457 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700458}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900459
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700460struct page *find_data_page(struct inode *inode, pgoff_t index)
461{
462 struct address_space *mapping = inode->i_mapping;
463 struct page *page;
464
465 page = find_get_page(mapping, index);
466 if (page && PageUptodate(page))
467 return page;
468 f2fs_put_page(page, 0);
469
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700470 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700471 if (IS_ERR(page))
472 return page;
473
474 if (PageUptodate(page))
475 return page;
476
477 wait_on_page_locked(page);
478 if (unlikely(!PageUptodate(page))) {
479 f2fs_put_page(page, 0);
480 return ERR_PTR(-EIO);
481 }
482 return page;
483}
484
485/*
486 * If it tries to access a hole, return an error.
487 * Because, the callers, functions in dir.c and GC, should be able to know
488 * whether this page exists or not.
489 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700490struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
491 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700492{
493 struct address_space *mapping = inode->i_mapping;
494 struct page *page;
495repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700496 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700497 if (IS_ERR(page))
498 return page;
499
500 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900501 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900502 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900503 f2fs_put_page(page, 1);
504 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900505 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900506 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900507 f2fs_put_page(page, 1);
508 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900509 }
510 return page;
511}
512
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900513/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900514 * Caller ensures that this data page is never allocated.
515 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900516 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800517 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
518 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800519 * Note that, ipage is set only by make_empty_dir, and if any error occur,
520 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900521 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900522struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900523 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900524{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900525 struct address_space *mapping = inode->i_mapping;
526 struct page *page;
527 struct dnode_of_data dn;
528 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800529
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700530 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800531 if (!page) {
532 /*
533 * before exiting, we should make sure ipage will be released
534 * if any error occur.
535 */
536 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700537 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800538 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900539
Jaegeuk Kima8865372013-12-27 17:04:17 +0900540 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800541 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700542 if (err) {
543 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900544 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900545 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700546 if (!ipage)
547 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900548
549 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700550 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900551
552 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300553 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900554 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900555 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700556 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900557
Jaegeuk Kim76121182016-01-01 22:03:47 -0800558 /* if ipage exists, blkaddr should be NEW_ADDR */
559 f2fs_bug_on(F2FS_I_SB(inode), ipage);
560 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700561 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800562 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900563 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700564got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800565 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700566 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700567 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900568 return page;
569}
570
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900571static int __allocate_data_block(struct dnode_of_data *dn)
572{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700573 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900574 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900575 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800576 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700577 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800578 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900579
Jaegeuk Kim91942322016-05-20 10:13:22 -0700580 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900581 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800582
583 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
584 if (dn->data_blkaddr == NEW_ADDR)
585 goto alloc;
586
Chao Yu46008c62016-05-09 19:56:30 +0800587 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900588 return -ENOSPC;
589
Chao Yudf6136e2015-03-23 10:33:37 +0800590alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900591 get_node_info(sbi, dn->nid, &ni);
592 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
593
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800594 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
595 seg = CURSEG_DIRECT_IO;
596
Chao Yudf6136e2015-03-23 10:33:37 +0800597 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
598 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800599 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900600
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700601 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800602 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700603 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300604 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700605 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300606 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900607 return 0;
608}
609
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800610ssize_t f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800611{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800612 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800613 struct f2fs_map_blocks map;
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800614 ssize_t ret = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800615
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700616 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
617 map.m_len = F2FS_BYTES_TO_BLK(iov_iter_count(from));
Chao Yuda859852016-01-26 15:42:58 +0800618 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800619
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800620 if (f2fs_encrypted_inode(inode))
621 return 0;
622
623 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800624 ret = f2fs_convert_inline_inode(inode);
625 if (ret)
626 return ret;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800627 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800628 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800629 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
630 ret = f2fs_convert_inline_inode(inode);
631 if (ret)
632 return ret;
633 }
634 if (!f2fs_has_inline_data(inode))
635 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800636 return ret;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800637}
638
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900639/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700640 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
641 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800642 * If original data blocks are allocated, then give them to blockdev.
643 * Otherwise,
644 * a. preallocate requested block addresses
645 * b. do not use extent cache for better performance
646 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900647 */
Chao Yud323d002015-10-27 09:53:45 +0800648int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800649 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900650{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700651 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900652 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800653 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900654 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
Chao Yu46008c62016-05-09 19:56:30 +0800655 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900656 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800657 unsigned int ofs_in_node, last_ofs_in_node;
658 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800659 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900660 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800661 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900662
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700663 map->m_len = 0;
664 map->m_flags = 0;
665
666 /* it only supports block size == page size */
667 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800668 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900669
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800670 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700671 map->m_pblk = ei.blk + pgofs - ei.fofs;
672 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
673 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900674 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800675 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900676
Chao Yu4fe71e82016-01-26 15:37:38 +0800677next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800678 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800679 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900680
681 /* When reading holes, we need its node page */
682 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900683 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900684 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800685 if (flag == F2FS_GET_BLOCK_BMAP)
686 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800687 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900688 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800689 if (map->m_next_pgofs)
690 *map->m_next_pgofs =
691 get_next_page_offset(&dn, pgofs);
692 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900693 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900694 }
Chao Yu973163f2015-09-18 16:51:51 +0800695
Chao Yu46008c62016-05-09 19:56:30 +0800696 prealloc = 0;
697 ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800698 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900699
Chao Yu4fe71e82016-01-26 15:37:38 +0800700next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800701 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800702
Fan Li7df3a432015-12-17 13:20:59 +0800703 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
704 if (create) {
705 if (unlikely(f2fs_cp_error(sbi))) {
706 err = -EIO;
707 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800708 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800709 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800710 if (blkaddr == NULL_ADDR) {
711 prealloc++;
712 last_ofs_in_node = dn.ofs_in_node;
713 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800714 } else {
715 err = __allocate_data_block(&dn);
Chao Yu46008c62016-05-09 19:56:30 +0800716 if (!err) {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700717 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yu46008c62016-05-09 19:56:30 +0800718 allocated = true;
719 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800720 }
Fan Li7df3a432015-12-17 13:20:59 +0800721 if (err)
722 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800723 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800724 blkaddr = dn.data_blkaddr;
725 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800726 if (flag == F2FS_GET_BLOCK_BMAP) {
727 map->m_pblk = 0;
728 goto sync_out;
729 }
Chao Yuda859852016-01-26 15:42:58 +0800730 if (flag == F2FS_GET_BLOCK_FIEMAP &&
731 blkaddr == NULL_ADDR) {
732 if (map->m_next_pgofs)
733 *map->m_next_pgofs = pgofs + 1;
734 }
Fan Li7df3a432015-12-17 13:20:59 +0800735 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800736 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800737 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900738 }
739 }
Fan Li7df3a432015-12-17 13:20:59 +0800740
Chao Yu46008c62016-05-09 19:56:30 +0800741 if (flag == F2FS_GET_BLOCK_PRE_AIO)
742 goto skip;
743
Chao Yu4fe71e82016-01-26 15:37:38 +0800744 if (map->m_len == 0) {
745 /* preallocated unwritten block should be mapped for fiemap. */
746 if (blkaddr == NEW_ADDR)
747 map->m_flags |= F2FS_MAP_UNWRITTEN;
748 map->m_flags |= F2FS_MAP_MAPPED;
749
750 map->m_pblk = blkaddr;
751 map->m_len = 1;
752 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800753 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800754 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800755 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800756 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800757 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800758 } else {
759 goto sync_out;
760 }
761
Chao Yu46008c62016-05-09 19:56:30 +0800762skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800763 dn.ofs_in_node++;
764 pgofs++;
765
Chao Yu46008c62016-05-09 19:56:30 +0800766 /* preallocate blocks in batch for one dnode page */
767 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
768 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800769
Chao Yu46008c62016-05-09 19:56:30 +0800770 dn.ofs_in_node = ofs_in_node;
771 err = reserve_new_blocks(&dn, prealloc);
772 if (err)
773 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800774
Chao Yu46008c62016-05-09 19:56:30 +0800775 map->m_len += dn.ofs_in_node - ofs_in_node;
776 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
777 err = -ENOSPC;
778 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800779 }
Chao Yu46008c62016-05-09 19:56:30 +0800780 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800781 }
782
Chao Yu46008c62016-05-09 19:56:30 +0800783 if (pgofs >= end)
784 goto sync_out;
785 else if (dn.ofs_in_node < end_offset)
786 goto next_block;
787
Chao Yu46008c62016-05-09 19:56:30 +0800788 f2fs_put_dnode(&dn);
789
790 if (create) {
791 f2fs_unlock_op(sbi);
792 f2fs_balance_fs(sbi, allocated);
793 }
794 allocated = false;
795 goto next_dnode;
796
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900797sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900798 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900799unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800800 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800801 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800802 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800803 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900804out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700805 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900806 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900807}
808
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700809static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800810 struct buffer_head *bh, int create, int flag,
811 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700812{
813 struct f2fs_map_blocks map;
814 int ret;
815
816 map.m_lblk = iblock;
817 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800818 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700819
Chao Yue2b4e2b2015-08-19 19:11:19 +0800820 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700821 if (!ret) {
822 map_bh(bh, inode->i_sb, map.m_pblk);
823 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
824 bh->b_size = map.m_len << inode->i_blkbits;
825 }
826 return ret;
827}
828
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900829static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800830 struct buffer_head *bh_result, int create, int flag,
831 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900832{
Chao Yuda859852016-01-26 15:42:58 +0800833 return __get_data_block(inode, iblock, bh_result, create,
834 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900835}
836
Chao Yue2b4e2b2015-08-19 19:11:19 +0800837static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900838 struct buffer_head *bh_result, int create)
839{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800840 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800841 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800842}
843
844static int get_data_block_bmap(struct inode *inode, sector_t iblock,
845 struct buffer_head *bh_result, int create)
846{
Yunlei He179448b2015-12-28 21:48:32 +0800847 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800848 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800849 return -EFBIG;
850
Chao Yue2b4e2b2015-08-19 19:11:19 +0800851 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800852 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900853}
854
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700855static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
856{
857 return (offset >> inode->i_blkbits);
858}
859
860static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
861{
862 return (blk << inode->i_blkbits);
863}
864
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900865int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
866 u64 start, u64 len)
867{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700868 struct buffer_head map_bh;
869 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800870 pgoff_t next_pgofs;
Fan Lide1475c2016-01-04 15:56:50 +0800871 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700872 u64 logical = 0, phys = 0, size = 0;
873 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700874 int ret = 0;
875
876 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
877 if (ret)
878 return ret;
879
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -0700880 if (f2fs_has_inline_data(inode)) {
881 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
882 if (ret != -EAGAIN)
883 return ret;
884 }
885
Al Viro59551022016-01-22 15:40:57 -0500886 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800887
888 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800889 if (start >= isize)
890 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700891
Fan Li9a950d52015-12-26 18:07:41 +0800892 if (start + len > isize)
893 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700894
895 if (logical_to_blk(inode, len) == 0)
896 len = blk_to_logical(inode, 1);
897
898 start_blk = logical_to_blk(inode, start);
899 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800900
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700901next:
902 memset(&map_bh, 0, sizeof(struct buffer_head));
903 map_bh.b_size = len;
904
Chao Yue2b4e2b2015-08-19 19:11:19 +0800905 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800906 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700907 if (ret)
908 goto out;
909
910 /* HOLE */
911 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800912 start_blk = next_pgofs;
Fan Li9a950d52015-12-26 18:07:41 +0800913 /* Go through holes util pass the EOF */
Chao Yuda859852016-01-26 15:42:58 +0800914 if (blk_to_logical(inode, start_blk) < isize)
Fan Li9a950d52015-12-26 18:07:41 +0800915 goto prep_next;
916 /* Found a hole beyond isize means no more extents.
917 * Note that the premise is that filesystems don't
918 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700919 */
Fan Li9a950d52015-12-26 18:07:41 +0800920 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700921 }
Fan Li9a950d52015-12-26 18:07:41 +0800922
Chao Yuda5af122016-01-08 20:19:27 +0800923 if (size) {
924 if (f2fs_encrypted_inode(inode))
925 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
926
Fan Li9a950d52015-12-26 18:07:41 +0800927 ret = fiemap_fill_next_extent(fieinfo, logical,
928 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800929 }
Fan Li9a950d52015-12-26 18:07:41 +0800930
931 if (start_blk > last_blk || ret)
932 goto out;
933
934 logical = blk_to_logical(inode, start_blk);
935 phys = blk_to_logical(inode, map_bh.b_blocknr);
936 size = map_bh.b_size;
937 flags = 0;
938 if (buffer_unwritten(&map_bh))
939 flags = FIEMAP_EXTENT_UNWRITTEN;
940
941 start_blk += logical_to_blk(inode, size);
942
943prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700944 cond_resched();
945 if (fatal_signal_pending(current))
946 ret = -EINTR;
947 else
948 goto next;
949out:
950 if (ret == 1)
951 ret = 0;
952
Al Viro59551022016-01-22 15:40:57 -0500953 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700954 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900955}
956
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700957/*
958 * This function was originally taken from fs/mpage.c, and customized for f2fs.
959 * Major change was from block_size == page_size in f2fs by default.
960 */
961static int f2fs_mpage_readpages(struct address_space *mapping,
962 struct list_head *pages, struct page *page,
963 unsigned nr_pages)
964{
965 struct bio *bio = NULL;
966 unsigned page_idx;
967 sector_t last_block_in_bio = 0;
968 struct inode *inode = mapping->host;
969 const unsigned blkbits = inode->i_blkbits;
970 const unsigned blocksize = 1 << blkbits;
971 sector_t block_in_file;
972 sector_t last_block;
973 sector_t last_block_in_file;
974 sector_t block_nr;
975 struct block_device *bdev = inode->i_sb->s_bdev;
976 struct f2fs_map_blocks map;
977
978 map.m_pblk = 0;
979 map.m_lblk = 0;
980 map.m_len = 0;
981 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +0800982 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700983
984 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
985
986 prefetchw(&page->flags);
987 if (pages) {
988 page = list_entry(pages->prev, struct page, lru);
989 list_del(&page->lru);
990 if (add_to_page_cache_lru(page, mapping,
991 page->index, GFP_KERNEL))
992 goto next_page;
993 }
994
995 block_in_file = (sector_t)page->index;
996 last_block = block_in_file + nr_pages;
997 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
998 blkbits;
999 if (last_block > last_block_in_file)
1000 last_block = last_block_in_file;
1001
1002 /*
1003 * Map blocks using the previous result first.
1004 */
1005 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1006 block_in_file > map.m_lblk &&
1007 block_in_file < (map.m_lblk + map.m_len))
1008 goto got_it;
1009
1010 /*
1011 * Then do more f2fs_map_blocks() calls until we are
1012 * done with this page.
1013 */
1014 map.m_flags = 0;
1015
1016 if (block_in_file < last_block) {
1017 map.m_lblk = block_in_file;
1018 map.m_len = last_block - block_in_file;
1019
Chao Yu46c9e142015-09-18 16:54:16 +08001020 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001021 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001022 goto set_error_page;
1023 }
1024got_it:
1025 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1026 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1027 SetPageMappedToDisk(page);
1028
1029 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1030 SetPageUptodate(page);
1031 goto confused;
1032 }
1033 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001034 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001035 SetPageUptodate(page);
1036 unlock_page(page);
1037 goto next_page;
1038 }
1039
1040 /*
1041 * This page will go to BIO. Do we need to send this
1042 * BIO off first?
1043 */
1044 if (bio && (last_block_in_bio != block_nr - 1)) {
1045submit_and_realloc:
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -07001046 __submit_bio(F2FS_I_SB(inode), READ, bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001047 bio = NULL;
1048 }
1049 if (bio == NULL) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001050 struct fscrypt_ctx *ctx = NULL;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001051
1052 if (f2fs_encrypted_inode(inode) &&
1053 S_ISREG(inode->i_mode)) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001054
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001055 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001056 if (IS_ERR(ctx))
1057 goto set_error_page;
1058
1059 /* wait the page to be moved by cleaning */
Chao Yu08b39fb2015-10-08 13:27:34 +08001060 f2fs_wait_on_encrypted_page_writeback(
1061 F2FS_I_SB(inode), block_nr);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001062 }
1063
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001064 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +02001065 min_t(int, nr_pages, BIO_MAX_PAGES));
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001066 if (!bio) {
1067 if (ctx)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001068 fscrypt_release_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001069 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001070 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001071 bio->bi_bdev = bdev;
1072 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +08001073 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001074 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001075 }
1076
1077 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1078 goto submit_and_realloc;
1079
1080 last_block_in_bio = block_nr;
1081 goto next_page;
1082set_error_page:
1083 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001084 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001085 unlock_page(page);
1086 goto next_page;
1087confused:
1088 if (bio) {
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -07001089 __submit_bio(F2FS_I_SB(inode), READ, bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001090 bio = NULL;
1091 }
1092 unlock_page(page);
1093next_page:
1094 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001095 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001096 }
1097 BUG_ON(pages && !list_empty(pages));
1098 if (bio)
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -07001099 __submit_bio(F2FS_I_SB(inode), READ, bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001100 return 0;
1101}
1102
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001103static int f2fs_read_data_page(struct file *file, struct page *page)
1104{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001105 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001106 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001107
Chao Yuc20e89c2014-05-06 16:53:08 +08001108 trace_f2fs_readpage(page, DATA);
1109
arter97e1c42042014-08-06 23:22:50 +09001110 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001111 if (f2fs_has_inline_data(inode))
1112 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001113 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001114 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001115 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001116}
1117
1118static int f2fs_read_data_pages(struct file *file,
1119 struct address_space *mapping,
1120 struct list_head *pages, unsigned nr_pages)
1121{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001122 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001123 struct page *page = list_entry(pages->prev, struct page, lru);
1124
1125 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001126
1127 /* If the file has inline data, skip readpages */
1128 if (f2fs_has_inline_data(inode))
1129 return 0;
1130
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001131 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001132}
1133
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001134int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001135{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001136 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001137 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001138 struct dnode_of_data dn;
1139 int err = 0;
1140
1141 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001142 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001143 if (err)
1144 return err;
1145
Chao Yu28bc1062016-02-06 14:40:34 +08001146 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001147
1148 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001149 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001150 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001151 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001152 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001153
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001154 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001155 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001156
1157 /* wait for GCed encrypted page writeback */
1158 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001159 fio->old_blkaddr);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001160retry_encrypt:
1161 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1162 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001163 if (IS_ERR(fio->encrypted_page)) {
1164 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001165 if (err == -ENOMEM) {
1166 /* flush pending ios and wait for a while */
1167 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1168 congestion_wait(BLK_RW_ASYNC, HZ/50);
1169 gfp_flags |= __GFP_NOFAIL;
1170 err = 0;
1171 goto retry_encrypt;
1172 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001173 goto out_writepage;
1174 }
1175 }
1176
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001177 set_page_writeback(page);
1178
1179 /*
1180 * If current allocation needs SSR,
1181 * it had better in-place writes for updated data.
1182 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001183 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001184 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001185 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001186 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001187 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001188 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001189 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001190 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001191 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001192 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001193 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001194 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001195 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001196 }
1197out_writepage:
1198 f2fs_put_dnode(&dn);
1199 return err;
1200}
1201
1202static int f2fs_write_data_page(struct page *page,
1203 struct writeback_control *wbc)
1204{
1205 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001206 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001207 loff_t i_size = i_size_read(inode);
1208 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001209 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001210 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001211 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001212 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001213 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001214 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001215 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001216 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001217 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001218 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001219 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001220 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001221
Chao Yuecda0de2014-05-06 16:48:26 +08001222 trace_f2fs_writepage(page, DATA);
1223
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001224 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001225 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001226
1227 /*
1228 * If the offset is out-of-range of file size,
1229 * this page does not have to be written to disk.
1230 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001231 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001232 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001233 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001234
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001235 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001236write:
Chao Yucaf00472015-01-28 17:48:42 +08001237 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001238 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001239 if (f2fs_is_drop_cache(inode))
1240 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001241 /* we should not write 0'th page having journal header */
1242 if (f2fs_is_volatile_file(inode) && (!page->index ||
1243 (!wbc->for_reclaim &&
1244 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001245 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001246
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001247 /* we should bypass data pages to proceed the kworkder jobs */
1248 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001249 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001250 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001251 }
1252
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001253 /* Dentry blocks are controlled by checkpoint */
1254 if (S_ISDIR(inode->i_mode)) {
1255 err = do_write_data_page(&fio);
1256 goto done;
1257 }
1258
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001259 if (!wbc->for_reclaim)
1260 need_balance_fs = true;
1261 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001262 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001263
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001264 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001265 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001266 if (f2fs_has_inline_data(inode))
1267 err = f2fs_write_inline_data(inode, page);
1268 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001269 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001270 if (F2FS_I(inode)->last_disk_size < psize)
1271 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001272 f2fs_unlock_op(sbi);
1273done:
1274 if (err && err != -ENOENT)
1275 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001276
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001277 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001278out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001279 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001280 if (err)
1281 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001282
1283 if (wbc->for_reclaim) {
1284 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001285 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001286 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001287
1288 unlock_page(page);
1289 f2fs_balance_fs(sbi, need_balance_fs);
1290
1291 if (unlikely(f2fs_cp_error(sbi)))
1292 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1293
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001294 return 0;
1295
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001296redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001297 redirty_page_for_writepage(wbc, page);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001298 unlock_page(page);
1299 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001300}
1301
Chao Yu8f46dca2015-07-14 18:56:10 +08001302/*
1303 * This function was copied from write_cche_pages from mm/page-writeback.c.
1304 * The major change is making write step of cold data page separately from
1305 * warm/hot data page.
1306 */
1307static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001308 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001309{
1310 int ret = 0;
1311 int done = 0;
1312 struct pagevec pvec;
1313 int nr_pages;
1314 pgoff_t uninitialized_var(writeback_index);
1315 pgoff_t index;
1316 pgoff_t end; /* Inclusive */
1317 pgoff_t done_index;
1318 int cycled;
1319 int range_whole = 0;
1320 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001321
1322 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001323
Chao Yu8f46dca2015-07-14 18:56:10 +08001324 if (wbc->range_cyclic) {
1325 writeback_index = mapping->writeback_index; /* prev offset */
1326 index = writeback_index;
1327 if (index == 0)
1328 cycled = 1;
1329 else
1330 cycled = 0;
1331 end = -1;
1332 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001333 index = wbc->range_start >> PAGE_SHIFT;
1334 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001335 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1336 range_whole = 1;
1337 cycled = 1; /* ignore range_cyclic tests */
1338 }
1339 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1340 tag = PAGECACHE_TAG_TOWRITE;
1341 else
1342 tag = PAGECACHE_TAG_DIRTY;
1343retry:
1344 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1345 tag_pages_for_writeback(mapping, index, end);
1346 done_index = index;
1347 while (!done && (index <= end)) {
1348 int i;
1349
1350 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1351 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1352 if (nr_pages == 0)
1353 break;
1354
1355 for (i = 0; i < nr_pages; i++) {
1356 struct page *page = pvec.pages[i];
1357
1358 if (page->index > end) {
1359 done = 1;
1360 break;
1361 }
1362
1363 done_index = page->index;
1364
1365 lock_page(page);
1366
1367 if (unlikely(page->mapping != mapping)) {
1368continue_unlock:
1369 unlock_page(page);
1370 continue;
1371 }
1372
1373 if (!PageDirty(page)) {
1374 /* someone wrote it for us */
1375 goto continue_unlock;
1376 }
1377
Chao Yu8f46dca2015-07-14 18:56:10 +08001378 if (PageWriteback(page)) {
1379 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001380 f2fs_wait_on_page_writeback(page,
1381 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001382 else
1383 goto continue_unlock;
1384 }
1385
1386 BUG_ON(PageWriteback(page));
1387 if (!clear_page_dirty_for_io(page))
1388 goto continue_unlock;
1389
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001390 ret = mapping->a_ops->writepage(page, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001391 if (unlikely(ret)) {
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001392 done_index = page->index + 1;
1393 done = 1;
1394 break;
Chao Yu8f46dca2015-07-14 18:56:10 +08001395 }
1396
1397 if (--wbc->nr_to_write <= 0 &&
1398 wbc->sync_mode == WB_SYNC_NONE) {
1399 done = 1;
1400 break;
1401 }
1402 }
1403 pagevec_release(&pvec);
1404 cond_resched();
1405 }
1406
Chao Yu8f46dca2015-07-14 18:56:10 +08001407 if (!cycled && !done) {
1408 cycled = 1;
1409 index = 0;
1410 end = writeback_index - 1;
1411 goto retry;
1412 }
1413 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1414 mapping->writeback_index = done_index;
1415
1416 return ret;
1417}
1418
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001419static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001420 struct writeback_control *wbc)
1421{
1422 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001423 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001424 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001425
P J Pcfb185a2013-04-03 11:38:00 +09001426 /* deal with chardevs and other special file */
1427 if (!mapping->a_ops->writepage)
1428 return 0;
1429
Chao Yu6a290542015-07-17 18:02:39 +08001430 /* skip writing if there is no dirty page in this inode */
1431 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1432 return 0;
1433
Jaegeuk Kima1257022015-10-08 10:40:07 -07001434 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1435 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1436 available_free_memory(sbi, DIRTY_DENTS))
1437 goto skip_write;
1438
Chao Yud323d002015-10-27 09:53:45 +08001439 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001440 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001441 goto skip_write;
1442
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001443 /* during POR, we don't need to trigger writepage at all. */
1444 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1445 goto skip_write;
1446
Yunlei Hed31c7c32016-02-04 16:14:00 +08001447 trace_f2fs_writepages(mapping->host, wbc, DATA);
1448
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001449 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001450 /*
1451 * if some pages were truncated, we cannot guarantee its mapping->host
1452 * to detect pending bios.
1453 */
1454 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001455
Chao Yuc227f912015-12-16 13:09:20 +08001456 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001457 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001458
1459skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001460 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001461 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001462 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001463}
1464
Chao Yu3aab8f82014-07-02 13:25:04 +08001465static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1466{
1467 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001468 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001469
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001470 if (to > i_size) {
1471 truncate_pagecache(inode, i_size);
1472 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001473 }
1474}
1475
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001476static int prepare_write_begin(struct f2fs_sb_info *sbi,
1477 struct page *page, loff_t pos, unsigned len,
1478 block_t *blk_addr, bool *node_changed)
1479{
1480 struct inode *inode = page->mapping->host;
1481 pgoff_t index = page->index;
1482 struct dnode_of_data dn;
1483 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001484 bool locked = false;
1485 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001486 int err = 0;
1487
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001488 /*
1489 * we already allocated all the blocks, so we don't need to get
1490 * the block addresses when there is no need to fill the page.
1491 */
1492 if (!f2fs_has_inline_data(inode) && !f2fs_encrypted_inode(inode) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001493 len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001494 return 0;
1495
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001496 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001497 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001498 f2fs_lock_op(sbi);
1499 locked = true;
1500 }
1501restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001502 /* check inline_data */
1503 ipage = get_node_page(sbi, inode->i_ino);
1504 if (IS_ERR(ipage)) {
1505 err = PTR_ERR(ipage);
1506 goto unlock_out;
1507 }
1508
1509 set_new_dnode(&dn, inode, ipage, ipage, 0);
1510
1511 if (f2fs_has_inline_data(inode)) {
1512 if (pos + len <= MAX_INLINE_DATA) {
1513 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001514 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001515 if (inode->i_nlink)
1516 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001517 } else {
1518 err = f2fs_convert_inline_page(&dn, page);
1519 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001520 goto out;
1521 if (dn.data_blkaddr == NULL_ADDR)
1522 err = f2fs_get_block(&dn, index);
1523 }
1524 } else if (locked) {
1525 err = f2fs_get_block(&dn, index);
1526 } else {
1527 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1528 dn.data_blkaddr = ei.blk + index - ei.fofs;
1529 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001530 /* hole case */
1531 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001532 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001533 f2fs_put_dnode(&dn);
1534 f2fs_lock_op(sbi);
1535 locked = true;
1536 goto restart;
1537 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001538 }
1539 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001540
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001541 /* convert_inline_page can make node_changed */
1542 *blk_addr = dn.data_blkaddr;
1543 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001544out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001545 f2fs_put_dnode(&dn);
1546unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001547 if (locked)
1548 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001549 return err;
1550}
1551
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001552static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1553 loff_t pos, unsigned len, unsigned flags,
1554 struct page **pagep, void **fsdata)
1555{
1556 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001557 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001558 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001559 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001560 bool need_balance = false;
1561 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001562 int err = 0;
1563
Chao Yu62aed042014-05-06 16:46:04 +08001564 trace_f2fs_write_begin(inode, pos, len, flags);
1565
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001566 /*
1567 * We should check this at this moment to avoid deadlock on inode page
1568 * and #0 page. The locking rule for inline_data conversion should be:
1569 * lock_page(page #0) -> lock_page(inode_page)
1570 */
1571 if (index != 0) {
1572 err = f2fs_convert_inline_inode(inode);
1573 if (err)
1574 goto fail;
1575 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001576repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001577 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001578 if (!page) {
1579 err = -ENOMEM;
1580 goto fail;
1581 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001582
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001583 *pagep = page;
1584
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001585 err = prepare_write_begin(sbi, page, pos, len,
1586 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001587 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001588 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001589
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001590 if (need_balance && has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001591 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001592 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001593 lock_page(page);
1594 if (page->mapping != mapping) {
1595 /* The page got truncated from under us */
1596 f2fs_put_page(page, 1);
1597 goto repeat;
1598 }
1599 }
1600
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001601 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001602
Chao Yu08b39fb2015-10-08 13:27:34 +08001603 /* wait for GCed encrypted page writeback */
1604 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001605 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001606
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001607 if (len == PAGE_SIZE)
Chao Yu90d43882015-07-08 18:24:38 +08001608 goto out_update;
1609 if (PageUptodate(page))
1610 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001611
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001612 if ((pos & PAGE_MASK) >= i_size_read(inode)) {
1613 unsigned start = pos & (PAGE_SIZE - 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001614 unsigned end = start + len;
1615
1616 /* Reading beyond i_size is simple: memset to zero */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001617 zero_user_segments(page, 0, start, end, PAGE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001618 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001619 }
1620
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001621 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001622 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001623 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001624 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001625 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001626 .type = DATA,
1627 .rw = READ_SYNC,
Chao Yu7a9d7542016-02-22 18:36:38 +08001628 .old_blkaddr = blkaddr,
1629 .new_blkaddr = blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001630 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001631 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001632 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001633 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001634 if (err)
1635 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001636
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001637 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001638 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001639 err = -EIO;
1640 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001641 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001642 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001643 f2fs_put_page(page, 1);
1644 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001645 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001646
1647 /* avoid symlink page */
1648 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001649 err = fscrypt_decrypt_page(page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001650 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001651 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001652 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001653 }
Chao Yu90d43882015-07-08 18:24:38 +08001654out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001655 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001656out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001657 clear_cold_data(page);
1658 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001659
Chao Yu3aab8f82014-07-02 13:25:04 +08001660fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001661 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001662 f2fs_write_failed(mapping, pos + len);
1663 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001664}
1665
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001666static int f2fs_write_end(struct file *file,
1667 struct address_space *mapping,
1668 loff_t pos, unsigned len, unsigned copied,
1669 struct page *page, void *fsdata)
1670{
1671 struct inode *inode = page->mapping->host;
1672
Chao Yudfb2bf32014-05-06 16:47:23 +08001673 trace_f2fs_write_end(inode, pos, len, copied);
1674
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001675 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001676
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001677 if (pos + copied > i_size_read(inode))
1678 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001679
Chao Yu75c3c8b2013-11-16 14:15:59 +08001680 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001681 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001682 return copied;
1683}
1684
Omar Sandoval6f673762015-03-16 04:33:52 -07001685static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1686 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001687{
1688 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001689
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001690 if (offset & blocksize_mask)
1691 return -EINVAL;
1692
Al Viro5b46f252014-03-16 18:07:34 -04001693 if (iov_iter_alignment(iter) & blocksize_mask)
1694 return -EINVAL;
1695
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001696 return 0;
1697}
1698
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001699static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001700{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001701 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001702 struct inode *inode = mapping->host;
1703 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001704 loff_t offset = iocb->ki_pos;
Chao Yu3aab8f82014-07-02 13:25:04 +08001705 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001706
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001707 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001708 if (err)
1709 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001710
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001711 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1712 return 0;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001713 if (test_opt(F2FS_I_SB(inode), LFS))
1714 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001715
Omar Sandoval6f673762015-03-16 04:33:52 -07001716 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001717
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001718 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001719 if (iov_iter_rw(iter) == WRITE) {
1720 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001721 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001722 else if (err < 0)
1723 f2fs_write_failed(mapping, offset + count);
1724 }
Chao Yu70407fa2014-07-31 21:11:22 +08001725
Omar Sandoval6f673762015-03-16 04:33:52 -07001726 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001727
Chao Yu3aab8f82014-07-02 13:25:04 +08001728 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001729}
1730
Chao Yu487261f2015-02-05 17:44:29 +08001731void f2fs_invalidate_page(struct page *page, unsigned int offset,
1732 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001733{
1734 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001735 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001736
Chao Yu487261f2015-02-05 17:44:29 +08001737 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001738 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001739 return;
1740
Chao Yu487261f2015-02-05 17:44:29 +08001741 if (PageDirty(page)) {
1742 if (inode->i_ino == F2FS_META_INO(sbi))
1743 dec_page_count(sbi, F2FS_DIRTY_META);
1744 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1745 dec_page_count(sbi, F2FS_DIRTY_NODES);
1746 else
1747 inode_dec_dirty_pages(inode);
1748 }
Chao Yudecd36b2015-08-07 18:42:09 +08001749
1750 /* This is atomic written page, keep Private */
1751 if (IS_ATOMIC_WRITTEN_PAGE(page))
1752 return;
1753
Chao Yu23dc9742016-04-29 20:09:15 +08001754 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001755 ClearPagePrivate(page);
1756}
1757
Chao Yu487261f2015-02-05 17:44:29 +08001758int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001759{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001760 /* If this is dirty page, keep PagePrivate */
1761 if (PageDirty(page))
1762 return 0;
1763
Chao Yudecd36b2015-08-07 18:42:09 +08001764 /* This is atomic written page, keep Private */
1765 if (IS_ATOMIC_WRITTEN_PAGE(page))
1766 return 0;
1767
Chao Yu23dc9742016-04-29 20:09:15 +08001768 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001769 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001770 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001771}
1772
1773static int f2fs_set_data_page_dirty(struct page *page)
1774{
1775 struct address_space *mapping = page->mapping;
1776 struct inode *inode = mapping->host;
1777
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001778 trace_f2fs_set_page_dirty(page, DATA);
1779
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001780 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001781
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001782 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001783 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1784 register_inmem_page(inode, page);
1785 return 1;
1786 }
1787 /*
1788 * Previously, this page has been registered, we just
1789 * return here.
1790 */
1791 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001792 }
1793
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001794 if (!PageDirty(page)) {
1795 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001796 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001797 return 1;
1798 }
1799 return 0;
1800}
1801
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001802static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1803{
Chao Yu454ae7e2014-04-22 13:34:01 +08001804 struct inode *inode = mapping->host;
1805
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001806 if (f2fs_has_inline_data(inode))
1807 return 0;
1808
1809 /* make sure allocating whole blocks */
1810 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1811 filemap_write_and_wait(mapping);
1812
Chao Yue2b4e2b2015-08-19 19:11:19 +08001813 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001814}
1815
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001816const struct address_space_operations f2fs_dblock_aops = {
1817 .readpage = f2fs_read_data_page,
1818 .readpages = f2fs_read_data_pages,
1819 .writepage = f2fs_write_data_page,
1820 .writepages = f2fs_write_data_pages,
1821 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001822 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001823 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001824 .invalidatepage = f2fs_invalidate_page,
1825 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001826 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001827 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001828};