blob: bb60e6afbb722ba09ce3942d9ae1c89578157795 [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 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 Yu0fd785e2016-01-18 18:24:59 +0800117bool is_merged_page(struct f2fs_sb_info *sbi, struct page *page,
118 enum page_type type)
119{
120 enum page_type btype = PAGE_TYPE_OF_BIO(type);
121 struct f2fs_bio_info *io = &sbi->write_io[btype];
122 struct bio_vec *bvec;
123 struct page *target;
124 int i;
125
126 down_read(&io->io_rwsem);
127 if (!io->bio) {
128 up_read(&io->io_rwsem);
129 return false;
130 }
131
132 bio_for_each_segment_all(bvec, io->bio, i) {
133
134 if (bvec->bv_page->mapping) {
135 target = bvec->bv_page;
136 } else {
137 struct f2fs_crypto_ctx *ctx;
138
139 /* encrypted page */
140 ctx = (struct f2fs_crypto_ctx *)page_private(
141 bvec->bv_page);
142 target = ctx->w.control_page;
143 }
144
145 if (page == target) {
146 up_read(&io->io_rwsem);
147 return true;
148 }
149 }
150
151 up_read(&io->io_rwsem);
152 return false;
153}
154
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900155void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900156 enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900157{
158 enum page_type btype = PAGE_TYPE_OF_BIO(type);
159 struct f2fs_bio_info *io;
160
161 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
162
Chao Yudf0f8dc2014-03-22 14:57:23 +0800163 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900164
165 /* change META to META_FLUSH in the checkpoint procedure */
166 if (type >= META_FLUSH) {
167 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700168 if (test_opt(sbi, NOBARRIER))
169 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
170 else
171 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900172 }
173 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800174 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900175}
176
177/*
178 * Fill the locked page with data located in the block address.
179 * Return unlocked page.
180 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700181int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900182{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900183 struct bio *bio;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700184 struct page *page = fio->encrypted_page ? fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900185
Chao Yu2ace38e2014-12-24 16:08:14 +0800186 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700187 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900188
189 /* Allocate a new bio */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700190 bio = __bio_alloc(fio->sbi, fio->blk_addr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900191
192 if (bio_add_page(bio, page, PAGE_CACHE_SIZE, 0) < PAGE_CACHE_SIZE) {
193 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900194 return -EFAULT;
195 }
196
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800197 submit_bio(fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900198 return 0;
199}
200
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700201void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900202{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700203 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900204 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900205 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800206 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700207 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900208
Gu Zheng940a6d32013-12-20 17:39:59 +0800209 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900210
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800211 verify_block_addr(sbi, fio->blk_addr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900212
Chao Yudf0f8dc2014-03-22 14:57:23 +0800213 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900214
Gu Zheng940a6d32013-12-20 17:39:59 +0800215 if (!is_read)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900216 inc_page_count(sbi, F2FS_WRITEBACK);
217
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800218 if (io->bio && (io->last_block_in_bio != fio->blk_addr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900219 io->fio.rw != fio->rw))
220 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900221alloc_new:
222 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700223 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800224
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800225 io->bio = __bio_alloc(sbi, fio->blk_addr, bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900226 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900227 }
228
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700229 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
230
231 if (bio_add_page(io->bio, bio_page, PAGE_CACHE_SIZE, 0) <
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900232 PAGE_CACHE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900233 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900234 goto alloc_new;
235 }
236
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800237 io->last_block_in_bio = fio->blk_addr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700238 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900239
Chao Yudf0f8dc2014-03-22 14:57:23 +0800240 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700241 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900242}
243
244/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900245 * Lock ordering for the change of data block address:
246 * ->data_page
247 * ->node_page
248 * update block addresses in the node page
249 */
Chao Yu216a6202015-03-19 19:23:32 +0800250void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900251{
252 struct f2fs_node *rn;
253 __le32 *addr_array;
254 struct page *node_page = dn->node_page;
255 unsigned int ofs_in_node = dn->ofs_in_node;
256
Jaegeuk Kimfec1d652016-01-20 23:43:51 +0800257 f2fs_wait_on_page_writeback(node_page, NODE, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900258
Gu Zheng45590712013-07-15 17:57:38 +0800259 rn = F2FS_NODE(node_page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900260
261 /* Get physical address of data block */
262 addr_array = blkaddr_in_node(rn);
Jaegeuk Kime1509cf2014-12-30 22:57:55 -0800263 addr_array[ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800264 if (set_page_dirty(node_page))
265 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900266}
267
268int reserve_new_block(struct dnode_of_data *dn)
269{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700270 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900271
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900272 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900273 return -EPERM;
Chao Yucfb271d2013-12-05 17:15:22 +0800274 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900275 return -ENOSPC;
276
Namjae Jeonc01e2852013-04-23 17:00:52 +0900277 trace_f2fs_reserve_new_block(dn->inode, dn->nid, dn->ofs_in_node);
278
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900279 dn->data_blkaddr = NEW_ADDR;
Chao Yu216a6202015-03-19 19:23:32 +0800280 set_data_blkaddr(dn);
Jaegeuk Kima18ff062014-01-21 13:32:12 +0900281 mark_inode_dirty(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900282 sync_inode_page(dn);
283 return 0;
284}
285
Huajun Lib6009652013-11-10 23:13:18 +0800286int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
287{
288 bool need_put = dn->inode_page ? false : true;
289 int err;
290
291 err = get_dnode_of_data(dn, index, ALLOC_NODE);
292 if (err)
293 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900294
Huajun Lib6009652013-11-10 23:13:18 +0800295 if (dn->data_blkaddr == NULL_ADDR)
296 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900297 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800298 f2fs_put_dnode(dn);
299 return err;
300}
301
Fan Li759af1c2015-08-05 15:52:16 +0800302int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900303{
Chao Yu028a41e2015-03-19 19:26:02 +0800304 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800305 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800306
Fan Li759af1c2015-08-05 15:52:16 +0800307 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
308 dn->data_blkaddr = ei.blk + index - ei.fofs;
309 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800310 }
311
Fan Li759af1c2015-08-05 15:52:16 +0800312 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900313}
314
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700315struct page *get_read_data_page(struct inode *inode, pgoff_t index,
316 int rw, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900317{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900318 struct address_space *mapping = inode->i_mapping;
319 struct dnode_of_data dn;
320 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800321 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900322 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800323 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700324 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800325 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700326 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700327 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800328 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900329
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700330 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
331 return read_mapping_page(mapping, index, NULL);
332
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700333 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900334 if (!page)
335 return ERR_PTR(-ENOMEM);
336
Chao Yucb3bc9e2015-02-05 18:03:40 +0800337 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
338 dn.data_blkaddr = ei.blk + index - ei.fofs;
339 goto got_it;
340 }
341
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900342 set_new_dnode(&dn, inode, NULL, NULL, 0);
343 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700344 if (err)
345 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900346 f2fs_put_dnode(&dn);
347
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900348 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700349 err = -ENOENT;
350 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900351 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800352got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700353 if (PageUptodate(page)) {
354 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900355 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700356 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900357
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900358 /*
359 * A new dentry page is allocated but not able to be written, since its
360 * new inode page couldn't be allocated due to -ENOSPC.
361 * In such the case, its blkaddr can be remained as NEW_ADDR.
362 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
363 */
364 if (dn.data_blkaddr == NEW_ADDR) {
365 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
366 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700367 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900368 return page;
369 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900370
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800371 fio.blk_addr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700372 fio.page = page;
373 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900374 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700375 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700376 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700377
378put_err:
379 f2fs_put_page(page, 1);
380 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700381}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900382
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700383struct page *find_data_page(struct inode *inode, pgoff_t index)
384{
385 struct address_space *mapping = inode->i_mapping;
386 struct page *page;
387
388 page = find_get_page(mapping, index);
389 if (page && PageUptodate(page))
390 return page;
391 f2fs_put_page(page, 0);
392
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700393 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700394 if (IS_ERR(page))
395 return page;
396
397 if (PageUptodate(page))
398 return page;
399
400 wait_on_page_locked(page);
401 if (unlikely(!PageUptodate(page))) {
402 f2fs_put_page(page, 0);
403 return ERR_PTR(-EIO);
404 }
405 return page;
406}
407
408/*
409 * If it tries to access a hole, return an error.
410 * Because, the callers, functions in dir.c and GC, should be able to know
411 * whether this page exists or not.
412 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700413struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
414 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700415{
416 struct address_space *mapping = inode->i_mapping;
417 struct page *page;
418repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700419 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700420 if (IS_ERR(page))
421 return page;
422
423 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900424 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900425 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900426 f2fs_put_page(page, 1);
427 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900428 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900429 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900430 f2fs_put_page(page, 1);
431 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900432 }
433 return page;
434}
435
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900436/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900437 * Caller ensures that this data page is never allocated.
438 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900439 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800440 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
441 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800442 * Note that, ipage is set only by make_empty_dir, and if any error occur,
443 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900444 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900445struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900446 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900447{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900448 struct address_space *mapping = inode->i_mapping;
449 struct page *page;
450 struct dnode_of_data dn;
451 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800452
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700453 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800454 if (!page) {
455 /*
456 * before exiting, we should make sure ipage will be released
457 * if any error occur.
458 */
459 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700460 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800461 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900462
Jaegeuk Kima8865372013-12-27 17:04:17 +0900463 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800464 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700465 if (err) {
466 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900467 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900468 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700469 if (!ipage)
470 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900471
472 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700473 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900474
475 if (dn.data_blkaddr == NEW_ADDR) {
476 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900477 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900478 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700479 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900480
Jaegeuk Kim76121182016-01-01 22:03:47 -0800481 /* if ipage exists, blkaddr should be NEW_ADDR */
482 f2fs_bug_on(F2FS_I_SB(inode), ipage);
483 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700484 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800485 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900486 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700487got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800488 if (new_i_size && i_size_read(inode) <
489 ((loff_t)(index + 1) << PAGE_CACHE_SHIFT)) {
490 i_size_write(inode, ((loff_t)(index + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kim699489b2013-06-07 22:08:23 +0900491 /* Only the directory inode sets new_i_size */
492 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900493 }
494 return page;
495}
496
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900497static int __allocate_data_block(struct dnode_of_data *dn)
498{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700499 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900500 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900501 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800502 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700503 pgoff_t fofs;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900504
505 if (unlikely(is_inode_flag_set(F2FS_I(dn->inode), FI_NO_ALLOC)))
506 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800507
508 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
509 if (dn->data_blkaddr == NEW_ADDR)
510 goto alloc;
511
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900512 if (unlikely(!inc_valid_block_count(sbi, dn->inode, 1)))
513 return -ENOSPC;
514
Chao Yudf6136e2015-03-23 10:33:37 +0800515alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900516 get_node_info(sbi, dn->nid, &ni);
517 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
518
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800519 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
520 seg = CURSEG_DIRECT_IO;
521
Chao Yudf6136e2015-03-23 10:33:37 +0800522 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
523 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800524 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900525
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700526 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800527 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700528 dn->ofs_in_node;
Chao Yu9edcdab2015-09-11 14:43:52 +0800529 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT))
530 i_size_write(dn->inode,
531 ((loff_t)(fofs + 1) << PAGE_CACHE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900532 return 0;
533}
534
Chao Yu6d5a1492015-12-24 18:04:56 +0800535static int __allocate_data_blocks(struct inode *inode, loff_t offset,
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800536 size_t count)
537{
Chao Yu5b8db7f2016-01-26 15:38:29 +0800538 struct f2fs_map_blocks map;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800539
Chao Yu5b8db7f2016-01-26 15:38:29 +0800540 map.m_lblk = F2FS_BYTES_TO_BLK(offset);
541 map.m_len = F2FS_BYTES_TO_BLK(count);
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800542
Chao Yu5b8db7f2016-01-26 15:38:29 +0800543 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_DIO);
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800544}
545
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900546/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700547 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
548 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800549 * If original data blocks are allocated, then give them to blockdev.
550 * Otherwise,
551 * a. preallocate requested block addresses
552 * b. do not use extent cache for better performance
553 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900554 */
Chao Yud323d002015-10-27 09:53:45 +0800555int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800556 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900557{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700558 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900559 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800560 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900561 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
562 pgoff_t pgofs, end_offset;
563 int err = 0, ofs = 1;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800564 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900565 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800566 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900567
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700568 map->m_len = 0;
569 map->m_flags = 0;
570
571 /* it only supports block size == page size */
572 pgofs = (pgoff_t)map->m_lblk;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900573
Chao Yu7e4dde72015-02-05 17:51:34 +0800574 if (f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700575 map->m_pblk = ei.blk + pgofs - ei.fofs;
576 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
577 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900578 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800579 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900580
Chao Yu4fe71e82016-01-26 15:37:38 +0800581next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800582 if (create)
Chao Yu3104af352015-12-23 17:11:43 +0800583 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900584
585 /* When reading holes, we need its node page */
586 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900587 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900588 if (err) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900589 if (err == -ENOENT)
590 err = 0;
591 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900592 }
Chao Yu973163f2015-09-18 16:51:51 +0800593
Chao Yu81ca7352016-01-26 15:39:35 +0800594 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900595
Chao Yu4fe71e82016-01-26 15:37:38 +0800596next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800597 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800598
Fan Li7df3a432015-12-17 13:20:59 +0800599 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
600 if (create) {
601 if (unlikely(f2fs_cp_error(sbi))) {
602 err = -EIO;
603 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800604 }
Fan Li7df3a432015-12-17 13:20:59 +0800605 err = __allocate_data_block(&dn);
606 if (err)
607 goto sync_out;
608 allocated = true;
Chao Yu4fe71e82016-01-26 15:37:38 +0800609 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800610 blkaddr = dn.data_blkaddr;
611 } else {
Fan Li7df3a432015-12-17 13:20:59 +0800612 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu4fe71e82016-01-26 15:37:38 +0800613 blkaddr != NEW_ADDR) {
614 if (flag == F2FS_GET_BLOCK_BMAP)
615 err = -ENOENT;
Fan Li7df3a432015-12-17 13:20:59 +0800616 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800617 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900618 }
619 }
Fan Li7df3a432015-12-17 13:20:59 +0800620
Chao Yu4fe71e82016-01-26 15:37:38 +0800621 if (map->m_len == 0) {
622 /* preallocated unwritten block should be mapped for fiemap. */
623 if (blkaddr == NEW_ADDR)
624 map->m_flags |= F2FS_MAP_UNWRITTEN;
625 map->m_flags |= F2FS_MAP_MAPPED;
626
627 map->m_pblk = blkaddr;
628 map->m_len = 1;
629 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800630 blkaddr == (map->m_pblk + ofs)) ||
Chao Yu4fe71e82016-01-26 15:37:38 +0800631 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR)) {
Fan Li7df3a432015-12-17 13:20:59 +0800632 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800633 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800634 } else {
635 goto sync_out;
636 }
637
638 dn.ofs_in_node++;
639 pgofs++;
640
641 if (map->m_len < maxblocks) {
642 if (dn.ofs_in_node < end_offset)
643 goto next_block;
644
645 if (allocated)
646 sync_inode_page(&dn);
647 f2fs_put_dnode(&dn);
648
649 if (create) {
650 f2fs_unlock_op(sbi);
651 f2fs_balance_fs(sbi, allocated);
652 }
653 allocated = false;
654 goto next_dnode;
Fan Li7df3a432015-12-17 13:20:59 +0800655 }
656
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900657sync_out:
658 if (allocated)
659 sync_inode_page(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900660 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900661unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800662 if (create) {
Chao Yu3104af352015-12-23 17:11:43 +0800663 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800664 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800665 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900666out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700667 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900668 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900669}
670
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700671static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800672 struct buffer_head *bh, int create, int flag)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700673{
674 struct f2fs_map_blocks map;
675 int ret;
676
677 map.m_lblk = iblock;
678 map.m_len = bh->b_size >> inode->i_blkbits;
679
Chao Yue2b4e2b2015-08-19 19:11:19 +0800680 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700681 if (!ret) {
682 map_bh(bh, inode->i_sb, map.m_pblk);
683 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
684 bh->b_size = map.m_len << inode->i_blkbits;
685 }
686 return ret;
687}
688
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900689static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800690 struct buffer_head *bh_result, int create, int flag)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900691{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800692 return __get_data_block(inode, iblock, bh_result, create, flag);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900693}
694
Chao Yue2b4e2b2015-08-19 19:11:19 +0800695static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900696 struct buffer_head *bh_result, int create)
697{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800698 return __get_data_block(inode, iblock, bh_result, create,
699 F2FS_GET_BLOCK_DIO);
700}
701
702static int get_data_block_bmap(struct inode *inode, sector_t iblock,
703 struct buffer_head *bh_result, int create)
704{
Yunlei He179448b2015-12-28 21:48:32 +0800705 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800706 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800707 return -EFBIG;
708
Chao Yue2b4e2b2015-08-19 19:11:19 +0800709 return __get_data_block(inode, iblock, bh_result, create,
710 F2FS_GET_BLOCK_BMAP);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900711}
712
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700713static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
714{
715 return (offset >> inode->i_blkbits);
716}
717
718static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
719{
720 return (blk << inode->i_blkbits);
721}
722
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900723int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
724 u64 start, u64 len)
725{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700726 struct buffer_head map_bh;
727 sector_t start_blk, last_blk;
Fan Lide1475c2016-01-04 15:56:50 +0800728 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700729 u64 logical = 0, phys = 0, size = 0;
730 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700731 int ret = 0;
732
733 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
734 if (ret)
735 return ret;
736
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700737 if (f2fs_has_inline_data(inode)) {
738 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
739 if (ret != -EAGAIN)
740 return ret;
741 }
742
Al Viro59551022016-01-22 15:40:57 -0500743 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800744
745 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800746 if (start >= isize)
747 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700748
Fan Li9a950d52015-12-26 18:07:41 +0800749 if (start + len > isize)
750 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700751
752 if (logical_to_blk(inode, len) == 0)
753 len = blk_to_logical(inode, 1);
754
755 start_blk = logical_to_blk(inode, start);
756 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800757
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700758next:
759 memset(&map_bh, 0, sizeof(struct buffer_head));
760 map_bh.b_size = len;
761
Chao Yue2b4e2b2015-08-19 19:11:19 +0800762 ret = get_data_block(inode, start_blk, &map_bh, 0,
763 F2FS_GET_BLOCK_FIEMAP);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700764 if (ret)
765 goto out;
766
767 /* HOLE */
768 if (!buffer_mapped(&map_bh)) {
Fan Li9a950d52015-12-26 18:07:41 +0800769 /* Go through holes util pass the EOF */
770 if (blk_to_logical(inode, start_blk++) < isize)
771 goto prep_next;
772 /* Found a hole beyond isize means no more extents.
773 * Note that the premise is that filesystems don't
774 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700775 */
Fan Li9a950d52015-12-26 18:07:41 +0800776 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700777 }
Fan Li9a950d52015-12-26 18:07:41 +0800778
Chao Yuda5af122016-01-08 20:19:27 +0800779 if (size) {
780 if (f2fs_encrypted_inode(inode))
781 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
782
Fan Li9a950d52015-12-26 18:07:41 +0800783 ret = fiemap_fill_next_extent(fieinfo, logical,
784 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800785 }
Fan Li9a950d52015-12-26 18:07:41 +0800786
787 if (start_blk > last_blk || ret)
788 goto out;
789
790 logical = blk_to_logical(inode, start_blk);
791 phys = blk_to_logical(inode, map_bh.b_blocknr);
792 size = map_bh.b_size;
793 flags = 0;
794 if (buffer_unwritten(&map_bh))
795 flags = FIEMAP_EXTENT_UNWRITTEN;
796
797 start_blk += logical_to_blk(inode, size);
798
799prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700800 cond_resched();
801 if (fatal_signal_pending(current))
802 ret = -EINTR;
803 else
804 goto next;
805out:
806 if (ret == 1)
807 ret = 0;
808
Al Viro59551022016-01-22 15:40:57 -0500809 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700810 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900811}
812
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700813/*
814 * This function was originally taken from fs/mpage.c, and customized for f2fs.
815 * Major change was from block_size == page_size in f2fs by default.
816 */
817static int f2fs_mpage_readpages(struct address_space *mapping,
818 struct list_head *pages, struct page *page,
819 unsigned nr_pages)
820{
821 struct bio *bio = NULL;
822 unsigned page_idx;
823 sector_t last_block_in_bio = 0;
824 struct inode *inode = mapping->host;
825 const unsigned blkbits = inode->i_blkbits;
826 const unsigned blocksize = 1 << blkbits;
827 sector_t block_in_file;
828 sector_t last_block;
829 sector_t last_block_in_file;
830 sector_t block_nr;
831 struct block_device *bdev = inode->i_sb->s_bdev;
832 struct f2fs_map_blocks map;
833
834 map.m_pblk = 0;
835 map.m_lblk = 0;
836 map.m_len = 0;
837 map.m_flags = 0;
838
839 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
840
841 prefetchw(&page->flags);
842 if (pages) {
843 page = list_entry(pages->prev, struct page, lru);
844 list_del(&page->lru);
845 if (add_to_page_cache_lru(page, mapping,
846 page->index, GFP_KERNEL))
847 goto next_page;
848 }
849
850 block_in_file = (sector_t)page->index;
851 last_block = block_in_file + nr_pages;
852 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
853 blkbits;
854 if (last_block > last_block_in_file)
855 last_block = last_block_in_file;
856
857 /*
858 * Map blocks using the previous result first.
859 */
860 if ((map.m_flags & F2FS_MAP_MAPPED) &&
861 block_in_file > map.m_lblk &&
862 block_in_file < (map.m_lblk + map.m_len))
863 goto got_it;
864
865 /*
866 * Then do more f2fs_map_blocks() calls until we are
867 * done with this page.
868 */
869 map.m_flags = 0;
870
871 if (block_in_file < last_block) {
872 map.m_lblk = block_in_file;
873 map.m_len = last_block - block_in_file;
874
Chao Yu46c9e142015-09-18 16:54:16 +0800875 if (f2fs_map_blocks(inode, &map, 0,
876 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700877 goto set_error_page;
878 }
879got_it:
880 if ((map.m_flags & F2FS_MAP_MAPPED)) {
881 block_nr = map.m_pblk + block_in_file - map.m_lblk;
882 SetPageMappedToDisk(page);
883
884 if (!PageUptodate(page) && !cleancache_get_page(page)) {
885 SetPageUptodate(page);
886 goto confused;
887 }
888 } else {
889 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
890 SetPageUptodate(page);
891 unlock_page(page);
892 goto next_page;
893 }
894
895 /*
896 * This page will go to BIO. Do we need to send this
897 * BIO off first?
898 */
899 if (bio && (last_block_in_bio != block_nr - 1)) {
900submit_and_realloc:
901 submit_bio(READ, bio);
902 bio = NULL;
903 }
904 if (bio == NULL) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700905 struct f2fs_crypto_ctx *ctx = NULL;
906
907 if (f2fs_encrypted_inode(inode) &&
908 S_ISREG(inode->i_mode)) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700909
910 ctx = f2fs_get_crypto_ctx(inode);
911 if (IS_ERR(ctx))
912 goto set_error_page;
913
914 /* wait the page to be moved by cleaning */
Chao Yu08b39fb2015-10-08 13:27:34 +0800915 f2fs_wait_on_encrypted_page_writeback(
916 F2FS_I_SB(inode), block_nr);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700917 }
918
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700919 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200920 min_t(int, nr_pages, BIO_MAX_PAGES));
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700921 if (!bio) {
922 if (ctx)
923 f2fs_release_crypto_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700924 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700925 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700926 bio->bi_bdev = bdev;
927 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +0800928 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700929 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700930 }
931
932 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
933 goto submit_and_realloc;
934
935 last_block_in_bio = block_nr;
936 goto next_page;
937set_error_page:
938 SetPageError(page);
939 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
940 unlock_page(page);
941 goto next_page;
942confused:
943 if (bio) {
944 submit_bio(READ, bio);
945 bio = NULL;
946 }
947 unlock_page(page);
948next_page:
949 if (pages)
950 page_cache_release(page);
951 }
952 BUG_ON(pages && !list_empty(pages));
953 if (bio)
954 submit_bio(READ, bio);
955 return 0;
956}
957
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900958static int f2fs_read_data_page(struct file *file, struct page *page)
959{
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800960 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700961 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800962
Chao Yuc20e89c2014-05-06 16:53:08 +0800963 trace_f2fs_readpage(page, DATA);
964
arter97e1c42042014-08-06 23:22:50 +0900965 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800966 if (f2fs_has_inline_data(inode))
967 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -0700968 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700969 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800970 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900971}
972
973static int f2fs_read_data_pages(struct file *file,
974 struct address_space *mapping,
975 struct list_head *pages, unsigned nr_pages)
976{
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800977 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +0800978 struct page *page = list_entry(pages->prev, struct page, lru);
979
980 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +0800981
982 /* If the file has inline data, skip readpages */
983 if (f2fs_has_inline_data(inode))
984 return 0;
985
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700986 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900987}
988
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700989int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900990{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700991 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900992 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900993 struct dnode_of_data dn;
994 int err = 0;
995
996 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +0900997 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900998 if (err)
999 return err;
1000
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001001 fio->blk_addr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001002
1003 /* This page is already truncated */
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001004 if (fio->blk_addr == NULL_ADDR) {
1005 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001006 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001007 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001008
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001009 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Chao Yu08b39fb2015-10-08 13:27:34 +08001010
1011 /* wait for GCed encrypted page writeback */
1012 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
1013 fio->blk_addr);
1014
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001015 fio->encrypted_page = f2fs_encrypt(inode, fio->page);
1016 if (IS_ERR(fio->encrypted_page)) {
1017 err = PTR_ERR(fio->encrypted_page);
1018 goto out_writepage;
1019 }
1020 }
1021
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001022 set_page_writeback(page);
1023
1024 /*
1025 * If current allocation needs SSR,
1026 * it had better in-place writes for updated data.
1027 */
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001028 if (unlikely(fio->blk_addr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001029 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001030 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001031 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001032 rewrite_data_page(fio);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001033 set_inode_flag(F2FS_I(inode), FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001034 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001035 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001036 write_data_page(&dn, fio);
Chao Yu216a6202015-03-19 19:23:32 +08001037 set_data_blkaddr(&dn);
Chao Yu7e4dde72015-02-05 17:51:34 +08001038 f2fs_update_extent_cache(&dn);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001039 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kimfff04f92014-07-25 07:40:59 -07001040 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001041 if (page->index == 0)
1042 set_inode_flag(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001043 }
1044out_writepage:
1045 f2fs_put_dnode(&dn);
1046 return err;
1047}
1048
1049static int f2fs_write_data_page(struct page *page,
1050 struct writeback_control *wbc)
1051{
1052 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001053 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001054 loff_t i_size = i_size_read(inode);
1055 const pgoff_t end_index = ((unsigned long long) i_size)
1056 >> PAGE_CACHE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001057 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001058 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001059 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001060 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001061 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001062 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001063 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001064 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001065 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001066 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001067
Chao Yuecda0de2014-05-06 16:48:26 +08001068 trace_f2fs_writepage(page, DATA);
1069
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001070 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001071 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001072
1073 /*
1074 * If the offset is out-of-range of file size,
1075 * this page does not have to be written to disk.
1076 */
1077 offset = i_size & (PAGE_CACHE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001078 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001079 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001080
1081 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001082write:
Chao Yucaf00472015-01-28 17:48:42 +08001083 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001084 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001085 if (f2fs_is_drop_cache(inode))
1086 goto out;
1087 if (f2fs_is_volatile_file(inode) && !wbc->for_reclaim &&
1088 available_free_memory(sbi, BASE_CHECK))
1089 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001090
Jaegeuk Kim39936832012-11-22 16:21:29 +09001091 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001092 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001093 if (unlikely(f2fs_cp_error(sbi)))
1094 goto redirty_out;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001095 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001096 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001097 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001098
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001099 /* we should bypass data pages to proceed the kworkder jobs */
1100 if (unlikely(f2fs_cp_error(sbi))) {
1101 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001102 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001103 }
1104
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001105 if (!wbc->for_reclaim)
1106 need_balance_fs = true;
1107 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001108 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001109
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001110 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001111 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001112 if (f2fs_has_inline_data(inode))
1113 err = f2fs_write_inline_data(inode, page);
1114 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001115 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001116 f2fs_unlock_op(sbi);
1117done:
1118 if (err && err != -ENOENT)
1119 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001120
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001121 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001122out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001123 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001124 if (err)
1125 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001126 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001127 f2fs_balance_fs(sbi, need_balance_fs);
Jaegeuk Kim8d4ea292015-12-31 13:08:02 -08001128 if (wbc->for_reclaim || unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim2aea39e2014-04-24 09:49:52 +09001129 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001130 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001131 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001132 return 0;
1133
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001134redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001135 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001136 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001137}
1138
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001139static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1140 void *data)
1141{
1142 struct address_space *mapping = data;
1143 int ret = mapping->a_ops->writepage(page, wbc);
1144 mapping_set_error(mapping, ret);
1145 return ret;
1146}
1147
Chao Yu8f46dca2015-07-14 18:56:10 +08001148/*
1149 * This function was copied from write_cche_pages from mm/page-writeback.c.
1150 * The major change is making write step of cold data page separately from
1151 * warm/hot data page.
1152 */
1153static int f2fs_write_cache_pages(struct address_space *mapping,
1154 struct writeback_control *wbc, writepage_t writepage,
1155 void *data)
1156{
1157 int ret = 0;
1158 int done = 0;
1159 struct pagevec pvec;
1160 int nr_pages;
1161 pgoff_t uninitialized_var(writeback_index);
1162 pgoff_t index;
1163 pgoff_t end; /* Inclusive */
1164 pgoff_t done_index;
1165 int cycled;
1166 int range_whole = 0;
1167 int tag;
1168 int step = 0;
1169
1170 pagevec_init(&pvec, 0);
1171next:
1172 if (wbc->range_cyclic) {
1173 writeback_index = mapping->writeback_index; /* prev offset */
1174 index = writeback_index;
1175 if (index == 0)
1176 cycled = 1;
1177 else
1178 cycled = 0;
1179 end = -1;
1180 } else {
1181 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1182 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1183 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1184 range_whole = 1;
1185 cycled = 1; /* ignore range_cyclic tests */
1186 }
1187 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1188 tag = PAGECACHE_TAG_TOWRITE;
1189 else
1190 tag = PAGECACHE_TAG_DIRTY;
1191retry:
1192 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1193 tag_pages_for_writeback(mapping, index, end);
1194 done_index = index;
1195 while (!done && (index <= end)) {
1196 int i;
1197
1198 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1199 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1200 if (nr_pages == 0)
1201 break;
1202
1203 for (i = 0; i < nr_pages; i++) {
1204 struct page *page = pvec.pages[i];
1205
1206 if (page->index > end) {
1207 done = 1;
1208 break;
1209 }
1210
1211 done_index = page->index;
1212
1213 lock_page(page);
1214
1215 if (unlikely(page->mapping != mapping)) {
1216continue_unlock:
1217 unlock_page(page);
1218 continue;
1219 }
1220
1221 if (!PageDirty(page)) {
1222 /* someone wrote it for us */
1223 goto continue_unlock;
1224 }
1225
Tiezhu Yang737f1892015-07-17 12:56:00 +08001226 if (step == is_cold_data(page))
Chao Yu8f46dca2015-07-14 18:56:10 +08001227 goto continue_unlock;
1228
1229 if (PageWriteback(page)) {
1230 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001231 f2fs_wait_on_page_writeback(page,
1232 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001233 else
1234 goto continue_unlock;
1235 }
1236
1237 BUG_ON(PageWriteback(page));
1238 if (!clear_page_dirty_for_io(page))
1239 goto continue_unlock;
1240
1241 ret = (*writepage)(page, wbc, data);
1242 if (unlikely(ret)) {
1243 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1244 unlock_page(page);
1245 ret = 0;
1246 } else {
1247 done_index = page->index + 1;
1248 done = 1;
1249 break;
1250 }
1251 }
1252
1253 if (--wbc->nr_to_write <= 0 &&
1254 wbc->sync_mode == WB_SYNC_NONE) {
1255 done = 1;
1256 break;
1257 }
1258 }
1259 pagevec_release(&pvec);
1260 cond_resched();
1261 }
1262
1263 if (step < 1) {
1264 step++;
1265 goto next;
1266 }
1267
1268 if (!cycled && !done) {
1269 cycled = 1;
1270 index = 0;
1271 end = writeback_index - 1;
1272 goto retry;
1273 }
1274 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1275 mapping->writeback_index = done_index;
1276
1277 return ret;
1278}
1279
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001280static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001281 struct writeback_control *wbc)
1282{
1283 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001284 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001285 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001286 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001287 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001288
Chao Yue5748432014-05-06 16:51:24 +08001289 trace_f2fs_writepages(mapping->host, wbc, DATA);
1290
P J Pcfb185a2013-04-03 11:38:00 +09001291 /* deal with chardevs and other special file */
1292 if (!mapping->a_ops->writepage)
1293 return 0;
1294
Chao Yu6a290542015-07-17 18:02:39 +08001295 /* skip writing if there is no dirty page in this inode */
1296 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1297 return 0;
1298
Jaegeuk Kima1257022015-10-08 10:40:07 -07001299 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1300 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1301 available_free_memory(sbi, DIRTY_DENTS))
1302 goto skip_write;
1303
Chao Yud323d002015-10-27 09:53:45 +08001304 /* skip writing during file defragment */
1305 if (is_inode_flag_set(F2FS_I(inode), FI_DO_DEFRAG))
1306 goto skip_write;
1307
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001308 /* during POR, we don't need to trigger writepage at all. */
1309 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1310 goto skip_write;
1311
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001312 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001313
Jaegeuk Kim25c13552016-01-20 23:46:05 +08001314 if (!S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_ALL) {
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001315 mutex_lock(&sbi->writepages);
1316 locked = true;
1317 }
Chao Yu8f46dca2015-07-14 18:56:10 +08001318 ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Chao Yubb96a8d2015-07-06 20:31:49 +08001319 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001320 if (locked)
1321 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001322
Chao Yuc227f912015-12-16 13:09:20 +08001323 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001324
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001325 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001326 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001327
1328skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001329 wbc->pages_skipped += get_dirty_pages(inode);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001330 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001331}
1332
Chao Yu3aab8f82014-07-02 13:25:04 +08001333static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1334{
1335 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001336 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001337
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001338 if (to > i_size) {
1339 truncate_pagecache(inode, i_size);
1340 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001341 }
1342}
1343
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001344static int prepare_write_begin(struct f2fs_sb_info *sbi,
1345 struct page *page, loff_t pos, unsigned len,
1346 block_t *blk_addr, bool *node_changed)
1347{
1348 struct inode *inode = page->mapping->host;
1349 pgoff_t index = page->index;
1350 struct dnode_of_data dn;
1351 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001352 bool locked = false;
1353 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001354 int err = 0;
1355
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001356 if (f2fs_has_inline_data(inode) ||
1357 (pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1358 f2fs_lock_op(sbi);
1359 locked = true;
1360 }
1361restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001362 /* check inline_data */
1363 ipage = get_node_page(sbi, inode->i_ino);
1364 if (IS_ERR(ipage)) {
1365 err = PTR_ERR(ipage);
1366 goto unlock_out;
1367 }
1368
1369 set_new_dnode(&dn, inode, ipage, ipage, 0);
1370
1371 if (f2fs_has_inline_data(inode)) {
1372 if (pos + len <= MAX_INLINE_DATA) {
1373 read_inline_data(page, ipage);
1374 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
Jaegeuk Kim2049d4f2016-01-25 05:57:05 -08001375 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001376 } else {
1377 err = f2fs_convert_inline_page(&dn, page);
1378 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001379 goto out;
1380 if (dn.data_blkaddr == NULL_ADDR)
1381 err = f2fs_get_block(&dn, index);
1382 }
1383 } else if (locked) {
1384 err = f2fs_get_block(&dn, index);
1385 } else {
1386 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1387 dn.data_blkaddr = ei.blk + index - ei.fofs;
1388 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001389 /* hole case */
1390 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kimb483fad2016-01-20 08:27:37 +08001391 if (err || (!err && dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001392 f2fs_put_dnode(&dn);
1393 f2fs_lock_op(sbi);
1394 locked = true;
1395 goto restart;
1396 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001397 }
1398 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001399
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001400 /* convert_inline_page can make node_changed */
1401 *blk_addr = dn.data_blkaddr;
1402 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001403out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001404 f2fs_put_dnode(&dn);
1405unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001406 if (locked)
1407 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001408 return err;
1409}
1410
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001411static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1412 loff_t pos, unsigned len, unsigned flags,
1413 struct page **pagep, void **fsdata)
1414{
1415 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001416 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001417 struct page *page = NULL;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001418 pgoff_t index = ((unsigned long long) pos) >> PAGE_CACHE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001419 bool need_balance = false;
1420 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001421 int err = 0;
1422
Chao Yu62aed042014-05-06 16:46:04 +08001423 trace_f2fs_write_begin(inode, pos, len, flags);
1424
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001425 /*
1426 * We should check this at this moment to avoid deadlock on inode page
1427 * and #0 page. The locking rule for inline_data conversion should be:
1428 * lock_page(page #0) -> lock_page(inode_page)
1429 */
1430 if (index != 0) {
1431 err = f2fs_convert_inline_inode(inode);
1432 if (err)
1433 goto fail;
1434 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001435repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001436 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001437 if (!page) {
1438 err = -ENOMEM;
1439 goto fail;
1440 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001441
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001442 *pagep = page;
1443
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001444 err = prepare_write_begin(sbi, page, pos, len,
1445 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001446 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001447 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001448
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001449 if (need_balance && has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001450 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001451 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001452 lock_page(page);
1453 if (page->mapping != mapping) {
1454 /* The page got truncated from under us */
1455 f2fs_put_page(page, 1);
1456 goto repeat;
1457 }
1458 }
1459
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001460 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001461
Chao Yu08b39fb2015-10-08 13:27:34 +08001462 /* wait for GCed encrypted page writeback */
1463 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001464 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001465
Chao Yu90d43882015-07-08 18:24:38 +08001466 if (len == PAGE_CACHE_SIZE)
1467 goto out_update;
1468 if (PageUptodate(page))
1469 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001470
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001471 if ((pos & PAGE_CACHE_MASK) >= i_size_read(inode)) {
1472 unsigned start = pos & (PAGE_CACHE_SIZE - 1);
1473 unsigned end = start + len;
1474
1475 /* Reading beyond i_size is simple: memset to zero */
1476 zero_user_segments(page, 0, start, end, PAGE_CACHE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001477 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001478 }
1479
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001480 if (blkaddr == NEW_ADDR) {
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001481 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
1482 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001483 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001484 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001485 .type = DATA,
1486 .rw = READ_SYNC,
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001487 .blk_addr = blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001488 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001489 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001490 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001491 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001492 if (err)
1493 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001494
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001495 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001496 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001497 err = -EIO;
1498 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001499 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001500 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001501 f2fs_put_page(page, 1);
1502 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001503 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001504
1505 /* avoid symlink page */
1506 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
1507 err = f2fs_decrypt_one(inode, page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001508 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001509 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001510 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001511 }
Chao Yu90d43882015-07-08 18:24:38 +08001512out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001513 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001514out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001515 clear_cold_data(page);
1516 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001517
Chao Yu3aab8f82014-07-02 13:25:04 +08001518fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001519 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001520 f2fs_write_failed(mapping, pos + len);
1521 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001522}
1523
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001524static int f2fs_write_end(struct file *file,
1525 struct address_space *mapping,
1526 loff_t pos, unsigned len, unsigned copied,
1527 struct page *page, void *fsdata)
1528{
1529 struct inode *inode = page->mapping->host;
1530
Chao Yudfb2bf32014-05-06 16:47:23 +08001531 trace_f2fs_write_end(inode, pos, len, copied);
1532
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001533 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001534
1535 if (pos + copied > i_size_read(inode)) {
1536 i_size_write(inode, pos + copied);
1537 mark_inode_dirty(inode);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001538 }
1539
Chao Yu75c3c8b2013-11-16 14:15:59 +08001540 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001541 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001542 return copied;
1543}
1544
Omar Sandoval6f673762015-03-16 04:33:52 -07001545static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1546 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001547{
1548 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001549
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001550 if (offset & blocksize_mask)
1551 return -EINVAL;
1552
Al Viro5b46f252014-03-16 18:07:34 -04001553 if (iov_iter_alignment(iter) & blocksize_mask)
1554 return -EINVAL;
1555
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001556 return 0;
1557}
1558
Omar Sandoval22c61862015-03-16 04:33:53 -07001559static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
1560 loff_t offset)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001561{
1562 struct file *file = iocb->ki_filp;
Chao Yu3aab8f82014-07-02 13:25:04 +08001563 struct address_space *mapping = file->f_mapping;
1564 struct inode *inode = mapping->host;
1565 size_t count = iov_iter_count(iter);
1566 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001567
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001568 /* we don't need to use inline_data strictly */
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001569 err = f2fs_convert_inline_inode(inode);
1570 if (err)
1571 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001572
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001573 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1574 return 0;
1575
Chao Yuc15e8592015-08-07 18:39:32 +08001576 err = check_direct_IO(inode, iter, offset);
1577 if (err)
1578 return err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001579
Omar Sandoval6f673762015-03-16 04:33:52 -07001580 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001581
Chao Yuf9811702015-09-21 20:17:52 +08001582 if (iov_iter_rw(iter) == WRITE) {
Chao Yu6d5a1492015-12-24 18:04:56 +08001583 err = __allocate_data_blocks(inode, offset, count);
1584 if (err)
Chao Yuf9811702015-09-21 20:17:52 +08001585 goto out;
Chao Yuf9811702015-09-21 20:17:52 +08001586 }
Jaegeuk Kim59b802e2015-02-09 12:09:53 -08001587
Chao Yue2b4e2b2015-08-19 19:11:19 +08001588 err = blockdev_direct_IO(iocb, inode, iter, offset, get_data_block_dio);
Chao Yuf9811702015-09-21 20:17:52 +08001589out:
Omar Sandoval6f673762015-03-16 04:33:52 -07001590 if (err < 0 && iov_iter_rw(iter) == WRITE)
Chao Yu3aab8f82014-07-02 13:25:04 +08001591 f2fs_write_failed(mapping, offset + count);
Chao Yu70407fa2014-07-31 21:11:22 +08001592
Omar Sandoval6f673762015-03-16 04:33:52 -07001593 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001594
Chao Yu3aab8f82014-07-02 13:25:04 +08001595 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001596}
1597
Chao Yu487261f2015-02-05 17:44:29 +08001598void f2fs_invalidate_page(struct page *page, unsigned int offset,
1599 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001600{
1601 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001602 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001603
Chao Yu487261f2015-02-05 17:44:29 +08001604 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
1605 (offset % PAGE_CACHE_SIZE || length != PAGE_CACHE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001606 return;
1607
Chao Yu487261f2015-02-05 17:44:29 +08001608 if (PageDirty(page)) {
1609 if (inode->i_ino == F2FS_META_INO(sbi))
1610 dec_page_count(sbi, F2FS_DIRTY_META);
1611 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1612 dec_page_count(sbi, F2FS_DIRTY_NODES);
1613 else
1614 inode_dec_dirty_pages(inode);
1615 }
Chao Yudecd36b2015-08-07 18:42:09 +08001616
1617 /* This is atomic written page, keep Private */
1618 if (IS_ATOMIC_WRITTEN_PAGE(page))
1619 return;
1620
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001621 ClearPagePrivate(page);
1622}
1623
Chao Yu487261f2015-02-05 17:44:29 +08001624int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001625{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001626 /* If this is dirty page, keep PagePrivate */
1627 if (PageDirty(page))
1628 return 0;
1629
Chao Yudecd36b2015-08-07 18:42:09 +08001630 /* This is atomic written page, keep Private */
1631 if (IS_ATOMIC_WRITTEN_PAGE(page))
1632 return 0;
1633
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001634 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001635 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001636}
1637
1638static int f2fs_set_data_page_dirty(struct page *page)
1639{
1640 struct address_space *mapping = page->mapping;
1641 struct inode *inode = mapping->host;
1642
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001643 trace_f2fs_set_page_dirty(page, DATA);
1644
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001645 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001646
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001647 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001648 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1649 register_inmem_page(inode, page);
1650 return 1;
1651 }
1652 /*
1653 * Previously, this page has been registered, we just
1654 * return here.
1655 */
1656 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001657 }
1658
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001659 if (!PageDirty(page)) {
1660 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001661 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001662 return 1;
1663 }
1664 return 0;
1665}
1666
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001667static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1668{
Chao Yu454ae7e2014-04-22 13:34:01 +08001669 struct inode *inode = mapping->host;
1670
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001671 if (f2fs_has_inline_data(inode))
1672 return 0;
1673
1674 /* make sure allocating whole blocks */
1675 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1676 filemap_write_and_wait(mapping);
1677
Chao Yue2b4e2b2015-08-19 19:11:19 +08001678 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001679}
1680
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001681const struct address_space_operations f2fs_dblock_aops = {
1682 .readpage = f2fs_read_data_page,
1683 .readpages = f2fs_read_data_pages,
1684 .writepage = f2fs_write_data_page,
1685 .writepages = f2fs_write_data_pages,
1686 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001687 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001688 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001689 .invalidatepage = f2fs_invalidate_page,
1690 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001691 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001692 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001693};