4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/syscalls.h>
9 #include <linux/smp_lock.h>
10 #include <linux/capability.h>
11 #include <linux/file.h>
13 #include <linux/security.h>
14 #include <linux/module.h>
16 #include <asm/uaccess.h>
17 #include <asm/ioctls.h>
19 static long do_ioctl(struct file
*filp
, unsigned int cmd
,
27 if (filp
->f_op
->unlocked_ioctl
) {
28 error
= filp
->f_op
->unlocked_ioctl(filp
, cmd
, arg
);
29 if (error
== -ENOIOCTLCMD
)
32 } else if (filp
->f_op
->ioctl
) {
34 error
= filp
->f_op
->ioctl(filp
->f_dentry
->d_inode
,
43 static int file_ioctl(struct file
*filp
, unsigned int cmd
,
48 struct inode
* inode
= filp
->f_dentry
->d_inode
;
49 int __user
*p
= (int __user
*)arg
;
54 struct address_space
*mapping
= filp
->f_mapping
;
56 /* do we support this mess? */
57 if (!mapping
->a_ops
->bmap
)
59 if (!capable(CAP_SYS_RAWIO
))
61 if ((error
= get_user(block
, p
)) != 0)
65 res
= mapping
->a_ops
->bmap(mapping
, block
);
67 return put_user(res
, p
);
70 if (inode
->i_sb
== NULL
)
72 return put_user(inode
->i_sb
->s_blocksize
, p
);
74 return put_user(i_size_read(inode
) - filp
->f_pos
, p
);
77 return do_ioctl(filp
, cmd
, arg
);
81 * When you add any new common ioctls to the switches above and below
82 * please update compat_sys_ioctl() too.
84 * vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
85 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
87 int vfs_ioctl(struct file
*filp
, unsigned int fd
, unsigned int cmd
, unsigned long arg
)
94 set_close_on_exec(fd
, 1);
98 set_close_on_exec(fd
, 0);
102 if ((error
= get_user(on
, (int __user
*)arg
)) != 0)
106 /* SunOS compatibility item. */
107 if(O_NONBLOCK
!= O_NDELAY
)
111 filp
->f_flags
|= flag
;
113 filp
->f_flags
&= ~flag
;
117 if ((error
= get_user(on
, (int __user
*)arg
)) != 0)
119 flag
= on
? FASYNC
: 0;
121 /* Did FASYNC state change ? */
122 if ((flag
^ filp
->f_flags
) & FASYNC
) {
123 if (filp
->f_op
&& filp
->f_op
->fasync
) {
125 error
= filp
->f_op
->fasync(fd
, filp
, on
);
128 else error
= -ENOTTY
;
134 filp
->f_flags
|= FASYNC
;
136 filp
->f_flags
&= ~FASYNC
;
140 if (S_ISDIR(filp
->f_dentry
->d_inode
->i_mode
) ||
141 S_ISREG(filp
->f_dentry
->d_inode
->i_mode
) ||
142 S_ISLNK(filp
->f_dentry
->d_inode
->i_mode
)) {
143 loff_t res
= inode_get_bytes(filp
->f_dentry
->d_inode
);
144 error
= copy_to_user((loff_t __user
*)arg
, &res
, sizeof(res
)) ? -EFAULT
: 0;
150 if (S_ISREG(filp
->f_dentry
->d_inode
->i_mode
))
151 error
= file_ioctl(filp
, cmd
, arg
);
153 error
= do_ioctl(filp
, cmd
, arg
);
159 asmlinkage
long sys_ioctl(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
165 filp
= fget_light(fd
, &fput_needed
);
169 error
= security_file_ioctl(filp
, cmd
, arg
);
173 error
= vfs_ioctl(filp
, fd
, cmd
, arg
);
175 fput_light(filp
, fput_needed
);
181 * Platforms implementing 32 bit compatibility ioctl handlers in
182 * modules need this exported
185 EXPORT_SYMBOL(sys_ioctl
);