4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
9 #include <linux/smp_lock.h>
10 #include <linux/file.h>
12 #include <linux/security.h>
13 #include <linux/module.h>
15 #include <asm/uaccess.h>
16 #include <asm/ioctls.h>
18 static int file_ioctl(struct file
*filp
,unsigned int cmd
,unsigned long arg
)
22 struct inode
* inode
= filp
->f_dentry
->d_inode
;
23 int __user
*p
= (int __user
*)arg
;
28 struct address_space
*mapping
= filp
->f_mapping
;
30 /* do we support this mess? */
31 if (!mapping
->a_ops
->bmap
)
33 if (!capable(CAP_SYS_RAWIO
))
35 if ((error
= get_user(block
, p
)) != 0)
38 res
= mapping
->a_ops
->bmap(mapping
, block
);
39 return put_user(res
, p
);
42 if (inode
->i_sb
== NULL
)
44 return put_user(inode
->i_sb
->s_blocksize
, p
);
46 return put_user(i_size_read(inode
) - filp
->f_pos
, p
);
48 if (filp
->f_op
&& filp
->f_op
->ioctl
)
49 return filp
->f_op
->ioctl(inode
, filp
, cmd
, arg
);
54 asmlinkage
long sys_ioctl(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
58 int on
, error
= -EBADF
;
64 error
= security_file_ioctl(filp
, cmd
, arg
);
73 set_close_on_exec(fd
, 1);
77 set_close_on_exec(fd
, 0);
81 if ((error
= get_user(on
, (int __user
*)arg
)) != 0)
85 /* SunOS compatibility item. */
86 if(O_NONBLOCK
!= O_NDELAY
)
90 filp
->f_flags
|= flag
;
92 filp
->f_flags
&= ~flag
;
96 if ((error
= get_user(on
, (int __user
*)arg
)) != 0)
98 flag
= on
? FASYNC
: 0;
100 /* Did FASYNC state change ? */
101 if ((flag
^ filp
->f_flags
) & FASYNC
) {
102 if (filp
->f_op
&& filp
->f_op
->fasync
)
103 error
= filp
->f_op
->fasync(fd
, filp
, on
);
104 else error
= -ENOTTY
;
110 filp
->f_flags
|= FASYNC
;
112 filp
->f_flags
&= ~FASYNC
;
116 if (S_ISDIR(filp
->f_dentry
->d_inode
->i_mode
) ||
117 S_ISREG(filp
->f_dentry
->d_inode
->i_mode
) ||
118 S_ISLNK(filp
->f_dentry
->d_inode
->i_mode
)) {
119 loff_t res
= inode_get_bytes(filp
->f_dentry
->d_inode
);
120 error
= copy_to_user((loff_t __user
*)arg
, &res
, sizeof(res
)) ? -EFAULT
: 0;
127 if (S_ISREG(filp
->f_dentry
->d_inode
->i_mode
))
128 error
= file_ioctl(filp
, cmd
, arg
);
129 else if (filp
->f_op
&& filp
->f_op
->ioctl
)
130 error
= filp
->f_op
->ioctl(filp
->f_dentry
->d_inode
, filp
, cmd
, arg
);
140 * Platforms implementing 32 bit compatibility ioctl handlers in
141 * modules need this exported
144 EXPORT_SYMBOL(sys_ioctl
);