blob: a3dea51f4702c25d5d2af68db04469b33d971d1c [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,
101 struct bio *bio)
102{
103 if (!is_read_io(rw))
104 atomic_inc(&sbi->nr_wb_bios);
105 submit_bio(rw, bio);
106}
107
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900108static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900109{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900110 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900111
112 if (!io->bio)
113 return;
114
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700115 if (is_read_io(fio->rw))
Chao Yu2ace38e2014-12-24 16:08:14 +0800116 trace_f2fs_submit_read_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim6a8f8ca2014-10-29 14:37:22 -0700117 else
Chao Yu2ace38e2014-12-24 16:08:14 +0800118 trace_f2fs_submit_write_bio(io->sbi->sb, fio, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900119
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700120 __submit_bio(io->sbi, fio->rw, io->bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900121 io->bio = NULL;
122}
123
Chao Yu0c3a5792016-01-18 18:28:11 +0800124static bool __has_merged_page(struct f2fs_bio_info *io, struct inode *inode,
125 struct page *page, nid_t ino)
Chao Yu0fd785e2016-01-18 18:24:59 +0800126{
Chao Yu0fd785e2016-01-18 18:24:59 +0800127 struct bio_vec *bvec;
128 struct page *target;
129 int i;
130
Chao Yu0c3a5792016-01-18 18:28:11 +0800131 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800132 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800133
134 if (!inode && !page && !ino)
135 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800136
137 bio_for_each_segment_all(bvec, io->bio, i) {
138
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700139 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800140 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700141 else
142 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800143
Chao Yu0c3a5792016-01-18 18:28:11 +0800144 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800145 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800146 if (page && page == target)
147 return true;
148 if (ino && ino == ino_of_node(target))
149 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800150 }
151
Chao Yu0fd785e2016-01-18 18:24:59 +0800152 return false;
153}
154
Chao Yu0c3a5792016-01-18 18:28:11 +0800155static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
156 struct page *page, nid_t ino,
157 enum page_type type)
158{
159 enum page_type btype = PAGE_TYPE_OF_BIO(type);
160 struct f2fs_bio_info *io = &sbi->write_io[btype];
161 bool ret;
162
163 down_read(&io->io_rwsem);
164 ret = __has_merged_page(io, inode, page, ino);
165 up_read(&io->io_rwsem);
166 return ret;
167}
168
169static void __f2fs_submit_merged_bio(struct f2fs_sb_info *sbi,
170 struct inode *inode, struct page *page,
171 nid_t ino, enum page_type type, int rw)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900172{
173 enum page_type btype = PAGE_TYPE_OF_BIO(type);
174 struct f2fs_bio_info *io;
175
176 io = is_read_io(rw) ? &sbi->read_io : &sbi->write_io[btype];
177
Chao Yudf0f8dc2014-03-22 14:57:23 +0800178 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900179
Chao Yu0c3a5792016-01-18 18:28:11 +0800180 if (!__has_merged_page(io, inode, page, ino))
181 goto out;
182
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900183 /* change META to META_FLUSH in the checkpoint procedure */
184 if (type >= META_FLUSH) {
185 io->fio.type = META_FLUSH;
Jaegeuk Kim0f7b2ab2014-07-23 09:57:31 -0700186 if (test_opt(sbi, NOBARRIER))
187 io->fio.rw = WRITE_FLUSH | REQ_META | REQ_PRIO;
188 else
189 io->fio.rw = WRITE_FLUSH_FUA | REQ_META | REQ_PRIO;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900190 }
191 __submit_merged_bio(io);
Chao Yu0c3a5792016-01-18 18:28:11 +0800192out:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800193 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900194}
195
Chao Yu0c3a5792016-01-18 18:28:11 +0800196void f2fs_submit_merged_bio(struct f2fs_sb_info *sbi, enum page_type type,
197 int rw)
198{
199 __f2fs_submit_merged_bio(sbi, NULL, NULL, 0, type, rw);
200}
201
202void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *sbi,
203 struct inode *inode, struct page *page,
204 nid_t ino, enum page_type type, int rw)
205{
206 if (has_merged_page(sbi, inode, page, ino, type))
207 __f2fs_submit_merged_bio(sbi, inode, page, ino, type, rw);
208}
209
Chao Yu406657d2016-02-24 17:17:55 +0800210void f2fs_flush_merged_bios(struct f2fs_sb_info *sbi)
211{
212 f2fs_submit_merged_bio(sbi, DATA, WRITE);
213 f2fs_submit_merged_bio(sbi, NODE, WRITE);
214 f2fs_submit_merged_bio(sbi, META, WRITE);
215}
216
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900217/*
218 * Fill the locked page with data located in the block address.
219 * Return unlocked page.
220 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700221int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900222{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900223 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700224 struct page *page = fio->encrypted_page ?
225 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900226
Chao Yu2ace38e2014-12-24 16:08:14 +0800227 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700228 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900229
230 /* Allocate a new bio */
Chao Yu7a9d7542016-02-22 18:36:38 +0800231 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->rw));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900232
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900234 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900235 return -EFAULT;
236 }
237
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700238 __submit_bio(fio->sbi, fio->rw, bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900239 return 0;
240}
241
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700242void f2fs_submit_page_mbio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900243{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700244 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900245 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900246 struct f2fs_bio_info *io;
Gu Zheng940a6d32013-12-20 17:39:59 +0800247 bool is_read = is_read_io(fio->rw);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700248 struct page *bio_page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900249
Gu Zheng940a6d32013-12-20 17:39:59 +0800250 io = is_read ? &sbi->read_io : &sbi->write_io[btype];
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900251
Chao Yu7a9d7542016-02-22 18:36:38 +0800252 if (fio->old_blkaddr != NEW_ADDR)
253 verify_block_addr(sbi, fio->old_blkaddr);
254 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900255
Chao Yudf0f8dc2014-03-22 14:57:23 +0800256 down_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900257
Chao Yu7a9d7542016-02-22 18:36:38 +0800258 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900259 io->fio.rw != fio->rw))
260 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900261alloc_new:
262 if (io->bio == NULL) {
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700263 int bio_blocks = MAX_BIO_BLOCKS(sbi);
Gu Zheng940a6d32013-12-20 17:39:59 +0800264
Chao Yu7a9d7542016-02-22 18:36:38 +0800265 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
266 bio_blocks, is_read);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900267 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900268 }
269
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700270 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
271
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300272 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) <
273 PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900274 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900275 goto alloc_new;
276 }
277
Chao Yu7a9d7542016-02-22 18:36:38 +0800278 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700279 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900280
Chao Yudf0f8dc2014-03-22 14:57:23 +0800281 up_write(&io->io_rwsem);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700282 trace_f2fs_submit_page_mbio(fio->page, fio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900283}
284
Chao Yu46008c62016-05-09 19:56:30 +0800285static void __set_data_blkaddr(struct dnode_of_data *dn)
286{
287 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
288 __le32 *addr_array;
289
290 /* Get physical address of data block */
291 addr_array = blkaddr_in_node(rn);
292 addr_array[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
293}
294
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900295/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900296 * Lock ordering for the change of data block address:
297 * ->data_page
298 * ->node_page
299 * update block addresses in the node page
300 */
Chao Yu216a6202015-03-19 19:23:32 +0800301void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900302{
Chao Yu46008c62016-05-09 19:56:30 +0800303 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
304 __set_data_blkaddr(dn);
305 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800306 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900307}
308
Chao Yuf28b3432016-02-24 17:16:47 +0800309void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
310{
311 dn->data_blkaddr = blkaddr;
312 set_data_blkaddr(dn);
313 f2fs_update_extent_cache(dn);
314}
315
Chao Yu46008c62016-05-09 19:56:30 +0800316/* dn->ofs_in_node will be returned with up-to-date last block pointer */
317int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900318{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700319 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900320
Chao Yu46008c62016-05-09 19:56:30 +0800321 if (!count)
322 return 0;
323
Jaegeuk Kim91942322016-05-20 10:13:22 -0700324 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900325 return -EPERM;
Chao Yu46008c62016-05-09 19:56:30 +0800326 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900327 return -ENOSPC;
328
Chao Yu46008c62016-05-09 19:56:30 +0800329 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
330 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900331
Chao Yu46008c62016-05-09 19:56:30 +0800332 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
333
334 for (; count > 0; dn->ofs_in_node++) {
335 block_t blkaddr =
336 datablock_addr(dn->node_page, dn->ofs_in_node);
337 if (blkaddr == NULL_ADDR) {
338 dn->data_blkaddr = NEW_ADDR;
339 __set_data_blkaddr(dn);
340 count--;
341 }
342 }
343
344 if (set_page_dirty(dn->node_page))
345 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900346 return 0;
347}
348
Chao Yu46008c62016-05-09 19:56:30 +0800349/* Should keep dn->ofs_in_node unchanged */
350int reserve_new_block(struct dnode_of_data *dn)
351{
352 unsigned int ofs_in_node = dn->ofs_in_node;
353 int ret;
354
355 ret = reserve_new_blocks(dn, 1);
356 dn->ofs_in_node = ofs_in_node;
357 return ret;
358}
359
Huajun Lib6009652013-11-10 23:13:18 +0800360int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
361{
362 bool need_put = dn->inode_page ? false : true;
363 int err;
364
365 err = get_dnode_of_data(dn, index, ALLOC_NODE);
366 if (err)
367 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900368
Huajun Lib6009652013-11-10 23:13:18 +0800369 if (dn->data_blkaddr == NULL_ADDR)
370 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900371 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800372 f2fs_put_dnode(dn);
373 return err;
374}
375
Fan Li759af1c2015-08-05 15:52:16 +0800376int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900377{
Chao Yu028a41e2015-03-19 19:26:02 +0800378 struct extent_info ei;
Fan Li759af1c2015-08-05 15:52:16 +0800379 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800380
Fan Li759af1c2015-08-05 15:52:16 +0800381 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
382 dn->data_blkaddr = ei.blk + index - ei.fofs;
383 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800384 }
385
Fan Li759af1c2015-08-05 15:52:16 +0800386 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900387}
388
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700389struct page *get_read_data_page(struct inode *inode, pgoff_t index,
390 int rw, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900391{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900392 struct address_space *mapping = inode->i_mapping;
393 struct dnode_of_data dn;
394 struct page *page;
Chao Yucb3bc9e2015-02-05 18:03:40 +0800395 struct extent_info ei;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900396 int err;
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800397 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700398 .sbi = F2FS_I_SB(inode),
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800399 .type = DATA,
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700400 .rw = rw,
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700401 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -0800402 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900403
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700404 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
405 return read_mapping_page(mapping, index, NULL);
406
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700407 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900408 if (!page)
409 return ERR_PTR(-ENOMEM);
410
Chao Yucb3bc9e2015-02-05 18:03:40 +0800411 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
412 dn.data_blkaddr = ei.blk + index - ei.fofs;
413 goto got_it;
414 }
415
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900416 set_new_dnode(&dn, inode, NULL, NULL, 0);
417 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700418 if (err)
419 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900420 f2fs_put_dnode(&dn);
421
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900422 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700423 err = -ENOENT;
424 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900425 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800426got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700427 if (PageUptodate(page)) {
428 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900429 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700430 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900431
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900432 /*
433 * A new dentry page is allocated but not able to be written, since its
434 * new inode page couldn't be allocated due to -ENOSPC.
435 * In such the case, its blkaddr can be remained as NEW_ADDR.
436 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
437 */
438 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300439 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900440 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700441 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900442 return page;
443 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900444
Chao Yu7a9d7542016-02-22 18:36:38 +0800445 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700446 fio.page = page;
447 err = f2fs_submit_page_bio(&fio);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900448 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700449 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700450 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700451
452put_err:
453 f2fs_put_page(page, 1);
454 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700455}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900456
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700457struct page *find_data_page(struct inode *inode, pgoff_t index)
458{
459 struct address_space *mapping = inode->i_mapping;
460 struct page *page;
461
462 page = find_get_page(mapping, index);
463 if (page && PageUptodate(page))
464 return page;
465 f2fs_put_page(page, 0);
466
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700467 page = get_read_data_page(inode, index, READ_SYNC, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700468 if (IS_ERR(page))
469 return page;
470
471 if (PageUptodate(page))
472 return page;
473
474 wait_on_page_locked(page);
475 if (unlikely(!PageUptodate(page))) {
476 f2fs_put_page(page, 0);
477 return ERR_PTR(-EIO);
478 }
479 return page;
480}
481
482/*
483 * If it tries to access a hole, return an error.
484 * Because, the callers, functions in dir.c and GC, should be able to know
485 * whether this page exists or not.
486 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700487struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
488 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700489{
490 struct address_space *mapping = inode->i_mapping;
491 struct page *page;
492repeat:
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700493 page = get_read_data_page(inode, index, READ_SYNC, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700494 if (IS_ERR(page))
495 return page;
496
497 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900498 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900499 if (unlikely(!PageUptodate(page))) {
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900500 f2fs_put_page(page, 1);
501 return ERR_PTR(-EIO);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900502 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900503 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900504 f2fs_put_page(page, 1);
505 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900506 }
507 return page;
508}
509
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900510/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900511 * Caller ensures that this data page is never allocated.
512 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900513 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800514 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
515 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800516 * Note that, ipage is set only by make_empty_dir, and if any error occur,
517 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900518 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900519struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900520 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900521{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900522 struct address_space *mapping = inode->i_mapping;
523 struct page *page;
524 struct dnode_of_data dn;
525 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800526
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700527 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800528 if (!page) {
529 /*
530 * before exiting, we should make sure ipage will be released
531 * if any error occur.
532 */
533 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700534 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800535 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900536
Jaegeuk Kima8865372013-12-27 17:04:17 +0900537 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800538 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700539 if (err) {
540 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900541 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900542 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700543 if (!ipage)
544 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900545
546 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700547 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900548
549 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300550 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900551 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900552 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700553 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900554
Jaegeuk Kim76121182016-01-01 22:03:47 -0800555 /* if ipage exists, blkaddr should be NEW_ADDR */
556 f2fs_bug_on(F2FS_I_SB(inode), ipage);
557 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700558 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800559 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900560 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700561got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800562 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700563 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700564 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900565 return page;
566}
567
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900568static int __allocate_data_block(struct dnode_of_data *dn)
569{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700570 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900571 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900572 struct node_info ni;
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800573 int seg = CURSEG_WARM_DATA;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700574 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800575 blkcnt_t count = 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900576
Jaegeuk Kim91942322016-05-20 10:13:22 -0700577 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900578 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800579
580 dn->data_blkaddr = datablock_addr(dn->node_page, dn->ofs_in_node);
581 if (dn->data_blkaddr == NEW_ADDR)
582 goto alloc;
583
Chao Yu46008c62016-05-09 19:56:30 +0800584 if (unlikely(!inc_valid_block_count(sbi, dn->inode, &count)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900585 return -ENOSPC;
586
Chao Yudf6136e2015-03-23 10:33:37 +0800587alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900588 get_node_info(sbi, dn->nid, &ni);
589 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
590
Jaegeuk Kim38aa0882015-01-05 16:02:20 -0800591 if (dn->ofs_in_node == 0 && dn->inode_page == dn->node_page)
592 seg = CURSEG_DIRECT_IO;
593
Chao Yudf6136e2015-03-23 10:33:37 +0800594 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
595 &sum, seg);
Chao Yu216a6202015-03-19 19:23:32 +0800596 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900597
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700598 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800599 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700600 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300601 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700602 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300603 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900604 return 0;
605}
606
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800607ssize_t f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800608{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800609 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800610 struct f2fs_map_blocks map;
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800611 ssize_t ret = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800612
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700613 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
614 map.m_len = F2FS_BYTES_TO_BLK(iov_iter_count(from));
Chao Yuda859852016-01-26 15:42:58 +0800615 map.m_next_pgofs = NULL;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800616
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800617 if (f2fs_encrypted_inode(inode))
618 return 0;
619
620 if (iocb->ki_flags & IOCB_DIRECT) {
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800621 ret = f2fs_convert_inline_inode(inode);
622 if (ret)
623 return ret;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800624 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_DIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800625 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800626 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA) {
627 ret = f2fs_convert_inline_inode(inode);
628 if (ret)
629 return ret;
630 }
631 if (!f2fs_has_inline_data(inode))
632 return f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800633 return ret;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800634}
635
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900636/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700637 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
638 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800639 * If original data blocks are allocated, then give them to blockdev.
640 * Otherwise,
641 * a. preallocate requested block addresses
642 * b. do not use extent cache for better performance
643 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900644 */
Chao Yud323d002015-10-27 09:53:45 +0800645int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800646 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900647{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700648 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900649 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800650 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900651 int mode = create ? ALLOC_NODE : LOOKUP_NODE_RA;
Chao Yu46008c62016-05-09 19:56:30 +0800652 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900653 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800654 unsigned int ofs_in_node, last_ofs_in_node;
655 blkcnt_t prealloc;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800656 struct extent_info ei;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900657 bool allocated = false;
Fan Li7df3a432015-12-17 13:20:59 +0800658 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900659
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700660 map->m_len = 0;
661 map->m_flags = 0;
662
663 /* it only supports block size == page size */
664 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800665 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900666
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800667 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700668 map->m_pblk = ei.blk + pgofs - ei.fofs;
669 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
670 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900671 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800672 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900673
Chao Yu4fe71e82016-01-26 15:37:38 +0800674next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800675 if (create)
Chao Yu3104af352015-12-23 17:11:43 +0800676 f2fs_lock_op(sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900677
678 /* When reading holes, we need its node page */
679 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900680 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900681 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800682 if (flag == F2FS_GET_BLOCK_BMAP)
683 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800684 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900685 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800686 if (map->m_next_pgofs)
687 *map->m_next_pgofs =
688 get_next_page_offset(&dn, pgofs);
689 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900690 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900691 }
Chao Yu973163f2015-09-18 16:51:51 +0800692
Chao Yu46008c62016-05-09 19:56:30 +0800693 prealloc = 0;
694 ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800695 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900696
Chao Yu4fe71e82016-01-26 15:37:38 +0800697next_block:
Fan Li7df3a432015-12-17 13:20:59 +0800698 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800699
Fan Li7df3a432015-12-17 13:20:59 +0800700 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
701 if (create) {
702 if (unlikely(f2fs_cp_error(sbi))) {
703 err = -EIO;
704 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800705 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800706 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800707 if (blkaddr == NULL_ADDR) {
708 prealloc++;
709 last_ofs_in_node = dn.ofs_in_node;
710 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800711 } else {
712 err = __allocate_data_block(&dn);
Chao Yu46008c62016-05-09 19:56:30 +0800713 if (!err) {
Jaegeuk Kim91942322016-05-20 10:13:22 -0700714 set_inode_flag(inode, FI_APPEND_WRITE);
Chao Yu46008c62016-05-09 19:56:30 +0800715 allocated = true;
716 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800717 }
Fan Li7df3a432015-12-17 13:20:59 +0800718 if (err)
719 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800720 map->m_flags = F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800721 blkaddr = dn.data_blkaddr;
722 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800723 if (flag == F2FS_GET_BLOCK_BMAP) {
724 map->m_pblk = 0;
725 goto sync_out;
726 }
Chao Yuda859852016-01-26 15:42:58 +0800727 if (flag == F2FS_GET_BLOCK_FIEMAP &&
728 blkaddr == NULL_ADDR) {
729 if (map->m_next_pgofs)
730 *map->m_next_pgofs = pgofs + 1;
731 }
Fan Li7df3a432015-12-17 13:20:59 +0800732 if (flag != F2FS_GET_BLOCK_FIEMAP ||
Chao Yu43473f92016-05-05 19:13:02 +0800733 blkaddr != NEW_ADDR)
Fan Li7df3a432015-12-17 13:20:59 +0800734 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900735 }
736 }
Fan Li7df3a432015-12-17 13:20:59 +0800737
Chao Yu46008c62016-05-09 19:56:30 +0800738 if (flag == F2FS_GET_BLOCK_PRE_AIO)
739 goto skip;
740
Chao Yu4fe71e82016-01-26 15:37:38 +0800741 if (map->m_len == 0) {
742 /* preallocated unwritten block should be mapped for fiemap. */
743 if (blkaddr == NEW_ADDR)
744 map->m_flags |= F2FS_MAP_UNWRITTEN;
745 map->m_flags |= F2FS_MAP_MAPPED;
746
747 map->m_pblk = blkaddr;
748 map->m_len = 1;
749 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +0800750 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800751 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +0800752 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +0800753 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +0800754 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +0800755 } else {
756 goto sync_out;
757 }
758
Chao Yu46008c62016-05-09 19:56:30 +0800759skip:
Chao Yu4fe71e82016-01-26 15:37:38 +0800760 dn.ofs_in_node++;
761 pgofs++;
762
Chao Yu46008c62016-05-09 19:56:30 +0800763 /* preallocate blocks in batch for one dnode page */
764 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
765 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +0800766
Chao Yu46008c62016-05-09 19:56:30 +0800767 dn.ofs_in_node = ofs_in_node;
768 err = reserve_new_blocks(&dn, prealloc);
769 if (err)
770 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800771
Chao Yu46008c62016-05-09 19:56:30 +0800772 map->m_len += dn.ofs_in_node - ofs_in_node;
773 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
774 err = -ENOSPC;
775 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +0800776 }
Chao Yu46008c62016-05-09 19:56:30 +0800777 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +0800778 }
779
Chao Yu46008c62016-05-09 19:56:30 +0800780 if (pgofs >= end)
781 goto sync_out;
782 else if (dn.ofs_in_node < end_offset)
783 goto next_block;
784
Chao Yu46008c62016-05-09 19:56:30 +0800785 f2fs_put_dnode(&dn);
786
787 if (create) {
788 f2fs_unlock_op(sbi);
789 f2fs_balance_fs(sbi, allocated);
790 }
791 allocated = false;
792 goto next_dnode;
793
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900794sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900795 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900796unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800797 if (create) {
Chao Yu3104af352015-12-23 17:11:43 +0800798 f2fs_unlock_op(sbi);
Jaegeuk Kim3c082b72016-01-23 13:35:18 -0800799 f2fs_balance_fs(sbi, allocated);
Jaegeuk Kim2a340762015-12-22 13:23:35 -0800800 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900801out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700802 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900803 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900804}
805
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700806static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800807 struct buffer_head *bh, int create, int flag,
808 pgoff_t *next_pgofs)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700809{
810 struct f2fs_map_blocks map;
811 int ret;
812
813 map.m_lblk = iblock;
814 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +0800815 map.m_next_pgofs = next_pgofs;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700816
Chao Yue2b4e2b2015-08-19 19:11:19 +0800817 ret = f2fs_map_blocks(inode, &map, create, flag);
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700818 if (!ret) {
819 map_bh(bh, inode->i_sb, map.m_pblk);
820 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
821 bh->b_size = map.m_len << inode->i_blkbits;
822 }
823 return ret;
824}
825
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900826static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +0800827 struct buffer_head *bh_result, int create, int flag,
828 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900829{
Chao Yuda859852016-01-26 15:42:58 +0800830 return __get_data_block(inode, iblock, bh_result, create,
831 flag, next_pgofs);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900832}
833
Chao Yue2b4e2b2015-08-19 19:11:19 +0800834static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900835 struct buffer_head *bh_result, int create)
836{
Chao Yue2b4e2b2015-08-19 19:11:19 +0800837 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800838 F2FS_GET_BLOCK_DIO, NULL);
Chao Yue2b4e2b2015-08-19 19:11:19 +0800839}
840
841static int get_data_block_bmap(struct inode *inode, sector_t iblock,
842 struct buffer_head *bh_result, int create)
843{
Yunlei He179448b2015-12-28 21:48:32 +0800844 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +0800845 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +0800846 return -EFBIG;
847
Chao Yue2b4e2b2015-08-19 19:11:19 +0800848 return __get_data_block(inode, iblock, bh_result, create,
Chao Yuda859852016-01-26 15:42:58 +0800849 F2FS_GET_BLOCK_BMAP, NULL);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +0900850}
851
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700852static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
853{
854 return (offset >> inode->i_blkbits);
855}
856
857static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
858{
859 return (blk << inode->i_blkbits);
860}
861
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900862int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
863 u64 start, u64 len)
864{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700865 struct buffer_head map_bh;
866 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +0800867 pgoff_t next_pgofs;
Fan Lide1475c2016-01-04 15:56:50 +0800868 loff_t isize;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700869 u64 logical = 0, phys = 0, size = 0;
870 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700871 int ret = 0;
872
873 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
874 if (ret)
875 return ret;
876
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -0700877 if (f2fs_has_inline_data(inode)) {
878 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
879 if (ret != -EAGAIN)
880 return ret;
881 }
882
Al Viro59551022016-01-22 15:40:57 -0500883 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +0800884
885 isize = i_size_read(inode);
Fan Li9a950d52015-12-26 18:07:41 +0800886 if (start >= isize)
887 goto out;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700888
Fan Li9a950d52015-12-26 18:07:41 +0800889 if (start + len > isize)
890 len = isize - start;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700891
892 if (logical_to_blk(inode, len) == 0)
893 len = blk_to_logical(inode, 1);
894
895 start_blk = logical_to_blk(inode, start);
896 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +0800897
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700898next:
899 memset(&map_bh, 0, sizeof(struct buffer_head));
900 map_bh.b_size = len;
901
Chao Yue2b4e2b2015-08-19 19:11:19 +0800902 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +0800903 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700904 if (ret)
905 goto out;
906
907 /* HOLE */
908 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +0800909 start_blk = next_pgofs;
Fan Li9a950d52015-12-26 18:07:41 +0800910 /* Go through holes util pass the EOF */
Chao Yuda859852016-01-26 15:42:58 +0800911 if (blk_to_logical(inode, start_blk) < isize)
Fan Li9a950d52015-12-26 18:07:41 +0800912 goto prep_next;
913 /* Found a hole beyond isize means no more extents.
914 * Note that the premise is that filesystems don't
915 * punch holes beyond isize and keep size unchanged.
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700916 */
Fan Li9a950d52015-12-26 18:07:41 +0800917 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700918 }
Fan Li9a950d52015-12-26 18:07:41 +0800919
Chao Yuda5af122016-01-08 20:19:27 +0800920 if (size) {
921 if (f2fs_encrypted_inode(inode))
922 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
923
Fan Li9a950d52015-12-26 18:07:41 +0800924 ret = fiemap_fill_next_extent(fieinfo, logical,
925 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +0800926 }
Fan Li9a950d52015-12-26 18:07:41 +0800927
928 if (start_blk > last_blk || ret)
929 goto out;
930
931 logical = blk_to_logical(inode, start_blk);
932 phys = blk_to_logical(inode, map_bh.b_blocknr);
933 size = map_bh.b_size;
934 flags = 0;
935 if (buffer_unwritten(&map_bh))
936 flags = FIEMAP_EXTENT_UNWRITTEN;
937
938 start_blk += logical_to_blk(inode, size);
939
940prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700941 cond_resched();
942 if (fatal_signal_pending(current))
943 ret = -EINTR;
944 else
945 goto next;
946out:
947 if (ret == 1)
948 ret = 0;
949
Al Viro59551022016-01-22 15:40:57 -0500950 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -0700951 return ret;
Jaegeuk Kim9ab701342014-06-08 04:30:14 +0900952}
953
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700954/*
955 * This function was originally taken from fs/mpage.c, and customized for f2fs.
956 * Major change was from block_size == page_size in f2fs by default.
957 */
958static int f2fs_mpage_readpages(struct address_space *mapping,
959 struct list_head *pages, struct page *page,
960 unsigned nr_pages)
961{
962 struct bio *bio = NULL;
963 unsigned page_idx;
964 sector_t last_block_in_bio = 0;
965 struct inode *inode = mapping->host;
966 const unsigned blkbits = inode->i_blkbits;
967 const unsigned blocksize = 1 << blkbits;
968 sector_t block_in_file;
969 sector_t last_block;
970 sector_t last_block_in_file;
971 sector_t block_nr;
972 struct block_device *bdev = inode->i_sb->s_bdev;
973 struct f2fs_map_blocks map;
974
975 map.m_pblk = 0;
976 map.m_lblk = 0;
977 map.m_len = 0;
978 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +0800979 map.m_next_pgofs = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -0700980
981 for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
982
983 prefetchw(&page->flags);
984 if (pages) {
985 page = list_entry(pages->prev, struct page, lru);
986 list_del(&page->lru);
987 if (add_to_page_cache_lru(page, mapping,
988 page->index, GFP_KERNEL))
989 goto next_page;
990 }
991
992 block_in_file = (sector_t)page->index;
993 last_block = block_in_file + nr_pages;
994 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
995 blkbits;
996 if (last_block > last_block_in_file)
997 last_block = last_block_in_file;
998
999 /*
1000 * Map blocks using the previous result first.
1001 */
1002 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1003 block_in_file > map.m_lblk &&
1004 block_in_file < (map.m_lblk + map.m_len))
1005 goto got_it;
1006
1007 /*
1008 * Then do more f2fs_map_blocks() calls until we are
1009 * done with this page.
1010 */
1011 map.m_flags = 0;
1012
1013 if (block_in_file < last_block) {
1014 map.m_lblk = block_in_file;
1015 map.m_len = last_block - block_in_file;
1016
Chao Yu46c9e142015-09-18 16:54:16 +08001017 if (f2fs_map_blocks(inode, &map, 0,
Chao Yuda859852016-01-26 15:42:58 +08001018 F2FS_GET_BLOCK_READ))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001019 goto set_error_page;
1020 }
1021got_it:
1022 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1023 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1024 SetPageMappedToDisk(page);
1025
1026 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1027 SetPageUptodate(page);
1028 goto confused;
1029 }
1030 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001031 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001032 SetPageUptodate(page);
1033 unlock_page(page);
1034 goto next_page;
1035 }
1036
1037 /*
1038 * This page will go to BIO. Do we need to send this
1039 * BIO off first?
1040 */
1041 if (bio && (last_block_in_bio != block_nr - 1)) {
1042submit_and_realloc:
Jaegeuk Kimf5730182016-05-17 16:23:36 -07001043 __submit_bio(F2FS_I_SB(inode), READ, bio);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001044 bio = NULL;
1045 }
1046 if (bio == NULL) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001047 struct fscrypt_ctx *ctx = NULL;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001048
1049 if (f2fs_encrypted_inode(inode) &&
1050 S_ISREG(inode->i_mode)) {
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001051
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001052 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001053 if (IS_ERR(ctx))
1054 goto set_error_page;
1055
1056 /* wait the page to be moved by cleaning */
Chao Yu08b39fb2015-10-08 13:27:34 +08001057 f2fs_wait_on_encrypted_page_writeback(
1058 F2FS_I_SB(inode), block_nr);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001059 }
1060
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001061 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +02001062 min_t(int, nr_pages, BIO_MAX_PAGES));
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001063 if (!bio) {
1064 if (ctx)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001065 fscrypt_release_ctx(ctx);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001066 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001067 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001068 bio->bi_bdev = bdev;
1069 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(block_nr);
Chao Yu12377022015-05-25 18:03:38 +08001070 bio->bi_end_io = f2fs_read_end_io;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001071 bio->bi_private = ctx;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001072 }
1073
1074 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1075 goto submit_and_realloc;
1076
1077 last_block_in_bio = block_nr;
1078 goto next_page;
1079set_error_page:
1080 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001081 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001082 unlock_page(page);
1083 goto next_page;
1084confused:
1085 if (bio) {
Jaegeuk Kimf5730182016-05-17 16:23:36 -07001086 __submit_bio(F2FS_I_SB(inode), READ, bio);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001087 bio = NULL;
1088 }
1089 unlock_page(page);
1090next_page:
1091 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001092 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001093 }
1094 BUG_ON(pages && !list_empty(pages));
1095 if (bio)
Jaegeuk Kimf5730182016-05-17 16:23:36 -07001096 __submit_bio(F2FS_I_SB(inode), READ, bio);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001097 return 0;
1098}
1099
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001100static int f2fs_read_data_page(struct file *file, struct page *page)
1101{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001102 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001103 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001104
Chao Yuc20e89c2014-05-06 16:53:08 +08001105 trace_f2fs_readpage(page, DATA);
1106
arter97e1c42042014-08-06 23:22:50 +09001107 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001108 if (f2fs_has_inline_data(inode))
1109 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001110 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001111 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001112 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001113}
1114
1115static int f2fs_read_data_pages(struct file *file,
1116 struct address_space *mapping,
1117 struct list_head *pages, unsigned nr_pages)
1118{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001119 struct inode *inode = file->f_mapping->host;
Chao Yub8c29402015-10-12 17:02:26 +08001120 struct page *page = list_entry(pages->prev, struct page, lru);
1121
1122 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001123
1124 /* If the file has inline data, skip readpages */
1125 if (f2fs_has_inline_data(inode))
1126 return 0;
1127
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001128 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001129}
1130
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001131int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001132{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001133 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001134 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001135 struct dnode_of_data dn;
1136 int err = 0;
1137
1138 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001139 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001140 if (err)
1141 return err;
1142
Chao Yu28bc1062016-02-06 14:40:34 +08001143 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001144
1145 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001146 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001147 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001148 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001149 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001150
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001151 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001152 gfp_t gfp_flags = GFP_NOFS;
Chao Yu08b39fb2015-10-08 13:27:34 +08001153
1154 /* wait for GCed encrypted page writeback */
1155 f2fs_wait_on_encrypted_page_writeback(F2FS_I_SB(inode),
Chao Yu7a9d7542016-02-22 18:36:38 +08001156 fio->old_blkaddr);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001157retry_encrypt:
1158 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1159 gfp_flags);
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001160 if (IS_ERR(fio->encrypted_page)) {
1161 err = PTR_ERR(fio->encrypted_page);
Jaegeuk Kimb32e44822016-04-11 15:51:57 -07001162 if (err == -ENOMEM) {
1163 /* flush pending ios and wait for a while */
1164 f2fs_flush_merged_bios(F2FS_I_SB(inode));
1165 congestion_wait(BLK_RW_ASYNC, HZ/50);
1166 gfp_flags |= __GFP_NOFAIL;
1167 err = 0;
1168 goto retry_encrypt;
1169 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001170 goto out_writepage;
1171 }
1172 }
1173
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001174 set_page_writeback(page);
1175
1176 /*
1177 * If current allocation needs SSR,
1178 * it had better in-place writes for updated data.
1179 */
Chao Yu7a9d7542016-02-22 18:36:38 +08001180 if (unlikely(fio->old_blkaddr != NEW_ADDR &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001181 !is_cold_data(page) &&
Chao Yu2da3e0272015-10-28 17:56:14 +08001182 !IS_ATOMIC_WRITTEN_PAGE(page) &&
Haicheng Lib25958b2013-06-13 16:59:29 +08001183 need_inplace_update(inode))) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001184 rewrite_data_page(fio);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001185 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001186 trace_f2fs_do_write_data_page(page, IPU);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001187 } else {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001188 write_data_page(&dn, fio);
Jaegeuk Kim8ce67cb2015-03-17 17:58:08 -07001189 trace_f2fs_do_write_data_page(page, OPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001190 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim3c6c2be2015-03-17 17:16:35 -07001191 if (page->index == 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001192 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001193 }
1194out_writepage:
1195 f2fs_put_dnode(&dn);
1196 return err;
1197}
1198
1199static int f2fs_write_data_page(struct page *page,
1200 struct writeback_control *wbc)
1201{
1202 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001203 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001204 loff_t i_size = i_size_read(inode);
1205 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001206 >> PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001207 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001208 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001209 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001210 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001211 .sbi = sbi,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001212 .type = DATA,
Chris Fries6c311ec2014-01-17 14:44:39 -06001213 .rw = (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : WRITE,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001214 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001215 .encrypted_page = NULL,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001216 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001217
Chao Yuecda0de2014-05-06 16:48:26 +08001218 trace_f2fs_writepage(page, DATA);
1219
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001220 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001221 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001222
1223 /*
1224 * If the offset is out-of-range of file size,
1225 * this page does not have to be written to disk.
1226 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001227 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001228 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001229 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001230
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001231 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001232write:
Chao Yucaf00472015-01-28 17:48:42 +08001233 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001234 goto redirty_out;
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001235 if (f2fs_is_drop_cache(inode))
1236 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001237 /* we should not write 0'th page having journal header */
1238 if (f2fs_is_volatile_file(inode) && (!page->index ||
1239 (!wbc->for_reclaim &&
1240 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001241 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001242
Jaegeuk Kim39936832012-11-22 16:21:29 +09001243 /* Dentry blocks are controlled by checkpoint */
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001244 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001245 if (unlikely(f2fs_cp_error(sbi)))
1246 goto redirty_out;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001247 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001248 goto done;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001249 }
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001250
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001251 /* we should bypass data pages to proceed the kworkder jobs */
1252 if (unlikely(f2fs_cp_error(sbi))) {
1253 SetPageError(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001254 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001255 }
1256
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001257 if (!wbc->for_reclaim)
1258 need_balance_fs = true;
1259 else if (has_not_enough_free_secs(sbi, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001260 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001261
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001262 err = -EAGAIN;
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001263 f2fs_lock_op(sbi);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001264 if (f2fs_has_inline_data(inode))
1265 err = f2fs_write_inline_data(inode, page);
1266 if (err == -EAGAIN)
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001267 err = do_write_data_page(&fio);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001268 f2fs_unlock_op(sbi);
1269done:
1270 if (err && err != -ENOENT)
1271 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001272
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001273 clear_cold_data(page);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001274out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001275 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001276 if (err)
1277 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001278
1279 if (wbc->for_reclaim) {
1280 f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, DATA, WRITE);
Chao Yuc227f912015-12-16 13:09:20 +08001281 remove_dirty_inode(inode);
Chao Yueb7e8132015-11-10 18:45:07 +08001282 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001283
1284 unlock_page(page);
1285 f2fs_balance_fs(sbi, need_balance_fs);
1286
1287 if (unlikely(f2fs_cp_error(sbi)))
1288 f2fs_submit_merged_bio(sbi, DATA, WRITE);
1289
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001290 return 0;
1291
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001292redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001293 redirty_page_for_writepage(wbc, page);
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001294 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001295}
1296
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001297static int __f2fs_writepage(struct page *page, struct writeback_control *wbc,
1298 void *data)
1299{
1300 struct address_space *mapping = data;
1301 int ret = mapping->a_ops->writepage(page, wbc);
1302 mapping_set_error(mapping, ret);
1303 return ret;
1304}
1305
Chao Yu8f46dca2015-07-14 18:56:10 +08001306/*
1307 * This function was copied from write_cche_pages from mm/page-writeback.c.
1308 * The major change is making write step of cold data page separately from
1309 * warm/hot data page.
1310 */
1311static int f2fs_write_cache_pages(struct address_space *mapping,
1312 struct writeback_control *wbc, writepage_t writepage,
1313 void *data)
1314{
1315 int ret = 0;
1316 int done = 0;
1317 struct pagevec pvec;
1318 int nr_pages;
1319 pgoff_t uninitialized_var(writeback_index);
1320 pgoff_t index;
1321 pgoff_t end; /* Inclusive */
1322 pgoff_t done_index;
1323 int cycled;
1324 int range_whole = 0;
1325 int tag;
1326 int step = 0;
1327
1328 pagevec_init(&pvec, 0);
1329next:
1330 if (wbc->range_cyclic) {
1331 writeback_index = mapping->writeback_index; /* prev offset */
1332 index = writeback_index;
1333 if (index == 0)
1334 cycled = 1;
1335 else
1336 cycled = 0;
1337 end = -1;
1338 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001339 index = wbc->range_start >> PAGE_SHIFT;
1340 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001341 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1342 range_whole = 1;
1343 cycled = 1; /* ignore range_cyclic tests */
1344 }
1345 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1346 tag = PAGECACHE_TAG_TOWRITE;
1347 else
1348 tag = PAGECACHE_TAG_DIRTY;
1349retry:
1350 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1351 tag_pages_for_writeback(mapping, index, end);
1352 done_index = index;
1353 while (!done && (index <= end)) {
1354 int i;
1355
1356 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
1357 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1);
1358 if (nr_pages == 0)
1359 break;
1360
1361 for (i = 0; i < nr_pages; i++) {
1362 struct page *page = pvec.pages[i];
1363
1364 if (page->index > end) {
1365 done = 1;
1366 break;
1367 }
1368
1369 done_index = page->index;
1370
1371 lock_page(page);
1372
1373 if (unlikely(page->mapping != mapping)) {
1374continue_unlock:
1375 unlock_page(page);
1376 continue;
1377 }
1378
1379 if (!PageDirty(page)) {
1380 /* someone wrote it for us */
1381 goto continue_unlock;
1382 }
1383
Tiezhu Yang737f1892015-07-17 12:56:00 +08001384 if (step == is_cold_data(page))
Chao Yu8f46dca2015-07-14 18:56:10 +08001385 goto continue_unlock;
1386
1387 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
1399 ret = (*writepage)(page, wbc, data);
1400 if (unlikely(ret)) {
1401 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1402 unlock_page(page);
1403 ret = 0;
1404 } else {
1405 done_index = page->index + 1;
1406 done = 1;
1407 break;
1408 }
1409 }
1410
1411 if (--wbc->nr_to_write <= 0 &&
1412 wbc->sync_mode == WB_SYNC_NONE) {
1413 done = 1;
1414 break;
1415 }
1416 }
1417 pagevec_release(&pvec);
1418 cond_resched();
1419 }
1420
1421 if (step < 1) {
1422 step++;
1423 goto next;
1424 }
1425
1426 if (!cycled && !done) {
1427 cycled = 1;
1428 index = 0;
1429 end = writeback_index - 1;
1430 goto retry;
1431 }
1432 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1433 mapping->writeback_index = done_index;
1434
1435 return ret;
1436}
1437
Jaegeuk Kim25ca9232012-11-28 16:12:41 +09001438static int f2fs_write_data_pages(struct address_space *mapping,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001439 struct writeback_control *wbc)
1440{
1441 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001442 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001443 bool locked = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001444 int ret;
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001445 long diff;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001446
P J Pcfb185a2013-04-03 11:38:00 +09001447 /* deal with chardevs and other special file */
1448 if (!mapping->a_ops->writepage)
1449 return 0;
1450
Chao Yu6a290542015-07-17 18:02:39 +08001451 /* skip writing if there is no dirty page in this inode */
1452 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1453 return 0;
1454
Jaegeuk Kima1257022015-10-08 10:40:07 -07001455 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1456 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1457 available_free_memory(sbi, DIRTY_DENTS))
1458 goto skip_write;
1459
Chao Yud323d002015-10-27 09:53:45 +08001460 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001461 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001462 goto skip_write;
1463
Jaegeuk Kimd5669f72015-02-27 13:37:39 -08001464 /* during POR, we don't need to trigger writepage at all. */
1465 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1466 goto skip_write;
1467
Yunlei Hed31c7c32016-02-04 16:14:00 +08001468 trace_f2fs_writepages(mapping->host, wbc, DATA);
1469
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001470 diff = nr_pages_to_write(sbi, DATA, wbc);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001471
Jaegeuk Kim25c13552016-01-20 23:46:05 +08001472 if (!S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_ALL) {
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001473 mutex_lock(&sbi->writepages);
1474 locked = true;
1475 }
Chao Yu8f46dca2015-07-14 18:56:10 +08001476 ret = f2fs_write_cache_pages(mapping, wbc, __f2fs_writepage, mapping);
Chao Yu0c3a5792016-01-18 18:28:11 +08001477 f2fs_submit_merged_bio_cond(sbi, inode, NULL, 0, DATA, WRITE);
Jaegeuk Kim5463e7c2015-04-21 10:40:54 -07001478 if (locked)
1479 mutex_unlock(&sbi->writepages);
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001480
Chao Yuc227f912015-12-16 13:09:20 +08001481 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001482
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +09001483 wbc->nr_to_write = max((long)0, wbc->nr_to_write - diff);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001484 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001485
1486skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001487 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001488 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001489 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001490}
1491
Chao Yu3aab8f82014-07-02 13:25:04 +08001492static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1493{
1494 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001495 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001496
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001497 if (to > i_size) {
1498 truncate_pagecache(inode, i_size);
1499 truncate_blocks(inode, i_size, true);
Chao Yu3aab8f82014-07-02 13:25:04 +08001500 }
1501}
1502
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001503static int prepare_write_begin(struct f2fs_sb_info *sbi,
1504 struct page *page, loff_t pos, unsigned len,
1505 block_t *blk_addr, bool *node_changed)
1506{
1507 struct inode *inode = page->mapping->host;
1508 pgoff_t index = page->index;
1509 struct dnode_of_data dn;
1510 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001511 bool locked = false;
1512 struct extent_info ei;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001513 int err = 0;
1514
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001515 /*
1516 * we already allocated all the blocks, so we don't need to get
1517 * the block addresses when there is no need to fill the page.
1518 */
1519 if (!f2fs_has_inline_data(inode) && !f2fs_encrypted_inode(inode) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001520 len == PAGE_SIZE)
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001521 return 0;
1522
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001523 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001524 (pos & PAGE_MASK) >= i_size_read(inode)) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001525 f2fs_lock_op(sbi);
1526 locked = true;
1527 }
1528restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001529 /* check inline_data */
1530 ipage = get_node_page(sbi, inode->i_ino);
1531 if (IS_ERR(ipage)) {
1532 err = PTR_ERR(ipage);
1533 goto unlock_out;
1534 }
1535
1536 set_new_dnode(&dn, inode, ipage, ipage, 0);
1537
1538 if (f2fs_has_inline_data(inode)) {
1539 if (pos + len <= MAX_INLINE_DATA) {
1540 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001541 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001542 if (inode->i_nlink)
1543 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001544 } else {
1545 err = f2fs_convert_inline_page(&dn, page);
1546 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001547 goto out;
1548 if (dn.data_blkaddr == NULL_ADDR)
1549 err = f2fs_get_block(&dn, index);
1550 }
1551 } else if (locked) {
1552 err = f2fs_get_block(&dn, index);
1553 } else {
1554 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1555 dn.data_blkaddr = ei.blk + index - ei.fofs;
1556 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001557 /* hole case */
1558 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001559 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001560 f2fs_put_dnode(&dn);
1561 f2fs_lock_op(sbi);
1562 locked = true;
1563 goto restart;
1564 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001565 }
1566 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001567
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001568 /* convert_inline_page can make node_changed */
1569 *blk_addr = dn.data_blkaddr;
1570 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001571out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001572 f2fs_put_dnode(&dn);
1573unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001574 if (locked)
1575 f2fs_unlock_op(sbi);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001576 return err;
1577}
1578
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001579static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1580 loff_t pos, unsigned len, unsigned flags,
1581 struct page **pagep, void **fsdata)
1582{
1583 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001584 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001585 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001586 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001587 bool need_balance = false;
1588 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001589 int err = 0;
1590
Chao Yu62aed042014-05-06 16:46:04 +08001591 trace_f2fs_write_begin(inode, pos, len, flags);
1592
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001593 /*
1594 * We should check this at this moment to avoid deadlock on inode page
1595 * and #0 page. The locking rule for inline_data conversion should be:
1596 * lock_page(page #0) -> lock_page(inode_page)
1597 */
1598 if (index != 0) {
1599 err = f2fs_convert_inline_inode(inode);
1600 if (err)
1601 goto fail;
1602 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001603repeat:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001604 page = grab_cache_page_write_begin(mapping, index, flags);
Chao Yu3aab8f82014-07-02 13:25:04 +08001605 if (!page) {
1606 err = -ENOMEM;
1607 goto fail;
1608 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001609
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001610 *pagep = page;
1611
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001612 err = prepare_write_begin(sbi, page, pos, len,
1613 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001614 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001615 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001616
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001617 if (need_balance && has_not_enough_free_secs(sbi, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001618 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001619 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001620 lock_page(page);
1621 if (page->mapping != mapping) {
1622 /* The page got truncated from under us */
1623 f2fs_put_page(page, 1);
1624 goto repeat;
1625 }
1626 }
1627
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001628 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001629
Chao Yu08b39fb2015-10-08 13:27:34 +08001630 /* wait for GCed encrypted page writeback */
1631 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001632 f2fs_wait_on_encrypted_page_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08001633
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001634 if (len == PAGE_SIZE)
Chao Yu90d43882015-07-08 18:24:38 +08001635 goto out_update;
1636 if (PageUptodate(page))
1637 goto out_clear;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001638
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001639 if ((pos & PAGE_MASK) >= i_size_read(inode)) {
1640 unsigned start = pos & (PAGE_SIZE - 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001641 unsigned end = start + len;
1642
1643 /* Reading beyond i_size is simple: memset to zero */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001644 zero_user_segments(page, 0, start, end, PAGE_SIZE);
Chao Yu90d43882015-07-08 18:24:38 +08001645 goto out_update;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001646 }
1647
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001648 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001649 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001650 } else {
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001651 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001652 .sbi = sbi,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001653 .type = DATA,
1654 .rw = READ_SYNC,
Chao Yu7a9d7542016-02-22 18:36:38 +08001655 .old_blkaddr = blkaddr,
1656 .new_blkaddr = blkaddr,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001657 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001658 .encrypted_page = NULL,
Jaegeuk Kimcf04e8e2014-12-17 19:33:13 -08001659 };
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001660 err = f2fs_submit_page_bio(&fio);
Jan Kara9234f312014-10-22 15:21:47 +02001661 if (err)
1662 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08001663
Jaegeuk Kim393ff912013-03-08 21:29:23 +09001664 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001665 if (unlikely(!PageUptodate(page))) {
Chao Yu3aab8f82014-07-02 13:25:04 +08001666 err = -EIO;
1667 goto fail;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001668 }
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09001669 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001670 f2fs_put_page(page, 1);
1671 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001672 }
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001673
1674 /* avoid symlink page */
1675 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001676 err = fscrypt_decrypt_page(page);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001677 if (err)
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001678 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001679 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001680 }
Chao Yu90d43882015-07-08 18:24:38 +08001681out_update:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001682 SetPageUptodate(page);
Chao Yu90d43882015-07-08 18:24:38 +08001683out_clear:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001684 clear_cold_data(page);
1685 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001686
Chao Yu3aab8f82014-07-02 13:25:04 +08001687fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001688 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08001689 f2fs_write_failed(mapping, pos + len);
1690 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001691}
1692
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001693static int f2fs_write_end(struct file *file,
1694 struct address_space *mapping,
1695 loff_t pos, unsigned len, unsigned copied,
1696 struct page *page, void *fsdata)
1697{
1698 struct inode *inode = page->mapping->host;
1699
Chao Yudfb2bf32014-05-06 16:47:23 +08001700 trace_f2fs_write_end(inode, pos, len, copied);
1701
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001702 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001703
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07001704 if (pos + copied > i_size_read(inode))
1705 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001706
Chao Yu75c3c8b2013-11-16 14:15:59 +08001707 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08001708 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001709 return copied;
1710}
1711
Omar Sandoval6f673762015-03-16 04:33:52 -07001712static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
1713 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001714{
1715 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001716
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001717 if (offset & blocksize_mask)
1718 return -EINVAL;
1719
Al Viro5b46f252014-03-16 18:07:34 -04001720 if (iov_iter_alignment(iter) & blocksize_mask)
1721 return -EINVAL;
1722
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001723 return 0;
1724}
1725
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001726static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001727{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001728 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08001729 struct inode *inode = mapping->host;
1730 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001731 loff_t offset = iocb->ki_pos;
Chao Yu3aab8f82014-07-02 13:25:04 +08001732 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09001733
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001734 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08001735 if (err)
1736 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001737
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07001738 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
1739 return 0;
1740
Omar Sandoval6f673762015-03-16 04:33:52 -07001741 trace_f2fs_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Chao Yu70407fa2014-07-31 21:11:22 +08001742
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07001743 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001744 if (iov_iter_rw(iter) == WRITE) {
1745 if (err > 0)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001746 set_inode_flag(inode, FI_UPDATE_WRITE);
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04001747 else if (err < 0)
1748 f2fs_write_failed(mapping, offset + count);
1749 }
Chao Yu70407fa2014-07-31 21:11:22 +08001750
Omar Sandoval6f673762015-03-16 04:33:52 -07001751 trace_f2fs_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), err);
Chao Yu70407fa2014-07-31 21:11:22 +08001752
Chao Yu3aab8f82014-07-02 13:25:04 +08001753 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001754}
1755
Chao Yu487261f2015-02-05 17:44:29 +08001756void f2fs_invalidate_page(struct page *page, unsigned int offset,
1757 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001758{
1759 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08001760 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001761
Chao Yu487261f2015-02-05 17:44:29 +08001762 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001763 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001764 return;
1765
Chao Yu487261f2015-02-05 17:44:29 +08001766 if (PageDirty(page)) {
1767 if (inode->i_ino == F2FS_META_INO(sbi))
1768 dec_page_count(sbi, F2FS_DIRTY_META);
1769 else if (inode->i_ino == F2FS_NODE_INO(sbi))
1770 dec_page_count(sbi, F2FS_DIRTY_NODES);
1771 else
1772 inode_dec_dirty_pages(inode);
1773 }
Chao Yudecd36b2015-08-07 18:42:09 +08001774
1775 /* This is atomic written page, keep Private */
1776 if (IS_ATOMIC_WRITTEN_PAGE(page))
1777 return;
1778
Chao Yu23dc9742016-04-29 20:09:15 +08001779 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001780 ClearPagePrivate(page);
1781}
1782
Chao Yu487261f2015-02-05 17:44:29 +08001783int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001784{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08001785 /* If this is dirty page, keep PagePrivate */
1786 if (PageDirty(page))
1787 return 0;
1788
Chao Yudecd36b2015-08-07 18:42:09 +08001789 /* This is atomic written page, keep Private */
1790 if (IS_ATOMIC_WRITTEN_PAGE(page))
1791 return 0;
1792
Chao Yu23dc9742016-04-29 20:09:15 +08001793 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001794 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09001795 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001796}
1797
1798static int f2fs_set_data_page_dirty(struct page *page)
1799{
1800 struct address_space *mapping = page->mapping;
1801 struct inode *inode = mapping->host;
1802
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09001803 trace_f2fs_set_page_dirty(page, DATA);
1804
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001805 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001806
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001807 if (f2fs_is_atomic_file(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08001808 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
1809 register_inmem_page(inode, page);
1810 return 1;
1811 }
1812 /*
1813 * Previously, this page has been registered, we just
1814 * return here.
1815 */
1816 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07001817 }
1818
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001819 if (!PageDirty(page)) {
1820 __set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001821 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001822 return 1;
1823 }
1824 return 0;
1825}
1826
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001827static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
1828{
Chao Yu454ae7e2014-04-22 13:34:01 +08001829 struct inode *inode = mapping->host;
1830
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07001831 if (f2fs_has_inline_data(inode))
1832 return 0;
1833
1834 /* make sure allocating whole blocks */
1835 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
1836 filemap_write_and_wait(mapping);
1837
Chao Yue2b4e2b2015-08-19 19:11:19 +08001838 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08001839}
1840
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001841const struct address_space_operations f2fs_dblock_aops = {
1842 .readpage = f2fs_read_data_page,
1843 .readpages = f2fs_read_data_pages,
1844 .writepage = f2fs_write_data_page,
1845 .writepages = f2fs_write_data_pages,
1846 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09001847 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001848 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08001849 .invalidatepage = f2fs_invalidate_page,
1850 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001851 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09001852 .bmap = f2fs_bmap,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001853};