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/syscalls.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;
207 down(&dentry->d_inode->i_sem);
208 err = notify_change(dentry, &newattrs);
209 up(&dentry->d_inode->i_sem);
213 static inline long do_sys_truncate(const char __user * path, loff_t length)
216 struct inode * inode;
220 if (length < 0) /* sorry, but loff_t says... */
223 error = user_path_walk(path, &nd);
226 inode = nd.dentry->d_inode;
228 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
230 if (S_ISDIR(inode->i_mode))
234 if (!S_ISREG(inode->i_mode))
237 error = permission(inode,MAY_WRITE,&nd);
242 if (IS_RDONLY(inode))
246 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
250 * Make sure that there are no leases.
252 error = break_lease(inode, FMODE_WRITE);
256 error = get_write_access(inode);
260 error = locks_verify_truncate(inode, NULL, length);
263 error = do_truncate(nd.dentry, length);
265 put_write_access(inode);
273 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
275 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
276 return do_sys_truncate(path, (long)length);
279 static inline long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
281 struct inode * inode;
282 struct dentry *dentry;
294 /* explicitly opened as large or we are on 64-bit box */
295 if (file->f_flags & O_LARGEFILE)
298 dentry = file->f_dentry;
299 inode = dentry->d_inode;
301 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
305 /* Cannot ftruncate over 2^31 bytes without large file support */
306 if (small && length > MAX_NON_LFS)
310 if (IS_APPEND(inode))
313 error = locks_verify_truncate(inode, file, length);
315 error = do_truncate(dentry, length);
322 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
324 return do_sys_ftruncate(fd, length, 1);
327 /* LFS versions of truncate are only needed on 32 bit machines */
328 #if BITS_PER_LONG == 32
329 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
331 return do_sys_truncate(path, length);
334 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
336 return do_sys_ftruncate(fd, length, 0);
340 #ifdef __ARCH_WANT_SYS_UTIME
343 * sys_utime() can be implemented in user-level using sys_utimes().
344 * Is this for backwards compatibility? If so, why not move it
345 * into the appropriate arch directory (for those architectures that
349 /* If times==NULL, set access and modification to current time,
350 * must be owner or have write permission.
351 * Else, update from *times, must be owner or super user.
353 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
357 struct inode * inode;
358 struct iattr newattrs;
360 error = user_path_walk(filename, &nd);
363 inode = nd.dentry->d_inode;
366 if (IS_RDONLY(inode))
369 /* Don't worry, the checks are done in inode_change_ok() */
370 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
373 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
376 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
377 newattrs.ia_atime.tv_nsec = 0;
379 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
380 newattrs.ia_mtime.tv_nsec = 0;
384 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
387 if (IS_IMMUTABLE(inode))
390 if (current->fsuid != inode->i_uid &&
391 (error = permission(inode,MAY_WRITE,&nd)) != 0)
395 error = notify_change(nd.dentry, &newattrs);
405 /* If times==NULL, set access and modification to current time,
406 * must be owner or have write permission.
407 * Else, update from *times, must be owner or super user.
409 long do_utimes(char __user * filename, struct timeval * times)
413 struct inode * inode;
414 struct iattr newattrs;
416 error = user_path_walk(filename, &nd);
420 inode = nd.dentry->d_inode;
423 if (IS_RDONLY(inode))
426 /* Don't worry, the checks are done in inode_change_ok() */
427 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
430 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
433 newattrs.ia_atime.tv_sec = times[0].tv_sec;
434 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
435 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
436 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
437 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
440 if (IS_IMMUTABLE(inode))
443 if (current->fsuid != inode->i_uid &&
444 (error = permission(inode,MAY_WRITE,&nd)) != 0)
448 error = notify_change(nd.dentry, &newattrs);
456 asmlinkage long sys_utimes(char __user * filename, struct timeval __user * utimes)
458 struct timeval times[2];
460 if (utimes && copy_from_user(×, utimes, sizeof(times)))
462 return do_utimes(filename, utimes ? times : NULL);
467 * access() needs to use the real uid/gid, not the effective uid/gid.
468 * We do this by temporarily clearing all FS-related capabilities and
469 * switching the fsuid/fsgid around to the real ones.
471 asmlinkage long sys_access(const char __user * filename, int mode)
474 int old_fsuid, old_fsgid;
475 kernel_cap_t old_cap;
478 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
481 old_fsuid = current->fsuid;
482 old_fsgid = current->fsgid;
483 old_cap = current->cap_effective;
485 current->fsuid = current->uid;
486 current->fsgid = current->gid;
489 * Clear the capabilities if we switch to a non-root user
491 * FIXME: There is a race here against sys_capset. The
492 * capabilities can change yet we will restore the old
493 * value below. We should hold task_capabilities_lock,
494 * but we cannot because user_path_walk can sleep.
497 cap_clear(current->cap_effective);
499 current->cap_effective = current->cap_permitted;
501 res = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
503 res = permission(nd.dentry->d_inode, mode, &nd);
504 /* SuS v2 requires we report a read only fs too */
505 if(!res && (mode & S_IWOTH) && IS_RDONLY(nd.dentry->d_inode)
506 && !special_file(nd.dentry->d_inode->i_mode))
511 current->fsuid = old_fsuid;
512 current->fsgid = old_fsgid;
513 current->cap_effective = old_cap;
518 asmlinkage long sys_chdir(const char __user * filename)
523 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
527 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
531 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
539 asmlinkage long sys_fchdir(unsigned int fd)
542 struct dentry *dentry;
544 struct vfsmount *mnt;
552 dentry = file->f_dentry;
553 mnt = file->f_vfsmnt;
554 inode = dentry->d_inode;
557 if (!S_ISDIR(inode->i_mode))
560 error = permission(inode, MAY_EXEC, NULL);
562 set_fs_pwd(current->fs, mnt, dentry);
569 asmlinkage long sys_chroot(const char __user * filename)
574 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
578 error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
583 if (!capable(CAP_SYS_CHROOT))
586 set_fs_root(current->fs, nd.mnt, nd.dentry);
595 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
597 struct inode * inode;
598 struct dentry * dentry;
601 struct iattr newattrs;
607 dentry = file->f_dentry;
608 inode = dentry->d_inode;
611 if (IS_RDONLY(inode))
614 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
617 if (mode == (mode_t) -1)
618 mode = inode->i_mode;
619 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
620 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
621 err = notify_change(dentry, &newattrs);
630 asmlinkage long sys_chmod(const char __user * filename, mode_t mode)
633 struct inode * inode;
635 struct iattr newattrs;
637 error = user_path_walk(filename, &nd);
640 inode = nd.dentry->d_inode;
643 if (IS_RDONLY(inode))
647 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
651 if (mode == (mode_t) -1)
652 mode = inode->i_mode;
653 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
654 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
655 error = notify_change(nd.dentry, &newattrs);
664 static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
666 struct inode * inode;
668 struct iattr newattrs;
671 if (!(inode = dentry->d_inode)) {
672 printk(KERN_ERR "chown_common: NULL inode\n");
676 if (IS_RDONLY(inode))
679 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
681 newattrs.ia_valid = ATTR_CTIME;
682 if (user != (uid_t) -1) {
683 newattrs.ia_valid |= ATTR_UID;
684 newattrs.ia_uid = user;
686 if (group != (gid_t) -1) {
687 newattrs.ia_valid |= ATTR_GID;
688 newattrs.ia_gid = group;
690 if (!S_ISDIR(inode->i_mode))
691 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
693 error = notify_change(dentry, &newattrs);
699 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
704 error = user_path_walk(filename, &nd);
706 error = chown_common(nd.dentry, user, group);
712 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
717 error = user_path_walk_link(filename, &nd);
719 error = chown_common(nd.dentry, user, group);
726 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
733 error = chown_common(file->f_dentry, user, group);
740 * Note that while the flag value (low two bits) for sys_open means:
746 * 00 - no permissions needed
747 * 01 - read-permission
748 * 10 - write-permission
750 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
753 struct file *filp_open(const char * filename, int flags, int mode)
755 int namei_flags, error;
759 if ((namei_flags+1) & O_ACCMODE)
761 if (namei_flags & O_TRUNC)
764 error = open_namei(filename, namei_flags, mode, &nd);
766 return dentry_open(nd.dentry, nd.mnt, flags);
768 return ERR_PTR(error);
771 EXPORT_SYMBOL(filp_open);
773 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
780 f = get_empty_filp();
784 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
785 inode = dentry->d_inode;
786 if (f->f_mode & FMODE_WRITE) {
787 error = get_write_access(inode);
792 f->f_mapping = inode->i_mapping;
793 f->f_dentry = dentry;
796 f->f_op = fops_get(inode->i_fop);
797 file_move(f, &inode->i_sb->s_files);
799 if (f->f_op && f->f_op->open) {
800 error = f->f_op->open(inode,f);
804 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
806 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
808 /* NB: we're sure to have correct a_ops only after f_op->open */
809 if (f->f_flags & O_DIRECT) {
810 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO) {
812 f = ERR_PTR(-EINVAL);
820 if (f->f_mode & FMODE_WRITE)
821 put_write_access(inode);
830 return ERR_PTR(error);
833 EXPORT_SYMBOL(dentry_open);
836 * Find an empty file descriptor entry, and mark it busy.
838 int get_unused_fd(void)
840 struct files_struct * files = current->files;
844 spin_lock(&files->file_lock);
847 fd = find_next_zero_bit(files->open_fds->fds_bits,
852 * N.B. For clone tasks sharing a files structure, this test
853 * will limit the total number of files that can be opened.
855 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
858 /* Do we need to expand the fd array or fd set? */
859 error = expand_files(files, fd);
865 * If we needed to expand the fs array we
866 * might have blocked - try again.
872 FD_SET(fd, files->open_fds);
873 FD_CLR(fd, files->close_on_exec);
874 files->next_fd = fd + 1;
877 if (files->fd[fd] != NULL) {
878 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
879 files->fd[fd] = NULL;
885 spin_unlock(&files->file_lock);
889 EXPORT_SYMBOL(get_unused_fd);
891 static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
893 __FD_CLR(fd, files->open_fds);
894 if (fd < files->next_fd)
898 void fastcall put_unused_fd(unsigned int fd)
900 struct files_struct *files = current->files;
901 spin_lock(&files->file_lock);
902 __put_unused_fd(files, fd);
903 spin_unlock(&files->file_lock);
906 EXPORT_SYMBOL(put_unused_fd);
909 * Install a file pointer in the fd array.
911 * The VFS is full of places where we drop the files lock between
912 * setting the open_fds bitmap and installing the file in the file
913 * array. At any such point, we are vulnerable to a dup2() race
914 * installing a file in the array before us. We need to detect this and
915 * fput() the struct file we are about to overwrite in this case.
917 * It should never happen - if we allow dup2() do it, _really_ bad things
921 void fastcall fd_install(unsigned int fd, struct file * file)
923 struct files_struct *files = current->files;
924 spin_lock(&files->file_lock);
925 if (unlikely(files->fd[fd] != NULL))
927 files->fd[fd] = file;
928 spin_unlock(&files->file_lock);
931 EXPORT_SYMBOL(fd_install);
933 asmlinkage long sys_open(const char __user * filename, int flags, int mode)
938 #if BITS_PER_LONG != 32
939 flags |= O_LARGEFILE;
941 tmp = getname(filename);
944 fd = get_unused_fd();
946 struct file *f = filp_open(tmp, flags, mode);
962 EXPORT_SYMBOL_GPL(sys_open);
967 * For backward compatibility? Maybe this should be moved
968 * into arch/i386 instead?
970 asmlinkage long sys_creat(const char __user * pathname, int mode)
972 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
978 * "id" is the POSIX thread ID. We use the
979 * files pointer for this..
981 int filp_close(struct file *filp, fl_owner_t id)
985 /* Report and clear outstanding errors */
986 retval = filp->f_error;
990 if (!file_count(filp)) {
991 printk(KERN_ERR "VFS: Close: file count is 0\n");
995 if (filp->f_op && filp->f_op->flush) {
996 int err = 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;
1019 spin_lock(&files->file_lock);
1020 if (fd >= files->max_fds)
1022 filp = files->fd[fd];
1025 files->fd[fd] = NULL;
1026 FD_CLR(fd, files->close_on_exec);
1027 __put_unused_fd(files, fd);
1028 spin_unlock(&files->file_lock);
1029 return filp_close(filp, files);
1032 spin_unlock(&files->file_lock);
1036 EXPORT_SYMBOL(sys_close);
1039 * This routine simulates a hangup on the tty, to arrange that users
1040 * are given clean terminals at login time.
1042 asmlinkage long sys_vhangup(void)
1044 if (capable(CAP_SYS_TTY_CONFIG)) {
1045 tty_vhangup(current->signal->tty);
1052 * Called when an inode is about to be open.
1053 * We use this to disallow opening large files on 32bit systems if
1054 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1055 * on this flag in sys_open.
1057 int generic_file_open(struct inode * inode, struct file * filp)
1059 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1064 EXPORT_SYMBOL(generic_file_open);
1067 * This is used by subsystems that don't want seekable
1070 int nonseekable_open(struct inode *inode, struct file *filp)
1072 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1076 EXPORT_SYMBOL(nonseekable_open);