blob: 30dc448cae6073e3ffc0a42de3281ede98bba000 [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 Kim91942322016-05-20 10:13:22 -0700324 if (unlikely(is_inode_flag_set(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;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900346 return 0;
347}
348
Chao Yu46008c62016-05-09 19:56:30 +0800349/* Should keep dn->ofs_in_node unchanged */
350int reserve_new_block(struct dnode_of_data *dn)
351{
352 unsigned int ofs_in_node = dn->ofs_in_node;
353 int ret;
354
355 ret = reserve_new_blocks(dn, 1);
356 dn->ofs_in_node = ofs_in_node;
357 return ret;
358}
359
Huajun Lib6009652013-11-10 23:13:18 +0800360int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
361{
362 bool need_put = dn->inode_page ? false : true;
363 int err;
364
365 err = get_dnode_of_data(dn, index, ALLOC_NODE);
366 if (err)
367 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900368
Huajun Lib6009652013-11-10 23:13:18 +0800369 if (dn->data_blkaddr == NULL_ADDR)
370 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900371 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800372 f2fs_put_dnode(dn);
373 return err;
374}
375
Fan Li759af1c2015-08-05 15:52:16 +0800376int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900377{
Chao Yu028a41e2015-03-19 19:26:02 +0800378 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800379 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800380
Fan Li759af1c2015-08-05 15:52:16 +0800381 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
382 dn->data_blkaddr = ei.blk + index - ei.fofs;
383 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800384 }
385
Fan Li759af1c2015-08-05 15:52:16 +0800386 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900387}
388
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700389struct page *get_read_data_page(struct inode *inode, pgoff_t index,
390 int rw, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900391{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900392 struct address_space *mapping = inode->i_mapping;
393 struct dnode_of_data dn;
394 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800395 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900396 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800397 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700398 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800399 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700400 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700401 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800402 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900403
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700404 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
405 return read_mapping_page(mapping, index, NULL);
406
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700407 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900408 if (!page)
409 return ERR_PTR(-ENOMEM);
410
Chao Yucb3bc9e2015-02-05 18:03:40 +0800411 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
412 dn.data_blkaddr = ei.blk + index - ei.fofs;
413 goto got_it;
414 }
415
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900416 set_new_dnode(&dn, inode, NULL, NULL, 0);
417 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700418 if (err)
419 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900420 f2fs_put_dnode(&dn);
421
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900422 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700423 err = -ENOENT;
424 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900425 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800426got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700427 if (PageUptodate(page)) {
428 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900429 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700430 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900431
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900432 /*
433 * A new dentry page is allocated but not able to be written, since its
434 * new inode page couldn't be allocated due to -ENOSPC.
435 * In such the case, its blkaddr can be remained as NEW_ADDR.
436 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
437 */
438 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300439 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900440 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700441 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900442 return page;
443 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900444
Chao Yu7a9d7542016-02-22 18:36:38 +0800445 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700446 fio.page = page;
447 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900448 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700449 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700450 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700451
452put_err:
453 f2fs_put_page(page, 1);
454 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700455}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900456
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700457struct page *find_data_page(struct inode *inode, pgoff_t index)
458{
459 struct address_space *mapping = inode->i_mapping;
460 struct page *page;
461
462 page = find_get_page(mapping, index);
463 if (page && PageUptodate(page))
464 return page;
465 f2fs_put_page(page, 0);
466
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700467 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700468 if (IS_ERR(page))
469 return page;
470
471 if (PageUptodate(page))
472 return page;
473
474 wait_on_page_locked(page);
475 if (unlikely(!PageUptodate(page))) {
476 f2fs_put_page(page, 0);
477 return ERR_PTR(-EIO);
478 }
479 return page;
480}
481
482/*
483 * If it tries to access a hole, return an error.
484 * Because, the callers, functions in dir.c and GC, should be able to know
485 * whether this page exists or not.
486 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700487struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
488 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700489{
490 struct address_space *mapping = inode->i_mapping;
491 struct page *page;
492repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700493 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700494 if (IS_ERR(page))
495 return page;
496
497 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900498 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900499 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900500 f2fs_put_page(page, 1);
501 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900502 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900503 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900504 f2fs_put_page(page, 1);
505 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900506 }
507 return page;
508}
509
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900510/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900511 * Caller ensures that this data page is never allocated.
512 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900513 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800514 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
515 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800516 * Note that, ipage is set only by make_empty_dir, and if any error occur,
517 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900518 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900519struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900520 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900521{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900522 struct address_space *mapping = inode->i_mapping;
523 struct page *page;
524 struct dnode_of_data dn;
525 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800526
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700527 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800528 if (!page) {
529 /*
530 * before exiting, we should make sure ipage will be released
531 * if any error occur.
532 */
533 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700534 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800535 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900536
Jaegeuk Kima8865372013-12-27 17:04:17 +0900537 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800538 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700539 if (err) {
540 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900541 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900542 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700543 if (!ipage)
544 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900545
546 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700547 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900548
549 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300550 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900551 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900552 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700553 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900554
Jaegeuk Kim76121182016-01-01 22:03:47 -0800555 /* if ipage exists, blkaddr should be NEW_ADDR */
556 f2fs_bug_on(F2FS_I_SB(inode), ipage);
557 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700558 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800559 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900560 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700561got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800562 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700563 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700564 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900565 return page;
566}
567
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900568static int __allocate_data_block(struct dnode_of_data *dn)
569{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700570 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900571 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900572 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800573 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700574 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800575 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900576
Jaegeuk Kim91942322016-05-20 10:13:22 -0700577 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900578 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800579
580 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
581 if (dn->data_blkaddr == NEW_ADDR)
582 goto alloc;
583
Chao Yu46008c62016-05-09 19:56:30 +0800584 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900585 return -ENOSPC;
586
Chao Yudf6136e2015-03-23 10:33:37 +0800587alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900588 get_node_info(sbi, dn->nid, &ni);
589 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
590
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800591 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
592 seg = CURSEG_DIRECT_IO;
593
Chao Yudf6136e2015-03-23 10:33:37 +0800594 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
595 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800596 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900597
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700598 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800599 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700600 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300601 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700602 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300603 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900604 return 0;
605}
606
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800607ssize_t f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800608{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800609 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800610 struct f2fs_map_blocks map;
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800611 ssize_t ret = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800612
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700613 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
614 map.m_len = F2FS_BYTES_TO_BLK(iov_iter_count(from));
Chao Yuda859852016-01-26 15:42:58 +0800615 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800616
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800617 if (f2fs_encrypted_inode(inode))
618 return 0;
619
620 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800621 ret = f2fs_convert_inline_inode(inode);
622 if (ret)
623 return ret;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800624 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800625 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800626 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
627 ret = f2fs_convert_inline_inode(inode);
628 if (ret)
629 return ret;
630 }
631 if (!f2fs_has_inline_data(inode))
632 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800633 return ret;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800634}
635
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900636/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700637 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
638 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800639 * If original data blocks are allocated, then give them to blockdev.
640 * Otherwise,
641 * a. preallocate requested block addresses
642 * b. do not use extent cache for better performance
643 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900644 */
Chao Yud323d002015-10-27 09:53:45 +0800645int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800646 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900647{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700648 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900649 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800650 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900651 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
Chao Yu46008c62016-05-09 19:56:30 +0800652 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900653 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800654 unsigned int ofs_in_node, last_ofs_in_node;
655 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800656 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900657 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800658 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900659
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700660 map->m_len = 0;
661 map->m_flags = 0;
662
663 /* it only supports block size == page size */
664 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800665 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900666
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800667 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700668 map->m_pblk = ei.blk + pgofs - ei.fofs;
669 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
670 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900671 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800672 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900673
Chao Yu4fe71e82016-01-26 15:37:38 +0800674next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800675 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800676 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900677
678 /* When reading holes, we need its node page */
679 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900680 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900681 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800682 if (flag == F2FS_GET_BLOCK_BMAP)
683 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800684 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900685 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800686 if (map->m_next_pgofs)
687 *map->m_next_pgofs =
688 get_next_page_offset(&dn, pgofs);
689 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900690 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900691 }
Chao Yu973163f2015-09-18 16:51:51 +0800692
Chao Yu46008c62016-05-09 19:56:30 +0800693 prealloc = 0;
694 ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800695 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900696
Chao Yu4fe71e82016-01-26 15:37:38 +0800697next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800698 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800699
Fan Li7df3a432015-12-17 13:20:59 +0800700 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
701 if (create) {
702 if (unlikely(f2fs_cp_error(sbi))) {
703 err = -EIO;
704 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800705 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800706 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800707 if (blkaddr == NULL_ADDR) {
708 prealloc++;
709 last_ofs_in_node = dn.ofs_in_node;
710 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800711 } else {
712 err = __allocate_data_block(&dn);
Chao Yu46008c62016-05-09 19:56:30 +0800713 if (!err) {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700714 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yu46008c62016-05-09 19:56:30 +0800715 allocated = true;
716 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800717 }
Fan Li7df3a432015-12-17 13:20:59 +0800718 if (err)
719 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800720 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800721 blkaddr = dn.data_blkaddr;
722 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800723 if (flag == F2FS_GET_BLOCK_BMAP) {
724 map->m_pblk = 0;
725 goto sync_out;
726 }
Chao Yuda859852016-01-26 15:42:58 +0800727 if (flag == F2FS_GET_BLOCK_FIEMAP &&
728 blkaddr == NULL_ADDR) {
729 if (map->m_next_pgofs)
730 *map->m_next_pgofs = pgofs + 1;
731 }
Fan Li7df3a432015-12-17 13:20:59 +0800732 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800733 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800734 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900735 }
736 }
Fan Li7df3a432015-12-17 13:20:59 +0800737
Chao Yu46008c62016-05-09 19:56:30 +0800738 if (flag == F2FS_GET_BLOCK_PRE_AIO)
739 goto skip;
740
Chao Yu4fe71e82016-01-26 15:37:38 +0800741 if (map->m_len == 0) {
742 /* preallocated unwritten block should be mapped for fiemap. */
743 if (blkaddr == NEW_ADDR)
744 map->m_flags |= F2FS_MAP_UNWRITTEN;
745 map->m_flags |= F2FS_MAP_MAPPED;
746
747 map->m_pblk = blkaddr;
748 map->m_len = 1;
749 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800750 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800751 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800752 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800753 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800754 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800755 } else {
756 goto sync_out;
757 }
758
Chao Yu46008c62016-05-09 19:56:30 +0800759skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800760 dn.ofs_in_node++;
761 pgofs++;
762
Chao Yu46008c62016-05-09 19:56:30 +0800763 /* preallocate blocks in batch for one dnode page */
764 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
765 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800766
Chao Yu46008c62016-05-09 19:56:30 +0800767 dn.ofs_in_node = ofs_in_node;
768 err = reserve_new_blocks(&dn, prealloc);
769 if (err)
770 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800771
Chao Yu46008c62016-05-09 19:56:30 +0800772 map->m_len += dn.ofs_in_node - ofs_in_node;
773 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
774 err = -ENOSPC;
775 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800776 }
Chao Yu46008c62016-05-09 19:56:30 +0800777 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800778 }
779
Chao Yu46008c62016-05-09 19:56:30 +0800780 if (pgofs >= end)
781 goto sync_out;
782 else if (dn.ofs_in_node < end_offset)
783 goto next_block;
784
Chao Yu46008c62016-05-09 19:56:30 +0800785 f2fs_put_dnode(&dn);
786
787 if (create) {
788 f2fs_unlock_op(sbi);
789 f2fs_balance_fs(sbi, allocated);
790 }
791 allocated = false;
792 goto next_dnode;
793
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900794sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900795 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900796unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800797 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800798 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800799 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800800 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900801out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700802 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900803 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900804}
805
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700806static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800807 struct buffer_head *bh, int create, int flag,
808 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700809{
810 struct f2fs_map_blocks map;
811 int ret;
812
813 map.m_lblk = iblock;
814 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800815 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700816
Chao Yue2b4e2b2015-08-19 19:11:19 +0800817 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700818 if (!ret) {
819 map_bh(bh, inode->i_sb, map.m_pblk);
820 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
821 bh->b_size = map.m_len << inode->i_blkbits;
822 }
823 return ret;
824}
825
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900826static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800827 struct buffer_head *bh_result, int create, int flag,
828 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900829{
Chao Yuda859852016-01-26 15:42:58 +0800830 return __get_data_block(inode, iblock, bh_result, create,
831 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900832}
833
Chao Yue2b4e2b2015-08-19 19:11:19 +0800834static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900835 struct buffer_head *bh_result, int create)
836{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800837 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800838 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800839}
840
841static int get_data_block_bmap(struct inode *inode, sector_t iblock,
842 struct buffer_head *bh_result, int create)
843{
Yunlei He179448b2015-12-28 21:48:32 +0800844 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800845 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800846 return -EFBIG;
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_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900850}
851
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700852static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
853{
854 return (offset >> inode->i_blkbits);
855}
856
857static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
858{
859 return (blk << inode->i_blkbits);
860}
861
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900862int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
863 u64 start, u64 len)
864{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700865 struct buffer_head map_bh;
866 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800867 pgoff_t next_pgofs;
Fan Lide1475c2016-01-04 15:56:50 +0800868 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700869 u64 logical = 0, phys = 0, size = 0;
870 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700871 int ret = 0;
872
873 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
874 if (ret)
875 return ret;
876
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700877 if (f2fs_has_inline_data(inode)) {
878 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
879 if (ret != -EAGAIN)
880 return ret;
881 }
882
Al Viro59551022016-01-22 15:40:57 -0500883 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800884
885 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800886 if (start >= isize)
887 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700888
Fan Li9a950d52015-12-26 18:07:41 +0800889 if (start + len > isize)
890 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700891
892 if (logical_to_blk(inode, len) == 0)
893 len = blk_to_logical(inode, 1);
894
895 start_blk = logical_to_blk(inode, start);
896 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800897
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700898next:
899 memset(&map_bh, 0, sizeof(struct buffer_head));
900 map_bh.b_size = len;
901
Chao Yue2b4e2b2015-08-19 19:11:19 +0800902 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800903 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700904 if (ret)
905 goto out;
906
907 /* HOLE */
908 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800909 start_blk = next_pgofs;
Fan Li9a950d52015-12-26 18:07:41 +0800910 /* Go through holes util pass the EOF */
Chao Yuda859852016-01-26 15:42:58 +0800911 if (blk_to_logical(inode, start_blk) < isize)
Fan Li9a950d52015-12-26 18:07:41 +0800912 goto prep_next;
913 /* Found a hole beyond isize means no more extents.
914 * Note that the premise is that filesystems don't
915 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700916 */
Fan Li9a950d52015-12-26 18:07:41 +0800917 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700918 }
Fan Li9a950d52015-12-26 18:07:41 +0800919
Chao Yuda5af122016-01-08 20:19:27 +0800920 if (size) {
921 if (f2fs_encrypted_inode(inode))
922 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
923
Fan Li9a950d52015-12-26 18:07:41 +0800924 ret = fiemap_fill_next_extent(fieinfo, logical,
925 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800926 }
Fan Li9a950d52015-12-26 18:07:41 +0800927
928 if (start_blk > last_blk || ret)
929 goto out;
930
931 logical = blk_to_logical(inode, start_blk);
932 phys = blk_to_logical(inode, map_bh.b_blocknr);
933 size = map_bh.b_size;
934 flags = 0;
935 if (buffer_unwritten(&map_bh))
936 flags = FIEMAP_EXTENT_UNWRITTEN;
937
938 start_blk += logical_to_blk(inode, size);
939
940prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700941 cond_resched();
942 if (fatal_signal_pending(current))
943 ret = -EINTR;
944 else
945 goto next;
946out:
947 if (ret == 1)
948 ret = 0;
949
Al Viro59551022016-01-22 15:40:57 -0500950 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700951 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900952}
953
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700954/*
955 * This function was originally taken from fs/mpage.c, and customized for f2fs.
956 * Major change was from block_size == page_size in f2fs by default.
957 */
958static int f2fs_mpage_readpages(struct address_space *mapping,
959 struct list_head *pages, struct page *page,
960 unsigned nr_pages)
961{
962 struct bio *bio = NULL;
963 unsigned page_idx;
964 sector_t last_block_in_bio = 0;
965 struct inode *inode = mapping->host;
966 const unsigned blkbits = inode->i_blkbits;
967 const unsigned blocksize = 1 << blkbits;
968 sector_t block_in_file;
969 sector_t last_block;
970 sector_t last_block_in_file;
971 sector_t block_nr;
972 struct block_device *bdev = inode->i_sb->s_bdev;
973 struct f2fs_map_blocks map;
974
975 map.m_pblk = 0;
976 map.m_lblk = 0;
977 map.m_len = 0;
978 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +0800979 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700980
981 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
982
983 prefetchw(&page->flags);
984 if (pages) {
985 page = list_entry(pages->prev, struct page, lru);
986 list_del(&page->lru);
987 if (add_to_page_cache_lru(page, mapping,
988 page->index, GFP_KERNEL))
989 goto next_page;
990 }
991
992 block_in_file = (sector_t)page->index;
993 last_block = block_in_file + nr_pages;
994 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
995 blkbits;
996 if (last_block > last_block_in_file)
997 last_block = last_block_in_file;
998
999 /*
1000 * Map blocks using the previous result first.
1001 */
1002 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1003 block_in_file > map.m_lblk &&
1004 block_in_file < (map.m_lblk + map.m_len))
1005 goto got_it;
1006
1007 /*
1008 * Then do more f2fs_map_blocks() calls until we are
1009 * done with this page.
1010 */
1011 map.m_flags = 0;
1012
1013 if (block_in_file < last_block) {
1014 map.m_lblk = block_in_file;
1015 map.m_len = last_block - block_in_file;
1016
Chao Yu46c9e142015-09-18 16:54:16 +08001017 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001018 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001019 goto set_error_page;
1020 }
1021got_it:
1022 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1023 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1024 SetPageMappedToDisk(page);
1025
1026 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1027 SetPageUptodate(page);
1028 goto confused;
1029 }
1030 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001031 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001032 SetPageUptodate(page);
1033 unlock_page(page);
1034 goto next_page;
1035 }
1036
1037 /*
1038 * This page will go to BIO. Do we need to send this
1039 * BIO off first?
1040 */
1041 if (bio && (last_block_in_bio != block_nr - 1)) {
1042submit_and_realloc:
Jaegeuk Kimf5730182016-05-17 16:23:36 -07001043 __submit_bio(F2FS_I_SB(inode), READ, bio);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001044 bio = NULL;
1045 }
1046 if (bio == NULL) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001047 struct fscrypt_ctx *ctx = NULL;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001048
1049 if (f2fs_encrypted_inode(inode) &&
1050 S_ISREG(inode->i_mode)) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001051
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001052 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001053 if (IS_ERR(ctx))
1054 goto set_error_page;
1055
1056 /* wait the page to be moved by cleaning */
Chao Yu08b39fb2015-10-08 13:27:34 +08001057 f2fs_wait_on_encrypted_page_writeback(
1058 F2FS_I_SB(inode), block_nr);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001059 }
1060
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001061 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +02001062 min_t(int, nr_pages, BIO_MAX_PAGES));
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001063 if (!bio) {
1064 if (ctx)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001065 fscrypt_release_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001066 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001067 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001068 bio->bi_bdev = bdev;
1069 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +08001070 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001071 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001072 }
1073
1074 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1075 goto submit_and_realloc;
1076
1077 last_block_in_bio = block_nr;
1078 goto next_page;
1079set_error_page:
1080 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001081 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001082 unlock_page(page);
1083 goto next_page;
1084confused:
1085 if (bio) {
Jaegeuk Kimf5730182016-05-17 16:23:36 -07001086 __submit_bio(F2FS_I_SB(inode), READ, bio);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001087 bio = NULL;
1088 }
1089 unlock_page(page);
1090next_page:
1091 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001092 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001093 }
1094 BUG_ON(pages && !list_empty(pages));
1095 if (bio)
Jaegeuk Kimf5730182016-05-17 16:23:36 -07001096 __submit_bio(F2FS_I_SB(inode), READ, bio);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001097 return 0;
1098}
1099
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001100static int f2fs_read_data_page(struct file *file, struct page *page)
1101{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001102 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001103 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001104
Chao Yuc20e89c2014-05-06 16:53:08 +08001105 trace_f2fs_readpage(page, DATA);
1106
arter97e1c42042014-08-06 23:22:50 +09001107 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001108 if (f2fs_has_inline_data(inode))
1109 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001110 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001111 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001112 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001113}
1114
1115static int f2fs_read_data_pages(struct file *file,
1116 struct address_space *mapping,
1117 struct list_head *pages, unsigned nr_pages)
1118{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001119 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001120 struct page *page = list_entry(pages->prev, struct page, lru);
1121
1122 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001123
1124 /* If the file has inline data, skip readpages */
1125 if (f2fs_has_inline_data(inode))
1126 return 0;
1127
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001128 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001129}
1130
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001131int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001132{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001133 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001134 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001135 struct dnode_of_data dn;
1136 int err = 0;
1137
1138 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001139 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001140 if (err)
1141 return err;
1142
Chao Yu28bc1062016-02-06 14:40:34 +08001143 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001144
1145 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001146 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001147 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001148 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001149 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001150
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001151 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001152 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001153
1154 /* wait for GCed encrypted page writeback */
1155 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001156 fio->old_blkaddr);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001157retry_encrypt:
1158 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1159 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001160 if (IS_ERR(fio->encrypted_page)) {
1161 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001162 if (err == -ENOMEM) {
1163 /* flush pending ios and wait for a while */
1164 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1165 congestion_wait(BLK_RW_ASYNC, HZ/50);
1166 gfp_flags |= __GFP_NOFAIL;
1167 err = 0;
1168 goto retry_encrypt;
1169 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001170 goto out_writepage;
1171 }
1172 }
1173
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001174 set_page_writeback(page);
1175
1176 /*
1177 * If current allocation needs SSR,
1178 * it had better in-place writes for updated data.
1179 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001180 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001181 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001182 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001183 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001184 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001185 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001186 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001187 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001188 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001189 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001190 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001191 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001192 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001193 }
1194out_writepage:
1195 f2fs_put_dnode(&dn);
1196 return err;
1197}
1198
1199static int f2fs_write_data_page(struct page *page,
1200 struct writeback_control *wbc)
1201{
1202 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001203 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001204 loff_t i_size = i_size_read(inode);
1205 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001206 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001207 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001208 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001209 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001210 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001211 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001212 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001213 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001214 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001215 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001216 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001217 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001218
Chao Yuecda0de2014-05-06 16:48:26 +08001219 trace_f2fs_writepage(page, DATA);
1220
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001221 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001222 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001223
1224 /*
1225 * If the offset is out-of-range of file size,
1226 * this page does not have to be written to disk.
1227 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001228 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001229 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001230 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001231
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001232 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001233write:
Chao Yucaf00472015-01-28 17:48:42 +08001234 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001235 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001236 if (f2fs_is_drop_cache(inode))
1237 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001238 /* we should not write 0'th page having journal header */
1239 if (f2fs_is_volatile_file(inode) && (!page->index ||
1240 (!wbc->for_reclaim &&
1241 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001242 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001243
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001244 /* we should bypass data pages to proceed the kworkder jobs */
1245 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001246 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001247 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001248 }
1249
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001250 /* Dentry blocks are controlled by checkpoint */
1251 if (S_ISDIR(inode->i_mode)) {
1252 err = do_write_data_page(&fio);
1253 goto done;
1254 }
1255
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001256 if (!wbc->for_reclaim)
1257 need_balance_fs = true;
1258 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001259 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001260
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001261 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001262 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001263 if (f2fs_has_inline_data(inode))
1264 err = f2fs_write_inline_data(inode, page);
1265 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001266 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001267 if (F2FS_I(inode)->last_disk_size < psize)
1268 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001269 f2fs_unlock_op(sbi);
1270done:
1271 if (err && err != -ENOENT)
1272 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001273
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001274 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001275out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001276 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001277 if (err)
1278 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001279
1280 if (wbc->for_reclaim) {
1281 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001282 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001283 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001284
1285 unlock_page(page);
1286 f2fs_balance_fs(sbi, need_balance_fs);
1287
1288 if (unlikely(f2fs_cp_error(sbi)))
1289 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1290
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001291 return 0;
1292
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001293redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001294 redirty_page_for_writepage(wbc, page);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001295 unlock_page(page);
1296 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001297}
1298
Chao Yu8f46dca2015-07-14 18:56:10 +08001299/*
1300 * This function was copied from write_cche_pages from mm/page-writeback.c.
1301 * The major change is making write step of cold data page separately from
1302 * warm/hot data page.
1303 */
1304static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001305 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001306{
1307 int ret = 0;
1308 int done = 0;
1309 struct pagevec pvec;
1310 int nr_pages;
1311 pgoff_t uninitialized_var(writeback_index);
1312 pgoff_t index;
1313 pgoff_t end; /* Inclusive */
1314 pgoff_t done_index;
1315 int cycled;
1316 int range_whole = 0;
1317 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001318
1319 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001320
Chao Yu8f46dca2015-07-14 18:56:10 +08001321 if (wbc->range_cyclic) {
1322 writeback_index = mapping->writeback_index; /* prev offset */
1323 index = writeback_index;
1324 if (index == 0)
1325 cycled = 1;
1326 else
1327 cycled = 0;
1328 end = -1;
1329 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001330 index = wbc->range_start >> PAGE_SHIFT;
1331 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001332 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1333 range_whole = 1;
1334 cycled = 1; /* ignore range_cyclic tests */
1335 }
1336 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1337 tag = PAGECACHE_TAG_TOWRITE;
1338 else
1339 tag = PAGECACHE_TAG_DIRTY;
1340retry:
1341 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1342 tag_pages_for_writeback(mapping, index, end);
1343 done_index = index;
1344 while (!done && (index <= end)) {
1345 int i;
1346
1347 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1348 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1349 if (nr_pages == 0)
1350 break;
1351
1352 for (i = 0; i < nr_pages; i++) {
1353 struct page *page = pvec.pages[i];
1354
1355 if (page->index > end) {
1356 done = 1;
1357 break;
1358 }
1359
1360 done_index = page->index;
1361
1362 lock_page(page);
1363
1364 if (unlikely(page->mapping != mapping)) {
1365continue_unlock:
1366 unlock_page(page);
1367 continue;
1368 }
1369
1370 if (!PageDirty(page)) {
1371 /* someone wrote it for us */
1372 goto continue_unlock;
1373 }
1374
Chao Yu8f46dca2015-07-14 18:56:10 +08001375 if (PageWriteback(page)) {
1376 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001377 f2fs_wait_on_page_writeback(page,
1378 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001379 else
1380 goto continue_unlock;
1381 }
1382
1383 BUG_ON(PageWriteback(page));
1384 if (!clear_page_dirty_for_io(page))
1385 goto continue_unlock;
1386
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001387 ret = mapping->a_ops->writepage(page, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001388 if (unlikely(ret)) {
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001389 done_index = page->index + 1;
1390 done = 1;
1391 break;
Chao Yu8f46dca2015-07-14 18:56:10 +08001392 }
1393
1394 if (--wbc->nr_to_write <= 0 &&
1395 wbc->sync_mode == WB_SYNC_NONE) {
1396 done = 1;
1397 break;
1398 }
1399 }
1400 pagevec_release(&pvec);
1401 cond_resched();
1402 }
1403
Chao Yu8f46dca2015-07-14 18:56:10 +08001404 if (!cycled && !done) {
1405 cycled = 1;
1406 index = 0;
1407 end = writeback_index - 1;
1408 goto retry;
1409 }
1410 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1411 mapping->writeback_index = done_index;
1412
1413 return ret;
1414}
1415
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001416static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001417 struct writeback_control *wbc)
1418{
1419 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001420 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001421 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001422
P J Pcfb185a2013-04-03 11:38:00 +09001423 /* deal with chardevs and other special file */
1424 if (!mapping->a_ops->writepage)
1425 return 0;
1426
Chao Yu6a290542015-07-17 18:02:39 +08001427 /* skip writing if there is no dirty page in this inode */
1428 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1429 return 0;
1430
Jaegeuk Kima1257022015-10-08 10:40:07 -07001431 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1432 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1433 available_free_memory(sbi, DIRTY_DENTS))
1434 goto skip_write;
1435
Chao Yud323d002015-10-27 09:53:45 +08001436 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001437 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001438 goto skip_write;
1439
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001440 /* during POR, we don't need to trigger writepage at all. */
1441 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1442 goto skip_write;
1443
Yunlei Hed31c7c32016-02-04 16:14:00 +08001444 trace_f2fs_writepages(mapping->host, wbc, DATA);
1445
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001446 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001447 /*
1448 * if some pages were truncated, we cannot guarantee its mapping->host
1449 * to detect pending bios.
1450 */
1451 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001452
Chao Yuc227f912015-12-16 13:09:20 +08001453 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001454 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001455
1456skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001457 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001458 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001459 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001460}
1461
Chao Yu3aab8f82014-07-02 13:25:04 +08001462static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1463{
1464 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001465 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001466
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001467 if (to > i_size) {
1468 truncate_pagecache(inode, i_size);
1469 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001470 }
1471}
1472
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001473static int prepare_write_begin(struct f2fs_sb_info *sbi,
1474 struct page *page, loff_t pos, unsigned len,
1475 block_t *blk_addr, bool *node_changed)
1476{
1477 struct inode *inode = page->mapping->host;
1478 pgoff_t index = page->index;
1479 struct dnode_of_data dn;
1480 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001481 bool locked = false;
1482 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001483 int err = 0;
1484
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001485 /*
1486 * we already allocated all the blocks, so we don't need to get
1487 * the block addresses when there is no need to fill the page.
1488 */
1489 if (!f2fs_has_inline_data(inode) && !f2fs_encrypted_inode(inode) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001490 len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001491 return 0;
1492
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001493 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001494 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001495 f2fs_lock_op(sbi);
1496 locked = true;
1497 }
1498restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001499 /* check inline_data */
1500 ipage = get_node_page(sbi, inode->i_ino);
1501 if (IS_ERR(ipage)) {
1502 err = PTR_ERR(ipage);
1503 goto unlock_out;
1504 }
1505
1506 set_new_dnode(&dn, inode, ipage, ipage, 0);
1507
1508 if (f2fs_has_inline_data(inode)) {
1509 if (pos + len <= MAX_INLINE_DATA) {
1510 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001511 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001512 if (inode->i_nlink)
1513 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001514 } else {
1515 err = f2fs_convert_inline_page(&dn, page);
1516 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001517 goto out;
1518 if (dn.data_blkaddr == NULL_ADDR)
1519 err = f2fs_get_block(&dn, index);
1520 }
1521 } else if (locked) {
1522 err = f2fs_get_block(&dn, index);
1523 } else {
1524 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1525 dn.data_blkaddr = ei.blk + index - ei.fofs;
1526 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001527 /* hole case */
1528 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001529 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001530 f2fs_put_dnode(&dn);
1531 f2fs_lock_op(sbi);
1532 locked = true;
1533 goto restart;
1534 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001535 }
1536 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001537
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001538 /* convert_inline_page can make node_changed */
1539 *blk_addr = dn.data_blkaddr;
1540 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001541out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001542 f2fs_put_dnode(&dn);
1543unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001544 if (locked)
1545 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001546 return err;
1547}
1548
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001549static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1550 loff_t pos, unsigned len, unsigned flags,
1551 struct page **pagep, void **fsdata)
1552{
1553 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001554 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001555 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001556 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001557 bool need_balance = false;
1558 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001559 int err = 0;
1560
Chao Yu62aed042014-05-06 16:46:04 +08001561 trace_f2fs_write_begin(inode, pos, len, flags);
1562
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001563 /*
1564 * We should check this at this moment to avoid deadlock on inode page
1565 * and #0 page. The locking rule for inline_data conversion should be:
1566 * lock_page(page #0) -> lock_page(inode_page)
1567 */
1568 if (index != 0) {
1569 err = f2fs_convert_inline_inode(inode);
1570 if (err)
1571 goto fail;
1572 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001573repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001574 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001575 if (!page) {
1576 err = -ENOMEM;
1577 goto fail;
1578 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001579
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001580 *pagep = page;
1581
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001582 err = prepare_write_begin(sbi, page, pos, len,
1583 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001584 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001585 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001586
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001587 if (need_balance && has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001588 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001589 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001590 lock_page(page);
1591 if (page->mapping != mapping) {
1592 /* The page got truncated from under us */
1593 f2fs_put_page(page, 1);
1594 goto repeat;
1595 }
1596 }
1597
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001598 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001599
Chao Yu08b39fb2015-10-08 13:27:34 +08001600 /* wait for GCed encrypted page writeback */
1601 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001602 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001603
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001604 if (len == PAGE_SIZE)
Chao Yu90d43882015-07-08 18:24:38 +08001605 goto out_update;
1606 if (PageUptodate(page))
1607 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001608
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001609 if ((pos & PAGE_MASK) >= i_size_read(inode)) {
1610 unsigned start = pos & (PAGE_SIZE - 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001611 unsigned end = start + len;
1612
1613 /* Reading beyond i_size is simple: memset to zero */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001614 zero_user_segments(page, 0, start, end, PAGE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001615 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001616 }
1617
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001618 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001619 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001620 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001621 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001622 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001623 .type = DATA,
1624 .rw = READ_SYNC,
Chao Yu7a9d7542016-02-22 18:36:38 +08001625 .old_blkaddr = blkaddr,
1626 .new_blkaddr = blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001627 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001628 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001629 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001630 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001631 if (err)
1632 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001633
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001634 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001635 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001636 err = -EIO;
1637 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001638 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001639 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001640 f2fs_put_page(page, 1);
1641 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001642 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001643
1644 /* avoid symlink page */
1645 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001646 err = fscrypt_decrypt_page(page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001647 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001648 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001649 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001650 }
Chao Yu90d43882015-07-08 18:24:38 +08001651out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001652 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001653out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001654 clear_cold_data(page);
1655 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001656
Chao Yu3aab8f82014-07-02 13:25:04 +08001657fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001658 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001659 f2fs_write_failed(mapping, pos + len);
1660 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001661}
1662
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001663static int f2fs_write_end(struct file *file,
1664 struct address_space *mapping,
1665 loff_t pos, unsigned len, unsigned copied,
1666 struct page *page, void *fsdata)
1667{
1668 struct inode *inode = page->mapping->host;
1669
Chao Yudfb2bf32014-05-06 16:47:23 +08001670 trace_f2fs_write_end(inode, pos, len, copied);
1671
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001672 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001673
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001674 if (pos + copied > i_size_read(inode))
1675 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001676
Chao Yu75c3c8b2013-11-16 14:15:59 +08001677 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001678 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001679 return copied;
1680}
1681
Omar Sandoval6f673762015-03-16 04:33:52 -07001682static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1683 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001684{
1685 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001686
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001687 if (offset & blocksize_mask)
1688 return -EINVAL;
1689
Al Viro5b46f252014-03-16 18:07:34 -04001690 if (iov_iter_alignment(iter) & blocksize_mask)
1691 return -EINVAL;
1692
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001693 return 0;
1694}
1695
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001696static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001697{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001698 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001699 struct inode *inode = mapping->host;
1700 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001701 loff_t offset = iocb->ki_pos;
Chao Yu3aab8f82014-07-02 13:25:04 +08001702 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001703
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001704 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001705 if (err)
1706 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001707
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001708 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1709 return 0;
1710
Omar Sandoval6f673762015-03-16 04:33:52 -07001711 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fad2014-07-31 21:11:22 +08001712
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001713 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001714 if (iov_iter_rw(iter) == WRITE) {
1715 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001716 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001717 else if (err < 0)
1718 f2fs_write_failed(mapping, offset + count);
1719 }
Chao Yu70407fad2014-07-31 21:11:22 +08001720
Omar Sandoval6f673762015-03-16 04:33:52 -07001721 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fad2014-07-31 21:11:22 +08001722
Chao Yu3aab8f82014-07-02 13:25:04 +08001723 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001724}
1725
Chao Yu487261f2015-02-05 17:44:29 +08001726void f2fs_invalidate_page(struct page *page, unsigned int offset,
1727 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001728{
1729 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001730 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001731
Chao Yu487261f2015-02-05 17:44:29 +08001732 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001733 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001734 return;
1735
Chao Yu487261f2015-02-05 17:44:29 +08001736 if (PageDirty(page)) {
1737 if (inode->i_ino == F2FS_META_INO(sbi))
1738 dec_page_count(sbi, F2FS_DIRTY_META);
1739 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1740 dec_page_count(sbi, F2FS_DIRTY_NODES);
1741 else
1742 inode_dec_dirty_pages(inode);
1743 }
Chao Yudecd36b2015-08-07 18:42:09 +08001744
1745 /* This is atomic written page, keep Private */
1746 if (IS_ATOMIC_WRITTEN_PAGE(page))
1747 return;
1748
Chao Yu23dc9742016-04-29 20:09:15 +08001749 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001750 ClearPagePrivate(page);
1751}
1752
Chao Yu487261f2015-02-05 17:44:29 +08001753int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001754{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001755 /* If this is dirty page, keep PagePrivate */
1756 if (PageDirty(page))
1757 return 0;
1758
Chao Yudecd36b2015-08-07 18:42:09 +08001759 /* This is atomic written page, keep Private */
1760 if (IS_ATOMIC_WRITTEN_PAGE(page))
1761 return 0;
1762
Chao Yu23dc9742016-04-29 20:09:15 +08001763 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001764 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001765 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001766}
1767
1768static int f2fs_set_data_page_dirty(struct page *page)
1769{
1770 struct address_space *mapping = page->mapping;
1771 struct inode *inode = mapping->host;
1772
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001773 trace_f2fs_set_page_dirty(page, DATA);
1774
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001775 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001776
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001777 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001778 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1779 register_inmem_page(inode, page);
1780 return 1;
1781 }
1782 /*
1783 * Previously, this page has been registered, we just
1784 * return here.
1785 */
1786 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001787 }
1788
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001789 if (!PageDirty(page)) {
1790 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001791 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001792 return 1;
1793 }
1794 return 0;
1795}
1796
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001797static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1798{
Chao Yu454ae7e2014-04-22 13:34:01 +08001799 struct inode *inode = mapping->host;
1800
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001801 if (f2fs_has_inline_data(inode))
1802 return 0;
1803
1804 /* make sure allocating whole blocks */
1805 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1806 filemap_write_and_wait(mapping);
1807
Chao Yue2b4e2b2015-08-19 19:11:19 +08001808 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001809}
1810
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001811const struct address_space_operations f2fs_dblock_aops = {
1812 .readpage = f2fs_read_data_page,
1813 .readpages = f2fs_read_data_pages,
1814 .writepage = f2fs_write_data_page,
1815 .writepages = f2fs_write_data_pages,
1816 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001817 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001818 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001819 .invalidatepage = f2fs_invalidate_page,
1820 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001821 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001822 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001823};