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)  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include "xfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include "xfs_acl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "xfs_quota.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include "xfs_attr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "xfs_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "xfs_icache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "xfs_symlink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "xfs_dir2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "xfs_iomap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "xfs_error.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/posix_acl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/fiemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  * Directories have different lock order w.r.t. mmap_lock compared to regular
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32)  * files. This is due to readdir potentially triggering page faults on a user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33)  * buffer inside filldir(), and this happens with the ilock on the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34)  * held. For regular files, the lock order is the other way around - the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35)  * mmap_lock is taken during the page fault, and then we lock the ilock to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36)  * block mapping. Hence we need a different class for the directory ilock so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37)  * that lockdep can tell them apart.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) static struct lock_class_key xfs_nondir_ilock_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) static struct lock_class_key xfs_dir_ilock_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) xfs_initxattrs(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	struct inode		*inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	const struct xattr	*xattr_array,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	void			*fs_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	const struct xattr	*xattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	struct xfs_inode	*ip = XFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 		struct xfs_da_args	args = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 			.dp		= ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 			.attr_filter	= XFS_ATTR_SECURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 			.name		= xattr->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 			.namelen	= strlen(xattr->name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 			.value		= xattr->value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 			.valuelen	= xattr->value_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 		error = xfs_attr_set(&args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * Hook in SELinux.  This is not quite correct yet, what we really need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * here (as we do for default ACLs) is a mechanism by which creation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * these attrs can be journalled at inode creation time (along with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * inode, of course, such that log replay can't cause these to be lost).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) xfs_init_security(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	struct inode	*inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	const struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	return security_inode_init_security(inode, dir, qstr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 					     &xfs_initxattrs, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) xfs_dentry_to_name(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	struct xfs_name	*namep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	struct dentry	*dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	namep->name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	namep->len = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	namep->type = XFS_DIR3_FT_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) xfs_dentry_mode_to_name(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	struct xfs_name	*namep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	struct dentry	*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	int		mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	namep->name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	namep->len = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	namep->type = xfs_mode_to_ftype(mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	if (unlikely(namep->type == XFS_DIR3_FT_UNKNOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) xfs_cleanup_inode(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	struct inode	*inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	struct dentry	*dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	struct xfs_name	teardown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	/* Oh, the horror.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	 * If we can't add the ACL or we fail in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	 * xfs_init_security we must back out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	 * ENOSPC can hit here, among other things.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	xfs_dentry_to_name(&teardown, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) xfs_generic_create(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	struct dentry	*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	umode_t		mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	dev_t		rdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	bool		tmpfile)	/* unnamed file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	struct inode	*inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	struct xfs_inode *ip = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	struct posix_acl *default_acl, *acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	struct xfs_name	name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	int		error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	 * Irix uses Missed'em'V split, but doesn't want to see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	 * the upper 5 bits of (14bit) major.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	if (S_ISCHR(mode) || S_ISBLK(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 		rdev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	error = posix_acl_create(dir, &mode, &default_acl, &acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	/* Verify mode is valid also for tmpfile case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	error = xfs_dentry_mode_to_name(&name, dentry, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		goto out_free_acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	if (!tmpfile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		error = xfs_create_tmpfile(XFS_I(dir), mode, &ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		goto out_free_acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	inode = VFS_I(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	error = xfs_init_security(inode, dir, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		goto out_cleanup_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) #ifdef CONFIG_XFS_POSIX_ACL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	if (default_acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		error = __xfs_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 			goto out_cleanup_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	if (acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		error = __xfs_set_acl(inode, acl, ACL_TYPE_ACCESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 			goto out_cleanup_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	xfs_setup_iops(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	if (tmpfile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		 * The VFS requires that any inode fed to d_tmpfile must have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		 * nlink == 1 so that it can decrement the nlink in d_tmpfile.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		 * However, we created the temp file with nlink == 0 because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		 * we're not allowed to put an inode with nlink > 0 on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		 * unlinked list.  Therefore we have to set nlink to 1 so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		 * d_tmpfile can immediately set it back to zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		set_nlink(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		d_tmpfile(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	xfs_finish_inode_setup(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  out_free_acl:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	if (default_acl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		posix_acl_release(default_acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	if (acl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		posix_acl_release(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  out_cleanup_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	xfs_finish_inode_setup(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	if (!tmpfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		xfs_cleanup_inode(dir, inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	xfs_irele(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	goto out_free_acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) xfs_vn_mknod(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	struct dentry	*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	umode_t		mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	dev_t		rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	return xfs_generic_create(dir, dentry, mode, rdev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) xfs_vn_create(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	struct dentry	*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	umode_t		mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	bool		flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	return xfs_generic_create(dir, dentry, mode, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) xfs_vn_mkdir(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	struct dentry	*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	umode_t		mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	return xfs_generic_create(dir, dentry, mode | S_IFDIR, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) STATIC struct dentry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) xfs_vn_lookup(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	struct dentry	*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	struct xfs_inode *cip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	struct xfs_name	name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	int		error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	if (dentry->d_name.len >= MAXNAMELEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		return ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	xfs_dentry_to_name(&name, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	if (likely(!error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		inode = VFS_I(cip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	else if (likely(error == -ENOENT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		inode = ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) STATIC struct dentry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) xfs_vn_ci_lookup(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	struct dentry	*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	struct xfs_inode *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	struct xfs_name	xname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	struct xfs_name ci_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	struct qstr	dname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	int		error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	if (dentry->d_name.len >= MAXNAMELEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		return ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	xfs_dentry_to_name(&xname, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	if (unlikely(error)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		if (unlikely(error != -ENOENT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 			return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		 * call d_add(dentry, NULL) here when d_drop_negative_children
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		 * is called in xfs_vn_mknod (ie. allow negative dentries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		 * with CI filesystems).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	/* if exact match, just splice and exit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	if (!ci_name.name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		return d_splice_alias(VFS_I(ip), dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	/* else case-insensitive match... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	dname.name = ci_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	dname.len = ci_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	dentry = d_add_ci(dentry, VFS_I(ip), &dname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	kmem_free(ci_name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) xfs_vn_link(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	struct dentry	*old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	struct dentry	*dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	struct inode	*inode = d_inode(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	struct xfs_name	name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	int		error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	error = xfs_dentry_mode_to_name(&name, dentry, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	ihold(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) xfs_vn_unlink(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	struct dentry	*dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	struct xfs_name	name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	int		error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	xfs_dentry_to_name(&name, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	error = xfs_remove(XFS_I(dir), &name, XFS_I(d_inode(dentry)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	 * With unlink, the VFS makes the dentry "negative": no inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	 * but still hashed. This is incompatible with case-insensitive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	 * mode, so invalidate (unhash) the dentry in CI-mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		d_invalidate(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) xfs_vn_symlink(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	struct dentry	*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	const char	*symname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	struct inode	*inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	struct xfs_inode *cip = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	struct xfs_name	name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	int		error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	umode_t		mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	mode = S_IFLNK |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		(irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	error = xfs_dentry_mode_to_name(&name, dentry, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	inode = VFS_I(cip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	error = xfs_init_security(inode, dir, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		goto out_cleanup_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	xfs_setup_iops(cip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	xfs_finish_inode_setup(cip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  out_cleanup_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	xfs_finish_inode_setup(cip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	xfs_cleanup_inode(dir, inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	xfs_irele(cip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) xfs_vn_rename(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	struct inode	*odir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	struct dentry	*odentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	struct inode	*ndir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	struct dentry	*ndentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	unsigned int	flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	struct inode	*new_inode = d_inode(ndentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	int		omode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	int		error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	struct xfs_name	oname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	struct xfs_name	nname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	/* if we are exchanging files, we need to set i_mode of both files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	if (flags & RENAME_EXCHANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		omode = d_inode(ndentry)->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	error = xfs_dentry_mode_to_name(&oname, odentry, omode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	if (omode && unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	error = xfs_dentry_mode_to_name(&nname, ndentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 					d_inode(odentry)->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	return xfs_rename(XFS_I(odir), &oname, XFS_I(d_inode(odentry)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			  XFS_I(ndir), &nname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 			  new_inode ? XFS_I(new_inode) : NULL, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442)  * careful here - this function can get called recursively, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443)  * we need to be very careful about how much stack we use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444)  * uio is kmalloced for this reason...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) STATIC const char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) xfs_vn_get_link(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	struct dentry		*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	struct inode		*inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	struct delayed_call	*done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	char			*link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	int			error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		return ERR_PTR(-ECHILD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	link = kmalloc(XFS_SYMLINK_MAXLEN+1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	if (!link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	error = xfs_readlink(XFS_I(d_inode(dentry)), link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	if (unlikely(error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		goto out_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	set_delayed_call(done, kfree_link, link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	return link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469)  out_kfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	kfree(link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471)  out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) STATIC const char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) xfs_vn_get_link_inline(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	struct dentry		*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	struct inode		*inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	struct delayed_call	*done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	struct xfs_inode	*ip = XFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	char			*link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	ASSERT(ip->i_df.if_flags & XFS_IFINLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	 * The VFS crashes on a NULL pointer, so return -EFSCORRUPTED if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	 * if_data is junk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	link = ip->i_df.if_u1.if_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	if (XFS_IS_CORRUPT(ip->i_mount, !link))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 		return ERR_PTR(-EFSCORRUPTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	return link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) static uint32_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) xfs_stat_blksize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	 * If the file blocks are being allocated from a realtime volume, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	 * always return the realtime extent size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	if (XFS_IS_REALTIME_INODE(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		return xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	 * Allow large block sizes to be reported to userspace programs if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	 * "largeio" mount option is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	 * If compatibility mode is specified, simply return the basic unit of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	 * caching so that we don't get inefficient read/modify/write I/O from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	 * user apps. Otherwise....
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	 * If the underlying volume is a stripe, then return the stripe width in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	 * bytes as the recommended I/O size. It is not a stripe and we've set a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	 * default buffered I/O size, return that, otherwise return the compat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	 * default.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	if (mp->m_flags & XFS_MOUNT_LARGEIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		if (mp->m_swidth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 			return mp->m_swidth << mp->m_sb.sb_blocklog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		if (mp->m_flags & XFS_MOUNT_ALLOCSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 			return 1U << mp->m_allocsize_log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	return PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) xfs_vn_getattr(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	const struct path	*path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	struct kstat		*stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	u32			request_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	unsigned int		query_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	struct inode		*inode = d_inode(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	struct xfs_inode	*ip = XFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	trace_xfs_getattr(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	if (XFS_FORCED_SHUTDOWN(mp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	stat->size = XFS_ISIZE(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	stat->dev = inode->i_sb->s_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	stat->mode = inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	stat->nlink = inode->i_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	stat->uid = inode->i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	stat->gid = inode->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	stat->ino = ip->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	stat->atime = inode->i_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	stat->mtime = inode->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	stat->ctime = inode->i_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	stat->blocks =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	if (xfs_sb_version_has_v3inode(&mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		if (request_mask & STATX_BTIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 			stat->result_mask |= STATX_BTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 			stat->btime = ip->i_d.di_crtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	 * Note: If you add another clause to set an attribute flag, please
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	 * update attributes_mask below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		stat->attributes |= STATX_ATTR_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		stat->attributes |= STATX_ATTR_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	if (ip->i_d.di_flags & XFS_DIFLAG_NODUMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		stat->attributes |= STATX_ATTR_NODUMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	stat->attributes_mask |= (STATX_ATTR_IMMUTABLE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 				  STATX_ATTR_APPEND |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 				  STATX_ATTR_NODUMP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	switch (inode->i_mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	case S_IFBLK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	case S_IFCHR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		stat->blksize = BLKDEV_IOSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		stat->rdev = inode->i_rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		stat->blksize = xfs_stat_blksize(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		stat->rdev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) xfs_setattr_mode(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	struct iattr		*iattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	struct inode		*inode = VFS_I(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	umode_t			mode = iattr->ia_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	inode->i_mode &= S_IFMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	inode->i_mode |= mode & ~S_IFMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) xfs_setattr_time(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	struct iattr		*iattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	struct inode		*inode = VFS_I(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	if (iattr->ia_valid & ATTR_ATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		inode->i_atime = iattr->ia_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	if (iattr->ia_valid & ATTR_CTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		inode->i_ctime = iattr->ia_ctime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	if (iattr->ia_valid & ATTR_MTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		inode->i_mtime = iattr->ia_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) xfs_vn_change_ok(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	struct dentry	*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	struct iattr	*iattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	struct xfs_mount	*mp = XFS_I(d_inode(dentry))->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	if (mp->m_flags & XFS_MOUNT_RDONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	if (XFS_FORCED_SHUTDOWN(mp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	return setattr_prepare(dentry, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646)  * Set non-size attributes of an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648)  * Caution: The caller of this function is responsible for calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649)  * setattr_prepare() or otherwise verifying the change is fine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) xfs_setattr_nonsize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	struct iattr		*iattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	int			flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	xfs_mount_t		*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	struct inode		*inode = VFS_I(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	int			mask = iattr->ia_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	xfs_trans_t		*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	kuid_t			uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	kgid_t			gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	struct xfs_dquot	*udqp = NULL, *gdqp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	struct xfs_dquot	*olddquot1 = NULL, *olddquot2 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	ASSERT((mask & ATTR_SIZE) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	 * If disk quotas is on, we make sure that the dquots do exist on disk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	 * before we start any other transactions. Trying to do this later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	 * is messy. We don't care to take a readlock to look at the ids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	 * in inode here, because we can't hold it across the trans_reserve.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	 * If the IDs do change before we take the ilock, we're covered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	 * because the i_*dquot fields will get updated anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		uint	qflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 			uid = iattr->ia_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 			qflags |= XFS_QMOPT_UQUOTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 			uid = inode->i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 			gid = iattr->ia_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 			qflags |= XFS_QMOPT_GQUOTA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		}  else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 			gid = inode->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		 * We take a reference when we initialize udqp and gdqp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		 * so it is important that we never blindly double trip on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		 * the same variable. See xfs_create() for an example.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		ASSERT(udqp == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		ASSERT(gdqp == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		error = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 					   qflags, &udqp, &gdqp, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		goto out_dqrele;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	xfs_trans_ijoin(tp, ip, 0);
^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) 	 * Change file ownership.  Must be the owner or privileged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	if (mask & (ATTR_UID|ATTR_GID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		 * These IDs could have changed since we last looked at them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		 * But, we're assured that if the ownership did change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		 * while we didn't have the inode locked, inode's dquot(s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		 * would have changed also.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		iuid = inode->i_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		igid = inode->i_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		 * Do a quota reservation only if uid/gid is actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		 * going to change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		if (XFS_IS_QUOTA_RUNNING(mp) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		    ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		     (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 			ASSERT(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 						NULL, capable(CAP_FOWNER) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 						XFS_QMOPT_FORCE_RES : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 			if (error)	/* out of quota */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 				goto out_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		 * CAP_FSETID overrides the following restrictions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		 * The set-user-ID and set-group-ID bits of a file will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		 * cleared upon successful return from chown()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		if ((inode->i_mode & (S_ISUID|S_ISGID)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 		    !capable(CAP_FSETID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 			inode->i_mode &= ~(S_ISUID|S_ISGID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		 * Change the ownerships and register quota modifications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		 * in the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		if (!uid_eq(iuid, uid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 			if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 				ASSERT(mask & ATTR_UID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 				ASSERT(udqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 				olddquot1 = xfs_qm_vop_chown(tp, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 							&ip->i_udquot, udqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 			inode->i_uid = uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		if (!gid_eq(igid, gid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 			if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 				ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 				       !XFS_IS_PQUOTA_ON(mp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 				ASSERT(mask & ATTR_GID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 				ASSERT(gdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 				olddquot2 = xfs_qm_vop_chown(tp, ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 							&ip->i_gdquot, gdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 			inode->i_gid = gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	if (mask & ATTR_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		xfs_setattr_mode(ip, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		xfs_setattr_time(ip, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	XFS_STATS_INC(mp, xs_ig_attrchg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (mp->m_flags & XFS_MOUNT_WSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		xfs_trans_set_sync(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	 * Release any dquot(s) the inode had kept before chown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	xfs_qm_dqrele(olddquot1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	xfs_qm_dqrele(olddquot2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	xfs_qm_dqrele(udqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	xfs_qm_dqrele(gdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	 * 	     update.  We could avoid this with linked transactions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	 * 	     and passing down the transaction pointer all the way
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	 *	     to attr_set.  No previous user of the generic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	 * 	     Posix ACL code seems to care about this issue either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		error = posix_acl_chmod(inode, inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) out_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	xfs_iunlock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) out_dqrele:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	xfs_qm_dqrele(udqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	xfs_qm_dqrele(gdqp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) xfs_vn_setattr_nonsize(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	struct dentry		*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	struct iattr		*iattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	struct xfs_inode	*ip = XFS_I(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	trace_xfs_setattr(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	error = xfs_vn_change_ok(dentry, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	return xfs_setattr_nonsize(ip, iattr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846)  * Truncate file.  Must have write permission and not be a directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848)  * Caution: The caller of this function is responsible for calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849)  * setattr_prepare() or otherwise verifying the change is fine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) xfs_setattr_size(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	struct iattr		*iattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	struct inode		*inode = VFS_I(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	xfs_off_t		oldsize, newsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	uint			lock_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	bool			did_zeroing = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	ASSERT(S_ISREG(inode->i_mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	ASSERT((iattr->ia_valid & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		ATTR_MTIME_SET|ATTR_TIMES_SET)) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	oldsize = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	newsize = iattr->ia_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	 * Short circuit the truncate case for zero length files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	if (newsize == 0 && oldsize == 0 && ip->i_df.if_nextents == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		if (!(iattr->ia_valid & (ATTR_CTIME|ATTR_MTIME)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		 * Use the regular setattr path to update the timestamps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		iattr->ia_valid &= ~ATTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		return xfs_setattr_nonsize(ip, iattr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	 * Make sure that the dquots are attached to the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	error = xfs_qm_dqattach(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		return error;
^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) 	 * Wait for all direct I/O to complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	inode_dio_wait(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	 * File data changes must be complete before we start the transaction to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	 * modify the inode.  This needs to be done before joining the inode to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	 * the transaction because the inode cannot be unlocked once it is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	 * part of the transaction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	 * Start with zeroing any data beyond EOF that we may expose on file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	 * extension, or zeroing out the rest of the block on a downward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	 * truncate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	if (newsize > oldsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		trace_xfs_zero_eof(ip, oldsize, newsize - oldsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		error = iomap_zero_range(inode, oldsize, newsize - oldsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 				&did_zeroing, &xfs_buffered_write_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		 * iomap won't detect a dirty page over an unwritten block (or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		 * cow block over a hole) and subsequently skips zeroing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		 * newly post-EOF portion of the page. Flush the new EOF to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		 * convert the block before the pagecache truncate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		error = filemap_write_and_wait_range(inode->i_mapping, newsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 						     newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		error = iomap_truncate_page(inode, newsize, &did_zeroing,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 				&xfs_buffered_write_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	 * We've already locked out new page faults, so now we can safely remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	 * pages from the page cache knowing they won't get refaulted until we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	 * drop the XFS_MMAP_EXCL lock after the extent manipulations are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	 * complete. The truncate_setsize() call also cleans partial EOF page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	 * PTEs on extending truncates and hence ensures sub-page block size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	 * filesystems are correctly handled, too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	 * We have to do all the page cache truncate work outside the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	 * transaction context as the "lock" order is page lock->log space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	 * reservation as defined by extent allocation in the writeback path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	 * Hence a truncate can fail with ENOMEM from xfs_trans_alloc(), but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	 * having already truncated the in-memory version of the file (i.e. made
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	 * user visible changes). There's not much we can do about this, except
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	 * to hope that the caller sees ENOMEM and retries the truncate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	 * operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	 * And we update in-core i_size and truncate page cache beyond newsize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	 * before writeback the [di_size, newsize] range, so we're guaranteed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	 * not to write stale data past the new EOF on truncate down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	truncate_setsize(inode, newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	 * We are going to log the inode size change in this transaction so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	 * any previous writes that are beyond the on disk EOF and the new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	 * EOF that have not been written out need to be written here.  If we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	 * do not write the data out, we expose ourselves to the null files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	 * problem. Note that this includes any block zeroing we did above;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	 * otherwise those blocks may not be zeroed after a crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	if (did_zeroing ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	    (newsize > ip->i_d.di_size && oldsize != ip->i_d.di_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 						ip->i_d.di_size, newsize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	lock_flags |= XFS_ILOCK_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	xfs_trans_ijoin(tp, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	 * Only change the c/mtime if we are changing the size or we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	 * explicitly asked to change it.  This handles the semantic difference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	 * between truncate() and ftruncate() as implemented in the VFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	 * special case where we need to update the times despite not having
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	 * these flags set.  For all other operations the VFS set these flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	 * explicitly if it wants a timestamp update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	if (newsize != oldsize &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	    !(iattr->ia_valid & (ATTR_CTIME | ATTR_MTIME))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		iattr->ia_ctime = iattr->ia_mtime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	 * The first thing we do is set the size to new_size permanently on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	 * disk.  This way we don't have to worry about anyone ever being able
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	 * to look at the data being freed even in the face of a crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	 * What we're getting around here is the case where we free a block, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	 * is allocated to another file, it is written to, and then we crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	 * If the new data gets written to the file but the log buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	 * containing the free and reallocation don't, then we'd end up with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	 * garbage in the blocks being freed.  As long as we make the new size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	 * permanent before actually freeing any blocks it doesn't matter if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	 * they get written to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	ip->i_d.di_size = newsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	if (newsize <= oldsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			goto out_trans_cancel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		 * Truncated "down", so we're removing references to old data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		 * here - if we delay flushing for a long time, we expose
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		 * ourselves unduly to the notorious NULL files problem.  So,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		 * we mark this inode and flush it when the file is closed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		 * and do not wait the usual (long) time for writeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		xfs_iflags_set(ip, XFS_ITRUNCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 		/* A truncate down always removes post-EOF blocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 		xfs_inode_clear_eofblocks_tag(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	if (iattr->ia_valid & ATTR_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		xfs_setattr_mode(ip, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	if (iattr->ia_valid & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		xfs_setattr_time(ip, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	XFS_STATS_INC(mp, xs_ig_attrchg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	if (mp->m_flags & XFS_MOUNT_WSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		xfs_trans_set_sync(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	error = xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	if (lock_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		xfs_iunlock(ip, lock_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) out_trans_cancel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	xfs_trans_cancel(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) xfs_vn_setattr_size(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	struct dentry		*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	struct iattr		*iattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	struct xfs_inode	*ip = XFS_I(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	trace_xfs_setattr(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	error = xfs_vn_change_ok(dentry, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	return xfs_setattr_size(ip, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) xfs_vn_setattr(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	struct dentry		*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	struct iattr		*iattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	if (iattr->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		struct inode		*inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		struct xfs_inode	*ip = XFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		uint			iolock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 		xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 			xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		error = xfs_vn_setattr_size(dentry, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		error = xfs_vn_setattr_nonsize(dentry, iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) xfs_vn_update_time(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	struct inode		*inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	struct timespec64	*now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	int			flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	struct xfs_inode	*ip = XFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	int			log_flags = XFS_ILOG_TIMESTAMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	struct xfs_trans	*tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	trace_xfs_update_time(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	if (inode->i_sb->s_flags & SB_LAZYTIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		if (!((flags & S_VERSION) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		      inode_maybe_inc_iversion(inode, false)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 			return generic_update_time(inode, now, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 		/* Capture the iversion update that just occurred */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 		log_flags |= XFS_ILOG_CORE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	xfs_ilock(ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	if (flags & S_CTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		inode->i_ctime = *now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	if (flags & S_MTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		inode->i_mtime = *now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	if (flags & S_ATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		inode->i_atime = *now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	xfs_trans_log_inode(tp, ip, log_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	return xfs_trans_commit(tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) xfs_vn_fiemap(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	struct inode		*inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	struct fiemap_extent_info *fieinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	u64			start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	u64			length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	xfs_ilock(XFS_I(inode), XFS_IOLOCK_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 		fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		error = iomap_fiemap(inode, fieinfo, start, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 				&xfs_xattr_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		error = iomap_fiemap(inode, fieinfo, start, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 				&xfs_read_iomap_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	xfs_iunlock(XFS_I(inode), XFS_IOLOCK_SHARED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) xfs_vn_tmpfile(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	struct inode	*dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	struct dentry	*dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	umode_t		mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	return xfs_generic_create(dir, dentry, mode, 0, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) static const struct inode_operations xfs_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	.get_acl		= xfs_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	.set_acl		= xfs_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	.getattr		= xfs_vn_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	.setattr		= xfs_vn_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	.listxattr		= xfs_vn_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	.fiemap			= xfs_vn_fiemap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	.update_time		= xfs_vn_update_time,
^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) static const struct inode_operations xfs_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	.create			= xfs_vn_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	.lookup			= xfs_vn_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	.link			= xfs_vn_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	.unlink			= xfs_vn_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	.symlink		= xfs_vn_symlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	.mkdir			= xfs_vn_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	 * Yes, XFS uses the same method for rmdir and unlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	 * There are some subtile differences deeper in the code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	 * but we use S_ISDIR to check for those.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	.rmdir			= xfs_vn_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	.mknod			= xfs_vn_mknod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	.rename			= xfs_vn_rename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	.get_acl		= xfs_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	.set_acl		= xfs_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	.getattr		= xfs_vn_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	.setattr		= xfs_vn_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	.listxattr		= xfs_vn_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	.update_time		= xfs_vn_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	.tmpfile		= xfs_vn_tmpfile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) static const struct inode_operations xfs_dir_ci_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	.create			= xfs_vn_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	.lookup			= xfs_vn_ci_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 	.link			= xfs_vn_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	.unlink			= xfs_vn_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	.symlink		= xfs_vn_symlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	.mkdir			= xfs_vn_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	 * Yes, XFS uses the same method for rmdir and unlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	 * There are some subtile differences deeper in the code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	 * but we use S_ISDIR to check for those.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	.rmdir			= xfs_vn_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	.mknod			= xfs_vn_mknod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	.rename			= xfs_vn_rename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	.get_acl		= xfs_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	.set_acl		= xfs_set_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	.getattr		= xfs_vn_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	.setattr		= xfs_vn_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	.listxattr		= xfs_vn_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	.update_time		= xfs_vn_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	.tmpfile		= xfs_vn_tmpfile,
^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) static const struct inode_operations xfs_symlink_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	.get_link		= xfs_vn_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	.getattr		= xfs_vn_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	.setattr		= xfs_vn_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	.listxattr		= xfs_vn_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	.update_time		= xfs_vn_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) static const struct inode_operations xfs_inline_symlink_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	.get_link		= xfs_vn_get_link_inline,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	.getattr		= xfs_vn_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	.setattr		= xfs_vn_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	.listxattr		= xfs_vn_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	.update_time		= xfs_vn_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) /* Figure out if this file actually supports DAX. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) xfs_inode_supports_dax(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	struct xfs_mount	*mp = ip->i_mount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	/* Only supported on regular files. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	if (!S_ISREG(VFS_I(ip)->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	/* Only supported on non-reflinked files. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	if (xfs_is_reflink_inode(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	/* Block size must match page size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	if (mp->m_sb.sb_blocksize != PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	/* Device has to support DAX too. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	return xfs_inode_buftarg(ip)->bt_daxdev != NULL;
^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) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) xfs_inode_should_enable_dax(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	struct xfs_inode *ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	if (!IS_ENABLED(CONFIG_FS_DAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	if (ip->i_mount->m_flags & XFS_MOUNT_DAX_NEVER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	if (!xfs_inode_supports_dax(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	if (ip->i_mount->m_flags & XFS_MOUNT_DAX_ALWAYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	if (ip->i_d.di_flags2 & XFS_DIFLAG2_DAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) xfs_diflags_to_iflags(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	bool init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	struct inode            *inode = VFS_I(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	unsigned int            xflags = xfs_ip2xflags(ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	unsigned int            flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	ASSERT(!(IS_DAX(inode) && init));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	if (xflags & FS_XFLAG_IMMUTABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 		flags |= S_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	if (xflags & FS_XFLAG_APPEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		flags |= S_APPEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	if (xflags & FS_XFLAG_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 		flags |= S_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	if (xflags & FS_XFLAG_NOATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		flags |= S_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	if (init && xfs_inode_should_enable_dax(ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		flags |= S_DAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	 * S_DAX can only be set during inode initialization and is never set by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	 * the VFS, so we cannot mask off S_DAX in i_flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	inode->i_flags &= ~(S_IMMUTABLE | S_APPEND | S_SYNC | S_NOATIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	inode->i_flags |= flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)  * Initialize the Linux inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318)  * When reading existing inodes from disk this is called directly from xfs_iget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319)  * when creating a new inode it is called from xfs_ialloc after setting up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)  * inode. These callers have different criteria for clearing XFS_INEW, so leave
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321)  * it up to the caller to deal with unlocking the inode appropriately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) xfs_setup_inode(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	struct inode		*inode = &ip->i_vnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	gfp_t			gfp_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 	inode->i_ino = ip->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	inode->i_state = I_NEW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	inode_sb_list_add(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	/* make the inode look hashed for the writeback code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	inode_fake_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	i_size_write(inode, ip->i_d.di_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	xfs_diflags_to_iflags(ip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		 * We set the i_rwsem class here to avoid potential races with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		 * lockdep_annotate_inode_mutex_key() reinitialising the lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		 * after a filehandle lookup has already found the inode in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		 * cache before it has been unlocked via unlock_new_inode().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		lockdep_set_class(&inode->i_rwsem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 				  &inode->i_sb->s_type->i_mutex_dir_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		lockdep_set_class(&ip->i_lock.mr_lock, &xfs_dir_ilock_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		lockdep_set_class(&ip->i_lock.mr_lock, &xfs_nondir_ilock_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	 * Ensure all page cache allocations are done from GFP_NOFS context to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	 * prevent direct reclaim recursion back into the filesystem and blowing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	 * stacks or deadlocking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	gfp_mask = mapping_gfp_mask(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	 * If there is no attribute fork no ACL can exist on this inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	 * and it can't have any file capabilities attached to it either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	if (!XFS_IFORK_Q(ip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		inode_has_no_xattr(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 		cache_no_acl(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) xfs_setup_iops(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 	struct inode		*inode = &ip->i_vnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	switch (inode->i_mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		inode->i_op = &xfs_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		inode->i_fop = &xfs_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		if (IS_DAX(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			inode->i_mapping->a_ops = &xfs_dax_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 			inode->i_mapping->a_ops = &xfs_address_space_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 			inode->i_op = &xfs_dir_ci_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 			inode->i_op = &xfs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		inode->i_fop = &xfs_dir_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	case S_IFLNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		if (ip->i_df.if_flags & XFS_IFINLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 			inode->i_op = &xfs_inline_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 			inode->i_op = &xfs_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		inode->i_op = &xfs_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }