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