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 #include <asm/unistd.h>
29 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
35 if (sb->s_op->statfs) {
36 memset(buf, 0, sizeof(*buf));
37 retval = security_sb_statfs(sb);
40 retval = sb->s_op->statfs(sb, buf);
41 if (retval == 0 && buf->f_frsize == 0)
42 buf->f_frsize = buf->f_bsize;
48 EXPORT_SYMBOL(vfs_statfs);
50 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
55 retval = vfs_statfs(sb, &st);
59 if (sizeof(*buf) == sizeof(st))
60 memcpy(buf, &st, sizeof(st));
62 if (sizeof buf->f_blocks == 4) {
63 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
64 0xffffffff00000000ULL)
67 * f_files and f_ffree may be -1; it's okay to stuff
70 if (st.f_files != -1 &&
71 (st.f_files & 0xffffffff00000000ULL))
73 if (st.f_ffree != -1 &&
74 (st.f_ffree & 0xffffffff00000000ULL))
78 buf->f_type = st.f_type;
79 buf->f_bsize = st.f_bsize;
80 buf->f_blocks = st.f_blocks;
81 buf->f_bfree = st.f_bfree;
82 buf->f_bavail = st.f_bavail;
83 buf->f_files = st.f_files;
84 buf->f_ffree = st.f_ffree;
85 buf->f_fsid = st.f_fsid;
86 buf->f_namelen = st.f_namelen;
87 buf->f_frsize = st.f_frsize;
88 memset(buf->f_spare, 0, sizeof(buf->f_spare));
93 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
98 retval = vfs_statfs(sb, &st);
102 if (sizeof(*buf) == sizeof(st))
103 memcpy(buf, &st, sizeof(st));
105 buf->f_type = st.f_type;
106 buf->f_bsize = st.f_bsize;
107 buf->f_blocks = st.f_blocks;
108 buf->f_bfree = st.f_bfree;
109 buf->f_bavail = st.f_bavail;
110 buf->f_files = st.f_files;
111 buf->f_ffree = st.f_ffree;
112 buf->f_fsid = st.f_fsid;
113 buf->f_namelen = st.f_namelen;
114 buf->f_frsize = st.f_frsize;
115 memset(buf->f_spare, 0, sizeof(buf->f_spare));
120 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
125 error = user_path_walk(path, &nd);
128 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
129 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
137 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
142 if (sz != sizeof(*buf))
144 error = user_path_walk(path, &nd);
147 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
148 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
156 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
166 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
167 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
174 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
180 if (sz != sizeof(*buf))
187 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
188 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
195 int do_truncate(struct dentry *dentry, loff_t length)
198 struct iattr newattrs;
200 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
204 newattrs.ia_size = length;
205 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
206 down(&dentry->d_inode->i_sem);
207 down_write(&dentry->d_inode->i_alloc_sem);
208 err = notify_change(dentry, &newattrs);
209 up_write(&dentry->d_inode->i_alloc_sem);
210 up(&dentry->d_inode->i_sem);
214 static inline long do_sys_truncate(const char __user * path, loff_t length)
217 struct inode * inode;
221 if (length < 0) /* sorry, but loff_t says... */
224 error = user_path_walk(path, &nd);
227 inode = nd.dentry->d_inode;
229 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
231 if (S_ISDIR(inode->i_mode))
235 if (!S_ISREG(inode->i_mode))
238 error = permission(inode,MAY_WRITE,&nd);
243 if (IS_RDONLY(inode))
247 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
251 * Make sure that there are no leases.
253 error = break_lease(inode, FMODE_WRITE);
257 error = get_write_access(inode);
261 error = locks_verify_truncate(inode, NULL, length);
264 error = do_truncate(nd.dentry, length);
266 put_write_access(inode);
274 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
276 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
277 return do_sys_truncate(path, (long)length);
280 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
282 struct inode * inode;
283 struct dentry *dentry;
295 /* explicitly opened as large or we are on 64-bit box */
296 if (file->f_flags & O_LARGEFILE)
299 dentry = file->f_dentry;
300 inode = dentry->d_inode;
302 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
306 /* Cannot ftruncate over 2^31 bytes without large file support */
307 if (small && length > MAX_NON_LFS)
311 if (IS_APPEND(inode))
314 error = locks_verify_truncate(inode, file, length);
316 error = do_truncate(dentry, length);
323 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
325 return do_sys_ftruncate(fd, length, 1);
328 /* LFS versions of truncate are only needed on 32 bit machines */
329 #if BITS_PER_LONG == 32
330 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
332 return do_sys_truncate(path, length);
335 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
337 return do_sys_ftruncate(fd, length, 0);
341 #ifdef __ARCH_WANT_SYS_UTIME
344 * sys_utime() can be implemented in user-level using sys_utimes().
345 * Is this for backwards compatibility? If so, why not move it
346 * into the appropriate arch directory (for those architectures that
350 /* If times==NULL, set access and modification to current time,
351 * must be owner or have write permission.
352 * Else, update from *times, must be owner or super user.
354 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
358 struct inode * inode;
359 struct iattr newattrs;
361 error = user_path_walk(filename, &nd);
364 inode = nd.dentry->d_inode;
367 if (IS_RDONLY(inode))
370 /* Don't worry, the checks are done in inode_change_ok() */
371 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
374 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
377 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
378 newattrs.ia_atime.tv_nsec = 0;
380 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
381 newattrs.ia_mtime.tv_nsec = 0;
385 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
388 if (IS_IMMUTABLE(inode))
391 if (current->fsuid != inode->i_uid &&
392 (error = permission(inode,MAY_WRITE,&nd)) != 0)
396 error = notify_change(nd.dentry, &newattrs);
406 /* If times==NULL, set access and modification to current time,
407 * must be owner or have write permission.
408 * Else, update from *times, must be owner or super user.
410 long do_utimes(char __user * filename, struct timeval * times)
414 struct inode * inode;
415 struct iattr newattrs;
417 error = user_path_walk(filename, &nd);
421 inode = nd.dentry->d_inode;
424 if (IS_RDONLY(inode))
427 /* Don't worry, the checks are done in inode_change_ok() */
428 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
431 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
434 newattrs.ia_atime.tv_sec = times[0].tv_sec;
435 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
436 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
437 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
438 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
441 if (IS_IMMUTABLE(inode))
444 if (current->fsuid != inode->i_uid &&
445 (error = permission(inode,MAY_WRITE,&nd)) != 0)
449 error = notify_change(nd.dentry, &newattrs);
457 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
459 struct timeval times[2];
461 if (utimes && copy_from_user(×, utimes, sizeof(times)))
463 return do_utimes(filename, utimes ? times : NULL);
468 * access() needs to use the real uid/gid, not the effective uid/gid.
469 * We do this by temporarily clearing all FS-related capabilities and
470 * switching the fsuid/fsgid around to the real ones.
472 asmlinkage long sys_access(const char __user * filename, int mode)
475 int old_fsuid, old_fsgid;
476 kernel_cap_t old_cap;
479 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
482 old_fsuid = current->fsuid;
483 old_fsgid = current->fsgid;
484 old_cap = current->cap_effective;
486 current->fsuid = current->uid;
487 current->fsgid = current->gid;
490 * Clear the capabilities if we switch to a non-root user
492 * FIXME: There is a race here against sys_capset. The
493 * capabilities can change yet we will restore the old
494 * value below. We should hold task_capabilities_lock,
495 * but we cannot because user_path_walk can sleep.
498 cap_clear(current->cap_effective);
500 current->cap_effective = current->cap_permitted;
502 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
504 res = permission(nd.dentry->d_inode, mode, &nd);
505 /* SuS v2 requires we report a read only fs too */
506 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
507 && !special_file(nd.dentry->d_inode->i_mode))
512 current->fsuid = old_fsuid;
513 current->fsgid = old_fsgid;
514 current->cap_effective = old_cap;
519 asmlinkage long sys_chdir(const char __user * filename)
524 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
528 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
532 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
540 asmlinkage long sys_fchdir(unsigned int fd)
543 struct dentry *dentry;
545 struct vfsmount *mnt;
553 dentry = file->f_dentry;
554 mnt = file->f_vfsmnt;
555 inode = dentry->d_inode;
558 if (!S_ISDIR(inode->i_mode))
561 error = permission(inode, MAY_EXEC, NULL);
563 set_fs_pwd(current->fs, mnt, dentry);
570 asmlinkage long sys_chroot(const char __user * filename)
575 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
579 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
584 if (!capable(CAP_SYS_CHROOT))
587 set_fs_root(current->fs, nd.mnt, nd.dentry);
596 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
598 struct inode * inode;
599 struct dentry * dentry;
602 struct iattr newattrs;
608 dentry = file->f_dentry;
609 inode = dentry->d_inode;
612 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
615 if (IS_RDONLY(inode))
618 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
621 if (mode == (mode_t) -1)
622 mode = inode->i_mode;
623 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
624 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
625 err = notify_change(dentry, &newattrs);
634 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
637 struct inode * inode;
639 struct iattr newattrs;
641 error = user_path_walk(filename, &nd);
644 inode = nd.dentry->d_inode;
647 if (IS_BARRIER(inode) && !vx_check(0, VX_ADMIN))
651 if (IS_RDONLY(inode))
655 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
659 if (mode == (mode_t) -1)
660 mode = inode->i_mode;
661 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
662 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
663 error = notify_change(nd.dentry, &newattrs);
672 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
674 struct inode * inode;
676 struct iattr newattrs;
679 if (!(inode = dentry->d_inode)) {
680 printk(KERN_ERR "chown_common: NULL inode\n");
684 if (IS_RDONLY(inode))
687 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
690 newattrs.ia_valid = ATTR_CTIME;
691 if (user != (uid_t) -1) {
692 newattrs.ia_valid |= ATTR_UID;
693 newattrs.ia_uid = vx_map_uid(user);
695 if (group != (gid_t) -1) {
696 newattrs.ia_valid |= ATTR_GID;
697 newattrs.ia_gid = vx_map_gid(group);
699 if (!S_ISDIR(inode->i_mode))
700 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
702 error = notify_change(dentry, &newattrs);
708 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
713 error = user_path_walk(filename, &nd);
715 error = chown_common(nd.dentry, user, group);
721 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
726 error = user_path_walk_link(filename, &nd);
728 error = chown_common(nd.dentry, user, group);
735 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
742 error = chown_common(file->f_dentry, user, group);
749 * Note that while the flag value (low two bits) for sys_open means:
755 * 00 - no permissions needed
756 * 01 - read-permission
757 * 10 - write-permission
759 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
762 struct file *filp_open(const char * filename, int flags, int mode)
764 int namei_flags, error;
768 if ((namei_flags+1) & O_ACCMODE)
770 if (namei_flags & O_TRUNC)
773 error = open_namei(filename, namei_flags, mode, &nd);
775 return dentry_open(nd.dentry, nd.mnt, flags);
777 return ERR_PTR(error);
780 EXPORT_SYMBOL(filp_open);
782 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
789 f = get_empty_filp();
793 f->f_mode = (flags+1) & O_ACCMODE;
794 inode = dentry->d_inode;
795 if (f->f_mode & FMODE_WRITE) {
796 error = get_write_access(inode);
801 f->f_mapping = inode->i_mapping;
802 f->f_dentry = dentry;
805 f->f_op = fops_get(inode->i_fop);
806 file_move(f, &inode->i_sb->s_files);
808 if (f->f_op && f->f_op->open) {
809 error = f->f_op->open(inode,f);
813 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
815 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
817 /* NB: we're sure to have correct a_ops only after f_op->open */
818 if (f->f_flags & O_DIRECT) {
819 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO) {
821 f = ERR_PTR(-EINVAL);
829 if (f->f_mode & FMODE_WRITE)
830 put_write_access(inode);
839 return ERR_PTR(error);
842 EXPORT_SYMBOL(dentry_open);
845 * Find an empty file descriptor entry, and mark it busy.
847 int get_unused_fd(void)
849 struct files_struct * files = current->files;
853 spin_lock(&files->file_lock);
856 fd = find_next_zero_bit(files->open_fds->fds_bits,
861 * N.B. For clone tasks sharing a files structure, this test
862 * will limit the total number of files that can be opened.
864 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
867 /* Do we need to expand the fdset array? */
868 if (fd >= files->max_fdset) {
869 error = expand_fdset(files, fd);
878 * Check whether we need to expand the fd array.
880 if (fd >= files->max_fds) {
881 error = expand_fd_array(files, fd);
889 FD_SET(fd, files->open_fds);
890 FD_CLR(fd, files->close_on_exec);
891 files->next_fd = fd + 1;
895 if (files->fd[fd] != NULL) {
896 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
897 files->fd[fd] = NULL;
903 spin_unlock(&files->file_lock);
907 EXPORT_SYMBOL(get_unused_fd);
909 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
911 __FD_CLR(fd, files->open_fds);
912 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);
1047 return filp_close(filp, files);
1050 spin_unlock(&files->file_lock);
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);