4 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/smp_lock.h>
9 #include <linux/file.h>
11 #include <asm/uaccess.h>
12 #include <asm/ioctls.h>
14 static int file_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
18 struct inode * inode = filp->f_dentry->d_inode;
23 struct address_space *mapping = inode->i_mapping;
25 /* do we support this mess? */
26 if (!mapping->a_ops->bmap)
28 if (!capable(CAP_SYS_RAWIO))
30 if ((error = get_user(block, (int *) arg)) != 0)
33 res = mapping->a_ops->bmap(mapping, block);
34 return put_user(res, (int *) arg);
37 if (inode->i_sb == NULL)
39 return put_user(inode->i_sb->s_blocksize, (int *) arg);
41 return put_user(inode->i_size - filp->f_pos, (int *) arg);
43 if (filp->f_op && filp->f_op->ioctl)
44 return filp->f_op->ioctl(inode, filp, cmd, arg);
49 asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
53 int on, error = -EBADF;
62 set_close_on_exec(fd, 1);
66 set_close_on_exec(fd, 0);
70 if ((error = get_user(on, (int *)arg)) != 0)
74 /* SunOS compatibility item. */
75 if(O_NONBLOCK != O_NDELAY)
79 filp->f_flags |= flag;
81 filp->f_flags &= ~flag;
85 if ((error = get_user(on, (int *)arg)) != 0)
87 flag = on ? FASYNC : 0;
89 /* Did FASYNC state change ? */
90 if ((flag ^ filp->f_flags) & FASYNC) {
91 if (filp->f_op && filp->f_op->fasync)
92 error = filp->f_op->fasync(fd, filp, on);
99 filp->f_flags |= FASYNC;
101 filp->f_flags &= ~FASYNC;
105 if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
106 S_ISREG(filp->f_dentry->d_inode->i_mode) ||
107 S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
108 loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
109 error = copy_to_user((loff_t *)arg, &res, sizeof(res)) ? -EFAULT : 0;
116 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
117 error = file_ioctl(filp, cmd, arg);
118 else if (filp->f_op && filp->f_op->ioctl)
119 error = filp->f_op->ioctl(filp->f_dentry->d_inode, filp, cmd, arg);