ioctl.c 4.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9
/*
 *  linux/fs/ioctl.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 */

#include <linux/syscalls.h>
#include <linux/mm.h>
#include <linux/smp_lock.h>
10
#include <linux/capability.h>
L
Linus Torvalds 已提交
11 12 13 14
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/security.h>
#include <linux/module.h>
15
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
16 17 18

#include <asm/ioctls.h>

19 20 21 22 23 24 25 26 27 28 29 30 31 32
/**
 * vfs_ioctl - call filesystem specific ioctl methods
 * @filp: [in]     open file to invoke ioctl method on
 * @cmd:  [in]     ioctl command to execute
 * @arg:  [in/out] command-specific argument for ioctl
 *
 * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
 * invokes * filesystem specific ->ioctl method.  If neither method exists,
 * returns -ENOTTY.
 *
 * Returns 0 on success, -errno on error.
 */
long vfs_ioctl(struct file *filp, unsigned int cmd,
	       unsigned long arg)
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40 41 42 43
{
	int error = -ENOTTY;

	if (!filp->f_op)
		goto out;

	if (filp->f_op->unlocked_ioctl) {
		error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
		if (error == -ENOIOCTLCMD)
			error = -EINVAL;
		goto out;
44
	} else if (filp->f_op->ioctl) {
L
Linus Torvalds 已提交
45
		lock_kernel();
46 47
		error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
					  filp, cmd, arg);
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58 59
		unlock_kernel();
	}

 out:
	return error;
}

static int file_ioctl(struct file *filp, unsigned int cmd,
		unsigned long arg)
{
	int error;
	int block;
60
	struct inode *inode = filp->f_path.dentry->d_inode;
L
Linus Torvalds 已提交
61 62 63
	int __user *p = (int __user *)arg;

	switch (cmd) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	case FIBMAP:
	{
		struct address_space *mapping = filp->f_mapping;
		int res;
		/* do we support this mess? */
		if (!mapping->a_ops->bmap)
			return -EINVAL;
		if (!capable(CAP_SYS_RAWIO))
			return -EPERM;
		error = get_user(block, p);
		if (error)
			return error;
		lock_kernel();
		res = mapping->a_ops->bmap(mapping, block);
		unlock_kernel();
		return put_user(res, p);
	}
	case FIGETBSZ:
		return put_user(inode->i_sb->s_blocksize, p);
	case FIONREAD:
		return put_user(i_size_read(inode) - filp->f_pos, p);
L
Linus Torvalds 已提交
85 86
	}

87
	return vfs_ioctl(filp, cmd, arg);
L
Linus Torvalds 已提交
88 89 90 91 92 93
}

/*
 * When you add any new common ioctls to the switches above and below
 * please update compat_sys_ioctl() too.
 *
94
 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
L
Linus Torvalds 已提交
95 96
 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
 */
97 98
int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
	     unsigned long arg)
L
Linus Torvalds 已提交
99 100 101 102 103
{
	unsigned int flag;
	int on, error = 0;

	switch (cmd) {
104 105 106
	case FIOCLEX:
		set_close_on_exec(fd, 1);
		break;
L
Linus Torvalds 已提交
107

108 109 110
	case FIONCLEX:
		set_close_on_exec(fd, 0);
		break;
L
Linus Torvalds 已提交
111

112 113 114 115 116
	case FIONBIO:
		error = get_user(on, (int __user *)arg);
		if (error)
			break;
		flag = O_NONBLOCK;
L
Linus Torvalds 已提交
117
#ifdef __sparc__
118 119 120
		/* SunOS compatibility item. */
		if (O_NONBLOCK != O_NDELAY)
			flag |= O_NDELAY;
L
Linus Torvalds 已提交
121
#endif
122 123 124 125 126 127 128 129 130
		if (on)
			filp->f_flags |= flag;
		else
			filp->f_flags &= ~flag;
		break;

	case FIOASYNC:
		error = get_user(on, (int __user *)arg);
		if (error)
L
Linus Torvalds 已提交
131
			break;
132 133 134 135 136 137 138 139 140
		flag = on ? FASYNC : 0;

		/* Did FASYNC state change ? */
		if ((flag ^ filp->f_flags) & FASYNC) {
			if (filp->f_op && filp->f_op->fasync) {
				lock_kernel();
				error = filp->f_op->fasync(fd, filp, on);
				unlock_kernel();
			} else
L
Linus Torvalds 已提交
141
				error = -ENOTTY;
142 143
		}
		if (error != 0)
L
Linus Torvalds 已提交
144
			break;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

		if (on)
			filp->f_flags |= FASYNC;
		else
			filp->f_flags &= ~FASYNC;
		break;

	case FIOQSIZE:
		if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
		    S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
		    S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
			loff_t res =
				inode_get_bytes(filp->f_path.dentry->d_inode);
			error = copy_to_user((loff_t __user *)arg, &res,
					     sizeof(res)) ? -EFAULT : 0;
		} else
			error = -ENOTTY;
		break;
	default:
		if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
			error = file_ioctl(filp, cmd, arg);
		else
167
			error = vfs_ioctl(filp, cmd, arg);
168
		break;
L
Linus Torvalds 已提交
169 170 171 172 173 174
	}
	return error;
}

asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
{
175
	struct file *filp;
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185 186
	int error = -EBADF;
	int fput_needed;

	filp = fget_light(fd, &fput_needed);
	if (!filp)
		goto out;

	error = security_file_ioctl(filp, cmd, arg);
	if (error)
		goto out_fput;

187
	error = do_vfs_ioctl(filp, fd, cmd, arg);
L
Linus Torvalds 已提交
188 189 190 191 192
 out_fput:
	fput_light(filp, fput_needed);
 out:
	return error;
}