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>
30 #include <asm/unistd.h>
32 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
38 if (sb->s_op->statfs) {
39 memset(buf, 0, sizeof(*buf));
40 retval = security_sb_statfs(sb);
43 retval = sb->s_op->statfs(sb, buf);
44 if (retval == 0 && buf->f_frsize == 0)
45 buf->f_frsize = buf->f_bsize;
51 EXPORT_SYMBOL(vfs_statfs);
53 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
58 retval = vfs_statfs(sb, &st);
62 if (sizeof(*buf) == sizeof(st))
63 memcpy(buf, &st, sizeof(st));
65 if (sizeof buf->f_blocks == 4) {
66 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
67 0xffffffff00000000ULL)
70 * f_files and f_ffree may be -1; it's okay to stuff
73 if (st.f_files != -1 &&
74 (st.f_files & 0xffffffff00000000ULL))
76 if (st.f_ffree != -1 &&
77 (st.f_ffree & 0xffffffff00000000ULL))
81 buf->f_type = st.f_type;
82 buf->f_bsize = st.f_bsize;
83 buf->f_blocks = st.f_blocks;
84 buf->f_bfree = st.f_bfree;
85 buf->f_bavail = st.f_bavail;
86 buf->f_files = st.f_files;
87 buf->f_ffree = st.f_ffree;
88 buf->f_fsid = st.f_fsid;
89 buf->f_namelen = st.f_namelen;
90 buf->f_frsize = st.f_frsize;
91 memset(buf->f_spare, 0, sizeof(buf->f_spare));
96 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
101 retval = vfs_statfs(sb, &st);
105 if (sizeof(*buf) == sizeof(st))
106 memcpy(buf, &st, sizeof(st));
108 buf->f_type = st.f_type;
109 buf->f_bsize = st.f_bsize;
110 buf->f_blocks = st.f_blocks;
111 buf->f_bfree = st.f_bfree;
112 buf->f_bavail = st.f_bavail;
113 buf->f_files = st.f_files;
114 buf->f_ffree = st.f_ffree;
115 buf->f_fsid = st.f_fsid;
116 buf->f_namelen = st.f_namelen;
117 buf->f_frsize = st.f_frsize;
118 memset(buf->f_spare, 0, sizeof(buf->f_spare));
123 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
128 error = user_path_walk(path, &nd);
131 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
132 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
140 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
145 if (sz != sizeof(*buf))
147 error = user_path_walk(path, &nd);
150 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
151 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
159 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
169 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
170 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
177 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
183 if (sz != sizeof(*buf))
190 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
191 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
198 int do_truncate(struct dentry *dentry, loff_t length)
201 struct iattr newattrs;
203 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
207 newattrs.ia_size = length;
208 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
209 down(&dentry->d_inode->i_sem);
210 down_write(&dentry->d_inode->i_alloc_sem);
211 err = notify_change(dentry, &newattrs);
212 up_write(&dentry->d_inode->i_alloc_sem);
213 up(&dentry->d_inode->i_sem);
217 static inline long do_sys_truncate(const char __user * path, loff_t length)
220 struct inode * inode;
224 if (length < 0) /* sorry, but loff_t says... */
227 error = user_path_walk(path, &nd);
230 inode = nd.dentry->d_inode;
232 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
234 if (S_ISDIR(inode->i_mode))
238 if (!S_ISREG(inode->i_mode))
241 error = permission(inode,MAY_WRITE,&nd);
246 if (IS_RDONLY(inode))
250 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
254 * Make sure that there are no leases.
256 error = break_lease(inode, FMODE_WRITE);
260 error = get_write_access(inode);
264 error = locks_verify_truncate(inode, NULL, length);
267 error = do_truncate(nd.dentry, length);
269 put_write_access(inode);
277 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
279 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
280 return do_sys_truncate(path, (long)length);
283 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
285 struct inode * inode;
286 struct dentry *dentry;
298 /* explicitly opened as large or we are on 64-bit box */
299 if (file->f_flags & O_LARGEFILE)
302 dentry = file->f_dentry;
303 inode = dentry->d_inode;
305 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
309 /* Cannot ftruncate over 2^31 bytes without large file support */
310 if (small && length > MAX_NON_LFS)
314 if (IS_APPEND(inode))
317 error = locks_verify_truncate(inode, file, length);
319 error = do_truncate(dentry, length);
326 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
328 return do_sys_ftruncate(fd, length, 1);
331 /* LFS versions of truncate are only needed on 32 bit machines */
332 #if BITS_PER_LONG == 32
333 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
335 return do_sys_truncate(path, length);
338 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
340 return do_sys_ftruncate(fd, length, 0);
344 #ifdef __ARCH_WANT_SYS_UTIME
347 * sys_utime() can be implemented in user-level using sys_utimes().
348 * Is this for backwards compatibility? If so, why not move it
349 * into the appropriate arch directory (for those architectures that
353 /* If times==NULL, set access and modification to current time,
354 * must be owner or have write permission.
355 * Else, update from *times, must be owner or super user.
357 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
361 struct inode * inode;
362 struct iattr newattrs;
364 error = user_path_walk(filename, &nd);
367 inode = nd.dentry->d_inode;
370 if (IS_RDONLY(inode))
373 /* Don't worry, the checks are done in inode_change_ok() */
374 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
377 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
380 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
381 newattrs.ia_atime.tv_nsec = 0;
383 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
384 newattrs.ia_mtime.tv_nsec = 0;
388 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
391 if (IS_IMMUTABLE(inode))
394 if (current->fsuid != inode->i_uid &&
395 (error = permission(inode,MAY_WRITE,&nd)) != 0)
399 error = notify_change(nd.dentry, &newattrs);
409 /* If times==NULL, set access and modification to current time,
410 * must be owner or have write permission.
411 * Else, update from *times, must be owner or super user.
413 long do_utimes(char __user * filename, struct timeval * times)
417 struct inode * inode;
418 struct iattr newattrs;
420 error = user_path_walk(filename, &nd);
424 inode = nd.dentry->d_inode;
427 if (IS_RDONLY(inode))
430 /* Don't worry, the checks are done in inode_change_ok() */
431 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
434 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
437 newattrs.ia_atime.tv_sec = times[0].tv_sec;
438 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
439 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
440 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
441 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
444 if (IS_IMMUTABLE(inode))
447 if (current->fsuid != inode->i_uid &&
448 (error = permission(inode,MAY_WRITE,&nd)) != 0)
452 error = notify_change(nd.dentry, &newattrs);
460 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
462 struct timeval times[2];
464 if (utimes && copy_from_user(×, utimes, sizeof(times)))
466 return do_utimes(filename, utimes ? times : NULL);
471 * access() needs to use the real uid/gid, not the effective uid/gid.
472 * We do this by temporarily clearing all FS-related capabilities and
473 * switching the fsuid/fsgid around to the real ones.
475 asmlinkage long sys_access(const char __user * filename, int mode)
478 int old_fsuid, old_fsgid;
479 kernel_cap_t old_cap;
482 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
485 old_fsuid = current->fsuid;
486 old_fsgid = current->fsgid;
487 old_cap = current->cap_effective;
489 current->fsuid = current->uid;
490 current->fsgid = current->gid;
493 * Clear the capabilities if we switch to a non-root user
495 * FIXME: There is a race here against sys_capset. The
496 * capabilities can change yet we will restore the old
497 * value below. We should hold task_capabilities_lock,
498 * but we cannot because user_path_walk can sleep.
501 cap_clear(current->cap_effective);
503 current->cap_effective = current->cap_permitted;
505 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
507 res = permission(nd.dentry->d_inode, mode, &nd);
508 /* SuS v2 requires we report a read only fs too */
509 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
510 && !special_file(nd.dentry->d_inode->i_mode))
515 current->fsuid = old_fsuid;
516 current->fsgid = old_fsgid;
517 current->cap_effective = old_cap;
522 asmlinkage long sys_chdir(const char __user * filename)
527 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
531 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
535 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
543 asmlinkage long sys_fchdir(unsigned int fd)
546 struct dentry *dentry;
548 struct vfsmount *mnt;
556 dentry = file->f_dentry;
557 mnt = file->f_vfsmnt;
558 inode = dentry->d_inode;
561 if (!S_ISDIR(inode->i_mode))
564 error = permission(inode, MAY_EXEC, NULL);
566 set_fs_pwd(current->fs, mnt, dentry);
573 asmlinkage long sys_chroot(const char __user * filename)
578 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
582 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
587 if (!capable(CAP_SYS_CHROOT))
590 set_fs_root(current->fs, nd.mnt, nd.dentry);
599 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
601 struct inode * inode;
602 struct dentry * dentry;
605 struct iattr newattrs;
611 dentry = file->f_dentry;
612 inode = dentry->d_inode;
615 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
618 if (IS_RDONLY(inode))
621 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
624 if (mode == (mode_t) -1)
625 mode = inode->i_mode;
626 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
627 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
628 err = notify_change(dentry, &newattrs);
637 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
640 struct inode * inode;
642 struct iattr newattrs;
644 error = user_path_walk(filename, &nd);
647 inode = nd.dentry->d_inode;
650 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
654 if (IS_RDONLY(inode))
658 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
662 if (mode == (mode_t) -1)
663 mode = inode->i_mode;
664 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
665 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
666 error = notify_change(nd.dentry, &newattrs);
675 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
677 struct inode * inode;
679 struct iattr newattrs;
682 if (!(inode = dentry->d_inode)) {
683 printk(KERN_ERR "chown_common: NULL inode\n");
687 if (IS_RDONLY(inode))
690 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
693 newattrs.ia_valid = ATTR_CTIME;
694 if (user != (uid_t) -1) {
695 newattrs.ia_valid |= ATTR_UID;
696 newattrs.ia_uid = vx_map_uid(user);
698 if (group != (gid_t) -1) {
699 newattrs.ia_valid |= ATTR_GID;
700 newattrs.ia_gid = vx_map_gid(group);
702 if (!S_ISDIR(inode->i_mode))
703 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
705 error = notify_change(dentry, &newattrs);
711 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
716 error = user_path_walk(filename, &nd);
718 error = chown_common(nd.dentry, user, group);
724 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
729 error = user_path_walk_link(filename, &nd);
731 error = chown_common(nd.dentry, user, group);
738 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
745 error = chown_common(file->f_dentry, user, group);
752 * Note that while the flag value (low two bits) for sys_open means:
758 * 00 - no permissions needed
759 * 01 - read-permission
760 * 10 - write-permission
762 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
765 struct file *filp_open(const char * filename, int flags, int mode)
767 int namei_flags, error;
771 if ((namei_flags+1) & O_ACCMODE)
773 if (namei_flags & O_TRUNC)
776 error = open_namei(filename, namei_flags, mode, &nd);
778 return dentry_open(nd.dentry, nd.mnt, flags);
780 return ERR_PTR(error);
783 EXPORT_SYMBOL(filp_open);
785 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
792 f = get_empty_filp();
796 f->f_mode = (flags+1) & O_ACCMODE;
797 inode = dentry->d_inode;
798 if (f->f_mode & FMODE_WRITE) {
799 error = get_write_access(inode);
804 f->f_mapping = inode->i_mapping;
805 f->f_dentry = dentry;
808 f->f_op = fops_get(inode->i_fop);
809 file_move(f, &inode->i_sb->s_files);
811 if (f->f_op && f->f_op->open) {
812 error = f->f_op->open(inode,f);
816 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
818 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
820 /* NB: we're sure to have correct a_ops only after f_op->open */
821 if (f->f_flags & O_DIRECT) {
822 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO) {
824 f = ERR_PTR(-EINVAL);
832 if (f->f_mode & FMODE_WRITE)
833 put_write_access(inode);
842 return ERR_PTR(error);
845 EXPORT_SYMBOL(dentry_open);
848 * Find an empty file descriptor entry, and mark it busy.
850 int get_unused_fd(void)
852 struct files_struct * files = current->files;
856 spin_lock(&files->file_lock);
859 fd = find_next_zero_bit(files->open_fds->fds_bits,
864 * N.B. For clone tasks sharing a files structure, this test
865 * will limit the total number of files that can be opened.
867 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
870 /* Do we need to expand the fdset array? */
871 if (fd >= files->max_fdset) {
872 error = expand_fdset(files, fd);
881 * Check whether we need to expand the fd array.
883 if (fd >= files->max_fds) {
884 error = expand_fd_array(files, fd);
892 FD_SET(fd, files->open_fds);
893 FD_CLR(fd, files->close_on_exec);
894 files->next_fd = fd + 1;
898 if (files->fd[fd] != NULL) {
899 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
900 files->fd[fd] = NULL;
906 spin_unlock(&files->file_lock);
910 EXPORT_SYMBOL(get_unused_fd);
912 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
914 __FD_CLR(fd, files->open_fds);
915 if (fd < files->next_fd)
919 void fastcall put_unused_fd(unsigned int fd)
921 struct files_struct *files = current->files;
922 spin_lock(&files->file_lock);
923 __put_unused_fd(files, fd);
924 spin_unlock(&files->file_lock);
927 EXPORT_SYMBOL(put_unused_fd);
930 * Install a file pointer in the fd array.
932 * The VFS is full of places where we drop the files lock between
933 * setting the open_fds bitmap and installing the file in the file
934 * array. At any such point, we are vulnerable to a dup2() race
935 * installing a file in the array before us. We need to detect this and
936 * fput() the struct file we are about to overwrite in this case.
938 * It should never happen - if we allow dup2() do it, _really_ bad things
942 void fastcall fd_install(unsigned int fd, struct file * file)
944 struct files_struct *files = current->files;
945 spin_lock(&files->file_lock);
946 if (unlikely(files->fd[fd] != NULL))
948 files->fd[fd] = file;
949 spin_unlock(&files->file_lock);
952 EXPORT_SYMBOL(fd_install);
954 asmlinkage long sys_open(const char __user * filename, int flags, int mode)
959 #if BITS_PER_LONG != 32
960 flags |= O_LARGEFILE;
962 tmp = getname(filename);
965 fd = get_unused_fd();
967 struct file *f = filp_open(tmp, flags, mode);
987 * For backward compatibility? Maybe this should be moved
988 * into arch/i386 instead?
990 asmlinkage long sys_creat(const char __user * pathname, int mode)
992 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
998 * "id" is the POSIX thread ID. We use the
999 * files pointer for this..
1001 int filp_close(struct file *filp, fl_owner_t id)
1005 /* Report and clear outstanding errors */
1006 retval = filp->f_error;
1010 if (!file_count(filp)) {
1011 printk(KERN_ERR "VFS: Close: file count is 0\n");
1015 if (filp->f_op && filp->f_op->flush) {
1016 int err = filp->f_op->flush(filp);
1021 dnotify_flush(filp, id);
1022 locks_remove_posix(filp, id);
1027 EXPORT_SYMBOL(filp_close);
1030 * Careful here! We test whether the file pointer is NULL before
1031 * releasing the fd. This ensures that one clone task can't release
1032 * an fd while another clone is opening it.
1034 asmlinkage long sys_close(unsigned int fd)
1037 struct files_struct *files = current->files;
1039 spin_lock(&files->file_lock);
1040 if (fd >= files->max_fds)
1042 filp = files->fd[fd];
1045 files->fd[fd] = NULL;
1046 FD_CLR(fd, files->close_on_exec);
1047 __put_unused_fd(files, fd);
1048 spin_unlock(&files->file_lock);
1050 return filp_close(filp, files);
1053 spin_unlock(&files->file_lock);
1059 * This routine simulates a hangup on the tty, to arrange that users
1060 * are given clean terminals at login time.
1062 asmlinkage long sys_vhangup(void)
1064 if (capable(CAP_SYS_TTY_CONFIG)) {
1065 tty_vhangup(current->signal->tty);
1072 * Called when an inode is about to be open.
1073 * We use this to disallow opening large files on 32bit systems if
1074 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1075 * on this flag in sys_open.
1077 int generic_file_open(struct inode * inode, struct file * filp)
1079 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1084 EXPORT_SYMBOL(generic_file_open);