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 #include <linux/pagemap.h>
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 EXPORT_SYMBOL(vfs_statfs
);
47 static int vfs_statfs_native(struct super_block
*sb
, struct statfs
*buf
)
52 retval
= vfs_statfs(sb
, &st
);
56 if (sizeof(*buf
) == sizeof(st
))
57 memcpy(buf
, &st
, sizeof(st
));
59 if (sizeof buf
->f_blocks
== 4) {
60 if ((st
.f_blocks
| st
.f_bfree
|
61 st
.f_bavail
| st
.f_files
| st
.f_ffree
) &
62 0xffffffff00000000ULL
)
66 buf
->f_type
= st
.f_type
;
67 buf
->f_bsize
= st
.f_bsize
;
68 buf
->f_blocks
= st
.f_blocks
;
69 buf
->f_bfree
= st
.f_bfree
;
70 buf
->f_bavail
= st
.f_bavail
;
71 buf
->f_files
= st
.f_files
;
72 buf
->f_ffree
= st
.f_ffree
;
73 buf
->f_fsid
= st
.f_fsid
;
74 buf
->f_namelen
= st
.f_namelen
;
75 buf
->f_frsize
= st
.f_frsize
;
76 memset(buf
->f_spare
, 0, sizeof(buf
->f_spare
));
81 static int vfs_statfs64(struct super_block
*sb
, struct statfs64
*buf
)
86 retval
= vfs_statfs(sb
, &st
);
90 if (sizeof(*buf
) == sizeof(st
))
91 memcpy(buf
, &st
, sizeof(st
));
93 buf
->f_type
= st
.f_type
;
94 buf
->f_bsize
= st
.f_bsize
;
95 buf
->f_blocks
= st
.f_blocks
;
96 buf
->f_bfree
= st
.f_bfree
;
97 buf
->f_bavail
= st
.f_bavail
;
98 buf
->f_files
= st
.f_files
;
99 buf
->f_ffree
= st
.f_ffree
;
100 buf
->f_fsid
= st
.f_fsid
;
101 buf
->f_namelen
= st
.f_namelen
;
102 buf
->f_frsize
= st
.f_frsize
;
103 memset(buf
->f_spare
, 0, sizeof(buf
->f_spare
));
108 asmlinkage
long sys_statfs(const char __user
* path
, struct statfs __user
* buf
)
113 error
= user_path_walk(path
, &nd
);
116 error
= vfs_statfs_native(nd
.dentry
->d_inode
->i_sb
, &tmp
);
117 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
125 asmlinkage
long sys_statfs64(const char __user
*path
, size_t sz
, struct statfs64 __user
*buf
)
130 if (sz
!= sizeof(*buf
))
132 error
= user_path_walk(path
, &nd
);
135 error
= vfs_statfs64(nd
.dentry
->d_inode
->i_sb
, &tmp
);
136 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
144 asmlinkage
long sys_fstatfs(unsigned int fd
, struct statfs __user
* buf
)
154 error
= vfs_statfs_native(file
->f_dentry
->d_inode
->i_sb
, &tmp
);
155 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
162 asmlinkage
long sys_fstatfs64(unsigned int fd
, size_t sz
, struct statfs64 __user
*buf
)
168 if (sz
!= sizeof(*buf
))
175 error
= vfs_statfs64(file
->f_dentry
->d_inode
->i_sb
, &tmp
);
176 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
183 int do_truncate(struct dentry
*dentry
, loff_t length
)
186 struct iattr newattrs
;
188 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
192 newattrs
.ia_size
= length
;
193 newattrs
.ia_valid
= ATTR_SIZE
| ATTR_CTIME
;
194 down(&dentry
->d_inode
->i_sem
);
195 err
= notify_change(dentry
, &newattrs
);
196 up(&dentry
->d_inode
->i_sem
);
200 static inline long do_sys_truncate(const char __user
* path
, loff_t length
)
203 struct inode
* inode
;
207 if (length
< 0) /* sorry, but loff_t says... */
210 error
= user_path_walk(path
, &nd
);
213 inode
= nd
.dentry
->d_inode
;
215 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
217 if (S_ISDIR(inode
->i_mode
))
221 if (!S_ISREG(inode
->i_mode
))
224 error
= permission(inode
,MAY_WRITE
,&nd
);
229 if (IS_RDONLY(inode
))
233 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
237 * Make sure that there are no leases.
239 error
= break_lease(inode
, FMODE_WRITE
);
243 error
= get_write_access(inode
);
247 error
= locks_verify_truncate(inode
, NULL
, length
);
250 error
= do_truncate(nd
.dentry
, length
);
252 put_write_access(inode
);
260 asmlinkage
long sys_truncate(const char __user
* path
, unsigned long length
)
262 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
263 return do_sys_truncate(path
, (long)length
);
266 static inline long do_sys_ftruncate(unsigned int fd
, loff_t length
, int small
)
268 struct inode
* inode
;
269 struct dentry
*dentry
;
281 /* explicitly opened as large or we are on 64-bit box */
282 if (file
->f_flags
& O_LARGEFILE
)
285 dentry
= file
->f_dentry
;
286 inode
= dentry
->d_inode
;
288 if (!S_ISREG(inode
->i_mode
) || !(file
->f_mode
& FMODE_WRITE
))
292 /* Cannot ftruncate over 2^31 bytes without large file support */
293 if (small
&& length
> MAX_NON_LFS
)
297 if (IS_APPEND(inode
))
300 error
= locks_verify_truncate(inode
, file
, length
);
302 error
= do_truncate(dentry
, length
);
309 asmlinkage
long sys_ftruncate(unsigned int fd
, unsigned long length
)
311 return do_sys_ftruncate(fd
, length
, 1);
314 /* LFS versions of truncate are only needed on 32 bit machines */
315 #if BITS_PER_LONG == 32
316 asmlinkage
long sys_truncate64(const char __user
* path
, loff_t length
)
318 return do_sys_truncate(path
, length
);
321 asmlinkage
long sys_ftruncate64(unsigned int fd
, loff_t length
)
323 return do_sys_ftruncate(fd
, length
, 0);
327 #if !(defined(__alpha__) || defined(__ia64__))
330 * sys_utime() can be implemented in user-level using sys_utimes().
331 * Is this for backwards compatibility? If so, why not move it
332 * into the appropriate arch directory (for those architectures that
336 /* If times==NULL, set access and modification to current time,
337 * must be owner or have write permission.
338 * Else, update from *times, must be owner or super user.
340 asmlinkage
long sys_utime(char __user
* filename
, struct utimbuf __user
* times
)
344 struct inode
* inode
;
345 struct iattr newattrs
;
347 error
= user_path_walk(filename
, &nd
);
350 inode
= nd
.dentry
->d_inode
;
353 if (IS_RDONLY(inode
))
356 /* Don't worry, the checks are done in inode_change_ok() */
357 newattrs
.ia_valid
= ATTR_CTIME
| ATTR_MTIME
| ATTR_ATIME
;
360 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
363 error
= get_user(newattrs
.ia_atime
.tv_sec
, ×
->actime
);
364 newattrs
.ia_atime
.tv_nsec
= 0;
366 error
= get_user(newattrs
.ia_mtime
.tv_sec
, ×
->modtime
);
367 newattrs
.ia_mtime
.tv_nsec
= 0;
371 newattrs
.ia_valid
|= ATTR_ATIME_SET
| ATTR_MTIME_SET
;
374 if (IS_IMMUTABLE(inode
))
377 if (current
->fsuid
!= inode
->i_uid
&&
378 (error
= permission(inode
,MAY_WRITE
,&nd
)) != 0)
382 error
= notify_change(nd
.dentry
, &newattrs
);
392 /* If times==NULL, set access and modification to current time,
393 * must be owner or have write permission.
394 * Else, update from *times, must be owner or super user.
396 long do_utimes(char __user
* filename
, struct timeval
* times
)
400 struct inode
* inode
;
401 struct iattr newattrs
;
403 error
= user_path_walk(filename
, &nd
);
407 inode
= nd
.dentry
->d_inode
;
410 if (IS_RDONLY(inode
))
413 /* Don't worry, the checks are done in inode_change_ok() */
414 newattrs
.ia_valid
= ATTR_CTIME
| ATTR_MTIME
| ATTR_ATIME
;
417 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
420 newattrs
.ia_atime
.tv_sec
= times
[0].tv_sec
;
421 newattrs
.ia_atime
.tv_nsec
= times
[0].tv_usec
* 1000;
422 newattrs
.ia_mtime
.tv_sec
= times
[1].tv_sec
;
423 newattrs
.ia_mtime
.tv_nsec
= times
[1].tv_usec
* 1000;
424 newattrs
.ia_valid
|= ATTR_ATIME_SET
| ATTR_MTIME_SET
;
427 if (IS_IMMUTABLE(inode
))
430 if (current
->fsuid
!= inode
->i_uid
&&
431 (error
= permission(inode
,MAY_WRITE
,&nd
)) != 0)
435 error
= notify_change(nd
.dentry
, &newattrs
);
443 asmlinkage
long sys_utimes(char __user
* filename
, struct timeval __user
* utimes
)
445 struct timeval times
[2];
447 if (utimes
&& copy_from_user(×
, utimes
, sizeof(times
)))
449 return do_utimes(filename
, utimes
? times
: NULL
);
454 * access() needs to use the real uid/gid, not the effective uid/gid.
455 * We do this by temporarily clearing all FS-related capabilities and
456 * switching the fsuid/fsgid around to the real ones.
458 asmlinkage
long sys_access(const char __user
* filename
, int mode
)
461 int old_fsuid
, old_fsgid
;
462 kernel_cap_t old_cap
;
465 if (mode
& ~S_IRWXO
) /* where's F_OK, X_OK, W_OK, R_OK? */
468 old_fsuid
= current
->fsuid
;
469 old_fsgid
= current
->fsgid
;
470 old_cap
= current
->cap_effective
;
472 current
->fsuid
= current
->uid
;
473 current
->fsgid
= current
->gid
;
476 * Clear the capabilities if we switch to a non-root user
478 * FIXME: There is a race here against sys_capset. The
479 * capabilities can change yet we will restore the old
480 * value below. We should hold task_capabilities_lock,
481 * but we cannot because user_path_walk can sleep.
484 cap_clear(current
->cap_effective
);
486 current
->cap_effective
= current
->cap_permitted
;
488 res
= __user_walk(filename
, LOOKUP_FOLLOW
|LOOKUP_ACCESS
, &nd
);
490 res
= permission(nd
.dentry
->d_inode
, mode
, &nd
);
491 /* SuS v2 requires we report a read only fs too */
492 if(!res
&& (mode
& S_IWOTH
) && IS_RDONLY(nd
.dentry
->d_inode
)
493 && !special_file(nd
.dentry
->d_inode
->i_mode
))
498 current
->fsuid
= old_fsuid
;
499 current
->fsgid
= old_fsgid
;
500 current
->cap_effective
= old_cap
;
505 asmlinkage
long sys_chdir(const char __user
* filename
)
510 error
= __user_walk(filename
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
, &nd
);
514 error
= permission(nd
.dentry
->d_inode
,MAY_EXEC
,&nd
);
518 set_fs_pwd(current
->fs
, nd
.mnt
, nd
.dentry
);
526 asmlinkage
long sys_fchdir(unsigned int fd
)
529 struct dentry
*dentry
;
531 struct vfsmount
*mnt
;
539 dentry
= file
->f_dentry
;
540 mnt
= file
->f_vfsmnt
;
541 inode
= dentry
->d_inode
;
544 if (!S_ISDIR(inode
->i_mode
))
547 error
= permission(inode
, MAY_EXEC
, NULL
);
549 set_fs_pwd(current
->fs
, mnt
, dentry
);
556 asmlinkage
long sys_chroot(const char __user
* filename
)
561 error
= __user_walk(filename
, LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
| LOOKUP_NOALT
, &nd
);
565 error
= permission(nd
.dentry
->d_inode
,MAY_EXEC
,&nd
);
570 if (!capable(CAP_SYS_CHROOT
))
573 set_fs_root(current
->fs
, nd
.mnt
, nd
.dentry
);
582 asmlinkage
long sys_fchmod(unsigned int fd
, mode_t mode
)
584 struct inode
* inode
;
585 struct dentry
* dentry
;
588 struct iattr newattrs
;
594 dentry
= file
->f_dentry
;
595 inode
= dentry
->d_inode
;
598 if (IS_RDONLY(inode
))
601 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
604 if (mode
== (mode_t
) -1)
605 mode
= inode
->i_mode
;
606 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
607 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
608 err
= notify_change(dentry
, &newattrs
);
617 asmlinkage
long sys_chmod(const char __user
* filename
, mode_t mode
)
620 struct inode
* inode
;
622 struct iattr newattrs
;
624 error
= user_path_walk(filename
, &nd
);
627 inode
= nd
.dentry
->d_inode
;
630 if (IS_RDONLY(inode
))
634 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
638 if (mode
== (mode_t
) -1)
639 mode
= inode
->i_mode
;
640 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
641 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
642 error
= notify_change(nd
.dentry
, &newattrs
);
651 static int chown_common(struct dentry
* dentry
, uid_t user
, gid_t group
)
653 struct inode
* inode
;
655 struct iattr newattrs
;
658 if (!(inode
= dentry
->d_inode
)) {
659 printk(KERN_ERR
"chown_common: NULL inode\n");
663 if (IS_RDONLY(inode
))
666 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
668 newattrs
.ia_valid
= ATTR_CTIME
;
669 if (user
!= (uid_t
) -1) {
670 newattrs
.ia_valid
|= ATTR_UID
;
671 newattrs
.ia_uid
= user
;
673 if (group
!= (gid_t
) -1) {
674 newattrs
.ia_valid
|= ATTR_GID
;
675 newattrs
.ia_gid
= group
;
677 if (!S_ISDIR(inode
->i_mode
))
678 newattrs
.ia_valid
|= ATTR_KILL_SUID
|ATTR_KILL_SGID
;
680 error
= notify_change(dentry
, &newattrs
);
686 asmlinkage
long sys_chown(const char __user
* filename
, uid_t user
, gid_t group
)
691 error
= user_path_walk(filename
, &nd
);
693 error
= chown_common(nd
.dentry
, user
, group
);
699 asmlinkage
long sys_lchown(const char __user
* filename
, uid_t user
, gid_t group
)
704 error
= user_path_walk_link(filename
, &nd
);
706 error
= chown_common(nd
.dentry
, user
, group
);
713 asmlinkage
long sys_fchown(unsigned int fd
, uid_t user
, gid_t group
)
720 error
= chown_common(file
->f_dentry
, user
, group
);
727 * Note that while the flag value (low two bits) for sys_open means:
733 * 00 - no permissions needed
734 * 01 - read-permission
735 * 10 - write-permission
737 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
740 struct file
*filp_open(const char * filename
, int flags
, int mode
)
742 int namei_flags
, error
;
746 if ((namei_flags
+1) & O_ACCMODE
)
748 if (namei_flags
& O_TRUNC
)
751 error
= open_namei(filename
, namei_flags
, mode
, &nd
);
753 return dentry_open(nd
.dentry
, nd
.mnt
, flags
);
755 return ERR_PTR(error
);
758 EXPORT_SYMBOL(filp_open
);
760 struct file
*dentry_open(struct dentry
*dentry
, struct vfsmount
*mnt
, int flags
)
767 f
= get_empty_filp();
771 f
->f_mode
= (flags
+1) & O_ACCMODE
;
772 inode
= dentry
->d_inode
;
773 if (f
->f_mode
& FMODE_WRITE
) {
774 error
= get_write_access(inode
);
779 f
->f_mapping
= inode
->i_mapping
;
780 file_ra_state_init(&f
->f_ra
, f
->f_mapping
);
781 f
->f_dentry
= dentry
;
784 f
->f_op
= fops_get(inode
->i_fop
);
785 file_move(f
, &inode
->i_sb
->s_files
);
787 if (f
->f_op
&& f
->f_op
->open
) {
788 error
= f
->f_op
->open(inode
,f
);
792 f
->f_flags
&= ~(O_CREAT
| O_EXCL
| O_NOCTTY
| O_TRUNC
);
794 /* NB: we're sure to have correct a_ops only after f_op->open */
795 if (f
->f_flags
& O_DIRECT
) {
796 if (!f
->f_mapping
|| !f
->f_mapping
->a_ops
||
797 !f
->f_mapping
->a_ops
->direct_IO
) {
799 f
= ERR_PTR(-EINVAL
);
807 if (f
->f_mode
& FMODE_WRITE
)
808 put_write_access(inode
);
817 return ERR_PTR(error
);
820 EXPORT_SYMBOL(dentry_open
);
823 * Find an empty file descriptor entry, and mark it busy.
825 int get_unused_fd(void)
827 struct files_struct
* files
= current
->files
;
831 spin_lock(&files
->file_lock
);
834 fd
= find_next_zero_bit(files
->open_fds
->fds_bits
,
839 * N.B. For clone tasks sharing a files structure, this test
840 * will limit the total number of files that can be opened.
842 if (fd
>= current
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
845 /* Do we need to expand the fdset array? */
846 if (fd
>= files
->max_fdset
) {
847 error
= expand_fdset(files
, fd
);
856 * Check whether we need to expand the fd array.
858 if (fd
>= files
->max_fds
) {
859 error
= expand_fd_array(files
, fd
);
867 FD_SET(fd
, files
->open_fds
);
868 FD_CLR(fd
, files
->close_on_exec
);
869 files
->next_fd
= fd
+ 1;
872 if (files
->fd
[fd
] != NULL
) {
873 printk(KERN_WARNING
"get_unused_fd: slot %d not NULL!\n", fd
);
874 files
->fd
[fd
] = NULL
;
880 spin_unlock(&files
->file_lock
);
884 EXPORT_SYMBOL(get_unused_fd
);
886 static inline void __put_unused_fd(struct files_struct
*files
, unsigned int fd
)
888 __FD_CLR(fd
, files
->open_fds
);
889 if (fd
< files
->next_fd
)
893 void fastcall
put_unused_fd(unsigned int fd
)
895 struct files_struct
*files
= current
->files
;
896 spin_lock(&files
->file_lock
);
897 __put_unused_fd(files
, fd
);
898 spin_unlock(&files
->file_lock
);
901 EXPORT_SYMBOL(put_unused_fd
);
904 * Install a file pointer in the fd array.
906 * The VFS is full of places where we drop the files lock between
907 * setting the open_fds bitmap and installing the file in the file
908 * array. At any such point, we are vulnerable to a dup2() race
909 * installing a file in the array before us. We need to detect this and
910 * fput() the struct file we are about to overwrite in this case.
912 * It should never happen - if we allow dup2() do it, _really_ bad things
916 void fastcall
fd_install(unsigned int fd
, struct file
* file
)
918 struct files_struct
*files
= current
->files
;
919 spin_lock(&files
->file_lock
);
920 if (unlikely(files
->fd
[fd
] != NULL
))
922 files
->fd
[fd
] = file
;
923 spin_unlock(&files
->file_lock
);
926 EXPORT_SYMBOL(fd_install
);
928 asmlinkage
long sys_open(const char __user
* filename
, int flags
, int mode
)
933 #if BITS_PER_LONG != 32
934 flags
|= O_LARGEFILE
;
936 tmp
= getname(filename
);
939 fd
= get_unused_fd();
941 struct file
*f
= filp_open(tmp
, flags
, mode
);
957 EXPORT_SYMBOL_GPL(sys_open
);
962 * For backward compatibility? Maybe this should be moved
963 * into arch/i386 instead?
965 asmlinkage
long sys_creat(const char __user
* pathname
, int mode
)
967 return sys_open(pathname
, O_CREAT
| O_WRONLY
| O_TRUNC
, mode
);
973 * "id" is the POSIX thread ID. We use the
974 * files pointer for this..
976 int filp_close(struct file
*filp
, fl_owner_t id
)
980 /* Report and clear outstanding errors */
981 retval
= filp
->f_error
;
985 if (!file_count(filp
)) {
986 printk(KERN_ERR
"VFS: Close: file count is 0\n");
990 if (filp
->f_op
&& filp
->f_op
->flush
) {
991 int err
= filp
->f_op
->flush(filp
);
996 dnotify_flush(filp
, id
);
997 locks_remove_posix(filp
, id
);
1002 EXPORT_SYMBOL(filp_close
);
1005 * Careful here! We test whether the file pointer is NULL before
1006 * releasing the fd. This ensures that one clone task can't release
1007 * an fd while another clone is opening it.
1009 asmlinkage
long sys_close(unsigned int fd
)
1012 struct files_struct
*files
= current
->files
;
1014 spin_lock(&files
->file_lock
);
1015 if (fd
>= files
->max_fds
)
1017 filp
= files
->fd
[fd
];
1020 files
->fd
[fd
] = NULL
;
1021 FD_CLR(fd
, files
->close_on_exec
);
1022 __put_unused_fd(files
, fd
);
1023 spin_unlock(&files
->file_lock
);
1024 return filp_close(filp
, files
);
1027 spin_unlock(&files
->file_lock
);
1031 EXPORT_SYMBOL(sys_close
);
1034 * This routine simulates a hangup on the tty, to arrange that users
1035 * are given clean terminals at login time.
1037 asmlinkage
long sys_vhangup(void)
1039 if (capable(CAP_SYS_TTY_CONFIG
)) {
1040 tty_vhangup(current
->tty
);
1047 * Called when an inode is about to be open.
1048 * We use this to disallow opening large files on 32bit systems if
1049 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1050 * on this flag in sys_open.
1052 int generic_file_open(struct inode
* inode
, struct file
* filp
)
1054 if (!(filp
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
1059 EXPORT_SYMBOL(generic_file_open
);