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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  linux/fs/fat/file.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Written 1992,1993 by Werner Almesberger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  regular file handling primitives for fat-based filesystems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/fsnotify.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/falloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "fat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static long fat_fallocate(struct file *file, int mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 			  loff_t offset, loff_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	u32 attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	inode_lock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	attr = fat_make_attrs(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	inode_unlock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	return put_user(attr, user_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int is_dir = S_ISDIR(inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	u32 attr, oldattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct iattr ia;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	err = get_user(attr, user_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	err = mnt_want_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	 * prevents the user from turning us into a VFAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	 * longname entry.  Also, we obviously can't set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 * any of the NTFS attributes in the high 24 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	/* Merge in ATTR_VOLUME and ATTR_DIR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		(is_dir ? ATTR_DIR : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	oldattr = fat_make_attrs(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	/* Equivalent to a chmod() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	ia.ia_valid = ATTR_MODE | ATTR_CTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	ia.ia_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	if (is_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		ia.ia_mode = fat_make_mode(sbi, attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
^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) 	/* The root directory has no attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		goto out_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (sbi->options.sys_immutable &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	    ((attr | oldattr) & ATTR_SYS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	    !capable(CAP_LINUX_IMMUTABLE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		goto out_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	 * The security check is questionable...  We single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	 * out the RO attribute for checking by the security
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	 * module, just because it maps to a file mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	err = security_inode_setattr(file->f_path.dentry, &ia);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		goto out_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	/* This MUST be done before doing anything irreversible... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	err = fat_setattr(file->f_path.dentry, &ia);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		goto out_unlock_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	fsnotify_change(file->f_path.dentry, ia.ia_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (sbi->options.sys_immutable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		if (attr & ATTR_SYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			inode->i_flags |= S_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			inode->i_flags &= ~S_IMMUTABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	fat_save_attrs(inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) out_unlock_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	mnt_drop_write_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return put_user(sbi->vol_id, user_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct fstrim_range __user *user_range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct fstrim_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct request_queue *q = bdev_get_queue(sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (!blk_queue_discard(q))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	user_range = (struct fstrim_range __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (copy_from_user(&range, user_range, sizeof(range)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	range.minlen = max_t(unsigned int, range.minlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			     q->limits.discard_granularity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	err = fat_trim_fs(inode, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (copy_to_user(user_range, &range, sizeof(range)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	u32 __user *user_attr = (u32 __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	case FAT_IOCTL_GET_ATTRIBUTES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		return fat_ioctl_get_attributes(inode, user_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	case FAT_IOCTL_SET_ATTRIBUTES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		return fat_ioctl_set_attributes(filp, user_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	case FAT_IOCTL_GET_VOLUME_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		return fat_ioctl_get_volume_id(inode, user_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	case FITRIM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		return fat_ioctl_fitrim(inode, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		return -ENOTTY;	/* Inappropriate ioctl for device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int fat_file_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if ((filp->f_mode & FMODE_WRITE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	     MSDOS_SB(inode->i_sb)->options.flush) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		fat_flush_inodes(inode->i_sb, inode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		congestion_wait(BLK_RW_ASYNC, HZ/10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct inode *inode = filp->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	err = __generic_file_fsync(filp, start, end, datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) const struct file_operations fat_file_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	.llseek		= generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	.read_iter	= generic_file_read_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	.write_iter	= generic_file_write_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	.mmap		= generic_file_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	.release	= fat_file_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	.unlocked_ioctl	= fat_generic_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	.compat_ioctl	= compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	.fsync		= fat_file_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	.splice_read	= generic_file_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	.splice_write	= iter_file_splice_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	.fallocate	= fat_fallocate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int fat_cont_expand(struct inode *inode, loff_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct address_space *mapping = inode->i_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	loff_t start = inode->i_size, count = size - inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	err = generic_cont_expand_simple(inode, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (IS_SYNC(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		int err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		 * Opencode syncing since we don't have a file open to use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		 * standard fsync path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		err = filemap_fdatawrite_range(mapping, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 					       start + count - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		err2 = sync_mapping_buffers(mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			err = err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		err2 = write_inode_now(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			err = err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			err =  filemap_fdatawait_range(mapping, start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 						       start + count - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return err;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  * Preallocate space for a file. This implements fat's fallocate file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  * operation, which gets called from sys_fallocate system call. User
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  * space requests len bytes at offset. If FALLOC_FL_KEEP_SIZE is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  * we just allocate clusters without zeroing them out. Otherwise we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  * allocate and zero out clusters via an expanding truncate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static long fat_fallocate(struct file *file, int mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			  loff_t offset, loff_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	int nr_cluster; /* Number of clusters to be allocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	loff_t mm_bytes; /* Number of bytes to be allocated for file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	loff_t ondisksize; /* block aligned on-disk size in bytes*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	/* No support for hole punch or other fallocate flags. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (mode & ~FALLOC_FL_KEEP_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	/* No support for dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (!S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (mode & FALLOC_FL_KEEP_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		ondisksize = inode->i_blocks << 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		if ((offset + len) <= ondisksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		/* First compute the number of clusters to be allocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		mm_bytes = offset + len - ondisksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		nr_cluster = (mm_bytes + (sbi->cluster_size - 1)) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			sbi->cluster_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		/* Start the allocation.We are not zeroing out the clusters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		while (nr_cluster-- > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			err = fat_add_cluster(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		if ((offset + len) <= i_size_read(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		/* This is just an expanding truncate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		err = fat_cont_expand(inode, (offset + len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /* Free all clusters after the skip'th cluster. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static int fat_free(struct inode *inode, int skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	int err, wait, free_start, i_start, i_logstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (MSDOS_I(inode)->i_start == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	fat_cache_inval_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	wait = IS_DIRSYNC(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	i_start = free_start = MSDOS_I(inode)->i_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	i_logstart = MSDOS_I(inode)->i_logstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	/* First, we write the new file size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (!skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		MSDOS_I(inode)->i_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		MSDOS_I(inode)->i_logstart = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (wait) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		err = fat_sync_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			MSDOS_I(inode)->i_start = i_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			MSDOS_I(inode)->i_logstart = i_logstart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	/* Write a new EOF, and get the remaining cluster chain for freeing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		struct fat_entry fatent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		int ret, fclus, dclus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		else if (ret == FAT_ENT_EOF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		fatent_init(&fatent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		ret = fat_ent_read(inode, &fatent, dclus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		if (ret == FAT_ENT_EOF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			fatent_brelse(&fatent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		} else if (ret == FAT_ENT_FREE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			fat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 				     "%s: invalid cluster chain (i_pos %lld)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 				     __func__, MSDOS_I(inode)->i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		} else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 				ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		fatent_brelse(&fatent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		free_start = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	/* Freeing the remained cluster chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	return fat_free_clusters(inode, free_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) void fat_truncate_blocks(struct inode *inode, loff_t offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	const unsigned int cluster_size = sbi->cluster_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	int nr_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	 * This protects against truncating a file bigger than it was then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	 * trying to write into the hole.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if (MSDOS_I(inode)->mmu_private > offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		MSDOS_I(inode)->mmu_private = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	fat_free(inode, nr_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	fat_flush_inodes(inode->i_sb, inode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int fat_getattr(const struct path *path, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		u32 request_mask, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	struct inode *inode = d_inode(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	generic_fillattr(inode, stat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	stat->blksize = MSDOS_SB(inode->i_sb)->cluster_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (MSDOS_SB(inode->i_sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		/* Use i_pos for ino. This is used as fileid of nfs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		stat->ino = fat_i_pos_read(MSDOS_SB(inode->i_sb), inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) EXPORT_SYMBOL_GPL(fat_getattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			     struct inode *inode, umode_t *mode_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	umode_t mask, perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	 * Note, the basic check is already done by a caller of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	 * (attr->ia_mode & ~FAT_VALID_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		mask = sbi->options.fs_fmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		mask = sbi->options.fs_dmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	perm = *mode_ptr & ~(S_IFMT | mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	 * Of the r and x bits, all (subject to umask) must be present. Of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	 * w bits, either all (subject to umask) or none must be present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	 * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (fat_mode_can_hold_ro(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	*mode_ptr &= S_IFMT | perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) static int fat_allow_set_time(struct msdos_sb_info *sbi, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	umode_t allow_utime = sbi->options.allow_utime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	if (!uid_eq(current_fsuid(), inode->i_uid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		if (in_group_p(inode->i_gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			allow_utime >>= 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		if (allow_utime & MAY_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	/* use a default check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) #define TIMES_SET_FLAGS	(ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /* valid file mode bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) #define FAT_VALID_MODE	(S_IFREG | S_IFDIR | S_IRWXUGO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) int fat_setattr(struct dentry *dentry, struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	unsigned int ia_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	/* Check for setting the inode time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	ia_valid = attr->ia_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (ia_valid & TIMES_SET_FLAGS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		if (fat_allow_set_time(sbi, inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			attr->ia_valid &= ~TIMES_SET_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	error = setattr_prepare(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	attr->ia_valid = ia_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		if (sbi->options.quiet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	 * Expand the file. Since inode_setattr() updates ->i_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	 * before calling the ->truncate(), but FAT needs to fill the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	 * hole before it. XXX: this is no longer true with new truncate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	 * sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	if (attr->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		inode_dio_wait(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		if (attr->ia_size > inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 			error = fat_cont_expand(inode, attr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			if (error || attr->ia_valid == ATTR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			attr->ia_valid &= ~ATTR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	if (((attr->ia_valid & ATTR_UID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	     (!uid_eq(attr->ia_uid, sbi->options.fs_uid))) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	    ((attr->ia_valid & ATTR_GID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	     (!gid_eq(attr->ia_gid, sbi->options.fs_gid))) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	    ((attr->ia_valid & ATTR_MODE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	     (attr->ia_mode & ~FAT_VALID_MODE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		error = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		if (sbi->options.quiet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	 * We don't return -EPERM here. Yes, strange, but this is too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	 * old behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	if (attr->ia_valid & ATTR_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			attr->ia_valid &= ~ATTR_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	if (attr->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		error = fat_block_truncate_page(inode, attr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		down_write(&MSDOS_I(inode)->truncate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		truncate_setsize(inode, attr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		fat_truncate_blocks(inode, attr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		up_write(&MSDOS_I(inode)->truncate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	 * setattr_copy can't truncate these appropriately, so we'll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	 * copy them ourselves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (attr->ia_valid & ATTR_ATIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		fat_truncate_time(inode, &attr->ia_atime, S_ATIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	if (attr->ia_valid & ATTR_CTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		fat_truncate_time(inode, &attr->ia_ctime, S_CTIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	if (attr->ia_valid & ATTR_MTIME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		fat_truncate_time(inode, &attr->ia_mtime, S_MTIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	attr->ia_valid &= ~(ATTR_ATIME|ATTR_CTIME|ATTR_MTIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	setattr_copy(inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) EXPORT_SYMBOL_GPL(fat_setattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) const struct inode_operations fat_file_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	.setattr	= fat_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	.getattr	= fat_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	.update_time	= fat_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) };