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