Linux Audio

Check our new training course

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