Orange Pi5 kernel

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

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * linux/fs/ext2/ioctl.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 1993, 1994, 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Remy Card (card@masi.ibp.fr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Laboratoire MASI - Institut Blaise Pascal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Universite Pierre et Marie Curie (Paris VI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "ext2.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <asm/current.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct ext2_inode_info *ei = EXT2_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	unsigned short rsv_window_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	case EXT2_IOC_GETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		flags = ei->i_flags & EXT2_FL_USER_VISIBLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		return put_user(flags, (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	case EXT2_IOC_SETFLAGS: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		unsigned int oldflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		ret = mnt_want_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		if (!inode_owner_or_capable(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 			ret = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 			goto setflags_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		if (get_user(flags, (int __user *) arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			goto setflags_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		flags = ext2_mask_flags(inode->i_mode, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		/* Is it quota file? Do not allow user to mess with it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		if (IS_NOQUOTA(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			goto setflags_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		oldflags = ei->i_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		ret = vfs_ioc_setflags_prepare(inode, oldflags, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			goto setflags_out;
^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) 		flags = flags & EXT2_FL_USER_MODIFIABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		ei->i_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		ext2_set_inode_flags(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) setflags_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		mnt_drop_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	case EXT2_IOC_GETVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return put_user(inode->i_generation, (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	case EXT2_IOC_SETVERSION: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		__u32 generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		if (!inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		ret = mnt_want_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		if (get_user(generation, (int __user *) arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			goto setversion_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		inode->i_generation = generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) setversion_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		mnt_drop_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	case EXT2_IOC_GETRSVSZ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if (test_opt(inode->i_sb, RESERVATION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			&& S_ISREG(inode->i_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 			&& ei->i_block_alloc_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			return put_user(rsv_window_size, (int __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	case EXT2_IOC_SETRSVSZ: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		if (!inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			return -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		if (get_user(rsv_window_size, (int __user *)arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		ret = mnt_want_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		 * need to allocate reservation structure for this inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		 * before set the window size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		 * XXX What lock should protect the rsv_goal_size?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		 * Accessed in ext2_get_block only.  ext3 uses i_truncate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		mutex_lock(&ei->truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		if (!ei->i_block_alloc_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			ext2_init_block_alloc_info(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (ei->i_block_alloc_info){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			rsv->rsv_goal_size = rsv_window_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		mutex_unlock(&ei->truncate_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		mnt_drop_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	/* These are just misnamed, they actually get/put from/to user an int */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	case EXT2_IOC32_GETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		cmd = EXT2_IOC_GETFLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	case EXT2_IOC32_SETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		cmd = EXT2_IOC_SETFLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	case EXT2_IOC32_GETVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		cmd = EXT2_IOC_GETVERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	case EXT2_IOC32_SETVERSION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		cmd = EXT2_IOC_SETVERSION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #endif