| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* fs/f2fs/data.c |
| 4 |
* |
| 5 |
* Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 6 |
* http://www.samsung.com/ |
| 7 |
*/ |
| 8 |
#include <linux/fs.h> |
| 9 |
#include <linux/f2fs_fs.h> |
| 10 |
#include <linux/sched/mm.h> |
| 11 |
#include <linux/mpage.h> |
| 12 |
#include <linux/writeback.h> |
| 13 |
#include <linux/pagevec.h> |
| 14 |
#include <linux/blkdev.h> |
| 15 |
#include <linux/bio.h> |
| 16 |
#include <linux/blk-crypto.h> |
| 17 |
#include <linux/swap.h> |
| 18 |
#include <linux/prefetch.h> |
| 19 |
#include <linux/uio.h> |
| 20 |
#include <linux/sched/signal.h> |
| 21 |
#include <linux/fiemap.h> |
| 22 |
#include <linux/iomap.h> |
| 23 |
|
| 24 |
#include "f2fs.h" |
| 25 |
#include "node.h" |
| 26 |
#include "segment.h" |
| 27 |
#include "iostat.h" |
| 28 |
#include <trace/events/f2fs.h> |
| 29 |
|
| 30 |
#define NUM_PREALLOC_POST_READ_CTXS 128 |
| 31 |
|
| 32 |
static struct kmem_cache *bio_post_read_ctx_cache; |
| 33 |
static struct kmem_cache *bio_entry_slab; |
| 34 |
static struct kmem_cache *ffs_entry_slab; |
| 35 |
static mempool_t *bio_post_read_ctx_pool; |
| 36 |
static struct bio_set f2fs_bioset; |
| 37 |
|
| 38 |
struct f2fs_folio_state { |
| 39 |
spinlock_t state_lock; |
| 40 |
unsigned int read_pages_pending; |
| 41 |
}; |
| 42 |
|
| 43 |
#define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE |
| 44 |
|
| 45 |
int __init f2fs_init_bioset(void) |
| 46 |
{ |
| 47 |
return bioset_init(&f2fs_bioset, F2FS_BIO_POOL_SIZE, |
| 48 |
0, BIOSET_NEED_BVECS); |
| 49 |
} |
| 50 |
|
| 51 |
void f2fs_destroy_bioset(void) |
| 52 |
{ |
| 53 |
bioset_exit(&f2fs_bioset); |
| 54 |
} |
| 55 |
|
| 56 |
bool f2fs_is_cp_guaranteed(const struct folio *folio) |
| 57 |
{ |
| 58 |
struct address_space *mapping = folio->mapping; |
| 59 |
struct inode *inode; |
| 60 |
struct f2fs_sb_info *sbi; |
| 61 |
|
| 62 |
if (fscrypt_is_bounce_folio(folio)) |
| 63 |
return folio_test_f2fs_gcing(fscrypt_pagecache_folio(folio)); |
| 64 |
|
| 65 |
inode = mapping->host; |
| 66 |
sbi = F2FS_I_SB(inode); |
| 67 |
|
| 68 |
if (inode->i_ino == F2FS_META_INO(sbi) || |
| 69 |
inode->i_ino == F2FS_NODE_INO(sbi) || |
| 70 |
S_ISDIR(inode->i_mode)) |
| 71 |
return true; |
| 72 |
|
| 73 |
if ((S_ISREG(inode->i_mode) && IS_NOQUOTA(inode)) || |
| 74 |
folio_test_f2fs_gcing(folio)) |
| 75 |
return true; |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
static enum count_type __read_io_type(struct folio *folio) |
| 80 |
{ |
| 81 |
struct address_space *mapping = folio->mapping; |
| 82 |
|
| 83 |
if (mapping) { |
| 84 |
struct inode *inode = mapping->host; |
| 85 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 86 |
|
| 87 |
if (inode->i_ino == F2FS_META_INO(sbi)) |
| 88 |
return F2FS_RD_META; |
| 89 |
|
| 90 |
if (inode->i_ino == F2FS_NODE_INO(sbi)) |
| 91 |
return F2FS_RD_NODE; |
| 92 |
} |
| 93 |
return F2FS_RD_DATA; |
| 94 |
} |
| 95 |
|
| 96 |
/* postprocessing steps for read bios */ |
| 97 |
enum bio_post_read_step { |
| 98 |
#ifdef CONFIG_FS_ENCRYPTION |
| 99 |
STEP_DECRYPT = BIT(0), |
| 100 |
#else |
| 101 |
STEP_DECRYPT = 0, /* compile out the decryption-related code */ |
| 102 |
#endif |
| 103 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 104 |
STEP_DECOMPRESS = BIT(1), |
| 105 |
#else |
| 106 |
STEP_DECOMPRESS = 0, /* compile out the decompression-related code */ |
| 107 |
#endif |
| 108 |
#ifdef CONFIG_FS_VERITY |
| 109 |
STEP_VERITY = BIT(2), |
| 110 |
#else |
| 111 |
STEP_VERITY = 0, /* compile out the verity-related code */ |
| 112 |
#endif |
| 113 |
}; |
| 114 |
|
| 115 |
struct bio_post_read_ctx { |
| 116 |
struct bio *bio; |
| 117 |
struct f2fs_sb_info *sbi; |
| 118 |
struct fsverity_info *vi; |
| 119 |
struct work_struct work; |
| 120 |
unsigned int enabled_steps; |
| 121 |
/* |
| 122 |
* decompression_attempted keeps track of whether |
| 123 |
* f2fs_end_read_compressed_page() has been called on the pages in the |
| 124 |
* bio that belong to a compressed cluster yet. |
| 125 |
*/ |
| 126 |
bool decompression_attempted; |
| 127 |
block_t fs_blkaddr; |
| 128 |
}; |
| 129 |
|
| 130 |
/* |
| 131 |
* Update and unlock a bio's pages, and free the bio. |
| 132 |
* |
| 133 |
* This marks pages up-to-date only if there was no error in the bio (I/O error, |
| 134 |
* decryption error, or verity error), as indicated by bio->bi_status. |
| 135 |
* |
| 136 |
* "Compressed pages" (pagecache pages backed by a compressed cluster on-disk) |
| 137 |
* aren't marked up-to-date here, as decompression is done on a per-compression- |
| 138 |
* cluster basis rather than a per-bio basis. Instead, we only must do two |
| 139 |
* things for each compressed page here: call f2fs_end_read_compressed_page() |
| 140 |
* with failed=true if an error occurred before it would have normally gotten |
| 141 |
* called (i.e., I/O error or decryption error, but *not* verity error), and |
| 142 |
* release the bio's reference to the decompress_io_ctx of the page's cluster. |
| 143 |
*/ |
| 144 |
static void f2fs_finish_read_bio(struct bio *bio, bool in_task) |
| 145 |
{ |
| 146 |
struct folio_iter fi; |
| 147 |
struct bio_post_read_ctx *ctx = bio->bi_private; |
| 148 |
unsigned long flags; |
| 149 |
|
| 150 |
bio_for_each_folio_all(fi, bio) { |
| 151 |
struct folio *folio = fi.folio; |
| 152 |
unsigned nr_pages = fi.length >> PAGE_SHIFT; |
| 153 |
bool finished = true; |
| 154 |
|
| 155 |
if (!folio_test_large(folio) && |
| 156 |
f2fs_is_compressed_page(folio)) { |
| 157 |
if (ctx && !ctx->decompression_attempted) |
| 158 |
f2fs_end_read_compressed_page(folio, true, 0, |
| 159 |
in_task); |
| 160 |
f2fs_put_folio_dic(folio, in_task); |
| 161 |
continue; |
| 162 |
} |
| 163 |
|
| 164 |
if (folio_test_large(folio)) { |
| 165 |
struct f2fs_folio_state *ffs = folio->private; |
| 166 |
|
| 167 |
spin_lock_irqsave(&ffs->state_lock, flags); |
| 168 |
ffs->read_pages_pending -= nr_pages; |
| 169 |
finished = !ffs->read_pages_pending; |
| 170 |
spin_unlock_irqrestore(&ffs->state_lock, flags); |
| 171 |
} |
| 172 |
|
| 173 |
while (nr_pages--) |
| 174 |
dec_page_count(F2FS_F_SB(folio), __read_io_type(folio)); |
| 175 |
|
| 176 |
if (bio->bi_status == BLK_STS_OK && |
| 177 |
F2FS_F_SB(folio)->node_inode && is_node_folio(folio) && |
| 178 |
f2fs_sanity_check_node_footer(F2FS_F_SB(folio), |
| 179 |
folio, folio->index, NODE_TYPE_REGULAR, true)) |
| 180 |
bio->bi_status = BLK_STS_IOERR; |
| 181 |
|
| 182 |
if (finished) |
| 183 |
folio_end_read(folio, bio->bi_status == BLK_STS_OK); |
| 184 |
} |
| 185 |
|
| 186 |
if (ctx) |
| 187 |
mempool_free(ctx, bio_post_read_ctx_pool); |
| 188 |
bio_put(bio); |
| 189 |
} |
| 190 |
|
| 191 |
static void f2fs_verify_bio(struct work_struct *work) |
| 192 |
{ |
| 193 |
struct bio_post_read_ctx *ctx = |
| 194 |
container_of(work, struct bio_post_read_ctx, work); |
| 195 |
struct bio *bio = ctx->bio; |
| 196 |
bool may_have_compressed_pages = (ctx->enabled_steps & STEP_DECOMPRESS); |
| 197 |
struct fsverity_info *vi = ctx->vi; |
| 198 |
|
| 199 |
/* |
| 200 |
* fsverity_verify_bio() may call readahead() again, and while verity |
| 201 |
* will be disabled for this, decryption and/or decompression may still |
| 202 |
* be needed, resulting in another bio_post_read_ctx being allocated. |
| 203 |
* So to prevent deadlocks we need to release the current ctx to the |
| 204 |
* mempool first. This assumes that verity is the last post-read step. |
| 205 |
*/ |
| 206 |
mempool_free(ctx, bio_post_read_ctx_pool); |
| 207 |
bio->bi_private = NULL; |
| 208 |
|
| 209 |
/* |
| 210 |
* Verify the bio's pages with fs-verity. Exclude compressed pages, |
| 211 |
* as those were handled separately by f2fs_end_read_compressed_page(). |
| 212 |
*/ |
| 213 |
if (may_have_compressed_pages) { |
| 214 |
struct folio_iter fi; |
| 215 |
|
| 216 |
bio_for_each_folio_all(fi, bio) { |
| 217 |
struct folio *folio = fi.folio; |
| 218 |
|
| 219 |
if (!f2fs_is_compressed_page(folio) && |
| 220 |
!fsverity_verify_folio(vi, folio)) { |
| 221 |
bio->bi_status = BLK_STS_IOERR; |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
} else { |
| 226 |
fsverity_verify_bio(vi, bio); |
| 227 |
} |
| 228 |
|
| 229 |
f2fs_finish_read_bio(bio, true); |
| 230 |
} |
| 231 |
|
| 232 |
/* |
| 233 |
* If the bio's data needs to be verified with fs-verity, then enqueue the |
| 234 |
* verity work for the bio. Otherwise finish the bio now. |
| 235 |
* |
| 236 |
* Note that to avoid deadlocks, the verity work can't be done on the |
| 237 |
* decryption/decompression workqueue. This is because verifying the data pages |
| 238 |
* can involve reading verity metadata pages from the file, and these verity |
| 239 |
* metadata pages may be encrypted and/or compressed. |
| 240 |
*/ |
| 241 |
static void f2fs_verify_and_finish_bio(struct bio *bio, bool in_task) |
| 242 |
{ |
| 243 |
struct bio_post_read_ctx *ctx = bio->bi_private; |
| 244 |
|
| 245 |
if (ctx && (ctx->enabled_steps & STEP_VERITY)) { |
| 246 |
INIT_WORK(&ctx->work, f2fs_verify_bio); |
| 247 |
fsverity_enqueue_verify_work(&ctx->work); |
| 248 |
} else { |
| 249 |
f2fs_finish_read_bio(bio, in_task); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/* |
| 254 |
* Handle STEP_DECOMPRESS by decompressing any compressed clusters whose last |
| 255 |
* remaining page was read by @ctx->bio. |
| 256 |
* |
| 257 |
* Note that a bio may span clusters (even a mix of compressed and uncompressed |
| 258 |
* clusters) or be for just part of a cluster. STEP_DECOMPRESS just indicates |
| 259 |
* that the bio includes at least one compressed page. The actual decompression |
| 260 |
* is done on a per-cluster basis, not a per-bio basis. |
| 261 |
*/ |
| 262 |
static void f2fs_handle_step_decompress(struct bio_post_read_ctx *ctx, |
| 263 |
bool in_task) |
| 264 |
{ |
| 265 |
struct folio_iter fi; |
| 266 |
bool all_compressed = true; |
| 267 |
block_t blkaddr = ctx->fs_blkaddr; |
| 268 |
|
| 269 |
bio_for_each_folio_all(fi, ctx->bio) { |
| 270 |
struct folio *folio = fi.folio; |
| 271 |
|
| 272 |
if (f2fs_is_compressed_page(folio)) |
| 273 |
f2fs_end_read_compressed_page(folio, false, blkaddr, |
| 274 |
in_task); |
| 275 |
else |
| 276 |
all_compressed = false; |
| 277 |
|
| 278 |
blkaddr++; |
| 279 |
} |
| 280 |
|
| 281 |
ctx->decompression_attempted = true; |
| 282 |
|
| 283 |
/* |
| 284 |
* Optimization: if all the bio's pages are compressed, then scheduling |
| 285 |
* the per-bio verity work is unnecessary, as verity will be fully |
| 286 |
* handled at the compression cluster level. |
| 287 |
*/ |
| 288 |
if (all_compressed) |
| 289 |
ctx->enabled_steps &= ~STEP_VERITY; |
| 290 |
} |
| 291 |
|
| 292 |
static void f2fs_post_read_work(struct work_struct *work) |
| 293 |
{ |
| 294 |
struct bio_post_read_ctx *ctx = |
| 295 |
container_of(work, struct bio_post_read_ctx, work); |
| 296 |
struct bio *bio = ctx->bio; |
| 297 |
|
| 298 |
if ((ctx->enabled_steps & STEP_DECRYPT) && !fscrypt_decrypt_bio(bio)) { |
| 299 |
f2fs_finish_read_bio(bio, true); |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
if (ctx->enabled_steps & STEP_DECOMPRESS) |
| 304 |
f2fs_handle_step_decompress(ctx, true); |
| 305 |
|
| 306 |
f2fs_verify_and_finish_bio(bio, true); |
| 307 |
} |
| 308 |
|
| 309 |
static void f2fs_read_end_io(struct bio *bio) |
| 310 |
{ |
| 311 |
struct f2fs_sb_info *sbi = F2FS_F_SB(bio_first_folio_all(bio)); |
| 312 |
struct bio_post_read_ctx *ctx; |
| 313 |
bool intask = in_task() && !irqs_disabled(); |
| 314 |
|
| 315 |
iostat_update_and_unbind_ctx(bio); |
| 316 |
ctx = bio->bi_private; |
| 317 |
|
| 318 |
if (time_to_inject(sbi, FAULT_READ_IO)) |
| 319 |
bio->bi_status = BLK_STS_IOERR; |
| 320 |
|
| 321 |
if (bio->bi_status != BLK_STS_OK) { |
| 322 |
f2fs_finish_read_bio(bio, intask); |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
if (ctx) { |
| 327 |
unsigned int enabled_steps = ctx->enabled_steps & |
| 328 |
(STEP_DECRYPT | STEP_DECOMPRESS); |
| 329 |
|
| 330 |
/* |
| 331 |
* If we have only decompression step between decompression and |
| 332 |
* decrypt, we don't need post processing for this. |
| 333 |
*/ |
| 334 |
if (enabled_steps == STEP_DECOMPRESS && |
| 335 |
!f2fs_low_mem_mode(sbi)) { |
| 336 |
f2fs_handle_step_decompress(ctx, intask); |
| 337 |
} else if (enabled_steps) { |
| 338 |
INIT_WORK(&ctx->work, f2fs_post_read_work); |
| 339 |
queue_work(ctx->sbi->post_read_wq, &ctx->work); |
| 340 |
return; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
f2fs_verify_and_finish_bio(bio, intask); |
| 345 |
} |
| 346 |
|
| 347 |
static void f2fs_write_end_io(struct bio *bio) |
| 348 |
{ |
| 349 |
struct f2fs_sb_info *sbi; |
| 350 |
struct folio_iter fi; |
| 351 |
|
| 352 |
iostat_update_and_unbind_ctx(bio); |
| 353 |
sbi = bio->bi_private; |
| 354 |
|
| 355 |
if (time_to_inject(sbi, FAULT_WRITE_IO)) |
| 356 |
bio->bi_status = BLK_STS_IOERR; |
| 357 |
|
| 358 |
bio_for_each_folio_all(fi, bio) { |
| 359 |
struct folio *folio = fi.folio; |
| 360 |
enum count_type type; |
| 361 |
|
| 362 |
if (fscrypt_is_bounce_folio(folio)) { |
| 363 |
struct folio *io_folio = folio; |
| 364 |
|
| 365 |
folio = fscrypt_pagecache_folio(io_folio); |
| 366 |
fscrypt_free_bounce_page(&io_folio->page); |
| 367 |
} |
| 368 |
|
| 369 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 370 |
if (f2fs_is_compressed_page(folio)) { |
| 371 |
f2fs_compress_write_end_io(bio, folio); |
| 372 |
continue; |
| 373 |
} |
| 374 |
#endif |
| 375 |
|
| 376 |
type = WB_DATA_TYPE(folio, false); |
| 377 |
|
| 378 |
if (unlikely(bio->bi_status != BLK_STS_OK)) { |
| 379 |
mapping_set_error(folio->mapping, -EIO); |
| 380 |
if (type == F2FS_WB_CP_DATA) |
| 381 |
f2fs_stop_checkpoint(sbi, true, |
| 382 |
STOP_CP_REASON_WRITE_FAIL); |
| 383 |
} |
| 384 |
|
| 385 |
if (is_node_folio(folio)) { |
| 386 |
f2fs_sanity_check_node_footer(sbi, folio, |
| 387 |
folio->index, NODE_TYPE_REGULAR, true); |
| 388 |
f2fs_bug_on(sbi, folio->index != nid_of_node(folio)); |
| 389 |
} |
| 390 |
if (f2fs_in_warm_node_list(sbi, folio)) |
| 391 |
f2fs_del_fsync_node_entry(sbi, folio); |
| 392 |
|
| 393 |
dec_page_count(sbi, type); |
| 394 |
|
| 395 |
/* |
| 396 |
* we should access sbi before folio_end_writeback() to |
| 397 |
* avoid racing w/ kill_f2fs_super() |
| 398 |
*/ |
| 399 |
if (type == F2FS_WB_CP_DATA && !get_pages(sbi, type) && |
| 400 |
wq_has_sleeper(&sbi->cp_wait)) |
| 401 |
wake_up(&sbi->cp_wait); |
| 402 |
|
| 403 |
folio_clear_f2fs_gcing(folio); |
| 404 |
folio_end_writeback(folio); |
| 405 |
} |
| 406 |
|
| 407 |
bio_put(bio); |
| 408 |
} |
| 409 |
|
| 410 |
#ifdef CONFIG_BLK_DEV_ZONED |
| 411 |
static void f2fs_zone_write_end_io(struct bio *bio) |
| 412 |
{ |
| 413 |
struct f2fs_bio_info *io = (struct f2fs_bio_info *)bio->bi_private; |
| 414 |
|
| 415 |
bio->bi_private = io->bi_private; |
| 416 |
complete(&io->zone_wait); |
| 417 |
f2fs_write_end_io(bio); |
| 418 |
} |
| 419 |
#endif |
| 420 |
|
| 421 |
struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi, |
| 422 |
block_t blk_addr, sector_t *sector) |
| 423 |
{ |
| 424 |
struct block_device *bdev = sbi->sb->s_bdev; |
| 425 |
int i; |
| 426 |
|
| 427 |
if (f2fs_is_multi_device(sbi)) { |
| 428 |
for (i = 0; i < sbi->s_ndevs; i++) { |
| 429 |
if (FDEV(i).start_blk <= blk_addr && |
| 430 |
FDEV(i).end_blk >= blk_addr) { |
| 431 |
blk_addr -= FDEV(i).start_blk; |
| 432 |
bdev = FDEV(i).bdev; |
| 433 |
break; |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
if (sector) |
| 439 |
*sector = SECTOR_FROM_BLOCK(blk_addr); |
| 440 |
return bdev; |
| 441 |
} |
| 442 |
|
| 443 |
int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr) |
| 444 |
{ |
| 445 |
int i; |
| 446 |
|
| 447 |
if (!f2fs_is_multi_device(sbi)) |
| 448 |
return 0; |
| 449 |
|
| 450 |
for (i = 0; i < sbi->s_ndevs; i++) |
| 451 |
if (FDEV(i).start_blk <= blkaddr && FDEV(i).end_blk >= blkaddr) |
| 452 |
return i; |
| 453 |
return 0; |
| 454 |
} |
| 455 |
|
| 456 |
static blk_opf_t f2fs_io_flags(struct f2fs_io_info *fio) |
| 457 |
{ |
| 458 |
unsigned int temp_mask = GENMASK(NR_TEMP_TYPE - 1, 0); |
| 459 |
unsigned int fua_flag, meta_flag, io_flag; |
| 460 |
blk_opf_t op_flags = 0; |
| 461 |
|
| 462 |
if (fio->op != REQ_OP_WRITE) |
| 463 |
return 0; |
| 464 |
if (fio->type == DATA) |
| 465 |
io_flag = fio->sbi->data_io_flag; |
| 466 |
else if (fio->type == NODE) |
| 467 |
io_flag = fio->sbi->node_io_flag; |
| 468 |
else |
| 469 |
return 0; |
| 470 |
|
| 471 |
fua_flag = io_flag & temp_mask; |
| 472 |
meta_flag = (io_flag >> NR_TEMP_TYPE) & temp_mask; |
| 473 |
|
| 474 |
/* |
| 475 |
* data/node io flag bits per temp: |
| 476 |
* REQ_META | REQ_FUA | |
| 477 |
* 5 | 4 | 3 | 2 | 1 | 0 | |
| 478 |
* Cold | Warm | Hot | Cold | Warm | Hot | |
| 479 |
*/ |
| 480 |
if (BIT(fio->temp) & meta_flag) |
| 481 |
op_flags |= REQ_META; |
| 482 |
if (BIT(fio->temp) & fua_flag) |
| 483 |
op_flags |= REQ_FUA; |
| 484 |
|
| 485 |
if (fio->type == DATA && |
| 486 |
F2FS_I(fio->folio->mapping->host)->ioprio_hint == F2FS_IOPRIO_WRITE) |
| 487 |
op_flags |= REQ_PRIO; |
| 488 |
|
| 489 |
return op_flags; |
| 490 |
} |
| 491 |
|
| 492 |
static struct bio *__bio_alloc(struct f2fs_io_info *fio, int npages) |
| 493 |
{ |
| 494 |
struct f2fs_sb_info *sbi = fio->sbi; |
| 495 |
struct block_device *bdev; |
| 496 |
sector_t sector; |
| 497 |
struct bio *bio; |
| 498 |
|
| 499 |
bdev = f2fs_target_device(sbi, fio->new_blkaddr, §or); |
| 500 |
bio = bio_alloc_bioset(bdev, npages, |
| 501 |
fio->op | fio->op_flags | f2fs_io_flags(fio), |
| 502 |
GFP_NOIO, &f2fs_bioset); |
| 503 |
bio->bi_iter.bi_sector = sector; |
| 504 |
if (is_read_io(fio->op)) { |
| 505 |
bio->bi_end_io = f2fs_read_end_io; |
| 506 |
bio->bi_private = NULL; |
| 507 |
} else { |
| 508 |
bio->bi_end_io = f2fs_write_end_io; |
| 509 |
bio->bi_private = sbi; |
| 510 |
bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, |
| 511 |
fio->type, fio->temp); |
| 512 |
} |
| 513 |
iostat_alloc_and_bind_ctx(sbi, bio, NULL); |
| 514 |
|
| 515 |
if (fio->io_wbc) |
| 516 |
wbc_init_bio(fio->io_wbc, bio); |
| 517 |
|
| 518 |
return bio; |
| 519 |
} |
| 520 |
|
| 521 |
static void f2fs_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode, |
| 522 |
pgoff_t first_idx, |
| 523 |
const struct f2fs_io_info *fio, |
| 524 |
gfp_t gfp_mask) |
| 525 |
{ |
| 526 |
/* |
| 527 |
* The f2fs garbage collector sets ->encrypted_page when it wants to |
| 528 |
* read/write raw data without encryption. |
| 529 |
*/ |
| 530 |
if (!fio || !fio->encrypted_page) |
| 531 |
fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask); |
| 532 |
} |
| 533 |
|
| 534 |
static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode, |
| 535 |
pgoff_t next_idx, |
| 536 |
const struct f2fs_io_info *fio) |
| 537 |
{ |
| 538 |
/* |
| 539 |
* The f2fs garbage collector sets ->encrypted_page when it wants to |
| 540 |
* read/write raw data without encryption. |
| 541 |
*/ |
| 542 |
if (fio && fio->encrypted_page) |
| 543 |
return !bio_has_crypt_ctx(bio); |
| 544 |
|
| 545 |
return fscrypt_mergeable_bio(bio, inode, next_idx); |
| 546 |
} |
| 547 |
|
| 548 |
void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio, |
| 549 |
enum page_type type) |
| 550 |
{ |
| 551 |
if (!bio) |
| 552 |
return; |
| 553 |
|
| 554 |
WARN_ON_ONCE(!is_read_io(bio_op(bio))); |
| 555 |
trace_f2fs_submit_read_bio(sbi->sb, type, bio); |
| 556 |
|
| 557 |
iostat_update_submit_ctx(bio, type); |
| 558 |
blk_crypto_submit_bio(bio); |
| 559 |
} |
| 560 |
|
| 561 |
static void f2fs_submit_write_bio(struct f2fs_sb_info *sbi, struct bio *bio, |
| 562 |
enum page_type type) |
| 563 |
{ |
| 564 |
WARN_ON_ONCE(is_read_io(bio_op(bio))); |
| 565 |
trace_f2fs_submit_write_bio(sbi->sb, type, bio); |
| 566 |
iostat_update_submit_ctx(bio, type); |
| 567 |
blk_crypto_submit_bio(bio); |
| 568 |
} |
| 569 |
|
| 570 |
static void __submit_merged_bio(struct f2fs_bio_info *io) |
| 571 |
{ |
| 572 |
struct f2fs_io_info *fio = &io->fio; |
| 573 |
|
| 574 |
if (!io->bio) |
| 575 |
return; |
| 576 |
|
| 577 |
if (is_read_io(fio->op)) { |
| 578 |
trace_f2fs_prepare_read_bio(io->sbi->sb, fio->type, io->bio); |
| 579 |
f2fs_submit_read_bio(io->sbi, io->bio, fio->type); |
| 580 |
} else { |
| 581 |
trace_f2fs_prepare_write_bio(io->sbi->sb, fio->type, io->bio); |
| 582 |
f2fs_submit_write_bio(io->sbi, io->bio, fio->type); |
| 583 |
} |
| 584 |
io->bio = NULL; |
| 585 |
} |
| 586 |
|
| 587 |
static bool __has_merged_page(struct bio *bio, struct inode *inode, |
| 588 |
struct folio *folio, nid_t ino) |
| 589 |
{ |
| 590 |
struct folio_iter fi; |
| 591 |
|
| 592 |
if (!bio) |
| 593 |
return false; |
| 594 |
|
| 595 |
if (!inode && !folio && !ino) |
| 596 |
return true; |
| 597 |
|
| 598 |
bio_for_each_folio_all(fi, bio) { |
| 599 |
struct folio *target = fi.folio; |
| 600 |
|
| 601 |
if (fscrypt_is_bounce_folio(target)) { |
| 602 |
target = fscrypt_pagecache_folio(target); |
| 603 |
if (IS_ERR(target)) |
| 604 |
continue; |
| 605 |
} |
| 606 |
if (f2fs_is_compressed_page(target)) { |
| 607 |
target = f2fs_compress_control_folio(target); |
| 608 |
if (IS_ERR(target)) |
| 609 |
continue; |
| 610 |
} |
| 611 |
|
| 612 |
if (inode && inode == target->mapping->host) |
| 613 |
return true; |
| 614 |
if (folio && folio == target) |
| 615 |
return true; |
| 616 |
if (ino && ino == ino_of_node(target)) |
| 617 |
return true; |
| 618 |
} |
| 619 |
|
| 620 |
return false; |
| 621 |
} |
| 622 |
|
| 623 |
int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi) |
| 624 |
{ |
| 625 |
int i; |
| 626 |
|
| 627 |
for (i = 0; i < NR_PAGE_TYPE; i++) { |
| 628 |
int n = (i == META) ? 1 : NR_TEMP_TYPE; |
| 629 |
int j; |
| 630 |
|
| 631 |
sbi->write_io[i] = f2fs_kmalloc(sbi, |
| 632 |
array_size(n, sizeof(struct f2fs_bio_info)), |
| 633 |
GFP_KERNEL); |
| 634 |
if (!sbi->write_io[i]) |
| 635 |
return -ENOMEM; |
| 636 |
|
| 637 |
for (j = HOT; j < n; j++) { |
| 638 |
struct f2fs_bio_info *io = &sbi->write_io[i][j]; |
| 639 |
|
| 640 |
init_f2fs_rwsem_trace(&io->io_rwsem, sbi, |
| 641 |
LOCK_NAME_IO_RWSEM); |
| 642 |
io->sbi = sbi; |
| 643 |
io->bio = NULL; |
| 644 |
io->last_block_in_bio = 0; |
| 645 |
spin_lock_init(&io->io_lock); |
| 646 |
INIT_LIST_HEAD(&io->io_list); |
| 647 |
INIT_LIST_HEAD(&io->bio_list); |
| 648 |
init_f2fs_rwsem(&io->bio_list_lock); |
| 649 |
#ifdef CONFIG_BLK_DEV_ZONED |
| 650 |
init_completion(&io->zone_wait); |
| 651 |
io->zone_pending_bio = NULL; |
| 652 |
io->bi_private = NULL; |
| 653 |
#endif |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
return 0; |
| 658 |
} |
| 659 |
|
| 660 |
static void __f2fs_submit_merged_write(struct f2fs_sb_info *sbi, |
| 661 |
enum page_type type, enum temp_type temp) |
| 662 |
{ |
| 663 |
enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 664 |
struct f2fs_bio_info *io = sbi->write_io[btype] + temp; |
| 665 |
struct f2fs_lock_context lc; |
| 666 |
|
| 667 |
f2fs_down_write_trace(&io->io_rwsem, &lc); |
| 668 |
|
| 669 |
if (!io->bio) |
| 670 |
goto unlock_out; |
| 671 |
|
| 672 |
/* change META to META_FLUSH in the checkpoint procedure */ |
| 673 |
if (type >= META_FLUSH) { |
| 674 |
io->fio.type = META_FLUSH; |
| 675 |
io->bio->bi_opf |= REQ_META | REQ_PRIO | REQ_SYNC; |
| 676 |
if (!test_opt(sbi, NOBARRIER)) |
| 677 |
io->bio->bi_opf |= REQ_PREFLUSH | REQ_FUA; |
| 678 |
} |
| 679 |
__submit_merged_bio(io); |
| 680 |
unlock_out: |
| 681 |
f2fs_up_write_trace(&io->io_rwsem, &lc); |
| 682 |
} |
| 683 |
|
| 684 |
static void __submit_merged_write_cond(struct f2fs_sb_info *sbi, |
| 685 |
struct inode *inode, struct folio *folio, |
| 686 |
nid_t ino, enum page_type type, bool writeback) |
| 687 |
{ |
| 688 |
enum temp_type temp; |
| 689 |
bool ret = true; |
| 690 |
bool force = !inode && !folio && !ino; |
| 691 |
|
| 692 |
for (temp = HOT; temp < NR_TEMP_TYPE; temp++) { |
| 693 |
if (!force) { |
| 694 |
enum page_type btype = PAGE_TYPE_OF_BIO(type); |
| 695 |
struct f2fs_bio_info *io = sbi->write_io[btype] + temp; |
| 696 |
struct f2fs_lock_context lc; |
| 697 |
|
| 698 |
f2fs_down_read_trace(&io->io_rwsem, &lc); |
| 699 |
ret = __has_merged_page(io->bio, inode, folio, ino); |
| 700 |
f2fs_up_read_trace(&io->io_rwsem, &lc); |
| 701 |
} |
| 702 |
if (ret) { |
| 703 |
__f2fs_submit_merged_write(sbi, type, temp); |
| 704 |
/* |
| 705 |
* For waitting writebck case, if the bio owned by the |
| 706 |
* folio is already submitted, we do not need to submit |
| 707 |
* other types of bios. |
| 708 |
*/ |
| 709 |
if (writeback) |
| 710 |
break; |
| 711 |
} |
| 712 |
|
| 713 |
/* TODO: use HOT temp only for meta pages now. */ |
| 714 |
if (type >= META) |
| 715 |
break; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type) |
| 720 |
{ |
| 721 |
__submit_merged_write_cond(sbi, NULL, NULL, 0, type, false); |
| 722 |
} |
| 723 |
|
| 724 |
void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi, |
| 725 |
struct inode *inode, struct folio *folio, |
| 726 |
nid_t ino, enum page_type type) |
| 727 |
{ |
| 728 |
__submit_merged_write_cond(sbi, inode, folio, ino, type, false); |
| 729 |
} |
| 730 |
|
| 731 |
void f2fs_submit_merged_write_folio(struct f2fs_sb_info *sbi, |
| 732 |
struct folio *folio, enum page_type type) |
| 733 |
{ |
| 734 |
__submit_merged_write_cond(sbi, NULL, folio, 0, type, true); |
| 735 |
} |
| 736 |
|
| 737 |
void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi) |
| 738 |
{ |
| 739 |
f2fs_submit_merged_write(sbi, DATA); |
| 740 |
f2fs_submit_merged_write(sbi, NODE); |
| 741 |
f2fs_submit_merged_write(sbi, META); |
| 742 |
} |
| 743 |
|
| 744 |
/* |
| 745 |
* Fill the locked page with data located in the block address. |
| 746 |
* A caller needs to unlock the page on failure. |
| 747 |
*/ |
| 748 |
int f2fs_submit_page_bio(struct f2fs_io_info *fio) |
| 749 |
{ |
| 750 |
struct bio *bio; |
| 751 |
struct folio *fio_folio = fio->folio; |
| 752 |
struct folio *data_folio = fio->encrypted_page ? |
| 753 |
page_folio(fio->encrypted_page) : fio_folio; |
| 754 |
|
| 755 |
if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, |
| 756 |
fio->is_por ? META_POR : (__is_meta_io(fio) ? |
| 757 |
META_GENERIC : DATA_GENERIC_ENHANCE))) |
| 758 |
return -EFSCORRUPTED; |
| 759 |
|
| 760 |
trace_f2fs_submit_folio_bio(data_folio, fio); |
| 761 |
|
| 762 |
/* Allocate a new bio */ |
| 763 |
bio = __bio_alloc(fio, 1); |
| 764 |
|
| 765 |
f2fs_set_bio_crypt_ctx(bio, fio_folio->mapping->host, |
| 766 |
fio_folio->index, fio, GFP_NOIO); |
| 767 |
bio_add_folio_nofail(bio, data_folio, folio_size(data_folio), 0); |
| 768 |
|
| 769 |
if (fio->io_wbc && !is_read_io(fio->op)) |
| 770 |
wbc_account_cgroup_owner(fio->io_wbc, fio_folio, PAGE_SIZE); |
| 771 |
|
| 772 |
inc_page_count(fio->sbi, is_read_io(fio->op) ? |
| 773 |
__read_io_type(data_folio) : WB_DATA_TYPE(fio->folio, false)); |
| 774 |
|
| 775 |
if (is_read_io(bio_op(bio))) |
| 776 |
f2fs_submit_read_bio(fio->sbi, bio, fio->type); |
| 777 |
else |
| 778 |
f2fs_submit_write_bio(fio->sbi, bio, fio->type); |
| 779 |
return 0; |
| 780 |
} |
| 781 |
|
| 782 |
static bool page_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio, |
| 783 |
block_t last_blkaddr, block_t cur_blkaddr) |
| 784 |
{ |
| 785 |
if (unlikely(sbi->max_io_bytes && |
| 786 |
bio->bi_iter.bi_size >= sbi->max_io_bytes)) |
| 787 |
return false; |
| 788 |
if (last_blkaddr + 1 != cur_blkaddr) |
| 789 |
return false; |
| 790 |
return bio->bi_bdev == f2fs_target_device(sbi, cur_blkaddr, NULL); |
| 791 |
} |
| 792 |
|
| 793 |
static bool io_type_is_mergeable(struct f2fs_bio_info *io, |
| 794 |
struct f2fs_io_info *fio) |
| 795 |
{ |
| 796 |
blk_opf_t mask = ~(REQ_PREFLUSH | REQ_FUA); |
| 797 |
|
| 798 |
if (io->fio.op != fio->op) |
| 799 |
return false; |
| 800 |
return (io->fio.op_flags & mask) == (fio->op_flags & mask); |
| 801 |
} |
| 802 |
|
| 803 |
static bool io_is_mergeable(struct f2fs_sb_info *sbi, struct bio *bio, |
| 804 |
struct f2fs_bio_info *io, |
| 805 |
struct f2fs_io_info *fio, |
| 806 |
block_t last_blkaddr, |
| 807 |
block_t cur_blkaddr) |
| 808 |
{ |
| 809 |
if (!page_is_mergeable(sbi, bio, last_blkaddr, cur_blkaddr)) |
| 810 |
return false; |
| 811 |
return io_type_is_mergeable(io, fio); |
| 812 |
} |
| 813 |
|
| 814 |
static void add_bio_entry(struct f2fs_sb_info *sbi, struct bio *bio, |
| 815 |
struct folio *folio, enum temp_type temp) |
| 816 |
{ |
| 817 |
struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; |
| 818 |
struct bio_entry *be; |
| 819 |
|
| 820 |
be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS, true, NULL); |
| 821 |
be->bio = bio; |
| 822 |
bio_get(bio); |
| 823 |
|
| 824 |
bio_add_folio_nofail(bio, folio, folio_size(folio), 0); |
| 825 |
|
| 826 |
f2fs_down_write(&io->bio_list_lock); |
| 827 |
list_add_tail(&be->list, &io->bio_list); |
| 828 |
f2fs_up_write(&io->bio_list_lock); |
| 829 |
} |
| 830 |
|
| 831 |
static void del_bio_entry(struct bio_entry *be) |
| 832 |
{ |
| 833 |
list_del(&be->list); |
| 834 |
kmem_cache_free(bio_entry_slab, be); |
| 835 |
} |
| 836 |
|
| 837 |
static int add_ipu_page(struct f2fs_io_info *fio, struct bio **bio, |
| 838 |
struct folio *folio) |
| 839 |
{ |
| 840 |
struct folio *fio_folio = fio->folio; |
| 841 |
struct f2fs_sb_info *sbi = fio->sbi; |
| 842 |
enum temp_type temp; |
| 843 |
bool found = false; |
| 844 |
int ret = -EAGAIN; |
| 845 |
|
| 846 |
for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { |
| 847 |
struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; |
| 848 |
struct list_head *head = &io->bio_list; |
| 849 |
struct bio_entry *be; |
| 850 |
|
| 851 |
f2fs_down_write(&io->bio_list_lock); |
| 852 |
list_for_each_entry(be, head, list) { |
| 853 |
if (be->bio != *bio) |
| 854 |
continue; |
| 855 |
|
| 856 |
found = true; |
| 857 |
|
| 858 |
f2fs_bug_on(sbi, !page_is_mergeable(sbi, *bio, |
| 859 |
*fio->last_block, |
| 860 |
fio->new_blkaddr)); |
| 861 |
if (f2fs_crypt_mergeable_bio(*bio, |
| 862 |
fio_folio->mapping->host, |
| 863 |
fio_folio->index, fio) && |
| 864 |
bio_add_folio(*bio, folio, folio_size(folio), 0)) { |
| 865 |
ret = 0; |
| 866 |
break; |
| 867 |
} |
| 868 |
|
| 869 |
/* page can't be merged into bio; submit the bio */ |
| 870 |
del_bio_entry(be); |
| 871 |
f2fs_submit_write_bio(sbi, *bio, DATA); |
| 872 |
break; |
| 873 |
} |
| 874 |
f2fs_up_write(&io->bio_list_lock); |
| 875 |
} |
| 876 |
|
| 877 |
if (ret) { |
| 878 |
bio_put(*bio); |
| 879 |
*bio = NULL; |
| 880 |
} |
| 881 |
|
| 882 |
return ret; |
| 883 |
} |
| 884 |
|
| 885 |
void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi, |
| 886 |
struct bio **bio, struct folio *folio) |
| 887 |
{ |
| 888 |
enum temp_type temp; |
| 889 |
bool found = false; |
| 890 |
struct bio *target = bio ? *bio : NULL; |
| 891 |
|
| 892 |
f2fs_bug_on(sbi, !target && !folio); |
| 893 |
|
| 894 |
for (temp = HOT; temp < NR_TEMP_TYPE && !found; temp++) { |
| 895 |
struct f2fs_bio_info *io = sbi->write_io[DATA] + temp; |
| 896 |
struct list_head *head = &io->bio_list; |
| 897 |
struct bio_entry *be; |
| 898 |
|
| 899 |
if (list_empty(head)) |
| 900 |
continue; |
| 901 |
|
| 902 |
f2fs_down_read(&io->bio_list_lock); |
| 903 |
list_for_each_entry(be, head, list) { |
| 904 |
if (target) |
| 905 |
found = (target == be->bio); |
| 906 |
else |
| 907 |
found = __has_merged_page(be->bio, NULL, |
| 908 |
folio, 0); |
| 909 |
if (found) |
| 910 |
break; |
| 911 |
} |
| 912 |
f2fs_up_read(&io->bio_list_lock); |
| 913 |
|
| 914 |
if (!found) |
| 915 |
continue; |
| 916 |
|
| 917 |
found = false; |
| 918 |
|
| 919 |
f2fs_down_write(&io->bio_list_lock); |
| 920 |
list_for_each_entry(be, head, list) { |
| 921 |
if (target) |
| 922 |
found = (target == be->bio); |
| 923 |
else |
| 924 |
found = __has_merged_page(be->bio, NULL, |
| 925 |
folio, 0); |
| 926 |
if (found) { |
| 927 |
target = be->bio; |
| 928 |
del_bio_entry(be); |
| 929 |
break; |
| 930 |
} |
| 931 |
} |
| 932 |
f2fs_up_write(&io->bio_list_lock); |
| 933 |
} |
| 934 |
|
| 935 |
if (found) |
| 936 |
f2fs_submit_write_bio(sbi, target, DATA); |
| 937 |
if (bio && *bio) { |
| 938 |
bio_put(*bio); |
| 939 |
*bio = NULL; |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
int f2fs_merge_page_bio(struct f2fs_io_info *fio) |
| 944 |
{ |
| 945 |
struct bio *bio = *fio->bio; |
| 946 |
struct folio *data_folio = fio->encrypted_page ? |
| 947 |
page_folio(fio->encrypted_page) : fio->folio; |
| 948 |
struct folio *folio = fio->folio; |
| 949 |
|
| 950 |
if (!f2fs_is_valid_blkaddr(fio->sbi, fio->new_blkaddr, |
| 951 |
__is_meta_io(fio) ? META_GENERIC : DATA_GENERIC)) |
| 952 |
return -EFSCORRUPTED; |
| 953 |
|
| 954 |
trace_f2fs_submit_folio_bio(data_folio, fio); |
| 955 |
|
| 956 |
if (bio && !page_is_mergeable(fio->sbi, bio, *fio->last_block, |
| 957 |
fio->new_blkaddr)) |
| 958 |
f2fs_submit_merged_ipu_write(fio->sbi, &bio, NULL); |
| 959 |
alloc_new: |
| 960 |
if (!bio) { |
| 961 |
bio = __bio_alloc(fio, BIO_MAX_VECS); |
| 962 |
f2fs_set_bio_crypt_ctx(bio, folio->mapping->host, |
| 963 |
folio->index, fio, GFP_NOIO); |
| 964 |
|
| 965 |
add_bio_entry(fio->sbi, bio, data_folio, fio->temp); |
| 966 |
} else { |
| 967 |
if (add_ipu_page(fio, &bio, data_folio)) |
| 968 |
goto alloc_new; |
| 969 |
} |
| 970 |
|
| 971 |
if (fio->io_wbc) |
| 972 |
wbc_account_cgroup_owner(fio->io_wbc, folio, folio_size(folio)); |
| 973 |
|
| 974 |
inc_page_count(fio->sbi, WB_DATA_TYPE(folio, false)); |
| 975 |
|
| 976 |
*fio->last_block = fio->new_blkaddr; |
| 977 |
*fio->bio = bio; |
| 978 |
|
| 979 |
return 0; |
| 980 |
} |
| 981 |
|
| 982 |
#ifdef CONFIG_BLK_DEV_ZONED |
| 983 |
static bool is_end_zone_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr) |
| 984 |
{ |
| 985 |
struct block_device *bdev = sbi->sb->s_bdev; |
| 986 |
int devi = 0; |
| 987 |
|
| 988 |
if (f2fs_is_multi_device(sbi)) { |
| 989 |
devi = f2fs_target_device_index(sbi, blkaddr); |
| 990 |
if (blkaddr < FDEV(devi).start_blk || |
| 991 |
blkaddr > FDEV(devi).end_blk) { |
| 992 |
f2fs_err(sbi, "Invalid block %x", blkaddr); |
| 993 |
return false; |
| 994 |
} |
| 995 |
blkaddr -= FDEV(devi).start_blk; |
| 996 |
bdev = FDEV(devi).bdev; |
| 997 |
} |
| 998 |
return bdev_is_zoned(bdev) && |
| 999 |
f2fs_blkz_is_seq(sbi, devi, blkaddr) && |
| 1000 |
(blkaddr % sbi->blocks_per_blkz == sbi->blocks_per_blkz - 1); |
| 1001 |
} |
| 1002 |
#endif |
| 1003 |
|
| 1004 |
void f2fs_submit_page_write(struct f2fs_io_info *fio) |
| 1005 |
{ |
| 1006 |
struct f2fs_sb_info *sbi = fio->sbi; |
| 1007 |
enum page_type btype = PAGE_TYPE_OF_BIO(fio->type); |
| 1008 |
struct f2fs_bio_info *io = sbi->write_io[btype] + fio->temp; |
| 1009 |
struct folio *bio_folio; |
| 1010 |
struct f2fs_lock_context lc; |
| 1011 |
enum count_type type; |
| 1012 |
|
| 1013 |
f2fs_bug_on(sbi, is_read_io(fio->op)); |
| 1014 |
|
| 1015 |
f2fs_down_write_trace(&io->io_rwsem, &lc); |
| 1016 |
next: |
| 1017 |
#ifdef CONFIG_BLK_DEV_ZONED |
| 1018 |
if (f2fs_sb_has_blkzoned(sbi) && btype < META && io->zone_pending_bio) { |
| 1019 |
wait_for_completion_io(&io->zone_wait); |
| 1020 |
bio_put(io->zone_pending_bio); |
| 1021 |
io->zone_pending_bio = NULL; |
| 1022 |
io->bi_private = NULL; |
| 1023 |
} |
| 1024 |
#endif |
| 1025 |
|
| 1026 |
if (fio->in_list) { |
| 1027 |
spin_lock(&io->io_lock); |
| 1028 |
if (list_empty(&io->io_list)) { |
| 1029 |
spin_unlock(&io->io_lock); |
| 1030 |
goto out; |
| 1031 |
} |
| 1032 |
fio = list_first_entry(&io->io_list, |
| 1033 |
struct f2fs_io_info, list); |
| 1034 |
list_del(&fio->list); |
| 1035 |
spin_unlock(&io->io_lock); |
| 1036 |
} |
| 1037 |
|
| 1038 |
verify_fio_blkaddr(fio); |
| 1039 |
|
| 1040 |
if (fio->encrypted_page) |
| 1041 |
bio_folio = page_folio(fio->encrypted_page); |
| 1042 |
else if (fio->compressed_page) |
| 1043 |
bio_folio = page_folio(fio->compressed_page); |
| 1044 |
else |
| 1045 |
bio_folio = fio->folio; |
| 1046 |
|
| 1047 |
/* set submitted = true as a return value */ |
| 1048 |
fio->submitted = 1; |
| 1049 |
|
| 1050 |
type = WB_DATA_TYPE(bio_folio, fio->compressed_page); |
| 1051 |
inc_page_count(sbi, type); |
| 1052 |
|
| 1053 |
if (io->bio && |
| 1054 |
(!io_is_mergeable(sbi, io->bio, io, fio, io->last_block_in_bio, |
| 1055 |
fio->new_blkaddr) || |
| 1056 |
!f2fs_crypt_mergeable_bio(io->bio, fio_inode(fio), |
| 1057 |
bio_folio->index, fio))) |
| 1058 |
__submit_merged_bio(io); |
| 1059 |
alloc_new: |
| 1060 |
if (io->bio == NULL) { |
| 1061 |
io->bio = __bio_alloc(fio, BIO_MAX_VECS); |
| 1062 |
f2fs_set_bio_crypt_ctx(io->bio, fio_inode(fio), |
| 1063 |
bio_folio->index, fio, GFP_NOIO); |
| 1064 |
io->fio = *fio; |
| 1065 |
} |
| 1066 |
|
| 1067 |
if (!bio_add_folio(io->bio, bio_folio, folio_size(bio_folio), 0)) { |
| 1068 |
__submit_merged_bio(io); |
| 1069 |
goto alloc_new; |
| 1070 |
} |
| 1071 |
|
| 1072 |
if (fio->io_wbc) |
| 1073 |
wbc_account_cgroup_owner(fio->io_wbc, fio->folio, |
| 1074 |
folio_size(fio->folio)); |
| 1075 |
|
| 1076 |
io->last_block_in_bio = fio->new_blkaddr; |
| 1077 |
|
| 1078 |
trace_f2fs_submit_folio_write(fio->folio, fio); |
| 1079 |
#ifdef CONFIG_BLK_DEV_ZONED |
| 1080 |
if (f2fs_sb_has_blkzoned(sbi) && btype < META && |
| 1081 |
is_end_zone_blkaddr(sbi, fio->new_blkaddr)) { |
| 1082 |
bio_get(io->bio); |
| 1083 |
reinit_completion(&io->zone_wait); |
| 1084 |
io->bi_private = io->bio->bi_private; |
| 1085 |
io->bio->bi_private = io; |
| 1086 |
io->bio->bi_end_io = f2fs_zone_write_end_io; |
| 1087 |
io->zone_pending_bio = io->bio; |
| 1088 |
__submit_merged_bio(io); |
| 1089 |
} |
| 1090 |
#endif |
| 1091 |
if (fio->in_list) |
| 1092 |
goto next; |
| 1093 |
out: |
| 1094 |
if (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || |
| 1095 |
!f2fs_is_checkpoint_ready(sbi)) |
| 1096 |
__submit_merged_bio(io); |
| 1097 |
f2fs_up_write_trace(&io->io_rwsem, &lc); |
| 1098 |
} |
| 1099 |
|
| 1100 |
static struct bio *f2fs_grab_read_bio(struct inode *inode, |
| 1101 |
struct fsverity_info *vi, block_t blkaddr, |
| 1102 |
unsigned nr_pages, blk_opf_t op_flag, |
| 1103 |
pgoff_t first_idx, bool for_write) |
| 1104 |
{ |
| 1105 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1106 |
struct bio *bio; |
| 1107 |
struct bio_post_read_ctx *ctx = NULL; |
| 1108 |
unsigned int post_read_steps = 0; |
| 1109 |
sector_t sector; |
| 1110 |
struct block_device *bdev = f2fs_target_device(sbi, blkaddr, §or); |
| 1111 |
|
| 1112 |
bio = bio_alloc_bioset(bdev, bio_max_segs(nr_pages), |
| 1113 |
REQ_OP_READ | op_flag, |
| 1114 |
for_write ? GFP_NOIO : GFP_KERNEL, &f2fs_bioset); |
| 1115 |
bio->bi_iter.bi_sector = sector; |
| 1116 |
f2fs_set_bio_crypt_ctx(bio, inode, first_idx, NULL, GFP_NOFS); |
| 1117 |
bio->bi_end_io = f2fs_read_end_io; |
| 1118 |
|
| 1119 |
if (fscrypt_inode_uses_fs_layer_crypto(inode)) |
| 1120 |
post_read_steps |= STEP_DECRYPT; |
| 1121 |
|
| 1122 |
if (vi) |
| 1123 |
post_read_steps |= STEP_VERITY; |
| 1124 |
|
| 1125 |
/* |
| 1126 |
* STEP_DECOMPRESS is handled specially, since a compressed file might |
| 1127 |
* contain both compressed and uncompressed clusters. We'll allocate a |
| 1128 |
* bio_post_read_ctx if the file is compressed, but the caller is |
| 1129 |
* responsible for enabling STEP_DECOMPRESS if it's actually needed. |
| 1130 |
*/ |
| 1131 |
|
| 1132 |
if (post_read_steps || f2fs_compressed_file(inode)) { |
| 1133 |
/* Due to the mempool, this never fails. */ |
| 1134 |
ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS); |
| 1135 |
ctx->bio = bio; |
| 1136 |
ctx->sbi = sbi; |
| 1137 |
ctx->vi = vi; |
| 1138 |
ctx->enabled_steps = post_read_steps; |
| 1139 |
ctx->fs_blkaddr = blkaddr; |
| 1140 |
ctx->decompression_attempted = false; |
| 1141 |
bio->bi_private = ctx; |
| 1142 |
} |
| 1143 |
iostat_alloc_and_bind_ctx(sbi, bio, ctx); |
| 1144 |
|
| 1145 |
return bio; |
| 1146 |
} |
| 1147 |
|
| 1148 |
/* This can handle encryption stuffs */ |
| 1149 |
static void f2fs_submit_page_read(struct inode *inode, struct fsverity_info *vi, |
| 1150 |
struct folio *folio, block_t blkaddr, |
| 1151 |
blk_opf_t op_flags, bool for_write) |
| 1152 |
{ |
| 1153 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1154 |
struct bio *bio; |
| 1155 |
|
| 1156 |
bio = f2fs_grab_read_bio(inode, vi, blkaddr, 1, op_flags, folio->index, |
| 1157 |
for_write); |
| 1158 |
|
| 1159 |
/* wait for GCed page writeback via META_MAPPING */ |
| 1160 |
f2fs_wait_on_block_writeback(inode, blkaddr); |
| 1161 |
|
| 1162 |
if (!bio_add_folio(bio, folio, PAGE_SIZE, 0)) |
| 1163 |
f2fs_bug_on(sbi, 1); |
| 1164 |
|
| 1165 |
inc_page_count(sbi, F2FS_RD_DATA); |
| 1166 |
f2fs_update_iostat(sbi, NULL, FS_DATA_READ_IO, F2FS_BLKSIZE); |
| 1167 |
f2fs_submit_read_bio(sbi, bio, DATA); |
| 1168 |
} |
| 1169 |
|
| 1170 |
static void __set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) |
| 1171 |
{ |
| 1172 |
__le32 *addr = get_dnode_addr(dn->inode, dn->node_folio); |
| 1173 |
|
| 1174 |
dn->data_blkaddr = blkaddr; |
| 1175 |
addr[dn->ofs_in_node] = cpu_to_le32(dn->data_blkaddr); |
| 1176 |
} |
| 1177 |
|
| 1178 |
/* |
| 1179 |
* Lock ordering for the change of data block address: |
| 1180 |
* ->data_page |
| 1181 |
* ->node_folio |
| 1182 |
* update block addresses in the node page |
| 1183 |
*/ |
| 1184 |
void f2fs_set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) |
| 1185 |
{ |
| 1186 |
f2fs_folio_wait_writeback(dn->node_folio, NODE, true, true); |
| 1187 |
__set_data_blkaddr(dn, blkaddr); |
| 1188 |
if (folio_mark_dirty(dn->node_folio)) |
| 1189 |
dn->node_changed = true; |
| 1190 |
} |
| 1191 |
|
| 1192 |
void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr) |
| 1193 |
{ |
| 1194 |
f2fs_set_data_blkaddr(dn, blkaddr); |
| 1195 |
f2fs_update_read_extent_cache(dn); |
| 1196 |
} |
| 1197 |
|
| 1198 |
/* dn->ofs_in_node will be returned with up-to-date last block pointer */ |
| 1199 |
int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count) |
| 1200 |
{ |
| 1201 |
struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
| 1202 |
int err; |
| 1203 |
|
| 1204 |
if (!count) |
| 1205 |
return 0; |
| 1206 |
|
| 1207 |
if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
| 1208 |
return -EPERM; |
| 1209 |
err = inc_valid_block_count(sbi, dn->inode, &count, true); |
| 1210 |
if (unlikely(err)) |
| 1211 |
return err; |
| 1212 |
|
| 1213 |
trace_f2fs_reserve_new_blocks(dn->inode, dn->nid, |
| 1214 |
dn->ofs_in_node, count); |
| 1215 |
|
| 1216 |
f2fs_folio_wait_writeback(dn->node_folio, NODE, true, true); |
| 1217 |
|
| 1218 |
for (; count > 0; dn->ofs_in_node++) { |
| 1219 |
block_t blkaddr = f2fs_data_blkaddr(dn); |
| 1220 |
|
| 1221 |
if (blkaddr == NULL_ADDR) { |
| 1222 |
__set_data_blkaddr(dn, NEW_ADDR); |
| 1223 |
count--; |
| 1224 |
} |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (folio_mark_dirty(dn->node_folio)) |
| 1228 |
dn->node_changed = true; |
| 1229 |
return 0; |
| 1230 |
} |
| 1231 |
|
| 1232 |
/* Should keep dn->ofs_in_node unchanged */ |
| 1233 |
int f2fs_reserve_new_block(struct dnode_of_data *dn) |
| 1234 |
{ |
| 1235 |
unsigned int ofs_in_node = dn->ofs_in_node; |
| 1236 |
int ret; |
| 1237 |
|
| 1238 |
ret = f2fs_reserve_new_blocks(dn, 1); |
| 1239 |
dn->ofs_in_node = ofs_in_node; |
| 1240 |
return ret; |
| 1241 |
} |
| 1242 |
|
| 1243 |
int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index) |
| 1244 |
{ |
| 1245 |
bool need_put = dn->inode_folio ? false : true; |
| 1246 |
int err; |
| 1247 |
|
| 1248 |
err = f2fs_get_dnode_of_data(dn, index, ALLOC_NODE); |
| 1249 |
if (err) |
| 1250 |
return err; |
| 1251 |
|
| 1252 |
if (dn->data_blkaddr == NULL_ADDR) |
| 1253 |
err = f2fs_reserve_new_block(dn); |
| 1254 |
if (err || need_put) |
| 1255 |
f2fs_put_dnode(dn); |
| 1256 |
return err; |
| 1257 |
} |
| 1258 |
|
| 1259 |
static inline struct fsverity_info *f2fs_need_verity(const struct inode *inode, |
| 1260 |
pgoff_t idx) |
| 1261 |
{ |
| 1262 |
if (idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE)) |
| 1263 |
return fsverity_get_info(inode); |
| 1264 |
return NULL; |
| 1265 |
} |
| 1266 |
|
| 1267 |
struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index, |
| 1268 |
blk_opf_t op_flags, bool for_write, pgoff_t *next_pgofs) |
| 1269 |
{ |
| 1270 |
struct address_space *mapping = inode->i_mapping; |
| 1271 |
struct dnode_of_data dn; |
| 1272 |
struct folio *folio; |
| 1273 |
int err; |
| 1274 |
retry: |
| 1275 |
folio = f2fs_grab_cache_folio(mapping, index, for_write); |
| 1276 |
if (IS_ERR(folio)) |
| 1277 |
return folio; |
| 1278 |
|
| 1279 |
if (folio_test_large(folio)) { |
| 1280 |
pgoff_t folio_index = mapping_align_index(mapping, index); |
| 1281 |
|
| 1282 |
f2fs_folio_put(folio, true); |
| 1283 |
invalidate_inode_pages2_range(mapping, folio_index, |
| 1284 |
folio_index + folio_nr_pages(folio) - 1); |
| 1285 |
f2fs_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT); |
| 1286 |
goto retry; |
| 1287 |
} |
| 1288 |
|
| 1289 |
if (f2fs_lookup_read_extent_cache_block(inode, index, |
| 1290 |
&dn.data_blkaddr)) { |
| 1291 |
if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), dn.data_blkaddr, |
| 1292 |
DATA_GENERIC_ENHANCE_READ)) { |
| 1293 |
err = -EFSCORRUPTED; |
| 1294 |
goto put_err; |
| 1295 |
} |
| 1296 |
goto got_it; |
| 1297 |
} |
| 1298 |
|
| 1299 |
set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 1300 |
err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 1301 |
if (err) { |
| 1302 |
if (err == -ENOENT && next_pgofs) |
| 1303 |
*next_pgofs = f2fs_get_next_page_offset(&dn, index); |
| 1304 |
goto put_err; |
| 1305 |
} |
| 1306 |
f2fs_put_dnode(&dn); |
| 1307 |
|
| 1308 |
if (unlikely(dn.data_blkaddr == NULL_ADDR)) { |
| 1309 |
err = -ENOENT; |
| 1310 |
if (next_pgofs) |
| 1311 |
*next_pgofs = index + 1; |
| 1312 |
goto put_err; |
| 1313 |
} |
| 1314 |
if (dn.data_blkaddr != NEW_ADDR && |
| 1315 |
!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), |
| 1316 |
dn.data_blkaddr, |
| 1317 |
DATA_GENERIC_ENHANCE)) { |
| 1318 |
err = -EFSCORRUPTED; |
| 1319 |
goto put_err; |
| 1320 |
} |
| 1321 |
got_it: |
| 1322 |
if (folio_test_uptodate(folio)) { |
| 1323 |
folio_unlock(folio); |
| 1324 |
return folio; |
| 1325 |
} |
| 1326 |
|
| 1327 |
/* |
| 1328 |
* A new dentry page is allocated but not able to be written, since its |
| 1329 |
* new inode page couldn't be allocated due to -ENOSPC. |
| 1330 |
* In such the case, its blkaddr can be remained as NEW_ADDR. |
| 1331 |
* see, f2fs_add_link -> f2fs_get_new_data_folio -> |
| 1332 |
* f2fs_init_inode_metadata. |
| 1333 |
*/ |
| 1334 |
if (dn.data_blkaddr == NEW_ADDR) { |
| 1335 |
folio_zero_segment(folio, 0, folio_size(folio)); |
| 1336 |
if (!folio_test_uptodate(folio)) |
| 1337 |
folio_mark_uptodate(folio); |
| 1338 |
folio_unlock(folio); |
| 1339 |
return folio; |
| 1340 |
} |
| 1341 |
|
| 1342 |
f2fs_submit_page_read(inode, f2fs_need_verity(inode, folio->index), |
| 1343 |
folio, dn.data_blkaddr, op_flags, for_write); |
| 1344 |
return folio; |
| 1345 |
|
| 1346 |
put_err: |
| 1347 |
f2fs_folio_put(folio, true); |
| 1348 |
return ERR_PTR(err); |
| 1349 |
} |
| 1350 |
|
| 1351 |
struct folio *f2fs_find_data_folio(struct inode *inode, pgoff_t index, |
| 1352 |
pgoff_t *next_pgofs) |
| 1353 |
{ |
| 1354 |
struct address_space *mapping = inode->i_mapping; |
| 1355 |
struct folio *folio; |
| 1356 |
|
| 1357 |
folio = f2fs_filemap_get_folio(mapping, index, FGP_ACCESSED, 0); |
| 1358 |
if (IS_ERR(folio)) |
| 1359 |
goto read; |
| 1360 |
if (folio_test_uptodate(folio)) |
| 1361 |
return folio; |
| 1362 |
f2fs_folio_put(folio, false); |
| 1363 |
|
| 1364 |
read: |
| 1365 |
folio = f2fs_get_read_data_folio(inode, index, 0, false, next_pgofs); |
| 1366 |
if (IS_ERR(folio)) |
| 1367 |
return folio; |
| 1368 |
|
| 1369 |
if (folio_test_uptodate(folio)) |
| 1370 |
return folio; |
| 1371 |
|
| 1372 |
folio_wait_locked(folio); |
| 1373 |
if (unlikely(!folio_test_uptodate(folio))) { |
| 1374 |
f2fs_folio_put(folio, false); |
| 1375 |
return ERR_PTR(-EIO); |
| 1376 |
} |
| 1377 |
return folio; |
| 1378 |
} |
| 1379 |
|
| 1380 |
/* |
| 1381 |
* If it tries to access a hole, return an error. |
| 1382 |
* Because, the callers, functions in dir.c and GC, should be able to know |
| 1383 |
* whether this page exists or not. |
| 1384 |
*/ |
| 1385 |
struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index, |
| 1386 |
bool for_write) |
| 1387 |
{ |
| 1388 |
struct address_space *mapping = inode->i_mapping; |
| 1389 |
struct folio *folio; |
| 1390 |
|
| 1391 |
folio = f2fs_get_read_data_folio(inode, index, 0, for_write, NULL); |
| 1392 |
if (IS_ERR(folio)) |
| 1393 |
return folio; |
| 1394 |
|
| 1395 |
/* wait for read completion */ |
| 1396 |
folio_lock(folio); |
| 1397 |
if (unlikely(folio->mapping != mapping || !folio_test_uptodate(folio))) { |
| 1398 |
f2fs_folio_put(folio, true); |
| 1399 |
return ERR_PTR(-EIO); |
| 1400 |
} |
| 1401 |
return folio; |
| 1402 |
} |
| 1403 |
|
| 1404 |
/* |
| 1405 |
* Caller ensures that this data page is never allocated. |
| 1406 |
* A new zero-filled data page is allocated in the page cache. |
| 1407 |
* |
| 1408 |
* Also, caller should grab and release a rwsem by calling f2fs_lock_op() and |
| 1409 |
* f2fs_unlock_op(). |
| 1410 |
* Note that, ifolio is set only by make_empty_dir, and if any error occur, |
| 1411 |
* ifolio should be released by this function. |
| 1412 |
*/ |
| 1413 |
struct folio *f2fs_get_new_data_folio(struct inode *inode, |
| 1414 |
struct folio *ifolio, pgoff_t index, bool new_i_size) |
| 1415 |
{ |
| 1416 |
struct address_space *mapping = inode->i_mapping; |
| 1417 |
struct folio *folio; |
| 1418 |
struct dnode_of_data dn; |
| 1419 |
int err; |
| 1420 |
|
| 1421 |
folio = f2fs_grab_cache_folio(mapping, index, true); |
| 1422 |
if (IS_ERR(folio)) { |
| 1423 |
/* |
| 1424 |
* before exiting, we should make sure ifolio will be released |
| 1425 |
* if any error occur. |
| 1426 |
*/ |
| 1427 |
f2fs_folio_put(ifolio, true); |
| 1428 |
return ERR_PTR(-ENOMEM); |
| 1429 |
} |
| 1430 |
|
| 1431 |
set_new_dnode(&dn, inode, ifolio, NULL, 0); |
| 1432 |
err = f2fs_reserve_block(&dn, index); |
| 1433 |
if (err) { |
| 1434 |
f2fs_folio_put(folio, true); |
| 1435 |
return ERR_PTR(err); |
| 1436 |
} |
| 1437 |
if (!ifolio) |
| 1438 |
f2fs_put_dnode(&dn); |
| 1439 |
|
| 1440 |
if (folio_test_uptodate(folio)) |
| 1441 |
goto got_it; |
| 1442 |
|
| 1443 |
if (dn.data_blkaddr == NEW_ADDR) { |
| 1444 |
folio_zero_segment(folio, 0, folio_size(folio)); |
| 1445 |
if (!folio_test_uptodate(folio)) |
| 1446 |
folio_mark_uptodate(folio); |
| 1447 |
} else { |
| 1448 |
f2fs_folio_put(folio, true); |
| 1449 |
|
| 1450 |
/* if ifolio exists, blkaddr should be NEW_ADDR */ |
| 1451 |
f2fs_bug_on(F2FS_I_SB(inode), ifolio); |
| 1452 |
folio = f2fs_get_lock_data_folio(inode, index, true); |
| 1453 |
if (IS_ERR(folio)) |
| 1454 |
return folio; |
| 1455 |
} |
| 1456 |
got_it: |
| 1457 |
if (new_i_size && i_size_read(inode) < |
| 1458 |
((loff_t)(index + 1) << PAGE_SHIFT)) |
| 1459 |
f2fs_i_size_write(inode, ((loff_t)(index + 1) << PAGE_SHIFT)); |
| 1460 |
return folio; |
| 1461 |
} |
| 1462 |
|
| 1463 |
static int __allocate_data_block(struct dnode_of_data *dn, int seg_type) |
| 1464 |
{ |
| 1465 |
struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
| 1466 |
struct f2fs_summary sum; |
| 1467 |
struct node_info ni; |
| 1468 |
block_t old_blkaddr; |
| 1469 |
blkcnt_t count = 1; |
| 1470 |
int err; |
| 1471 |
|
| 1472 |
if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) |
| 1473 |
return -EPERM; |
| 1474 |
|
| 1475 |
err = f2fs_get_node_info(sbi, dn->nid, &ni, false); |
| 1476 |
if (err) |
| 1477 |
return err; |
| 1478 |
|
| 1479 |
dn->data_blkaddr = f2fs_data_blkaddr(dn); |
| 1480 |
if (dn->data_blkaddr == NULL_ADDR) { |
| 1481 |
err = inc_valid_block_count(sbi, dn->inode, &count, true); |
| 1482 |
if (unlikely(err)) |
| 1483 |
return err; |
| 1484 |
} |
| 1485 |
|
| 1486 |
set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); |
| 1487 |
old_blkaddr = dn->data_blkaddr; |
| 1488 |
err = f2fs_allocate_data_block(sbi, NULL, old_blkaddr, |
| 1489 |
&dn->data_blkaddr, &sum, seg_type, NULL); |
| 1490 |
if (err) |
| 1491 |
return err; |
| 1492 |
|
| 1493 |
if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) |
| 1494 |
f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1); |
| 1495 |
|
| 1496 |
f2fs_update_data_blkaddr(dn, dn->data_blkaddr); |
| 1497 |
return 0; |
| 1498 |
} |
| 1499 |
|
| 1500 |
static void f2fs_map_lock(struct f2fs_sb_info *sbi, |
| 1501 |
struct f2fs_lock_context *lc, |
| 1502 |
int flag) |
| 1503 |
{ |
| 1504 |
if (flag == F2FS_GET_BLOCK_PRE_AIO) |
| 1505 |
f2fs_down_read_trace(&sbi->node_change, lc); |
| 1506 |
else |
| 1507 |
f2fs_lock_op(sbi, lc); |
| 1508 |
} |
| 1509 |
|
| 1510 |
static void f2fs_map_unlock(struct f2fs_sb_info *sbi, |
| 1511 |
struct f2fs_lock_context *lc, |
| 1512 |
int flag) |
| 1513 |
{ |
| 1514 |
if (flag == F2FS_GET_BLOCK_PRE_AIO) |
| 1515 |
f2fs_up_read_trace(&sbi->node_change, lc); |
| 1516 |
else |
| 1517 |
f2fs_unlock_op(sbi, lc); |
| 1518 |
} |
| 1519 |
|
| 1520 |
int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index) |
| 1521 |
{ |
| 1522 |
struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
| 1523 |
struct f2fs_lock_context lc; |
| 1524 |
int err = 0; |
| 1525 |
|
| 1526 |
f2fs_map_lock(sbi, &lc, F2FS_GET_BLOCK_PRE_AIO); |
| 1527 |
if (!f2fs_lookup_read_extent_cache_block(dn->inode, index, |
| 1528 |
&dn->data_blkaddr)) |
| 1529 |
err = f2fs_reserve_block(dn, index); |
| 1530 |
f2fs_map_unlock(sbi, &lc, F2FS_GET_BLOCK_PRE_AIO); |
| 1531 |
|
| 1532 |
return err; |
| 1533 |
} |
| 1534 |
|
| 1535 |
static int f2fs_map_no_dnode(struct inode *inode, |
| 1536 |
struct f2fs_map_blocks *map, struct dnode_of_data *dn, |
| 1537 |
pgoff_t pgoff) |
| 1538 |
{ |
| 1539 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1540 |
|
| 1541 |
/* |
| 1542 |
* There is one exceptional case that read_node_page() may return |
| 1543 |
* -ENOENT due to filesystem has been shutdown or cp_error, return |
| 1544 |
* -EIO in that case. |
| 1545 |
*/ |
| 1546 |
if (map->m_may_create && |
| 1547 |
(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || f2fs_cp_error(sbi))) |
| 1548 |
return -EIO; |
| 1549 |
|
| 1550 |
if (map->m_next_pgofs) |
| 1551 |
*map->m_next_pgofs = f2fs_get_next_page_offset(dn, pgoff); |
| 1552 |
if (map->m_next_extent) |
| 1553 |
*map->m_next_extent = f2fs_get_next_page_offset(dn, pgoff); |
| 1554 |
return 0; |
| 1555 |
} |
| 1556 |
|
| 1557 |
static bool f2fs_map_blocks_cached(struct inode *inode, |
| 1558 |
struct f2fs_map_blocks *map, int flag) |
| 1559 |
{ |
| 1560 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1561 |
unsigned int maxblocks = map->m_len; |
| 1562 |
pgoff_t pgoff = (pgoff_t)map->m_lblk; |
| 1563 |
struct extent_info ei = {}; |
| 1564 |
|
| 1565 |
if (!f2fs_lookup_read_extent_cache(inode, pgoff, &ei)) |
| 1566 |
return false; |
| 1567 |
|
| 1568 |
map->m_pblk = ei.blk + pgoff - ei.fofs; |
| 1569 |
map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgoff); |
| 1570 |
map->m_flags = F2FS_MAP_MAPPED; |
| 1571 |
if (map->m_next_extent) |
| 1572 |
*map->m_next_extent = pgoff + map->m_len; |
| 1573 |
|
| 1574 |
/* for hardware encryption, but to avoid potential issue in future */ |
| 1575 |
if (flag == F2FS_GET_BLOCK_DIO) |
| 1576 |
f2fs_wait_on_block_writeback_range(inode, |
| 1577 |
map->m_pblk, map->m_len); |
| 1578 |
|
| 1579 |
map->m_multidev_dio = f2fs_allow_multi_device_dio(sbi, flag); |
| 1580 |
if (map->m_multidev_dio) { |
| 1581 |
int bidx = f2fs_target_device_index(sbi, map->m_pblk); |
| 1582 |
struct f2fs_dev_info *dev = &sbi->devs[bidx]; |
| 1583 |
|
| 1584 |
map->m_bdev = dev->bdev; |
| 1585 |
map->m_len = min(map->m_len, dev->end_blk + 1 - map->m_pblk); |
| 1586 |
map->m_pblk -= dev->start_blk; |
| 1587 |
} else { |
| 1588 |
map->m_bdev = inode->i_sb->s_bdev; |
| 1589 |
} |
| 1590 |
return true; |
| 1591 |
} |
| 1592 |
|
| 1593 |
static bool map_is_mergeable(struct f2fs_sb_info *sbi, |
| 1594 |
struct f2fs_map_blocks *map, |
| 1595 |
block_t blkaddr, int flag, int bidx, |
| 1596 |
int ofs) |
| 1597 |
{ |
| 1598 |
if (map->m_multidev_dio && map->m_bdev != FDEV(bidx).bdev) |
| 1599 |
return false; |
| 1600 |
if (map->m_pblk != NEW_ADDR && blkaddr == (map->m_pblk + ofs)) |
| 1601 |
return true; |
| 1602 |
if (map->m_pblk == NEW_ADDR && blkaddr == NEW_ADDR) |
| 1603 |
return true; |
| 1604 |
if (flag == F2FS_GET_BLOCK_PRE_DIO) |
| 1605 |
return true; |
| 1606 |
if (flag == F2FS_GET_BLOCK_DIO && |
| 1607 |
map->m_pblk == NULL_ADDR && blkaddr == NULL_ADDR) |
| 1608 |
return true; |
| 1609 |
return false; |
| 1610 |
} |
| 1611 |
|
| 1612 |
/* |
| 1613 |
* f2fs_map_blocks() tries to find or build mapping relationship which |
| 1614 |
* maps continuous logical blocks to physical blocks, and return such |
| 1615 |
* info via f2fs_map_blocks structure. |
| 1616 |
*/ |
| 1617 |
int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag) |
| 1618 |
{ |
| 1619 |
unsigned int maxblocks = map->m_len; |
| 1620 |
struct dnode_of_data dn; |
| 1621 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1622 |
struct f2fs_lock_context lc; |
| 1623 |
int mode = map->m_may_create ? ALLOC_NODE : LOOKUP_NODE; |
| 1624 |
pgoff_t pgofs, end_offset, end; |
| 1625 |
int err = 0, ofs = 1; |
| 1626 |
unsigned int ofs_in_node, last_ofs_in_node; |
| 1627 |
blkcnt_t prealloc; |
| 1628 |
block_t blkaddr; |
| 1629 |
unsigned int start_pgofs; |
| 1630 |
int bidx = 0; |
| 1631 |
bool is_hole; |
| 1632 |
bool lfs_dio_write; |
| 1633 |
|
| 1634 |
if (!maxblocks) |
| 1635 |
return 0; |
| 1636 |
|
| 1637 |
lfs_dio_write = (flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) && |
| 1638 |
map->m_may_create); |
| 1639 |
|
| 1640 |
if (!map->m_may_create && f2fs_map_blocks_cached(inode, map, flag)) { |
| 1641 |
struct extent_info ei; |
| 1642 |
|
| 1643 |
/* |
| 1644 |
* 1. If map->m_multidev_dio is true, map->m_pblk cannot be |
| 1645 |
* waitted by f2fs_wait_on_block_writeback_range() and are not |
| 1646 |
* mergeable. |
| 1647 |
* 2. If pgofs hits the read extent cache, it means the mapping |
| 1648 |
* is already cached in the extent cache, but it is not |
| 1649 |
* mergeable, and there is no need to query the mapping again |
| 1650 |
* via f2fs_get_dnode_of_data(). |
| 1651 |
*/ |
| 1652 |
pgofs = (pgoff_t)map->m_lblk + map->m_len; |
| 1653 |
if (map->m_len == maxblocks || |
| 1654 |
map->m_multidev_dio || |
| 1655 |
f2fs_lookup_read_extent_cache(inode, pgofs, &ei)) |
| 1656 |
goto out; |
| 1657 |
ofs = map->m_len; |
| 1658 |
goto map_more; |
| 1659 |
} |
| 1660 |
|
| 1661 |
map->m_bdev = inode->i_sb->s_bdev; |
| 1662 |
map->m_multidev_dio = |
| 1663 |
f2fs_allow_multi_device_dio(F2FS_I_SB(inode), flag); |
| 1664 |
|
| 1665 |
map->m_len = 0; |
| 1666 |
map->m_flags = 0; |
| 1667 |
|
| 1668 |
/* it only supports block size == page size */ |
| 1669 |
pgofs = (pgoff_t)map->m_lblk; |
| 1670 |
map_more: |
| 1671 |
end = (pgoff_t)map->m_lblk + maxblocks; |
| 1672 |
|
| 1673 |
if (flag == F2FS_GET_BLOCK_PRECACHE) |
| 1674 |
mode = LOOKUP_NODE_RA; |
| 1675 |
|
| 1676 |
next_dnode: |
| 1677 |
if (map->m_may_create) { |
| 1678 |
if (f2fs_lfs_mode(sbi)) |
| 1679 |
f2fs_balance_fs(sbi, true); |
| 1680 |
f2fs_map_lock(sbi, &lc, flag); |
| 1681 |
} |
| 1682 |
|
| 1683 |
/* When reading holes, we need its node page */ |
| 1684 |
set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 1685 |
err = f2fs_get_dnode_of_data(&dn, pgofs, mode); |
| 1686 |
if (err) { |
| 1687 |
if (flag == F2FS_GET_BLOCK_BMAP) |
| 1688 |
map->m_pblk = 0; |
| 1689 |
if (err == -ENOENT) |
| 1690 |
err = f2fs_map_no_dnode(inode, map, &dn, pgofs); |
| 1691 |
goto unlock_out; |
| 1692 |
} |
| 1693 |
|
| 1694 |
start_pgofs = pgofs; |
| 1695 |
prealloc = 0; |
| 1696 |
last_ofs_in_node = ofs_in_node = dn.ofs_in_node; |
| 1697 |
end_offset = ADDRS_PER_PAGE(dn.node_folio, inode); |
| 1698 |
|
| 1699 |
next_block: |
| 1700 |
blkaddr = f2fs_data_blkaddr(&dn); |
| 1701 |
is_hole = !__is_valid_data_blkaddr(blkaddr); |
| 1702 |
if (!is_hole && |
| 1703 |
!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC_ENHANCE)) { |
| 1704 |
err = -EFSCORRUPTED; |
| 1705 |
goto sync_out; |
| 1706 |
} |
| 1707 |
|
| 1708 |
/* use out-place-update for direct IO under LFS mode */ |
| 1709 |
if (map->m_may_create && (is_hole || |
| 1710 |
(flag == F2FS_GET_BLOCK_DIO && f2fs_lfs_mode(sbi) && |
| 1711 |
!f2fs_is_pinned_file(inode) && map->m_last_pblk != blkaddr))) { |
| 1712 |
if (unlikely(f2fs_cp_error(sbi))) { |
| 1713 |
err = -EIO; |
| 1714 |
goto sync_out; |
| 1715 |
} |
| 1716 |
|
| 1717 |
switch (flag) { |
| 1718 |
case F2FS_GET_BLOCK_PRE_AIO: |
| 1719 |
if (blkaddr == NULL_ADDR) { |
| 1720 |
prealloc++; |
| 1721 |
last_ofs_in_node = dn.ofs_in_node; |
| 1722 |
} |
| 1723 |
break; |
| 1724 |
case F2FS_GET_BLOCK_PRE_DIO: |
| 1725 |
case F2FS_GET_BLOCK_DIO: |
| 1726 |
err = __allocate_data_block(&dn, map->m_seg_type); |
| 1727 |
if (err) |
| 1728 |
goto sync_out; |
| 1729 |
if (flag == F2FS_GET_BLOCK_PRE_DIO) |
| 1730 |
file_need_truncate(inode); |
| 1731 |
set_inode_flag(inode, FI_APPEND_WRITE); |
| 1732 |
break; |
| 1733 |
default: |
| 1734 |
WARN_ON_ONCE(1); |
| 1735 |
err = -EIO; |
| 1736 |
goto sync_out; |
| 1737 |
} |
| 1738 |
|
| 1739 |
blkaddr = dn.data_blkaddr; |
| 1740 |
if (is_hole) |
| 1741 |
map->m_flags |= F2FS_MAP_NEW; |
| 1742 |
} else if (is_hole) { |
| 1743 |
if (f2fs_compressed_file(inode) && |
| 1744 |
f2fs_sanity_check_cluster(&dn)) { |
| 1745 |
err = -EFSCORRUPTED; |
| 1746 |
f2fs_handle_error(sbi, |
| 1747 |
ERROR_CORRUPTED_CLUSTER); |
| 1748 |
goto sync_out; |
| 1749 |
} |
| 1750 |
|
| 1751 |
switch (flag) { |
| 1752 |
case F2FS_GET_BLOCK_PRECACHE: |
| 1753 |
goto sync_out; |
| 1754 |
case F2FS_GET_BLOCK_BMAP: |
| 1755 |
map->m_pblk = 0; |
| 1756 |
goto sync_out; |
| 1757 |
case F2FS_GET_BLOCK_FIEMAP: |
| 1758 |
if (blkaddr == NULL_ADDR) { |
| 1759 |
if (map->m_next_pgofs) |
| 1760 |
*map->m_next_pgofs = pgofs + 1; |
| 1761 |
goto sync_out; |
| 1762 |
} |
| 1763 |
break; |
| 1764 |
case F2FS_GET_BLOCK_DIO: |
| 1765 |
if (map->m_next_pgofs) |
| 1766 |
*map->m_next_pgofs = pgofs + 1; |
| 1767 |
break; |
| 1768 |
default: |
| 1769 |
/* for defragment case */ |
| 1770 |
if (map->m_next_pgofs) |
| 1771 |
*map->m_next_pgofs = pgofs + 1; |
| 1772 |
goto sync_out; |
| 1773 |
} |
| 1774 |
} |
| 1775 |
|
| 1776 |
if (flag == F2FS_GET_BLOCK_PRE_AIO) |
| 1777 |
goto skip; |
| 1778 |
|
| 1779 |
if (map->m_multidev_dio) |
| 1780 |
bidx = f2fs_target_device_index(sbi, blkaddr); |
| 1781 |
|
| 1782 |
if (map->m_len == 0) { |
| 1783 |
/* reserved delalloc block should be mapped for fiemap. */ |
| 1784 |
if (blkaddr == NEW_ADDR) |
| 1785 |
map->m_flags |= F2FS_MAP_DELALLOC; |
| 1786 |
/* DIO READ and hole case, should not map the blocks. */ |
| 1787 |
if (!(flag == F2FS_GET_BLOCK_DIO && is_hole && !map->m_may_create)) |
| 1788 |
map->m_flags |= F2FS_MAP_MAPPED; |
| 1789 |
|
| 1790 |
map->m_pblk = blkaddr; |
| 1791 |
map->m_len = 1; |
| 1792 |
|
| 1793 |
if (map->m_multidev_dio) |
| 1794 |
map->m_bdev = FDEV(bidx).bdev; |
| 1795 |
|
| 1796 |
if (lfs_dio_write) |
| 1797 |
map->m_last_pblk = NULL_ADDR; |
| 1798 |
} else if (map_is_mergeable(sbi, map, blkaddr, flag, bidx, ofs)) { |
| 1799 |
ofs++; |
| 1800 |
map->m_len++; |
| 1801 |
} else { |
| 1802 |
if (lfs_dio_write && !f2fs_is_pinned_file(inode)) |
| 1803 |
map->m_last_pblk = blkaddr; |
| 1804 |
goto sync_out; |
| 1805 |
} |
| 1806 |
|
| 1807 |
skip: |
| 1808 |
dn.ofs_in_node++; |
| 1809 |
pgofs++; |
| 1810 |
|
| 1811 |
/* preallocate blocks in batch for one dnode page */ |
| 1812 |
if (flag == F2FS_GET_BLOCK_PRE_AIO && |
| 1813 |
(pgofs == end || dn.ofs_in_node == end_offset)) { |
| 1814 |
|
| 1815 |
dn.ofs_in_node = ofs_in_node; |
| 1816 |
err = f2fs_reserve_new_blocks(&dn, prealloc); |
| 1817 |
if (err) |
| 1818 |
goto sync_out; |
| 1819 |
|
| 1820 |
map->m_len += dn.ofs_in_node - ofs_in_node; |
| 1821 |
if (prealloc && dn.ofs_in_node != last_ofs_in_node + 1) { |
| 1822 |
err = -ENOSPC; |
| 1823 |
goto sync_out; |
| 1824 |
} |
| 1825 |
dn.ofs_in_node = end_offset; |
| 1826 |
} |
| 1827 |
|
| 1828 |
if (pgofs >= end) |
| 1829 |
goto sync_out; |
| 1830 |
else if (dn.ofs_in_node < end_offset) |
| 1831 |
goto next_block; |
| 1832 |
|
| 1833 |
if (flag == F2FS_GET_BLOCK_PRECACHE) { |
| 1834 |
if (map->m_flags & F2FS_MAP_MAPPED) { |
| 1835 |
unsigned int ofs = start_pgofs - map->m_lblk; |
| 1836 |
|
| 1837 |
f2fs_update_read_extent_cache_range(&dn, |
| 1838 |
start_pgofs, map->m_pblk + ofs, |
| 1839 |
map->m_len - ofs); |
| 1840 |
} |
| 1841 |
} |
| 1842 |
|
| 1843 |
f2fs_put_dnode(&dn); |
| 1844 |
|
| 1845 |
if (map->m_may_create) { |
| 1846 |
f2fs_map_unlock(sbi, &lc, flag); |
| 1847 |
f2fs_balance_fs(sbi, dn.node_changed); |
| 1848 |
} |
| 1849 |
goto next_dnode; |
| 1850 |
|
| 1851 |
sync_out: |
| 1852 |
|
| 1853 |
if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) { |
| 1854 |
/* |
| 1855 |
* for hardware encryption, but to avoid potential issue |
| 1856 |
* in future |
| 1857 |
*/ |
| 1858 |
f2fs_wait_on_block_writeback_range(inode, |
| 1859 |
map->m_pblk, map->m_len); |
| 1860 |
|
| 1861 |
if (map->m_multidev_dio) { |
| 1862 |
block_t blk_addr = map->m_pblk; |
| 1863 |
|
| 1864 |
bidx = f2fs_target_device_index(sbi, map->m_pblk); |
| 1865 |
|
| 1866 |
map->m_bdev = FDEV(bidx).bdev; |
| 1867 |
map->m_pblk -= FDEV(bidx).start_blk; |
| 1868 |
|
| 1869 |
if (map->m_may_create) |
| 1870 |
f2fs_update_device_state(sbi, inode->i_ino, |
| 1871 |
blk_addr, map->m_len); |
| 1872 |
|
| 1873 |
f2fs_bug_on(sbi, blk_addr + map->m_len > |
| 1874 |
FDEV(bidx).end_blk + 1); |
| 1875 |
} |
| 1876 |
} |
| 1877 |
|
| 1878 |
if (flag == F2FS_GET_BLOCK_PRECACHE) { |
| 1879 |
if (map->m_flags & F2FS_MAP_MAPPED) { |
| 1880 |
unsigned int ofs = start_pgofs - map->m_lblk; |
| 1881 |
|
| 1882 |
if (map->m_len > ofs) |
| 1883 |
f2fs_update_read_extent_cache_range(&dn, |
| 1884 |
start_pgofs, map->m_pblk + ofs, |
| 1885 |
map->m_len - ofs); |
| 1886 |
} |
| 1887 |
if (map->m_next_extent) |
| 1888 |
*map->m_next_extent = is_hole ? pgofs + 1 : pgofs; |
| 1889 |
} |
| 1890 |
f2fs_put_dnode(&dn); |
| 1891 |
unlock_out: |
| 1892 |
if (map->m_may_create) { |
| 1893 |
f2fs_map_unlock(sbi, &lc, flag); |
| 1894 |
f2fs_balance_fs(sbi, dn.node_changed); |
| 1895 |
} |
| 1896 |
out: |
| 1897 |
trace_f2fs_map_blocks(inode, map, flag, err); |
| 1898 |
return err; |
| 1899 |
} |
| 1900 |
|
| 1901 |
static bool __f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len, |
| 1902 |
bool check_first) |
| 1903 |
{ |
| 1904 |
struct f2fs_map_blocks map; |
| 1905 |
block_t last_lblk; |
| 1906 |
int err; |
| 1907 |
|
| 1908 |
if (pos + len > i_size_read(inode)) |
| 1909 |
return false; |
| 1910 |
|
| 1911 |
map.m_lblk = F2FS_BYTES_TO_BLK(pos); |
| 1912 |
map.m_next_pgofs = NULL; |
| 1913 |
map.m_next_extent = NULL; |
| 1914 |
map.m_seg_type = NO_CHECK_TYPE; |
| 1915 |
map.m_may_create = false; |
| 1916 |
last_lblk = F2FS_BLK_ALIGN(pos + len); |
| 1917 |
|
| 1918 |
while (map.m_lblk < last_lblk) { |
| 1919 |
map.m_len = last_lblk - map.m_lblk; |
| 1920 |
err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT); |
| 1921 |
if (err || map.m_len == 0) |
| 1922 |
return false; |
| 1923 |
map.m_lblk += map.m_len; |
| 1924 |
if (check_first) |
| 1925 |
break; |
| 1926 |
} |
| 1927 |
return true; |
| 1928 |
} |
| 1929 |
|
| 1930 |
bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len) |
| 1931 |
{ |
| 1932 |
return __f2fs_overwrite_io(inode, pos, len, false); |
| 1933 |
} |
| 1934 |
|
| 1935 |
static int f2fs_xattr_fiemap(struct inode *inode, |
| 1936 |
struct fiemap_extent_info *fieinfo) |
| 1937 |
{ |
| 1938 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 1939 |
struct node_info ni; |
| 1940 |
__u64 phys = 0, len; |
| 1941 |
__u32 flags; |
| 1942 |
nid_t xnid = F2FS_I(inode)->i_xattr_nid; |
| 1943 |
int err = 0; |
| 1944 |
|
| 1945 |
if (f2fs_has_inline_xattr(inode)) { |
| 1946 |
int offset; |
| 1947 |
struct folio *folio = f2fs_grab_cache_folio(NODE_MAPPING(sbi), |
| 1948 |
inode->i_ino, false); |
| 1949 |
|
| 1950 |
if (IS_ERR(folio)) |
| 1951 |
return PTR_ERR(folio); |
| 1952 |
|
| 1953 |
err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false); |
| 1954 |
if (err) { |
| 1955 |
f2fs_folio_put(folio, true); |
| 1956 |
return err; |
| 1957 |
} |
| 1958 |
|
| 1959 |
phys = F2FS_BLK_TO_BYTES(ni.blk_addr); |
| 1960 |
offset = offsetof(struct f2fs_inode, i_addr) + |
| 1961 |
sizeof(__le32) * (DEF_ADDRS_PER_INODE - |
| 1962 |
get_inline_xattr_addrs(inode)); |
| 1963 |
|
| 1964 |
phys += offset; |
| 1965 |
len = inline_xattr_size(inode); |
| 1966 |
|
| 1967 |
f2fs_folio_put(folio, true); |
| 1968 |
|
| 1969 |
flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED; |
| 1970 |
|
| 1971 |
if (!xnid) |
| 1972 |
flags |= FIEMAP_EXTENT_LAST; |
| 1973 |
|
| 1974 |
err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); |
| 1975 |
trace_f2fs_fiemap(inode, 0, phys, len, flags, err); |
| 1976 |
if (err) |
| 1977 |
return err; |
| 1978 |
} |
| 1979 |
|
| 1980 |
if (xnid) { |
| 1981 |
struct folio *folio = f2fs_grab_cache_folio(NODE_MAPPING(sbi), |
| 1982 |
xnid, false); |
| 1983 |
|
| 1984 |
if (IS_ERR(folio)) |
| 1985 |
return PTR_ERR(folio); |
| 1986 |
|
| 1987 |
err = f2fs_get_node_info(sbi, xnid, &ni, false); |
| 1988 |
if (err) { |
| 1989 |
f2fs_folio_put(folio, true); |
| 1990 |
return err; |
| 1991 |
} |
| 1992 |
|
| 1993 |
phys = F2FS_BLK_TO_BYTES(ni.blk_addr); |
| 1994 |
len = inode->i_sb->s_blocksize; |
| 1995 |
|
| 1996 |
f2fs_folio_put(folio, true); |
| 1997 |
|
| 1998 |
flags = FIEMAP_EXTENT_LAST; |
| 1999 |
} |
| 2000 |
|
| 2001 |
if (phys) { |
| 2002 |
err = fiemap_fill_next_extent(fieinfo, 0, phys, len, flags); |
| 2003 |
trace_f2fs_fiemap(inode, 0, phys, len, flags, err); |
| 2004 |
} |
| 2005 |
|
| 2006 |
return (err < 0 ? err : 0); |
| 2007 |
} |
| 2008 |
|
| 2009 |
int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 2010 |
u64 start, u64 len) |
| 2011 |
{ |
| 2012 |
struct f2fs_map_blocks map; |
| 2013 |
sector_t start_blk, last_blk, blk_len, max_len; |
| 2014 |
pgoff_t next_pgofs; |
| 2015 |
u64 logical = 0, phys = 0, size = 0; |
| 2016 |
u32 flags = 0; |
| 2017 |
int ret = 0; |
| 2018 |
bool compr_cluster = false, compr_appended; |
| 2019 |
unsigned int cluster_size = F2FS_I(inode)->i_cluster_size; |
| 2020 |
unsigned int count_in_cluster = 0; |
| 2021 |
loff_t maxbytes; |
| 2022 |
|
| 2023 |
if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { |
| 2024 |
ret = f2fs_precache_extents(inode); |
| 2025 |
if (ret) |
| 2026 |
return ret; |
| 2027 |
} |
| 2028 |
|
| 2029 |
ret = fiemap_prep(inode, fieinfo, start, &len, FIEMAP_FLAG_XATTR); |
| 2030 |
if (ret) |
| 2031 |
return ret; |
| 2032 |
|
| 2033 |
inode_lock_shared(inode); |
| 2034 |
|
| 2035 |
maxbytes = F2FS_BLK_TO_BYTES(max_file_blocks(inode)); |
| 2036 |
if (start > maxbytes) { |
| 2037 |
ret = -EFBIG; |
| 2038 |
goto out; |
| 2039 |
} |
| 2040 |
|
| 2041 |
if (len > maxbytes || (maxbytes - len) < start) |
| 2042 |
len = maxbytes - start; |
| 2043 |
|
| 2044 |
if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
| 2045 |
ret = f2fs_xattr_fiemap(inode, fieinfo); |
| 2046 |
goto out; |
| 2047 |
} |
| 2048 |
|
| 2049 |
if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) { |
| 2050 |
ret = f2fs_inline_data_fiemap(inode, fieinfo, start, len); |
| 2051 |
if (ret != -EAGAIN) |
| 2052 |
goto out; |
| 2053 |
} |
| 2054 |
|
| 2055 |
start_blk = F2FS_BYTES_TO_BLK(start); |
| 2056 |
last_blk = F2FS_BYTES_TO_BLK(start + len - 1); |
| 2057 |
blk_len = last_blk - start_blk + 1; |
| 2058 |
max_len = F2FS_BYTES_TO_BLK(maxbytes) - start_blk; |
| 2059 |
|
| 2060 |
next: |
| 2061 |
memset(&map, 0, sizeof(map)); |
| 2062 |
map.m_lblk = start_blk; |
| 2063 |
map.m_len = blk_len; |
| 2064 |
map.m_next_pgofs = &next_pgofs; |
| 2065 |
map.m_seg_type = NO_CHECK_TYPE; |
| 2066 |
|
| 2067 |
if (compr_cluster) { |
| 2068 |
map.m_lblk += 1; |
| 2069 |
map.m_len = cluster_size - count_in_cluster; |
| 2070 |
} |
| 2071 |
|
| 2072 |
ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP); |
| 2073 |
if (ret) |
| 2074 |
goto out; |
| 2075 |
|
| 2076 |
/* HOLE */ |
| 2077 |
if (!compr_cluster && !(map.m_flags & F2FS_MAP_FLAGS)) { |
| 2078 |
start_blk = next_pgofs; |
| 2079 |
|
| 2080 |
if (F2FS_BLK_TO_BYTES(start_blk) < maxbytes) |
| 2081 |
goto prep_next; |
| 2082 |
|
| 2083 |
flags |= FIEMAP_EXTENT_LAST; |
| 2084 |
} |
| 2085 |
|
| 2086 |
/* |
| 2087 |
* current extent may cross boundary of inquiry, increase len to |
| 2088 |
* requery. |
| 2089 |
*/ |
| 2090 |
if (!compr_cluster && (map.m_flags & F2FS_MAP_MAPPED) && |
| 2091 |
map.m_lblk + map.m_len - 1 == last_blk && |
| 2092 |
blk_len != max_len) { |
| 2093 |
blk_len = max_len; |
| 2094 |
goto next; |
| 2095 |
} |
| 2096 |
|
| 2097 |
compr_appended = false; |
| 2098 |
/* In a case of compressed cluster, append this to the last extent */ |
| 2099 |
if (compr_cluster && ((map.m_flags & F2FS_MAP_DELALLOC) || |
| 2100 |
!(map.m_flags & F2FS_MAP_FLAGS))) { |
| 2101 |
compr_appended = true; |
| 2102 |
goto skip_fill; |
| 2103 |
} |
| 2104 |
|
| 2105 |
if (size) { |
| 2106 |
flags |= FIEMAP_EXTENT_MERGED; |
| 2107 |
if (IS_ENCRYPTED(inode)) |
| 2108 |
flags |= FIEMAP_EXTENT_DATA_ENCRYPTED; |
| 2109 |
|
| 2110 |
ret = fiemap_fill_next_extent(fieinfo, logical, |
| 2111 |
phys, size, flags); |
| 2112 |
trace_f2fs_fiemap(inode, logical, phys, size, flags, ret); |
| 2113 |
if (ret) |
| 2114 |
goto out; |
| 2115 |
size = 0; |
| 2116 |
} |
| 2117 |
|
| 2118 |
if (start_blk > last_blk) |
| 2119 |
goto out; |
| 2120 |
|
| 2121 |
skip_fill: |
| 2122 |
if (map.m_pblk == COMPRESS_ADDR) { |
| 2123 |
compr_cluster = true; |
| 2124 |
count_in_cluster = 1; |
| 2125 |
} else if (compr_appended) { |
| 2126 |
unsigned int appended_blks = cluster_size - |
| 2127 |
count_in_cluster + 1; |
| 2128 |
size += F2FS_BLK_TO_BYTES(appended_blks); |
| 2129 |
start_blk += appended_blks; |
| 2130 |
compr_cluster = false; |
| 2131 |
} else { |
| 2132 |
logical = F2FS_BLK_TO_BYTES(start_blk); |
| 2133 |
phys = __is_valid_data_blkaddr(map.m_pblk) ? |
| 2134 |
F2FS_BLK_TO_BYTES(map.m_pblk) : 0; |
| 2135 |
size = F2FS_BLK_TO_BYTES(map.m_len); |
| 2136 |
flags = 0; |
| 2137 |
|
| 2138 |
if (compr_cluster) { |
| 2139 |
flags = FIEMAP_EXTENT_ENCODED; |
| 2140 |
count_in_cluster += map.m_len; |
| 2141 |
if (count_in_cluster == cluster_size) { |
| 2142 |
compr_cluster = false; |
| 2143 |
size += F2FS_BLKSIZE; |
| 2144 |
} |
| 2145 |
} else if (map.m_flags & F2FS_MAP_DELALLOC) { |
| 2146 |
flags = FIEMAP_EXTENT_UNWRITTEN; |
| 2147 |
} |
| 2148 |
|
| 2149 |
start_blk += F2FS_BYTES_TO_BLK(size); |
| 2150 |
} |
| 2151 |
|
| 2152 |
prep_next: |
| 2153 |
cond_resched(); |
| 2154 |
if (fatal_signal_pending(current)) |
| 2155 |
ret = -EINTR; |
| 2156 |
else |
| 2157 |
goto next; |
| 2158 |
out: |
| 2159 |
if (ret == 1) |
| 2160 |
ret = 0; |
| 2161 |
|
| 2162 |
inode_unlock_shared(inode); |
| 2163 |
return ret; |
| 2164 |
} |
| 2165 |
|
| 2166 |
static inline loff_t f2fs_readpage_limit(struct inode *inode) |
| 2167 |
{ |
| 2168 |
if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode)) |
| 2169 |
return F2FS_BLK_TO_BYTES(max_file_blocks(inode)); |
| 2170 |
|
| 2171 |
return i_size_read(inode); |
| 2172 |
} |
| 2173 |
|
| 2174 |
static inline blk_opf_t f2fs_ra_op_flags(struct readahead_control *rac) |
| 2175 |
{ |
| 2176 |
return rac ? REQ_RAHEAD : 0; |
| 2177 |
} |
| 2178 |
|
| 2179 |
static int f2fs_read_single_page(struct inode *inode, struct fsverity_info *vi, |
| 2180 |
struct folio *folio, unsigned int nr_pages, |
| 2181 |
struct f2fs_map_blocks *map, |
| 2182 |
struct bio **bio_ret, |
| 2183 |
sector_t *last_block_in_bio, |
| 2184 |
struct readahead_control *rac) |
| 2185 |
{ |
| 2186 |
struct bio *bio = *bio_ret; |
| 2187 |
const unsigned int blocksize = F2FS_BLKSIZE; |
| 2188 |
sector_t block_in_file; |
| 2189 |
sector_t last_block; |
| 2190 |
sector_t last_block_in_file; |
| 2191 |
sector_t block_nr; |
| 2192 |
pgoff_t index = folio->index; |
| 2193 |
int ret = 0; |
| 2194 |
|
| 2195 |
block_in_file = (sector_t)index; |
| 2196 |
last_block = block_in_file + nr_pages; |
| 2197 |
last_block_in_file = F2FS_BYTES_TO_BLK(f2fs_readpage_limit(inode) + |
| 2198 |
blocksize - 1); |
| 2199 |
if (last_block > last_block_in_file) |
| 2200 |
last_block = last_block_in_file; |
| 2201 |
|
| 2202 |
/* just zeroing out page which is beyond EOF */ |
| 2203 |
if (block_in_file >= last_block) |
| 2204 |
goto zero_out; |
| 2205 |
/* |
| 2206 |
* Map blocks using the previous result first. |
| 2207 |
*/ |
| 2208 |
if (map->m_flags & F2FS_MAP_MAPPED) { |
| 2209 |
if (block_in_file > map->m_lblk && |
| 2210 |
block_in_file < (map->m_lblk + map->m_len)) |
| 2211 |
goto got_it; |
| 2212 |
} else if (block_in_file < *map->m_next_pgofs) { |
| 2213 |
goto got_it; |
| 2214 |
} |
| 2215 |
|
| 2216 |
/* |
| 2217 |
* Then do more f2fs_map_blocks() calls until we are |
| 2218 |
* done with this page. |
| 2219 |
*/ |
| 2220 |
map->m_lblk = block_in_file; |
| 2221 |
map->m_len = last_block - block_in_file; |
| 2222 |
|
| 2223 |
ret = f2fs_map_blocks(inode, map, F2FS_GET_BLOCK_DEFAULT); |
| 2224 |
if (ret) |
| 2225 |
goto out; |
| 2226 |
got_it: |
| 2227 |
if ((map->m_flags & F2FS_MAP_MAPPED)) { |
| 2228 |
block_nr = map->m_pblk + block_in_file - map->m_lblk; |
| 2229 |
folio_set_mappedtodisk(folio); |
| 2230 |
|
| 2231 |
if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr, |
| 2232 |
DATA_GENERIC_ENHANCE_READ)) { |
| 2233 |
ret = -EFSCORRUPTED; |
| 2234 |
goto out; |
| 2235 |
} |
| 2236 |
} else { |
| 2237 |
zero_out: |
| 2238 |
folio_zero_segment(folio, 0, folio_size(folio)); |
| 2239 |
if (vi && !fsverity_verify_folio(vi, folio)) { |
| 2240 |
ret = -EIO; |
| 2241 |
goto out; |
| 2242 |
} |
| 2243 |
if (!folio_test_uptodate(folio)) |
| 2244 |
folio_mark_uptodate(folio); |
| 2245 |
folio_unlock(folio); |
| 2246 |
goto out; |
| 2247 |
} |
| 2248 |
|
| 2249 |
/* |
| 2250 |
* This page will go to BIO. Do we need to send this |
| 2251 |
* BIO off first? |
| 2252 |
*/ |
| 2253 |
if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio, |
| 2254 |
*last_block_in_bio, block_nr) || |
| 2255 |
!f2fs_crypt_mergeable_bio(bio, inode, index, NULL))) { |
| 2256 |
submit_and_realloc: |
| 2257 |
f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); |
| 2258 |
bio = NULL; |
| 2259 |
} |
| 2260 |
if (bio == NULL) |
| 2261 |
bio = f2fs_grab_read_bio(inode, vi, block_nr, nr_pages, |
| 2262 |
f2fs_ra_op_flags(rac), index, false); |
| 2263 |
|
| 2264 |
/* |
| 2265 |
* If the page is under writeback, we need to wait for |
| 2266 |
* its completion to see the correct decrypted data. |
| 2267 |
*/ |
| 2268 |
f2fs_wait_on_block_writeback(inode, block_nr); |
| 2269 |
|
| 2270 |
if (!bio_add_folio(bio, folio, blocksize, 0)) |
| 2271 |
goto submit_and_realloc; |
| 2272 |
|
| 2273 |
inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA); |
| 2274 |
f2fs_update_iostat(F2FS_I_SB(inode), NULL, FS_DATA_READ_IO, |
| 2275 |
F2FS_BLKSIZE); |
| 2276 |
*last_block_in_bio = block_nr; |
| 2277 |
out: |
| 2278 |
*bio_ret = bio; |
| 2279 |
return ret; |
| 2280 |
} |
| 2281 |
|
| 2282 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 2283 |
int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret, |
| 2284 |
unsigned nr_pages, sector_t *last_block_in_bio, |
| 2285 |
struct readahead_control *rac, bool for_write) |
| 2286 |
{ |
| 2287 |
struct dnode_of_data dn; |
| 2288 |
struct inode *inode = cc->inode; |
| 2289 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 2290 |
struct bio *bio = *bio_ret; |
| 2291 |
unsigned int start_idx = cc->cluster_idx << cc->log_cluster_size; |
| 2292 |
sector_t last_block_in_file; |
| 2293 |
const unsigned int blocksize = F2FS_BLKSIZE; |
| 2294 |
struct decompress_io_ctx *dic = NULL; |
| 2295 |
struct extent_info ei = {}; |
| 2296 |
bool from_dnode = true; |
| 2297 |
int i; |
| 2298 |
int ret = 0; |
| 2299 |
|
| 2300 |
if (unlikely(f2fs_cp_error(sbi))) { |
| 2301 |
ret = -EIO; |
| 2302 |
from_dnode = false; |
| 2303 |
goto out_put_dnode; |
| 2304 |
} |
| 2305 |
|
| 2306 |
f2fs_bug_on(sbi, f2fs_cluster_is_empty(cc)); |
| 2307 |
|
| 2308 |
last_block_in_file = F2FS_BYTES_TO_BLK(f2fs_readpage_limit(inode) + |
| 2309 |
blocksize - 1); |
| 2310 |
|
| 2311 |
/* get rid of pages beyond EOF */ |
| 2312 |
for (i = 0; i < cc->cluster_size; i++) { |
| 2313 |
struct page *page = cc->rpages[i]; |
| 2314 |
struct folio *folio; |
| 2315 |
|
| 2316 |
if (!page) |
| 2317 |
continue; |
| 2318 |
|
| 2319 |
folio = page_folio(page); |
| 2320 |
if ((sector_t)folio->index >= last_block_in_file) { |
| 2321 |
folio_zero_segment(folio, 0, folio_size(folio)); |
| 2322 |
if (!folio_test_uptodate(folio)) |
| 2323 |
folio_mark_uptodate(folio); |
| 2324 |
} else if (!folio_test_uptodate(folio)) { |
| 2325 |
continue; |
| 2326 |
} |
| 2327 |
folio_unlock(folio); |
| 2328 |
if (for_write) |
| 2329 |
folio_put(folio); |
| 2330 |
cc->rpages[i] = NULL; |
| 2331 |
cc->nr_rpages--; |
| 2332 |
} |
| 2333 |
|
| 2334 |
/* we are done since all pages are beyond EOF */ |
| 2335 |
if (f2fs_cluster_is_empty(cc)) |
| 2336 |
goto out; |
| 2337 |
|
| 2338 |
if (f2fs_lookup_read_extent_cache(inode, start_idx, &ei)) |
| 2339 |
from_dnode = false; |
| 2340 |
|
| 2341 |
if (!from_dnode) |
| 2342 |
goto skip_reading_dnode; |
| 2343 |
|
| 2344 |
set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 2345 |
ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); |
| 2346 |
if (ret) |
| 2347 |
goto out; |
| 2348 |
|
| 2349 |
f2fs_bug_on(sbi, dn.data_blkaddr != COMPRESS_ADDR); |
| 2350 |
|
| 2351 |
skip_reading_dnode: |
| 2352 |
for (i = 1; i < cc->cluster_size; i++) { |
| 2353 |
block_t blkaddr; |
| 2354 |
|
| 2355 |
blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_folio, |
| 2356 |
dn.ofs_in_node + i) : |
| 2357 |
ei.blk + i - 1; |
| 2358 |
|
| 2359 |
if (!__is_valid_data_blkaddr(blkaddr)) |
| 2360 |
break; |
| 2361 |
|
| 2362 |
if (!f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC)) { |
| 2363 |
ret = -EFAULT; |
| 2364 |
goto out_put_dnode; |
| 2365 |
} |
| 2366 |
cc->nr_cpages++; |
| 2367 |
|
| 2368 |
if (!from_dnode && i >= ei.c_len) |
| 2369 |
break; |
| 2370 |
} |
| 2371 |
|
| 2372 |
/* nothing to decompress */ |
| 2373 |
if (cc->nr_cpages == 0) { |
| 2374 |
ret = 0; |
| 2375 |
goto out_put_dnode; |
| 2376 |
} |
| 2377 |
|
| 2378 |
dic = f2fs_alloc_dic(cc); |
| 2379 |
if (IS_ERR(dic)) { |
| 2380 |
ret = PTR_ERR(dic); |
| 2381 |
goto out_put_dnode; |
| 2382 |
} |
| 2383 |
|
| 2384 |
for (i = 0; i < cc->nr_cpages; i++) { |
| 2385 |
struct folio *folio = page_folio(dic->cpages[i]); |
| 2386 |
block_t blkaddr; |
| 2387 |
struct bio_post_read_ctx *ctx; |
| 2388 |
|
| 2389 |
blkaddr = from_dnode ? data_blkaddr(dn.inode, dn.node_folio, |
| 2390 |
dn.ofs_in_node + i + 1) : |
| 2391 |
ei.blk + i; |
| 2392 |
|
| 2393 |
f2fs_wait_on_block_writeback(inode, blkaddr); |
| 2394 |
|
| 2395 |
if (f2fs_load_compressed_folio(sbi, folio, blkaddr)) { |
| 2396 |
if (atomic_dec_and_test(&dic->remaining_pages)) { |
| 2397 |
f2fs_decompress_cluster(dic, true); |
| 2398 |
break; |
| 2399 |
} |
| 2400 |
continue; |
| 2401 |
} |
| 2402 |
|
| 2403 |
if (bio && (!page_is_mergeable(sbi, bio, |
| 2404 |
*last_block_in_bio, blkaddr) || |
| 2405 |
!f2fs_crypt_mergeable_bio(bio, inode, folio->index, NULL))) { |
| 2406 |
submit_and_realloc: |
| 2407 |
f2fs_submit_read_bio(sbi, bio, DATA); |
| 2408 |
bio = NULL; |
| 2409 |
} |
| 2410 |
|
| 2411 |
if (!bio) |
| 2412 |
bio = f2fs_grab_read_bio(inode, cc->vi, blkaddr, |
| 2413 |
nr_pages - i, |
| 2414 |
f2fs_ra_op_flags(rac), |
| 2415 |
folio->index, for_write); |
| 2416 |
|
| 2417 |
if (!bio_add_folio(bio, folio, blocksize, 0)) |
| 2418 |
goto submit_and_realloc; |
| 2419 |
|
| 2420 |
ctx = get_post_read_ctx(bio); |
| 2421 |
ctx->enabled_steps |= STEP_DECOMPRESS; |
| 2422 |
refcount_inc(&dic->refcnt); |
| 2423 |
|
| 2424 |
inc_page_count(sbi, F2FS_RD_DATA); |
| 2425 |
f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE); |
| 2426 |
*last_block_in_bio = blkaddr; |
| 2427 |
} |
| 2428 |
|
| 2429 |
if (from_dnode) |
| 2430 |
f2fs_put_dnode(&dn); |
| 2431 |
|
| 2432 |
*bio_ret = bio; |
| 2433 |
return 0; |
| 2434 |
|
| 2435 |
out_put_dnode: |
| 2436 |
if (from_dnode) |
| 2437 |
f2fs_put_dnode(&dn); |
| 2438 |
out: |
| 2439 |
for (i = 0; i < cc->cluster_size; i++) { |
| 2440 |
if (cc->rpages[i]) { |
| 2441 |
ClearPageUptodate(cc->rpages[i]); |
| 2442 |
unlock_page(cc->rpages[i]); |
| 2443 |
} |
| 2444 |
} |
| 2445 |
*bio_ret = bio; |
| 2446 |
return ret; |
| 2447 |
} |
| 2448 |
#endif |
| 2449 |
|
| 2450 |
static struct f2fs_folio_state *ffs_find_or_alloc(struct folio *folio) |
| 2451 |
{ |
| 2452 |
struct f2fs_folio_state *ffs = folio->private; |
| 2453 |
|
| 2454 |
if (ffs) |
| 2455 |
return ffs; |
| 2456 |
|
| 2457 |
ffs = f2fs_kmem_cache_alloc(ffs_entry_slab, |
| 2458 |
GFP_NOIO | __GFP_ZERO, true, NULL); |
| 2459 |
|
| 2460 |
spin_lock_init(&ffs->state_lock); |
| 2461 |
folio_attach_private(folio, ffs); |
| 2462 |
return ffs; |
| 2463 |
} |
| 2464 |
|
| 2465 |
static void ffs_detach_free(struct folio *folio) |
| 2466 |
{ |
| 2467 |
struct f2fs_folio_state *ffs; |
| 2468 |
|
| 2469 |
if (!folio_test_large(folio)) { |
| 2470 |
folio_detach_private(folio); |
| 2471 |
return; |
| 2472 |
} |
| 2473 |
|
| 2474 |
ffs = folio_detach_private(folio); |
| 2475 |
if (!ffs) |
| 2476 |
return; |
| 2477 |
|
| 2478 |
WARN_ON_ONCE(ffs->read_pages_pending != 0); |
| 2479 |
kmem_cache_free(ffs_entry_slab, ffs); |
| 2480 |
} |
| 2481 |
|
| 2482 |
static int f2fs_read_data_large_folio(struct inode *inode, |
| 2483 |
struct fsverity_info *vi, |
| 2484 |
struct readahead_control *rac, struct folio *folio) |
| 2485 |
{ |
| 2486 |
struct bio *bio = NULL; |
| 2487 |
sector_t last_block_in_bio = 0; |
| 2488 |
struct f2fs_map_blocks map = {0, }; |
| 2489 |
pgoff_t index, offset, next_pgofs = 0; |
| 2490 |
unsigned max_nr_pages = rac ? readahead_count(rac) : |
| 2491 |
folio_nr_pages(folio); |
| 2492 |
unsigned nrpages; |
| 2493 |
struct f2fs_folio_state *ffs; |
| 2494 |
int ret = 0; |
| 2495 |
bool folio_in_bio; |
| 2496 |
|
| 2497 |
if (!IS_IMMUTABLE(inode) || f2fs_compressed_file(inode)) { |
| 2498 |
if (folio) |
| 2499 |
folio_unlock(folio); |
| 2500 |
return -EOPNOTSUPP; |
| 2501 |
} |
| 2502 |
|
| 2503 |
map.m_seg_type = NO_CHECK_TYPE; |
| 2504 |
|
| 2505 |
if (rac) |
| 2506 |
folio = readahead_folio(rac); |
| 2507 |
next_folio: |
| 2508 |
if (!folio) |
| 2509 |
goto out; |
| 2510 |
|
| 2511 |
folio_in_bio = false; |
| 2512 |
index = folio->index; |
| 2513 |
offset = 0; |
| 2514 |
ffs = NULL; |
| 2515 |
nrpages = folio_nr_pages(folio); |
| 2516 |
|
| 2517 |
for (; nrpages; nrpages--, max_nr_pages--, index++, offset++) { |
| 2518 |
sector_t block_nr; |
| 2519 |
/* |
| 2520 |
* Map blocks using the previous result first. |
| 2521 |
*/ |
| 2522 |
if (map.m_flags & F2FS_MAP_MAPPED) { |
| 2523 |
if (index > map.m_lblk && |
| 2524 |
index < (map.m_lblk + map.m_len)) |
| 2525 |
goto got_it; |
| 2526 |
} else if (index < next_pgofs) { |
| 2527 |
/* hole case */ |
| 2528 |
goto got_it; |
| 2529 |
} |
| 2530 |
|
| 2531 |
/* |
| 2532 |
* Then do more f2fs_map_blocks() calls until we are |
| 2533 |
* done with this page. |
| 2534 |
*/ |
| 2535 |
memset(&map, 0, sizeof(map)); |
| 2536 |
map.m_next_pgofs = &next_pgofs; |
| 2537 |
map.m_seg_type = NO_CHECK_TYPE; |
| 2538 |
map.m_lblk = index; |
| 2539 |
map.m_len = max_nr_pages; |
| 2540 |
|
| 2541 |
ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT); |
| 2542 |
if (ret) |
| 2543 |
goto err_out; |
| 2544 |
got_it: |
| 2545 |
if ((map.m_flags & F2FS_MAP_MAPPED)) { |
| 2546 |
block_nr = map.m_pblk + index - map.m_lblk; |
| 2547 |
if (!f2fs_is_valid_blkaddr(F2FS_I_SB(inode), block_nr, |
| 2548 |
DATA_GENERIC_ENHANCE_READ)) { |
| 2549 |
ret = -EFSCORRUPTED; |
| 2550 |
goto err_out; |
| 2551 |
} |
| 2552 |
} else { |
| 2553 |
size_t page_offset = offset << PAGE_SHIFT; |
| 2554 |
folio_zero_range(folio, page_offset, PAGE_SIZE); |
| 2555 |
if (vi && !fsverity_verify_blocks(vi, folio, PAGE_SIZE, page_offset)) { |
| 2556 |
ret = -EIO; |
| 2557 |
goto err_out; |
| 2558 |
} |
| 2559 |
continue; |
| 2560 |
} |
| 2561 |
|
| 2562 |
/* We must increment read_pages_pending before possible BIOs submitting |
| 2563 |
* to prevent from premature folio_end_read() call on folio |
| 2564 |
*/ |
| 2565 |
if (folio_test_large(folio)) { |
| 2566 |
ffs = ffs_find_or_alloc(folio); |
| 2567 |
|
| 2568 |
/* set the bitmap to wait */ |
| 2569 |
spin_lock_irq(&ffs->state_lock); |
| 2570 |
ffs->read_pages_pending++; |
| 2571 |
spin_unlock_irq(&ffs->state_lock); |
| 2572 |
} |
| 2573 |
|
| 2574 |
/* |
| 2575 |
* This page will go to BIO. Do we need to send this |
| 2576 |
* BIO off first? |
| 2577 |
*/ |
| 2578 |
if (bio && (!page_is_mergeable(F2FS_I_SB(inode), bio, |
| 2579 |
last_block_in_bio, block_nr) || |
| 2580 |
!f2fs_crypt_mergeable_bio(bio, inode, index, NULL))) { |
| 2581 |
submit_and_realloc: |
| 2582 |
f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); |
| 2583 |
bio = NULL; |
| 2584 |
} |
| 2585 |
if (bio == NULL) |
| 2586 |
bio = f2fs_grab_read_bio(inode, vi, |
| 2587 |
block_nr, max_nr_pages, |
| 2588 |
f2fs_ra_op_flags(rac), |
| 2589 |
index, false); |
| 2590 |
|
| 2591 |
/* |
| 2592 |
* If the page is under writeback, we need to wait for |
| 2593 |
* its completion to see the correct decrypted data. |
| 2594 |
*/ |
| 2595 |
f2fs_wait_on_block_writeback(inode, block_nr); |
| 2596 |
|
| 2597 |
if (!bio_add_folio(bio, folio, F2FS_BLKSIZE, |
| 2598 |
offset << PAGE_SHIFT)) |
| 2599 |
goto submit_and_realloc; |
| 2600 |
|
| 2601 |
folio_in_bio = true; |
| 2602 |
inc_page_count(F2FS_I_SB(inode), F2FS_RD_DATA); |
| 2603 |
f2fs_update_iostat(F2FS_I_SB(inode), NULL, FS_DATA_READ_IO, |
| 2604 |
F2FS_BLKSIZE); |
| 2605 |
last_block_in_bio = block_nr; |
| 2606 |
} |
| 2607 |
trace_f2fs_read_folio(folio, DATA); |
| 2608 |
err_out: |
| 2609 |
if (!folio_in_bio) { |
| 2610 |
folio_end_read(folio, !ret); |
| 2611 |
if (ret) |
| 2612 |
return ret; |
| 2613 |
} |
| 2614 |
if (rac) { |
| 2615 |
folio = readahead_folio(rac); |
| 2616 |
goto next_folio; |
| 2617 |
} |
| 2618 |
out: |
| 2619 |
f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); |
| 2620 |
if (ret) { |
| 2621 |
/* Wait bios and clear uptodate. */ |
| 2622 |
folio_lock(folio); |
| 2623 |
folio_clear_uptodate(folio); |
| 2624 |
folio_unlock(folio); |
| 2625 |
} |
| 2626 |
return ret; |
| 2627 |
} |
| 2628 |
|
| 2629 |
/* |
| 2630 |
* This function was originally taken from fs/mpage.c, and customized for f2fs. |
| 2631 |
* Major change was from block_size == page_size in f2fs by default. |
| 2632 |
*/ |
| 2633 |
static int f2fs_mpage_readpages(struct inode *inode, struct fsverity_info *vi, |
| 2634 |
struct readahead_control *rac, struct folio *folio) |
| 2635 |
{ |
| 2636 |
struct bio *bio = NULL; |
| 2637 |
sector_t last_block_in_bio = 0; |
| 2638 |
struct f2fs_map_blocks map; |
| 2639 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 2640 |
struct compress_ctx cc = { |
| 2641 |
.inode = inode, |
| 2642 |
.log_cluster_size = F2FS_I(inode)->i_log_cluster_size, |
| 2643 |
.cluster_size = F2FS_I(inode)->i_cluster_size, |
| 2644 |
.cluster_idx = NULL_CLUSTER, |
| 2645 |
.rpages = NULL, |
| 2646 |
.cpages = NULL, |
| 2647 |
.nr_rpages = 0, |
| 2648 |
.nr_cpages = 0, |
| 2649 |
}; |
| 2650 |
pgoff_t nc_cluster_idx = NULL_CLUSTER; |
| 2651 |
pgoff_t index; |
| 2652 |
#endif |
| 2653 |
pgoff_t next_pgofs = 0; |
| 2654 |
unsigned nr_pages = rac ? readahead_count(rac) : 1; |
| 2655 |
struct address_space *mapping = rac ? rac->mapping : folio->mapping; |
| 2656 |
unsigned max_nr_pages = nr_pages; |
| 2657 |
int ret = 0; |
| 2658 |
|
| 2659 |
if (mapping_large_folio_support(mapping)) |
| 2660 |
return f2fs_read_data_large_folio(inode, vi, rac, folio); |
| 2661 |
|
| 2662 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 2663 |
if (f2fs_compressed_file(inode)) { |
| 2664 |
index = rac ? readahead_index(rac) : folio->index; |
| 2665 |
max_nr_pages = round_up(index + nr_pages, cc.cluster_size) - |
| 2666 |
round_down(index, cc.cluster_size); |
| 2667 |
} |
| 2668 |
#endif |
| 2669 |
|
| 2670 |
map.m_pblk = 0; |
| 2671 |
map.m_lblk = 0; |
| 2672 |
map.m_len = 0; |
| 2673 |
map.m_flags = 0; |
| 2674 |
map.m_next_pgofs = &next_pgofs; |
| 2675 |
map.m_next_extent = NULL; |
| 2676 |
map.m_seg_type = NO_CHECK_TYPE; |
| 2677 |
map.m_may_create = false; |
| 2678 |
|
| 2679 |
for (; nr_pages; nr_pages--) { |
| 2680 |
if (rac) { |
| 2681 |
folio = readahead_folio(rac); |
| 2682 |
prefetchw(&folio->flags); |
| 2683 |
} |
| 2684 |
|
| 2685 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 2686 |
index = folio->index; |
| 2687 |
|
| 2688 |
if (!f2fs_compressed_file(inode)) |
| 2689 |
goto read_single_page; |
| 2690 |
|
| 2691 |
/* there are remained compressed pages, submit them */ |
| 2692 |
if (!f2fs_cluster_can_merge_page(&cc, index)) { |
| 2693 |
cc.vi = vi; |
| 2694 |
ret = f2fs_read_multi_pages(&cc, &bio, |
| 2695 |
max_nr_pages, |
| 2696 |
&last_block_in_bio, |
| 2697 |
rac, false); |
| 2698 |
f2fs_destroy_compress_ctx(&cc, false); |
| 2699 |
if (ret) |
| 2700 |
goto set_error_page; |
| 2701 |
} |
| 2702 |
if (cc.cluster_idx == NULL_CLUSTER) { |
| 2703 |
if (nc_cluster_idx == index >> cc.log_cluster_size) |
| 2704 |
goto read_single_page; |
| 2705 |
|
| 2706 |
ret = f2fs_is_compressed_cluster(inode, index); |
| 2707 |
if (ret < 0) |
| 2708 |
goto set_error_page; |
| 2709 |
else if (!ret) { |
| 2710 |
nc_cluster_idx = |
| 2711 |
index >> cc.log_cluster_size; |
| 2712 |
goto read_single_page; |
| 2713 |
} |
| 2714 |
|
| 2715 |
nc_cluster_idx = NULL_CLUSTER; |
| 2716 |
} |
| 2717 |
ret = f2fs_init_compress_ctx(&cc); |
| 2718 |
if (ret) |
| 2719 |
goto set_error_page; |
| 2720 |
|
| 2721 |
f2fs_compress_ctx_add_page(&cc, folio); |
| 2722 |
|
| 2723 |
goto next_page; |
| 2724 |
read_single_page: |
| 2725 |
#endif |
| 2726 |
|
| 2727 |
ret = f2fs_read_single_page(inode, vi, folio, max_nr_pages, |
| 2728 |
&map, &bio, &last_block_in_bio, |
| 2729 |
rac); |
| 2730 |
if (ret) { |
| 2731 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 2732 |
set_error_page: |
| 2733 |
#endif |
| 2734 |
folio_zero_segment(folio, 0, folio_size(folio)); |
| 2735 |
folio_unlock(folio); |
| 2736 |
} |
| 2737 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 2738 |
next_page: |
| 2739 |
#endif |
| 2740 |
|
| 2741 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 2742 |
if (f2fs_compressed_file(inode)) { |
| 2743 |
/* last page */ |
| 2744 |
if (nr_pages == 1 && !f2fs_cluster_is_empty(&cc)) { |
| 2745 |
cc.vi = vi; |
| 2746 |
ret = f2fs_read_multi_pages(&cc, &bio, |
| 2747 |
max_nr_pages, |
| 2748 |
&last_block_in_bio, |
| 2749 |
rac, false); |
| 2750 |
f2fs_destroy_compress_ctx(&cc, false); |
| 2751 |
} |
| 2752 |
} |
| 2753 |
#endif |
| 2754 |
} |
| 2755 |
f2fs_submit_read_bio(F2FS_I_SB(inode), bio, DATA); |
| 2756 |
return ret; |
| 2757 |
} |
| 2758 |
|
| 2759 |
static int f2fs_read_data_folio(struct file *file, struct folio *folio) |
| 2760 |
{ |
| 2761 |
struct inode *inode = folio->mapping->host; |
| 2762 |
struct fsverity_info *vi = NULL; |
| 2763 |
int ret; |
| 2764 |
|
| 2765 |
trace_f2fs_readpage(folio, DATA); |
| 2766 |
|
| 2767 |
if (!f2fs_is_compress_backend_ready(inode)) { |
| 2768 |
folio_unlock(folio); |
| 2769 |
return -EOPNOTSUPP; |
| 2770 |
} |
| 2771 |
|
| 2772 |
/* If the file has inline data, try to read it directly */ |
| 2773 |
if (f2fs_has_inline_data(inode)) { |
| 2774 |
ret = f2fs_read_inline_data(inode, folio); |
| 2775 |
if (ret != -EAGAIN) |
| 2776 |
return ret; |
| 2777 |
} |
| 2778 |
|
| 2779 |
vi = f2fs_need_verity(inode, folio->index); |
| 2780 |
if (vi) |
| 2781 |
fsverity_readahead(vi, folio->index, folio_nr_pages(folio)); |
| 2782 |
return f2fs_mpage_readpages(inode, vi, NULL, folio); |
| 2783 |
} |
| 2784 |
|
| 2785 |
static void f2fs_readahead(struct readahead_control *rac) |
| 2786 |
{ |
| 2787 |
struct inode *inode = rac->mapping->host; |
| 2788 |
struct fsverity_info *vi = NULL; |
| 2789 |
|
| 2790 |
trace_f2fs_readpages(inode, readahead_index(rac), readahead_count(rac)); |
| 2791 |
|
| 2792 |
if (!f2fs_is_compress_backend_ready(inode)) |
| 2793 |
return; |
| 2794 |
|
| 2795 |
/* If the file has inline data, skip readahead */ |
| 2796 |
if (f2fs_has_inline_data(inode)) |
| 2797 |
return; |
| 2798 |
|
| 2799 |
vi = f2fs_need_verity(inode, readahead_index(rac)); |
| 2800 |
if (vi) |
| 2801 |
fsverity_readahead(vi, readahead_index(rac), |
| 2802 |
readahead_count(rac)); |
| 2803 |
f2fs_mpage_readpages(inode, vi, rac, NULL); |
| 2804 |
} |
| 2805 |
|
| 2806 |
int f2fs_encrypt_one_page(struct f2fs_io_info *fio) |
| 2807 |
{ |
| 2808 |
struct inode *inode = fio_inode(fio); |
| 2809 |
struct folio *mfolio; |
| 2810 |
struct page *page; |
| 2811 |
gfp_t gfp_flags = GFP_NOFS; |
| 2812 |
|
| 2813 |
if (!f2fs_encrypted_file(inode)) |
| 2814 |
return 0; |
| 2815 |
|
| 2816 |
page = fio->compressed_page ? fio->compressed_page : fio->page; |
| 2817 |
|
| 2818 |
if (fscrypt_inode_uses_inline_crypto(inode)) |
| 2819 |
return 0; |
| 2820 |
|
| 2821 |
retry_encrypt: |
| 2822 |
fio->encrypted_page = fscrypt_encrypt_pagecache_blocks(page_folio(page), |
| 2823 |
PAGE_SIZE, 0, gfp_flags); |
| 2824 |
if (IS_ERR(fio->encrypted_page)) { |
| 2825 |
/* flush pending IOs and wait for a while in the ENOMEM case */ |
| 2826 |
if (PTR_ERR(fio->encrypted_page) == -ENOMEM) { |
| 2827 |
f2fs_flush_merged_writes(fio->sbi); |
| 2828 |
memalloc_retry_wait(GFP_NOFS); |
| 2829 |
gfp_flags |= __GFP_NOFAIL; |
| 2830 |
goto retry_encrypt; |
| 2831 |
} |
| 2832 |
return PTR_ERR(fio->encrypted_page); |
| 2833 |
} |
| 2834 |
|
| 2835 |
mfolio = filemap_lock_folio(META_MAPPING(fio->sbi), fio->old_blkaddr); |
| 2836 |
if (!IS_ERR(mfolio)) { |
| 2837 |
if (folio_test_uptodate(mfolio)) |
| 2838 |
memcpy(folio_address(mfolio), |
| 2839 |
page_address(fio->encrypted_page), PAGE_SIZE); |
| 2840 |
f2fs_folio_put(mfolio, true); |
| 2841 |
} |
| 2842 |
return 0; |
| 2843 |
} |
| 2844 |
|
| 2845 |
static inline bool check_inplace_update_policy(struct inode *inode, |
| 2846 |
struct f2fs_io_info *fio) |
| 2847 |
{ |
| 2848 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 2849 |
|
| 2850 |
if (IS_F2FS_IPU_HONOR_OPU_WRITE(sbi) && |
| 2851 |
is_inode_flag_set(inode, FI_OPU_WRITE)) |
| 2852 |
return false; |
| 2853 |
if (IS_F2FS_IPU_FORCE(sbi)) |
| 2854 |
return true; |
| 2855 |
if (IS_F2FS_IPU_SSR(sbi) && f2fs_need_SSR(sbi)) |
| 2856 |
return true; |
| 2857 |
if (IS_F2FS_IPU_UTIL(sbi) && utilization(sbi) > SM_I(sbi)->min_ipu_util) |
| 2858 |
return true; |
| 2859 |
if (IS_F2FS_IPU_SSR_UTIL(sbi) && f2fs_need_SSR(sbi) && |
| 2860 |
utilization(sbi) > SM_I(sbi)->min_ipu_util) |
| 2861 |
return true; |
| 2862 |
|
| 2863 |
/* |
| 2864 |
* IPU for rewrite async pages |
| 2865 |
*/ |
| 2866 |
if (IS_F2FS_IPU_ASYNC(sbi) && fio && fio->op == REQ_OP_WRITE && |
| 2867 |
!(fio->op_flags & REQ_SYNC) && !IS_ENCRYPTED(inode)) |
| 2868 |
return true; |
| 2869 |
|
| 2870 |
/* this is only set during fdatasync */ |
| 2871 |
if (IS_F2FS_IPU_FSYNC(sbi) && is_inode_flag_set(inode, FI_NEED_IPU)) |
| 2872 |
return true; |
| 2873 |
|
| 2874 |
if (unlikely(fio && is_sbi_flag_set(sbi, SBI_CP_DISABLED) && |
| 2875 |
!f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) |
| 2876 |
return true; |
| 2877 |
|
| 2878 |
return false; |
| 2879 |
} |
| 2880 |
|
| 2881 |
bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio) |
| 2882 |
{ |
| 2883 |
/* swap file is migrating in aligned write mode */ |
| 2884 |
if (is_inode_flag_set(inode, FI_ALIGNED_WRITE)) |
| 2885 |
return false; |
| 2886 |
|
| 2887 |
if (f2fs_is_pinned_file(inode)) |
| 2888 |
return true; |
| 2889 |
|
| 2890 |
/* if this is cold file, we should overwrite to avoid fragmentation */ |
| 2891 |
if (file_is_cold(inode) && !is_inode_flag_set(inode, FI_OPU_WRITE)) |
| 2892 |
return true; |
| 2893 |
|
| 2894 |
return check_inplace_update_policy(inode, fio); |
| 2895 |
} |
| 2896 |
|
| 2897 |
bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio) |
| 2898 |
{ |
| 2899 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 2900 |
|
| 2901 |
/* The below cases were checked when setting it. */ |
| 2902 |
if (f2fs_is_pinned_file(inode)) |
| 2903 |
return false; |
| 2904 |
if (fio && is_sbi_flag_set(sbi, SBI_NEED_FSCK)) |
| 2905 |
return true; |
| 2906 |
if (f2fs_lfs_mode(sbi)) |
| 2907 |
return true; |
| 2908 |
if (S_ISDIR(inode->i_mode)) |
| 2909 |
return true; |
| 2910 |
if (IS_NOQUOTA(inode)) |
| 2911 |
return true; |
| 2912 |
if (f2fs_used_in_atomic_write(inode)) |
| 2913 |
return true; |
| 2914 |
/* rewrite low ratio compress data w/ OPU mode to avoid fragmentation */ |
| 2915 |
if (f2fs_compressed_file(inode) && |
| 2916 |
F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER && |
| 2917 |
is_inode_flag_set(inode, FI_ENABLE_COMPRESS)) |
| 2918 |
return true; |
| 2919 |
|
| 2920 |
/* swap file is migrating in aligned write mode */ |
| 2921 |
if (is_inode_flag_set(inode, FI_ALIGNED_WRITE)) |
| 2922 |
return true; |
| 2923 |
|
| 2924 |
if (is_inode_flag_set(inode, FI_OPU_WRITE)) |
| 2925 |
return true; |
| 2926 |
|
| 2927 |
if (fio) { |
| 2928 |
if (page_private_gcing(fio->page)) |
| 2929 |
return true; |
| 2930 |
if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED) && |
| 2931 |
f2fs_is_checkpointed_data(sbi, fio->old_blkaddr))) |
| 2932 |
return true; |
| 2933 |
} |
| 2934 |
return false; |
| 2935 |
} |
| 2936 |
|
| 2937 |
static inline bool need_inplace_update(struct f2fs_io_info *fio) |
| 2938 |
{ |
| 2939 |
struct inode *inode = fio_inode(fio); |
| 2940 |
|
| 2941 |
if (f2fs_should_update_outplace(inode, fio)) |
| 2942 |
return false; |
| 2943 |
|
| 2944 |
return f2fs_should_update_inplace(inode, fio); |
| 2945 |
} |
| 2946 |
|
| 2947 |
int f2fs_do_write_data_page(struct f2fs_io_info *fio) |
| 2948 |
{ |
| 2949 |
struct folio *folio = fio->folio; |
| 2950 |
struct inode *inode = folio->mapping->host; |
| 2951 |
struct dnode_of_data dn; |
| 2952 |
struct node_info ni; |
| 2953 |
struct f2fs_lock_context lc; |
| 2954 |
bool ipu_force = false; |
| 2955 |
bool atomic_commit; |
| 2956 |
int err = 0; |
| 2957 |
|
| 2958 |
/* Use COW inode to make dnode_of_data for atomic write */ |
| 2959 |
atomic_commit = f2fs_is_atomic_file(inode) && |
| 2960 |
folio_test_f2fs_atomic(folio); |
| 2961 |
if (atomic_commit) |
| 2962 |
set_new_dnode(&dn, F2FS_I(inode)->cow_inode, NULL, NULL, 0); |
| 2963 |
else |
| 2964 |
set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 2965 |
|
| 2966 |
if (need_inplace_update(fio) && |
| 2967 |
f2fs_lookup_read_extent_cache_block(inode, folio->index, |
| 2968 |
&fio->old_blkaddr)) { |
| 2969 |
if (!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, |
| 2970 |
DATA_GENERIC_ENHANCE)) |
| 2971 |
return -EFSCORRUPTED; |
| 2972 |
|
| 2973 |
ipu_force = true; |
| 2974 |
fio->need_lock = LOCK_DONE; |
| 2975 |
goto got_it; |
| 2976 |
} |
| 2977 |
|
| 2978 |
if (is_sbi_flag_set(fio->sbi, SBI_ENABLE_CHECKPOINT) && |
| 2979 |
time_to_inject(fio->sbi, FAULT_SKIP_WRITE)) |
| 2980 |
return -EINVAL; |
| 2981 |
|
| 2982 |
/* Deadlock due to between page->lock and f2fs_lock_op */ |
| 2983 |
if (fio->need_lock == LOCK_REQ && !f2fs_trylock_op(fio->sbi, &lc)) |
| 2984 |
return -EAGAIN; |
| 2985 |
|
| 2986 |
err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE); |
| 2987 |
if (err) |
| 2988 |
goto out; |
| 2989 |
|
| 2990 |
fio->old_blkaddr = dn.data_blkaddr; |
| 2991 |
|
| 2992 |
/* This page is already truncated */ |
| 2993 |
if (fio->old_blkaddr == NULL_ADDR) { |
| 2994 |
folio_clear_uptodate(folio); |
| 2995 |
folio_clear_f2fs_gcing(folio); |
| 2996 |
goto out_writepage; |
| 2997 |
} |
| 2998 |
got_it: |
| 2999 |
if (__is_valid_data_blkaddr(fio->old_blkaddr) && |
| 3000 |
!f2fs_is_valid_blkaddr(fio->sbi, fio->old_blkaddr, |
| 3001 |
DATA_GENERIC_ENHANCE)) { |
| 3002 |
err = -EFSCORRUPTED; |
| 3003 |
goto out_writepage; |
| 3004 |
} |
| 3005 |
|
| 3006 |
/* wait for GCed page writeback via META_MAPPING */ |
| 3007 |
if (fio->meta_gc) |
| 3008 |
f2fs_wait_on_block_writeback(inode, fio->old_blkaddr); |
| 3009 |
|
| 3010 |
/* |
| 3011 |
* If current allocation needs SSR, |
| 3012 |
* it had better in-place writes for updated data. |
| 3013 |
*/ |
| 3014 |
if (ipu_force || |
| 3015 |
(__is_valid_data_blkaddr(fio->old_blkaddr) && |
| 3016 |
need_inplace_update(fio))) { |
| 3017 |
err = f2fs_encrypt_one_page(fio); |
| 3018 |
if (err) |
| 3019 |
goto out_writepage; |
| 3020 |
|
| 3021 |
folio_start_writeback(folio); |
| 3022 |
f2fs_put_dnode(&dn); |
| 3023 |
if (fio->need_lock == LOCK_REQ) |
| 3024 |
f2fs_unlock_op(fio->sbi, &lc); |
| 3025 |
err = f2fs_inplace_write_data(fio); |
| 3026 |
if (err) { |
| 3027 |
if (fscrypt_inode_uses_fs_layer_crypto(inode)) |
| 3028 |
fscrypt_finalize_bounce_page(&fio->encrypted_page); |
| 3029 |
folio_end_writeback(folio); |
| 3030 |
} else { |
| 3031 |
set_inode_flag(inode, FI_UPDATE_WRITE); |
| 3032 |
} |
| 3033 |
trace_f2fs_do_write_data_page(folio, IPU); |
| 3034 |
return err; |
| 3035 |
} |
| 3036 |
|
| 3037 |
if (fio->need_lock == LOCK_RETRY) { |
| 3038 |
if (!f2fs_trylock_op(fio->sbi, &lc)) { |
| 3039 |
err = -EAGAIN; |
| 3040 |
goto out_writepage; |
| 3041 |
} |
| 3042 |
fio->need_lock = LOCK_REQ; |
| 3043 |
} |
| 3044 |
|
| 3045 |
err = f2fs_get_node_info(fio->sbi, dn.nid, &ni, false); |
| 3046 |
if (err) |
| 3047 |
goto out_writepage; |
| 3048 |
|
| 3049 |
fio->version = ni.version; |
| 3050 |
|
| 3051 |
err = f2fs_encrypt_one_page(fio); |
| 3052 |
if (err) |
| 3053 |
goto out_writepage; |
| 3054 |
|
| 3055 |
folio_start_writeback(folio); |
| 3056 |
|
| 3057 |
if (fio->compr_blocks && fio->old_blkaddr == COMPRESS_ADDR) |
| 3058 |
f2fs_i_compr_blocks_update(inode, fio->compr_blocks - 1, false); |
| 3059 |
|
| 3060 |
/* LFS mode write path */ |
| 3061 |
f2fs_outplace_write_data(&dn, fio); |
| 3062 |
trace_f2fs_do_write_data_page(folio, OPU); |
| 3063 |
set_inode_flag(inode, FI_APPEND_WRITE); |
| 3064 |
if (atomic_commit) |
| 3065 |
folio_clear_f2fs_atomic(folio); |
| 3066 |
out_writepage: |
| 3067 |
f2fs_put_dnode(&dn); |
| 3068 |
out: |
| 3069 |
if (fio->need_lock == LOCK_REQ) |
| 3070 |
f2fs_unlock_op(fio->sbi, &lc); |
| 3071 |
return err; |
| 3072 |
} |
| 3073 |
|
| 3074 |
int f2fs_write_single_data_page(struct folio *folio, int *submitted, |
| 3075 |
struct bio **bio, |
| 3076 |
sector_t *last_block, |
| 3077 |
struct writeback_control *wbc, |
| 3078 |
enum iostat_type io_type, |
| 3079 |
int compr_blocks, |
| 3080 |
bool allow_balance) |
| 3081 |
{ |
| 3082 |
struct inode *inode = folio->mapping->host; |
| 3083 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 3084 |
loff_t i_size = i_size_read(inode); |
| 3085 |
const pgoff_t end_index = ((unsigned long long)i_size) |
| 3086 |
>> PAGE_SHIFT; |
| 3087 |
loff_t psize = (loff_t)(folio->index + 1) << PAGE_SHIFT; |
| 3088 |
unsigned offset = 0; |
| 3089 |
bool need_balance_fs = false; |
| 3090 |
bool quota_inode = IS_NOQUOTA(inode); |
| 3091 |
int err = 0; |
| 3092 |
struct f2fs_io_info fio = { |
| 3093 |
.sbi = sbi, |
| 3094 |
.ino = inode->i_ino, |
| 3095 |
.type = DATA, |
| 3096 |
.op = REQ_OP_WRITE, |
| 3097 |
.op_flags = wbc_to_write_flags(wbc), |
| 3098 |
.old_blkaddr = NULL_ADDR, |
| 3099 |
.folio = folio, |
| 3100 |
.encrypted_page = NULL, |
| 3101 |
.submitted = 0, |
| 3102 |
.compr_blocks = compr_blocks, |
| 3103 |
.need_lock = compr_blocks ? LOCK_DONE : LOCK_RETRY, |
| 3104 |
.meta_gc = f2fs_meta_inode_gc_required(inode) ? 1 : 0, |
| 3105 |
.io_type = io_type, |
| 3106 |
.io_wbc = wbc, |
| 3107 |
.bio = bio, |
| 3108 |
.last_block = last_block, |
| 3109 |
}; |
| 3110 |
|
| 3111 |
trace_f2fs_writepage(folio, DATA); |
| 3112 |
|
| 3113 |
/* we should bypass data pages to proceed the kworker jobs */ |
| 3114 |
if (unlikely(f2fs_cp_error(sbi))) { |
| 3115 |
mapping_set_error(folio->mapping, -EIO); |
| 3116 |
/* |
| 3117 |
* don't drop any dirty dentry pages for keeping lastest |
| 3118 |
* directory structure. |
| 3119 |
*/ |
| 3120 |
if (S_ISDIR(inode->i_mode) && |
| 3121 |
!is_sbi_flag_set(sbi, SBI_IS_CLOSE)) |
| 3122 |
goto redirty_out; |
| 3123 |
|
| 3124 |
/* keep data pages in remount-ro mode */ |
| 3125 |
if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY) |
| 3126 |
goto redirty_out; |
| 3127 |
goto out; |
| 3128 |
} |
| 3129 |
|
| 3130 |
if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 3131 |
goto redirty_out; |
| 3132 |
|
| 3133 |
if (folio->index < end_index || |
| 3134 |
f2fs_verity_in_progress(inode) || |
| 3135 |
compr_blocks) |
| 3136 |
goto write; |
| 3137 |
|
| 3138 |
/* |
| 3139 |
* If the offset is out-of-range of file size, |
| 3140 |
* this page does not have to be written to disk. |
| 3141 |
*/ |
| 3142 |
offset = i_size & (PAGE_SIZE - 1); |
| 3143 |
if ((folio->index >= end_index + 1) || !offset) |
| 3144 |
goto out; |
| 3145 |
|
| 3146 |
folio_zero_segment(folio, offset, folio_size(folio)); |
| 3147 |
write: |
| 3148 |
/* Dentry/quota blocks are controlled by checkpoint */ |
| 3149 |
if (S_ISDIR(inode->i_mode) || quota_inode) { |
| 3150 |
struct f2fs_lock_context lc; |
| 3151 |
|
| 3152 |
/* |
| 3153 |
* We need to wait for node_write to avoid block allocation during |
| 3154 |
* checkpoint. This can only happen to quota writes which can cause |
| 3155 |
* the below discard race condition. |
| 3156 |
*/ |
| 3157 |
if (quota_inode) |
| 3158 |
f2fs_down_read_trace(&sbi->node_write, &lc); |
| 3159 |
|
| 3160 |
fio.need_lock = LOCK_DONE; |
| 3161 |
err = f2fs_do_write_data_page(&fio); |
| 3162 |
|
| 3163 |
if (quota_inode) |
| 3164 |
f2fs_up_read_trace(&sbi->node_write, &lc); |
| 3165 |
|
| 3166 |
goto done; |
| 3167 |
} |
| 3168 |
|
| 3169 |
need_balance_fs = true; |
| 3170 |
err = -EAGAIN; |
| 3171 |
if (f2fs_has_inline_data(inode)) { |
| 3172 |
err = f2fs_write_inline_data(inode, folio); |
| 3173 |
if (!err) |
| 3174 |
goto out; |
| 3175 |
} |
| 3176 |
|
| 3177 |
if (err == -EAGAIN) { |
| 3178 |
err = f2fs_do_write_data_page(&fio); |
| 3179 |
if (err == -EAGAIN) { |
| 3180 |
f2fs_bug_on(sbi, compr_blocks); |
| 3181 |
fio.need_lock = LOCK_REQ; |
| 3182 |
err = f2fs_do_write_data_page(&fio); |
| 3183 |
} |
| 3184 |
} |
| 3185 |
|
| 3186 |
if (err) { |
| 3187 |
file_set_keep_isize(inode); |
| 3188 |
} else { |
| 3189 |
spin_lock(&F2FS_I(inode)->i_size_lock); |
| 3190 |
if (F2FS_I(inode)->last_disk_size < psize) |
| 3191 |
F2FS_I(inode)->last_disk_size = psize; |
| 3192 |
spin_unlock(&F2FS_I(inode)->i_size_lock); |
| 3193 |
} |
| 3194 |
|
| 3195 |
done: |
| 3196 |
if (err && err != -ENOENT) |
| 3197 |
goto redirty_out; |
| 3198 |
|
| 3199 |
out: |
| 3200 |
inode_dec_dirty_pages(inode); |
| 3201 |
if (err) { |
| 3202 |
folio_clear_uptodate(folio); |
| 3203 |
folio_clear_f2fs_gcing(folio); |
| 3204 |
} |
| 3205 |
folio_unlock(folio); |
| 3206 |
if (!S_ISDIR(inode->i_mode) && !IS_NOQUOTA(inode) && |
| 3207 |
!F2FS_I(inode)->wb_task && allow_balance) |
| 3208 |
f2fs_balance_fs(sbi, need_balance_fs); |
| 3209 |
|
| 3210 |
if (unlikely(f2fs_cp_error(sbi))) { |
| 3211 |
f2fs_submit_merged_write(sbi, DATA); |
| 3212 |
if (bio && *bio) |
| 3213 |
f2fs_submit_merged_ipu_write(sbi, bio, NULL); |
| 3214 |
submitted = NULL; |
| 3215 |
} |
| 3216 |
|
| 3217 |
if (submitted) |
| 3218 |
*submitted = fio.submitted; |
| 3219 |
|
| 3220 |
return 0; |
| 3221 |
|
| 3222 |
redirty_out: |
| 3223 |
folio_redirty_for_writepage(wbc, folio); |
| 3224 |
/* |
| 3225 |
* pageout() in MM translates EAGAIN, so calls handle_write_error() |
| 3226 |
* -> mapping_set_error() -> set_bit(AS_EIO, ...). |
| 3227 |
* file_write_and_wait_range() will see EIO error, which is critical |
| 3228 |
* to return value of fsync() followed by atomic_write failure to user. |
| 3229 |
*/ |
| 3230 |
folio_unlock(folio); |
| 3231 |
if (!err) |
| 3232 |
return 1; |
| 3233 |
return err; |
| 3234 |
} |
| 3235 |
|
| 3236 |
/* |
| 3237 |
* This function was copied from write_cache_pages from mm/page-writeback.c. |
| 3238 |
* The major change is making write step of cold data page separately from |
| 3239 |
* warm/hot data page. |
| 3240 |
*/ |
| 3241 |
static int f2fs_write_cache_pages(struct address_space *mapping, |
| 3242 |
struct writeback_control *wbc, |
| 3243 |
enum iostat_type io_type) |
| 3244 |
{ |
| 3245 |
int ret = 0; |
| 3246 |
int done = 0, retry = 0; |
| 3247 |
struct page *pages_local[F2FS_ONSTACK_PAGES]; |
| 3248 |
struct page **pages = pages_local; |
| 3249 |
struct folio_batch fbatch; |
| 3250 |
struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); |
| 3251 |
struct bio *bio = NULL; |
| 3252 |
sector_t last_block; |
| 3253 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 3254 |
struct inode *inode = mapping->host; |
| 3255 |
struct compress_ctx cc = { |
| 3256 |
.inode = inode, |
| 3257 |
.log_cluster_size = F2FS_I(inode)->i_log_cluster_size, |
| 3258 |
.cluster_size = F2FS_I(inode)->i_cluster_size, |
| 3259 |
.cluster_idx = NULL_CLUSTER, |
| 3260 |
.rpages = NULL, |
| 3261 |
.nr_rpages = 0, |
| 3262 |
.cpages = NULL, |
| 3263 |
.valid_nr_cpages = 0, |
| 3264 |
.rbuf = NULL, |
| 3265 |
.cbuf = NULL, |
| 3266 |
.rlen = PAGE_SIZE * F2FS_I(inode)->i_cluster_size, |
| 3267 |
.private = NULL, |
| 3268 |
}; |
| 3269 |
#endif |
| 3270 |
int nr_folios, p, idx; |
| 3271 |
int nr_pages; |
| 3272 |
unsigned int max_pages = F2FS_ONSTACK_PAGES; |
| 3273 |
pgoff_t index; |
| 3274 |
pgoff_t end; /* Inclusive */ |
| 3275 |
pgoff_t done_index; |
| 3276 |
int range_whole = 0; |
| 3277 |
xa_mark_t tag; |
| 3278 |
int nwritten = 0; |
| 3279 |
int submitted = 0; |
| 3280 |
int i; |
| 3281 |
|
| 3282 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 3283 |
if (f2fs_compressed_file(inode) && |
| 3284 |
1 << cc.log_cluster_size > F2FS_ONSTACK_PAGES) { |
| 3285 |
pages = f2fs_kzalloc(sbi, sizeof(struct page *) << |
| 3286 |
cc.log_cluster_size, GFP_NOFS | __GFP_NOFAIL); |
| 3287 |
max_pages = 1 << cc.log_cluster_size; |
| 3288 |
} |
| 3289 |
#endif |
| 3290 |
|
| 3291 |
folio_batch_init(&fbatch); |
| 3292 |
|
| 3293 |
if (get_dirty_pages(mapping->host) <= |
| 3294 |
SM_I(F2FS_M_SB(mapping))->min_hot_blocks) |
| 3295 |
set_inode_flag(mapping->host, FI_HOT_DATA); |
| 3296 |
else |
| 3297 |
clear_inode_flag(mapping->host, FI_HOT_DATA); |
| 3298 |
|
| 3299 |
if (wbc->range_cyclic) { |
| 3300 |
index = mapping->writeback_index; /* prev offset */ |
| 3301 |
end = -1; |
| 3302 |
} else { |
| 3303 |
index = wbc->range_start >> PAGE_SHIFT; |
| 3304 |
end = wbc->range_end >> PAGE_SHIFT; |
| 3305 |
if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 3306 |
range_whole = 1; |
| 3307 |
} |
| 3308 |
tag = wbc_to_tag(wbc); |
| 3309 |
retry: |
| 3310 |
retry = 0; |
| 3311 |
if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
| 3312 |
tag_pages_for_writeback(mapping, index, end); |
| 3313 |
done_index = index; |
| 3314 |
while (!done && !retry && (index <= end)) { |
| 3315 |
nr_pages = 0; |
| 3316 |
again: |
| 3317 |
nr_folios = filemap_get_folios_tag(mapping, &index, end, |
| 3318 |
tag, &fbatch); |
| 3319 |
if (nr_folios == 0) { |
| 3320 |
if (nr_pages) |
| 3321 |
goto write; |
| 3322 |
break; |
| 3323 |
} |
| 3324 |
|
| 3325 |
for (i = 0; i < nr_folios; i++) { |
| 3326 |
struct folio *folio = fbatch.folios[i]; |
| 3327 |
|
| 3328 |
idx = 0; |
| 3329 |
p = folio_nr_pages(folio); |
| 3330 |
add_more: |
| 3331 |
pages[nr_pages] = folio_page(folio, idx); |
| 3332 |
folio_get(folio); |
| 3333 |
if (++nr_pages == max_pages) { |
| 3334 |
index = folio->index + idx + 1; |
| 3335 |
folio_batch_release(&fbatch); |
| 3336 |
goto write; |
| 3337 |
} |
| 3338 |
if (++idx < p) |
| 3339 |
goto add_more; |
| 3340 |
} |
| 3341 |
folio_batch_release(&fbatch); |
| 3342 |
goto again; |
| 3343 |
write: |
| 3344 |
for (i = 0; i < nr_pages; i++) { |
| 3345 |
struct page *page = pages[i]; |
| 3346 |
struct folio *folio = page_folio(page); |
| 3347 |
bool need_readd; |
| 3348 |
readd: |
| 3349 |
need_readd = false; |
| 3350 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 3351 |
if (f2fs_compressed_file(inode)) { |
| 3352 |
void *fsdata = NULL; |
| 3353 |
struct page *pagep; |
| 3354 |
int ret2; |
| 3355 |
|
| 3356 |
ret = f2fs_init_compress_ctx(&cc); |
| 3357 |
if (ret) { |
| 3358 |
done = 1; |
| 3359 |
break; |
| 3360 |
} |
| 3361 |
|
| 3362 |
if (!f2fs_cluster_can_merge_page(&cc, |
| 3363 |
folio->index)) { |
| 3364 |
ret = f2fs_write_multi_pages(&cc, |
| 3365 |
&submitted, wbc, io_type); |
| 3366 |
if (!ret) |
| 3367 |
need_readd = true; |
| 3368 |
goto result; |
| 3369 |
} |
| 3370 |
|
| 3371 |
if (unlikely(f2fs_cp_error(sbi))) |
| 3372 |
goto lock_folio; |
| 3373 |
|
| 3374 |
if (!f2fs_cluster_is_empty(&cc)) |
| 3375 |
goto lock_folio; |
| 3376 |
|
| 3377 |
if (f2fs_all_cluster_page_ready(&cc, |
| 3378 |
pages, i, nr_pages, true)) |
| 3379 |
goto lock_folio; |
| 3380 |
|
| 3381 |
ret2 = f2fs_prepare_compress_overwrite( |
| 3382 |
inode, &pagep, |
| 3383 |
folio->index, &fsdata); |
| 3384 |
if (ret2 < 0) { |
| 3385 |
ret = ret2; |
| 3386 |
done = 1; |
| 3387 |
break; |
| 3388 |
} else if (ret2 && |
| 3389 |
(!f2fs_compress_write_end(inode, |
| 3390 |
fsdata, folio->index, 1) || |
| 3391 |
!f2fs_all_cluster_page_ready(&cc, |
| 3392 |
pages, i, nr_pages, |
| 3393 |
false))) { |
| 3394 |
retry = 1; |
| 3395 |
break; |
| 3396 |
} |
| 3397 |
} |
| 3398 |
#endif |
| 3399 |
/* give a priority to WB_SYNC threads */ |
| 3400 |
if (atomic_read(&sbi->wb_sync_req[DATA]) && |
| 3401 |
wbc->sync_mode == WB_SYNC_NONE) { |
| 3402 |
done = 1; |
| 3403 |
break; |
| 3404 |
} |
| 3405 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 3406 |
lock_folio: |
| 3407 |
#endif |
| 3408 |
done_index = folio->index; |
| 3409 |
retry_write: |
| 3410 |
folio_lock(folio); |
| 3411 |
|
| 3412 |
if (unlikely(folio->mapping != mapping)) { |
| 3413 |
continue_unlock: |
| 3414 |
folio_unlock(folio); |
| 3415 |
continue; |
| 3416 |
} |
| 3417 |
|
| 3418 |
if (!folio_test_dirty(folio)) { |
| 3419 |
/* someone wrote it for us */ |
| 3420 |
goto continue_unlock; |
| 3421 |
} |
| 3422 |
|
| 3423 |
if (folio_test_writeback(folio)) { |
| 3424 |
if (wbc->sync_mode == WB_SYNC_NONE) |
| 3425 |
goto continue_unlock; |
| 3426 |
f2fs_folio_wait_writeback(folio, DATA, true, true); |
| 3427 |
} |
| 3428 |
|
| 3429 |
if (!folio_clear_dirty_for_io(folio)) |
| 3430 |
goto continue_unlock; |
| 3431 |
|
| 3432 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 3433 |
if (f2fs_compressed_file(inode)) { |
| 3434 |
folio_get(folio); |
| 3435 |
f2fs_compress_ctx_add_page(&cc, folio); |
| 3436 |
continue; |
| 3437 |
} |
| 3438 |
#endif |
| 3439 |
submitted = 0; |
| 3440 |
ret = f2fs_write_single_data_page(folio, |
| 3441 |
&submitted, &bio, &last_block, |
| 3442 |
wbc, io_type, 0, true); |
| 3443 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 3444 |
result: |
| 3445 |
#endif |
| 3446 |
nwritten += submitted; |
| 3447 |
wbc->nr_to_write -= submitted; |
| 3448 |
|
| 3449 |
if (unlikely(ret)) { |
| 3450 |
/* |
| 3451 |
* keep nr_to_write, since vfs uses this to |
| 3452 |
* get # of written pages. |
| 3453 |
*/ |
| 3454 |
if (ret == 1) { |
| 3455 |
ret = 0; |
| 3456 |
goto next; |
| 3457 |
} else if (ret == -EAGAIN) { |
| 3458 |
ret = 0; |
| 3459 |
if (wbc->sync_mode == WB_SYNC_ALL) { |
| 3460 |
f2fs_schedule_timeout( |
| 3461 |
DEFAULT_SCHEDULE_TIMEOUT); |
| 3462 |
goto retry_write; |
| 3463 |
} |
| 3464 |
goto next; |
| 3465 |
} |
| 3466 |
done_index = folio_next_index(folio); |
| 3467 |
done = 1; |
| 3468 |
break; |
| 3469 |
} |
| 3470 |
|
| 3471 |
if (wbc->nr_to_write <= 0 && |
| 3472 |
wbc->sync_mode == WB_SYNC_NONE) { |
| 3473 |
done = 1; |
| 3474 |
break; |
| 3475 |
} |
| 3476 |
next: |
| 3477 |
if (need_readd) |
| 3478 |
goto readd; |
| 3479 |
} |
| 3480 |
release_pages(pages, nr_pages); |
| 3481 |
cond_resched(); |
| 3482 |
} |
| 3483 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 3484 |
/* flush remained pages in compress cluster */ |
| 3485 |
if (f2fs_compressed_file(inode) && !f2fs_cluster_is_empty(&cc)) { |
| 3486 |
ret = f2fs_write_multi_pages(&cc, &submitted, wbc, io_type); |
| 3487 |
nwritten += submitted; |
| 3488 |
wbc->nr_to_write -= submitted; |
| 3489 |
if (ret) { |
| 3490 |
done = 1; |
| 3491 |
retry = 0; |
| 3492 |
} |
| 3493 |
} |
| 3494 |
if (f2fs_compressed_file(inode)) |
| 3495 |
f2fs_destroy_compress_ctx(&cc, false); |
| 3496 |
#endif |
| 3497 |
if (retry) { |
| 3498 |
index = 0; |
| 3499 |
end = -1; |
| 3500 |
goto retry; |
| 3501 |
} |
| 3502 |
if (wbc->range_cyclic && !done) |
| 3503 |
done_index = 0; |
| 3504 |
if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 3505 |
mapping->writeback_index = done_index; |
| 3506 |
|
| 3507 |
if (nwritten) |
| 3508 |
f2fs_submit_merged_write_cond(F2FS_M_SB(mapping), mapping->host, |
| 3509 |
NULL, 0, DATA); |
| 3510 |
/* submit cached bio of IPU write */ |
| 3511 |
if (bio) |
| 3512 |
f2fs_submit_merged_ipu_write(sbi, &bio, NULL); |
| 3513 |
|
| 3514 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 3515 |
if (pages != pages_local) |
| 3516 |
kfree(pages); |
| 3517 |
#endif |
| 3518 |
|
| 3519 |
return ret; |
| 3520 |
} |
| 3521 |
|
| 3522 |
static inline bool __should_serialize_io(struct inode *inode, |
| 3523 |
struct writeback_control *wbc) |
| 3524 |
{ |
| 3525 |
/* to avoid deadlock in path of data flush */ |
| 3526 |
if (F2FS_I(inode)->wb_task) |
| 3527 |
return false; |
| 3528 |
|
| 3529 |
if (!S_ISREG(inode->i_mode)) |
| 3530 |
return false; |
| 3531 |
if (IS_NOQUOTA(inode)) |
| 3532 |
return false; |
| 3533 |
|
| 3534 |
if (f2fs_is_pinned_file(inode)) |
| 3535 |
return false; |
| 3536 |
if (f2fs_need_compress_data(inode)) |
| 3537 |
return true; |
| 3538 |
if (wbc->sync_mode != WB_SYNC_ALL) |
| 3539 |
return true; |
| 3540 |
if (get_dirty_pages(inode) >= SM_I(F2FS_I_SB(inode))->min_seq_blocks) |
| 3541 |
return true; |
| 3542 |
return false; |
| 3543 |
} |
| 3544 |
|
| 3545 |
static inline void account_writeback(struct inode *inode, bool inc) |
| 3546 |
{ |
| 3547 |
if (!f2fs_sb_has_compression(F2FS_I_SB(inode))) |
| 3548 |
return; |
| 3549 |
|
| 3550 |
f2fs_down_read(&F2FS_I(inode)->i_sem); |
| 3551 |
if (inc) |
| 3552 |
atomic_inc(&F2FS_I(inode)->writeback); |
| 3553 |
else |
| 3554 |
atomic_dec(&F2FS_I(inode)->writeback); |
| 3555 |
f2fs_up_read(&F2FS_I(inode)->i_sem); |
| 3556 |
} |
| 3557 |
|
| 3558 |
static inline void update_skipped_write(struct f2fs_sb_info *sbi, |
| 3559 |
struct writeback_control *wbc) |
| 3560 |
{ |
| 3561 |
long skipped = wbc->pages_skipped; |
| 3562 |
|
| 3563 |
if (is_sbi_flag_set(sbi, SBI_ENABLE_CHECKPOINT) && skipped && |
| 3564 |
wbc->sync_mode == WB_SYNC_ALL) |
| 3565 |
atomic_add(skipped, &sbi->nr_pages[F2FS_SKIPPED_WRITE]); |
| 3566 |
} |
| 3567 |
|
| 3568 |
static int __f2fs_write_data_pages(struct address_space *mapping, |
| 3569 |
struct writeback_control *wbc, |
| 3570 |
enum iostat_type io_type) |
| 3571 |
{ |
| 3572 |
struct inode *inode = mapping->host; |
| 3573 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 3574 |
struct blk_plug plug; |
| 3575 |
int ret; |
| 3576 |
bool locked = false; |
| 3577 |
|
| 3578 |
/* skip writing if there is no dirty page in this inode */ |
| 3579 |
if (!get_dirty_pages(inode) && wbc->sync_mode == WB_SYNC_NONE) |
| 3580 |
return 0; |
| 3581 |
|
| 3582 |
/* during POR, we don't need to trigger writepage at all. */ |
| 3583 |
if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) |
| 3584 |
goto skip_write; |
| 3585 |
|
| 3586 |
if ((S_ISDIR(inode->i_mode) || IS_NOQUOTA(inode)) && |
| 3587 |
wbc->sync_mode == WB_SYNC_NONE && |
| 3588 |
get_dirty_pages(inode) < nr_pages_to_skip(sbi, DATA) && |
| 3589 |
f2fs_available_free_memory(sbi, DIRTY_DENTS)) |
| 3590 |
goto skip_write; |
| 3591 |
|
| 3592 |
/* skip writing in file defragment preparing stage */ |
| 3593 |
if (is_inode_flag_set(inode, FI_SKIP_WRITES)) |
| 3594 |
goto skip_write; |
| 3595 |
|
| 3596 |
trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 3597 |
|
| 3598 |
/* to avoid spliting IOs due to mixed WB_SYNC_ALL and WB_SYNC_NONE */ |
| 3599 |
if (wbc->sync_mode == WB_SYNC_ALL) |
| 3600 |
atomic_inc(&sbi->wb_sync_req[DATA]); |
| 3601 |
else if (atomic_read(&sbi->wb_sync_req[DATA])) { |
| 3602 |
/* to avoid potential deadlock */ |
| 3603 |
if (current->plug) |
| 3604 |
blk_finish_plug(current->plug); |
| 3605 |
goto skip_write; |
| 3606 |
} |
| 3607 |
|
| 3608 |
if (__should_serialize_io(inode, wbc)) { |
| 3609 |
mutex_lock(&sbi->writepages); |
| 3610 |
locked = true; |
| 3611 |
} |
| 3612 |
|
| 3613 |
account_writeback(inode, true); |
| 3614 |
|
| 3615 |
blk_start_plug(&plug); |
| 3616 |
ret = f2fs_write_cache_pages(mapping, wbc, io_type); |
| 3617 |
blk_finish_plug(&plug); |
| 3618 |
|
| 3619 |
account_writeback(inode, false); |
| 3620 |
|
| 3621 |
if (locked) |
| 3622 |
mutex_unlock(&sbi->writepages); |
| 3623 |
|
| 3624 |
if (wbc->sync_mode == WB_SYNC_ALL) |
| 3625 |
atomic_dec(&sbi->wb_sync_req[DATA]); |
| 3626 |
/* |
| 3627 |
* if some pages were truncated, we cannot guarantee its mapping->host |
| 3628 |
* to detect pending bios. |
| 3629 |
*/ |
| 3630 |
|
| 3631 |
f2fs_remove_dirty_inode(inode); |
| 3632 |
|
| 3633 |
/* |
| 3634 |
* f2fs_write_cache_pages() has retry logic for EAGAIN case which is |
| 3635 |
* common when racing w/ checkpoint, so only update skipped write |
| 3636 |
* when ret is non-zero. |
| 3637 |
*/ |
| 3638 |
if (ret) |
| 3639 |
update_skipped_write(sbi, wbc); |
| 3640 |
return ret; |
| 3641 |
|
| 3642 |
skip_write: |
| 3643 |
wbc->pages_skipped += get_dirty_pages(inode); |
| 3644 |
update_skipped_write(sbi, wbc); |
| 3645 |
trace_f2fs_writepages(mapping->host, wbc, DATA); |
| 3646 |
return 0; |
| 3647 |
} |
| 3648 |
|
| 3649 |
static int f2fs_write_data_pages(struct address_space *mapping, |
| 3650 |
struct writeback_control *wbc) |
| 3651 |
{ |
| 3652 |
struct inode *inode = mapping->host; |
| 3653 |
|
| 3654 |
return __f2fs_write_data_pages(mapping, wbc, |
| 3655 |
F2FS_I(inode)->cp_task == current ? |
| 3656 |
FS_CP_DATA_IO : FS_DATA_IO); |
| 3657 |
} |
| 3658 |
|
| 3659 |
void f2fs_write_failed(struct inode *inode, loff_t to) |
| 3660 |
{ |
| 3661 |
loff_t i_size = i_size_read(inode); |
| 3662 |
|
| 3663 |
if (IS_NOQUOTA(inode)) |
| 3664 |
return; |
| 3665 |
|
| 3666 |
/* In the fs-verity case, f2fs_end_enable_verity() does the truncate */ |
| 3667 |
if (to > i_size && !f2fs_verity_in_progress(inode)) { |
| 3668 |
f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
| 3669 |
filemap_invalidate_lock(inode->i_mapping); |
| 3670 |
|
| 3671 |
truncate_pagecache(inode, i_size); |
| 3672 |
f2fs_truncate_blocks(inode, i_size, true); |
| 3673 |
|
| 3674 |
filemap_invalidate_unlock(inode->i_mapping); |
| 3675 |
f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
| 3676 |
} |
| 3677 |
} |
| 3678 |
|
| 3679 |
static int prepare_write_begin(struct f2fs_sb_info *sbi, |
| 3680 |
struct folio *folio, loff_t pos, unsigned int len, |
| 3681 |
block_t *blk_addr, bool *node_changed) |
| 3682 |
{ |
| 3683 |
struct inode *inode = folio->mapping->host; |
| 3684 |
pgoff_t index = folio->index; |
| 3685 |
struct dnode_of_data dn; |
| 3686 |
struct f2fs_lock_context lc; |
| 3687 |
struct folio *ifolio; |
| 3688 |
bool locked = false; |
| 3689 |
int flag = F2FS_GET_BLOCK_PRE_AIO; |
| 3690 |
int err = 0; |
| 3691 |
|
| 3692 |
/* |
| 3693 |
* If a whole page is being written and we already preallocated all the |
| 3694 |
* blocks, then there is no need to get a block address now. |
| 3695 |
*/ |
| 3696 |
if (len == PAGE_SIZE && is_inode_flag_set(inode, FI_PREALLOCATED_ALL)) |
| 3697 |
return 0; |
| 3698 |
|
| 3699 |
/* f2fs_lock_op avoids race between write CP and convert_inline_page */ |
| 3700 |
if (f2fs_has_inline_data(inode)) { |
| 3701 |
if (pos + len > MAX_INLINE_DATA(inode)) |
| 3702 |
flag = F2FS_GET_BLOCK_DEFAULT; |
| 3703 |
f2fs_map_lock(sbi, &lc, flag); |
| 3704 |
locked = true; |
| 3705 |
} else if ((pos & PAGE_MASK) >= i_size_read(inode)) { |
| 3706 |
f2fs_map_lock(sbi, &lc, flag); |
| 3707 |
locked = true; |
| 3708 |
} |
| 3709 |
|
| 3710 |
restart: |
| 3711 |
/* check inline_data */ |
| 3712 |
ifolio = f2fs_get_inode_folio(sbi, inode->i_ino); |
| 3713 |
if (IS_ERR(ifolio)) { |
| 3714 |
err = PTR_ERR(ifolio); |
| 3715 |
goto unlock_out; |
| 3716 |
} |
| 3717 |
|
| 3718 |
set_new_dnode(&dn, inode, ifolio, ifolio, 0); |
| 3719 |
|
| 3720 |
if (f2fs_has_inline_data(inode)) { |
| 3721 |
if (pos + len <= MAX_INLINE_DATA(inode)) { |
| 3722 |
f2fs_do_read_inline_data(folio, ifolio); |
| 3723 |
set_inode_flag(inode, FI_DATA_EXIST); |
| 3724 |
if (inode->i_nlink) |
| 3725 |
folio_set_f2fs_inline(ifolio); |
| 3726 |
goto out; |
| 3727 |
} |
| 3728 |
err = f2fs_convert_inline_folio(&dn, folio); |
| 3729 |
if (err || dn.data_blkaddr != NULL_ADDR) |
| 3730 |
goto out; |
| 3731 |
} |
| 3732 |
|
| 3733 |
if (!f2fs_lookup_read_extent_cache_block(inode, index, |
| 3734 |
&dn.data_blkaddr)) { |
| 3735 |
if (IS_DEVICE_ALIASING(inode)) { |
| 3736 |
err = -ENODATA; |
| 3737 |
goto out; |
| 3738 |
} |
| 3739 |
|
| 3740 |
if (locked) { |
| 3741 |
err = f2fs_reserve_block(&dn, index); |
| 3742 |
goto out; |
| 3743 |
} |
| 3744 |
|
| 3745 |
/* hole case */ |
| 3746 |
err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 3747 |
if (!err && dn.data_blkaddr != NULL_ADDR) |
| 3748 |
goto out; |
| 3749 |
f2fs_put_dnode(&dn); |
| 3750 |
f2fs_map_lock(sbi, &lc, F2FS_GET_BLOCK_PRE_AIO); |
| 3751 |
WARN_ON(flag != F2FS_GET_BLOCK_PRE_AIO); |
| 3752 |
locked = true; |
| 3753 |
goto restart; |
| 3754 |
} |
| 3755 |
out: |
| 3756 |
if (!err) { |
| 3757 |
/* convert_inline_page can make node_changed */ |
| 3758 |
*blk_addr = dn.data_blkaddr; |
| 3759 |
*node_changed = dn.node_changed; |
| 3760 |
} |
| 3761 |
f2fs_put_dnode(&dn); |
| 3762 |
unlock_out: |
| 3763 |
if (locked) |
| 3764 |
f2fs_map_unlock(sbi, &lc, flag); |
| 3765 |
return err; |
| 3766 |
} |
| 3767 |
|
| 3768 |
static int __find_data_block(struct inode *inode, pgoff_t index, |
| 3769 |
block_t *blk_addr) |
| 3770 |
{ |
| 3771 |
struct dnode_of_data dn; |
| 3772 |
struct folio *ifolio; |
| 3773 |
int err = 0; |
| 3774 |
|
| 3775 |
ifolio = f2fs_get_inode_folio(F2FS_I_SB(inode), inode->i_ino); |
| 3776 |
if (IS_ERR(ifolio)) |
| 3777 |
return PTR_ERR(ifolio); |
| 3778 |
|
| 3779 |
set_new_dnode(&dn, inode, ifolio, ifolio, 0); |
| 3780 |
|
| 3781 |
if (!f2fs_lookup_read_extent_cache_block(inode, index, |
| 3782 |
&dn.data_blkaddr)) { |
| 3783 |
/* hole case */ |
| 3784 |
err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); |
| 3785 |
if (err) { |
| 3786 |
dn.data_blkaddr = NULL_ADDR; |
| 3787 |
err = 0; |
| 3788 |
} |
| 3789 |
} |
| 3790 |
*blk_addr = dn.data_blkaddr; |
| 3791 |
f2fs_put_dnode(&dn); |
| 3792 |
return err; |
| 3793 |
} |
| 3794 |
|
| 3795 |
static int __reserve_data_block(struct inode *inode, pgoff_t index, |
| 3796 |
block_t *blk_addr, bool *node_changed) |
| 3797 |
{ |
| 3798 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 3799 |
struct dnode_of_data dn; |
| 3800 |
struct f2fs_lock_context lc; |
| 3801 |
struct folio *ifolio; |
| 3802 |
int err = 0; |
| 3803 |
|
| 3804 |
f2fs_map_lock(sbi, &lc, F2FS_GET_BLOCK_PRE_AIO); |
| 3805 |
|
| 3806 |
ifolio = f2fs_get_inode_folio(sbi, inode->i_ino); |
| 3807 |
if (IS_ERR(ifolio)) { |
| 3808 |
err = PTR_ERR(ifolio); |
| 3809 |
goto unlock_out; |
| 3810 |
} |
| 3811 |
set_new_dnode(&dn, inode, ifolio, ifolio, 0); |
| 3812 |
|
| 3813 |
if (!f2fs_lookup_read_extent_cache_block(dn.inode, index, |
| 3814 |
&dn.data_blkaddr)) |
| 3815 |
err = f2fs_reserve_block(&dn, index); |
| 3816 |
|
| 3817 |
*blk_addr = dn.data_blkaddr; |
| 3818 |
*node_changed = dn.node_changed; |
| 3819 |
f2fs_put_dnode(&dn); |
| 3820 |
|
| 3821 |
unlock_out: |
| 3822 |
f2fs_map_unlock(sbi, &lc, F2FS_GET_BLOCK_PRE_AIO); |
| 3823 |
return err; |
| 3824 |
} |
| 3825 |
|
| 3826 |
static int prepare_atomic_write_begin(struct f2fs_sb_info *sbi, |
| 3827 |
struct folio *folio, loff_t pos, unsigned int len, |
| 3828 |
block_t *blk_addr, bool *node_changed, bool *use_cow) |
| 3829 |
{ |
| 3830 |
struct inode *inode = folio->mapping->host; |
| 3831 |
struct inode *cow_inode = F2FS_I(inode)->cow_inode; |
| 3832 |
pgoff_t index = folio->index; |
| 3833 |
int err = 0; |
| 3834 |
block_t ori_blk_addr = NULL_ADDR; |
| 3835 |
|
| 3836 |
/* If pos is beyond the end of file, reserve a new block in COW inode */ |
| 3837 |
if ((pos & PAGE_MASK) >= i_size_read(inode)) |
| 3838 |
goto reserve_block; |
| 3839 |
|
| 3840 |
/* Look for the block in COW inode first */ |
| 3841 |
err = __find_data_block(cow_inode, index, blk_addr); |
| 3842 |
if (err) { |
| 3843 |
return err; |
| 3844 |
} else if (*blk_addr != NULL_ADDR) { |
| 3845 |
*use_cow = true; |
| 3846 |
return 0; |
| 3847 |
} |
| 3848 |
|
| 3849 |
if (is_inode_flag_set(inode, FI_ATOMIC_REPLACE)) |
| 3850 |
goto reserve_block; |
| 3851 |
|
| 3852 |
/* Look for the block in the original inode */ |
| 3853 |
err = __find_data_block(inode, index, &ori_blk_addr); |
| 3854 |
if (err) |
| 3855 |
return err; |
| 3856 |
|
| 3857 |
reserve_block: |
| 3858 |
/* Finally, we should reserve a new block in COW inode for the update */ |
| 3859 |
err = __reserve_data_block(cow_inode, index, blk_addr, node_changed); |
| 3860 |
if (err) |
| 3861 |
return err; |
| 3862 |
inc_atomic_write_cnt(inode); |
| 3863 |
|
| 3864 |
if (ori_blk_addr != NULL_ADDR) |
| 3865 |
*blk_addr = ori_blk_addr; |
| 3866 |
return 0; |
| 3867 |
} |
| 3868 |
|
| 3869 |
static int f2fs_write_begin(const struct kiocb *iocb, |
| 3870 |
struct address_space *mapping, |
| 3871 |
loff_t pos, unsigned len, struct folio **foliop, |
| 3872 |
void **fsdata) |
| 3873 |
{ |
| 3874 |
struct inode *inode = mapping->host; |
| 3875 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 3876 |
struct folio *folio; |
| 3877 |
pgoff_t index = pos >> PAGE_SHIFT; |
| 3878 |
bool need_balance = false; |
| 3879 |
bool use_cow = false; |
| 3880 |
block_t blkaddr = NULL_ADDR; |
| 3881 |
int err = 0; |
| 3882 |
|
| 3883 |
trace_f2fs_write_begin(inode, pos, len); |
| 3884 |
|
| 3885 |
if (!f2fs_is_checkpoint_ready(sbi)) { |
| 3886 |
err = -ENOSPC; |
| 3887 |
goto fail; |
| 3888 |
} |
| 3889 |
|
| 3890 |
/* |
| 3891 |
* We should check this at this moment to avoid deadlock on inode page |
| 3892 |
* and #0 page. The locking rule for inline_data conversion should be: |
| 3893 |
* folio_lock(folio #0) -> folio_lock(inode_page) |
| 3894 |
*/ |
| 3895 |
if (index != 0) { |
| 3896 |
err = f2fs_convert_inline_inode(inode); |
| 3897 |
if (err) |
| 3898 |
goto fail; |
| 3899 |
} |
| 3900 |
|
| 3901 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 3902 |
if (f2fs_compressed_file(inode)) { |
| 3903 |
int ret; |
| 3904 |
struct page *page; |
| 3905 |
|
| 3906 |
*fsdata = NULL; |
| 3907 |
|
| 3908 |
if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode))) |
| 3909 |
goto repeat; |
| 3910 |
|
| 3911 |
ret = f2fs_prepare_compress_overwrite(inode, &page, |
| 3912 |
index, fsdata); |
| 3913 |
if (ret < 0) { |
| 3914 |
err = ret; |
| 3915 |
goto fail; |
| 3916 |
} else if (ret) { |
| 3917 |
*foliop = page_folio(page); |
| 3918 |
return 0; |
| 3919 |
} |
| 3920 |
} |
| 3921 |
#endif |
| 3922 |
|
| 3923 |
repeat: |
| 3924 |
/* |
| 3925 |
* Do not use FGP_STABLE to avoid deadlock. |
| 3926 |
* Will wait that below with our IO control. |
| 3927 |
*/ |
| 3928 |
folio = f2fs_filemap_get_folio(mapping, index, |
| 3929 |
FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_NOFS, |
| 3930 |
mapping_gfp_mask(mapping)); |
| 3931 |
if (IS_ERR(folio)) { |
| 3932 |
err = PTR_ERR(folio); |
| 3933 |
goto fail; |
| 3934 |
} |
| 3935 |
|
| 3936 |
/* TODO: cluster can be compressed due to race with .writepage */ |
| 3937 |
|
| 3938 |
*foliop = folio; |
| 3939 |
|
| 3940 |
if (f2fs_is_atomic_file(inode)) |
| 3941 |
err = prepare_atomic_write_begin(sbi, folio, pos, len, |
| 3942 |
&blkaddr, &need_balance, &use_cow); |
| 3943 |
else |
| 3944 |
err = prepare_write_begin(sbi, folio, pos, len, |
| 3945 |
&blkaddr, &need_balance); |
| 3946 |
if (err) |
| 3947 |
goto put_folio; |
| 3948 |
|
| 3949 |
if (need_balance && !IS_NOQUOTA(inode) && |
| 3950 |
has_not_enough_free_secs(sbi, 0, 0)) { |
| 3951 |
folio_unlock(folio); |
| 3952 |
f2fs_balance_fs(sbi, true); |
| 3953 |
folio_lock(folio); |
| 3954 |
if (folio->mapping != mapping) { |
| 3955 |
/* The folio got truncated from under us */ |
| 3956 |
folio_unlock(folio); |
| 3957 |
folio_put(folio); |
| 3958 |
goto repeat; |
| 3959 |
} |
| 3960 |
} |
| 3961 |
|
| 3962 |
f2fs_folio_wait_writeback(folio, DATA, false, true); |
| 3963 |
|
| 3964 |
if (len == folio_size(folio) || folio_test_uptodate(folio)) |
| 3965 |
return 0; |
| 3966 |
|
| 3967 |
if (!(pos & (PAGE_SIZE - 1)) && (pos + len) >= i_size_read(inode) && |
| 3968 |
!f2fs_verity_in_progress(inode)) { |
| 3969 |
folio_zero_segment(folio, len, folio_size(folio)); |
| 3970 |
return 0; |
| 3971 |
} |
| 3972 |
|
| 3973 |
if (blkaddr == NEW_ADDR) { |
| 3974 |
folio_zero_segment(folio, 0, folio_size(folio)); |
| 3975 |
folio_mark_uptodate(folio); |
| 3976 |
} else { |
| 3977 |
if (!f2fs_is_valid_blkaddr(sbi, blkaddr, |
| 3978 |
DATA_GENERIC_ENHANCE_READ)) { |
| 3979 |
err = -EFSCORRUPTED; |
| 3980 |
goto put_folio; |
| 3981 |
} |
| 3982 |
f2fs_submit_page_read(use_cow ? F2FS_I(inode)->cow_inode : |
| 3983 |
inode, |
| 3984 |
NULL, /* can't write to fsverity files */ |
| 3985 |
folio, blkaddr, 0, true); |
| 3986 |
|
| 3987 |
folio_lock(folio); |
| 3988 |
if (unlikely(folio->mapping != mapping)) { |
| 3989 |
folio_unlock(folio); |
| 3990 |
folio_put(folio); |
| 3991 |
goto repeat; |
| 3992 |
} |
| 3993 |
if (unlikely(!folio_test_uptodate(folio))) { |
| 3994 |
err = -EIO; |
| 3995 |
goto put_folio; |
| 3996 |
} |
| 3997 |
} |
| 3998 |
return 0; |
| 3999 |
|
| 4000 |
put_folio: |
| 4001 |
f2fs_folio_put(folio, true); |
| 4002 |
fail: |
| 4003 |
f2fs_write_failed(inode, pos + len); |
| 4004 |
return err; |
| 4005 |
} |
| 4006 |
|
| 4007 |
static int f2fs_write_end(const struct kiocb *iocb, |
| 4008 |
struct address_space *mapping, |
| 4009 |
loff_t pos, unsigned len, unsigned copied, |
| 4010 |
struct folio *folio, void *fsdata) |
| 4011 |
{ |
| 4012 |
struct inode *inode = folio->mapping->host; |
| 4013 |
|
| 4014 |
trace_f2fs_write_end(inode, pos, len, copied); |
| 4015 |
|
| 4016 |
/* |
| 4017 |
* This should be come from len == PAGE_SIZE, and we expect copied |
| 4018 |
* should be PAGE_SIZE. Otherwise, we treat it with zero copied and |
| 4019 |
* let generic_perform_write() try to copy data again through copied=0. |
| 4020 |
*/ |
| 4021 |
if (!folio_test_uptodate(folio)) { |
| 4022 |
if (unlikely(copied != len)) |
| 4023 |
copied = 0; |
| 4024 |
else |
| 4025 |
folio_mark_uptodate(folio); |
| 4026 |
} |
| 4027 |
|
| 4028 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 4029 |
/* overwrite compressed file */ |
| 4030 |
if (f2fs_compressed_file(inode) && fsdata) { |
| 4031 |
f2fs_compress_write_end(inode, fsdata, folio->index, copied); |
| 4032 |
f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
| 4033 |
|
| 4034 |
if (pos + copied > i_size_read(inode) && |
| 4035 |
!f2fs_verity_in_progress(inode)) |
| 4036 |
f2fs_i_size_write(inode, pos + copied); |
| 4037 |
return copied; |
| 4038 |
} |
| 4039 |
#endif |
| 4040 |
|
| 4041 |
if (!copied) |
| 4042 |
goto unlock_out; |
| 4043 |
|
| 4044 |
folio_mark_dirty(folio); |
| 4045 |
|
| 4046 |
if (f2fs_is_atomic_file(inode)) |
| 4047 |
folio_set_f2fs_atomic(folio); |
| 4048 |
|
| 4049 |
if (pos + copied > i_size_read(inode) && |
| 4050 |
!f2fs_verity_in_progress(inode)) { |
| 4051 |
f2fs_i_size_write(inode, pos + copied); |
| 4052 |
if (f2fs_is_atomic_file(inode)) |
| 4053 |
f2fs_i_size_write(F2FS_I(inode)->cow_inode, |
| 4054 |
pos + copied); |
| 4055 |
} |
| 4056 |
unlock_out: |
| 4057 |
f2fs_folio_put(folio, true); |
| 4058 |
f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); |
| 4059 |
return copied; |
| 4060 |
} |
| 4061 |
|
| 4062 |
void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length) |
| 4063 |
{ |
| 4064 |
struct inode *inode = folio->mapping->host; |
| 4065 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 4066 |
|
| 4067 |
if (inode->i_ino >= F2FS_ROOT_INO(sbi) && |
| 4068 |
(offset || length != folio_size(folio))) |
| 4069 |
return; |
| 4070 |
|
| 4071 |
if (folio_test_dirty(folio)) { |
| 4072 |
if (inode->i_ino == F2FS_META_INO(sbi)) { |
| 4073 |
dec_page_count(sbi, F2FS_DIRTY_META); |
| 4074 |
} else if (inode->i_ino == F2FS_NODE_INO(sbi)) { |
| 4075 |
dec_page_count(sbi, F2FS_DIRTY_NODES); |
| 4076 |
} else { |
| 4077 |
inode_dec_dirty_pages(inode); |
| 4078 |
f2fs_remove_dirty_inode(inode); |
| 4079 |
} |
| 4080 |
} |
| 4081 |
|
| 4082 |
if (offset || length != folio_size(folio)) |
| 4083 |
return; |
| 4084 |
|
| 4085 |
folio_cancel_dirty(folio); |
| 4086 |
ffs_detach_free(folio); |
| 4087 |
} |
| 4088 |
|
| 4089 |
bool f2fs_release_folio(struct folio *folio, gfp_t wait) |
| 4090 |
{ |
| 4091 |
/* If this is dirty folio, keep private data */ |
| 4092 |
if (folio_test_dirty(folio)) |
| 4093 |
return false; |
| 4094 |
|
| 4095 |
ffs_detach_free(folio); |
| 4096 |
return true; |
| 4097 |
} |
| 4098 |
|
| 4099 |
static bool f2fs_dirty_data_folio(struct address_space *mapping, |
| 4100 |
struct folio *folio) |
| 4101 |
{ |
| 4102 |
struct inode *inode = mapping->host; |
| 4103 |
|
| 4104 |
trace_f2fs_set_page_dirty(folio, DATA); |
| 4105 |
|
| 4106 |
if (!folio_test_uptodate(folio)) |
| 4107 |
folio_mark_uptodate(folio); |
| 4108 |
BUG_ON(folio_test_swapcache(folio)); |
| 4109 |
|
| 4110 |
if (filemap_dirty_folio(mapping, folio)) { |
| 4111 |
f2fs_update_dirty_folio(inode, folio); |
| 4112 |
return true; |
| 4113 |
} |
| 4114 |
return false; |
| 4115 |
} |
| 4116 |
|
| 4117 |
|
| 4118 |
static sector_t f2fs_bmap_compress(struct inode *inode, sector_t block) |
| 4119 |
{ |
| 4120 |
#ifdef CONFIG_F2FS_FS_COMPRESSION |
| 4121 |
struct dnode_of_data dn; |
| 4122 |
sector_t start_idx, blknr = 0; |
| 4123 |
int ret; |
| 4124 |
|
| 4125 |
start_idx = round_down(block, F2FS_I(inode)->i_cluster_size); |
| 4126 |
|
| 4127 |
set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 4128 |
ret = f2fs_get_dnode_of_data(&dn, start_idx, LOOKUP_NODE); |
| 4129 |
if (ret) |
| 4130 |
return 0; |
| 4131 |
|
| 4132 |
if (dn.data_blkaddr != COMPRESS_ADDR) { |
| 4133 |
dn.ofs_in_node += block - start_idx; |
| 4134 |
blknr = f2fs_data_blkaddr(&dn); |
| 4135 |
if (!__is_valid_data_blkaddr(blknr)) |
| 4136 |
blknr = 0; |
| 4137 |
} |
| 4138 |
|
| 4139 |
f2fs_put_dnode(&dn); |
| 4140 |
return blknr; |
| 4141 |
#else |
| 4142 |
return 0; |
| 4143 |
#endif |
| 4144 |
} |
| 4145 |
|
| 4146 |
|
| 4147 |
static sector_t f2fs_bmap(struct address_space *mapping, sector_t block) |
| 4148 |
{ |
| 4149 |
struct inode *inode = mapping->host; |
| 4150 |
sector_t blknr = 0; |
| 4151 |
|
| 4152 |
if (f2fs_has_inline_data(inode)) |
| 4153 |
goto out; |
| 4154 |
|
| 4155 |
/* make sure allocating whole blocks */ |
| 4156 |
if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
| 4157 |
filemap_write_and_wait(mapping); |
| 4158 |
|
| 4159 |
/* Block number less than F2FS MAX BLOCKS */ |
| 4160 |
if (unlikely(block >= max_file_blocks(inode))) |
| 4161 |
goto out; |
| 4162 |
|
| 4163 |
if (f2fs_compressed_file(inode)) { |
| 4164 |
blknr = f2fs_bmap_compress(inode, block); |
| 4165 |
} else { |
| 4166 |
struct f2fs_map_blocks map; |
| 4167 |
|
| 4168 |
memset(&map, 0, sizeof(map)); |
| 4169 |
map.m_lblk = block; |
| 4170 |
map.m_len = 1; |
| 4171 |
map.m_next_pgofs = NULL; |
| 4172 |
map.m_seg_type = NO_CHECK_TYPE; |
| 4173 |
|
| 4174 |
if (!f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_BMAP)) |
| 4175 |
blknr = map.m_pblk; |
| 4176 |
} |
| 4177 |
out: |
| 4178 |
trace_f2fs_bmap(inode, block, blknr); |
| 4179 |
return blknr; |
| 4180 |
} |
| 4181 |
|
| 4182 |
#ifdef CONFIG_SWAP |
| 4183 |
static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk, |
| 4184 |
unsigned int blkcnt) |
| 4185 |
{ |
| 4186 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 4187 |
unsigned int blkofs; |
| 4188 |
unsigned int blk_per_sec = BLKS_PER_SEC(sbi); |
| 4189 |
unsigned int end_blk = start_blk + blkcnt - 1; |
| 4190 |
unsigned int secidx = start_blk / blk_per_sec; |
| 4191 |
unsigned int end_sec; |
| 4192 |
int ret = 0; |
| 4193 |
|
| 4194 |
if (!blkcnt) |
| 4195 |
return 0; |
| 4196 |
end_sec = end_blk / blk_per_sec; |
| 4197 |
|
| 4198 |
f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
| 4199 |
filemap_invalidate_lock(inode->i_mapping); |
| 4200 |
|
| 4201 |
set_inode_flag(inode, FI_ALIGNED_WRITE); |
| 4202 |
set_inode_flag(inode, FI_OPU_WRITE); |
| 4203 |
|
| 4204 |
for (; secidx <= end_sec; secidx++) { |
| 4205 |
unsigned int blkofs_end = secidx == end_sec ? |
| 4206 |
end_blk % blk_per_sec : blk_per_sec - 1; |
| 4207 |
|
| 4208 |
f2fs_down_write(&sbi->pin_sem); |
| 4209 |
|
| 4210 |
ret = f2fs_allocate_pinning_section(sbi); |
| 4211 |
if (ret) { |
| 4212 |
f2fs_up_write(&sbi->pin_sem); |
| 4213 |
break; |
| 4214 |
} |
| 4215 |
|
| 4216 |
set_inode_flag(inode, FI_SKIP_WRITES); |
| 4217 |
|
| 4218 |
for (blkofs = 0; blkofs <= blkofs_end; blkofs++) { |
| 4219 |
struct folio *folio; |
| 4220 |
unsigned int blkidx = secidx * blk_per_sec + blkofs; |
| 4221 |
|
| 4222 |
folio = f2fs_get_lock_data_folio(inode, blkidx, true); |
| 4223 |
if (IS_ERR(folio)) { |
| 4224 |
f2fs_up_write(&sbi->pin_sem); |
| 4225 |
ret = PTR_ERR(folio); |
| 4226 |
goto done; |
| 4227 |
} |
| 4228 |
|
| 4229 |
folio_mark_dirty(folio); |
| 4230 |
f2fs_folio_put(folio, true); |
| 4231 |
} |
| 4232 |
|
| 4233 |
clear_inode_flag(inode, FI_SKIP_WRITES); |
| 4234 |
|
| 4235 |
ret = filemap_fdatawrite(inode->i_mapping); |
| 4236 |
|
| 4237 |
f2fs_up_write(&sbi->pin_sem); |
| 4238 |
|
| 4239 |
if (ret) |
| 4240 |
break; |
| 4241 |
} |
| 4242 |
|
| 4243 |
done: |
| 4244 |
clear_inode_flag(inode, FI_SKIP_WRITES); |
| 4245 |
clear_inode_flag(inode, FI_OPU_WRITE); |
| 4246 |
clear_inode_flag(inode, FI_ALIGNED_WRITE); |
| 4247 |
|
| 4248 |
filemap_invalidate_unlock(inode->i_mapping); |
| 4249 |
f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); |
| 4250 |
|
| 4251 |
return ret; |
| 4252 |
} |
| 4253 |
|
| 4254 |
static int check_swap_activate(struct swap_info_struct *sis, |
| 4255 |
struct file *swap_file, sector_t *span) |
| 4256 |
{ |
| 4257 |
struct address_space *mapping = swap_file->f_mapping; |
| 4258 |
struct inode *inode = mapping->host; |
| 4259 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 4260 |
block_t cur_lblock; |
| 4261 |
block_t last_lblock; |
| 4262 |
block_t pblock; |
| 4263 |
block_t lowest_pblock = -1; |
| 4264 |
block_t highest_pblock = 0; |
| 4265 |
int nr_extents = 0; |
| 4266 |
unsigned int nr_pblocks; |
| 4267 |
unsigned int blks_per_sec = BLKS_PER_SEC(sbi); |
| 4268 |
unsigned int not_aligned = 0; |
| 4269 |
int ret = 0; |
| 4270 |
|
| 4271 |
/* |
| 4272 |
* Map all the blocks into the extent list. This code doesn't try |
| 4273 |
* to be very smart. |
| 4274 |
*/ |
| 4275 |
cur_lblock = 0; |
| 4276 |
last_lblock = F2FS_BYTES_TO_BLK(i_size_read(inode)); |
| 4277 |
|
| 4278 |
while (cur_lblock < last_lblock && cur_lblock < sis->max) { |
| 4279 |
struct f2fs_map_blocks map; |
| 4280 |
bool last_extent = false; |
| 4281 |
retry: |
| 4282 |
cond_resched(); |
| 4283 |
|
| 4284 |
memset(&map, 0, sizeof(map)); |
| 4285 |
map.m_lblk = cur_lblock; |
| 4286 |
map.m_len = last_lblock - cur_lblock; |
| 4287 |
map.m_next_pgofs = NULL; |
| 4288 |
map.m_next_extent = NULL; |
| 4289 |
map.m_seg_type = NO_CHECK_TYPE; |
| 4290 |
map.m_may_create = false; |
| 4291 |
|
| 4292 |
ret = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_FIEMAP); |
| 4293 |
if (ret) |
| 4294 |
goto out; |
| 4295 |
|
| 4296 |
/* hole */ |
| 4297 |
if (!(map.m_flags & F2FS_MAP_FLAGS)) { |
| 4298 |
f2fs_err(sbi, "Swapfile has holes"); |
| 4299 |
ret = -EINVAL; |
| 4300 |
goto out; |
| 4301 |
} |
| 4302 |
|
| 4303 |
pblock = map.m_pblk; |
| 4304 |
nr_pblocks = map.m_len; |
| 4305 |
|
| 4306 |
if (!last_extent && |
| 4307 |
((pblock - SM_I(sbi)->main_blkaddr) % blks_per_sec || |
| 4308 |
nr_pblocks % blks_per_sec || |
| 4309 |
f2fs_is_sequential_zone_area(sbi, pblock))) { |
| 4310 |
not_aligned++; |
| 4311 |
|
| 4312 |
nr_pblocks = roundup(nr_pblocks, blks_per_sec); |
| 4313 |
if (cur_lblock + nr_pblocks > sis->max) |
| 4314 |
nr_pblocks -= blks_per_sec; |
| 4315 |
|
| 4316 |
/* this extent is last one */ |
| 4317 |
if (!nr_pblocks) { |
| 4318 |
nr_pblocks = last_lblock - cur_lblock; |
| 4319 |
last_extent = true; |
| 4320 |
} |
| 4321 |
|
| 4322 |
ret = f2fs_migrate_blocks(inode, cur_lblock, |
| 4323 |
nr_pblocks); |
| 4324 |
if (ret) { |
| 4325 |
if (ret == -ENOENT) |
| 4326 |
ret = -EINVAL; |
| 4327 |
goto out; |
| 4328 |
} |
| 4329 |
|
| 4330 |
/* lookup block mapping info after block migration */ |
| 4331 |
goto retry; |
| 4332 |
} |
| 4333 |
|
| 4334 |
if (cur_lblock + nr_pblocks >= sis->max) |
| 4335 |
nr_pblocks = sis->max - cur_lblock; |
| 4336 |
|
| 4337 |
if (cur_lblock) { /* exclude the header page */ |
| 4338 |
if (pblock < lowest_pblock) |
| 4339 |
lowest_pblock = pblock; |
| 4340 |
if (pblock + nr_pblocks - 1 > highest_pblock) |
| 4341 |
highest_pblock = pblock + nr_pblocks - 1; |
| 4342 |
} |
| 4343 |
|
| 4344 |
/* |
| 4345 |
* We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks |
| 4346 |
*/ |
| 4347 |
ret = add_swap_extent(sis, cur_lblock, nr_pblocks, pblock); |
| 4348 |
if (ret < 0) |
| 4349 |
goto out; |
| 4350 |
nr_extents += ret; |
| 4351 |
cur_lblock += nr_pblocks; |
| 4352 |
} |
| 4353 |
ret = nr_extents; |
| 4354 |
*span = 1 + highest_pblock - lowest_pblock; |
| 4355 |
if (cur_lblock == 0) |
| 4356 |
cur_lblock = 1; /* force Empty message */ |
| 4357 |
sis->max = cur_lblock; |
| 4358 |
sis->pages = cur_lblock - 1; |
| 4359 |
out: |
| 4360 |
if (not_aligned) |
| 4361 |
f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%lu * N)", |
| 4362 |
not_aligned, blks_per_sec * F2FS_BLKSIZE); |
| 4363 |
return ret; |
| 4364 |
} |
| 4365 |
|
| 4366 |
static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 4367 |
sector_t *span) |
| 4368 |
{ |
| 4369 |
struct inode *inode = file_inode(file); |
| 4370 |
struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
| 4371 |
int ret; |
| 4372 |
|
| 4373 |
if (!S_ISREG(inode->i_mode)) |
| 4374 |
return -EINVAL; |
| 4375 |
|
| 4376 |
if (f2fs_readonly(sbi->sb)) |
| 4377 |
return -EROFS; |
| 4378 |
|
| 4379 |
if (f2fs_lfs_mode(sbi) && !f2fs_sb_has_blkzoned(sbi)) { |
| 4380 |
f2fs_err(sbi, "Swapfile not supported in LFS mode"); |
| 4381 |
return -EINVAL; |
| 4382 |
} |
| 4383 |
|
| 4384 |
ret = f2fs_convert_inline_inode(inode); |
| 4385 |
if (ret) |
| 4386 |
return ret; |
| 4387 |
|
| 4388 |
if (!f2fs_disable_compressed_file(inode)) |
| 4389 |
return -EINVAL; |
| 4390 |
|
| 4391 |
ret = filemap_fdatawrite(inode->i_mapping); |
| 4392 |
if (ret < 0) |
| 4393 |
return ret; |
| 4394 |
|
| 4395 |
f2fs_precache_extents(inode); |
| 4396 |
|
| 4397 |
ret = check_swap_activate(sis, file, span); |
| 4398 |
if (ret < 0) |
| 4399 |
return ret; |
| 4400 |
|
| 4401 |
stat_inc_swapfile_inode(inode); |
| 4402 |
set_inode_flag(inode, FI_PIN_FILE); |
| 4403 |
f2fs_update_time(sbi, REQ_TIME); |
| 4404 |
return ret; |
| 4405 |
} |
| 4406 |
|
| 4407 |
static void f2fs_swap_deactivate(struct file *file) |
| 4408 |
{ |
| 4409 |
struct inode *inode = file_inode(file); |
| 4410 |
|
| 4411 |
stat_dec_swapfile_inode(inode); |
| 4412 |
clear_inode_flag(inode, FI_PIN_FILE); |
| 4413 |
} |
| 4414 |
#else |
| 4415 |
static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, |
| 4416 |
sector_t *span) |
| 4417 |
{ |
| 4418 |
return -EOPNOTSUPP; |
| 4419 |
} |
| 4420 |
|
| 4421 |
static void f2fs_swap_deactivate(struct file *file) |
| 4422 |
{ |
| 4423 |
} |
| 4424 |
#endif |
| 4425 |
|
| 4426 |
const struct address_space_operations f2fs_dblock_aops = { |
| 4427 |
.read_folio = f2fs_read_data_folio, |
| 4428 |
.readahead = f2fs_readahead, |
| 4429 |
.writepages = f2fs_write_data_pages, |
| 4430 |
.write_begin = f2fs_write_begin, |
| 4431 |
.write_end = f2fs_write_end, |
| 4432 |
.dirty_folio = f2fs_dirty_data_folio, |
| 4433 |
.migrate_folio = filemap_migrate_folio, |
| 4434 |
.invalidate_folio = f2fs_invalidate_folio, |
| 4435 |
.release_folio = f2fs_release_folio, |
| 4436 |
.bmap = f2fs_bmap, |
| 4437 |
.swap_activate = f2fs_swap_activate, |
| 4438 |
.swap_deactivate = f2fs_swap_deactivate, |
| 4439 |
}; |
| 4440 |
|
| 4441 |
void f2fs_clear_page_cache_dirty_tag(struct folio *folio) |
| 4442 |
{ |
| 4443 |
struct address_space *mapping = folio->mapping; |
| 4444 |
unsigned long flags; |
| 4445 |
|
| 4446 |
xa_lock_irqsave(&mapping->i_pages, flags); |
| 4447 |
__xa_clear_mark(&mapping->i_pages, folio->index, |
| 4448 |
PAGECACHE_TAG_DIRTY); |
| 4449 |
xa_unlock_irqrestore(&mapping->i_pages, flags); |
| 4450 |
} |
| 4451 |
|
| 4452 |
int __init f2fs_init_post_read_processing(void) |
| 4453 |
{ |
| 4454 |
bio_post_read_ctx_cache = |
| 4455 |
kmem_cache_create("f2fs_bio_post_read_ctx", |
| 4456 |
sizeof(struct bio_post_read_ctx), 0, 0, NULL); |
| 4457 |
if (!bio_post_read_ctx_cache) |
| 4458 |
goto fail; |
| 4459 |
bio_post_read_ctx_pool = |
| 4460 |
mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS, |
| 4461 |
bio_post_read_ctx_cache); |
| 4462 |
if (!bio_post_read_ctx_pool) |
| 4463 |
goto fail_free_cache; |
| 4464 |
return 0; |
| 4465 |
|
| 4466 |
fail_free_cache: |
| 4467 |
kmem_cache_destroy(bio_post_read_ctx_cache); |
| 4468 |
fail: |
| 4469 |
return -ENOMEM; |
| 4470 |
} |
| 4471 |
|
| 4472 |
void f2fs_destroy_post_read_processing(void) |
| 4473 |
{ |
| 4474 |
mempool_destroy(bio_post_read_ctx_pool); |
| 4475 |
kmem_cache_destroy(bio_post_read_ctx_cache); |
| 4476 |
} |
| 4477 |
|
| 4478 |
int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi) |
| 4479 |
{ |
| 4480 |
if (!f2fs_sb_has_encrypt(sbi) && |
| 4481 |
!f2fs_sb_has_verity(sbi) && |
| 4482 |
!f2fs_sb_has_compression(sbi)) |
| 4483 |
return 0; |
| 4484 |
|
| 4485 |
sbi->post_read_wq = alloc_workqueue("f2fs_post_read_wq", |
| 4486 |
WQ_UNBOUND | WQ_HIGHPRI, |
| 4487 |
num_online_cpus()); |
| 4488 |
return sbi->post_read_wq ? 0 : -ENOMEM; |
| 4489 |
} |
| 4490 |
|
| 4491 |
void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi) |
| 4492 |
{ |
| 4493 |
if (sbi->post_read_wq) |
| 4494 |
destroy_workqueue(sbi->post_read_wq); |
| 4495 |
} |
| 4496 |
|
| 4497 |
int __init f2fs_init_bio_entry_cache(void) |
| 4498 |
{ |
| 4499 |
bio_entry_slab = f2fs_kmem_cache_create("f2fs_bio_entry_slab", |
| 4500 |
sizeof(struct bio_entry)); |
| 4501 |
|
| 4502 |
if (!bio_entry_slab) |
| 4503 |
return -ENOMEM; |
| 4504 |
|
| 4505 |
ffs_entry_slab = f2fs_kmem_cache_create("f2fs_ffs_slab", |
| 4506 |
sizeof(struct f2fs_folio_state)); |
| 4507 |
|
| 4508 |
if (!ffs_entry_slab) { |
| 4509 |
kmem_cache_destroy(bio_entry_slab); |
| 4510 |
return -ENOMEM; |
| 4511 |
} |
| 4512 |
|
| 4513 |
return 0; |
| 4514 |
} |
| 4515 |
|
| 4516 |
void f2fs_destroy_bio_entry_cache(void) |
| 4517 |
{ |
| 4518 |
kmem_cache_destroy(bio_entry_slab); |
| 4519 |
kmem_cache_destroy(ffs_entry_slab); |
| 4520 |
} |
| 4521 |
|
| 4522 |
static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, |
| 4523 |
unsigned int flags, struct iomap *iomap, |
| 4524 |
struct iomap *srcmap) |
| 4525 |
{ |
| 4526 |
struct f2fs_map_blocks map = { NULL, }; |
| 4527 |
pgoff_t next_pgofs = 0; |
| 4528 |
int err; |
| 4529 |
|
| 4530 |
map.m_lblk = F2FS_BYTES_TO_BLK(offset); |
| 4531 |
map.m_len = F2FS_BYTES_TO_BLK(offset + length - 1) - map.m_lblk + 1; |
| 4532 |
map.m_next_pgofs = &next_pgofs; |
| 4533 |
map.m_seg_type = f2fs_rw_hint_to_seg_type(F2FS_I_SB(inode), |
| 4534 |
inode->i_write_hint); |
| 4535 |
if (flags & IOMAP_WRITE && iomap->private) { |
| 4536 |
map.m_last_pblk = (unsigned long)iomap->private; |
| 4537 |
iomap->private = NULL; |
| 4538 |
} |
| 4539 |
|
| 4540 |
/* |
| 4541 |
* If the blocks being overwritten are already allocated, |
| 4542 |
* f2fs_map_lock and f2fs_balance_fs are not necessary. |
| 4543 |
*/ |
| 4544 |
if ((flags & IOMAP_WRITE) && |
| 4545 |
!__f2fs_overwrite_io(inode, offset, length, true)) |
| 4546 |
map.m_may_create = true; |
| 4547 |
|
| 4548 |
err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DIO); |
| 4549 |
if (err) |
| 4550 |
return err; |
| 4551 |
|
| 4552 |
iomap->offset = F2FS_BLK_TO_BYTES(map.m_lblk); |
| 4553 |
|
| 4554 |
/* |
| 4555 |
* When inline encryption is enabled, sometimes I/O to an encrypted file |
| 4556 |
* has to be broken up to guarantee DUN contiguity. Handle this by |
| 4557 |
* limiting the length of the mapping returned. |
| 4558 |
*/ |
| 4559 |
map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len); |
| 4560 |
|
| 4561 |
/* |
| 4562 |
* We should never see delalloc or compressed extents here based on |
| 4563 |
* prior flushing and checks. |
| 4564 |
*/ |
| 4565 |
if (WARN_ON_ONCE(map.m_pblk == COMPRESS_ADDR)) |
| 4566 |
return -EINVAL; |
| 4567 |
|
| 4568 |
if (map.m_flags & F2FS_MAP_MAPPED) { |
| 4569 |
if (WARN_ON_ONCE(map.m_pblk == NEW_ADDR)) |
| 4570 |
return -EINVAL; |
| 4571 |
|
| 4572 |
iomap->length = F2FS_BLK_TO_BYTES(map.m_len); |
| 4573 |
iomap->type = IOMAP_MAPPED; |
| 4574 |
iomap->flags |= IOMAP_F_MERGED; |
| 4575 |
iomap->bdev = map.m_bdev; |
| 4576 |
iomap->addr = F2FS_BLK_TO_BYTES(map.m_pblk); |
| 4577 |
|
| 4578 |
if (flags & IOMAP_WRITE && map.m_last_pblk) |
| 4579 |
iomap->private = (void *)map.m_last_pblk; |
| 4580 |
} else { |
| 4581 |
if (flags & IOMAP_WRITE) |
| 4582 |
return -ENOTBLK; |
| 4583 |
|
| 4584 |
if (map.m_pblk == NULL_ADDR) { |
| 4585 |
iomap->length = F2FS_BLK_TO_BYTES(next_pgofs) - |
| 4586 |
iomap->offset; |
| 4587 |
iomap->type = IOMAP_HOLE; |
| 4588 |
} else if (map.m_pblk == NEW_ADDR) { |
| 4589 |
iomap->length = F2FS_BLK_TO_BYTES(map.m_len); |
| 4590 |
iomap->type = IOMAP_UNWRITTEN; |
| 4591 |
} else { |
| 4592 |
f2fs_bug_on(F2FS_I_SB(inode), 1); |
| 4593 |
} |
| 4594 |
iomap->addr = IOMAP_NULL_ADDR; |
| 4595 |
} |
| 4596 |
|
| 4597 |
if (map.m_flags & F2FS_MAP_NEW) |
| 4598 |
iomap->flags |= IOMAP_F_NEW; |
| 4599 |
if ((inode_state_read_once(inode) & I_DIRTY_DATASYNC) || |
| 4600 |
offset + length > i_size_read(inode)) |
| 4601 |
iomap->flags |= IOMAP_F_DIRTY; |
| 4602 |
|
| 4603 |
return 0; |
| 4604 |
} |
| 4605 |
|
| 4606 |
const struct iomap_ops f2fs_iomap_ops = { |
| 4607 |
.iomap_begin = f2fs_iomap_begin, |
| 4608 |
}; |