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>
25 #include <linux/vserver/xid.h>
27 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
33 if (sb->s_op->statfs) {
34 memset(buf, 0, sizeof(*buf));
35 retval = security_sb_statfs(sb);
38 retval = sb->s_op->statfs(sb, buf);
39 if (retval == 0 && buf->f_frsize == 0)
40 buf->f_frsize = buf->f_bsize;
46 EXPORT_SYMBOL(vfs_statfs);
48 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
53 retval = vfs_statfs(sb, &st);
57 if (sizeof(*buf) == sizeof(st))
58 memcpy(buf, &st, sizeof(st));
60 if (sizeof buf->f_blocks == 4) {
61 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
62 0xffffffff00000000ULL)
65 * f_files and f_ffree may be -1; it's okay to stuff
68 if (st.f_files != -1 &&
69 (st.f_files & 0xffffffff00000000ULL))
71 if (st.f_ffree != -1 &&
72 (st.f_ffree & 0xffffffff00000000ULL))
76 buf->f_type = st.f_type;
77 buf->f_bsize = st.f_bsize;
78 buf->f_blocks = st.f_blocks;
79 buf->f_bfree = st.f_bfree;
80 buf->f_bavail = st.f_bavail;
81 buf->f_files = st.f_files;
82 buf->f_ffree = st.f_ffree;
83 buf->f_fsid = st.f_fsid;
84 buf->f_namelen = st.f_namelen;
85 buf->f_frsize = st.f_frsize;
86 memset(buf->f_spare, 0, sizeof(buf->f_spare));
91 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
96 retval = vfs_statfs(sb, &st);
100 if (sizeof(*buf) == sizeof(st))
101 memcpy(buf, &st, sizeof(st));
103 buf->f_type = st.f_type;
104 buf->f_bsize = st.f_bsize;
105 buf->f_blocks = st.f_blocks;
106 buf->f_bfree = st.f_bfree;
107 buf->f_bavail = st.f_bavail;
108 buf->f_files = st.f_files;
109 buf->f_ffree = st.f_ffree;
110 buf->f_fsid = st.f_fsid;
111 buf->f_namelen = st.f_namelen;
112 buf->f_frsize = st.f_frsize;
113 memset(buf->f_spare, 0, sizeof(buf->f_spare));
118 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
123 error = user_path_walk(path, &nd);
126 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
127 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
135 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
140 if (sz != sizeof(*buf))
142 error = user_path_walk(path, &nd);
145 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
146 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
154 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
164 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
165 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
172 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
178 if (sz != sizeof(*buf))
185 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
186 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
193 int do_truncate(struct dentry *dentry, loff_t length)
196 struct iattr newattrs;
198 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
202 newattrs.ia_size = length;
203 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
204 down(&dentry->d_inode->i_sem);
205 down_write(&dentry->d_inode->i_alloc_sem);
206 err = notify_change(dentry, &newattrs);
207 up_write(&dentry->d_inode->i_alloc_sem);
208 up(&dentry->d_inode->i_sem);
212 static inline long do_sys_truncate(const char __user * path, loff_t length)
215 struct inode * inode;
219 if (length < 0) /* sorry, but loff_t says... */
222 error = user_path_walk(path, &nd);
225 inode = nd.dentry->d_inode;
227 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
229 if (S_ISDIR(inode->i_mode))
233 if (!S_ISREG(inode->i_mode))
236 error = permission(inode,MAY_WRITE,&nd);
241 if (IS_RDONLY(inode))
245 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
249 * Make sure that there are no leases.
251 error = break_lease(inode, FMODE_WRITE);
255 error = get_write_access(inode);
259 error = locks_verify_truncate(inode, NULL, length);
262 error = do_truncate(nd.dentry, length);
264 put_write_access(inode);
272 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
274 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
275 return do_sys_truncate(path, (long)length);
278 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
280 struct inode * inode;
281 struct dentry *dentry;
293 /* explicitly opened as large or we are on 64-bit box */
294 if (file->f_flags & O_LARGEFILE)
297 dentry = file->f_dentry;
298 inode = dentry->d_inode;
300 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
304 /* Cannot ftruncate over 2^31 bytes without large file support */
305 if (small && length > MAX_NON_LFS)
309 if (IS_APPEND(inode))
312 error = locks_verify_truncate(inode, file, length);
314 error = do_truncate(dentry, length);
321 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
323 return do_sys_ftruncate(fd, length, 1);
326 /* LFS versions of truncate are only needed on 32 bit machines */
327 #if BITS_PER_LONG == 32
328 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
330 return do_sys_truncate(path, length);
333 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
335 return do_sys_ftruncate(fd, length, 0);
339 #if !(defined(__alpha__) || defined(__ia64__))
342 * sys_utime() can be implemented in user-level using sys_utimes().
343 * Is this for backwards compatibility? If so, why not move it
344 * into the appropriate arch directory (for those architectures that
348 /* If times==NULL, set access and modification to current time,
349 * must be owner or have write permission.
350 * Else, update from *times, must be owner or super user.
352 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
356 struct inode * inode;
357 struct iattr newattrs;
359 error = user_path_walk(filename, &nd);
362 inode = nd.dentry->d_inode;
365 if (IS_RDONLY(inode))
368 /* Don't worry, the checks are done in inode_change_ok() */
369 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
372 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
375 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
376 newattrs.ia_atime.tv_nsec = 0;
378 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
379 newattrs.ia_mtime.tv_nsec = 0;
383 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
386 if (IS_IMMUTABLE(inode))
389 if (current->fsuid != inode->i_uid &&
390 (error = permission(inode,MAY_WRITE,&nd)) != 0)
394 error = notify_change(nd.dentry, &newattrs);
404 /* If times==NULL, set access and modification to current time,
405 * must be owner or have write permission.
406 * Else, update from *times, must be owner or super user.
408 long do_utimes(char __user * filename, struct timeval * times)
412 struct inode * inode;
413 struct iattr newattrs;
415 error = user_path_walk(filename, &nd);
419 inode = nd.dentry->d_inode;
422 if (IS_RDONLY(inode))
425 /* Don't worry, the checks are done in inode_change_ok() */
426 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
429 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
432 newattrs.ia_atime.tv_sec = times[0].tv_sec;
433 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
434 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
435 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
436 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
439 if (IS_IMMUTABLE(inode))
442 if (current->fsuid != inode->i_uid &&
443 (error = permission(inode,MAY_WRITE,&nd)) != 0)
447 error = notify_change(nd.dentry, &newattrs);
455 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
457 struct timeval times[2];
459 if (utimes && copy_from_user(×, utimes, sizeof(times)))
461 return do_utimes(filename, utimes ? times : NULL);
466 * access() needs to use the real uid/gid, not the effective uid/gid.
467 * We do this by temporarily clearing all FS-related capabilities and
468 * switching the fsuid/fsgid around to the real ones.
470 asmlinkage long sys_access(const char __user * filename, int mode)
473 int old_fsuid, old_fsgid;
474 kernel_cap_t old_cap;
477 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
480 old_fsuid = current->fsuid;
481 old_fsgid = current->fsgid;
482 old_cap = current->cap_effective;
484 current->fsuid = current->uid;
485 current->fsgid = current->gid;
488 * Clear the capabilities if we switch to a non-root user
490 * FIXME: There is a race here against sys_capset. The
491 * capabilities can change yet we will restore the old
492 * value below. We should hold task_capabilities_lock,
493 * but we cannot because user_path_walk can sleep.
496 cap_clear(current->cap_effective);
498 current->cap_effective = current->cap_permitted;
500 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
502 res = permission(nd.dentry->d_inode, mode, &nd);
503 /* SuS v2 requires we report a read only fs too */
504 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
505 && !special_file(nd.dentry->d_inode->i_mode))
510 current->fsuid = old_fsuid;
511 current->fsgid = old_fsgid;
512 current->cap_effective = old_cap;
517 asmlinkage long sys_chdir(const char __user * filename)
522 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
526 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
530 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
538 asmlinkage long sys_fchdir(unsigned int fd)
541 struct dentry *dentry;
543 struct vfsmount *mnt;
551 dentry = file->f_dentry;
552 mnt = file->f_vfsmnt;
553 inode = dentry->d_inode;
556 if (!S_ISDIR(inode->i_mode))
559 error = permission(inode, MAY_EXEC, NULL);
561 set_fs_pwd(current->fs, mnt, dentry);
568 asmlinkage long sys_chroot(const char __user * filename)
573 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
577 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
582 if (!capable(CAP_SYS_CHROOT))
585 set_fs_root(current->fs, nd.mnt, nd.dentry);
594 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
596 struct inode * inode;
597 struct dentry * dentry;
600 struct iattr newattrs;
606 dentry = file->f_dentry;
607 inode = dentry->d_inode;
610 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
613 if (IS_RDONLY(inode))
616 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
619 if (mode == (mode_t) -1)
620 mode = inode->i_mode;
621 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
622 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
623 err = notify_change(dentry, &newattrs);
632 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
635 struct inode * inode;
637 struct iattr newattrs;
639 error = user_path_walk(filename, &nd);
642 inode = nd.dentry->d_inode;
645 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
649 if (IS_RDONLY(inode))
653 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
657 if (mode == (mode_t) -1)
658 mode = inode->i_mode;
659 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
660 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
661 error = notify_change(nd.dentry, &newattrs);
670 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
672 struct inode * inode;
674 struct iattr newattrs;
677 if (!(inode = dentry->d_inode)) {
678 printk(KERN_ERR "chown_common: NULL inode\n");
682 if (IS_RDONLY(inode))
685 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
688 newattrs.ia_valid = ATTR_CTIME;
689 if (user != (uid_t) -1) {
690 newattrs.ia_valid |= ATTR_UID;
691 newattrs.ia_uid = vx_map_uid(user);
693 if (group != (gid_t) -1) {
694 newattrs.ia_valid |= ATTR_GID;
695 newattrs.ia_gid = vx_map_gid(group);
697 if (!S_ISDIR(inode->i_mode))
698 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
700 error = notify_change(dentry, &newattrs);
706 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
711 error = user_path_walk(filename, &nd);
713 error = chown_common(nd.dentry, user, group);
719 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
724 error = user_path_walk_link(filename, &nd);
726 error = chown_common(nd.dentry, user, group);
733 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
740 error = chown_common(file->f_dentry, user, group);
747 * Note that while the flag value (low two bits) for sys_open means:
753 * 00 - no permissions needed
754 * 01 - read-permission
755 * 10 - write-permission
757 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
760 struct file *filp_open(const char * filename, int flags, int mode)
762 int namei_flags, error;
766 if ((namei_flags+1) & O_ACCMODE)
768 if (namei_flags & O_TRUNC)
771 error = open_namei(filename, namei_flags, mode, &nd);
773 return dentry_open(nd.dentry, nd.mnt, flags);
775 return ERR_PTR(error);
778 EXPORT_SYMBOL(filp_open);
780 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
787 f = get_empty_filp();
791 f->f_mode = (flags+1) & O_ACCMODE;
792 inode = dentry->d_inode;
793 if (f->f_mode & FMODE_WRITE) {
794 error = get_write_access(inode);
799 f->f_mapping = inode->i_mapping;
800 file_ra_state_init(&f->f_ra, f->f_mapping);
801 f->f_dentry = dentry;
804 f->f_op = fops_get(inode->i_fop);
805 file_move(f, &inode->i_sb->s_files);
807 if (f->f_op && f->f_op->open) {
808 error = f->f_op->open(inode,f);
812 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
814 /* NB: we're sure to have correct a_ops only after f_op->open */
815 if (f->f_flags & O_DIRECT) {
816 if (!f->f_mapping || !f->f_mapping->a_ops ||
817 !f->f_mapping->a_ops->direct_IO) {
819 f = ERR_PTR(-EINVAL);
827 if (f->f_mode & FMODE_WRITE)
828 put_write_access(inode);
837 return ERR_PTR(error);
840 EXPORT_SYMBOL(dentry_open);
843 * Find an empty file descriptor entry, and mark it busy.
845 int get_unused_fd(void)
847 struct files_struct * files = current->files;
851 spin_lock(&files->file_lock);
854 fd = find_next_zero_bit(files->open_fds->fds_bits,
859 * N.B. For clone tasks sharing a files structure, this test
860 * will limit the total number of files that can be opened.
862 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
865 /* Do we need to expand the fdset array? */
866 if (fd >= files->max_fdset) {
867 error = expand_fdset(files, fd);
876 * Check whether we need to expand the fd array.
878 if (fd >= files->max_fds) {
879 error = expand_fd_array(files, fd);
887 FD_SET(fd, files->open_fds);
888 FD_CLR(fd, files->close_on_exec);
889 files->next_fd = fd + 1;
893 if (files->fd[fd] != NULL) {
894 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
895 files->fd[fd] = NULL;
901 spin_unlock(&files->file_lock);
905 EXPORT_SYMBOL(get_unused_fd);
907 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
909 __FD_CLR(fd, files->open_fds);
910 if (fd < files->next_fd)
914 void fastcall put_unused_fd(unsigned int fd)
916 struct files_struct *files = current->files;
917 spin_lock(&files->file_lock);
918 __put_unused_fd(files, fd);
919 spin_unlock(&files->file_lock);
922 EXPORT_SYMBOL(put_unused_fd);
925 * Install a file pointer in the fd array.
927 * The VFS is full of places where we drop the files lock between
928 * setting the open_fds bitmap and installing the file in the file
929 * array. At any such point, we are vulnerable to a dup2() race
930 * installing a file in the array before us. We need to detect this and
931 * fput() the struct file we are about to overwrite in this case.
933 * It should never happen - if we allow dup2() do it, _really_ bad things
937 void fastcall fd_install(unsigned int fd, struct file * file)
939 struct files_struct *files = current->files;
940 spin_lock(&files->file_lock);
941 if (unlikely(files->fd[fd] != NULL))
943 files->fd[fd] = file;
944 spin_unlock(&files->file_lock);
947 EXPORT_SYMBOL(fd_install);
949 asmlinkage long sys_open(const char __user * filename, int flags, int mode)
954 #if BITS_PER_LONG != 32
955 flags |= O_LARGEFILE;
957 tmp = getname(filename);
960 fd = get_unused_fd();
962 struct file *f = filp_open(tmp, flags, mode);
978 EXPORT_SYMBOL_GPL(sys_open);
983 * For backward compatibility? Maybe this should be moved
984 * into arch/i386 instead?
986 asmlinkage long sys_creat(const char __user * pathname, int mode)
988 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
994 * "id" is the POSIX thread ID. We use the
995 * files pointer for this..
997 int filp_close(struct file *filp, fl_owner_t id)
1001 /* Report and clear outstanding errors */
1002 retval = filp->f_error;
1006 if (!file_count(filp)) {
1007 printk(KERN_ERR "VFS: Close: file count is 0\n");
1011 if (filp->f_op && filp->f_op->flush) {
1012 int err = filp->f_op->flush(filp);
1017 dnotify_flush(filp, id);
1018 locks_remove_posix(filp, id);
1023 EXPORT_SYMBOL(filp_close);
1026 * Careful here! We test whether the file pointer is NULL before
1027 * releasing the fd. This ensures that one clone task can't release
1028 * an fd while another clone is opening it.
1030 asmlinkage long sys_close(unsigned int fd)
1033 struct files_struct *files = current->files;
1035 spin_lock(&files->file_lock);
1036 if (fd >= files->max_fds)
1038 filp = files->fd[fd];
1041 files->fd[fd] = NULL;
1042 FD_CLR(fd, files->close_on_exec);
1043 __put_unused_fd(files, fd);
1044 spin_unlock(&files->file_lock);
1046 return filp_close(filp, files);
1049 spin_unlock(&files->file_lock);
1053 EXPORT_SYMBOL(sys_close);
1056 * This routine simulates a hangup on the tty, to arrange that users
1057 * are given clean terminals at login time.
1059 asmlinkage long sys_vhangup(void)
1061 if (capable(CAP_SYS_TTY_CONFIG)) {
1062 tty_vhangup(current->signal->tty);
1069 * Called when an inode is about to be open.
1070 * We use this to disallow opening large files on 32bit systems if
1071 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1072 * on this flag in sys_open.
1074 int generic_file_open(struct inode * inode, struct file * filp)
1076 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1081 EXPORT_SYMBOL(generic_file_open);