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