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_base.h>
32 #include <linux/vs_limit.h>
33 #include <linux/vs_dlimit.h>
34 #include <linux/vserver/xid.h>
36 #include <asm/unistd.h>
38 int vfs_statfs(struct super_block *sb, struct kstatfs *buf)
44 if (sb->s_op->statfs) {
45 memset(buf, 0, sizeof(*buf));
46 retval = security_sb_statfs(sb);
49 retval = sb->s_op->statfs(sb, buf);
50 if (retval == 0 && buf->f_frsize == 0)
51 buf->f_frsize = buf->f_bsize;
53 if (!vx_check(0, VX_ADMIN|VX_WATCH))
54 vx_vsi_statfs(sb, buf);
59 EXPORT_SYMBOL(vfs_statfs);
61 static int vfs_statfs_native(struct super_block *sb, struct statfs *buf)
66 retval = vfs_statfs(sb, &st);
70 if (sizeof(*buf) == sizeof(st))
71 memcpy(buf, &st, sizeof(st));
73 if (sizeof buf->f_blocks == 4) {
74 if ((st.f_blocks | st.f_bfree | st.f_bavail) &
75 0xffffffff00000000ULL)
78 * f_files and f_ffree may be -1; it's okay to stuff
81 if (st.f_files != -1 &&
82 (st.f_files & 0xffffffff00000000ULL))
84 if (st.f_ffree != -1 &&
85 (st.f_ffree & 0xffffffff00000000ULL))
89 buf->f_type = st.f_type;
90 buf->f_bsize = st.f_bsize;
91 buf->f_blocks = st.f_blocks;
92 buf->f_bfree = st.f_bfree;
93 buf->f_bavail = st.f_bavail;
94 buf->f_files = st.f_files;
95 buf->f_ffree = st.f_ffree;
96 buf->f_fsid = st.f_fsid;
97 buf->f_namelen = st.f_namelen;
98 buf->f_frsize = st.f_frsize;
99 memset(buf->f_spare, 0, sizeof(buf->f_spare));
104 static int vfs_statfs64(struct super_block *sb, struct statfs64 *buf)
109 retval = vfs_statfs(sb, &st);
113 if (sizeof(*buf) == sizeof(st))
114 memcpy(buf, &st, sizeof(st));
116 buf->f_type = st.f_type;
117 buf->f_bsize = st.f_bsize;
118 buf->f_blocks = st.f_blocks;
119 buf->f_bfree = st.f_bfree;
120 buf->f_bavail = st.f_bavail;
121 buf->f_files = st.f_files;
122 buf->f_ffree = st.f_ffree;
123 buf->f_fsid = st.f_fsid;
124 buf->f_namelen = st.f_namelen;
125 buf->f_frsize = st.f_frsize;
126 memset(buf->f_spare, 0, sizeof(buf->f_spare));
131 asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
136 error = user_path_walk(path, &nd);
139 error = vfs_statfs_native(nd.dentry->d_inode->i_sb, &tmp);
140 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
148 asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
153 if (sz != sizeof(*buf))
155 error = user_path_walk(path, &nd);
158 error = vfs_statfs64(nd.dentry->d_inode->i_sb, &tmp);
159 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
167 asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
177 error = vfs_statfs_native(file->f_dentry->d_inode->i_sb, &tmp);
178 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
185 asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
191 if (sz != sizeof(*buf))
198 error = vfs_statfs64(file->f_dentry->d_inode->i_sb, &tmp);
199 if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
206 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
210 struct iattr newattrs;
212 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
216 newattrs.ia_size = length;
217 newattrs.ia_valid = ATTR_SIZE | time_attrs;
219 newattrs.ia_file = filp;
220 newattrs.ia_valid |= ATTR_FILE;
223 mutex_lock(&dentry->d_inode->i_mutex);
224 err = notify_change(dentry, &newattrs);
225 mutex_unlock(&dentry->d_inode->i_mutex);
229 static long do_sys_truncate(const char __user * path, loff_t length)
232 struct inode * inode;
236 if (length < 0) /* sorry, but loff_t says... */
239 error = user_path_walk(path, &nd);
242 inode = nd.dentry->d_inode;
244 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
246 if (S_ISDIR(inode->i_mode))
250 if (!S_ISREG(inode->i_mode))
253 error = vfs_permission(&nd, MAY_WRITE);
258 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
262 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
266 * Make sure that there are no leases.
268 error = break_lease(inode, FMODE_WRITE);
272 error = get_write_access(inode);
276 error = locks_verify_truncate(inode, NULL, length);
279 error = do_truncate(nd.dentry, length, 0, NULL);
281 put_write_access(inode);
289 asmlinkage long sys_truncate(const char __user * path, unsigned long length)
291 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
292 return do_sys_truncate(path, (long)length);
295 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
297 struct inode * inode;
298 struct dentry *dentry;
310 /* explicitly opened as large or we are on 64-bit box */
311 if (file->f_flags & O_LARGEFILE)
314 dentry = file->f_dentry;
315 inode = dentry->d_inode;
317 if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
321 /* Cannot ftruncate over 2^31 bytes without large file support */
322 if (small && length > MAX_NON_LFS)
326 if (IS_APPEND(inode))
329 error = locks_verify_truncate(inode, file, length);
331 error = do_truncate(dentry, length, 0, file);
338 asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
340 long ret = do_sys_ftruncate(fd, length, 1);
341 /* avoid REGPARM breakage on x86: */
342 prevent_tail_call(ret);
346 /* LFS versions of truncate are only needed on 32 bit machines */
347 #if BITS_PER_LONG == 32
348 asmlinkage long sys_truncate64(const char __user * path, loff_t length)
350 return do_sys_truncate(path, length);
353 asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
355 long ret = do_sys_ftruncate(fd, length, 0);
356 /* avoid REGPARM breakage on x86: */
357 prevent_tail_call(ret);
362 #ifdef __ARCH_WANT_SYS_UTIME
365 * sys_utime() can be implemented in user-level using sys_utimes().
366 * Is this for backwards compatibility? If so, why not move it
367 * into the appropriate arch directory (for those architectures that
371 /* If times==NULL, set access and modification to current time,
372 * must be owner or have write permission.
373 * Else, update from *times, must be owner or super user.
375 asmlinkage long sys_utime(char __user * filename, struct utimbuf __user * times)
379 struct inode * inode;
380 struct iattr newattrs;
382 error = user_path_walk(filename, &nd);
385 inode = nd.dentry->d_inode;
388 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
391 /* Don't worry, the checks are done in inode_change_ok() */
392 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
395 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
398 error = get_user(newattrs.ia_atime.tv_sec, ×->actime);
399 newattrs.ia_atime.tv_nsec = 0;
401 error = get_user(newattrs.ia_mtime.tv_sec, ×->modtime);
402 newattrs.ia_mtime.tv_nsec = 0;
406 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
409 if (IS_IMMUTABLE(inode))
412 if (current->fsuid != inode->i_uid &&
413 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
416 mutex_lock(&inode->i_mutex);
417 error = notify_change(nd.dentry, &newattrs);
418 mutex_unlock(&inode->i_mutex);
427 /* If times==NULL, set access and modification to current time,
428 * must be owner or have write permission.
429 * Else, update from *times, must be owner or super user.
431 long do_utimes(int dfd, char __user *filename, struct timeval *times)
435 struct inode * inode;
436 struct iattr newattrs;
438 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
442 inode = nd.dentry->d_inode;
445 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
448 /* Don't worry, the checks are done in inode_change_ok() */
449 newattrs.ia_valid = ATTR_CTIME | ATTR_MTIME | ATTR_ATIME;
452 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
455 newattrs.ia_atime.tv_sec = times[0].tv_sec;
456 newattrs.ia_atime.tv_nsec = times[0].tv_usec * 1000;
457 newattrs.ia_mtime.tv_sec = times[1].tv_sec;
458 newattrs.ia_mtime.tv_nsec = times[1].tv_usec * 1000;
459 newattrs.ia_valid |= ATTR_ATIME_SET | ATTR_MTIME_SET;
462 if (IS_IMMUTABLE(inode))
465 if (current->fsuid != inode->i_uid &&
466 (error = vfs_permission(&nd, MAY_WRITE)) != 0)
469 mutex_lock(&inode->i_mutex);
470 error = notify_change(nd.dentry, &newattrs);
471 mutex_unlock(&inode->i_mutex);
478 asmlinkage long sys_futimesat(int dfd, char __user *filename, struct timeval __user *utimes)
480 struct timeval times[2];
482 if (utimes && copy_from_user(×, utimes, sizeof(times)))
484 return do_utimes(dfd, filename, utimes ? times : NULL);
487 asmlinkage long sys_utimes(char __user *filename, struct timeval __user *utimes)
489 return sys_futimesat(AT_FDCWD, filename, utimes);
494 * access() needs to use the real uid/gid, not the effective uid/gid.
495 * We do this by temporarily clearing all FS-related capabilities and
496 * switching the fsuid/fsgid around to the real ones.
498 asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
501 int old_fsuid, old_fsgid;
502 kernel_cap_t old_cap;
505 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
508 old_fsuid = current->fsuid;
509 old_fsgid = current->fsgid;
510 old_cap = current->cap_effective;
512 current->fsuid = current->uid;
513 current->fsgid = current->gid;
516 * Clear the capabilities if we switch to a non-root user
518 * FIXME: There is a race here against sys_capset. The
519 * capabilities can change yet we will restore the old
520 * value below. We should hold task_capabilities_lock,
521 * but we cannot because user_path_walk can sleep.
524 cap_clear(current->cap_effective);
526 current->cap_effective = current->cap_permitted;
528 res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
530 res = vfs_permission(&nd, mode);
531 /* SuS v2 requires we report a read only fs too */
532 if(!res && (mode & S_IWOTH)
533 && (IS_RDONLY(nd.dentry->d_inode) || MNT_IS_RDONLY(nd.mnt))
534 && !special_file(nd.dentry->d_inode->i_mode))
539 current->fsuid = old_fsuid;
540 current->fsgid = old_fsgid;
541 current->cap_effective = old_cap;
546 asmlinkage long sys_access(const char __user *filename, int mode)
548 return sys_faccessat(AT_FDCWD, filename, mode);
551 asmlinkage long sys_chdir(const char __user * filename)
556 error = __user_walk(filename, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &nd);
560 error = vfs_permission(&nd, MAY_EXEC);
564 set_fs_pwd(current->fs, nd.mnt, nd.dentry);
572 EXPORT_SYMBOL_GPL(sys_chdir);
574 asmlinkage long sys_fchdir(unsigned int fd)
577 struct dentry *dentry;
579 struct vfsmount *mnt;
587 dentry = file->f_dentry;
588 mnt = file->f_vfsmnt;
589 inode = dentry->d_inode;
592 if (!S_ISDIR(inode->i_mode))
595 error = file_permission(file, MAY_EXEC);
597 set_fs_pwd(current->fs, mnt, dentry);
604 asmlinkage long sys_chroot(const char __user * filename)
609 error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
613 error = vfs_permission(&nd, MAY_EXEC);
618 if (!capable(CAP_SYS_CHROOT))
621 set_fs_root(current->fs, nd.mnt, nd.dentry);
630 EXPORT_SYMBOL_GPL(sys_chroot);
632 asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
634 struct inode * inode;
635 struct dentry * dentry;
638 struct iattr newattrs;
644 dentry = file->f_dentry;
645 inode = dentry->d_inode;
647 audit_inode(NULL, inode, 0);
650 if (IS_RDONLY(inode) || MNT_IS_RDONLY(file->f_vfsmnt))
653 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
655 mutex_lock(&inode->i_mutex);
656 if (mode == (mode_t) -1)
657 mode = inode->i_mode;
658 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
659 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
660 err = notify_change(dentry, &newattrs);
661 mutex_unlock(&inode->i_mutex);
669 asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
673 struct inode * inode;
675 struct iattr newattrs;
677 error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
680 inode = nd.dentry->d_inode;
683 if (IS_RDONLY(inode) || MNT_IS_RDONLY(nd.mnt))
687 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
690 mutex_lock(&inode->i_mutex);
691 if (mode == (mode_t) -1)
692 mode = inode->i_mode;
693 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
694 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
695 error = notify_change(nd.dentry, &newattrs);
696 mutex_unlock(&inode->i_mutex);
704 asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
706 return sys_fchmodat(AT_FDCWD, filename, mode);
709 static int chown_common(struct dentry *dentry, struct vfsmount *mnt,
710 uid_t user, gid_t group)
712 struct inode * inode;
714 struct iattr newattrs;
717 if (!(inode = dentry->d_inode)) {
718 printk(KERN_ERR "chown_common: NULL inode\n");
722 if (IS_RDONLY(inode) || MNT_IS_RDONLY(mnt))
725 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
727 newattrs.ia_valid = ATTR_CTIME;
728 if (user != (uid_t) -1) {
729 newattrs.ia_valid |= ATTR_UID;
730 newattrs.ia_uid = vx_map_uid(user);
732 if (group != (gid_t) -1) {
733 newattrs.ia_valid |= ATTR_GID;
734 newattrs.ia_gid = vx_map_gid(group);
736 if (!S_ISDIR(inode->i_mode))
737 newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
738 mutex_lock(&inode->i_mutex);
739 error = notify_change(dentry, &newattrs);
740 mutex_unlock(&inode->i_mutex);
745 asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
750 error = user_path_walk(filename, &nd);
752 error = chown_common(nd.dentry, nd.mnt, user, group);
758 asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
759 gid_t group, int flag)
765 if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
768 follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
769 error = __user_walk_fd(dfd, filename, follow, &nd);
771 error = chown_common(nd.dentry, nd.mnt, user, group);
778 asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
783 error = user_path_walk_link(filename, &nd);
785 error = chown_common(nd.dentry, nd.mnt, user, group);
792 asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
799 struct dentry * dentry;
800 dentry = file->f_dentry;
801 audit_inode(NULL, dentry->d_inode, 0);
802 error = chown_common(dentry, file->f_vfsmnt, user, group);
808 static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
809 int flags, struct file *f,
810 int (*open)(struct inode *, struct file *))
816 f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
817 FMODE_PREAD | FMODE_PWRITE;
818 inode = dentry->d_inode;
819 if (f->f_mode & FMODE_WRITE) {
820 error = get_write_access(inode);
825 f->f_mapping = inode->i_mapping;
826 f->f_dentry = dentry;
829 f->f_op = fops_get(inode->i_fop);
830 file_move(f, &inode->i_sb->s_files);
832 if (!open && f->f_op)
833 open = f->f_op->open;
835 error = open(inode, f);
840 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
842 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
844 /* NB: we're sure to have correct a_ops only after f_op->open */
845 if (f->f_flags & O_DIRECT) {
846 if (!f->f_mapping->a_ops ||
847 ((!f->f_mapping->a_ops->direct_IO) &&
848 (!f->f_mapping->a_ops->get_xip_page))) {
850 f = ERR_PTR(-EINVAL);
858 if (f->f_mode & FMODE_WRITE)
859 put_write_access(inode);
867 return ERR_PTR(error);
871 * Note that while the flag value (low two bits) for sys_open means:
877 * 00 - no permissions needed
878 * 01 - read-permission
879 * 10 - write-permission
881 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
884 static struct file *do_filp_open(int dfd, const char *filename, int flags,
887 int namei_flags, error;
891 if ((namei_flags+1) & O_ACCMODE)
894 error = open_namei(dfd, filename, namei_flags, mode, &nd);
896 return nameidata_to_filp(&nd, flags);
898 return ERR_PTR(error);
901 struct file *filp_open(const char *filename, int flags, int mode)
903 return do_filp_open(AT_FDCWD, filename, flags, mode);
905 EXPORT_SYMBOL(filp_open);
908 * lookup_instantiate_filp - instantiates the open intent filp
909 * @nd: pointer to nameidata
910 * @dentry: pointer to dentry
911 * @open: open callback
913 * Helper for filesystems that want to use lookup open intents and pass back
914 * a fully instantiated struct file to the caller.
915 * This function is meant to be called from within a filesystem's
917 * Beware of calling it for non-regular files! Those ->open methods might block
918 * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
919 * leading to a deadlock, as nobody can open that fifo anymore, because
920 * another process to open fifo will block on locked parent when doing lookup).
921 * Note that in case of error, nd->intent.open.file is destroyed, but the
922 * path information remains valid.
923 * If the open callback is set to NULL, then the standard f_op->open()
924 * filesystem callback is substituted.
926 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
927 int (*open)(struct inode *, struct file *))
929 if (IS_ERR(nd->intent.open.file))
933 nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->mnt),
934 nd->intent.open.flags - 1,
935 nd->intent.open.file,
938 return nd->intent.open.file;
940 release_open_intent(nd);
941 nd->intent.open.file = (struct file *)dentry;
944 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
947 * nameidata_to_filp - convert a nameidata to an open filp.
948 * @nd: pointer to nameidata
951 * Note that this function destroys the original nameidata
953 struct file *nameidata_to_filp(struct nameidata *nd, int flags)
957 /* Pick up the filp from the open intent */
958 filp = nd->intent.open.file;
959 /* Has the filesystem initialised the file for us? */
960 if (filp->f_dentry == NULL)
961 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);
968 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
971 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
977 f = get_empty_filp();
981 return ERR_PTR(error);
984 return __dentry_open(dentry, mnt, flags, f, NULL);
986 EXPORT_SYMBOL(dentry_open);
989 * Find an empty file descriptor entry, and mark it busy.
991 int get_unused_fd(void)
993 struct files_struct * files = current->files;
998 spin_lock(&files->file_lock);
1001 fdt = files_fdtable(files);
1002 fd = find_next_zero_bit(fdt->open_fds->fds_bits,
1007 * N.B. For clone tasks sharing a files structure, this test
1008 * will limit the total number of files that can be opened.
1010 if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
1013 /* Do we need to expand the fd array or fd set? */
1014 error = expand_files(files, fd);
1020 * If we needed to expand the fs array we
1021 * might have blocked - try again.
1027 FD_SET(fd, fdt->open_fds);
1028 FD_CLR(fd, fdt->close_on_exec);
1029 files->next_fd = fd + 1;
1033 if (fdt->fd[fd] != NULL) {
1034 printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
1041 spin_unlock(&files->file_lock);
1045 EXPORT_SYMBOL(get_unused_fd);
1047 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
1049 struct fdtable *fdt = files_fdtable(files);
1050 __FD_CLR(fd, fdt->open_fds);
1051 if (fd < files->next_fd)
1052 files->next_fd = fd;
1056 void fastcall put_unused_fd(unsigned int fd)
1058 struct files_struct *files = current->files;
1059 spin_lock(&files->file_lock);
1060 __put_unused_fd(files, fd);
1061 spin_unlock(&files->file_lock);
1064 EXPORT_SYMBOL(put_unused_fd);
1067 * Install a file pointer in the fd array.
1069 * The VFS is full of places where we drop the files lock between
1070 * setting the open_fds bitmap and installing the file in the file
1071 * array. At any such point, we are vulnerable to a dup2() race
1072 * installing a file in the array before us. We need to detect this and
1073 * fput() the struct file we are about to overwrite in this case.
1075 * It should never happen - if we allow dup2() do it, _really_ bad things
1079 void fastcall fd_install(unsigned int fd, struct file * file)
1081 struct files_struct *files = current->files;
1082 struct fdtable *fdt;
1083 spin_lock(&files->file_lock);
1084 fdt = files_fdtable(files);
1085 BUG_ON(fdt->fd[fd] != NULL);
1086 rcu_assign_pointer(fdt->fd[fd], file);
1087 spin_unlock(&files->file_lock);
1090 EXPORT_SYMBOL(fd_install);
1092 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
1094 char *tmp = getname(filename);
1095 int fd = PTR_ERR(tmp);
1098 fd = get_unused_fd();
1100 struct file *f = do_filp_open(dfd, tmp, flags, mode);
1105 fsnotify_open(f->f_dentry);
1114 asmlinkage long sys_open(const char __user *filename, int flags, int mode)
1118 if (force_o_largefile())
1119 flags |= O_LARGEFILE;
1121 ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1122 /* avoid REGPARM breakage on x86: */
1123 prevent_tail_call(ret);
1127 asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
1132 if (force_o_largefile())
1133 flags |= O_LARGEFILE;
1135 ret = do_sys_open(dfd, filename, flags, mode);
1136 /* avoid REGPARM breakage on x86: */
1137 prevent_tail_call(ret);
1144 * For backward compatibility? Maybe this should be moved
1145 * into arch/i386 instead?
1147 asmlinkage long sys_creat(const char __user * pathname, int mode)
1149 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1155 * "id" is the POSIX thread ID. We use the
1156 * files pointer for this..
1158 int filp_close(struct file *filp, fl_owner_t id)
1162 if (!file_count(filp)) {
1163 printk(KERN_ERR "VFS: Close: file count is 0\n");
1167 if (filp->f_op && filp->f_op->flush)
1168 retval = filp->f_op->flush(filp);
1170 dnotify_flush(filp, id);
1171 locks_remove_posix(filp, id);
1176 EXPORT_SYMBOL(filp_close);
1179 * Careful here! We test whether the file pointer is NULL before
1180 * releasing the fd. This ensures that one clone task can't release
1181 * an fd while another clone is opening it.
1183 asmlinkage long sys_close(unsigned int fd)
1186 struct files_struct *files = current->files;
1187 struct fdtable *fdt;
1189 spin_lock(&files->file_lock);
1190 fdt = files_fdtable(files);
1191 if (fd >= fdt->max_fds)
1196 rcu_assign_pointer(fdt->fd[fd], NULL);
1197 FD_CLR(fd, fdt->close_on_exec);
1198 __put_unused_fd(files, fd);
1199 spin_unlock(&files->file_lock);
1200 return filp_close(filp, files);
1203 spin_unlock(&files->file_lock);
1207 EXPORT_SYMBOL(sys_close);
1210 * This routine simulates a hangup on the tty, to arrange that users
1211 * are given clean terminals at login time.
1213 asmlinkage long sys_vhangup(void)
1215 if (capable(CAP_SYS_TTY_CONFIG)) {
1216 tty_vhangup(current->signal->tty);
1223 * Called when an inode is about to be open.
1224 * We use this to disallow opening large files on 32bit systems if
1225 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1226 * on this flag in sys_open.
1228 int generic_file_open(struct inode * inode, struct file * filp)
1230 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1235 EXPORT_SYMBOL(generic_file_open);
1238 * This is used by subsystems that don't want seekable
1241 int nonseekable_open(struct inode *inode, struct file *filp)
1243 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1247 EXPORT_SYMBOL(nonseekable_open);