blob: a11a8c789cd5edecd1fc66a487b03c0f220c689c [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) {
Chao Yu12377022015-05-25 18:03:38 +080037 f2fs_release_crypto_ctx(bio->bi_private);
38 } else {
39 f2fs_end_io_crypto_work(bio->bi_private, bio);
40 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 Kim4375a332015-04-23 12:04:33 -070067 f2fs_restore_and_release_control_page(&page);
68
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020069 if (unlikely(bio->bi_error)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -070070 set_page_dirty(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090071 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim744602c2014-01-24 09:42:16 +090072 f2fs_stop_checkpoint(sbi);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090073 }
74 end_page_writeback(page);
75 dec_page_count(sbi, F2FS_WRITEBACK);
Linus Torvaldsf5688492014-01-30 11:19:05 -080076 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090077
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090078 if (!get_pages(sbi, F2FS_WRITEBACK) &&
79 !list_empty(&sbi->cp_wait.task_list))
80 wake_up(&sbi->cp_wait);
81
82 bio_put(bio);
83}
84
Gu Zheng940a6d32013-12-20 17:39:59 +080085/*
86 * Low-level block read/write IO operations.
87 */
88static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
89 int npages, bool is_read)
90{
91 struct bio *bio;
92
Jaegeuk Kim740432f2015-08-14 11:43:56 -070093 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +080094
95 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +080096 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +080097 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +080098 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +080099
100 return bio;
101}
102
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900103static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900104{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900105 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900106
107 if (!io->bio)
108 return;
109
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700110 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800111 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700112 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800113 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900114
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700115 submit_bio(fio->rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900116 io->bio = NULL;
117}
118
Chao Yu0fd785e2016-01-18 18:24:59 +0800119bool is_merged_page(struct f2fs_sb_info *sbi, struct page *page,
120 enum page_type type)
121{
122 enum page_type btype = PAGE_TYPE_OF_BIO(type);
123 struct f2fs_bio_info *io = &sbi->write_io[btype];
124 struct bio_vec *bvec;
125 struct page *target;
126 int i;
127
128 down_read(&io->io_rwsem);
129 if (!io->bio) {
130 up_read(&io->io_rwsem);
131 return false;
132 }
133
134 bio_for_each_segment_all(bvec, io->bio, i) {
135
136 if (bvec->bv_page->mapping) {
137 target = bvec->bv_page;
138 } else {
139 struct f2fs_crypto_ctx *ctx;
140
141 /* encrypted page */
142 ctx = (struct f2fs_crypto_ctx *)page_private(
143 bvec->bv_page);
144 target = ctx->w.control_page;
145 }
146
147 if (page == target) {
148 up_read(&io->io_rwsem);
149 return true;
150 }
151 }
152
153 up_read(&io->io_rwsem);
154 return false;
155}
156
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900157void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900158 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900159{
160 enum page_type btype = PAGE_TYPE_OF_BIO(type);
161 struct f2fs_bio_info *io;
162
163 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
164
Chao Yudf0f8dc2014-03-22 14:57:23 +0800165 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900166
167 /* change META to META_FLUSH in the checkpoint procedure */
168 if (type >= META_FLUSH) {
169 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700170 if (test_opt(sbi, NOBARRIER))
171 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
172 else
173 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900174 }
175 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800176 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900177}
178
179/*
180 * Fill the locked page with data located in the block address.
181 * Return unlocked page.
182 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700183int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900184{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900185 struct bio *bio;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700186 struct page *page = fio->encrypted_page ? fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900187
Chao Yu2ace38e2014-12-24 16:08:14 +0800188 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700189 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900190
191 /* Allocate a new bio */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700192 bio = __bio_alloc(fio->sbi, fio->blk_addr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900193
194 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
195 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900196 return -EFAULT;
197 }
198
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800199 submit_bio(fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900200 return 0;
201}
202
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700203void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900204{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700205 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900206 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900207 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800208 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700209 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900210
Gu Zheng940a6d32013-12-20 17:39:59 +0800211 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900212
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800213 verify_block_addr(sbi, fio->blk_addr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900214
Chao Yudf0f8dc2014-03-22 14:57:23 +0800215 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900216
Gu Zheng940a6d32013-12-20 17:39:59 +0800217 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900218 inc_page_count(sbi, F2FS_WRITEBACK);
219
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800220 if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900221 io->fio.rw != fio->rw))
222 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900223alloc_new:
224 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700225 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800226
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800227 io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900228 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900229 }
230
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700231 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
232
233 if (bio_add_page(io->bio, bio_page, PAGE_CACHE_SIZE, 0) <
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900234 PAGE_CACHE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900235 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900236 goto alloc_new;
237 }
238
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800239 io->last_block_in_bio = fio->blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700240 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900241
Chao Yudf0f8dc2014-03-22 14:57:23 +0800242 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700243 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900244}
245
246/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900247 * Lock ordering for the change of data block address:
248 * ->data_page
249 * ->node_page
250 * update block addresses in the node page
251 */
Chao Yu216a6202015-03-19 19:23:32 +0800252void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900253{
254 struct f2fs_node *rn;
255 __le32 *addr_array;
256 struct page *node_page = dn->node_page;
257 unsigned int ofs_in_node = dn->ofs_in_node;
258
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000259 f2fs_wait_on_page_writeback(node_page, NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900260
Gu Zheng45590712013-07-15 17:57:38 +0800261 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900262
263 /* Get physical address of data block */
264 addr_array = blkaddr_in_node(rn);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800265 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800266 if (set_page_dirty(node_page))
267 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900268}
269
270int reserve_new_block(struct dnode_of_data *dn)
271{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700272 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900273
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900274 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900275 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800276 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900277 return -ENOSPC;
278
Namjae Jeonc01e2852013-04-23 17:00:52 +0900279 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
280
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900281 dn->data_blkaddr = NEW_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800282 set_data_blkaddr(dn);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900283 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900284 sync_inode_page(dn);
285 return 0;
286}
287
Huajun Lib6009652013-11-10 23:13:18 +0800288int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
289{
290 bool need_put = dn->inode_page ? false : true;
291 int err;
292
293 err = get_dnode_of_data(dn, index, ALLOC_NODE);
294 if (err)
295 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900296
Huajun Lib6009652013-11-10 23:13:18 +0800297 if (dn->data_blkaddr == NULL_ADDR)
298 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900299 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800300 f2fs_put_dnode(dn);
301 return err;
302}
303
Fan Li759af1c2015-08-05 15:52:16 +0800304int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900305{
Chao Yu028a41e2015-03-19 19:26:02 +0800306 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800307 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800308
Fan Li759af1c2015-08-05 15:52:16 +0800309 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
310 dn->data_blkaddr = ei.blk + index - ei.fofs;
311 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800312 }
313
Fan Li759af1c2015-08-05 15:52:16 +0800314 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900315}
316
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700317struct page *get_read_data_page(struct inode *inode, pgoff_t index,
318 int rw, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900319{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900320 struct address_space *mapping = inode->i_mapping;
321 struct dnode_of_data dn;
322 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800323 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900324 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800325 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700326 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800327 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700328 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700329 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800330 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900331
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700332 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
333 return read_mapping_page(mapping, index, NULL);
334
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700335 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900336 if (!page)
337 return ERR_PTR(-ENOMEM);
338
Chao Yucb3bc9e2015-02-05 18:03:40 +0800339 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
340 dn.data_blkaddr = ei.blk + index - ei.fofs;
341 goto got_it;
342 }
343
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900344 set_new_dnode(&dn, inode, NULL, NULL, 0);
345 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700346 if (err)
347 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900348 f2fs_put_dnode(&dn);
349
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900350 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700351 err = -ENOENT;
352 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900353 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800354got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700355 if (PageUptodate(page)) {
356 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900357 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700358 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900359
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900360 /*
361 * A new dentry page is allocated but not able to be written, since its
362 * new inode page couldn't be allocated due to -ENOSPC.
363 * In such the case, its blkaddr can be remained as NEW_ADDR.
364 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
365 */
366 if (dn.data_blkaddr == NEW_ADDR) {
367 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
368 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700369 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900370 return page;
371 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900372
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800373 fio.blk_addr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700374 fio.page = page;
375 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900376 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700377 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700378 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700379
380put_err:
381 f2fs_put_page(page, 1);
382 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700383}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900384
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700385struct page *find_data_page(struct inode *inode, pgoff_t index)
386{
387 struct address_space *mapping = inode->i_mapping;
388 struct page *page;
389
390 page = find_get_page(mapping, index);
391 if (page && PageUptodate(page))
392 return page;
393 f2fs_put_page(page, 0);
394
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700395 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700396 if (IS_ERR(page))
397 return page;
398
399 if (PageUptodate(page))
400 return page;
401
402 wait_on_page_locked(page);
403 if (unlikely(!PageUptodate(page))) {
404 f2fs_put_page(page, 0);
405 return ERR_PTR(-EIO);
406 }
407 return page;
408}
409
410/*
411 * If it tries to access a hole, return an error.
412 * Because, the callers, functions in dir.c and GC, should be able to know
413 * whether this page exists or not.
414 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700415struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
416 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700417{
418 struct address_space *mapping = inode->i_mapping;
419 struct page *page;
420repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700421 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700422 if (IS_ERR(page))
423 return page;
424
425 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900426 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900427 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900428 f2fs_put_page(page, 1);
429 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900430 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900431 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900432 f2fs_put_page(page, 1);
433 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900434 }
435 return page;
436}
437
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900438/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900439 * Caller ensures that this data page is never allocated.
440 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900441 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800442 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
443 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800444 * Note that, ipage is set only by make_empty_dir, and if any error occur,
445 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900446 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900447struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900448 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900449{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900450 struct address_space *mapping = inode->i_mapping;
451 struct page *page;
452 struct dnode_of_data dn;
453 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800454
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700455 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800456 if (!page) {
457 /*
458 * before exiting, we should make sure ipage will be released
459 * if any error occur.
460 */
461 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700462 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800463 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900464
Jaegeuk Kima8865372013-12-27 17:04:17 +0900465 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800466 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700467 if (err) {
468 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900469 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900470 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700471 if (!ipage)
472 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900473
474 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700475 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900476
477 if (dn.data_blkaddr == NEW_ADDR) {
478 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900479 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900480 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700481 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900482
Jaegeuk Kim76121182016-01-01 22:03:47 -0800483 /* if ipage exists, blkaddr should be NEW_ADDR */
484 f2fs_bug_on(F2FS_I_SB(inode), ipage);
485 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700486 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800487 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900488 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700489got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800490 if (new_i_size && i_size_read(inode) <
491 ((loff_t)(index + 1) << PAGE_CACHE_SHIFT)) {
492 i_size_write(inode, ((loff_t)(index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900493 /* Only the directory inode sets new_i_size */
494 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900495 }
496 return page;
497}
498
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900499static int __allocate_data_block(struct dnode_of_data *dn)
500{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700501 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700502 struct f2fs_inode_info *fi = F2FS_I(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900503 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900504 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800505 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700506 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900507
508 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
509 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800510
511 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
512 if (dn->data_blkaddr == NEW_ADDR)
513 goto alloc;
514
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900515 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
516 return -ENOSPC;
517
Chao Yudf6136e2015-03-23 10:33:37 +0800518alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900519 get_node_info(sbi, dn->nid, &ni);
520 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
521
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800522 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
523 seg = CURSEG_DIRECT_IO;
524
Chao Yudf6136e2015-03-23 10:33:37 +0800525 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
526 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800527 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900528
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700529 /* update i_size */
530 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), fi) +
531 dn->ofs_in_node;
Chao Yu9edcdab2015-09-11 14:43:52 +0800532 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT))
533 i_size_write(dn->inode,
534 ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900535 return 0;
536}
537
Chao Yu6d5a1492015-12-24 18:04:56 +0800538static int __allocate_data_blocks(struct inode *inode, loff_t offset,
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800539 size_t count)
540{
541 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
542 struct dnode_of_data dn;
543 u64 start = F2FS_BYTES_TO_BLK(offset);
544 u64 len = F2FS_BYTES_TO_BLK(count);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800545 bool allocated = false;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800546 u64 end_offset;
Chao Yu6d5a1492015-12-24 18:04:56 +0800547 int err = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800548
549 while (len) {
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800550 f2fs_lock_op(sbi);
551
552 /* When reading holes, we need its node page */
553 set_new_dnode(&dn, inode, NULL, NULL, 0);
Chao Yu6d5a1492015-12-24 18:04:56 +0800554 err = get_dnode_of_data(&dn, start, ALLOC_NODE);
555 if (err)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800556 goto out;
557
558 allocated = false;
559 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
560
561 while (dn.ofs_in_node < end_offset && len) {
Chao Yud6d4f1c2015-03-12 17:04:24 +0800562 block_t blkaddr;
563
Chao Yu6d5a1492015-12-24 18:04:56 +0800564 if (unlikely(f2fs_cp_error(sbi))) {
565 err = -EIO;
Chao Yuf9811702015-09-21 20:17:52 +0800566 goto sync_out;
Chao Yu6d5a1492015-12-24 18:04:56 +0800567 }
Chao Yuf9811702015-09-21 20:17:52 +0800568
Chao Yud6d4f1c2015-03-12 17:04:24 +0800569 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800570 if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR) {
Chao Yu6d5a1492015-12-24 18:04:56 +0800571 err = __allocate_data_block(&dn);
572 if (err)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800573 goto sync_out;
574 allocated = true;
575 }
576 len--;
577 start++;
578 dn.ofs_in_node++;
579 }
580
581 if (allocated)
582 sync_inode_page(&dn);
583
584 f2fs_put_dnode(&dn);
585 f2fs_unlock_op(sbi);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800586
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800587 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800588 }
Chao Yu6d5a1492015-12-24 18:04:56 +0800589 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800590
591sync_out:
592 if (allocated)
593 sync_inode_page(&dn);
594 f2fs_put_dnode(&dn);
595out:
596 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800597 f2fs_balance_fs(sbi, allocated);
Chao Yu6d5a1492015-12-24 18:04:56 +0800598 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800599}
600
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900601/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700602 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
603 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800604 * If original data blocks are allocated, then give them to blockdev.
605 * Otherwise,
606 * a. preallocate requested block addresses
607 * b. do not use extent cache for better performance
608 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900609 */
Chao Yud323d002015-10-27 09:53:45 +0800610int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800611 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900612{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700613 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900614 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800615 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900616 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
617 pgoff_t pgofs, end_offset;
618 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800619 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900620 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800621 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900622
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700623 map->m_len = 0;
624 map->m_flags = 0;
625
626 /* it only supports block size == page size */
627 pgofs = (pgoff_t)map->m_lblk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900628
Chao Yu7e4dde72015-02-05 17:51:34 +0800629 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700630 map->m_pblk = ei.blk + pgofs - ei.fofs;
631 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
632 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900633 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800634 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900635
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800636 if (create)
Chao Yu3104af32015-12-23 17:11:43 +0800637 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900638
639 /* When reading holes, we need its node page */
640 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900641 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900642 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900643 if (err == -ENOENT)
644 err = 0;
645 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900646 }
Chao Yu973163f2015-09-18 16:51:51 +0800647
648 if (dn.data_blkaddr == NEW_ADDR || dn.data_blkaddr == NULL_ADDR) {
649 if (create) {
Chao Yuf9811702015-09-21 20:17:52 +0800650 if (unlikely(f2fs_cp_error(sbi))) {
651 err = -EIO;
652 goto put_out;
653 }
Chao Yu973163f2015-09-18 16:51:51 +0800654 err = __allocate_data_block(&dn);
655 if (err)
656 goto put_out;
657 allocated = true;
658 map->m_flags = F2FS_MAP_NEW;
659 } else {
660 if (flag != F2FS_GET_BLOCK_FIEMAP ||
661 dn.data_blkaddr != NEW_ADDR) {
662 if (flag == F2FS_GET_BLOCK_BMAP)
663 err = -ENOENT;
664 goto put_out;
665 }
666
667 /*
668 * preallocated unwritten block should be mapped
669 * for fiemap.
670 */
671 if (dn.data_blkaddr == NEW_ADDR)
672 map->m_flags = F2FS_MAP_UNWRITTEN;
Chao Yue2b4e2b2015-08-19 19:11:19 +0800673 }
Chao Yue2b4e2b2015-08-19 19:11:19 +0800674 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900675
Chao Yu973163f2015-09-18 16:51:51 +0800676 map->m_flags |= F2FS_MAP_MAPPED;
677 map->m_pblk = dn.data_blkaddr;
678 map->m_len = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900679
Chao Yu6403eb12014-04-26 19:59:52 +0800680 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900681 dn.ofs_in_node++;
682 pgofs++;
683
684get_next:
Fan Li7df3a432015-12-17 13:20:59 +0800685 if (map->m_len >= maxblocks)
686 goto sync_out;
687
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900688 if (dn.ofs_in_node >= end_offset) {
689 if (allocated)
690 sync_inode_page(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900691 f2fs_put_dnode(&dn);
692
Chao Yu3104af32015-12-23 17:11:43 +0800693 if (create) {
694 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800695 f2fs_balance_fs(sbi, allocated);
Chao Yu3104af32015-12-23 17:11:43 +0800696 f2fs_lock_op(sbi);
697 }
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800698 allocated = false;
Chao Yu3104af32015-12-23 17:11:43 +0800699
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900700 set_new_dnode(&dn, inode, NULL, NULL, 0);
701 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900702 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900703 if (err == -ENOENT)
704 err = 0;
705 goto unlock_out;
706 }
Chao Yue2b4e2b2015-08-19 19:11:19 +0800707
Chao Yu6403eb12014-04-26 19:59:52 +0800708 end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900709 }
710
Fan Li7df3a432015-12-17 13:20:59 +0800711 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800712
Fan Li7df3a432015-12-17 13:20:59 +0800713 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
714 if (create) {
715 if (unlikely(f2fs_cp_error(sbi))) {
716 err = -EIO;
717 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800718 }
Fan Li7df3a432015-12-17 13:20:59 +0800719 err = __allocate_data_block(&dn);
720 if (err)
721 goto sync_out;
722 allocated = true;
723 map->m_flags |= F2FS_MAP_NEW;
724 blkaddr = dn.data_blkaddr;
725 } else {
726 /*
727 * we only merge preallocated unwritten blocks
728 * for fiemap.
729 */
730 if (flag != F2FS_GET_BLOCK_FIEMAP ||
731 blkaddr != NEW_ADDR)
732 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900733 }
734 }
Fan Li7df3a432015-12-17 13:20:59 +0800735
736 /* Give more consecutive addresses for the readahead */
737 if ((map->m_pblk != NEW_ADDR &&
738 blkaddr == (map->m_pblk + ofs)) ||
739 (map->m_pblk == NEW_ADDR &&
740 blkaddr == NEW_ADDR)) {
741 ofs++;
742 dn.ofs_in_node++;
743 pgofs++;
744 map->m_len++;
745 goto get_next;
746 }
747
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900748sync_out:
749 if (allocated)
750 sync_inode_page(&dn);
751put_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900752 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900753unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800754 if (create) {
Chao Yu3104af32015-12-23 17:11:43 +0800755 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800756 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800757 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900758out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700759 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900760 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900761}
762
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700763static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800764 struct buffer_head *bh, int create, int flag)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700765{
766 struct f2fs_map_blocks map;
767 int ret;
768
769 map.m_lblk = iblock;
770 map.m_len = bh->b_size >> inode->i_blkbits;
771
Chao Yue2b4e2b2015-08-19 19:11:19 +0800772 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700773 if (!ret) {
774 map_bh(bh, inode->i_sb, map.m_pblk);
775 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
776 bh->b_size = map.m_len << inode->i_blkbits;
777 }
778 return ret;
779}
780
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900781static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800782 struct buffer_head *bh_result, int create, int flag)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900783{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800784 return __get_data_block(inode, iblock, bh_result, create, flag);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900785}
786
Chao Yue2b4e2b2015-08-19 19:11:19 +0800787static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900788 struct buffer_head *bh_result, int create)
789{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800790 return __get_data_block(inode, iblock, bh_result, create,
791 F2FS_GET_BLOCK_DIO);
792}
793
794static int get_data_block_bmap(struct inode *inode, sector_t iblock,
795 struct buffer_head *bh_result, int create)
796{
Yunlei He179448b2015-12-28 21:48:32 +0800797 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800798 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800799 return -EFBIG;
800
Chao Yue2b4e2b2015-08-19 19:11:19 +0800801 return __get_data_block(inode, iblock, bh_result, create,
802 F2FS_GET_BLOCK_BMAP);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900803}
804
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700805static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
806{
807 return (offset >> inode->i_blkbits);
808}
809
810static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
811{
812 return (blk << inode->i_blkbits);
813}
814
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900815int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
816 u64 start, u64 len)
817{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700818 struct buffer_head map_bh;
819 sector_t start_blk, last_blk;
Fan Lide1475c2016-01-04 15:56:50 +0800820 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700821 u64 logical = 0, phys = 0, size = 0;
822 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700823 int ret = 0;
824
825 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
826 if (ret)
827 return ret;
828
Jaegeuk Kim67f8cf3c2015-10-15 11:34:49 -0700829 if (f2fs_has_inline_data(inode)) {
830 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
831 if (ret != -EAGAIN)
832 return ret;
833 }
834
Al Viro59551022016-01-22 15:40:57 -0500835 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800836
837 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800838 if (start >= isize)
839 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700840
Fan Li9a950d52015-12-26 18:07:41 +0800841 if (start + len > isize)
842 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700843
844 if (logical_to_blk(inode, len) == 0)
845 len = blk_to_logical(inode, 1);
846
847 start_blk = logical_to_blk(inode, start);
848 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800849
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700850next:
851 memset(&map_bh, 0, sizeof(struct buffer_head));
852 map_bh.b_size = len;
853
Chao Yue2b4e2b2015-08-19 19:11:19 +0800854 ret = get_data_block(inode, start_blk, &map_bh, 0,
855 F2FS_GET_BLOCK_FIEMAP);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700856 if (ret)
857 goto out;
858
859 /* HOLE */
860 if (!buffer_mapped(&map_bh)) {
Fan Li9a950d52015-12-26 18:07:41 +0800861 /* Go through holes util pass the EOF */
862 if (blk_to_logical(inode, start_blk++) < isize)
863 goto prep_next;
864 /* Found a hole beyond isize means no more extents.
865 * Note that the premise is that filesystems don't
866 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700867 */
Fan Li9a950d52015-12-26 18:07:41 +0800868 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700869 }
Fan Li9a950d52015-12-26 18:07:41 +0800870
Chao Yuda5af122016-01-08 20:19:27 +0800871 if (size) {
872 if (f2fs_encrypted_inode(inode))
873 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
874
Fan Li9a950d52015-12-26 18:07:41 +0800875 ret = fiemap_fill_next_extent(fieinfo, logical,
876 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800877 }
Fan Li9a950d52015-12-26 18:07:41 +0800878
879 if (start_blk > last_blk || ret)
880 goto out;
881
882 logical = blk_to_logical(inode, start_blk);
883 phys = blk_to_logical(inode, map_bh.b_blocknr);
884 size = map_bh.b_size;
885 flags = 0;
886 if (buffer_unwritten(&map_bh))
887 flags = FIEMAP_EXTENT_UNWRITTEN;
888
889 start_blk += logical_to_blk(inode, size);
890
891prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700892 cond_resched();
893 if (fatal_signal_pending(current))
894 ret = -EINTR;
895 else
896 goto next;
897out:
898 if (ret == 1)
899 ret = 0;
900
Al Viro59551022016-01-22 15:40:57 -0500901 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700902 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900903}
904
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700905/*
906 * This function was originally taken from fs/mpage.c, and customized for f2fs.
907 * Major change was from block_size == page_size in f2fs by default.
908 */
909static int f2fs_mpage_readpages(struct address_space *mapping,
910 struct list_head *pages, struct page *page,
911 unsigned nr_pages)
912{
913 struct bio *bio = NULL;
914 unsigned page_idx;
915 sector_t last_block_in_bio = 0;
916 struct inode *inode = mapping->host;
917 const unsigned blkbits = inode->i_blkbits;
918 const unsigned blocksize = 1 << blkbits;
919 sector_t block_in_file;
920 sector_t last_block;
921 sector_t last_block_in_file;
922 sector_t block_nr;
923 struct block_device *bdev = inode->i_sb->s_bdev;
924 struct f2fs_map_blocks map;
925
926 map.m_pblk = 0;
927 map.m_lblk = 0;
928 map.m_len = 0;
929 map.m_flags = 0;
930
931 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
932
933 prefetchw(&page->flags);
934 if (pages) {
935 page = list_entry(pages->prev, struct page, lru);
936 list_del(&page->lru);
937 if (add_to_page_cache_lru(page, mapping,
938 page->index, GFP_KERNEL))
939 goto next_page;
940 }
941
942 block_in_file = (sector_t)page->index;
943 last_block = block_in_file + nr_pages;
944 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
945 blkbits;
946 if (last_block > last_block_in_file)
947 last_block = last_block_in_file;
948
949 /*
950 * Map blocks using the previous result first.
951 */
952 if ((map.m_flags & F2FS_MAP_MAPPED) &&
953 block_in_file > map.m_lblk &&
954 block_in_file < (map.m_lblk + map.m_len))
955 goto got_it;
956
957 /*
958 * Then do more f2fs_map_blocks() calls until we are
959 * done with this page.
960 */
961 map.m_flags = 0;
962
963 if (block_in_file < last_block) {
964 map.m_lblk = block_in_file;
965 map.m_len = last_block - block_in_file;
966
Chao Yu46c9e142015-09-18 16:54:16 +0800967 if (f2fs_map_blocks(inode, &map, 0,
968 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700969 goto set_error_page;
970 }
971got_it:
972 if ((map.m_flags & F2FS_MAP_MAPPED)) {
973 block_nr = map.m_pblk + block_in_file - map.m_lblk;
974 SetPageMappedToDisk(page);
975
976 if (!PageUptodate(page) && !cleancache_get_page(page)) {
977 SetPageUptodate(page);
978 goto confused;
979 }
980 } else {
981 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
982 SetPageUptodate(page);
983 unlock_page(page);
984 goto next_page;
985 }
986
987 /*
988 * This page will go to BIO. Do we need to send this
989 * BIO off first?
990 */
991 if (bio && (last_block_in_bio != block_nr - 1)) {
992submit_and_realloc:
993 submit_bio(READ, bio);
994 bio = NULL;
995 }
996 if (bio == NULL) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700997 struct f2fs_crypto_ctx *ctx = NULL;
998
999 if (f2fs_encrypted_inode(inode) &&
1000 S_ISREG(inode->i_mode)) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001001
1002 ctx = f2fs_get_crypto_ctx(inode);
1003 if (IS_ERR(ctx))
1004 goto set_error_page;
1005
1006 /* wait the page to be moved by cleaning */
Chao Yu08b39fb2015-10-08 13:27:34 +08001007 f2fs_wait_on_encrypted_page_writeback(
1008 F2FS_I_SB(inode), block_nr);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001009 }
1010
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001011 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +02001012 min_t(int, nr_pages, BIO_MAX_PAGES));
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001013 if (!bio) {
1014 if (ctx)
1015 f2fs_release_crypto_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001016 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001017 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001018 bio->bi_bdev = bdev;
1019 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +08001020 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001021 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001022 }
1023
1024 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1025 goto submit_and_realloc;
1026
1027 last_block_in_bio = block_nr;
1028 goto next_page;
1029set_error_page:
1030 SetPageError(page);
1031 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1032 unlock_page(page);
1033 goto next_page;
1034confused:
1035 if (bio) {
1036 submit_bio(READ, bio);
1037 bio = NULL;
1038 }
1039 unlock_page(page);
1040next_page:
1041 if (pages)
1042 page_cache_release(page);
1043 }
1044 BUG_ON(pages && !list_empty(pages));
1045 if (bio)
1046 submit_bio(READ, bio);
1047 return 0;
1048}
1049
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001050static int f2fs_read_data_page(struct file *file, struct page *page)
1051{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001052 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001053 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001054
Chao Yuc20e89c2014-05-06 16:53:08 +08001055 trace_f2fs_readpage(page, DATA);
1056
arter97e1c42042014-08-06 23:22:50 +09001057 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001058 if (f2fs_has_inline_data(inode))
1059 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001060 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001061 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001062 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001063}
1064
1065static int f2fs_read_data_pages(struct file *file,
1066 struct address_space *mapping,
1067 struct list_head *pages, unsigned nr_pages)
1068{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001069 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001070 struct page *page = list_entry(pages->prev, struct page, lru);
1071
1072 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001073
1074 /* If the file has inline data, skip readpages */
1075 if (f2fs_has_inline_data(inode))
1076 return 0;
1077
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001078 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001079}
1080
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001081int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001082{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001083 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001084 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001085 struct dnode_of_data dn;
1086 int err = 0;
1087
1088 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001089 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001090 if (err)
1091 return err;
1092
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001093 fio->blk_addr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001094
1095 /* This page is already truncated */
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001096 if (fio->blk_addr == NULL_ADDR) {
1097 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001098 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001099 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001100
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001101 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Chao Yu08b39fb2015-10-08 13:27:34 +08001102
1103 /* wait for GCed encrypted page writeback */
1104 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
1105 fio->blk_addr);
1106
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001107 fio->encrypted_page = f2fs_encrypt(inode, fio->page);
1108 if (IS_ERR(fio->encrypted_page)) {
1109 err = PTR_ERR(fio->encrypted_page);
1110 goto out_writepage;
1111 }
1112 }
1113
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001114 set_page_writeback(page);
1115
1116 /*
1117 * If current allocation needs SSR,
1118 * it had better in-place writes for updated data.
1119 */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001120 if (unlikely(fio->blk_addr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001121 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001122 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001123 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001124 rewrite_data_page(fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001125 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001126 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001127 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001128 write_data_page(&dn, fio);
Chao Yu216a6202015-03-19 19:23:32 +08001129 set_data_blkaddr(&dn);
Chao Yu7e4dde72015-02-05 17:51:34 +08001130 f2fs_update_extent_cache(&dn);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001131 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001132 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001133 if (page->index == 0)
1134 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001135 }
1136out_writepage:
1137 f2fs_put_dnode(&dn);
1138 return err;
1139}
1140
1141static int f2fs_write_data_page(struct page *page,
1142 struct writeback_control *wbc)
1143{
1144 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001145 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001146 loff_t i_size = i_size_read(inode);
1147 const pgoff_t end_index = ((unsigned long long) i_size)
1148 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001149 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001150 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001151 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001152 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001153 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001154 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001155 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001156 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001157 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001158 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001159
Chao Yuecda0de2014-05-06 16:48:26 +08001160 trace_f2fs_writepage(page, DATA);
1161
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001162 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001163 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001164
1165 /*
1166 * If the offset is out-of-range of file size,
1167 * this page does not have to be written to disk.
1168 */
1169 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001170 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001171 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001172
1173 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001174write:
Chao Yucaf00472015-01-28 17:48:42 +08001175 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001176 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001177 if (f2fs_is_drop_cache(inode))
1178 goto out;
1179 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1180 available_free_memory(sbi, BASE_CHECK))
1181 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001182
Jaegeuk Kim39936832012-11-22 16:21:29 +09001183 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001184 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001185 if (unlikely(f2fs_cp_error(sbi)))
1186 goto redirty_out;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001187 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001188 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001189 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001190
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001191 /* we should bypass data pages to proceed the kworkder jobs */
1192 if (unlikely(f2fs_cp_error(sbi))) {
1193 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001194 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001195 }
1196
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001197 if (!wbc->for_reclaim)
1198 need_balance_fs = true;
1199 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001200 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001201
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001202 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001203 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001204 if (f2fs_has_inline_data(inode))
1205 err = f2fs_write_inline_data(inode, page);
1206 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001207 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001208 f2fs_unlock_op(sbi);
1209done:
1210 if (err && err != -ENOENT)
1211 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001212
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001213 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001214out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001215 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001216 if (err)
1217 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001218 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001219 f2fs_balance_fs(sbi, need_balance_fs);
Jaegeuk Kim8d4ea292015-12-31 13:08:02 -08001220 if (wbc->for_reclaim || unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +09001221 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001222 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001223 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001224 return 0;
1225
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001226redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001227 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001228 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001229}
1230
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001231static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1232 void *data)
1233{
1234 struct address_space *mapping = data;
1235 int ret = mapping->a_ops->writepage(page, wbc);
1236 mapping_set_error(mapping, ret);
1237 return ret;
1238}
1239
Chao Yu8f46dca2015-07-14 18:56:10 +08001240/*
1241 * This function was copied from write_cche_pages from mm/page-writeback.c.
1242 * The major change is making write step of cold data page separately from
1243 * warm/hot data page.
1244 */
1245static int f2fs_write_cache_pages(struct address_space *mapping,
1246 struct writeback_control *wbc, writepage_t writepage,
1247 void *data)
1248{
1249 int ret = 0;
1250 int done = 0;
1251 struct pagevec pvec;
1252 int nr_pages;
1253 pgoff_t uninitialized_var(writeback_index);
1254 pgoff_t index;
1255 pgoff_t end; /* Inclusive */
1256 pgoff_t done_index;
1257 int cycled;
1258 int range_whole = 0;
1259 int tag;
1260 int step = 0;
1261
1262 pagevec_init(&pvec, 0);
1263next:
1264 if (wbc->range_cyclic) {
1265 writeback_index = mapping->writeback_index; /* prev offset */
1266 index = writeback_index;
1267 if (index == 0)
1268 cycled = 1;
1269 else
1270 cycled = 0;
1271 end = -1;
1272 } else {
1273 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1274 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1275 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1276 range_whole = 1;
1277 cycled = 1; /* ignore range_cyclic tests */
1278 }
1279 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1280 tag = PAGECACHE_TAG_TOWRITE;
1281 else
1282 tag = PAGECACHE_TAG_DIRTY;
1283retry:
1284 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1285 tag_pages_for_writeback(mapping, index, end);
1286 done_index = index;
1287 while (!done && (index <= end)) {
1288 int i;
1289
1290 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1291 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1292 if (nr_pages == 0)
1293 break;
1294
1295 for (i = 0; i < nr_pages; i++) {
1296 struct page *page = pvec.pages[i];
1297
1298 if (page->index > end) {
1299 done = 1;
1300 break;
1301 }
1302
1303 done_index = page->index;
1304
1305 lock_page(page);
1306
1307 if (unlikely(page->mapping != mapping)) {
1308continue_unlock:
1309 unlock_page(page);
1310 continue;
1311 }
1312
1313 if (!PageDirty(page)) {
1314 /* someone wrote it for us */
1315 goto continue_unlock;
1316 }
1317
Tiezhu Yang737f1892015-07-17 12:56:00 +08001318 if (step == is_cold_data(page))
Chao Yu8f46dca2015-07-14 18:56:10 +08001319 goto continue_unlock;
1320
1321 if (PageWriteback(page)) {
1322 if (wbc->sync_mode != WB_SYNC_NONE)
1323 f2fs_wait_on_page_writeback(page, DATA);
1324 else
1325 goto continue_unlock;
1326 }
1327
1328 BUG_ON(PageWriteback(page));
1329 if (!clear_page_dirty_for_io(page))
1330 goto continue_unlock;
1331
1332 ret = (*writepage)(page, wbc, data);
1333 if (unlikely(ret)) {
1334 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1335 unlock_page(page);
1336 ret = 0;
1337 } else {
1338 done_index = page->index + 1;
1339 done = 1;
1340 break;
1341 }
1342 }
1343
1344 if (--wbc->nr_to_write <= 0 &&
1345 wbc->sync_mode == WB_SYNC_NONE) {
1346 done = 1;
1347 break;
1348 }
1349 }
1350 pagevec_release(&pvec);
1351 cond_resched();
1352 }
1353
1354 if (step < 1) {
1355 step++;
1356 goto next;
1357 }
1358
1359 if (!cycled && !done) {
1360 cycled = 1;
1361 index = 0;
1362 end = writeback_index - 1;
1363 goto retry;
1364 }
1365 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1366 mapping->writeback_index = done_index;
1367
1368 return ret;
1369}
1370
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001371static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001372 struct writeback_control *wbc)
1373{
1374 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001375 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001376 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001377 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001378 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001379
Chao Yue5748432014-05-06 16:51:24 +08001380 trace_f2fs_writepages(mapping->host, wbc, DATA);
1381
P J Pcfb185a2013-04-03 11:38:00 +09001382 /* deal with chardevs and other special file */
1383 if (!mapping->a_ops->writepage)
1384 return 0;
1385
Chao Yu6a290542015-07-17 18:02:39 +08001386 /* skip writing if there is no dirty page in this inode */
1387 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1388 return 0;
1389
Jaegeuk Kima1257022015-10-08 10:40:07 -07001390 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1391 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1392 available_free_memory(sbi, DIRTY_DENTS))
1393 goto skip_write;
1394
Chao Yud323d002015-10-27 09:53:45 +08001395 /* skip writing during file defragment */
1396 if (is_inode_flag_set(F2FS_I(inode), FI_DO_DEFRAG))
1397 goto skip_write;
1398
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001399 /* during POR, we don't need to trigger writepage at all. */
1400 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1401 goto skip_write;
1402
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001403 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001404
Jaegeuk Kim25c13552016-01-20 23:46:05 +08001405 if (!S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_ALL) {
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001406 mutex_lock(&sbi->writepages);
1407 locked = true;
1408 }
Chao Yu8f46dca2015-07-14 18:56:10 +08001409 ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Chao Yubb96a8d2015-07-06 20:31:49 +08001410 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001411 if (locked)
1412 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001413
Chao Yuc227f912015-12-16 13:09:20 +08001414 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001415
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001416 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001417 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001418
1419skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001420 wbc->pages_skipped += get_dirty_pages(inode);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001421 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001422}
1423
Chao Yu3aab8f82014-07-02 13:25:04 +08001424static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1425{
1426 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001427 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001428
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001429 if (to > i_size) {
1430 truncate_pagecache(inode, i_size);
1431 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001432 }
1433}
1434
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001435static int prepare_write_begin(struct f2fs_sb_info *sbi,
1436 struct page *page, loff_t pos, unsigned len,
1437 block_t *blk_addr, bool *node_changed)
1438{
1439 struct inode *inode = page->mapping->host;
1440 pgoff_t index = page->index;
1441 struct dnode_of_data dn;
1442 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001443 bool locked = false;
1444 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001445 int err = 0;
1446
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001447 if (f2fs_has_inline_data(inode) ||
1448 (pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1449 f2fs_lock_op(sbi);
1450 locked = true;
1451 }
1452restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001453 /* check inline_data */
1454 ipage = get_node_page(sbi, inode->i_ino);
1455 if (IS_ERR(ipage)) {
1456 err = PTR_ERR(ipage);
1457 goto unlock_out;
1458 }
1459
1460 set_new_dnode(&dn, inode, ipage, ipage, 0);
1461
1462 if (f2fs_has_inline_data(inode)) {
1463 if (pos + len <= MAX_INLINE_DATA) {
1464 read_inline_data(page, ipage);
1465 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001466 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001467 sync_inode_page(&dn);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001468 } else {
1469 err = f2fs_convert_inline_page(&dn, page);
1470 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001471 goto out;
1472 if (dn.data_blkaddr == NULL_ADDR)
1473 err = f2fs_get_block(&dn, index);
1474 }
1475 } else if (locked) {
1476 err = f2fs_get_block(&dn, index);
1477 } else {
1478 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1479 dn.data_blkaddr = ei.blk + index - ei.fofs;
1480 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001481 /* hole case */
1482 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimb483fad2016-01-20 08:27:37 +08001483 if (err || (!err && dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001484 f2fs_put_dnode(&dn);
1485 f2fs_lock_op(sbi);
1486 locked = true;
1487 goto restart;
1488 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001489 }
1490 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001491
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001492 /* convert_inline_page can make node_changed */
1493 *blk_addr = dn.data_blkaddr;
1494 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001495out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001496 f2fs_put_dnode(&dn);
1497unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001498 if (locked)
1499 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001500 return err;
1501}
1502
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001503static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1504 loff_t pos, unsigned len, unsigned flags,
1505 struct page **pagep, void **fsdata)
1506{
1507 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001508 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001509 struct page *page = NULL;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001510 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001511 bool need_balance = false;
1512 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001513 int err = 0;
1514
Chao Yu62aed042014-05-06 16:46:04 +08001515 trace_f2fs_write_begin(inode, pos, len, flags);
1516
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001517 /*
1518 * We should check this at this moment to avoid deadlock on inode page
1519 * and #0 page. The locking rule for inline_data conversion should be:
1520 * lock_page(page #0) -> lock_page(inode_page)
1521 */
1522 if (index != 0) {
1523 err = f2fs_convert_inline_inode(inode);
1524 if (err)
1525 goto fail;
1526 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001527repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001528 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001529 if (!page) {
1530 err = -ENOMEM;
1531 goto fail;
1532 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001533
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001534 *pagep = page;
1535
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001536 err = prepare_write_begin(sbi, page, pos, len,
1537 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001538 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001539 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001540
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001541 if (need_balance && has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001542 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001543 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001544 lock_page(page);
1545 if (page->mapping != mapping) {
1546 /* The page got truncated from under us */
1547 f2fs_put_page(page, 1);
1548 goto repeat;
1549 }
1550 }
1551
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001552 f2fs_wait_on_page_writeback(page, DATA);
1553
Chao Yu08b39fb2015-10-08 13:27:34 +08001554 /* wait for GCed encrypted page writeback */
1555 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001556 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001557
Chao Yu90d43882015-07-08 18:24:38 +08001558 if (len == PAGE_CACHE_SIZE)
1559 goto out_update;
1560 if (PageUptodate(page))
1561 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001562
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001563 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1564 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1565 unsigned end = start + len;
1566
1567 /* Reading beyond i_size is simple: memset to zero */
1568 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001569 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001570 }
1571
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001572 if (blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001573 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1574 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001575 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001576 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001577 .type = DATA,
1578 .rw = READ_SYNC,
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001579 .blk_addr = blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001580 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001581 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001582 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001583 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001584 if (err)
1585 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001586
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001587 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001588 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001589 err = -EIO;
1590 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001591 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001592 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001593 f2fs_put_page(page, 1);
1594 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001595 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001596
1597 /* avoid symlink page */
1598 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1599 err = f2fs_decrypt_one(inode, page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001600 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001601 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001602 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001603 }
Chao Yu90d43882015-07-08 18:24:38 +08001604out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001605 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001606out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001607 clear_cold_data(page);
1608 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001609
Chao Yu3aab8f82014-07-02 13:25:04 +08001610fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001611 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001612 f2fs_write_failed(mapping, pos + len);
1613 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001614}
1615
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001616static int f2fs_write_end(struct file *file,
1617 struct address_space *mapping,
1618 loff_t pos, unsigned len, unsigned copied,
1619 struct page *page, void *fsdata)
1620{
1621 struct inode *inode = page->mapping->host;
1622
Chao Yudfb2bf32014-05-06 16:47:23 +08001623 trace_f2fs_write_end(inode, pos, len, copied);
1624
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001625 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001626
1627 if (pos + copied > i_size_read(inode)) {
1628 i_size_write(inode, pos + copied);
1629 mark_inode_dirty(inode);
1630 update_inode_page(inode);
1631 }
1632
Chao Yu75c3c8b2013-11-16 14:15:59 +08001633 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001634 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001635 return copied;
1636}
1637
Omar Sandoval6f673762015-03-16 04:33:52 -07001638static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1639 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001640{
1641 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001642
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001643 if (offset & blocksize_mask)
1644 return -EINVAL;
1645
Al Viro5b46f252014-03-16 18:07:34 -04001646 if (iov_iter_alignment(iter) & blocksize_mask)
1647 return -EINVAL;
1648
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001649 return 0;
1650}
1651
Omar Sandoval22c61862015-03-16 04:33:53 -07001652static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
1653 loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001654{
1655 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001656 struct address_space *mapping = file->f_mapping;
1657 struct inode *inode = mapping->host;
1658 size_t count = iov_iter_count(iter);
1659 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001660
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001661 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001662 err = f2fs_convert_inline_inode(inode);
1663 if (err)
1664 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001665
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001666 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1667 return 0;
1668
Chao Yuc15e8592015-08-07 18:39:32 +08001669 err = check_direct_IO(inode, iter, offset);
1670 if (err)
1671 return err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001672
Omar Sandoval6f673762015-03-16 04:33:52 -07001673 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001674
Chao Yuf9811702015-09-21 20:17:52 +08001675 if (iov_iter_rw(iter) == WRITE) {
Chao Yu6d5a1492015-12-24 18:04:56 +08001676 err = __allocate_data_blocks(inode, offset, count);
1677 if (err)
Chao Yuf9811702015-09-21 20:17:52 +08001678 goto out;
Chao Yuf9811702015-09-21 20:17:52 +08001679 }
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001680
Chao Yue2b4e2b2015-08-19 19:11:19 +08001681 err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block_dio);
Chao Yuf9811702015-09-21 20:17:52 +08001682out:
Omar Sandoval6f673762015-03-16 04:33:52 -07001683 if (err < 0 && iov_iter_rw(iter) == WRITE)
Chao Yu3aab8f82014-07-02 13:25:04 +08001684 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001685
Omar Sandoval6f673762015-03-16 04:33:52 -07001686 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001687
Chao Yu3aab8f82014-07-02 13:25:04 +08001688 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001689}
1690
Chao Yu487261f2015-02-05 17:44:29 +08001691void f2fs_invalidate_page(struct page *page, unsigned int offset,
1692 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001693{
1694 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001695 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001696
Chao Yu487261f2015-02-05 17:44:29 +08001697 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1698 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001699 return;
1700
Chao Yu487261f2015-02-05 17:44:29 +08001701 if (PageDirty(page)) {
1702 if (inode->i_ino == F2FS_META_INO(sbi))
1703 dec_page_count(sbi, F2FS_DIRTY_META);
1704 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1705 dec_page_count(sbi, F2FS_DIRTY_NODES);
1706 else
1707 inode_dec_dirty_pages(inode);
1708 }
Chao Yudecd36b2015-08-07 18:42:09 +08001709
1710 /* This is atomic written page, keep Private */
1711 if (IS_ATOMIC_WRITTEN_PAGE(page))
1712 return;
1713
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001714 ClearPagePrivate(page);
1715}
1716
Chao Yu487261f2015-02-05 17:44:29 +08001717int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001718{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001719 /* If this is dirty page, keep PagePrivate */
1720 if (PageDirty(page))
1721 return 0;
1722
Chao Yudecd36b2015-08-07 18:42:09 +08001723 /* This is atomic written page, keep Private */
1724 if (IS_ATOMIC_WRITTEN_PAGE(page))
1725 return 0;
1726
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001727 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001728 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001729}
1730
1731static int f2fs_set_data_page_dirty(struct page *page)
1732{
1733 struct address_space *mapping = page->mapping;
1734 struct inode *inode = mapping->host;
1735
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001736 trace_f2fs_set_page_dirty(page, DATA);
1737
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001738 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001739
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001740 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001741 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1742 register_inmem_page(inode, page);
1743 return 1;
1744 }
1745 /*
1746 * Previously, this page has been registered, we just
1747 * return here.
1748 */
1749 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001750 }
1751
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001752 if (!PageDirty(page)) {
1753 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001754 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001755 return 1;
1756 }
1757 return 0;
1758}
1759
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001760static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1761{
Chao Yu454ae7e2014-04-22 13:34:01 +08001762 struct inode *inode = mapping->host;
1763
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001764 if (f2fs_has_inline_data(inode))
1765 return 0;
1766
1767 /* make sure allocating whole blocks */
1768 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1769 filemap_write_and_wait(mapping);
1770
Chao Yue2b4e2b2015-08-19 19:11:19 +08001771 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001772}
1773
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001774const struct address_space_operations f2fs_dblock_aops = {
1775 .readpage = f2fs_read_data_page,
1776 .readpages = f2fs_read_data_pages,
1777 .writepage = f2fs_write_data_page,
1778 .writepages = f2fs_write_data_pages,
1779 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001780 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001781 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001782 .invalidatepage = f2fs_invalidate_page,
1783 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001784 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001785 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001786};