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/jfs/ioctl.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2006 Herbert Poetzl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * adapted from Remy Card's ext2/ioctl.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/mount.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/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <asm/current.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "jfs_filsys.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "jfs_debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "jfs_incore.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "jfs_dinode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "jfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "jfs_dmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "jfs_discard.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	long jfs_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	long ext2_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) } jfs_map[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	{JFS_NOATIME_FL,	FS_NOATIME_FL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	{JFS_DIRSYNC_FL,	FS_DIRSYNC_FL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	{JFS_SYNC_FL,		FS_SYNC_FL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	{JFS_SECRM_FL,		FS_SECRM_FL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	{JFS_UNRM_FL,		FS_UNRM_FL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	{JFS_APPEND_FL,		FS_APPEND_FL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	{JFS_IMMUTABLE_FL,	FS_IMMUTABLE_FL},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	{0, 0},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static long jfs_map_ext2(unsigned long flags, int from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int index=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	long mapped=0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	while (jfs_map[index].jfs_flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		if (from) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			if (jfs_map[index].ext2_flag & flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 				mapped |= jfs_map[index].jfs_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			if (jfs_map[index].jfs_flag & flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 				mapped |= jfs_map[index].ext2_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	return mapped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) long jfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct jfs_inode_info *jfs_inode = JFS_IP(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	unsigned int flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	case JFS_IOC_GETFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		flags = jfs_inode->mode2 & JFS_FL_USER_VISIBLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		flags = jfs_map_ext2(flags, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return put_user(flags, (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	case JFS_IOC_SETFLAGS: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		unsigned int oldflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		err = mnt_want_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		if (!inode_owner_or_capable(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			goto setflags_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		if (get_user(flags, (int __user *) arg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			goto setflags_out;
^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) 		flags = jfs_map_ext2(flags, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		if (!S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			flags &= ~JFS_DIRSYNC_FL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		/* Is it quota file? Do not allow user to mess with it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		if (IS_NOQUOTA(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			goto setflags_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		/* Lock against other parallel changes of flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		oldflags = jfs_map_ext2(jfs_inode->mode2 & JFS_FL_USER_VISIBLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 					0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		err = vfs_ioc_setflags_prepare(inode, oldflags, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			goto setflags_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		flags = flags & JFS_FL_USER_MODIFIABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		flags |= jfs_inode->mode2 & ~JFS_FL_USER_MODIFIABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		jfs_inode->mode2 = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		jfs_set_inode_flags(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) setflags_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		mnt_drop_write_file(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	case FITRIM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		struct fstrim_range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		s64 ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		if (!blk_queue_discard(q)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			jfs_warn("FITRIM not supported on device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		if (copy_from_user(&range, (struct fstrim_range __user *)arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		    sizeof(range)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		range.minlen = max_t(unsigned int, range.minlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			q->limits.discard_granularity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		ret = jfs_ioc_trim(inode, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		if (copy_to_user((struct fstrim_range __user *)arg, &range,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		    sizeof(range)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	}
^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) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) long jfs_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	/* While these ioctl numbers defined with 'long' and have different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	 * numbers than the 64bit ABI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	 * the actual implementation only deals with ints and is compatible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	case JFS_IOC_GETFLAGS32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		cmd = JFS_IOC_GETFLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	case JFS_IOC_SETFLAGS32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		cmd = JFS_IOC_SETFLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return jfs_ioctl(filp, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) #endif