ioctl.c 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * linux/fs/jfs/ioctl.c
 *
 * Copyright (C) 2006 Herbert Poetzl
 * adapted from Remy Card's ext2/ioctl.c
 */

#include <linux/fs.h>
#include <linux/ctype.h>
#include <linux/capability.h>
11
#include <linux/mount.h>
12
#include <linux/time.h>
13
#include <linux/sched.h>
14 15 16 17 18 19 20 21 22 23 24 25
#include <asm/current.h>
#include <asm/uaccess.h>

#include "jfs_incore.h"
#include "jfs_dinode.h"
#include "jfs_inode.h"


static struct {
	long jfs_flag;
	long ext2_flag;
} jfs_map[] = {
26 27 28 29 30 31 32
	{JFS_NOATIME_FL,	FS_NOATIME_FL},
	{JFS_DIRSYNC_FL,	FS_DIRSYNC_FL},
	{JFS_SYNC_FL,		FS_SYNC_FL},
	{JFS_SECRM_FL,		FS_SECRM_FL},
	{JFS_UNRM_FL,		FS_UNRM_FL},
	{JFS_APPEND_FL,		FS_APPEND_FL},
	{JFS_IMMUTABLE_FL,	FS_IMMUTABLE_FL},
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	{0, 0},
};

static long jfs_map_ext2(unsigned long flags, int from)
{
	int index=0;
	long mapped=0;

	while (jfs_map[index].jfs_flag) {
		if (from) {
			if (jfs_map[index].ext2_flag & flags)
				mapped |= jfs_map[index].jfs_flag;
		} else {
			if (jfs_map[index].jfs_flag & flags)
				mapped |= jfs_map[index].ext2_flag;
		}
		index++;
	}
	return mapped;
}


55
long jfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
56
{
57
	struct inode *inode = filp->f_dentry->d_inode;
58 59 60 61 62
	struct jfs_inode_info *jfs_inode = JFS_IP(inode);
	unsigned int flags;

	switch (cmd) {
	case JFS_IOC_GETFLAGS:
63
		jfs_get_inode_flags(jfs_inode);
64 65 66 67 68
		flags = jfs_inode->mode2 & JFS_FL_USER_VISIBLE;
		flags = jfs_map_ext2(flags, 0);
		return put_user(flags, (int __user *) arg);
	case JFS_IOC_SETFLAGS: {
		unsigned int oldflags;
69
		int err;
70

71
		err = mnt_want_write_file(filp);
72 73
		if (err)
			return err;
74

75
		if (!inode_owner_or_capable(inode)) {
76 77 78 79 80 81 82
			err = -EACCES;
			goto setflags_out;
		}
		if (get_user(flags, (int __user *) arg)) {
			err = -EFAULT;
			goto setflags_out;
		}
83 84 85 86 87

		flags = jfs_map_ext2(flags, 1);
		if (!S_ISDIR(inode->i_mode))
			flags &= ~JFS_DIRSYNC_FL;

88
		/* Is it quota file? Do not allow user to mess with it */
89 90 91 92
		if (IS_NOQUOTA(inode)) {
			err = -EPERM;
			goto setflags_out;
		}
93 94 95 96

		/* Lock against other parallel changes of flags */
		mutex_lock(&inode->i_mutex);

97
		jfs_get_inode_flags(jfs_inode);
98 99 100 101 102 103 104 105 106
		oldflags = jfs_inode->mode2;

		/*
		 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
		 * the relevant capability.
		 */
		if ((oldflags & JFS_IMMUTABLE_FL) ||
			((flags ^ oldflags) &
			(JFS_APPEND_FL | JFS_IMMUTABLE_FL))) {
107 108
			if (!capable(CAP_LINUX_IMMUTABLE)) {
				mutex_unlock(&inode->i_mutex);
109 110
				err = -EPERM;
				goto setflags_out;
111
			}
112 113 114 115 116 117 118
		}

		flags = flags & JFS_FL_USER_MODIFIABLE;
		flags |= oldflags & ~JFS_FL_USER_MODIFIABLE;
		jfs_inode->mode2 = flags;

		jfs_set_inode_flags(inode);
119
		mutex_unlock(&inode->i_mutex);
120 121
		inode->i_ctime = CURRENT_TIME_SEC;
		mark_inode_dirty(inode);
122
setflags_out:
A
Al Viro 已提交
123
		mnt_drop_write_file(filp);
124
		return err;
125 126 127 128 129 130
	}
	default:
		return -ENOTTY;
	}
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#ifdef CONFIG_COMPAT
long jfs_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	/* While these ioctl numbers defined with 'long' and have different
	 * numbers than the 64bit ABI,
	 * the actual implementation only deals with ints and is compatible.
	 */
	switch (cmd) {
	case JFS_IOC_GETFLAGS32:
		cmd = JFS_IOC_GETFLAGS;
		break;
	case JFS_IOC_SETFLAGS32:
		cmd = JFS_IOC_SETFLAGS;
		break;
	}
	return jfs_ioctl(filp, cmd, arg);
}
#endif