4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
8 #include <linux/syscalls.h>
10 #include <linux/smp_lock.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 int file_ioctl(struct file
*filp
,unsigned int cmd
,unsigned long arg
)
23 struct inode
* inode
= filp
->f_dentry
->d_inode
;
24 int __user
*p
= (int __user
*)arg
;
29 struct address_space
*mapping
= filp
->f_mapping
;
31 /* do we support this mess? */
32 if (!mapping
->a_ops
->bmap
)
34 if (!capable(CAP_SYS_RAWIO
))
36 if ((error
= get_user(block
, p
)) != 0)
39 res
= mapping
->a_ops
->bmap(mapping
, block
);
40 return put_user(res
, p
);
43 if (inode
->i_sb
== NULL
)
45 return put_user(inode
->i_sb
->s_blocksize
, p
);
47 return put_user(i_size_read(inode
) - filp
->f_pos
, p
);
49 if (filp
->f_op
&& filp
->f_op
->ioctl
)
50 return filp
->f_op
->ioctl(inode
, filp
, cmd
, arg
);
55 asmlinkage
long sys_ioctl(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
59 int on
, error
= -EBADF
;
65 error
= security_file_ioctl(filp
, cmd
, arg
);
74 set_close_on_exec(fd
, 1);
78 set_close_on_exec(fd
, 0);
82 if ((error
= get_user(on
, (int __user
*)arg
)) != 0)
86 /* SunOS compatibility item. */
87 if(O_NONBLOCK
!= O_NDELAY
)
91 filp
->f_flags
|= flag
;
93 filp
->f_flags
&= ~flag
;
97 if ((error
= get_user(on
, (int __user
*)arg
)) != 0)
99 flag
= on
? FASYNC
: 0;
101 /* Did FASYNC state change ? */
102 if ((flag
^ filp
->f_flags
) & FASYNC
) {
103 if (filp
->f_op
&& filp
->f_op
->fasync
)
104 error
= filp
->f_op
->fasync(fd
, filp
, on
);
105 else error
= -ENOTTY
;
111 filp
->f_flags
|= FASYNC
;
113 filp
->f_flags
&= ~FASYNC
;
117 if (S_ISDIR(filp
->f_dentry
->d_inode
->i_mode
) ||
118 S_ISREG(filp
->f_dentry
->d_inode
->i_mode
) ||
119 S_ISLNK(filp
->f_dentry
->d_inode
->i_mode
)) {
120 loff_t res
= inode_get_bytes(filp
->f_dentry
->d_inode
);
121 error
= copy_to_user((loff_t __user
*)arg
, &res
, sizeof(res
)) ? -EFAULT
: 0;
128 if (S_ISREG(filp
->f_dentry
->d_inode
->i_mode
))
129 error
= file_ioctl(filp
, cmd
, arg
);
130 else if (filp
->f_op
&& filp
->f_op
->ioctl
)
131 error
= filp
->f_op
->ioctl(filp
->f_dentry
->d_inode
, filp
, cmd
, arg
);
141 * Platforms implementing 32 bit compatibility ioctl handlers in
142 * modules need this exported
145 EXPORT_SYMBOL(sys_ioctl
);