4 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/smp_lock.h>
9 #include <linux/file.h>
11 #include <linux/security.h>
13 #include <asm/uaccess.h>
14 #include <asm/ioctls.h>
16 static int file_ioctl(struct file
*filp
,unsigned int cmd
,unsigned long arg
)
20 struct inode
* inode
= filp
->f_dentry
->d_inode
;
25 struct address_space
*mapping
= inode
->i_mapping
;
27 /* do we support this mess? */
28 if (!mapping
->a_ops
->bmap
)
30 if (!capable(CAP_SYS_RAWIO
))
32 if ((error
= get_user(block
, (int *) arg
)) != 0)
35 res
= mapping
->a_ops
->bmap(mapping
, block
);
36 return put_user(res
, (int *) arg
);
39 if (inode
->i_sb
== NULL
)
41 return put_user(inode
->i_sb
->s_blocksize
, (int *) arg
);
43 return put_user(inode
->i_size
- filp
->f_pos
, (int *) arg
);
45 if (filp
->f_op
&& filp
->f_op
->ioctl
)
46 return filp
->f_op
->ioctl(inode
, filp
, cmd
, arg
);
51 asmlinkage
long sys_ioctl(unsigned int fd
, unsigned int cmd
, unsigned long arg
)
55 int on
, error
= -EBADF
;
62 error
= security_ops
->file_ioctl(filp
, cmd
, arg
);
71 set_close_on_exec(fd
, 1);
75 set_close_on_exec(fd
, 0);
79 if ((error
= get_user(on
, (int *)arg
)) != 0)
83 /* SunOS compatibility item. */
84 if(O_NONBLOCK
!= O_NDELAY
)
88 filp
->f_flags
|= flag
;
90 filp
->f_flags
&= ~flag
;
94 if ((error
= get_user(on
, (int *)arg
)) != 0)
96 flag
= on
? FASYNC
: 0;
98 /* Did FASYNC state change ? */
99 if ((flag
^ filp
->f_flags
) & FASYNC
) {
100 if (filp
->f_op
&& filp
->f_op
->fasync
)
101 error
= filp
->f_op
->fasync(fd
, filp
, on
);
102 else error
= -ENOTTY
;
108 filp
->f_flags
|= FASYNC
;
110 filp
->f_flags
&= ~FASYNC
;
114 if (S_ISDIR(filp
->f_dentry
->d_inode
->i_mode
) ||
115 S_ISREG(filp
->f_dentry
->d_inode
->i_mode
) ||
116 S_ISLNK(filp
->f_dentry
->d_inode
->i_mode
)) {
117 loff_t res
= inode_get_bytes(filp
->f_dentry
->d_inode
);
118 error
= copy_to_user((loff_t
*)arg
, &res
, sizeof(res
)) ? -EFAULT
: 0;
125 if (S_ISREG(filp
->f_dentry
->d_inode
->i_mode
))
126 error
= file_ioctl(filp
, cmd
, arg
);
127 else if (filp
->f_op
&& filp
->f_op
->ioctl
)
128 error
= filp
->f_op
->ioctl(filp
->f_dentry
->d_inode
, filp
, cmd
, arg
);