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