blob: 02237d4d91f5a3e459114516cf7797f72ddc1daf [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
Ming Lei263663c2017-12-18 20:22:04 +080059 if (time_to_inject(F2FS_P_SB(bio_first_page_all(bio)), FAULT_IO)) {
Chao Yu55523512017-02-25 11:08:28 +080060 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 Kimb1ca3212017-12-31 16:26:38 -0800114 if (type == F2FS_WB_CP_DATA)
115 f2fs_stop_checkpoint(sbi, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900116 }
Yunlei He7dff55d2018-01-11 14:19:32 +0800117
118 f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) &&
119 page->index != nid_of_node(page));
120
Chao Yu36951b32016-11-16 10:41:20 +0800121 dec_page_count(sbi, type);
122 clear_cold_data(page);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900123 end_page_writeback(page);
Linus Torvaldsf5688492014-01-30 11:19:05 -0800124 }
Chao Yu36951b32016-11-16 10:41:20 +0800125 if (!get_pages(sbi, F2FS_WB_CP_DATA) &&
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700126 wq_has_sleeper(&sbi->cp_wait))
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900127 wake_up(&sbi->cp_wait);
128
129 bio_put(bio);
130}
131
Gu Zheng940a6d32013-12-20 17:39:59 +0800132/*
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700133 * Return true, if pre_bio's bdev is same as its target device.
134 */
135struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
136 block_t blk_addr, struct bio *bio)
137{
138 struct block_device *bdev = sbi->sb->s_bdev;
139 int i;
140
141 for (i = 0; i < sbi->s_ndevs; i++) {
142 if (FDEV(i).start_blk <= blk_addr &&
143 FDEV(i).end_blk >= blk_addr) {
144 blk_addr -= FDEV(i).start_blk;
145 bdev = FDEV(i).bdev;
146 break;
147 }
148 }
149 if (bio) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200150 bio_set_dev(bio, bdev);
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700151 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(blk_addr);
152 }
153 return bdev;
154}
155
156int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr)
157{
158 int i;
159
160 for (i = 0; i < sbi->s_ndevs; i++)
161 if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr)
162 return i;
163 return 0;
164}
165
166static bool __same_bdev(struct f2fs_sb_info *sbi,
167 block_t blk_addr, struct bio *bio)
168{
Christoph Hellwig74d46992017-08-23 19:10:32 +0200169 struct block_device *b = f2fs_target_device(sbi, blk_addr, NULL);
170 return bio->bi_disk == b->bd_disk && bio->bi_partno == b->bd_partno;
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700171}
172
173/*
Gu Zheng940a6d32013-12-20 17:39:59 +0800174 * Low-level block read/write IO operations.
175 */
176static struct bio *__bio_alloc(struct f2fs_sb_info *sbi, block_t blk_addr,
Yufen Yu578c6472018-01-09 19:33:39 +0800177 struct writeback_control *wbc,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900178 int npages, bool is_read,
179 enum page_type type, enum temp_type temp)
Gu Zheng940a6d32013-12-20 17:39:59 +0800180{
181 struct bio *bio;
182
Chao Yud62fe972017-10-28 16:52:31 +0800183 bio = f2fs_bio_alloc(sbi, npages, true);
Gu Zheng940a6d32013-12-20 17:39:59 +0800184
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700185 f2fs_target_device(sbi, blk_addr, bio);
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900186 if (is_read) {
187 bio->bi_end_io = f2fs_read_end_io;
188 bio->bi_private = NULL;
189 } else {
190 bio->bi_end_io = f2fs_write_end_io;
191 bio->bi_private = sbi;
192 bio->bi_write_hint = io_type_to_rw_hint(sbi, type, temp);
193 }
Yufen Yu578c6472018-01-09 19:33:39 +0800194 if (wbc)
195 wbc_init_bio(wbc, bio);
Gu Zheng940a6d32013-12-20 17:39:59 +0800196
197 return bio;
198}
199
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700200static inline void __submit_bio(struct f2fs_sb_info *sbi,
201 struct bio *bio, enum page_type type)
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700202{
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700203 if (!is_read_io(bio_op(bio))) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800204 unsigned int start;
205
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800206 if (type != DATA && type != NODE)
207 goto submit_io;
208
Sheng Yongccd31cb2018-02-06 12:31:17 +0800209 if (f2fs_sb_has_blkzoned(sbi->sb) && current->plug)
Tiezhu Yang3bb09a02018-02-06 08:21:45 +0800210 blk_finish_plug(current->plug);
211
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800212 start = bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS;
213 start %= F2FS_IO_SIZE(sbi);
214
215 if (start == 0)
216 goto submit_io;
217
218 /* fill dummy pages */
219 for (; start < F2FS_IO_SIZE(sbi); start++) {
220 struct page *page =
221 mempool_alloc(sbi->write_io_dummy,
222 GFP_NOIO | __GFP_ZERO | __GFP_NOFAIL);
223 f2fs_bug_on(sbi, !page);
224
225 SetPagePrivate(page);
226 set_page_private(page, (unsigned long)DUMMY_WRITTEN_PAGE);
227 lock_page(page);
228 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE)
229 f2fs_bug_on(sbi, 1);
230 }
231 /*
232 * In the NODE case, we lose next block address chain. So, we
233 * need to do checkpoint in f2fs_sync_file.
234 */
235 if (type == NODE)
236 set_sbi_flag(sbi, SBI_NEED_CP);
Jaegeuk Kim19a5f5e2016-06-04 14:25:24 -0700237 }
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800238submit_io:
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800239 if (is_read_io(bio_op(bio)))
240 trace_f2fs_submit_read_bio(sbi->sb, type, bio);
241 else
242 trace_f2fs_submit_write_bio(sbi->sb, type, bio);
Mike Christie4e49ea42016-06-05 14:31:41 -0500243 submit_bio(bio);
Jaegeuk Kimf5730182016-05-17 16:23:36 -0700244}
245
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900246static void __submit_merged_bio(struct f2fs_bio_info *io)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900247{
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900248 struct f2fs_io_info *fio = &io->fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900249
250 if (!io->bio)
251 return;
252
Mike Christie04d328d2016-06-05 14:31:55 -0500253 bio_set_op_attrs(io->bio, fio->op, fio->op_flags);
254
Jaegeuk Kim554b5122016-12-21 12:13:03 -0800255 if (is_read_io(fio->op))
256 trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio);
257 else
258 trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio);
259
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700260 __submit_bio(io->sbi, io->bio, fio->type);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900261 io->bio = NULL;
262}
263
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800264static bool __has_merged_page(struct f2fs_bio_info *io,
265 struct inode *inode, nid_t ino, pgoff_t idx)
Chao Yu0fd785e2016-01-18 18:24:59 +0800266{
Chao Yu0fd785e2016-01-18 18:24:59 +0800267 struct bio_vec *bvec;
268 struct page *target;
269 int i;
270
Chao Yu0c3a5792016-01-18 18:28:11 +0800271 if (!io->bio)
Chao Yu0fd785e2016-01-18 18:24:59 +0800272 return false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800273
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800274 if (!inode && !ino)
Chao Yu0c3a5792016-01-18 18:28:11 +0800275 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800276
277 bio_for_each_segment_all(bvec, io->bio, i) {
278
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700279 if (bvec->bv_page->mapping)
Chao Yu0fd785e2016-01-18 18:24:59 +0800280 target = bvec->bv_page;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700281 else
282 target = fscrypt_control_page(bvec->bv_page);
Chao Yu0fd785e2016-01-18 18:24:59 +0800283
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800284 if (idx != target->index)
285 continue;
286
Chao Yu0c3a5792016-01-18 18:28:11 +0800287 if (inode && inode == target->mapping->host)
Chao Yu0fd785e2016-01-18 18:24:59 +0800288 return true;
Chao Yu0c3a5792016-01-18 18:28:11 +0800289 if (ino && ino == ino_of_node(target))
290 return true;
Chao Yu0fd785e2016-01-18 18:24:59 +0800291 }
292
Chao Yu0fd785e2016-01-18 18:24:59 +0800293 return false;
294}
295
Chao Yu0c3a5792016-01-18 18:28:11 +0800296static bool has_merged_page(struct f2fs_sb_info *sbi, struct inode *inode,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800297 nid_t ino, pgoff_t idx, enum page_type type)
Chao Yu0c3a5792016-01-18 18:28:11 +0800298{
299 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700300 enum temp_type temp;
301 struct f2fs_bio_info *io;
302 bool ret = false;
Chao Yu0c3a5792016-01-18 18:28:11 +0800303
Jaegeuk Kima912b542017-05-10 11:18:25 -0700304 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
305 io = sbi->write_io[btype] + temp;
306
307 down_read(&io->io_rwsem);
308 ret = __has_merged_page(io, inode, ino, idx);
309 up_read(&io->io_rwsem);
310
311 /* TODO: use HOT temp only for meta pages now. */
312 if (ret || btype == META)
313 break;
314 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800315 return ret;
316}
317
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700318static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700319 enum page_type type, enum temp_type temp)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900320{
321 enum page_type btype = PAGE_TYPE_OF_BIO(type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700322 struct f2fs_bio_info *io = sbi->write_io[btype] + temp;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900323
Chao Yudf0f8dc2014-03-22 14:57:23 +0800324 down_write(&io->io_rwsem);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900325
326 /* change META to META_FLUSH in the checkpoint procedure */
327 if (type >= META_FLUSH) {
328 io->fio.type = META_FLUSH;
Mike Christie04d328d2016-06-05 14:31:55 -0500329 io->fio.op = REQ_OP_WRITE;
Jan Kara3adc5fcb2017-05-02 17:03:47 +0200330 io->fio.op_flags = REQ_META | REQ_PRIO | REQ_SYNC;
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600331 if (!test_opt(sbi, NOBARRIER))
Jaegeuk Kim7f54f512017-02-06 13:57:58 -0800332 io->fio.op_flags |= REQ_PREFLUSH | REQ_FUA;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900333 }
334 __submit_merged_bio(io);
Chao Yudf0f8dc2014-03-22 14:57:23 +0800335 up_write(&io->io_rwsem);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900336}
337
Jaegeuk Kima912b542017-05-10 11:18:25 -0700338static void __submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim942fd312017-02-01 16:51:22 -0800339 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kima912b542017-05-10 11:18:25 -0700340 enum page_type type, bool force)
Chao Yu0c3a5792016-01-18 18:28:11 +0800341{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700342 enum temp_type temp;
343
344 if (!force && !has_merged_page(sbi, inode, ino, idx, type))
345 return;
346
347 for (temp = HOT; temp < NR_TEMP_TYPE; temp++) {
348
349 __f2fs_submit_merged_write(sbi, type, temp);
350
351 /* TODO: use HOT temp only for meta pages now. */
352 if (type >= META)
353 break;
354 }
Chao Yu0c3a5792016-01-18 18:28:11 +0800355}
356
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700357void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type)
Chao Yu406657d2016-02-24 17:17:55 +0800358{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700359 __submit_merged_write_cond(sbi, NULL, 0, 0, type, true);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900360}
361
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700362void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900363 struct inode *inode, nid_t ino, pgoff_t idx,
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700364 enum page_type type)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900365{
Jaegeuk Kima912b542017-05-10 11:18:25 -0700366 __submit_merged_write_cond(sbi, inode, ino, idx, type, false);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900367}
368
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700369void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi)
Chao Yu406657d2016-02-24 17:17:55 +0800370{
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700371 f2fs_submit_merged_write(sbi, DATA);
372 f2fs_submit_merged_write(sbi, NODE);
373 f2fs_submit_merged_write(sbi, META);
Chao Yu406657d2016-02-24 17:17:55 +0800374}
375
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900376/*
377 * Fill the locked page with data located in the block address.
Tomohiro Kusumi771a9a72017-04-05 22:49:44 +0300378 * A caller needs to unlock the page on failure.
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900379 */
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700380int f2fs_submit_page_bio(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900381{
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900382 struct bio *bio;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700383 struct page *page = fio->encrypted_page ?
384 fio->encrypted_page : fio->page;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900385
Yunlei He08337212018-03-08 16:29:13 +0800386 verify_block_addr(fio, fio->new_blkaddr);
Chao Yu2ace38e2014-12-24 16:08:14 +0800387 trace_f2fs_submit_page_bio(page, fio);
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700388 f2fs_trace_ios(fio, 0);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900389
390 /* Allocate a new bio */
Yufen Yu578c6472018-01-09 19:33:39 +0800391 bio = __bio_alloc(fio->sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900392 1, is_read_io(fio->op), fio->type, fio->temp);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900393
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300394 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900395 bio_put(bio);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900396 return -EFAULT;
397 }
Mike Christie04d328d2016-06-05 14:31:55 -0500398 bio_set_op_attrs(bio, fio->op, fio->op_flags);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900399
Linus Torvalds4fc29c12016-07-27 10:36:31 -0700400 __submit_bio(fio->sbi, bio, fio->type);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -0700401
402 if (!is_read_io(fio->op))
403 inc_page_count(fio->sbi, WB_DATA_TYPE(fio->page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900404 return 0;
405}
406
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700407int f2fs_submit_page_write(struct f2fs_io_info *fio)
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900408{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700409 struct f2fs_sb_info *sbi = fio->sbi;
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900410 enum page_type btype = PAGE_TYPE_OF_BIO(fio->type);
Jaegeuk Kima912b542017-05-10 11:18:25 -0700411 struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700412 struct page *bio_page;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800413 int err = 0;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900414
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700415 f2fs_bug_on(sbi, is_read_io(fio->op));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900416
Chao Yufb830fc2017-05-19 23:37:01 +0800417 down_write(&io->io_rwsem);
418next:
419 if (fio->in_list) {
420 spin_lock(&io->io_lock);
421 if (list_empty(&io->io_list)) {
422 spin_unlock(&io->io_lock);
423 goto out_fail;
424 }
425 fio = list_first_entry(&io->io_list,
426 struct f2fs_io_info, list);
427 list_del(&fio->list);
428 spin_unlock(&io->io_lock);
429 }
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900430
Chao Yu7a9d7542016-02-22 18:36:38 +0800431 if (fio->old_blkaddr != NEW_ADDR)
Yunlei He08337212018-03-08 16:29:13 +0800432 verify_block_addr(fio, fio->old_blkaddr);
433 verify_block_addr(fio, fio->new_blkaddr);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900434
Chao Yu36951b32016-11-16 10:41:20 +0800435 bio_page = fio->encrypted_page ? fio->encrypted_page : fio->page;
436
Thomas Meyerebf7c522017-10-07 16:02:21 +0200437 /* set submitted = true as a return value */
438 fio->submitted = true;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -0800439
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700440 inc_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900441
Chao Yu7a9d7542016-02-22 18:36:38 +0800442 if (io->bio && (io->last_block_in_bio != fio->new_blkaddr - 1 ||
Jaegeuk Kim3c62be12016-10-06 19:02:05 -0700443 (io->fio.op != fio->op || io->fio.op_flags != fio->op_flags) ||
444 !__same_bdev(sbi, fio->new_blkaddr, io->bio)))
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900445 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900446alloc_new:
447 if (io->bio == NULL) {
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800448 if ((fio->type == DATA || fio->type == NODE) &&
449 fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) {
450 err = -EAGAIN;
Jaegeuk Kimb9109b02017-05-10 11:28:38 -0700451 dec_page_count(sbi, WB_DATA_TYPE(bio_page));
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800452 goto out_fail;
453 }
Yufen Yu578c6472018-01-09 19:33:39 +0800454 io->bio = __bio_alloc(sbi, fio->new_blkaddr, fio->io_wbc,
Hyunchul Lee0cdd3192018-01-31 11:36:57 +0900455 BIO_MAX_PAGES, false,
456 fio->type, fio->temp);
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900457 io->fio = *fio;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900458 }
459
Jaegeuk Kima912b542017-05-10 11:18:25 -0700460 if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) {
Jaegeuk Kim458e6192013-12-11 13:54:01 +0900461 __submit_merged_bio(io);
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900462 goto alloc_new;
463 }
464
Yufen Yu578c6472018-01-09 19:33:39 +0800465 if (fio->io_wbc)
466 wbc_account_io(fio->io_wbc, bio_page, PAGE_SIZE);
467
Chao Yu7a9d7542016-02-22 18:36:38 +0800468 io->last_block_in_bio = fio->new_blkaddr;
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700469 f2fs_trace_ios(fio, 0);
Chao Yufb830fc2017-05-19 23:37:01 +0800470
471 trace_f2fs_submit_page_write(fio->page, fio);
472
473 if (fio->in_list)
474 goto next;
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800475out_fail:
Chao Yudf0f8dc2014-03-22 14:57:23 +0800476 up_write(&io->io_rwsem);
Jaegeuk Kim0a595eb2016-12-14 10:12:56 -0800477 return err;
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900478}
479
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700480static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
481 unsigned nr_pages)
482{
483 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
484 struct fscrypt_ctx *ctx = NULL;
485 struct bio *bio;
486
487 if (f2fs_encrypted_file(inode)) {
488 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
489 if (IS_ERR(ctx))
490 return ERR_CAST(ctx);
491
492 /* wait the page to be moved by cleaning */
493 f2fs_wait_on_block_writeback(sbi, blkaddr);
494 }
495
Chao Yud62fe972017-10-28 16:52:31 +0800496 bio = f2fs_bio_alloc(sbi, min_t(int, nr_pages, BIO_MAX_PAGES), false);
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700497 if (!bio) {
498 if (ctx)
499 fscrypt_release_ctx(ctx);
500 return ERR_PTR(-ENOMEM);
501 }
502 f2fs_target_device(sbi, blkaddr, bio);
503 bio->bi_end_io = f2fs_read_end_io;
504 bio->bi_private = ctx;
505 bio_set_op_attrs(bio, REQ_OP_READ, 0);
506
507 return bio;
508}
509
510/* This can handle encryption stuffs */
511static int f2fs_submit_page_read(struct inode *inode, struct page *page,
512 block_t blkaddr)
513{
514 struct bio *bio = f2fs_grab_read_bio(inode, blkaddr, 1);
515
516 if (IS_ERR(bio))
517 return PTR_ERR(bio);
518
519 if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
520 bio_put(bio);
521 return -EFAULT;
522 }
523 __submit_bio(F2FS_I_SB(inode), bio, DATA);
524 return 0;
525}
526
Chao Yu46008c62016-05-09 19:56:30 +0800527static void __set_data_blkaddr(struct dnode_of_data *dn)
528{
529 struct f2fs_node *rn = F2FS_NODE(dn->node_page);
530 __le32 *addr_array;
Chao Yu7a2af762017-07-19 00:19:06 +0800531 int base = 0;
532
533 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
534 base = get_extra_isize(dn->inode);
Chao Yu46008c62016-05-09 19:56:30 +0800535
536 /* Get physical address of data block */
537 addr_array = blkaddr_in_node(rn);
Chao Yu7a2af762017-07-19 00:19:06 +0800538 addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr);
Chao Yu46008c62016-05-09 19:56:30 +0800539}
540
Jaegeuk Kim93dfe2a2013-11-30 12:51:14 +0900541/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900542 * Lock ordering for the change of data block address:
543 * ->data_page
544 * ->node_page
545 * update block addresses in the node page
546 */
Chao Yu216a6202015-03-19 19:23:32 +0800547void set_data_blkaddr(struct dnode_of_data *dn)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900548{
Chao Yu46008c62016-05-09 19:56:30 +0800549 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
550 __set_data_blkaddr(dn);
551 if (set_page_dirty(dn->node_page))
Jaegeuk Kim12719ae2016-01-07 13:23:12 -0800552 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900553}
554
Chao Yuf28b3432016-02-24 17:16:47 +0800555void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr)
556{
557 dn->data_blkaddr = blkaddr;
558 set_data_blkaddr(dn);
559 f2fs_update_extent_cache(dn);
560}
561
Chao Yu46008c62016-05-09 19:56:30 +0800562/* dn->ofs_in_node will be returned with up-to-date last block pointer */
563int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900564{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700565 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Chao Yu0abd6752017-07-09 00:13:07 +0800566 int err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900567
Chao Yu46008c62016-05-09 19:56:30 +0800568 if (!count)
569 return 0;
570
Jaegeuk Kim91942322016-05-20 10:13:22 -0700571 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900572 return -EPERM;
Chao Yu0abd6752017-07-09 00:13:07 +0800573 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
574 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900575
Chao Yu46008c62016-05-09 19:56:30 +0800576 trace_f2fs_reserve_new_blocks(dn->inode, dn->nid,
577 dn->ofs_in_node, count);
Namjae Jeonc01e2852013-04-23 17:00:52 +0900578
Chao Yu46008c62016-05-09 19:56:30 +0800579 f2fs_wait_on_page_writeback(dn->node_page, NODE, true);
580
581 for (; count > 0; dn->ofs_in_node++) {
Chao Yu7a2af762017-07-19 00:19:06 +0800582 block_t blkaddr = datablock_addr(dn->inode,
583 dn->node_page, dn->ofs_in_node);
Chao Yu46008c62016-05-09 19:56:30 +0800584 if (blkaddr == NULL_ADDR) {
585 dn->data_blkaddr = NEW_ADDR;
586 __set_data_blkaddr(dn);
587 count--;
588 }
589 }
590
591 if (set_page_dirty(dn->node_page))
592 dn->node_changed = true;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900593 return 0;
594}
595
Chao Yu46008c62016-05-09 19:56:30 +0800596/* Should keep dn->ofs_in_node unchanged */
597int reserve_new_block(struct dnode_of_data *dn)
598{
599 unsigned int ofs_in_node = dn->ofs_in_node;
600 int ret;
601
602 ret = reserve_new_blocks(dn, 1);
603 dn->ofs_in_node = ofs_in_node;
604 return ret;
605}
606
Huajun Lib6009652013-11-10 23:13:18 +0800607int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)
608{
609 bool need_put = dn->inode_page ? false : true;
610 int err;
611
612 err = get_dnode_of_data(dn, index, ALLOC_NODE);
613 if (err)
614 return err;
Jaegeuk Kima8865372013-12-27 17:04:17 +0900615
Huajun Lib6009652013-11-10 23:13:18 +0800616 if (dn->data_blkaddr == NULL_ADDR)
617 err = reserve_new_block(dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900618 if (err || need_put)
Huajun Lib6009652013-11-10 23:13:18 +0800619 f2fs_put_dnode(dn);
620 return err;
621}
622
Fan Li759af1c2015-08-05 15:52:16 +0800623int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900624{
Hou Pengyange15882b2017-02-23 09:18:05 +0000625 struct extent_info ei = {0,0,0};
Fan Li759af1c2015-08-05 15:52:16 +0800626 struct inode *inode = dn->inode;
Chao Yu028a41e2015-03-19 19:26:02 +0800627
Fan Li759af1c2015-08-05 15:52:16 +0800628 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
629 dn->data_blkaddr = ei.blk + index - ei.fofs;
630 return 0;
Chao Yu429511c2015-02-05 17:54:31 +0800631 }
632
Fan Li759af1c2015-08-05 15:52:16 +0800633 return f2fs_reserve_block(dn, index);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900634}
635
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700636struct page *get_read_data_page(struct inode *inode, pgoff_t index,
Mike Christie04d328d2016-06-05 14:31:55 -0500637 int op_flags, bool for_write)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900638{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900639 struct address_space *mapping = inode->i_mapping;
640 struct dnode_of_data dn;
641 struct page *page;
Hou Pengyange15882b2017-02-23 09:18:05 +0000642 struct extent_info ei = {0,0,0};
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900643 int err;
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700644
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700645 page = f2fs_grab_cache_page(mapping, index, for_write);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900646 if (!page)
647 return ERR_PTR(-ENOMEM);
648
Chao Yucb3bc9e2015-02-05 18:03:40 +0800649 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
650 dn.data_blkaddr = ei.blk + index - ei.fofs;
651 goto got_it;
652 }
653
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900654 set_new_dnode(&dn, inode, NULL, NULL, 0);
655 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700656 if (err)
657 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900658 f2fs_put_dnode(&dn);
659
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900660 if (unlikely(dn.data_blkaddr == NULL_ADDR)) {
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700661 err = -ENOENT;
662 goto put_err;
Jaegeuk Kim650495d2013-05-13 08:38:35 +0900663 }
Chao Yucb3bc9e2015-02-05 18:03:40 +0800664got_it:
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700665 if (PageUptodate(page)) {
666 unlock_page(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900667 return page;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700668 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900669
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900670 /*
671 * A new dentry page is allocated but not able to be written, since its
672 * new inode page couldn't be allocated due to -ENOSPC.
673 * In such the case, its blkaddr can be remained as NEW_ADDR.
674 * see, f2fs_add_link -> get_new_data_page -> init_inode_metadata.
675 */
676 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300677 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700678 if (!PageUptodate(page))
679 SetPageUptodate(page);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700680 unlock_page(page);
Jaegeuk Kimd59ff4d2013-08-20 19:13:07 +0900681 return page;
682 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900683
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -0700684 err = f2fs_submit_page_read(inode, page, dn.data_blkaddr);
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900685 if (err)
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700686 goto put_err;
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700687 return page;
Jaegeuk Kim86531d62015-07-15 13:08:21 -0700688
689put_err:
690 f2fs_put_page(page, 1);
691 return ERR_PTR(err);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700692}
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900693
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700694struct page *find_data_page(struct inode *inode, pgoff_t index)
695{
696 struct address_space *mapping = inode->i_mapping;
697 struct page *page;
698
699 page = find_get_page(mapping, index);
700 if (page && PageUptodate(page))
701 return page;
702 f2fs_put_page(page, 0);
703
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600704 page = get_read_data_page(inode, index, 0, false);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700705 if (IS_ERR(page))
706 return page;
707
708 if (PageUptodate(page))
709 return page;
710
711 wait_on_page_locked(page);
712 if (unlikely(!PageUptodate(page))) {
713 f2fs_put_page(page, 0);
714 return ERR_PTR(-EIO);
715 }
716 return page;
717}
718
719/*
720 * If it tries to access a hole, return an error.
721 * Because, the callers, functions in dir.c and GC, should be able to know
722 * whether this page exists or not.
723 */
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700724struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
725 bool for_write)
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700726{
727 struct address_space *mapping = inode->i_mapping;
728 struct page *page;
729repeat:
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600730 page = get_read_data_page(inode, index, 0, for_write);
Jaegeuk Kim43f3eae2015-04-30 17:00:33 -0700731 if (IS_ERR(page))
732 return page;
733
734 /* wait for read completion */
Jaegeuk Kim393ff912013-03-08 21:29:23 +0900735 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900736 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +0900737 f2fs_put_page(page, 1);
738 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900739 }
Chao Yu1563ac72016-07-03 22:05:12 +0800740 if (unlikely(!PageUptodate(page))) {
741 f2fs_put_page(page, 1);
742 return ERR_PTR(-EIO);
743 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900744 return page;
745}
746
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900747/*
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900748 * Caller ensures that this data page is never allocated.
749 * A new zero-filled data page is allocated in the page cache.
Jaegeuk Kim39936832012-11-22 16:21:29 +0900750 *
Chao Yu4f4124d2013-12-21 18:02:14 +0800751 * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and
752 * f2fs_unlock_op().
Chao Yu470f00e2015-07-14 18:14:06 +0800753 * Note that, ipage is set only by make_empty_dir, and if any error occur,
754 * ipage should be released by this function.
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900755 */
Jaegeuk Kim64aa7ed2013-05-20 09:55:50 +0900756struct page *get_new_data_page(struct inode *inode,
Jaegeuk Kima8865372013-12-27 17:04:17 +0900757 struct page *ipage, pgoff_t index, bool new_i_size)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900758{
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900759 struct address_space *mapping = inode->i_mapping;
760 struct page *page;
761 struct dnode_of_data dn;
762 int err;
Jaegeuk Kim76121182016-01-01 22:03:47 -0800763
Jaegeuk Kima56c7c62015-10-09 15:11:38 -0700764 page = f2fs_grab_cache_page(mapping, index, true);
Chao Yu470f00e2015-07-14 18:14:06 +0800765 if (!page) {
766 /*
767 * before exiting, we should make sure ipage will be released
768 * if any error occur.
769 */
770 f2fs_put_page(ipage, 1);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700771 return ERR_PTR(-ENOMEM);
Chao Yu470f00e2015-07-14 18:14:06 +0800772 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900773
Jaegeuk Kima8865372013-12-27 17:04:17 +0900774 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lib6009652013-11-10 23:13:18 +0800775 err = f2fs_reserve_block(&dn, index);
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700776 if (err) {
777 f2fs_put_page(page, 1);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900778 return ERR_PTR(err);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900779 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700780 if (!ipage)
781 f2fs_put_dnode(&dn);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900782
783 if (PageUptodate(page))
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700784 goto got_it;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900785
786 if (dn.data_blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300787 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -0700788 if (!PageUptodate(page))
789 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900790 } else {
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700791 f2fs_put_page(page, 1);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900792
Jaegeuk Kim76121182016-01-01 22:03:47 -0800793 /* if ipage exists, blkaddr should be NEW_ADDR */
794 f2fs_bug_on(F2FS_I_SB(inode), ipage);
795 page = get_lock_data_page(inode, index, true);
Jaegeuk Kim4375a332015-04-23 12:04:33 -0700796 if (IS_ERR(page))
Jaegeuk Kim76121182016-01-01 22:03:47 -0800797 return page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900798 }
Jaegeuk Kim01f28612015-04-29 11:18:42 -0700799got_it:
Chao Yu9edcdab2015-09-11 14:43:52 +0800800 if (new_i_size && i_size_read(inode) <
Jaegeuk Kimee6d1822016-05-20 16:32:49 -0700801 ((loff_t)(index + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700802 f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT));
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900803 return page;
804}
805
Hyunchul Leed5097be2017-11-28 09:23:00 +0900806static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900807{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700808 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900809 struct f2fs_summary sum;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900810 struct node_info ni;
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700811 pgoff_t fofs;
Chao Yu46008c62016-05-09 19:56:30 +0800812 blkcnt_t count = 1;
Chao Yu0abd6752017-07-09 00:13:07 +0800813 int err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900814
Jaegeuk Kim91942322016-05-20 10:13:22 -0700815 if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC)))
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900816 return -EPERM;
Chao Yudf6136e2015-03-23 10:33:37 +0800817
Chao Yu7a2af762017-07-19 00:19:06 +0800818 dn->data_blkaddr = datablock_addr(dn->inode,
819 dn->node_page, dn->ofs_in_node);
Chao Yudf6136e2015-03-23 10:33:37 +0800820 if (dn->data_blkaddr == NEW_ADDR)
821 goto alloc;
822
Chao Yu0abd6752017-07-09 00:13:07 +0800823 if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count))))
824 return err;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900825
Chao Yudf6136e2015-03-23 10:33:37 +0800826alloc:
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900827 get_node_info(sbi, dn->nid, &ni);
828 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
829
Chao Yudf6136e2015-03-23 10:33:37 +0800830 allocate_data_block(sbi, NULL, dn->data_blkaddr, &dn->data_blkaddr,
Hyunchul Leed5097be2017-11-28 09:23:00 +0900831 &sum, seg_type, NULL, false);
Chao Yu216a6202015-03-19 19:23:32 +0800832 set_data_blkaddr(dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900833
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700834 /* update i_size */
Chao Yu81ca7352016-01-26 15:39:35 +0800835 fofs = start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
Jaegeuk Kim976e4c52014-09-15 19:32:16 -0700836 dn->ofs_in_node;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300837 if (i_size_read(dn->inode) < ((loff_t)(fofs + 1) << PAGE_SHIFT))
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -0700838 f2fs_i_size_write(dn->inode,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300839 ((loff_t)(fofs + 1) << PAGE_SHIFT));
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900840 return 0;
841}
842
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800843int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from)
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800844{
Jaegeuk Kimb439b102016-02-03 13:09:09 -0800845 struct inode *inode = file_inode(iocb->ki_filp);
Chao Yu5b8db7f2016-01-26 15:38:29 +0800846 struct f2fs_map_blocks map;
Chao Yud6d478a2018-01-03 17:30:19 +0800847 int flag;
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800848 int err = 0;
Chao Yud6d478a2018-01-03 17:30:19 +0800849 bool direct_io = iocb->ki_flags & IOCB_DIRECT;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800850
Weichao Guo71ad6822017-09-29 22:43:23 +0800851 /* convert inline data for Direct I/O*/
Chao Yud6d478a2018-01-03 17:30:19 +0800852 if (direct_io) {
Weichao Guo71ad6822017-09-29 22:43:23 +0800853 err = f2fs_convert_inline_inode(inode);
854 if (err)
855 return err;
856 }
857
Jaegeuk Kimdc91de72017-01-13 13:12:29 -0800858 if (is_inode_flag_set(inode, FI_NO_PREALLOC))
859 return 0;
860
Jaegeuk Kim0080c502016-05-07 08:52:57 -0700861 map.m_lblk = F2FS_BLK_ALIGN(iocb->ki_pos);
Chao Yudfd02e42016-08-20 15:12:01 +0800862 map.m_len = F2FS_BYTES_TO_BLK(iocb->ki_pos + iov_iter_count(from));
863 if (map.m_len > map.m_lblk)
864 map.m_len -= map.m_lblk;
865 else
866 map.m_len = 0;
867
Chao Yuda859852016-01-26 15:42:58 +0800868 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +0800869 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +0900870 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800871
Chao Yud6d478a2018-01-03 17:30:19 +0800872 if (direct_io) {
Hyunchul Leed5097be2017-11-28 09:23:00 +0900873 map.m_seg_type = rw_hint_to_seg_type(iocb->ki_hint);
Hyunchul Leeb91050a2018-03-08 19:34:38 +0900874 flag = f2fs_force_buffered_io(inode, WRITE) ?
Chao Yud6d478a2018-01-03 17:30:19 +0800875 F2FS_GET_BLOCK_PRE_AIO :
876 F2FS_GET_BLOCK_PRE_DIO;
877 goto map_blocks;
Hyunchul Leed5097be2017-11-28 09:23:00 +0900878 }
Chao Yuf2470372017-07-19 00:19:05 +0800879 if (iocb->ki_pos + iov_iter_count(from) > MAX_INLINE_DATA(inode)) {
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800880 err = f2fs_convert_inline_inode(inode);
881 if (err)
882 return err;
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800883 }
Chao Yud6d478a2018-01-03 17:30:19 +0800884 if (f2fs_has_inline_data(inode))
Sheng Yong250066452017-11-22 18:23:39 +0800885 return err;
Chao Yud6d478a2018-01-03 17:30:19 +0800886
887 flag = F2FS_GET_BLOCK_PRE_AIO;
888
889map_blocks:
890 err = f2fs_map_blocks(inode, &map, 1, flag);
891 if (map.m_len > 0 && err == -ENOSPC) {
892 if (!direct_io)
893 set_inode_flag(inode, FI_NO_PREALLOC);
894 err = 0;
Sheng Yong250066452017-11-22 18:23:39 +0800895 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -0800896 return err;
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800897}
898
Yunlei He59c90812017-03-13 20:22:18 +0800899static inline void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock)
900{
901 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
902 if (lock)
903 down_read(&sbi->node_change);
904 else
905 up_read(&sbi->node_change);
906 } else {
907 if (lock)
908 f2fs_lock_op(sbi);
909 else
910 f2fs_unlock_op(sbi);
911 }
912}
913
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +0900914/*
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700915 * f2fs_map_blocks() now supported readahead/bmap/rw direct_IO with
916 * f2fs_map_blocks structure.
Chao Yu4f4124d2013-12-21 18:02:14 +0800917 * If original data blocks are allocated, then give them to blockdev.
918 * Otherwise,
919 * a. preallocate requested block addresses
920 * b. do not use extent cache for better performance
921 * c. give the block addresses to blockdev
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900922 */
Chao Yud323d002015-10-27 09:53:45 +0800923int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
Chao Yue2b4e2b2015-08-19 19:11:19 +0800924 int create, int flag)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900925{
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700926 unsigned int maxblocks = map->m_len;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900927 struct dnode_of_data dn;
Chao Yuf9811702015-09-21 20:17:52 +0800928 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimac6f1992016-06-16 17:03:23 -0700929 int mode = create ? ALLOC_NODE : LOOKUP_NODE;
Chao Yu46008c62016-05-09 19:56:30 +0800930 pgoff_t pgofs, end_offset, end;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900931 int err = 0, ofs = 1;
Chao Yu46008c62016-05-09 19:56:30 +0800932 unsigned int ofs_in_node, last_ofs_in_node;
933 blkcnt_t prealloc;
Hou Pengyange15882b2017-02-23 09:18:05 +0000934 struct extent_info ei = {0,0,0};
Fan Li7df3a432015-12-17 13:20:59 +0800935 block_t blkaddr;
Chao Yuc4020b22018-01-11 14:42:30 +0800936 unsigned int start_pgofs;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900937
Chao Yudfd02e42016-08-20 15:12:01 +0800938 if (!maxblocks)
939 return 0;
940
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700941 map->m_len = 0;
942 map->m_flags = 0;
943
944 /* it only supports block size == page size */
945 pgofs = (pgoff_t)map->m_lblk;
Chao Yu46008c62016-05-09 19:56:30 +0800946 end = pgofs + maxblocks;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900947
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800948 if (!create && f2fs_lookup_extent_cache(inode, pgofs, &ei)) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -0700949 map->m_pblk = ei.blk + pgofs - ei.fofs;
950 map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs);
951 map->m_flags = F2FS_MAP_MAPPED;
Chao Yuc4020b22018-01-11 14:42:30 +0800952 if (map->m_next_extent)
953 *map->m_next_extent = pgofs + map->m_len;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900954 goto out;
Chao Yua2e7d1b2015-02-05 17:50:30 +0800955 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900956
Chao Yu4fe71e82016-01-26 15:37:38 +0800957next_dnode:
Jaegeuk Kim59b802e2015-02-09 12:09:53 -0800958 if (create)
Yunlei He59c90812017-03-13 20:22:18 +0800959 __do_map_lock(sbi, flag, true);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +0900960
961 /* When reading holes, we need its node page */
962 set_new_dnode(&dn, inode, NULL, NULL, 0);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900963 err = get_dnode_of_data(&dn, pgofs, mode);
Jaegeuk Kim1ec79082013-12-26 16:55:22 +0900964 if (err) {
Chao Yu43473f92016-05-05 19:13:02 +0800965 if (flag == F2FS_GET_BLOCK_BMAP)
966 map->m_pblk = 0;
Chao Yuda859852016-01-26 15:42:58 +0800967 if (err == -ENOENT) {
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900968 err = 0;
Chao Yuda859852016-01-26 15:42:58 +0800969 if (map->m_next_pgofs)
970 *map->m_next_pgofs =
971 get_next_page_offset(&dn, pgofs);
Chao Yuc4020b22018-01-11 14:42:30 +0800972 if (map->m_next_extent)
973 *map->m_next_extent =
974 get_next_page_offset(&dn, pgofs);
Chao Yuda859852016-01-26 15:42:58 +0800975 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900976 goto unlock_out;
Namjae Jeon848753a2013-04-23 16:38:02 +0900977 }
Chao Yu973163f2015-09-18 16:51:51 +0800978
Chao Yuc4020b22018-01-11 14:42:30 +0800979 start_pgofs = pgofs;
Chao Yu46008c62016-05-09 19:56:30 +0800980 prealloc = 0;
Arnd Bergmann230436b2016-11-02 14:52:15 +0100981 last_ofs_in_node = ofs_in_node = dn.ofs_in_node;
Chao Yu81ca7352016-01-26 15:39:35 +0800982 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +0900983
Chao Yu4fe71e82016-01-26 15:37:38 +0800984next_block:
Chao Yu7a2af762017-07-19 00:19:06 +0800985 blkaddr = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
Chao Yu973163f2015-09-18 16:51:51 +0800986
Fan Li7df3a432015-12-17 13:20:59 +0800987 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) {
988 if (create) {
989 if (unlikely(f2fs_cp_error(sbi))) {
990 err = -EIO;
991 goto sync_out;
Chao Yu973163f2015-09-18 16:51:51 +0800992 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800993 if (flag == F2FS_GET_BLOCK_PRE_AIO) {
Chao Yu46008c62016-05-09 19:56:30 +0800994 if (blkaddr == NULL_ADDR) {
995 prealloc++;
996 last_ofs_in_node = dn.ofs_in_node;
997 }
Jaegeuk Kim24b84912016-02-03 13:49:44 -0800998 } else {
Hyunchul Leed5097be2017-11-28 09:23:00 +0900999 err = __allocate_data_block(&dn,
1000 map->m_seg_type);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001001 if (!err)
Jaegeuk Kim91942322016-05-20 10:13:22 -07001002 set_inode_flag(inode, FI_APPEND_WRITE);
Jaegeuk Kim24b84912016-02-03 13:49:44 -08001003 }
Fan Li7df3a432015-12-17 13:20:59 +08001004 if (err)
1005 goto sync_out;
Kinglong Mee3f2be042017-02-23 19:55:05 +08001006 map->m_flags |= F2FS_MAP_NEW;
Fan Li7df3a432015-12-17 13:20:59 +08001007 blkaddr = dn.data_blkaddr;
1008 } else {
Chao Yu43473f92016-05-05 19:13:02 +08001009 if (flag == F2FS_GET_BLOCK_BMAP) {
1010 map->m_pblk = 0;
1011 goto sync_out;
1012 }
Chao Yuc4020b22018-01-11 14:42:30 +08001013 if (flag == F2FS_GET_BLOCK_PRECACHE)
1014 goto sync_out;
Chao Yuda859852016-01-26 15:42:58 +08001015 if (flag == F2FS_GET_BLOCK_FIEMAP &&
1016 blkaddr == NULL_ADDR) {
1017 if (map->m_next_pgofs)
1018 *map->m_next_pgofs = pgofs + 1;
Fan Li7df3a432015-12-17 13:20:59 +08001019 goto sync_out;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001020 }
Chao Yuf3d98e72018-01-10 18:18:52 +08001021 if (flag != F2FS_GET_BLOCK_FIEMAP) {
1022 /* for defragment case */
1023 if (map->m_next_pgofs)
1024 *map->m_next_pgofs = pgofs + 1;
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001025 goto sync_out;
Chao Yuf3d98e72018-01-10 18:18:52 +08001026 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001027 }
1028 }
Fan Li7df3a432015-12-17 13:20:59 +08001029
Chao Yu46008c62016-05-09 19:56:30 +08001030 if (flag == F2FS_GET_BLOCK_PRE_AIO)
1031 goto skip;
1032
Chao Yu4fe71e82016-01-26 15:37:38 +08001033 if (map->m_len == 0) {
1034 /* preallocated unwritten block should be mapped for fiemap. */
1035 if (blkaddr == NEW_ADDR)
1036 map->m_flags |= F2FS_MAP_UNWRITTEN;
1037 map->m_flags |= F2FS_MAP_MAPPED;
1038
1039 map->m_pblk = blkaddr;
1040 map->m_len = 1;
1041 } else if ((map->m_pblk != NEW_ADDR &&
Fan Li7df3a432015-12-17 13:20:59 +08001042 blkaddr == (map->m_pblk + ofs)) ||
Jaegeuk Kimb439b102016-02-03 13:09:09 -08001043 (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) ||
Chao Yu46008c62016-05-09 19:56:30 +08001044 flag == F2FS_GET_BLOCK_PRE_DIO) {
Fan Li7df3a432015-12-17 13:20:59 +08001045 ofs++;
Fan Li7df3a432015-12-17 13:20:59 +08001046 map->m_len++;
Chao Yu4fe71e82016-01-26 15:37:38 +08001047 } else {
1048 goto sync_out;
1049 }
1050
Chao Yu46008c62016-05-09 19:56:30 +08001051skip:
Chao Yu4fe71e82016-01-26 15:37:38 +08001052 dn.ofs_in_node++;
1053 pgofs++;
1054
Chao Yu46008c62016-05-09 19:56:30 +08001055 /* preallocate blocks in batch for one dnode page */
1056 if (flag == F2FS_GET_BLOCK_PRE_AIO &&
1057 (pgofs == end || dn.ofs_in_node == end_offset)) {
Chao Yu4fe71e82016-01-26 15:37:38 +08001058
Chao Yu46008c62016-05-09 19:56:30 +08001059 dn.ofs_in_node = ofs_in_node;
1060 err = reserve_new_blocks(&dn, prealloc);
1061 if (err)
1062 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001063
Chao Yu46008c62016-05-09 19:56:30 +08001064 map->m_len += dn.ofs_in_node - ofs_in_node;
1065 if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) {
1066 err = -ENOSPC;
1067 goto sync_out;
Chao Yu4fe71e82016-01-26 15:37:38 +08001068 }
Chao Yu46008c62016-05-09 19:56:30 +08001069 dn.ofs_in_node = end_offset;
Fan Li7df3a432015-12-17 13:20:59 +08001070 }
1071
Chao Yu46008c62016-05-09 19:56:30 +08001072 if (pgofs >= end)
1073 goto sync_out;
1074 else if (dn.ofs_in_node < end_offset)
1075 goto next_block;
1076
Chao Yuc4020b22018-01-11 14:42:30 +08001077 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1078 if (map->m_flags & F2FS_MAP_MAPPED) {
1079 unsigned int ofs = start_pgofs - map->m_lblk;
1080
1081 f2fs_update_extent_cache_range(&dn,
1082 start_pgofs, map->m_pblk + ofs,
1083 map->m_len - ofs);
1084 }
1085 }
1086
Chao Yu46008c62016-05-09 19:56:30 +08001087 f2fs_put_dnode(&dn);
1088
1089 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001090 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001091 f2fs_balance_fs(sbi, dn.node_changed);
Chao Yu46008c62016-05-09 19:56:30 +08001092 }
Chao Yu46008c62016-05-09 19:56:30 +08001093 goto next_dnode;
1094
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001095sync_out:
Chao Yuc4020b22018-01-11 14:42:30 +08001096 if (flag == F2FS_GET_BLOCK_PRECACHE) {
1097 if (map->m_flags & F2FS_MAP_MAPPED) {
1098 unsigned int ofs = start_pgofs - map->m_lblk;
1099
1100 f2fs_update_extent_cache_range(&dn,
1101 start_pgofs, map->m_pblk + ofs,
1102 map->m_len - ofs);
1103 }
1104 if (map->m_next_extent)
1105 *map->m_next_extent = pgofs + 1;
1106 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001107 f2fs_put_dnode(&dn);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001108unlock_out:
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001109 if (create) {
Yunlei He59c90812017-03-13 20:22:18 +08001110 __do_map_lock(sbi, flag, false);
Chao Yu6f2d8ed2016-10-11 22:57:03 +08001111 f2fs_balance_fs(sbi, dn.node_changed);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08001112 }
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001113out:
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001114 trace_f2fs_map_blocks(inode, map, err);
Jaegeuk Kimbfad7c22013-12-16 19:04:05 +09001115 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001116}
1117
Hyunchul Leeb91050a2018-03-08 19:34:38 +09001118bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len)
1119{
1120 struct f2fs_map_blocks map;
1121 block_t last_lblk;
1122 int err;
1123
1124 if (pos + len > i_size_read(inode))
1125 return false;
1126
1127 map.m_lblk = F2FS_BYTES_TO_BLK(pos);
1128 map.m_next_pgofs = NULL;
1129 map.m_next_extent = NULL;
1130 map.m_seg_type = NO_CHECK_TYPE;
1131 last_lblk = F2FS_BLK_ALIGN(pos + len);
1132
1133 while (map.m_lblk < last_lblk) {
1134 map.m_len = last_lblk - map.m_lblk;
1135 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
1136 if (err || map.m_len == 0)
1137 return false;
1138 map.m_lblk += map.m_len;
1139 }
1140 return true;
1141}
1142
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001143static int __get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001144 struct buffer_head *bh, int create, int flag,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001145 pgoff_t *next_pgofs, int seg_type)
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001146{
1147 struct f2fs_map_blocks map;
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001148 int err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001149
1150 map.m_lblk = iblock;
1151 map.m_len = bh->b_size >> inode->i_blkbits;
Chao Yuda859852016-01-26 15:42:58 +08001152 map.m_next_pgofs = next_pgofs;
Chao Yuc4020b22018-01-11 14:42:30 +08001153 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001154 map.m_seg_type = seg_type;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001155
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001156 err = f2fs_map_blocks(inode, &map, create, flag);
1157 if (!err) {
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001158 map_bh(bh, inode->i_sb, map.m_pblk);
1159 bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
Wei Fangb86e3302017-01-22 12:21:02 +08001160 bh->b_size = (u64)map.m_len << inode->i_blkbits;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001161 }
Jaegeuk Kima7de6082016-11-11 16:31:56 -08001162 return err;
Jaegeuk Kim003a3e12015-04-06 19:55:34 -07001163}
1164
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001165static int get_data_block(struct inode *inode, sector_t iblock,
Chao Yuda859852016-01-26 15:42:58 +08001166 struct buffer_head *bh_result, int create, int flag,
1167 pgoff_t *next_pgofs)
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001168{
Chao Yuda859852016-01-26 15:42:58 +08001169 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001170 flag, next_pgofs,
1171 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001172}
1173
Chao Yue2b4e2b2015-08-19 19:11:19 +08001174static int get_data_block_dio(struct inode *inode, sector_t iblock,
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001175 struct buffer_head *bh_result, int create)
1176{
Chao Yue2b4e2b2015-08-19 19:11:19 +08001177 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001178 F2FS_GET_BLOCK_DEFAULT, NULL,
1179 rw_hint_to_seg_type(
1180 inode->i_write_hint));
Chao Yue2b4e2b2015-08-19 19:11:19 +08001181}
1182
1183static int get_data_block_bmap(struct inode *inode, sector_t iblock,
1184 struct buffer_head *bh_result, int create)
1185{
Yunlei He179448b2015-12-28 21:48:32 +08001186 /* Block number less than F2FS MAX BLOCKS */
Chao Yue0afc4d2015-12-31 14:35:37 +08001187 if (unlikely(iblock >= F2FS_I_SB(inode)->max_file_blocks))
Yunlei He179448b2015-12-28 21:48:32 +08001188 return -EFBIG;
1189
Chao Yue2b4e2b2015-08-19 19:11:19 +08001190 return __get_data_block(inode, iblock, bh_result, create,
Hyunchul Leed5097be2017-11-28 09:23:00 +09001191 F2FS_GET_BLOCK_BMAP, NULL,
1192 NO_CHECK_TYPE);
Jaegeuk Kimccfb3002014-06-13 13:02:11 +09001193}
1194
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001195static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
1196{
1197 return (offset >> inode->i_blkbits);
1198}
1199
1200static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
1201{
1202 return (blk << inode->i_blkbits);
1203}
1204
Chao Yu442a9db2018-01-11 14:39:57 +08001205static int f2fs_xattr_fiemap(struct inode *inode,
1206 struct fiemap_extent_info *fieinfo)
1207{
1208 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1209 struct page *page;
1210 struct node_info ni;
1211 __u64 phys = 0, len;
1212 __u32 flags;
1213 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
1214 int err = 0;
1215
1216 if (f2fs_has_inline_xattr(inode)) {
1217 int offset;
1218
1219 page = f2fs_grab_cache_page(NODE_MAPPING(sbi),
1220 inode->i_ino, false);
1221 if (!page)
1222 return -ENOMEM;
1223
1224 get_node_info(sbi, inode->i_ino, &ni);
1225
1226 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1227 offset = offsetof(struct f2fs_inode, i_addr) +
1228 sizeof(__le32) * (DEF_ADDRS_PER_INODE -
Chao Yub323fd22018-01-17 16:31:36 +08001229 get_inline_xattr_addrs(inode));
Chao Yu442a9db2018-01-11 14:39:57 +08001230
1231 phys += offset;
1232 len = inline_xattr_size(inode);
1233
1234 f2fs_put_page(page, 1);
1235
1236 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED;
1237
1238 if (!xnid)
1239 flags |= FIEMAP_EXTENT_LAST;
1240
1241 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1242 if (err || err == 1)
1243 return err;
1244 }
1245
1246 if (xnid) {
1247 page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false);
1248 if (!page)
1249 return -ENOMEM;
1250
1251 get_node_info(sbi, xnid, &ni);
1252
1253 phys = (__u64)blk_to_logical(inode, ni.blk_addr);
1254 len = inode->i_sb->s_blocksize;
1255
1256 f2fs_put_page(page, 1);
1257
1258 flags = FIEMAP_EXTENT_LAST;
1259 }
1260
1261 if (phys)
1262 err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags);
1263
1264 return (err < 0 ? err : 0);
1265}
1266
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001267int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1268 u64 start, u64 len)
1269{
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001270 struct buffer_head map_bh;
1271 sector_t start_blk, last_blk;
Chao Yuda859852016-01-26 15:42:58 +08001272 pgoff_t next_pgofs;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001273 u64 logical = 0, phys = 0, size = 0;
1274 u32 flags = 0;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001275 int ret = 0;
1276
Chao Yuc4020b22018-01-11 14:42:30 +08001277 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
1278 ret = f2fs_precache_extents(inode);
1279 if (ret)
1280 return ret;
1281 }
1282
Chao Yu442a9db2018-01-11 14:39:57 +08001283 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001284 if (ret)
1285 return ret;
1286
Chao Yuf1b43d42018-01-11 14:37:35 +08001287 inode_lock(inode);
1288
Chao Yu442a9db2018-01-11 14:39:57 +08001289 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1290 ret = f2fs_xattr_fiemap(inode, fieinfo);
1291 goto out;
1292 }
1293
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001294 if (f2fs_has_inline_data(inode)) {
1295 ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len);
1296 if (ret != -EAGAIN)
Chao Yuf1b43d42018-01-11 14:37:35 +08001297 goto out;
Jaegeuk Kim67f8cf32015-10-15 11:34:49 -07001298 }
1299
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001300 if (logical_to_blk(inode, len) == 0)
1301 len = blk_to_logical(inode, 1);
1302
1303 start_blk = logical_to_blk(inode, start);
1304 last_blk = logical_to_blk(inode, start + len - 1);
Fan Li9a950d52015-12-26 18:07:41 +08001305
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001306next:
1307 memset(&map_bh, 0, sizeof(struct buffer_head));
1308 map_bh.b_size = len;
1309
Chao Yue2b4e2b2015-08-19 19:11:19 +08001310 ret = get_data_block(inode, start_blk, &map_bh, 0,
Chao Yuda859852016-01-26 15:42:58 +08001311 F2FS_GET_BLOCK_FIEMAP, &next_pgofs);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001312 if (ret)
1313 goto out;
1314
1315 /* HOLE */
1316 if (!buffer_mapped(&map_bh)) {
Chao Yuda859852016-01-26 15:42:58 +08001317 start_blk = next_pgofs;
Chao Yu58736fa2016-10-11 22:57:04 +08001318
1319 if (blk_to_logical(inode, start_blk) < blk_to_logical(inode,
1320 F2FS_I_SB(inode)->max_file_blocks))
Fan Li9a950d52015-12-26 18:07:41 +08001321 goto prep_next;
Chao Yu58736fa2016-10-11 22:57:04 +08001322
Fan Li9a950d52015-12-26 18:07:41 +08001323 flags |= FIEMAP_EXTENT_LAST;
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001324 }
Fan Li9a950d52015-12-26 18:07:41 +08001325
Chao Yuda5af122016-01-08 20:19:27 +08001326 if (size) {
1327 if (f2fs_encrypted_inode(inode))
1328 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1329
Fan Li9a950d52015-12-26 18:07:41 +08001330 ret = fiemap_fill_next_extent(fieinfo, logical,
1331 phys, size, flags);
Chao Yuda5af122016-01-08 20:19:27 +08001332 }
Fan Li9a950d52015-12-26 18:07:41 +08001333
1334 if (start_blk > last_blk || ret)
1335 goto out;
1336
1337 logical = blk_to_logical(inode, start_blk);
1338 phys = blk_to_logical(inode, map_bh.b_blocknr);
1339 size = map_bh.b_size;
1340 flags = 0;
1341 if (buffer_unwritten(&map_bh))
1342 flags = FIEMAP_EXTENT_UNWRITTEN;
1343
1344 start_blk += logical_to_blk(inode, size);
1345
1346prep_next:
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001347 cond_resched();
1348 if (fatal_signal_pending(current))
1349 ret = -EINTR;
1350 else
1351 goto next;
1352out:
1353 if (ret == 1)
1354 ret = 0;
1355
Al Viro59551022016-01-22 15:40:57 -05001356 inode_unlock(inode);
Jaegeuk Kim7f63eb72015-05-08 19:30:32 -07001357 return ret;
Jaegeuk Kim9ab70132014-06-08 04:30:14 +09001358}
1359
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001360/*
1361 * This function was originally taken from fs/mpage.c, and customized for f2fs.
1362 * Major change was from block_size == page_size in f2fs by default.
1363 */
1364static int f2fs_mpage_readpages(struct address_space *mapping,
1365 struct list_head *pages, struct page *page,
1366 unsigned nr_pages)
1367{
1368 struct bio *bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001369 sector_t last_block_in_bio = 0;
1370 struct inode *inode = mapping->host;
1371 const unsigned blkbits = inode->i_blkbits;
1372 const unsigned blocksize = 1 << blkbits;
1373 sector_t block_in_file;
1374 sector_t last_block;
1375 sector_t last_block_in_file;
1376 sector_t block_nr;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001377 struct f2fs_map_blocks map;
1378
1379 map.m_pblk = 0;
1380 map.m_lblk = 0;
1381 map.m_len = 0;
1382 map.m_flags = 0;
Chao Yuda859852016-01-26 15:42:58 +08001383 map.m_next_pgofs = NULL;
Chao Yuc4020b22018-01-11 14:42:30 +08001384 map.m_next_extent = NULL;
Hyunchul Leed5097be2017-11-28 09:23:00 +09001385 map.m_seg_type = NO_CHECK_TYPE;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001386
LiFan736c0a72017-11-25 11:46:18 +08001387 for (; nr_pages; nr_pages--) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001388 if (pages) {
Chao Yu939afa92017-01-07 18:49:42 +08001389 page = list_last_entry(pages, struct page, lru);
Kinglong Meea83d50b2017-03-13 16:35:13 +08001390
1391 prefetchw(&page->flags);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001392 list_del(&page->lru);
1393 if (add_to_page_cache_lru(page, mapping,
Michal Hocko8a5c7432016-07-26 15:24:53 -07001394 page->index,
1395 readahead_gfp_mask(mapping)))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001396 goto next_page;
1397 }
1398
1399 block_in_file = (sector_t)page->index;
1400 last_block = block_in_file + nr_pages;
1401 last_block_in_file = (i_size_read(inode) + blocksize - 1) >>
1402 blkbits;
1403 if (last_block > last_block_in_file)
1404 last_block = last_block_in_file;
1405
1406 /*
1407 * Map blocks using the previous result first.
1408 */
1409 if ((map.m_flags & F2FS_MAP_MAPPED) &&
1410 block_in_file > map.m_lblk &&
1411 block_in_file < (map.m_lblk + map.m_len))
1412 goto got_it;
1413
1414 /*
1415 * Then do more f2fs_map_blocks() calls until we are
1416 * done with this page.
1417 */
1418 map.m_flags = 0;
1419
1420 if (block_in_file < last_block) {
1421 map.m_lblk = block_in_file;
1422 map.m_len = last_block - block_in_file;
1423
Chao Yu46c9e142015-09-18 16:54:16 +08001424 if (f2fs_map_blocks(inode, &map, 0,
Qiuyang Sunf2220c72017-08-09 17:27:30 +08001425 F2FS_GET_BLOCK_DEFAULT))
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001426 goto set_error_page;
1427 }
1428got_it:
1429 if ((map.m_flags & F2FS_MAP_MAPPED)) {
1430 block_nr = map.m_pblk + block_in_file - map.m_lblk;
1431 SetPageMappedToDisk(page);
1432
1433 if (!PageUptodate(page) && !cleancache_get_page(page)) {
1434 SetPageUptodate(page);
1435 goto confused;
1436 }
1437 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001438 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim237c0792016-06-30 18:49:15 -07001439 if (!PageUptodate(page))
1440 SetPageUptodate(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001441 unlock_page(page);
1442 goto next_page;
1443 }
1444
1445 /*
1446 * This page will go to BIO. Do we need to send this
1447 * BIO off first?
1448 */
Jaegeuk Kim3c62be12016-10-06 19:02:05 -07001449 if (bio && (last_block_in_bio != block_nr - 1 ||
1450 !__same_bdev(F2FS_I_SB(inode), block_nr, bio))) {
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001451submit_and_realloc:
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001452 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001453 bio = NULL;
1454 }
1455 if (bio == NULL) {
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07001456 bio = f2fs_grab_read_bio(inode, block_nr, nr_pages);
Jaegeuk Kim1d353eb2016-07-12 09:38:48 -07001457 if (IS_ERR(bio)) {
1458 bio = NULL;
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001459 goto set_error_page;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001460 }
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001461 }
1462
1463 if (bio_add_page(bio, page, blocksize, 0) < blocksize)
1464 goto submit_and_realloc;
1465
1466 last_block_in_bio = block_nr;
1467 goto next_page;
1468set_error_page:
1469 SetPageError(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001470 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001471 unlock_page(page);
1472 goto next_page;
1473confused:
1474 if (bio) {
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001475 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001476 bio = NULL;
1477 }
1478 unlock_page(page);
1479next_page:
1480 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001481 put_page(page);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001482 }
1483 BUG_ON(pages && !list_empty(pages));
1484 if (bio)
Linus Torvalds4fc29c12016-07-27 10:36:31 -07001485 __submit_bio(F2FS_I_SB(inode), bio, DATA);
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001486 return 0;
1487}
1488
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001489static int f2fs_read_data_page(struct file *file, struct page *page)
1490{
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001491 struct inode *inode = page->mapping->host;
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001492 int ret = -EAGAIN;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001493
Chao Yuc20e89c2014-05-06 16:53:08 +08001494 trace_f2fs_readpage(page, DATA);
1495
arter97e1c42042014-08-06 23:22:50 +09001496 /* If the file has inline data, try to read it directly */
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001497 if (f2fs_has_inline_data(inode))
1498 ret = f2fs_read_inline_data(inode, page);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001499 if (ret == -EAGAIN)
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001500 ret = f2fs_mpage_readpages(page->mapping, NULL, page, 1);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001501 return ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001502}
1503
1504static int f2fs_read_data_pages(struct file *file,
1505 struct address_space *mapping,
1506 struct list_head *pages, unsigned nr_pages)
1507{
Hsiang Kao71cb4af2017-09-24 02:45:42 +08001508 struct inode *inode = mapping->host;
Chao Yu939afa92017-01-07 18:49:42 +08001509 struct page *page = list_last_entry(pages, struct page, lru);
Chao Yub8c29402015-10-12 17:02:26 +08001510
1511 trace_f2fs_readpages(inode, page, nr_pages);
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001512
1513 /* If the file has inline data, skip readpages */
1514 if (f2fs_has_inline_data(inode))
1515 return 0;
1516
Jaegeuk Kimf1e88662015-04-09 11:20:42 -07001517 return f2fs_mpage_readpages(mapping, pages, NULL, nr_pages);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001518}
1519
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001520static int encrypt_one_page(struct f2fs_io_info *fio)
1521{
1522 struct inode *inode = fio->page->mapping->host;
1523 gfp_t gfp_flags = GFP_NOFS;
1524
Jaegeuk Kim19585932017-09-05 16:54:24 -07001525 if (!f2fs_encrypted_file(inode))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001526 return 0;
1527
1528 /* wait for GCed encrypted page writeback */
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -07001529 f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001530
1531retry_encrypt:
1532 fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
1533 PAGE_SIZE, 0, fio->page->index, gfp_flags);
1534 if (!IS_ERR(fio->encrypted_page))
1535 return 0;
1536
1537 /* flush pending IOs and wait for a while in the ENOMEM case */
1538 if (PTR_ERR(fio->encrypted_page) == -ENOMEM) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001539 f2fs_flush_merged_writes(fio->sbi);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001540 congestion_wait(BLK_RW_ASYNC, HZ/50);
1541 gfp_flags |= __GFP_NOFAIL;
1542 goto retry_encrypt;
1543 }
1544 return PTR_ERR(fio->encrypted_page);
1545}
1546
Chao Yubb9e3bb2018-01-17 16:31:38 +08001547static inline bool check_inplace_update_policy(struct inode *inode,
1548 struct f2fs_io_info *fio)
1549{
1550 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1551 unsigned int policy = SM_I(sbi)->ipu_policy;
1552
1553 if (policy & (0x1 << F2FS_IPU_FORCE))
1554 return true;
1555 if (policy & (0x1 << F2FS_IPU_SSR) && need_SSR(sbi))
1556 return true;
1557 if (policy & (0x1 << F2FS_IPU_UTIL) &&
1558 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1559 return true;
1560 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && need_SSR(sbi) &&
1561 utilization(sbi) > SM_I(sbi)->min_ipu_util)
1562 return true;
1563
1564 /*
1565 * IPU for rewrite async pages
1566 */
1567 if (policy & (0x1 << F2FS_IPU_ASYNC) &&
1568 fio && fio->op == REQ_OP_WRITE &&
1569 !(fio->op_flags & REQ_SYNC) &&
1570 !f2fs_encrypted_inode(inode))
1571 return true;
1572
1573 /* this is only set during fdatasync */
1574 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
1575 is_inode_flag_set(inode, FI_NEED_IPU))
1576 return true;
1577
1578 return false;
1579}
1580
1581bool should_update_inplace(struct inode *inode, struct f2fs_io_info *fio)
1582{
1583 if (f2fs_is_pinned_file(inode))
1584 return true;
1585
1586 /* if this is cold file, we should overwrite to avoid fragmentation */
1587 if (file_is_cold(inode))
1588 return true;
1589
1590 return check_inplace_update_policy(inode, fio);
1591}
1592
1593bool should_update_outplace(struct inode *inode, struct f2fs_io_info *fio)
1594{
1595 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1596
1597 if (test_opt(sbi, LFS))
1598 return true;
1599 if (S_ISDIR(inode->i_mode))
1600 return true;
1601 if (f2fs_is_atomic_file(inode))
1602 return true;
1603 if (fio) {
1604 if (is_cold_data(fio->page))
1605 return true;
1606 if (IS_ATOMIC_WRITTEN_PAGE(fio->page))
1607 return true;
1608 }
1609 return false;
1610}
1611
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001612static inline bool need_inplace_update(struct f2fs_io_info *fio)
1613{
1614 struct inode *inode = fio->page->mapping->host;
1615
Chao Yubb9e3bb2018-01-17 16:31:38 +08001616 if (should_update_outplace(inode, fio))
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001617 return false;
1618
Chao Yubb9e3bb2018-01-17 16:31:38 +08001619 return should_update_inplace(inode, fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001620}
1621
Jaegeuk Kima8177372017-04-24 15:20:16 -07001622static inline bool valid_ipu_blkaddr(struct f2fs_io_info *fio)
1623{
1624 if (fio->old_blkaddr == NEW_ADDR)
1625 return false;
1626 if (fio->old_blkaddr == NULL_ADDR)
1627 return false;
1628 return true;
1629}
1630
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001631int do_write_data_page(struct f2fs_io_info *fio)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001632{
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001633 struct page *page = fio->page;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001634 struct inode *inode = page->mapping->host;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001635 struct dnode_of_data dn;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001636 struct extent_info ei = {0,0,0};
1637 bool ipu_force = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001638 int err = 0;
1639
1640 set_new_dnode(&dn, inode, NULL, NULL, 0);
Hou Pengyange959c8f2017-04-25 12:45:13 +00001641 if (need_inplace_update(fio) &&
1642 f2fs_lookup_extent_cache(inode, page->index, &ei)) {
1643 fio->old_blkaddr = ei.blk + page->index - ei.fofs;
Jaegeuk Kima8177372017-04-24 15:20:16 -07001644
1645 if (valid_ipu_blkaddr(fio)) {
Hou Pengyange959c8f2017-04-25 12:45:13 +00001646 ipu_force = true;
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001647 fio->need_lock = LOCK_DONE;
Hou Pengyange959c8f2017-04-25 12:45:13 +00001648 goto got_it;
1649 }
1650 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001651
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001652 /* Deadlock due to between page->lock and f2fs_lock_op */
1653 if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi))
1654 return -EAGAIN;
Hou Pengyang279d6df2017-04-27 00:17:21 +08001655
Jaegeuk Kim266e97a2013-02-26 13:10:46 +09001656 err = get_dnode_of_data(&dn, page->index, LOOKUP_NODE);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001657 if (err)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001658 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001659
Chao Yu28bc1062016-02-06 14:40:34 +08001660 fio->old_blkaddr = dn.data_blkaddr;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001661
1662 /* This page is already truncated */
Chao Yu7a9d7542016-02-22 18:36:38 +08001663 if (fio->old_blkaddr == NULL_ADDR) {
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001664 ClearPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001665 goto out_writepage;
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001666 }
Hou Pengyange959c8f2017-04-25 12:45:13 +00001667got_it:
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001668 /*
1669 * If current allocation needs SSR,
1670 * it had better in-place writes for updated data.
1671 */
Jaegeuk Kima8177372017-04-24 15:20:16 -07001672 if (ipu_force || (valid_ipu_blkaddr(fio) && need_inplace_update(fio))) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001673 err = encrypt_one_page(fio);
1674 if (err)
1675 goto out_writepage;
1676
1677 set_page_writeback(page);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001678 f2fs_put_dnode(&dn);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001679 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001680 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimd1b3e722017-03-30 21:02:46 -07001681 err = rewrite_data_page(fio);
Hou Pengyang7eab0c02017-04-25 12:45:12 +00001682 trace_f2fs_do_write_data_page(fio->page, IPU);
Jaegeuk Kim91942322016-05-20 10:13:22 -07001683 set_inode_flag(inode, FI_UPDATE_WRITE);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001684 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001685 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001686
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001687 if (fio->need_lock == LOCK_RETRY) {
1688 if (!f2fs_trylock_op(fio->sbi)) {
1689 err = -EAGAIN;
1690 goto out_writepage;
1691 }
1692 fio->need_lock = LOCK_REQ;
1693 }
1694
1695 err = encrypt_one_page(fio);
1696 if (err)
1697 goto out_writepage;
1698
1699 set_page_writeback(page);
1700
Hou Pengyang279d6df2017-04-27 00:17:21 +08001701 /* LFS mode write path */
1702 write_data_page(&dn, fio);
1703 trace_f2fs_do_write_data_page(page, OPU);
1704 set_inode_flag(inode, FI_APPEND_WRITE);
1705 if (page->index == 0)
1706 set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001707out_writepage:
1708 f2fs_put_dnode(&dn);
Hou Pengyang279d6df2017-04-27 00:17:21 +08001709out:
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001710 if (fio->need_lock == LOCK_REQ)
Hou Pengyang279d6df2017-04-27 00:17:21 +08001711 f2fs_unlock_op(fio->sbi);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001712 return err;
1713}
1714
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001715static int __write_data_page(struct page *page, bool *submitted,
Chao Yub0af6d42017-08-02 23:21:48 +08001716 struct writeback_control *wbc,
1717 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001718{
1719 struct inode *inode = page->mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07001720 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001721 loff_t i_size = i_size_read(inode);
1722 const pgoff_t end_index = ((unsigned long long) i_size)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001723 >> PAGE_SHIFT;
Jaegeuk Kim26de9b12016-05-20 20:42:37 -07001724 loff_t psize = (page->index + 1) << PAGE_SHIFT;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08001725 unsigned offset = 0;
Jaegeuk Kim39936832012-11-22 16:21:29 +09001726 bool need_balance_fs = false;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001727 int err = 0;
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001728 struct f2fs_io_info fio = {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001729 .sbi = sbi,
Chao Yu39d787b2017-09-29 13:59:38 +08001730 .ino = inode->i_ino,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001731 .type = DATA,
Mike Christie04d328d2016-06-05 14:31:55 -05001732 .op = REQ_OP_WRITE,
Jens Axboe76372412016-11-01 10:00:38 -06001733 .op_flags = wbc_to_write_flags(wbc),
Hou Pengyange959c8f2017-04-25 12:45:13 +00001734 .old_blkaddr = NULL_ADDR,
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001735 .page = page,
Jaegeuk Kim4375a332015-04-23 12:04:33 -07001736 .encrypted_page = NULL,
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001737 .submitted = false,
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001738 .need_lock = LOCK_RETRY,
Chao Yub0af6d42017-08-02 23:21:48 +08001739 .io_type = io_type,
Yufen Yu578c6472018-01-09 19:33:39 +08001740 .io_wbc = wbc,
Jaegeuk Kim458e6192013-12-11 13:54:01 +09001741 };
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001742
Chao Yuecda0de2014-05-06 16:48:26 +08001743 trace_f2fs_writepage(page, DATA);
1744
Chao Yudb198ae2018-01-18 17:29:10 +08001745 /* we should bypass data pages to proceed the kworkder jobs */
1746 if (unlikely(f2fs_cp_error(sbi))) {
1747 mapping_set_error(page->mapping, -EIO);
1748 goto out;
1749 }
1750
Chao Yu0771fcc2017-06-29 23:20:45 +08001751 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
1752 goto redirty_out;
1753
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001754 if (page->index < end_index)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001755 goto write;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001756
1757 /*
1758 * If the offset is out-of-range of file size,
1759 * this page does not have to be written to disk.
1760 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001761 offset = i_size & (PAGE_SIZE - 1);
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001762 if ((page->index >= end_index + 1) || !offset)
Jaegeuk Kim39936832012-11-22 16:21:29 +09001763 goto out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001764
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001765 zero_user_segment(page, offset, PAGE_SIZE);
Jaegeuk Kim39936832012-11-22 16:21:29 +09001766write:
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001767 if (f2fs_is_drop_cache(inode))
1768 goto out;
Jaegeuk Kime6e5f562016-04-14 16:48:52 -07001769 /* we should not write 0'th page having journal header */
1770 if (f2fs_is_volatile_file(inode) && (!page->index ||
1771 (!wbc->for_reclaim &&
1772 available_free_memory(sbi, BASE_CHECK))))
Jaegeuk Kim1e843712014-12-09 06:08:59 -08001773 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001774
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001775 /* Dentry blocks are controlled by checkpoint */
1776 if (S_ISDIR(inode->i_mode)) {
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001777 fio.need_lock = LOCK_DONE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001778 err = do_write_data_page(&fio);
1779 goto done;
1780 }
1781
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001782 if (!wbc->for_reclaim)
1783 need_balance_fs = true;
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07001784 else if (has_not_enough_free_secs(sbi, 0, 0))
Jaegeuk Kim39936832012-11-22 16:21:29 +09001785 goto redirty_out;
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001786 else
1787 set_inode_flag(inode, FI_HOT_DATA);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001788
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001789 err = -EAGAIN;
Yunlei Hedd7b2332017-02-23 20:31:20 +08001790 if (f2fs_has_inline_data(inode)) {
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07001791 err = f2fs_write_inline_data(inode, page);
Yunlei Hedd7b2332017-02-23 20:31:20 +08001792 if (!err)
1793 goto out;
1794 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001795
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001796 if (err == -EAGAIN) {
Jaegeuk Kim05ca3632015-04-23 14:38:15 -07001797 err = do_write_data_page(&fio);
Jaegeuk Kimcc156202017-05-12 13:51:34 -07001798 if (err == -EAGAIN) {
1799 fio.need_lock = LOCK_REQ;
1800 err = do_write_data_page(&fio);
1801 }
1802 }
Chao Yua0d00fa2017-10-09 17:55:19 +08001803
Chao Yueb449792018-01-17 16:31:37 +08001804 if (err) {
1805 file_set_keep_isize(inode);
1806 } else {
1807 down_write(&F2FS_I(inode)->i_sem);
1808 if (F2FS_I(inode)->last_disk_size < psize)
1809 F2FS_I(inode)->last_disk_size = psize;
1810 up_write(&F2FS_I(inode)->i_sem);
1811 }
Hou Pengyang279d6df2017-04-27 00:17:21 +08001812
Jaegeuk Kim8618b882014-02-17 19:29:27 +09001813done:
1814 if (err && err != -ENOENT)
1815 goto redirty_out;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001816
Jaegeuk Kim39936832012-11-22 16:21:29 +09001817out:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07001818 inode_dec_dirty_pages(inode);
Jaegeuk Kim2bca1e22015-02-25 19:25:01 -08001819 if (err)
1820 ClearPageUptodate(page);
Chao Yu0c3a5792016-01-18 18:28:11 +08001821
1822 if (wbc->for_reclaim) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001823 f2fs_submit_merged_write_cond(sbi, inode, 0, page->index, DATA);
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001824 clear_inode_flag(inode, FI_HOT_DATA);
Chao Yuc227f912015-12-16 13:09:20 +08001825 remove_dirty_inode(inode);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001826 submitted = NULL;
Chao Yueb7e8132015-11-10 18:45:07 +08001827 }
Chao Yu0c3a5792016-01-18 18:28:11 +08001828
1829 unlock_page(page);
Jaegeuk Kima7881892017-04-20 13:51:57 -07001830 if (!S_ISDIR(inode->i_mode))
1831 f2fs_balance_fs(sbi, need_balance_fs);
Chao Yu0c3a5792016-01-18 18:28:11 +08001832
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001833 if (unlikely(f2fs_cp_error(sbi))) {
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001834 f2fs_submit_merged_write(sbi, DATA);
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001835 submitted = NULL;
1836 }
1837
1838 if (submitted)
1839 *submitted = fio.submitted;
Chao Yu0c3a5792016-01-18 18:28:11 +08001840
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001841 return 0;
1842
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09001843redirty_out:
Jaegeuk Kim76f60262014-04-15 16:04:15 +09001844 redirty_page_for_writepage(wbc, page);
Chao Yu0002b612016-11-28 19:13:43 -08001845 if (!err)
1846 return AOP_WRITEPAGE_ACTIVATE;
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001847 unlock_page(page);
1848 return err;
Namjae Jeonfa9150a2013-01-15 16:45:24 +09001849}
1850
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001851static int f2fs_write_data_page(struct page *page,
1852 struct writeback_control *wbc)
1853{
Chao Yub0af6d42017-08-02 23:21:48 +08001854 return __write_data_page(page, NULL, wbc, FS_DATA_IO);
Jaegeuk Kimf566bae2017-02-03 17:18:00 -08001855}
1856
Chao Yu8f46dca2015-07-14 18:56:10 +08001857/*
1858 * This function was copied from write_cche_pages from mm/page-writeback.c.
1859 * The major change is making write step of cold data page separately from
1860 * warm/hot data page.
1861 */
1862static int f2fs_write_cache_pages(struct address_space *mapping,
Chao Yub0af6d42017-08-02 23:21:48 +08001863 struct writeback_control *wbc,
1864 enum iostat_type io_type)
Chao Yu8f46dca2015-07-14 18:56:10 +08001865{
1866 int ret = 0;
1867 int done = 0;
1868 struct pagevec pvec;
1869 int nr_pages;
1870 pgoff_t uninitialized_var(writeback_index);
1871 pgoff_t index;
1872 pgoff_t end; /* Inclusive */
1873 pgoff_t done_index;
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001874 pgoff_t last_idx = ULONG_MAX;
Chao Yu8f46dca2015-07-14 18:56:10 +08001875 int cycled;
1876 int range_whole = 0;
1877 int tag;
Chao Yu8f46dca2015-07-14 18:56:10 +08001878
Mel Gorman86679822017-11-15 17:37:52 -08001879 pagevec_init(&pvec);
Jaegeuk Kim46ae9572016-05-25 20:57:16 -07001880
Jaegeuk Kimef095d12017-03-24 20:05:13 -04001881 if (get_dirty_pages(mapping->host) <=
1882 SM_I(F2FS_M_SB(mapping))->min_hot_blocks)
1883 set_inode_flag(mapping->host, FI_HOT_DATA);
1884 else
1885 clear_inode_flag(mapping->host, FI_HOT_DATA);
1886
Chao Yu8f46dca2015-07-14 18:56:10 +08001887 if (wbc->range_cyclic) {
1888 writeback_index = mapping->writeback_index; /* prev offset */
1889 index = writeback_index;
1890 if (index == 0)
1891 cycled = 1;
1892 else
1893 cycled = 0;
1894 end = -1;
1895 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001896 index = wbc->range_start >> PAGE_SHIFT;
1897 end = wbc->range_end >> PAGE_SHIFT;
Chao Yu8f46dca2015-07-14 18:56:10 +08001898 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1899 range_whole = 1;
1900 cycled = 1; /* ignore range_cyclic tests */
1901 }
1902 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1903 tag = PAGECACHE_TAG_TOWRITE;
1904 else
1905 tag = PAGECACHE_TAG_DIRTY;
1906retry:
1907 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
1908 tag_pages_for_writeback(mapping, index, end);
1909 done_index = index;
1910 while (!done && (index <= end)) {
1911 int i;
1912
Jan Kara69c4f352017-11-15 17:34:48 -08001913 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08001914 tag);
Chao Yu8f46dca2015-07-14 18:56:10 +08001915 if (nr_pages == 0)
1916 break;
1917
1918 for (i = 0; i < nr_pages; i++) {
1919 struct page *page = pvec.pages[i];
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001920 bool submitted = false;
Chao Yu8f46dca2015-07-14 18:56:10 +08001921
Chao Yu8f46dca2015-07-14 18:56:10 +08001922 done_index = page->index;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001923retry_write:
Chao Yu8f46dca2015-07-14 18:56:10 +08001924 lock_page(page);
1925
1926 if (unlikely(page->mapping != mapping)) {
1927continue_unlock:
1928 unlock_page(page);
1929 continue;
1930 }
1931
1932 if (!PageDirty(page)) {
1933 /* someone wrote it for us */
1934 goto continue_unlock;
1935 }
1936
Chao Yu8f46dca2015-07-14 18:56:10 +08001937 if (PageWriteback(page)) {
1938 if (wbc->sync_mode != WB_SYNC_NONE)
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08001939 f2fs_wait_on_page_writeback(page,
1940 DATA, true);
Chao Yu8f46dca2015-07-14 18:56:10 +08001941 else
1942 goto continue_unlock;
1943 }
1944
1945 BUG_ON(PageWriteback(page));
1946 if (!clear_page_dirty_for_io(page))
1947 goto continue_unlock;
1948
Chao Yub0af6d42017-08-02 23:21:48 +08001949 ret = __write_data_page(page, &submitted, wbc, io_type);
Chao Yu8f46dca2015-07-14 18:56:10 +08001950 if (unlikely(ret)) {
Chao Yu0002b612016-11-28 19:13:43 -08001951 /*
1952 * keep nr_to_write, since vfs uses this to
1953 * get # of written pages.
1954 */
1955 if (ret == AOP_WRITEPAGE_ACTIVATE) {
1956 unlock_page(page);
1957 ret = 0;
1958 continue;
Jaegeuk Kimd29460e2017-06-21 17:52:39 -07001959 } else if (ret == -EAGAIN) {
1960 ret = 0;
1961 if (wbc->sync_mode == WB_SYNC_ALL) {
1962 cond_resched();
1963 congestion_wait(BLK_RW_ASYNC,
1964 HZ/50);
1965 goto retry_write;
1966 }
1967 continue;
Chao Yu0002b612016-11-28 19:13:43 -08001968 }
Jaegeuk Kimb230e6c2016-05-29 21:18:23 -07001969 done_index = page->index + 1;
1970 done = 1;
1971 break;
Jaegeuk Kimd68f7352017-02-03 17:44:04 -08001972 } else if (submitted) {
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001973 last_idx = page->index;
Chao Yu8f46dca2015-07-14 18:56:10 +08001974 }
1975
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07001976 /* give a priority to WB_SYNC threads */
1977 if ((atomic_read(&F2FS_M_SB(mapping)->wb_sync_req) ||
1978 --wbc->nr_to_write <= 0) &&
1979 wbc->sync_mode == WB_SYNC_NONE) {
Chao Yu8f46dca2015-07-14 18:56:10 +08001980 done = 1;
1981 break;
1982 }
1983 }
1984 pagevec_release(&pvec);
1985 cond_resched();
1986 }
1987
Chao Yu8f46dca2015-07-14 18:56:10 +08001988 if (!cycled && !done) {
1989 cycled = 1;
1990 index = 0;
1991 end = writeback_index - 1;
1992 goto retry;
1993 }
1994 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1995 mapping->writeback_index = done_index;
1996
Jaegeuk Kim942fd312017-02-01 16:51:22 -08001997 if (last_idx != ULONG_MAX)
Jaegeuk Kimb9109b02017-05-10 11:28:38 -07001998 f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host,
1999 0, last_idx, DATA);
Chao Yu6ca56ca2016-09-29 18:50:11 +08002000
Chao Yu8f46dca2015-07-14 18:56:10 +08002001 return ret;
2002}
2003
Chao Yub0af6d42017-08-02 23:21:48 +08002004int __f2fs_write_data_pages(struct address_space *mapping,
2005 struct writeback_control *wbc,
2006 enum iostat_type io_type)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002007{
2008 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002009 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002010 struct blk_plug plug;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002011 int ret;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002012
P J Pcfb185a2013-04-03 11:38:00 +09002013 /* deal with chardevs and other special file */
2014 if (!mapping->a_ops->writepage)
2015 return 0;
2016
Chao Yu6a290542015-07-17 18:02:39 +08002017 /* skip writing if there is no dirty page in this inode */
2018 if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE)
2019 return 0;
2020
Chao Yu0771fcc2017-06-29 23:20:45 +08002021 /* during POR, we don't need to trigger writepage at all. */
2022 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
2023 goto skip_write;
2024
Jaegeuk Kima1257022015-10-08 10:40:07 -07002025 if (S_ISDIR(inode->i_mode) && wbc->sync_mode == WB_SYNC_NONE &&
2026 get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) &&
2027 available_free_memory(sbi, DIRTY_DENTS))
2028 goto skip_write;
2029
Chao Yud323d002015-10-27 09:53:45 +08002030 /* skip writing during file defragment */
Jaegeuk Kim91942322016-05-20 10:13:22 -07002031 if (is_inode_flag_set(inode, FI_DO_DEFRAG))
Chao Yud323d002015-10-27 09:53:45 +08002032 goto skip_write;
2033
Yunlei Hed31c7c32016-02-04 16:14:00 +08002034 trace_f2fs_writepages(mapping->host, wbc, DATA);
2035
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002036 /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */
2037 if (wbc->sync_mode == WB_SYNC_ALL)
2038 atomic_inc(&sbi->wb_sync_req);
2039 else if (atomic_read(&sbi->wb_sync_req))
2040 goto skip_write;
2041
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002042 blk_start_plug(&plug);
Chao Yub0af6d42017-08-02 23:21:48 +08002043 ret = f2fs_write_cache_pages(mapping, wbc, io_type);
Jaegeuk Kim9dfa1ba2016-07-13 19:33:19 -07002044 blk_finish_plug(&plug);
Jaegeuk Kim687de7f2017-03-28 18:07:38 -07002045
2046 if (wbc->sync_mode == WB_SYNC_ALL)
2047 atomic_dec(&sbi->wb_sync_req);
Jaegeuk Kim28ea6162016-05-25 17:17:56 -07002048 /*
2049 * if some pages were truncated, we cannot guarantee its mapping->host
2050 * to detect pending bios.
2051 */
Jaegeuk Kim458e6192013-12-11 13:54:01 +09002052
Chao Yuc227f912015-12-16 13:09:20 +08002053 remove_dirty_inode(inode);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002054 return ret;
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002055
2056skip_write:
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002057 wbc->pages_skipped += get_dirty_pages(inode);
Yunlei Hed31c7c32016-02-04 16:14:00 +08002058 trace_f2fs_writepages(mapping->host, wbc, DATA);
Jaegeuk Kimd3baf952014-03-18 13:43:05 +09002059 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002060}
2061
Chao Yub0af6d42017-08-02 23:21:48 +08002062static int f2fs_write_data_pages(struct address_space *mapping,
2063 struct writeback_control *wbc)
2064{
2065 struct inode *inode = mapping->host;
2066
2067 return __f2fs_write_data_pages(mapping, wbc,
2068 F2FS_I(inode)->cp_task == current ?
2069 FS_CP_DATA_IO : FS_DATA_IO);
2070}
2071
Chao Yu3aab8f82014-07-02 13:25:04 +08002072static void f2fs_write_failed(struct address_space *mapping, loff_t to)
2073{
2074 struct inode *inode = mapping->host;
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002075 loff_t i_size = i_size_read(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002076
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002077 if (to > i_size) {
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08002078 down_write(&F2FS_I(inode)->i_mmap_sem);
Jaegeuk Kim819d9152015-12-28 13:48:11 -08002079 truncate_pagecache(inode, i_size);
2080 truncate_blocks(inode, i_size, true);
Qiuyang Sun5a3a2d82017-05-18 11:06:45 +08002081 up_write(&F2FS_I(inode)->i_mmap_sem);
Chao Yu3aab8f82014-07-02 13:25:04 +08002082 }
2083}
2084
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002085static int prepare_write_begin(struct f2fs_sb_info *sbi,
2086 struct page *page, loff_t pos, unsigned len,
2087 block_t *blk_addr, bool *node_changed)
2088{
2089 struct inode *inode = page->mapping->host;
2090 pgoff_t index = page->index;
2091 struct dnode_of_data dn;
2092 struct page *ipage;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002093 bool locked = false;
Hou Pengyange15882b2017-02-23 09:18:05 +00002094 struct extent_info ei = {0,0,0};
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002095 int err = 0;
2096
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002097 /*
2098 * we already allocated all the blocks, so we don't need to get
2099 * the block addresses when there is no need to fill the page.
2100 */
Jaegeuk Kimdc91de72017-01-13 13:12:29 -08002101 if (!f2fs_has_inline_data(inode) && len == PAGE_SIZE &&
2102 !is_inode_flag_set(inode, FI_NO_PREALLOC))
Jaegeuk Kim24b84912016-02-03 13:49:44 -08002103 return 0;
2104
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002105 if (f2fs_has_inline_data(inode) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002106 (pos & PAGE_MASK) >= i_size_read(inode)) {
Yunlei He59c90812017-03-13 20:22:18 +08002107 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002108 locked = true;
2109 }
2110restart:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002111 /* check inline_data */
2112 ipage = get_node_page(sbi, inode->i_ino);
2113 if (IS_ERR(ipage)) {
2114 err = PTR_ERR(ipage);
2115 goto unlock_out;
2116 }
2117
2118 set_new_dnode(&dn, inode, ipage, ipage, 0);
2119
2120 if (f2fs_has_inline_data(inode)) {
Chao Yuf2470372017-07-19 00:19:05 +08002121 if (pos + len <= MAX_INLINE_DATA(inode)) {
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002122 read_inline_data(page, ipage);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002123 set_inode_flag(inode, FI_DATA_EXIST);
Chao Yuab470362016-05-11 19:48:44 +08002124 if (inode->i_nlink)
2125 set_inline_node(ipage);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002126 } else {
2127 err = f2fs_convert_inline_page(&dn, page);
2128 if (err)
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002129 goto out;
2130 if (dn.data_blkaddr == NULL_ADDR)
2131 err = f2fs_get_block(&dn, index);
2132 }
2133 } else if (locked) {
2134 err = f2fs_get_block(&dn, index);
2135 } else {
2136 if (f2fs_lookup_extent_cache(inode, index, &ei)) {
2137 dn.data_blkaddr = ei.blk + index - ei.fofs;
2138 } else {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002139 /* hole case */
2140 err = get_dnode_of_data(&dn, index, LOOKUP_NODE);
Jaegeuk Kim4da7bf52016-04-06 11:27:03 -07002141 if (err || dn.data_blkaddr == NULL_ADDR) {
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002142 f2fs_put_dnode(&dn);
Yunlei He59c90812017-03-13 20:22:18 +08002143 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO,
2144 true);
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002145 locked = true;
2146 goto restart;
2147 }
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002148 }
2149 }
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002150
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002151 /* convert_inline_page can make node_changed */
2152 *blk_addr = dn.data_blkaddr;
2153 *node_changed = dn.node_changed;
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002154out:
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002155 f2fs_put_dnode(&dn);
2156unlock_out:
Jaegeuk Kimb4d07a32015-12-23 13:48:58 -08002157 if (locked)
Yunlei He59c90812017-03-13 20:22:18 +08002158 __do_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO, false);
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002159 return err;
2160}
2161
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002162static int f2fs_write_begin(struct file *file, struct address_space *mapping,
2163 loff_t pos, unsigned len, unsigned flags,
2164 struct page **pagep, void **fsdata)
2165{
2166 struct inode *inode = mapping->host;
Jaegeuk Kim40813632014-09-02 15:31:18 -07002167 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002168 struct page *page = NULL;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002169 pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT;
Chao Yua2e2e762018-01-15 17:16:46 +08002170 bool need_balance = false, drop_atomic = false;
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002171 block_t blkaddr = NULL_ADDR;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002172 int err = 0;
2173
Chao Yu62aed042014-05-06 16:46:04 +08002174 trace_f2fs_write_begin(inode, pos, len, flags);
2175
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002176 if (f2fs_is_atomic_file(inode) &&
2177 !available_free_memory(sbi, INMEM_PAGES)) {
2178 err = -ENOMEM;
Chao Yua2e2e762018-01-15 17:16:46 +08002179 drop_atomic = true;
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002180 goto fail;
2181 }
2182
Jaegeuk Kim5f727392014-11-25 10:59:45 -08002183 /*
2184 * We should check this at this moment to avoid deadlock on inode page
2185 * and #0 page. The locking rule for inline_data conversion should be:
2186 * lock_page(page #0) -> lock_page(inode_page)
2187 */
2188 if (index != 0) {
2189 err = f2fs_convert_inline_inode(inode);
2190 if (err)
2191 goto fail;
2192 }
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002193repeat:
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002194 /*
2195 * Do not use grab_cache_page_write_begin() to avoid deadlock due to
2196 * wait_for_stable_page. Will wait that below with our IO control.
2197 */
Chao Yu01eccef2017-10-28 16:52:30 +08002198 page = f2fs_pagecache_get_page(mapping, index,
Jaegeuk Kim86d54792017-02-17 09:55:55 -08002199 FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS);
Chao Yu3aab8f82014-07-02 13:25:04 +08002200 if (!page) {
2201 err = -ENOMEM;
2202 goto fail;
2203 }
Jaegeuk Kimd5f66992014-04-30 09:22:45 +09002204
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002205 *pagep = page;
2206
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002207 err = prepare_write_begin(sbi, page, pos, len,
2208 &blkaddr, &need_balance);
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002209 if (err)
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002210 goto fail;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002211
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -07002212 if (need_balance && has_not_enough_free_secs(sbi, 0, 0)) {
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002213 unlock_page(page);
Jaegeuk Kim2c4db1a2016-01-07 14:15:04 -08002214 f2fs_balance_fs(sbi, true);
Jaegeuk Kim2a340762015-12-22 13:23:35 -08002215 lock_page(page);
2216 if (page->mapping != mapping) {
2217 /* The page got truncated from under us */
2218 f2fs_put_page(page, 1);
2219 goto repeat;
2220 }
2221 }
2222
Jaegeuk Kimfec1d652016-01-20 23:43:51 +08002223 f2fs_wait_on_page_writeback(page, DATA, false);
Jaegeuk Kimb3d208f2014-10-23 19:48:09 -07002224
Chao Yu08b39fb2015-10-08 13:27:34 +08002225 /* wait for GCed encrypted page writeback */
Jaegeuk Kim19585932017-09-05 16:54:24 -07002226 if (f2fs_encrypted_file(inode))
Jaegeuk Kimd4c759e2017-09-05 17:04:35 -07002227 f2fs_wait_on_block_writeback(sbi, blkaddr);
Chao Yu08b39fb2015-10-08 13:27:34 +08002228
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002229 if (len == PAGE_SIZE || PageUptodate(page))
2230 return 0;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002231
Yunlei He746e2402016-12-20 11:11:35 +08002232 if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode)) {
2233 zero_user_segment(page, len, PAGE_SIZE);
2234 return 0;
2235 }
2236
Jaegeuk Kim2aadac02015-12-23 11:55:18 -08002237 if (blkaddr == NEW_ADDR) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002238 zero_user_segment(page, 0, PAGE_SIZE);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002239 SetPageUptodate(page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002240 } else {
Jaegeuk Kim13ba41e2017-09-06 21:04:44 -07002241 err = f2fs_submit_page_read(inode, page, blkaddr);
2242 if (err)
Chao Yu3aab8f82014-07-02 13:25:04 +08002243 goto fail;
Chao Yud54c7952014-03-29 15:30:40 +08002244
Jaegeuk Kim393ff912013-03-08 21:29:23 +09002245 lock_page(page);
Jaegeuk Kim6bacf522013-12-06 15:00:58 +09002246 if (unlikely(page->mapping != mapping)) {
Jaegeuk Kimafcb7ca02013-04-26 11:55:17 +09002247 f2fs_put_page(page, 1);
2248 goto repeat;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002249 }
Chao Yu1563ac72016-07-03 22:05:12 +08002250 if (unlikely(!PageUptodate(page))) {
2251 err = -EIO;
2252 goto fail;
Jaegeuk Kim4375a332015-04-23 12:04:33 -07002253 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002254 }
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002255 return 0;
Jaegeuk Kim9ba69cf2014-10-17 20:33:55 -07002256
Chao Yu3aab8f82014-07-02 13:25:04 +08002257fail:
Jaegeuk Kim86531d62015-07-15 13:08:21 -07002258 f2fs_put_page(page, 1);
Chao Yu3aab8f82014-07-02 13:25:04 +08002259 f2fs_write_failed(mapping, pos + len);
Chao Yua2e2e762018-01-15 17:16:46 +08002260 if (drop_atomic)
Jaegeuk Kim57864ae2017-10-18 19:05:57 -07002261 drop_inmem_pages_all(sbi);
Chao Yu3aab8f82014-07-02 13:25:04 +08002262 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002263}
2264
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002265static int f2fs_write_end(struct file *file,
2266 struct address_space *mapping,
2267 loff_t pos, unsigned len, unsigned copied,
2268 struct page *page, void *fsdata)
2269{
2270 struct inode *inode = page->mapping->host;
2271
Chao Yudfb2bf32014-05-06 16:47:23 +08002272 trace_f2fs_write_end(inode, pos, len, copied);
2273
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002274 /*
2275 * This should be come from len == PAGE_SIZE, and we expect copied
2276 * should be PAGE_SIZE. Otherwise, we treat it with zero copied and
2277 * let generic_perform_write() try to copy data again through copied=0.
2278 */
2279 if (!PageUptodate(page)) {
Yunlei He746e2402016-12-20 11:11:35 +08002280 if (unlikely(copied != len))
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002281 copied = 0;
2282 else
2283 SetPageUptodate(page);
2284 }
2285 if (!copied)
2286 goto unlock_out;
2287
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002288 set_page_dirty(page);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002289
Jaegeuk Kimfc9581c2016-05-20 09:22:03 -07002290 if (pos + copied > i_size_read(inode))
2291 f2fs_i_size_write(inode, pos + copied);
Jaegeuk Kim649d7df2016-09-06 11:02:03 -07002292unlock_out:
Chao Yu3024c9a2016-08-06 21:09:41 +08002293 f2fs_put_page(page, 1);
Jaegeuk Kimd0239e12016-01-08 16:57:48 -08002294 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002295 return copied;
2296}
2297
Omar Sandoval6f673762015-03-16 04:33:52 -07002298static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
2299 loff_t offset)
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002300{
2301 unsigned blocksize_mask = inode->i_sb->s_blocksize - 1;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002302
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002303 if (offset & blocksize_mask)
2304 return -EINVAL;
2305
Al Viro5b46f252014-03-16 18:07:34 -04002306 if (iov_iter_alignment(iter) & blocksize_mask)
2307 return -EINVAL;
2308
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002309 return 0;
2310}
2311
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002312static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002313{
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002314 struct address_space *mapping = iocb->ki_filp->f_mapping;
Chao Yu3aab8f82014-07-02 13:25:04 +08002315 struct inode *inode = mapping->host;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002316 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Chao Yu3aab8f82014-07-02 13:25:04 +08002317 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002318 loff_t offset = iocb->ki_pos;
Chao Yu82e0a5a2016-07-13 09:18:29 +08002319 int rw = iov_iter_rw(iter);
Chao Yu3aab8f82014-07-02 13:25:04 +08002320 int err;
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002321 enum rw_hint hint = iocb->ki_hint;
Chao Yu63189b72018-03-08 14:22:56 +08002322 int whint_mode = F2FS_OPTION(sbi).whint_mode;
Jaegeuk Kim944fcfc2013-12-26 20:15:09 +09002323
Jaegeuk Kimb439b102016-02-03 13:09:09 -08002324 err = check_direct_IO(inode, iter, offset);
Jaegeuk Kimb9d777b2015-12-22 11:09:35 -08002325 if (err)
2326 return err;
Huajun Li9ffe0fb2013-11-10 23:13:20 +08002327
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002328 if (f2fs_force_buffered_io(inode, rw))
Jaegeuk Kim36abef42016-06-03 19:29:38 -07002329 return 0;
Jaegeuk Kimfcc85a42015-04-21 20:39:58 -07002330
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002331 trace_f2fs_direct_IO_enter(inode, offset, count, rw);
Chao Yu70407fad2014-07-31 21:11:22 +08002332
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002333 if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
2334 iocb->ki_hint = WRITE_LIFE_NOT_SET;
2335
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002336 if (!down_read_trylock(&F2FS_I(inode)->dio_rwsem[rw])) {
2337 if (iocb->ki_flags & IOCB_NOWAIT) {
2338 iocb->ki_hint = hint;
2339 err = -EAGAIN;
2340 goto out;
2341 }
2342 down_read(&F2FS_I(inode)->dio_rwsem[rw]);
2343 }
2344
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07002345 err = blockdev_direct_IO(iocb, inode, iter, get_data_block_dio);
Chao Yu82e0a5a2016-07-13 09:18:29 +08002346 up_read(&F2FS_I(inode)->dio_rwsem[rw]);
2347
2348 if (rw == WRITE) {
Hyunchul Lee0cdd3192018-01-31 11:36:57 +09002349 if (whint_mode == WHINT_MODE_OFF)
2350 iocb->ki_hint = hint;
Chao Yub0af6d42017-08-02 23:21:48 +08002351 if (err > 0) {
2352 f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
2353 err);
Jaegeuk Kim91942322016-05-20 10:13:22 -07002354 set_inode_flag(inode, FI_UPDATE_WRITE);
Chao Yub0af6d42017-08-02 23:21:48 +08002355 } else if (err < 0) {
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002356 f2fs_write_failed(mapping, offset + count);
Chao Yub0af6d42017-08-02 23:21:48 +08002357 }
Jaegeuk Kim6bfc4912016-04-18 17:07:44 -04002358 }
Chao Yu70407fad2014-07-31 21:11:22 +08002359
Hyunchul Leeb91050a2018-03-08 19:34:38 +09002360out:
Jaegeuk Kim5302fb02016-07-22 15:25:47 -07002361 trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
Chao Yu70407fad2014-07-31 21:11:22 +08002362
Chao Yu3aab8f82014-07-02 13:25:04 +08002363 return err;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002364}
2365
Chao Yu487261f2015-02-05 17:44:29 +08002366void f2fs_invalidate_page(struct page *page, unsigned int offset,
2367 unsigned int length)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002368{
2369 struct inode *inode = page->mapping->host;
Chao Yu487261f2015-02-05 17:44:29 +08002370 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002371
Chao Yu487261f2015-02-05 17:44:29 +08002372 if (inode->i_ino >= F2FS_ROOT_INO(sbi) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002373 (offset % PAGE_SIZE || length != PAGE_SIZE))
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002374 return;
2375
Chao Yu487261f2015-02-05 17:44:29 +08002376 if (PageDirty(page)) {
Chao Yu933439c2016-10-11 22:57:01 +08002377 if (inode->i_ino == F2FS_META_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002378 dec_page_count(sbi, F2FS_DIRTY_META);
Chao Yu933439c2016-10-11 22:57:01 +08002379 } else if (inode->i_ino == F2FS_NODE_INO(sbi)) {
Chao Yu487261f2015-02-05 17:44:29 +08002380 dec_page_count(sbi, F2FS_DIRTY_NODES);
Chao Yu933439c2016-10-11 22:57:01 +08002381 } else {
Chao Yu487261f2015-02-05 17:44:29 +08002382 inode_dec_dirty_pages(inode);
Chao Yu933439c2016-10-11 22:57:01 +08002383 remove_dirty_inode(inode);
2384 }
Chao Yu487261f2015-02-05 17:44:29 +08002385 }
Chao Yudecd36b2015-08-07 18:42:09 +08002386
2387 /* This is atomic written page, keep Private */
2388 if (IS_ATOMIC_WRITTEN_PAGE(page))
Jaegeuk Kim8c242db2017-03-17 09:55:52 +08002389 return drop_inmem_page(inode, page);
Chao Yudecd36b2015-08-07 18:42:09 +08002390
Chao Yu23dc9742016-04-29 20:09:15 +08002391 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002392 ClearPagePrivate(page);
2393}
2394
Chao Yu487261f2015-02-05 17:44:29 +08002395int f2fs_release_page(struct page *page, gfp_t wait)
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002396{
Jaegeuk Kimf68daee2015-01-30 11:39:08 -08002397 /* If this is dirty page, keep PagePrivate */
2398 if (PageDirty(page))
2399 return 0;
2400
Chao Yudecd36b2015-08-07 18:42:09 +08002401 /* This is atomic written page, keep Private */
2402 if (IS_ATOMIC_WRITTEN_PAGE(page))
2403 return 0;
2404
Chao Yu23dc9742016-04-29 20:09:15 +08002405 set_page_private(page, 0);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002406 ClearPagePrivate(page);
Jaegeuk Kimc3850aa2013-03-14 09:24:32 +09002407 return 1;
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002408}
2409
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002410/*
2411 * This was copied from __set_page_dirty_buffers which gives higher performance
2412 * in very high speed storages. (e.g., pmem)
2413 */
2414void f2fs_set_page_dirty_nobuffers(struct page *page)
2415{
2416 struct address_space *mapping = page->mapping;
2417 unsigned long flags;
2418
2419 if (unlikely(!mapping))
2420 return;
2421
2422 spin_lock(&mapping->private_lock);
2423 lock_page_memcg(page);
2424 SetPageDirty(page);
2425 spin_unlock(&mapping->private_lock);
2426
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002427 xa_lock_irqsave(&mapping->i_pages, flags);
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002428 WARN_ON_ONCE(!PageUptodate(page));
2429 account_page_dirtied(page, mapping);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002430 radix_tree_tag_set(&mapping->i_pages,
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002431 page_index(page), PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002432 xa_unlock_irqrestore(&mapping->i_pages, flags);
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002433 unlock_page_memcg(page);
2434
2435 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
2436 return;
2437}
2438
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002439static int f2fs_set_data_page_dirty(struct page *page)
2440{
2441 struct address_space *mapping = page->mapping;
2442 struct inode *inode = mapping->host;
2443
Jaegeuk Kim26c6b882013-10-24 17:53:29 +09002444 trace_f2fs_set_page_dirty(page, DATA);
2445
Jaegeuk Kim237c0792016-06-30 18:49:15 -07002446 if (!PageUptodate(page))
2447 SetPageUptodate(page);
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002448
Chao Yu5fe45742017-01-07 18:50:26 +08002449 if (f2fs_is_atomic_file(inode) && !f2fs_is_commit_atomic_write(inode)) {
Chao Yudecd36b2015-08-07 18:42:09 +08002450 if (!IS_ATOMIC_WRITTEN_PAGE(page)) {
2451 register_inmem_page(inode, page);
2452 return 1;
2453 }
2454 /*
2455 * Previously, this page has been registered, we just
2456 * return here.
2457 */
2458 return 0;
Jaegeuk Kim34ba94b2014-10-09 13:19:53 -07002459 }
2460
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002461 if (!PageDirty(page)) {
Jaegeuk Kimfe76b792016-06-30 18:40:10 -07002462 f2fs_set_page_dirty_nobuffers(page);
Jaegeuk Kima7ffdbe2014-09-12 15:53:45 -07002463 update_dirty_page(inode, page);
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002464 return 1;
2465 }
2466 return 0;
2467}
2468
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002469static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)
2470{
Chao Yu454ae7e2014-04-22 13:34:01 +08002471 struct inode *inode = mapping->host;
2472
Jaegeuk Kim1d373a02015-10-19 10:29:51 -07002473 if (f2fs_has_inline_data(inode))
2474 return 0;
2475
2476 /* make sure allocating whole blocks */
2477 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2478 filemap_write_and_wait(mapping);
2479
Chao Yue2b4e2b2015-08-19 19:11:19 +08002480 return generic_block_bmap(mapping, block, get_data_block_bmap);
Chao Yu429511c2015-02-05 17:54:31 +08002481}
2482
Weichao Guo5b7a4872016-09-20 05:03:27 +08002483#ifdef CONFIG_MIGRATION
2484#include <linux/migrate.h>
2485
2486int f2fs_migrate_page(struct address_space *mapping,
2487 struct page *newpage, struct page *page, enum migrate_mode mode)
2488{
2489 int rc, extra_count;
2490 struct f2fs_inode_info *fi = F2FS_I(mapping->host);
2491 bool atomic_written = IS_ATOMIC_WRITTEN_PAGE(page);
2492
2493 BUG_ON(PageWriteback(page));
2494
2495 /* migrating an atomic written page is safe with the inmem_lock hold */
Jaegeuk Kimff1048e2017-07-06 14:46:01 -07002496 if (atomic_written) {
2497 if (mode != MIGRATE_SYNC)
2498 return -EBUSY;
2499 if (!mutex_trylock(&fi->inmem_lock))
2500 return -EAGAIN;
2501 }
Weichao Guo5b7a4872016-09-20 05:03:27 +08002502
2503 /*
2504 * A reference is expected if PagePrivate set when move mapping,
2505 * however F2FS breaks this for maintaining dirty page counts when
2506 * truncating pages. So here adjusting the 'extra_count' make it work.
2507 */
2508 extra_count = (atomic_written ? 1 : 0) - page_has_private(page);
2509 rc = migrate_page_move_mapping(mapping, newpage,
2510 page, NULL, mode, extra_count);
2511 if (rc != MIGRATEPAGE_SUCCESS) {
2512 if (atomic_written)
2513 mutex_unlock(&fi->inmem_lock);
2514 return rc;
2515 }
2516
2517 if (atomic_written) {
2518 struct inmem_pages *cur;
2519 list_for_each_entry(cur, &fi->inmem_pages, list)
2520 if (cur->page == page) {
2521 cur->page = newpage;
2522 break;
2523 }
2524 mutex_unlock(&fi->inmem_lock);
2525 put_page(page);
2526 get_page(newpage);
2527 }
2528
2529 if (PagePrivate(page))
2530 SetPagePrivate(newpage);
2531 set_page_private(newpage, page_private(page));
2532
Jérôme Glisse2916ecc2017-09-08 16:12:06 -07002533 if (mode != MIGRATE_SYNC_NO_COPY)
2534 migrate_page_copy(newpage, page);
2535 else
2536 migrate_page_states(newpage, page);
Weichao Guo5b7a4872016-09-20 05:03:27 +08002537
2538 return MIGRATEPAGE_SUCCESS;
2539}
2540#endif
2541
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002542const struct address_space_operations f2fs_dblock_aops = {
2543 .readpage = f2fs_read_data_page,
2544 .readpages = f2fs_read_data_pages,
2545 .writepage = f2fs_write_data_page,
2546 .writepages = f2fs_write_data_pages,
2547 .write_begin = f2fs_write_begin,
Jaegeuk Kima1dd3c12013-06-27 13:04:08 +09002548 .write_end = f2fs_write_end,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002549 .set_page_dirty = f2fs_set_data_page_dirty,
Chao Yu487261f2015-02-05 17:44:29 +08002550 .invalidatepage = f2fs_invalidate_page,
2551 .releasepage = f2fs_release_page,
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002552 .direct_IO = f2fs_direct_IO,
Jaegeuk Kimc01e54b2013-01-17 20:30:23 +09002553 .bmap = f2fs_bmap,
Weichao Guo5b7a4872016-09-20 05:03:27 +08002554#ifdef CONFIG_MIGRATION
2555 .migratepage = f2fs_migrate_page,
2556#endif
Jaegeuk Kimeb47b802012-11-02 17:10:12 +09002557};