blob: ba4963f51beebdb14a9fd83ee8ca55e607351042 [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 Yu3104af32015-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 Yu3104af32015-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
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700958/*
959 * This function was originally taken from fs/mpage.c, and customized for f2fs.
960 * Major change was from block_size == page_size in f2fs by default.
961 */
962static int f2fs_mpage_readpages(struct address_space *mapping,
963 struct list_head *pages, struct page *page,
964 unsigned nr_pages)
965{
966 struct bio *bio = NULL;
967 unsigned page_idx;
968 sector_t last_block_in_bio = 0;
969 struct inode *inode = mapping->host;
970 const unsigned blkbits = inode->i_blkbits;
971 const unsigned blocksize = 1 << blkbits;
972 sector_t block_in_file;
973 sector_t last_block;
974 sector_t last_block_in_file;
975 sector_t block_nr;
976 struct block_device *bdev = inode->i_sb->s_bdev;
977 struct f2fs_map_blocks map;
978
979 map.m_pblk = 0;
980 map.m_lblk = 0;
981 map.m_len = 0;
982 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +0800983 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700984
985 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
986
987 prefetchw(&page->flags);
988 if (pages) {
989 page = list_entry(pages->prev, struct page, lru);
990 list_del(&page->lru);
991 if (add_to_page_cache_lru(page, mapping,
992 page->index, GFP_KERNEL))
993 goto next_page;
994 }
995
996 block_in_file = (sector_t)page->index;
997 last_block = block_in_file + nr_pages;
998 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
999 blkbits;
1000 if (last_block > last_block_in_file)
1001 last_block = last_block_in_file;
1002
1003 /*
1004 * Map blocks using the previous result first.
1005 */
1006 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1007 block_in_file > map.m_lblk &&
1008 block_in_file < (map.m_lblk + map.m_len))
1009 goto got_it;
1010
1011 /*
1012 * Then do more f2fs_map_blocks() calls until we are
1013 * done with this page.
1014 */
1015 map.m_flags = 0;
1016
1017 if (block_in_file < last_block) {
1018 map.m_lblk = block_in_file;
1019 map.m_len = last_block - block_in_file;
1020
Chao Yu46c9e142015-09-18 16:54:16 +08001021 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001022 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001023 goto set_error_page;
1024 }
1025got_it:
1026 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1027 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1028 SetPageMappedToDisk(page);
1029
1030 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1031 SetPageUptodate(page);
1032 goto confused;
1033 }
1034 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001035 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001036 SetPageUptodate(page);
1037 unlock_page(page);
1038 goto next_page;
1039 }
1040
1041 /*
1042 * This page will go to BIO. Do we need to send this
1043 * BIO off first?
1044 */
1045 if (bio && (last_block_in_bio != block_nr - 1)) {
1046submit_and_realloc:
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -07001047 __submit_bio(F2FS_I_SB(inode), READ, bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001048 bio = NULL;
1049 }
1050 if (bio == NULL) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001051 struct fscrypt_ctx *ctx = NULL;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001052
1053 if (f2fs_encrypted_inode(inode) &&
1054 S_ISREG(inode->i_mode)) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001055
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001056 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001057 if (IS_ERR(ctx))
1058 goto set_error_page;
1059
1060 /* wait the page to be moved by cleaning */
Chao Yu08b39fb2015-10-08 13:27:34 +08001061 f2fs_wait_on_encrypted_page_writeback(
1062 F2FS_I_SB(inode), block_nr);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001063 }
1064
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001065 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +02001066 min_t(int, nr_pages, BIO_MAX_PAGES));
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001067 if (!bio) {
1068 if (ctx)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001069 fscrypt_release_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001070 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001071 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001072 bio->bi_bdev = bdev;
1073 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +08001074 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001075 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001076 }
1077
1078 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1079 goto submit_and_realloc;
1080
1081 last_block_in_bio = block_nr;
1082 goto next_page;
1083set_error_page:
1084 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001085 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001086 unlock_page(page);
1087 goto next_page;
1088confused:
1089 if (bio) {
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -07001090 __submit_bio(F2FS_I_SB(inode), READ, bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001091 bio = NULL;
1092 }
1093 unlock_page(page);
1094next_page:
1095 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001096 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001097 }
1098 BUG_ON(pages && !list_empty(pages));
1099 if (bio)
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -07001100 __submit_bio(F2FS_I_SB(inode), READ, bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001101 return 0;
1102}
1103
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001104static int f2fs_read_data_page(struct file *file, struct page *page)
1105{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001106 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001107 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001108
Chao Yuc20e89c2014-05-06 16:53:08 +08001109 trace_f2fs_readpage(page, DATA);
1110
arter97e1c42042014-08-06 23:22:50 +09001111 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001112 if (f2fs_has_inline_data(inode))
1113 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001114 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001115 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001116 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001117}
1118
1119static int f2fs_read_data_pages(struct file *file,
1120 struct address_space *mapping,
1121 struct list_head *pages, unsigned nr_pages)
1122{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001123 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001124 struct page *page = list_entry(pages->prev, struct page, lru);
1125
1126 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001127
1128 /* If the file has inline data, skip readpages */
1129 if (f2fs_has_inline_data(inode))
1130 return 0;
1131
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001132 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001133}
1134
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001135int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001136{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001137 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001138 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001139 struct dnode_of_data dn;
1140 int err = 0;
1141
1142 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001143 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001144 if (err)
1145 return err;
1146
Chao Yu28bc1062016-02-06 14:40:34 +08001147 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001148
1149 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001150 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001151 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001152 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001153 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001154
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001155 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001156 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001157
1158 /* wait for GCed encrypted page writeback */
1159 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001160 fio->old_blkaddr);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001161retry_encrypt:
1162 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1163 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001164 if (IS_ERR(fio->encrypted_page)) {
1165 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e4482016-04-11 15:51:57 -07001166 if (err == -ENOMEM) {
1167 /* flush pending ios and wait for a while */
1168 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1169 congestion_wait(BLK_RW_ASYNC, HZ/50);
1170 gfp_flags |= __GFP_NOFAIL;
1171 err = 0;
1172 goto retry_encrypt;
1173 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001174 goto out_writepage;
1175 }
1176 }
1177
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001178 set_page_writeback(page);
1179
1180 /*
1181 * If current allocation needs SSR,
1182 * it had better in-place writes for updated data.
1183 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001184 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001185 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001186 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001187 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001188 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001189 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001190 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001191 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001192 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001193 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001194 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001195 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001196 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001197 }
1198out_writepage:
1199 f2fs_put_dnode(&dn);
1200 return err;
1201}
1202
1203static int f2fs_write_data_page(struct page *page,
1204 struct writeback_control *wbc)
1205{
1206 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001207 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001208 loff_t i_size = i_size_read(inode);
1209 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001210 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001211 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001212 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001213 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001214 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001215 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001216 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001217 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001218 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001219 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001220 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001221 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001222
Chao Yuecda0de2014-05-06 16:48:26 +08001223 trace_f2fs_writepage(page, DATA);
1224
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001225 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001226 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001227
1228 /*
1229 * If the offset is out-of-range of file size,
1230 * this page does not have to be written to disk.
1231 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001232 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001233 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001234 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001235
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001236 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001237write:
Chao Yucaf00472015-01-28 17:48:42 +08001238 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001239 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001240 if (f2fs_is_drop_cache(inode))
1241 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001242 /* we should not write 0'th page having journal header */
1243 if (f2fs_is_volatile_file(inode) && (!page->index ||
1244 (!wbc->for_reclaim &&
1245 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001246 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001247
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001248 /* we should bypass data pages to proceed the kworkder jobs */
1249 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001250 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001251 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001252 }
1253
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001254 /* Dentry blocks are controlled by checkpoint */
1255 if (S_ISDIR(inode->i_mode)) {
1256 err = do_write_data_page(&fio);
1257 goto done;
1258 }
1259
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001260 if (!wbc->for_reclaim)
1261 need_balance_fs = true;
1262 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001263 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001264
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001265 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001266 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001267 if (f2fs_has_inline_data(inode))
1268 err = f2fs_write_inline_data(inode, page);
1269 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001270 err = do_write_data_page(&fio);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001271 if (F2FS_I(inode)->last_disk_size < psize)
1272 F2FS_I(inode)->last_disk_size = psize;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001273 f2fs_unlock_op(sbi);
1274done:
1275 if (err && err != -ENOENT)
1276 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001277
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001278 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001279out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001280 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001281 if (err)
1282 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001283
1284 if (wbc->for_reclaim) {
1285 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001286 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001287 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001288
1289 unlock_page(page);
1290 f2fs_balance_fs(sbi, need_balance_fs);
1291
1292 if (unlikely(f2fs_cp_error(sbi)))
1293 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1294
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001295 return 0;
1296
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001297redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001298 redirty_page_for_writepage(wbc, page);
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001299 unlock_page(page);
1300 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001301}
1302
Chao Yu8f46dca2015-07-14 18:56:10 +08001303/*
1304 * This function was copied from write_cche_pages from mm/page-writeback.c.
1305 * The major change is making write step of cold data page separately from
1306 * warm/hot data page.
1307 */
1308static int f2fs_write_cache_pages(struct address_space *mapping,
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001309 struct writeback_control *wbc)
Chao Yu8f46dca2015-07-14 18:56:10 +08001310{
1311 int ret = 0;
1312 int done = 0;
1313 struct pagevec pvec;
1314 int nr_pages;
1315 pgoff_t uninitialized_var(writeback_index);
1316 pgoff_t index;
1317 pgoff_t end; /* Inclusive */
1318 pgoff_t done_index;
1319 int cycled;
1320 int range_whole = 0;
1321 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001322
1323 pagevec_init(&pvec, 0);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001324
Chao Yu8f46dca2015-07-14 18:56:10 +08001325 if (wbc->range_cyclic) {
1326 writeback_index = mapping->writeback_index; /* prev offset */
1327 index = writeback_index;
1328 if (index == 0)
1329 cycled = 1;
1330 else
1331 cycled = 0;
1332 end = -1;
1333 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001334 index = wbc->range_start >> PAGE_SHIFT;
1335 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001336 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1337 range_whole = 1;
1338 cycled = 1; /* ignore range_cyclic tests */
1339 }
1340 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1341 tag = PAGECACHE_TAG_TOWRITE;
1342 else
1343 tag = PAGECACHE_TAG_DIRTY;
1344retry:
1345 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1346 tag_pages_for_writeback(mapping, index, end);
1347 done_index = index;
1348 while (!done && (index <= end)) {
1349 int i;
1350
1351 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1352 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1353 if (nr_pages == 0)
1354 break;
1355
1356 for (i = 0; i < nr_pages; i++) {
1357 struct page *page = pvec.pages[i];
1358
1359 if (page->index > end) {
1360 done = 1;
1361 break;
1362 }
1363
1364 done_index = page->index;
1365
1366 lock_page(page);
1367
1368 if (unlikely(page->mapping != mapping)) {
1369continue_unlock:
1370 unlock_page(page);
1371 continue;
1372 }
1373
1374 if (!PageDirty(page)) {
1375 /* someone wrote it for us */
1376 goto continue_unlock;
1377 }
1378
Chao Yu8f46dca2015-07-14 18:56:10 +08001379 if (PageWriteback(page)) {
1380 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001381 f2fs_wait_on_page_writeback(page,
1382 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001383 else
1384 goto continue_unlock;
1385 }
1386
1387 BUG_ON(PageWriteback(page));
1388 if (!clear_page_dirty_for_io(page))
1389 goto continue_unlock;
1390
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001391 ret = mapping->a_ops->writepage(page, wbc);
Chao Yu8f46dca2015-07-14 18:56:10 +08001392 if (unlikely(ret)) {
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001393 done_index = page->index + 1;
1394 done = 1;
1395 break;
Chao Yu8f46dca2015-07-14 18:56:10 +08001396 }
1397
1398 if (--wbc->nr_to_write <= 0 &&
1399 wbc->sync_mode == WB_SYNC_NONE) {
1400 done = 1;
1401 break;
1402 }
1403 }
1404 pagevec_release(&pvec);
1405 cond_resched();
1406 }
1407
Chao Yu8f46dca2015-07-14 18:56:10 +08001408 if (!cycled && !done) {
1409 cycled = 1;
1410 index = 0;
1411 end = writeback_index - 1;
1412 goto retry;
1413 }
1414 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1415 mapping->writeback_index = done_index;
1416
1417 return ret;
1418}
1419
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001420static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001421 struct writeback_control *wbc)
1422{
1423 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001424 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001425 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001426
P J Pcfb185a2013-04-03 11:38:00 +09001427 /* deal with chardevs and other special file */
1428 if (!mapping->a_ops->writepage)
1429 return 0;
1430
Chao Yu6a290542015-07-17 18:02:39 +08001431 /* skip writing if there is no dirty page in this inode */
1432 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1433 return 0;
1434
Jaegeuk Kima1257022015-10-08 10:40:07 -07001435 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1436 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1437 available_free_memory(sbi, DIRTY_DENTS))
1438 goto skip_write;
1439
Chao Yud323d002015-10-27 09:53:45 +08001440 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001441 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001442 goto skip_write;
1443
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001444 /* during POR, we don't need to trigger writepage at all. */
1445 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1446 goto skip_write;
1447
Yunlei Hed31c7c32016-02-04 16:14:00 +08001448 trace_f2fs_writepages(mapping->host, wbc, DATA);
1449
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001450 ret = f2fs_write_cache_pages(mapping, wbc);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001451 /*
1452 * if some pages were truncated, we cannot guarantee its mapping->host
1453 * to detect pending bios.
1454 */
1455 f2fs_submit_merged_bio(sbi, DATA, WRITE);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001456
Chao Yuc227f912015-12-16 13:09:20 +08001457 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001458 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001459
1460skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001461 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001462 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001463 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001464}
1465
Chao Yu3aab8f82014-07-02 13:25:04 +08001466static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1467{
1468 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001469 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001470
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001471 if (to > i_size) {
1472 truncate_pagecache(inode, i_size);
1473 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001474 }
1475}
1476
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001477static int prepare_write_begin(struct f2fs_sb_info *sbi,
1478 struct page *page, loff_t pos, unsigned len,
1479 block_t *blk_addr, bool *node_changed)
1480{
1481 struct inode *inode = page->mapping->host;
1482 pgoff_t index = page->index;
1483 struct dnode_of_data dn;
1484 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001485 bool locked = false;
1486 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001487 int err = 0;
1488
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001489 /*
1490 * we already allocated all the blocks, so we don't need to get
1491 * the block addresses when there is no need to fill the page.
1492 */
1493 if (!f2fs_has_inline_data(inode) && !f2fs_encrypted_inode(inode) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001494 len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001495 return 0;
1496
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001497 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001498 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001499 f2fs_lock_op(sbi);
1500 locked = true;
1501 }
1502restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001503 /* check inline_data */
1504 ipage = get_node_page(sbi, inode->i_ino);
1505 if (IS_ERR(ipage)) {
1506 err = PTR_ERR(ipage);
1507 goto unlock_out;
1508 }
1509
1510 set_new_dnode(&dn, inode, ipage, ipage, 0);
1511
1512 if (f2fs_has_inline_data(inode)) {
1513 if (pos + len <= MAX_INLINE_DATA) {
1514 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001515 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001516 if (inode->i_nlink)
1517 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001518 } else {
1519 err = f2fs_convert_inline_page(&dn, page);
1520 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001521 goto out;
1522 if (dn.data_blkaddr == NULL_ADDR)
1523 err = f2fs_get_block(&dn, index);
1524 }
1525 } else if (locked) {
1526 err = f2fs_get_block(&dn, index);
1527 } else {
1528 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1529 dn.data_blkaddr = ei.blk + index - ei.fofs;
1530 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001531 /* hole case */
1532 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001533 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001534 f2fs_put_dnode(&dn);
1535 f2fs_lock_op(sbi);
1536 locked = true;
1537 goto restart;
1538 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001539 }
1540 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001541
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001542 /* convert_inline_page can make node_changed */
1543 *blk_addr = dn.data_blkaddr;
1544 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001545out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001546 f2fs_put_dnode(&dn);
1547unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001548 if (locked)
1549 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001550 return err;
1551}
1552
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001553static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1554 loff_t pos, unsigned len, unsigned flags,
1555 struct page **pagep, void **fsdata)
1556{
1557 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001558 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001559 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001560 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001561 bool need_balance = false;
1562 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001563 int err = 0;
1564
Chao Yu62aed042014-05-06 16:46:04 +08001565 trace_f2fs_write_begin(inode, pos, len, flags);
1566
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001567 /*
1568 * We should check this at this moment to avoid deadlock on inode page
1569 * and #0 page. The locking rule for inline_data conversion should be:
1570 * lock_page(page #0) -> lock_page(inode_page)
1571 */
1572 if (index != 0) {
1573 err = f2fs_convert_inline_inode(inode);
1574 if (err)
1575 goto fail;
1576 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001577repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001578 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001579 if (!page) {
1580 err = -ENOMEM;
1581 goto fail;
1582 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001583
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001584 *pagep = page;
1585
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001586 err = prepare_write_begin(sbi, page, pos, len,
1587 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001588 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001589 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001590
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001591 if (need_balance && has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001592 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001593 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001594 lock_page(page);
1595 if (page->mapping != mapping) {
1596 /* The page got truncated from under us */
1597 f2fs_put_page(page, 1);
1598 goto repeat;
1599 }
1600 }
1601
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001602 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001603
Chao Yu08b39fb2015-10-08 13:27:34 +08001604 /* wait for GCed encrypted page writeback */
1605 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001606 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001607
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001608 if (len == PAGE_SIZE)
Chao Yu90d43882015-07-08 18:24:38 +08001609 goto out_update;
1610 if (PageUptodate(page))
1611 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001612
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001613 if ((pos & PAGE_MASK) >= i_size_read(inode)) {
1614 unsigned start = pos & (PAGE_SIZE - 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001615 unsigned end = start + len;
1616
1617 /* Reading beyond i_size is simple: memset to zero */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001618 zero_user_segments(page, 0, start, end, PAGE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001619 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001620 }
1621
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001622 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001623 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001624 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001625 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001626 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001627 .type = DATA,
1628 .rw = READ_SYNC,
Chao Yu7a9d7542016-02-22 18:36:38 +08001629 .old_blkaddr = blkaddr,
1630 .new_blkaddr = blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001631 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001632 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001633 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001634 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001635 if (err)
1636 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001637
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001638 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001639 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001640 err = -EIO;
1641 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001642 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001643 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001644 f2fs_put_page(page, 1);
1645 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001646 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001647
1648 /* avoid symlink page */
1649 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001650 err = fscrypt_decrypt_page(page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001651 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001652 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001653 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001654 }
Chao Yu90d43882015-07-08 18:24:38 +08001655out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001656 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001657out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001658 clear_cold_data(page);
1659 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001660
Chao Yu3aab8f82014-07-02 13:25:04 +08001661fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001662 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001663 f2fs_write_failed(mapping, pos + len);
1664 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001665}
1666
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001667static int f2fs_write_end(struct file *file,
1668 struct address_space *mapping,
1669 loff_t pos, unsigned len, unsigned copied,
1670 struct page *page, void *fsdata)
1671{
1672 struct inode *inode = page->mapping->host;
1673
Chao Yudfb2bf32014-05-06 16:47:23 +08001674 trace_f2fs_write_end(inode, pos, len, copied);
1675
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001676 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001677
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001678 if (pos + copied > i_size_read(inode))
1679 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001680
Chao Yu75c3c8b2013-11-16 14:15:59 +08001681 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001682 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001683 return copied;
1684}
1685
Omar Sandoval6f673762015-03-16 04:33:52 -07001686static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1687 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001688{
1689 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001690
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001691 if (offset & blocksize_mask)
1692 return -EINVAL;
1693
Al Viro5b46f252014-03-16 18:07:34 -04001694 if (iov_iter_alignment(iter) & blocksize_mask)
1695 return -EINVAL;
1696
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001697 return 0;
1698}
1699
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001700static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001701{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001702 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001703 struct inode *inode = mapping->host;
1704 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001705 loff_t offset = iocb->ki_pos;
Chao Yu3aab8f82014-07-02 13:25:04 +08001706 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001707
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001708 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001709 if (err)
1710 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001711
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001712 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1713 return 0;
Jaegeuk Kim36abef42016-06-03 19:29:38 -07001714 if (test_opt(F2FS_I_SB(inode), LFS))
1715 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001716
Omar Sandoval6f673762015-03-16 04:33:52 -07001717 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fad2014-07-31 21:11:22 +08001718
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001719 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001720 if (iov_iter_rw(iter) == WRITE) {
1721 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001722 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001723 else if (err < 0)
1724 f2fs_write_failed(mapping, offset + count);
1725 }
Chao Yu70407fad2014-07-31 21:11:22 +08001726
Omar Sandoval6f673762015-03-16 04:33:52 -07001727 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fad2014-07-31 21:11:22 +08001728
Chao Yu3aab8f82014-07-02 13:25:04 +08001729 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001730}
1731
Chao Yu487261f2015-02-05 17:44:29 +08001732void f2fs_invalidate_page(struct page *page, unsigned int offset,
1733 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001734{
1735 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001736 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001737
Chao Yu487261f2015-02-05 17:44:29 +08001738 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001739 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001740 return;
1741
Chao Yu487261f2015-02-05 17:44:29 +08001742 if (PageDirty(page)) {
1743 if (inode->i_ino == F2FS_META_INO(sbi))
1744 dec_page_count(sbi, F2FS_DIRTY_META);
1745 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1746 dec_page_count(sbi, F2FS_DIRTY_NODES);
1747 else
1748 inode_dec_dirty_pages(inode);
1749 }
Chao Yudecd36b2015-08-07 18:42:09 +08001750
1751 /* This is atomic written page, keep Private */
1752 if (IS_ATOMIC_WRITTEN_PAGE(page))
1753 return;
1754
Chao Yu23dc9742016-04-29 20:09:15 +08001755 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001756 ClearPagePrivate(page);
1757}
1758
Chao Yu487261f2015-02-05 17:44:29 +08001759int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001760{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001761 /* If this is dirty page, keep PagePrivate */
1762 if (PageDirty(page))
1763 return 0;
1764
Chao Yudecd36b2015-08-07 18:42:09 +08001765 /* This is atomic written page, keep Private */
1766 if (IS_ATOMIC_WRITTEN_PAGE(page))
1767 return 0;
1768
Chao Yu23dc9742016-04-29 20:09:15 +08001769 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001770 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001771 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001772}
1773
1774static int f2fs_set_data_page_dirty(struct page *page)
1775{
1776 struct address_space *mapping = page->mapping;
1777 struct inode *inode = mapping->host;
1778
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001779 trace_f2fs_set_page_dirty(page, DATA);
1780
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001781 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001782
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001783 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001784 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1785 register_inmem_page(inode, page);
1786 return 1;
1787 }
1788 /*
1789 * Previously, this page has been registered, we just
1790 * return here.
1791 */
1792 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001793 }
1794
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001795 if (!PageDirty(page)) {
1796 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001797 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001798 return 1;
1799 }
1800 return 0;
1801}
1802
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001803static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1804{
Chao Yu454ae7e2014-04-22 13:34:01 +08001805 struct inode *inode = mapping->host;
1806
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001807 if (f2fs_has_inline_data(inode))
1808 return 0;
1809
1810 /* make sure allocating whole blocks */
1811 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1812 filemap_write_and_wait(mapping);
1813
Chao Yue2b4e2b2015-08-19 19:11:19 +08001814 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001815}
1816
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001817const struct address_space_operations f2fs_dblock_aops = {
1818 .readpage = f2fs_read_data_page,
1819 .readpages = f2fs_read_data_pages,
1820 .writepage = f2fs_write_data_page,
1821 .writepages = f2fs_write_data_pages,
1822 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001823 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001824 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001825 .invalidatepage = f2fs_invalidate_page,
1826 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001827 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001828 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001829};