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/vs_base.h>
26 #include <linux/vs_limit.h>
27 #include <linux/vs_dlimit.h>
28 #include <linux/vserver/xid.h>
29 #include <linux/syscalls.h>
30 #include <linux/vs_limit.h>
31 #include <linux/vs_dlimit.h>
32 #include <linux/vserver/xid.h>
34 #include <asm/unistd.h>
36 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
42 if (sb->s_op->statfs) {
43 memset(buf, 0, sizeof(*buf));
44 retval = security_sb_statfs(sb);
47 retval = sb->s_op->statfs(sb, buf);
48 if (retval == 0 && buf->f_frsize == 0)
49 buf->f_frsize = buf->f_bsize;
51 if (!vx_check(0, VX_ADMIN|VX_WATCH))
52 vx_vsi_statfs(sb, buf);
57 EXPORT_SYMBOL(vfs_statfs);
59 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
64 retval = vfs_statfs(sb, &st);
68 if (sizeof(*buf) == sizeof(st))
69 memcpy(buf, &st, sizeof(st));
71 if (sizeof buf->f_blocks == 4) {
72 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
73 0xffffffff00000000ULL)
76 * f_files and f_ffree may be -1; it's okay to stuff
79 if (st.f_files != -1 &&
80 (st.f_files & 0xffffffff00000000ULL))
82 if (st.f_ffree != -1 &&
83 (st.f_ffree & 0xffffffff00000000ULL))
87 buf->f_type = st.f_type;
88 buf->f_bsize = st.f_bsize;
89 buf->f_blocks = st.f_blocks;
90 buf->f_bfree = st.f_bfree;
91 buf->f_bavail = st.f_bavail;
92 buf->f_files = st.f_files;
93 buf->f_ffree = st.f_ffree;
94 buf->f_fsid = st.f_fsid;
95 buf->f_namelen = st.f_namelen;
96 buf->f_frsize = st.f_frsize;
97 memset(buf->f_spare, 0, sizeof(buf->f_spare));
102 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
107 retval = vfs_statfs(sb, &st);
111 if (sizeof(*buf) == sizeof(st))
112 memcpy(buf, &st, sizeof(st));
114 buf->f_type = st.f_type;
115 buf->f_bsize = st.f_bsize;
116 buf->f_blocks = st.f_blocks;
117 buf->f_bfree = st.f_bfree;
118 buf->f_bavail = st.f_bavail;
119 buf->f_files = st.f_files;
120 buf->f_ffree = st.f_ffree;
121 buf->f_fsid = st.f_fsid;
122 buf->f_namelen = st.f_namelen;
123 buf->f_frsize = st.f_frsize;
124 memset(buf->f_spare, 0, sizeof(buf->f_spare));
129 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
134 error = user_path_walk(path, &nd);
137 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
138 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
146 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
151 if (sz != sizeof(*buf))
153 error = user_path_walk(path, &nd);
156 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
157 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
165 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
175 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
176 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
183 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
189 if (sz != sizeof(*buf))
196 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
197 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
204 int do_truncate(struct dentry *dentry, loff_t length)
207 struct iattr newattrs;
209 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
213 newattrs.ia_size = length;
214 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
216 down(&dentry->d_inode->i_sem);
217 err = notify_change(dentry, &newattrs);
218 up(&dentry->d_inode->i_sem);
222 static inline long do_sys_truncate(const char __user * path, loff_t length)
225 struct inode * inode;
229 if (length < 0) /* sorry, but loff_t says... */
232 error = user_path_walk(path, &nd);
235 inode = nd.dentry->d_inode;
237 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
239 if (S_ISDIR(inode->i_mode))
243 if (!S_ISREG(inode->i_mode))
246 error = permission(inode,MAY_WRITE,&nd);
251 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
255 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
259 * Make sure that there are no leases.
261 error = break_lease(inode, FMODE_WRITE);
265 error = get_write_access(inode);
269 error = locks_verify_truncate(inode, NULL, length);
272 error = do_truncate(nd.dentry, length);
274 put_write_access(inode);
282 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
284 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
285 return do_sys_truncate(path, (long)length);
288 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
290 struct inode * inode;
291 struct dentry *dentry;
303 /* explicitly opened as large or we are on 64-bit box */
304 if (file->f_flags & O_LARGEFILE)
307 dentry = file->f_dentry;
308 inode = dentry->d_inode;
310 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
314 /* Cannot ftruncate over 2^31 bytes without large file support */
315 if (small && length > MAX_NON_LFS)
319 if (IS_APPEND(inode))
322 error = locks_verify_truncate(inode, file, length);
324 error = do_truncate(dentry, length);
331 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
333 return do_sys_ftruncate(fd, length, 1);
336 /* LFS versions of truncate are only needed on 32 bit machines */
337 #if BITS_PER_LONG == 32
338 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
340 return do_sys_truncate(path, length);
343 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
345 return do_sys_ftruncate(fd, length, 0);
349 #ifdef __ARCH_WANT_SYS_UTIME
352 * sys_utime() can be implemented in user-level using sys_utimes().
353 * Is this for backwards compatibility? If so, why not move it
354 * into the appropriate arch directory (for those architectures that
358 /* If times==NULL, set access and modification to current time,
359 * must be owner or have write permission.
360 * Else, update from *times, must be owner or super user.
362 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
366 struct inode * inode;
367 struct iattr newattrs;
369 error = user_path_walk(filename, &nd);
372 inode = nd.dentry->d_inode;
375 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
378 /* Don't worry, the checks are done in inode_change_ok() */
379 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
382 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
385 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
386 newattrs.ia_atime.tv_nsec = 0;
388 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
389 newattrs.ia_mtime.tv_nsec = 0;
393 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
396 if (IS_IMMUTABLE(inode))
399 if (current->fsuid != inode->i_uid &&
400 (error = permission(inode,MAY_WRITE,&nd)) != 0)
404 error = notify_change(nd.dentry, &newattrs);
414 /* If times==NULL, set access and modification to current time,
415 * must be owner or have write permission.
416 * Else, update from *times, must be owner or super user.
418 long do_utimes(char __user * filename, struct timeval * times)
422 struct inode * inode;
423 struct iattr newattrs;
425 error = user_path_walk(filename, &nd);
429 inode = nd.dentry->d_inode;
432 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
435 /* Don't worry, the checks are done in inode_change_ok() */
436 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
439 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
442 newattrs.ia_atime.tv_sec = times[0].tv_sec;
443 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
444 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
445 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
446 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
449 if (IS_IMMUTABLE(inode))
452 if (current->fsuid != inode->i_uid &&
453 (error = permission(inode,MAY_WRITE,&nd)) != 0)
457 error = notify_change(nd.dentry, &newattrs);
465 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
467 struct timeval times[2];
469 if (utimes && copy_from_user(×, utimes, sizeof(times)))
471 return do_utimes(filename, utimes ? times : NULL);
476 * access() needs to use the real uid/gid, not the effective uid/gid.
477 * We do this by temporarily clearing all FS-related capabilities and
478 * switching the fsuid/fsgid around to the real ones.
480 asmlinkage long sys_access(const char __user * filename, int mode)
483 int old_fsuid, old_fsgid;
484 kernel_cap_t old_cap;
487 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
490 old_fsuid = current->fsuid;
491 old_fsgid = current->fsgid;
492 old_cap = current->cap_effective;
494 current->fsuid = current->uid;
495 current->fsgid = current->gid;
498 * Clear the capabilities if we switch to a non-root user
500 * FIXME: There is a race here against sys_capset. The
501 * capabilities can change yet we will restore the old
502 * value below. We should hold task_capabilities_lock,
503 * but we cannot because user_path_walk can sleep.
506 cap_clear(current->cap_effective);
508 current->cap_effective = current->cap_permitted;
510 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
512 res = permission(nd.dentry->d_inode, mode, &nd);
513 /* SuS v2 requires we report a read only fs too */
514 if(!res && (mode & S_IWOTH)
515 && (IS_RDONLY(nd.dentry->d_inode) || MNT_IS_RDONLY(nd.mnt))
516 && !special_file(nd.dentry->d_inode->i_mode))
521 current->fsuid = old_fsuid;
522 current->fsgid = old_fsgid;
523 current->cap_effective = old_cap;
528 asmlinkage long sys_chdir(const char __user * filename)
533 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
537 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
541 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
549 EXPORT_SYMBOL_GPL(sys_chdir);
551 asmlinkage long sys_fchdir(unsigned int fd)
554 struct dentry *dentry;
556 struct vfsmount *mnt;
564 dentry = file->f_dentry;
565 mnt = file->f_vfsmnt;
566 inode = dentry->d_inode;
569 if (!S_ISDIR(inode->i_mode))
572 error = permission(inode, MAY_EXEC, NULL);
574 set_fs_pwd(current->fs, mnt, dentry);
581 asmlinkage long sys_chroot(const char __user * filename)
586 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
590 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
595 if (!capable(CAP_SYS_CHROOT))
598 set_fs_root(current->fs, nd.mnt, nd.dentry);
607 EXPORT_SYMBOL_GPL(sys_chroot);
609 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
611 struct inode * inode;
612 struct dentry * dentry;
615 struct iattr newattrs;
621 dentry = file->f_dentry;
622 inode = dentry->d_inode;
625 if (IS_RDONLY(inode) || (file && MNT_IS_RDONLY(file->f_vfsmnt)))
628 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
631 if (mode == (mode_t) -1)
632 mode = inode->i_mode;
633 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
634 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
635 err = notify_change(dentry, &newattrs);
644 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
647 struct inode * inode;
649 struct iattr newattrs;
651 error = user_path_walk(filename, &nd);
654 inode = nd.dentry->d_inode;
657 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
661 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
665 if (mode == (mode_t) -1)
666 mode = inode->i_mode;
667 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
668 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
669 error = notify_change(nd.dentry, &newattrs);
678 static int chown_common(struct dentry *dentry, struct vfsmount *mnt,
679 uid_t user, gid_t group)
681 struct inode * inode;
683 struct iattr newattrs;
686 if (!(inode = dentry->d_inode)) {
687 printk(KERN_ERR "chown_common: NULL inode\n");
691 if (IS_RDONLY(inode) || MNT_IS_RDONLY(mnt))
694 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
697 newattrs.ia_valid = ATTR_CTIME;
698 if (user != (uid_t) -1) {
699 newattrs.ia_valid |= ATTR_UID;
700 newattrs.ia_uid = vx_map_uid(user);
702 if (group != (gid_t) -1) {
703 newattrs.ia_valid |= ATTR_GID;
704 newattrs.ia_gid = vx_map_gid(group);
706 if (!S_ISDIR(inode->i_mode))
707 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
709 error = notify_change(dentry, &newattrs);
715 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
720 error = user_path_walk(filename, &nd);
722 error = chown_common(nd.dentry, nd.mnt, user, group);
728 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
733 error = user_path_walk_link(filename, &nd);
735 error = chown_common(nd.dentry, nd.mnt, user, group);
742 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
749 error = chown_common(file->f_dentry, file->f_vfsmnt, user, group);
756 * Note that while the flag value (low two bits) for sys_open means:
762 * 00 - no permissions needed
763 * 01 - read-permission
764 * 10 - write-permission
766 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
769 struct file *filp_open(const char * filename, int flags, int mode)
771 int namei_flags, error;
775 if ((namei_flags+1) & O_ACCMODE)
777 if (namei_flags & O_TRUNC)
780 error = open_namei(filename, namei_flags, mode, &nd);
782 return dentry_open(nd.dentry, nd.mnt, flags);
784 return ERR_PTR(error);
787 EXPORT_SYMBOL(filp_open);
789 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
796 f = get_empty_filp();
800 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
801 inode = dentry->d_inode;
802 if (f->f_mode & FMODE_WRITE) {
803 error = get_write_access(inode);
808 f->f_mapping = inode->i_mapping;
809 f->f_dentry = dentry;
812 f->f_op = fops_get(inode->i_fop);
813 file_move(f, &inode->i_sb->s_files);
815 if (f->f_op && f->f_op->open) {
816 error = f->f_op->open(inode,f);
820 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
822 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
824 /* NB: we're sure to have correct a_ops only after f_op->open */
825 if (f->f_flags & O_DIRECT) {
826 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO) {
828 f = ERR_PTR(-EINVAL);
836 if (f->f_mode & FMODE_WRITE)
837 put_write_access(inode);
846 return ERR_PTR(error);
849 EXPORT_SYMBOL(dentry_open);
852 * Find an empty file descriptor entry, and mark it busy.
854 int get_unused_fd(void)
856 struct files_struct * files = current->files;
860 spin_lock(&files->file_lock);
863 fd = find_next_zero_bit(files->open_fds->fds_bits,
868 * N.B. For clone tasks sharing a files structure, this test
869 * will limit the total number of files that can be opened.
871 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
874 /* Do we need to expand the fd array or fd set? */
875 error = expand_files(files, fd);
881 * If we needed to expand the fs array we
882 * might have blocked - try again.
888 FD_SET(fd, files->open_fds);
889 FD_CLR(fd, files->close_on_exec);
890 files->next_fd = fd + 1;
894 if (files->fd[fd] != NULL) {
895 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
896 files->fd[fd] = NULL;
902 spin_unlock(&files->file_lock);
906 EXPORT_SYMBOL(get_unused_fd);
908 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
910 __FD_CLR(fd, files->open_fds);
911 if (fd < files->next_fd)
916 void fastcall put_unused_fd(unsigned int fd)
918 struct files_struct *files = current->files;
919 spin_lock(&files->file_lock);
920 __put_unused_fd(files, fd);
921 spin_unlock(&files->file_lock);
924 EXPORT_SYMBOL(put_unused_fd);
927 * Install a file pointer in the fd array.
929 * The VFS is full of places where we drop the files lock between
930 * setting the open_fds bitmap and installing the file in the file
931 * array. At any such point, we are vulnerable to a dup2() race
932 * installing a file in the array before us. We need to detect this and
933 * fput() the struct file we are about to overwrite in this case.
935 * It should never happen - if we allow dup2() do it, _really_ bad things
939 void fastcall fd_install(unsigned int fd, struct file * file)
941 struct files_struct *files = current->files;
942 spin_lock(&files->file_lock);
943 if (unlikely(files->fd[fd] != NULL))
945 files->fd[fd] = file;
946 spin_unlock(&files->file_lock);
949 EXPORT_SYMBOL(fd_install);
951 asmlinkage long sys_open(const char __user * filename, int flags, int mode)
956 #if BITS_PER_LONG != 32
957 flags |= O_LARGEFILE;
959 tmp = getname(filename);
962 fd = get_unused_fd();
964 struct file *f = filp_open(tmp, flags, mode);
984 * For backward compatibility? Maybe this should be moved
985 * into arch/i386 instead?
987 asmlinkage long sys_creat(const char __user * pathname, int mode)
989 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
995 * "id" is the POSIX thread ID. We use the
996 * files pointer for this..
998 int filp_close(struct file *filp, fl_owner_t id)
1002 /* Report and clear outstanding errors */
1003 retval = filp->f_error;
1007 if (!file_count(filp)) {
1008 printk(KERN_ERR "VFS: Close: file count is 0\n");
1012 if (filp->f_op && filp->f_op->flush) {
1013 int err = filp->f_op->flush(filp);
1018 dnotify_flush(filp, id);
1019 locks_remove_posix(filp, id);
1024 EXPORT_SYMBOL(filp_close);
1027 * Careful here! We test whether the file pointer is NULL before
1028 * releasing the fd. This ensures that one clone task can't release
1029 * an fd while another clone is opening it.
1031 asmlinkage long sys_close(unsigned int fd)
1034 struct files_struct *files = current->files;
1036 spin_lock(&files->file_lock);
1037 if (fd >= files->max_fds)
1039 filp = files->fd[fd];
1042 files->fd[fd] = NULL;
1043 FD_CLR(fd, files->close_on_exec);
1044 __put_unused_fd(files, fd);
1045 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);
1084 * This is used by subsystems that don't want seekable
1087 int nonseekable_open(struct inode *inode, struct file *filp)
1089 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1093 EXPORT_SYMBOL(nonseekable_open);