blob: 53fae38009d3346d1edb094b704d292dd51008df [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002 * fs/f2fs/data.c
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13#include <linux/buffer_head.h>
14#include <linux/mpage.h>
15#include <linux/writeback.h>
16#include <linux/backing-dev.h>
Chao Yu8f46dca2015-07-14 18:56:10 +080017#include <linux/pagevec.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090018#include <linux/blkdev.h>
19#include <linux/bio.h>
Geert Uytterhoeven690e4a32012-12-19 22:19:30 +010020#include <linux/prefetch.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080021#include <linux/uio.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070022#include <linux/cleancache.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090023
24#include "f2fs.h"
25#include "node.h"
26#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080027#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090028#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090029
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020030static void f2fs_read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090031{
Linus Torvaldsf5688492014-01-30 11:19:05 -080032 struct bio_vec *bvec;
33 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090034
Chao Yu12377022015-05-25 18:03:38 +080035 if (f2fs_bio_encrypted(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020036 if (bio->bi_error) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070037 fscrypt_release_ctx(bio->bi_private);
Chao Yu12377022015-05-25 18:03:38 +080038 } else {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070039 fscrypt_decrypt_bio_pages(bio->bi_private, bio);
Chao Yu12377022015-05-25 18:03:38 +080040 return;
41 }
42 }
43
Linus Torvaldsf5688492014-01-30 11:19:05 -080044 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090045 struct page *page = bvec->bv_page;
46
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020047 if (!bio->bi_error) {
Linus Torvaldsf5688492014-01-30 11:19:05 -080048 SetPageUptodate(page);
49 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090050 ClearPageUptodate(page);
51 SetPageError(page);
52 }
53 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080054 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090055 bio_put(bio);
56}
57
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020058static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090059{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090060 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080061 struct bio_vec *bvec;
62 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090063
Linus Torvaldsf5688492014-01-30 11:19:05 -080064 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090065 struct page *page = bvec->bv_page;
66
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070067 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -070068
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020069 if (unlikely(bio->bi_error)) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090070 set_bit(AS_EIO, &page->mapping->flags);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -070071 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090072 }
73 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080074 }
Jaegeuk Kimf5730182016-05-17 16:23:36 -070075 if (atomic_dec_and_test(&sbi->nr_wb_bios) &&
76 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090077 wake_up(&sbi->cp_wait);
78
79 bio_put(bio);
80}
81
Gu Zheng940a6d32013-12-20 17:39:59 +080082/*
83 * Low-level block read/write IO operations.
84 */
85static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
86 int npages, bool is_read)
87{
88 struct bio *bio;
89
Jaegeuk Kim740432f2015-08-14 11:43:56 -070090 bio = f2fs_bio_alloc(npages);
Gu Zheng940a6d32013-12-20 17:39:59 +080091
92 bio->bi_bdev = sbi->sb->s_bdev;
Chao Yu55cf9cb2014-09-15 18:01:10 +080093 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
Gu Zheng940a6d32013-12-20 17:39:59 +080094 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +080095 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +080096
97 return bio;
98}
99
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700100static inline void __submit_bio(struct f2fs_sb_info *sbi, int rw,
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700101 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700102{
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700103 if (!is_read_io(rw)) {
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700104 atomic_inc(&sbi->nr_wb_bios);
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700105 if (f2fs_sb_mounted_hmsmr(sbi->sb) &&
106 current->plug && (type == DATA || type == NODE))
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700107 blk_finish_plug(current->plug);
108 }
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700109 submit_bio(rw, bio);
110}
111
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900112static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900113{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900114 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900115
116 if (!io->bio)
117 return;
118
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700119 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800120 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700121 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800122 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900123
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700124 __submit_bio(io->sbi, fio->rw, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900125 io->bio = NULL;
126}
127
Chao Yu0c3a5792016-01-18 18:28:11 +0800128static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
129 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800130{
Chao Yu0fd785e2016-01-18 18:24:59 +0800131 struct bio_vec *bvec;
132 struct page *target;
133 int i;
134
Chao Yu0c3a5792016-01-18 18:28:11 +0800135 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800136 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800137
138 if (!inode && !page && !ino)
139 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800140
141 bio_for_each_segment_all(bvec, io->bio, i) {
142
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700143 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800144 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700145 else
146 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800147
Chao Yu0c3a5792016-01-18 18:28:11 +0800148 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800149 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800150 if (page && page == target)
151 return true;
152 if (ino && ino == ino_of_node(target))
153 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800154 }
155
Chao Yu0fd785e2016-01-18 18:24:59 +0800156 return false;
157}
158
Chao Yu0c3a5792016-01-18 18:28:11 +0800159static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
160 struct page *page, nid_t ino,
161 enum page_type type)
162{
163 enum page_type btype = PAGE_TYPE_OF_BIO(type);
164 struct f2fs_bio_info *io = &sbi->write_io[btype];
165 bool ret;
166
167 down_read(&io->io_rwsem);
168 ret = __has_merged_page(io, inode, page, ino);
169 up_read(&io->io_rwsem);
170 return ret;
171}
172
173static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
174 struct inode *inode, struct page *page,
175 nid_t ino, enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900176{
177 enum page_type btype = PAGE_TYPE_OF_BIO(type);
178 struct f2fs_bio_info *io;
179
180 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
181
Chao Yudf0f8dc2014-03-22 14:57:23 +0800182 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900183
Chao Yu0c3a5792016-01-18 18:28:11 +0800184 if (!__has_merged_page(io, inode, page, ino))
185 goto out;
186
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900187 /* change META to META_FLUSH in the checkpoint procedure */
188 if (type >= META_FLUSH) {
189 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700190 if (test_opt(sbi, NOBARRIER))
191 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
192 else
193 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900194 }
195 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800196out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800197 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900198}
199
Chao Yu0c3a5792016-01-18 18:28:11 +0800200void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
201 int rw)
202{
203 __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw);
204}
205
206void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
207 struct inode *inode, struct page *page,
208 nid_t ino, enum page_type type, int rw)
209{
210 if (has_merged_page(sbi, inode, page, ino, type))
211 __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw);
212}
213
Chao Yu406657d2016-02-24 17:17:55 +0800214void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
215{
216 f2fs_submit_merged_bio(sbi, DATA, WRITE);
217 f2fs_submit_merged_bio(sbi, NODE, WRITE);
218 f2fs_submit_merged_bio(sbi, META, WRITE);
219}
220
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900221/*
222 * Fill the locked page with data located in the block address.
223 * Return unlocked page.
224 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700225int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900226{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900227 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700228 struct page *page = fio->encrypted_page ?
229 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900230
Chao Yu2ace38e2014-12-24 16:08:14 +0800231 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700232 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900233
234 /* Allocate a new bio */
Chao Yu7a9d7542016-02-22 18:36:38 +0800235 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900236
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300237 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900238 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900239 return -EFAULT;
240 }
241
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700242 __submit_bio(fio->sbi, fio->rw, bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900243 return 0;
244}
245
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700246void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900247{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700248 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900249 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900250 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800251 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700252 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900253
Gu Zheng940a6d32013-12-20 17:39:59 +0800254 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900255
Chao Yu7a9d7542016-02-22 18:36:38 +0800256 if (fio->old_blkaddr != NEW_ADDR)
257 verify_block_addr(sbi, fio->old_blkaddr);
258 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900259
Chao Yudf0f8dc2014-03-22 14:57:23 +0800260 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900261
Chao Yu7a9d7542016-02-22 18:36:38 +0800262 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900263 io->fio.rw != fio->rw))
264 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900265alloc_new:
266 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700267 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800268
Chao Yu7a9d7542016-02-22 18:36:38 +0800269 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
270 bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900271 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900272 }
273
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700274 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
275
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300276 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
277 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900278 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900279 goto alloc_new;
280 }
281
Chao Yu7a9d7542016-02-22 18:36:38 +0800282 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700283 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900284
Chao Yudf0f8dc2014-03-22 14:57:23 +0800285 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700286 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900287}
288
Chao Yu46008c62016-05-09 19:56:30 +0800289static void __set_data_blkaddr(struct dnode_of_data *dn)
290{
291 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
292 __le32 *addr_array;
293
294 /* Get physical address of data block */
295 addr_array = blkaddr_in_node(rn);
296 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
297}
298
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900299/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900300 * Lock ordering for the change of data block address:
301 * ->data_page
302 * ->node_page
303 * update block addresses in the node page
304 */
Chao Yu216a6202015-03-19 19:23:32 +0800305void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900306{
Chao Yu46008c62016-05-09 19:56:30 +0800307 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
308 __set_data_blkaddr(dn);
309 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800310 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900311}
312
Chao Yuf28b3432016-02-24 17:16:47 +0800313void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
314{
315 dn->data_blkaddr = blkaddr;
316 set_data_blkaddr(dn);
317 f2fs_update_extent_cache(dn);
318}
319
Chao Yu46008c62016-05-09 19:56:30 +0800320/* dn->ofs_in_node will be returned with up-to-date last block pointer */
321int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900322{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700323 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900324
Chao Yu46008c62016-05-09 19:56:30 +0800325 if (!count)
326 return 0;
327
Jaegeuk Kim91942322016-05-20 10:13:22 -0700328 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900329 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800330 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900331 return -ENOSPC;
332
Chao Yu46008c62016-05-09 19:56:30 +0800333 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
334 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900335
Chao Yu46008c62016-05-09 19:56:30 +0800336 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
337
338 for (; count > 0; dn->ofs_in_node++) {
339 block_t blkaddr =
340 datablock_addr(dn->node_page, dn->ofs_in_node);
341 if (blkaddr == NULL_ADDR) {
342 dn->data_blkaddr = NEW_ADDR;
343 __set_data_blkaddr(dn);
344 count--;
345 }
346 }
347
348 if (set_page_dirty(dn->node_page))
349 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900350 return 0;
351}
352
Chao Yu46008c62016-05-09 19:56:30 +0800353/* Should keep dn->ofs_in_node unchanged */
354int reserve_new_block(struct dnode_of_data *dn)
355{
356 unsigned int ofs_in_node = dn->ofs_in_node;
357 int ret;
358
359 ret = reserve_new_blocks(dn, 1);
360 dn->ofs_in_node = ofs_in_node;
361 return ret;
362}
363
Huajun Lib6009652013-11-10 23:13:18 +0800364int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
365{
366 bool need_put = dn->inode_page ? false : true;
367 int err;
368
369 err = get_dnode_of_data(dn, index, ALLOC_NODE);
370 if (err)
371 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900372
Huajun Lib6009652013-11-10 23:13:18 +0800373 if (dn->data_blkaddr == NULL_ADDR)
374 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900375 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800376 f2fs_put_dnode(dn);
377 return err;
378}
379
Fan Li759af1c2015-08-05 15:52:16 +0800380int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900381{
Chao Yu028a41e2015-03-19 19:26:02 +0800382 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800383 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800384
Fan Li759af1c2015-08-05 15:52:16 +0800385 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
386 dn->data_blkaddr = ei.blk + index - ei.fofs;
387 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800388 }
389
Fan Li759af1c2015-08-05 15:52:16 +0800390 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900391}
392
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700393struct page *get_read_data_page(struct inode *inode, pgoff_t index,
394 int rw, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900395{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900396 struct address_space *mapping = inode->i_mapping;
397 struct dnode_of_data dn;
398 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800399 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900400 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800401 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700402 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800403 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700404 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700405 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800406 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900407
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700408 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
409 return read_mapping_page(mapping, index, NULL);
410
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700411 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900412 if (!page)
413 return ERR_PTR(-ENOMEM);
414
Chao Yucb3bc9e2015-02-05 18:03:40 +0800415 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
416 dn.data_blkaddr = ei.blk + index - ei.fofs;
417 goto got_it;
418 }
419
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900420 set_new_dnode(&dn, inode, NULL, NULL, 0);
421 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700422 if (err)
423 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900424 f2fs_put_dnode(&dn);
425
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900426 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700427 err = -ENOENT;
428 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900429 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800430got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700431 if (PageUptodate(page)) {
432 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900433 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700434 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900435
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900436 /*
437 * A new dentry page is allocated but not able to be written, since its
438 * new inode page couldn't be allocated due to -ENOSPC.
439 * In such the case, its blkaddr can be remained as NEW_ADDR.
440 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
441 */
442 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300443 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900444 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700445 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900446 return page;
447 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900448
Chao Yu7a9d7542016-02-22 18:36:38 +0800449 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700450 fio.page = page;
451 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900452 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700453 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700454 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700455
456put_err:
457 f2fs_put_page(page, 1);
458 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700459}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900460
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700461struct page *find_data_page(struct inode *inode, pgoff_t index)
462{
463 struct address_space *mapping = inode->i_mapping;
464 struct page *page;
465
466 page = find_get_page(mapping, index);
467 if (page && PageUptodate(page))
468 return page;
469 f2fs_put_page(page, 0);
470
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700471 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700472 if (IS_ERR(page))
473 return page;
474
475 if (PageUptodate(page))
476 return page;
477
478 wait_on_page_locked(page);
479 if (unlikely(!PageUptodate(page))) {
480 f2fs_put_page(page, 0);
481 return ERR_PTR(-EIO);
482 }
483 return page;
484}
485
486/*
487 * If it tries to access a hole, return an error.
488 * Because, the callers, functions in dir.c and GC, should be able to know
489 * whether this page exists or not.
490 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700491struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
492 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700493{
494 struct address_space *mapping = inode->i_mapping;
495 struct page *page;
496repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700497 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700498 if (IS_ERR(page))
499 return page;
500
501 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900502 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900503 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900504 f2fs_put_page(page, 1);
505 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900506 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900507 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900508 f2fs_put_page(page, 1);
509 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900510 }
511 return page;
512}
513
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900514/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900515 * Caller ensures that this data page is never allocated.
516 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900517 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800518 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
519 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800520 * Note that, ipage is set only by make_empty_dir, and if any error occur,
521 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900522 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900523struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900524 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900525{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900526 struct address_space *mapping = inode->i_mapping;
527 struct page *page;
528 struct dnode_of_data dn;
529 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800530
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700531 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800532 if (!page) {
533 /*
534 * before exiting, we should make sure ipage will be released
535 * if any error occur.
536 */
537 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700538 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800539 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900540
Jaegeuk Kima8865372013-12-27 17:04:17 +0900541 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800542 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700543 if (err) {
544 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900545 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900546 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700547 if (!ipage)
548 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900549
550 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700551 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900552
553 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300554 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900555 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900556 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700557 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900558
Jaegeuk Kim76121182016-01-01 22:03:47 -0800559 /* if ipage exists, blkaddr should be NEW_ADDR */
560 f2fs_bug_on(F2FS_I_SB(inode), ipage);
561 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700562 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800563 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900564 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700565got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800566 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700567 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700568 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900569 return page;
570}
571
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900572static int __allocate_data_block(struct dnode_of_data *dn)
573{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700574 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900575 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900576 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800577 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700578 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800579 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900580
Jaegeuk Kim91942322016-05-20 10:13:22 -0700581 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900582 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800583
584 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
585 if (dn->data_blkaddr == NEW_ADDR)
586 goto alloc;
587
Chao Yu46008c62016-05-09 19:56:30 +0800588 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900589 return -ENOSPC;
590
Chao Yudf6136e2015-03-23 10:33:37 +0800591alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900592 get_node_info(sbi, dn->nid, &ni);
593 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
594
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800595 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
596 seg = CURSEG_DIRECT_IO;
597
Chao Yudf6136e2015-03-23 10:33:37 +0800598 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
599 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800600 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900601
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700602 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800603 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700604 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300605 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700606 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300607 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900608 return 0;
609}
610
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800611ssize_t f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800612{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800613 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800614 struct f2fs_map_blocks map;
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800615 ssize_t ret = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800616
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700617 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
618 map.m_len = F2FS_BYTES_TO_BLK(iov_iter_count(from));
Chao Yuda859852016-01-26 15:42:58 +0800619 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800620
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800621 if (f2fs_encrypted_inode(inode))
622 return 0;
623
624 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800625 ret = f2fs_convert_inline_inode(inode);
626 if (ret)
627 return ret;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800628 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800629 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800630 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
631 ret = f2fs_convert_inline_inode(inode);
632 if (ret)
633 return ret;
634 }
635 if (!f2fs_has_inline_data(inode))
636 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800637 return ret;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800638}
639
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900640/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700641 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
642 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800643 * If original data blocks are allocated, then give them to blockdev.
644 * Otherwise,
645 * a. preallocate requested block addresses
646 * b. do not use extent cache for better performance
647 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900648 */
Chao Yud323d002015-10-27 09:53:45 +0800649int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800650 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900651{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700652 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900653 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800654 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700655 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800656 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900657 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800658 unsigned int ofs_in_node, last_ofs_in_node;
659 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800660 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900661 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800662 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900663
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700664 map->m_len = 0;
665 map->m_flags = 0;
666
667 /* it only supports block size == page size */
668 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800669 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900670
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800671 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700672 map->m_pblk = ei.blk + pgofs - ei.fofs;
673 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
674 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900675 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800676 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900677
Chao Yu4fe71e82016-01-26 15:37:38 +0800678next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800679 if (create)
Chao Yu3104af352015-12-23 17:11:43 +0800680 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900681
682 /* When reading holes, we need its node page */
683 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900684 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900685 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800686 if (flag == F2FS_GET_BLOCK_BMAP)
687 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800688 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900689 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800690 if (map->m_next_pgofs)
691 *map->m_next_pgofs =
692 get_next_page_offset(&dn, pgofs);
693 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900694 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900695 }
Chao Yu973163f2015-09-18 16:51:51 +0800696
Chao Yu46008c62016-05-09 19:56:30 +0800697 prealloc = 0;
698 ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800699 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900700
Chao Yu4fe71e82016-01-26 15:37:38 +0800701next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800702 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800703
Fan Li7df3a432015-12-17 13:20:59 +0800704 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
705 if (create) {
706 if (unlikely(f2fs_cp_error(sbi))) {
707 err = -EIO;
708 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800709 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800710 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800711 if (blkaddr == NULL_ADDR) {
712 prealloc++;
713 last_ofs_in_node = dn.ofs_in_node;
714 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800715 } else {
716 err = __allocate_data_block(&dn);
Chao Yu46008c62016-05-09 19:56:30 +0800717 if (!err) {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700718 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yu46008c62016-05-09 19:56:30 +0800719 allocated = true;
720 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800721 }
Fan Li7df3a432015-12-17 13:20:59 +0800722 if (err)
723 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800724 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800725 blkaddr = dn.data_blkaddr;
726 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800727 if (flag == F2FS_GET_BLOCK_BMAP) {
728 map->m_pblk = 0;
729 goto sync_out;
730 }
Chao Yuda859852016-01-26 15:42:58 +0800731 if (flag == F2FS_GET_BLOCK_FIEMAP &&
732 blkaddr == NULL_ADDR) {
733 if (map->m_next_pgofs)
734 *map->m_next_pgofs = pgofs + 1;
735 }
Fan Li7df3a432015-12-17 13:20:59 +0800736 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800737 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800738 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900739 }
740 }
Fan Li7df3a432015-12-17 13:20:59 +0800741
Chao Yu46008c62016-05-09 19:56:30 +0800742 if (flag == F2FS_GET_BLOCK_PRE_AIO)
743 goto skip;
744
Chao Yu4fe71e82016-01-26 15:37:38 +0800745 if (map->m_len == 0) {
746 /* preallocated unwritten block should be mapped for fiemap. */
747 if (blkaddr == NEW_ADDR)
748 map->m_flags |= F2FS_MAP_UNWRITTEN;
749 map->m_flags |= F2FS_MAP_MAPPED;
750
751 map->m_pblk = blkaddr;
752 map->m_len = 1;
753 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800754 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800755 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800756 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800757 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800758 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800759 } else {
760 goto sync_out;
761 }
762
Chao Yu46008c62016-05-09 19:56:30 +0800763skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800764 dn.ofs_in_node++;
765 pgofs++;
766
Chao Yu46008c62016-05-09 19:56:30 +0800767 /* preallocate blocks in batch for one dnode page */
768 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
769 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800770
Chao Yu46008c62016-05-09 19:56:30 +0800771 dn.ofs_in_node = ofs_in_node;
772 err = reserve_new_blocks(&dn, prealloc);
773 if (err)
774 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800775
Chao Yu46008c62016-05-09 19:56:30 +0800776 map->m_len += dn.ofs_in_node - ofs_in_node;
777 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
778 err = -ENOSPC;
779 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800780 }
Chao Yu46008c62016-05-09 19:56:30 +0800781 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800782 }
783
Chao Yu46008c62016-05-09 19:56:30 +0800784 if (pgofs >= end)
785 goto sync_out;
786 else if (dn.ofs_in_node < end_offset)
787 goto next_block;
788
Chao Yu46008c62016-05-09 19:56:30 +0800789 f2fs_put_dnode(&dn);
790
791 if (create) {
792 f2fs_unlock_op(sbi);
793 f2fs_balance_fs(sbi, allocated);
794 }
795 allocated = false;
796 goto next_dnode;
797
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900798sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900799 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900800unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800801 if (create) {
Chao Yu3104af352015-12-23 17:11:43 +0800802 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800803 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800804 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900805out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700806 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900807 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900808}
809
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700810static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800811 struct buffer_head *bh, int create, int flag,
812 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700813{
814 struct f2fs_map_blocks map;
815 int ret;
816
817 map.m_lblk = iblock;
818 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800819 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700820
Chao Yue2b4e2b2015-08-19 19:11:19 +0800821 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700822 if (!ret) {
823 map_bh(bh, inode->i_sb, map.m_pblk);
824 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
825 bh->b_size = map.m_len << inode->i_blkbits;
826 }
827 return ret;
828}
829
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900830static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800831 struct buffer_head *bh_result, int create, int flag,
832 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900833{
Chao Yuda859852016-01-26 15:42:58 +0800834 return __get_data_block(inode, iblock, bh_result, create,
835 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900836}
837
Chao Yue2b4e2b2015-08-19 19:11:19 +0800838static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900839 struct buffer_head *bh_result, int create)
840{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800841 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800842 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800843}
844
845static int get_data_block_bmap(struct inode *inode, sector_t iblock,
846 struct buffer_head *bh_result, int create)
847{
Yunlei He179448b2015-12-28 21:48:32 +0800848 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800849 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800850 return -EFBIG;
851
Chao Yue2b4e2b2015-08-19 19:11:19 +0800852 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800853 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900854}
855
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700856static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
857{
858 return (offset >> inode->i_blkbits);
859}
860
861static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
862{
863 return (blk << inode->i_blkbits);
864}
865
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900866int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
867 u64 start, u64 len)
868{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700869 struct buffer_head map_bh;
870 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800871 pgoff_t next_pgofs;
Fan Lide1475c2016-01-04 15:56:50 +0800872 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700873 u64 logical = 0, phys = 0, size = 0;
874 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700875 int ret = 0;
876
877 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
878 if (ret)
879 return ret;
880
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700881 if (f2fs_has_inline_data(inode)) {
882 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
883 if (ret != -EAGAIN)
884 return ret;
885 }
886
Al Viro59551022016-01-22 15:40:57 -0500887 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800888
889 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800890 if (start >= isize)
891 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700892
Fan Li9a950d52015-12-26 18:07:41 +0800893 if (start + len > isize)
894 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700895
896 if (logical_to_blk(inode, len) == 0)
897 len = blk_to_logical(inode, 1);
898
899 start_blk = logical_to_blk(inode, start);
900 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800901
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700902next:
903 memset(&map_bh, 0, sizeof(struct buffer_head));
904 map_bh.b_size = len;
905
Chao Yue2b4e2b2015-08-19 19:11:19 +0800906 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800907 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700908 if (ret)
909 goto out;
910
911 /* HOLE */
912 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800913 start_blk = next_pgofs;
Fan Li9a950d52015-12-26 18:07:41 +0800914 /* Go through holes util pass the EOF */
Chao Yuda859852016-01-26 15:42:58 +0800915 if (blk_to_logical(inode, start_blk) < isize)
Fan Li9a950d52015-12-26 18:07:41 +0800916 goto prep_next;
917 /* Found a hole beyond isize means no more extents.
918 * Note that the premise is that filesystems don't
919 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700920 */
Fan Li9a950d52015-12-26 18:07:41 +0800921 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700922 }
Fan Li9a950d52015-12-26 18:07:41 +0800923
Chao Yuda5af122016-01-08 20:19:27 +0800924 if (size) {
925 if (f2fs_encrypted_inode(inode))
926 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
927
Fan Li9a950d52015-12-26 18:07:41 +0800928 ret = fiemap_fill_next_extent(fieinfo, logical,
929 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800930 }
Fan Li9a950d52015-12-26 18:07:41 +0800931
932 if (start_blk > last_blk || ret)
933 goto out;
934
935 logical = blk_to_logical(inode, start_blk);
936 phys = blk_to_logical(inode, map_bh.b_blocknr);
937 size = map_bh.b_size;
938 flags = 0;
939 if (buffer_unwritten(&map_bh))
940 flags = FIEMAP_EXTENT_UNWRITTEN;
941
942 start_blk += logical_to_blk(inode, size);
943
944prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700945 cond_resched();
946 if (fatal_signal_pending(current))
947 ret = -EINTR;
948 else
949 goto next;
950out:
951 if (ret == 1)
952 ret = 0;
953
Al Viro59551022016-01-22 15:40:57 -0500954 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700955 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +0900956}
957
Chao Yu78682f72016-07-03 22:05:11 +0800958struct bio *f2fs_grab_bio(struct inode *inode, block_t blkaddr,
959 unsigned nr_pages)
960{
961 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
962 struct fscrypt_ctx *ctx = NULL;
963 struct block_device *bdev = sbi->sb->s_bdev;
964 struct bio *bio;
965
966 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
967 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
968 if (IS_ERR(ctx))
969 return ERR_CAST(ctx);
970
971 /* wait the page to be moved by cleaning */
972 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
973 }
974
975 bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES));
976 if (!bio) {
977 if (ctx)
978 fscrypt_release_ctx(ctx);
979 return ERR_PTR(-ENOMEM);
980 }
981 bio->bi_bdev = bdev;
982 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blkaddr);
983 bio->bi_end_io = f2fs_read_end_io;
984 bio->bi_private = ctx;
985
986 return bio;
987}
988
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700989/*
990 * This function was originally taken from fs/mpage.c, and customized for f2fs.
991 * Major change was from block_size == page_size in f2fs by default.
992 */
993static int f2fs_mpage_readpages(struct address_space *mapping,
994 struct list_head *pages, struct page *page,
995 unsigned nr_pages)
996{
997 struct bio *bio = NULL;
998 unsigned page_idx;
999 sector_t last_block_in_bio = 0;
1000 struct inode *inode = mapping->host;
1001 const unsigned blkbits = inode->i_blkbits;
1002 const unsigned blocksize = 1 << blkbits;
1003 sector_t block_in_file;
1004 sector_t last_block;
1005 sector_t last_block_in_file;
1006 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001007 struct f2fs_map_blocks map;
1008
1009 map.m_pblk = 0;
1010 map.m_lblk = 0;
1011 map.m_len = 0;
1012 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001013 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001014
1015 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
1016
1017 prefetchw(&page->flags);
1018 if (pages) {
1019 page = list_entry(pages->prev, struct page, lru);
1020 list_del(&page->lru);
1021 if (add_to_page_cache_lru(page, mapping,
1022 page->index, GFP_KERNEL))
1023 goto next_page;
1024 }
1025
1026 block_in_file = (sector_t)page->index;
1027 last_block = block_in_file + nr_pages;
1028 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1029 blkbits;
1030 if (last_block > last_block_in_file)
1031 last_block = last_block_in_file;
1032
1033 /*
1034 * Map blocks using the previous result first.
1035 */
1036 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1037 block_in_file > map.m_lblk &&
1038 block_in_file < (map.m_lblk + map.m_len))
1039 goto got_it;
1040
1041 /*
1042 * Then do more f2fs_map_blocks() calls until we are
1043 * done with this page.
1044 */
1045 map.m_flags = 0;
1046
1047 if (block_in_file < last_block) {
1048 map.m_lblk = block_in_file;
1049 map.m_len = last_block - block_in_file;
1050
Chao Yu46c9e142015-09-18 16:54:16 +08001051 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001052 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001053 goto set_error_page;
1054 }
1055got_it:
1056 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1057 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1058 SetPageMappedToDisk(page);
1059
1060 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1061 SetPageUptodate(page);
1062 goto confused;
1063 }
1064 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001065 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001066 SetPageUptodate(page);
1067 unlock_page(page);
1068 goto next_page;
1069 }
1070
1071 /*
1072 * This page will go to BIO. Do we need to send this
1073 * BIO off first?
1074 */
1075 if (bio && (last_block_in_bio != block_nr - 1)) {
1076submit_and_realloc:
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -07001077 __submit_bio(F2FS_I_SB(inode), READ, bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001078 bio = NULL;
1079 }
1080 if (bio == NULL) {
Chao Yu78682f72016-07-03 22:05:11 +08001081 bio = f2fs_grab_bio(inode, block_nr, nr_pages);
1082 if (IS_ERR(bio))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001083 goto set_error_page;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001084 }
1085
1086 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1087 goto submit_and_realloc;
1088
1089 last_block_in_bio = block_nr;
1090 goto next_page;
1091set_error_page:
1092 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001093 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001094 unlock_page(page);
1095 goto next_page;
1096confused:
1097 if (bio) {
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -07001098 __submit_bio(F2FS_I_SB(inode), READ, bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001099 bio = NULL;
1100 }
1101 unlock_page(page);
1102next_page:
1103 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001104 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001105 }
1106 BUG_ON(pages && !list_empty(pages));
1107 if (bio)
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -07001108 __submit_bio(F2FS_I_SB(inode), READ, bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001109 return 0;
1110}
1111
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001112static int f2fs_read_data_page(struct file *file, struct page *page)
1113{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001114 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001115 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001116
Chao Yuc20e89c2014-05-06 16:53:08 +08001117 trace_f2fs_readpage(page, DATA);
1118
arter97e1c42042014-08-06 23:22:50 +09001119 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001120 if (f2fs_has_inline_data(inode))
1121 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001122 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001123 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001124 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001125}
1126
1127static int f2fs_read_data_pages(struct file *file,
1128 struct address_space *mapping,
1129 struct list_head *pages, unsigned nr_pages)
1130{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001131 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001132 struct page *page = list_entry(pages->prev, struct page, lru);
1133
1134 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001135
1136 /* If the file has inline data, skip readpages */
1137 if (f2fs_has_inline_data(inode))
1138 return 0;
1139
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001140 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001141}
1142
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001143int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001144{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001145 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001146 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001147 struct dnode_of_data dn;
1148 int err = 0;
1149
1150 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001151 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001152 if (err)
1153 return err;
1154
Chao Yu28bc1062016-02-06 14:40:34 +08001155 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001156
1157 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001158 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001159 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001160 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001161 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001162
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001163 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001164 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001165
1166 /* wait for GCed encrypted page writeback */
1167 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001168 fio->old_blkaddr);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001169retry_encrypt:
1170 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1171 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001172 if (IS_ERR(fio->encrypted_page)) {
1173 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001174 if (err == -ENOMEM) {
1175 /* flush pending ios and wait for a while */
1176 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1177 congestion_wait(BLK_RW_ASYNC, HZ/50);
1178 gfp_flags |= __GFP_NOFAIL;
1179 err = 0;
1180 goto retry_encrypt;
1181 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001182 goto out_writepage;
1183 }
1184 }
1185
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001186 set_page_writeback(page);
1187
1188 /*
1189 * If current allocation needs SSR,
1190 * it had better in-place writes for updated data.
1191 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001192 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001193 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001194 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001195 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001196 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001197 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001198 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001199 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001200 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001201 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001202 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001203 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001204 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001205 }
1206out_writepage:
1207 f2fs_put_dnode(&dn);
1208 return err;
1209}
1210
1211static int f2fs_write_data_page(struct page *page,
1212 struct writeback_control *wbc)
1213{
1214 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001215 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001216 loff_t i_size = i_size_read(inode);
1217 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001218 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001219 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001220 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001221 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001222 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001223 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001224 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001225 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001226 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001227 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001228 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001229 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001230
Chao Yuecda0de2014-05-06 16:48:26 +08001231 trace_f2fs_writepage(page, DATA);
1232
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001233 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001234 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001235
1236 /*
1237 * If the offset is out-of-range of file size,
1238 * this page does not have to be written to disk.
1239 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001240 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001241 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001242 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001243
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001244 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001245write:
Chao Yucaf00472015-01-28 17:48:42 +08001246 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001247 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001248 if (f2fs_is_drop_cache(inode))
1249 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001250 /* we should not write 0'th page having journal header */
1251 if (f2fs_is_volatile_file(inode) && (!page->index ||
1252 (!wbc->for_reclaim &&
1253 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001254 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001255
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001256 /* we should bypass data pages to proceed the kworkder jobs */
1257 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001258 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001259 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001260 }
1261
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001262 /* Dentry blocks are controlled by checkpoint */
1263 if (S_ISDIR(inode->i_mode)) {
1264 err = do_write_data_page(&fio);
1265 goto done;
1266 }
1267
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001268 if (!wbc->for_reclaim)
1269 need_balance_fs = true;
1270 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001271 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001272
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001273 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001274 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001275 if (f2fs_has_inline_data(inode))
1276 err = f2fs_write_inline_data(inode, page);
1277 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001278 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001279 if (F2FS_I(inode)->last_disk_size < psize)
1280 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001281 f2fs_unlock_op(sbi);
1282done:
1283 if (err && err != -ENOENT)
1284 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001285
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001286 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001287out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001288 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001289 if (err)
1290 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001291
1292 if (wbc->for_reclaim) {
1293 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001294 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001295 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001296
1297 unlock_page(page);
1298 f2fs_balance_fs(sbi, need_balance_fs);
1299
1300 if (unlikely(f2fs_cp_error(sbi)))
1301 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1302
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001303 return 0;
1304
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001305redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001306 redirty_page_for_writepage(wbc, page);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001307 unlock_page(page);
1308 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001309}
1310
Chao Yu8f46dca2015-07-14 18:56:10 +08001311/*
1312 * This function was copied from write_cche_pages from mm/page-writeback.c.
1313 * The major change is making write step of cold data page separately from
1314 * warm/hot data page.
1315 */
1316static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001317 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001318{
1319 int ret = 0;
1320 int done = 0;
1321 struct pagevec pvec;
1322 int nr_pages;
1323 pgoff_t uninitialized_var(writeback_index);
1324 pgoff_t index;
1325 pgoff_t end; /* Inclusive */
1326 pgoff_t done_index;
1327 int cycled;
1328 int range_whole = 0;
1329 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001330
1331 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001332
Chao Yu8f46dca2015-07-14 18:56:10 +08001333 if (wbc->range_cyclic) {
1334 writeback_index = mapping->writeback_index; /* prev offset */
1335 index = writeback_index;
1336 if (index == 0)
1337 cycled = 1;
1338 else
1339 cycled = 0;
1340 end = -1;
1341 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001342 index = wbc->range_start >> PAGE_SHIFT;
1343 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001344 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1345 range_whole = 1;
1346 cycled = 1; /* ignore range_cyclic tests */
1347 }
1348 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1349 tag = PAGECACHE_TAG_TOWRITE;
1350 else
1351 tag = PAGECACHE_TAG_DIRTY;
1352retry:
1353 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1354 tag_pages_for_writeback(mapping, index, end);
1355 done_index = index;
1356 while (!done && (index <= end)) {
1357 int i;
1358
1359 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1360 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1361 if (nr_pages == 0)
1362 break;
1363
1364 for (i = 0; i < nr_pages; i++) {
1365 struct page *page = pvec.pages[i];
1366
1367 if (page->index > end) {
1368 done = 1;
1369 break;
1370 }
1371
1372 done_index = page->index;
1373
1374 lock_page(page);
1375
1376 if (unlikely(page->mapping != mapping)) {
1377continue_unlock:
1378 unlock_page(page);
1379 continue;
1380 }
1381
1382 if (!PageDirty(page)) {
1383 /* someone wrote it for us */
1384 goto continue_unlock;
1385 }
1386
Chao Yu8f46dca2015-07-14 18:56:10 +08001387 if (PageWriteback(page)) {
1388 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001389 f2fs_wait_on_page_writeback(page,
1390 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001391 else
1392 goto continue_unlock;
1393 }
1394
1395 BUG_ON(PageWriteback(page));
1396 if (!clear_page_dirty_for_io(page))
1397 goto continue_unlock;
1398
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001399 ret = mapping->a_ops->writepage(page, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001400 if (unlikely(ret)) {
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001401 done_index = page->index + 1;
1402 done = 1;
1403 break;
Chao Yu8f46dca2015-07-14 18:56:10 +08001404 }
1405
1406 if (--wbc->nr_to_write <= 0 &&
1407 wbc->sync_mode == WB_SYNC_NONE) {
1408 done = 1;
1409 break;
1410 }
1411 }
1412 pagevec_release(&pvec);
1413 cond_resched();
1414 }
1415
Chao Yu8f46dca2015-07-14 18:56:10 +08001416 if (!cycled && !done) {
1417 cycled = 1;
1418 index = 0;
1419 end = writeback_index - 1;
1420 goto retry;
1421 }
1422 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1423 mapping->writeback_index = done_index;
1424
1425 return ret;
1426}
1427
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001428static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001429 struct writeback_control *wbc)
1430{
1431 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001432 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001433 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001434
P J Pcfb185a2013-04-03 11:38:00 +09001435 /* deal with chardevs and other special file */
1436 if (!mapping->a_ops->writepage)
1437 return 0;
1438
Chao Yu6a290542015-07-17 18:02:39 +08001439 /* skip writing if there is no dirty page in this inode */
1440 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1441 return 0;
1442
Jaegeuk Kima1257022015-10-08 10:40:07 -07001443 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1444 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1445 available_free_memory(sbi, DIRTY_DENTS))
1446 goto skip_write;
1447
Chao Yud323d002015-10-27 09:53:45 +08001448 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001449 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001450 goto skip_write;
1451
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001452 /* during POR, we don't need to trigger writepage at all. */
1453 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1454 goto skip_write;
1455
Yunlei Hed31c7c32016-02-04 16:14:00 +08001456 trace_f2fs_writepages(mapping->host, wbc, DATA);
1457
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001458 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001459 /*
1460 * if some pages were truncated, we cannot guarantee its mapping->host
1461 * to detect pending bios.
1462 */
1463 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001464
Chao Yuc227f912015-12-16 13:09:20 +08001465 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001466 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001467
1468skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001469 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001470 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001471 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001472}
1473
Chao Yu3aab8f82014-07-02 13:25:04 +08001474static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1475{
1476 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001477 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001478
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001479 if (to > i_size) {
1480 truncate_pagecache(inode, i_size);
1481 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001482 }
1483}
1484
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001485static int prepare_write_begin(struct f2fs_sb_info *sbi,
1486 struct page *page, loff_t pos, unsigned len,
1487 block_t *blk_addr, bool *node_changed)
1488{
1489 struct inode *inode = page->mapping->host;
1490 pgoff_t index = page->index;
1491 struct dnode_of_data dn;
1492 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001493 bool locked = false;
1494 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001495 int err = 0;
1496
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001497 /*
1498 * we already allocated all the blocks, so we don't need to get
1499 * the block addresses when there is no need to fill the page.
1500 */
1501 if (!f2fs_has_inline_data(inode) && !f2fs_encrypted_inode(inode) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001502 len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001503 return 0;
1504
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001505 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001506 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001507 f2fs_lock_op(sbi);
1508 locked = true;
1509 }
1510restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001511 /* check inline_data */
1512 ipage = get_node_page(sbi, inode->i_ino);
1513 if (IS_ERR(ipage)) {
1514 err = PTR_ERR(ipage);
1515 goto unlock_out;
1516 }
1517
1518 set_new_dnode(&dn, inode, ipage, ipage, 0);
1519
1520 if (f2fs_has_inline_data(inode)) {
1521 if (pos + len <= MAX_INLINE_DATA) {
1522 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001523 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001524 if (inode->i_nlink)
1525 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001526 } else {
1527 err = f2fs_convert_inline_page(&dn, page);
1528 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001529 goto out;
1530 if (dn.data_blkaddr == NULL_ADDR)
1531 err = f2fs_get_block(&dn, index);
1532 }
1533 } else if (locked) {
1534 err = f2fs_get_block(&dn, index);
1535 } else {
1536 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1537 dn.data_blkaddr = ei.blk + index - ei.fofs;
1538 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001539 /* hole case */
1540 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001541 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001542 f2fs_put_dnode(&dn);
1543 f2fs_lock_op(sbi);
1544 locked = true;
1545 goto restart;
1546 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001547 }
1548 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001549
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001550 /* convert_inline_page can make node_changed */
1551 *blk_addr = dn.data_blkaddr;
1552 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001553out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001554 f2fs_put_dnode(&dn);
1555unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001556 if (locked)
1557 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001558 return err;
1559}
1560
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001561static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1562 loff_t pos, unsigned len, unsigned flags,
1563 struct page **pagep, void **fsdata)
1564{
1565 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001566 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001567 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001568 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001569 bool need_balance = false;
1570 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001571 int err = 0;
1572
Chao Yu62aed042014-05-06 16:46:04 +08001573 trace_f2fs_write_begin(inode, pos, len, flags);
1574
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001575 /*
1576 * We should check this at this moment to avoid deadlock on inode page
1577 * and #0 page. The locking rule for inline_data conversion should be:
1578 * lock_page(page #0) -> lock_page(inode_page)
1579 */
1580 if (index != 0) {
1581 err = f2fs_convert_inline_inode(inode);
1582 if (err)
1583 goto fail;
1584 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001585repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001586 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001587 if (!page) {
1588 err = -ENOMEM;
1589 goto fail;
1590 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001591
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001592 *pagep = page;
1593
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001594 err = prepare_write_begin(sbi, page, pos, len,
1595 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001596 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001597 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001598
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001599 if (need_balance && has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001600 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001601 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001602 lock_page(page);
1603 if (page->mapping != mapping) {
1604 /* The page got truncated from under us */
1605 f2fs_put_page(page, 1);
1606 goto repeat;
1607 }
1608 }
1609
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001610 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001611
Chao Yu08b39fb2015-10-08 13:27:34 +08001612 /* wait for GCed encrypted page writeback */
1613 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001614 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001615
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001616 if (len == PAGE_SIZE)
Chao Yu90d43882015-07-08 18:24:38 +08001617 goto out_update;
1618 if (PageUptodate(page))
1619 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001620
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001621 if ((pos & PAGE_MASK) >= i_size_read(inode)) {
1622 unsigned start = pos & (PAGE_SIZE - 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001623 unsigned end = start + len;
1624
1625 /* Reading beyond i_size is simple: memset to zero */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001626 zero_user_segments(page, 0, start, end, PAGE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001627 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001628 }
1629
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001630 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001631 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001632 } else {
Chao Yu78682f72016-07-03 22:05:11 +08001633 struct bio *bio;
1634
1635 bio = f2fs_grab_bio(inode, blkaddr, 1);
1636 if (IS_ERR(bio)) {
1637 err = PTR_ERR(bio);
Jan Kara9234f312014-10-22 15:21:47 +02001638 goto fail;
Chao Yu78682f72016-07-03 22:05:11 +08001639 }
1640
1641 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
1642 bio_put(bio);
1643 err = -EFAULT;
1644 goto fail;
1645 }
1646
1647 __submit_bio(sbi, READ_SYNC, bio, DATA);
Chao Yud54c7952014-03-29 15:30:40 +08001648
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001649 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001650 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001651 err = -EIO;
1652 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001653 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001654 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001655 f2fs_put_page(page, 1);
1656 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001657 }
1658 }
Chao Yu90d43882015-07-08 18:24:38 +08001659out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001660 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001661out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001662 clear_cold_data(page);
1663 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001664
Chao Yu3aab8f82014-07-02 13:25:04 +08001665fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001666 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001667 f2fs_write_failed(mapping, pos + len);
1668 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001669}
1670
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001671static int f2fs_write_end(struct file *file,
1672 struct address_space *mapping,
1673 loff_t pos, unsigned len, unsigned copied,
1674 struct page *page, void *fsdata)
1675{
1676 struct inode *inode = page->mapping->host;
1677
Chao Yudfb2bf32014-05-06 16:47:23 +08001678 trace_f2fs_write_end(inode, pos, len, copied);
1679
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001680 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001681
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001682 if (pos + copied > i_size_read(inode))
1683 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001684
Chao Yu75c3c8b2013-11-16 14:15:59 +08001685 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001686 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001687 return copied;
1688}
1689
Omar Sandoval6f673762015-03-16 04:33:52 -07001690static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1691 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001692{
1693 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001694
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001695 if (offset & blocksize_mask)
1696 return -EINVAL;
1697
Al Viro5b46f252014-03-16 18:07:34 -04001698 if (iov_iter_alignment(iter) & blocksize_mask)
1699 return -EINVAL;
1700
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001701 return 0;
1702}
1703
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001704static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001705{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001706 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001707 struct inode *inode = mapping->host;
1708 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001709 loff_t offset = iocb->ki_pos;
Chao Yu3aab8f82014-07-02 13:25:04 +08001710 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001711
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001712 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001713 if (err)
1714 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001715
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001716 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1717 return 0;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001718 if (test_opt(F2FS_I_SB(inode), LFS))
1719 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001720
Omar Sandoval6f673762015-03-16 04:33:52 -07001721 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001722
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001723 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001724 if (iov_iter_rw(iter) == WRITE) {
1725 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001726 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001727 else if (err < 0)
1728 f2fs_write_failed(mapping, offset + count);
1729 }
Chao Yu70407fa2014-07-31 21:11:22 +08001730
Omar Sandoval6f673762015-03-16 04:33:52 -07001731 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001732
Chao Yu3aab8f82014-07-02 13:25:04 +08001733 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001734}
1735
Chao Yu487261f2015-02-05 17:44:29 +08001736void f2fs_invalidate_page(struct page *page, unsigned int offset,
1737 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001738{
1739 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001740 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001741
Chao Yu487261f2015-02-05 17:44:29 +08001742 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001743 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001744 return;
1745
Chao Yu487261f2015-02-05 17:44:29 +08001746 if (PageDirty(page)) {
1747 if (inode->i_ino == F2FS_META_INO(sbi))
1748 dec_page_count(sbi, F2FS_DIRTY_META);
1749 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1750 dec_page_count(sbi, F2FS_DIRTY_NODES);
1751 else
1752 inode_dec_dirty_pages(inode);
1753 }
Chao Yudecd36b2015-08-07 18:42:09 +08001754
1755 /* This is atomic written page, keep Private */
1756 if (IS_ATOMIC_WRITTEN_PAGE(page))
1757 return;
1758
Chao Yu23dc9742016-04-29 20:09:15 +08001759 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001760 ClearPagePrivate(page);
1761}
1762
Chao Yu487261f2015-02-05 17:44:29 +08001763int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001764{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001765 /* If this is dirty page, keep PagePrivate */
1766 if (PageDirty(page))
1767 return 0;
1768
Chao Yudecd36b2015-08-07 18:42:09 +08001769 /* This is atomic written page, keep Private */
1770 if (IS_ATOMIC_WRITTEN_PAGE(page))
1771 return 0;
1772
Chao Yu23dc9742016-04-29 20:09:15 +08001773 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001774 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001775 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001776}
1777
1778static int f2fs_set_data_page_dirty(struct page *page)
1779{
1780 struct address_space *mapping = page->mapping;
1781 struct inode *inode = mapping->host;
1782
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001783 trace_f2fs_set_page_dirty(page, DATA);
1784
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001785 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001786
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001787 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001788 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1789 register_inmem_page(inode, page);
1790 return 1;
1791 }
1792 /*
1793 * Previously, this page has been registered, we just
1794 * return here.
1795 */
1796 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001797 }
1798
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001799 if (!PageDirty(page)) {
1800 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001801 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001802 return 1;
1803 }
1804 return 0;
1805}
1806
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001807static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1808{
Chao Yu454ae7e2014-04-22 13:34:01 +08001809 struct inode *inode = mapping->host;
1810
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001811 if (f2fs_has_inline_data(inode))
1812 return 0;
1813
1814 /* make sure allocating whole blocks */
1815 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1816 filemap_write_and_wait(mapping);
1817
Chao Yue2b4e2b2015-08-19 19:11:19 +08001818 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001819}
1820
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001821const struct address_space_operations f2fs_dblock_aops = {
1822 .readpage = f2fs_read_data_page,
1823 .readpages = f2fs_read_data_pages,
1824 .writepage = f2fs_write_data_page,
1825 .writepages = f2fs_write_data_pages,
1826 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001827 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001828 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001829 .invalidatepage = f2fs_invalidate_page,
1830 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001831 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001832 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001833};