blob: 7aca6ccd01f6ae6af309c47c8deb6a017cc21b3d [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 Kimfe76b792016-06-30 18:40:10 -070022#include <linux/mm.h>
23#include <linux/memcontrol.h>
Jaegeuk Kimf1e88662015-04-09 11:20:42 -070024#include <linux/cleancache.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090026
27#include "f2fs.h"
28#include "node.h"
29#include "segment.h"
Jaegeuk Kimdb9f7c12014-12-17 20:04:08 -080030#include "trace.h"
Namjae Jeon848753a2013-04-23 16:38:02 +090031#include <trace/events/f2fs.h>
Jaegeuk Kimeb47b802012-11-02 17:10:12 +090032
Chao Yu36951b32016-11-16 10:41:20 +080033static bool __is_cp_guaranteed(struct page *page)
34{
35 struct address_space *mapping = page->mapping;
36 struct inode *inode;
37 struct f2fs_sb_info *sbi;
38
39 if (!mapping)
40 return false;
41
42 inode = mapping->host;
43 sbi = F2FS_I_SB(inode);
44
45 if (inode->i_ino == F2FS_META_INO(sbi) ||
46 inode->i_ino == F2FS_NODE_INO(sbi) ||
47 S_ISDIR(inode->i_mode) ||
48 is_cold_data(page))
49 return true;
50 return false;
51}
52
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020053static void f2fs_read_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090054{
Linus Torvaldsf5688492014-01-30 11:19:05 -080055 struct bio_vec *bvec;
56 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090057
Chao Yu8b038c72016-09-18 23:30:07 +080058#ifdef CONFIG_F2FS_FAULT_INJECTION
Chao Yu55523512017-02-25 11:08:28 +080059 if (time_to_inject(F2FS_P_SB(bio->bi_io_vec->bv_page), FAULT_IO)) {
60 f2fs_show_injection_info(FAULT_IO);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020061 bio->bi_status = BLK_STS_IOERR;
Chao Yu55523512017-02-25 11:08:28 +080062 }
Chao Yu8b038c72016-09-18 23:30:07 +080063#endif
64
Chao Yu12377022015-05-25 18:03:38 +080065 if (f2fs_bio_encrypted(bio)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020066 if (bio->bi_status) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070067 fscrypt_release_ctx(bio->bi_private);
Chao Yu12377022015-05-25 18:03:38 +080068 } else {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070069 fscrypt_decrypt_bio_pages(bio->bi_private, bio);
Chao Yu12377022015-05-25 18:03:38 +080070 return;
71 }
72 }
73
Linus Torvaldsf5688492014-01-30 11:19:05 -080074 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090075 struct page *page = bvec->bv_page;
76
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020077 if (!bio->bi_status) {
Jaegeuk Kim237c0792016-06-30 18:49:15 -070078 if (!PageUptodate(page))
79 SetPageUptodate(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080080 } else {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090081 ClearPageUptodate(page);
82 SetPageError(page);
83 }
84 unlock_page(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -080085 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090086 bio_put(bio);
87}
88
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020089static void f2fs_write_end_io(struct bio *bio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090090{
Jaegeuk Kim1b1f5592014-02-03 10:50:22 +090091 struct f2fs_sb_info *sbi = bio->bi_private;
Linus Torvaldsf5688492014-01-30 11:19:05 -080092 struct bio_vec *bvec;
93 int i;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090094
Linus Torvaldsf5688492014-01-30 11:19:05 -080095 bio_for_each_segment_all(bvec, bio, i) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090096 struct page *page = bvec->bv_page;
Chao Yu36951b32016-11-16 10:41:20 +080097 enum count_type type = WB_DATA_TYPE(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +090098
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -080099 if (IS_DUMMY_WRITTEN_PAGE(page)) {
100 set_page_private(page, (unsigned long)NULL);
101 ClearPagePrivate(page);
102 unlock_page(page);
103 mempool_free(page, sbi->write_io_dummy);
104
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200105 if (unlikely(bio->bi_status))
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800106 f2fs_stop_checkpoint(sbi, true);
107 continue;
108 }
109
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700110 fscrypt_pullback_bio_page(&page, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700111
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200112 if (unlikely(bio->bi_status)) {
Michal Hocko5114a972016-10-11 13:56:01 -0700113 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kim38f91ca2016-05-18 14:07:56 -0700114 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900115 }
Chao Yu36951b32016-11-16 10:41:20 +0800116 dec_page_count(sbi, type);
117 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900118 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800119 }
Chao Yu36951b32016-11-16 10:41:20 +0800120 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700121 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900122 wake_up(&sbi->cp_wait);
123
124 bio_put(bio);
125}
126
Gu Zheng940a6d32013-12-20 17:39:59 +0800127/*
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700128 * Return true, if pre_bio's bdev is same as its target device.
129 */
130struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
131 block_t blk_addr, struct bio *bio)
132{
133 struct block_device *bdev = sbi->sb->s_bdev;
134 int i;
135
136 for (i = 0; i < sbi->s_ndevs; i++) {
137 if (FDEV(i).start_blk <= blk_addr &&
138 FDEV(i).end_blk >= blk_addr) {
139 blk_addr -= FDEV(i).start_blk;
140 bdev = FDEV(i).bdev;
141 break;
142 }
143 }
144 if (bio) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200145 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700146 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
147 }
148 return bdev;
149}
150
151int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
152{
153 int i;
154
155 for (i = 0; i < sbi->s_ndevs; i++)
156 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
157 return i;
158 return 0;
159}
160
161static bool __same_bdev(struct f2fs_sb_info *sbi,
162 block_t blk_addr, struct bio *bio)
163{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200164 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
165 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700166}
167
168/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800169 * Low-level block read/write IO operations.
170 */
171static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
172 int npages, bool is_read)
173{
174 struct bio *bio;
175
Chao Yud62fe972017-10-28 16:52:31 +0800176 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800177
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700178 f2fs_target_device(sbi, blk_addr, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800179 bio->bi_end_io = is_read ? f2fs_read_end_io : f2fs_write_end_io;
Chao Yu12377022015-05-25 18:03:38 +0800180 bio->bi_private = is_read ? NULL : sbi;
Gu Zheng940a6d32013-12-20 17:39:59 +0800181
182 return bio;
183}
184
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700185static inline void __submit_bio(struct f2fs_sb_info *sbi,
186 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700187{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700188 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800189 unsigned int start;
190
Damien Le Moal0bfd7a02016-10-28 17:45:00 +0900191 if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
Jaegeuk Kim52763a42016-06-13 09:47:48 -0700192 current->plug && (type == DATA || type == NODE))
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700193 blk_finish_plug(current->plug);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800194
195 if (type != DATA && type != NODE)
196 goto submit_io;
197
198 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
199 start %= F2FS_IO_SIZE(sbi);
200
201 if (start == 0)
202 goto submit_io;
203
204 /* fill dummy pages */
205 for (; start < F2FS_IO_SIZE(sbi); start++) {
206 struct page *page =
207 mempool_alloc(sbi->write_io_dummy,
208 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
209 f2fs_bug_on(sbi, !page);
210
211 SetPagePrivate(page);
212 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
213 lock_page(page);
214 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
215 f2fs_bug_on(sbi, 1);
216 }
217 /*
218 * In the NODE case, we lose next block address chain. So, we
219 * need to do checkpoint in f2fs_sync_file.
220 */
221 if (type == NODE)
222 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700223 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800224submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800225 if (is_read_io(bio_op(bio)))
226 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
227 else
228 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500229 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700230}
231
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900232static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900233{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900234 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900235
236 if (!io->bio)
237 return;
238
Mike Christie04d328d2016-06-05 14:31:55 -0500239 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
240
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800241 if (is_read_io(fio->op))
242 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
243 else
244 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
245
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700246 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900247 io->bio = NULL;
248}
249
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800250static bool __has_merged_page(struct f2fs_bio_info *io,
251 struct inode *inode, nid_t ino, pgoff_t idx)
Chao Yu0fd785e2016-01-18 18:24:59 +0800252{
Chao Yu0fd785e2016-01-18 18:24:59 +0800253 struct bio_vec *bvec;
254 struct page *target;
255 int i;
256
Chao Yu0c3a5792016-01-18 18:28:11 +0800257 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800258 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800259
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800260 if (!inode && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800261 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800262
263 bio_for_each_segment_all(bvec, io->bio, i) {
264
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700265 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800266 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700267 else
268 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800269
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800270 if (idx != target->index)
271 continue;
272
Chao Yu0c3a5792016-01-18 18:28:11 +0800273 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800274 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800275 if (ino && ino == ino_of_node(target))
276 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800277 }
278
Chao Yu0fd785e2016-01-18 18:24:59 +0800279 return false;
280}
281
Chao Yu0c3a5792016-01-18 18:28:11 +0800282static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800283 nid_t ino, pgoff_t idx, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800284{
285 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700286 enum temp_type temp;
287 struct f2fs_bio_info *io;
288 bool ret = false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800289
Jaegeuk Kima912b542017-05-10 11:18:25 -0700290 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
291 io = sbi->write_io[btype] + temp;
292
293 down_read(&io->io_rwsem);
294 ret = __has_merged_page(io, inode, ino, idx);
295 up_read(&io->io_rwsem);
296
297 /* TODO: use HOT temp only for meta pages now. */
298 if (ret || btype == META)
299 break;
300 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800301 return ret;
302}
303
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700304static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700305 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900306{
307 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700308 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900309
Chao Yudf0f8dc2014-03-22 14:57:23 +0800310 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900311
312 /* change META to META_FLUSH in the checkpoint procedure */
313 if (type >= META_FLUSH) {
314 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500315 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200316 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600317 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800318 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900319 }
320 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800321 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900322}
323
Jaegeuk Kima912b542017-05-10 11:18:25 -0700324static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800325 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700326 enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800327{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700328 enum temp_type temp;
329
330 if (!force && !has_merged_page(sbi, inode, ino, idx, type))
331 return;
332
333 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
334
335 __f2fs_submit_merged_write(sbi, type, temp);
336
337 /* TODO: use HOT temp only for meta pages now. */
338 if (type >= META)
339 break;
340 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800341}
342
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700343void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800344{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700345 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900346}
347
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700348void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900349 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700350 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900351{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700352 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900353}
354
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700355void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800356{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700357 f2fs_submit_merged_write(sbi, DATA);
358 f2fs_submit_merged_write(sbi, NODE);
359 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800360}
361
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900362/*
363 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300364 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900365 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700366int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900367{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900368 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700369 struct page *page = fio->encrypted_page ?
370 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900371
Chao Yu2ace38e2014-12-24 16:08:14 +0800372 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700373 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900374
375 /* Allocate a new bio */
Mike Christie04d328d2016-06-05 14:31:55 -0500376 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, 1, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900377
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300378 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900379 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900380 return -EFAULT;
381 }
Mike Christie04d328d2016-06-05 14:31:55 -0500382 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900383
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700384 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -0700385
386 if (!is_read_io(fio->op))
387 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900388 return 0;
389}
390
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700391int f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900392{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700393 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900394 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700395 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700396 struct page *bio_page;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800397 int err = 0;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900398
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700399 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900400
Chao Yufb830fc2017-05-19 23:37:01 +0800401 down_write(&io->io_rwsem);
402next:
403 if (fio->in_list) {
404 spin_lock(&io->io_lock);
405 if (list_empty(&io->io_list)) {
406 spin_unlock(&io->io_lock);
407 goto out_fail;
408 }
409 fio = list_first_entry(&io->io_list,
410 struct f2fs_io_info, list);
411 list_del(&fio->list);
412 spin_unlock(&io->io_lock);
413 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900414
Chao Yu7a9d7542016-02-22 18:36:38 +0800415 if (fio->old_blkaddr != NEW_ADDR)
416 verify_block_addr(sbi, fio->old_blkaddr);
417 verify_block_addr(sbi, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900418
Chao Yu36951b32016-11-16 10:41:20 +0800419 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
420
Thomas Meyerebf7c522017-10-07 16:02:21 +0200421 /* set submitted = true as a return value */
422 fio->submitted = true;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800423
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700424 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900425
Chao Yu7a9d7542016-02-22 18:36:38 +0800426 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700427 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
428 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900429 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900430alloc_new:
431 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800432 if ((fio->type == DATA || fio->type == NODE) &&
433 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
434 err = -EAGAIN;
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700435 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800436 goto out_fail;
437 }
Chao Yu7a9d7542016-02-22 18:36:38 +0800438 io->bio = __bio_alloc(sbi, fio->new_blkaddr,
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700439 BIO_MAX_PAGES, false);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900440 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900441 }
442
Jaegeuk Kima912b542017-05-10 11:18:25 -0700443 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900444 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900445 goto alloc_new;
446 }
447
Chao Yu7a9d7542016-02-22 18:36:38 +0800448 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700449 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800450
451 trace_f2fs_submit_page_write(fio->page, fio);
452
453 if (fio->in_list)
454 goto next;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800455out_fail:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800456 up_write(&io->io_rwsem);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800457 return err;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900458}
459
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700460static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
461 unsigned nr_pages)
462{
463 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
464 struct fscrypt_ctx *ctx = NULL;
465 struct bio *bio;
466
467 if (f2fs_encrypted_file(inode)) {
468 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
469 if (IS_ERR(ctx))
470 return ERR_CAST(ctx);
471
472 /* wait the page to be moved by cleaning */
473 f2fs_wait_on_block_writeback(sbi, blkaddr);
474 }
475
Chao Yud62fe972017-10-28 16:52:31 +0800476 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700477 if (!bio) {
478 if (ctx)
479 fscrypt_release_ctx(ctx);
480 return ERR_PTR(-ENOMEM);
481 }
482 f2fs_target_device(sbi, blkaddr, bio);
483 bio->bi_end_io = f2fs_read_end_io;
484 bio->bi_private = ctx;
485 bio_set_op_attrs(bio, REQ_OP_READ, 0);
486
487 return bio;
488}
489
490/* This can handle encryption stuffs */
491static int f2fs_submit_page_read(struct inode *inode, struct page *page,
492 block_t blkaddr)
493{
494 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1);
495
496 if (IS_ERR(bio))
497 return PTR_ERR(bio);
498
499 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
500 bio_put(bio);
501 return -EFAULT;
502 }
503 __submit_bio(F2FS_I_SB(inode), bio, DATA);
504 return 0;
505}
506
Chao Yu46008c62016-05-09 19:56:30 +0800507static void __set_data_blkaddr(struct dnode_of_data *dn)
508{
509 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
510 __le32 *addr_array;
Chao Yu7a2af762017-07-19 00:19:06 +0800511 int base = 0;
512
513 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
514 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800515
516 /* Get physical address of data block */
517 addr_array = blkaddr_in_node(rn);
Chao Yu7a2af762017-07-19 00:19:06 +0800518 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800519}
520
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900521/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900522 * Lock ordering for the change of data block address:
523 * ->data_page
524 * ->node_page
525 * update block addresses in the node page
526 */
Chao Yu216a6202015-03-19 19:23:32 +0800527void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900528{
Chao Yu46008c62016-05-09 19:56:30 +0800529 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
530 __set_data_blkaddr(dn);
531 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800532 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900533}
534
Chao Yuf28b3432016-02-24 17:16:47 +0800535void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
536{
537 dn->data_blkaddr = blkaddr;
538 set_data_blkaddr(dn);
539 f2fs_update_extent_cache(dn);
540}
541
Chao Yu46008c62016-05-09 19:56:30 +0800542/* dn->ofs_in_node will be returned with up-to-date last block pointer */
543int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900544{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700545 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800546 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900547
Chao Yu46008c62016-05-09 19:56:30 +0800548 if (!count)
549 return 0;
550
Jaegeuk Kim91942322016-05-20 10:13:22 -0700551 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900552 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +0800553 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
554 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900555
Chao Yu46008c62016-05-09 19:56:30 +0800556 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
557 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900558
Chao Yu46008c62016-05-09 19:56:30 +0800559 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
560
561 for (; count > 0; dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800562 block_t blkaddr = datablock_addr(dn->inode,
563 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800564 if (blkaddr == NULL_ADDR) {
565 dn->data_blkaddr = NEW_ADDR;
566 __set_data_blkaddr(dn);
567 count--;
568 }
569 }
570
571 if (set_page_dirty(dn->node_page))
572 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900573 return 0;
574}
575
Chao Yu46008c62016-05-09 19:56:30 +0800576/* Should keep dn->ofs_in_node unchanged */
577int reserve_new_block(struct dnode_of_data *dn)
578{
579 unsigned int ofs_in_node = dn->ofs_in_node;
580 int ret;
581
582 ret = reserve_new_blocks(dn, 1);
583 dn->ofs_in_node = ofs_in_node;
584 return ret;
585}
586
Huajun Lib6009652013-11-10 23:13:18 +0800587int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
588{
589 bool need_put = dn->inode_page ? false : true;
590 int err;
591
592 err = get_dnode_of_data(dn, index, ALLOC_NODE);
593 if (err)
594 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900595
Huajun Lib6009652013-11-10 23:13:18 +0800596 if (dn->data_blkaddr == NULL_ADDR)
597 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900598 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800599 f2fs_put_dnode(dn);
600 return err;
601}
602
Fan Li759af1c2015-08-05 15:52:16 +0800603int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900604{
Hou Pengyange15882b2017-02-23 09:18:05 +0000605 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800606 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800607
Fan Li759af1c2015-08-05 15:52:16 +0800608 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
609 dn->data_blkaddr = ei.blk + index - ei.fofs;
610 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800611 }
612
Fan Li759af1c2015-08-05 15:52:16 +0800613 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900614}
615
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700616struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500617 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900618{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900619 struct address_space *mapping = inode->i_mapping;
620 struct dnode_of_data dn;
621 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +0000622 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900623 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700624
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700625 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900626 if (!page)
627 return ERR_PTR(-ENOMEM);
628
Chao Yucb3bc9e2015-02-05 18:03:40 +0800629 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
630 dn.data_blkaddr = ei.blk + index - ei.fofs;
631 goto got_it;
632 }
633
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900634 set_new_dnode(&dn, inode, NULL, NULL, 0);
635 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700636 if (err)
637 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900638 f2fs_put_dnode(&dn);
639
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900640 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700641 err = -ENOENT;
642 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900643 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800644got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700645 if (PageUptodate(page)) {
646 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900647 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700648 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900649
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900650 /*
651 * A new dentry page is allocated but not able to be written, since its
652 * new inode page couldn't be allocated due to -ENOSPC.
653 * In such the case, its blkaddr can be remained as NEW_ADDR.
654 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
655 */
656 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300657 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700658 if (!PageUptodate(page))
659 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700660 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900661 return page;
662 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900663
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700664 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900665 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700666 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700667 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700668
669put_err:
670 f2fs_put_page(page, 1);
671 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700672}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900673
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700674struct page *find_data_page(struct inode *inode, pgoff_t index)
675{
676 struct address_space *mapping = inode->i_mapping;
677 struct page *page;
678
679 page = find_get_page(mapping, index);
680 if (page && PageUptodate(page))
681 return page;
682 f2fs_put_page(page, 0);
683
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600684 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700685 if (IS_ERR(page))
686 return page;
687
688 if (PageUptodate(page))
689 return page;
690
691 wait_on_page_locked(page);
692 if (unlikely(!PageUptodate(page))) {
693 f2fs_put_page(page, 0);
694 return ERR_PTR(-EIO);
695 }
696 return page;
697}
698
699/*
700 * If it tries to access a hole, return an error.
701 * Because, the callers, functions in dir.c and GC, should be able to know
702 * whether this page exists or not.
703 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700704struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
705 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700706{
707 struct address_space *mapping = inode->i_mapping;
708 struct page *page;
709repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600710 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700711 if (IS_ERR(page))
712 return page;
713
714 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900715 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900716 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900717 f2fs_put_page(page, 1);
718 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900719 }
Chao Yu1563ac72016-07-03 22:05:12 +0800720 if (unlikely(!PageUptodate(page))) {
721 f2fs_put_page(page, 1);
722 return ERR_PTR(-EIO);
723 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900724 return page;
725}
726
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900727/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900728 * Caller ensures that this data page is never allocated.
729 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900730 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800731 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
732 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800733 * Note that, ipage is set only by make_empty_dir, and if any error occur,
734 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900735 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900736struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900737 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900738{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900739 struct address_space *mapping = inode->i_mapping;
740 struct page *page;
741 struct dnode_of_data dn;
742 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800743
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700744 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800745 if (!page) {
746 /*
747 * before exiting, we should make sure ipage will be released
748 * if any error occur.
749 */
750 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700751 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800752 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900753
Jaegeuk Kima8865372013-12-27 17:04:17 +0900754 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800755 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700756 if (err) {
757 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900758 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900759 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700760 if (!ipage)
761 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900762
763 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700764 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900765
766 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300767 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700768 if (!PageUptodate(page))
769 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900770 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700771 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900772
Jaegeuk Kim76121182016-01-01 22:03:47 -0800773 /* if ipage exists, blkaddr should be NEW_ADDR */
774 f2fs_bug_on(F2FS_I_SB(inode), ipage);
775 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700776 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800777 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900778 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700779got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800780 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700781 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700782 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900783 return page;
784}
785
Hyunchul Leed5097be2017-11-28 09:23:00 +0900786static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900787{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700788 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900789 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900790 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700791 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800792 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +0800793 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900794
Jaegeuk Kim91942322016-05-20 10:13:22 -0700795 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900796 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800797
Chao Yu7a2af762017-07-19 00:19:06 +0800798 dn->data_blkaddr = datablock_addr(dn->inode,
799 dn->node_page, dn->ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800800 if (dn->data_blkaddr == NEW_ADDR)
801 goto alloc;
802
Chao Yu0abd6752017-07-09 00:13:07 +0800803 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
804 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900805
Chao Yudf6136e2015-03-23 10:33:37 +0800806alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900807 get_node_info(sbi, dn->nid, &ni);
808 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
809
Chao Yudf6136e2015-03-23 10:33:37 +0800810 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Hyunchul Leed5097be2017-11-28 09:23:00 +0900811 &sum, seg_type, NULL, false);
Chao Yu216a6202015-03-19 19:23:32 +0800812 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900813
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700814 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800815 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700816 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300817 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700818 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300819 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900820 return 0;
821}
822
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800823static inline bool __force_buffered_io(struct inode *inode, int rw)
824{
Jaegeuk Kim19585932017-09-05 16:54:24 -0700825 return (f2fs_encrypted_file(inode) ||
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800826 (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
827 F2FS_I_SB(inode)->s_ndevs);
828}
829
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800830int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800831{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800832 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800833 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800834 int err = 0;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800835
Weichao Guo71ad6822017-09-29 22:43:23 +0800836 /* convert inline data for Direct I/O*/
837 if (iocb->ki_flags & IOCB_DIRECT) {
838 err = f2fs_convert_inline_inode(inode);
839 if (err)
840 return err;
841 }
842
Jaegeuk Kimdc91de72017-01-13 13:12:29 -0800843 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
844 return 0;
845
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700846 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800847 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
848 if (map.m_len > map.m_lblk)
849 map.m_len -= map.m_lblk;
850 else
851 map.m_len = 0;
852
Chao Yuda859852016-01-26 15:42:58 +0800853 map.m_next_pgofs = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +0900854 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800855
Hyunchul Leed5097be2017-11-28 09:23:00 +0900856 if (iocb->ki_flags & IOCB_DIRECT) {
857 map.m_seg_type = rw_hint_to_seg_type(iocb->ki_hint);
Jaegeuk Kimc040ff92016-11-11 16:46:40 -0800858 return f2fs_map_blocks(inode, &map, 1,
859 __force_buffered_io(inode, WRITE) ?
860 F2FS_GET_BLOCK_PRE_AIO :
861 F2FS_GET_BLOCK_PRE_DIO);
Hyunchul Leed5097be2017-11-28 09:23:00 +0900862 }
Chao Yuf2470372017-07-19 00:19:05 +0800863 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800864 err = f2fs_convert_inline_inode(inode);
865 if (err)
866 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800867 }
Sheng Yong250066452017-11-22 18:23:39 +0800868 if (!f2fs_has_inline_data(inode)) {
869 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
870 if (map.m_len > 0 && err == -ENOSPC) {
871 set_inode_flag(inode, FI_NO_PREALLOC);
872 err = 0;
873 }
874 return err;
875 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800876 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800877}
878
Yunlei He59c90812017-03-13 20:22:18 +0800879static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
880{
881 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
882 if (lock)
883 down_read(&sbi->node_change);
884 else
885 up_read(&sbi->node_change);
886 } else {
887 if (lock)
888 f2fs_lock_op(sbi);
889 else
890 f2fs_unlock_op(sbi);
891 }
892}
893
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900894/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700895 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
896 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800897 * If original data blocks are allocated, then give them to blockdev.
898 * Otherwise,
899 * a. preallocate requested block addresses
900 * b. do not use extent cache for better performance
901 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900902 */
Chao Yud323d002015-10-27 09:53:45 +0800903int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800904 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900905{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700906 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900907 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800908 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700909 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800910 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900911 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800912 unsigned int ofs_in_node, last_ofs_in_node;
913 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +0000914 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +0800915 block_t blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900916
Chao Yudfd02e42016-08-20 15:12:01 +0800917 if (!maxblocks)
918 return 0;
919
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700920 map->m_len = 0;
921 map->m_flags = 0;
922
923 /* it only supports block size == page size */
924 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800925 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900926
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800927 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700928 map->m_pblk = ei.blk + pgofs - ei.fofs;
929 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
930 map->m_flags = F2FS_MAP_MAPPED;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900931 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800932 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900933
Chao Yu4fe71e82016-01-26 15:37:38 +0800934next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800935 if (create)
Yunlei He59c90812017-03-13 20:22:18 +0800936 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900937
938 /* When reading holes, we need its node page */
939 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900940 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900941 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800942 if (flag == F2FS_GET_BLOCK_BMAP)
943 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800944 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900945 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800946 if (map->m_next_pgofs)
947 *map->m_next_pgofs =
948 get_next_page_offset(&dn, pgofs);
949 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900950 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900951 }
Chao Yu973163f2015-09-18 16:51:51 +0800952
Chao Yu46008c62016-05-09 19:56:30 +0800953 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +0100954 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800955 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900956
Chao Yu4fe71e82016-01-26 15:37:38 +0800957next_block:
Chao Yu7a2af762017-07-19 00:19:06 +0800958 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800959
Fan Li7df3a432015-12-17 13:20:59 +0800960 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
961 if (create) {
962 if (unlikely(f2fs_cp_error(sbi))) {
963 err = -EIO;
964 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800965 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800966 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800967 if (blkaddr == NULL_ADDR) {
968 prealloc++;
969 last_ofs_in_node = dn.ofs_in_node;
970 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800971 } else {
Hyunchul Leed5097be2017-11-28 09:23:00 +0900972 err = __allocate_data_block(&dn,
973 map->m_seg_type);
Chao Yu6f2d8ed2016-10-11 22:57:03 +0800974 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -0700975 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800976 }
Fan Li7df3a432015-12-17 13:20:59 +0800977 if (err)
978 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +0800979 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +0800980 blkaddr = dn.data_blkaddr;
981 } else {
Chao Yu43473f92016-05-05 19:13:02 +0800982 if (flag == F2FS_GET_BLOCK_BMAP) {
983 map->m_pblk = 0;
984 goto sync_out;
985 }
Chao Yuda859852016-01-26 15:42:58 +0800986 if (flag == F2FS_GET_BLOCK_FIEMAP &&
987 blkaddr == NULL_ADDR) {
988 if (map->m_next_pgofs)
989 *map->m_next_pgofs = pgofs + 1;
Chao Yu4c2ac6a2017-11-30 19:28:22 +0800990 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +0800991 }
Chao Yu4c2ac6a2017-11-30 19:28:22 +0800992 if (flag != F2FS_GET_BLOCK_FIEMAP)
Fan Li7df3a432015-12-17 13:20:59 +0800993 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900994 }
995 }
Fan Li7df3a432015-12-17 13:20:59 +0800996
Chao Yu46008c62016-05-09 19:56:30 +0800997 if (flag == F2FS_GET_BLOCK_PRE_AIO)
998 goto skip;
999
Chao Yu4fe71e82016-01-26 15:37:38 +08001000 if (map->m_len == 0) {
1001 /* preallocated unwritten block should be mapped for fiemap. */
1002 if (blkaddr == NEW_ADDR)
1003 map->m_flags |= F2FS_MAP_UNWRITTEN;
1004 map->m_flags |= F2FS_MAP_MAPPED;
1005
1006 map->m_pblk = blkaddr;
1007 map->m_len = 1;
1008 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001009 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001010 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001011 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001012 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001013 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001014 } else {
1015 goto sync_out;
1016 }
1017
Chao Yu46008c62016-05-09 19:56:30 +08001018skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001019 dn.ofs_in_node++;
1020 pgofs++;
1021
Chao Yu46008c62016-05-09 19:56:30 +08001022 /* preallocate blocks in batch for one dnode page */
1023 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1024 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001025
Chao Yu46008c62016-05-09 19:56:30 +08001026 dn.ofs_in_node = ofs_in_node;
1027 err = reserve_new_blocks(&dn, prealloc);
1028 if (err)
1029 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001030
Chao Yu46008c62016-05-09 19:56:30 +08001031 map->m_len += dn.ofs_in_node - ofs_in_node;
1032 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1033 err = -ENOSPC;
1034 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001035 }
Chao Yu46008c62016-05-09 19:56:30 +08001036 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001037 }
1038
Chao Yu46008c62016-05-09 19:56:30 +08001039 if (pgofs >= end)
1040 goto sync_out;
1041 else if (dn.ofs_in_node < end_offset)
1042 goto next_block;
1043
Chao Yu46008c62016-05-09 19:56:30 +08001044 f2fs_put_dnode(&dn);
1045
1046 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001047 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001048 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001049 }
Chao Yu46008c62016-05-09 19:56:30 +08001050 goto next_dnode;
1051
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001052sync_out:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001053 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001054unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001055 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001056 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001057 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001058 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001059out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001060 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001061 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001062}
1063
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001064static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001065 struct buffer_head *bh, int create, int flag,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001066 pgoff_t *next_pgofs, int seg_type)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001067{
1068 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001069 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001070
1071 map.m_lblk = iblock;
1072 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001073 map.m_next_pgofs = next_pgofs;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001074 map.m_seg_type = seg_type;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001075
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001076 err = f2fs_map_blocks(inode, &map, create, flag);
1077 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001078 map_bh(bh, inode->i_sb, map.m_pblk);
1079 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001080 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001081 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001082 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001083}
1084
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001085static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001086 struct buffer_head *bh_result, int create, int flag,
1087 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001088{
Chao Yuda859852016-01-26 15:42:58 +08001089 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001090 flag, next_pgofs,
1091 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001092}
1093
Chao Yue2b4e2b2015-08-19 19:11:19 +08001094static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001095 struct buffer_head *bh_result, int create)
1096{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001097 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001098 F2FS_GET_BLOCK_DEFAULT, NULL,
1099 rw_hint_to_seg_type(
1100 inode->i_write_hint));
Chao Yue2b4e2b2015-08-19 19:11:19 +08001101}
1102
1103static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1104 struct buffer_head *bh_result, int create)
1105{
Yunlei He179448b2015-12-28 21:48:32 +08001106 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001107 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001108 return -EFBIG;
1109
Chao Yue2b4e2b2015-08-19 19:11:19 +08001110 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001111 F2FS_GET_BLOCK_BMAP, NULL,
1112 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001113}
1114
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001115static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1116{
1117 return (offset >> inode->i_blkbits);
1118}
1119
1120static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1121{
1122 return (blk << inode->i_blkbits);
1123}
1124
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001125int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1126 u64 start, u64 len)
1127{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001128 struct buffer_head map_bh;
1129 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001130 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001131 u64 logical = 0, phys = 0, size = 0;
1132 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001133 int ret = 0;
1134
1135 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1136 if (ret)
1137 return ret;
1138
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001139 if (f2fs_has_inline_data(inode)) {
1140 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1141 if (ret != -EAGAIN)
1142 return ret;
1143 }
1144
Al Viro59551022016-01-22 15:40:57 -05001145 inode_lock(inode);
Fan Lide1475c2016-01-04 15:56:50 +08001146
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001147 if (logical_to_blk(inode, len) == 0)
1148 len = blk_to_logical(inode, 1);
1149
1150 start_blk = logical_to_blk(inode, start);
1151 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001152
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001153next:
1154 memset(&map_bh, 0, sizeof(struct buffer_head));
1155 map_bh.b_size = len;
1156
Chao Yue2b4e2b2015-08-19 19:11:19 +08001157 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001158 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001159 if (ret)
1160 goto out;
1161
1162 /* HOLE */
1163 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001164 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001165
1166 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1167 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001168 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001169
Fan Li9a950d52015-12-26 18:07:41 +08001170 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001171 }
Fan Li9a950d52015-12-26 18:07:41 +08001172
Chao Yuda5af122016-01-08 20:19:27 +08001173 if (size) {
1174 if (f2fs_encrypted_inode(inode))
1175 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1176
Fan Li9a950d52015-12-26 18:07:41 +08001177 ret = fiemap_fill_next_extent(fieinfo, logical,
1178 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001179 }
Fan Li9a950d52015-12-26 18:07:41 +08001180
1181 if (start_blk > last_blk || ret)
1182 goto out;
1183
1184 logical = blk_to_logical(inode, start_blk);
1185 phys = blk_to_logical(inode, map_bh.b_blocknr);
1186 size = map_bh.b_size;
1187 flags = 0;
1188 if (buffer_unwritten(&map_bh))
1189 flags = FIEMAP_EXTENT_UNWRITTEN;
1190
1191 start_blk += logical_to_blk(inode, size);
1192
1193prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001194 cond_resched();
1195 if (fatal_signal_pending(current))
1196 ret = -EINTR;
1197 else
1198 goto next;
1199out:
1200 if (ret == 1)
1201 ret = 0;
1202
Al Viro59551022016-01-22 15:40:57 -05001203 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001204 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001205}
1206
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001207/*
1208 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1209 * Major change was from block_size == page_size in f2fs by default.
1210 */
1211static int f2fs_mpage_readpages(struct address_space *mapping,
1212 struct list_head *pages, struct page *page,
1213 unsigned nr_pages)
1214{
1215 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001216 sector_t last_block_in_bio = 0;
1217 struct inode *inode = mapping->host;
1218 const unsigned blkbits = inode->i_blkbits;
1219 const unsigned blocksize = 1 << blkbits;
1220 sector_t block_in_file;
1221 sector_t last_block;
1222 sector_t last_block_in_file;
1223 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001224 struct f2fs_map_blocks map;
1225
1226 map.m_pblk = 0;
1227 map.m_lblk = 0;
1228 map.m_len = 0;
1229 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001230 map.m_next_pgofs = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001231 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001232
LiFan736c0a72017-11-25 11:46:18 +08001233 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001234 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001235 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08001236
1237 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001238 list_del(&page->lru);
1239 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001240 page->index,
1241 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001242 goto next_page;
1243 }
1244
1245 block_in_file = (sector_t)page->index;
1246 last_block = block_in_file + nr_pages;
1247 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1248 blkbits;
1249 if (last_block > last_block_in_file)
1250 last_block = last_block_in_file;
1251
1252 /*
1253 * Map blocks using the previous result first.
1254 */
1255 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1256 block_in_file > map.m_lblk &&
1257 block_in_file < (map.m_lblk + map.m_len))
1258 goto got_it;
1259
1260 /*
1261 * Then do more f2fs_map_blocks() calls until we are
1262 * done with this page.
1263 */
1264 map.m_flags = 0;
1265
1266 if (block_in_file < last_block) {
1267 map.m_lblk = block_in_file;
1268 map.m_len = last_block - block_in_file;
1269
Chao Yu46c9e142015-09-18 16:54:16 +08001270 if (f2fs_map_blocks(inode, &map, 0,
Qiuyang Sunf2220c72017-08-09 17:27:30 +08001271 F2FS_GET_BLOCK_DEFAULT))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001272 goto set_error_page;
1273 }
1274got_it:
1275 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1276 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1277 SetPageMappedToDisk(page);
1278
1279 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1280 SetPageUptodate(page);
1281 goto confused;
1282 }
1283 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001284 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001285 if (!PageUptodate(page))
1286 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001287 unlock_page(page);
1288 goto next_page;
1289 }
1290
1291 /*
1292 * This page will go to BIO. Do we need to send this
1293 * BIO off first?
1294 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001295 if (bio && (last_block_in_bio != block_nr - 1 ||
1296 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001297submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001298 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001299 bio = NULL;
1300 }
1301 if (bio == NULL) {
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001302 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001303 if (IS_ERR(bio)) {
1304 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001305 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001306 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001307 }
1308
1309 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1310 goto submit_and_realloc;
1311
1312 last_block_in_bio = block_nr;
1313 goto next_page;
1314set_error_page:
1315 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001316 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001317 unlock_page(page);
1318 goto next_page;
1319confused:
1320 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001321 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001322 bio = NULL;
1323 }
1324 unlock_page(page);
1325next_page:
1326 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001327 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001328 }
1329 BUG_ON(pages && !list_empty(pages));
1330 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001331 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001332 return 0;
1333}
1334
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001335static int f2fs_read_data_page(struct file *file, struct page *page)
1336{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001337 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001338 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001339
Chao Yuc20e89c2014-05-06 16:53:08 +08001340 trace_f2fs_readpage(page, DATA);
1341
arter97e1c42042014-08-06 23:22:50 +09001342 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001343 if (f2fs_has_inline_data(inode))
1344 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001345 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001346 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001347 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001348}
1349
1350static int f2fs_read_data_pages(struct file *file,
1351 struct address_space *mapping,
1352 struct list_head *pages, unsigned nr_pages)
1353{
Hsiang Kao71cb4af2017-09-24 02:45:42 +08001354 struct inode *inode = mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001355 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001356
1357 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001358
1359 /* If the file has inline data, skip readpages */
1360 if (f2fs_has_inline_data(inode))
1361 return 0;
1362
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001363 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001364}
1365
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001366static int encrypt_one_page(struct f2fs_io_info *fio)
1367{
1368 struct inode *inode = fio->page->mapping->host;
1369 gfp_t gfp_flags = GFP_NOFS;
1370
Jaegeuk Kim19585932017-09-05 16:54:24 -07001371 if (!f2fs_encrypted_file(inode))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001372 return 0;
1373
1374 /* wait for GCed encrypted page writeback */
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -07001375 f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001376
1377retry_encrypt:
1378 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1379 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1380 if (!IS_ERR(fio->encrypted_page))
1381 return 0;
1382
1383 /* flush pending IOs and wait for a while in the ENOMEM case */
1384 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001385 f2fs_flush_merged_writes(fio->sbi);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001386 congestion_wait(BLK_RW_ASYNC, HZ/50);
1387 gfp_flags |= __GFP_NOFAIL;
1388 goto retry_encrypt;
1389 }
1390 return PTR_ERR(fio->encrypted_page);
1391}
1392
1393static inline bool need_inplace_update(struct f2fs_io_info *fio)
1394{
1395 struct inode *inode = fio->page->mapping->host;
1396
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001397 if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode))
1398 return false;
1399 if (is_cold_data(fio->page))
1400 return false;
1401 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1402 return false;
1403
1404 return need_inplace_update_policy(inode, fio);
1405}
1406
Jaegeuk Kima8177372017-04-24 15:20:16 -07001407static inline bool valid_ipu_blkaddr(struct f2fs_io_info *fio)
1408{
1409 if (fio->old_blkaddr == NEW_ADDR)
1410 return false;
1411 if (fio->old_blkaddr == NULL_ADDR)
1412 return false;
1413 return true;
1414}
1415
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001416int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001417{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001418 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001419 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001420 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001421 struct extent_info ei = {0,0,0};
1422 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001423 int err = 0;
1424
1425 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00001426 if (need_inplace_update(fio) &&
1427 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1428 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07001429
1430 if (valid_ipu_blkaddr(fio)) {
Hou Pengyange959c8f2017-04-25 12:45:13 +00001431 ipu_force = true;
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001432 fio->need_lock = LOCK_DONE;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001433 goto got_it;
1434 }
1435 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001436
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001437 /* Deadlock due to between page->lock and f2fs_lock_op */
1438 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1439 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001440
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001441 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001442 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001443 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001444
Chao Yu28bc1062016-02-06 14:40:34 +08001445 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001446
1447 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001448 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001449 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001450 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001451 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00001452got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001453 /*
1454 * If current allocation needs SSR,
1455 * it had better in-place writes for updated data.
1456 */
Jaegeuk Kima8177372017-04-24 15:20:16 -07001457 if (ipu_force || (valid_ipu_blkaddr(fio) && need_inplace_update(fio))) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001458 err = encrypt_one_page(fio);
1459 if (err)
1460 goto out_writepage;
1461
1462 set_page_writeback(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001463 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001464 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001465 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -07001466 err = rewrite_data_page(fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001467 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001468 set_inode_flag(inode, FI_UPDATE_WRITE);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001469 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001470 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001471
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001472 if (fio->need_lock == LOCK_RETRY) {
1473 if (!f2fs_trylock_op(fio->sbi)) {
1474 err = -EAGAIN;
1475 goto out_writepage;
1476 }
1477 fio->need_lock = LOCK_REQ;
1478 }
1479
1480 err = encrypt_one_page(fio);
1481 if (err)
1482 goto out_writepage;
1483
1484 set_page_writeback(page);
1485
Hou Pengyang279d6df2017-04-27 00:17:21 +08001486 /* LFS mode write path */
1487 write_data_page(&dn, fio);
1488 trace_f2fs_do_write_data_page(page, OPU);
1489 set_inode_flag(inode, FI_APPEND_WRITE);
1490 if (page->index == 0)
1491 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001492out_writepage:
1493 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001494out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001495 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001496 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001497 return err;
1498}
1499
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001500static int __write_data_page(struct page *page, bool *submitted,
Chao Yub0af6d42017-08-02 23:21:48 +08001501 struct writeback_control *wbc,
1502 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001503{
1504 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001505 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001506 loff_t i_size = i_size_read(inode);
1507 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001508 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001509 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001510 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001511 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001512 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001513 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001514 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08001515 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001516 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001517 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001518 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00001519 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001520 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001521 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001522 .submitted = false,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001523 .need_lock = LOCK_RETRY,
Chao Yub0af6d42017-08-02 23:21:48 +08001524 .io_type = io_type,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001525 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001526
Chao Yuecda0de2014-05-06 16:48:26 +08001527 trace_f2fs_writepage(page, DATA);
1528
Chao Yu0771fcc2017-06-29 23:20:45 +08001529 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1530 goto redirty_out;
1531
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001532 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001533 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001534
1535 /*
1536 * If the offset is out-of-range of file size,
1537 * this page does not have to be written to disk.
1538 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001539 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001540 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001541 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001542
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001543 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001544write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001545 if (f2fs_is_drop_cache(inode))
1546 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001547 /* we should not write 0'th page having journal header */
1548 if (f2fs_is_volatile_file(inode) && (!page->index ||
1549 (!wbc->for_reclaim &&
1550 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001551 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001552
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001553 /* we should bypass data pages to proceed the kworkder jobs */
1554 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kim7f319972016-06-03 12:28:26 -07001555 mapping_set_error(page->mapping, -EIO);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001556 goto out;
Jaegeuk Kimcf779ca2014-08-11 18:37:46 -07001557 }
1558
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001559 /* Dentry blocks are controlled by checkpoint */
1560 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001561 fio.need_lock = LOCK_DONE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001562 err = do_write_data_page(&fio);
1563 goto done;
1564 }
1565
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001566 if (!wbc->for_reclaim)
1567 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001568 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001569 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001570 else
1571 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001572
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001573 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08001574 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001575 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08001576 if (!err)
1577 goto out;
1578 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001579
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001580 if (err == -EAGAIN) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001581 err = do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001582 if (err == -EAGAIN) {
1583 fio.need_lock = LOCK_REQ;
1584 err = do_write_data_page(&fio);
1585 }
1586 }
Chao Yua0d00fa2017-10-09 17:55:19 +08001587
1588 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001589 if (F2FS_I(inode)->last_disk_size < psize)
1590 F2FS_I(inode)->last_disk_size = psize;
Chao Yua0d00fa2017-10-09 17:55:19 +08001591 up_write(&F2FS_I(inode)->i_sem);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001592
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001593done:
1594 if (err && err != -ENOENT)
1595 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001596
Jaegeuk Kim39936832012-11-22 16:21:29 +09001597out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001598 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001599 if (err)
1600 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001601
1602 if (wbc->for_reclaim) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001603 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001604 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yuc227f912015-12-16 13:09:20 +08001605 remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001606 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001607 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001608
1609 unlock_page(page);
Jaegeuk Kima7881892017-04-20 13:51:57 -07001610 if (!S_ISDIR(inode->i_mode))
1611 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001612
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001613 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001614 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001615 submitted = NULL;
1616 }
1617
1618 if (submitted)
1619 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001620
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001621 return 0;
1622
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001623redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001624 redirty_page_for_writepage(wbc, page);
Chao Yu0002b612016-11-28 19:13:43 -08001625 if (!err)
1626 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001627 unlock_page(page);
1628 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001629}
1630
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001631static int f2fs_write_data_page(struct page *page,
1632 struct writeback_control *wbc)
1633{
Chao Yub0af6d42017-08-02 23:21:48 +08001634 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001635}
1636
Chao Yu8f46dca2015-07-14 18:56:10 +08001637/*
1638 * This function was copied from write_cche_pages from mm/page-writeback.c.
1639 * The major change is making write step of cold data page separately from
1640 * warm/hot data page.
1641 */
1642static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08001643 struct writeback_control *wbc,
1644 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08001645{
1646 int ret = 0;
1647 int done = 0;
1648 struct pagevec pvec;
1649 int nr_pages;
1650 pgoff_t uninitialized_var(writeback_index);
1651 pgoff_t index;
1652 pgoff_t end; /* Inclusive */
1653 pgoff_t done_index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001654 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001655 int cycled;
1656 int range_whole = 0;
1657 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001658
Mel Gorman86679822017-11-15 17:37:52 -08001659 pagevec_init(&pvec);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001660
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001661 if (get_dirty_pages(mapping->host) <=
1662 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1663 set_inode_flag(mapping->host, FI_HOT_DATA);
1664 else
1665 clear_inode_flag(mapping->host, FI_HOT_DATA);
1666
Chao Yu8f46dca2015-07-14 18:56:10 +08001667 if (wbc->range_cyclic) {
1668 writeback_index = mapping->writeback_index; /* prev offset */
1669 index = writeback_index;
1670 if (index == 0)
1671 cycled = 1;
1672 else
1673 cycled = 0;
1674 end = -1;
1675 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001676 index = wbc->range_start >> PAGE_SHIFT;
1677 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001678 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1679 range_whole = 1;
1680 cycled = 1; /* ignore range_cyclic tests */
1681 }
1682 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1683 tag = PAGECACHE_TAG_TOWRITE;
1684 else
1685 tag = PAGECACHE_TAG_DIRTY;
1686retry:
1687 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1688 tag_pages_for_writeback(mapping, index, end);
1689 done_index = index;
1690 while (!done && (index <= end)) {
1691 int i;
1692
Jan Kara69c4f352017-11-15 17:34:48 -08001693 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08001694 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08001695 if (nr_pages == 0)
1696 break;
1697
1698 for (i = 0; i < nr_pages; i++) {
1699 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001700 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001701
Chao Yu8f46dca2015-07-14 18:56:10 +08001702 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001703retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08001704 lock_page(page);
1705
1706 if (unlikely(page->mapping != mapping)) {
1707continue_unlock:
1708 unlock_page(page);
1709 continue;
1710 }
1711
1712 if (!PageDirty(page)) {
1713 /* someone wrote it for us */
1714 goto continue_unlock;
1715 }
1716
Chao Yu8f46dca2015-07-14 18:56:10 +08001717 if (PageWriteback(page)) {
1718 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001719 f2fs_wait_on_page_writeback(page,
1720 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001721 else
1722 goto continue_unlock;
1723 }
1724
1725 BUG_ON(PageWriteback(page));
1726 if (!clear_page_dirty_for_io(page))
1727 goto continue_unlock;
1728
Chao Yub0af6d42017-08-02 23:21:48 +08001729 ret = __write_data_page(page, &submitted, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08001730 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08001731 /*
1732 * keep nr_to_write, since vfs uses this to
1733 * get # of written pages.
1734 */
1735 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1736 unlock_page(page);
1737 ret = 0;
1738 continue;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001739 } else if (ret == -EAGAIN) {
1740 ret = 0;
1741 if (wbc->sync_mode == WB_SYNC_ALL) {
1742 cond_resched();
1743 congestion_wait(BLK_RW_ASYNC,
1744 HZ/50);
1745 goto retry_write;
1746 }
1747 continue;
Chao Yu0002b612016-11-28 19:13:43 -08001748 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001749 done_index = page->index + 1;
1750 done = 1;
1751 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001752 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001753 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08001754 }
1755
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001756 /* give a priority to WB_SYNC threads */
1757 if ((atomic_read(&F2FS_M_SB(mapping)->wb_sync_req) ||
1758 --wbc->nr_to_write <= 0) &&
1759 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08001760 done = 1;
1761 break;
1762 }
1763 }
1764 pagevec_release(&pvec);
1765 cond_resched();
1766 }
1767
Chao Yu8f46dca2015-07-14 18:56:10 +08001768 if (!cycled && !done) {
1769 cycled = 1;
1770 index = 0;
1771 end = writeback_index - 1;
1772 goto retry;
1773 }
1774 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1775 mapping->writeback_index = done_index;
1776
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001777 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001778 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
1779 0, last_idx, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08001780
Chao Yu8f46dca2015-07-14 18:56:10 +08001781 return ret;
1782}
1783
Chao Yub0af6d42017-08-02 23:21:48 +08001784int __f2fs_write_data_pages(struct address_space *mapping,
1785 struct writeback_control *wbc,
1786 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001787{
1788 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001789 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001790 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001791 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001792
P J Pcfb185a2013-04-03 11:38:00 +09001793 /* deal with chardevs and other special file */
1794 if (!mapping->a_ops->writepage)
1795 return 0;
1796
Chao Yu6a290542015-07-17 18:02:39 +08001797 /* skip writing if there is no dirty page in this inode */
1798 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
1799 return 0;
1800
Chao Yu0771fcc2017-06-29 23:20:45 +08001801 /* during POR, we don't need to trigger writepage at all. */
1802 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1803 goto skip_write;
1804
Jaegeuk Kima1257022015-10-08 10:40:07 -07001805 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
1806 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
1807 available_free_memory(sbi, DIRTY_DENTS))
1808 goto skip_write;
1809
Chao Yud323d002015-10-27 09:53:45 +08001810 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07001811 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08001812 goto skip_write;
1813
Yunlei Hed31c7c32016-02-04 16:14:00 +08001814 trace_f2fs_writepages(mapping->host, wbc, DATA);
1815
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001816 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
1817 if (wbc->sync_mode == WB_SYNC_ALL)
1818 atomic_inc(&sbi->wb_sync_req);
1819 else if (atomic_read(&sbi->wb_sync_req))
1820 goto skip_write;
1821
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001822 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08001823 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07001824 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001825
1826 if (wbc->sync_mode == WB_SYNC_ALL)
1827 atomic_dec(&sbi->wb_sync_req);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07001828 /*
1829 * if some pages were truncated, we cannot guarantee its mapping->host
1830 * to detect pending bios.
1831 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001832
Chao Yuc227f912015-12-16 13:09:20 +08001833 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001834 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001835
1836skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001837 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08001838 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09001839 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001840}
1841
Chao Yub0af6d42017-08-02 23:21:48 +08001842static int f2fs_write_data_pages(struct address_space *mapping,
1843 struct writeback_control *wbc)
1844{
1845 struct inode *inode = mapping->host;
1846
1847 return __f2fs_write_data_pages(mapping, wbc,
1848 F2FS_I(inode)->cp_task == current ?
1849 FS_CP_DATA_IO : FS_DATA_IO);
1850}
1851
Chao Yu3aab8f82014-07-02 13:25:04 +08001852static void f2fs_write_failed(struct address_space *mapping, loff_t to)
1853{
1854 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001855 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08001856
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001857 if (to > i_size) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001858 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim819d9152015-12-28 13:48:11 -08001859 truncate_pagecache(inode, i_size);
1860 truncate_blocks(inode, i_size, true);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08001861 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3aab8f82014-07-02 13:25:04 +08001862 }
1863}
1864
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001865static int prepare_write_begin(struct f2fs_sb_info *sbi,
1866 struct page *page, loff_t pos, unsigned len,
1867 block_t *blk_addr, bool *node_changed)
1868{
1869 struct inode *inode = page->mapping->host;
1870 pgoff_t index = page->index;
1871 struct dnode_of_data dn;
1872 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001873 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00001874 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001875 int err = 0;
1876
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001877 /*
1878 * we already allocated all the blocks, so we don't need to get
1879 * the block addresses when there is no need to fill the page.
1880 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08001881 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
1882 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001883 return 0;
1884
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001885 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001886 (pos & PAGE_MASK) >= i_size_read(inode)) {
Yunlei He59c90812017-03-13 20:22:18 +08001887 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001888 locked = true;
1889 }
1890restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001891 /* check inline_data */
1892 ipage = get_node_page(sbi, inode->i_ino);
1893 if (IS_ERR(ipage)) {
1894 err = PTR_ERR(ipage);
1895 goto unlock_out;
1896 }
1897
1898 set_new_dnode(&dn, inode, ipage, ipage, 0);
1899
1900 if (f2fs_has_inline_data(inode)) {
Chao Yuf2470372017-07-19 00:19:05 +08001901 if (pos + len <= MAX_INLINE_DATA(inode)) {
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001902 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001903 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08001904 if (inode->i_nlink)
1905 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001906 } else {
1907 err = f2fs_convert_inline_page(&dn, page);
1908 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001909 goto out;
1910 if (dn.data_blkaddr == NULL_ADDR)
1911 err = f2fs_get_block(&dn, index);
1912 }
1913 } else if (locked) {
1914 err = f2fs_get_block(&dn, index);
1915 } else {
1916 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
1917 dn.data_blkaddr = ei.blk + index - ei.fofs;
1918 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001919 /* hole case */
1920 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07001921 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001922 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08001923 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
1924 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001925 locked = true;
1926 goto restart;
1927 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001928 }
1929 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001930
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001931 /* convert_inline_page can make node_changed */
1932 *blk_addr = dn.data_blkaddr;
1933 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001934out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001935 f2fs_put_dnode(&dn);
1936unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08001937 if (locked)
Yunlei He59c90812017-03-13 20:22:18 +08001938 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001939 return err;
1940}
1941
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001942static int f2fs_write_begin(struct file *file, struct address_space *mapping,
1943 loff_t pos, unsigned len, unsigned flags,
1944 struct page **pagep, void **fsdata)
1945{
1946 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001947 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07001948 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001949 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001950 bool need_balance = false;
1951 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001952 int err = 0;
1953
Chao Yu62aed042014-05-06 16:46:04 +08001954 trace_f2fs_write_begin(inode, pos, len, flags);
1955
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07001956 if (f2fs_is_atomic_file(inode) &&
1957 !available_free_memory(sbi, INMEM_PAGES)) {
1958 err = -ENOMEM;
1959 goto fail;
1960 }
1961
Jaegeuk Kim5f727392014-11-25 10:59:45 -08001962 /*
1963 * We should check this at this moment to avoid deadlock on inode page
1964 * and #0 page. The locking rule for inline_data conversion should be:
1965 * lock_page(page #0) -> lock_page(inode_page)
1966 */
1967 if (index != 0) {
1968 err = f2fs_convert_inline_inode(inode);
1969 if (err)
1970 goto fail;
1971 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09001972repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08001973 /*
1974 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
1975 * wait_for_stable_page. Will wait that below with our IO control.
1976 */
Chao Yu01eccef2017-10-28 16:52:30 +08001977 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kim86d54792017-02-17 09:55:55 -08001978 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08001979 if (!page) {
1980 err = -ENOMEM;
1981 goto fail;
1982 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09001983
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001984 *pagep = page;
1985
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001986 err = prepare_write_begin(sbi, page, pos, len,
1987 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001988 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08001989 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07001990
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001991 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001992 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08001993 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001994 lock_page(page);
1995 if (page->mapping != mapping) {
1996 /* The page got truncated from under us */
1997 f2fs_put_page(page, 1);
1998 goto repeat;
1999 }
2000 }
2001
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002002 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002003
Chao Yu08b39fb2015-10-08 13:27:34 +08002004 /* wait for GCed encrypted page writeback */
Jaegeuk Kim19585932017-09-05 16:54:24 -07002005 if (f2fs_encrypted_file(inode))
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -07002006 f2fs_wait_on_block_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08002007
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002008 if (len == PAGE_SIZE || PageUptodate(page))
2009 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002010
Yunlei He746e2402016-12-20 11:11:35 +08002011 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2012 zero_user_segment(page, len, PAGE_SIZE);
2013 return 0;
2014 }
2015
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002016 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002017 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002018 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002019 } else {
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07002020 err = f2fs_submit_page_read(inode, page, blkaddr);
2021 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08002022 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08002023
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002024 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002025 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002026 f2fs_put_page(page, 1);
2027 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002028 }
Chao Yu1563ac72016-07-03 22:05:12 +08002029 if (unlikely(!PageUptodate(page))) {
2030 err = -EIO;
2031 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002032 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002033 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002034 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002035
Chao Yu3aab8f82014-07-02 13:25:04 +08002036fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002037 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002038 f2fs_write_failed(mapping, pos + len);
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002039 if (f2fs_is_atomic_file(inode))
2040 drop_inmem_pages_all(sbi);
Chao Yu3aab8f82014-07-02 13:25:04 +08002041 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002042}
2043
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002044static int f2fs_write_end(struct file *file,
2045 struct address_space *mapping,
2046 loff_t pos, unsigned len, unsigned copied,
2047 struct page *page, void *fsdata)
2048{
2049 struct inode *inode = page->mapping->host;
2050
Chao Yudfb2bf32014-05-06 16:47:23 +08002051 trace_f2fs_write_end(inode, pos, len, copied);
2052
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002053 /*
2054 * This should be come from len == PAGE_SIZE, and we expect copied
2055 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2056 * let generic_perform_write() try to copy data again through copied=0.
2057 */
2058 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08002059 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002060 copied = 0;
2061 else
2062 SetPageUptodate(page);
2063 }
2064 if (!copied)
2065 goto unlock_out;
2066
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002067 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002068
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002069 if (pos + copied > i_size_read(inode))
2070 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002071unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002072 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002073 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002074 return copied;
2075}
2076
Omar Sandoval6f673762015-03-16 04:33:52 -07002077static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2078 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002079{
2080 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002081
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002082 if (offset & blocksize_mask)
2083 return -EINVAL;
2084
Al Viro5b46f252014-03-16 18:07:34 -04002085 if (iov_iter_alignment(iter) & blocksize_mask)
2086 return -EINVAL;
2087
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002088 return 0;
2089}
2090
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002091static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002092{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002093 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002094 struct inode *inode = mapping->host;
2095 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002096 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002097 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002098 int err;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002099
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002100 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002101 if (err)
2102 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002103
Jaegeuk Kimc040ff92016-11-11 16:46:40 -08002104 if (__force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002105 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002106
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002107 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fa2014-07-31 21:11:22 +08002108
Chao Yu82e0a5a2016-07-13 09:18:29 +08002109 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002110 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002111 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
2112
2113 if (rw == WRITE) {
Chao Yub0af6d42017-08-02 23:21:48 +08002114 if (err > 0) {
2115 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2116 err);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002117 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yub0af6d42017-08-02 23:21:48 +08002118 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002119 f2fs_write_failed(mapping, offset + count);
Chao Yub0af6d42017-08-02 23:21:48 +08002120 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002121 }
Chao Yu70407fa2014-07-31 21:11:22 +08002122
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002123 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fa2014-07-31 21:11:22 +08002124
Chao Yu3aab8f82014-07-02 13:25:04 +08002125 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002126}
2127
Chao Yu487261f2015-02-05 17:44:29 +08002128void f2fs_invalidate_page(struct page *page, unsigned int offset,
2129 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002130{
2131 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002132 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002133
Chao Yu487261f2015-02-05 17:44:29 +08002134 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002135 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002136 return;
2137
Chao Yu487261f2015-02-05 17:44:29 +08002138 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08002139 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002140 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08002141 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002142 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08002143 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002144 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08002145 remove_dirty_inode(inode);
2146 }
Chao Yu487261f2015-02-05 17:44:29 +08002147 }
Chao Yudecd36b2015-08-07 18:42:09 +08002148
2149 /* This is atomic written page, keep Private */
2150 if (IS_ATOMIC_WRITTEN_PAGE(page))
Jaegeuk Kim8c242db2017-03-17 09:55:52 +08002151 return drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002152
Chao Yu23dc9742016-04-29 20:09:15 +08002153 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002154 ClearPagePrivate(page);
2155}
2156
Chao Yu487261f2015-02-05 17:44:29 +08002157int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002158{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002159 /* If this is dirty page, keep PagePrivate */
2160 if (PageDirty(page))
2161 return 0;
2162
Chao Yudecd36b2015-08-07 18:42:09 +08002163 /* This is atomic written page, keep Private */
2164 if (IS_ATOMIC_WRITTEN_PAGE(page))
2165 return 0;
2166
Chao Yu23dc9742016-04-29 20:09:15 +08002167 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002168 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002169 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002170}
2171
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002172/*
2173 * This was copied from __set_page_dirty_buffers which gives higher performance
2174 * in very high speed storages. (e.g., pmem)
2175 */
2176void f2fs_set_page_dirty_nobuffers(struct page *page)
2177{
2178 struct address_space *mapping = page->mapping;
2179 unsigned long flags;
2180
2181 if (unlikely(!mapping))
2182 return;
2183
2184 spin_lock(&mapping->private_lock);
2185 lock_page_memcg(page);
2186 SetPageDirty(page);
2187 spin_unlock(&mapping->private_lock);
2188
2189 spin_lock_irqsave(&mapping->tree_lock, flags);
2190 WARN_ON_ONCE(!PageUptodate(page));
2191 account_page_dirtied(page, mapping);
2192 radix_tree_tag_set(&mapping->page_tree,
2193 page_index(page), PAGECACHE_TAG_DIRTY);
2194 spin_unlock_irqrestore(&mapping->tree_lock, flags);
2195 unlock_page_memcg(page);
2196
2197 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
2198 return;
2199}
2200
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002201static int f2fs_set_data_page_dirty(struct page *page)
2202{
2203 struct address_space *mapping = page->mapping;
2204 struct inode *inode = mapping->host;
2205
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002206 trace_f2fs_set_page_dirty(page, DATA);
2207
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002208 if (!PageUptodate(page))
2209 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002210
Chao Yu5fe45742017-01-07 18:50:26 +08002211 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002212 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2213 register_inmem_page(inode, page);
2214 return 1;
2215 }
2216 /*
2217 * Previously, this page has been registered, we just
2218 * return here.
2219 */
2220 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002221 }
2222
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002223 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002224 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002225 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002226 return 1;
2227 }
2228 return 0;
2229}
2230
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002231static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2232{
Chao Yu454ae7e2014-04-22 13:34:01 +08002233 struct inode *inode = mapping->host;
2234
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002235 if (f2fs_has_inline_data(inode))
2236 return 0;
2237
2238 /* make sure allocating whole blocks */
2239 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2240 filemap_write_and_wait(mapping);
2241
Chao Yue2b4e2b2015-08-19 19:11:19 +08002242 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002243}
2244
Weichao Guo5b7a4872016-09-20 05:03:27 +08002245#ifdef CONFIG_MIGRATION
2246#include <linux/migrate.h>
2247
2248int f2fs_migrate_page(struct address_space *mapping,
2249 struct page *newpage, struct page *page, enum migrate_mode mode)
2250{
2251 int rc, extra_count;
2252 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2253 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2254
2255 BUG_ON(PageWriteback(page));
2256
2257 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07002258 if (atomic_written) {
2259 if (mode != MIGRATE_SYNC)
2260 return -EBUSY;
2261 if (!mutex_trylock(&fi->inmem_lock))
2262 return -EAGAIN;
2263 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002264
2265 /*
2266 * A reference is expected if PagePrivate set when move mapping,
2267 * however F2FS breaks this for maintaining dirty page counts when
2268 * truncating pages. So here adjusting the 'extra_count' make it work.
2269 */
2270 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2271 rc = migrate_page_move_mapping(mapping, newpage,
2272 page, NULL, mode, extra_count);
2273 if (rc != MIGRATEPAGE_SUCCESS) {
2274 if (atomic_written)
2275 mutex_unlock(&fi->inmem_lock);
2276 return rc;
2277 }
2278
2279 if (atomic_written) {
2280 struct inmem_pages *cur;
2281 list_for_each_entry(cur, &fi->inmem_pages, list)
2282 if (cur->page == page) {
2283 cur->page = newpage;
2284 break;
2285 }
2286 mutex_unlock(&fi->inmem_lock);
2287 put_page(page);
2288 get_page(newpage);
2289 }
2290
2291 if (PagePrivate(page))
2292 SetPagePrivate(newpage);
2293 set_page_private(newpage, page_private(page));
2294
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07002295 if (mode != MIGRATE_SYNC_NO_COPY)
2296 migrate_page_copy(newpage, page);
2297 else
2298 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002299
2300 return MIGRATEPAGE_SUCCESS;
2301}
2302#endif
2303
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002304const struct address_space_operations f2fs_dblock_aops = {
2305 .readpage = f2fs_read_data_page,
2306 .readpages = f2fs_read_data_pages,
2307 .writepage = f2fs_write_data_page,
2308 .writepages = f2fs_write_data_pages,
2309 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002310 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002311 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002312 .invalidatepage = f2fs_invalidate_page,
2313 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002314 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002315 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002316#ifdef CONFIG_MIGRATION
2317 .migratepage = f2fs_migrate_page,
2318#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002319};