blob: 9a8bbc1fb1faa6865665285b37217f7993692431 [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,
101 struct bio *bio)
102{
103 if (!is_read_io(rw))
104 atomic_inc(&sbi->nr_wb_bios);
105 submit_bio(rw, bio);
106}
107
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900108static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900109{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900110 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900111
112 if (!io->bio)
113 return;
114
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700115 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800116 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700117 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800118 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900119
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700120 __submit_bio(io->sbi, fio->rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900121 io->bio = NULL;
122}
123
Chao Yu0c3a5792016-01-18 18:28:11 +0800124static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
125 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800126{
Chao Yu0fd785e2016-01-18 18:24:59 +0800127 struct bio_vec *bvec;
128 struct page *target;
129 int i;
130
Chao Yu0c3a5792016-01-18 18:28:11 +0800131 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800132 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800133
134 if (!inode && !page && !ino)
135 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800136
137 bio_for_each_segment_all(bvec, io->bio, i) {
138
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700139 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800140 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700141 else
142 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800143
Chao Yu0c3a5792016-01-18 18:28:11 +0800144 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800145 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800146 if (page && page == target)
147 return true;
148 if (ino && ino == ino_of_node(target))
149 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800150 }
151
Chao Yu0fd785e2016-01-18 18:24:59 +0800152 return false;
153}
154
Chao Yu0c3a5792016-01-18 18:28:11 +0800155static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
156 struct page *page, nid_t ino,
157 enum page_type type)
158{
159 enum page_type btype = PAGE_TYPE_OF_BIO(type);
160 struct f2fs_bio_info *io = &sbi->write_io[btype];
161 bool ret;
162
163 down_read(&io->io_rwsem);
164 ret = __has_merged_page(io, inode, page, ino);
165 up_read(&io->io_rwsem);
166 return ret;
167}
168
169static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
170 struct inode *inode, struct page *page,
171 nid_t ino, enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900172{
173 enum page_type btype = PAGE_TYPE_OF_BIO(type);
174 struct f2fs_bio_info *io;
175
176 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
177
Chao Yudf0f8dc2014-03-22 14:57:23 +0800178 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900179
Chao Yu0c3a5792016-01-18 18:28:11 +0800180 if (!__has_merged_page(io, inode, page, ino))
181 goto out;
182
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900183 /* change META to META_FLUSH in the checkpoint procedure */
184 if (type >= META_FLUSH) {
185 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700186 if (test_opt(sbi, NOBARRIER))
187 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
188 else
189 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900190 }
191 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800192out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800193 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900194}
195
Chao Yu0c3a5792016-01-18 18:28:11 +0800196void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
197 int rw)
198{
199 __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw);
200}
201
202void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
203 struct inode *inode, struct page *page,
204 nid_t ino, enum page_type type, int rw)
205{
206 if (has_merged_page(sbi, inode, page, ino, type))
207 __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw);
208}
209
Chao Yu406657d2016-02-24 17:17:55 +0800210void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
211{
212 f2fs_submit_merged_bio(sbi, DATA, WRITE);
213 f2fs_submit_merged_bio(sbi, NODE, WRITE);
214 f2fs_submit_merged_bio(sbi, META, WRITE);
215}
216
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900217/*
218 * Fill the locked page with data located in the block address.
219 * Return unlocked page.
220 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700221int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900222{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900223 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700224 struct page *page = fio->encrypted_page ?
225 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900226
Chao Yu2ace38e2014-12-24 16:08:14 +0800227 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700228 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900229
230 /* Allocate a new bio */
Chao Yu7a9d7542016-02-22 18:36:38 +0800231 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900232
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900234 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900235 return -EFAULT;
236 }
237
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700238 __submit_bio(fio->sbi, fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900239 return 0;
240}
241
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700242void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900243{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700244 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900245 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900246 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800247 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700248 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900249
Gu Zheng940a6d32013-12-20 17:39:59 +0800250 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900251
Chao Yu7a9d7542016-02-22 18:36:38 +0800252 if (fio->old_blkaddr != NEW_ADDR)
253 verify_block_addr(sbi, fio->old_blkaddr);
254 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900255
Chao Yudf0f8dc2014-03-22 14:57:23 +0800256 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900257
Chao Yu7a9d7542016-02-22 18:36:38 +0800258 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900259 io->fio.rw != fio->rw))
260 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900261alloc_new:
262 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700263 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800264
Chao Yu7a9d7542016-02-22 18:36:38 +0800265 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
266 bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900267 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900268 }
269
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700270 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
271
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300272 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
273 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900274 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900275 goto alloc_new;
276 }
277
Chao Yu7a9d7542016-02-22 18:36:38 +0800278 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700279 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900280
Chao Yudf0f8dc2014-03-22 14:57:23 +0800281 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700282 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900283}
284
Chao Yu46008c62016-05-09 19:56:30 +0800285static void __set_data_blkaddr(struct dnode_of_data *dn)
286{
287 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
288 __le32 *addr_array;
289
290 /* Get physical address of data block */
291 addr_array = blkaddr_in_node(rn);
292 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
293}
294
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900295/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900296 * Lock ordering for the change of data block address:
297 * ->data_page
298 * ->node_page
299 * update block addresses in the node page
300 */
Chao Yu216a6202015-03-19 19:23:32 +0800301void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900302{
Chao Yu46008c62016-05-09 19:56:30 +0800303 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
304 __set_data_blkaddr(dn);
305 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800306 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900307}
308
Chao Yuf28b3432016-02-24 17:16:47 +0800309void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
310{
311 dn->data_blkaddr = blkaddr;
312 set_data_blkaddr(dn);
313 f2fs_update_extent_cache(dn);
314}
315
Chao Yu46008c62016-05-09 19:56:30 +0800316/* dn->ofs_in_node will be returned with up-to-date last block pointer */
317int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900318{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700319 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900320
Chao Yu46008c62016-05-09 19:56:30 +0800321 if (!count)
322 return 0;
323
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900324 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900325 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800326 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900327 return -ENOSPC;
328
Chao Yu46008c62016-05-09 19:56:30 +0800329 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
330 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900331
Chao Yu46008c62016-05-09 19:56:30 +0800332 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
333
334 for (; count > 0; dn->ofs_in_node++) {
335 block_t blkaddr =
336 datablock_addr(dn->node_page, dn->ofs_in_node);
337 if (blkaddr == NULL_ADDR) {
338 dn->data_blkaddr = NEW_ADDR;
339 __set_data_blkaddr(dn);
340 count--;
341 }
342 }
343
344 if (set_page_dirty(dn->node_page))
345 dn->node_changed = true;
346
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900347 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900348 sync_inode_page(dn);
349 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) <
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300566 ((loff_t)(index + 1) << PAGE_SHIFT)) {
567 i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900568 /* Only the directory inode sets new_i_size */
569 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900570 }
571 return page;
572}
573
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900574static int __allocate_data_block(struct dnode_of_data *dn)
575{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700576 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900577 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900578 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800579 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700580 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800581 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900582
583 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
584 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800585
586 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
587 if (dn->data_blkaddr == NEW_ADDR)
588 goto alloc;
589
Chao Yu46008c62016-05-09 19:56:30 +0800590 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900591 return -ENOSPC;
592
Chao Yudf6136e2015-03-23 10:33:37 +0800593alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900594 get_node_info(sbi, dn->nid, &ni);
595 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
596
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800597 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
598 seg = CURSEG_DIRECT_IO;
599
Chao Yudf6136e2015-03-23 10:33:37 +0800600 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
601 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800602 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900603
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700604 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800605 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700606 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300607 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Chao Yu9edcdab2015-09-11 14:43:52 +0800608 i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300609 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900610 return 0;
611}
612
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800613ssize_t f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800614{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800615 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800616 struct f2fs_map_blocks map;
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800617 ssize_t ret = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800618
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700619 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
620 map.m_len = F2FS_BYTES_TO_BLK(iov_iter_count(from));
Chao Yuda859852016-01-26 15:42:58 +0800621 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800622
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800623 if (f2fs_encrypted_inode(inode))
624 return 0;
625
626 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800627 ret = f2fs_convert_inline_inode(inode);
628 if (ret)
629 return ret;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800630 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800631 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800632 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
633 ret = f2fs_convert_inline_inode(inode);
634 if (ret)
635 return ret;
636 }
637 if (!f2fs_has_inline_data(inode))
638 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800639 return ret;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800640}
641
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900642/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700643 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
644 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800645 * If original data blocks are allocated, then give them to blockdev.
646 * Otherwise,
647 * a. preallocate requested block addresses
648 * b. do not use extent cache for better performance
649 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900650 */
Chao Yud323d002015-10-27 09:53:45 +0800651int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800652 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900653{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700654 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900655 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800656 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900657 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
Chao Yu46008c62016-05-09 19:56:30 +0800658 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900659 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800660 unsigned int ofs_in_node, last_ofs_in_node;
661 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800662 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900663 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800664 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900665
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700666 map->m_len = 0;
667 map->m_flags = 0;
668
669 /* it only supports block size == page size */
670 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800671 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900672
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800673 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700674 map->m_pblk = ei.blk + pgofs - ei.fofs;
675 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
676 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900677 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800678 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900679
Chao Yu4fe71e82016-01-26 15:37:38 +0800680next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800681 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800682 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900683
684 /* When reading holes, we need its node page */
685 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900686 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900687 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800688 if (flag == F2FS_GET_BLOCK_BMAP)
689 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800690 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900691 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800692 if (map->m_next_pgofs)
693 *map->m_next_pgofs =
694 get_next_page_offset(&dn, pgofs);
695 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900696 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900697 }
Chao Yu973163f2015-09-18 16:51:51 +0800698
Chao Yu46008c62016-05-09 19:56:30 +0800699 prealloc = 0;
700 ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800701 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900702
Chao Yu4fe71e82016-01-26 15:37:38 +0800703next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800704 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800705
Fan Li7df3a432015-12-17 13:20:59 +0800706 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
707 if (create) {
708 if (unlikely(f2fs_cp_error(sbi))) {
709 err = -EIO;
710 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800711 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800712 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800713 if (blkaddr == NULL_ADDR) {
714 prealloc++;
715 last_ofs_in_node = dn.ofs_in_node;
716 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800717 } else {
718 err = __allocate_data_block(&dn);
Chao Yu46008c62016-05-09 19:56:30 +0800719 if (!err) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -0400720 set_inode_flag(F2FS_I(inode),
721 FI_APPEND_WRITE);
Chao Yu46008c62016-05-09 19:56:30 +0800722 allocated = true;
723 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800724 }
Fan Li7df3a432015-12-17 13:20:59 +0800725 if (err)
726 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800727 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800728 blkaddr = dn.data_blkaddr;
729 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800730 if (flag == F2FS_GET_BLOCK_BMAP) {
731 map->m_pblk = 0;
732 goto sync_out;
733 }
Chao Yuda859852016-01-26 15:42:58 +0800734 if (flag == F2FS_GET_BLOCK_FIEMAP &&
735 blkaddr == NULL_ADDR) {
736 if (map->m_next_pgofs)
737 *map->m_next_pgofs = pgofs + 1;
738 }
Fan Li7df3a432015-12-17 13:20:59 +0800739 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800740 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800741 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900742 }
743 }
Fan Li7df3a432015-12-17 13:20:59 +0800744
Chao Yu46008c62016-05-09 19:56:30 +0800745 if (flag == F2FS_GET_BLOCK_PRE_AIO)
746 goto skip;
747
Chao Yu4fe71e82016-01-26 15:37:38 +0800748 if (map->m_len == 0) {
749 /* preallocated unwritten block should be mapped for fiemap. */
750 if (blkaddr == NEW_ADDR)
751 map->m_flags |= F2FS_MAP_UNWRITTEN;
752 map->m_flags |= F2FS_MAP_MAPPED;
753
754 map->m_pblk = blkaddr;
755 map->m_len = 1;
756 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800757 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800758 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800759 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800760 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800761 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800762 } else {
763 goto sync_out;
764 }
765
Chao Yu46008c62016-05-09 19:56:30 +0800766skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800767 dn.ofs_in_node++;
768 pgofs++;
769
Chao Yu46008c62016-05-09 19:56:30 +0800770 /* preallocate blocks in batch for one dnode page */
771 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
772 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800773
Chao Yu46008c62016-05-09 19:56:30 +0800774 dn.ofs_in_node = ofs_in_node;
775 err = reserve_new_blocks(&dn, prealloc);
776 if (err)
777 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800778
Chao Yu46008c62016-05-09 19:56:30 +0800779 map->m_len += dn.ofs_in_node - ofs_in_node;
780 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
781 err = -ENOSPC;
782 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800783 }
Chao Yu46008c62016-05-09 19:56:30 +0800784 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800785 }
786
Chao Yu46008c62016-05-09 19:56:30 +0800787 if (pgofs >= end)
788 goto sync_out;
789 else if (dn.ofs_in_node < end_offset)
790 goto next_block;
791
792 if (allocated)
793 sync_inode_page(&dn);
794 f2fs_put_dnode(&dn);
795
796 if (create) {
797 f2fs_unlock_op(sbi);
798 f2fs_balance_fs(sbi, allocated);
799 }
800 allocated = false;
801 goto next_dnode;
802
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900803sync_out:
804 if (allocated)
805 sync_inode_page(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900806 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900807unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800808 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800809 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800810 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800811 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900812out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700813 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900814 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900815}
816
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700817static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800818 struct buffer_head *bh, int create, int flag,
819 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700820{
821 struct f2fs_map_blocks map;
822 int ret;
823
824 map.m_lblk = iblock;
825 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800826 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700827
Chao Yue2b4e2b2015-08-19 19:11:19 +0800828 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700829 if (!ret) {
830 map_bh(bh, inode->i_sb, map.m_pblk);
831 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
832 bh->b_size = map.m_len << inode->i_blkbits;
833 }
834 return ret;
835}
836
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900837static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800838 struct buffer_head *bh_result, int create, int flag,
839 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900840{
Chao Yuda859852016-01-26 15:42:58 +0800841 return __get_data_block(inode, iblock, bh_result, create,
842 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900843}
844
Chao Yue2b4e2b2015-08-19 19:11:19 +0800845static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900846 struct buffer_head *bh_result, int create)
847{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800848 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800849 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800850}
851
852static int get_data_block_bmap(struct inode *inode, sector_t iblock,
853 struct buffer_head *bh_result, int create)
854{
Yunlei He179448b2015-12-28 21:48:32 +0800855 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800856 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800857 return -EFBIG;
858
Chao Yue2b4e2b2015-08-19 19:11:19 +0800859 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800860 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900861}
862
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700863static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
864{
865 return (offset >> inode->i_blkbits);
866}
867
868static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
869{
870 return (blk << inode->i_blkbits);
871}
872
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900873int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
874 u64 start, u64 len)
875{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700876 struct buffer_head map_bh;
877 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800878 pgoff_t next_pgofs;
Fan Lide1475c2016-01-04 15:56:50 +0800879 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700880 u64 logical = 0, phys = 0, size = 0;
881 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700882 int ret = 0;
883
884 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
885 if (ret)
886 return ret;
887
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700888 if (f2fs_has_inline_data(inode)) {
889 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
890 if (ret != -EAGAIN)
891 return ret;
892 }
893
Al Viro59551022016-01-22 15:40:57 -0500894 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800895
896 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800897 if (start >= isize)
898 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700899
Fan Li9a950d52015-12-26 18:07:41 +0800900 if (start + len > isize)
901 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700902
903 if (logical_to_blk(inode, len) == 0)
904 len = blk_to_logical(inode, 1);
905
906 start_blk = logical_to_blk(inode, start);
907 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800908
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700909next:
910 memset(&map_bh, 0, sizeof(struct buffer_head));
911 map_bh.b_size = len;
912
Chao Yue2b4e2b2015-08-19 19:11:19 +0800913 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800914 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700915 if (ret)
916 goto out;
917
918 /* HOLE */
919 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800920 start_blk = next_pgofs;
Fan Li9a950d52015-12-26 18:07:41 +0800921 /* Go through holes util pass the EOF */
Chao Yuda859852016-01-26 15:42:58 +0800922 if (blk_to_logical(inode, start_blk) < isize)
Fan Li9a950d52015-12-26 18:07:41 +0800923 goto prep_next;
924 /* Found a hole beyond isize means no more extents.
925 * Note that the premise is that filesystems don't
926 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700927 */
Fan Li9a950d52015-12-26 18:07:41 +0800928 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700929 }
Fan Li9a950d52015-12-26 18:07:41 +0800930
Chao Yuda5af122016-01-08 20:19:27 +0800931 if (size) {
932 if (f2fs_encrypted_inode(inode))
933 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
934
Fan Li9a950d52015-12-26 18:07:41 +0800935 ret = fiemap_fill_next_extent(fieinfo, logical,
936 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800937 }
Fan Li9a950d52015-12-26 18:07:41 +0800938
939 if (start_blk > last_blk || ret)
940 goto out;
941
942 logical = blk_to_logical(inode, start_blk);
943 phys = blk_to_logical(inode, map_bh.b_blocknr);
944 size = map_bh.b_size;
945 flags = 0;
946 if (buffer_unwritten(&map_bh))
947 flags = FIEMAP_EXTENT_UNWRITTEN;
948
949 start_blk += logical_to_blk(inode, size);
950
951prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700952 cond_resched();
953 if (fatal_signal_pending(current))
954 ret = -EINTR;
955 else
956 goto next;
957out:
958 if (ret == 1)
959 ret = 0;
960
Al Viro59551022016-01-22 15:40:57 -0500961 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700962 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900963}
964
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700965/*
966 * This function was originally taken from fs/mpage.c, and customized for f2fs.
967 * Major change was from block_size == page_size in f2fs by default.
968 */
969static int f2fs_mpage_readpages(struct address_space *mapping,
970 struct list_head *pages, struct page *page,
971 unsigned nr_pages)
972{
973 struct bio *bio = NULL;
974 unsigned page_idx;
975 sector_t last_block_in_bio = 0;
976 struct inode *inode = mapping->host;
977 const unsigned blkbits = inode->i_blkbits;
978 const unsigned blocksize = 1 << blkbits;
979 sector_t block_in_file;
980 sector_t last_block;
981 sector_t last_block_in_file;
982 sector_t block_nr;
983 struct block_device *bdev = inode->i_sb->s_bdev;
984 struct f2fs_map_blocks map;
985
986 map.m_pblk = 0;
987 map.m_lblk = 0;
988 map.m_len = 0;
989 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +0800990 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700991
992 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
993
994 prefetchw(&page->flags);
995 if (pages) {
996 page = list_entry(pages->prev, struct page, lru);
997 list_del(&page->lru);
998 if (add_to_page_cache_lru(page, mapping,
999 page->index, GFP_KERNEL))
1000 goto next_page;
1001 }
1002
1003 block_in_file = (sector_t)page->index;
1004 last_block = block_in_file + nr_pages;
1005 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1006 blkbits;
1007 if (last_block > last_block_in_file)
1008 last_block = last_block_in_file;
1009
1010 /*
1011 * Map blocks using the previous result first.
1012 */
1013 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1014 block_in_file > map.m_lblk &&
1015 block_in_file < (map.m_lblk + map.m_len))
1016 goto got_it;
1017
1018 /*
1019 * Then do more f2fs_map_blocks() calls until we are
1020 * done with this page.
1021 */
1022 map.m_flags = 0;
1023
1024 if (block_in_file < last_block) {
1025 map.m_lblk = block_in_file;
1026 map.m_len = last_block - block_in_file;
1027
Chao Yu46c9e142015-09-18 16:54:16 +08001028 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001029 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001030 goto set_error_page;
1031 }
1032got_it:
1033 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1034 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1035 SetPageMappedToDisk(page);
1036
1037 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1038 SetPageUptodate(page);
1039 goto confused;
1040 }
1041 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001042 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001043 SetPageUptodate(page);
1044 unlock_page(page);
1045 goto next_page;
1046 }
1047
1048 /*
1049 * This page will go to BIO. Do we need to send this
1050 * BIO off first?
1051 */
1052 if (bio && (last_block_in_bio != block_nr - 1)) {
1053submit_and_realloc:
Jaegeuk Kimf5730182016-05-17 16:23:36 -07001054 __submit_bio(F2FS_I_SB(inode), READ, bio);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001055 bio = NULL;
1056 }
1057 if (bio == NULL) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001058 struct fscrypt_ctx *ctx = NULL;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001059
1060 if (f2fs_encrypted_inode(inode) &&
1061 S_ISREG(inode->i_mode)) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001062
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001063 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001064 if (IS_ERR(ctx))
1065 goto set_error_page;
1066
1067 /* wait the page to be moved by cleaning */
Chao Yu08b39fb2015-10-08 13:27:34 +08001068 f2fs_wait_on_encrypted_page_writeback(
1069 F2FS_I_SB(inode), block_nr);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001070 }
1071
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001072 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +02001073 min_t(int, nr_pages, BIO_MAX_PAGES));
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001074 if (!bio) {
1075 if (ctx)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001076 fscrypt_release_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001077 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001078 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001079 bio->bi_bdev = bdev;
1080 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +08001081 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001082 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001083 }
1084
1085 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1086 goto submit_and_realloc;
1087
1088 last_block_in_bio = block_nr;
1089 goto next_page;
1090set_error_page:
1091 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001092 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001093 unlock_page(page);
1094 goto next_page;
1095confused:
1096 if (bio) {
Jaegeuk Kimf5730182016-05-17 16:23:36 -07001097 __submit_bio(F2FS_I_SB(inode), READ, bio);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001098 bio = NULL;
1099 }
1100 unlock_page(page);
1101next_page:
1102 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001103 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001104 }
1105 BUG_ON(pages && !list_empty(pages));
1106 if (bio)
Jaegeuk Kimf5730182016-05-17 16:23:36 -07001107 __submit_bio(F2FS_I_SB(inode), READ, bio);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001108 return 0;
1109}
1110
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001111static int f2fs_read_data_page(struct file *file, struct page *page)
1112{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001113 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001114 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001115
Chao Yuc20e89c2014-05-06 16:53:08 +08001116 trace_f2fs_readpage(page, DATA);
1117
arter97e1c42042014-08-06 23:22:50 +09001118 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001119 if (f2fs_has_inline_data(inode))
1120 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001121 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001122 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001123 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001124}
1125
1126static int f2fs_read_data_pages(struct file *file,
1127 struct address_space *mapping,
1128 struct list_head *pages, unsigned nr_pages)
1129{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001130 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001131 struct page *page = list_entry(pages->prev, struct page, lru);
1132
1133 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001134
1135 /* If the file has inline data, skip readpages */
1136 if (f2fs_has_inline_data(inode))
1137 return 0;
1138
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001139 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001140}
1141
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001142int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001143{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001144 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001145 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001146 struct dnode_of_data dn;
1147 int err = 0;
1148
1149 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001150 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001151 if (err)
1152 return err;
1153
Chao Yu28bc1062016-02-06 14:40:34 +08001154 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001155
1156 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001157 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001158 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001159 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001160 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001161
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001162 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001163 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001164
1165 /* wait for GCed encrypted page writeback */
1166 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001167 fio->old_blkaddr);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001168retry_encrypt:
1169 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1170 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001171 if (IS_ERR(fio->encrypted_page)) {
1172 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001173 if (err == -ENOMEM) {
1174 /* flush pending ios and wait for a while */
1175 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1176 congestion_wait(BLK_RW_ASYNC, HZ/50);
1177 gfp_flags |= __GFP_NOFAIL;
1178 err = 0;
1179 goto retry_encrypt;
1180 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001181 goto out_writepage;
1182 }
1183 }
1184
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001185 set_page_writeback(page);
1186
1187 /*
1188 * If current allocation needs SSR,
1189 * it had better in-place writes for updated data.
1190 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001191 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001192 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001193 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001194 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001195 rewrite_data_page(fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001196 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001197 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001198 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001199 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001200 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001201 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001202 if (page->index == 0)
1203 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001204 }
1205out_writepage:
1206 f2fs_put_dnode(&dn);
1207 return err;
1208}
1209
1210static int f2fs_write_data_page(struct page *page,
1211 struct writeback_control *wbc)
1212{
1213 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001214 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001215 loff_t i_size = i_size_read(inode);
1216 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001217 >> PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001218 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001219 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001220 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001221 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001222 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001223 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001224 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001225 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001226 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001227 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001228
Chao Yuecda0de2014-05-06 16:48:26 +08001229 trace_f2fs_writepage(page, DATA);
1230
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001231 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001232 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001233
1234 /*
1235 * If the offset is out-of-range of file size,
1236 * this page does not have to be written to disk.
1237 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001238 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001239 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001240 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001241
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001242 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001243write:
Chao Yucaf00472015-01-28 17:48:42 +08001244 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001245 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001246 if (f2fs_is_drop_cache(inode))
1247 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001248 /* we should not write 0'th page having journal header */
1249 if (f2fs_is_volatile_file(inode) && (!page->index ||
1250 (!wbc->for_reclaim &&
1251 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001252 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001253
Jaegeuk Kim39936832012-11-22 16:21:29 +09001254 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001255 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001256 if (unlikely(f2fs_cp_error(sbi)))
1257 goto redirty_out;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001258 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001259 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001260 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001261
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001262 /* we should bypass data pages to proceed the kworkder jobs */
1263 if (unlikely(f2fs_cp_error(sbi))) {
1264 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001265 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001266 }
1267
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001268 if (!wbc->for_reclaim)
1269 need_balance_fs = true;
1270 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001271 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001272
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001273 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001274 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001275 if (f2fs_has_inline_data(inode))
1276 err = f2fs_write_inline_data(inode, page);
1277 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001278 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001279 f2fs_unlock_op(sbi);
1280done:
1281 if (err && err != -ENOENT)
1282 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001283
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001284 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001285out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001286 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001287 if (err)
1288 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001289
1290 if (wbc->for_reclaim) {
1291 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001292 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001293 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001294
1295 unlock_page(page);
1296 f2fs_balance_fs(sbi, need_balance_fs);
1297
1298 if (unlikely(f2fs_cp_error(sbi)))
1299 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1300
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001301 return 0;
1302
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001303redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001304 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001305 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001306}
1307
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001308static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1309 void *data)
1310{
1311 struct address_space *mapping = data;
1312 int ret = mapping->a_ops->writepage(page, wbc);
1313 mapping_set_error(mapping, ret);
1314 return ret;
1315}
1316
Chao Yu8f46dca2015-07-14 18:56:10 +08001317/*
1318 * This function was copied from write_cche_pages from mm/page-writeback.c.
1319 * The major change is making write step of cold data page separately from
1320 * warm/hot data page.
1321 */
1322static int f2fs_write_cache_pages(struct address_space *mapping,
1323 struct writeback_control *wbc, writepage_t writepage,
1324 void *data)
1325{
1326 int ret = 0;
1327 int done = 0;
1328 struct pagevec pvec;
1329 int nr_pages;
1330 pgoff_t uninitialized_var(writeback_index);
1331 pgoff_t index;
1332 pgoff_t end; /* Inclusive */
1333 pgoff_t done_index;
1334 int cycled;
1335 int range_whole = 0;
1336 int tag;
1337 int step = 0;
1338
1339 pagevec_init(&pvec, 0);
1340next:
1341 if (wbc->range_cyclic) {
1342 writeback_index = mapping->writeback_index; /* prev offset */
1343 index = writeback_index;
1344 if (index == 0)
1345 cycled = 1;
1346 else
1347 cycled = 0;
1348 end = -1;
1349 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001350 index = wbc->range_start >> PAGE_SHIFT;
1351 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001352 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1353 range_whole = 1;
1354 cycled = 1; /* ignore range_cyclic tests */
1355 }
1356 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1357 tag = PAGECACHE_TAG_TOWRITE;
1358 else
1359 tag = PAGECACHE_TAG_DIRTY;
1360retry:
1361 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1362 tag_pages_for_writeback(mapping, index, end);
1363 done_index = index;
1364 while (!done && (index <= end)) {
1365 int i;
1366
1367 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1368 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1369 if (nr_pages == 0)
1370 break;
1371
1372 for (i = 0; i < nr_pages; i++) {
1373 struct page *page = pvec.pages[i];
1374
1375 if (page->index > end) {
1376 done = 1;
1377 break;
1378 }
1379
1380 done_index = page->index;
1381
1382 lock_page(page);
1383
1384 if (unlikely(page->mapping != mapping)) {
1385continue_unlock:
1386 unlock_page(page);
1387 continue;
1388 }
1389
1390 if (!PageDirty(page)) {
1391 /* someone wrote it for us */
1392 goto continue_unlock;
1393 }
1394
Tiezhu Yang737f1892015-07-17 12:56:00 +08001395 if (step == is_cold_data(page))
Chao Yu8f46dca2015-07-14 18:56:10 +08001396 goto continue_unlock;
1397
1398 if (PageWriteback(page)) {
1399 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001400 f2fs_wait_on_page_writeback(page,
1401 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001402 else
1403 goto continue_unlock;
1404 }
1405
1406 BUG_ON(PageWriteback(page));
1407 if (!clear_page_dirty_for_io(page))
1408 goto continue_unlock;
1409
1410 ret = (*writepage)(page, wbc, data);
1411 if (unlikely(ret)) {
1412 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1413 unlock_page(page);
1414 ret = 0;
1415 } else {
1416 done_index = page->index + 1;
1417 done = 1;
1418 break;
1419 }
1420 }
1421
1422 if (--wbc->nr_to_write <= 0 &&
1423 wbc->sync_mode == WB_SYNC_NONE) {
1424 done = 1;
1425 break;
1426 }
1427 }
1428 pagevec_release(&pvec);
1429 cond_resched();
1430 }
1431
1432 if (step < 1) {
1433 step++;
1434 goto next;
1435 }
1436
1437 if (!cycled && !done) {
1438 cycled = 1;
1439 index = 0;
1440 end = writeback_index - 1;
1441 goto retry;
1442 }
1443 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1444 mapping->writeback_index = done_index;
1445
1446 return ret;
1447}
1448
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001449static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001450 struct writeback_control *wbc)
1451{
1452 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001453 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001454 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001455 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001456 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001457
P J Pcfb185a2013-04-03 11:38:00 +09001458 /* deal with chardevs and other special file */
1459 if (!mapping->a_ops->writepage)
1460 return 0;
1461
Chao Yu6a290542015-07-17 18:02:39 +08001462 /* skip writing if there is no dirty page in this inode */
1463 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1464 return 0;
1465
Jaegeuk Kima1257022015-10-08 10:40:07 -07001466 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1467 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1468 available_free_memory(sbi, DIRTY_DENTS))
1469 goto skip_write;
1470
Chao Yud323d002015-10-27 09:53:45 +08001471 /* skip writing during file defragment */
1472 if (is_inode_flag_set(F2FS_I(inode), FI_DO_DEFRAG))
1473 goto skip_write;
1474
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001475 /* during POR, we don't need to trigger writepage at all. */
1476 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1477 goto skip_write;
1478
Yunlei Hed31c7c32016-02-04 16:14:00 +08001479 trace_f2fs_writepages(mapping->host, wbc, DATA);
1480
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001481 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001482
Jaegeuk Kim25c13552016-01-20 23:46:05 +08001483 if (!S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_ALL) {
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001484 mutex_lock(&sbi->writepages);
1485 locked = true;
1486 }
Chao Yu8f46dca2015-07-14 18:56:10 +08001487 ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Chao Yu0c3a5792016-01-18 18:28:11 +08001488 f2fs_submit_merged_bio_cond(sbi, inode, NULL, 0, DATA, WRITE);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001489 if (locked)
1490 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001491
Chao Yuc227f912015-12-16 13:09:20 +08001492 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001493
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001494 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001495 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001496
1497skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001498 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001499 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001500 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001501}
1502
Chao Yu3aab8f82014-07-02 13:25:04 +08001503static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1504{
1505 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001506 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001507
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001508 if (to > i_size) {
1509 truncate_pagecache(inode, i_size);
1510 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001511 }
1512}
1513
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001514static int prepare_write_begin(struct f2fs_sb_info *sbi,
1515 struct page *page, loff_t pos, unsigned len,
1516 block_t *blk_addr, bool *node_changed)
1517{
1518 struct inode *inode = page->mapping->host;
1519 pgoff_t index = page->index;
1520 struct dnode_of_data dn;
1521 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001522 bool locked = false;
1523 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001524 int err = 0;
1525
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001526 /*
1527 * we already allocated all the blocks, so we don't need to get
1528 * the block addresses when there is no need to fill the page.
1529 */
1530 if (!f2fs_has_inline_data(inode) && !f2fs_encrypted_inode(inode) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001531 len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001532 return 0;
1533
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001534 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001535 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001536 f2fs_lock_op(sbi);
1537 locked = true;
1538 }
1539restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001540 /* check inline_data */
1541 ipage = get_node_page(sbi, inode->i_ino);
1542 if (IS_ERR(ipage)) {
1543 err = PTR_ERR(ipage);
1544 goto unlock_out;
1545 }
1546
1547 set_new_dnode(&dn, inode, ipage, ipage, 0);
1548
1549 if (f2fs_has_inline_data(inode)) {
1550 if (pos + len <= MAX_INLINE_DATA) {
1551 read_inline_data(page, ipage);
1552 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001553 if (inode->i_nlink)
1554 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001555 } else {
1556 err = f2fs_convert_inline_page(&dn, page);
1557 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001558 goto out;
1559 if (dn.data_blkaddr == NULL_ADDR)
1560 err = f2fs_get_block(&dn, index);
1561 }
1562 } else if (locked) {
1563 err = f2fs_get_block(&dn, index);
1564 } else {
1565 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1566 dn.data_blkaddr = ei.blk + index - ei.fofs;
1567 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001568 /* hole case */
1569 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001570 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001571 f2fs_put_dnode(&dn);
1572 f2fs_lock_op(sbi);
1573 locked = true;
1574 goto restart;
1575 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001576 }
1577 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001578
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001579 /* convert_inline_page can make node_changed */
1580 *blk_addr = dn.data_blkaddr;
1581 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001582out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001583 f2fs_put_dnode(&dn);
1584unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001585 if (locked)
1586 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001587 return err;
1588}
1589
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001590static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1591 loff_t pos, unsigned len, unsigned flags,
1592 struct page **pagep, void **fsdata)
1593{
1594 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001595 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001596 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001597 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001598 bool need_balance = false;
1599 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001600 int err = 0;
1601
Chao Yu62aed042014-05-06 16:46:04 +08001602 trace_f2fs_write_begin(inode, pos, len, flags);
1603
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001604 /*
1605 * We should check this at this moment to avoid deadlock on inode page
1606 * and #0 page. The locking rule for inline_data conversion should be:
1607 * lock_page(page #0) -> lock_page(inode_page)
1608 */
1609 if (index != 0) {
1610 err = f2fs_convert_inline_inode(inode);
1611 if (err)
1612 goto fail;
1613 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001614repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001615 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001616 if (!page) {
1617 err = -ENOMEM;
1618 goto fail;
1619 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001620
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001621 *pagep = page;
1622
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001623 err = prepare_write_begin(sbi, page, pos, len,
1624 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001625 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001626 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001627
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001628 if (need_balance && has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001629 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001630 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001631 lock_page(page);
1632 if (page->mapping != mapping) {
1633 /* The page got truncated from under us */
1634 f2fs_put_page(page, 1);
1635 goto repeat;
1636 }
1637 }
1638
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001639 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001640
Chao Yu08b39fb2015-10-08 13:27:34 +08001641 /* wait for GCed encrypted page writeback */
1642 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001643 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001644
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001645 if (len == PAGE_SIZE)
Chao Yu90d43882015-07-08 18:24:38 +08001646 goto out_update;
1647 if (PageUptodate(page))
1648 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001649
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001650 if ((pos & PAGE_MASK) >= i_size_read(inode)) {
1651 unsigned start = pos & (PAGE_SIZE - 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001652 unsigned end = start + len;
1653
1654 /* Reading beyond i_size is simple: memset to zero */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001655 zero_user_segments(page, 0, start, end, PAGE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001656 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001657 }
1658
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001659 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001660 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001661 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001662 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001663 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001664 .type = DATA,
1665 .rw = READ_SYNC,
Chao Yu7a9d7542016-02-22 18:36:38 +08001666 .old_blkaddr = blkaddr,
1667 .new_blkaddr = blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001668 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001669 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001670 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001671 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001672 if (err)
1673 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001674
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001675 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001676 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001677 err = -EIO;
1678 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001679 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001680 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001681 f2fs_put_page(page, 1);
1682 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001683 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001684
1685 /* avoid symlink page */
1686 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001687 err = fscrypt_decrypt_page(page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001688 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001689 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001690 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001691 }
Chao Yu90d43882015-07-08 18:24:38 +08001692out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001693 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001694out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001695 clear_cold_data(page);
1696 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001697
Chao Yu3aab8f82014-07-02 13:25:04 +08001698fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001699 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001700 f2fs_write_failed(mapping, pos + len);
1701 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001702}
1703
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001704static int f2fs_write_end(struct file *file,
1705 struct address_space *mapping,
1706 loff_t pos, unsigned len, unsigned copied,
1707 struct page *page, void *fsdata)
1708{
1709 struct inode *inode = page->mapping->host;
1710
Chao Yudfb2bf32014-05-06 16:47:23 +08001711 trace_f2fs_write_end(inode, pos, len, copied);
1712
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001713 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001714
1715 if (pos + copied > i_size_read(inode)) {
1716 i_size_write(inode, pos + copied);
1717 mark_inode_dirty(inode);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001718 }
1719
Chao Yu75c3c8b2013-11-16 14:15:59 +08001720 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001721 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001722 return copied;
1723}
1724
Omar Sandoval6f673762015-03-16 04:33:52 -07001725static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1726 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001727{
1728 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001729
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001730 if (offset & blocksize_mask)
1731 return -EINVAL;
1732
Al Viro5b46f252014-03-16 18:07:34 -04001733 if (iov_iter_alignment(iter) & blocksize_mask)
1734 return -EINVAL;
1735
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001736 return 0;
1737}
1738
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001739static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001740{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001741 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001742 struct inode *inode = mapping->host;
1743 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001744 loff_t offset = iocb->ki_pos;
Chao Yu3aab8f82014-07-02 13:25:04 +08001745 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001746
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001747 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001748 if (err)
1749 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001750
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001751 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1752 return 0;
1753
Omar Sandoval6f673762015-03-16 04:33:52 -07001754 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fad2014-07-31 21:11:22 +08001755
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001756 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001757 if (iov_iter_rw(iter) == WRITE) {
1758 if (err > 0)
1759 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
1760 else if (err < 0)
1761 f2fs_write_failed(mapping, offset + count);
1762 }
Chao Yu70407fad2014-07-31 21:11:22 +08001763
Omar Sandoval6f673762015-03-16 04:33:52 -07001764 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fad2014-07-31 21:11:22 +08001765
Chao Yu3aab8f82014-07-02 13:25:04 +08001766 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001767}
1768
Chao Yu487261f2015-02-05 17:44:29 +08001769void f2fs_invalidate_page(struct page *page, unsigned int offset,
1770 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001771{
1772 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001773 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001774
Chao Yu487261f2015-02-05 17:44:29 +08001775 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001776 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001777 return;
1778
Chao Yu487261f2015-02-05 17:44:29 +08001779 if (PageDirty(page)) {
1780 if (inode->i_ino == F2FS_META_INO(sbi))
1781 dec_page_count(sbi, F2FS_DIRTY_META);
1782 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1783 dec_page_count(sbi, F2FS_DIRTY_NODES);
1784 else
1785 inode_dec_dirty_pages(inode);
1786 }
Chao Yudecd36b2015-08-07 18:42:09 +08001787
1788 /* This is atomic written page, keep Private */
1789 if (IS_ATOMIC_WRITTEN_PAGE(page))
1790 return;
1791
Chao Yu23dc9742016-04-29 20:09:15 +08001792 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001793 ClearPagePrivate(page);
1794}
1795
Chao Yu487261f2015-02-05 17:44:29 +08001796int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001797{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001798 /* If this is dirty page, keep PagePrivate */
1799 if (PageDirty(page))
1800 return 0;
1801
Chao Yudecd36b2015-08-07 18:42:09 +08001802 /* This is atomic written page, keep Private */
1803 if (IS_ATOMIC_WRITTEN_PAGE(page))
1804 return 0;
1805
Chao Yu23dc9742016-04-29 20:09:15 +08001806 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001807 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001808 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001809}
1810
1811static int f2fs_set_data_page_dirty(struct page *page)
1812{
1813 struct address_space *mapping = page->mapping;
1814 struct inode *inode = mapping->host;
1815
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001816 trace_f2fs_set_page_dirty(page, DATA);
1817
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001818 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001819
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001820 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001821 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1822 register_inmem_page(inode, page);
1823 return 1;
1824 }
1825 /*
1826 * Previously, this page has been registered, we just
1827 * return here.
1828 */
1829 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001830 }
1831
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001832 if (!PageDirty(page)) {
1833 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001834 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001835 return 1;
1836 }
1837 return 0;
1838}
1839
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001840static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1841{
Chao Yu454ae7e2014-04-22 13:34:01 +08001842 struct inode *inode = mapping->host;
1843
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001844 if (f2fs_has_inline_data(inode))
1845 return 0;
1846
1847 /* make sure allocating whole blocks */
1848 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1849 filemap_write_and_wait(mapping);
1850
Chao Yue2b4e2b2015-08-19 19:11:19 +08001851 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001852}
1853
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001854const struct address_space_operations f2fs_dblock_aops = {
1855 .readpage = f2fs_read_data_page,
1856 .readpages = f2fs_read_data_pages,
1857 .writepage = f2fs_write_data_page,
1858 .writepages = f2fs_write_data_pages,
1859 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001860 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001861 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001862 .invalidatepage = f2fs_invalidate_page,
1863 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001864 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001865 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001866};