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