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/capability.h>
20 #include <linux/security.h>
21 #include <linux/mount.h>
22 #include <linux/vfs.h>
23 #include <linux/fcntl.h>
24 #include <asm/uaccess.h>
26 #include <linux/personality.h>
27 #include <linux/pagemap.h>
28 #include <linux/syscalls.h>
29 #include <linux/rcupdate.h>
30 #include <linux/audit.h>
31 #include <linux/vs_limit.h>
32 #include <linux/vs_dlimit.h>
33 #include <linux/vserver/xid.h>
35 #include <asm/unistd.h>
37 int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
42 struct super_block *sb = dentry->d_sb;
45 if (sb->s_op->statfs) {
46 memset(buf, 0, sizeof(*buf));
47 retval = security_sb_statfs(dentry);
50 retval = sb->s_op->statfs(dentry, buf);
51 if (retval == 0 && buf->f_frsize == 0)
52 buf->f_frsize = buf->f_bsize;
54 if (!vx_check(0, VX_ADMIN|VX_WATCH))
55 vx_vsi_statfs(sb, buf);
60 EXPORT_SYMBOL(vfs_statfs);
62 static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)
67 retval = vfs_statfs(dentry, &st);
71 if (sizeof(*buf) == sizeof(st))
72 memcpy(buf, &st, sizeof(st));
74 if (sizeof buf->f_blocks == 4) {
75 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
76 0xffffffff00000000ULL)
79 * f_files and f_ffree may be -1; it's okay to stuff
82 if (st.f_files != -1 &&
83 (st.f_files & 0xffffffff00000000ULL))
85 if (st.f_ffree != -1 &&
86 (st.f_ffree & 0xffffffff00000000ULL))
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 static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
110 retval = vfs_statfs(dentry, &st);
114 if (sizeof(*buf) == sizeof(st))
115 memcpy(buf, &st, sizeof(st));
117 buf->f_type = st.f_type;
118 buf->f_bsize = st.f_bsize;
119 buf->f_blocks = st.f_blocks;
120 buf->f_bfree = st.f_bfree;
121 buf->f_bavail = st.f_bavail;
122 buf->f_files = st.f_files;
123 buf->f_ffree = st.f_ffree;
124 buf->f_fsid = st.f_fsid;
125 buf->f_namelen = st.f_namelen;
126 buf->f_frsize = st.f_frsize;
127 memset(buf->f_spare, 0, sizeof(buf->f_spare));
132 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
137 error = user_path_walk(path, &nd);
140 error = vfs_statfs_native(nd.dentry, &tmp);
141 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
149 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
154 if (sz != sizeof(*buf))
156 error = user_path_walk(path, &nd);
159 error = vfs_statfs64(nd.dentry, &tmp);
160 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
168 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
178 error = vfs_statfs_native(file->f_dentry, &tmp);
179 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
186 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
192 if (sz != sizeof(*buf))
199 error = vfs_statfs64(file->f_dentry, &tmp);
200 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
207 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
211 struct iattr newattrs;
213 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
217 newattrs.ia_size = length;
218 newattrs.ia_valid = ATTR_SIZE | time_attrs;
220 newattrs.ia_file = filp;
221 newattrs.ia_valid |= ATTR_FILE;
224 mutex_lock(&dentry->d_inode->i_mutex);
225 err = notify_change(dentry, &newattrs);
226 mutex_unlock(&dentry->d_inode->i_mutex);
230 static long do_sys_truncate(const char __user * path, loff_t length)
233 struct inode * inode;
237 if (length < 0) /* sorry, but loff_t says... */
240 error = user_path_walk(path, &nd);
243 inode = nd.dentry->d_inode;
245 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
247 if (S_ISDIR(inode->i_mode))
251 if (!S_ISREG(inode->i_mode))
254 error = vfs_permission(&nd, MAY_WRITE);
259 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
263 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
267 * Make sure that there are no leases.
269 error = break_lease(inode, FMODE_WRITE);
273 error = get_write_access(inode);
277 error = locks_verify_truncate(inode, NULL, length);
280 error = do_truncate(nd.dentry, length, 0, NULL);
282 put_write_access(inode);
290 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
292 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
293 return do_sys_truncate(path, (long)length);
296 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
298 struct inode * inode;
299 struct dentry *dentry;
311 /* explicitly opened as large or we are on 64-bit box */
312 if (file->f_flags & O_LARGEFILE)
315 dentry = file->f_dentry;
316 inode = dentry->d_inode;
318 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
322 /* Cannot ftruncate over 2^31 bytes without large file support */
323 if (small && length > MAX_NON_LFS)
327 if (IS_APPEND(inode))
330 error = locks_verify_truncate(inode, file, length);
332 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
339 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
341 long ret = do_sys_ftruncate(fd, length, 1);
342 /* avoid REGPARM breakage on x86: */
343 prevent_tail_call(ret);
347 /* LFS versions of truncate are only needed on 32 bit machines */
348 #if BITS_PER_LONG == 32
349 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
351 return do_sys_truncate(path, length);
354 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
356 long ret = do_sys_ftruncate(fd, length, 0);
357 /* avoid REGPARM breakage on x86: */
358 prevent_tail_call(ret);
363 #ifdef __ARCH_WANT_SYS_UTIME
366 * sys_utime() can be implemented in user-level using sys_utimes().
367 * Is this for backwards compatibility? If so, why not move it
368 * into the appropriate arch directory (for those architectures that
372 /* If times==NULL, set access and modification to current time,
373 * must be owner or have write permission.
374 * Else, update from *times, must be owner or super user.
376 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
380 struct inode * inode;
381 struct iattr newattrs;
383 error = user_path_walk(filename, &nd);
386 inode = nd.dentry->d_inode;
389 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
392 /* Don't worry, the checks are done in inode_change_ok() */
393 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
396 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
399 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
400 newattrs.ia_atime.tv_nsec = 0;
402 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
403 newattrs.ia_mtime.tv_nsec = 0;
407 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
410 if (IS_IMMUTABLE(inode))
413 if (current->fsuid != inode->i_uid &&
414 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
417 mutex_lock(&inode->i_mutex);
418 error = notify_change(nd.dentry, &newattrs);
419 mutex_unlock(&inode->i_mutex);
428 /* If times==NULL, set access and modification to current time,
429 * must be owner or have write permission.
430 * Else, update from *times, must be owner or super user.
432 long do_utimes(int dfd, char __user *filename, struct timeval *times)
436 struct inode * inode;
437 struct iattr newattrs;
439 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
443 inode = nd.dentry->d_inode;
446 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
449 /* Don't worry, the checks are done in inode_change_ok() */
450 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
453 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
456 newattrs.ia_atime.tv_sec = times[0].tv_sec;
457 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
458 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
459 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
460 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
463 if (IS_IMMUTABLE(inode))
466 if (current->fsuid != inode->i_uid &&
467 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
470 mutex_lock(&inode->i_mutex);
471 error = notify_change(nd.dentry, &newattrs);
472 mutex_unlock(&inode->i_mutex);
479 asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
481 struct timeval times[2];
483 if (utimes && copy_from_user(×, utimes, sizeof(times)))
485 return do_utimes(dfd, filename, utimes ? times : NULL);
488 asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
490 return sys_futimesat(AT_FDCWD, filename, utimes);
495 * access() needs to use the real uid/gid, not the effective uid/gid.
496 * We do this by temporarily clearing all FS-related capabilities and
497 * switching the fsuid/fsgid around to the real ones.
499 asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
502 int old_fsuid, old_fsgid;
503 kernel_cap_t old_cap;
506 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
509 old_fsuid = current->fsuid;
510 old_fsgid = current->fsgid;
511 old_cap = current->cap_effective;
513 current->fsuid = current->uid;
514 current->fsgid = current->gid;
517 * Clear the capabilities if we switch to a non-root user
519 * FIXME: There is a race here against sys_capset. The
520 * capabilities can change yet we will restore the old
521 * value below. We should hold task_capabilities_lock,
522 * but we cannot because user_path_walk can sleep.
525 cap_clear(current->cap_effective);
527 current->cap_effective = current->cap_permitted;
529 res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
531 res = vfs_permission(&nd, mode);
532 /* SuS v2 requires we report a read only fs too */
533 if(!res && (mode & S_IWOTH)
534 && (IS_RDONLY(nd.dentry->d_inode) || MNT_IS_RDONLY(nd.mnt))
535 && !special_file(nd.dentry->d_inode->i_mode))
540 current->fsuid = old_fsuid;
541 current->fsgid = old_fsgid;
542 current->cap_effective = old_cap;
547 asmlinkage long sys_access(const char __user *filename, int mode)
549 return sys_faccessat(AT_FDCWD, filename, mode);
552 asmlinkage long sys_chdir(const char __user * filename)
557 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
561 error = vfs_permission(&nd, MAY_EXEC);
565 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
573 EXPORT_SYMBOL_GPL(sys_chdir);
575 asmlinkage long sys_fchdir(unsigned int fd)
578 struct dentry *dentry;
580 struct vfsmount *mnt;
588 dentry = file->f_dentry;
589 mnt = file->f_vfsmnt;
590 inode = dentry->d_inode;
593 if (!S_ISDIR(inode->i_mode))
596 error = file_permission(file, MAY_EXEC);
598 set_fs_pwd(current->fs, mnt, dentry);
605 asmlinkage long sys_chroot(const char __user * filename)
610 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
614 error = vfs_permission(&nd, MAY_EXEC);
619 if (!capable(CAP_SYS_CHROOT))
622 set_fs_root(current->fs, nd.mnt, nd.dentry);
631 EXPORT_SYMBOL_GPL(sys_chroot);
633 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
635 struct inode * inode;
636 struct dentry * dentry;
639 struct iattr newattrs;
645 dentry = file->f_dentry;
646 inode = dentry->d_inode;
648 audit_inode(NULL, inode);
651 if (IS_RDONLY(inode) || MNT_IS_RDONLY(file->f_vfsmnt))
654 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
656 mutex_lock(&inode->i_mutex);
657 if (mode == (mode_t) -1)
658 mode = inode->i_mode;
659 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
660 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
661 err = notify_change(dentry, &newattrs);
662 mutex_unlock(&inode->i_mutex);
670 asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
674 struct inode * inode;
676 struct iattr newattrs;
678 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
681 inode = nd.dentry->d_inode;
684 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
688 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
691 mutex_lock(&inode->i_mutex);
692 if (mode == (mode_t) -1)
693 mode = inode->i_mode;
694 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
695 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
696 error = notify_change(nd.dentry, &newattrs);
697 mutex_unlock(&inode->i_mutex);
705 asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
707 return sys_fchmodat(AT_FDCWD, filename, mode);
710 static int chown_common(struct dentry *dentry, struct vfsmount *mnt,
711 uid_t user, gid_t group)
713 struct inode * inode;
715 struct iattr newattrs;
718 if (!(inode = dentry->d_inode)) {
719 printk(KERN_ERR "chown_common: NULL inode\n");
723 if (IS_RDONLY(inode) || MNT_IS_RDONLY(mnt))
726 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
728 newattrs.ia_valid = ATTR_CTIME;
729 if (user != (uid_t) -1) {
730 newattrs.ia_valid |= ATTR_UID;
731 newattrs.ia_uid = vx_map_uid(user);
733 if (group != (gid_t) -1) {
734 newattrs.ia_valid |= ATTR_GID;
735 newattrs.ia_gid = vx_map_gid(group);
737 if (!S_ISDIR(inode->i_mode))
738 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
739 mutex_lock(&inode->i_mutex);
740 error = notify_change(dentry, &newattrs);
741 mutex_unlock(&inode->i_mutex);
746 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
751 error = user_path_walk(filename, &nd);
753 error = chown_common(nd.dentry, nd.mnt, user, group);
759 asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
760 gid_t group, int flag)
766 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
769 follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
770 error = __user_walk_fd(dfd, filename, follow, &nd);
772 error = chown_common(nd.dentry, nd.mnt, user, group);
779 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
784 error = user_path_walk_link(filename, &nd);
786 error = chown_common(nd.dentry, nd.mnt, user, group);
793 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
800 struct dentry * dentry;
801 dentry = file->f_dentry;
802 audit_inode(NULL, dentry->d_inode);
803 error = chown_common(dentry, file->f_vfsmnt, user, group);
809 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
810 int flags, struct file *f,
811 int (*open)(struct inode *, struct file *))
817 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
818 FMODE_PREAD | FMODE_PWRITE;
819 inode = dentry->d_inode;
820 if (f->f_mode & FMODE_WRITE) {
821 error = get_write_access(inode);
826 f->f_mapping = inode->i_mapping;
827 f->f_dentry = dentry;
830 f->f_op = fops_get(inode->i_fop);
831 file_move(f, &inode->i_sb->s_files);
833 if (!open && f->f_op)
834 open = f->f_op->open;
836 error = open(inode, f);
841 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
843 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
845 /* NB: we're sure to have correct a_ops only after f_op->open */
846 if (f->f_flags & O_DIRECT) {
847 if (!f->f_mapping->a_ops ||
848 ((!f->f_mapping->a_ops->direct_IO) &&
849 (!f->f_mapping->a_ops->get_xip_page))) {
851 f = ERR_PTR(-EINVAL);
859 if (f->f_mode & FMODE_WRITE)
860 put_write_access(inode);
868 return ERR_PTR(error);
872 * Note that while the flag value (low two bits) for sys_open means:
878 * 00 - no permissions needed
879 * 01 - read-permission
880 * 10 - write-permission
882 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
885 static struct file *do_filp_open(int dfd, const char *filename, int flags,
888 int namei_flags, error;
892 if ((namei_flags+1) & O_ACCMODE)
895 error = open_namei(dfd, filename, namei_flags, mode, &nd);
897 return nameidata_to_filp(&nd, flags);
899 return ERR_PTR(error);
902 struct file *filp_open(const char *filename, int flags, int mode)
904 return do_filp_open(AT_FDCWD, filename, flags, mode);
906 EXPORT_SYMBOL(filp_open);
909 * lookup_instantiate_filp - instantiates the open intent filp
910 * @nd: pointer to nameidata
911 * @dentry: pointer to dentry
912 * @open: open callback
914 * Helper for filesystems that want to use lookup open intents and pass back
915 * a fully instantiated struct file to the caller.
916 * This function is meant to be called from within a filesystem's
918 * Beware of calling it for non-regular files! Those ->open methods might block
919 * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
920 * leading to a deadlock, as nobody can open that fifo anymore, because
921 * another process to open fifo will block on locked parent when doing lookup).
922 * Note that in case of error, nd->intent.open.file is destroyed, but the
923 * path information remains valid.
924 * If the open callback is set to NULL, then the standard f_op->open()
925 * filesystem callback is substituted.
927 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
928 int (*open)(struct inode *, struct file *))
930 if (IS_ERR(nd->intent.open.file))
934 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
935 nd->intent.open.flags - 1,
936 nd->intent.open.file,
939 return nd->intent.open.file;
941 release_open_intent(nd);
942 nd->intent.open.file = (struct file *)dentry;
945 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
948 * nameidata_to_filp - convert a nameidata to an open filp.
949 * @nd: pointer to nameidata
952 * Note that this function destroys the original nameidata
954 struct file *nameidata_to_filp(struct nameidata *nd, int flags)
958 /* Pick up the filp from the open intent */
959 filp = nd->intent.open.file;
960 /* Has the filesystem initialised the file for us? */
961 if (filp->f_dentry == NULL)
962 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
969 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
972 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
978 f = get_empty_filp();
982 return ERR_PTR(error);
985 return __dentry_open(dentry, mnt, flags, f, NULL);
987 EXPORT_SYMBOL(dentry_open);
990 * Find an empty file descriptor entry, and mark it busy.
992 int get_unused_fd(void)
994 struct files_struct * files = current->files;
999 spin_lock(&files->file_lock);
1002 fdt = files_fdtable(files);
1003 fd = find_next_zero_bit(fdt->open_fds->fds_bits,
1008 * N.B. For clone tasks sharing a files structure, this test
1009 * will limit the total number of files that can be opened.
1011 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
1014 /* Do we need to expand the fd array or fd set? */
1015 error = expand_files(files, fd);
1021 * If we needed to expand the fs array we
1022 * might have blocked - try again.
1028 FD_SET(fd, fdt->open_fds);
1029 FD_CLR(fd, fdt->close_on_exec);
1030 files->next_fd = fd + 1;
1034 if (fdt->fd[fd] != NULL) {
1035 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
1042 spin_unlock(&files->file_lock);
1046 EXPORT_SYMBOL(get_unused_fd);
1048 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
1050 struct fdtable *fdt = files_fdtable(files);
1051 __FD_CLR(fd, fdt->open_fds);
1052 if (fd < files->next_fd)
1053 files->next_fd = fd;
1057 void fastcall put_unused_fd(unsigned int fd)
1059 struct files_struct *files = current->files;
1060 spin_lock(&files->file_lock);
1061 __put_unused_fd(files, fd);
1062 spin_unlock(&files->file_lock);
1065 EXPORT_SYMBOL(put_unused_fd);
1068 * Install a file pointer in the fd array.
1070 * The VFS is full of places where we drop the files lock between
1071 * setting the open_fds bitmap and installing the file in the file
1072 * array. At any such point, we are vulnerable to a dup2() race
1073 * installing a file in the array before us. We need to detect this and
1074 * fput() the struct file we are about to overwrite in this case.
1076 * It should never happen - if we allow dup2() do it, _really_ bad things
1080 void fastcall fd_install(unsigned int fd, struct file * file)
1082 struct files_struct *files = current->files;
1083 struct fdtable *fdt;
1084 spin_lock(&files->file_lock);
1085 fdt = files_fdtable(files);
1086 BUG_ON(fdt->fd[fd] != NULL);
1087 rcu_assign_pointer(fdt->fd[fd], file);
1088 spin_unlock(&files->file_lock);
1091 EXPORT_SYMBOL(fd_install);
1093 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
1095 char *tmp = getname(filename);
1096 int fd = PTR_ERR(tmp);
1099 fd = get_unused_fd();
1101 struct file *f = do_filp_open(dfd, tmp, flags, mode);
1106 fsnotify_open(f->f_dentry);
1115 asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1119 if (force_o_largefile())
1120 flags |= O_LARGEFILE;
1122 ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1123 /* avoid REGPARM breakage on x86: */
1124 prevent_tail_call(ret);
1128 asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1133 if (force_o_largefile())
1134 flags |= O_LARGEFILE;
1136 ret = do_sys_open(dfd, filename, flags, mode);
1137 /* avoid REGPARM breakage on x86: */
1138 prevent_tail_call(ret);
1145 * For backward compatibility? Maybe this should be moved
1146 * into arch/i386 instead?
1148 asmlinkage long sys_creat(const char __user * pathname, int mode)
1150 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1156 * "id" is the POSIX thread ID. We use the
1157 * files pointer for this..
1159 int filp_close(struct file *filp, fl_owner_t id)
1163 if (!file_count(filp)) {
1164 printk(KERN_ERR "VFS: Close: file count is 0\n");
1168 if (filp->f_op && filp->f_op->flush)
1169 retval = filp->f_op->flush(filp, id);
1171 dnotify_flush(filp, id);
1172 locks_remove_posix(filp, id);
1177 EXPORT_SYMBOL(filp_close);
1180 * Careful here! We test whether the file pointer is NULL before
1181 * releasing the fd. This ensures that one clone task can't release
1182 * an fd while another clone is opening it.
1184 asmlinkage long sys_close(unsigned int fd)
1187 struct files_struct *files = current->files;
1188 struct fdtable *fdt;
1190 spin_lock(&files->file_lock);
1191 fdt = files_fdtable(files);
1192 if (fd >= fdt->max_fds)
1197 rcu_assign_pointer(fdt->fd[fd], NULL);
1198 FD_CLR(fd, fdt->close_on_exec);
1199 __put_unused_fd(files, fd);
1200 spin_unlock(&files->file_lock);
1201 return filp_close(filp, files);
1204 spin_unlock(&files->file_lock);
1208 EXPORT_SYMBOL(sys_close);
1211 * This routine simulates a hangup on the tty, to arrange that users
1212 * are given clean terminals at login time.
1214 asmlinkage long sys_vhangup(void)
1216 if (capable(CAP_SYS_TTY_CONFIG)) {
1217 /* XXX: this needs locking */
1218 tty_vhangup(current->signal->tty);
1225 * Called when an inode is about to be open.
1226 * We use this to disallow opening large files on 32bit systems if
1227 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1228 * on this flag in sys_open.
1230 int generic_file_open(struct inode * inode, struct file * filp)
1232 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1237 EXPORT_SYMBOL(generic_file_open);
1240 * This is used by subsystems that don't want seekable
1243 int nonseekable_open(struct inode *inode, struct file *filp)
1245 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1249 EXPORT_SYMBOL(nonseekable_open);