blob: 5dafb9cef12e7116c545a07cbac5abc16cb76715 [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 Kim744602c2014-01-24 09:42:16 +090071 f2fs_stop_checkpoint(sbi);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090072 }
73 end_page_writeback(page);
74 dec_page_count(sbi, F2FS_WRITEBACK);
Linus Torvaldsf5688492014-01-30 11:19:05 -080075 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090076
Jaegeuk Kim7c506892016-01-26 11:55:35 -080077 if (!get_pages(sbi, F2FS_WRITEBACK) && wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090078 wake_up(&sbi->cp_wait);
79
80 bio_put(bio);
81}
82
Gu Zheng940a6d32013-12-20 17:39:59 +080083/*
84 * Low-level block read/write IO operations.
85 */
86static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
87 int npages, bool is_read)
88{
89 struct bio *bio;
90
Jaegeuk Kim740432f2015-08-14 11:43:56 -070091 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +080092
93 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +080094 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +080095 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +080096 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +080097
98 return bio;
99}
100
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900101static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900102{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900103 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900104
105 if (!io->bio)
106 return;
107
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700108 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800109 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700110 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800111 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900112
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700113 submit_bio(fio->rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900114 io->bio = NULL;
115}
116
Chao Yu0c3a5792016-01-18 18:28:11 +0800117static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
118 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800119{
Chao Yu0fd785e2016-01-18 18:24:59 +0800120 struct bio_vec *bvec;
121 struct page *target;
122 int i;
123
Chao Yu0c3a5792016-01-18 18:28:11 +0800124 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800125 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800126
127 if (!inode && !page && !ino)
128 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800129
130 bio_for_each_segment_all(bvec, io->bio, i) {
131
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700132 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800133 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700134 else
135 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800136
Chao Yu0c3a5792016-01-18 18:28:11 +0800137 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800138 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800139 if (page && page == target)
140 return true;
141 if (ino && ino == ino_of_node(target))
142 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800143 }
144
Chao Yu0fd785e2016-01-18 18:24:59 +0800145 return false;
146}
147
Chao Yu0c3a5792016-01-18 18:28:11 +0800148static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
149 struct page *page, nid_t ino,
150 enum page_type type)
151{
152 enum page_type btype = PAGE_TYPE_OF_BIO(type);
153 struct f2fs_bio_info *io = &sbi->write_io[btype];
154 bool ret;
155
156 down_read(&io->io_rwsem);
157 ret = __has_merged_page(io, inode, page, ino);
158 up_read(&io->io_rwsem);
159 return ret;
160}
161
162static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
163 struct inode *inode, struct page *page,
164 nid_t ino, enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900165{
166 enum page_type btype = PAGE_TYPE_OF_BIO(type);
167 struct f2fs_bio_info *io;
168
169 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
170
Chao Yudf0f8dc2014-03-22 14:57:23 +0800171 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900172
Chao Yu0c3a5792016-01-18 18:28:11 +0800173 if (!__has_merged_page(io, inode, page, ino))
174 goto out;
175
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900176 /* change META to META_FLUSH in the checkpoint procedure */
177 if (type >= META_FLUSH) {
178 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700179 if (test_opt(sbi, NOBARRIER))
180 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
181 else
182 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900183 }
184 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800185out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800186 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900187}
188
Chao Yu0c3a5792016-01-18 18:28:11 +0800189void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
190 int rw)
191{
192 __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw);
193}
194
195void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
196 struct inode *inode, struct page *page,
197 nid_t ino, enum page_type type, int rw)
198{
199 if (has_merged_page(sbi, inode, page, ino, type))
200 __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw);
201}
202
Chao Yu406657d2016-02-24 17:17:55 +0800203void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
204{
205 f2fs_submit_merged_bio(sbi, DATA, WRITE);
206 f2fs_submit_merged_bio(sbi, NODE, WRITE);
207 f2fs_submit_merged_bio(sbi, META, WRITE);
208}
209
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900210/*
211 * Fill the locked page with data located in the block address.
212 * Return unlocked page.
213 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700214int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900215{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900216 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700217 struct page *page = fio->encrypted_page ?
218 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900219
Chao Yu2ace38e2014-12-24 16:08:14 +0800220 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700221 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900222
223 /* Allocate a new bio */
Chao Yu7a9d7542016-02-22 18:36:38 +0800224 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900225
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300226 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900227 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900228 return -EFAULT;
229 }
230
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800231 submit_bio(fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900232 return 0;
233}
234
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700235void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900236{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700237 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900238 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900239 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800240 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700241 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900242
Gu Zheng940a6d32013-12-20 17:39:59 +0800243 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900244
Chao Yu7a9d7542016-02-22 18:36:38 +0800245 if (fio->old_blkaddr != NEW_ADDR)
246 verify_block_addr(sbi, fio->old_blkaddr);
247 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900248
Chao Yudf0f8dc2014-03-22 14:57:23 +0800249 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900250
Gu Zheng940a6d32013-12-20 17:39:59 +0800251 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900252 inc_page_count(sbi, F2FS_WRITEBACK);
253
Chao Yu7a9d7542016-02-22 18:36:38 +0800254 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900255 io->fio.rw != fio->rw))
256 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900257alloc_new:
258 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700259 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800260
Chao Yu7a9d7542016-02-22 18:36:38 +0800261 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
262 bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900263 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900264 }
265
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700266 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
267
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300268 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
269 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900270 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900271 goto alloc_new;
272 }
273
Chao Yu7a9d7542016-02-22 18:36:38 +0800274 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700275 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900276
Chao Yudf0f8dc2014-03-22 14:57:23 +0800277 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700278 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900279}
280
281/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900282 * Lock ordering for the change of data block address:
283 * ->data_page
284 * ->node_page
285 * update block addresses in the node page
286 */
Chao Yu216a6202015-03-19 19:23:32 +0800287void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900288{
289 struct f2fs_node *rn;
290 __le32 *addr_array;
291 struct page *node_page = dn->node_page;
292 unsigned int ofs_in_node = dn->ofs_in_node;
293
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800294 f2fs_wait_on_page_writeback(node_page, NODE, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900295
Gu Zheng45590712013-07-15 17:57:38 +0800296 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900297
298 /* Get physical address of data block */
299 addr_array = blkaddr_in_node(rn);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800300 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800301 if (set_page_dirty(node_page))
302 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900303}
304
Chao Yuf28b3432016-02-24 17:16:47 +0800305void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
306{
307 dn->data_blkaddr = blkaddr;
308 set_data_blkaddr(dn);
309 f2fs_update_extent_cache(dn);
310}
311
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900312int reserve_new_block(struct dnode_of_data *dn)
313{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700314 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900315
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900316 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900317 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800318 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900319 return -ENOSPC;
320
Namjae Jeonc01e2852013-04-23 17:00:52 +0900321 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
322
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900323 dn->data_blkaddr = NEW_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800324 set_data_blkaddr(dn);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900325 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900326 sync_inode_page(dn);
327 return 0;
328}
329
Huajun Lib6009652013-11-10 23:13:18 +0800330int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
331{
332 bool need_put = dn->inode_page ? false : true;
333 int err;
334
335 err = get_dnode_of_data(dn, index, ALLOC_NODE);
336 if (err)
337 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900338
Huajun Lib6009652013-11-10 23:13:18 +0800339 if (dn->data_blkaddr == NULL_ADDR)
340 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900341 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800342 f2fs_put_dnode(dn);
343 return err;
344}
345
Fan Li759af1c2015-08-05 15:52:16 +0800346int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900347{
Chao Yu028a41e2015-03-19 19:26:02 +0800348 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800349 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800350
Fan Li759af1c2015-08-05 15:52:16 +0800351 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
352 dn->data_blkaddr = ei.blk + index - ei.fofs;
353 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800354 }
355
Fan Li759af1c2015-08-05 15:52:16 +0800356 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900357}
358
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700359struct page *get_read_data_page(struct inode *inode, pgoff_t index,
360 int rw, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900361{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900362 struct address_space *mapping = inode->i_mapping;
363 struct dnode_of_data dn;
364 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800365 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900366 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800367 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700368 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800369 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700370 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700371 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800372 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900373
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700374 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
375 return read_mapping_page(mapping, index, NULL);
376
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700377 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900378 if (!page)
379 return ERR_PTR(-ENOMEM);
380
Chao Yucb3bc9e2015-02-05 18:03:40 +0800381 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
382 dn.data_blkaddr = ei.blk + index - ei.fofs;
383 goto got_it;
384 }
385
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900386 set_new_dnode(&dn, inode, NULL, NULL, 0);
387 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700388 if (err)
389 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900390 f2fs_put_dnode(&dn);
391
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900392 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700393 err = -ENOENT;
394 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900395 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800396got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700397 if (PageUptodate(page)) {
398 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900399 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700400 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900401
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900402 /*
403 * A new dentry page is allocated but not able to be written, since its
404 * new inode page couldn't be allocated due to -ENOSPC.
405 * In such the case, its blkaddr can be remained as NEW_ADDR.
406 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
407 */
408 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300409 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900410 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700411 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900412 return page;
413 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900414
Chao Yu7a9d7542016-02-22 18:36:38 +0800415 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700416 fio.page = page;
417 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900418 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700419 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700420 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700421
422put_err:
423 f2fs_put_page(page, 1);
424 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700425}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900426
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700427struct page *find_data_page(struct inode *inode, pgoff_t index)
428{
429 struct address_space *mapping = inode->i_mapping;
430 struct page *page;
431
432 page = find_get_page(mapping, index);
433 if (page && PageUptodate(page))
434 return page;
435 f2fs_put_page(page, 0);
436
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700437 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700438 if (IS_ERR(page))
439 return page;
440
441 if (PageUptodate(page))
442 return page;
443
444 wait_on_page_locked(page);
445 if (unlikely(!PageUptodate(page))) {
446 f2fs_put_page(page, 0);
447 return ERR_PTR(-EIO);
448 }
449 return page;
450}
451
452/*
453 * If it tries to access a hole, return an error.
454 * Because, the callers, functions in dir.c and GC, should be able to know
455 * whether this page exists or not.
456 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700457struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
458 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700459{
460 struct address_space *mapping = inode->i_mapping;
461 struct page *page;
462repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700463 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700464 if (IS_ERR(page))
465 return page;
466
467 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900468 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900469 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900470 f2fs_put_page(page, 1);
471 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900472 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900473 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900474 f2fs_put_page(page, 1);
475 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900476 }
477 return page;
478}
479
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900480/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900481 * Caller ensures that this data page is never allocated.
482 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900483 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800484 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
485 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800486 * Note that, ipage is set only by make_empty_dir, and if any error occur,
487 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900488 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900489struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900490 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900491{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900492 struct address_space *mapping = inode->i_mapping;
493 struct page *page;
494 struct dnode_of_data dn;
495 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800496
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700497 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800498 if (!page) {
499 /*
500 * before exiting, we should make sure ipage will be released
501 * if any error occur.
502 */
503 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700504 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800505 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900506
Jaegeuk Kima8865372013-12-27 17:04:17 +0900507 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800508 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700509 if (err) {
510 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900511 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900512 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700513 if (!ipage)
514 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900515
516 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700517 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900518
519 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300520 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900521 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900522 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700523 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900524
Jaegeuk Kim76121182016-01-01 22:03:47 -0800525 /* if ipage exists, blkaddr should be NEW_ADDR */
526 f2fs_bug_on(F2FS_I_SB(inode), ipage);
527 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700528 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800529 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900530 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700531got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800532 if (new_i_size && i_size_read(inode) <
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300533 ((loff_t)(index + 1) << PAGE_SHIFT)) {
534 i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900535 /* Only the directory inode sets new_i_size */
536 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900537 }
538 return page;
539}
540
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900541static int __allocate_data_block(struct dnode_of_data *dn)
542{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700543 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900544 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900545 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800546 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700547 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900548
549 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
550 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800551
552 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
553 if (dn->data_blkaddr == NEW_ADDR)
554 goto alloc;
555
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900556 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
557 return -ENOSPC;
558
Chao Yudf6136e2015-03-23 10:33:37 +0800559alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900560 get_node_info(sbi, dn->nid, &ni);
561 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
562
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800563 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
564 seg = CURSEG_DIRECT_IO;
565
Chao Yudf6136e2015-03-23 10:33:37 +0800566 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
567 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800568 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900569
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700570 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800571 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700572 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300573 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Chao Yu9edcdab2015-09-11 14:43:52 +0800574 i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300575 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900576 return 0;
577}
578
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800579ssize_t f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800580{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800581 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800582 struct f2fs_map_blocks map;
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800583 ssize_t ret = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800584
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800585 map.m_lblk = F2FS_BYTES_TO_BLK(iocb->ki_pos);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800586 map.m_len = F2FS_BLK_ALIGN(iov_iter_count(from));
Chao Yuda859852016-01-26 15:42:58 +0800587 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800588
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800589 if (f2fs_encrypted_inode(inode))
590 return 0;
591
592 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800593 ret = f2fs_convert_inline_inode(inode);
594 if (ret)
595 return ret;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800596 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800597 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800598 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
599 ret = f2fs_convert_inline_inode(inode);
600 if (ret)
601 return ret;
602 }
603 if (!f2fs_has_inline_data(inode))
604 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800605 return ret;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800606}
607
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900608/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700609 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
610 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800611 * If original data blocks are allocated, then give them to blockdev.
612 * Otherwise,
613 * a. preallocate requested block addresses
614 * b. do not use extent cache for better performance
615 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900616 */
Chao Yud323d002015-10-27 09:53:45 +0800617int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800618 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900619{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700620 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900621 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800622 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900623 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
624 pgoff_t pgofs, end_offset;
625 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800626 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900627 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800628 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900629
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700630 map->m_len = 0;
631 map->m_flags = 0;
632
633 /* it only supports block size == page size */
634 pgofs = (pgoff_t)map->m_lblk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900635
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800636 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700637 map->m_pblk = ei.blk + pgofs - ei.fofs;
638 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
639 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900640 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800641 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900642
Chao Yu4fe71e82016-01-26 15:37:38 +0800643next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800644 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800645 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900646
647 /* When reading holes, we need its node page */
648 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900649 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900650 if (err) {
Chao Yuda859852016-01-26 15:42:58 +0800651 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900652 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800653 if (map->m_next_pgofs)
654 *map->m_next_pgofs =
655 get_next_page_offset(&dn, pgofs);
656 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900657 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900658 }
Chao Yu973163f2015-09-18 16:51:51 +0800659
Chao Yu81ca7352016-01-26 15:39:35 +0800660 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900661
Chao Yu4fe71e82016-01-26 15:37:38 +0800662next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800663 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800664
Fan Li7df3a432015-12-17 13:20:59 +0800665 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
666 if (create) {
667 if (unlikely(f2fs_cp_error(sbi))) {
668 err = -EIO;
669 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800670 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800671 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
672 if (blkaddr == NULL_ADDR)
673 err = reserve_new_block(&dn);
674 } else {
675 err = __allocate_data_block(&dn);
676 }
Fan Li7df3a432015-12-17 13:20:59 +0800677 if (err)
678 goto sync_out;
679 allocated = true;
Chao Yu4fe71e82016-01-26 15:37:38 +0800680 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800681 blkaddr = dn.data_blkaddr;
682 } else {
Chao Yuda859852016-01-26 15:42:58 +0800683 if (flag == F2FS_GET_BLOCK_FIEMAP &&
684 blkaddr == NULL_ADDR) {
685 if (map->m_next_pgofs)
686 *map->m_next_pgofs = pgofs + 1;
687 }
Fan Li7df3a432015-12-17 13:20:59 +0800688 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu4fe71e82016-01-26 15:37:38 +0800689 blkaddr != NEW_ADDR) {
690 if (flag == F2FS_GET_BLOCK_BMAP)
691 err = -ENOENT;
Fan Li7df3a432015-12-17 13:20:59 +0800692 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800693 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900694 }
695 }
Fan Li7df3a432015-12-17 13:20:59 +0800696
Chao Yu4fe71e82016-01-26 15:37:38 +0800697 if (map->m_len == 0) {
698 /* preallocated unwritten block should be mapped for fiemap. */
699 if (blkaddr == NEW_ADDR)
700 map->m_flags |= F2FS_MAP_UNWRITTEN;
701 map->m_flags |= F2FS_MAP_MAPPED;
702
703 map->m_pblk = blkaddr;
704 map->m_len = 1;
705 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800706 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800707 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800708 flag == F2FS_GET_BLOCK_PRE_DIO ||
709 flag == F2FS_GET_BLOCK_PRE_AIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800710 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800711 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800712 } else {
713 goto sync_out;
714 }
715
716 dn.ofs_in_node++;
717 pgofs++;
718
719 if (map->m_len < maxblocks) {
720 if (dn.ofs_in_node < end_offset)
721 goto next_block;
722
723 if (allocated)
724 sync_inode_page(&dn);
725 f2fs_put_dnode(&dn);
726
727 if (create) {
728 f2fs_unlock_op(sbi);
729 f2fs_balance_fs(sbi, allocated);
730 }
731 allocated = false;
732 goto next_dnode;
Fan Li7df3a432015-12-17 13:20:59 +0800733 }
734
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900735sync_out:
736 if (allocated)
737 sync_inode_page(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900738 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900739unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800740 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800741 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800742 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800743 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900744out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700745 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900746 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900747}
748
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700749static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800750 struct buffer_head *bh, int create, int flag,
751 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700752{
753 struct f2fs_map_blocks map;
754 int ret;
755
756 map.m_lblk = iblock;
757 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800758 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700759
Chao Yue2b4e2b2015-08-19 19:11:19 +0800760 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700761 if (!ret) {
762 map_bh(bh, inode->i_sb, map.m_pblk);
763 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
764 bh->b_size = map.m_len << inode->i_blkbits;
765 }
766 return ret;
767}
768
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900769static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800770 struct buffer_head *bh_result, int create, int flag,
771 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900772{
Chao Yuda859852016-01-26 15:42:58 +0800773 return __get_data_block(inode, iblock, bh_result, create,
774 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900775}
776
Chao Yue2b4e2b2015-08-19 19:11:19 +0800777static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900778 struct buffer_head *bh_result, int create)
779{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800780 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800781 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800782}
783
784static int get_data_block_bmap(struct inode *inode, sector_t iblock,
785 struct buffer_head *bh_result, int create)
786{
Yunlei He179448b2015-12-28 21:48:32 +0800787 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800788 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800789 return -EFBIG;
790
Chao Yue2b4e2b2015-08-19 19:11:19 +0800791 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800792 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900793}
794
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700795static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
796{
797 return (offset >> inode->i_blkbits);
798}
799
800static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
801{
802 return (blk << inode->i_blkbits);
803}
804
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900805int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
806 u64 start, u64 len)
807{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700808 struct buffer_head map_bh;
809 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800810 pgoff_t next_pgofs;
Fan Lide1475c2016-01-04 15:56:50 +0800811 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700812 u64 logical = 0, phys = 0, size = 0;
813 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700814 int ret = 0;
815
816 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
817 if (ret)
818 return ret;
819
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -0700820 if (f2fs_has_inline_data(inode)) {
821 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
822 if (ret != -EAGAIN)
823 return ret;
824 }
825
Al Viro59551022016-01-22 15:40:57 -0500826 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800827
828 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800829 if (start >= isize)
830 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700831
Fan Li9a950d52015-12-26 18:07:41 +0800832 if (start + len > isize)
833 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700834
835 if (logical_to_blk(inode, len) == 0)
836 len = blk_to_logical(inode, 1);
837
838 start_blk = logical_to_blk(inode, start);
839 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800840
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700841next:
842 memset(&map_bh, 0, sizeof(struct buffer_head));
843 map_bh.b_size = len;
844
Chao Yue2b4e2b2015-08-19 19:11:19 +0800845 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800846 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700847 if (ret)
848 goto out;
849
850 /* HOLE */
851 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800852 start_blk = next_pgofs;
Fan Li9a950d52015-12-26 18:07:41 +0800853 /* Go through holes util pass the EOF */
Chao Yuda859852016-01-26 15:42:58 +0800854 if (blk_to_logical(inode, start_blk) < isize)
Fan Li9a950d52015-12-26 18:07:41 +0800855 goto prep_next;
856 /* Found a hole beyond isize means no more extents.
857 * Note that the premise is that filesystems don't
858 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700859 */
Fan Li9a950d52015-12-26 18:07:41 +0800860 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700861 }
Fan Li9a950d52015-12-26 18:07:41 +0800862
Chao Yuda5af122016-01-08 20:19:27 +0800863 if (size) {
864 if (f2fs_encrypted_inode(inode))
865 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
866
Fan Li9a950d52015-12-26 18:07:41 +0800867 ret = fiemap_fill_next_extent(fieinfo, logical,
868 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800869 }
Fan Li9a950d52015-12-26 18:07:41 +0800870
871 if (start_blk > last_blk || ret)
872 goto out;
873
874 logical = blk_to_logical(inode, start_blk);
875 phys = blk_to_logical(inode, map_bh.b_blocknr);
876 size = map_bh.b_size;
877 flags = 0;
878 if (buffer_unwritten(&map_bh))
879 flags = FIEMAP_EXTENT_UNWRITTEN;
880
881 start_blk += logical_to_blk(inode, size);
882
883prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700884 cond_resched();
885 if (fatal_signal_pending(current))
886 ret = -EINTR;
887 else
888 goto next;
889out:
890 if (ret == 1)
891 ret = 0;
892
Al Viro59551022016-01-22 15:40:57 -0500893 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700894 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900895}
896
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700897/*
898 * This function was originally taken from fs/mpage.c, and customized for f2fs.
899 * Major change was from block_size == page_size in f2fs by default.
900 */
901static int f2fs_mpage_readpages(struct address_space *mapping,
902 struct list_head *pages, struct page *page,
903 unsigned nr_pages)
904{
905 struct bio *bio = NULL;
906 unsigned page_idx;
907 sector_t last_block_in_bio = 0;
908 struct inode *inode = mapping->host;
909 const unsigned blkbits = inode->i_blkbits;
910 const unsigned blocksize = 1 << blkbits;
911 sector_t block_in_file;
912 sector_t last_block;
913 sector_t last_block_in_file;
914 sector_t block_nr;
915 struct block_device *bdev = inode->i_sb->s_bdev;
916 struct f2fs_map_blocks map;
917
918 map.m_pblk = 0;
919 map.m_lblk = 0;
920 map.m_len = 0;
921 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +0800922 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700923
924 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
925
926 prefetchw(&page->flags);
927 if (pages) {
928 page = list_entry(pages->prev, struct page, lru);
929 list_del(&page->lru);
930 if (add_to_page_cache_lru(page, mapping,
931 page->index, GFP_KERNEL))
932 goto next_page;
933 }
934
935 block_in_file = (sector_t)page->index;
936 last_block = block_in_file + nr_pages;
937 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
938 blkbits;
939 if (last_block > last_block_in_file)
940 last_block = last_block_in_file;
941
942 /*
943 * Map blocks using the previous result first.
944 */
945 if ((map.m_flags & F2FS_MAP_MAPPED) &&
946 block_in_file > map.m_lblk &&
947 block_in_file < (map.m_lblk + map.m_len))
948 goto got_it;
949
950 /*
951 * Then do more f2fs_map_blocks() calls until we are
952 * done with this page.
953 */
954 map.m_flags = 0;
955
956 if (block_in_file < last_block) {
957 map.m_lblk = block_in_file;
958 map.m_len = last_block - block_in_file;
959
Chao Yu46c9e142015-09-18 16:54:16 +0800960 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +0800961 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700962 goto set_error_page;
963 }
964got_it:
965 if ((map.m_flags & F2FS_MAP_MAPPED)) {
966 block_nr = map.m_pblk + block_in_file - map.m_lblk;
967 SetPageMappedToDisk(page);
968
969 if (!PageUptodate(page) && !cleancache_get_page(page)) {
970 SetPageUptodate(page);
971 goto confused;
972 }
973 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300974 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700975 SetPageUptodate(page);
976 unlock_page(page);
977 goto next_page;
978 }
979
980 /*
981 * This page will go to BIO. Do we need to send this
982 * BIO off first?
983 */
984 if (bio && (last_block_in_bio != block_nr - 1)) {
985submit_and_realloc:
986 submit_bio(READ, bio);
987 bio = NULL;
988 }
989 if (bio == NULL) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700990 struct fscrypt_ctx *ctx = NULL;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700991
992 if (f2fs_encrypted_inode(inode) &&
993 S_ISREG(inode->i_mode)) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700994
Jaegeuk Kimb32e4482016-04-11 15:51:57 -0700995 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700996 if (IS_ERR(ctx))
997 goto set_error_page;
998
999 /* wait the page to be moved by cleaning */
Chao Yu08b39fb2015-10-08 13:27:34 +08001000 f2fs_wait_on_encrypted_page_writeback(
1001 F2FS_I_SB(inode), block_nr);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001002 }
1003
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001004 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +02001005 min_t(int, nr_pages, BIO_MAX_PAGES));
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001006 if (!bio) {
1007 if (ctx)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001008 fscrypt_release_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001009 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001010 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001011 bio->bi_bdev = bdev;
1012 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +08001013 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001014 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001015 }
1016
1017 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1018 goto submit_and_realloc;
1019
1020 last_block_in_bio = block_nr;
1021 goto next_page;
1022set_error_page:
1023 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001024 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001025 unlock_page(page);
1026 goto next_page;
1027confused:
1028 if (bio) {
1029 submit_bio(READ, bio);
1030 bio = NULL;
1031 }
1032 unlock_page(page);
1033next_page:
1034 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001035 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001036 }
1037 BUG_ON(pages && !list_empty(pages));
1038 if (bio)
1039 submit_bio(READ, bio);
1040 return 0;
1041}
1042
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001043static int f2fs_read_data_page(struct file *file, struct page *page)
1044{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001045 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001046 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001047
Chao Yuc20e89c2014-05-06 16:53:08 +08001048 trace_f2fs_readpage(page, DATA);
1049
arter97e1c42042014-08-06 23:22:50 +09001050 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001051 if (f2fs_has_inline_data(inode))
1052 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001053 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001054 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001055 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001056}
1057
1058static int f2fs_read_data_pages(struct file *file,
1059 struct address_space *mapping,
1060 struct list_head *pages, unsigned nr_pages)
1061{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001062 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001063 struct page *page = list_entry(pages->prev, struct page, lru);
1064
1065 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001066
1067 /* If the file has inline data, skip readpages */
1068 if (f2fs_has_inline_data(inode))
1069 return 0;
1070
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001071 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001072}
1073
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001074int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001075{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001076 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001077 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001078 struct dnode_of_data dn;
1079 int err = 0;
1080
1081 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001082 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001083 if (err)
1084 return err;
1085
Chao Yu28bc1062016-02-06 14:40:34 +08001086 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001087
1088 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001089 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001090 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001091 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001092 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001093
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001094 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001095 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001096
1097 /* wait for GCed encrypted page writeback */
1098 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001099 fio->old_blkaddr);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001100retry_encrypt:
1101 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1102 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001103 if (IS_ERR(fio->encrypted_page)) {
1104 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001105 if (err == -ENOMEM) {
1106 /* flush pending ios and wait for a while */
1107 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1108 congestion_wait(BLK_RW_ASYNC, HZ/50);
1109 gfp_flags |= __GFP_NOFAIL;
1110 err = 0;
1111 goto retry_encrypt;
1112 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001113 goto out_writepage;
1114 }
1115 }
1116
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001117 set_page_writeback(page);
1118
1119 /*
1120 * If current allocation needs SSR,
1121 * it had better in-place writes for updated data.
1122 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001123 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001124 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001125 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001126 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001127 rewrite_data_page(fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001128 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001129 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001130 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001131 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001132 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001133 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001134 if (page->index == 0)
1135 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001136 }
1137out_writepage:
1138 f2fs_put_dnode(&dn);
1139 return err;
1140}
1141
1142static int f2fs_write_data_page(struct page *page,
1143 struct writeback_control *wbc)
1144{
1145 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001146 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001147 loff_t i_size = i_size_read(inode);
1148 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001149 >> PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001150 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001151 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001152 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001153 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001154 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001155 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001156 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001157 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001158 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001159 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001160
Chao Yuecda0de2014-05-06 16:48:26 +08001161 trace_f2fs_writepage(page, DATA);
1162
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001163 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001164 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001165
1166 /*
1167 * If the offset is out-of-range of file size,
1168 * this page does not have to be written to disk.
1169 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001170 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001171 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001172 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001173
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001174 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001175write:
Chao Yucaf00472015-01-28 17:48:42 +08001176 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001177 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001178 if (f2fs_is_drop_cache(inode))
1179 goto out;
1180 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1181 available_free_memory(sbi, BASE_CHECK))
1182 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001183
Jaegeuk Kim39936832012-11-22 16:21:29 +09001184 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001185 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001186 if (unlikely(f2fs_cp_error(sbi)))
1187 goto redirty_out;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001188 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001189 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001190 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001191
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001192 /* we should bypass data pages to proceed the kworkder jobs */
1193 if (unlikely(f2fs_cp_error(sbi))) {
1194 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001195 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001196 }
1197
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001198 if (!wbc->for_reclaim)
1199 need_balance_fs = true;
1200 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001201 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001202
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001203 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001204 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001205 if (f2fs_has_inline_data(inode))
1206 err = f2fs_write_inline_data(inode, page);
1207 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001208 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001209 f2fs_unlock_op(sbi);
1210done:
1211 if (err && err != -ENOENT)
1212 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001213
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001214 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001215out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001216 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001217 if (err)
1218 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001219
1220 if (wbc->for_reclaim) {
1221 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001222 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001223 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001224
1225 unlock_page(page);
1226 f2fs_balance_fs(sbi, need_balance_fs);
1227
1228 if (unlikely(f2fs_cp_error(sbi)))
1229 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1230
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001231 return 0;
1232
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001233redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001234 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001235 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001236}
1237
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001238static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1239 void *data)
1240{
1241 struct address_space *mapping = data;
1242 int ret = mapping->a_ops->writepage(page, wbc);
1243 mapping_set_error(mapping, ret);
1244 return ret;
1245}
1246
Chao Yu8f46dca2015-07-14 18:56:10 +08001247/*
1248 * This function was copied from write_cche_pages from mm/page-writeback.c.
1249 * The major change is making write step of cold data page separately from
1250 * warm/hot data page.
1251 */
1252static int f2fs_write_cache_pages(struct address_space *mapping,
1253 struct writeback_control *wbc, writepage_t writepage,
1254 void *data)
1255{
1256 int ret = 0;
1257 int done = 0;
1258 struct pagevec pvec;
1259 int nr_pages;
1260 pgoff_t uninitialized_var(writeback_index);
1261 pgoff_t index;
1262 pgoff_t end; /* Inclusive */
1263 pgoff_t done_index;
1264 int cycled;
1265 int range_whole = 0;
1266 int tag;
1267 int step = 0;
1268
1269 pagevec_init(&pvec, 0);
1270next:
1271 if (wbc->range_cyclic) {
1272 writeback_index = mapping->writeback_index; /* prev offset */
1273 index = writeback_index;
1274 if (index == 0)
1275 cycled = 1;
1276 else
1277 cycled = 0;
1278 end = -1;
1279 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001280 index = wbc->range_start >> PAGE_SHIFT;
1281 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001282 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1283 range_whole = 1;
1284 cycled = 1; /* ignore range_cyclic tests */
1285 }
1286 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1287 tag = PAGECACHE_TAG_TOWRITE;
1288 else
1289 tag = PAGECACHE_TAG_DIRTY;
1290retry:
1291 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1292 tag_pages_for_writeback(mapping, index, end);
1293 done_index = index;
1294 while (!done && (index <= end)) {
1295 int i;
1296
1297 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1298 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1299 if (nr_pages == 0)
1300 break;
1301
1302 for (i = 0; i < nr_pages; i++) {
1303 struct page *page = pvec.pages[i];
1304
1305 if (page->index > end) {
1306 done = 1;
1307 break;
1308 }
1309
1310 done_index = page->index;
1311
1312 lock_page(page);
1313
1314 if (unlikely(page->mapping != mapping)) {
1315continue_unlock:
1316 unlock_page(page);
1317 continue;
1318 }
1319
1320 if (!PageDirty(page)) {
1321 /* someone wrote it for us */
1322 goto continue_unlock;
1323 }
1324
Tiezhu Yang737f1892015-07-17 12:56:00 +08001325 if (step == is_cold_data(page))
Chao Yu8f46dca2015-07-14 18:56:10 +08001326 goto continue_unlock;
1327
1328 if (PageWriteback(page)) {
1329 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001330 f2fs_wait_on_page_writeback(page,
1331 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001332 else
1333 goto continue_unlock;
1334 }
1335
1336 BUG_ON(PageWriteback(page));
1337 if (!clear_page_dirty_for_io(page))
1338 goto continue_unlock;
1339
1340 ret = (*writepage)(page, wbc, data);
1341 if (unlikely(ret)) {
1342 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1343 unlock_page(page);
1344 ret = 0;
1345 } else {
1346 done_index = page->index + 1;
1347 done = 1;
1348 break;
1349 }
1350 }
1351
1352 if (--wbc->nr_to_write <= 0 &&
1353 wbc->sync_mode == WB_SYNC_NONE) {
1354 done = 1;
1355 break;
1356 }
1357 }
1358 pagevec_release(&pvec);
1359 cond_resched();
1360 }
1361
1362 if (step < 1) {
1363 step++;
1364 goto next;
1365 }
1366
1367 if (!cycled && !done) {
1368 cycled = 1;
1369 index = 0;
1370 end = writeback_index - 1;
1371 goto retry;
1372 }
1373 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1374 mapping->writeback_index = done_index;
1375
1376 return ret;
1377}
1378
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001379static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001380 struct writeback_control *wbc)
1381{
1382 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001383 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001384 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001385 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001386 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001387
P J Pcfb185a2013-04-03 11:38:00 +09001388 /* deal with chardevs and other special file */
1389 if (!mapping->a_ops->writepage)
1390 return 0;
1391
Chao Yu6a290542015-07-17 18:02:39 +08001392 /* skip writing if there is no dirty page in this inode */
1393 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1394 return 0;
1395
Jaegeuk Kima1257022015-10-08 10:40:07 -07001396 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1397 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1398 available_free_memory(sbi, DIRTY_DENTS))
1399 goto skip_write;
1400
Chao Yud323d002015-10-27 09:53:45 +08001401 /* skip writing during file defragment */
1402 if (is_inode_flag_set(F2FS_I(inode), FI_DO_DEFRAG))
1403 goto skip_write;
1404
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001405 /* during POR, we don't need to trigger writepage at all. */
1406 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1407 goto skip_write;
1408
Yunlei Hed31c7c32016-02-04 16:14:00 +08001409 trace_f2fs_writepages(mapping->host, wbc, DATA);
1410
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001411 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001412
Jaegeuk Kim25c13552016-01-20 23:46:05 +08001413 if (!S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_ALL) {
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001414 mutex_lock(&sbi->writepages);
1415 locked = true;
1416 }
Chao Yu8f46dca2015-07-14 18:56:10 +08001417 ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Chao Yu0c3a5792016-01-18 18:28:11 +08001418 f2fs_submit_merged_bio_cond(sbi, inode, NULL, 0, DATA, WRITE);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001419 if (locked)
1420 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001421
Chao Yuc227f912015-12-16 13:09:20 +08001422 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001423
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001424 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001425 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001426
1427skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001428 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001429 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001430 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001431}
1432
Chao Yu3aab8f82014-07-02 13:25:04 +08001433static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1434{
1435 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001436 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001437
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001438 if (to > i_size) {
1439 truncate_pagecache(inode, i_size);
1440 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001441 }
1442}
1443
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001444static int prepare_write_begin(struct f2fs_sb_info *sbi,
1445 struct page *page, loff_t pos, unsigned len,
1446 block_t *blk_addr, bool *node_changed)
1447{
1448 struct inode *inode = page->mapping->host;
1449 pgoff_t index = page->index;
1450 struct dnode_of_data dn;
1451 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001452 bool locked = false;
1453 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001454 int err = 0;
1455
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001456 /*
1457 * we already allocated all the blocks, so we don't need to get
1458 * the block addresses when there is no need to fill the page.
1459 */
1460 if (!f2fs_has_inline_data(inode) && !f2fs_encrypted_inode(inode) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001461 len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001462 return 0;
1463
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001464 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001465 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001466 f2fs_lock_op(sbi);
1467 locked = true;
1468 }
1469restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001470 /* check inline_data */
1471 ipage = get_node_page(sbi, inode->i_ino);
1472 if (IS_ERR(ipage)) {
1473 err = PTR_ERR(ipage);
1474 goto unlock_out;
1475 }
1476
1477 set_new_dnode(&dn, inode, ipage, ipage, 0);
1478
1479 if (f2fs_has_inline_data(inode)) {
1480 if (pos + len <= MAX_INLINE_DATA) {
1481 read_inline_data(page, ipage);
1482 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001483 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001484 } else {
1485 err = f2fs_convert_inline_page(&dn, page);
1486 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001487 goto out;
1488 if (dn.data_blkaddr == NULL_ADDR)
1489 err = f2fs_get_block(&dn, index);
1490 }
1491 } else if (locked) {
1492 err = f2fs_get_block(&dn, index);
1493 } else {
1494 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1495 dn.data_blkaddr = ei.blk + index - ei.fofs;
1496 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001497 /* hole case */
1498 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimb483fad2016-01-20 08:27:37 +08001499 if (err || (!err && dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001500 f2fs_put_dnode(&dn);
1501 f2fs_lock_op(sbi);
1502 locked = true;
1503 goto restart;
1504 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001505 }
1506 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001507
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001508 /* convert_inline_page can make node_changed */
1509 *blk_addr = dn.data_blkaddr;
1510 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001511out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001512 f2fs_put_dnode(&dn);
1513unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001514 if (locked)
1515 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001516 return err;
1517}
1518
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001519static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1520 loff_t pos, unsigned len, unsigned flags,
1521 struct page **pagep, void **fsdata)
1522{
1523 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001524 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001525 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001526 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001527 bool need_balance = false;
1528 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001529 int err = 0;
1530
Chao Yu62aed042014-05-06 16:46:04 +08001531 trace_f2fs_write_begin(inode, pos, len, flags);
1532
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001533 /*
1534 * We should check this at this moment to avoid deadlock on inode page
1535 * and #0 page. The locking rule for inline_data conversion should be:
1536 * lock_page(page #0) -> lock_page(inode_page)
1537 */
1538 if (index != 0) {
1539 err = f2fs_convert_inline_inode(inode);
1540 if (err)
1541 goto fail;
1542 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001543repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001544 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001545 if (!page) {
1546 err = -ENOMEM;
1547 goto fail;
1548 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001549
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001550 *pagep = page;
1551
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001552 err = prepare_write_begin(sbi, page, pos, len,
1553 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001554 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001555 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001556
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001557 if (need_balance && has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001558 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001559 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001560 lock_page(page);
1561 if (page->mapping != mapping) {
1562 /* The page got truncated from under us */
1563 f2fs_put_page(page, 1);
1564 goto repeat;
1565 }
1566 }
1567
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001568 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001569
Chao Yu08b39fb2015-10-08 13:27:34 +08001570 /* wait for GCed encrypted page writeback */
1571 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001572 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001573
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001574 if (len == PAGE_SIZE)
Chao Yu90d43882015-07-08 18:24:38 +08001575 goto out_update;
1576 if (PageUptodate(page))
1577 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001578
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001579 if ((pos & PAGE_MASK) >= i_size_read(inode)) {
1580 unsigned start = pos & (PAGE_SIZE - 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001581 unsigned end = start + len;
1582
1583 /* Reading beyond i_size is simple: memset to zero */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001584 zero_user_segments(page, 0, start, end, PAGE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001585 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001586 }
1587
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001588 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001589 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001590 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001591 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001592 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001593 .type = DATA,
1594 .rw = READ_SYNC,
Chao Yu7a9d7542016-02-22 18:36:38 +08001595 .old_blkaddr = blkaddr,
1596 .new_blkaddr = blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001597 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001598 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001599 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001600 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001601 if (err)
1602 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001603
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001604 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001605 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001606 err = -EIO;
1607 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001608 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001609 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001610 f2fs_put_page(page, 1);
1611 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001612 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001613
1614 /* avoid symlink page */
1615 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001616 err = fscrypt_decrypt_page(page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001617 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001618 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001619 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001620 }
Chao Yu90d43882015-07-08 18:24:38 +08001621out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001622 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001623out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001624 clear_cold_data(page);
1625 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001626
Chao Yu3aab8f82014-07-02 13:25:04 +08001627fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001628 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001629 f2fs_write_failed(mapping, pos + len);
1630 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001631}
1632
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001633static int f2fs_write_end(struct file *file,
1634 struct address_space *mapping,
1635 loff_t pos, unsigned len, unsigned copied,
1636 struct page *page, void *fsdata)
1637{
1638 struct inode *inode = page->mapping->host;
1639
Chao Yudfb2bf32014-05-06 16:47:23 +08001640 trace_f2fs_write_end(inode, pos, len, copied);
1641
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001642 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001643
1644 if (pos + copied > i_size_read(inode)) {
1645 i_size_write(inode, pos + copied);
1646 mark_inode_dirty(inode);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001647 }
1648
Chao Yu75c3c8b2013-11-16 14:15:59 +08001649 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001650 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001651 return copied;
1652}
1653
Omar Sandoval6f673762015-03-16 04:33:52 -07001654static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1655 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001656{
1657 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001658
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001659 if (offset & blocksize_mask)
1660 return -EINVAL;
1661
Al Viro5b46f252014-03-16 18:07:34 -04001662 if (iov_iter_alignment(iter) & blocksize_mask)
1663 return -EINVAL;
1664
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001665 return 0;
1666}
1667
Omar Sandoval22c61862015-03-16 04:33:53 -07001668static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
1669 loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001670{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001671 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001672 struct inode *inode = mapping->host;
1673 size_t count = iov_iter_count(iter);
1674 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001675
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001676 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001677 if (err)
1678 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001679
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001680 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1681 return 0;
1682
Omar Sandoval6f673762015-03-16 04:33:52 -07001683 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001684
Chao Yue2b4e2b2015-08-19 19:11:19 +08001685 err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block_dio);
Omar Sandoval6f673762015-03-16 04:33:52 -07001686 if (err < 0 && iov_iter_rw(iter) == WRITE)
Chao Yu3aab8f82014-07-02 13:25:04 +08001687 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001688
Omar Sandoval6f673762015-03-16 04:33:52 -07001689 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001690
Chao Yu3aab8f82014-07-02 13:25:04 +08001691 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001692}
1693
Chao Yu487261f2015-02-05 17:44:29 +08001694void f2fs_invalidate_page(struct page *page, unsigned int offset,
1695 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001696{
1697 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001698 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001699
Chao Yu487261f2015-02-05 17:44:29 +08001700 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001701 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001702 return;
1703
Chao Yu487261f2015-02-05 17:44:29 +08001704 if (PageDirty(page)) {
1705 if (inode->i_ino == F2FS_META_INO(sbi))
1706 dec_page_count(sbi, F2FS_DIRTY_META);
1707 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1708 dec_page_count(sbi, F2FS_DIRTY_NODES);
1709 else
1710 inode_dec_dirty_pages(inode);
1711 }
Chao Yudecd36b2015-08-07 18:42:09 +08001712
1713 /* This is atomic written page, keep Private */
1714 if (IS_ATOMIC_WRITTEN_PAGE(page))
1715 return;
1716
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001717 ClearPagePrivate(page);
1718}
1719
Chao Yu487261f2015-02-05 17:44:29 +08001720int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001721{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001722 /* If this is dirty page, keep PagePrivate */
1723 if (PageDirty(page))
1724 return 0;
1725
Chao Yudecd36b2015-08-07 18:42:09 +08001726 /* This is atomic written page, keep Private */
1727 if (IS_ATOMIC_WRITTEN_PAGE(page))
1728 return 0;
1729
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001730 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001731 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001732}
1733
1734static int f2fs_set_data_page_dirty(struct page *page)
1735{
1736 struct address_space *mapping = page->mapping;
1737 struct inode *inode = mapping->host;
1738
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001739 trace_f2fs_set_page_dirty(page, DATA);
1740
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001741 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001742
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001743 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001744 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1745 register_inmem_page(inode, page);
1746 return 1;
1747 }
1748 /*
1749 * Previously, this page has been registered, we just
1750 * return here.
1751 */
1752 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001753 }
1754
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001755 if (!PageDirty(page)) {
1756 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001757 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001758 return 1;
1759 }
1760 return 0;
1761}
1762
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001763static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1764{
Chao Yu454ae7e2014-04-22 13:34:01 +08001765 struct inode *inode = mapping->host;
1766
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001767 if (f2fs_has_inline_data(inode))
1768 return 0;
1769
1770 /* make sure allocating whole blocks */
1771 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1772 filemap_write_and_wait(mapping);
1773
Chao Yue2b4e2b2015-08-19 19:11:19 +08001774 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001775}
1776
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001777const struct address_space_operations f2fs_dblock_aops = {
1778 .readpage = f2fs_read_data_page,
1779 .readpages = f2fs_read_data_pages,
1780 .writepage = f2fs_write_data_page,
1781 .writepages = f2fs_write_data_pages,
1782 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001783 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001784 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001785 .invalidatepage = f2fs_invalidate_page,
1786 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001787 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001788 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001789};