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/fsnotify.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/personality.h>
25 #include <linux/pagemap.h>
26 #include <linux/syscalls.h>
27 #include <linux/rcupdate.h>
29 #include <asm/unistd.h>
31 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
37 if (sb->s_op->statfs) {
38 memset(buf, 0, sizeof(*buf));
39 retval = security_sb_statfs(sb);
42 retval = sb->s_op->statfs(sb, buf);
43 if (retval == 0 && buf->f_frsize == 0)
44 buf->f_frsize = buf->f_bsize;
50 EXPORT_SYMBOL(vfs_statfs);
52 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
57 retval = vfs_statfs(sb, &st);
61 if (sizeof(*buf) == sizeof(st))
62 memcpy(buf, &st, sizeof(st));
64 if (sizeof buf->f_blocks == 4) {
65 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
66 0xffffffff00000000ULL)
69 * f_files and f_ffree may be -1; it's okay to stuff
72 if (st.f_files != -1 &&
73 (st.f_files & 0xffffffff00000000ULL))
75 if (st.f_ffree != -1 &&
76 (st.f_ffree & 0xffffffff00000000ULL))
80 buf->f_type = st.f_type;
81 buf->f_bsize = st.f_bsize;
82 buf->f_blocks = st.f_blocks;
83 buf->f_bfree = st.f_bfree;
84 buf->f_bavail = st.f_bavail;
85 buf->f_files = st.f_files;
86 buf->f_ffree = st.f_ffree;
87 buf->f_fsid = st.f_fsid;
88 buf->f_namelen = st.f_namelen;
89 buf->f_frsize = st.f_frsize;
90 memset(buf->f_spare, 0, sizeof(buf->f_spare));
95 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
100 retval = vfs_statfs(sb, &st);
104 if (sizeof(*buf) == sizeof(st))
105 memcpy(buf, &st, sizeof(st));
107 buf->f_type = st.f_type;
108 buf->f_bsize = st.f_bsize;
109 buf->f_blocks = st.f_blocks;
110 buf->f_bfree = st.f_bfree;
111 buf->f_bavail = st.f_bavail;
112 buf->f_files = st.f_files;
113 buf->f_ffree = st.f_ffree;
114 buf->f_fsid = st.f_fsid;
115 buf->f_namelen = st.f_namelen;
116 buf->f_frsize = st.f_frsize;
117 memset(buf->f_spare, 0, sizeof(buf->f_spare));
122 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
127 error = user_path_walk(path, &nd);
130 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
131 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
139 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
144 if (sz != sizeof(*buf))
146 error = user_path_walk(path, &nd);
149 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
150 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
158 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
168 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
169 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
176 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
182 if (sz != sizeof(*buf))
189 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
190 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
197 int do_truncate(struct dentry *dentry, loff_t length)
200 struct iattr newattrs;
202 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
206 newattrs.ia_size = length;
207 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
209 down(&dentry->d_inode->i_sem);
210 err = notify_change(dentry, &newattrs);
211 up(&dentry->d_inode->i_sem);
215 static inline long do_sys_truncate(const char __user * path, loff_t length)
218 struct inode * inode;
222 if (length < 0) /* sorry, but loff_t says... */
225 error = user_path_walk(path, &nd);
228 inode = nd.dentry->d_inode;
230 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
232 if (S_ISDIR(inode->i_mode))
236 if (!S_ISREG(inode->i_mode))
239 error = permission(inode,MAY_WRITE,&nd);
244 if (IS_RDONLY(inode))
248 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
252 * Make sure that there are no leases.
254 error = break_lease(inode, FMODE_WRITE);
258 error = get_write_access(inode);
262 error = locks_verify_truncate(inode, NULL, length);
265 error = do_truncate(nd.dentry, length);
267 put_write_access(inode);
275 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
277 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
278 return do_sys_truncate(path, (long)length);
281 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
283 struct inode * inode;
284 struct dentry *dentry;
296 /* explicitly opened as large or we are on 64-bit box */
297 if (file->f_flags & O_LARGEFILE)
300 dentry = file->f_dentry;
301 inode = dentry->d_inode;
303 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
307 /* Cannot ftruncate over 2^31 bytes without large file support */
308 if (small && length > MAX_NON_LFS)
312 if (IS_APPEND(inode))
315 error = locks_verify_truncate(inode, file, length);
317 error = do_truncate(dentry, length);
324 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
326 return do_sys_ftruncate(fd, length, 1);
329 /* LFS versions of truncate are only needed on 32 bit machines */
330 #if BITS_PER_LONG == 32
331 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
333 return do_sys_truncate(path, length);
336 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
338 return do_sys_ftruncate(fd, length, 0);
342 #ifdef __ARCH_WANT_SYS_UTIME
345 * sys_utime() can be implemented in user-level using sys_utimes().
346 * Is this for backwards compatibility? If so, why not move it
347 * into the appropriate arch directory (for those architectures that
351 /* If times==NULL, set access and modification to current time,
352 * must be owner or have write permission.
353 * Else, update from *times, must be owner or super user.
355 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
359 struct inode * inode;
360 struct iattr newattrs;
362 error = user_path_walk(filename, &nd);
365 inode = nd.dentry->d_inode;
368 if (IS_RDONLY(inode))
371 /* Don't worry, the checks are done in inode_change_ok() */
372 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
375 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
378 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
379 newattrs.ia_atime.tv_nsec = 0;
381 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
382 newattrs.ia_mtime.tv_nsec = 0;
386 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
389 if (IS_IMMUTABLE(inode))
392 if (current->fsuid != inode->i_uid &&
393 (error = permission(inode,MAY_WRITE,&nd)) != 0)
397 error = notify_change(nd.dentry, &newattrs);
407 /* If times==NULL, set access and modification to current time,
408 * must be owner or have write permission.
409 * Else, update from *times, must be owner or super user.
411 long do_utimes(char __user * filename, struct timeval * times)
415 struct inode * inode;
416 struct iattr newattrs;
418 error = user_path_walk(filename, &nd);
422 inode = nd.dentry->d_inode;
425 if (IS_RDONLY(inode))
428 /* Don't worry, the checks are done in inode_change_ok() */
429 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
432 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
435 newattrs.ia_atime.tv_sec = times[0].tv_sec;
436 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
437 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
438 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
439 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
442 if (IS_IMMUTABLE(inode))
445 if (current->fsuid != inode->i_uid &&
446 (error = permission(inode,MAY_WRITE,&nd)) != 0)
450 error = notify_change(nd.dentry, &newattrs);
458 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
460 struct timeval times[2];
462 if (utimes && copy_from_user(×, utimes, sizeof(times)))
464 return do_utimes(filename, utimes ? times : NULL);
469 * access() needs to use the real uid/gid, not the effective uid/gid.
470 * We do this by temporarily clearing all FS-related capabilities and
471 * switching the fsuid/fsgid around to the real ones.
473 asmlinkage long sys_access(const char __user * filename, int mode)
476 int old_fsuid, old_fsgid;
477 kernel_cap_t old_cap;
480 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
483 old_fsuid = current->fsuid;
484 old_fsgid = current->fsgid;
485 old_cap = current->cap_effective;
487 current->fsuid = current->uid;
488 current->fsgid = current->gid;
491 * Clear the capabilities if we switch to a non-root user
493 * FIXME: There is a race here against sys_capset. The
494 * capabilities can change yet we will restore the old
495 * value below. We should hold task_capabilities_lock,
496 * but we cannot because user_path_walk can sleep.
499 cap_clear(current->cap_effective);
501 current->cap_effective = current->cap_permitted;
503 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
505 res = permission(nd.dentry->d_inode, mode, &nd);
506 /* SuS v2 requires we report a read only fs too */
507 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
508 && !special_file(nd.dentry->d_inode->i_mode))
513 current->fsuid = old_fsuid;
514 current->fsgid = old_fsgid;
515 current->cap_effective = old_cap;
520 asmlinkage long sys_chdir(const char __user * filename)
525 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
529 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
533 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
541 asmlinkage long sys_fchdir(unsigned int fd)
544 struct dentry *dentry;
546 struct vfsmount *mnt;
554 dentry = file->f_dentry;
555 mnt = file->f_vfsmnt;
556 inode = dentry->d_inode;
559 if (!S_ISDIR(inode->i_mode))
562 error = permission(inode, MAY_EXEC, NULL);
564 set_fs_pwd(current->fs, mnt, dentry);
571 asmlinkage long sys_chroot(const char __user * filename)
576 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
580 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
585 if (!capable(CAP_SYS_CHROOT))
588 set_fs_root(current->fs, nd.mnt, nd.dentry);
597 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
599 struct inode * inode;
600 struct dentry * dentry;
603 struct iattr newattrs;
609 dentry = file->f_dentry;
610 inode = dentry->d_inode;
613 if (IS_RDONLY(inode))
616 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
619 if (mode == (mode_t) -1)
620 mode = inode->i_mode;
621 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
622 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
623 err = notify_change(dentry, &newattrs);
632 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
635 struct inode * inode;
637 struct iattr newattrs;
639 error = user_path_walk(filename, &nd);
642 inode = nd.dentry->d_inode;
645 if (IS_RDONLY(inode))
649 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
653 if (mode == (mode_t) -1)
654 mode = inode->i_mode;
655 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
656 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
657 error = notify_change(nd.dentry, &newattrs);
666 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
668 struct inode * inode;
670 struct iattr newattrs;
673 if (!(inode = dentry->d_inode)) {
674 printk(KERN_ERR "chown_common: NULL inode\n");
678 if (IS_RDONLY(inode))
681 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
683 newattrs.ia_valid = ATTR_CTIME;
684 if (user != (uid_t) -1) {
685 newattrs.ia_valid |= ATTR_UID;
686 newattrs.ia_uid = user;
688 if (group != (gid_t) -1) {
689 newattrs.ia_valid |= ATTR_GID;
690 newattrs.ia_gid = group;
692 if (!S_ISDIR(inode->i_mode))
693 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
695 error = notify_change(dentry, &newattrs);
701 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
706 error = user_path_walk(filename, &nd);
708 error = chown_common(nd.dentry, user, group);
714 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
719 error = user_path_walk_link(filename, &nd);
721 error = chown_common(nd.dentry, user, group);
728 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
735 error = chown_common(file->f_dentry, user, group);
742 * Note that while the flag value (low two bits) for sys_open means:
748 * 00 - no permissions needed
749 * 01 - read-permission
750 * 10 - write-permission
752 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
755 struct file *filp_open(const char * filename, int flags, int mode)
757 int namei_flags, error;
761 if ((namei_flags+1) & O_ACCMODE)
763 if (namei_flags & O_TRUNC)
766 error = open_namei(filename, namei_flags, mode, &nd);
768 return dentry_open(nd.dentry, nd.mnt, flags);
770 return ERR_PTR(error);
773 EXPORT_SYMBOL(filp_open);
775 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
782 f = get_empty_filp();
786 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
787 inode = dentry->d_inode;
788 if (f->f_mode & FMODE_WRITE) {
789 error = get_write_access(inode);
794 f->f_mapping = inode->i_mapping;
795 f->f_dentry = dentry;
798 f->f_op = fops_get(inode->i_fop);
799 file_move(f, &inode->i_sb->s_files);
801 if (f->f_op && f->f_op->open) {
802 error = f->f_op->open(inode,f);
806 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
808 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
810 /* NB: we're sure to have correct a_ops only after f_op->open */
811 if (f->f_flags & O_DIRECT) {
812 if (!f->f_mapping->a_ops ||
813 ((!f->f_mapping->a_ops->direct_IO) &&
814 (!f->f_mapping->a_ops->get_xip_page))) {
816 f = ERR_PTR(-EINVAL);
824 if (f->f_mode & FMODE_WRITE)
825 put_write_access(inode);
834 return ERR_PTR(error);
837 EXPORT_SYMBOL(dentry_open);
840 * Find an empty file descriptor entry, and mark it busy.
842 int get_unused_fd(void)
844 struct files_struct * files = current->files;
849 spin_lock(&files->file_lock);
852 fdt = files_fdtable(files);
853 fd = find_next_zero_bit(fdt->open_fds->fds_bits,
858 * N.B. For clone tasks sharing a files structure, this test
859 * will limit the total number of files that can be opened.
861 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
864 /* Do we need to expand the fd array or fd set? */
865 error = expand_files(files, fd);
871 * If we needed to expand the fs array we
872 * might have blocked - try again.
878 FD_SET(fd, fdt->open_fds);
879 FD_CLR(fd, fdt->close_on_exec);
880 fdt->next_fd = fd + 1;
883 if (fdt->fd[fd] != NULL) {
884 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
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 struct fdtable *fdt = files_fdtable(files);
900 __FD_CLR(fd, fdt->open_fds);
901 if (fd < fdt->next_fd)
905 void fastcall put_unused_fd(unsigned int fd)
907 struct files_struct *files = current->files;
908 spin_lock(&files->file_lock);
909 __put_unused_fd(files, fd);
910 spin_unlock(&files->file_lock);
913 EXPORT_SYMBOL(put_unused_fd);
916 * Install a file pointer in the fd array.
918 * The VFS is full of places where we drop the files lock between
919 * setting the open_fds bitmap and installing the file in the file
920 * array. At any such point, we are vulnerable to a dup2() race
921 * installing a file in the array before us. We need to detect this and
922 * fput() the struct file we are about to overwrite in this case.
924 * It should never happen - if we allow dup2() do it, _really_ bad things
928 void fastcall fd_install(unsigned int fd, struct file * file)
930 struct files_struct *files = current->files;
932 spin_lock(&files->file_lock);
933 fdt = files_fdtable(files);
934 BUG_ON(fdt->fd[fd] != NULL);
935 rcu_assign_pointer(fdt->fd[fd], file);
936 spin_unlock(&files->file_lock);
939 EXPORT_SYMBOL(fd_install);
941 long do_sys_open(const char __user *filename, int flags, int mode)
943 char *tmp = getname(filename);
944 int fd = PTR_ERR(tmp);
947 fd = get_unused_fd();
949 struct file *f = filp_open(tmp, flags, mode);
954 fsnotify_open(f->f_dentry);
963 asmlinkage long sys_open(const char __user *filename, int flags, int mode)
965 if (force_o_largefile())
966 flags |= O_LARGEFILE;
968 return do_sys_open(filename, flags, mode);
970 EXPORT_SYMBOL_GPL(sys_open);
975 * For backward compatibility? Maybe this should be moved
976 * into arch/i386 instead?
978 asmlinkage long sys_creat(const char __user * pathname, int mode)
980 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
986 * "id" is the POSIX thread ID. We use the
987 * files pointer for this..
989 int filp_close(struct file *filp, fl_owner_t id)
993 if (!file_count(filp)) {
994 printk(KERN_ERR "VFS: Close: file count is 0\n");
998 if (filp->f_op && filp->f_op->flush)
999 retval = filp->f_op->flush(filp);
1001 dnotify_flush(filp, id);
1002 locks_remove_posix(filp, id);
1007 EXPORT_SYMBOL(filp_close);
1010 * Careful here! We test whether the file pointer is NULL before
1011 * releasing the fd. This ensures that one clone task can't release
1012 * an fd while another clone is opening it.
1014 asmlinkage long sys_close(unsigned int fd)
1017 struct files_struct *files = current->files;
1018 struct fdtable *fdt;
1020 spin_lock(&files->file_lock);
1021 fdt = files_fdtable(files);
1022 if (fd >= fdt->max_fds)
1027 rcu_assign_pointer(fdt->fd[fd], NULL);
1028 FD_CLR(fd, fdt->close_on_exec);
1029 __put_unused_fd(files, fd);
1030 spin_unlock(&files->file_lock);
1031 return filp_close(filp, files);
1034 spin_unlock(&files->file_lock);
1038 EXPORT_SYMBOL(sys_close);
1041 * This routine simulates a hangup on the tty, to arrange that users
1042 * are given clean terminals at login time.
1044 asmlinkage long sys_vhangup(void)
1046 if (capable(CAP_SYS_TTY_CONFIG)) {
1047 tty_vhangup(current->signal->tty);
1054 * Called when an inode is about to be open.
1055 * We use this to disallow opening large files on 32bit systems if
1056 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1057 * on this flag in sys_open.
1059 int generic_file_open(struct inode * inode, struct file * filp)
1061 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1066 EXPORT_SYMBOL(generic_file_open);
1069 * This is used by subsystems that don't want seekable
1072 int nonseekable_open(struct inode *inode, struct file *filp)
1074 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1078 EXPORT_SYMBOL(nonseekable_open);