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>
25 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
31 if (sb->s_op->statfs) {
32 memset(buf, 0, sizeof(*buf));
33 retval = security_sb_statfs(sb);
36 retval = sb->s_op->statfs(sb, buf);
37 if (retval == 0 && buf->f_frsize == 0)
38 buf->f_frsize = buf->f_bsize;
44 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
49 retval = vfs_statfs(sb, &st);
53 if (sizeof(*buf) == sizeof(st))
54 memcpy(buf, &st, sizeof(st));
56 if (sizeof buf->f_blocks == 4) {
57 if ((st.f_blocks | st.f_bfree |
58 st.f_bavail | st.f_files | st.f_ffree) &
59 0xffffffff00000000ULL)
63 buf->f_type = st.f_type;
64 buf->f_bsize = st.f_bsize;
65 buf->f_blocks = st.f_blocks;
66 buf->f_bfree = st.f_bfree;
67 buf->f_bavail = st.f_bavail;
68 buf->f_files = st.f_files;
69 buf->f_ffree = st.f_ffree;
70 buf->f_fsid = st.f_fsid;
71 buf->f_namelen = st.f_namelen;
72 buf->f_frsize = st.f_frsize;
73 memset(buf->f_spare, 0, sizeof(buf->f_spare));
78 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
83 retval = vfs_statfs(sb, &st);
87 if (sizeof(*buf) == sizeof(st))
88 memcpy(buf, &st, sizeof(st));
90 buf->f_type = st.f_type;
91 buf->f_bsize = st.f_bsize;
92 buf->f_blocks = st.f_blocks;
93 buf->f_bfree = st.f_bfree;
94 buf->f_bavail = st.f_bavail;
95 buf->f_files = st.f_files;
96 buf->f_ffree = st.f_ffree;
97 buf->f_fsid = st.f_fsid;
98 buf->f_namelen = st.f_namelen;
99 buf->f_frsize = st.f_frsize;
100 memset(buf->f_spare, 0, sizeof(buf->f_spare));
105 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
110 error = user_path_walk(path, &nd);
113 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
114 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
122 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
127 if (sz != sizeof(*buf))
129 error = user_path_walk(path, &nd);
132 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
133 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
141 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
151 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
152 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
159 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
165 if (sz != sizeof(*buf))
172 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
173 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
180 int do_truncate(struct dentry *dentry, loff_t length)
183 struct iattr newattrs;
185 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
189 newattrs.ia_size = length;
190 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
191 down(&dentry->d_inode->i_sem);
192 err = notify_change(dentry, &newattrs);
193 up(&dentry->d_inode->i_sem);
197 static inline long do_sys_truncate(const char __user * path, loff_t length)
200 struct inode * inode;
204 if (length < 0) /* sorry, but loff_t says... */
207 error = user_path_walk(path, &nd);
210 inode = nd.dentry->d_inode;
212 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
214 if (S_ISDIR(inode->i_mode))
218 if (!S_ISREG(inode->i_mode))
221 error = permission(inode,MAY_WRITE,&nd);
226 if (IS_RDONLY(inode))
230 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
234 * Make sure that there are no leases.
236 error = break_lease(inode, FMODE_WRITE);
240 error = get_write_access(inode);
244 error = locks_verify_truncate(inode, NULL, length);
247 error = do_truncate(nd.dentry, length);
249 put_write_access(inode);
257 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
259 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
260 return do_sys_truncate(path, (long)length);
263 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
265 struct inode * inode;
266 struct dentry *dentry;
278 /* explicitly opened as large or we are on 64-bit box */
279 if (file->f_flags & O_LARGEFILE)
282 dentry = file->f_dentry;
283 inode = dentry->d_inode;
285 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
289 /* Cannot ftruncate over 2^31 bytes without large file support */
290 if (small && length > MAX_NON_LFS)
294 if (IS_APPEND(inode))
297 error = locks_verify_truncate(inode, file, length);
299 error = do_truncate(dentry, length);
306 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
308 return do_sys_ftruncate(fd, length, 1);
311 /* LFS versions of truncate are only needed on 32 bit machines */
312 #if BITS_PER_LONG == 32
313 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
315 return do_sys_truncate(path, length);
318 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
320 return do_sys_ftruncate(fd, length, 0);
324 #if !(defined(__alpha__) || defined(__ia64__))
327 * sys_utime() can be implemented in user-level using sys_utimes().
328 * Is this for backwards compatibility? If so, why not move it
329 * into the appropriate arch directory (for those architectures that
333 /* If times==NULL, set access and modification to current time,
334 * must be owner or have write permission.
335 * Else, update from *times, must be owner or super user.
337 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
341 struct inode * inode;
342 struct iattr newattrs;
344 error = user_path_walk(filename, &nd);
347 inode = nd.dentry->d_inode;
350 if (IS_RDONLY(inode))
353 /* Don't worry, the checks are done in inode_change_ok() */
354 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
356 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
357 newattrs.ia_atime.tv_nsec = 0;
359 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
360 newattrs.ia_mtime.tv_nsec = 0;
364 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
366 if (current->fsuid != inode->i_uid &&
367 (error = permission(inode,MAY_WRITE,&nd)) != 0)
371 error = notify_change(nd.dentry, &newattrs);
381 /* If times==NULL, set access and modification to current time,
382 * must be owner or have write permission.
383 * Else, update from *times, must be owner or super user.
385 long do_utimes(char __user * filename, struct timeval * times)
389 struct inode * inode;
390 struct iattr newattrs;
392 error = user_path_walk(filename, &nd);
396 inode = nd.dentry->d_inode;
399 if (IS_RDONLY(inode))
402 /* Don't worry, the checks are done in inode_change_ok() */
403 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
405 newattrs.ia_atime.tv_sec = times[0].tv_sec;
406 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
407 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
408 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
409 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
411 if (current->fsuid != inode->i_uid &&
412 (error = permission(inode,MAY_WRITE,&nd)) != 0)
416 error = notify_change(nd.dentry, &newattrs);
424 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
426 struct timeval times[2];
428 if (utimes && copy_from_user(×, utimes, sizeof(times)))
430 return do_utimes(filename, utimes ? times : NULL);
435 * access() needs to use the real uid/gid, not the effective uid/gid.
436 * We do this by temporarily clearing all FS-related capabilities and
437 * switching the fsuid/fsgid around to the real ones.
439 asmlinkage long sys_access(const char __user * filename, int mode)
442 int old_fsuid, old_fsgid;
443 kernel_cap_t old_cap;
446 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
449 old_fsuid = current->fsuid;
450 old_fsgid = current->fsgid;
451 old_cap = current->cap_effective;
453 current->fsuid = current->uid;
454 current->fsgid = current->gid;
457 * Clear the capabilities if we switch to a non-root user
459 * FIXME: There is a race here against sys_capset. The
460 * capabilities can change yet we will restore the old
461 * value below. We should hold task_capabilities_lock,
462 * but we cannot because user_path_walk can sleep.
465 cap_clear(current->cap_effective);
467 current->cap_effective = current->cap_permitted;
469 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
471 res = permission(nd.dentry->d_inode, mode, &nd);
472 /* SuS v2 requires we report a read only fs too */
473 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
474 && !special_file(nd.dentry->d_inode->i_mode))
479 current->fsuid = old_fsuid;
480 current->fsgid = old_fsgid;
481 current->cap_effective = old_cap;
486 asmlinkage long sys_chdir(const char __user * filename)
491 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
495 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
499 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
507 asmlinkage long sys_fchdir(unsigned int fd)
510 struct dentry *dentry;
512 struct vfsmount *mnt;
520 dentry = file->f_dentry;
521 mnt = file->f_vfsmnt;
522 inode = dentry->d_inode;
525 if (!S_ISDIR(inode->i_mode))
528 error = permission(inode, MAY_EXEC, NULL);
530 set_fs_pwd(current->fs, mnt, dentry);
537 asmlinkage long sys_chroot(const char __user * filename)
542 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
546 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
551 if (!capable(CAP_SYS_CHROOT))
554 set_fs_root(current->fs, nd.mnt, nd.dentry);
563 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
565 struct inode * inode;
566 struct dentry * dentry;
569 struct iattr newattrs;
575 dentry = file->f_dentry;
576 inode = dentry->d_inode;
579 if (IS_RDONLY(inode))
582 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
585 if (mode == (mode_t) -1)
586 mode = inode->i_mode;
587 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
588 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
589 err = notify_change(dentry, &newattrs);
598 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
601 struct inode * inode;
603 struct iattr newattrs;
605 error = user_path_walk(filename, &nd);
608 inode = nd.dentry->d_inode;
611 if (IS_RDONLY(inode))
615 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 error = notify_change(nd.dentry, &newattrs);
632 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
634 struct inode * inode;
636 struct iattr newattrs;
639 if (!(inode = dentry->d_inode)) {
640 printk(KERN_ERR "chown_common: NULL inode\n");
644 if (IS_RDONLY(inode))
647 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
649 newattrs.ia_valid = ATTR_CTIME;
650 if (user != (uid_t) -1) {
651 newattrs.ia_valid |= ATTR_UID;
652 newattrs.ia_uid = user;
654 if (group != (gid_t) -1) {
655 newattrs.ia_valid |= ATTR_GID;
656 newattrs.ia_gid = group;
658 if (!S_ISDIR(inode->i_mode))
659 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
661 error = notify_change(dentry, &newattrs);
667 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
672 error = user_path_walk(filename, &nd);
674 error = chown_common(nd.dentry, user, group);
680 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
685 error = user_path_walk_link(filename, &nd);
687 error = chown_common(nd.dentry, user, group);
694 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
701 error = chown_common(file->f_dentry, user, group);
708 * Note that while the flag value (low two bits) for sys_open means:
714 * 00 - no permissions needed
715 * 01 - read-permission
716 * 10 - write-permission
718 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
721 struct file *filp_open(const char * filename, int flags, int mode)
723 int namei_flags, error;
727 if ((namei_flags+1) & O_ACCMODE)
729 if (namei_flags & O_TRUNC)
732 error = open_namei(filename, namei_flags, mode, &nd);
734 return dentry_open(nd.dentry, nd.mnt, flags);
736 return ERR_PTR(error);
739 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
746 f = get_empty_filp();
750 f->f_mode = (flags+1) & O_ACCMODE;
751 inode = dentry->d_inode;
752 if (f->f_mode & FMODE_WRITE) {
753 error = get_write_access(inode);
758 file_ra_state_init(&f->f_ra, inode->i_mapping);
759 f->f_dentry = dentry;
762 f->f_op = fops_get(inode->i_fop);
763 file_move(f, &inode->i_sb->s_files);
765 if (f->f_op && f->f_op->open) {
766 error = f->f_op->open(inode,f);
770 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
772 /* NB: we're sure to have correct a_ops only after f_op->open */
773 if (f->f_flags & O_DIRECT) {
774 if (!inode->i_mapping || !inode->i_mapping->a_ops ||
775 !inode->i_mapping->a_ops->direct_IO) {
777 f = ERR_PTR(-EINVAL);
785 if (f->f_mode & FMODE_WRITE)
786 put_write_access(inode);
795 return ERR_PTR(error);
799 * Find an empty file descriptor entry, and mark it busy.
801 int get_unused_fd(void)
803 struct files_struct * files = current->files;
807 spin_lock(&files->file_lock);
810 fd = find_next_zero_bit(files->open_fds->fds_bits,
815 * N.B. For clone tasks sharing a files structure, this test
816 * will limit the total number of files that can be opened.
818 if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
821 /* Do we need to expand the fdset array? */
822 if (fd >= files->max_fdset) {
823 error = expand_fdset(files, fd);
832 * Check whether we need to expand the fd array.
834 if (fd >= files->max_fds) {
835 error = expand_fd_array(files, fd);
843 FD_SET(fd, files->open_fds);
844 FD_CLR(fd, files->close_on_exec);
845 files->next_fd = fd + 1;
848 if (files->fd[fd] != NULL) {
849 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
850 files->fd[fd] = NULL;
856 spin_unlock(&files->file_lock);
860 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
862 __FD_CLR(fd, files->open_fds);
863 if (fd < files->next_fd)
867 void put_unused_fd(unsigned int fd)
869 struct files_struct *files = current->files;
870 spin_lock(&files->file_lock);
871 __put_unused_fd(files, fd);
872 spin_unlock(&files->file_lock);
876 * Install a file pointer in the fd array.
878 * The VFS is full of places where we drop the files lock between
879 * setting the open_fds bitmap and installing the file in the file
880 * array. At any such point, we are vulnerable to a dup2() race
881 * installing a file in the array before us. We need to detect this and
882 * fput() the struct file we are about to overwrite in this case.
884 * It should never happen - if we allow dup2() do it, _really_ bad things
888 void fd_install(unsigned int fd, struct file * file)
890 struct files_struct *files = current->files;
891 spin_lock(&files->file_lock);
892 if (unlikely(files->fd[fd] != NULL))
894 files->fd[fd] = file;
895 spin_unlock(&files->file_lock);
898 asmlinkage long sys_open(const char __user * filename, int flags, int mode)
903 #if BITS_PER_LONG != 32
904 flags |= O_LARGEFILE;
906 tmp = getname(filename);
909 fd = get_unused_fd();
911 struct file *f = filp_open(tmp, flags, mode);
931 * For backward compatibility? Maybe this should be moved
932 * into arch/i386 instead?
934 asmlinkage long sys_creat(const char __user * pathname, int mode)
936 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
942 * "id" is the POSIX thread ID. We use the
943 * files pointer for this..
945 int filp_close(struct file *filp, fl_owner_t id)
949 if (!file_count(filp)) {
950 printk(KERN_ERR "VFS: Close: file count is 0\n");
954 if (filp->f_op && filp->f_op->flush)
955 retval = filp->f_op->flush(filp);
956 dnotify_flush(filp, id);
957 locks_remove_posix(filp, id);
963 * Careful here! We test whether the file pointer is NULL before
964 * releasing the fd. This ensures that one clone task can't release
965 * an fd while another clone is opening it.
967 asmlinkage long sys_close(unsigned int fd)
970 struct files_struct *files = current->files;
972 spin_lock(&files->file_lock);
973 if (fd >= files->max_fds)
975 filp = files->fd[fd];
978 files->fd[fd] = NULL;
979 FD_CLR(fd, files->close_on_exec);
980 __put_unused_fd(files, fd);
981 spin_unlock(&files->file_lock);
982 return filp_close(filp, files);
985 spin_unlock(&files->file_lock);
990 * This routine simulates a hangup on the tty, to arrange that users
991 * are given clean terminals at login time.
993 asmlinkage long sys_vhangup(void)
995 if (capable(CAP_SYS_TTY_CONFIG)) {
996 tty_vhangup(current->tty);
1003 * Called when an inode is about to be open.
1004 * We use this to disallow opening large files on 32bit systems if
1005 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1006 * on this flag in sys_open.
1008 int generic_file_open(struct inode * inode, struct file * filp)
1010 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1015 EXPORT_SYMBOL(generic_file_open);