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>
26 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
32 if (sb->s_op->statfs) {
33 memset(buf, 0, sizeof(*buf));
34 retval = security_sb_statfs(sb);
37 retval = sb->s_op->statfs(sb, buf);
38 if (retval == 0 && buf->f_frsize == 0)
39 buf->f_frsize = buf->f_bsize;
45 EXPORT_SYMBOL(vfs_statfs);
47 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
52 retval = vfs_statfs(sb, &st);
56 if (sizeof(*buf) == sizeof(st))
57 memcpy(buf, &st, sizeof(st));
59 if (sizeof buf->f_blocks == 4) {
60 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
61 0xffffffff00000000ULL)
64 * f_files and f_ffree may be -1; it's okay to stuff
67 if (st.f_files != -1 &&
68 (st.f_files & 0xffffffff00000000ULL))
70 if (st.f_ffree != -1 &&
71 (st.f_ffree & 0xffffffff00000000ULL))
75 buf->f_type = st.f_type;
76 buf->f_bsize = st.f_bsize;
77 buf->f_blocks = st.f_blocks;
78 buf->f_bfree = st.f_bfree;
79 buf->f_bavail = st.f_bavail;
80 buf->f_files = st.f_files;
81 buf->f_ffree = st.f_ffree;
82 buf->f_fsid = st.f_fsid;
83 buf->f_namelen = st.f_namelen;
84 buf->f_frsize = st.f_frsize;
85 memset(buf->f_spare, 0, sizeof(buf->f_spare));
90 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
95 retval = vfs_statfs(sb, &st);
99 if (sizeof(*buf) == sizeof(st))
100 memcpy(buf, &st, sizeof(st));
102 buf->f_type = st.f_type;
103 buf->f_bsize = st.f_bsize;
104 buf->f_blocks = st.f_blocks;
105 buf->f_bfree = st.f_bfree;
106 buf->f_bavail = st.f_bavail;
107 buf->f_files = st.f_files;
108 buf->f_ffree = st.f_ffree;
109 buf->f_fsid = st.f_fsid;
110 buf->f_namelen = st.f_namelen;
111 buf->f_frsize = st.f_frsize;
112 memset(buf->f_spare, 0, sizeof(buf->f_spare));
117 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
122 error = user_path_walk(path, &nd);
125 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
126 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
134 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
139 if (sz != sizeof(*buf))
141 error = user_path_walk(path, &nd);
144 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
145 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
153 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
163 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
164 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
171 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
177 if (sz != sizeof(*buf))
184 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
185 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
192 int do_truncate(struct dentry *dentry, loff_t length)
195 struct iattr newattrs;
197 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
201 newattrs.ia_size = length;
202 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
203 down(&dentry->d_inode->i_sem);
204 down_write(&dentry->d_inode->i_alloc_sem);
205 err = notify_change(dentry, &newattrs);
206 up_write(&dentry->d_inode->i_alloc_sem);
207 up(&dentry->d_inode->i_sem);
211 static inline long do_sys_truncate(const char __user * path, loff_t length)
214 struct inode * inode;
218 if (length < 0) /* sorry, but loff_t says... */
221 error = user_path_walk(path, &nd);
224 inode = nd.dentry->d_inode;
226 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
228 if (S_ISDIR(inode->i_mode))
232 if (!S_ISREG(inode->i_mode))
235 error = permission(inode,MAY_WRITE,&nd);
240 if (IS_RDONLY(inode))
244 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
248 * Make sure that there are no leases.
250 error = break_lease(inode, FMODE_WRITE);
254 error = get_write_access(inode);
258 error = locks_verify_truncate(inode, NULL, length);
261 error = do_truncate(nd.dentry, length);
263 put_write_access(inode);
271 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
273 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
274 return do_sys_truncate(path, (long)length);
277 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
279 struct inode * inode;
280 struct dentry *dentry;
292 /* explicitly opened as large or we are on 64-bit box */
293 if (file->f_flags & O_LARGEFILE)
296 dentry = file->f_dentry;
297 inode = dentry->d_inode;
299 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
303 /* Cannot ftruncate over 2^31 bytes without large file support */
304 if (small && length > MAX_NON_LFS)
308 if (IS_APPEND(inode))
311 error = locks_verify_truncate(inode, file, length);
313 error = do_truncate(dentry, length);
320 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
322 return do_sys_ftruncate(fd, length, 1);
325 /* LFS versions of truncate are only needed on 32 bit machines */
326 #if BITS_PER_LONG == 32
327 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
329 return do_sys_truncate(path, length);
332 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
334 return do_sys_ftruncate(fd, length, 0);
338 #if !(defined(__alpha__) || defined(__ia64__))
341 * sys_utime() can be implemented in user-level using sys_utimes().
342 * Is this for backwards compatibility? If so, why not move it
343 * into the appropriate arch directory (for those architectures that
347 /* If times==NULL, set access and modification to current time,
348 * must be owner or have write permission.
349 * Else, update from *times, must be owner or super user.
351 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
355 struct inode * inode;
356 struct iattr newattrs;
358 error = user_path_walk(filename, &nd);
361 inode = nd.dentry->d_inode;
364 if (IS_RDONLY(inode))
367 /* Don't worry, the checks are done in inode_change_ok() */
368 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
371 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
374 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
375 newattrs.ia_atime.tv_nsec = 0;
377 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
378 newattrs.ia_mtime.tv_nsec = 0;
382 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
385 if (IS_IMMUTABLE(inode))
388 if (current->fsuid != inode->i_uid &&
389 (error = permission(inode,MAY_WRITE,&nd)) != 0)
393 error = notify_change(nd.dentry, &newattrs);
403 /* If times==NULL, set access and modification to current time,
404 * must be owner or have write permission.
405 * Else, update from *times, must be owner or super user.
407 long do_utimes(char __user * filename, struct timeval * times)
411 struct inode * inode;
412 struct iattr newattrs;
414 error = user_path_walk(filename, &nd);
418 inode = nd.dentry->d_inode;
421 if (IS_RDONLY(inode))
424 /* Don't worry, the checks are done in inode_change_ok() */
425 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
428 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
431 newattrs.ia_atime.tv_sec = times[0].tv_sec;
432 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
433 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
434 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
435 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
438 if (IS_IMMUTABLE(inode))
441 if (current->fsuid != inode->i_uid &&
442 (error = permission(inode,MAY_WRITE,&nd)) != 0)
446 error = notify_change(nd.dentry, &newattrs);
454 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
456 struct timeval times[2];
458 if (utimes && copy_from_user(×, utimes, sizeof(times)))
460 return do_utimes(filename, utimes ? times : NULL);
465 * access() needs to use the real uid/gid, not the effective uid/gid.
466 * We do this by temporarily clearing all FS-related capabilities and
467 * switching the fsuid/fsgid around to the real ones.
469 asmlinkage long sys_access(const char __user * filename, int mode)
472 int old_fsuid, old_fsgid;
473 kernel_cap_t old_cap;
476 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
479 old_fsuid = current->fsuid;
480 old_fsgid = current->fsgid;
481 old_cap = current->cap_effective;
483 current->fsuid = current->uid;
484 current->fsgid = current->gid;
487 * Clear the capabilities if we switch to a non-root user
489 * FIXME: There is a race here against sys_capset. The
490 * capabilities can change yet we will restore the old
491 * value below. We should hold task_capabilities_lock,
492 * but we cannot because user_path_walk can sleep.
495 cap_clear(current->cap_effective);
497 current->cap_effective = current->cap_permitted;
499 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
501 res = permission(nd.dentry->d_inode, mode, &nd);
502 /* SuS v2 requires we report a read only fs too */
503 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
504 && !special_file(nd.dentry->d_inode->i_mode))
509 current->fsuid = old_fsuid;
510 current->fsgid = old_fsgid;
511 current->cap_effective = old_cap;
516 asmlinkage long sys_chdir(const char __user * filename)
521 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
525 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
529 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
537 asmlinkage long sys_fchdir(unsigned int fd)
540 struct dentry *dentry;
542 struct vfsmount *mnt;
550 dentry = file->f_dentry;
551 mnt = file->f_vfsmnt;
552 inode = dentry->d_inode;
555 if (!S_ISDIR(inode->i_mode))
558 error = permission(inode, MAY_EXEC, NULL);
560 set_fs_pwd(current->fs, mnt, dentry);
567 asmlinkage long sys_chroot(const char __user * filename)
572 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
576 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
581 if (!capable(CAP_SYS_CHROOT))
584 set_fs_root(current->fs, nd.mnt, nd.dentry);
593 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
595 struct inode * inode;
596 struct dentry * dentry;
599 struct iattr newattrs;
605 dentry = file->f_dentry;
606 inode = dentry->d_inode;
609 if (IS_RDONLY(inode))
612 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
615 if (mode == (mode_t) -1)
616 mode = inode->i_mode;
617 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
618 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
619 err = notify_change(dentry, &newattrs);
628 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
631 struct inode * inode;
633 struct iattr newattrs;
635 error = user_path_walk(filename, &nd);
638 inode = nd.dentry->d_inode;
641 if (IS_RDONLY(inode))
645 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
649 if (mode == (mode_t) -1)
650 mode = inode->i_mode;
651 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
652 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
653 error = notify_change(nd.dentry, &newattrs);
662 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
664 struct inode * inode;
666 struct iattr newattrs;
669 if (!(inode = dentry->d_inode)) {
670 printk(KERN_ERR "chown_common: NULL inode\n");
674 if (IS_RDONLY(inode))
677 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
679 newattrs.ia_valid = ATTR_CTIME;
680 if (user != (uid_t) -1) {
681 newattrs.ia_valid |= ATTR_UID;
682 newattrs.ia_uid = user;
684 if (group != (gid_t) -1) {
685 newattrs.ia_valid |= ATTR_GID;
686 newattrs.ia_gid = group;
688 if (!S_ISDIR(inode->i_mode))
689 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
691 error = notify_change(dentry, &newattrs);
697 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
702 error = user_path_walk(filename, &nd);
704 error = chown_common(nd.dentry, user, group);
710 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
715 error = user_path_walk_link(filename, &nd);
717 error = chown_common(nd.dentry, user, group);
724 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
731 error = chown_common(file->f_dentry, user, group);
738 * Note that while the flag value (low two bits) for sys_open means:
744 * 00 - no permissions needed
745 * 01 - read-permission
746 * 10 - write-permission
748 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
751 struct file *filp_open(const char * filename, int flags, int mode)
753 int namei_flags, error;
757 if ((namei_flags+1) & O_ACCMODE)
759 if (namei_flags & O_TRUNC)
762 error = open_namei(filename, namei_flags, mode, &nd);
764 return dentry_open(nd.dentry, nd.mnt, flags);
766 return ERR_PTR(error);
769 EXPORT_SYMBOL(filp_open);
771 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
778 f = get_empty_filp();
782 f->f_mode = (flags+1) & O_ACCMODE;
783 inode = dentry->d_inode;
784 if (f->f_mode & FMODE_WRITE) {
785 error = get_write_access(inode);
790 f->f_mapping = inode->i_mapping;
791 file_ra_state_init(&f->f_ra, f->f_mapping);
792 f->f_dentry = dentry;
795 f->f_op = fops_get(inode->i_fop);
796 file_move(f, &inode->i_sb->s_files);
798 if (f->f_op && f->f_op->open) {
799 error = f->f_op->open(inode,f);
803 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
805 /* NB: we're sure to have correct a_ops only after f_op->open */
806 if (f->f_flags & O_DIRECT) {
807 if (!f->f_mapping || !f->f_mapping->a_ops ||
808 !f->f_mapping->a_ops->direct_IO) {
810 f = ERR_PTR(-EINVAL);
818 if (f->f_mode & FMODE_WRITE)
819 put_write_access(inode);
828 return ERR_PTR(error);
831 EXPORT_SYMBOL(dentry_open);
834 * Find an empty file descriptor entry, and mark it busy.
836 int get_unused_fd(void)
838 struct files_struct * files = current->files;
842 spin_lock(&files->file_lock);
845 fd = find_next_zero_bit(files->open_fds->fds_bits,
850 * N.B. For clone tasks sharing a files structure, this test
851 * will limit the total number of files that can be opened.
853 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
856 /* Do we need to expand the fdset array? */
857 if (fd >= files->max_fdset) {
858 error = expand_fdset(files, fd);
867 * Check whether we need to expand the fd array.
869 if (fd >= files->max_fds) {
870 error = expand_fd_array(files, fd);
878 FD_SET(fd, files->open_fds);
879 FD_CLR(fd, files->close_on_exec);
880 files->next_fd = fd + 1;
883 if (files->fd[fd] != NULL) {
884 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
885 files->fd[fd] = NULL;
891 spin_unlock(&files->file_lock);
895 EXPORT_SYMBOL(get_unused_fd);
897 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
899 __FD_CLR(fd, files->open_fds);
900 if (fd < files->next_fd)
904 void fastcall put_unused_fd(unsigned int fd)
906 struct files_struct *files = current->files;
907 spin_lock(&files->file_lock);
908 __put_unused_fd(files, fd);
909 spin_unlock(&files->file_lock);
912 EXPORT_SYMBOL(put_unused_fd);
915 * Install a file pointer in the fd array.
917 * The VFS is full of places where we drop the files lock between
918 * setting the open_fds bitmap and installing the file in the file
919 * array. At any such point, we are vulnerable to a dup2() race
920 * installing a file in the array before us. We need to detect this and
921 * fput() the struct file we are about to overwrite in this case.
923 * It should never happen - if we allow dup2() do it, _really_ bad things
927 void fastcall fd_install(unsigned int fd, struct file * file)
929 struct files_struct *files = current->files;
930 spin_lock(&files->file_lock);
931 if (unlikely(files->fd[fd] != NULL))
933 files->fd[fd] = file;
934 spin_unlock(&files->file_lock);
937 EXPORT_SYMBOL(fd_install);
939 asmlinkage long sys_open(const char __user * filename, int flags, int mode)
944 #if BITS_PER_LONG != 32
945 flags |= O_LARGEFILE;
947 tmp = getname(filename);
950 fd = get_unused_fd();
952 struct file *f = filp_open(tmp, flags, mode);
968 EXPORT_SYMBOL_GPL(sys_open);
973 * For backward compatibility? Maybe this should be moved
974 * into arch/i386 instead?
976 asmlinkage long sys_creat(const char __user * pathname, int mode)
978 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
984 * "id" is the POSIX thread ID. We use the
985 * files pointer for this..
987 int filp_close(struct file *filp, fl_owner_t id)
991 /* Report and clear outstanding errors */
992 retval = filp->f_error;
996 if (!file_count(filp)) {
997 printk(KERN_ERR "VFS: Close: file count is 0\n");
1001 if (filp->f_op && filp->f_op->flush) {
1002 int err = filp->f_op->flush(filp);
1007 dnotify_flush(filp, id);
1008 locks_remove_posix(filp, id);
1013 EXPORT_SYMBOL(filp_close);
1016 * Careful here! We test whether the file pointer is NULL before
1017 * releasing the fd. This ensures that one clone task can't release
1018 * an fd while another clone is opening it.
1020 asmlinkage long sys_close(unsigned int fd)
1023 struct files_struct *files = current->files;
1025 spin_lock(&files->file_lock);
1026 if (fd >= files->max_fds)
1028 filp = files->fd[fd];
1031 files->fd[fd] = NULL;
1032 FD_CLR(fd, files->close_on_exec);
1033 __put_unused_fd(files, fd);
1034 spin_unlock(&files->file_lock);
1035 return filp_close(filp, files);
1038 spin_unlock(&files->file_lock);
1042 EXPORT_SYMBOL(sys_close);
1045 * This routine simulates a hangup on the tty, to arrange that users
1046 * are given clean terminals at login time.
1048 asmlinkage long sys_vhangup(void)
1050 if (capable(CAP_SYS_TTY_CONFIG)) {
1051 tty_vhangup(current->signal->tty);
1058 * Called when an inode is about to be open.
1059 * We use this to disallow opening large files on 32bit systems if
1060 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1061 * on this flag in sys_open.
1063 int generic_file_open(struct inode * inode, struct file * filp)
1065 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1070 EXPORT_SYMBOL(generic_file_open);