Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  linux/fs/ext4/inode.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 1992, 1993, 1994, 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Remy Card (card@masi.ibp.fr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Laboratoire MASI - Institut Blaise Pascal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * Universite Pierre et Marie Curie (Paris VI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *  from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *  linux/fs/minix/inode.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *  Copyright (C) 1991, 1992  Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *  64-bit file support on 64-bit platforms by Jakub Jelinek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *	(jj@sunsite.ms.mff.cuni.cz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *  Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/highuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/dax.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/pagevec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/mpage.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/iomap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include "ext4_jbd2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include "xattr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include "acl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include "truncate.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #include <trace/events/ext4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #include <trace/events/android_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 			      struct ext4_inode_info *ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	__u32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	__u16 dummy_csum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	int offset = offsetof(struct ext4_inode, i_checksum_lo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	unsigned int csum_size = sizeof(dummy_csum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 	offset += csum_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 			   EXT4_GOOD_OLD_INODE_SIZE - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 		offset = offsetof(struct ext4_inode, i_checksum_hi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		csum = ext4_chksum(sbi, csum, (__u8 *)raw +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 				   EXT4_GOOD_OLD_INODE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 				   offset - EXT4_GOOD_OLD_INODE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 		if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 			csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 					   csum_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 			offset += csum_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 				   EXT4_INODE_SIZE(inode->i_sb) - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	return csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 				  struct ext4_inode_info *ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	__u32 provided, calculated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	    cpu_to_le32(EXT4_OS_LINUX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	    !ext4_has_metadata_csum(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	provided = le16_to_cpu(raw->i_checksum_lo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	calculated = ext4_inode_csum(inode, raw, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	    EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		calculated &= 0xFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	return provided == calculated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 			 struct ext4_inode_info *ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	__u32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	    cpu_to_le32(EXT4_OS_LINUX) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	    !ext4_has_metadata_csum(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	csum = ext4_inode_csum(inode, raw, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	    EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		raw->i_checksum_hi = cpu_to_le16(csum >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) static inline int ext4_begin_ordered_truncate(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 					      loff_t new_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	trace_ext4_begin_ordered_truncate(inode, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	 * If jinode is zero, then we never opened the file for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	 * writing, so there's no need to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	 * jbd2_journal_begin_ordered_truncate() since there's no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	 * outstanding writes we need to flush.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	if (!EXT4_I(inode)->jinode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 						   EXT4_I(inode)->jinode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 						   new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) static void ext4_invalidatepage(struct page *page, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 				unsigned int length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) static int __ext4_journalled_writepage(struct page *page, unsigned int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 				  int pextents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  * Test whether an inode is a fast symlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148)  * A fast symlink has its symlink data stored in ext4_inode_info->i_data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) int ext4_inode_is_fast_symlink(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		int ea_blocks = EXT4_I(inode)->i_file_acl ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 				EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		if (ext4_has_inline_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	return S_ISLNK(inode->i_mode) && inode->i_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	       (inode->i_size < EXT4_N_BLOCKS * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  * Called at the last iput() if i_nlink is zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) void ext4_evict_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	 * Credits for final inode cleanup and freeing:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	 * sb + inode (ext4_orphan_del()), block bitmap, group descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	 * (xattr block freeing), bitmap, group descriptor (inode freeing)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	int extra_credits = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	struct ext4_xattr_inode_array *ea_inode_array = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	bool freeze_protected = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	trace_ext4_evict_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	if (inode->i_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		 * When journalling data dirty buffers are tracked only in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		 * journal. So although mm thinks everything is clean and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		 * ready for reaping the inode might still have some pages to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		 * write in the running transaction or waiting to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		 * checkpointed. Thus calling jbd2_journal_invalidatepage()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		 * (via truncate_inode_pages()) to discard these buffers can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		 * cause data loss. Also even if we did not discard these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		 * buffers, we would have no way to find them after the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		 * is reaped and thus user could see stale data if he tries to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		 * read them before the transaction is checkpointed. So be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		 * careful and force everything to disk here... We use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		 * ei->i_datasync_tid to store the newest transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		 * containing inode's data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		 * Note that directories do not have this problem because they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		 * don't use page cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		if (inode->i_ino != EXT4_JOURNAL_INO &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		    ext4_should_journal_data(inode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		    (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		    inode->i_data.nrpages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 			journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 			jbd2_complete_transaction(journal, commit_tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 			filemap_write_and_wait(&inode->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		truncate_inode_pages_final(&inode->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 		goto no_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	if (is_bad_inode(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		goto no_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	if (ext4_should_order_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		ext4_begin_ordered_truncate(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	truncate_inode_pages_final(&inode->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	 * For inodes with journalled data, transaction commit could have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	 * dirtied the inode. Flush worker is ignoring it because of I_FREEING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	 * flag but we still need to remove the inode from the writeback lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (!list_empty_careful(&inode->i_io_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		WARN_ON_ONCE(!ext4_should_journal_data(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		inode_io_list_del(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	 * Protect us against freezing - iput() caller didn't have to have any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	 * protection against it. When we are in a running transaction though,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	 * we are already protected against freezing and we cannot grab further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	 * protection due to lock ordering constraints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	if (!ext4_journal_current_handle()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		sb_start_intwrite(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		freeze_protected = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	if (!IS_NOQUOTA(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	 * Block bitmap, group descriptor, and inode are accounted in both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	 * ext4_blocks_for_truncate() and extra_credits. So subtract 3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 			 ext4_blocks_for_truncate(inode) + extra_credits - 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		ext4_std_error(inode->i_sb, PTR_ERR(handle));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		 * If we're going to skip the normal cleanup, we still need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		 * make sure that the in-core orphan linked list is properly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		 * cleaned up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		ext4_orphan_del(NULL, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		if (freeze_protected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 			sb_end_intwrite(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		goto no_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	if (IS_SYNC(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	 * Set inode->i_size to 0 before calling ext4_truncate(). We need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	 * special handling of symlinks here because i_size is used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	 * determine whether ext4_inode_info->i_data contains symlink data or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	 * block mappings. Setting i_size to 0 will remove its fast symlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	 * status. Erase i_data so that it becomes a valid empty block map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	if (ext4_inode_is_fast_symlink(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	inode->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	err = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		ext4_warning(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 			     "couldn't mark inode dirty (err %d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		goto stop_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	if (inode->i_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		err = ext4_truncate(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 			ext4_error_err(inode->i_sb, -err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 				       "couldn't truncate inode %lu (err %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 				       inode->i_ino, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 			goto stop_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	/* Remove xattr references. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 				      extra_credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		ext4_warning(inode->i_sb, "xattr delete (err %d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) stop_handle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		ext4_orphan_del(NULL, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		if (freeze_protected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 			sb_end_intwrite(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		ext4_xattr_inode_array_free(ea_inode_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		goto no_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	 * Kill off the orphan record which ext4_truncate created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	 * AKPM: I think this can be inside the above `if'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	 * Note that ext4_orphan_del() has to be able to cope with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	 * deletion of a non-existent orphan - this is because we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	 * know if ext4_truncate() actually created an orphan record.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	 * (Well, we could do this if we need to, but heck - it works)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	ext4_orphan_del(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	EXT4_I(inode)->i_dtime	= (__u32)ktime_get_real_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	 * One subtle ordering requirement: if anything has gone wrong
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	 * (transaction abort, IO errors, whatever), then we can still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	 * do these next steps (the fs will already have been marked as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	 * having errors), but we can't free the inode if the mark_dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	 * fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	if (ext4_mark_inode_dirty(handle, inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		/* If that failed, just do the required in-core inode clear. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		ext4_clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		ext4_free_inode(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	if (freeze_protected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		sb_end_intwrite(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	ext4_xattr_inode_array_free(ea_inode_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) no_delete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	if (!list_empty(&EXT4_I(inode)->i_fc_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	ext4_clear_inode(inode);	/* We must guarantee clearing of inode... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) #ifdef CONFIG_QUOTA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) qsize_t *ext4_get_reserved_space(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	return &EXT4_I(inode)->i_reserved_quota;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353)  * Called with i_data_sem down, which is important since we can call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354)  * ext4_discard_preallocations() from here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) void ext4_da_update_reserve_space(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 					int used, int quota_claim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	spin_lock(&ei->i_block_reservation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	trace_ext4_da_update_reserve_space(inode, used, quota_claim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	if (unlikely(used > ei->i_reserved_data_blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 			 "with only %d reserved data blocks",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			 __func__, inode->i_ino, used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 			 ei->i_reserved_data_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 		used = ei->i_reserved_data_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	/* Update per-inode reservations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	ei->i_reserved_data_blocks -= used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	/* Update quota subsystem for data blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	if (quota_claim)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		dquot_claim_block(inode, EXT4_C2B(sbi, used));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		 * We did fallocate with an offset that is already delayed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		 * allocated. So on delayed allocated writeback we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		 * not re-claim the quota for fallocated blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	 * If we have done all the pending block allocations and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	 * there aren't any writers on the inode, we can discard the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	 * inode's preallocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	if ((ei->i_reserved_data_blocks == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	    !inode_is_open_for_write(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		ext4_discard_preallocations(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) static int __check_block_validity(struct inode *inode, const char *func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 				unsigned int line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 				struct ext4_map_blocks *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	if (ext4_has_feature_journal(inode->i_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	    (inode->i_ino ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	     le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	if (!ext4_inode_block_valid(inode, map->m_pblk, map->m_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		ext4_error_inode(inode, func, line, map->m_pblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 				 "lblock %lu mapped to illegal pblock %llu "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 				 "(length %d)", (unsigned long) map->m_lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 				 map->m_pblk, map->m_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		       ext4_lblk_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		return fscrypt_zeroout_range(inode, lblk, pblk, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	if (ret > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) #define check_block_validity(inode, map)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	__check_block_validity((inode), __func__, __LINE__, (map))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) #ifdef ES_AGGRESSIVE_TEST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) static void ext4_map_blocks_es_recheck(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 				       struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 				       struct ext4_map_blocks *es_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 				       struct ext4_map_blocks *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 				       int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	map->m_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	 * There is a race window that the result is not the same.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	 * e.g. xfstests #223 when dioread_nolock enables.  The reason
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	 * is that we lookup a block mapping in extent status tree with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	 * out taking i_data_sem.  So at the time the unwritten extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	 * could be converted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	down_read(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		retval = ext4_ext_map_blocks(handle, inode, map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		retval = ext4_ind_map_blocks(handle, inode, map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	up_read((&EXT4_I(inode)->i_data_sem));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	 * We don't check m_len because extent will be collpased in status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	 * tree.  So the m_len might not equal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	if (es_map->m_lblk != map->m_lblk ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	    es_map->m_flags != map->m_flags ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	    es_map->m_pblk != map->m_pblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 		printk("ES cache assertion failed for inode: %lu "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		       "es_cached ex [%d/%d/%llu/%x] != "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		       "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		       inode->i_ino, es_map->m_lblk, es_map->m_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		       es_map->m_pblk, es_map->m_flags, map->m_lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		       map->m_len, map->m_pblk, map->m_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		       retval, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) #endif /* ES_AGGRESSIVE_TEST */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481)  * The ext4_map_blocks() function tries to look up the requested blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482)  * and returns if the blocks are already mapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484)  * Otherwise it takes the write lock of the i_data_sem and allocate blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485)  * and store the allocated blocks in the result buffer head and mark it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486)  * mapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488)  * If file type is extents based, it will call ext4_ext_map_blocks(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489)  * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  * based files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492)  * On success, it returns the number of blocks being mapped or allocated.  if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493)  * create==0 and the blocks are pre-allocated and unwritten, the resulting @map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494)  * is marked as unwritten. If the create == 1, it will mark @map as mapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  * It returns 0 if plain look up failed (blocks have not been allocated), in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497)  * that case, @map is returned as unmapped but we still do fill map->m_len to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498)  * indicate the length of a hole starting at map->m_lblk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500)  * It returns the error in case of allocation failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) int ext4_map_blocks(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		    struct ext4_map_blocks *map, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	struct extent_status es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) #ifdef ES_AGGRESSIVE_TEST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	struct ext4_map_blocks orig_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	memcpy(&orig_map, map, sizeof(*map));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	map->m_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		  flags, map->m_len, (unsigned long) map->m_lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	 * ext4_map_blocks returns an int, and m_len is an unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	if (unlikely(map->m_len > INT_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		map->m_len = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	/* We can handle the block number less than EXT_MAX_BLOCKS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	/* Lookup extent status tree firstly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	    ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 			map->m_pblk = ext4_es_pblock(&es) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 					map->m_lblk - es.es_lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 			map->m_flags |= ext4_es_is_written(&es) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 					EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 			retval = es.es_len - (map->m_lblk - es.es_lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 			if (retval > map->m_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 				retval = map->m_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 			map->m_len = retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		} else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 			map->m_pblk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 			retval = es.es_len - (map->m_lblk - es.es_lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 			if (retval > map->m_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 				retval = map->m_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 			map->m_len = retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 			retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) #ifdef ES_AGGRESSIVE_TEST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		ext4_map_blocks_es_recheck(handle, inode, map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 					   &orig_map, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	 * Try to see if we can get the block without requesting a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	 * file system block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	down_read(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		retval = ext4_ext_map_blocks(handle, inode, map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		retval = ext4_ind_map_blocks(handle, inode, map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	if (retval > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		unsigned int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		if (unlikely(retval != map->m_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 			ext4_warning(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 				     "ES len assertion failed for inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 				     "%lu: retval %d != map->m_len %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 				     inode->i_ino, retval, map->m_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 			WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		status = map->m_flags & EXT4_MAP_UNWRITTEN ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 				EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		    !(status & EXTENT_STATUS_WRITTEN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		    ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 				       map->m_lblk + map->m_len - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 			status |= EXTENT_STATUS_DELAYED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		ret = ext4_es_insert_extent(inode, map->m_lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 					    map->m_len, map->m_pblk, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 			retval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	up_read((&EXT4_I(inode)->i_data_sem));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 		ret = check_block_validity(inode, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	/* If it is only a block(s) look up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	 * Returns if the blocks have already allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	 * Note that if blocks have been preallocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	 * ext4_ext_get_block() returns the create = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	 * with buffer head unmapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		 * If we need to convert extent to unwritten
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		 * we continue and do the actual work in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		 * ext4_ext_map_blocks()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	 * Here we clear m_flags because after allocating an new extent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	 * it will be set again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	map->m_flags &= ~EXT4_MAP_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	 * New blocks allocate and/or writing to unwritten extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	 * will possibly result in updating i_data, so we take
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	 * the write lock of i_data_sem, and call get_block()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	 * with create == 1 flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	down_write(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	 * We need to check for EXT4 here because migrate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	 * could have changed the inode type in between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		retval = ext4_ext_map_blocks(handle, inode, map, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		retval = ext4_ind_map_blocks(handle, inode, map, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 			 * We allocated new blocks which will result in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 			 * i_data's format changing.  Force the migrate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 			 * to fail by clearing migrate flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		 * Update reserved blocks/metadata blocks after successful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		 * block allocation which had been deferred till now. We don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		 * support fallocate for non extent files. So we can update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		 * reserve space here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		if ((retval > 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 			ext4_da_update_reserve_space(inode, retval, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	if (retval > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		unsigned int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		if (unlikely(retval != map->m_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 			ext4_warning(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 				     "ES len assertion failed for inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 				     "%lu: retval %d != map->m_len %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 				     inode->i_ino, retval, map->m_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		 * We have to zeroout blocks before inserting them into extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		 * status tree. Otherwise someone could look them up there and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		 * use them before they are really zeroed. We also have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		 * unmap metadata before zeroing as otherwise writeback can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		 * overwrite zeros with stale data from block device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		if (flags & EXT4_GET_BLOCKS_ZERO &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		    map->m_flags & EXT4_MAP_MAPPED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		    map->m_flags & EXT4_MAP_NEW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			ret = ext4_issue_zeroout(inode, map->m_lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 						 map->m_pblk, map->m_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 				retval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 				goto out_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 		 * If the extent has been zeroed out, we don't need to update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		 * extent status tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		    ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 			if (ext4_es_is_written(&es))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 				goto out_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		status = map->m_flags & EXT4_MAP_UNWRITTEN ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 				EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		    !(status & EXTENT_STATUS_WRITTEN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		    ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 				       map->m_lblk + map->m_len - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 			status |= EXTENT_STATUS_DELAYED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 		ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 					    map->m_pblk, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 			retval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 			goto out_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) out_sem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	up_write((&EXT4_I(inode)->i_data_sem));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		ret = check_block_validity(inode, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		 * Inodes with freshly allocated blocks where contents will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		 * visible after transaction commit must be on transaction's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		 * ordered data list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		if (map->m_flags & EXT4_MAP_NEW &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		    !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		    !(flags & EXT4_GET_BLOCKS_ZERO) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		    !ext4_is_quota_file(inode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		    ext4_should_order_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 			loff_t start_byte =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 				(loff_t)map->m_lblk << inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 			loff_t length = (loff_t)map->m_len << inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 				ret = ext4_jbd2_inode_add_wait(handle, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 						start_byte, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 				ret = ext4_jbd2_inode_add_write(handle, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 						start_byte, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	if (retval > 0 && (map->m_flags & EXT4_MAP_UNWRITTEN ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 				map->m_flags & EXT4_MAP_MAPPED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		ext4_fc_track_range(handle, inode, map->m_lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 					map->m_lblk + map->m_len - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		ext_debug(inode, "failed with err %d\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756)  * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757)  * we have to be careful as someone else may be manipulating b_state as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	unsigned long old_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	unsigned long new_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	flags &= EXT4_MAP_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	/* Dummy buffer_head? Set non-atomically. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	if (!bh->b_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	 * Someone else may be modifying b_state. Be careful! This is ugly but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	 * once we get rid of using bh as a container for mapping information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	 * to pass to / from get_block functions, this can go away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		old_state = READ_ONCE(bh->b_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	} while (unlikely(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		 cmpxchg(&bh->b_state, old_state, new_state) != old_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) static int _ext4_get_block(struct inode *inode, sector_t iblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 			   struct buffer_head *bh, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	struct ext4_map_blocks map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	if (ext4_has_inline_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	map.m_lblk = iblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	map.m_len = bh->b_size >> inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 			      flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		map_bh(bh, inode->i_sb, map.m_pblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		ext4_update_bh_state(bh, map.m_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		bh->b_size = inode->i_sb->s_blocksize * map.m_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	} else if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		/* hole case, need to fill in bh->b_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		bh->b_size = inode->i_sb->s_blocksize * map.m_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) int ext4_get_block(struct inode *inode, sector_t iblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		   struct buffer_head *bh, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	return _ext4_get_block(inode, iblock, bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 			       create ? EXT4_GET_BLOCKS_CREATE : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817)  * Get block function used when preparing for buffered write if we require
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818)  * creating an unwritten extent if blocks haven't been allocated.  The extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819)  * will be converted to written after the IO is complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			     struct buffer_head *bh_result, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		   inode->i_ino, create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	return _ext4_get_block(inode, iblock, bh_result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			       EXT4_GET_BLOCKS_IO_CREATE_EXT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) /* Maximum number of blocks we map for direct IO at once. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) #define DIO_MAX_BLOCKS 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834)  * `handle' can be NULL if create is zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 				ext4_lblk_t block, int map_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	struct ext4_map_blocks map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	int create = map_flags & EXT4_GET_BLOCKS_CREATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	J_ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		 || handle != NULL || create == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	map.m_lblk = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	map.m_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	err = ext4_map_blocks(handle, inode, &map, map_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	if (err == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		return create ? ERR_PTR(-ENOSPC) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	bh = sb_getblk(inode->i_sb, map.m_pblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	if (unlikely(!bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	if (map.m_flags & EXT4_MAP_NEW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		J_ASSERT(create != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		J_ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 			 || (handle != NULL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		 * Now that we do not always journal data, we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		 * keep in mind whether this should always journal the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		 * new buffer as metadata.  For now, regular file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		 * writes use ext4_get_block instead, so it's not a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		 * problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 		BUFFER_TRACE(bh, "call get_create_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		err = ext4_journal_get_create_access(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		if (!buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 			memset(bh->b_data, 0, inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 			set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		err = ext4_handle_dirty_metadata(handle, inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		BUFFER_TRACE(bh, "not a new buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 			       ext4_lblk_t block, int map_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	bh = ext4_getblk(handle, inode, block, map_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	if (IS_ERR(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	if (!bh || ext4_buffer_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		put_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) /* Read a contiguous batch of blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		     bool wait, struct buffer_head **bhs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	for (i = 0; i < bh_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		if (IS_ERR(bhs[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 			err = PTR_ERR(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 			bh_count = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 			goto out_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	for (i = 0; i < bh_count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		/* Note that NULL bhs[i] is valid because of holes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		if (bhs[i] && !ext4_buffer_uptodate(bhs[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			ext4_read_bh_lock(bhs[i], REQ_META | REQ_PRIO, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	if (!wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	for (i = 0; i < bh_count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		if (bhs[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 			wait_on_buffer(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	for (i = 0; i < bh_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		if (bhs[i] && !buffer_uptodate(bhs[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 			goto out_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) out_brelse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	for (i = 0; i < bh_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		brelse(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		bhs[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) int ext4_walk_page_buffers(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			   struct buffer_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			   unsigned from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 			   unsigned to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			   int *partial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			   int (*fn)(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 				     struct buffer_head *bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	unsigned block_start, block_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	unsigned blocksize = head->b_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	int err, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	struct buffer_head *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	for (bh = head, block_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	     ret == 0 && (bh != head || !block_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	     block_start = block_end, bh = next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		next = bh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		block_end = block_start + blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		if (block_end <= from || block_start >= to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 			if (partial && !buffer_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 				*partial = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		err = (*fn)(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990)  * To preserve ordering, it is essential that the hole instantiation and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991)  * the data write be encapsulated in a single transaction.  We cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992)  * close off a transaction and start a new one between the ext4_get_block()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993)  * and the commit_write().  So doing the jbd2_journal_start at the start of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994)  * prepare_write() is the right place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996)  * Also, this function can nest inside ext4_writepage().  In that case, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997)  * *know* that ext4_writepage() has generated enough buffer credits to do the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  * whole page.  So we won't block on the journal in that case, which is good,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  * because the caller may be PF_MEMALLOC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)  * By accident, ext4 can be reentered when a transaction is open via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)  * quota file writes.  If we were to commit the transaction while thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)  * reentered, there can be a deadlock - we would be holding a quota
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)  * lock, and the commit would never complete if another thread had a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)  * transaction open and was blocking on the quota lock - a ranking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006)  * violation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)  * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)  * will _not_ run commit under these circumstances because handle->h_ref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010)  * is elevated.  We'll still have enough credits for the tiny quotafile
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011)  * write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) int do_journal_get_write_access(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 				struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	int dirty = buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	if (!buffer_mapped(bh) || buffer_freed(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	 * __block_write_begin() could have dirtied some buffers. Clean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	 * the dirty bit as jbd2_journal_get_write_access() could complain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	 * otherwise about fs integrity issues. Setting of the dirty bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	 * by __block_write_begin() isn't a real problem here as we clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	 * the bit before releasing a page lock and thus writeback cannot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	 * ever write the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	if (dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 		clear_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	BUFFER_TRACE(bh, "get write access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	ret = ext4_journal_get_write_access(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	if (!ret && dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		ret = ext4_handle_dirty_metadata(handle, NULL, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) #ifdef CONFIG_FS_ENCRYPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 				  get_block_t *get_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	unsigned from = pos & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	unsigned to = from + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	unsigned block_start, block_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	sector_t block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	unsigned blocksize = inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	unsigned bbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	struct buffer_head *bh, *head, *wait[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	int nr_wait = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	BUG_ON(!PageLocked(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	BUG_ON(from > PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	BUG_ON(to > PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	BUG_ON(from > to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	if (!page_has_buffers(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		create_empty_buffers(page, blocksize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	head = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	bbits = ilog2(blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	block = (sector_t)page->index << (PAGE_SHIFT - bbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	for (bh = head, block_start = 0; bh != head || !block_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	    block++, block_start = block_end, bh = bh->b_this_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		block_end = block_start + blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		if (block_end <= from || block_start >= to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			if (PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 				if (!buffer_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 					set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		if (buffer_new(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 			clear_buffer_new(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		if (!buffer_mapped(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 			WARN_ON(bh->b_size != blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 			err = get_block(inode, block, bh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			if (buffer_new(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 				if (PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 					clear_buffer_new(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 					set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 					mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 				if (block_end > to || block_start < from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 					zero_user_segments(page, to, block_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 							   block_start, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		if (PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			if (!buffer_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 				set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		    !buffer_unwritten(bh) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		    (block_start < from || block_end > to)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			ext4_read_bh_lock(bh, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			wait[nr_wait++] = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	 * If we issued read requests, let them complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	for (i = 0; i < nr_wait; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		wait_on_buffer(wait[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		if (!buffer_uptodate(wait[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 	if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		page_zero_new_buffers(page, from, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	} else if (fscrypt_inode_uses_fs_layer_crypto(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		for (i = 0; i < nr_wait; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 			int err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 			err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 								bh_offset(wait[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 			if (err2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 				clear_buffer_uptodate(wait[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 				err = err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) static int ext4_write_begin(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 			    loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 			    struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	int ret, needed_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	int retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	pgoff_t index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	unsigned from, to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	if (trace_android_fs_datawrite_start_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		path = android_fstrace_get_pathname(pathbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 						    MAX_TRACE_PATHBUF_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 						    inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 		trace_android_fs_datawrite_start(inode, pos, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 						 current->pid, path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 						 current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	trace_ext4_write_begin(inode, pos, len, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	 * Reserve one block more for addition to orphan list in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	 * we allocate blocks but write fails for some reason
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	index = pos >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	from = pos & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	to = from + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 						    flags, pagep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	 * grab_cache_page_write_begin() can take a long time if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	 * system is thrashing due to memory pressure, or if the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	 * is being written back.  So grab it first before we start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	 * the transaction handle.  This also allows us to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	 * the page (if needed) without using GFP_NOFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) retry_grab:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	page = grab_cache_page_write_begin(mapping, index, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) retry_journal:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		return PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	if (page->mapping != mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 		/* The page got truncated from under us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 		goto retry_grab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	/* In case writeback began while the page was unlocked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	wait_for_stable_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) #ifdef CONFIG_FS_ENCRYPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	if (ext4_should_dioread_nolock(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		ret = ext4_block_write_begin(page, pos, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 					     ext4_get_block_unwritten);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		ret = ext4_block_write_begin(page, pos, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 					     ext4_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	if (ext4_should_dioread_nolock(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		ret = __block_write_begin(page, pos, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 					  ext4_get_block_unwritten);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		ret = __block_write_begin(page, pos, len, ext4_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	if (!ret && ext4_should_journal_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		ret = ext4_walk_page_buffers(handle, page_buffers(page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 					     from, to, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 					     do_journal_get_write_access);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		bool extended = (pos + len > inode->i_size) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 				!ext4_verity_in_progress(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		 * __block_write_begin may have instantiated a few blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		 * outside i_size.  Trim these off again. Don't need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		 * i_size_read because we hold i_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		 * Add inode to orphan list in case we crash before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		 * truncate finishes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		if (extended && ext4_can_truncate(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			ext4_orphan_add(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 		if (extended) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			ext4_truncate_failed_write(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 			 * If truncate failed early the inode might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 			 * still be on the orphan list; we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 			 * make sure the inode is removed from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 			 * orphan list in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 			if (inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 				ext4_orphan_del(NULL, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		if (ret == -ENOSPC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		    ext4_should_retry_alloc(inode->i_sb, &retries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 			goto retry_journal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	*pagep = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) /* For write_end() in data=journal mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) static int write_end_fn(handle_t *handle, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	if (!buffer_mapped(bh) || buffer_freed(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	ret = ext4_handle_dirty_metadata(handle, NULL, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	clear_buffer_meta(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	clear_buffer_prio(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)  * We need to pick up the new inode size which generic_commit_write gave us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)  * `file' can be NULL - eg, when called from page_symlink().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)  * ext4 never places buffers on inode->i_mapping->private_list.  metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)  * buffers are managed internally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) static int ext4_write_end(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 			  struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			  loff_t pos, unsigned len, unsigned copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 			  struct page *page, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	handle_t *handle = ext4_journal_current_handle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	loff_t old_size = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	int ret = 0, ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	int i_size_changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	int inline_data = ext4_has_inline_data(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	bool verity = ext4_verity_in_progress(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	trace_android_fs_datawrite_end(inode, pos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	trace_ext4_write_end(inode, pos, len, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	if (inline_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		ret = ext4_write_inline_data_end(inode, pos, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 						 copied, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		copied = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		copied = block_write_end(file, mapping, pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 					 len, copied, page, fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	 * it's important to update i_size while still holding page lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	 * page writeout could otherwise come in and zero beyond i_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	 * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	 * blocks are being written past EOF, so skip the i_size update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	if (!verity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		i_size_changed = ext4_update_inode_size(inode, pos + copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	if (old_size < pos && !verity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		pagecache_isize_extended(inode, old_size, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	 * Don't mark the inode dirty under page lock. First, it unnecessarily
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	 * makes the holding time of page lock longer. Second, it forces lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	 * ordering of page lock and transaction start for journaling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	 * filesystems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	if (i_size_changed || inline_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		ret = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		/* if we have allocated more blocks and copied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		 * less. We will have blocks allocated outside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		 * inode->i_size. So truncate them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		ext4_orphan_add(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	ret2 = ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		ret = ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	if (pos + len > inode->i_size && !verity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		ext4_truncate_failed_write(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		 * If truncate failed early the inode might still be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		 * on the orphan list; we need to make sure the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		 * is removed from the orphan list in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		if (inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 			ext4_orphan_del(NULL, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	return ret ? ret : copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)  * This is a private version of page_zero_new_buffers() which doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)  * set the buffer to be dirty, since in data=journalled mode we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)  * to call ext4_handle_dirty_metadata() instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) static void ext4_journalled_zero_new_buffers(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 					    struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 					    unsigned from, unsigned to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	unsigned int block_start = 0, block_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	struct buffer_head *head, *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	bh = head = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		block_end = block_start + bh->b_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		if (buffer_new(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 			if (block_end > from && block_start < to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 				if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 					unsigned start, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 					start = max(from, block_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 					size = min(to, block_end) - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 					zero_user(page, start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 					write_end_fn(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 				clear_buffer_new(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		block_start = block_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		bh = bh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	} while (bh != head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) static int ext4_journalled_write_end(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 				     struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 				     loff_t pos, unsigned len, unsigned copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 				     struct page *page, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	handle_t *handle = ext4_journal_current_handle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	loff_t old_size = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	int ret = 0, ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	int partial = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	unsigned from, to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	int size_changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	int inline_data = ext4_has_inline_data(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	bool verity = ext4_verity_in_progress(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	trace_android_fs_datawrite_end(inode, pos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	trace_ext4_journalled_write_end(inode, pos, len, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	from = pos & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	to = from + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	BUG_ON(!ext4_handle_valid(handle));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	if (inline_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		ret = ext4_write_inline_data_end(inode, pos, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 						 copied, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		copied = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	} else if (unlikely(copied < len) && !PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		copied = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		ext4_journalled_zero_new_buffers(handle, page, from, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		if (unlikely(copied < len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 			ext4_journalled_zero_new_buffers(handle, page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 							 from + copied, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 		ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 					     from + copied, &partial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 					     write_end_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		if (!partial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 			SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	if (!verity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		size_changed = ext4_update_inode_size(inode, pos + copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	ext4_set_inode_state(inode, EXT4_STATE_JDATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	if (old_size < pos && !verity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		pagecache_isize_extended(inode, old_size, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	if (size_changed || inline_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		ret2 = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 			ret = ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		/* if we have allocated more blocks and copied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		 * less. We will have blocks allocated outside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 		 * inode->i_size. So truncate them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		ext4_orphan_add(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	ret2 = ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 		ret = ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	if (pos + len > inode->i_size && !verity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		ext4_truncate_failed_write(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 		 * If truncate failed early the inode might still be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		 * on the orphan list; we need to make sure the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		 * is removed from the orphan list in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		if (inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 			ext4_orphan_del(NULL, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	return ret ? ret : copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486)  * Reserve space for a single cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) static int ext4_da_reserve_space(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	 * We will charge metadata quota at writeout time; this saves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	 * us from metadata over-estimation, though we may go over by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	 * a small amount in the end.  Here we just reserve for data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	spin_lock(&ei->i_block_reservation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	if (ext4_claim_free_clusters(sbi, 1, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		spin_unlock(&ei->i_block_reservation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	ei->i_reserved_data_blocks++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	trace_ext4_da_reserve_space(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	spin_unlock(&ei->i_block_reservation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	return 0;       /* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) void ext4_da_release_space(struct inode *inode, int to_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	if (!to_free)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		return;		/* Nothing to release, exit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	trace_ext4_da_release_space(inode, to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	if (unlikely(to_free > ei->i_reserved_data_blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 		 * if there aren't enough reserved blocks, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 		 * counter is messed up somewhere.  Since this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		 * function is called from invalidate page, it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		 * harmless to return without any action.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 		ext4_warning(inode->i_sb, "ext4_da_release_space: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 			 "ino %lu, to_free %d with only %d reserved "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 			 "data blocks", inode->i_ino, to_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 			 ei->i_reserved_data_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 		to_free = ei->i_reserved_data_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	ei->i_reserved_data_blocks -= to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	/* update fs dirty data blocks counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552)  * Delayed allocation stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) struct mpage_da_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	struct writeback_control *wbc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	pgoff_t first_page;	/* The first page to write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	pgoff_t next_page;	/* Current page to examine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	pgoff_t last_page;	/* Last page to examine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	 * Extent to map - this can be after first_page because that can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 	 * fully mapped. We somewhat abuse m_flags to store whether the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	 * is delalloc or unwritten.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	struct ext4_map_blocks map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	struct ext4_io_submit io_submit;	/* IO submission data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	unsigned int do_map:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	unsigned int scanned_until_end:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) static void mpage_release_unused_pages(struct mpage_da_data *mpd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 				       bool invalidate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	int nr_pages, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	pgoff_t index, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	struct pagevec pvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	struct inode *inode = mpd->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	struct address_space *mapping = inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	/* This is necessary when next_page == 0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	if (mpd->first_page >= mpd->next_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	mpd->scanned_until_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	index = mpd->first_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	end   = mpd->next_page - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	if (invalidate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 		ext4_lblk_t start, last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		start = index << (PAGE_SHIFT - inode->i_blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 		last = end << (PAGE_SHIFT - inode->i_blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 		ext4_es_remove_extent(inode, start, last - start + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	pagevec_init(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	while (index <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 		nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		if (nr_pages == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 			struct page *page = pvec.pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 			BUG_ON(!PageLocked(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 			BUG_ON(PageWriteback(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 			if (invalidate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 				if (page_mapped(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 					clear_page_dirty_for_io(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 				block_invalidatepage(page, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 				ClearPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) static void ext4_print_free_blocks(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 	ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	       EXT4_C2B(EXT4_SB(inode->i_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 			ext4_count_free_clusters(sb)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 	ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	       (long long) EXT4_C2B(EXT4_SB(sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 		percpu_counter_sum(&sbi->s_freeclusters_counter)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	       (long long) EXT4_C2B(EXT4_SB(sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	ext4_msg(sb, KERN_CRIT, "Block reservation details");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		 ei->i_reserved_data_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646)  * ext4_insert_delayed_block - adds a delayed block to the extents status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647)  *                             tree, incrementing the reserved cluster/block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)  *                             count or making a pending reservation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)  *                             where needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)  * @inode - file containing the newly added block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652)  * @lblk - logical block to be added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)  * Returns 0 on success, negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	bool allocated = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	bool reserved = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	 * If the cluster containing lblk is shared with a delayed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	 * written, or unwritten extent in a bigalloc file system, it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	 * already been accounted for and does not need to be reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	 * A pending reservation must be made for the cluster if it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	 * shared with a written or unwritten extent and doesn't already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	 * have one.  Written and unwritten extents can be purged from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	 * extents status tree if the system is under memory pressure, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	 * it's necessary to examine the extent tree if a search of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	 * extents status tree doesn't get a match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	if (sbi->s_cluster_ratio == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		ret = ext4_da_reserve_space(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		if (ret != 0)   /* ENOSPC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 			goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 		reserved = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	} else {   /* bigalloc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 			if (!ext4_es_scan_clu(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 					      &ext4_es_is_mapped, lblk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 				ret = ext4_clu_mapped(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 						      EXT4_B2C(sbi, lblk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 				if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 					goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 				if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 					ret = ext4_da_reserve_space(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 					if (ret != 0)   /* ENOSPC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 						goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 					reserved = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 					allocated = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 				allocated = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	ret = ext4_es_insert_delayed_block(inode, lblk, allocated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	if (ret && reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		ext4_da_release_space(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)  * This function is grabs code from the very beginning of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711)  * ext4_map_blocks, but assumes that the caller is from delayed write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)  * time. This function looks up the requested blocks and sets the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)  * buffer delay bit under the protection of i_data_sem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 			      struct ext4_map_blocks *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 			      struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	struct extent_status es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	sector_t invalid_block = ~((sector_t) 0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) #ifdef ES_AGGRESSIVE_TEST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	struct ext4_map_blocks orig_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	memcpy(&orig_map, map, sizeof(*map));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 		invalid_block = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	map->m_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 		  (unsigned long) map->m_lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	/* Lookup extent status tree firstly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		if (ext4_es_is_hole(&es)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 			retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 			down_read(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 			goto add_delayed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 		 * Delayed extent could be allocated by fallocate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		 * So we need to check it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 		if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 			map_bh(bh, inode->i_sb, invalid_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 			set_buffer_new(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 			set_buffer_delay(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 		retval = es.es_len - (iblock - es.es_lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		if (retval > map->m_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 			retval = map->m_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		map->m_len = retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 		if (ext4_es_is_written(&es))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 			map->m_flags |= EXT4_MAP_MAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		else if (ext4_es_is_unwritten(&es))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 			map->m_flags |= EXT4_MAP_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) #ifdef ES_AGGRESSIVE_TEST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 		ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	 * Try to see if we can get the block without requesting a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 	 * file system block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	down_read(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	if (ext4_has_inline_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 		retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 		retval = ext4_ext_map_blocks(NULL, inode, map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		retval = ext4_ind_map_blocks(NULL, inode, map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) add_delayed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	if (retval == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		 * XXX: __block_prepare_write() unmaps passed block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 		 * is it OK?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		ret = ext4_insert_delayed_block(inode, map->m_lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 		if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 			retval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		map_bh(bh, inode->i_sb, invalid_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		set_buffer_new(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 		set_buffer_delay(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	} else if (retval > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		unsigned int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 		if (unlikely(retval != map->m_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 			ext4_warning(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 				     "ES len assertion failed for inode "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 				     "%lu: retval %d != map->m_len %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 				     inode->i_ino, retval, map->m_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 			WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		status = map->m_flags & EXT4_MAP_UNWRITTEN ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 				EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 					    map->m_pblk, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 			retval = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	up_read((&EXT4_I(inode)->i_data_sem));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829)  * This is a special get_block_t callback which is used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830)  * ext4_da_write_begin().  It will either return mapped block or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831)  * reserve space for a single block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833)  * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834)  * We also have b_blocknr = -1 and b_bdev initialized properly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836)  * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837)  * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838)  * initialized properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 			   struct buffer_head *bh, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	struct ext4_map_blocks map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	BUG_ON(create == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	map.m_lblk = iblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	map.m_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	 * first, we need to know whether the block is allocated already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	 * preallocated blocks are unmapped but should treated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	 * the same as allocated blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 	ret = ext4_da_map_blocks(inode, iblock, &map, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	if (ret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	map_bh(bh, inode->i_sb, map.m_pblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	ext4_update_bh_state(bh, map.m_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	if (buffer_unwritten(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		/* A delayed write to unwritten bh should be marked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 		 * new and mapped.  Mapped ensures that we don't do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 		 * get_block multiple times when we write to the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		 * offset and new ensures that we do proper zero out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 		 * for partial write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		set_buffer_new(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		set_buffer_mapped(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) static int bget_one(handle_t *handle, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	get_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) static int bput_one(handle_t *handle, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 	put_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) static int __ext4_journalled_writepage(struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 				       unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	struct address_space *mapping = page->mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	struct buffer_head *page_bufs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	handle_t *handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	int ret = 0, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	int inline_data = ext4_has_inline_data(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	struct buffer_head *inode_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	if (inline_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		BUG_ON(page->index != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 		BUG_ON(len > ext4_get_max_inline_size(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 		inode_bh = ext4_journalled_write_inline_data(inode, len, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		if (inode_bh == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 		page_bufs = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 		if (!page_bufs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 			BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 		ext4_walk_page_buffers(handle, page_bufs, 0, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 				       NULL, bget_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	 * We need to release the page lock before we start the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	 * journal, so grab a reference so the page won't disappear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	 * out from under us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 	get_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 				    ext4_writepage_trans_blocks(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 		ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		goto out_no_pagelock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	BUG_ON(!ext4_handle_valid(handle));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	if (page->mapping != mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		/* The page got truncated from under us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	if (inline_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 		ret = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 		ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 					     do_journal_get_write_access);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 		err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 					     write_end_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 		ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	err = ext4_jbd2_inode_add_write(handle, inode, page_offset(page), len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 		ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 	err = ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	ext4_set_inode_state(inode, EXT4_STATE_JDATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) out_no_pagelock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	if (!inline_data && page_bufs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 		ext4_walk_page_buffers(NULL, page_bufs, 0, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 				       NULL, bput_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	brelse(inode_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974)  * Note that we don't need to start a transaction unless we're journaling data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975)  * because we should have holes filled from ext4_page_mkwrite(). We even don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976)  * need to file the inode to the transaction's list in ordered mode because if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977)  * we are writing back data added by write(), the inode is already there and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978)  * we are writing back data modified via mmap(), no one guarantees in which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979)  * transaction the data will hit the disk. In case we are journaling data, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980)  * cannot start transaction directly because transaction start ranks above page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981)  * lock so we have to do some magic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983)  * This function can get called via...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984)  *   - ext4_writepages after taking page lock (have journal handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985)  *   - journal_submit_inode_data_buffers (no journal handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986)  *   - shrink_page_list via the kswapd/direct reclaim (no journal handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987)  *   - grab_page_cache when doing write_begin (have journal handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989)  * We don't do any block allocation in this function. If we have page with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990)  * multiple blocks we need to write those buffer_heads that are mapped. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991)  * is important for mmaped based write. So if we do with blocksize 1K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992)  * truncate(f, 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993)  * a = mmap(f, 0, 4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994)  * a[0] = 'a';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995)  * truncate(f, 4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996)  * we have in the page first buffer_head mapped via page_mkwrite call back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997)  * but other buffer_heads would be unmapped but dirty (dirty done via the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998)  * do_wp_page). So writepage should write the first block. If we modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999)  * the mmap area beyond 1024 we will again get a page_fault and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000)  * page_mkwrite callback will do the block allocation and mark the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001)  * buffer_heads mapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003)  * We redirty the page if we have any buffer_heads that is either delay or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004)  * unwritten in the page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006)  * We can get recursively called as show below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008)  *	ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009)  *		ext4_writepage()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011)  * But since we don't do any block allocation we should not deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012)  * Page also have the dirty flag cleared so we don't get recurive page_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) static int ext4_writepage(struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 			  struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	loff_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	struct buffer_head *page_bufs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	struct ext4_io_submit io_submit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	bool keep_towrite = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 		inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	trace_ext4_writepage(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	if (page->index == size >> PAGE_SHIFT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 	    !ext4_verity_in_progress(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		len = size & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 		len = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	/* Should never happen but for bugs in other kernel subsystems */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	if (!page_has_buffers(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 		ext4_warning_inode(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		   "page %lu does not have buffers attached", page->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 		ClearPageDirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	page_bufs = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 	 * We cannot do block allocation or other extent handling in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	 * function. If there are buffers needing that, we have to redirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	 * the page. But we may reach here when we do a journal commit via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 	 * journal_submit_inode_data_buffers() and in that case we must write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	 * allocated buffers to achieve data=ordered mode guarantees.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 	 * Also, if there is only one buffer per page (the fs block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 	 * size == the page size), if one buffer needs block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	 * allocation or needs to modify the extent tree to clear the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	 * unwritten flag, we know that the page can't be written at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 	 * all, so we might as well refuse the write immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	 * Unfortunately if the block size != page size, we can't as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 	 * easily detect this case using ext4_walk_page_buffers(), but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	 * for the extremely common case, this is an optimization that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 	 * skips a useless round trip through ext4_bio_write_page().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 				   ext4_bh_delay_or_unwritten)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 		redirty_page_for_writepage(wbc, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 		if ((current->flags & PF_MEMALLOC) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 		    (inode->i_sb->s_blocksize == PAGE_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 			 * For memory cleaning there's no point in writing only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 			 * some buffers. So just bail out. Warn if we came here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 			 * from direct reclaim.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 			WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 							== PF_MEMALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 		keep_towrite = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 	if (PageChecked(page) && ext4_should_journal_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 		 * It's mmapped pagecache.  Add buffers and journal it.  There
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 		 * doesn't seem much point in redirtying the page here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 		return __ext4_journalled_writepage(page, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	ext4_io_submit_init(&io_submit, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 	io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	if (!io_submit.io_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 		redirty_page_for_writepage(wbc, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	ext4_io_submit(&io_submit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	/* Drop io_end reference we got from init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	ext4_put_io_end_defer(io_submit.io_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	loff_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 	BUG_ON(page->index != mpd->first_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	clear_page_dirty_for_io(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	 * We have to be very careful here!  Nothing protects writeback path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 	 * against i_size changes and the page can be writeably mapped into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	 * page tables. So an application can be growing i_size and writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 	 * data through mmap while writeback runs. clear_page_dirty_for_io()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	 * write-protects our page in page tables and the page cannot get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	 * written to again until we release page lock. So only after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	 * clear_page_dirty_for_io() we are safe to sample i_size for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	 * ext4_bio_write_page() to zero-out tail of the written page. We rely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 	 * on the barrier provided by TestClearPageDirty in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 	 * clear_page_dirty_for_io() to make sure i_size is really sampled only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 	 * after page tables are updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	size = i_size_read(mpd->inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	if (page->index == size >> PAGE_SHIFT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	    !ext4_verity_in_progress(mpd->inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 		len = size & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 		len = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 		mpd->wbc->nr_to_write--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	mpd->first_page++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) #define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143)  * mballoc gives us at most this number of blocks...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144)  * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145)  * The rest of mballoc seems to handle chunks up to full group size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) #define MAX_WRITEPAGES_EXTENT_LEN 2048
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150)  * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152)  * @mpd - extent of blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153)  * @lblk - logical number of the block in the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154)  * @bh - buffer head we want to add to the extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156)  * The function is used to collect contig. blocks in the same state. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157)  * buffer doesn't require mapping for writeback and we haven't started the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158)  * extent of buffers to map yet, the function returns 'true' immediately - the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159)  * caller can write the buffer right away. Otherwise the function returns true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160)  * if the block has been added to the extent, false if the block couldn't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161)  * added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 				   struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 	struct ext4_map_blocks *map = &mpd->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 	/* Buffer that doesn't need mapping for writeback? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 	if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 	    (!buffer_delay(bh) && !buffer_unwritten(bh))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 		/* So far no extent to map => we write the buffer right away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 		if (map->m_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 	/* First block in the extent? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 	if (map->m_len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 		/* We cannot map unless handle is started... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 		if (!mpd->do_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 		map->m_lblk = lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 		map->m_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 		map->m_flags = bh->b_state & BH_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	/* Don't go larger than mballoc is willing to allocate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 	if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 	/* Can we merge the block to our big extent? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 	if (lblk == map->m_lblk + map->m_len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 	    (bh->b_state & BH_FLAGS) == map->m_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 		map->m_len++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202)  * mpage_process_page_bufs - submit page buffers for IO or add them to extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204)  * @mpd - extent of blocks for mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205)  * @head - the first buffer in the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206)  * @bh - buffer we should start processing from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207)  * @lblk - logical number of the block in the file corresponding to @bh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209)  * Walk through page buffers from @bh upto @head (exclusive) and either submit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210)  * the page for IO if all buffers in this page were mapped and there's no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211)  * accumulated extent of buffers to map or add buffers in the page to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212)  * extent of buffers to map. The function returns 1 if the caller can continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213)  * by processing the next page, 0 if it should stop adding buffers to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214)  * extent to map because we cannot extend it anymore. It can also return value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215)  * < 0 in case of error during IO submission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) static int mpage_process_page_bufs(struct mpage_da_data *mpd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 				   struct buffer_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 				   struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 				   ext4_lblk_t lblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	struct inode *inode = mpd->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 	ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 							>> inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	if (ext4_verity_in_progress(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 		blocks = EXT_MAX_BLOCKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 		BUG_ON(buffer_locked(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 		if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 			/* Found extent to map? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 			if (mpd->map.m_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 			/* Buffer needs mapping and handle is not started? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 			if (!mpd->do_map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 			/* Everything mapped so far and we hit EOF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 	} while (lblk++, (bh = bh->b_this_page) != head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	/* So far everything mapped? Submit the page for IO. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 	if (mpd->map.m_len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 		err = mpage_submit_page(mpd, head->b_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 	if (lblk >= blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 		mpd->scanned_until_end = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258)  * mpage_process_page - update page buffers corresponding to changed extent and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259)  *		       may submit fully mapped page for IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261)  * @mpd		- description of extent to map, on return next extent to map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262)  * @m_lblk	- logical block mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263)  * @m_pblk	- corresponding physical mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264)  * @map_bh	- determines on return whether this page requires any further
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265)  *		  mapping or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266)  * Scan given page buffers corresponding to changed extent and update buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267)  * state according to new extent state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268)  * We map delalloc buffers to their physical location, clear unwritten bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269)  * If the given page is not fully mapped, we update @map to the next extent in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270)  * the given page that needs mapping & return @map_bh as true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) static int mpage_process_page(struct mpage_da_data *mpd, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 			      ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 			      bool *map_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 	struct buffer_head *head, *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 	ext4_io_end_t *io_end = mpd->io_submit.io_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	ext4_lblk_t lblk = *m_lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	ext4_fsblk_t pblock = *m_pblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	int blkbits = mpd->inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	ssize_t io_end_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 	struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	bh = head = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 		if (lblk < mpd->map.m_lblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 		if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 			 * Buffer after end of mapped extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 			 * Find next buffer in the page to map.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 			mpd->map.m_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 			mpd->map.m_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 			io_end_vec->size += io_end_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 			io_end_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 			err = mpage_process_page_bufs(mpd, head, bh, lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 			if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 				err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 			if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 				io_end_vec = ext4_alloc_io_end_vec(io_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 				if (IS_ERR(io_end_vec)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 					err = PTR_ERR(io_end_vec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 					goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 				io_end_vec->offset = (loff_t)mpd->map.m_lblk << blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 			*map_bh = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 		if (buffer_delay(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 			clear_buffer_delay(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 			bh->b_blocknr = pblock++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 		clear_buffer_unwritten(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 		io_end_size += (1 << blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	} while (lblk++, (bh = bh->b_this_page) != head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	io_end_vec->size += io_end_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	io_end_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	*map_bh = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	*m_lblk = lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	*m_pblk = pblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331)  * mpage_map_buffers - update buffers corresponding to changed extent and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332)  *		       submit fully mapped pages for IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334)  * @mpd - description of extent to map, on return next extent to map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336)  * Scan buffers corresponding to changed extent (we expect corresponding pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337)  * to be already locked) and update buffer state according to new extent state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338)  * We map delalloc buffers to their physical location, clear unwritten bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339)  * and mark buffers as uninit when we perform writes to unwritten extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340)  * and do extent conversion after IO is finished. If the last page is not fully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341)  * mapped, we update @map to the next extent in the last page that needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342)  * mapping. Otherwise we submit the page for IO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	struct pagevec pvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	int nr_pages, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	struct inode *inode = mpd->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	int bpp_bits = PAGE_SHIFT - inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 	pgoff_t start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	ext4_lblk_t lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 	ext4_fsblk_t pblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 	bool map_bh = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 	start = mpd->map.m_lblk >> bpp_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	lblk = start << bpp_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	pblock = mpd->map.m_pblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	pagevec_init(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	while (start <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 		nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 						&start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 		if (nr_pages == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 		for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 			struct page *page = pvec.pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 			err = mpage_process_page(mpd, page, &lblk, &pblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 						 &map_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 			 * If map_bh is true, means page may require further bh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 			 * mapping, or maybe the page was submitted for IO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 			 * So we return to call further extent mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 			if (err < 0 || map_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 			/* Page fully mapped - let IO run! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 			err = mpage_submit_page(mpd, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 			if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 		pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	/* Extent fully mapped and matches with page boundary. We are done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 	mpd->map.m_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	mpd->map.m_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 	pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 	struct inode *inode = mpd->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 	struct ext4_map_blocks *map = &mpd->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	int get_blocks_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 	int err, dioread_nolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 	trace_ext4_da_write_pages_extent(inode, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	 * to convert an unwritten extent to be initialized (in the case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	 * where we have written into one or more preallocated blocks).  It is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	 * possible that we're going to need more metadata blocks than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	 * previously reserved. However we must not fail because we're in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	 * writeback and there is nothing we can do about it so it might result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	 * in data loss.  So use reserved blocks to allocate metadata if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 	 * possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 	 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	 * the blocks in question are delalloc blocks.  This indicates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 	 * that the blocks and quotas has already been checked when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 	 * the data was copied into the page cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 			   EXT4_GET_BLOCKS_METADATA_NOFAIL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 			   EXT4_GET_BLOCKS_IO_SUBMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 	dioread_nolock = ext4_should_dioread_nolock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 	if (dioread_nolock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 		get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	if (map->m_flags & BIT(BH_Delay))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 		get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 	if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 		if (!mpd->io_submit.io_end->handle &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 		    ext4_handle_valid(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 			mpd->io_submit.io_end->handle = handle->h_rsv_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 			handle->h_rsv_handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 		ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 	BUG_ON(map->m_len == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444)  * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445)  *				 mpd->len and submit pages underlying it for IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447)  * @handle - handle for journal operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448)  * @mpd - extent to map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449)  * @give_up_on_write - we set this to true iff there is a fatal error and there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450)  *                     is no hope of writing the data. The caller should discard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451)  *                     dirty pages to avoid infinite loops.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453)  * The function maps extent starting at mpd->lblk of length mpd->len. If it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454)  * delayed, blocks are allocated, if it is unwritten, we may need to convert
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455)  * them to initialized or split the described range from larger unwritten
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456)  * extent. Note that we need not map all the described range since allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457)  * can return less blocks or the range is covered by more unwritten extents. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458)  * cannot map more because we are limited by reserved transaction credits. On
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459)  * the other hand we always make sure that the last touched page is fully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460)  * mapped so that it can be written out (and thus forward progress is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461)  * guaranteed). After mapping we submit all mapped pages for IO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) static int mpage_map_and_submit_extent(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 				       struct mpage_da_data *mpd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 				       bool *give_up_on_write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 	struct inode *inode = mpd->inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 	struct ext4_map_blocks *map = &mpd->map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 	loff_t disksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 	int progress = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 	ext4_io_end_t *io_end = mpd->io_submit.io_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	struct ext4_io_end_vec *io_end_vec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	io_end_vec = ext4_alloc_io_end_vec(io_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	if (IS_ERR(io_end_vec))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 		return PTR_ERR(io_end_vec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 		err = mpage_map_one_extent(handle, mpd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 			struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 			if (ext4_forced_shutdown(EXT4_SB(sb)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 			    ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 				goto invalidate_dirty_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 			 * Let the uper layers retry transient errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 			 * In the case of ENOSPC, if ext4_count_free_blocks()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 			 * is non-zero, a commit should free up blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 			if ((err == -ENOMEM) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 			    (err == -ENOSPC && ext4_count_free_clusters(sb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 				if (progress)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 					goto update_disksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 				return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 			ext4_msg(sb, KERN_CRIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 				 "Delayed block allocation failed for "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 				 "inode %lu at logical offset %llu with"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 				 " max blocks %u with error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 				 inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 				 (unsigned long long)map->m_lblk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 				 (unsigned)map->m_len, -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 			ext4_msg(sb, KERN_CRIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 				 "This should not happen!! Data will "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 				 "be lost\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 			if (err == -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 				ext4_print_free_blocks(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 		invalidate_dirty_pages:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 			*give_up_on_write = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 		progress = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 		 * Update buffer state, submit mapped pages, and get us new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 		 * extent to map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 		err = mpage_map_and_submit_buffers(mpd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 			goto update_disksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	} while (map->m_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) update_disksize:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 	 * Update on-disk size after IO is submitted.  Races with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	 * truncate are avoided by checking i_size under i_data_sem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 	disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 	if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 		int err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 		loff_t i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 		down_write(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 		i_size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 		if (disksize > i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 			disksize = i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 		if (disksize > EXT4_I(inode)->i_disksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 			EXT4_I(inode)->i_disksize = disksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 		up_write(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 		err2 = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 		if (err2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 			ext4_error_err(inode->i_sb, -err2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 				       "Failed to mark inode %lu dirty",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 				       inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 			err = err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554)  * Calculate the total number of credits to reserve for one writepages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555)  * iteration. This is called from ext4_writepages(). We map an extent of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556)  * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557)  * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558)  * bpp - 1 blocks in bpp different extents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) static int ext4_da_writepages_trans_blocks(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 	int bpp = ext4_journal_blocks_per_page(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 	return ext4_meta_trans_blocks(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 				MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569)  * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570)  * 				 and underlying extent to map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572)  * @mpd - where to look for pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574)  * Walk dirty pages in the mapping. If they are fully mapped, submit them for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575)  * IO immediately. When we find a page which isn't mapped we start accumulating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576)  * extent of buffers underlying these pages that needs mapping (formed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577)  * either delayed or unwritten buffers). We also lock the pages containing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578)  * these buffers. The extent found is returned in @mpd structure (starting at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579)  * mpd->lblk with length mpd->len blocks).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581)  * Note that this function can attach bios to one io_end structure which are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582)  * neither logically nor physically contiguous. Although it may seem as an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583)  * unnecessary complication, it is actually inevitable in blocksize < pagesize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584)  * case as we need to track IO to all buffers underlying a page in one io_end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) 	struct address_space *mapping = mpd->inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 	struct pagevec pvec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 	unsigned int nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	long left = mpd->wbc->nr_to_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	pgoff_t index = mpd->first_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 	pgoff_t end = mpd->last_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	xa_mark_t tag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	int i, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	int blkbits = mpd->inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	ext4_lblk_t lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 	struct buffer_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 		tag = PAGECACHE_TAG_TOWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 		tag = PAGECACHE_TAG_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	pagevec_init(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 	mpd->map.m_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	mpd->next_page = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 	while (index <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 		nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 				tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 		if (nr_pages == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 		for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 			struct page *page = pvec.pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 			 * Accumulated enough dirty pages? This doesn't apply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 			 * to WB_SYNC_ALL mode. For integrity sync we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 			 * keep going because someone may be concurrently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 			 * dirtying pages, and we might have synced a lot of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 			 * newly appeared dirty pages, but have not synced all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 			 * of the old dirty pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 			if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 			/* If we can't merge this page, we are done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 			if (mpd->map.m_len > 0 && mpd->next_page != page->index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 			lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 			 * If the page is no longer dirty, or its mapping no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 			 * longer corresponds to inode we are writing (which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 			 * means it has been truncated or invalidated), or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 			 * page is already under writeback and we are not doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 			 * a data integrity writeback, skip the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 			if (!PageDirty(page) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 			    (PageWriteback(page) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 			     (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 			    unlikely(page->mapping != mapping)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 				unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 			wait_on_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 			BUG_ON(PageWriteback(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 			 * Should never happen but for buggy code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 			 * other subsystems that call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 			 * set_page_dirty() without properly warning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 			 * the file system first.  See [1] for more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 			 * information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 			 * [1] https://lore.kernel.org/linux-mm/20180103100430.GE4911@quack2.suse.cz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 			if (!page_has_buffers(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 				ext4_warning_inode(mpd->inode, "page %lu does not have buffers attached", page->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 				ClearPageDirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 				unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 			if (mpd->map.m_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 				mpd->first_page = page->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 			mpd->next_page = page->index + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 			/* Add all dirty buffers to mpd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 			lblk = ((ext4_lblk_t)page->index) <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 				(PAGE_SHIFT - blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 			head = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 			err = mpage_process_page_bufs(mpd, head, head, lblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 			if (err <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 			left--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 		pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 		cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 	mpd->scanned_until_end = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 	pagevec_release(&pvec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) static int ext4_writepages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 			   struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	pgoff_t	writeback_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	long nr_to_write = wbc->nr_to_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 	int range_whole = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 	int cycled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	handle_t *handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 	struct mpage_da_data mpd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	int needed_blocks, rsv_blocks = 0, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 	struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 	struct blk_plug plug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 	bool give_up_on_write = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	percpu_down_read(&sbi->s_writepages_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	trace_ext4_writepages(inode, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 	 * No pages to write? This is mainly a kludge to avoid starting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 	 * a transaction for special inodes like journal inode on last iput()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 	 * because that could violate lock ordering on umount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 	if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 		goto out_writepages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 	if (ext4_should_journal_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 		ret = generic_writepages(mapping, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 		goto out_writepages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 	 * If the filesystem has aborted, it is read-only, so return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 	 * right away instead of dumping stack traces later on that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 	 * will obscure the real source of the problem.  We test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 	 * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) 	 * the latter could be true if the filesystem is mounted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 	 * read-only, and in that case, ext4_writepages should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) 	 * *never* be called, so if that ever happens, we would want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) 	 * the stack trace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 		     ext4_test_mount_flag(inode->i_sb, EXT4_MF_FS_ABORTED))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 		ret = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 		goto out_writepages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 	 * If we have inline data and arrive here, it means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 	 * we will soon create the block for the 1st page, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	 * we'd better clear the inline data here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 	if (ext4_has_inline_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 		/* Just inode will be modified... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 		handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 		if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 			ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 			goto out_writepages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 		BUG_ON(ext4_test_inode_state(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 				EXT4_STATE_MAY_INLINE_DATA));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 		ext4_destroy_inline_data(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 	if (ext4_should_dioread_nolock(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 		 * We may need to convert up to one extent per block in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 		 * the page and we may dirty the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 		rsv_blocks = 1 + ext4_chunk_trans_blocks(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 						PAGE_SIZE >> inode->i_blkbits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 	if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 		range_whole = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	if (wbc->range_cyclic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 		writeback_index = mapping->writeback_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 		if (writeback_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 			cycled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 		mpd.first_page = writeback_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 		mpd.last_page = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 		mpd.first_page = wbc->range_start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 		mpd.last_page = wbc->range_end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 	mpd.inode = inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 	mpd.wbc = wbc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 	ext4_io_submit_init(&mpd.io_submit, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 		tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 	blk_start_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 	 * First writeback pages that don't need mapping - we can avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 	 * starting a transaction unnecessarily and also avoid being blocked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 	 * in the block layer on device congestion while having transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 	 * started.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 	mpd.do_map = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 	mpd.scanned_until_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 	mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 	if (!mpd.io_submit.io_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 		goto unplug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 	ret = mpage_prepare_extent_to_map(&mpd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 	/* Unlock pages we didn't use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 	mpage_release_unused_pages(&mpd, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 	/* Submit prepared bio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 	ext4_io_submit(&mpd.io_submit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 	ext4_put_io_end_defer(mpd.io_submit.io_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 	mpd.io_submit.io_end = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 		goto unplug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	while (!mpd.scanned_until_end && wbc->nr_to_write > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 		/* For each extent of pages we use new io_end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 		mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 		if (!mpd.io_submit.io_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 		 * We have two constraints: We find one extent to map and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 		 * must always write out whole page (makes a difference when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 		 * blocksize < pagesize) so that we don't block on IO when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) 		 * try to write out the rest of the page. Journalled mode is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 		 * not supported by delalloc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 		BUG_ON(ext4_should_journal_data(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 		needed_blocks = ext4_da_writepages_trans_blocks(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 		/* start a new transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 		handle = ext4_journal_start_with_reserve(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 				EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 		if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 			ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 			ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 			       "%ld pages, ino %lu; err %d", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 				wbc->nr_to_write, inode->i_ino, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 			/* Release allocated io_end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 			ext4_put_io_end(mpd.io_submit.io_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 			mpd.io_submit.io_end = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 		mpd.do_map = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 		trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 		ret = mpage_prepare_extent_to_map(&mpd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 		if (!ret && mpd.map.m_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 			ret = mpage_map_and_submit_extent(handle, &mpd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 					&give_up_on_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 		 * Caution: If the handle is synchronous,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 		 * ext4_journal_stop() can wait for transaction commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 		 * to finish which may depend on writeback of pages to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 		 * complete or on page lock to be released.  In that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 		 * case, we have to wait until after we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 		 * submitted all the IO, released page locks we hold,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 		 * and dropped io_end reference (for extent conversion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 		 * to be able to complete) before stopping the handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 		if (!ext4_handle_valid(handle) || handle->h_sync == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 			ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 			handle = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 			mpd.do_map = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 		/* Unlock pages we didn't use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) 		mpage_release_unused_pages(&mpd, give_up_on_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 		/* Submit prepared bio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 		ext4_io_submit(&mpd.io_submit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 		 * Drop our io_end reference we got from init. We have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 		 * to be careful and use deferred io_end finishing if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 		 * we are still holding the transaction as we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 		 * release the last reference to io_end which may end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 		 * up doing unwritten extent conversion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 		if (handle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 			ext4_put_io_end_defer(mpd.io_submit.io_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 			ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 			ext4_put_io_end(mpd.io_submit.io_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 		mpd.io_submit.io_end = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 		if (ret == -ENOSPC && sbi->s_journal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 			 * Commit the transaction which would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 			 * free blocks released in the transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) 			 * and try again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 			jbd2_journal_force_commit_nested(sbi->s_journal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) 		/* Fatal error - ENOMEM, EIO... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) unplug:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 	blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 	if (!ret && !cycled && wbc->nr_to_write > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 		cycled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 		mpd.last_page = writeback_index - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 		mpd.first_page = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 	/* Update index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 		 * Set the writeback_index so that range_cyclic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 		 * mode will write it back later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 		mapping->writeback_index = mpd.first_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) out_writepages:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 	trace_ext4_writepages_result(inode, wbc, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 				     nr_to_write - wbc->nr_to_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 	percpu_up_read(&sbi->s_writepages_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) static int ext4_dax_writepages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 			       struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 	long nr_to_write = wbc->nr_to_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 	struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 	percpu_down_read(&sbi->s_writepages_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 	trace_ext4_writepages(inode, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 	ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 	trace_ext4_writepages_result(inode, wbc, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 				     nr_to_write - wbc->nr_to_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 	percpu_up_read(&sbi->s_writepages_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) static int ext4_nonda_switch(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 	s64 free_clusters, dirty_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 	struct ext4_sb_info *sbi = EXT4_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 	 * switch to non delalloc mode if we are running low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 	 * on free block. The free block accounting via percpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 	 * counters can get slightly wrong with percpu_counter_batch getting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 	 * accumulated on each CPU without updating global counters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 	 * Delalloc need an accurate free block accounting. So switch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 	 * to non delalloc when we are near to error range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 	free_clusters =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 		percpu_counter_read_positive(&sbi->s_freeclusters_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 	dirty_clusters =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 		percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 	 * Start pushing delalloc when 1/2 of free blocks are dirty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 	if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 		try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 	if (2 * free_clusters < 3 * dirty_clusters ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 	    free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 		 * free block count is less than 150% of dirty blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 		 * or free blocks is less than watermark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) /* We always reserve for an inode update; the superblock could be there too */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 	if (likely(ext4_has_feature_large_file(inode->i_sb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 	if (pos + len <= 0x7fffffffULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 	/* We might need to update the superblock to set LARGE_FILE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 	return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 			       loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 			       struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 	int ret, retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 	pgoff_t index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 	index = pos >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 	if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 	    ext4_verity_in_progress(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 		*fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 		return ext4_write_begin(file, mapping, pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 					len, flags, pagep, fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 	*fsdata = (void *)0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 	if (trace_android_fs_datawrite_start_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 		char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 		path = android_fstrace_get_pathname(pathbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 						    MAX_TRACE_PATHBUF_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 						    inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 		trace_android_fs_datawrite_start(inode, pos, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 						 current->pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 						 path, current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 	trace_ext4_da_write_begin(inode, pos, len, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 	if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 		ret = ext4_da_write_inline_data_begin(mapping, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 						      pos, len, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 						      pagep, fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 		if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 	 * grab_cache_page_write_begin() can take a long time if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 	 * system is thrashing due to memory pressure, or if the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 	 * is being written back.  So grab it first before we start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 	 * the transaction handle.  This also allows us to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 	 * the page (if needed) without using GFP_NOFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) retry_grab:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 	page = grab_cache_page_write_begin(mapping, index, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 	 * With delayed allocation, we don't log the i_disksize update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 	 * if there is delayed block allocation. But we still need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 	 * to journalling the i_disksize update if writes to the end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 	 * of file which has an already mapped buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) retry_journal:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 				ext4_da_write_credits(inode, pos, len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 		return PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 	if (page->mapping != mapping) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 		/* The page got truncated from under us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 		goto retry_grab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 	/* In case writeback began while the page was unlocked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 	wait_for_stable_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) #ifdef CONFIG_FS_ENCRYPTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 	ret = ext4_block_write_begin(page, pos, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 				     ext4_da_get_block_prep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 	ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 		 * block_write_begin may have instantiated a few blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) 		 * outside i_size.  Trim these off again. Don't need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 		 * i_size_read because we hold i_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 		if (pos + len > inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 			ext4_truncate_failed_write(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 		if (ret == -ENOSPC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 		    ext4_should_retry_alloc(inode->i_sb, &retries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 			goto retry_journal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 	*pagep = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102)  * Check if we should update i_disksize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103)  * when write to the end of file but not require block allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) static int ext4_da_should_update_i_disksize(struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 					    unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 	unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 	bh = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 	idx = offset >> inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 	for (i = 0; i < idx; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 		bh = bh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 	if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) static int ext4_da_write_end(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 			     struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 			     loff_t pos, unsigned len, unsigned copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 			     struct page *page, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 	int ret = 0, ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 	handle_t *handle = ext4_journal_current_handle();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 	loff_t new_i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 	unsigned long start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 	int write_mode = (int)(unsigned long)fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 	if (write_mode == FALL_BACK_TO_NONDELALLOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 		return ext4_write_end(file, mapping, pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 				      len, copied, page, fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 	trace_android_fs_datawrite_end(inode, pos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) 	trace_ext4_da_write_end(inode, pos, len, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 	start = pos & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) 	end = start + copied - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 	 * Since we are holding inode lock, we are sure i_disksize <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 	 * i_size. We also know that if i_disksize < i_size, there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 	 * delalloc writes pending in the range upto i_size. If the end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 	 * the current write is <= i_size, there's no need to touch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 	 * i_disksize since writeback will push i_disksize upto i_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 	 * eventually. If the end of the current write is > i_size and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 	 * inside an allocated block (ext4_da_should_update_i_disksize()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 	 * check), we need to update i_disksize here as neither
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 	 * ext4_writepage() nor certain ext4_writepages() paths not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 	 * allocating blocks update i_disksize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 	 * Note that we defer inode dirtying to generic_write_end() /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 	 * ext4_da_write_inline_data_end().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 	new_i_size = pos + copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 	if (copied && new_i_size > inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 		if (ext4_has_inline_data(inode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 		    ext4_da_should_update_i_disksize(page, end))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 			ext4_update_i_disksize(inode, new_i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 	if (write_mode != CONVERT_INLINE_DATA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 	    ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 	    ext4_has_inline_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 		ret = ext4_da_write_inline_data_end(inode, pos, len, copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 						     page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 		ret = generic_write_end(file, mapping, pos, len, copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 							page, fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 	copied = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 	ret2 = ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 	if (unlikely(ret2 && !ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 		ret = ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 	return ret ? ret : copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185)  * Force all delayed allocation blocks to be allocated for a given inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) int ext4_alloc_da_blocks(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 	trace_ext4_alloc_da_blocks(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 	if (!EXT4_I(inode)->i_reserved_data_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 	 * We do something simple for now.  The filemap_flush() will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 	 * also start triggering a write of the data blocks, which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 	 * not strictly speaking necessary (and for users of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 	 * laptop_mode, not even desirable).  However, to do otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	 * would require replicating code paths in:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	 * ext4_writepages() ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 	 *    write_cache_pages() ---> (via passed in callback function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 	 *        __mpage_da_writepage() -->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 	 *           mpage_add_bh_to_extent()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 	 *           mpage_da_map_blocks()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 	 * The problem is that write_cache_pages(), located in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 	 * mm/page-writeback.c, marks pages clean in preparation for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 	 * doing I/O, which is not desirable if we're not planning on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 	 * doing I/O at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 	 * We could call write_cache_pages(), and then redirty all of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 	 * the pages by calling redirty_page_for_writepage() but that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 	 * would be ugly in the extreme.  So instead we would need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 	 * replicate parts of the code in the above functions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 	 * simplifying them because we wouldn't actually intend to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 	 * write out the pages, but rather only collect contiguous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 	 * logical block extents, call the multi-block allocator, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 	 * then update the buffer heads with the block allocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 	 * For now, though, we'll cheat by calling filemap_flush(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 	 * which will map the blocks, and start the I/O, but not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 	 * actually wait for the I/O to complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 	return filemap_flush(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229)  * bmap() is special.  It gets used by applications such as lilo and by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230)  * the swapper to find the on-disk block of a specific piece of data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232)  * Naturally, this is dangerous if the block concerned is still in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233)  * journal.  If somebody makes a swapfile on an ext4 data-journaling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234)  * filesystem and enables swap, then they may get a nasty shock when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235)  * data getting swapped to that swapfile suddenly gets overwritten by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236)  * the original zero's written out previously to the journal and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237)  * awaiting writeback in the kernel's buffer cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239)  * So, if we see any bmap calls here on a modified, data-journaled file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240)  * take extra steps to flush any blocks which might be in the cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) 	journal_t *journal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 	 * We can get here for an inline file via the FIBMAP ioctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) 	if (ext4_has_inline_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) 			test_opt(inode->i_sb, DELALLOC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) 		 * With delalloc we want to sync the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 		 * so that we can make sure we allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 		 * blocks for file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 		filemap_write_and_wait(mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 	if (EXT4_JOURNAL(inode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 	    ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 		 * This is a REALLY heavyweight approach, but the use of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 		 * bmap on dirty files is expected to be extremely rare:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 		 * only if we run lilo or swapon on a freshly made file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 		 * do we expect this to happen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 		 * (bmap requires CAP_SYS_RAWIO so this does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 		 * represent an unprivileged user DOS attack --- we'd be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 		 * in trouble if mortal users could trigger this path at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 		 * will.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 		 * NB. EXT4_STATE_JDATA is not set on files other than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 		 * regular files.  If somebody wants to bmap a directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 		 * or symlink and gets confused because the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 		 * hasn't yet been flushed to disk, they deserve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 		 * everything they get.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 		ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 		journal = EXT4_JOURNAL(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 		jbd2_journal_lock_updates(journal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 		err = jbd2_journal_flush(journal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 		jbd2_journal_unlock_updates(journal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 	return iomap_bmap(mapping, block, &ext4_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) static int ext4_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 	int ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 	struct inode *inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 	trace_ext4_readpage(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 	if (ext4_has_inline_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 		ret = ext4_readpage_inline(inode, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 	if (ret == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 		return ext4_mpage_readpages(inode, NULL, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) static void ext4_readahead(struct readahead_control *rac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 	struct inode *inode = rac->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 	/* If the file has inline data, no need to do readahead. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 	if (ext4_has_inline_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 	ext4_mpage_readpages(inode, rac, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) static void ext4_invalidatepage(struct page *page, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 				unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 	trace_ext4_invalidatepage(page, offset, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 	/* No journalling happens on data buffers when this function is used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 	WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 	block_invalidatepage(page, offset, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) static int __ext4_journalled_invalidatepage(struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) 					    unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 					    unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) 	journal_t *journal = EXT4_JOURNAL(page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) 	trace_ext4_journalled_invalidatepage(page, offset, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 	 * If it's a full truncate we just forget about the pending dirtying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 	if (offset == 0 && length == PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 		ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 	return jbd2_journal_invalidatepage(journal, page, offset, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) /* Wrapper for aops... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) static void ext4_journalled_invalidatepage(struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) 					   unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 					   unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) 	WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) static int ext4_releasepage(struct page *page, gfp_t wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 	journal_t *journal = EXT4_JOURNAL(page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 	trace_ext4_releasepage(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 	/* Page has dirty journalled data -> cannot release */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 	if (PageChecked(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 	if (journal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 		return jbd2_journal_try_to_free_buffers(journal, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) 		return try_to_free_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) static bool ext4_inode_datasync_dirty(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) 	if (journal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 		if (jbd2_transaction_committed(journal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 			EXT4_I(inode)->i_datasync_tid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 		if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) 			return !list_empty(&EXT4_I(inode)->i_fc_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 	/* Any metadata buffers to write? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 	if (!list_empty(&inode->i_mapping->private_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 	return inode->i_state & I_DIRTY_DATASYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 			   struct ext4_map_blocks *map, loff_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 			   loff_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) 	u8 blkbits = inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 	 * Writes that span EOF might trigger an I/O size update on completion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 	 * so consider them to be dirty for the purpose of O_DSYNC, even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 	 * there is no other metadata changes being made or are pending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 	iomap->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 	if (ext4_inode_datasync_dirty(inode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 	    offset + length > i_size_read(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 		iomap->flags |= IOMAP_F_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 	if (map->m_flags & EXT4_MAP_NEW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 		iomap->flags |= IOMAP_F_NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 	iomap->bdev = inode->i_sb->s_bdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 	iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 	iomap->offset = (u64) map->m_lblk << blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) 	iomap->length = (u64) map->m_len << blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 	if ((map->m_flags & EXT4_MAP_MAPPED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) 	    !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) 		iomap->flags |= IOMAP_F_MERGED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 	 * Flags passed to ext4_map_blocks() for direct I/O writes can result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 	 * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 	 * set. In order for any allocated unwritten extents to be converted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 	 * into written extents correctly within the ->end_io() handler, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 	 * need to ensure that the iomap->type is set appropriately. Hence, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 	 * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) 	 * been set first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) 	if (map->m_flags & EXT4_MAP_UNWRITTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 		iomap->type = IOMAP_UNWRITTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) 		iomap->addr = (u64) map->m_pblk << blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434) 	} else if (map->m_flags & EXT4_MAP_MAPPED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) 		iomap->type = IOMAP_MAPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) 		iomap->addr = (u64) map->m_pblk << blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) 		iomap->type = IOMAP_HOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) 		iomap->addr = IOMAP_NULL_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) 			    unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 	u8 blkbits = inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 	int ret, dio_credits, m_flags = 0, retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 	 * Trim the mapping request to the maximum value that we can map at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 	 * once for direct I/O.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 	if (map->m_len > DIO_MAX_BLOCKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 		map->m_len = DIO_MAX_BLOCKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 	dio_credits = ext4_chunk_trans_blocks(inode, map->m_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 	 * Either we allocate blocks and then don't get an unwritten extent, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 	 * in that case we have reserved enough credits. Or, the blocks are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 	 * already allocated and unwritten. In that case, the extent conversion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 	 * fits into the credits as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) 	handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 	if (IS_ERR(handle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) 		return PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) 	 * DAX and direct I/O are the only two operations that are currently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) 	 * supported with IOMAP_WRITE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 	WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 	if (IS_DAX(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 		m_flags = EXT4_GET_BLOCKS_CREATE_ZERO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 	 * We use i_size instead of i_disksize here because delalloc writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 	 * can complete at any point during the I/O and subsequently push the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) 	 * i_disksize out to i_size. This could be beyond where direct I/O is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 	 * happening and thus expose allocated blocks to direct I/O reads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) 	else if (((loff_t)map->m_lblk << blkbits) >= i_size_read(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 		m_flags = EXT4_GET_BLOCKS_CREATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 	else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) 		m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 	ret = ext4_map_blocks(handle, inode, map, m_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 	 * We cannot fill holes in indirect tree based inodes as that could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 	 * expose stale data in the case of a crash. Use the magic error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 	 * to fallback to buffered I/O.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 	if (!m_flags && !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 		ret = -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 		unsigned flags, struct iomap *iomap, struct iomap *srcmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 	struct ext4_map_blocks map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 	u8 blkbits = inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 	if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 	if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 	 * Calculate the first and last logical blocks respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 	map.m_lblk = offset >> blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) 	map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) 			  EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) 	if (flags & IOMAP_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 		 * We check here if the blocks are already allocated, then we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 		 * don't need to start a journal txn and we can directly return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 		 * the mapping information. This could boost performance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 		 * especially in multi-threaded overwrite requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) 		if (offset + length <= i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) 			ret = ext4_map_blocks(NULL, inode, &map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) 			if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) 		ret = ext4_iomap_alloc(inode, &map, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 		ret = ext4_map_blocks(NULL, inode, &map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) 	 * When inline encryption is enabled, sometimes I/O to an encrypted file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) 	 * has to be broken up to guarantee DUN contiguity. Handle this by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 	 * limiting the length of the mapping returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) 	map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) 	ext4_set_iomap(inode, iomap, &map, offset, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 		loff_t length, unsigned flags, struct iomap *iomap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 		struct iomap *srcmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 	 * Even for writes we don't need to allocate blocks, so just pretend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 	 * we are reading to save overhead of starting a transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 	flags &= ~IOMAP_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 	ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 	WARN_ON_ONCE(iomap->type != IOMAP_MAPPED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) 			  ssize_t written, unsigned flags, struct iomap *iomap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 	 * Check to see whether an error occurred while writing out the data to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) 	 * the allocated blocks. If so, return the magic error code so that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 	 * fallback to buffered I/O and attempt to complete the remainder of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 	 * the I/O. Any blocks that may have been allocated in preparation for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 	 * the direct I/O will be reused during buffered I/O.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) 	if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 		return -ENOTBLK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) const struct iomap_ops ext4_iomap_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 	.iomap_begin		= ext4_iomap_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 	.iomap_end		= ext4_iomap_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) const struct iomap_ops ext4_iomap_overwrite_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 	.iomap_begin		= ext4_iomap_overwrite_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 	.iomap_end		= ext4_iomap_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) static bool ext4_iomap_is_delalloc(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 				   struct ext4_map_blocks *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 	struct extent_status es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 	ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) 	ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 				  map->m_lblk, end, &es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 	if (!es.es_len || es.es_lblk > end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) 	if (es.es_lblk > map->m_lblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) 		map->m_len = es.es_lblk - map->m_lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) 	offset = map->m_lblk - es.es_lblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 	map->m_len = es.es_len - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) static int ext4_iomap_begin_report(struct inode *inode, loff_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 				   loff_t length, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) 				   struct iomap *iomap, struct iomap *srcmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) 	bool delalloc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 	struct ext4_map_blocks map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 	u8 blkbits = inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 	if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 	if (ext4_has_inline_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 		ret = ext4_inline_data_iomap(inode, iomap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 		if (ret != -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 			if (ret == 0 && offset >= iomap->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 				ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 	 * Calculate the first and last logical block respectively.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 	map.m_lblk = offset >> blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 	map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 			  EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 	 * Fiemap callers may call for offset beyond s_bitmap_maxbytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 	 * So handle it here itself instead of querying ext4_map_blocks().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 	 * Since ext4_map_blocks() will warn about it and will return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 	 * -EIO error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 		if (offset >= sbi->s_bitmap_maxbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 			map.m_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 			goto set_iomap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 	ret = ext4_map_blocks(NULL, inode, &map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 	if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 		delalloc = ext4_iomap_is_delalloc(inode, &map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) set_iomap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 	ext4_set_iomap(inode, iomap, &map, offset, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 	if (delalloc && iomap->type == IOMAP_HOLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 		iomap->type = IOMAP_DELALLOC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) const struct iomap_ops ext4_iomap_report_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 	.iomap_begin = ext4_iomap_begin_report,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685)  * Pages can be marked dirty completely asynchronously from ext4's journalling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686)  * activity.  By filemap_sync_pte(), try_to_unmap_one(), etc.  We cannot do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687)  * much here because ->set_page_dirty is called under VFS locks.  The page is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688)  * not necessarily locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690)  * We cannot just dirty the page and leave attached buffers clean, because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691)  * buffers' dirty state is "definitive".  We cannot just set the buffers dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692)  * or jbddirty because all the journalling code will explode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694)  * So what we do is to mark the page "pending dirty" and next time writepage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695)  * is called, propagate that into the buffers appropriately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) static int ext4_journalled_set_page_dirty(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 	SetPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) 	return __set_page_dirty_nobuffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) static int ext4_set_page_dirty(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 	WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) 	WARN_ON_ONCE(!page_has_buffers(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 	return __set_page_dirty_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) static int ext4_iomap_swap_activate(struct swap_info_struct *sis,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 				    struct file *file, sector_t *span)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 	return iomap_swapfile_activate(sis, file, span,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 				       &ext4_iomap_report_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) static const struct address_space_operations ext4_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 	.readpage		= ext4_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 	.readahead		= ext4_readahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 	.writepage		= ext4_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 	.writepages		= ext4_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) 	.write_begin		= ext4_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 	.write_end		= ext4_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 	.set_page_dirty		= ext4_set_page_dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 	.bmap			= ext4_bmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) 	.invalidatepage		= ext4_invalidatepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 	.releasepage		= ext4_releasepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 	.direct_IO		= noop_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 	.migratepage		= buffer_migrate_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) 	.is_partially_uptodate  = block_is_partially_uptodate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) 	.error_remove_page	= generic_error_remove_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) 	.swap_activate		= ext4_iomap_swap_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) static const struct address_space_operations ext4_journalled_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 	.readpage		= ext4_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) 	.readahead		= ext4_readahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) 	.writepage		= ext4_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 	.writepages		= ext4_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) 	.write_begin		= ext4_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) 	.write_end		= ext4_journalled_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 	.set_page_dirty		= ext4_journalled_set_page_dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) 	.bmap			= ext4_bmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 	.invalidatepage		= ext4_journalled_invalidatepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) 	.releasepage		= ext4_releasepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 	.direct_IO		= noop_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 	.is_partially_uptodate  = block_is_partially_uptodate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 	.error_remove_page	= generic_error_remove_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 	.swap_activate		= ext4_iomap_swap_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) static const struct address_space_operations ext4_da_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) 	.readpage		= ext4_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 	.readahead		= ext4_readahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) 	.writepage		= ext4_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) 	.writepages		= ext4_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) 	.write_begin		= ext4_da_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) 	.write_end		= ext4_da_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) 	.set_page_dirty		= ext4_set_page_dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 	.bmap			= ext4_bmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) 	.invalidatepage		= ext4_invalidatepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) 	.releasepage		= ext4_releasepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) 	.direct_IO		= noop_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) 	.migratepage		= buffer_migrate_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) 	.is_partially_uptodate  = block_is_partially_uptodate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 	.error_remove_page	= generic_error_remove_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) 	.swap_activate		= ext4_iomap_swap_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) static const struct address_space_operations ext4_dax_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) 	.writepages		= ext4_dax_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 	.direct_IO		= noop_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 	.set_page_dirty		= noop_set_page_dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 	.bmap			= ext4_bmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 	.invalidatepage		= noop_invalidatepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 	.swap_activate		= ext4_iomap_swap_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) void ext4_set_aops(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) 	switch (ext4_inode_journal_mode(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) 	case EXT4_INODE_ORDERED_DATA_MODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) 	case EXT4_INODE_WRITEBACK_DATA_MODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) 	case EXT4_INODE_JOURNAL_DATA_MODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) 		inode->i_mapping->a_ops = &ext4_journalled_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 	if (IS_DAX(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 		inode->i_mapping->a_ops = &ext4_dax_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) 	else if (test_opt(inode->i_sb, DELALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 		inode->i_mapping->a_ops = &ext4_da_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 		inode->i_mapping->a_ops = &ext4_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) static int __ext4_block_zero_page_range(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 		struct address_space *mapping, loff_t from, loff_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) 	ext4_fsblk_t index = from >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) 	unsigned offset = from & (PAGE_SIZE-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) 	unsigned blocksize, pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) 	ext4_lblk_t iblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 	struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 	page = find_or_create_page(mapping, from >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 				   mapping_gfp_constraint(mapping, ~__GFP_FS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) 	blocksize = inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) 	iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 	if (!page_has_buffers(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) 		create_empty_buffers(page, blocksize, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) 	/* Find the buffer that contains "offset" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) 	bh = page_buffers(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) 	pos = blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) 	while (offset >= pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) 		bh = bh->b_this_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) 		iblock++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) 		pos += blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 	if (buffer_freed(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) 		BUFFER_TRACE(bh, "freed: skip");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) 	if (!buffer_mapped(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) 		BUFFER_TRACE(bh, "unmapped");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) 		ext4_get_block(inode, iblock, bh, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) 		/* unmapped? It's a hole - nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) 		if (!buffer_mapped(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) 			BUFFER_TRACE(bh, "still unmapped");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) 	/* Ok, it's mapped. Make sure it's up-to-date */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) 	if (PageUptodate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) 		set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) 	if (!buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) 		err = ext4_read_bh_lock(bh, 0, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) 		if (fscrypt_inode_uses_fs_layer_crypto(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) 			/* We expect the key to be set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) 			BUG_ON(!fscrypt_has_encryption_key(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) 			err = fscrypt_decrypt_pagecache_blocks(page, blocksize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) 							       bh_offset(bh));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) 				clear_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) 				goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) 	if (ext4_should_journal_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) 		BUFFER_TRACE(bh, "get write access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) 		err = ext4_journal_get_write_access(handle, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) 	zero_user(page, offset, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) 	BUFFER_TRACE(bh, "zeroed end of block");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) 	if (ext4_should_journal_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) 		err = ext4_handle_dirty_metadata(handle, inode, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) 		mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) 		if (ext4_should_order_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) 			err = ext4_jbd2_inode_add_write(handle, inode, from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) 					length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890)  * ext4_block_zero_page_range() zeros out a mapping of length 'length'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891)  * starting from file offset 'from'.  The range to be zero'd must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892)  * be contained with in one block.  If the specified range exceeds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893)  * the end of the block it will be shortened to end of the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894)  * that cooresponds to 'from'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) static int ext4_block_zero_page_range(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) 		struct address_space *mapping, loff_t from, loff_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) 	unsigned offset = from & (PAGE_SIZE-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) 	unsigned blocksize = inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) 	unsigned max = blocksize - (offset & (blocksize - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) 	 * correct length if it does not fall between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) 	 * 'from' and the end of the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) 	if (length > max || length < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) 		length = max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) 	if (IS_DAX(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) 		return iomap_zero_range(inode, from, length, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) 					&ext4_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) 	return __ext4_block_zero_page_range(handle, mapping, from, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919)  * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920)  * up to the end of the block which corresponds to `from'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921)  * This required during truncate. We need to physically zero the tail end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922)  * of that block so it doesn't yield old data if the file is later grown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) static int ext4_block_truncate_page(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) 		struct address_space *mapping, loff_t from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) 	unsigned offset = from & (PAGE_SIZE-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) 	unsigned length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) 	unsigned blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) 	/* If we are processing an encrypted inode during orphan list handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) 	if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) 	blocksize = inode->i_sb->s_blocksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) 	length = blocksize - (offset & (blocksize - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) 	return ext4_block_zero_page_range(handle, mapping, from, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) 			     loff_t lstart, loff_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) 	struct address_space *mapping = inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) 	unsigned partial_start, partial_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) 	ext4_fsblk_t start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) 	loff_t byte_end = (lstart + length - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) 	partial_start = lstart & (sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) 	partial_end = byte_end & (sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) 	start = lstart >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) 	end = byte_end >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) 	/* Handle partial zero within the single block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) 	if (start == end &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) 	    (partial_start || (partial_end != sb->s_blocksize - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) 		err = ext4_block_zero_page_range(handle, mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) 						 lstart, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) 	/* Handle partial zero out on the start of the range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) 	if (partial_start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) 		err = ext4_block_zero_page_range(handle, mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) 						 lstart, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) 	/* Handle partial zero out on the end of the range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) 	if (partial_end != sb->s_blocksize - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) 		err = ext4_block_zero_page_range(handle, mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) 						 byte_end - partial_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) 						 partial_end + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) int ext4_can_truncate(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) 	if (S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) 	if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) 	if (S_ISLNK(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) 		return !ext4_inode_is_fast_symlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992)  * We have to make sure i_disksize gets properly updated before we truncate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993)  * page cache due to hole punching or zero range. Otherwise i_disksize update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994)  * can get lost as it may have been postponed to submission of writeback but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995)  * that will never happen after we truncate page cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) 				      loff_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) 	loff_t size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) 	WARN_ON(!inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) 	if (offset > size || offset + len < size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) 	if (EXT4_I(inode)->i_disksize >= size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) 	handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) 	if (IS_ERR(handle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) 		return PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) 	ext4_update_i_disksize(inode, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) 	ret = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) static void ext4_wait_dax_page(struct ext4_inode_info *ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) 	up_write(&ei->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) 	schedule();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) 	down_write(&ei->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) int ext4_break_layouts(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) 	if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) 		page = dax_layout_busy_page(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) 		error = ___wait_var_event(&page->_refcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) 				atomic_read(&page->_refcount) == 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) 				TASK_INTERRUPTIBLE, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) 				ext4_wait_dax_page(ei));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) 	} while (error == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053)  * ext4_punch_hole: punches a hole in a file by releasing the blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054)  * associated with the given offset and length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056)  * @inode:  File inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057)  * @offset: The offset where the hole will begin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058)  * @len:    The length of the hole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060)  * Returns: 0 on success or negative on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) 	ext4_lblk_t first_block, stop_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) 	struct address_space *mapping = inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) 	loff_t first_block_offset, last_block_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) 	unsigned int credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) 	int ret = 0, ret2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) 	trace_ext4_punch_hole(inode, offset, length, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) 	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) 	if (ext4_has_inline_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) 		down_write(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) 		ret = ext4_convert_inline_data(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) 		up_write(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) 	 * Write out all dirty pages to avoid race conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) 	 * Then release them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) 	if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) 		ret = filemap_write_and_wait_range(mapping, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) 						   offset + length - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) 	/* No need to punch hole beyond i_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) 	if (offset >= inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) 		goto out_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) 	 * If the hole extends beyond i_size, set the hole
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) 	 * to end after the page that contains i_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) 	if (offset + length > inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) 		length = inode->i_size +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) 		   PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) 		   offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) 	if (offset & (sb->s_blocksize - 1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) 	    (offset + length) & (sb->s_blocksize - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) 		 * Attach jinode to inode for jbd2 if we do any zeroing of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) 		 * partial block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) 		ret = ext4_inode_attach_jinode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) 			goto out_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) 	/* Wait all existing dio workers, newcomers will block on i_mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) 	inode_dio_wait(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) 	 * Prevent page faults from reinstantiating pages we have released from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) 	 * page cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) 	down_write(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) 	ret = ext4_break_layouts(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) 		goto out_dio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) 	first_block_offset = round_up(offset, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) 	last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) 	/* Now release the pages and zero block aligned part of pages*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) 	if (last_block_offset > first_block_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) 		ret = ext4_update_disksize_before_punch(inode, offset, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) 			goto out_dio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) 		truncate_pagecache_range(inode, first_block_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) 					 last_block_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) 		credits = ext4_writepage_trans_blocks(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) 		credits = ext4_blocks_for_truncate(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) 	handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) 		ret = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) 		ext4_std_error(sb, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) 		goto out_dio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) 	ret = ext4_zero_partial_blocks(handle, inode, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) 				       length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) 		goto out_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) 	first_block = (offset + sb->s_blocksize - 1) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) 		EXT4_BLOCK_SIZE_BITS(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) 	stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) 	/* If there are blocks to remove, do it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) 	if (stop_block > first_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) 		down_write(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) 		ext4_discard_preallocations(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) 		ret = ext4_es_remove_extent(inode, first_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) 					    stop_block - first_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) 			up_write(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) 			goto out_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) 		if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) 			ret = ext4_ext_remove_space(inode, first_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) 						    stop_block - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) 			ret = ext4_ind_remove_space(handle, inode, first_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) 						    stop_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) 		up_write(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) 	ext4_fc_track_range(handle, inode, first_block, stop_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) 	if (IS_SYNC(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) 		ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) 	inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) 	ret2 = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) 	if (unlikely(ret2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) 		ret = ret2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) 	if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) 		ext4_update_inode_fsync_trans(handle, inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) out_stop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) out_dio:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) 	up_write(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) out_mutex:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) int ext4_inode_attach_jinode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) 	struct jbd2_inode *jinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) 	if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) 	jinode = jbd2_alloc_inode(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) 	spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) 	if (!ei->jinode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) 		if (!jinode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) 			spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) 		ei->jinode = jinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) 		jbd2_journal_init_jbd_inode(ei->jinode, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) 		jinode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) 	spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) 	if (unlikely(jinode != NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) 		jbd2_free_inode(jinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235)  * ext4_truncate()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237)  * We block out ext4_get_block() block instantiations across the entire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238)  * transaction, and VFS/VM ensures that ext4_truncate() cannot run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239)  * simultaneously on behalf of the same inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241)  * As we work through the truncate and commit bits of it to the journal there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242)  * is one core, guiding principle: the file's tree must always be consistent on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243)  * disk.  We must be able to restart the truncate after a crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245)  * The file's tree may be transiently inconsistent in memory (although it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246)  * probably isn't), but whenever we close off and commit a journal transaction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247)  * the contents of (the filesystem + the journal) must be consistent and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248)  * restartable.  It's pretty simple, really: bottom up, right to left (although
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249)  * left-to-right works OK too).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251)  * Note that at recovery time, journal replay occurs *before* the restart of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252)  * truncate against the orphan inode list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254)  * The committed inode has the new, desired i_size (which is the same as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255)  * i_disksize in this case).  After a crash, ext4_orphan_cleanup() will see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256)  * that this inode's truncate did not complete and it will again call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257)  * ext4_truncate() to have another go.  So there will be instantiated blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258)  * to the right of the truncation point in a crashed ext4 filesystem.  But
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259)  * that's fine - as long as they are linked from the inode, the post-crash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260)  * ext4_truncate() run will find them and release them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) int ext4_truncate(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) 	unsigned int credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) 	int err = 0, err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) 	struct address_space *mapping = inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) 	 * There is a possibility that we're either freeing the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) 	 * or it's a completely new inode. In those cases we might not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) 	 * have i_mutex locked because it's not necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) 	if (!(inode->i_state & (I_NEW|I_FREEING)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) 		WARN_ON(!inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) 	trace_ext4_truncate_enter(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) 	if (!ext4_can_truncate(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) 		goto out_trace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) 	if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) 		ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) 	if (ext4_has_inline_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) 		int has_inline = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) 		err = ext4_inline_data_truncate(inode, &has_inline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) 		if (err || has_inline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) 			goto out_trace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) 	/* If we zero-out tail of the page, we have to create jinode for jbd2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) 	if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) 		if (ext4_inode_attach_jinode(inode) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) 			goto out_trace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) 		credits = ext4_writepage_trans_blocks(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) 		credits = ext4_blocks_for_truncate(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) 	handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) 		err = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) 		goto out_trace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) 	if (inode->i_size & (inode->i_sb->s_blocksize - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) 		ext4_block_truncate_page(handle, mapping, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) 	 * We add the inode to the orphan list, so that if this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) 	 * truncate spans multiple transactions, and we crash, we will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) 	 * resume the truncate when the filesystem recovers.  It also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) 	 * marks the inode dirty, to catch the new size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) 	 * Implication: the file must always be in a sane, consistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) 	 * truncatable state while each transaction commits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) 	err = ext4_orphan_add(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) 		goto out_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) 	down_write(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) 	ext4_discard_preallocations(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) 		err = ext4_ext_truncate(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) 		ext4_ind_truncate(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) 	up_write(&ei->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) 		goto out_stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) 	if (IS_SYNC(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) 		ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) out_stop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) 	 * If this was a simple ftruncate() and the file will remain alive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) 	 * then we need to clear up the orphan record which we created above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) 	 * However, if this was a real unlink then we were called by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) 	 * ext4_evict_inode(), and we allow that function to clean up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) 	 * orphan info for us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) 	if (inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) 		ext4_orphan_del(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) 	inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) 	err2 = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) 	if (unlikely(err2 && !err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) 		err = err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) out_trace:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) 	trace_ext4_truncate_exit(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365)  * ext4_get_inode_loc returns with an extra refcount against the inode's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366)  * underlying buffer_head on success. If 'in_mem' is true, we have all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367)  * data in memory that is needed to recreate the on-disk version of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368)  * inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) 				struct ext4_iloc *iloc, int in_mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) 				ext4_fsblk_t *ret_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) 	struct ext4_group_desc	*gdp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) 	struct buffer_head	*bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) 	ext4_fsblk_t		block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) 	struct blk_plug		plug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) 	int			inodes_per_block, inode_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) 	iloc->bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) 	if (ino < EXT4_ROOT_INO ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) 	    ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) 	iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) 	gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) 	if (!gdp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) 	 * Figure out the offset within the block group inode table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) 	inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) 	inode_offset = ((ino - 1) %
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) 			EXT4_INODES_PER_GROUP(sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) 	block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) 	iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) 	bh = sb_getblk(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) 	if (unlikely(!bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) 	if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) 		goto simulate_eio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) 	if (!buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) 		lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) 		if (ext4_buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) 			/* someone brought it uptodate while we waited */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) 			unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) 			goto has_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) 		 * If we have all information of the inode in memory and this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) 		 * is the only valid inode in the block, we need not read the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) 		 * block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) 		if (in_mem) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) 			struct buffer_head *bitmap_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) 			int i, start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) 			start = inode_offset & ~(inodes_per_block - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) 			/* Is the inode bitmap in cache? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) 			bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) 			if (unlikely(!bitmap_bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) 				goto make_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) 			 * If the inode bitmap isn't in cache then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) 			 * optimisation may end up performing two reads instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) 			 * of one, so skip it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) 			if (!buffer_uptodate(bitmap_bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) 				brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) 				goto make_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) 			for (i = start; i < start + inodes_per_block; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) 				if (i == inode_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) 				if (ext4_test_bit(i, bitmap_bh->b_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) 			brelse(bitmap_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) 			if (i == start + inodes_per_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) 				/* all other inodes are free, so skip I/O */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) 				memset(bh->b_data, 0, bh->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) 				set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) 				unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) 				goto has_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) make_io:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) 		 * If we need to do any I/O, try to pre-readahead extra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) 		 * blocks from the inode table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) 		blk_start_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) 		if (EXT4_SB(sb)->s_inode_readahead_blks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) 			ext4_fsblk_t b, end, table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) 			unsigned num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) 			__u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) 			table = ext4_inode_table(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) 			/* s_inode_readahead_blks is always a power of 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) 			b = block & ~((ext4_fsblk_t) ra_blks - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) 			if (table > b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) 				b = table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) 			end = b + ra_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) 			num = EXT4_INODES_PER_GROUP(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) 			if (ext4_has_group_desc_csum(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) 				num -= ext4_itable_unused_count(sb, gdp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) 			table += num / inodes_per_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) 			if (end > table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) 				end = table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) 			while (b <= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) 				ext4_sb_breadahead_unmovable(sb, b++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) 		 * There are other valid inodes in the buffer, this inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) 		 * has in-inode xattrs, or we don't have this inode in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) 		 * Read the block from disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) 		trace_ext4_load_inode(sb, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) 		ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488) 		blk_finish_plug(&plug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) 		wait_on_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) 		if (!buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) 		simulate_eio:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) 			if (ret_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) 				*ret_block = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) 			brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) has_buffer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) 	iloc->bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) static int __ext4_get_inode_loc_noinmem(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) 					struct ext4_iloc *iloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) 	ext4_fsblk_t err_blk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) 	ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) 					&err_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) 	if (ret == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513) 		ext4_error_inode_block(inode, err_blk, EIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) 					"unable to read itable block");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) 	ext4_fsblk_t err_blk = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) 	/* We have all inode data except xattrs in memory here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525) 	ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526) 		!ext4_test_inode_state(inode, EXT4_STATE_XATTR), &err_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) 	if (ret == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) 		ext4_error_inode_block(inode, err_blk, EIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) 					"unable to read itable block");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) 			  struct ext4_iloc *iloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) 	return __ext4_get_inode_loc(sb, ino, iloc, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) static bool ext4_should_enable_dax(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) 	if (test_opt2(inode->i_sb, DAX_NEVER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) 	if (!S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) 	if (ext4_should_journal_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552) 	if (ext4_has_inline_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) 	if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) 	if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) 	if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) 	if (test_opt(inode->i_sb, DAX_ALWAYS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) 	return ext4_test_inode_flag(inode, EXT4_INODE_DAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) void ext4_set_inode_flags(struct inode *inode, bool init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568) 	unsigned int flags = EXT4_I(inode)->i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) 	unsigned int new_fl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) 	WARN_ON_ONCE(IS_DAX(inode) && init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) 	if (flags & EXT4_SYNC_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) 		new_fl |= S_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) 	if (flags & EXT4_APPEND_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576) 		new_fl |= S_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) 	if (flags & EXT4_IMMUTABLE_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) 		new_fl |= S_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) 	if (flags & EXT4_NOATIME_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) 		new_fl |= S_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) 	if (flags & EXT4_DIRSYNC_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) 		new_fl |= S_DIRSYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) 	/* Because of the way inode_set_flags() works we must preserve S_DAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) 	 * here if already set. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) 	new_fl |= (inode->i_flags & S_DAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) 	if (init && ext4_should_enable_dax(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) 		new_fl |= S_DAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) 	if (flags & EXT4_ENCRYPT_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) 		new_fl |= S_ENCRYPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) 	if (flags & EXT4_CASEFOLD_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) 		new_fl |= S_CASEFOLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) 	if (flags & EXT4_VERITY_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) 		new_fl |= S_VERITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) 	inode_set_flags(inode, new_fl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) 			S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598) 			S_ENCRYPTED|S_CASEFOLD|S_VERITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) 				  struct ext4_inode_info *ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) 	blkcnt_t i_blocks ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) 	struct inode *inode = &(ei->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) 	if (ext4_has_feature_huge_file(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) 		/* we are using combined 48 bit field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) 		i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) 					le32_to_cpu(raw_inode->i_blocks_lo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612) 		if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613) 			/* i_blocks represent file system block size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614) 			return i_blocks  << (inode->i_blkbits - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616) 			return i_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) 		return le32_to_cpu(raw_inode->i_blocks_lo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623) static inline int ext4_iget_extra_inode(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) 					 struct ext4_inode *raw_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) 					 struct ext4_inode_info *ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627) 	__le32 *magic = (void *)raw_inode +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) 			EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) 	if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631) 	    EXT4_INODE_SIZE(inode->i_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) 	    *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) 		ext4_set_inode_state(inode, EXT4_STATE_XATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) 		return ext4_find_inline_data_nolock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) 		EXT4_I(inode)->i_inline_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) int ext4_get_projid(struct inode *inode, kprojid_t *projid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) 	if (!ext4_has_feature_project(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) 	*projid = EXT4_I(inode)->i_projid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649)  * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650)  * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651)  * set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655) 	if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) 		inode_set_iversion_raw(inode, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) 		inode_set_iversion_queried(inode, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) static inline u64 ext4_inode_peek_iversion(const struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) 	if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) 		return inode_peek_iversion_raw(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) 		return inode_peek_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) 			  ext4_iget_flags flags, const char *function,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) 			  unsigned int line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) 	struct ext4_iloc iloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) 	struct ext4_inode *raw_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) 	struct ext4_inode_info *ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) 	journal_t *journal = EXT4_SB(sb)->s_journal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) 	long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) 	loff_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) 	int block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) 	uid_t i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) 	gid_t i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) 	projid_t i_projid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) 	if ((!(flags & EXT4_IGET_SPECIAL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) 	     (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) 	    (ino < EXT4_ROOT_INO) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) 	    (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) 		if (flags & EXT4_IGET_HANDLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) 			return ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) 		__ext4_error(sb, function, line, EFSCORRUPTED, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) 			     "inode #%lu: comm %s: iget: illegal inode #",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) 			     ino, current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) 		return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) 	inode = iget_locked(sb, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) 	if (!(inode->i_state & I_NEW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) 		return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702) 	ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703) 	iloc.bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) 	ret = __ext4_get_inode_loc_noinmem(inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707) 		goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708) 	raw_inode = ext4_raw_inode(&iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710) 	if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711) 		ext4_error_inode(inode, function, line, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712) 				 "iget: root inode unallocated");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) 		ret = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714) 		goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717) 	if ((flags & EXT4_IGET_HANDLE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) 	    (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) 		ret = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) 		goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) 		ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) 		if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726) 			EXT4_INODE_SIZE(inode->i_sb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) 		    (ei->i_extra_isize & 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) 			ext4_error_inode(inode, function, line, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) 					 "iget: bad extra_isize %u "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) 					 "(inode size %u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) 					 ei->i_extra_isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732) 					 EXT4_INODE_SIZE(inode->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) 			ret = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) 			goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737) 		ei->i_extra_isize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) 	/* Precompute checksum seed for inode metadata */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740) 	if (ext4_has_metadata_csum(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) 		__u32 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) 		__le32 inum = cpu_to_le32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) 		__le32 gen = raw_inode->i_generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) 		csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) 				   sizeof(inum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) 		ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) 					      sizeof(gen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) 	if ((!ext4_inode_csum_verify(inode, raw_inode, ei) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752) 	    ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) 	     (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) 		ext4_error_inode_err(inode, function, line, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) 				EFSBADCRC, "iget: checksum invalid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756) 		ret = -EFSBADCRC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) 		goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) 	inode->i_mode = le16_to_cpu(raw_inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) 	i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762) 	i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763) 	if (ext4_has_feature_project(sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) 	    EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765) 	    EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766) 		i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) 		i_projid = EXT4_DEF_PROJID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770) 	if (!(test_opt(inode->i_sb, NO_UID32))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771) 		i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) 		i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774) 	i_uid_write(inode, i_uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) 	i_gid_write(inode, i_gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776) 	ei->i_projid = make_kprojid(&init_user_ns, i_projid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) 	set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779) 	ext4_clear_state_flags(ei);	/* Only relevant on 32-bit archs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) 	ei->i_inline_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) 	ei->i_dir_start_lookup = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) 	ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) 	/* We now have enough fields to check if the inode was active or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) 	 * This is needed because nfsd might try to access dead inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785) 	 * the test is that same one that e2fsck uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) 	 * NeilBrown 1999oct15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) 	if (inode->i_nlink == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) 		if ((inode->i_mode == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) 		     !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) 		    ino != EXT4_BOOT_LOADER_INO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) 			/* this inode is deleted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) 			ret = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) 			goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796) 		/* The only unlinked inodes we let through here have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797) 		 * valid i_mode and are being read by the orphan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798) 		 * recovery code: that's fine, we're about to complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) 		 * the process of deleting those.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800) 		 * OR it is the EXT4_BOOT_LOADER_INO which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801) 		 * not initialized on a new filesystem. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) 	ei->i_flags = le32_to_cpu(raw_inode->i_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) 	ext4_set_inode_flags(inode, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) 	inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806) 	ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) 	if (ext4_has_feature_64bit(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) 		ei->i_file_acl |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) 			((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810) 	inode->i_size = ext4_isize(sb, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) 	if ((size = i_size_read(inode)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) 		ext4_error_inode(inode, function, line, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813) 				 "iget: bad i_size value: %lld", size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) 		ret = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) 		goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818) 	 * If dir_index is not enabled but there's dir with INDEX flag set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819) 	 * we'd normally treat htree data as empty space. But with metadata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820) 	 * checksumming that corrupts checksums so forbid that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822) 	if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823) 	    ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824) 		ext4_error_inode(inode, function, line, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825) 			 "iget: Dir with htree data on filesystem without dir_index feature.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826) 		ret = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827) 		goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829) 	ei->i_disksize = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830) #ifdef CONFIG_QUOTA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831) 	ei->i_reserved_quota = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833) 	inode->i_generation = le32_to_cpu(raw_inode->i_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834) 	ei->i_block_group = iloc.block_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835) 	ei->i_last_alloc_group = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837) 	 * NOTE! The in-memory inode i_data array is in little-endian order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) 	 * even on big-endian machines: we do NOT byteswap the block numbers!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840) 	for (block = 0; block < EXT4_N_BLOCKS; block++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841) 		ei->i_data[block] = raw_inode->i_block[block];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) 	INIT_LIST_HEAD(&ei->i_orphan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) 	ext4_fc_init_inode(&ei->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846) 	 * Set transaction id's of transactions that have to be committed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847) 	 * to finish f[data]sync. We set them to currently running transaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848) 	 * as we cannot be sure that the inode or some of its metadata isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849) 	 * part of the transaction - the inode could have been reclaimed and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) 	 * now it is reread from disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) 	if (journal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) 		transaction_t *transaction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) 		tid_t tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) 		read_lock(&journal->j_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) 		if (journal->j_running_transaction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) 			transaction = journal->j_running_transaction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) 			transaction = journal->j_committing_transaction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) 		if (transaction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) 			tid = transaction->t_tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864) 			tid = journal->j_commit_sequence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865) 		read_unlock(&journal->j_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866) 		ei->i_sync_tid = tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867) 		ei->i_datasync_tid = tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870) 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) 		if (ei->i_extra_isize == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872) 			/* The extra space is currently unused. Use it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) 			BUILD_BUG_ON(sizeof(struct ext4_inode) & 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) 			ei->i_extra_isize = sizeof(struct ext4_inode) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875) 					    EXT4_GOOD_OLD_INODE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877) 			ret = ext4_iget_extra_inode(inode, raw_inode, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879) 				goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) 	EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) 	EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885) 	EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) 	EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) 	if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) 		u64 ivers = le32_to_cpu(raw_inode->i_disk_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891) 		if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892) 			if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) 				ivers |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894) 		    (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) 		ext4_inode_set_iversion_queried(inode, ivers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4900) 	if (ei->i_file_acl &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4901) 	    !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4902) 		ext4_error_inode(inode, function, line, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4903) 				 "iget: bad extended attribute block %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) 				 ei->i_file_acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905) 		ret = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) 		goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) 	} else if (!ext4_has_inline_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) 		/* validate the block references in the inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909) 		if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) 			(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) 			(S_ISLNK(inode->i_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) 			!ext4_inode_is_fast_symlink(inode)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913) 			if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914) 				ret = ext4_ext_check_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916) 				ret = ext4_ind_check_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920) 		goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) 	if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) 		inode->i_op = &ext4_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924) 		inode->i_fop = &ext4_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925) 		ext4_set_aops(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) 	} else if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927) 		inode->i_op = &ext4_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4928) 		inode->i_fop = &ext4_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4929) 	} else if (S_ISLNK(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4930) 		/* VFS does not allow setting these so must be corruption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4931) 		if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4932) 			ext4_error_inode(inode, function, line, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4933) 					 "iget: immutable or append flags "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4934) 					 "not allowed on symlinks");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4935) 			ret = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4936) 			goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4937) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4938) 		if (IS_ENCRYPTED(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4939) 			inode->i_op = &ext4_encrypted_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4940) 			ext4_set_aops(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4941) 		} else if (ext4_inode_is_fast_symlink(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4942) 			inode->i_link = (char *)ei->i_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4943) 			inode->i_op = &ext4_fast_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4944) 			nd_terminate_link(ei->i_data, inode->i_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4945) 				sizeof(ei->i_data) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4946) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4947) 			inode->i_op = &ext4_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4948) 			ext4_set_aops(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4949) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4950) 		inode_nohighmem(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4951) 	} else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4952) 	      S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4953) 		inode->i_op = &ext4_special_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4954) 		if (raw_inode->i_block[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4955) 			init_special_inode(inode, inode->i_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4956) 			   old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4957) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4958) 			init_special_inode(inode, inode->i_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4959) 			   new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4960) 	} else if (ino == EXT4_BOOT_LOADER_INO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4961) 		make_bad_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4962) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4963) 		ret = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4964) 		ext4_error_inode(inode, function, line, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4965) 				 "iget: bogus i_mode (%o)", inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4966) 		goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4967) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4968) 	if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4969) 		ext4_error_inode(inode, function, line, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4970) 				 "casefold flag without casefold feature");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4971) 	brelse(iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4973) 	unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4974) 	return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4976) bad_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4977) 	brelse(iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4978) 	iget_failed(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4979) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4982) static int ext4_inode_blocks_set(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4983) 				struct ext4_inode *raw_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4984) 				struct ext4_inode_info *ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4985) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4986) 	struct inode *inode = &(ei->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4987) 	u64 i_blocks = READ_ONCE(inode->i_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4988) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4990) 	if (i_blocks <= ~0U) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4991) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4992) 		 * i_blocks can be represented in a 32 bit variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4993) 		 * as multiple of 512 bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4994) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4995) 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4996) 		raw_inode->i_blocks_high = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4997) 		ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4998) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4999) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5000) 	if (!ext4_has_feature_huge_file(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5001) 		return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5002) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5003) 	if (i_blocks <= 0xffffffffffffULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5004) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5005) 		 * i_blocks can be represented in a 48 bit variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5006) 		 * as multiple of 512 bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5007) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5008) 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5009) 		raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5010) 		ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5011) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5012) 		ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5013) 		/* i_block is stored in file system block size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5014) 		i_blocks = i_blocks >> (inode->i_blkbits - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5015) 		raw_inode->i_blocks_lo   = cpu_to_le32(i_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5016) 		raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5017) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5018) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5021) static void __ext4_update_other_inode_time(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5022) 					   unsigned long orig_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5023) 					   unsigned long ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5024) 					   struct ext4_inode *raw_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5025) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5026) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5028) 	inode = find_inode_by_ino_rcu(sb, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5029) 	if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5030) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5032) 	if ((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5033) 			       I_DIRTY_INODE)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5034) 	    ((inode->i_state & I_DIRTY_TIME) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5035) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5037) 	spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5038) 	if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5039) 				I_DIRTY_INODE)) == 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5040) 	    (inode->i_state & I_DIRTY_TIME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5041) 		struct ext4_inode_info	*ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5043) 		inode->i_state &= ~I_DIRTY_TIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5044) 		spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5046) 		spin_lock(&ei->i_raw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5047) 		EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5048) 		EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5049) 		EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5050) 		ext4_inode_csum_set(inode, raw_inode, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5051) 		spin_unlock(&ei->i_raw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5052) 		trace_ext4_other_inode_update_time(inode, orig_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5053) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5054) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5055) 	spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5058) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5059)  * Opportunistically update the other time fields for other inodes in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5060)  * the same inode table block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5061)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5062) static void ext4_update_other_inodes_time(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5063) 					  unsigned long orig_ino, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5065) 	unsigned long ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5066) 	int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5067) 	int inode_size = EXT4_INODE_SIZE(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5069) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5070) 	 * Calculate the first inode in the inode table block.  Inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5071) 	 * numbers are one-based.  That is, the first inode in a block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5072) 	 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5073) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5074) 	ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5075) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5076) 	for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5077) 		if (ino == orig_ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5078) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5079) 		__ext4_update_other_inode_time(sb, orig_ino, ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5080) 					       (struct ext4_inode *)buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5081) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5082) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5083) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5084) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5085) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5086)  * Post the struct inode info into an on-disk inode location in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5087)  * buffer-cache.  This gobbles the caller's reference to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5088)  * buffer_head in the inode location struct.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5089)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5090)  * The caller must have write access to iloc->bh.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5091)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5092) static int ext4_do_update_inode(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5093) 				struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5094) 				struct ext4_iloc *iloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5095) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5096) 	struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5097) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5098) 	struct buffer_head *bh = iloc->bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5099) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5100) 	int err = 0, block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5101) 	int need_datasync = 0, set_large_file = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5102) 	uid_t i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5103) 	gid_t i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5104) 	projid_t i_projid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5106) 	spin_lock(&ei->i_raw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5108) 	/* For fields not tracked in the in-memory inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5109) 	 * initialise them to zero for new inodes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5110) 	if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5111) 		memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5113) 	err = ext4_inode_blocks_set(handle, raw_inode, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5114) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5115) 		spin_unlock(&ei->i_raw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5116) 		goto out_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5119) 	raw_inode->i_mode = cpu_to_le16(inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5120) 	i_uid = i_uid_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5121) 	i_gid = i_gid_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5122) 	i_projid = from_kprojid(&init_user_ns, ei->i_projid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5123) 	if (!(test_opt(inode->i_sb, NO_UID32))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5124) 		raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5125) 		raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5127)  * Fix up interoperability with old kernels. Otherwise, old inodes get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5128)  * re-used with the upper 16 bits of the uid/gid intact
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5129)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5130) 		if (ei->i_dtime && list_empty(&ei->i_orphan)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5131) 			raw_inode->i_uid_high = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5132) 			raw_inode->i_gid_high = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5133) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5134) 			raw_inode->i_uid_high =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5135) 				cpu_to_le16(high_16_bits(i_uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5136) 			raw_inode->i_gid_high =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5137) 				cpu_to_le16(high_16_bits(i_gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5139) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5140) 		raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5141) 		raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5142) 		raw_inode->i_uid_high = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5143) 		raw_inode->i_gid_high = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5145) 	raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5147) 	EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5148) 	EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5149) 	EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5150) 	EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5152) 	raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5153) 	raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5154) 	if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5155) 		raw_inode->i_file_acl_high =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5156) 			cpu_to_le16(ei->i_file_acl >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5157) 	raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5158) 	if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5159) 		ext4_isize_set(raw_inode, ei->i_disksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5160) 		need_datasync = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5162) 	if (ei->i_disksize > 0x7fffffffULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5163) 		if (!ext4_has_feature_large_file(sb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5164) 				EXT4_SB(sb)->s_es->s_rev_level ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5165) 		    cpu_to_le32(EXT4_GOOD_OLD_REV))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5166) 			set_large_file = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5168) 	raw_inode->i_generation = cpu_to_le32(inode->i_generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5169) 	if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5170) 		if (old_valid_dev(inode->i_rdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5171) 			raw_inode->i_block[0] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5172) 				cpu_to_le32(old_encode_dev(inode->i_rdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5173) 			raw_inode->i_block[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5174) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5175) 			raw_inode->i_block[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5176) 			raw_inode->i_block[1] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5177) 				cpu_to_le32(new_encode_dev(inode->i_rdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5178) 			raw_inode->i_block[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5179) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5180) 	} else if (!ext4_has_inline_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5181) 		for (block = 0; block < EXT4_N_BLOCKS; block++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5182) 			raw_inode->i_block[block] = ei->i_data[block];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5185) 	if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5186) 		u64 ivers = ext4_inode_peek_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5188) 		raw_inode->i_disk_version = cpu_to_le32(ivers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5189) 		if (ei->i_extra_isize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5190) 			if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5191) 				raw_inode->i_version_hi =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5192) 					cpu_to_le32(ivers >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5193) 			raw_inode->i_extra_isize =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5194) 				cpu_to_le16(ei->i_extra_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5195) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5198) 	BUG_ON(!ext4_has_feature_project(inode->i_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5199) 	       i_projid != EXT4_DEF_PROJID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5201) 	if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5202) 	    EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5203) 		raw_inode->i_projid = cpu_to_le32(i_projid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5205) 	ext4_inode_csum_set(inode, raw_inode, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5206) 	spin_unlock(&ei->i_raw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5207) 	if (inode->i_sb->s_flags & SB_LAZYTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5208) 		ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5209) 					      bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5211) 	BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5212) 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5213) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5214) 		goto out_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5215) 	ext4_clear_inode_state(inode, EXT4_STATE_NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5216) 	if (set_large_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5217) 		BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5218) 		err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5219) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5220) 			goto out_brelse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5221) 		ext4_set_feature_large_file(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5222) 		ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5223) 		err = ext4_handle_dirty_super(handle, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5225) 	ext4_update_inode_fsync_trans(handle, inode, need_datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5226) out_brelse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5227) 	brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5228) 	ext4_std_error(inode->i_sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5229) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5232) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5233)  * ext4_write_inode()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5234)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5235)  * We are called from a few places:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5236)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5237)  * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5238)  *   Here, there will be no transaction running. We wait for any running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5239)  *   transaction to commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5240)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5241)  * - Within flush work (sys_sync(), kupdate and such).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5242)  *   We wait on commit, if told to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5243)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5244)  * - Within iput_final() -> write_inode_now()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5245)  *   We wait on commit, if told to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5246)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5247)  * In all cases it is actually safe for us to return without doing anything,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5248)  * because the inode has been copied into a raw inode buffer in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5249)  * ext4_mark_inode_dirty().  This is a correctness thing for WB_SYNC_ALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5250)  * writeback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5251)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5252)  * Note that we are absolutely dependent upon all inode dirtiers doing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5253)  * right thing: they *must* call mark_inode_dirty() after dirtying info in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5254)  * which we are interested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5255)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5256)  * It would be a bug for them to not do this.  The code:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5257)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5258)  *	mark_inode_dirty(inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5259)  *	stuff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5260)  *	inode->i_size = expr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5261)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5262)  * is in error because write_inode() could occur while `stuff()' is running,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5263)  * and the new i_size will be lost.  Plus the inode will no longer be on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5264)  * superblock's dirty inode list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5265)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5266) int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5268) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5270) 	if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5271) 	    sb_rdonly(inode->i_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5272) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5274) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5275) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5277) 	if (EXT4_SB(inode->i_sb)->s_journal) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5278) 		if (ext4_journal_current_handle()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5279) 			jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5280) 			dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5281) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5282) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5284) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5285) 		 * No need to force transaction in WB_SYNC_NONE mode. Also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5286) 		 * ext4_sync_fs() will force the commit after everything is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5287) 		 * written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5288) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5289) 		if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5290) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5292) 		err = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5293) 						EXT4_I(inode)->i_sync_tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5294) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5295) 		struct ext4_iloc iloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5297) 		err = __ext4_get_inode_loc_noinmem(inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5298) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5299) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5300) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5301) 		 * sync(2) will flush the whole buffer cache. No need to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5302) 		 * it here separately for each inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5303) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5304) 		if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5305) 			sync_dirty_buffer(iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5306) 		if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5307) 			ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5308) 					       "IO error syncing inode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5309) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5310) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5311) 		brelse(iloc.bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5313) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5317)  * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5318)  * buffers that are attached to a page stradding i_size and are undergoing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5319)  * commit. In that case we have to wait for commit to finish and try again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5320)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5321) static void ext4_wait_for_tail_page_commit(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5323) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5324) 	unsigned offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5325) 	journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5326) 	tid_t commit_tid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5327) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5329) 	offset = inode->i_size & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5330) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5331) 	 * If the page is fully truncated, we don't need to wait for any commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5332) 	 * (and we even should not as __ext4_journalled_invalidatepage() may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5333) 	 * strip all buffers from the page but keep the page dirty which can then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5334) 	 * confuse e.g. concurrent ext4_writepage() seeing dirty page without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5335) 	 * buffers). Also we don't need to wait for any commit if all buffers in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5336) 	 * the page remain valid. This is most beneficial for the common case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5337) 	 * blocksize == PAGESIZE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5338) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5339) 	if (!offset || offset > (PAGE_SIZE - i_blocksize(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5340) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5341) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5342) 		page = find_lock_page(inode->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5343) 				      inode->i_size >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5344) 		if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5345) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5346) 		ret = __ext4_journalled_invalidatepage(page, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5347) 						PAGE_SIZE - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5348) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5349) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5350) 		if (ret != -EBUSY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5351) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5352) 		commit_tid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5353) 		read_lock(&journal->j_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5354) 		if (journal->j_committing_transaction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5355) 			commit_tid = journal->j_committing_transaction->t_tid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5356) 		read_unlock(&journal->j_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5357) 		if (commit_tid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5358) 			jbd2_log_wait_commit(journal, commit_tid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5362) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5363)  * ext4_setattr()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5364)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5365)  * Called from notify_change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5366)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5367)  * We want to trap VFS attempts to truncate the file as soon as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5368)  * possible.  In particular, we want to make sure that when the VFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5369)  * shrinks i_size, we put the inode on the orphan list and modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5370)  * i_disksize immediately, so that during the subsequent flushing of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5371)  * dirty pages and freeing of disk blocks, we can guarantee that any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5372)  * commit will leave the blocks being flushed in an unused state on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5373)  * disk.  (On recovery, the inode will get truncated and the blocks will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5374)  * be freed, so we have a strong guarantee that no future commit will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5375)  * leave these blocks visible to the user.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5376)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5377)  * Another thing we have to assure is that if we are in ordered mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5378)  * and inode is still attached to the committing transaction, we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5379)  * we start writeout of all the dirty pages which are being truncated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5380)  * This way we are sure that all the data written in the previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5381)  * transaction are already on disk (truncate waits for pages under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5382)  * writeback).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5383)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5384)  * Called with inode->i_mutex down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5385)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5386) int ext4_setattr(struct dentry *dentry, struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5388) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5389) 	int error, rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5390) 	int orphan = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5391) 	const unsigned int ia_valid = attr->ia_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5393) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5394) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5396) 	if (unlikely(IS_IMMUTABLE(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5397) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5399) 	if (unlikely(IS_APPEND(inode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5400) 		     (ia_valid & (ATTR_MODE | ATTR_UID |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5401) 				  ATTR_GID | ATTR_TIMES_SET))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5402) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5404) 	error = setattr_prepare(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5405) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5406) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5408) 	error = fscrypt_prepare_setattr(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5409) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5410) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5412) 	error = fsverity_prepare_setattr(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5413) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5414) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5416) 	if (is_quota_modification(inode, attr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5417) 		error = dquot_initialize(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5418) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5419) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5421) 	ext4_fc_start_update(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5422) 	if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5423) 	    (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5424) 		handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5426) 		/* (user+group)*(old+new) structure, inode write (sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5427) 		 * inode block, ? - but truncate inode update has it) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5428) 		handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5429) 			(EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5430) 			 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5431) 		if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5432) 			error = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5433) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5434) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5436) 		/* dquot_transfer() calls back ext4_get_inode_usage() which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5437) 		 * counts xattr inode references.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5438) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5439) 		down_read(&EXT4_I(inode)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5440) 		error = dquot_transfer(inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5441) 		up_read(&EXT4_I(inode)->xattr_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5443) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5444) 			ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5445) 			ext4_fc_stop_update(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5446) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5447) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5448) 		/* Update corresponding info in inode so that everything is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5449) 		 * one transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5450) 		if (attr->ia_valid & ATTR_UID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5451) 			inode->i_uid = attr->ia_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5452) 		if (attr->ia_valid & ATTR_GID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5453) 			inode->i_gid = attr->ia_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5454) 		error = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5455) 		ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5456) 		if (unlikely(error)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5457) 			ext4_fc_stop_update(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5458) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5459) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5460) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5462) 	if (attr->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5463) 		handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5464) 		loff_t oldsize = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5465) 		int shrink = (attr->ia_size < inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5467) 		if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5468) 			struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5470) 			if (attr->ia_size > sbi->s_bitmap_maxbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5471) 				ext4_fc_stop_update(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5472) 				return -EFBIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5473) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5474) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5475) 		if (!S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5476) 			ext4_fc_stop_update(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5477) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5478) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5480) 		if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5481) 			inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5483) 		if (shrink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5484) 			if (ext4_should_order_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5485) 				error = ext4_begin_ordered_truncate(inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5486) 							    attr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5487) 				if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5488) 					goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5489) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5490) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5491) 			 * Blocks are going to be removed from the inode. Wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5492) 			 * for dio in flight.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5493) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5494) 			inode_dio_wait(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5495) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5497) 		down_write(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5499) 		rc = ext4_break_layouts(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5500) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5501) 			up_write(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5502) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5503) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5505) 		if (attr->ia_size != inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5506) 			handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5507) 			if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5508) 				error = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5509) 				goto out_mmap_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5510) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5511) 			if (ext4_handle_valid(handle) && shrink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5512) 				error = ext4_orphan_add(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5513) 				orphan = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5514) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5515) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5516) 			 * Update c/mtime on truncate up, ext4_truncate() will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5517) 			 * update c/mtime in shrink case below
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5518) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5519) 			if (!shrink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5520) 				inode->i_mtime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5521) 				inode->i_ctime = inode->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5522) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5524) 			if (shrink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5525) 				ext4_fc_track_range(handle, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5526) 					(attr->ia_size > 0 ? attr->ia_size - 1 : 0) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5527) 					inode->i_sb->s_blocksize_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5528) 					EXT_MAX_BLOCKS - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5529) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5530) 				ext4_fc_track_range(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5531) 					handle, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5532) 					(oldsize > 0 ? oldsize - 1 : oldsize) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5533) 					inode->i_sb->s_blocksize_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5534) 					(attr->ia_size > 0 ? attr->ia_size - 1 : 0) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5535) 					inode->i_sb->s_blocksize_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5537) 			down_write(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5538) 			EXT4_I(inode)->i_disksize = attr->ia_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5539) 			rc = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5540) 			if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5541) 				error = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5542) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5543) 			 * We have to update i_size under i_data_sem together
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5544) 			 * with i_disksize to avoid races with writeback code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5545) 			 * running ext4_wb_update_i_disksize().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5546) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5547) 			if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5548) 				i_size_write(inode, attr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5549) 			up_write(&EXT4_I(inode)->i_data_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5550) 			ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5551) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5552) 				goto out_mmap_sem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5553) 			if (!shrink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5554) 				pagecache_isize_extended(inode, oldsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5555) 							 inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5556) 			} else if (ext4_should_journal_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5557) 				ext4_wait_for_tail_page_commit(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5558) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5559) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5561) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5562) 		 * Truncate pagecache after we've waited for commit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5563) 		 * in data=journal mode to make pages freeable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5564) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5565) 		truncate_pagecache(inode, inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5566) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5567) 		 * Call ext4_truncate() even if i_size didn't change to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5568) 		 * truncate possible preallocated blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5569) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5570) 		if (attr->ia_size <= oldsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5571) 			rc = ext4_truncate(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5572) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5573) 				error = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5574) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5575) out_mmap_sem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5576) 		up_write(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5579) 	if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5580) 		setattr_copy(inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5581) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5582) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5584) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5585) 	 * If the call to ext4_truncate failed to get a transaction handle at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5586) 	 * all, we need to clean up the in-core orphan list manually.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5587) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5588) 	if (orphan && inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5589) 		ext4_orphan_del(NULL, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5591) 	if (!error && (ia_valid & ATTR_MODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5592) 		rc = posix_acl_chmod(inode, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5594) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5595) 	if  (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5596) 		ext4_std_error(inode->i_sb, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5597) 	if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5598) 		error = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5599) 	ext4_fc_stop_update(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5600) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5603) int ext4_getattr(const struct path *path, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5604) 		 u32 request_mask, unsigned int query_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5606) 	struct inode *inode = d_inode(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5607) 	struct ext4_inode *raw_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5608) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5609) 	unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5611) 	if ((request_mask & STATX_BTIME) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5612) 	    EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5613) 		stat->result_mask |= STATX_BTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5614) 		stat->btime.tv_sec = ei->i_crtime.tv_sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5615) 		stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5618) 	flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5619) 	if (flags & EXT4_APPEND_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5620) 		stat->attributes |= STATX_ATTR_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5621) 	if (flags & EXT4_COMPR_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5622) 		stat->attributes |= STATX_ATTR_COMPRESSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5623) 	if (flags & EXT4_ENCRYPT_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5624) 		stat->attributes |= STATX_ATTR_ENCRYPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5625) 	if (flags & EXT4_IMMUTABLE_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5626) 		stat->attributes |= STATX_ATTR_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5627) 	if (flags & EXT4_NODUMP_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5628) 		stat->attributes |= STATX_ATTR_NODUMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5629) 	if (flags & EXT4_VERITY_FL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5630) 		stat->attributes |= STATX_ATTR_VERITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5632) 	stat->attributes_mask |= (STATX_ATTR_APPEND |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5633) 				  STATX_ATTR_COMPRESSED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5634) 				  STATX_ATTR_ENCRYPTED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5635) 				  STATX_ATTR_IMMUTABLE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5636) 				  STATX_ATTR_NODUMP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5637) 				  STATX_ATTR_VERITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5639) 	generic_fillattr(inode, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5640) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5643) int ext4_file_getattr(const struct path *path, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5644) 		      u32 request_mask, unsigned int query_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5646) 	struct inode *inode = d_inode(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5647) 	u64 delalloc_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5649) 	ext4_getattr(path, stat, request_mask, query_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5651) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5652) 	 * If there is inline data in the inode, the inode will normally not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5653) 	 * have data blocks allocated (it may have an external xattr block).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5654) 	 * Report at least one sector for such files, so tools like tar, rsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5655) 	 * others don't incorrectly think the file is completely sparse.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5656) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5657) 	if (unlikely(ext4_has_inline_data(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5658) 		stat->blocks += (stat->size + 511) >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5660) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5661) 	 * We can't update i_blocks if the block allocation is delayed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5662) 	 * otherwise in the case of system crash before the real block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5663) 	 * allocation is done, we will have i_blocks inconsistent with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5664) 	 * on-disk file blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5665) 	 * We always keep i_blocks updated together with real
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5666) 	 * allocation. But to not confuse with user, stat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5667) 	 * will return the blocks that include the delayed allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5668) 	 * blocks for this file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5669) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5670) 	delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5671) 				   EXT4_I(inode)->i_reserved_data_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5672) 	stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5673) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5676) static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5677) 				   int pextents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5678) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5679) 	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5680) 		return ext4_ind_trans_blocks(inode, lblocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5681) 	return ext4_ext_index_trans_blocks(inode, pextents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5684) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5685)  * Account for index blocks, block groups bitmaps and block group
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5686)  * descriptor blocks if modify datablocks and index blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5687)  * worse case, the indexs blocks spread over different block groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5688)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5689)  * If datablocks are discontiguous, they are possible to spread over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5690)  * different block groups too. If they are contiguous, with flexbg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5691)  * they could still across block group boundary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5692)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5693)  * Also account for superblock, inode, quota and xattr blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5694)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5695) static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5696) 				  int pextents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5698) 	ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5699) 	int gdpblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5700) 	int idxblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5701) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5703) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5704) 	 * How many index blocks need to touch to map @lblocks logical blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5705) 	 * to @pextents physical extents?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5706) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5707) 	idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5709) 	ret = idxblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5711) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5712) 	 * Now let's see how many group bitmaps and group descriptors need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5713) 	 * to account
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5714) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5715) 	groups = idxblocks + pextents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5716) 	gdpblocks = groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5717) 	if (groups > ngroups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5718) 		groups = ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5719) 	if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5720) 		gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5722) 	/* bitmaps and block group descriptor blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5723) 	ret += groups + gdpblocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5725) 	/* Blocks for super block, inode, quota and xattr blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5726) 	ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5728) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5731) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5732)  * Calculate the total number of credits to reserve to fit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5733)  * the modification of a single pages into a single transaction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5734)  * which may include multiple chunks of block allocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5735)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5736)  * This could be called via ext4_write_begin()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5737)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5738)  * We need to consider the worse case, when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5739)  * one new block per extent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5740)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5741) int ext4_writepage_trans_blocks(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5743) 	int bpp = ext4_journal_blocks_per_page(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5744) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5746) 	ret = ext4_meta_trans_blocks(inode, bpp, bpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5748) 	/* Account for data blocks for journalled mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5749) 	if (ext4_should_journal_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5750) 		ret += bpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5751) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5754) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5755)  * Calculate the journal credits for a chunk of data modification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5756)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5757)  * This is called from DIO, fallocate or whoever calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5758)  * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5759)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5760)  * journal buffers for data blocks are not included here, as DIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5761)  * and fallocate do no need to journal data buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5762)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5763) int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5765) 	return ext4_meta_trans_blocks(inode, nrblocks, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5768) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5769)  * The caller must have previously called ext4_reserve_inode_write().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5770)  * Give this, we know that the caller already has write access to iloc->bh.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5771)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5772) int ext4_mark_iloc_dirty(handle_t *handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5773) 			 struct inode *inode, struct ext4_iloc *iloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5775) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5777) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5778) 		put_bh(iloc->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5779) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5780) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5781) 	ext4_fc_track_inode(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5783) 	if (IS_I_VERSION(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5784) 		inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5786) 	/* the do_update_inode consumes one bh->b_count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5787) 	get_bh(iloc->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5789) 	/* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5790) 	err = ext4_do_update_inode(handle, inode, iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5791) 	put_bh(iloc->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5792) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5793) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5795) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5796)  * On success, We end up with an outstanding reference count against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5797)  * iloc->bh.  This _must_ be cleaned up later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5798)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5800) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5801) ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5802) 			 struct ext4_iloc *iloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5804) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5806) 	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5807) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5809) 	err = ext4_get_inode_loc(inode, iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5810) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5811) 		BUFFER_TRACE(iloc->bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5812) 		err = ext4_journal_get_write_access(handle, iloc->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5813) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5814) 			brelse(iloc->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5815) 			iloc->bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5816) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5817) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5818) 	ext4_std_error(inode->i_sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5819) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5822) static int __ext4_expand_extra_isize(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5823) 				     unsigned int new_extra_isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5824) 				     struct ext4_iloc *iloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5825) 				     handle_t *handle, int *no_expand)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5827) 	struct ext4_inode *raw_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5828) 	struct ext4_xattr_ibody_header *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5829) 	unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5830) 	struct ext4_inode_info *ei = EXT4_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5831) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5833) 	/* this was checked at iget time, but double check for good measure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5834) 	if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5835) 	    (ei->i_extra_isize & 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5836) 		EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5837) 				 ei->i_extra_isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5838) 				 EXT4_INODE_SIZE(inode->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5839) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5840) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5841) 	if ((new_extra_isize < ei->i_extra_isize) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5842) 	    (new_extra_isize < 4) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5843) 	    (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5844) 		return -EINVAL;	/* Should never happen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5846) 	raw_inode = ext4_raw_inode(iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5848) 	header = IHDR(inode, raw_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5850) 	/* No extended attributes present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5851) 	if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5852) 	    header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5853) 		memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5854) 		       EXT4_I(inode)->i_extra_isize, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5855) 		       new_extra_isize - EXT4_I(inode)->i_extra_isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5856) 		EXT4_I(inode)->i_extra_isize = new_extra_isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5857) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5858) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5860) 	/* try to expand with EAs present */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5861) 	error = ext4_expand_extra_isize_ea(inode, new_extra_isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5862) 					   raw_inode, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5863) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5864) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5865) 		 * Inode size expansion failed; don't try again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5866) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5867) 		*no_expand = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5870) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5871) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5873) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5874)  * Expand an inode by new_extra_isize bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5875)  * Returns 0 on success or negative error number on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5876)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5877) static int ext4_try_to_expand_extra_isize(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5878) 					  unsigned int new_extra_isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5879) 					  struct ext4_iloc iloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5880) 					  handle_t *handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5881) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5882) 	int no_expand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5883) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5885) 	if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5886) 		return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5888) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5889) 	 * In nojournal mode, we can immediately attempt to expand
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5890) 	 * the inode.  When journaled, we first need to obtain extra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5891) 	 * buffer credits since we may write into the EA block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5892) 	 * with this same handle. If journal_extend fails, then it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5893) 	 * only result in a minor loss of functionality for that inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5894) 	 * If this is felt to be critical, then e2fsck should be run to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5895) 	 * force a large enough s_min_extra_isize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5896) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5897) 	if (ext4_journal_extend(handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5898) 				EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5899) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5901) 	if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5902) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5904) 	error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5905) 					  handle, &no_expand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5906) 	ext4_write_unlock_xattr(inode, &no_expand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5908) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5911) int ext4_expand_extra_isize(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5912) 			    unsigned int new_extra_isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5913) 			    struct ext4_iloc *iloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5915) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5916) 	int no_expand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5917) 	int error, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5919) 	if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5920) 		brelse(iloc->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5921) 		return -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5922) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5924) 	handle = ext4_journal_start(inode, EXT4_HT_INODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5925) 				    EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5926) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5927) 		error = PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5928) 		brelse(iloc->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5929) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5930) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5932) 	ext4_write_lock_xattr(inode, &no_expand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5934) 	BUFFER_TRACE(iloc->bh, "get_write_access");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5935) 	error = ext4_journal_get_write_access(handle, iloc->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5936) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5937) 		brelse(iloc->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5938) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5939) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5941) 	error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5942) 					  handle, &no_expand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5944) 	rc = ext4_mark_iloc_dirty(handle, inode, iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5945) 	if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5946) 		error = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5948) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5949) 	ext4_write_unlock_xattr(inode, &no_expand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5950) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5951) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5954) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5955)  * What we do here is to mark the in-core inode as clean with respect to inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5956)  * dirtiness (it may still be data-dirty).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5957)  * This means that the in-core inode may be reaped by prune_icache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5958)  * without having to perform any I/O.  This is a very good thing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5959)  * because *any* task may call prune_icache - even ones which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5960)  * have a transaction open against a different journal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5961)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5962)  * Is this cheating?  Not really.  Sure, we haven't written the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5963)  * inode out, but prune_icache isn't a user-visible syncing function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5964)  * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5965)  * we start and wait on commits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5966)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5967) int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5968) 				const char *func, unsigned int line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5970) 	struct ext4_iloc iloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5971) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5972) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5974) 	might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5975) 	trace_ext4_mark_inode_dirty(inode, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5976) 	err = ext4_reserve_inode_write(handle, inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5977) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5978) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5980) 	if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5981) 		ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5982) 					       iloc, handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5984) 	err = ext4_mark_iloc_dirty(handle, inode, &iloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5985) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5986) 	if (unlikely(err))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5987) 		ext4_error_inode_err(inode, func, line, 0, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5988) 					"mark_inode_dirty error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5989) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5992) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5993)  * ext4_dirty_inode() is called from __mark_inode_dirty()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5994)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5995)  * We're really interested in the case where a file is being extended.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5996)  * i_size has been changed by generic_commit_write() and we thus need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5997)  * to include the updated inode in the current transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5998)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5999)  * Also, dquot_alloc_block() will always dirty the inode when blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6000)  * are allocated to the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6001)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6002)  * If the inode is marked synchronous, we don't honour that here - doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6003)  * so would cause a commit on atime updates, which we don't bother doing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6004)  * We handle synchronous inodes at the highest possible level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6005)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6006)  * If only the I_DIRTY_TIME flag is set, we can skip everything.  If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6007)  * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6008)  * to copy into the on-disk inode structure are the timestamp files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6009)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6010) void ext4_dirty_inode(struct inode *inode, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6011) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6012) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6014) 	if (flags == I_DIRTY_TIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6015) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6016) 	handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6017) 	if (IS_ERR(handle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6018) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6020) 	ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6022) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6023) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6024) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6027) int ext4_change_inode_journal_flag(struct inode *inode, int val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6028) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6029) 	journal_t *journal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6030) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6031) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6032) 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6034) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6035) 	 * We have to be very careful here: changing a data block's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6036) 	 * journaling status dynamically is dangerous.  If we write a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6037) 	 * data block to the journal, change the status and then delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6038) 	 * that block, we risk forgetting to revoke the old log record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6039) 	 * from the journal and so a subsequent replay can corrupt data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6040) 	 * So, first we make sure that the journal is empty and that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6041) 	 * nobody is changing anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6042) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6044) 	journal = EXT4_JOURNAL(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6045) 	if (!journal)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6046) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6047) 	if (is_journal_aborted(journal))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6048) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6050) 	/* Wait for all existing dio workers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6051) 	inode_dio_wait(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6053) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6054) 	 * Before flushing the journal and switching inode's aops, we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6055) 	 * to flush all dirty data the inode has. There can be outstanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6056) 	 * delayed allocations, there can be unwritten extents created by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6057) 	 * fallocate or buffered writes in dioread_nolock mode covered by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6058) 	 * dirty data which can be converted only after flushing the dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6059) 	 * data (and journalled aops don't know how to handle these cases).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6060) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6061) 	if (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6062) 		down_write(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6063) 		err = filemap_write_and_wait(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6064) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6065) 			up_write(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6066) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6067) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6068) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6070) 	percpu_down_write(&sbi->s_writepages_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6071) 	jbd2_journal_lock_updates(journal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6073) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6074) 	 * OK, there are no updates running now, and all cached data is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6075) 	 * synced to disk.  We are now in a completely consistent state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6076) 	 * which doesn't have anything in the journal, and we know that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6077) 	 * no filesystem updates are running, so it is safe to modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6078) 	 * the inode's in-core data-journaling state flag now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6079) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6081) 	if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6082) 		ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6083) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6084) 		err = jbd2_journal_flush(journal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6085) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6086) 			jbd2_journal_unlock_updates(journal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6087) 			percpu_up_write(&sbi->s_writepages_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6088) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6089) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6090) 		ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6091) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6092) 	ext4_set_aops(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6094) 	jbd2_journal_unlock_updates(journal);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6095) 	percpu_up_write(&sbi->s_writepages_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6097) 	if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6098) 		up_write(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6100) 	/* Finally we can mark the inode as dirty. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6102) 	handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6103) 	if (IS_ERR(handle))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6104) 		return PTR_ERR(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6106) 	ext4_fc_mark_ineligible(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6107) 		EXT4_FC_REASON_JOURNAL_FLAG_CHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6108) 	err = ext4_mark_inode_dirty(handle, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6109) 	ext4_handle_sync(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6110) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6111) 	ext4_std_error(inode->i_sb, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6113) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6116) static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6118) 	return !buffer_mapped(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6121) vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6123) 	struct vm_area_struct *vma = vmf->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6124) 	struct page *page = vmf->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6125) 	loff_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6126) 	unsigned long len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6127) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6128) 	vm_fault_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6129) 	struct file *file = vma->vm_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6130) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6131) 	struct address_space *mapping = inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6132) 	handle_t *handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6133) 	get_block_t *get_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6134) 	int retries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6136) 	if (unlikely(IS_IMMUTABLE(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6137) 		return VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6139) 	sb_start_pagefault(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6140) 	file_update_time(vma->vm_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6142) 	down_read(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6144) 	err = ext4_convert_inline_data(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6145) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6146) 		goto out_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6148) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6149) 	 * On data journalling we skip straight to the transaction handle:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6150) 	 * there's no delalloc; page truncated will be checked later; the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6151) 	 * early return w/ all buffers mapped (calculates size/len) can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6152) 	 * be used; and there's no dioread_nolock, so only ext4_get_block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6153) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6154) 	if (ext4_should_journal_data(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6155) 		goto retry_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6157) 	/* Delalloc case is easy... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6158) 	if (test_opt(inode->i_sb, DELALLOC) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6159) 	    !ext4_nonda_switch(inode->i_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6160) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6161) 			err = block_page_mkwrite(vma, vmf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6162) 						   ext4_da_get_block_prep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6163) 		} while (err == -ENOSPC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6164) 		       ext4_should_retry_alloc(inode->i_sb, &retries));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6165) 		goto out_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6168) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6169) 	size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6170) 	/* Page got truncated from under us? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6171) 	if (page->mapping != mapping || page_offset(page) > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6172) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6173) 		ret = VM_FAULT_NOPAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6174) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6175) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6177) 	if (page->index == size >> PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6178) 		len = size & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6179) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6180) 		len = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6181) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6182) 	 * Return if we have all the buffers mapped. This avoids the need to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6183) 	 * journal_start/journal_stop which can block and take a long time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6184) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6185) 	 * This cannot be done for data journalling, as we have to add the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6186) 	 * inode to the transaction's list to writeprotect pages on commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6187) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6188) 	if (page_has_buffers(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6189) 		if (!ext4_walk_page_buffers(NULL, page_buffers(page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6190) 					    0, len, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6191) 					    ext4_bh_unmapped)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6192) 			/* Wait so that we don't change page under IO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6193) 			wait_for_stable_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6194) 			ret = VM_FAULT_LOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6195) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6196) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6198) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6199) 	/* OK, we need to fill the hole... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6200) 	if (ext4_should_dioread_nolock(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6201) 		get_block = ext4_get_block_unwritten;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6202) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6203) 		get_block = ext4_get_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6204) retry_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6205) 	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6206) 				    ext4_writepage_trans_blocks(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6207) 	if (IS_ERR(handle)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6208) 		ret = VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6209) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6211) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6212) 	 * Data journalling can't use block_page_mkwrite() because it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6213) 	 * will set_buffer_dirty() before do_journal_get_write_access()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6214) 	 * thus might hit warning messages for dirty metadata buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6215) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6216) 	if (!ext4_should_journal_data(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6217) 		err = block_page_mkwrite(vma, vmf, get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6218) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6219) 		lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6220) 		size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6221) 		/* Page got truncated from under us? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6222) 		if (page->mapping != mapping || page_offset(page) > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6223) 			ret = VM_FAULT_NOPAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6224) 			goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6225) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6227) 		if (page->index == size >> PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6228) 			len = size & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6229) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6230) 			len = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6232) 		err = __block_write_begin(page, 0, len, ext4_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6233) 		if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6234) 			ret = VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6235) 			if (ext4_walk_page_buffers(handle, page_buffers(page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6236) 					0, len, NULL, do_journal_get_write_access))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6237) 				goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6238) 			if (ext4_walk_page_buffers(handle, page_buffers(page),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6239) 					0, len, NULL, write_end_fn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6240) 				goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6241) 			if (ext4_jbd2_inode_add_write(handle, inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6242) 						      page_offset(page), len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6243) 				goto out_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6244) 			ext4_set_inode_state(inode, EXT4_STATE_JDATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6245) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6246) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6247) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6249) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6250) 	if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6251) 		goto retry_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6252) out_ret:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6253) 	ret = block_page_mkwrite_return(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6254) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6255) 	up_read(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6256) 	sb_end_pagefault(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6257) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6258) out_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6259) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6260) 	ext4_journal_stop(handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6261) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6264) vm_fault_t ext4_filemap_fault(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6266) 	struct inode *inode = file_inode(vmf->vma->vm_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6267) 	vm_fault_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6269) 	down_read(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6270) 	ret = filemap_fault(vmf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6271) 	up_read(&EXT4_I(inode)->i_mmap_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6273) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6274) }