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