4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/string.h>
9 #include <linux/utime.h>
10 #include <linux/file.h>
11 #include <linux/smp_lock.h>
12 #include <linux/quotaops.h>
13 #include <linux/dnotify.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/tty.h>
17 #include <linux/namei.h>
18 #include <linux/backing-dev.h>
19 #include <linux/security.h>
20 #include <linux/mount.h>
21 #include <linux/vfs.h>
22 #include <asm/uaccess.h>
24 #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
26 int vfs_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
32 if (sb
->s_op
->statfs
) {
33 memset(buf
, 0, sizeof(*buf
));
34 retval
= security_sb_statfs(sb
);
37 retval
= sb
->s_op
->statfs(sb
, buf
);
38 if (retval
== 0 && buf
->f_frsize
== 0)
39 buf
->f_frsize
= buf
->f_bsize
;
45 static int vfs_statfs_native(struct super_block
*sb
, struct statfs
*buf
)
50 retval
= vfs_statfs(sb
, &st
);
54 if (sizeof(*buf
) == sizeof(st
))
55 memcpy(buf
, &st
, sizeof(st
));
57 if (sizeof buf
->f_blocks
== 4) {
58 if ((st
.f_blocks
| st
.f_bfree
|
59 st
.f_bavail
| st
.f_files
| st
.f_ffree
) &
60 0xffffffff00000000ULL
)
64 buf
->f_type
= st
.f_type
;
65 buf
->f_bsize
= st
.f_bsize
;
66 buf
->f_blocks
= st
.f_blocks
;
67 buf
->f_bfree
= st
.f_bfree
;
68 buf
->f_bavail
= st
.f_bavail
;
69 buf
->f_files
= st
.f_files
;
70 buf
->f_ffree
= st
.f_ffree
;
71 buf
->f_fsid
= st
.f_fsid
;
72 buf
->f_namelen
= st
.f_namelen
;
73 buf
->f_frsize
= st
.f_frsize
;
74 memset(buf
->f_spare
, 0, sizeof(buf
->f_spare
));
79 static int vfs_statfs64(struct super_block
*sb
, struct statfs64
*buf
)
84 retval
= vfs_statfs(sb
, &st
);
88 if (sizeof(*buf
) == sizeof(st
))
89 memcpy(buf
, &st
, sizeof(st
));
91 buf
->f_type
= st
.f_type
;
92 buf
->f_bsize
= st
.f_bsize
;
93 buf
->f_blocks
= st
.f_blocks
;
94 buf
->f_bfree
= st
.f_bfree
;
95 buf
->f_bavail
= st
.f_bavail
;
96 buf
->f_files
= st
.f_files
;
97 buf
->f_ffree
= st
.f_ffree
;
98 buf
->f_fsid
= st
.f_fsid
;
99 buf
->f_namelen
= st
.f_namelen
;
100 buf
->f_frsize
= st
.f_frsize
;
101 memset(buf
->f_spare
, 0, sizeof(buf
->f_spare
));
106 asmlinkage
long sys_statfs(const char __user
* path
, struct statfs __user
* buf
)
111 error
= user_path_walk(path
, &nd
);
114 error
= vfs_statfs_native(nd
.dentry
->d_inode
->i_sb
, &tmp
);
115 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
123 asmlinkage
long sys_statfs64(const char __user
*path
, size_t sz
, struct statfs64 __user
*buf
)
128 if (sz
!= sizeof(*buf
))
130 error
= user_path_walk(path
, &nd
);
133 error
= vfs_statfs64(nd
.dentry
->d_inode
->i_sb
, &tmp
);
134 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
142 asmlinkage
long sys_fstatfs(unsigned int fd
, struct statfs __user
* buf
)
152 error
= vfs_statfs_native(file
->f_dentry
->d_inode
->i_sb
, &tmp
);
153 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
160 asmlinkage
long sys_fstatfs64(unsigned int fd
, size_t sz
, struct statfs64 __user
*buf
)
166 if (sz
!= sizeof(*buf
))
173 error
= vfs_statfs64(file
->f_dentry
->d_inode
->i_sb
, &tmp
);
174 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
181 int do_truncate(struct dentry
*dentry
, loff_t length
)
184 struct iattr newattrs
;
186 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
190 newattrs
.ia_size
= length
;
191 newattrs
.ia_valid
= ATTR_SIZE
| ATTR_CTIME
;
192 down(&dentry
->d_inode
->i_sem
);
193 err
= notify_change(dentry
, &newattrs
);
194 up(&dentry
->d_inode
->i_sem
);
198 static inline long do_sys_truncate(const char __user
* path
, loff_t length
)
201 struct inode
* inode
;
205 if (length
< 0) /* sorry, but loff_t says... */
208 error
= user_path_walk(path
, &nd
);
211 inode
= nd
.dentry
->d_inode
;
213 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
215 if (S_ISDIR(inode
->i_mode
))
219 if (!S_ISREG(inode
->i_mode
))
222 error
= permission(inode
,MAY_WRITE
,&nd
);
227 if (IS_RDONLY(inode
))
231 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
235 * Make sure that there are no leases.
237 error
= break_lease(inode
, FMODE_WRITE
);
241 error
= get_write_access(inode
);
245 error
= locks_verify_truncate(inode
, NULL
, length
);
248 error
= do_truncate(nd
.dentry
, length
);
250 put_write_access(inode
);
258 asmlinkage
long sys_truncate(const char __user
* path
, unsigned long length
)
260 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
261 return do_sys_truncate(path
, (long)length
);
264 static inline long do_sys_ftruncate(unsigned int fd
, loff_t length
, int small
)
266 struct inode
* inode
;
267 struct dentry
*dentry
;
279 /* explicitly opened as large or we are on 64-bit box */
280 if (file
->f_flags
& O_LARGEFILE
)
283 dentry
= file
->f_dentry
;
284 inode
= dentry
->d_inode
;
286 if (!S_ISREG(inode
->i_mode
) || !(file
->f_mode
& FMODE_WRITE
))
290 /* Cannot ftruncate over 2^31 bytes without large file support */
291 if (small
&& length
> MAX_NON_LFS
)
295 if (IS_APPEND(inode
))
298 error
= locks_verify_truncate(inode
, file
, length
);
300 error
= do_truncate(dentry
, length
);
307 asmlinkage
long sys_ftruncate(unsigned int fd
, unsigned long length
)
309 return do_sys_ftruncate(fd
, length
, 1);
312 /* LFS versions of truncate are only needed on 32 bit machines */
313 #if BITS_PER_LONG == 32
314 asmlinkage
long sys_truncate64(const char __user
* path
, loff_t length
)
316 return do_sys_truncate(path
, length
);
319 asmlinkage
long sys_ftruncate64(unsigned int fd
, loff_t length
)
321 return do_sys_ftruncate(fd
, length
, 0);
325 #if !(defined(__alpha__) || defined(__ia64__))
328 * sys_utime() can be implemented in user-level using sys_utimes().
329 * Is this for backwards compatibility? If so, why not move it
330 * into the appropriate arch directory (for those architectures that
334 /* If times==NULL, set access and modification to current time,
335 * must be owner or have write permission.
336 * Else, update from *times, must be owner or super user.
338 asmlinkage
long sys_utime(char __user
* filename
, struct utimbuf __user
* times
)
342 struct inode
* inode
;
343 struct iattr newattrs
;
345 error
= user_path_walk(filename
, &nd
);
348 inode
= nd
.dentry
->d_inode
;
351 if (IS_RDONLY(inode
))
354 /* Don't worry, the checks are done in inode_change_ok() */
355 newattrs
.ia_valid
= ATTR_CTIME
| ATTR_MTIME
| ATTR_ATIME
;
357 error
= get_user(newattrs
.ia_atime
.tv_sec
, ×
->actime
);
358 newattrs
.ia_atime
.tv_nsec
= 0;
360 error
= get_user(newattrs
.ia_mtime
.tv_sec
, ×
->modtime
);
361 newattrs
.ia_mtime
.tv_nsec
= 0;
365 newattrs
.ia_valid
|= ATTR_ATIME_SET
| ATTR_MTIME_SET
;
367 if (current
->fsuid
!= inode
->i_uid
&&
368 (error
= permission(inode
,MAY_WRITE
,&nd
)) != 0)
372 error
= notify_change(nd
.dentry
, &newattrs
);
382 /* If times==NULL, set access and modification to current time,
383 * must be owner or have write permission.
384 * Else, update from *times, must be owner or super user.
386 long do_utimes(char __user
* filename
, struct timeval
* times
)
390 struct inode
* inode
;
391 struct iattr newattrs
;
393 error
= user_path_walk(filename
, &nd
);
397 inode
= nd
.dentry
->d_inode
;
400 if (IS_RDONLY(inode
))
403 /* Don't worry, the checks are done in inode_change_ok() */
404 newattrs
.ia_valid
= ATTR_CTIME
| ATTR_MTIME
| ATTR_ATIME
;
406 newattrs
.ia_atime
.tv_sec
= times
[0].tv_sec
;
407 newattrs
.ia_atime
.tv_nsec
= times
[0].tv_usec
* 1000;
408 newattrs
.ia_mtime
.tv_sec
= times
[1].tv_sec
;
409 newattrs
.ia_mtime
.tv_nsec
= times
[1].tv_usec
* 1000;
410 newattrs
.ia_valid
|= ATTR_ATIME_SET
| ATTR_MTIME_SET
;
412 if (current
->fsuid
!= inode
->i_uid
&&
413 (error
= permission(inode
,MAY_WRITE
,&nd
)) != 0)
417 error
= notify_change(nd
.dentry
, &newattrs
);
425 asmlinkage
long sys_utimes(char __user
* filename
, struct timeval __user
* utimes
)
427 struct timeval times
[2];
429 if (utimes
&& copy_from_user(×
, utimes
, sizeof(times
)))
431 return do_utimes(filename
, utimes
? times
: NULL
);
436 * access() needs to use the real uid/gid, not the effective uid/gid.
437 * We do this by temporarily clearing all FS-related capabilities and
438 * switching the fsuid/fsgid around to the real ones.
440 asmlinkage
long sys_access(const char __user
* filename
, int mode
)
443 int old_fsuid
, old_fsgid
;
444 kernel_cap_t old_cap
;
447 if (mode
& ~S_IRWXO
) /* where's F_OK, X_OK, W_OK, R_OK? */
450 old_fsuid
= current
->fsuid
;
451 old_fsgid
= current
->fsgid
;
452 old_cap
= current
->cap_effective
;
454 current
->fsuid
= current
->uid
;
455 current
->fsgid
= current
->gid
;
458 * Clear the capabilities if we switch to a non-root user
460 * FIXME: There is a race here against sys_capset. The
461 * capabilities can change yet we will restore the old
462 * value below. We should hold task_capabilities_lock,
463 * but we cannot because user_path_walk can sleep.
466 cap_clear(current
->cap_effective
);
468 current
->cap_effective
= current
->cap_permitted
;
470 res
= __user_walk(filename
, LOOKUP_FOLLOW
|LOOKUP_ACCESS
, &nd
);
472 res
= permission(nd
.dentry
->d_inode
, mode
, &nd
);
473 /* SuS v2 requires we report a read only fs too */
474 if(!res
&& (mode
& S_IWOTH
) && IS_RDONLY(nd
.dentry
->d_inode
)
475 && !special_file(nd
.dentry
->d_inode
->i_mode
))
480 current
->fsuid
= old_fsuid
;
481 current
->fsgid
= old_fsgid
;
482 current
->cap_effective
= old_cap
;
487 asmlinkage
long sys_chdir(const char __user
* filename
)
492 error
= __user_walk(filename
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
, &nd
);
496 error
= permission(nd
.dentry
->d_inode
,MAY_EXEC
,&nd
);
500 set_fs_pwd(current
->fs
, nd
.mnt
, nd
.dentry
);
508 asmlinkage
long sys_fchdir(unsigned int fd
)
511 struct dentry
*dentry
;
513 struct vfsmount
*mnt
;
521 dentry
= file
->f_dentry
;
522 mnt
= file
->f_vfsmnt
;
523 inode
= dentry
->d_inode
;
526 if (!S_ISDIR(inode
->i_mode
))
529 error
= permission(inode
, MAY_EXEC
, NULL
);
531 set_fs_pwd(current
->fs
, mnt
, dentry
);
538 asmlinkage
long sys_chroot(const char __user
* filename
)
543 error
= __user_walk(filename
, LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
| LOOKUP_NOALT
, &nd
);
547 error
= permission(nd
.dentry
->d_inode
,MAY_EXEC
,&nd
);
552 if (!capable(CAP_SYS_CHROOT
))
555 set_fs_root(current
->fs
, nd
.mnt
, nd
.dentry
);
564 asmlinkage
long sys_fchmod(unsigned int fd
, mode_t mode
)
566 struct inode
* inode
;
567 struct dentry
* dentry
;
570 struct iattr newattrs
;
576 dentry
= file
->f_dentry
;
577 inode
= dentry
->d_inode
;
580 if (IS_RDONLY(inode
))
583 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
586 if (mode
== (mode_t
) -1)
587 mode
= inode
->i_mode
;
588 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
589 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
590 err
= notify_change(dentry
, &newattrs
);
599 asmlinkage
long sys_chmod(const char __user
* filename
, mode_t mode
)
602 struct inode
* inode
;
604 struct iattr newattrs
;
606 error
= user_path_walk(filename
, &nd
);
609 inode
= nd
.dentry
->d_inode
;
612 if (IS_RDONLY(inode
))
616 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
620 if (mode
== (mode_t
) -1)
621 mode
= inode
->i_mode
;
622 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
623 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
624 error
= notify_change(nd
.dentry
, &newattrs
);
633 static int chown_common(struct dentry
* dentry
, uid_t user
, gid_t group
)
635 struct inode
* inode
;
637 struct iattr newattrs
;
640 if (!(inode
= dentry
->d_inode
)) {
641 printk(KERN_ERR
"chown_common: NULL inode\n");
645 if (IS_RDONLY(inode
))
648 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
650 newattrs
.ia_valid
= ATTR_CTIME
;
651 if (user
!= (uid_t
) -1) {
652 newattrs
.ia_valid
|= ATTR_UID
;
653 newattrs
.ia_uid
= user
;
655 if (group
!= (gid_t
) -1) {
656 newattrs
.ia_valid
|= ATTR_GID
;
657 newattrs
.ia_gid
= group
;
659 if (!S_ISDIR(inode
->i_mode
))
660 newattrs
.ia_valid
|= ATTR_KILL_SUID
|ATTR_KILL_SGID
;
662 error
= notify_change(dentry
, &newattrs
);
668 asmlinkage
long sys_chown(const char __user
* filename
, uid_t user
, gid_t group
)
673 error
= user_path_walk(filename
, &nd
);
675 error
= chown_common(nd
.dentry
, user
, group
);
681 asmlinkage
long sys_lchown(const char __user
* filename
, uid_t user
, gid_t group
)
686 error
= user_path_walk_link(filename
, &nd
);
688 error
= chown_common(nd
.dentry
, user
, group
);
695 asmlinkage
long sys_fchown(unsigned int fd
, uid_t user
, gid_t group
)
702 error
= chown_common(file
->f_dentry
, user
, group
);
709 * Note that while the flag value (low two bits) for sys_open means:
715 * 00 - no permissions needed
716 * 01 - read-permission
717 * 10 - write-permission
719 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
722 struct file
*filp_open(const char * filename
, int flags
, int mode
)
724 int namei_flags
, error
;
728 if ((namei_flags
+1) & O_ACCMODE
)
730 if (namei_flags
& O_TRUNC
)
733 error
= open_namei(filename
, namei_flags
, mode
, &nd
);
735 return dentry_open(nd
.dentry
, nd
.mnt
, flags
);
737 return ERR_PTR(error
);
740 struct file
*dentry_open(struct dentry
*dentry
, struct vfsmount
*mnt
, int flags
)
747 f
= get_empty_filp();
751 f
->f_mode
= (flags
+1) & O_ACCMODE
;
752 inode
= dentry
->d_inode
;
753 if (f
->f_mode
& FMODE_WRITE
) {
754 error
= get_write_access(inode
);
759 file_ra_state_init(&f
->f_ra
, inode
->i_mapping
);
760 f
->f_dentry
= dentry
;
763 f
->f_op
= fops_get(inode
->i_fop
);
764 file_move(f
, &inode
->i_sb
->s_files
);
766 if (f
->f_op
&& f
->f_op
->open
) {
767 error
= f
->f_op
->open(inode
,f
);
771 f
->f_flags
&= ~(O_CREAT
| O_EXCL
| O_NOCTTY
| O_TRUNC
);
773 /* NB: we're sure to have correct a_ops only after f_op->open */
774 if (f
->f_flags
& O_DIRECT
) {
775 if (!inode
->i_mapping
|| !inode
->i_mapping
->a_ops
||
776 !inode
->i_mapping
->a_ops
->direct_IO
) {
778 f
= ERR_PTR(-EINVAL
);
786 if (f
->f_mode
& FMODE_WRITE
)
787 put_write_access(inode
);
796 return ERR_PTR(error
);
800 * Find an empty file descriptor entry, and mark it busy.
802 int get_unused_fd(void)
804 struct files_struct
* files
= current
->files
;
808 spin_lock(&files
->file_lock
);
811 fd
= find_next_zero_bit(files
->open_fds
->fds_bits
,
816 * N.B. For clone tasks sharing a files structure, this test
817 * will limit the total number of files that can be opened.
819 if (fd
>= current
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
822 /* Do we need to expand the fdset array? */
823 if (fd
>= files
->max_fdset
) {
824 error
= expand_fdset(files
, fd
);
833 * Check whether we need to expand the fd array.
835 if (fd
>= files
->max_fds
) {
836 error
= expand_fd_array(files
, fd
);
844 FD_SET(fd
, files
->open_fds
);
845 FD_CLR(fd
, files
->close_on_exec
);
846 files
->next_fd
= fd
+ 1;
849 if (files
->fd
[fd
] != NULL
) {
850 printk(KERN_WARNING
"get_unused_fd: slot %d not NULL!\n", fd
);
851 files
->fd
[fd
] = NULL
;
857 spin_unlock(&files
->file_lock
);
861 static inline void __put_unused_fd(struct files_struct
*files
, unsigned int fd
)
863 __FD_CLR(fd
, files
->open_fds
);
864 if (fd
< files
->next_fd
)
868 void put_unused_fd(unsigned int fd
)
870 struct files_struct
*files
= current
->files
;
871 spin_lock(&files
->file_lock
);
872 __put_unused_fd(files
, fd
);
873 spin_unlock(&files
->file_lock
);
877 * Install a file pointer in the fd array.
879 * The VFS is full of places where we drop the files lock between
880 * setting the open_fds bitmap and installing the file in the file
881 * array. At any such point, we are vulnerable to a dup2() race
882 * installing a file in the array before us. We need to detect this and
883 * fput() the struct file we are about to overwrite in this case.
885 * It should never happen - if we allow dup2() do it, _really_ bad things
889 void fd_install(unsigned int fd
, struct file
* file
)
891 struct files_struct
*files
= current
->files
;
892 spin_lock(&files
->file_lock
);
893 if (unlikely(files
->fd
[fd
] != NULL
))
895 files
->fd
[fd
] = file
;
896 spin_unlock(&files
->file_lock
);
899 asmlinkage
long sys_open(const char __user
* filename
, int flags
, int mode
)
904 #if BITS_PER_LONG != 32
905 flags
|= O_LARGEFILE
;
907 tmp
= getname(filename
);
910 fd
= get_unused_fd();
912 struct file
*f
= filp_open(tmp
, flags
, mode
);
932 * For backward compatibility? Maybe this should be moved
933 * into arch/i386 instead?
935 asmlinkage
long sys_creat(const char __user
* pathname
, int mode
)
937 return sys_open(pathname
, O_CREAT
| O_WRONLY
| O_TRUNC
, mode
);
943 * "id" is the POSIX thread ID. We use the
944 * files pointer for this..
946 int filp_close(struct file
*filp
, fl_owner_t id
)
950 if (!file_count(filp
)) {
951 printk(KERN_ERR
"VFS: Close: file count is 0\n");
955 if (filp
->f_op
&& filp
->f_op
->flush
)
956 retval
= filp
->f_op
->flush(filp
);
957 dnotify_flush(filp
, id
);
958 locks_remove_posix(filp
, id
);
964 * Careful here! We test whether the file pointer is NULL before
965 * releasing the fd. This ensures that one clone task can't release
966 * an fd while another clone is opening it.
968 asmlinkage
long sys_close(unsigned int fd
)
971 struct files_struct
*files
= current
->files
;
973 spin_lock(&files
->file_lock
);
974 if (fd
>= files
->max_fds
)
976 filp
= files
->fd
[fd
];
979 files
->fd
[fd
] = NULL
;
980 FD_CLR(fd
, files
->close_on_exec
);
981 __put_unused_fd(files
, fd
);
982 spin_unlock(&files
->file_lock
);
983 return filp_close(filp
, files
);
986 spin_unlock(&files
->file_lock
);
991 * This routine simulates a hangup on the tty, to arrange that users
992 * are given clean terminals at login time.
994 asmlinkage
long sys_vhangup(void)
996 if (capable(CAP_SYS_TTY_CONFIG
)) {
997 tty_vhangup(current
->tty
);
1004 * Called when an inode is about to be open.
1005 * We use this to disallow opening large files on 32bit systems if
1006 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1007 * on this flag in sys_open.
1009 int generic_file_open(struct inode
* inode
, struct file
* filp
)
1011 if (!(filp
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
1016 EXPORT_SYMBOL(generic_file_open
);