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