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;
215 down(&dentry->d_inode->i_sem);
216 down_write(&dentry->d_inode->i_alloc_sem);
217 err = notify_change(dentry, &newattrs);
218 up_write(&dentry->d_inode->i_alloc_sem);
219 up(&dentry->d_inode->i_sem);
223 static inline long do_sys_truncate(const char __user * path, loff_t length)
226 struct inode * inode;
230 if (length < 0) /* sorry, but loff_t says... */
233 error = user_path_walk(path, &nd);
236 inode = nd.dentry->d_inode;
238 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
240 if (S_ISDIR(inode->i_mode))
244 if (!S_ISREG(inode->i_mode))
247 error = permission(inode,MAY_WRITE,&nd);
252 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
256 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
260 * Make sure that there are no leases.
262 error = break_lease(inode, FMODE_WRITE);
266 error = get_write_access(inode);
270 error = locks_verify_truncate(inode, NULL, length);
273 error = do_truncate(nd.dentry, length);
275 put_write_access(inode);
283 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
285 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
286 return do_sys_truncate(path, (long)length);
289 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
291 struct inode * inode;
292 struct dentry *dentry;
304 /* explicitly opened as large or we are on 64-bit box */
305 if (file->f_flags & O_LARGEFILE)
308 dentry = file->f_dentry;
309 inode = dentry->d_inode;
311 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
315 /* Cannot ftruncate over 2^31 bytes without large file support */
316 if (small && length > MAX_NON_LFS)
320 if (IS_APPEND(inode))
323 error = locks_verify_truncate(inode, file, length);
325 error = do_truncate(dentry, length);
332 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
334 return do_sys_ftruncate(fd, length, 1);
337 /* LFS versions of truncate are only needed on 32 bit machines */
338 #if BITS_PER_LONG == 32
339 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
341 return do_sys_truncate(path, length);
344 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
346 return do_sys_ftruncate(fd, length, 0);
350 #ifdef __ARCH_WANT_SYS_UTIME
353 * sys_utime() can be implemented in user-level using sys_utimes().
354 * Is this for backwards compatibility? If so, why not move it
355 * into the appropriate arch directory (for those architectures that
359 /* If times==NULL, set access and modification to current time,
360 * must be owner or have write permission.
361 * Else, update from *times, must be owner or super user.
363 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
367 struct inode * inode;
368 struct iattr newattrs;
370 error = user_path_walk(filename, &nd);
373 inode = nd.dentry->d_inode;
376 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
379 /* Don't worry, the checks are done in inode_change_ok() */
380 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
383 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
386 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
387 newattrs.ia_atime.tv_nsec = 0;
389 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
390 newattrs.ia_mtime.tv_nsec = 0;
394 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
397 if (IS_IMMUTABLE(inode))
400 if (current->fsuid != inode->i_uid &&
401 (error = permission(inode,MAY_WRITE,&nd)) != 0)
405 error = notify_change(nd.dentry, &newattrs);
415 /* If times==NULL, set access and modification to current time,
416 * must be owner or have write permission.
417 * Else, update from *times, must be owner or super user.
419 long do_utimes(char __user * filename, struct timeval * times)
423 struct inode * inode;
424 struct iattr newattrs;
426 error = user_path_walk(filename, &nd);
430 inode = nd.dentry->d_inode;
433 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
436 /* Don't worry, the checks are done in inode_change_ok() */
437 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
440 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
443 newattrs.ia_atime.tv_sec = times[0].tv_sec;
444 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
445 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
446 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
447 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
450 if (IS_IMMUTABLE(inode))
453 if (current->fsuid != inode->i_uid &&
454 (error = permission(inode,MAY_WRITE,&nd)) != 0)
458 error = notify_change(nd.dentry, &newattrs);
466 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
468 struct timeval times[2];
470 if (utimes && copy_from_user(×, utimes, sizeof(times)))
472 return do_utimes(filename, utimes ? times : NULL);
477 * access() needs to use the real uid/gid, not the effective uid/gid.
478 * We do this by temporarily clearing all FS-related capabilities and
479 * switching the fsuid/fsgid around to the real ones.
481 asmlinkage long sys_access(const char __user * filename, int mode)
484 int old_fsuid, old_fsgid;
485 kernel_cap_t old_cap;
488 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
491 old_fsuid = current->fsuid;
492 old_fsgid = current->fsgid;
493 old_cap = current->cap_effective;
495 current->fsuid = current->uid;
496 current->fsgid = current->gid;
499 * Clear the capabilities if we switch to a non-root user
501 * FIXME: There is a race here against sys_capset. The
502 * capabilities can change yet we will restore the old
503 * value below. We should hold task_capabilities_lock,
504 * but we cannot because user_path_walk can sleep.
507 cap_clear(current->cap_effective);
509 current->cap_effective = current->cap_permitted;
511 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
513 res = permission(nd.dentry->d_inode, mode, &nd);
514 /* SuS v2 requires we report a read only fs too */
515 if(!res && (mode & S_IWOTH)
516 && (IS_RDONLY(nd.dentry->d_inode) || MNT_IS_RDONLY(nd.mnt))
517 && !special_file(nd.dentry->d_inode->i_mode))
522 current->fsuid = old_fsuid;
523 current->fsgid = old_fsgid;
524 current->cap_effective = old_cap;
529 asmlinkage long sys_chdir(const char __user * filename)
534 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
538 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
542 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
550 EXPORT_SYMBOL_GPL(sys_chdir);
552 asmlinkage long sys_fchdir(unsigned int fd)
555 struct dentry *dentry;
557 struct vfsmount *mnt;
565 dentry = file->f_dentry;
566 mnt = file->f_vfsmnt;
567 inode = dentry->d_inode;
570 if (!S_ISDIR(inode->i_mode))
573 error = permission(inode, MAY_EXEC, NULL);
575 set_fs_pwd(current->fs, mnt, dentry);
582 asmlinkage long sys_chroot(const char __user * filename)
587 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
591 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
596 if (!capable(CAP_SYS_CHROOT))
599 set_fs_root(current->fs, nd.mnt, nd.dentry);
608 EXPORT_SYMBOL_GPL(sys_chroot);
610 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
612 struct inode * inode;
613 struct dentry * dentry;
616 struct iattr newattrs;
622 dentry = file->f_dentry;
623 inode = dentry->d_inode;
626 if (IS_RDONLY(inode) || (file && MNT_IS_RDONLY(file->f_vfsmnt)))
629 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
632 if (mode == (mode_t) -1)
633 mode = inode->i_mode;
634 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
635 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
636 err = notify_change(dentry, &newattrs);
645 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
648 struct inode * inode;
650 struct iattr newattrs;
652 error = user_path_walk(filename, &nd);
655 inode = nd.dentry->d_inode;
658 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
662 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
666 if (mode == (mode_t) -1)
667 mode = inode->i_mode;
668 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
669 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
670 error = notify_change(nd.dentry, &newattrs);
679 static int chown_common(struct dentry *dentry, struct vfsmount *mnt,
680 uid_t user, gid_t group)
682 struct inode * inode;
684 struct iattr newattrs;
687 if (!(inode = dentry->d_inode)) {
688 printk(KERN_ERR "chown_common: NULL inode\n");
692 if (IS_RDONLY(inode) || MNT_IS_RDONLY(mnt))
695 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
698 newattrs.ia_valid = ATTR_CTIME;
699 if (user != (uid_t) -1) {
700 newattrs.ia_valid |= ATTR_UID;
701 newattrs.ia_uid = vx_map_uid(user);
703 if (group != (gid_t) -1) {
704 newattrs.ia_valid |= ATTR_GID;
705 newattrs.ia_gid = vx_map_gid(group);
707 if (!S_ISDIR(inode->i_mode))
708 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
710 error = notify_change(dentry, &newattrs);
716 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
721 error = user_path_walk(filename, &nd);
723 error = chown_common(nd.dentry, nd.mnt, user, group);
729 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
734 error = user_path_walk_link(filename, &nd);
736 error = chown_common(nd.dentry, nd.mnt, user, group);
743 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
750 error = chown_common(file->f_dentry, file->f_vfsmnt, user, group);
757 * Note that while the flag value (low two bits) for sys_open means:
763 * 00 - no permissions needed
764 * 01 - read-permission
765 * 10 - write-permission
767 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
770 struct file *filp_open(const char * filename, int flags, int mode)
772 int namei_flags, error;
776 if ((namei_flags+1) & O_ACCMODE)
778 if (namei_flags & O_TRUNC)
781 error = open_namei(filename, namei_flags, mode, &nd);
783 return dentry_open(nd.dentry, nd.mnt, flags);
785 return ERR_PTR(error);
788 EXPORT_SYMBOL(filp_open);
790 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
797 f = get_empty_filp();
801 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
802 inode = dentry->d_inode;
803 if (f->f_mode & FMODE_WRITE) {
804 error = get_write_access(inode);
809 f->f_mapping = inode->i_mapping;
810 f->f_dentry = dentry;
813 f->f_op = fops_get(inode->i_fop);
814 file_move(f, &inode->i_sb->s_files);
816 if (f->f_op && f->f_op->open) {
817 error = f->f_op->open(inode,f);
821 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
823 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
825 /* NB: we're sure to have correct a_ops only after f_op->open */
826 if (f->f_flags & O_DIRECT) {
827 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO) {
829 f = ERR_PTR(-EINVAL);
837 if (f->f_mode & FMODE_WRITE)
838 put_write_access(inode);
847 return ERR_PTR(error);
850 EXPORT_SYMBOL(dentry_open);
853 * Find an empty file descriptor entry, and mark it busy.
855 int get_unused_fd(void)
857 struct files_struct * files = current->files;
861 spin_lock(&files->file_lock);
864 fd = find_next_zero_bit(files->open_fds->fds_bits,
869 * N.B. For clone tasks sharing a files structure, this test
870 * will limit the total number of files that can be opened.
872 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
875 /* Do we need to expand the fdset array? */
876 if (fd >= files->max_fdset) {
877 error = expand_fdset(files, fd);
886 * Check whether we need to expand the fd array.
888 if (fd >= files->max_fds) {
889 error = expand_fd_array(files, fd);
897 FD_SET(fd, files->open_fds);
898 FD_CLR(fd, files->close_on_exec);
899 files->next_fd = fd + 1;
900 // vx_openfd_inc(fd);
903 if (files->fd[fd] != NULL) {
904 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
905 files->fd[fd] = NULL;
911 spin_unlock(&files->file_lock);
915 EXPORT_SYMBOL(get_unused_fd);
917 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
919 __FD_CLR(fd, files->open_fds);
920 if (fd < files->next_fd)
922 // vx_openfd_dec(fd);
925 void fastcall put_unused_fd(unsigned int fd)
927 struct files_struct *files = current->files;
928 spin_lock(&files->file_lock);
929 __put_unused_fd(files, fd);
930 spin_unlock(&files->file_lock);
933 EXPORT_SYMBOL(put_unused_fd);
936 * Install a file pointer in the fd array.
938 * The VFS is full of places where we drop the files lock between
939 * setting the open_fds bitmap and installing the file in the file
940 * array. At any such point, we are vulnerable to a dup2() race
941 * installing a file in the array before us. We need to detect this and
942 * fput() the struct file we are about to overwrite in this case.
944 * It should never happen - if we allow dup2() do it, _really_ bad things
948 void fastcall fd_install(unsigned int fd, struct file * file)
950 struct files_struct *files = current->files;
951 spin_lock(&files->file_lock);
952 if (unlikely(files->fd[fd] != NULL))
954 files->fd[fd] = file;
955 spin_unlock(&files->file_lock);
958 EXPORT_SYMBOL(fd_install);
960 asmlinkage long sys_open(const char __user * filename, int flags, int mode)
965 #if BITS_PER_LONG != 32
966 flags |= O_LARGEFILE;
968 tmp = getname(filename);
971 fd = get_unused_fd();
973 struct file *f = filp_open(tmp, flags, mode);
993 * For backward compatibility? Maybe this should be moved
994 * into arch/i386 instead?
996 asmlinkage long sys_creat(const char __user * pathname, int mode)
998 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1004 * "id" is the POSIX thread ID. We use the
1005 * files pointer for this..
1007 int filp_close(struct file *filp, fl_owner_t id)
1011 /* Report and clear outstanding errors */
1012 retval = filp->f_error;
1016 if (!file_count(filp)) {
1017 printk(KERN_ERR "VFS: Close: file count is 0\n");
1021 if (filp->f_op && filp->f_op->flush) {
1022 int err = filp->f_op->flush(filp);
1027 dnotify_flush(filp, id);
1028 locks_remove_posix(filp, id);
1033 EXPORT_SYMBOL(filp_close);
1036 * Careful here! We test whether the file pointer is NULL before
1037 * releasing the fd. This ensures that one clone task can't release
1038 * an fd while another clone is opening it.
1040 asmlinkage long sys_close(unsigned int fd)
1043 struct files_struct *files = current->files;
1045 spin_lock(&files->file_lock);
1046 if (fd >= files->max_fds)
1048 filp = files->fd[fd];
1051 files->fd[fd] = NULL;
1052 FD_CLR(fd, files->close_on_exec);
1053 __put_unused_fd(files, fd);
1054 spin_unlock(&files->file_lock);
1055 return filp_close(filp, files);
1058 spin_unlock(&files->file_lock);
1062 EXPORT_SYMBOL(sys_close);
1065 * This routine simulates a hangup on the tty, to arrange that users
1066 * are given clean terminals at login time.
1068 asmlinkage long sys_vhangup(void)
1070 if (capable(CAP_SYS_TTY_CONFIG)) {
1071 tty_vhangup(current->signal->tty);
1078 * Called when an inode is about to be open.
1079 * We use this to disallow opening large files on 32bit systems if
1080 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1081 * on this flag in sys_open.
1083 int generic_file_open(struct inode * inode, struct file * filp)
1085 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1090 EXPORT_SYMBOL(generic_file_open);
1093 * This is used by subsystems that don't want seekable
1096 int nonseekable_open(struct inode *inode, struct file *filp)
1098 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1102 EXPORT_SYMBOL(nonseekable_open);