Commit | Line | Data |
---|---|---|
7c1a000d | 1 | // SPDX-License-Identifier: GPL-2.0 |
0a8165d7 | 2 | /* |
eb47b800 JK |
3 | * fs/f2fs/data.c |
4 | * | |
5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. | |
6 | * http://www.samsung.com/ | |
eb47b800 JK |
7 | */ |
8 | #include <linux/fs.h> | |
9 | #include <linux/f2fs_fs.h> | |
10 | #include <linux/buffer_head.h> | |
4034247a | 11 | #include <linux/sched/mm.h> |
eb47b800 JK |
12 | #include <linux/mpage.h> |
13 | #include <linux/writeback.h> | |
8f46dcae | 14 | #include <linux/pagevec.h> |
eb47b800 JK |
15 | #include <linux/blkdev.h> |
16 | #include <linux/bio.h> | |
27aacd28 | 17 | #include <linux/blk-crypto.h> |
4969c06a | 18 | #include <linux/swap.h> |
690e4a3e | 19 | #include <linux/prefetch.h> |
e2e40f2c | 20 | #include <linux/uio.h> |
174cd4b1 | 21 | #include <linux/sched/signal.h> |
10c5db28 | 22 | #include <linux/fiemap.h> |
1517c1a7 | 23 | #include <linux/iomap.h> |
eb47b800 JK |
24 | |
25 | #include "f2fs.h" | |
26 | #include "node.h" | |
27 | #include "segment.h" | |
52118743 | 28 | #include "iostat.h" |
848753aa | 29 | #include <trace/events/f2fs.h> |
eb47b800 | 30 | |
6dbb1796 EB |
31 | #define NUM_PREALLOC_POST_READ_CTXS 128 |
32 | ||
33 | static struct kmem_cache *bio_post_read_ctx_cache; | |
0b20fcec | 34 | static struct kmem_cache *bio_entry_slab; |
6dbb1796 | 35 | static mempool_t *bio_post_read_ctx_pool; |
f543805f CY |
36 | static struct bio_set f2fs_bioset; |
37 | ||
38 | #define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE | |
39 | ||
40 | int __init f2fs_init_bioset(void) | |
41 | { | |
870af777 YL |
42 | return bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE, |
43 | 0, BIOSET_NEED_BVECS); | |
f543805f CY |
44 | } |
45 | ||
46 | void f2fs_destroy_bioset(void) | |
47 | { | |
48 | bioset_exit(&f2fs_bioset); | |
49 | } | |
50 | ||
36951b38 CY |
51 | static bool __is_cp_guaranteed(struct page *page) |
52 | { | |
53 | struct address_space *mapping = page->mapping; | |
54 | struct inode *inode; | |
55 | struct f2fs_sb_info *sbi; | |
56 | ||
57 | if (!mapping) | |
58 | return false; | |
59 | ||
60 | inode = mapping->host; | |
61 | sbi = F2FS_I_SB(inode); | |
62 | ||
63 | if (inode->i_ino == F2FS_META_INO(sbi) || | |
a87aff1d | 64 | inode->i_ino == F2FS_NODE_INO(sbi) || |
b763f3be CY |
65 | S_ISDIR(inode->i_mode)) |
66 | return true; | |
67 | ||
68 | if (f2fs_is_compressed_page(page)) | |
69 | return false; | |
3db1de0e | 70 | if ((S_ISREG(inode->i_mode) && IS_NOQUOTA(inode)) || |
b763f3be | 71 | page_private_gcing(page)) |
36951b38 CY |
72 | return true; |
73 | return false; | |
74 | } | |
75 | ||
5f9abab4 JK |
76 | static enum count_type __read_io_type(struct page *page) |
77 | { | |
4969c06a | 78 | struct address_space *mapping = page_file_mapping(page); |
5f9abab4 JK |
79 | |
80 | if (mapping) { | |
81 | struct inode *inode = mapping->host; | |
82 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
83 | ||
84 | if (inode->i_ino == F2FS_META_INO(sbi)) | |
85 | return F2FS_RD_META; | |
86 | ||
87 | if (inode->i_ino == F2FS_NODE_INO(sbi)) | |
88 | return F2FS_RD_NODE; | |
89 | } | |
90 | return F2FS_RD_DATA; | |
91 | } | |
92 | ||
6dbb1796 EB |
93 | /* postprocessing steps for read bios */ |
94 | enum bio_post_read_step { | |
7f59b277 | 95 | #ifdef CONFIG_FS_ENCRYPTION |
447286eb | 96 | STEP_DECRYPT = BIT(0), |
7f59b277 EB |
97 | #else |
98 | STEP_DECRYPT = 0, /* compile out the decryption-related code */ | |
99 | #endif | |
100 | #ifdef CONFIG_F2FS_FS_COMPRESSION | |
447286eb | 101 | STEP_DECOMPRESS = BIT(1), |
7f59b277 EB |
102 | #else |
103 | STEP_DECOMPRESS = 0, /* compile out the decompression-related code */ | |
104 | #endif | |
105 | #ifdef CONFIG_FS_VERITY | |
447286eb | 106 | STEP_VERITY = BIT(2), |
7f59b277 EB |
107 | #else |
108 | STEP_VERITY = 0, /* compile out the verity-related code */ | |
109 | #endif | |
6dbb1796 EB |
110 | }; |
111 | ||
112 | struct bio_post_read_ctx { | |
113 | struct bio *bio; | |
4c8ff709 | 114 | struct f2fs_sb_info *sbi; |
6dbb1796 | 115 | struct work_struct work; |
6dbb1796 | 116 | unsigned int enabled_steps; |
98dc08ba EB |
117 | /* |
118 | * decompression_attempted keeps track of whether | |
119 | * f2fs_end_read_compressed_page() has been called on the pages in the | |
120 | * bio that belong to a compressed cluster yet. | |
121 | */ | |
122 | bool decompression_attempted; | |
4931e0c9 | 123 | block_t fs_blkaddr; |
6dbb1796 EB |
124 | }; |
125 | ||
98dc08ba EB |
126 | /* |
127 | * Update and unlock a bio's pages, and free the bio. | |
128 | * | |
129 | * This marks pages up-to-date only if there was no error in the bio (I/O error, | |
130 | * decryption error, or verity error), as indicated by bio->bi_status. | |
131 | * | |
132 | * "Compressed pages" (pagecache pages backed by a compressed cluster on-disk) | |
133 | * aren't marked up-to-date here, as decompression is done on a per-compression- | |
134 | * cluster basis rather than a per-bio basis. Instead, we only must do two | |
135 | * things for each compressed page here: call f2fs_end_read_compressed_page() | |
136 | * with failed=true if an error occurred before it would have normally gotten | |
137 | * called (i.e., I/O error or decryption error, but *not* verity error), and | |
138 | * release the bio's reference to the decompress_io_ctx of the page's cluster. | |
139 | */ | |
bff139b4 | 140 | static void f2fs_finish_read_bio(struct bio *bio, bool in_task) |
93dfe2ac | 141 | { |
6dbb1796 | 142 | struct bio_vec *bv; |
6dc4f100 | 143 | struct bvec_iter_all iter_all; |
98dc08ba | 144 | struct bio_post_read_ctx *ctx = bio->bi_private; |
93dfe2ac | 145 | |
2b070cfe | 146 | bio_for_each_segment_all(bv, bio, iter_all) { |
7f59b277 | 147 | struct page *page = bv->bv_page; |
6dbb1796 | 148 | |
7f59b277 | 149 | if (f2fs_is_compressed_page(page)) { |
98dc08ba | 150 | if (ctx && !ctx->decompression_attempted) |
bff139b4 DJ |
151 | f2fs_end_read_compressed_page(page, true, 0, |
152 | in_task); | |
153 | f2fs_put_page_dic(page, in_task); | |
4c8ff709 CY |
154 | continue; |
155 | } | |
4c8ff709 | 156 | |
98dc08ba | 157 | if (bio->bi_status) |
6dbb1796 | 158 | ClearPageUptodate(page); |
98dc08ba | 159 | else |
6dbb1796 | 160 | SetPageUptodate(page); |
5f9abab4 | 161 | dec_page_count(F2FS_P_SB(page), __read_io_type(page)); |
6dbb1796 EB |
162 | unlock_page(page); |
163 | } | |
79bbefb1 | 164 | |
98dc08ba EB |
165 | if (ctx) |
166 | mempool_free(ctx, bio_post_read_ctx_pool); | |
7f59b277 | 167 | bio_put(bio); |
4c8ff709 | 168 | } |
4c8ff709 | 169 | |
7f59b277 | 170 | static void f2fs_verify_bio(struct work_struct *work) |
6dbb1796 EB |
171 | { |
172 | struct bio_post_read_ctx *ctx = | |
173 | container_of(work, struct bio_post_read_ctx, work); | |
644c8c92 | 174 | struct bio *bio = ctx->bio; |
7f59b277 | 175 | bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS); |
644c8c92 EB |
176 | |
177 | /* | |
704528d8 | 178 | * fsverity_verify_bio() may call readahead() again, and while verity |
7f59b277 EB |
179 | * will be disabled for this, decryption and/or decompression may still |
180 | * be needed, resulting in another bio_post_read_ctx being allocated. | |
181 | * So to prevent deadlocks we need to release the current ctx to the | |
182 | * mempool first. This assumes that verity is the last post-read step. | |
644c8c92 EB |
183 | */ |
184 | mempool_free(ctx, bio_post_read_ctx_pool); | |
185 | bio->bi_private = NULL; | |
6dbb1796 | 186 | |
7f59b277 EB |
187 | /* |
188 | * Verify the bio's pages with fs-verity. Exclude compressed pages, | |
189 | * as those were handled separately by f2fs_end_read_compressed_page(). | |
190 | */ | |
191 | if (may_have_compressed_pages) { | |
192 | struct bio_vec *bv; | |
193 | struct bvec_iter_all iter_all; | |
194 | ||
195 | bio_for_each_segment_all(bv, bio, iter_all) { | |
196 | struct page *page = bv->bv_page; | |
197 | ||
198 | if (!f2fs_is_compressed_page(page) && | |
98dc08ba EB |
199 | !fsverity_verify_page(page)) { |
200 | bio->bi_status = BLK_STS_IOERR; | |
201 | break; | |
202 | } | |
7f59b277 EB |
203 | } |
204 | } else { | |
205 | fsverity_verify_bio(bio); | |
4c8ff709 | 206 | } |
6dbb1796 | 207 | |
bff139b4 | 208 | f2fs_finish_read_bio(bio, true); |
6dbb1796 EB |
209 | } |
210 | ||
7f59b277 EB |
211 | /* |
212 | * If the bio's data needs to be verified with fs-verity, then enqueue the | |
213 | * verity work for the bio. Otherwise finish the bio now. | |
214 | * | |
215 | * Note that to avoid deadlocks, the verity work can't be done on the | |
216 | * decryption/decompression workqueue. This is because verifying the data pages | |
217 | * can involve reading verity metadata pages from the file, and these verity | |
218 | * metadata pages may be encrypted and/or compressed. | |
219 | */ | |
bff139b4 | 220 | static void f2fs_verify_and_finish_bio(struct bio *bio, bool in_task) |
95ae251f | 221 | { |
7f59b277 | 222 | struct bio_post_read_ctx *ctx = bio->bi_private; |
4c8ff709 | 223 | |
7f59b277 EB |
224 | if (ctx && (ctx->enabled_steps & STEP_VERITY)) { |
225 | INIT_WORK(&ctx->work, f2fs_verify_bio); | |
4c8ff709 | 226 | fsverity_enqueue_verify_work(&ctx->work); |
7f59b277 | 227 | } else { |
bff139b4 | 228 | f2fs_finish_read_bio(bio, in_task); |
4c8ff709 | 229 | } |
4c8ff709 | 230 | } |
95ae251f | 231 | |
7f59b277 EB |
232 | /* |
233 | * Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last | |
234 | * remaining page was read by @ctx->bio. | |
235 | * | |
236 | * Note that a bio may span clusters (even a mix of compressed and uncompressed | |
237 | * clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates | |
238 | * that the bio includes at least one compressed page. The actual decompression | |
239 | * is done on a per-cluster basis, not a per-bio basis. | |
240 | */ | |
bff139b4 DJ |
241 | static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx, |
242 | bool in_task) | |
6dbb1796 | 243 | { |
7f59b277 EB |
244 | struct bio_vec *bv; |
245 | struct bvec_iter_all iter_all; | |
246 | bool all_compressed = true; | |
4931e0c9 | 247 | block_t blkaddr = ctx->fs_blkaddr; |
4c8ff709 | 248 | |
7f59b277 EB |
249 | bio_for_each_segment_all(bv, ctx->bio, iter_all) { |
250 | struct page *page = bv->bv_page; | |
4c8ff709 | 251 | |
7f59b277 | 252 | if (f2fs_is_compressed_page(page)) |
14db0b3c EB |
253 | f2fs_end_read_compressed_page(page, false, blkaddr, |
254 | in_task); | |
7f59b277 EB |
255 | else |
256 | all_compressed = false; | |
6ce19aff CY |
257 | |
258 | blkaddr++; | |
6dbb1796 | 259 | } |
4c8ff709 | 260 | |
98dc08ba EB |
261 | ctx->decompression_attempted = true; |
262 | ||
7f59b277 EB |
263 | /* |
264 | * Optimization: if all the bio's pages are compressed, then scheduling | |
265 | * the per-bio verity work is unnecessary, as verity will be fully | |
266 | * handled at the compression cluster level. | |
267 | */ | |
268 | if (all_compressed) | |
269 | ctx->enabled_steps &= ~STEP_VERITY; | |
6dbb1796 EB |
270 | } |
271 | ||
7f59b277 | 272 | static void f2fs_post_read_work(struct work_struct *work) |
6dbb1796 | 273 | { |
7f59b277 EB |
274 | struct bio_post_read_ctx *ctx = |
275 | container_of(work, struct bio_post_read_ctx, work); | |
14db0b3c | 276 | struct bio *bio = ctx->bio; |
7f59b277 | 277 | |
14db0b3c EB |
278 | if ((ctx->enabled_steps & STEP_DECRYPT) && !fscrypt_decrypt_bio(bio)) { |
279 | f2fs_finish_read_bio(bio, true); | |
280 | return; | |
281 | } | |
7f59b277 EB |
282 | |
283 | if (ctx->enabled_steps & STEP_DECOMPRESS) | |
bff139b4 | 284 | f2fs_handle_step_decompress(ctx, true); |
7f59b277 | 285 | |
14db0b3c | 286 | f2fs_verify_and_finish_bio(bio, true); |
6dbb1796 EB |
287 | } |
288 | ||
289 | static void f2fs_read_end_io(struct bio *bio) | |
290 | { | |
c45d6002 | 291 | struct f2fs_sb_info *sbi = F2FS_P_SB(bio_first_page_all(bio)); |
a4b68176 | 292 | struct bio_post_read_ctx *ctx; |
bff139b4 | 293 | bool intask = in_task(); |
a4b68176 | 294 | |
d9bac032 | 295 | iostat_update_and_unbind_ctx(bio); |
a4b68176 | 296 | ctx = bio->bi_private; |
c45d6002 | 297 | |
c40e15a9 | 298 | if (time_to_inject(sbi, FAULT_READ_IO)) |
4e4cbee9 | 299 | bio->bi_status = BLK_STS_IOERR; |
8b038c70 | 300 | |
7f59b277 | 301 | if (bio->bi_status) { |
bff139b4 | 302 | f2fs_finish_read_bio(bio, intask); |
6dbb1796 | 303 | return; |
f1e88660 | 304 | } |
6dbb1796 | 305 | |
bff139b4 DJ |
306 | if (ctx) { |
307 | unsigned int enabled_steps = ctx->enabled_steps & | |
308 | (STEP_DECRYPT | STEP_DECOMPRESS); | |
309 | ||
310 | /* | |
311 | * If we have only decompression step between decompression and | |
312 | * decrypt, we don't need post processing for this. | |
313 | */ | |
314 | if (enabled_steps == STEP_DECOMPRESS && | |
315 | !f2fs_low_mem_mode(sbi)) { | |
316 | f2fs_handle_step_decompress(ctx, intask); | |
317 | } else if (enabled_steps) { | |
318 | INIT_WORK(&ctx->work, f2fs_post_read_work); | |
319 | queue_work(ctx->sbi->post_read_wq, &ctx->work); | |
320 | return; | |
321 | } | |
7f59b277 | 322 | } |
bff139b4 DJ |
323 | |
324 | f2fs_verify_and_finish_bio(bio, intask); | |
f1e88660 JK |
325 | } |
326 | ||
4246a0b6 | 327 | static void f2fs_write_end_io(struct bio *bio) |
93dfe2ac | 328 | { |
a4b68176 | 329 | struct f2fs_sb_info *sbi; |
f568849e | 330 | struct bio_vec *bvec; |
6dc4f100 | 331 | struct bvec_iter_all iter_all; |
93dfe2ac | 332 | |
d9bac032 | 333 | iostat_update_and_unbind_ctx(bio); |
a4b68176 DJ |
334 | sbi = bio->bi_private; |
335 | ||
c40e15a9 | 336 | if (time_to_inject(sbi, FAULT_WRITE_IO)) |
6f5c2ed0 | 337 | bio->bi_status = BLK_STS_IOERR; |
6f5c2ed0 | 338 | |
2b070cfe | 339 | bio_for_each_segment_all(bvec, bio, iter_all) { |
93dfe2ac | 340 | struct page *page = bvec->bv_page; |
36951b38 | 341 | enum count_type type = WB_DATA_TYPE(page); |
93dfe2ac | 342 | |
b763f3be CY |
343 | if (page_private_dummy(page)) { |
344 | clear_page_private_dummy(page); | |
0a595eba JK |
345 | unlock_page(page); |
346 | mempool_free(page, sbi->write_io_dummy); | |
347 | ||
4e4cbee9 | 348 | if (unlikely(bio->bi_status)) |
a9cfee0e CY |
349 | f2fs_stop_checkpoint(sbi, true, |
350 | STOP_CP_REASON_WRITE_FAIL); | |
0a595eba JK |
351 | continue; |
352 | } | |
353 | ||
d2d0727b | 354 | fscrypt_finalize_bounce_page(&page); |
4375a336 | 355 | |
4c8ff709 CY |
356 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
357 | if (f2fs_is_compressed_page(page)) { | |
358 | f2fs_compress_write_end_io(bio, page); | |
359 | continue; | |
360 | } | |
361 | #endif | |
362 | ||
4e4cbee9 | 363 | if (unlikely(bio->bi_status)) { |
5114a97a | 364 | mapping_set_error(page->mapping, -EIO); |
b1ca321d | 365 | if (type == F2FS_WB_CP_DATA) |
a9cfee0e CY |
366 | f2fs_stop_checkpoint(sbi, true, |
367 | STOP_CP_REASON_WRITE_FAIL); | |
93dfe2ac | 368 | } |
7dff55d2 YH |
369 | |
370 | f2fs_bug_on(sbi, page->mapping == NODE_MAPPING(sbi) && | |
371 | page->index != nid_of_node(page)); | |
372 | ||
36951b38 | 373 | dec_page_count(sbi, type); |
50fa53ec CY |
374 | if (f2fs_in_warm_node_list(sbi, page)) |
375 | f2fs_del_fsync_node_entry(sbi, page); | |
b763f3be | 376 | clear_page_private_gcing(page); |
93dfe2ac | 377 | end_page_writeback(page); |
f568849e | 378 | } |
36951b38 | 379 | if (!get_pages(sbi, F2FS_WB_CP_DATA) && |
f5730184 | 380 | wq_has_sleeper(&sbi->cp_wait)) |
93dfe2ac JK |
381 | wake_up(&sbi->cp_wait); |
382 | ||
383 | bio_put(bio); | |
384 | } | |
385 | ||
e067dc3c DJ |
386 | #ifdef CONFIG_BLK_DEV_ZONED |
387 | static void f2fs_zone_write_end_io(struct bio *bio) | |
388 | { | |
389 | struct f2fs_bio_info *io = (struct f2fs_bio_info *)bio->bi_private; | |
390 | ||
391 | bio->bi_private = io->bi_private; | |
392 | complete(&io->zone_wait); | |
393 | f2fs_write_end_io(bio); | |
394 | } | |
395 | #endif | |
396 | ||
3c62be17 | 397 | struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi, |
5189810a | 398 | block_t blk_addr, sector_t *sector) |
3c62be17 JK |
399 | { |
400 | struct block_device *bdev = sbi->sb->s_bdev; | |
401 | int i; | |
402 | ||
0916878d DLM |
403 | if (f2fs_is_multi_device(sbi)) { |
404 | for (i = 0; i < sbi->s_ndevs; i++) { | |
405 | if (FDEV(i).start_blk <= blk_addr && | |
406 | FDEV(i).end_blk >= blk_addr) { | |
407 | blk_addr -= FDEV(i).start_blk; | |
408 | bdev = FDEV(i).bdev; | |
409 | break; | |
410 | } | |
3c62be17 JK |
411 | } |
412 | } | |
5189810a CH |
413 | |
414 | if (sector) | |
415 | *sector = SECTOR_FROM_BLOCK(blk_addr); | |
3c62be17 JK |
416 | return bdev; |
417 | } | |
418 | ||
419 | int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr) | |
420 | { | |
421 | int i; | |
422 | ||
0916878d DLM |
423 | if (!f2fs_is_multi_device(sbi)) |
424 | return 0; | |
425 | ||
3c62be17 JK |
426 | for (i = 0; i < sbi->s_ndevs; i++) |
427 | if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr) | |
428 | return i; | |
429 | return 0; | |
430 | } | |
431 | ||
7649c873 | 432 | static blk_opf_t f2fs_io_flags(struct f2fs_io_info *fio) |
64bf0eef | 433 | { |
447286eb | 434 | unsigned int temp_mask = GENMASK(NR_TEMP_TYPE - 1, 0); |
0adc2ab0 | 435 | unsigned int fua_flag, meta_flag, io_flag; |
7649c873 | 436 | blk_opf_t op_flags = 0; |
0adc2ab0 JK |
437 | |
438 | if (fio->op != REQ_OP_WRITE) | |
439 | return 0; | |
440 | if (fio->type == DATA) | |
441 | io_flag = fio->sbi->data_io_flag; | |
442 | else if (fio->type == NODE) | |
443 | io_flag = fio->sbi->node_io_flag; | |
444 | else | |
445 | return 0; | |
446 | ||
447 | fua_flag = io_flag & temp_mask; | |
448 | meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask; | |
64bf0eef CH |
449 | |
450 | /* | |
451 | * data/node io flag bits per temp: | |
452 | * REQ_META | REQ_FUA | | |
453 | * 5 | 4 | 3 | 2 | 1 | 0 | | |
454 | * Cold | Warm | Hot | Cold | Warm | Hot | | |
455 | */ | |
447286eb | 456 | if (BIT(fio->temp) & meta_flag) |
0adc2ab0 | 457 | op_flags |= REQ_META; |
447286eb | 458 | if (BIT(fio->temp) & fua_flag) |
0adc2ab0 JK |
459 | op_flags |= REQ_FUA; |
460 | return op_flags; | |
64bf0eef CH |
461 | } |
462 | ||
b757f6ed | 463 | static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages) |
940a6d34 | 464 | { |
b757f6ed | 465 | struct f2fs_sb_info *sbi = fio->sbi; |
5189810a CH |
466 | struct block_device *bdev; |
467 | sector_t sector; | |
940a6d34 GZ |
468 | struct bio *bio; |
469 | ||
5189810a | 470 | bdev = f2fs_target_device(sbi, fio->new_blkaddr, §or); |
0adc2ab0 JK |
471 | bio = bio_alloc_bioset(bdev, npages, |
472 | fio->op | fio->op_flags | f2fs_io_flags(fio), | |
473 | GFP_NOIO, &f2fs_bioset); | |
5189810a | 474 | bio->bi_iter.bi_sector = sector; |
b757f6ed | 475 | if (is_read_io(fio->op)) { |
0cdd3195 HL |
476 | bio->bi_end_io = f2fs_read_end_io; |
477 | bio->bi_private = NULL; | |
478 | } else { | |
479 | bio->bi_end_io = f2fs_write_end_io; | |
480 | bio->bi_private = sbi; | |
0cdd3195 | 481 | } |
a4b68176 DJ |
482 | iostat_alloc_and_bind_ctx(sbi, bio, NULL); |
483 | ||
b757f6ed CY |
484 | if (fio->io_wbc) |
485 | wbc_init_bio(fio->io_wbc, bio); | |
940a6d34 GZ |
486 | |
487 | return bio; | |
488 | } | |
489 | ||
27aacd28 ST |
490 | static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode, |
491 | pgoff_t first_idx, | |
492 | const struct f2fs_io_info *fio, | |
493 | gfp_t gfp_mask) | |
494 | { | |
495 | /* | |
496 | * The f2fs garbage collector sets ->encrypted_page when it wants to | |
497 | * read/write raw data without encryption. | |
498 | */ | |
499 | if (!fio || !fio->encrypted_page) | |
500 | fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask); | |
501 | } | |
502 | ||
503 | static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode, | |
504 | pgoff_t next_idx, | |
505 | const struct f2fs_io_info *fio) | |
506 | { | |
507 | /* | |
508 | * The f2fs garbage collector sets ->encrypted_page when it wants to | |
509 | * read/write raw data without encryption. | |
510 | */ | |
511 | if (fio && fio->encrypted_page) | |
512 | return !bio_has_crypt_ctx(bio); | |
513 | ||
514 | return fscrypt_mergeable_bio(bio, inode, next_idx); | |
515 | } | |
516 | ||
bc29835a CH |
517 | void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio, |
518 | enum page_type type) | |
f5730184 | 519 | { |
bc29835a CH |
520 | WARN_ON_ONCE(!is_read_io(bio_op(bio))); |
521 | trace_f2fs_submit_read_bio(sbi->sb, type, bio); | |
0a595eba | 522 | |
bc29835a CH |
523 | iostat_update_submit_ctx(bio, type); |
524 | submit_bio(bio); | |
525 | } | |
0a595eba | 526 | |
bc29835a CH |
527 | static void f2fs_align_write_bio(struct f2fs_sb_info *sbi, struct bio *bio) |
528 | { | |
529 | unsigned int start = | |
530 | (bio->bi_iter.bi_size >> F2FS_BLKSIZE_BITS) % F2FS_IO_SIZE(sbi); | |
3bb09a0e | 531 | |
bc29835a CH |
532 | if (start == 0) |
533 | return; | |
8223ecc4 | 534 | |
bc29835a CH |
535 | /* fill dummy pages */ |
536 | for (; start < F2FS_IO_SIZE(sbi); start++) { | |
537 | struct page *page = | |
538 | mempool_alloc(sbi->write_io_dummy, | |
539 | GFP_NOIO | __GFP_NOFAIL); | |
540 | f2fs_bug_on(sbi, !page); | |
0a595eba | 541 | |
bc29835a | 542 | lock_page(page); |
0a595eba | 543 | |
bc29835a CH |
544 | zero_user_segment(page, 0, PAGE_SIZE); |
545 | set_page_private_dummy(page); | |
0a595eba | 546 | |
bc29835a CH |
547 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) |
548 | f2fs_bug_on(sbi, 1); | |
549 | } | |
550 | } | |
b763f3be | 551 | |
bc29835a CH |
552 | static void f2fs_submit_write_bio(struct f2fs_sb_info *sbi, struct bio *bio, |
553 | enum page_type type) | |
554 | { | |
555 | WARN_ON_ONCE(is_read_io(bio_op(bio))); | |
b763f3be | 556 | |
bc29835a CH |
557 | if (type == DATA || type == NODE) { |
558 | if (f2fs_lfs_mode(sbi) && current->plug) | |
559 | blk_finish_plug(current->plug); | |
b763f3be | 560 | |
bc29835a CH |
561 | if (F2FS_IO_ALIGNED(sbi)) { |
562 | f2fs_align_write_bio(sbi, bio); | |
563 | /* | |
564 | * In the NODE case, we lose next block address chain. | |
565 | * So, we need to do checkpoint in f2fs_sync_file. | |
566 | */ | |
567 | if (type == NODE) | |
568 | set_sbi_flag(sbi, SBI_NEED_CP); | |
0a595eba | 569 | } |
19a5f5e2 | 570 | } |
a4b68176 | 571 | |
bc29835a | 572 | trace_f2fs_submit_write_bio(sbi->sb, type, bio); |
a4b68176 | 573 | iostat_update_submit_ctx(bio, type); |
4e49ea4a | 574 | submit_bio(bio); |
f5730184 JK |
575 | } |
576 | ||
458e6197 | 577 | static void __submit_merged_bio(struct f2fs_bio_info *io) |
93dfe2ac | 578 | { |
458e6197 | 579 | struct f2fs_io_info *fio = &io->fio; |
93dfe2ac JK |
580 | |
581 | if (!io->bio) | |
582 | return; | |
583 | ||
bc29835a | 584 | if (is_read_io(fio->op)) { |
554b5125 | 585 | trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio); |
bc29835a CH |
586 | f2fs_submit_read_bio(io->sbi, io->bio, fio->type); |
587 | } else { | |
554b5125 | 588 | trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio); |
bc29835a CH |
589 | f2fs_submit_write_bio(io->sbi, io->bio, fio->type); |
590 | } | |
93dfe2ac JK |
591 | io->bio = NULL; |
592 | } | |
593 | ||
8648de2c | 594 | static bool __has_merged_page(struct bio *bio, struct inode *inode, |
bab475c5 | 595 | struct page *page, nid_t ino) |
0fd785eb | 596 | { |
0fd785eb | 597 | struct bio_vec *bvec; |
6dc4f100 | 598 | struct bvec_iter_all iter_all; |
0fd785eb | 599 | |
8648de2c | 600 | if (!bio) |
0fd785eb | 601 | return false; |
0c3a5797 | 602 | |
bab475c5 | 603 | if (!inode && !page && !ino) |
0c3a5797 | 604 | return true; |
0fd785eb | 605 | |
8648de2c | 606 | bio_for_each_segment_all(bvec, bio, iter_all) { |
4c8ff709 | 607 | struct page *target = bvec->bv_page; |
0fd785eb | 608 | |
4c8ff709 | 609 | if (fscrypt_is_bounce_page(target)) { |
d2d0727b | 610 | target = fscrypt_pagecache_page(target); |
4c8ff709 CY |
611 | if (IS_ERR(target)) |
612 | continue; | |
613 | } | |
614 | if (f2fs_is_compressed_page(target)) { | |
615 | target = f2fs_compress_control_page(target); | |
616 | if (IS_ERR(target)) | |
617 | continue; | |
618 | } | |
0fd785eb | 619 | |
0c3a5797 CY |
620 | if (inode && inode == target->mapping->host) |
621 | return true; | |
bab475c5 CY |
622 | if (page && page == target) |
623 | return true; | |
0c3a5797 | 624 | if (ino && ino == ino_of_node(target)) |
0fd785eb | 625 | return true; |
0fd785eb CY |
626 | } |
627 | ||
0fd785eb CY |
628 | return false; |
629 | } | |
630 | ||
908ea654 YY |
631 | int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi) |
632 | { | |
633 | int i; | |
634 | ||
635 | for (i = 0; i < NR_PAGE_TYPE; i++) { | |
636 | int n = (i == META) ? 1 : NR_TEMP_TYPE; | |
637 | int j; | |
638 | ||
639 | sbi->write_io[i] = f2fs_kmalloc(sbi, | |
640 | array_size(n, sizeof(struct f2fs_bio_info)), | |
641 | GFP_KERNEL); | |
642 | if (!sbi->write_io[i]) | |
643 | return -ENOMEM; | |
644 | ||
645 | for (j = HOT; j < n; j++) { | |
646 | init_f2fs_rwsem(&sbi->write_io[i][j].io_rwsem); | |
647 | sbi->write_io[i][j].sbi = sbi; | |
648 | sbi->write_io[i][j].bio = NULL; | |
649 | spin_lock_init(&sbi->write_io[i][j].io_lock); | |
650 | INIT_LIST_HEAD(&sbi->write_io[i][j].io_list); | |
651 | INIT_LIST_HEAD(&sbi->write_io[i][j].bio_list); | |
652 | init_f2fs_rwsem(&sbi->write_io[i][j].bio_list_lock); | |
e067dc3c DJ |
653 | #ifdef CONFIG_BLK_DEV_ZONED |
654 | init_completion(&sbi->write_io[i][j].zone_wait); | |
655 | sbi->write_io[i][j].zone_pending_bio = NULL; | |
656 | sbi->write_io[i][j].bi_private = NULL; | |
657 | #endif | |
908ea654 YY |
658 | } |
659 | } | |
660 | ||
661 | return 0; | |
662 | } | |
663 | ||
b9109b0e | 664 | static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi, |
a912b54d | 665 | enum page_type type, enum temp_type temp) |
93dfe2ac JK |
666 | { |
667 | enum page_type btype = PAGE_TYPE_OF_BIO(type); | |
a912b54d | 668 | struct f2fs_bio_info *io = sbi->write_io[btype] + temp; |
93dfe2ac | 669 | |
e4544b63 | 670 | f2fs_down_write(&io->io_rwsem); |
458e6197 | 671 | |
267c159f JK |
672 | if (!io->bio) |
673 | goto unlock_out; | |
674 | ||
458e6197 JK |
675 | /* change META to META_FLUSH in the checkpoint procedure */ |
676 | if (type >= META_FLUSH) { | |
677 | io->fio.type = META_FLUSH; | |
64bf0eef | 678 | io->bio->bi_opf |= REQ_META | REQ_PRIO | REQ_SYNC; |
70fd7614 | 679 | if (!test_opt(sbi, NOBARRIER)) |
64bf0eef | 680 | io->bio->bi_opf |= REQ_PREFLUSH | REQ_FUA; |
458e6197 JK |
681 | } |
682 | __submit_merged_bio(io); | |
267c159f | 683 | unlock_out: |
e4544b63 | 684 | f2fs_up_write(&io->io_rwsem); |
93dfe2ac JK |
685 | } |
686 | ||
a912b54d | 687 | static void __submit_merged_write_cond(struct f2fs_sb_info *sbi, |
bab475c5 CY |
688 | struct inode *inode, struct page *page, |
689 | nid_t ino, enum page_type type, bool force) | |
0c3a5797 | 690 | { |
a912b54d | 691 | enum temp_type temp; |
1e771e83 | 692 | bool ret = true; |
a912b54d JK |
693 | |
694 | for (temp = HOT; temp < NR_TEMP_TYPE; temp++) { | |
1e771e83 YS |
695 | if (!force) { |
696 | enum page_type btype = PAGE_TYPE_OF_BIO(type); | |
697 | struct f2fs_bio_info *io = sbi->write_io[btype] + temp; | |
a912b54d | 698 | |
e4544b63 | 699 | f2fs_down_read(&io->io_rwsem); |
8648de2c | 700 | ret = __has_merged_page(io->bio, inode, page, ino); |
e4544b63 | 701 | f2fs_up_read(&io->io_rwsem); |
1e771e83 YS |
702 | } |
703 | if (ret) | |
704 | __f2fs_submit_merged_write(sbi, type, temp); | |
a912b54d JK |
705 | |
706 | /* TODO: use HOT temp only for meta pages now. */ | |
707 | if (type >= META) | |
708 | break; | |
709 | } | |
0c3a5797 CY |
710 | } |
711 | ||
b9109b0e | 712 | void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type) |
0c3a5797 | 713 | { |
adcc00f7 | 714 | __submit_merged_write_cond(sbi, NULL, NULL, 0, type, true); |
0c3a5797 CY |
715 | } |
716 | ||
b9109b0e | 717 | void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi, |
bab475c5 CY |
718 | struct inode *inode, struct page *page, |
719 | nid_t ino, enum page_type type) | |
0c3a5797 | 720 | { |
bab475c5 | 721 | __submit_merged_write_cond(sbi, inode, page, ino, type, false); |
0c3a5797 CY |
722 | } |
723 | ||
b9109b0e | 724 | void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi) |
406657dd | 725 | { |
b9109b0e JK |
726 | f2fs_submit_merged_write(sbi, DATA); |
727 | f2fs_submit_merged_write(sbi, NODE); | |
728 | f2fs_submit_merged_write(sbi, META); | |
406657dd CY |
729 | } |
730 | ||
93dfe2ac JK |
731 | /* |
732 | * Fill the locked page with data located in the block address. | |
771a9a71 | 733 | * A caller needs to unlock the page on failure. |
93dfe2ac | 734 | */ |
05ca3632 | 735 | int f2fs_submit_page_bio(struct f2fs_io_info *fio) |
93dfe2ac | 736 | { |
93dfe2ac | 737 | struct bio *bio; |
0b81d077 JK |
738 | struct page *page = fio->encrypted_page ? |
739 | fio->encrypted_page : fio->page; | |
93dfe2ac | 740 | |
c9b60788 | 741 | if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, |
93770ab7 | 742 | fio->is_por ? META_POR : (__is_meta_io(fio) ? |
95fa90c9 CY |
743 | META_GENERIC : DATA_GENERIC_ENHANCE))) { |
744 | f2fs_handle_error(fio->sbi, ERROR_INVALID_BLKADDR); | |
10f966bb | 745 | return -EFSCORRUPTED; |
95fa90c9 | 746 | } |
c9b60788 | 747 | |
2ace38e0 | 748 | trace_f2fs_submit_page_bio(page, fio); |
93dfe2ac JK |
749 | |
750 | /* Allocate a new bio */ | |
b757f6ed | 751 | bio = __bio_alloc(fio, 1); |
93dfe2ac | 752 | |
27aacd28 ST |
753 | f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host, |
754 | fio->page->index, fio, GFP_NOIO); | |
755 | ||
09cbfeaf | 756 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { |
93dfe2ac | 757 | bio_put(bio); |
93dfe2ac JK |
758 | return -EFAULT; |
759 | } | |
78efac53 CY |
760 | |
761 | if (fio->io_wbc && !is_read_io(fio->op)) | |
844545c5 | 762 | wbc_account_cgroup_owner(fio->io_wbc, fio->page, PAGE_SIZE); |
78efac53 | 763 | |
5f9abab4 | 764 | inc_page_count(fio->sbi, is_read_io(fio->op) ? |
544b53da | 765 | __read_io_type(page) : WB_DATA_TYPE(fio->page)); |
4c58ed07 | 766 | |
bc29835a CH |
767 | if (is_read_io(bio_op(bio))) |
768 | f2fs_submit_read_bio(fio->sbi, bio, fio->type); | |
769 | else | |
770 | f2fs_submit_write_bio(fio->sbi, bio, fio->type); | |
93dfe2ac JK |
771 | return 0; |
772 | } | |
773 | ||
8896cbdf CY |
774 | static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio, |
775 | block_t last_blkaddr, block_t cur_blkaddr) | |
776 | { | |
10208567 JK |
777 | if (unlikely(sbi->max_io_bytes && |
778 | bio->bi_iter.bi_size >= sbi->max_io_bytes)) | |
779 | return false; | |
8896cbdf CY |
780 | if (last_blkaddr + 1 != cur_blkaddr) |
781 | return false; | |
309dca30 | 782 | return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL); |
8896cbdf CY |
783 | } |
784 | ||
785 | static bool io_type_is_mergeable(struct f2fs_bio_info *io, | |
786 | struct f2fs_io_info *fio) | |
787 | { | |
788 | if (io->fio.op != fio->op) | |
789 | return false; | |
790 | return io->fio.op_flags == fio->op_flags; | |
791 | } | |
792 | ||
793 | static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio, | |
794 | struct f2fs_bio_info *io, | |
795 | struct f2fs_io_info *fio, | |
796 | block_t last_blkaddr, | |
797 | block_t cur_blkaddr) | |
798 | { | |
c72db71e CY |
799 | if (F2FS_IO_ALIGNED(sbi) && (fio->type == DATA || fio->type == NODE)) { |
800 | unsigned int filled_blocks = | |
801 | F2FS_BYTES_TO_BLK(bio->bi_iter.bi_size); | |
802 | unsigned int io_size = F2FS_IO_SIZE(sbi); | |
803 | unsigned int left_vecs = bio->bi_max_vecs - bio->bi_vcnt; | |
804 | ||
805 | /* IOs in bio is aligned and left space of vectors is not enough */ | |
806 | if (!(filled_blocks % io_size) && left_vecs < io_size) | |
807 | return false; | |
808 | } | |
8896cbdf CY |
809 | if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr)) |
810 | return false; | |
811 | return io_type_is_mergeable(io, fio); | |
812 | } | |
813 | ||
0b20fcec CY |
814 | static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio, |
815 | struct page *page, enum temp_type temp) | |
816 | { | |
817 | struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; | |
818 | struct bio_entry *be; | |
819 | ||
32410577 | 820 | be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL); |
0b20fcec CY |
821 | be->bio = bio; |
822 | bio_get(bio); | |
823 | ||
824 | if (bio_add_page(bio, page, PAGE_SIZE, 0) != PAGE_SIZE) | |
825 | f2fs_bug_on(sbi, 1); | |
826 | ||
e4544b63 | 827 | f2fs_down_write(&io->bio_list_lock); |
0b20fcec | 828 | list_add_tail(&be->list, &io->bio_list); |
e4544b63 | 829 | f2fs_up_write(&io->bio_list_lock); |
0b20fcec CY |
830 | } |
831 | ||
832 | static void del_bio_entry(struct bio_entry *be) | |
833 | { | |
834 | list_del(&be->list); | |
835 | kmem_cache_free(bio_entry_slab, be); | |
836 | } | |
837 | ||
27aacd28 | 838 | static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio, |
0b20fcec CY |
839 | struct page *page) |
840 | { | |
27aacd28 | 841 | struct f2fs_sb_info *sbi = fio->sbi; |
0b20fcec CY |
842 | enum temp_type temp; |
843 | bool found = false; | |
844 | int ret = -EAGAIN; | |
845 | ||
846 | for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { | |
847 | struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; | |
848 | struct list_head *head = &io->bio_list; | |
849 | struct bio_entry *be; | |
850 | ||
e4544b63 | 851 | f2fs_down_write(&io->bio_list_lock); |
0b20fcec CY |
852 | list_for_each_entry(be, head, list) { |
853 | if (be->bio != *bio) | |
854 | continue; | |
855 | ||
856 | found = true; | |
857 | ||
27aacd28 ST |
858 | f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio, |
859 | *fio->last_block, | |
860 | fio->new_blkaddr)); | |
861 | if (f2fs_crypt_mergeable_bio(*bio, | |
862 | fio->page->mapping->host, | |
863 | fio->page->index, fio) && | |
864 | bio_add_page(*bio, page, PAGE_SIZE, 0) == | |
865 | PAGE_SIZE) { | |
0b20fcec CY |
866 | ret = 0; |
867 | break; | |
868 | } | |
869 | ||
27aacd28 | 870 | /* page can't be merged into bio; submit the bio */ |
0b20fcec | 871 | del_bio_entry(be); |
bc29835a | 872 | f2fs_submit_write_bio(sbi, *bio, DATA); |
0b20fcec CY |
873 | break; |
874 | } | |
e4544b63 | 875 | f2fs_up_write(&io->bio_list_lock); |
0b20fcec CY |
876 | } |
877 | ||
878 | if (ret) { | |
879 | bio_put(*bio); | |
880 | *bio = NULL; | |
881 | } | |
882 | ||
883 | return ret; | |
884 | } | |
885 | ||
886 | void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi, | |
887 | struct bio **bio, struct page *page) | |
888 | { | |
889 | enum temp_type temp; | |
890 | bool found = false; | |
891 | struct bio *target = bio ? *bio : NULL; | |
892 | ||
5cdb422c CY |
893 | f2fs_bug_on(sbi, !target && !page); |
894 | ||
0b20fcec CY |
895 | for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { |
896 | struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; | |
897 | struct list_head *head = &io->bio_list; | |
898 | struct bio_entry *be; | |
899 | ||
900 | if (list_empty(head)) | |
901 | continue; | |
902 | ||
e4544b63 | 903 | f2fs_down_read(&io->bio_list_lock); |
0b20fcec CY |
904 | list_for_each_entry(be, head, list) { |
905 | if (target) | |
906 | found = (target == be->bio); | |
907 | else | |
908 | found = __has_merged_page(be->bio, NULL, | |
909 | page, 0); | |
910 | if (found) | |
911 | break; | |
912 | } | |
e4544b63 | 913 | f2fs_up_read(&io->bio_list_lock); |
0b20fcec CY |
914 | |
915 | if (!found) | |
916 | continue; | |
917 | ||
918 | found = false; | |
919 | ||
e4544b63 | 920 | f2fs_down_write(&io->bio_list_lock); |
0b20fcec CY |
921 | list_for_each_entry(be, head, list) { |
922 | if (target) | |
923 | found = (target == be->bio); | |
924 | else | |
925 | found = __has_merged_page(be->bio, NULL, | |
926 | page, 0); | |
927 | if (found) { | |
928 | target = be->bio; | |
929 | del_bio_entry(be); | |
930 | break; | |
931 | } | |
932 | } | |
e4544b63 | 933 | f2fs_up_write(&io->bio_list_lock); |
0b20fcec CY |
934 | } |
935 | ||
936 | if (found) | |
bc29835a | 937 | f2fs_submit_write_bio(sbi, target, DATA); |
0b20fcec CY |
938 | if (bio && *bio) { |
939 | bio_put(*bio); | |
940 | *bio = NULL; | |
941 | } | |
942 | } | |
943 | ||
8648de2c CY |
944 | int f2fs_merge_page_bio(struct f2fs_io_info *fio) |
945 | { | |
946 | struct bio *bio = *fio->bio; | |
947 | struct page *page = fio->encrypted_page ? | |
948 | fio->encrypted_page : fio->page; | |
949 | ||
950 | if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, | |
95fa90c9 CY |
951 | __is_meta_io(fio) ? META_GENERIC : DATA_GENERIC)) { |
952 | f2fs_handle_error(fio->sbi, ERROR_INVALID_BLKADDR); | |
10f966bb | 953 | return -EFSCORRUPTED; |
95fa90c9 | 954 | } |
8648de2c CY |
955 | |
956 | trace_f2fs_submit_page_bio(page, fio); | |
8648de2c | 957 | |
8896cbdf | 958 | if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block, |
0b20fcec CY |
959 | fio->new_blkaddr)) |
960 | f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL); | |
8648de2c CY |
961 | alloc_new: |
962 | if (!bio) { | |
a8affc03 | 963 | bio = __bio_alloc(fio, BIO_MAX_VECS); |
27aacd28 ST |
964 | f2fs_set_bio_crypt_ctx(bio, fio->page->mapping->host, |
965 | fio->page->index, fio, GFP_NOIO); | |
8648de2c | 966 | |
0b20fcec CY |
967 | add_bio_entry(fio->sbi, bio, page, fio->temp); |
968 | } else { | |
27aacd28 | 969 | if (add_ipu_page(fio, &bio, page)) |
0b20fcec | 970 | goto alloc_new; |
8648de2c CY |
971 | } |
972 | ||
973 | if (fio->io_wbc) | |
844545c5 | 974 | wbc_account_cgroup_owner(fio->io_wbc, fio->page, PAGE_SIZE); |
8648de2c CY |
975 | |
976 | inc_page_count(fio->sbi, WB_DATA_TYPE(page)); | |
977 | ||
978 | *fio->last_block = fio->new_blkaddr; | |
979 | *fio->bio = bio; | |
980 | ||
981 | return 0; | |
982 | } | |
983 | ||
e067dc3c DJ |
984 | #ifdef CONFIG_BLK_DEV_ZONED |
985 | static bool is_end_zone_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr) | |
986 | { | |
987 | int devi = 0; | |
988 | ||
989 | if (f2fs_is_multi_device(sbi)) { | |
990 | devi = f2fs_target_device_index(sbi, blkaddr); | |
991 | if (blkaddr < FDEV(devi).start_blk || | |
992 | blkaddr > FDEV(devi).end_blk) { | |
993 | f2fs_err(sbi, "Invalid block %x", blkaddr); | |
994 | return false; | |
995 | } | |
996 | blkaddr -= FDEV(devi).start_blk; | |
997 | } | |
998 | return bdev_zoned_model(FDEV(devi).bdev) == BLK_ZONED_HM && | |
999 | f2fs_blkz_is_seq(sbi, devi, blkaddr) && | |
1000 | (blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1); | |
1001 | } | |
1002 | #endif | |
1003 | ||
fe16efe6 | 1004 | void f2fs_submit_page_write(struct f2fs_io_info *fio) |
93dfe2ac | 1005 | { |
05ca3632 | 1006 | struct f2fs_sb_info *sbi = fio->sbi; |
458e6197 | 1007 | enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
a912b54d | 1008 | struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp; |
4375a336 | 1009 | struct page *bio_page; |
93dfe2ac | 1010 | |
b9109b0e | 1011 | f2fs_bug_on(sbi, is_read_io(fio->op)); |
93dfe2ac | 1012 | |
e4544b63 | 1013 | f2fs_down_write(&io->io_rwsem); |
e067dc3c DJ |
1014 | |
1015 | #ifdef CONFIG_BLK_DEV_ZONED | |
1016 | if (f2fs_sb_has_blkzoned(sbi) && btype < META && io->zone_pending_bio) { | |
1017 | wait_for_completion_io(&io->zone_wait); | |
1018 | bio_put(io->zone_pending_bio); | |
1019 | io->zone_pending_bio = NULL; | |
1020 | io->bi_private = NULL; | |
1021 | } | |
1022 | #endif | |
1023 | ||
fb830fc5 CY |
1024 | next: |
1025 | if (fio->in_list) { | |
1026 | spin_lock(&io->io_lock); | |
1027 | if (list_empty(&io->io_list)) { | |
1028 | spin_unlock(&io->io_lock); | |
fe16efe6 | 1029 | goto out; |
fb830fc5 CY |
1030 | } |
1031 | fio = list_first_entry(&io->io_list, | |
1032 | struct f2fs_io_info, list); | |
1033 | list_del(&fio->list); | |
1034 | spin_unlock(&io->io_lock); | |
1035 | } | |
93dfe2ac | 1036 | |
93770ab7 | 1037 | verify_fio_blkaddr(fio); |
93dfe2ac | 1038 | |
4c8ff709 CY |
1039 | if (fio->encrypted_page) |
1040 | bio_page = fio->encrypted_page; | |
1041 | else if (fio->compressed_page) | |
1042 | bio_page = fio->compressed_page; | |
1043 | else | |
1044 | bio_page = fio->page; | |
36951b38 | 1045 | |
ebf7c522 | 1046 | /* set submitted = true as a return value */ |
2eae077e | 1047 | fio->submitted = 1; |
d68f735b | 1048 | |
b9109b0e | 1049 | inc_page_count(sbi, WB_DATA_TYPE(bio_page)); |
93dfe2ac | 1050 | |
27aacd28 ST |
1051 | if (io->bio && |
1052 | (!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio, | |
1053 | fio->new_blkaddr) || | |
1054 | !f2fs_crypt_mergeable_bio(io->bio, fio->page->mapping->host, | |
1055 | bio_page->index, fio))) | |
458e6197 | 1056 | __submit_merged_bio(io); |
93dfe2ac JK |
1057 | alloc_new: |
1058 | if (io->bio == NULL) { | |
8223ecc4 CY |
1059 | if (F2FS_IO_ALIGNED(sbi) && |
1060 | (fio->type == DATA || fio->type == NODE) && | |
0a595eba | 1061 | fio->new_blkaddr & F2FS_IO_SIZE_MASK(sbi)) { |
b9109b0e | 1062 | dec_page_count(sbi, WB_DATA_TYPE(bio_page)); |
2eae077e | 1063 | fio->retry = 1; |
fe16efe6 | 1064 | goto skip; |
0a595eba | 1065 | } |
a8affc03 | 1066 | io->bio = __bio_alloc(fio, BIO_MAX_VECS); |
27aacd28 ST |
1067 | f2fs_set_bio_crypt_ctx(io->bio, fio->page->mapping->host, |
1068 | bio_page->index, fio, GFP_NOIO); | |
458e6197 | 1069 | io->fio = *fio; |
93dfe2ac JK |
1070 | } |
1071 | ||
a912b54d | 1072 | if (bio_add_page(io->bio, bio_page, PAGE_SIZE, 0) < PAGE_SIZE) { |
458e6197 | 1073 | __submit_merged_bio(io); |
93dfe2ac JK |
1074 | goto alloc_new; |
1075 | } | |
1076 | ||
578c6478 | 1077 | if (fio->io_wbc) |
844545c5 | 1078 | wbc_account_cgroup_owner(fio->io_wbc, fio->page, PAGE_SIZE); |
578c6478 | 1079 | |
7a9d7548 | 1080 | io->last_block_in_bio = fio->new_blkaddr; |
fb830fc5 CY |
1081 | |
1082 | trace_f2fs_submit_page_write(fio->page, fio); | |
fe16efe6 | 1083 | skip: |
fb830fc5 CY |
1084 | if (fio->in_list) |
1085 | goto next; | |
fe16efe6 | 1086 | out: |
e067dc3c DJ |
1087 | #ifdef CONFIG_BLK_DEV_ZONED |
1088 | if (f2fs_sb_has_blkzoned(sbi) && btype < META && | |
1089 | is_end_zone_blkaddr(sbi, fio->new_blkaddr)) { | |
1090 | bio_get(io->bio); | |
1091 | reinit_completion(&io->zone_wait); | |
1092 | io->bi_private = io->bio->bi_private; | |
1093 | io->bio->bi_private = io; | |
1094 | io->bio->bi_end_io = f2fs_zone_write_end_io; | |
1095 | io->zone_pending_bio = io->bio; | |
1096 | __submit_merged_bio(io); | |
1097 | } | |
1098 | #endif | |
4354994f | 1099 | if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || |
00e09c0b | 1100 | !f2fs_is_checkpoint_ready(sbi)) |
5ce80586 | 1101 | __submit_merged_bio(io); |
e4544b63 | 1102 | f2fs_up_write(&io->io_rwsem); |
93dfe2ac JK |
1103 | } |
1104 | ||
13ba41e3 | 1105 | static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr, |
7649c873 | 1106 | unsigned nr_pages, blk_opf_t op_flag, |
7f59b277 | 1107 | pgoff_t first_idx, bool for_write) |
13ba41e3 JK |
1108 | { |
1109 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
13ba41e3 | 1110 | struct bio *bio; |
a4b68176 | 1111 | struct bio_post_read_ctx *ctx = NULL; |
6dbb1796 | 1112 | unsigned int post_read_steps = 0; |
5189810a CH |
1113 | sector_t sector; |
1114 | struct block_device *bdev = f2fs_target_device(sbi, blkaddr, §or); | |
13ba41e3 | 1115 | |
64bf0eef CH |
1116 | bio = bio_alloc_bioset(bdev, bio_max_segs(nr_pages), |
1117 | REQ_OP_READ | op_flag, | |
609be106 | 1118 | for_write ? GFP_NOIO : GFP_KERNEL, &f2fs_bioset); |
6dbb1796 | 1119 | if (!bio) |
13ba41e3 | 1120 | return ERR_PTR(-ENOMEM); |
5189810a | 1121 | bio->bi_iter.bi_sector = sector; |
27aacd28 | 1122 | f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS); |
13ba41e3 | 1123 | bio->bi_end_io = f2fs_read_end_io; |
13ba41e3 | 1124 | |
27aacd28 | 1125 | if (fscrypt_inode_uses_fs_layer_crypto(inode)) |
7f59b277 | 1126 | post_read_steps |= STEP_DECRYPT; |
95ae251f | 1127 | |
7f59b277 EB |
1128 | if (f2fs_need_verity(inode, first_idx)) |
1129 | post_read_steps |= STEP_VERITY; | |
1130 | ||
1131 | /* | |
1132 | * STEP_DECOMPRESS is handled specially, since a compressed file might | |
1133 | * contain both compressed and uncompressed clusters. We'll allocate a | |
1134 | * bio_post_read_ctx if the file is compressed, but the caller is | |
1135 | * responsible for enabling STEP_DECOMPRESS if it's actually needed. | |
1136 | */ | |
1137 | ||
1138 | if (post_read_steps || f2fs_compressed_file(inode)) { | |
e8ce5749 | 1139 | /* Due to the mempool, this never fails. */ |
6dbb1796 | 1140 | ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS); |
6dbb1796 | 1141 | ctx->bio = bio; |
4c8ff709 | 1142 | ctx->sbi = sbi; |
6dbb1796 | 1143 | ctx->enabled_steps = post_read_steps; |
4931e0c9 | 1144 | ctx->fs_blkaddr = blkaddr; |
98dc08ba | 1145 | ctx->decompression_attempted = false; |
6dbb1796 | 1146 | bio->bi_private = ctx; |
6dbb1796 | 1147 | } |
a4b68176 | 1148 | iostat_alloc_and_bind_ctx(sbi, bio, ctx); |
6dbb1796 | 1149 | |
13ba41e3 JK |
1150 | return bio; |
1151 | } | |
1152 | ||
1153 | /* This can handle encryption stuffs */ | |
1154 | static int f2fs_submit_page_read(struct inode *inode, struct page *page, | |
7649c873 BVA |
1155 | block_t blkaddr, blk_opf_t op_flags, |
1156 | bool for_write) | |
13ba41e3 | 1157 | { |
93770ab7 CY |
1158 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
1159 | struct bio *bio; | |
13ba41e3 | 1160 | |
b7973091 | 1161 | bio = f2fs_grab_read_bio(inode, blkaddr, 1, op_flags, |
7f59b277 | 1162 | page->index, for_write); |
13ba41e3 JK |
1163 | if (IS_ERR(bio)) |
1164 | return PTR_ERR(bio); | |
1165 | ||
0ded69f6 JK |
1166 | /* wait for GCed page writeback via META_MAPPING */ |
1167 | f2fs_wait_on_block_writeback(inode, blkaddr); | |
1168 | ||
13ba41e3 | 1169 | if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { |
5118697f CY |
1170 | iostat_update_and_unbind_ctx(bio); |
1171 | if (bio->bi_private) | |
1172 | mempool_free(bio->bi_private, bio_post_read_ctx_pool); | |
13ba41e3 JK |
1173 | bio_put(bio); |
1174 | return -EFAULT; | |
1175 | } | |
93770ab7 | 1176 | inc_page_count(sbi, F2FS_RD_DATA); |
34a23525 | 1177 | f2fs_update_iostat(sbi, NULL, FS_DATA_READ_IO, F2FS_BLKSIZE); |
bc29835a | 1178 | f2fs_submit_read_bio(sbi, bio, DATA); |
13ba41e3 JK |
1179 | return 0; |
1180 | } | |
1181 | ||
46008c6d CY |
1182 | static void __set_data_blkaddr(struct dnode_of_data *dn) |
1183 | { | |
1184 | struct f2fs_node *rn = F2FS_NODE(dn->node_page); | |
1185 | __le32 *addr_array; | |
7a2af766 CY |
1186 | int base = 0; |
1187 | ||
1188 | if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode)) | |
1189 | base = get_extra_isize(dn->inode); | |
46008c6d CY |
1190 | |
1191 | /* Get physical address of data block */ | |
1192 | addr_array = blkaddr_in_node(rn); | |
7a2af766 | 1193 | addr_array[base + dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr); |
46008c6d CY |
1194 | } |
1195 | ||
0a8165d7 | 1196 | /* |
eb47b800 JK |
1197 | * Lock ordering for the change of data block address: |
1198 | * ->data_page | |
1199 | * ->node_page | |
1200 | * update block addresses in the node page | |
1201 | */ | |
4d57b86d | 1202 | void f2fs_set_data_blkaddr(struct dnode_of_data *dn) |
eb47b800 | 1203 | { |
bae0ee7a | 1204 | f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true); |
46008c6d CY |
1205 | __set_data_blkaddr(dn); |
1206 | if (set_page_dirty(dn->node_page)) | |
12719ae1 | 1207 | dn->node_changed = true; |
eb47b800 JK |
1208 | } |
1209 | ||
f28b3434 CY |
1210 | void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) |
1211 | { | |
1212 | dn->data_blkaddr = blkaddr; | |
4d57b86d | 1213 | f2fs_set_data_blkaddr(dn); |
e7547dac | 1214 | f2fs_update_read_extent_cache(dn); |
f28b3434 CY |
1215 | } |
1216 | ||
46008c6d | 1217 | /* dn->ofs_in_node will be returned with up-to-date last block pointer */ |
4d57b86d | 1218 | int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count) |
eb47b800 | 1219 | { |
4081363f | 1220 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
0abd675e | 1221 | int err; |
eb47b800 | 1222 | |
46008c6d CY |
1223 | if (!count) |
1224 | return 0; | |
1225 | ||
91942321 | 1226 | if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
eb47b800 | 1227 | return -EPERM; |
0abd675e CY |
1228 | if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count)))) |
1229 | return err; | |
eb47b800 | 1230 | |
46008c6d CY |
1231 | trace_f2fs_reserve_new_blocks(dn->inode, dn->nid, |
1232 | dn->ofs_in_node, count); | |
1233 | ||
bae0ee7a | 1234 | f2fs_wait_on_page_writeback(dn->node_page, NODE, true, true); |
46008c6d CY |
1235 | |
1236 | for (; count > 0; dn->ofs_in_node++) { | |
a2ced1ce | 1237 | block_t blkaddr = f2fs_data_blkaddr(dn); |
5f029c04 | 1238 | |
46008c6d CY |
1239 | if (blkaddr == NULL_ADDR) { |
1240 | dn->data_blkaddr = NEW_ADDR; | |
1241 | __set_data_blkaddr(dn); | |
1242 | count--; | |
1243 | } | |
1244 | } | |
1245 | ||
1246 | if (set_page_dirty(dn->node_page)) | |
1247 | dn->node_changed = true; | |
eb47b800 JK |
1248 | return 0; |
1249 | } | |
1250 | ||
46008c6d | 1251 | /* Should keep dn->ofs_in_node unchanged */ |
4d57b86d | 1252 | int f2fs_reserve_new_block(struct dnode_of_data *dn) |
46008c6d CY |
1253 | { |
1254 | unsigned int ofs_in_node = dn->ofs_in_node; | |
1255 | int ret; | |
1256 | ||
4d57b86d | 1257 | ret = f2fs_reserve_new_blocks(dn, 1); |
46008c6d CY |
1258 | dn->ofs_in_node = ofs_in_node; |
1259 | return ret; | |
1260 | } | |
1261 | ||
b600965c HL |
1262 | int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
1263 | { | |
1264 | bool need_put = dn->inode_page ? false : true; | |
1265 | int err; | |
1266 | ||
4d57b86d | 1267 | err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE); |
b600965c HL |
1268 | if (err) |
1269 | return err; | |
a8865372 | 1270 | |
b600965c | 1271 | if (dn->data_blkaddr == NULL_ADDR) |
4d57b86d | 1272 | err = f2fs_reserve_new_block(dn); |
a8865372 | 1273 | if (err || need_put) |
b600965c HL |
1274 | f2fs_put_dnode(dn); |
1275 | return err; | |
1276 | } | |
1277 | ||
4d57b86d | 1278 | struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index, |
59237a21 CY |
1279 | blk_opf_t op_flags, bool for_write, |
1280 | pgoff_t *next_pgofs) | |
eb47b800 | 1281 | { |
eb47b800 JK |
1282 | struct address_space *mapping = inode->i_mapping; |
1283 | struct dnode_of_data dn; | |
1284 | struct page *page; | |
1285 | int err; | |
4375a336 | 1286 | |
a56c7c6f | 1287 | page = f2fs_grab_cache_page(mapping, index, for_write); |
650495de JK |
1288 | if (!page) |
1289 | return ERR_PTR(-ENOMEM); | |
1290 | ||
04a91ab0 CH |
1291 | if (f2fs_lookup_read_extent_cache_block(inode, index, |
1292 | &dn.data_blkaddr)) { | |
93770ab7 CY |
1293 | if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr, |
1294 | DATA_GENERIC_ENHANCE_READ)) { | |
10f966bb | 1295 | err = -EFSCORRUPTED; |
95fa90c9 CY |
1296 | f2fs_handle_error(F2FS_I_SB(inode), |
1297 | ERROR_INVALID_BLKADDR); | |
93770ab7 CY |
1298 | goto put_err; |
1299 | } | |
cb3bc9ee CY |
1300 | goto got_it; |
1301 | } | |
1302 | ||
eb47b800 | 1303 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
4d57b86d | 1304 | err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); |
59237a21 CY |
1305 | if (err) { |
1306 | if (err == -ENOENT && next_pgofs) | |
1307 | *next_pgofs = f2fs_get_next_page_offset(&dn, index); | |
86531d6b | 1308 | goto put_err; |
59237a21 | 1309 | } |
eb47b800 JK |
1310 | f2fs_put_dnode(&dn); |
1311 | ||
6bacf52f | 1312 | if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
86531d6b | 1313 | err = -ENOENT; |
59237a21 CY |
1314 | if (next_pgofs) |
1315 | *next_pgofs = index + 1; | |
86531d6b | 1316 | goto put_err; |
650495de | 1317 | } |
93770ab7 CY |
1318 | if (dn.data_blkaddr != NEW_ADDR && |
1319 | !f2fs_is_valid_blkaddr(F2FS_I_SB(inode), | |
1320 | dn.data_blkaddr, | |
1321 | DATA_GENERIC_ENHANCE)) { | |
10f966bb | 1322 | err = -EFSCORRUPTED; |
95fa90c9 CY |
1323 | f2fs_handle_error(F2FS_I_SB(inode), |
1324 | ERROR_INVALID_BLKADDR); | |
93770ab7 CY |
1325 | goto put_err; |
1326 | } | |
cb3bc9ee | 1327 | got_it: |
43f3eae1 JK |
1328 | if (PageUptodate(page)) { |
1329 | unlock_page(page); | |
eb47b800 | 1330 | return page; |
43f3eae1 | 1331 | } |
eb47b800 | 1332 | |
d59ff4df JK |
1333 | /* |
1334 | * A new dentry page is allocated but not able to be written, since its | |
1335 | * new inode page couldn't be allocated due to -ENOSPC. | |
1336 | * In such the case, its blkaddr can be remained as NEW_ADDR. | |
4d57b86d CY |
1337 | * see, f2fs_add_link -> f2fs_get_new_data_page -> |
1338 | * f2fs_init_inode_metadata. | |
d59ff4df JK |
1339 | */ |
1340 | if (dn.data_blkaddr == NEW_ADDR) { | |
09cbfeaf | 1341 | zero_user_segment(page, 0, PAGE_SIZE); |
237c0790 JK |
1342 | if (!PageUptodate(page)) |
1343 | SetPageUptodate(page); | |
43f3eae1 | 1344 | unlock_page(page); |
d59ff4df JK |
1345 | return page; |
1346 | } | |
eb47b800 | 1347 | |
b7973091 JY |
1348 | err = f2fs_submit_page_read(inode, page, dn.data_blkaddr, |
1349 | op_flags, for_write); | |
393ff91f | 1350 | if (err) |
86531d6b | 1351 | goto put_err; |
43f3eae1 | 1352 | return page; |
86531d6b JK |
1353 | |
1354 | put_err: | |
1355 | f2fs_put_page(page, 1); | |
1356 | return ERR_PTR(err); | |
43f3eae1 JK |
1357 | } |
1358 | ||
59237a21 CY |
1359 | struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index, |
1360 | pgoff_t *next_pgofs) | |
43f3eae1 JK |
1361 | { |
1362 | struct address_space *mapping = inode->i_mapping; | |
1363 | struct page *page; | |
1364 | ||
1365 | page = find_get_page(mapping, index); | |
1366 | if (page && PageUptodate(page)) | |
1367 | return page; | |
1368 | f2fs_put_page(page, 0); | |
1369 | ||
59237a21 | 1370 | page = f2fs_get_read_data_page(inode, index, 0, false, next_pgofs); |
43f3eae1 JK |
1371 | if (IS_ERR(page)) |
1372 | return page; | |
1373 | ||
1374 | if (PageUptodate(page)) | |
1375 | return page; | |
1376 | ||
1377 | wait_on_page_locked(page); | |
1378 | if (unlikely(!PageUptodate(page))) { | |
1379 | f2fs_put_page(page, 0); | |
1380 | return ERR_PTR(-EIO); | |
1381 | } | |
1382 | return page; | |
1383 | } | |
1384 | ||
1385 | /* | |
1386 | * If it tries to access a hole, return an error. | |
1387 | * Because, the callers, functions in dir.c and GC, should be able to know | |
1388 | * whether this page exists or not. | |
1389 | */ | |
4d57b86d | 1390 | struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index, |
a56c7c6f | 1391 | bool for_write) |
43f3eae1 JK |
1392 | { |
1393 | struct address_space *mapping = inode->i_mapping; | |
1394 | struct page *page; | |
d2d9bb3b | 1395 | |
59237a21 | 1396 | page = f2fs_get_read_data_page(inode, index, 0, for_write, NULL); |
43f3eae1 JK |
1397 | if (IS_ERR(page)) |
1398 | return page; | |
393ff91f | 1399 | |
43f3eae1 | 1400 | /* wait for read completion */ |
393ff91f | 1401 | lock_page(page); |
d2d9bb3b | 1402 | if (unlikely(page->mapping != mapping || !PageUptodate(page))) { |
1563ac75 CY |
1403 | f2fs_put_page(page, 1); |
1404 | return ERR_PTR(-EIO); | |
1405 | } | |
eb47b800 JK |
1406 | return page; |
1407 | } | |
1408 | ||
0a8165d7 | 1409 | /* |
eb47b800 JK |
1410 | * Caller ensures that this data page is never allocated. |
1411 | * A new zero-filled data page is allocated in the page cache. | |
39936837 | 1412 | * |
4f4124d0 CY |
1413 | * Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
1414 | * f2fs_unlock_op(). | |
470f00e9 CY |
1415 | * Note that, ipage is set only by make_empty_dir, and if any error occur, |
1416 | * ipage should be released by this function. | |
eb47b800 | 1417 | */ |
4d57b86d | 1418 | struct page *f2fs_get_new_data_page(struct inode *inode, |
a8865372 | 1419 | struct page *ipage, pgoff_t index, bool new_i_size) |
eb47b800 | 1420 | { |
eb47b800 JK |
1421 | struct address_space *mapping = inode->i_mapping; |
1422 | struct page *page; | |
1423 | struct dnode_of_data dn; | |
1424 | int err; | |
7612118a | 1425 | |
a56c7c6f | 1426 | page = f2fs_grab_cache_page(mapping, index, true); |
470f00e9 CY |
1427 | if (!page) { |
1428 | /* | |
1429 | * before exiting, we should make sure ipage will be released | |
1430 | * if any error occur. | |
1431 | */ | |
1432 | f2fs_put_page(ipage, 1); | |
01f28610 | 1433 | return ERR_PTR(-ENOMEM); |
470f00e9 | 1434 | } |
eb47b800 | 1435 | |
a8865372 | 1436 | set_new_dnode(&dn, inode, ipage, NULL, 0); |
b600965c | 1437 | err = f2fs_reserve_block(&dn, index); |
01f28610 JK |
1438 | if (err) { |
1439 | f2fs_put_page(page, 1); | |
eb47b800 | 1440 | return ERR_PTR(err); |
a8865372 | 1441 | } |
01f28610 JK |
1442 | if (!ipage) |
1443 | f2fs_put_dnode(&dn); | |
eb47b800 JK |
1444 | |
1445 | if (PageUptodate(page)) | |
01f28610 | 1446 | goto got_it; |
eb47b800 JK |
1447 | |
1448 | if (dn.data_blkaddr == NEW_ADDR) { | |
09cbfeaf | 1449 | zero_user_segment(page, 0, PAGE_SIZE); |
237c0790 JK |
1450 | if (!PageUptodate(page)) |
1451 | SetPageUptodate(page); | |
eb47b800 | 1452 | } else { |
4375a336 | 1453 | f2fs_put_page(page, 1); |
a8865372 | 1454 | |
7612118a JK |
1455 | /* if ipage exists, blkaddr should be NEW_ADDR */ |
1456 | f2fs_bug_on(F2FS_I_SB(inode), ipage); | |
4d57b86d | 1457 | page = f2fs_get_lock_data_page(inode, index, true); |
4375a336 | 1458 | if (IS_ERR(page)) |
7612118a | 1459 | return page; |
eb47b800 | 1460 | } |
01f28610 | 1461 | got_it: |
9edcdabf | 1462 | if (new_i_size && i_size_read(inode) < |
ee6d182f | 1463 | ((loff_t)(index + 1) << PAGE_SHIFT)) |
fc9581c8 | 1464 | f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT)); |
eb47b800 JK |
1465 | return page; |
1466 | } | |
1467 | ||
d5097be5 | 1468 | static int __allocate_data_block(struct dnode_of_data *dn, int seg_type) |
bfad7c2d | 1469 | { |
4081363f | 1470 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
bfad7c2d | 1471 | struct f2fs_summary sum; |
bfad7c2d | 1472 | struct node_info ni; |
6aa58d8a | 1473 | block_t old_blkaddr; |
46008c6d | 1474 | blkcnt_t count = 1; |
0abd675e | 1475 | int err; |
bfad7c2d | 1476 | |
91942321 | 1477 | if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
bfad7c2d | 1478 | return -EPERM; |
df6136ef | 1479 | |
a9419b63 | 1480 | err = f2fs_get_node_info(sbi, dn->nid, &ni, false); |
7735730d CY |
1481 | if (err) |
1482 | return err; | |
1483 | ||
a2ced1ce | 1484 | dn->data_blkaddr = f2fs_data_blkaddr(dn); |
3cf684f2 CH |
1485 | if (dn->data_blkaddr == NULL_ADDR) { |
1486 | err = inc_valid_block_count(sbi, dn->inode, &count); | |
1487 | if (unlikely(err)) | |
1488 | return err; | |
1489 | } | |
bfad7c2d | 1490 | |
bfad7c2d | 1491 | set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
6aa58d8a CY |
1492 | old_blkaddr = dn->data_blkaddr; |
1493 | f2fs_allocate_data_block(sbi, NULL, old_blkaddr, &dn->data_blkaddr, | |
093749e2 | 1494 | &sum, seg_type, NULL); |
6ce19aff | 1495 | if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) { |
6aa58d8a CY |
1496 | invalidate_mapping_pages(META_MAPPING(sbi), |
1497 | old_blkaddr, old_blkaddr); | |
6ce19aff CY |
1498 | f2fs_invalidate_compress_page(sbi, old_blkaddr); |
1499 | } | |
86f35dc3 | 1500 | f2fs_update_data_blkaddr(dn, dn->data_blkaddr); |
bfad7c2d JK |
1501 | return 0; |
1502 | } | |
1503 | ||
2f51ade9 | 1504 | static void f2fs_map_lock(struct f2fs_sb_info *sbi, int flag) |
59c9081b | 1505 | { |
2f51ade9 CH |
1506 | if (flag == F2FS_GET_BLOCK_PRE_AIO) |
1507 | f2fs_down_read(&sbi->node_change); | |
1508 | else | |
1509 | f2fs_lock_op(sbi); | |
1510 | } | |
1511 | ||
1512 | static void f2fs_map_unlock(struct f2fs_sb_info *sbi, int flag) | |
1513 | { | |
1514 | if (flag == F2FS_GET_BLOCK_PRE_AIO) | |
1515 | f2fs_up_read(&sbi->node_change); | |
1516 | else | |
1517 | f2fs_unlock_op(sbi); | |
59c9081b YH |
1518 | } |
1519 | ||
cf342d3b CH |
1520 | int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index) |
1521 | { | |
1522 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); | |
ffdeab71 | 1523 | int err = 0; |
cf342d3b | 1524 | |
2f51ade9 | 1525 | f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO); |
ffdeab71 CH |
1526 | if (!f2fs_lookup_read_extent_cache_block(dn->inode, index, |
1527 | &dn->data_blkaddr)) | |
1528 | err = f2fs_reserve_block(dn, index); | |
2f51ade9 | 1529 | f2fs_map_unlock(sbi, F2FS_GET_BLOCK_PRE_AIO); |
cf342d3b CH |
1530 | |
1531 | return err; | |
1532 | } | |
1533 | ||
817c968b CH |
1534 | static int f2fs_map_no_dnode(struct inode *inode, |
1535 | struct f2fs_map_blocks *map, struct dnode_of_data *dn, | |
1536 | pgoff_t pgoff) | |
1537 | { | |
1538 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
1539 | ||
1540 | /* | |
1541 | * There is one exceptional case that read_node_page() may return | |
1542 | * -ENOENT due to filesystem has been shutdown or cp_error, return | |
1543 | * -EIO in that case. | |
1544 | */ | |
1545 | if (map->m_may_create && | |
1546 | (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || f2fs_cp_error(sbi))) | |
1547 | return -EIO; | |
1548 | ||
1549 | if (map->m_next_pgofs) | |
1550 | *map->m_next_pgofs = f2fs_get_next_page_offset(dn, pgoff); | |
1551 | if (map->m_next_extent) | |
1552 | *map->m_next_extent = f2fs_get_next_page_offset(dn, pgoff); | |
1553 | return 0; | |
1554 | } | |
1555 | ||
0094e98b CH |
1556 | static bool f2fs_map_blocks_cached(struct inode *inode, |
1557 | struct f2fs_map_blocks *map, int flag) | |
1558 | { | |
1559 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
1560 | unsigned int maxblocks = map->m_len; | |
1561 | pgoff_t pgoff = (pgoff_t)map->m_lblk; | |
1562 | struct extent_info ei = {}; | |
1563 | ||
1564 | if (!f2fs_lookup_read_extent_cache(inode, pgoff, &ei)) | |
1565 | return false; | |
1566 | ||
1567 | map->m_pblk = ei.blk + pgoff - ei.fofs; | |
1568 | map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgoff); | |
1569 | map->m_flags = F2FS_MAP_MAPPED; | |
1570 | if (map->m_next_extent) | |
1571 | *map->m_next_extent = pgoff + map->m_len; | |
1572 | ||
1573 | /* for hardware encryption, but to avoid potential issue in future */ | |
1574 | if (flag == F2FS_GET_BLOCK_DIO) | |
1575 | f2fs_wait_on_block_writeback_range(inode, | |
1576 | map->m_pblk, map->m_len); | |
1577 | ||
1578 | if (f2fs_allow_multi_device_dio(sbi, flag)) { | |
1579 | int bidx = f2fs_target_device_index(sbi, map->m_pblk); | |
1580 | struct f2fs_dev_info *dev = &sbi->devs[bidx]; | |
1581 | ||
1582 | map->m_bdev = dev->bdev; | |
1583 | map->m_pblk -= dev->start_blk; | |
1584 | map->m_len = min(map->m_len, dev->end_blk + 1 - map->m_pblk); | |
59c9081b | 1585 | } else { |
0094e98b | 1586 | map->m_bdev = inode->i_sb->s_bdev; |
59c9081b | 1587 | } |
0094e98b | 1588 | return true; |
59c9081b YH |
1589 | } |
1590 | ||
0a8165d7 | 1591 | /* |
7a88ddb5 CY |
1592 | * f2fs_map_blocks() tries to find or build mapping relationship which |
1593 | * maps continuous logical blocks to physical blocks, and return such | |
1594 | * info via f2fs_map_blocks structure. | |
eb47b800 | 1595 | */ |
cd8fc522 | 1596 | int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag) |
eb47b800 | 1597 | { |
003a3e1d | 1598 | unsigned int maxblocks = map->m_len; |
eb47b800 | 1599 | struct dnode_of_data dn; |
f9811703 | 1600 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
f9d6d059 | 1601 | int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE; |
46008c6d | 1602 | pgoff_t pgofs, end_offset, end; |
bfad7c2d | 1603 | int err = 0, ofs = 1; |
46008c6d CY |
1604 | unsigned int ofs_in_node, last_ofs_in_node; |
1605 | blkcnt_t prealloc; | |
7df3a431 | 1606 | block_t blkaddr; |
c4020b2d | 1607 | unsigned int start_pgofs; |
71f2c820 | 1608 | int bidx = 0; |
fdbf69a7 | 1609 | bool is_hole; |
eb47b800 | 1610 | |
dfd02e4d CY |
1611 | if (!maxblocks) |
1612 | return 0; | |
1613 | ||
0094e98b CH |
1614 | if (!map->m_may_create && f2fs_map_blocks_cached(inode, map, flag)) |
1615 | goto out; | |
1616 | ||
71f2c820 CY |
1617 | map->m_bdev = inode->i_sb->s_bdev; |
1618 | map->m_multidev_dio = | |
1619 | f2fs_allow_multi_device_dio(F2FS_I_SB(inode), flag); | |
1620 | ||
003a3e1d JK |
1621 | map->m_len = 0; |
1622 | map->m_flags = 0; | |
1623 | ||
1624 | /* it only supports block size == page size */ | |
1625 | pgofs = (pgoff_t)map->m_lblk; | |
46008c6d | 1626 | end = pgofs + maxblocks; |
eb47b800 | 1627 | |
4fe71e88 | 1628 | next_dnode: |
f9d6d059 | 1629 | if (map->m_may_create) |
2f51ade9 | 1630 | f2fs_map_lock(sbi, flag); |
eb47b800 JK |
1631 | |
1632 | /* When reading holes, we need its node page */ | |
1633 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
4d57b86d | 1634 | err = f2fs_get_dnode_of_data(&dn, pgofs, mode); |
1ec79083 | 1635 | if (err) { |
43473f96 CY |
1636 | if (flag == F2FS_GET_BLOCK_BMAP) |
1637 | map->m_pblk = 0; | |
817c968b CH |
1638 | if (err == -ENOENT) |
1639 | err = f2fs_map_no_dnode(inode, map, &dn, pgofs); | |
bfad7c2d | 1640 | goto unlock_out; |
848753aa | 1641 | } |
973163fc | 1642 | |
c4020b2d | 1643 | start_pgofs = pgofs; |
46008c6d | 1644 | prealloc = 0; |
230436b3 | 1645 | last_ofs_in_node = ofs_in_node = dn.ofs_in_node; |
81ca7350 | 1646 | end_offset = ADDRS_PER_PAGE(dn.node_page, inode); |
4fe71e88 CY |
1647 | |
1648 | next_block: | |
a2ced1ce | 1649 | blkaddr = f2fs_data_blkaddr(&dn); |
fdbf69a7 CH |
1650 | is_hole = !__is_valid_data_blkaddr(blkaddr); |
1651 | if (!is_hole && | |
1652 | !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) { | |
10f966bb | 1653 | err = -EFSCORRUPTED; |
95fa90c9 | 1654 | f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR); |
c9b60788 CY |
1655 | goto sync_out; |
1656 | } | |
1657 | ||
fdbf69a7 CH |
1658 | /* use out-place-update for direct IO under LFS mode */ |
1659 | if (map->m_may_create && | |
1660 | (is_hole || (f2fs_lfs_mode(sbi) && flag == F2FS_GET_BLOCK_DIO))) { | |
1661 | if (unlikely(f2fs_cp_error(sbi))) { | |
1662 | err = -EIO; | |
1663 | goto sync_out; | |
1664 | } | |
1665 | ||
1666 | switch (flag) { | |
1667 | case F2FS_GET_BLOCK_PRE_AIO: | |
1668 | if (blkaddr == NULL_ADDR) { | |
1669 | prealloc++; | |
1670 | last_ofs_in_node = dn.ofs_in_node; | |
1671 | } | |
1672 | break; | |
1673 | case F2FS_GET_BLOCK_PRE_DIO: | |
1674 | case F2FS_GET_BLOCK_DIO: | |
f847c699 | 1675 | err = __allocate_data_block(&dn, map->m_seg_type); |
05e36006 CY |
1676 | if (err) |
1677 | goto sync_out; | |
fdbf69a7 CH |
1678 | if (flag == F2FS_GET_BLOCK_PRE_DIO) |
1679 | file_need_truncate(inode); | |
05e36006 | 1680 | set_inode_flag(inode, FI_APPEND_WRITE); |
fdbf69a7 CH |
1681 | break; |
1682 | default: | |
1683 | WARN_ON_ONCE(1); | |
1684 | err = -EIO; | |
1685 | goto sync_out; | |
f847c699 | 1686 | } |
fdbf69a7 CH |
1687 | |
1688 | blkaddr = dn.data_blkaddr; | |
1689 | if (is_hole) | |
3f2be043 | 1690 | map->m_flags |= F2FS_MAP_NEW; |
fdbf69a7 CH |
1691 | } else if (is_hole) { |
1692 | if (f2fs_compressed_file(inode) && | |
2aaea533 | 1693 | f2fs_sanity_check_cluster(&dn)) { |
fdbf69a7 CH |
1694 | err = -EFSCORRUPTED; |
1695 | f2fs_handle_error(sbi, | |
1696 | ERROR_CORRUPTED_CLUSTER); | |
1697 | goto sync_out; | |
1698 | } | |
1699 | ||
1700 | switch (flag) { | |
1701 | case F2FS_GET_BLOCK_PRECACHE: | |
1702 | goto sync_out; | |
1703 | case F2FS_GET_BLOCK_BMAP: | |
1704 | map->m_pblk = 0; | |
1705 | goto sync_out; | |
1706 | case F2FS_GET_BLOCK_FIEMAP: | |
1707 | if (blkaddr == NULL_ADDR) { | |
f3d98e74 CY |
1708 | if (map->m_next_pgofs) |
1709 | *map->m_next_pgofs = pgofs + 1; | |
4fe71e88 | 1710 | goto sync_out; |
f3d98e74 | 1711 | } |
fdbf69a7 CH |
1712 | break; |
1713 | default: | |
1714 | /* for defragment case */ | |
1715 | if (map->m_next_pgofs) | |
1716 | *map->m_next_pgofs = pgofs + 1; | |
1717 | goto sync_out; | |
e2b4e2bc | 1718 | } |
e2b4e2bc | 1719 | } |
eb47b800 | 1720 | |
46008c6d CY |
1721 | if (flag == F2FS_GET_BLOCK_PRE_AIO) |
1722 | goto skip; | |
1723 | ||
71f2c820 CY |
1724 | if (map->m_multidev_dio) |
1725 | bidx = f2fs_target_device_index(sbi, blkaddr); | |
1726 | ||
4fe71e88 | 1727 | if (map->m_len == 0) { |
da8c7fec | 1728 | /* reserved delalloc block should be mapped for fiemap. */ |
4fe71e88 | 1729 | if (blkaddr == NEW_ADDR) |
da8c7fec | 1730 | map->m_flags |= F2FS_MAP_DELALLOC; |
4fe71e88 CY |
1731 | map->m_flags |= F2FS_MAP_MAPPED; |
1732 | ||
1733 | map->m_pblk = blkaddr; | |
1734 | map->m_len = 1; | |
71f2c820 CY |
1735 | |
1736 | if (map->m_multidev_dio) | |
1737 | map->m_bdev = FDEV(bidx).bdev; | |
4fe71e88 CY |
1738 | } else if ((map->m_pblk != NEW_ADDR && |
1739 | blkaddr == (map->m_pblk + ofs)) || | |
b439b103 | 1740 | (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) || |
46008c6d | 1741 | flag == F2FS_GET_BLOCK_PRE_DIO) { |
71f2c820 CY |
1742 | if (map->m_multidev_dio && map->m_bdev != FDEV(bidx).bdev) |
1743 | goto sync_out; | |
4fe71e88 CY |
1744 | ofs++; |
1745 | map->m_len++; | |
1746 | } else { | |
1747 | goto sync_out; | |
1748 | } | |
bfad7c2d | 1749 | |
46008c6d | 1750 | skip: |
bfad7c2d JK |
1751 | dn.ofs_in_node++; |
1752 | pgofs++; | |
1753 | ||
46008c6d CY |
1754 | /* preallocate blocks in batch for one dnode page */ |
1755 | if (flag == F2FS_GET_BLOCK_PRE_AIO && | |
1756 | (pgofs == end || dn.ofs_in_node == end_offset)) { | |
7df3a431 | 1757 | |
46008c6d | 1758 | dn.ofs_in_node = ofs_in_node; |
4d57b86d | 1759 | err = f2fs_reserve_new_blocks(&dn, prealloc); |
46008c6d CY |
1760 | if (err) |
1761 | goto sync_out; | |
bfad7c2d | 1762 | |
46008c6d CY |
1763 | map->m_len += dn.ofs_in_node - ofs_in_node; |
1764 | if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) { | |
1765 | err = -ENOSPC; | |
1766 | goto sync_out; | |
3104af35 | 1767 | } |
46008c6d CY |
1768 | dn.ofs_in_node = end_offset; |
1769 | } | |
1770 | ||
1771 | if (pgofs >= end) | |
1772 | goto sync_out; | |
1773 | else if (dn.ofs_in_node < end_offset) | |
1774 | goto next_block; | |
1775 | ||
c4020b2d CY |
1776 | if (flag == F2FS_GET_BLOCK_PRECACHE) { |
1777 | if (map->m_flags & F2FS_MAP_MAPPED) { | |
1778 | unsigned int ofs = start_pgofs - map->m_lblk; | |
1779 | ||
e7547dac | 1780 | f2fs_update_read_extent_cache_range(&dn, |
c4020b2d CY |
1781 | start_pgofs, map->m_pblk + ofs, |
1782 | map->m_len - ofs); | |
1783 | } | |
1784 | } | |
1785 | ||
46008c6d CY |
1786 | f2fs_put_dnode(&dn); |
1787 | ||
f9d6d059 | 1788 | if (map->m_may_create) { |
2f51ade9 | 1789 | f2fs_map_unlock(sbi, flag); |
6f2d8ed6 | 1790 | f2fs_balance_fs(sbi, dn.node_changed); |
eb47b800 | 1791 | } |
46008c6d | 1792 | goto next_dnode; |
7df3a431 | 1793 | |
bfad7c2d | 1794 | sync_out: |
1e78e8bd | 1795 | |
71f2c820 CY |
1796 | if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) { |
1797 | /* | |
1798 | * for hardware encryption, but to avoid potential issue | |
1799 | * in future | |
1800 | */ | |
1e78e8bd ST |
1801 | f2fs_wait_on_block_writeback_range(inode, |
1802 | map->m_pblk, map->m_len); | |
1803 | ||
71f2c820 CY |
1804 | if (map->m_multidev_dio) { |
1805 | block_t blk_addr = map->m_pblk; | |
1806 | ||
1807 | bidx = f2fs_target_device_index(sbi, map->m_pblk); | |
1808 | ||
1809 | map->m_bdev = FDEV(bidx).bdev; | |
1810 | map->m_pblk -= FDEV(bidx).start_blk; | |
1811 | ||
1812 | if (map->m_may_create) | |
1813 | f2fs_update_device_state(sbi, inode->i_ino, | |
1814 | blk_addr, map->m_len); | |
1815 | ||
1816 | f2fs_bug_on(sbi, blk_addr + map->m_len > | |
1817 | FDEV(bidx).end_blk + 1); | |
1818 | } | |
1819 | } | |
1820 | ||
c4020b2d CY |
1821 | if (flag == F2FS_GET_BLOCK_PRECACHE) { |
1822 | if (map->m_flags & F2FS_MAP_MAPPED) { | |
1823 | unsigned int ofs = start_pgofs - map->m_lblk; | |
1824 | ||
e7547dac | 1825 | f2fs_update_read_extent_cache_range(&dn, |
c4020b2d CY |
1826 | start_pgofs, map->m_pblk + ofs, |
1827 | map->m_len - ofs); | |
1828 | } | |
1829 | if (map->m_next_extent) | |
1830 | *map->m_next_extent = pgofs + 1; | |
1831 | } | |
eb47b800 | 1832 | f2fs_put_dnode(&dn); |
bfad7c2d | 1833 | unlock_out: |
f9d6d059 | 1834 | if (map->m_may_create) { |
2f51ade9 | 1835 | f2fs_map_unlock(sbi, flag); |
6f2d8ed6 | 1836 | f2fs_balance_fs(sbi, dn.node_changed); |
2a340760 | 1837 | } |
bfad7c2d | 1838 | out: |
cd8fc522 | 1839 | trace_f2fs_map_blocks(inode, map, flag, err); |
bfad7c2d | 1840 | return err; |
eb47b800 JK |
1841 | } |
1842 | ||
b91050a8 HL |
1843 | bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len) |
1844 | { | |
1845 | struct f2fs_map_blocks map; | |
1846 | block_t last_lblk; | |
1847 | int err; | |
1848 | ||
1849 | if (pos + len > i_size_read(inode)) | |
1850 | return false; | |
1851 | ||
1852 | map.m_lblk = F2FS_BYTES_TO_BLK(pos); | |
1853 | map.m_next_pgofs = NULL; | |
1854 | map.m_next_extent = NULL; | |
1855 | map.m_seg_type = NO_CHECK_TYPE; | |
f4f0b677 | 1856 | map.m_may_create = false; |
b91050a8 HL |
1857 | last_lblk = F2FS_BLK_ALIGN(pos + len); |
1858 | ||
1859 | while (map.m_lblk < last_lblk) { | |
1860 | map.m_len = last_lblk - map.m_lblk; | |
cd8fc522 | 1861 | err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT); |
b91050a8 HL |
1862 | if (err || map.m_len == 0) |
1863 | return false; | |
1864 | map.m_lblk += map.m_len; | |
1865 | } | |
1866 | return true; | |
1867 | } | |
1868 | ||
43b9d4b4 JK |
1869 | static inline u64 bytes_to_blks(struct inode *inode, u64 bytes) |
1870 | { | |
1871 | return (bytes >> inode->i_blkbits); | |
1872 | } | |
1873 | ||
1874 | static inline u64 blks_to_bytes(struct inode *inode, u64 blks) | |
1875 | { | |
1876 | return (blks << inode->i_blkbits); | |
1877 | } | |
1878 | ||
442a9dbd CY |
1879 | static int f2fs_xattr_fiemap(struct inode *inode, |
1880 | struct fiemap_extent_info *fieinfo) | |
1881 | { | |
1882 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
1883 | struct page *page; | |
1884 | struct node_info ni; | |
1885 | __u64 phys = 0, len; | |
1886 | __u32 flags; | |
1887 | nid_t xnid = F2FS_I(inode)->i_xattr_nid; | |
1888 | int err = 0; | |
1889 | ||
1890 | if (f2fs_has_inline_xattr(inode)) { | |
1891 | int offset; | |
1892 | ||
1893 | page = f2fs_grab_cache_page(NODE_MAPPING(sbi), | |
1894 | inode->i_ino, false); | |
1895 | if (!page) | |
1896 | return -ENOMEM; | |
1897 | ||
a9419b63 | 1898 | err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false); |
7735730d CY |
1899 | if (err) { |
1900 | f2fs_put_page(page, 1); | |
1901 | return err; | |
1902 | } | |
442a9dbd | 1903 | |
6cbfcab5 | 1904 | phys = blks_to_bytes(inode, ni.blk_addr); |
442a9dbd CY |
1905 | offset = offsetof(struct f2fs_inode, i_addr) + |
1906 | sizeof(__le32) * (DEF_ADDRS_PER_INODE - | |
b323fd28 | 1907 | get_inline_xattr_addrs(inode)); |
442a9dbd CY |
1908 | |
1909 | phys += offset; | |
1910 | len = inline_xattr_size(inode); | |
1911 | ||
1912 | f2fs_put_page(page, 1); | |
1913 | ||
1914 | flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED; | |
1915 | ||
1916 | if (!xnid) | |
1917 | flags |= FIEMAP_EXTENT_LAST; | |
1918 | ||
1919 | err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); | |
dd5a09bd | 1920 | trace_f2fs_fiemap(inode, 0, phys, len, flags, err); |
ca7efd71 | 1921 | if (err) |
442a9dbd CY |
1922 | return err; |
1923 | } | |
1924 | ||
1925 | if (xnid) { | |
1926 | page = f2fs_grab_cache_page(NODE_MAPPING(sbi), xnid, false); | |
1927 | if (!page) | |
1928 | return -ENOMEM; | |
1929 | ||
a9419b63 | 1930 | err = f2fs_get_node_info(sbi, xnid, &ni, false); |
7735730d CY |
1931 | if (err) { |
1932 | f2fs_put_page(page, 1); | |
1933 | return err; | |
1934 | } | |
442a9dbd | 1935 | |
6cbfcab5 | 1936 | phys = blks_to_bytes(inode, ni.blk_addr); |
442a9dbd CY |
1937 | len = inode->i_sb->s_blocksize; |
1938 | ||
1939 | f2fs_put_page(page, 1); | |
1940 | ||
1941 | flags = FIEMAP_EXTENT_LAST; | |
1942 | } | |
1943 | ||
dd5a09bd | 1944 | if (phys) { |
442a9dbd | 1945 | err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); |
dd5a09bd CY |
1946 | trace_f2fs_fiemap(inode, 0, phys, len, flags, err); |
1947 | } | |
442a9dbd CY |
1948 | |
1949 | return (err < 0 ? err : 0); | |
1950 | } | |
1951 | ||
bf38fbad CY |
1952 | static loff_t max_inode_blocks(struct inode *inode) |
1953 | { | |
1954 | loff_t result = ADDRS_PER_INODE(inode); | |
1955 | loff_t leaf_count = ADDRS_PER_BLOCK(inode); | |
1956 | ||
1957 | /* two direct node blocks */ | |
1958 | result += (leaf_count * 2); | |
1959 | ||
1960 | /* two indirect node blocks */ | |
1961 | leaf_count *= NIDS_PER_BLOCK; | |
1962 | result += (leaf_count * 2); | |
1963 | ||
1964 | /* one double indirect node block */ | |
1965 | leaf_count *= NIDS_PER_BLOCK; | |
1966 | result += leaf_count; | |
1967 | ||
1968 | return result; | |
1969 | } | |
1970 | ||
9ab70134 JK |
1971 | int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
1972 | u64 start, u64 len) | |
1973 | { | |
b876f4c9 | 1974 | struct f2fs_map_blocks map; |
7f63eb77 | 1975 | sector_t start_blk, last_blk; |
da85985c | 1976 | pgoff_t next_pgofs; |
7f63eb77 JK |
1977 | u64 logical = 0, phys = 0, size = 0; |
1978 | u32 flags = 0; | |
7f63eb77 | 1979 | int ret = 0; |
093f0bac | 1980 | bool compr_cluster = false, compr_appended; |
bf38fbad | 1981 | unsigned int cluster_size = F2FS_I(inode)->i_cluster_size; |
093f0bac | 1982 | unsigned int count_in_cluster = 0; |
0bb2045c | 1983 | loff_t maxbytes; |
7f63eb77 | 1984 | |
c4020b2d CY |
1985 | if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { |
1986 | ret = f2fs_precache_extents(inode); | |
1987 | if (ret) | |
1988 | return ret; | |
1989 | } | |
1990 | ||
45dd052e | 1991 | ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR); |
7f63eb77 JK |
1992 | if (ret) |
1993 | return ret; | |
1994 | ||
f1b43d4c CY |
1995 | inode_lock(inode); |
1996 | ||
0bb2045c CX |
1997 | maxbytes = max_file_blocks(inode) << F2FS_BLKSIZE_BITS; |
1998 | if (start > maxbytes) { | |
1999 | ret = -EFBIG; | |
2000 | goto out; | |
2001 | } | |
2002 | ||
2003 | if (len > maxbytes || (maxbytes - len) < start) | |
2004 | len = maxbytes - start; | |
2005 | ||
442a9dbd CY |
2006 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
2007 | ret = f2fs_xattr_fiemap(inode, fieinfo); | |
2008 | goto out; | |
2009 | } | |
2010 | ||
7975f349 | 2011 | if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) { |
67f8cf3c JK |
2012 | ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len); |
2013 | if (ret != -EAGAIN) | |
f1b43d4c | 2014 | goto out; |
67f8cf3c JK |
2015 | } |
2016 | ||
6cbfcab5 JK |
2017 | if (bytes_to_blks(inode, len) == 0) |
2018 | len = blks_to_bytes(inode, 1); | |
7f63eb77 | 2019 | |
6cbfcab5 JK |
2020 | start_blk = bytes_to_blks(inode, start); |
2021 | last_blk = bytes_to_blks(inode, start + len - 1); | |
9a950d52 | 2022 | |
7f63eb77 | 2023 | next: |
b876f4c9 JK |
2024 | memset(&map, 0, sizeof(map)); |
2025 | map.m_lblk = start_blk; | |
2026 | map.m_len = bytes_to_blks(inode, len); | |
2027 | map.m_next_pgofs = &next_pgofs; | |
2028 | map.m_seg_type = NO_CHECK_TYPE; | |
7f63eb77 | 2029 | |
093f0bac DJ |
2030 | if (compr_cluster) { |
2031 | map.m_lblk += 1; | |
2032 | map.m_len = cluster_size - count_in_cluster; | |
2033 | } | |
bf38fbad | 2034 | |
cd8fc522 | 2035 | ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP); |
7f63eb77 JK |
2036 | if (ret) |
2037 | goto out; | |
2038 | ||
2039 | /* HOLE */ | |
093f0bac | 2040 | if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) { |
da85985c | 2041 | start_blk = next_pgofs; |
58736fa6 | 2042 | |
6cbfcab5 | 2043 | if (blks_to_bytes(inode, start_blk) < blks_to_bytes(inode, |
bf38fbad | 2044 | max_inode_blocks(inode))) |
9a950d52 | 2045 | goto prep_next; |
58736fa6 | 2046 | |
9a950d52 FL |
2047 | flags |= FIEMAP_EXTENT_LAST; |
2048 | } | |
7f63eb77 | 2049 | |
093f0bac DJ |
2050 | compr_appended = false; |
2051 | /* In a case of compressed cluster, append this to the last extent */ | |
da8c7fec | 2052 | if (compr_cluster && ((map.m_flags & F2FS_MAP_DELALLOC) || |
093f0bac DJ |
2053 | !(map.m_flags & F2FS_MAP_FLAGS))) { |
2054 | compr_appended = true; | |
2055 | goto skip_fill; | |
2056 | } | |
2057 | ||
da5af127 | 2058 | if (size) { |
0953fe86 | 2059 | flags |= FIEMAP_EXTENT_MERGED; |
62230e0d | 2060 | if (IS_ENCRYPTED(inode)) |
da5af127 CY |
2061 | flags |= FIEMAP_EXTENT_DATA_ENCRYPTED; |
2062 | ||
9a950d52 FL |
2063 | ret = fiemap_fill_next_extent(fieinfo, logical, |
2064 | phys, size, flags); | |
dd5a09bd | 2065 | trace_f2fs_fiemap(inode, logical, phys, size, flags, ret); |
bf38fbad CY |
2066 | if (ret) |
2067 | goto out; | |
2068 | size = 0; | |
da5af127 | 2069 | } |
7f63eb77 | 2070 | |
bf38fbad | 2071 | if (start_blk > last_blk) |
9a950d52 | 2072 | goto out; |
7f63eb77 | 2073 | |
093f0bac | 2074 | skip_fill: |
b876f4c9 | 2075 | if (map.m_pblk == COMPRESS_ADDR) { |
bf38fbad | 2076 | compr_cluster = true; |
093f0bac DJ |
2077 | count_in_cluster = 1; |
2078 | } else if (compr_appended) { | |
2079 | unsigned int appended_blks = cluster_size - | |
2080 | count_in_cluster + 1; | |
2081 | size += blks_to_bytes(inode, appended_blks); | |
2082 | start_blk += appended_blks; | |
2083 | compr_cluster = false; | |
2084 | } else { | |
2085 | logical = blks_to_bytes(inode, start_blk); | |
2086 | phys = __is_valid_data_blkaddr(map.m_pblk) ? | |
2087 | blks_to_bytes(inode, map.m_pblk) : 0; | |
2088 | size = blks_to_bytes(inode, map.m_len); | |
2089 | flags = 0; | |
2090 | ||
2091 | if (compr_cluster) { | |
2092 | flags = FIEMAP_EXTENT_ENCODED; | |
2093 | count_in_cluster += map.m_len; | |
2094 | if (count_in_cluster == cluster_size) { | |
2095 | compr_cluster = false; | |
2096 | size += blks_to_bytes(inode, 1); | |
2097 | } | |
da8c7fec | 2098 | } else if (map.m_flags & F2FS_MAP_DELALLOC) { |
093f0bac DJ |
2099 | flags = FIEMAP_EXTENT_UNWRITTEN; |
2100 | } | |
7f63eb77 | 2101 | |
093f0bac DJ |
2102 | start_blk += bytes_to_blks(inode, size); |
2103 | } | |
7f63eb77 | 2104 | |
9a950d52 | 2105 | prep_next: |
7f63eb77 JK |
2106 | cond_resched(); |
2107 | if (fatal_signal_pending(current)) | |
2108 | ret = -EINTR; | |
2109 | else | |
2110 | goto next; | |
2111 | out: | |
2112 | if (ret == 1) | |
2113 | ret = 0; | |
2114 | ||
5955102c | 2115 | inode_unlock(inode); |
7f63eb77 | 2116 | return ret; |
9ab70134 JK |
2117 | } |
2118 | ||
95ae251f EB |
2119 | static inline loff_t f2fs_readpage_limit(struct inode *inode) |
2120 | { | |
feb0576a | 2121 | if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode)) |
95ae251f EB |
2122 | return inode->i_sb->s_maxbytes; |
2123 | ||
2124 | return i_size_read(inode); | |
2125 | } | |
2126 | ||
2df0ab04 CY |
2127 | static int f2fs_read_single_page(struct inode *inode, struct page *page, |
2128 | unsigned nr_pages, | |
2129 | struct f2fs_map_blocks *map, | |
2130 | struct bio **bio_ret, | |
2131 | sector_t *last_block_in_bio, | |
2132 | bool is_readahead) | |
2133 | { | |
2134 | struct bio *bio = *bio_ret; | |
43b9d4b4 | 2135 | const unsigned blocksize = blks_to_bytes(inode, 1); |
2df0ab04 CY |
2136 | sector_t block_in_file; |
2137 | sector_t last_block; | |
2138 | sector_t last_block_in_file; | |
2139 | sector_t block_nr; | |
2140 | int ret = 0; | |
2141 | ||
4969c06a | 2142 | block_in_file = (sector_t)page_index(page); |
2df0ab04 | 2143 | last_block = block_in_file + nr_pages; |
43b9d4b4 JK |
2144 | last_block_in_file = bytes_to_blks(inode, |
2145 | f2fs_readpage_limit(inode) + blocksize - 1); | |
2df0ab04 CY |
2146 | if (last_block > last_block_in_file) |
2147 | last_block = last_block_in_file; | |
2148 | ||
2149 | /* just zeroing out page which is beyond EOF */ | |
2150 | if (block_in_file >= last_block) | |
2151 | goto zero_out; | |
2152 | /* | |
2153 | * Map blocks using the previous result first. | |
2154 | */ | |
2155 | if ((map->m_flags & F2FS_MAP_MAPPED) && | |
2156 | block_in_file > map->m_lblk && | |
2157 | block_in_file < (map->m_lblk + map->m_len)) | |
2158 | goto got_it; | |
2159 | ||
2160 | /* | |
2161 | * Then do more f2fs_map_blocks() calls until we are | |
2162 | * done with this page. | |
2163 | */ | |
2164 | map->m_lblk = block_in_file; | |
2165 | map->m_len = last_block - block_in_file; | |
2166 | ||
cd8fc522 | 2167 | ret = f2fs_map_blocks(inode, map, F2FS_GET_BLOCK_DEFAULT); |
2df0ab04 CY |
2168 | if (ret) |
2169 | goto out; | |
2170 | got_it: | |
2171 | if ((map->m_flags & F2FS_MAP_MAPPED)) { | |
2172 | block_nr = map->m_pblk + block_in_file - map->m_lblk; | |
2173 | SetPageMappedToDisk(page); | |
2174 | ||
2df0ab04 | 2175 | if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr, |
93770ab7 | 2176 | DATA_GENERIC_ENHANCE_READ)) { |
10f966bb | 2177 | ret = -EFSCORRUPTED; |
95fa90c9 CY |
2178 | f2fs_handle_error(F2FS_I_SB(inode), |
2179 | ERROR_INVALID_BLKADDR); | |
2df0ab04 CY |
2180 | goto out; |
2181 | } | |
2182 | } else { | |
2183 | zero_out: | |
2184 | zero_user_segment(page, 0, PAGE_SIZE); | |
95ae251f EB |
2185 | if (f2fs_need_verity(inode, page->index) && |
2186 | !fsverity_verify_page(page)) { | |
2187 | ret = -EIO; | |
2188 | goto out; | |
2189 | } | |
2df0ab04 CY |
2190 | if (!PageUptodate(page)) |
2191 | SetPageUptodate(page); | |
2192 | unlock_page(page); | |
2193 | goto out; | |
2194 | } | |
2195 | ||
2196 | /* | |
2197 | * This page will go to BIO. Do we need to send this | |
2198 | * BIO off first? | |
2199 | */ | |
27aacd28 ST |
2200 | if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio, |
2201 | *last_block_in_bio, block_nr) || | |
2202 | !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) { | |
2df0ab04 | 2203 | submit_and_realloc: |
bc29835a | 2204 | f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); |
2df0ab04 CY |
2205 | bio = NULL; |
2206 | } | |
2207 | if (bio == NULL) { | |
2208 | bio = f2fs_grab_read_bio(inode, block_nr, nr_pages, | |
0683728a | 2209 | is_readahead ? REQ_RAHEAD : 0, page->index, |
7f59b277 | 2210 | false); |
2df0ab04 CY |
2211 | if (IS_ERR(bio)) { |
2212 | ret = PTR_ERR(bio); | |
2213 | bio = NULL; | |
2214 | goto out; | |
2215 | } | |
2216 | } | |
2217 | ||
2218 | /* | |
2219 | * If the page is under writeback, we need to wait for | |
2220 | * its completion to see the correct decrypted data. | |
2221 | */ | |
2222 | f2fs_wait_on_block_writeback(inode, block_nr); | |
2223 | ||
2224 | if (bio_add_page(bio, page, blocksize, 0) < blocksize) | |
2225 | goto submit_and_realloc; | |
2226 | ||
2227 | inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA); | |
34a23525 CY |
2228 | f2fs_update_iostat(F2FS_I_SB(inode), NULL, FS_DATA_READ_IO, |
2229 | F2FS_BLKSIZE); | |
2df0ab04 | 2230 | *last_block_in_bio = block_nr; |
2df0ab04 CY |
2231 | out: |
2232 | *bio_ret = bio; | |
2233 | return ret; | |
2234 | } | |
2235 | ||
4c8ff709 CY |
2236 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
2237 | int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret, | |
2238 | unsigned nr_pages, sector_t *last_block_in_bio, | |
0683728a | 2239 | bool is_readahead, bool for_write) |
4c8ff709 CY |
2240 | { |
2241 | struct dnode_of_data dn; | |
2242 | struct inode *inode = cc->inode; | |
2243 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2244 | struct bio *bio = *bio_ret; | |
2245 | unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size; | |
2246 | sector_t last_block_in_file; | |
43b9d4b4 | 2247 | const unsigned blocksize = blks_to_bytes(inode, 1); |
4c8ff709 | 2248 | struct decompress_io_ctx *dic = NULL; |
fe59109a | 2249 | struct extent_info ei = {}; |
94afd6d6 | 2250 | bool from_dnode = true; |
4c8ff709 CY |
2251 | int i; |
2252 | int ret = 0; | |
2253 | ||
2254 | f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc)); | |
2255 | ||
43b9d4b4 JK |
2256 | last_block_in_file = bytes_to_blks(inode, |
2257 | f2fs_readpage_limit(inode) + blocksize - 1); | |
4c8ff709 CY |
2258 | |
2259 | /* get rid of pages beyond EOF */ | |
2260 | for (i = 0; i < cc->cluster_size; i++) { | |
2261 | struct page *page = cc->rpages[i]; | |
2262 | ||
2263 | if (!page) | |
2264 | continue; | |
2265 | if ((sector_t)page->index >= last_block_in_file) { | |
2266 | zero_user_segment(page, 0, PAGE_SIZE); | |
2267 | if (!PageUptodate(page)) | |
2268 | SetPageUptodate(page); | |
2269 | } else if (!PageUptodate(page)) { | |
2270 | continue; | |
2271 | } | |
2272 | unlock_page(page); | |
9605f75c JK |
2273 | if (for_write) |
2274 | put_page(page); | |
4c8ff709 CY |
2275 | cc->rpages[i] = NULL; |
2276 | cc->nr_rpages--; | |
2277 | } | |
2278 | ||
2279 | /* we are done since all pages are beyond EOF */ | |
2280 | if (f2fs_cluster_is_empty(cc)) | |
2281 | goto out; | |
2282 | ||
e7547dac | 2283 | if (f2fs_lookup_read_extent_cache(inode, start_idx, &ei)) |
94afd6d6 CY |
2284 | from_dnode = false; |
2285 | ||
2286 | if (!from_dnode) | |
2287 | goto skip_reading_dnode; | |
2288 | ||
4c8ff709 CY |
2289 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
2290 | ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); | |
2291 | if (ret) | |
2292 | goto out; | |
2293 | ||
bd90c5cd JK |
2294 | if (unlikely(f2fs_cp_error(sbi))) { |
2295 | ret = -EIO; | |
2296 | goto out_put_dnode; | |
2297 | } | |
a86d27dd | 2298 | f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR); |
4c8ff709 | 2299 | |
94afd6d6 | 2300 | skip_reading_dnode: |
4c8ff709 CY |
2301 | for (i = 1; i < cc->cluster_size; i++) { |
2302 | block_t blkaddr; | |
2303 | ||
94afd6d6 CY |
2304 | blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page, |
2305 | dn.ofs_in_node + i) : | |
2306 | ei.blk + i - 1; | |
4c8ff709 CY |
2307 | |
2308 | if (!__is_valid_data_blkaddr(blkaddr)) | |
2309 | break; | |
2310 | ||
2311 | if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) { | |
2312 | ret = -EFAULT; | |
2313 | goto out_put_dnode; | |
2314 | } | |
2315 | cc->nr_cpages++; | |
94afd6d6 CY |
2316 | |
2317 | if (!from_dnode && i >= ei.c_len) | |
2318 | break; | |
4c8ff709 CY |
2319 | } |
2320 | ||
2321 | /* nothing to decompress */ | |
2322 | if (cc->nr_cpages == 0) { | |
2323 | ret = 0; | |
2324 | goto out_put_dnode; | |
2325 | } | |
2326 | ||
2327 | dic = f2fs_alloc_dic(cc); | |
2328 | if (IS_ERR(dic)) { | |
2329 | ret = PTR_ERR(dic); | |
2330 | goto out_put_dnode; | |
2331 | } | |
2332 | ||
6ce19aff | 2333 | for (i = 0; i < cc->nr_cpages; i++) { |
4c8ff709 CY |
2334 | struct page *page = dic->cpages[i]; |
2335 | block_t blkaddr; | |
7f59b277 | 2336 | struct bio_post_read_ctx *ctx; |
4c8ff709 | 2337 | |
94afd6d6 CY |
2338 | blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_page, |
2339 | dn.ofs_in_node + i + 1) : | |
2340 | ei.blk + i; | |
4c8ff709 | 2341 | |
6ce19aff CY |
2342 | f2fs_wait_on_block_writeback(inode, blkaddr); |
2343 | ||
2344 | if (f2fs_load_compressed_page(sbi, page, blkaddr)) { | |
b0327c84 | 2345 | if (atomic_dec_and_test(&dic->remaining_pages)) { |
bff139b4 | 2346 | f2fs_decompress_cluster(dic, true); |
b0327c84 CY |
2347 | break; |
2348 | } | |
6ce19aff CY |
2349 | continue; |
2350 | } | |
2351 | ||
27aacd28 ST |
2352 | if (bio && (!page_is_mergeable(sbi, bio, |
2353 | *last_block_in_bio, blkaddr) || | |
2354 | !f2fs_crypt_mergeable_bio(bio, inode, page->index, NULL))) { | |
4c8ff709 | 2355 | submit_and_realloc: |
bc29835a | 2356 | f2fs_submit_read_bio(sbi, bio, DATA); |
4c8ff709 CY |
2357 | bio = NULL; |
2358 | } | |
2359 | ||
2360 | if (!bio) { | |
2361 | bio = f2fs_grab_read_bio(inode, blkaddr, nr_pages, | |
2362 | is_readahead ? REQ_RAHEAD : 0, | |
7f59b277 | 2363 | page->index, for_write); |
4c8ff709 CY |
2364 | if (IS_ERR(bio)) { |
2365 | ret = PTR_ERR(bio); | |
bff139b4 | 2366 | f2fs_decompress_end_io(dic, ret, true); |
4c8ff709 | 2367 | f2fs_put_dnode(&dn); |
f3494345 | 2368 | *bio_ret = NULL; |
4c8ff709 CY |
2369 | return ret; |
2370 | } | |
2371 | } | |
2372 | ||
4c8ff709 CY |
2373 | if (bio_add_page(bio, page, blocksize, 0) < blocksize) |
2374 | goto submit_and_realloc; | |
2375 | ||
a4b68176 | 2376 | ctx = get_post_read_ctx(bio); |
7f59b277 EB |
2377 | ctx->enabled_steps |= STEP_DECOMPRESS; |
2378 | refcount_inc(&dic->refcnt); | |
03382f1a | 2379 | |
4c8ff709 | 2380 | inc_page_count(sbi, F2FS_RD_DATA); |
34a23525 | 2381 | f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE); |
4c8ff709 CY |
2382 | *last_block_in_bio = blkaddr; |
2383 | } | |
2384 | ||
94afd6d6 CY |
2385 | if (from_dnode) |
2386 | f2fs_put_dnode(&dn); | |
4c8ff709 CY |
2387 | |
2388 | *bio_ret = bio; | |
2389 | return 0; | |
2390 | ||
2391 | out_put_dnode: | |
94afd6d6 CY |
2392 | if (from_dnode) |
2393 | f2fs_put_dnode(&dn); | |
4c8ff709 | 2394 | out: |
7f59b277 EB |
2395 | for (i = 0; i < cc->cluster_size; i++) { |
2396 | if (cc->rpages[i]) { | |
2397 | ClearPageUptodate(cc->rpages[i]); | |
7f59b277 EB |
2398 | unlock_page(cc->rpages[i]); |
2399 | } | |
2400 | } | |
4c8ff709 CY |
2401 | *bio_ret = bio; |
2402 | return ret; | |
2403 | } | |
2404 | #endif | |
2405 | ||
f1e88660 JK |
2406 | /* |
2407 | * This function was originally taken from fs/mpage.c, and customized for f2fs. | |
2408 | * Major change was from block_size == page_size in f2fs by default. | |
2409 | */ | |
e20a7693 | 2410 | static int f2fs_mpage_readpages(struct inode *inode, |
23323196 | 2411 | struct readahead_control *rac, struct page *page) |
f1e88660 JK |
2412 | { |
2413 | struct bio *bio = NULL; | |
f1e88660 | 2414 | sector_t last_block_in_bio = 0; |
f1e88660 | 2415 | struct f2fs_map_blocks map; |
4c8ff709 CY |
2416 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
2417 | struct compress_ctx cc = { | |
2418 | .inode = inode, | |
2419 | .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, | |
2420 | .cluster_size = F2FS_I(inode)->i_cluster_size, | |
2421 | .cluster_idx = NULL_CLUSTER, | |
2422 | .rpages = NULL, | |
2423 | .cpages = NULL, | |
2424 | .nr_rpages = 0, | |
2425 | .nr_cpages = 0, | |
2426 | }; | |
a2649315 | 2427 | pgoff_t nc_cluster_idx = NULL_CLUSTER; |
4c8ff709 | 2428 | #endif |
23323196 | 2429 | unsigned nr_pages = rac ? readahead_count(rac) : 1; |
4c8ff709 | 2430 | unsigned max_nr_pages = nr_pages; |
2df0ab04 | 2431 | int ret = 0; |
f1e88660 JK |
2432 | |
2433 | map.m_pblk = 0; | |
2434 | map.m_lblk = 0; | |
2435 | map.m_len = 0; | |
2436 | map.m_flags = 0; | |
da85985c | 2437 | map.m_next_pgofs = NULL; |
c4020b2d | 2438 | map.m_next_extent = NULL; |
d5097be5 | 2439 | map.m_seg_type = NO_CHECK_TYPE; |
f9d6d059 | 2440 | map.m_may_create = false; |
f1e88660 | 2441 | |
736c0a74 | 2442 | for (; nr_pages; nr_pages--) { |
23323196 MWO |
2443 | if (rac) { |
2444 | page = readahead_page(rac); | |
a83d50bc | 2445 | prefetchw(&page->flags); |
f1e88660 JK |
2446 | } |
2447 | ||
4c8ff709 CY |
2448 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
2449 | if (f2fs_compressed_file(inode)) { | |
146949de | 2450 | /* there are remained compressed pages, submit them */ |
4c8ff709 CY |
2451 | if (!f2fs_cluster_can_merge_page(&cc, page->index)) { |
2452 | ret = f2fs_read_multi_pages(&cc, &bio, | |
2453 | max_nr_pages, | |
2454 | &last_block_in_bio, | |
23323196 | 2455 | rac != NULL, false); |
8bfbfb0d | 2456 | f2fs_destroy_compress_ctx(&cc, false); |
4c8ff709 CY |
2457 | if (ret) |
2458 | goto set_error_page; | |
2459 | } | |
a2649315 FC |
2460 | if (cc.cluster_idx == NULL_CLUSTER) { |
2461 | if (nc_cluster_idx == | |
2462 | page->index >> cc.log_cluster_size) { | |
2463 | goto read_single_page; | |
2464 | } | |
4c8ff709 | 2465 | |
a2649315 FC |
2466 | ret = f2fs_is_compressed_cluster(inode, page->index); |
2467 | if (ret < 0) | |
2468 | goto set_error_page; | |
2469 | else if (!ret) { | |
2470 | nc_cluster_idx = | |
2471 | page->index >> cc.log_cluster_size; | |
2472 | goto read_single_page; | |
2473 | } | |
4c8ff709 | 2474 | |
a2649315 FC |
2475 | nc_cluster_idx = NULL_CLUSTER; |
2476 | } | |
4c8ff709 CY |
2477 | ret = f2fs_init_compress_ctx(&cc); |
2478 | if (ret) | |
2479 | goto set_error_page; | |
2480 | ||
2481 | f2fs_compress_ctx_add_page(&cc, page); | |
2482 | ||
2483 | goto next_page; | |
2484 | } | |
2485 | read_single_page: | |
2486 | #endif | |
2487 | ||
2488 | ret = f2fs_read_single_page(inode, page, max_nr_pages, &map, | |
23323196 | 2489 | &bio, &last_block_in_bio, rac); |
2df0ab04 | 2490 | if (ret) { |
4c8ff709 CY |
2491 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
2492 | set_error_page: | |
2493 | #endif | |
09cbfeaf | 2494 | zero_user_segment(page, 0, PAGE_SIZE); |
f1e88660 | 2495 | unlock_page(page); |
f1e88660 | 2496 | } |
23323196 | 2497 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
f1e88660 | 2498 | next_page: |
23323196 MWO |
2499 | #endif |
2500 | if (rac) | |
09cbfeaf | 2501 | put_page(page); |
4c8ff709 CY |
2502 | |
2503 | #ifdef CONFIG_F2FS_FS_COMPRESSION | |
2504 | if (f2fs_compressed_file(inode)) { | |
2505 | /* last page */ | |
2506 | if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) { | |
2507 | ret = f2fs_read_multi_pages(&cc, &bio, | |
2508 | max_nr_pages, | |
2509 | &last_block_in_bio, | |
23323196 | 2510 | rac != NULL, false); |
8bfbfb0d | 2511 | f2fs_destroy_compress_ctx(&cc, false); |
4c8ff709 CY |
2512 | } |
2513 | } | |
2514 | #endif | |
f1e88660 | 2515 | } |
f1e88660 | 2516 | if (bio) |
bc29835a | 2517 | f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); |
23323196 | 2518 | return ret; |
f1e88660 JK |
2519 | } |
2520 | ||
be05584f | 2521 | static int f2fs_read_data_folio(struct file *file, struct folio *folio) |
eb47b800 | 2522 | { |
be05584f | 2523 | struct page *page = &folio->page; |
4969c06a | 2524 | struct inode *inode = page_file_mapping(page)->host; |
b3d208f9 | 2525 | int ret = -EAGAIN; |
9ffe0fb5 | 2526 | |
c20e89cd CY |
2527 | trace_f2fs_readpage(page, DATA); |
2528 | ||
4c8ff709 CY |
2529 | if (!f2fs_is_compress_backend_ready(inode)) { |
2530 | unlock_page(page); | |
2531 | return -EOPNOTSUPP; | |
2532 | } | |
2533 | ||
e1c42045 | 2534 | /* If the file has inline data, try to read it directly */ |
9ffe0fb5 HL |
2535 | if (f2fs_has_inline_data(inode)) |
2536 | ret = f2fs_read_inline_data(inode, page); | |
b3d208f9 | 2537 | if (ret == -EAGAIN) |
e20a7693 | 2538 | ret = f2fs_mpage_readpages(inode, NULL, page); |
9ffe0fb5 | 2539 | return ret; |
eb47b800 JK |
2540 | } |
2541 | ||
23323196 | 2542 | static void f2fs_readahead(struct readahead_control *rac) |
eb47b800 | 2543 | { |
23323196 | 2544 | struct inode *inode = rac->mapping->host; |
b8c29400 | 2545 | |
23323196 | 2546 | trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac)); |
9ffe0fb5 | 2547 | |
4c8ff709 | 2548 | if (!f2fs_is_compress_backend_ready(inode)) |
23323196 | 2549 | return; |
4c8ff709 | 2550 | |
704528d8 | 2551 | /* If the file has inline data, skip readahead */ |
9ffe0fb5 | 2552 | if (f2fs_has_inline_data(inode)) |
23323196 | 2553 | return; |
9ffe0fb5 | 2554 | |
e20a7693 | 2555 | f2fs_mpage_readpages(inode, rac, NULL); |
eb47b800 JK |
2556 | } |
2557 | ||
4c8ff709 | 2558 | int f2fs_encrypt_one_page(struct f2fs_io_info *fio) |
7eab0c0d HP |
2559 | { |
2560 | struct inode *inode = fio->page->mapping->host; | |
4c8ff709 | 2561 | struct page *mpage, *page; |
7eab0c0d HP |
2562 | gfp_t gfp_flags = GFP_NOFS; |
2563 | ||
1958593e | 2564 | if (!f2fs_encrypted_file(inode)) |
7eab0c0d HP |
2565 | return 0; |
2566 | ||
4c8ff709 CY |
2567 | page = fio->compressed_page ? fio->compressed_page : fio->page; |
2568 | ||
6dbb1796 | 2569 | /* wait for GCed page writeback via META_MAPPING */ |
0ded69f6 | 2570 | f2fs_wait_on_block_writeback(inode, fio->old_blkaddr); |
7eab0c0d | 2571 | |
27aacd28 ST |
2572 | if (fscrypt_inode_uses_inline_crypto(inode)) |
2573 | return 0; | |
2574 | ||
7eab0c0d | 2575 | retry_encrypt: |
4c8ff709 CY |
2576 | fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page, |
2577 | PAGE_SIZE, 0, gfp_flags); | |
6aa58d8a CY |
2578 | if (IS_ERR(fio->encrypted_page)) { |
2579 | /* flush pending IOs and wait for a while in the ENOMEM case */ | |
2580 | if (PTR_ERR(fio->encrypted_page) == -ENOMEM) { | |
2581 | f2fs_flush_merged_writes(fio->sbi); | |
4034247a | 2582 | memalloc_retry_wait(GFP_NOFS); |
6aa58d8a CY |
2583 | gfp_flags |= __GFP_NOFAIL; |
2584 | goto retry_encrypt; | |
2585 | } | |
2586 | return PTR_ERR(fio->encrypted_page); | |
2587 | } | |
7eab0c0d | 2588 | |
6aa58d8a CY |
2589 | mpage = find_lock_page(META_MAPPING(fio->sbi), fio->old_blkaddr); |
2590 | if (mpage) { | |
2591 | if (PageUptodate(mpage)) | |
2592 | memcpy(page_address(mpage), | |
2593 | page_address(fio->encrypted_page), PAGE_SIZE); | |
2594 | f2fs_put_page(mpage, 1); | |
7eab0c0d | 2595 | } |
6aa58d8a | 2596 | return 0; |
7eab0c0d HP |
2597 | } |
2598 | ||
bb9e3bb8 CY |
2599 | static inline bool check_inplace_update_policy(struct inode *inode, |
2600 | struct f2fs_io_info *fio) | |
7eab0c0d | 2601 | { |
bb9e3bb8 | 2602 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
7eab0c0d | 2603 | |
fdb7ccc3 YL |
2604 | if (IS_F2FS_IPU_HONOR_OPU_WRITE(sbi) && |
2605 | is_inode_flag_set(inode, FI_OPU_WRITE)) | |
1018a546 | 2606 | return false; |
fdb7ccc3 | 2607 | if (IS_F2FS_IPU_FORCE(sbi)) |
bb9e3bb8 | 2608 | return true; |
fdb7ccc3 | 2609 | if (IS_F2FS_IPU_SSR(sbi) && f2fs_need_SSR(sbi)) |
bb9e3bb8 | 2610 | return true; |
fdb7ccc3 | 2611 | if (IS_F2FS_IPU_UTIL(sbi) && utilization(sbi) > SM_I(sbi)->min_ipu_util) |
bb9e3bb8 | 2612 | return true; |
fdb7ccc3 YL |
2613 | if (IS_F2FS_IPU_SSR_UTIL(sbi) && f2fs_need_SSR(sbi) && |
2614 | utilization(sbi) > SM_I(sbi)->min_ipu_util) | |
bb9e3bb8 CY |
2615 | return true; |
2616 | ||
2617 | /* | |
2618 | * IPU for rewrite async pages | |
2619 | */ | |
fdb7ccc3 YL |
2620 | if (IS_F2FS_IPU_ASYNC(sbi) && fio && fio->op == REQ_OP_WRITE && |
2621 | !(fio->op_flags & REQ_SYNC) && !IS_ENCRYPTED(inode)) | |
bb9e3bb8 CY |
2622 | return true; |
2623 | ||
2624 | /* this is only set during fdatasync */ | |
fdb7ccc3 | 2625 | if (IS_F2FS_IPU_FSYNC(sbi) && is_inode_flag_set(inode, FI_NEED_IPU)) |
bb9e3bb8 CY |
2626 | return true; |
2627 | ||
4354994f DR |
2628 | if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) && |
2629 | !f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) | |
2630 | return true; | |
2631 | ||
bb9e3bb8 CY |
2632 | return false; |
2633 | } | |
2634 | ||
4d57b86d | 2635 | bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio) |
bb9e3bb8 | 2636 | { |
859fca6b CY |
2637 | /* swap file is migrating in aligned write mode */ |
2638 | if (is_inode_flag_set(inode, FI_ALIGNED_WRITE)) | |
2639 | return false; | |
2640 | ||
1ad71a27 JK |
2641 | if (f2fs_is_pinned_file(inode)) |
2642 | return true; | |
bb9e3bb8 CY |
2643 | |
2644 | /* if this is cold file, we should overwrite to avoid fragmentation */ | |
f3b23c78 | 2645 | if (file_is_cold(inode) && !is_inode_flag_set(inode, FI_OPU_WRITE)) |
bb9e3bb8 CY |
2646 | return true; |
2647 | ||
2648 | return check_inplace_update_policy(inode, fio); | |
2649 | } | |
2650 | ||
4d57b86d | 2651 | bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio) |
bb9e3bb8 CY |
2652 | { |
2653 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
2654 | ||
19bdba52 JK |
2655 | /* The below cases were checked when setting it. */ |
2656 | if (f2fs_is_pinned_file(inode)) | |
2657 | return false; | |
2658 | if (fio && is_sbi_flag_set(sbi, SBI_NEED_FSCK)) | |
2659 | return true; | |
b0332a0f | 2660 | if (f2fs_lfs_mode(sbi)) |
bb9e3bb8 CY |
2661 | return true; |
2662 | if (S_ISDIR(inode->i_mode)) | |
2663 | return true; | |
af033b2a CY |
2664 | if (IS_NOQUOTA(inode)) |
2665 | return true; | |
bb9e3bb8 CY |
2666 | if (f2fs_is_atomic_file(inode)) |
2667 | return true; | |
943f7c6f CY |
2668 | /* rewrite low ratio compress data w/ OPU mode to avoid fragmentation */ |
2669 | if (f2fs_compressed_file(inode) && | |
2670 | F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER && | |
2671 | is_inode_flag_set(inode, FI_ENABLE_COMPRESS)) | |
2672 | return true; | |
859fca6b CY |
2673 | |
2674 | /* swap file is migrating in aligned write mode */ | |
2675 | if (is_inode_flag_set(inode, FI_ALIGNED_WRITE)) | |
2676 | return true; | |
2677 | ||
1018a546 CY |
2678 | if (is_inode_flag_set(inode, FI_OPU_WRITE)) |
2679 | return true; | |
2680 | ||
bb9e3bb8 | 2681 | if (fio) { |
b763f3be | 2682 | if (page_private_gcing(fio->page)) |
bb9e3bb8 | 2683 | return true; |
b763f3be | 2684 | if (page_private_dummy(fio->page)) |
bb9e3bb8 | 2685 | return true; |
4354994f DR |
2686 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) && |
2687 | f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) | |
2688 | return true; | |
bb9e3bb8 CY |
2689 | } |
2690 | return false; | |
2691 | } | |
2692 | ||
7eab0c0d HP |
2693 | static inline bool need_inplace_update(struct f2fs_io_info *fio) |
2694 | { | |
2695 | struct inode *inode = fio->page->mapping->host; | |
2696 | ||
4d57b86d | 2697 | if (f2fs_should_update_outplace(inode, fio)) |
7eab0c0d HP |
2698 | return false; |
2699 | ||
4d57b86d | 2700 | return f2fs_should_update_inplace(inode, fio); |
7eab0c0d HP |
2701 | } |
2702 | ||
4d57b86d | 2703 | int f2fs_do_write_data_page(struct f2fs_io_info *fio) |
eb47b800 | 2704 | { |
05ca3632 | 2705 | struct page *page = fio->page; |
eb47b800 | 2706 | struct inode *inode = page->mapping->host; |
eb47b800 | 2707 | struct dnode_of_data dn; |
7735730d | 2708 | struct node_info ni; |
e959c8f5 | 2709 | bool ipu_force = false; |
eb47b800 JK |
2710 | int err = 0; |
2711 | ||
3db1de0e DJ |
2712 | /* Use COW inode to make dnode_of_data for atomic write */ |
2713 | if (f2fs_is_atomic_file(inode)) | |
2714 | set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0); | |
2715 | else | |
2716 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
2717 | ||
e959c8f5 | 2718 | if (need_inplace_update(fio) && |
04a91ab0 CH |
2719 | f2fs_lookup_read_extent_cache_block(inode, page->index, |
2720 | &fio->old_blkaddr)) { | |
c9b60788 | 2721 | if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, |
95fa90c9 CY |
2722 | DATA_GENERIC_ENHANCE)) { |
2723 | f2fs_handle_error(fio->sbi, | |
2724 | ERROR_INVALID_BLKADDR); | |
10f966bb | 2725 | return -EFSCORRUPTED; |
95fa90c9 | 2726 | } |
c9b60788 CY |
2727 | |
2728 | ipu_force = true; | |
2729 | fio->need_lock = LOCK_DONE; | |
2730 | goto got_it; | |
e959c8f5 | 2731 | } |
279d6df2 | 2732 | |
d29460e5 JK |
2733 | /* Deadlock due to between page->lock and f2fs_lock_op */ |
2734 | if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi)) | |
2735 | return -EAGAIN; | |
279d6df2 | 2736 | |
4d57b86d | 2737 | err = f2fs_get_dnode_of_data(&dn, page->index, LOOKUP_NODE); |
eb47b800 | 2738 | if (err) |
279d6df2 | 2739 | goto out; |
eb47b800 | 2740 | |
28bc106b | 2741 | fio->old_blkaddr = dn.data_blkaddr; |
eb47b800 JK |
2742 | |
2743 | /* This page is already truncated */ | |
7a9d7548 | 2744 | if (fio->old_blkaddr == NULL_ADDR) { |
2bca1e23 | 2745 | ClearPageUptodate(page); |
b763f3be | 2746 | clear_page_private_gcing(page); |
eb47b800 | 2747 | goto out_writepage; |
2bca1e23 | 2748 | } |
e959c8f5 | 2749 | got_it: |
c9b60788 CY |
2750 | if (__is_valid_data_blkaddr(fio->old_blkaddr) && |
2751 | !f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, | |
93770ab7 | 2752 | DATA_GENERIC_ENHANCE)) { |
10f966bb | 2753 | err = -EFSCORRUPTED; |
95fa90c9 | 2754 | f2fs_handle_error(fio->sbi, ERROR_INVALID_BLKADDR); |
c9b60788 CY |
2755 | goto out_writepage; |
2756 | } | |
3db1de0e | 2757 | |
eb47b800 JK |
2758 | /* |
2759 | * If current allocation needs SSR, | |
2760 | * it had better in-place writes for updated data. | |
2761 | */ | |
93770ab7 CY |
2762 | if (ipu_force || |
2763 | (__is_valid_data_blkaddr(fio->old_blkaddr) && | |
7b525dd0 | 2764 | need_inplace_update(fio))) { |
4c8ff709 | 2765 | err = f2fs_encrypt_one_page(fio); |
cc15620b JK |
2766 | if (err) |
2767 | goto out_writepage; | |
2768 | ||
2769 | set_page_writeback(page); | |
279d6df2 | 2770 | f2fs_put_dnode(&dn); |
cc15620b | 2771 | if (fio->need_lock == LOCK_REQ) |
279d6df2 | 2772 | f2fs_unlock_op(fio->sbi); |
4d57b86d | 2773 | err = f2fs_inplace_write_data(fio); |
6492a335 | 2774 | if (err) { |
27aacd28 | 2775 | if (fscrypt_inode_uses_fs_layer_crypto(inode)) |
d2d0727b | 2776 | fscrypt_finalize_bounce_page(&fio->encrypted_page); |
6492a335 CY |
2777 | if (PageWriteback(page)) |
2778 | end_page_writeback(page); | |
cd23ffa9 CY |
2779 | } else { |
2780 | set_inode_flag(inode, FI_UPDATE_WRITE); | |
6492a335 | 2781 | } |
7eab0c0d | 2782 | trace_f2fs_do_write_data_page(fio->page, IPU); |
279d6df2 | 2783 | return err; |
eb47b800 | 2784 | } |
279d6df2 | 2785 | |
cc15620b JK |
2786 | if (fio->need_lock == LOCK_RETRY) { |
2787 | if (!f2fs_trylock_op(fio->sbi)) { | |
2788 | err = -EAGAIN; | |
2789 | goto out_writepage; | |
2790 | } | |
2791 | fio->need_lock = LOCK_REQ; | |
2792 | } | |
2793 | ||
a9419b63 | 2794 | err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false); |
7735730d CY |
2795 | if (err) |
2796 | goto out_writepage; | |
2797 | ||
2798 | fio->version = ni.version; | |
2799 | ||
4c8ff709 | 2800 | err = f2fs_encrypt_one_page(fio); |
cc15620b JK |
2801 | if (err) |
2802 | goto out_writepage; | |
2803 | ||
2804 | set_page_writeback(page); | |
2805 | ||
4c8ff709 CY |
2806 | if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR) |
2807 | f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false); | |
2808 | ||
279d6df2 | 2809 | /* LFS mode write path */ |
4d57b86d | 2810 | f2fs_outplace_write_data(&dn, fio); |
279d6df2 HP |
2811 | trace_f2fs_do_write_data_page(page, OPU); |
2812 | set_inode_flag(inode, FI_APPEND_WRITE); | |
2813 | if (page->index == 0) | |
2814 | set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN); | |
eb47b800 JK |
2815 | out_writepage: |
2816 | f2fs_put_dnode(&dn); | |
279d6df2 | 2817 | out: |
cc15620b | 2818 | if (fio->need_lock == LOCK_REQ) |
279d6df2 | 2819 | f2fs_unlock_op(fio->sbi); |
eb47b800 JK |
2820 | return err; |
2821 | } | |
2822 | ||
4c8ff709 | 2823 | int f2fs_write_single_data_page(struct page *page, int *submitted, |
8648de2c CY |
2824 | struct bio **bio, |
2825 | sector_t *last_block, | |
b0af6d49 | 2826 | struct writeback_control *wbc, |
4c8ff709 | 2827 | enum iostat_type io_type, |
3afae09f CY |
2828 | int compr_blocks, |
2829 | bool allow_balance) | |
eb47b800 JK |
2830 | { |
2831 | struct inode *inode = page->mapping->host; | |
4081363f | 2832 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
eb47b800 | 2833 | loff_t i_size = i_size_read(inode); |
4c8ff709 | 2834 | const pgoff_t end_index = ((unsigned long long)i_size) |
09cbfeaf | 2835 | >> PAGE_SHIFT; |
1f0d5c91 | 2836 | loff_t psize = (loff_t)(page->index + 1) << PAGE_SHIFT; |
9ffe0fb5 | 2837 | unsigned offset = 0; |
39936837 | 2838 | bool need_balance_fs = false; |
f082c6b2 | 2839 | bool quota_inode = IS_NOQUOTA(inode); |
eb47b800 | 2840 | int err = 0; |
458e6197 | 2841 | struct f2fs_io_info fio = { |
05ca3632 | 2842 | .sbi = sbi, |
39d787be | 2843 | .ino = inode->i_ino, |
458e6197 | 2844 | .type = DATA, |
04d328de | 2845 | .op = REQ_OP_WRITE, |
7637241e | 2846 | .op_flags = wbc_to_write_flags(wbc), |
e959c8f5 | 2847 | .old_blkaddr = NULL_ADDR, |
05ca3632 | 2848 | .page = page, |
4375a336 | 2849 | .encrypted_page = NULL, |
2eae077e | 2850 | .submitted = 0, |
4c8ff709 | 2851 | .compr_blocks = compr_blocks, |
cc15620b | 2852 | .need_lock = LOCK_RETRY, |
2eae077e | 2853 | .post_read = f2fs_post_read_required(inode) ? 1 : 0, |
b0af6d49 | 2854 | .io_type = io_type, |
578c6478 | 2855 | .io_wbc = wbc, |
8648de2c CY |
2856 | .bio = bio, |
2857 | .last_block = last_block, | |
458e6197 | 2858 | }; |
eb47b800 | 2859 | |
ecda0de3 CY |
2860 | trace_f2fs_writepage(page, DATA); |
2861 | ||
146949de | 2862 | /* we should bypass data pages to proceed the kworker jobs */ |
db198ae0 CY |
2863 | if (unlikely(f2fs_cp_error(sbi))) { |
2864 | mapping_set_error(page->mapping, -EIO); | |
1174abfd CY |
2865 | /* |
2866 | * don't drop any dirty dentry pages for keeping lastest | |
2867 | * directory structure. | |
2868 | */ | |
c9b3649a CY |
2869 | if (S_ISDIR(inode->i_mode) && |
2870 | !is_sbi_flag_set(sbi, SBI_IS_CLOSE)) | |
1174abfd | 2871 | goto redirty_out; |
b62e71be CY |
2872 | |
2873 | /* keep data pages in remount-ro mode */ | |
2874 | if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY) | |
2875 | goto redirty_out; | |
db198ae0 CY |
2876 | goto out; |
2877 | } | |
2878 | ||
0771fcc7 CY |
2879 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
2880 | goto redirty_out; | |
2881 | ||
4c8ff709 CY |
2882 | if (page->index < end_index || |
2883 | f2fs_verity_in_progress(inode) || | |
2884 | compr_blocks) | |
39936837 | 2885 | goto write; |
eb47b800 JK |
2886 | |
2887 | /* | |
2888 | * If the offset is out-of-range of file size, | |
2889 | * this page does not have to be written to disk. | |
2890 | */ | |
09cbfeaf | 2891 | offset = i_size & (PAGE_SIZE - 1); |
76f60268 | 2892 | if ((page->index >= end_index + 1) || !offset) |
39936837 | 2893 | goto out; |
eb47b800 | 2894 | |
09cbfeaf | 2895 | zero_user_segment(page, offset, PAGE_SIZE); |
39936837 | 2896 | write: |
1e84371f JK |
2897 | if (f2fs_is_drop_cache(inode)) |
2898 | goto out; | |
eb47b800 | 2899 | |
435cbab9 | 2900 | /* Dentry/quota blocks are controlled by checkpoint */ |
f082c6b2 | 2901 | if (S_ISDIR(inode->i_mode) || quota_inode) { |
79963d96 CY |
2902 | /* |
2903 | * We need to wait for node_write to avoid block allocation during | |
2904 | * checkpoint. This can only happen to quota writes which can cause | |
2905 | * the below discard race condition. | |
2906 | */ | |
f082c6b2 | 2907 | if (quota_inode) |
e4544b63 | 2908 | f2fs_down_read(&sbi->node_write); |
79963d96 | 2909 | |
cc15620b | 2910 | fio.need_lock = LOCK_DONE; |
4d57b86d | 2911 | err = f2fs_do_write_data_page(&fio); |
79963d96 | 2912 | |
f082c6b2 | 2913 | if (quota_inode) |
e4544b63 | 2914 | f2fs_up_read(&sbi->node_write); |
79963d96 | 2915 | |
8618b881 JK |
2916 | goto done; |
2917 | } | |
9ffe0fb5 | 2918 | |
8618b881 | 2919 | if (!wbc->for_reclaim) |
39936837 | 2920 | need_balance_fs = true; |
7f3037a5 | 2921 | else if (has_not_enough_free_secs(sbi, 0, 0)) |
39936837 | 2922 | goto redirty_out; |
ef095d19 JK |
2923 | else |
2924 | set_inode_flag(inode, FI_HOT_DATA); | |
eb47b800 | 2925 | |
b3d208f9 | 2926 | err = -EAGAIN; |
dd7b2333 | 2927 | if (f2fs_has_inline_data(inode)) { |
b3d208f9 | 2928 | err = f2fs_write_inline_data(inode, page); |
dd7b2333 YH |
2929 | if (!err) |
2930 | goto out; | |
2931 | } | |
279d6df2 | 2932 | |
cc15620b | 2933 | if (err == -EAGAIN) { |
4d57b86d | 2934 | err = f2fs_do_write_data_page(&fio); |
cc15620b JK |
2935 | if (err == -EAGAIN) { |
2936 | fio.need_lock = LOCK_REQ; | |
4d57b86d | 2937 | err = f2fs_do_write_data_page(&fio); |
cc15620b JK |
2938 | } |
2939 | } | |
a0d00fad | 2940 | |
eb449797 CY |
2941 | if (err) { |
2942 | file_set_keep_isize(inode); | |
2943 | } else { | |
c10c9820 | 2944 | spin_lock(&F2FS_I(inode)->i_size_lock); |
eb449797 CY |
2945 | if (F2FS_I(inode)->last_disk_size < psize) |
2946 | F2FS_I(inode)->last_disk_size = psize; | |
c10c9820 | 2947 | spin_unlock(&F2FS_I(inode)->i_size_lock); |
eb449797 | 2948 | } |
279d6df2 | 2949 | |
8618b881 JK |
2950 | done: |
2951 | if (err && err != -ENOENT) | |
2952 | goto redirty_out; | |
eb47b800 | 2953 | |
39936837 | 2954 | out: |
a7ffdbe2 | 2955 | inode_dec_dirty_pages(inode); |
2baf0781 | 2956 | if (err) { |
2bca1e23 | 2957 | ClearPageUptodate(page); |
b763f3be | 2958 | clear_page_private_gcing(page); |
2baf0781 | 2959 | } |
0c3a5797 CY |
2960 | |
2961 | if (wbc->for_reclaim) { | |
bab475c5 | 2962 | f2fs_submit_merged_write_cond(sbi, NULL, page, 0, DATA); |
ef095d19 | 2963 | clear_inode_flag(inode, FI_HOT_DATA); |
4d57b86d | 2964 | f2fs_remove_dirty_inode(inode); |
d68f735b | 2965 | submitted = NULL; |
0c3a5797 | 2966 | } |
eb47b800 | 2967 | unlock_page(page); |
186857c5 | 2968 | if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) && |
d80afefb | 2969 | !F2FS_I(inode)->wb_task && allow_balance) |
a7881893 | 2970 | f2fs_balance_fs(sbi, need_balance_fs); |
0c3a5797 | 2971 | |
d68f735b | 2972 | if (unlikely(f2fs_cp_error(sbi))) { |
b9109b0e | 2973 | f2fs_submit_merged_write(sbi, DATA); |
5cdb422c CY |
2974 | if (bio && *bio) |
2975 | f2fs_submit_merged_ipu_write(sbi, bio, NULL); | |
d68f735b JK |
2976 | submitted = NULL; |
2977 | } | |
2978 | ||
2979 | if (submitted) | |
2eae077e | 2980 | *submitted = fio.submitted; |
0c3a5797 | 2981 | |
eb47b800 JK |
2982 | return 0; |
2983 | ||
eb47b800 | 2984 | redirty_out: |
76f60268 | 2985 | redirty_page_for_writepage(wbc, page); |
5b19d284 | 2986 | /* |
146949de | 2987 | * pageout() in MM translates EAGAIN, so calls handle_write_error() |
5b19d284 JK |
2988 | * -> mapping_set_error() -> set_bit(AS_EIO, ...). |
2989 | * file_write_and_wait_range() will see EIO error, which is critical | |
2990 | * to return value of fsync() followed by atomic_write failure to user. | |
2991 | */ | |
2992 | if (!err || wbc->for_reclaim) | |
0002b61b | 2993 | return AOP_WRITEPAGE_ACTIVATE; |
b230e6ca JK |
2994 | unlock_page(page); |
2995 | return err; | |
fa9150a8 NJ |
2996 | } |
2997 | ||
f566bae8 JK |
2998 | static int f2fs_write_data_page(struct page *page, |
2999 | struct writeback_control *wbc) | |
3000 | { | |
4c8ff709 CY |
3001 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
3002 | struct inode *inode = page->mapping->host; | |
3003 | ||
3004 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) | |
3005 | goto out; | |
3006 | ||
3007 | if (f2fs_compressed_file(inode)) { | |
3008 | if (f2fs_is_compressed_cluster(inode, page->index)) { | |
3009 | redirty_page_for_writepage(wbc, page); | |
3010 | return AOP_WRITEPAGE_ACTIVATE; | |
3011 | } | |
3012 | } | |
3013 | out: | |
3014 | #endif | |
3015 | ||
3016 | return f2fs_write_single_data_page(page, NULL, NULL, NULL, | |
3afae09f | 3017 | wbc, FS_DATA_IO, 0, true); |
f566bae8 JK |
3018 | } |
3019 | ||
8f46dcae | 3020 | /* |
146949de | 3021 | * This function was copied from write_cache_pages from mm/page-writeback.c. |
8f46dcae CY |
3022 | * The major change is making write step of cold data page separately from |
3023 | * warm/hot data page. | |
3024 | */ | |
3025 | static int f2fs_write_cache_pages(struct address_space *mapping, | |
b0af6d49 CY |
3026 | struct writeback_control *wbc, |
3027 | enum iostat_type io_type) | |
8f46dcae CY |
3028 | { |
3029 | int ret = 0; | |
4c8ff709 | 3030 | int done = 0, retry = 0; |
c5d3f9b7 CY |
3031 | struct page *pages_local[F2FS_ONSTACK_PAGES]; |
3032 | struct page **pages = pages_local; | |
1cd98ee7 | 3033 | struct folio_batch fbatch; |
c29fd0c0 | 3034 | struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); |
8648de2c CY |
3035 | struct bio *bio = NULL; |
3036 | sector_t last_block; | |
4c8ff709 CY |
3037 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
3038 | struct inode *inode = mapping->host; | |
3039 | struct compress_ctx cc = { | |
3040 | .inode = inode, | |
3041 | .log_cluster_size = F2FS_I(inode)->i_log_cluster_size, | |
3042 | .cluster_size = F2FS_I(inode)->i_cluster_size, | |
3043 | .cluster_idx = NULL_CLUSTER, | |
3044 | .rpages = NULL, | |
3045 | .nr_rpages = 0, | |
3046 | .cpages = NULL, | |
3271d7eb | 3047 | .valid_nr_cpages = 0, |
4c8ff709 CY |
3048 | .rbuf = NULL, |
3049 | .cbuf = NULL, | |
3050 | .rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size, | |
3051 | .private = NULL, | |
3052 | }; | |
3053 | #endif | |
1cd98ee7 | 3054 | int nr_folios, p, idx; |
8f46dcae | 3055 | int nr_pages; |
c5d3f9b7 | 3056 | unsigned int max_pages = F2FS_ONSTACK_PAGES; |
8f46dcae CY |
3057 | pgoff_t index; |
3058 | pgoff_t end; /* Inclusive */ | |
3059 | pgoff_t done_index; | |
8f46dcae | 3060 | int range_whole = 0; |
10bbd235 | 3061 | xa_mark_t tag; |
bab475c5 | 3062 | int nwritten = 0; |
4c8ff709 CY |
3063 | int submitted = 0; |
3064 | int i; | |
8f46dcae | 3065 | |
c5d3f9b7 CY |
3066 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
3067 | if (f2fs_compressed_file(inode) && | |
3068 | 1 << cc.log_cluster_size > F2FS_ONSTACK_PAGES) { | |
3069 | pages = f2fs_kzalloc(sbi, sizeof(struct page *) << | |
3070 | cc.log_cluster_size, GFP_NOFS | __GFP_NOFAIL); | |
3071 | max_pages = 1 << cc.log_cluster_size; | |
3072 | } | |
3073 | #endif | |
3074 | ||
1cd98ee7 VMO |
3075 | folio_batch_init(&fbatch); |
3076 | ||
ef095d19 JK |
3077 | if (get_dirty_pages(mapping->host) <= |
3078 | SM_I(F2FS_M_SB(mapping))->min_hot_blocks) | |
3079 | set_inode_flag(mapping->host, FI_HOT_DATA); | |
3080 | else | |
3081 | clear_inode_flag(mapping->host, FI_HOT_DATA); | |
3082 | ||
8f46dcae | 3083 | if (wbc->range_cyclic) { |
4df7a75f | 3084 | index = mapping->writeback_index; /* prev offset */ |
8f46dcae CY |
3085 | end = -1; |
3086 | } else { | |
09cbfeaf KS |
3087 | index = wbc->range_start >> PAGE_SHIFT; |
3088 | end = wbc->range_end >> PAGE_SHIFT; | |
8f46dcae CY |
3089 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
3090 | range_whole = 1; | |
8f46dcae CY |
3091 | } |
3092 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) | |
3093 | tag = PAGECACHE_TAG_TOWRITE; | |
3094 | else | |
3095 | tag = PAGECACHE_TAG_DIRTY; | |
3096 | retry: | |
4c8ff709 | 3097 | retry = 0; |
8f46dcae CY |
3098 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
3099 | tag_pages_for_writeback(mapping, index, end); | |
3100 | done_index = index; | |
4c8ff709 | 3101 | while (!done && !retry && (index <= end)) { |
1cd98ee7 VMO |
3102 | nr_pages = 0; |
3103 | again: | |
3104 | nr_folios = filemap_get_folios_tag(mapping, &index, end, | |
3105 | tag, &fbatch); | |
3106 | if (nr_folios == 0) { | |
3107 | if (nr_pages) | |
3108 | goto write; | |
8f46dcae | 3109 | break; |
1cd98ee7 | 3110 | } |
8f46dcae | 3111 | |
1cd98ee7 VMO |
3112 | for (i = 0; i < nr_folios; i++) { |
3113 | struct folio *folio = fbatch.folios[i]; | |
3114 | ||
3115 | idx = 0; | |
3116 | p = folio_nr_pages(folio); | |
3117 | add_more: | |
3118 | pages[nr_pages] = folio_page(folio, idx); | |
3119 | folio_get(folio); | |
c5d3f9b7 | 3120 | if (++nr_pages == max_pages) { |
1cd98ee7 VMO |
3121 | index = folio->index + idx + 1; |
3122 | folio_batch_release(&fbatch); | |
3123 | goto write; | |
3124 | } | |
3125 | if (++idx < p) | |
3126 | goto add_more; | |
3127 | } | |
3128 | folio_batch_release(&fbatch); | |
3129 | goto again; | |
3130 | write: | |
8f46dcae | 3131 | for (i = 0; i < nr_pages; i++) { |
01fc4b9a | 3132 | struct page *page = pages[i]; |
1cd98ee7 | 3133 | struct folio *folio = page_folio(page); |
4c8ff709 CY |
3134 | bool need_readd; |
3135 | readd: | |
3136 | need_readd = false; | |
3137 | #ifdef CONFIG_F2FS_FS_COMPRESSION | |
3138 | if (f2fs_compressed_file(inode)) { | |
b368cc5e FC |
3139 | void *fsdata = NULL; |
3140 | struct page *pagep; | |
3141 | int ret2; | |
3142 | ||
4c8ff709 CY |
3143 | ret = f2fs_init_compress_ctx(&cc); |
3144 | if (ret) { | |
3145 | done = 1; | |
3146 | break; | |
3147 | } | |
3148 | ||
3149 | if (!f2fs_cluster_can_merge_page(&cc, | |
1cd98ee7 | 3150 | folio->index)) { |
4c8ff709 CY |
3151 | ret = f2fs_write_multi_pages(&cc, |
3152 | &submitted, wbc, io_type); | |
3153 | if (!ret) | |
3154 | need_readd = true; | |
3155 | goto result; | |
3156 | } | |
8f46dcae | 3157 | |
4c8ff709 | 3158 | if (unlikely(f2fs_cp_error(sbi))) |
1cd98ee7 | 3159 | goto lock_folio; |
4c8ff709 | 3160 | |
b368cc5e | 3161 | if (!f2fs_cluster_is_empty(&cc)) |
1cd98ee7 | 3162 | goto lock_folio; |
4c8ff709 | 3163 | |
4f8219f8 | 3164 | if (f2fs_all_cluster_page_ready(&cc, |
01fc4b9a | 3165 | pages, i, nr_pages, true)) |
1cd98ee7 | 3166 | goto lock_folio; |
4f8219f8 | 3167 | |
b368cc5e | 3168 | ret2 = f2fs_prepare_compress_overwrite( |
4c8ff709 | 3169 | inode, &pagep, |
1cd98ee7 | 3170 | folio->index, &fsdata); |
b368cc5e FC |
3171 | if (ret2 < 0) { |
3172 | ret = ret2; | |
3173 | done = 1; | |
3174 | break; | |
3175 | } else if (ret2 && | |
3176 | (!f2fs_compress_write_end(inode, | |
1cd98ee7 | 3177 | fsdata, folio->index, 1) || |
4f8219f8 | 3178 | !f2fs_all_cluster_page_ready(&cc, |
1cd98ee7 VMO |
3179 | pages, i, nr_pages, |
3180 | false))) { | |
b368cc5e FC |
3181 | retry = 1; |
3182 | break; | |
4c8ff709 CY |
3183 | } |
3184 | } | |
3185 | #endif | |
f8de4331 | 3186 | /* give a priority to WB_SYNC threads */ |
c29fd0c0 | 3187 | if (atomic_read(&sbi->wb_sync_req[DATA]) && |
f8de4331 CY |
3188 | wbc->sync_mode == WB_SYNC_NONE) { |
3189 | done = 1; | |
3190 | break; | |
3191 | } | |
4c8ff709 | 3192 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
1cd98ee7 | 3193 | lock_folio: |
4c8ff709 | 3194 | #endif |
1cd98ee7 | 3195 | done_index = folio->index; |
d29460e5 | 3196 | retry_write: |
1cd98ee7 | 3197 | folio_lock(folio); |
8f46dcae | 3198 | |
1cd98ee7 | 3199 | if (unlikely(folio->mapping != mapping)) { |
8f46dcae | 3200 | continue_unlock: |
1cd98ee7 | 3201 | folio_unlock(folio); |
8f46dcae CY |
3202 | continue; |
3203 | } | |
3204 | ||
1cd98ee7 | 3205 | if (!folio_test_dirty(folio)) { |
8f46dcae CY |
3206 | /* someone wrote it for us */ |
3207 | goto continue_unlock; | |
3208 | } | |
3209 | ||
1cd98ee7 | 3210 | if (folio_test_writeback(folio)) { |
c948be79 | 3211 | if (wbc->sync_mode == WB_SYNC_NONE) |
8f46dcae | 3212 | goto continue_unlock; |
c948be79 | 3213 | f2fs_wait_on_page_writeback(&folio->page, DATA, true, true); |
8f46dcae CY |
3214 | } |
3215 | ||
1cd98ee7 | 3216 | if (!folio_clear_dirty_for_io(folio)) |
8f46dcae CY |
3217 | goto continue_unlock; |
3218 | ||
4c8ff709 CY |
3219 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
3220 | if (f2fs_compressed_file(inode)) { | |
1cd98ee7 VMO |
3221 | folio_get(folio); |
3222 | f2fs_compress_ctx_add_page(&cc, &folio->page); | |
4c8ff709 CY |
3223 | continue; |
3224 | } | |
3225 | #endif | |
1cd98ee7 VMO |
3226 | ret = f2fs_write_single_data_page(&folio->page, |
3227 | &submitted, &bio, &last_block, | |
3228 | wbc, io_type, 0, true); | |
4c8ff709 | 3229 | if (ret == AOP_WRITEPAGE_ACTIVATE) |
1cd98ee7 | 3230 | folio_unlock(folio); |
4c8ff709 CY |
3231 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
3232 | result: | |
3233 | #endif | |
3234 | nwritten += submitted; | |
3235 | wbc->nr_to_write -= submitted; | |
3236 | ||
8f46dcae | 3237 | if (unlikely(ret)) { |
0002b61b CY |
3238 | /* |
3239 | * keep nr_to_write, since vfs uses this to | |
3240 | * get # of written pages. | |
3241 | */ | |
3242 | if (ret == AOP_WRITEPAGE_ACTIVATE) { | |
0002b61b | 3243 | ret = 0; |
4c8ff709 | 3244 | goto next; |
d29460e5 JK |
3245 | } else if (ret == -EAGAIN) { |
3246 | ret = 0; | |
3247 | if (wbc->sync_mode == WB_SYNC_ALL) { | |
a64239d0 | 3248 | f2fs_io_schedule_timeout( |
5df7731f | 3249 | DEFAULT_IO_TIMEOUT); |
d29460e5 JK |
3250 | goto retry_write; |
3251 | } | |
4c8ff709 | 3252 | goto next; |
0002b61b | 3253 | } |
a842a909 | 3254 | done_index = folio_next_index(folio); |
b230e6ca JK |
3255 | done = 1; |
3256 | break; | |
8f46dcae CY |
3257 | } |
3258 | ||
4c8ff709 | 3259 | if (wbc->nr_to_write <= 0 && |
687de7f1 | 3260 | wbc->sync_mode == WB_SYNC_NONE) { |
8f46dcae CY |
3261 | done = 1; |
3262 | break; | |
3263 | } | |
4c8ff709 CY |
3264 | next: |
3265 | if (need_readd) | |
3266 | goto readd; | |
8f46dcae | 3267 | } |
01fc4b9a | 3268 | release_pages(pages, nr_pages); |
8f46dcae CY |
3269 | cond_resched(); |
3270 | } | |
4c8ff709 CY |
3271 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
3272 | /* flush remained pages in compress cluster */ | |
3273 | if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) { | |
3274 | ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type); | |
3275 | nwritten += submitted; | |
3276 | wbc->nr_to_write -= submitted; | |
3277 | if (ret) { | |
3278 | done = 1; | |
3279 | retry = 0; | |
3280 | } | |
3281 | } | |
adfc6943 | 3282 | if (f2fs_compressed_file(inode)) |
8bfbfb0d | 3283 | f2fs_destroy_compress_ctx(&cc, false); |
4c8ff709 | 3284 | #endif |
e78790f8 | 3285 | if (retry) { |
8f46dcae | 3286 | index = 0; |
e78790f8 | 3287 | end = -1; |
8f46dcae CY |
3288 | goto retry; |
3289 | } | |
e78790f8 ST |
3290 | if (wbc->range_cyclic && !done) |
3291 | done_index = 0; | |
8f46dcae CY |
3292 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
3293 | mapping->writeback_index = done_index; | |
3294 | ||
bab475c5 | 3295 | if (nwritten) |
b9109b0e | 3296 | f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host, |
bab475c5 | 3297 | NULL, 0, DATA); |
8648de2c CY |
3298 | /* submit cached bio of IPU write */ |
3299 | if (bio) | |
0b20fcec | 3300 | f2fs_submit_merged_ipu_write(sbi, &bio, NULL); |
6ca56ca4 | 3301 | |
c5d3f9b7 CY |
3302 | #ifdef CONFIG_F2FS_FS_COMPRESSION |
3303 | if (pages != pages_local) | |
3304 | kfree(pages); | |
3305 | #endif | |
3306 | ||
8f46dcae CY |
3307 | return ret; |
3308 | } | |
3309 | ||
853137ce JK |
3310 | static inline bool __should_serialize_io(struct inode *inode, |
3311 | struct writeback_control *wbc) | |
3312 | { | |
b13f67ff | 3313 | /* to avoid deadlock in path of data flush */ |
d80afefb | 3314 | if (F2FS_I(inode)->wb_task) |
b13f67ff CY |
3315 | return false; |
3316 | ||
853137ce JK |
3317 | if (!S_ISREG(inode->i_mode)) |
3318 | return false; | |
af033b2a CY |
3319 | if (IS_NOQUOTA(inode)) |
3320 | return false; | |
b13f67ff | 3321 | |
602a16d5 | 3322 | if (f2fs_need_compress_data(inode)) |
b13f67ff | 3323 | return true; |
853137ce JK |
3324 | if (wbc->sync_mode != WB_SYNC_ALL) |
3325 | return true; | |
3326 | if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks) | |
3327 | return true; | |
3328 | return false; | |
3329 | } | |
3330 | ||
fc99fe27 | 3331 | static int __f2fs_write_data_pages(struct address_space *mapping, |
b0af6d49 CY |
3332 | struct writeback_control *wbc, |
3333 | enum iostat_type io_type) | |
eb47b800 JK |
3334 | { |
3335 | struct inode *inode = mapping->host; | |
4081363f | 3336 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
9dfa1baf | 3337 | struct blk_plug plug; |
eb47b800 | 3338 | int ret; |
853137ce | 3339 | bool locked = false; |
eb47b800 | 3340 | |
cfb185a1 | 3341 | /* deal with chardevs and other special file */ |
3342 | if (!mapping->a_ops->writepage) | |
3343 | return 0; | |
3344 | ||
6a290544 CY |
3345 | /* skip writing if there is no dirty page in this inode */ |
3346 | if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) | |
3347 | return 0; | |
3348 | ||
0771fcc7 CY |
3349 | /* during POR, we don't need to trigger writepage at all. */ |
3350 | if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) | |
3351 | goto skip_write; | |
3352 | ||
af033b2a CY |
3353 | if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) && |
3354 | wbc->sync_mode == WB_SYNC_NONE && | |
a1257023 | 3355 | get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && |
4d57b86d | 3356 | f2fs_available_free_memory(sbi, DIRTY_DENTS)) |
a1257023 JK |
3357 | goto skip_write; |
3358 | ||
1018a546 CY |
3359 | /* skip writing in file defragment preparing stage */ |
3360 | if (is_inode_flag_set(inode, FI_SKIP_WRITES)) | |
d323d005 CY |
3361 | goto skip_write; |
3362 | ||
d31c7c3f YH |
3363 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
3364 | ||
687de7f1 JK |
3365 | /* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */ |
3366 | if (wbc->sync_mode == WB_SYNC_ALL) | |
c29fd0c0 | 3367 | atomic_inc(&sbi->wb_sync_req[DATA]); |
34415099 CY |
3368 | else if (atomic_read(&sbi->wb_sync_req[DATA])) { |
3369 | /* to avoid potential deadlock */ | |
3370 | if (current->plug) | |
3371 | blk_finish_plug(current->plug); | |
687de7f1 | 3372 | goto skip_write; |
34415099 | 3373 | } |
687de7f1 | 3374 | |
853137ce JK |
3375 | if (__should_serialize_io(inode, wbc)) { |
3376 | mutex_lock(&sbi->writepages); | |
3377 | locked = true; | |
3378 | } | |
3379 | ||
9dfa1baf | 3380 | blk_start_plug(&plug); |
b0af6d49 | 3381 | ret = f2fs_write_cache_pages(mapping, wbc, io_type); |
9dfa1baf | 3382 | blk_finish_plug(&plug); |
687de7f1 | 3383 | |
853137ce JK |
3384 | if (locked) |
3385 | mutex_unlock(&sbi->writepages); | |
3386 | ||
687de7f1 | 3387 | if (wbc->sync_mode == WB_SYNC_ALL) |
c29fd0c0 | 3388 | atomic_dec(&sbi->wb_sync_req[DATA]); |
28ea6162 JK |
3389 | /* |
3390 | * if some pages were truncated, we cannot guarantee its mapping->host | |
3391 | * to detect pending bios. | |
3392 | */ | |
458e6197 | 3393 | |
4d57b86d | 3394 | f2fs_remove_dirty_inode(inode); |
eb47b800 | 3395 | return ret; |
d3baf95d JK |
3396 | |
3397 | skip_write: | |
a7ffdbe2 | 3398 | wbc->pages_skipped += get_dirty_pages(inode); |
d31c7c3f | 3399 | trace_f2fs_writepages(mapping->host, wbc, DATA); |
d3baf95d | 3400 | return 0; |
eb47b800 JK |
3401 | } |
3402 | ||
b0af6d49 CY |
3403 | static int f2fs_write_data_pages(struct address_space *mapping, |
3404 | struct writeback_control *wbc) | |
3405 | { | |
3406 | struct inode *inode = mapping->host; | |
3407 | ||
3408 | return __f2fs_write_data_pages(mapping, wbc, | |
3409 | F2FS_I(inode)->cp_task == current ? | |
3410 | FS_CP_DATA_IO : FS_DATA_IO); | |
3411 | } | |
3412 | ||
a1e09b03 | 3413 | void f2fs_write_failed(struct inode *inode, loff_t to) |
3aab8f82 | 3414 | { |
819d9153 | 3415 | loff_t i_size = i_size_read(inode); |
3aab8f82 | 3416 | |
3f188c23 JK |
3417 | if (IS_NOQUOTA(inode)) |
3418 | return; | |
3419 | ||
95ae251f EB |
3420 | /* In the fs-verity case, f2fs_end_enable_verity() does the truncate */ |
3421 | if (to > i_size && !f2fs_verity_in_progress(inode)) { | |
e4544b63 | 3422 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
6abaa83c | 3423 | filemap_invalidate_lock(inode->i_mapping); |
a33c1502 | 3424 | |
819d9153 | 3425 | truncate_pagecache(inode, i_size); |
3f188c23 | 3426 | f2fs_truncate_blocks(inode, i_size, true); |
a33c1502 | 3427 | |
6abaa83c | 3428 | filemap_invalidate_unlock(inode->i_mapping); |
e4544b63 | 3429 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
3aab8f82 CY |
3430 | } |
3431 | } | |
3432 | ||
2aadac08 JK |
3433 | static int prepare_write_begin(struct f2fs_sb_info *sbi, |
3434 | struct page *page, loff_t pos, unsigned len, | |
3435 | block_t *blk_addr, bool *node_changed) | |
3436 | { | |
3437 | struct inode *inode = page->mapping->host; | |
3438 | pgoff_t index = page->index; | |
3439 | struct dnode_of_data dn; | |
3440 | struct page *ipage; | |
b4d07a3e | 3441 | bool locked = false; |
44b0dfeb | 3442 | int flag = F2FS_GET_BLOCK_PRE_AIO; |
2aadac08 JK |
3443 | int err = 0; |
3444 | ||
24b84912 | 3445 | /* |
3d697a4a EB |
3446 | * If a whole page is being written and we already preallocated all the |
3447 | * blocks, then there is no need to get a block address now. | |
24b84912 | 3448 | */ |
3d697a4a | 3449 | if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL)) |
24b84912 JK |
3450 | return 0; |
3451 | ||
2866fb16 | 3452 | /* f2fs_lock_op avoids race between write CP and convert_inline_page */ |
44b0dfeb CH |
3453 | if (f2fs_has_inline_data(inode)) { |
3454 | if (pos + len > MAX_INLINE_DATA(inode)) | |
3455 | flag = F2FS_GET_BLOCK_DEFAULT; | |
3456 | f2fs_map_lock(sbi, flag); | |
3457 | locked = true; | |
3458 | } else if ((pos & PAGE_MASK) >= i_size_read(inode)) { | |
2f51ade9 | 3459 | f2fs_map_lock(sbi, flag); |
b4d07a3e JK |
3460 | locked = true; |
3461 | } | |
4c8ff709 | 3462 | |
b4d07a3e | 3463 | restart: |
2aadac08 | 3464 | /* check inline_data */ |
4d57b86d | 3465 | ipage = f2fs_get_node_page(sbi, inode->i_ino); |
2aadac08 JK |
3466 | if (IS_ERR(ipage)) { |
3467 | err = PTR_ERR(ipage); | |
3468 | goto unlock_out; | |
3469 | } | |
3470 | ||
3471 | set_new_dnode(&dn, inode, ipage, ipage, 0); | |
3472 | ||
3473 | if (f2fs_has_inline_data(inode)) { | |
f2470371 | 3474 | if (pos + len <= MAX_INLINE_DATA(inode)) { |
4d57b86d | 3475 | f2fs_do_read_inline_data(page, ipage); |
91942321 | 3476 | set_inode_flag(inode, FI_DATA_EXIST); |
ab47036d | 3477 | if (inode->i_nlink) |
b763f3be | 3478 | set_page_private_inline(ipage); |
44b0dfeb | 3479 | goto out; |
2aadac08 | 3480 | } |
44b0dfeb CH |
3481 | err = f2fs_convert_inline_page(&dn, page); |
3482 | if (err || dn.data_blkaddr != NULL_ADDR) | |
3483 | goto out; | |
2aadac08 | 3484 | } |
b4d07a3e | 3485 | |
44b0dfeb CH |
3486 | if (!f2fs_lookup_read_extent_cache_block(inode, index, |
3487 | &dn.data_blkaddr)) { | |
3488 | if (locked) { | |
3489 | err = f2fs_reserve_block(&dn, index); | |
3490 | goto out; | |
b4d07a3e | 3491 | } |
44b0dfeb | 3492 | |
04a91ab0 CH |
3493 | /* hole case */ |
3494 | err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); | |
44b0dfeb CH |
3495 | if (!err && dn.data_blkaddr != NULL_ADDR) |
3496 | goto out; | |
3497 | f2fs_put_dnode(&dn); | |
3498 | f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO); | |
3499 | WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO); | |
3500 | locked = true; | |
3501 | goto restart; | |
2aadac08 | 3502 | } |
b4d07a3e | 3503 | out: |
44b0dfeb CH |
3504 | if (!err) { |
3505 | /* convert_inline_page can make node_changed */ | |
3506 | *blk_addr = dn.data_blkaddr; | |
3507 | *node_changed = dn.node_changed; | |
3508 | } | |
2aadac08 JK |
3509 | f2fs_put_dnode(&dn); |
3510 | unlock_out: | |
b4d07a3e | 3511 | if (locked) |
2f51ade9 | 3512 | f2fs_map_unlock(sbi, flag); |
2aadac08 JK |
3513 | return err; |
3514 | } | |
3515 | ||
3db1de0e DJ |
3516 | static int __find_data_block(struct inode *inode, pgoff_t index, |
3517 | block_t *blk_addr) | |
3518 | { | |
3519 | struct dnode_of_data dn; | |
3520 | struct page *ipage; | |
3db1de0e DJ |
3521 | int err = 0; |
3522 | ||
3523 | ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino); | |
3524 | if (IS_ERR(ipage)) | |
3525 | return PTR_ERR(ipage); | |
3526 | ||
3527 | set_new_dnode(&dn, inode, ipage, ipage, 0); | |
3528 | ||
04a91ab0 CH |
3529 | if (!f2fs_lookup_read_extent_cache_block(inode, index, |
3530 | &dn.data_blkaddr)) { | |
3db1de0e DJ |
3531 | /* hole case */ |
3532 | err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); | |
3533 | if (err) { | |
3534 | dn.data_blkaddr = NULL_ADDR; | |
3535 | err = 0; | |
3536 | } | |
3537 | } | |
3538 | *blk_addr = dn.data_blkaddr; | |
3539 | f2fs_put_dnode(&dn); | |
3540 | return err; | |
3541 | } | |
3542 | ||
3543 | static int __reserve_data_block(struct inode *inode, pgoff_t index, | |
3544 | block_t *blk_addr, bool *node_changed) | |
3545 | { | |
3546 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3547 | struct dnode_of_data dn; | |
3548 | struct page *ipage; | |
3549 | int err = 0; | |
3550 | ||
2f51ade9 | 3551 | f2fs_map_lock(sbi, F2FS_GET_BLOCK_PRE_AIO); |
3db1de0e DJ |
3552 | |
3553 | ipage = f2fs_get_node_page(sbi, inode->i_ino); | |
3554 | if (IS_ERR(ipage)) { | |
3555 | err = PTR_ERR(ipage); | |
3556 | goto unlock_out; | |
3557 | } | |
3558 | set_new_dnode(&dn, inode, ipage, ipage, 0); | |
3559 | ||
ffdeab71 CH |
3560 | if (!f2fs_lookup_read_extent_cache_block(dn.inode, index, |
3561 | &dn.data_blkaddr)) | |
3562 | err = f2fs_reserve_block(&dn, index); | |
3db1de0e DJ |
3563 | |
3564 | *blk_addr = dn.data_blkaddr; | |
3565 | *node_changed = dn.node_changed; | |
3566 | f2fs_put_dnode(&dn); | |
3567 | ||
3568 | unlock_out: | |
2f51ade9 | 3569 | f2fs_map_unlock(sbi, F2FS_GET_BLOCK_PRE_AIO); |
3db1de0e DJ |
3570 | return err; |
3571 | } | |
3572 | ||
3573 | static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi, | |
3574 | struct page *page, loff_t pos, unsigned int len, | |
591fc34e | 3575 | block_t *blk_addr, bool *node_changed, bool *use_cow) |
3db1de0e DJ |
3576 | { |
3577 | struct inode *inode = page->mapping->host; | |
3578 | struct inode *cow_inode = F2FS_I(inode)->cow_inode; | |
3579 | pgoff_t index = page->index; | |
3580 | int err = 0; | |
f8e2f32b | 3581 | block_t ori_blk_addr = NULL_ADDR; |
3db1de0e DJ |
3582 | |
3583 | /* If pos is beyond the end of file, reserve a new block in COW inode */ | |
3584 | if ((pos & PAGE_MASK) >= i_size_read(inode)) | |
f8e2f32b | 3585 | goto reserve_block; |
3db1de0e DJ |
3586 | |
3587 | /* Look for the block in COW inode first */ | |
3588 | err = __find_data_block(cow_inode, index, blk_addr); | |
591fc34e | 3589 | if (err) { |
3db1de0e | 3590 | return err; |
591fc34e DJ |
3591 | } else if (*blk_addr != NULL_ADDR) { |
3592 | *use_cow = true; | |
3db1de0e | 3593 | return 0; |
591fc34e | 3594 | } |
3db1de0e | 3595 | |
41e8f85a DJ |
3596 | if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE)) |
3597 | goto reserve_block; | |
3598 | ||
3db1de0e DJ |
3599 | /* Look for the block in the original inode */ |
3600 | err = __find_data_block(inode, index, &ori_blk_addr); | |
3601 | if (err) | |
3602 | return err; | |
3603 | ||
f8e2f32b | 3604 | reserve_block: |
3db1de0e DJ |
3605 | /* Finally, we should reserve a new block in COW inode for the update */ |
3606 | err = __reserve_data_block(cow_inode, index, blk_addr, node_changed); | |
3607 | if (err) | |
3608 | return err; | |
f8e2f32b | 3609 | inc_atomic_write_cnt(inode); |
3db1de0e DJ |
3610 | |
3611 | if (ori_blk_addr != NULL_ADDR) | |
3612 | *blk_addr = ori_blk_addr; | |
3613 | return 0; | |
3614 | } | |
3615 | ||
eb47b800 | 3616 | static int f2fs_write_begin(struct file *file, struct address_space *mapping, |
9d6b0cd7 | 3617 | loff_t pos, unsigned len, struct page **pagep, void **fsdata) |
eb47b800 JK |
3618 | { |
3619 | struct inode *inode = mapping->host; | |
4081363f | 3620 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
86531d6b | 3621 | struct page *page = NULL; |
09cbfeaf | 3622 | pgoff_t index = ((unsigned long long) pos) >> PAGE_SHIFT; |
3db1de0e | 3623 | bool need_balance = false; |
591fc34e | 3624 | bool use_cow = false; |
2aadac08 | 3625 | block_t blkaddr = NULL_ADDR; |
eb47b800 JK |
3626 | int err = 0; |
3627 | ||
9d6b0cd7 | 3628 | trace_f2fs_write_begin(inode, pos, len); |
62aed044 | 3629 | |
00e09c0b CY |
3630 | if (!f2fs_is_checkpoint_ready(sbi)) { |
3631 | err = -ENOSPC; | |
4354994f | 3632 | goto fail; |
00e09c0b | 3633 | } |
4354994f | 3634 | |
5f727395 JK |
3635 | /* |
3636 | * We should check this at this moment to avoid deadlock on inode page | |
3637 | * and #0 page. The locking rule for inline_data conversion should be: | |
3638 | * lock_page(page #0) -> lock_page(inode_page) | |
3639 | */ | |
3640 | if (index != 0) { | |
3641 | err = f2fs_convert_inline_inode(inode); | |
3642 | if (err) | |
3643 | goto fail; | |
3644 | } | |
4c8ff709 CY |
3645 | |
3646 | #ifdef CONFIG_F2FS_FS_COMPRESSION | |
3647 | if (f2fs_compressed_file(inode)) { | |
3648 | int ret; | |
3649 | ||
3650 | *fsdata = NULL; | |
3651 | ||
9b56adcf | 3652 | if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode))) |
7eab7a69 FC |
3653 | goto repeat; |
3654 | ||
4c8ff709 CY |
3655 | ret = f2fs_prepare_compress_overwrite(inode, pagep, |
3656 | index, fsdata); | |
3657 | if (ret < 0) { | |
3658 | err = ret; | |
3659 | goto fail; | |
3660 | } else if (ret) { | |
3661 | return 0; | |
3662 | } | |
3663 | } | |
3664 | #endif | |
3665 | ||
afcb7ca0 | 3666 | repeat: |
86d54795 JK |
3667 | /* |
3668 | * Do not use grab_cache_page_write_begin() to avoid deadlock due to | |
3669 | * wait_for_stable_page. Will wait that below with our IO control. | |
3670 | */ | |
01eccef7 | 3671 | page = f2fs_pagecache_get_page(mapping, index, |
86d54795 | 3672 | FGP_LOCK | FGP_WRITE | FGP_CREAT, GFP_NOFS); |
3aab8f82 CY |
3673 | if (!page) { |
3674 | err = -ENOMEM; | |
3675 | goto fail; | |
3676 | } | |
d5f66990 | 3677 | |
4c8ff709 CY |
3678 | /* TODO: cluster can be compressed due to race with .writepage */ |
3679 | ||
eb47b800 JK |
3680 | *pagep = page; |
3681 | ||
3db1de0e DJ |
3682 | if (f2fs_is_atomic_file(inode)) |
3683 | err = prepare_atomic_write_begin(sbi, page, pos, len, | |
591fc34e | 3684 | &blkaddr, &need_balance, &use_cow); |
3db1de0e DJ |
3685 | else |
3686 | err = prepare_write_begin(sbi, page, pos, len, | |
2aadac08 | 3687 | &blkaddr, &need_balance); |
9ba69cf9 | 3688 | if (err) |
2aadac08 | 3689 | goto fail; |
9ba69cf9 | 3690 | |
af033b2a CY |
3691 | if (need_balance && !IS_NOQUOTA(inode) && |
3692 | has_not_enough_free_secs(sbi, 0, 0)) { | |
2a340760 | 3693 | unlock_page(page); |
2c4db1a6 | 3694 | f2fs_balance_fs(sbi, true); |
2a340760 JK |
3695 | lock_page(page); |
3696 | if (page->mapping != mapping) { | |
3697 | /* The page got truncated from under us */ | |
3698 | f2fs_put_page(page, 1); | |
3699 | goto repeat; | |
3700 | } | |
3701 | } | |
3702 | ||
bae0ee7a | 3703 | f2fs_wait_on_page_writeback(page, DATA, false, true); |
b3d208f9 | 3704 | |
649d7df2 JK |
3705 | if (len == PAGE_SIZE || PageUptodate(page)) |
3706 | return 0; | |
eb47b800 | 3707 | |
95ae251f EB |
3708 | if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) && |
3709 | !f2fs_verity_in_progress(inode)) { | |
746e2403 YH |
3710 | zero_user_segment(page, len, PAGE_SIZE); |
3711 | return 0; | |
3712 | } | |
3713 | ||
2aadac08 | 3714 | if (blkaddr == NEW_ADDR) { |
09cbfeaf | 3715 | zero_user_segment(page, 0, PAGE_SIZE); |
649d7df2 | 3716 | SetPageUptodate(page); |
eb47b800 | 3717 | } else { |
93770ab7 CY |
3718 | if (!f2fs_is_valid_blkaddr(sbi, blkaddr, |
3719 | DATA_GENERIC_ENHANCE_READ)) { | |
10f966bb | 3720 | err = -EFSCORRUPTED; |
95fa90c9 | 3721 | f2fs_handle_error(sbi, ERROR_INVALID_BLKADDR); |
93770ab7 CY |
3722 | goto fail; |
3723 | } | |
591fc34e DJ |
3724 | err = f2fs_submit_page_read(use_cow ? |
3725 | F2FS_I(inode)->cow_inode : inode, page, | |
3726 | blkaddr, 0, true); | |
13ba41e3 | 3727 | if (err) |
78682f79 | 3728 | goto fail; |
d54c795b | 3729 | |
393ff91f | 3730 | lock_page(page); |
6bacf52f | 3731 | if (unlikely(page->mapping != mapping)) { |
afcb7ca0 JK |
3732 | f2fs_put_page(page, 1); |
3733 | goto repeat; | |
eb47b800 | 3734 | } |
1563ac75 CY |
3735 | if (unlikely(!PageUptodate(page))) { |
3736 | err = -EIO; | |
3737 | goto fail; | |
4375a336 | 3738 | } |
eb47b800 | 3739 | } |
eb47b800 | 3740 | return 0; |
9ba69cf9 | 3741 | |
3aab8f82 | 3742 | fail: |
86531d6b | 3743 | f2fs_put_page(page, 1); |
3e679dc7 | 3744 | f2fs_write_failed(inode, pos + len); |
3aab8f82 | 3745 | return err; |
eb47b800 JK |
3746 | } |
3747 | ||
a1dd3c13 JK |
3748 | static int f2fs_write_end(struct file *file, |
3749 | struct address_space *mapping, | |
3750 | loff_t pos, unsigned len, unsigned copied, | |
3751 | struct page *page, void *fsdata) | |
3752 | { | |
3753 | struct inode *inode = page->mapping->host; | |
3754 | ||
dfb2bf38 CY |
3755 | trace_f2fs_write_end(inode, pos, len, copied); |
3756 | ||
649d7df2 JK |
3757 | /* |
3758 | * This should be come from len == PAGE_SIZE, and we expect copied | |
3759 | * should be PAGE_SIZE. Otherwise, we treat it with zero copied and | |
3760 | * let generic_perform_write() try to copy data again through copied=0. | |
3761 | */ | |
3762 | if (!PageUptodate(page)) { | |
746e2403 | 3763 | if (unlikely(copied != len)) |
649d7df2 JK |
3764 | copied = 0; |
3765 | else | |
3766 | SetPageUptodate(page); | |
3767 | } | |
4c8ff709 CY |
3768 | |
3769 | #ifdef CONFIG_F2FS_FS_COMPRESSION | |
3770 | /* overwrite compressed file */ | |
3771 | if (f2fs_compressed_file(inode) && fsdata) { | |
3772 | f2fs_compress_write_end(inode, fsdata, page->index, copied); | |
3773 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); | |
944dd22e CY |
3774 | |
3775 | if (pos + copied > i_size_read(inode) && | |
3776 | !f2fs_verity_in_progress(inode)) | |
3777 | f2fs_i_size_write(inode, pos + copied); | |
4c8ff709 CY |
3778 | return copied; |
3779 | } | |
3780 | #endif | |
3781 | ||
649d7df2 JK |
3782 | if (!copied) |
3783 | goto unlock_out; | |
3784 | ||
34ba94ba | 3785 | set_page_dirty(page); |
a1dd3c13 | 3786 | |
95ae251f | 3787 | if (pos + copied > i_size_read(inode) && |
3db1de0e | 3788 | !f2fs_verity_in_progress(inode)) { |
fc9581c8 | 3789 | f2fs_i_size_write(inode, pos + copied); |
3db1de0e DJ |
3790 | if (f2fs_is_atomic_file(inode)) |
3791 | f2fs_i_size_write(F2FS_I(inode)->cow_inode, | |
3792 | pos + copied); | |
3793 | } | |
649d7df2 | 3794 | unlock_out: |
3024c9a1 | 3795 | f2fs_put_page(page, 1); |
d0239e1b | 3796 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
a1dd3c13 JK |
3797 | return copied; |
3798 | } | |
3799 | ||
91503996 | 3800 | void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length) |
eb47b800 | 3801 | { |
91503996 | 3802 | struct inode *inode = folio->mapping->host; |
487261f3 | 3803 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
a7ffdbe2 | 3804 | |
487261f3 | 3805 | if (inode->i_ino >= F2FS_ROOT_INO(sbi) && |
91503996 | 3806 | (offset || length != folio_size(folio))) |
a7ffdbe2 JK |
3807 | return; |
3808 | ||
91503996 | 3809 | if (folio_test_dirty(folio)) { |
933439c8 | 3810 | if (inode->i_ino == F2FS_META_INO(sbi)) { |
487261f3 | 3811 | dec_page_count(sbi, F2FS_DIRTY_META); |
933439c8 | 3812 | } else if (inode->i_ino == F2FS_NODE_INO(sbi)) { |
487261f3 | 3813 | dec_page_count(sbi, F2FS_DIRTY_NODES); |
933439c8 | 3814 | } else { |
487261f3 | 3815 | inode_dec_dirty_pages(inode); |
4d57b86d | 3816 | f2fs_remove_dirty_inode(inode); |
933439c8 | 3817 | } |
487261f3 | 3818 | } |
635a52da | 3819 | clear_page_private_all(&folio->page); |
eb47b800 JK |
3820 | } |
3821 | ||
c26cd045 | 3822 | bool f2fs_release_folio(struct folio *folio, gfp_t wait) |
eb47b800 | 3823 | { |
c26cd045 MWO |
3824 | /* If this is dirty folio, keep private data */ |
3825 | if (folio_test_dirty(folio)) | |
3826 | return false; | |
f68daeeb | 3827 | |
635a52da | 3828 | clear_page_private_all(&folio->page); |
c26cd045 | 3829 | return true; |
eb47b800 JK |
3830 | } |
3831 | ||
4f5e34f7 MWO |
3832 | static bool f2fs_dirty_data_folio(struct address_space *mapping, |
3833 | struct folio *folio) | |
eb47b800 | 3834 | { |
4f5e34f7 | 3835 | struct inode *inode = mapping->host; |
eb47b800 | 3836 | |
4f5e34f7 | 3837 | trace_f2fs_set_page_dirty(&folio->page, DATA); |
26c6b887 | 3838 | |
4f5e34f7 MWO |
3839 | if (!folio_test_uptodate(folio)) |
3840 | folio_mark_uptodate(folio); | |
3841 | BUG_ON(folio_test_swapcache(folio)); | |
34ba94ba | 3842 | |
9b7eadd9 | 3843 | if (filemap_dirty_folio(mapping, folio)) { |
4f5e34f7 MWO |
3844 | f2fs_update_dirty_folio(inode, folio); |
3845 | return true; | |
eb47b800 | 3846 | } |
0fb5b2eb | 3847 | return false; |
eb47b800 JK |
3848 | } |
3849 | ||
c1c63387 CY |
3850 | |
3851 | static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block) | |
3852 | { | |
3853 | #ifdef CONFIG_F2FS_FS_COMPRESSION | |
3854 | struct dnode_of_data dn; | |
3855 | sector_t start_idx, blknr = 0; | |
3856 | int ret; | |
3857 | ||
3858 | start_idx = round_down(block, F2FS_I(inode)->i_cluster_size); | |
3859 | ||
3860 | set_new_dnode(&dn, inode, NULL, NULL, 0); | |
3861 | ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); | |
3862 | if (ret) | |
3863 | return 0; | |
3864 | ||
3865 | if (dn.data_blkaddr != COMPRESS_ADDR) { | |
3866 | dn.ofs_in_node += block - start_idx; | |
3867 | blknr = f2fs_data_blkaddr(&dn); | |
3868 | if (!__is_valid_data_blkaddr(blknr)) | |
3869 | blknr = 0; | |
3870 | } | |
3871 | ||
3872 | f2fs_put_dnode(&dn); | |
c1c63387 CY |
3873 | return blknr; |
3874 | #else | |
250e84d7 | 3875 | return 0; |
c1c63387 CY |
3876 | #endif |
3877 | } | |
3878 | ||
3879 | ||
c01e54b7 JK |
3880 | static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
3881 | { | |
454ae7e5 | 3882 | struct inode *inode = mapping->host; |
b79b0a31 | 3883 | sector_t blknr = 0; |
454ae7e5 | 3884 | |
1d373a0e | 3885 | if (f2fs_has_inline_data(inode)) |
b79b0a31 | 3886 | goto out; |
1d373a0e JK |
3887 | |
3888 | /* make sure allocating whole blocks */ | |
3889 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) | |
3890 | filemap_write_and_wait(mapping); | |
3891 | ||
4eda1682 | 3892 | /* Block number less than F2FS MAX BLOCKS */ |
6d1451bf | 3893 | if (unlikely(block >= max_file_blocks(inode))) |
4eda1682 | 3894 | goto out; |
c1c63387 | 3895 | |
4eda1682 DJ |
3896 | if (f2fs_compressed_file(inode)) { |
3897 | blknr = f2fs_bmap_compress(inode, block); | |
3898 | } else { | |
b876f4c9 JK |
3899 | struct f2fs_map_blocks map; |
3900 | ||
3901 | memset(&map, 0, sizeof(map)); | |
3902 | map.m_lblk = block; | |
3903 | map.m_len = 1; | |
3904 | map.m_next_pgofs = NULL; | |
3905 | map.m_seg_type = NO_CHECK_TYPE; | |
3906 | ||
cd8fc522 | 3907 | if (!f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_BMAP)) |
b876f4c9 | 3908 | blknr = map.m_pblk; |
4eda1682 | 3909 | } |
b79b0a31 CY |
3910 | out: |
3911 | trace_f2fs_bmap(inode, block, blknr); | |
3912 | return blknr; | |
429511cd CY |
3913 | } |
3914 | ||
4969c06a | 3915 | #ifdef CONFIG_SWAP |
859fca6b CY |
3916 | static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk, |
3917 | unsigned int blkcnt) | |
3918 | { | |
3919 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); | |
3920 | unsigned int blkofs; | |
3921 | unsigned int blk_per_sec = BLKS_PER_SEC(sbi); | |
3922 | unsigned int secidx = start_blk / blk_per_sec; | |
3923 | unsigned int end_sec = secidx + blkcnt / blk_per_sec; | |
3924 | int ret = 0; | |
3925 | ||
e4544b63 | 3926 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
edc6d01b | 3927 | filemap_invalidate_lock(inode->i_mapping); |
859fca6b CY |
3928 | |
3929 | set_inode_flag(inode, FI_ALIGNED_WRITE); | |
1018a546 | 3930 | set_inode_flag(inode, FI_OPU_WRITE); |
859fca6b CY |
3931 | |
3932 | for (; secidx < end_sec; secidx++) { | |
e4544b63 | 3933 | f2fs_down_write(&sbi->pin_sem); |
859fca6b CY |
3934 | |
3935 | f2fs_lock_op(sbi); | |
3936 | f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false); | |
3937 | f2fs_unlock_op(sbi); | |
3938 | ||
1018a546 | 3939 | set_inode_flag(inode, FI_SKIP_WRITES); |
859fca6b CY |
3940 | |
3941 | for (blkofs = 0; blkofs < blk_per_sec; blkofs++) { | |
3942 | struct page *page; | |
3943 | unsigned int blkidx = secidx * blk_per_sec + blkofs; | |
3944 | ||
3945 | page = f2fs_get_lock_data_page(inode, blkidx, true); | |
3946 | if (IS_ERR(page)) { | |
e4544b63 | 3947 | f2fs_up_write(&sbi->pin_sem); |
859fca6b CY |
3948 | ret = PTR_ERR(page); |
3949 | goto done; | |
3950 | } | |
3951 | ||
3952 | set_page_dirty(page); | |
3953 | f2fs_put_page(page, 1); | |
3954 | } | |
3955 | ||
1018a546 | 3956 | clear_inode_flag(inode, FI_SKIP_WRITES); |
859fca6b CY |
3957 | |
3958 | ret = filemap_fdatawrite(inode->i_mapping); | |
3959 | ||
e4544b63 | 3960 | f2fs_up_write(&sbi->pin_sem); |
859fca6b CY |
3961 | |
3962 | if (ret) | |
3963 | break; | |
3964 | } | |
3965 | ||
3966 | done: | |
1018a546 CY |
3967 | clear_inode_flag(inode, FI_SKIP_WRITES); |
3968 | clear_inode_flag(inode, FI_OPU_WRITE); | |
859fca6b CY |
3969 | clear_inode_flag(inode, FI_ALIGNED_WRITE); |
3970 | ||
edc6d01b | 3971 | filemap_invalidate_unlock(inode->i_mapping); |
e4544b63 | 3972 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
859fca6b CY |
3973 | |
3974 | return ret; | |
3975 | } | |
3976 | ||
0b8fc006 | 3977 | static int check_swap_activate(struct swap_info_struct *sis, |
af4b6b8e CY |
3978 | struct file *swap_file, sector_t *span) |
3979 | { | |
3980 | struct address_space *mapping = swap_file->f_mapping; | |
3981 | struct inode *inode = mapping->host; | |
36e4d958 | 3982 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
af4b6b8e CY |
3983 | sector_t cur_lblock; |
3984 | sector_t last_lblock; | |
3985 | sector_t pblock; | |
3986 | sector_t lowest_pblock = -1; | |
3987 | sector_t highest_pblock = 0; | |
3988 | int nr_extents = 0; | |
3989 | unsigned long nr_pblocks; | |
859fca6b CY |
3990 | unsigned int blks_per_sec = BLKS_PER_SEC(sbi); |
3991 | unsigned int sec_blks_mask = BLKS_PER_SEC(sbi) - 1; | |
ca298241 | 3992 | unsigned int not_aligned = 0; |
36e4d958 | 3993 | int ret = 0; |
af4b6b8e CY |
3994 | |
3995 | /* | |
3996 | * Map all the blocks into the extent list. This code doesn't try | |
3997 | * to be very smart. | |
3998 | */ | |
3999 | cur_lblock = 0; | |
6cbfcab5 | 4000 | last_lblock = bytes_to_blks(inode, i_size_read(inode)); |
af4b6b8e | 4001 | |
1da66103 | 4002 | while (cur_lblock < last_lblock && cur_lblock < sis->max) { |
b876f4c9 | 4003 | struct f2fs_map_blocks map; |
859fca6b | 4004 | retry: |
af4b6b8e CY |
4005 | cond_resched(); |
4006 | ||
b876f4c9 JK |
4007 | memset(&map, 0, sizeof(map)); |
4008 | map.m_lblk = cur_lblock; | |
36e4d958 | 4009 | map.m_len = last_lblock - cur_lblock; |
4010 | map.m_next_pgofs = NULL; | |
4011 | map.m_next_extent = NULL; | |
b876f4c9 | 4012 | map.m_seg_type = NO_CHECK_TYPE; |
36e4d958 | 4013 | map.m_may_create = false; |
af4b6b8e | 4014 | |
cd8fc522 | 4015 | ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP); |
af4b6b8e | 4016 | if (ret) |
36e4d958 | 4017 | goto out; |
af4b6b8e CY |
4018 | |
4019 | /* hole */ | |
36e4d958 | 4020 | if (!(map.m_flags & F2FS_MAP_FLAGS)) { |
833dcd35 | 4021 | f2fs_err(sbi, "Swapfile has holes"); |
f395183f | 4022 | ret = -EINVAL; |
36e4d958 | 4023 | goto out; |
4024 | } | |
af4b6b8e | 4025 | |
b876f4c9 JK |
4026 | pblock = map.m_pblk; |
4027 | nr_pblocks = map.m_len; | |
af4b6b8e | 4028 | |
859fca6b CY |
4029 | if ((pblock - SM_I(sbi)->main_blkaddr) & sec_blks_mask || |
4030 | nr_pblocks & sec_blks_mask) { | |
ca298241 | 4031 | not_aligned++; |
36e4d958 | 4032 | |
859fca6b CY |
4033 | nr_pblocks = roundup(nr_pblocks, blks_per_sec); |
4034 | if (cur_lblock + nr_pblocks > sis->max) | |
4035 | nr_pblocks -= blks_per_sec; | |
4036 | ||
4037 | if (!nr_pblocks) { | |
4038 | /* this extent is last one */ | |
4039 | nr_pblocks = map.m_len; | |
4040 | f2fs_warn(sbi, "Swapfile: last extent is not aligned to section"); | |
4041 | goto next; | |
4042 | } | |
4043 | ||
4044 | ret = f2fs_migrate_blocks(inode, cur_lblock, | |
4045 | nr_pblocks); | |
4046 | if (ret) | |
4047 | goto out; | |
4048 | goto retry; | |
4049 | } | |
4050 | next: | |
af4b6b8e CY |
4051 | if (cur_lblock + nr_pblocks >= sis->max) |
4052 | nr_pblocks = sis->max - cur_lblock; | |
4053 | ||
4054 | if (cur_lblock) { /* exclude the header page */ | |
4055 | if (pblock < lowest_pblock) | |
4056 | lowest_pblock = pblock; | |
4057 | if (pblock + nr_pblocks - 1 > highest_pblock) | |
4058 | highest_pblock = pblock + nr_pblocks - 1; | |
4059 | } | |
4060 | ||
4061 | /* | |
4062 | * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks | |
4063 | */ | |
4064 | ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock); | |
4065 | if (ret < 0) | |
4066 | goto out; | |
4067 | nr_extents += ret; | |
4068 | cur_lblock += nr_pblocks; | |
4069 | } | |
4070 | ret = nr_extents; | |
4071 | *span = 1 + highest_pblock - lowest_pblock; | |
4072 | if (cur_lblock == 0) | |
4073 | cur_lblock = 1; /* force Empty message */ | |
4074 | sis->max = cur_lblock; | |
4075 | sis->pages = cur_lblock - 1; | |
4076 | sis->highest_bit = cur_lblock - 1; | |
4077 | out: | |
859fca6b | 4078 | if (not_aligned) |
d7e9a903 | 4079 | f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%lu * N)", |
859fca6b | 4080 | not_aligned, blks_per_sec * F2FS_BLKSIZE); |
af4b6b8e | 4081 | return ret; |
af4b6b8e CY |
4082 | } |
4083 | ||
4969c06a JK |
4084 | static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, |
4085 | sector_t *span) | |
4086 | { | |
4087 | struct inode *inode = file_inode(file); | |
4088 | int ret; | |
4089 | ||
4090 | if (!S_ISREG(inode->i_mode)) | |
4091 | return -EINVAL; | |
4092 | ||
4093 | if (f2fs_readonly(F2FS_I_SB(inode)->sb)) | |
4094 | return -EROFS; | |
4095 | ||
d927ccfc SK |
4096 | if (f2fs_lfs_mode(F2FS_I_SB(inode))) { |
4097 | f2fs_err(F2FS_I_SB(inode), | |
4098 | "Swapfile not supported in LFS mode"); | |
4099 | return -EINVAL; | |
4100 | } | |
4101 | ||
4969c06a JK |
4102 | ret = f2fs_convert_inline_inode(inode); |
4103 | if (ret) | |
4104 | return ret; | |
4105 | ||
78134d03 | 4106 | if (!f2fs_disable_compressed_file(inode)) |
4c8ff709 CY |
4107 | return -EINVAL; |
4108 | ||
0b979f1b CY |
4109 | f2fs_precache_extents(inode); |
4110 | ||
3e5e479a CY |
4111 | ret = check_swap_activate(sis, file, span); |
4112 | if (ret < 0) | |
4969c06a JK |
4113 | return ret; |
4114 | ||
8ec071c3 | 4115 | stat_inc_swapfile_inode(inode); |
4969c06a | 4116 | set_inode_flag(inode, FI_PIN_FILE); |
4969c06a | 4117 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
3e5e479a | 4118 | return ret; |
4969c06a JK |
4119 | } |
4120 | ||
4121 | static void f2fs_swap_deactivate(struct file *file) | |
4122 | { | |
4123 | struct inode *inode = file_inode(file); | |
4124 | ||
8ec071c3 | 4125 | stat_dec_swapfile_inode(inode); |
4969c06a JK |
4126 | clear_inode_flag(inode, FI_PIN_FILE); |
4127 | } | |
4128 | #else | |
4129 | static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, | |
4130 | sector_t *span) | |
4131 | { | |
4132 | return -EOPNOTSUPP; | |
4133 | } | |
4134 | ||
4135 | static void f2fs_swap_deactivate(struct file *file) | |
4136 | { | |
4137 | } | |
4138 | #endif | |
4139 | ||
eb47b800 | 4140 | const struct address_space_operations f2fs_dblock_aops = { |
be05584f | 4141 | .read_folio = f2fs_read_data_folio, |
23323196 | 4142 | .readahead = f2fs_readahead, |
eb47b800 JK |
4143 | .writepage = f2fs_write_data_page, |
4144 | .writepages = f2fs_write_data_pages, | |
4145 | .write_begin = f2fs_write_begin, | |
a1dd3c13 | 4146 | .write_end = f2fs_write_end, |
4f5e34f7 | 4147 | .dirty_folio = f2fs_dirty_data_folio, |
1d5b9bd6 | 4148 | .migrate_folio = filemap_migrate_folio, |
91503996 | 4149 | .invalidate_folio = f2fs_invalidate_folio, |
c26cd045 | 4150 | .release_folio = f2fs_release_folio, |
c01e54b7 | 4151 | .bmap = f2fs_bmap, |
4969c06a JK |
4152 | .swap_activate = f2fs_swap_activate, |
4153 | .swap_deactivate = f2fs_swap_deactivate, | |
eb47b800 | 4154 | }; |
6dbb1796 | 4155 | |
5ec2d99d | 4156 | void f2fs_clear_page_cache_dirty_tag(struct page *page) |
aec2f729 CY |
4157 | { |
4158 | struct address_space *mapping = page_mapping(page); | |
4159 | unsigned long flags; | |
4160 | ||
4161 | xa_lock_irqsave(&mapping->i_pages, flags); | |
5ec2d99d | 4162 | __xa_clear_mark(&mapping->i_pages, page_index(page), |
aec2f729 CY |
4163 | PAGECACHE_TAG_DIRTY); |
4164 | xa_unlock_irqrestore(&mapping->i_pages, flags); | |
4165 | } | |
4166 | ||
6dbb1796 EB |
4167 | int __init f2fs_init_post_read_processing(void) |
4168 | { | |
95ae251f EB |
4169 | bio_post_read_ctx_cache = |
4170 | kmem_cache_create("f2fs_bio_post_read_ctx", | |
4171 | sizeof(struct bio_post_read_ctx), 0, 0, NULL); | |
6dbb1796 EB |
4172 | if (!bio_post_read_ctx_cache) |
4173 | goto fail; | |
4174 | bio_post_read_ctx_pool = | |
4175 | mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS, | |
4176 | bio_post_read_ctx_cache); | |
4177 | if (!bio_post_read_ctx_pool) | |
4178 | goto fail_free_cache; | |
4179 | return 0; | |
4180 | ||
4181 | fail_free_cache: | |
4182 | kmem_cache_destroy(bio_post_read_ctx_cache); | |
4183 | fail: | |
4184 | return -ENOMEM; | |
4185 | } | |
4186 | ||
0b20fcec | 4187 | void f2fs_destroy_post_read_processing(void) |
6dbb1796 EB |
4188 | { |
4189 | mempool_destroy(bio_post_read_ctx_pool); | |
4190 | kmem_cache_destroy(bio_post_read_ctx_cache); | |
4191 | } | |
0b20fcec | 4192 | |
4c8ff709 CY |
4193 | int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi) |
4194 | { | |
4195 | if (!f2fs_sb_has_encrypt(sbi) && | |
4196 | !f2fs_sb_has_verity(sbi) && | |
4197 | !f2fs_sb_has_compression(sbi)) | |
4198 | return 0; | |
4199 | ||
4200 | sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq", | |
4201 | WQ_UNBOUND | WQ_HIGHPRI, | |
4202 | num_online_cpus()); | |
870af777 | 4203 | return sbi->post_read_wq ? 0 : -ENOMEM; |
4c8ff709 CY |
4204 | } |
4205 | ||
4206 | void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi) | |
4207 | { | |
4208 | if (sbi->post_read_wq) | |
4209 | destroy_workqueue(sbi->post_read_wq); | |
4210 | } | |
4211 | ||
0b20fcec CY |
4212 | int __init f2fs_init_bio_entry_cache(void) |
4213 | { | |
98510003 | 4214 | bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab", |
0b20fcec | 4215 | sizeof(struct bio_entry)); |
870af777 | 4216 | return bio_entry_slab ? 0 : -ENOMEM; |
0b20fcec CY |
4217 | } |
4218 | ||
f543805f | 4219 | void f2fs_destroy_bio_entry_cache(void) |
0b20fcec CY |
4220 | { |
4221 | kmem_cache_destroy(bio_entry_slab); | |
4222 | } | |
1517c1a7 EB |
4223 | |
4224 | static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, | |
4225 | unsigned int flags, struct iomap *iomap, | |
4226 | struct iomap *srcmap) | |
4227 | { | |
4228 | struct f2fs_map_blocks map = {}; | |
4229 | pgoff_t next_pgofs = 0; | |
4230 | int err; | |
4231 | ||
4232 | map.m_lblk = bytes_to_blks(inode, offset); | |
4233 | map.m_len = bytes_to_blks(inode, offset + length - 1) - map.m_lblk + 1; | |
4234 | map.m_next_pgofs = &next_pgofs; | |
4235 | map.m_seg_type = f2fs_rw_hint_to_seg_type(inode->i_write_hint); | |
4236 | if (flags & IOMAP_WRITE) | |
4237 | map.m_may_create = true; | |
4238 | ||
cd8fc522 | 4239 | err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DIO); |
1517c1a7 EB |
4240 | if (err) |
4241 | return err; | |
4242 | ||
4243 | iomap->offset = blks_to_bytes(inode, map.m_lblk); | |
4244 | ||
8a2c77bc EB |
4245 | /* |
4246 | * When inline encryption is enabled, sometimes I/O to an encrypted file | |
4247 | * has to be broken up to guarantee DUN contiguity. Handle this by | |
4248 | * limiting the length of the mapping returned. | |
4249 | */ | |
4250 | map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len); | |
4251 | ||
8d3c1fa3 CH |
4252 | /* |
4253 | * We should never see delalloc or compressed extents here based on | |
4254 | * prior flushing and checks. | |
4255 | */ | |
4256 | if (WARN_ON_ONCE(map.m_pblk == NEW_ADDR)) | |
4257 | return -EINVAL; | |
4258 | if (WARN_ON_ONCE(map.m_pblk == COMPRESS_ADDR)) | |
4259 | return -EINVAL; | |
1517c1a7 | 4260 | |
8d3c1fa3 CH |
4261 | if (map.m_pblk != NULL_ADDR) { |
4262 | iomap->length = blks_to_bytes(inode, map.m_len); | |
4263 | iomap->type = IOMAP_MAPPED; | |
4264 | iomap->flags |= IOMAP_F_MERGED; | |
1517c1a7 EB |
4265 | iomap->bdev = map.m_bdev; |
4266 | iomap->addr = blks_to_bytes(inode, map.m_pblk); | |
4267 | } else { | |
8d3c1fa3 CH |
4268 | if (flags & IOMAP_WRITE) |
4269 | return -ENOTBLK; | |
1517c1a7 EB |
4270 | iomap->length = blks_to_bytes(inode, next_pgofs) - |
4271 | iomap->offset; | |
4272 | iomap->type = IOMAP_HOLE; | |
4273 | iomap->addr = IOMAP_NULL_ADDR; | |
4274 | } | |
4275 | ||
4276 | if (map.m_flags & F2FS_MAP_NEW) | |
4277 | iomap->flags |= IOMAP_F_NEW; | |
4278 | if ((inode->i_state & I_DIRTY_DATASYNC) || | |
4279 | offset + length > i_size_read(inode)) | |
4280 | iomap->flags |= IOMAP_F_DIRTY; | |
4281 | ||
4282 | return 0; | |
4283 | } | |
4284 | ||
4285 | const struct iomap_ops f2fs_iomap_ops = { | |
4286 | .iomap_begin = f2fs_iomap_begin, | |
4287 | }; |