4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/string.h>
9 #include <linux/file.h>
10 #include <linux/fdtable.h>
11 #include <linux/quotaops.h>
12 #include <linux/fsnotify.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/tty.h>
16 #include <linux/namei.h>
17 #include <linux/backing-dev.h>
18 #include <linux/capability.h>
19 #include <linux/security.h>
20 #include <linux/mount.h>
21 #include <linux/vfs.h>
22 #include <linux/fcntl.h>
23 #include <asm/uaccess.h>
25 #include <linux/personality.h>
26 #include <linux/pagemap.h>
27 #include <linux/syscalls.h>
28 #include <linux/rcupdate.h>
29 #include <linux/audit.h>
30 #include <linux/falloc.h>
32 int vfs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
38 if (dentry
->d_sb
->s_op
->statfs
) {
39 memset(buf
, 0, sizeof(*buf
));
40 retval
= security_sb_statfs(dentry
);
43 retval
= dentry
->d_sb
->s_op
->statfs(dentry
, buf
);
44 if (retval
== 0 && buf
->f_frsize
== 0)
45 buf
->f_frsize
= buf
->f_bsize
;
51 EXPORT_SYMBOL(vfs_statfs
);
53 static int vfs_statfs_native(struct dentry
*dentry
, struct statfs
*buf
)
58 retval
= vfs_statfs(dentry
, &st
);
62 if (sizeof(*buf
) == sizeof(st
))
63 memcpy(buf
, &st
, sizeof(st
));
65 if (sizeof buf
->f_blocks
== 4) {
66 if ((st
.f_blocks
| st
.f_bfree
| st
.f_bavail
) &
67 0xffffffff00000000ULL
)
70 * f_files and f_ffree may be -1; it's okay to stuff
73 if (st
.f_files
!= -1 &&
74 (st
.f_files
& 0xffffffff00000000ULL
))
76 if (st
.f_ffree
!= -1 &&
77 (st
.f_ffree
& 0xffffffff00000000ULL
))
81 buf
->f_type
= st
.f_type
;
82 buf
->f_bsize
= st
.f_bsize
;
83 buf
->f_blocks
= st
.f_blocks
;
84 buf
->f_bfree
= st
.f_bfree
;
85 buf
->f_bavail
= st
.f_bavail
;
86 buf
->f_files
= st
.f_files
;
87 buf
->f_ffree
= st
.f_ffree
;
88 buf
->f_fsid
= st
.f_fsid
;
89 buf
->f_namelen
= st
.f_namelen
;
90 buf
->f_frsize
= st
.f_frsize
;
91 memset(buf
->f_spare
, 0, sizeof(buf
->f_spare
));
96 static int vfs_statfs64(struct dentry
*dentry
, struct statfs64
*buf
)
101 retval
= vfs_statfs(dentry
, &st
);
105 if (sizeof(*buf
) == sizeof(st
))
106 memcpy(buf
, &st
, sizeof(st
));
108 buf
->f_type
= st
.f_type
;
109 buf
->f_bsize
= st
.f_bsize
;
110 buf
->f_blocks
= st
.f_blocks
;
111 buf
->f_bfree
= st
.f_bfree
;
112 buf
->f_bavail
= st
.f_bavail
;
113 buf
->f_files
= st
.f_files
;
114 buf
->f_ffree
= st
.f_ffree
;
115 buf
->f_fsid
= st
.f_fsid
;
116 buf
->f_namelen
= st
.f_namelen
;
117 buf
->f_frsize
= st
.f_frsize
;
118 memset(buf
->f_spare
, 0, sizeof(buf
->f_spare
));
123 asmlinkage
long sys_statfs(const char __user
* path
, struct statfs __user
* buf
)
128 error
= user_path_walk(path
, &nd
);
131 error
= vfs_statfs_native(nd
.path
.dentry
, &tmp
);
132 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
140 asmlinkage
long sys_statfs64(const char __user
*path
, size_t sz
, struct statfs64 __user
*buf
)
145 if (sz
!= sizeof(*buf
))
147 error
= user_path_walk(path
, &nd
);
150 error
= vfs_statfs64(nd
.path
.dentry
, &tmp
);
151 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
159 asmlinkage
long sys_fstatfs(unsigned int fd
, struct statfs __user
* buf
)
169 error
= vfs_statfs_native(file
->f_path
.dentry
, &tmp
);
170 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
177 asmlinkage
long sys_fstatfs64(unsigned int fd
, size_t sz
, struct statfs64 __user
*buf
)
183 if (sz
!= sizeof(*buf
))
190 error
= vfs_statfs64(file
->f_path
.dentry
, &tmp
);
191 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
198 int do_truncate(struct dentry
*dentry
, loff_t length
, unsigned int time_attrs
,
202 struct iattr newattrs
;
204 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
208 newattrs
.ia_size
= length
;
209 newattrs
.ia_valid
= ATTR_SIZE
| time_attrs
;
211 newattrs
.ia_file
= filp
;
212 newattrs
.ia_valid
|= ATTR_FILE
;
215 /* Remove suid/sgid on truncate too */
216 newattrs
.ia_valid
|= should_remove_suid(dentry
);
218 mutex_lock(&dentry
->d_inode
->i_mutex
);
219 err
= notify_change(dentry
, &newattrs
);
220 mutex_unlock(&dentry
->d_inode
->i_mutex
);
224 static long do_sys_truncate(const char __user
* path
, loff_t length
)
227 struct inode
* inode
;
231 if (length
< 0) /* sorry, but loff_t says... */
234 error
= user_path_walk(path
, &nd
);
237 inode
= nd
.path
.dentry
->d_inode
;
239 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
241 if (S_ISDIR(inode
->i_mode
))
245 if (!S_ISREG(inode
->i_mode
))
248 error
= mnt_want_write(nd
.path
.mnt
);
252 error
= vfs_permission(&nd
, MAY_WRITE
);
254 goto mnt_drop_write_and_out
;
257 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
258 goto mnt_drop_write_and_out
;
260 error
= get_write_access(inode
);
262 goto mnt_drop_write_and_out
;
265 * Make sure that there are no leases. get_write_access() protects
266 * against the truncate racing with a lease-granting setlease().
268 error
= break_lease(inode
, FMODE_WRITE
);
270 goto put_write_and_out
;
272 error
= locks_verify_truncate(inode
, NULL
, length
);
275 error
= do_truncate(nd
.path
.dentry
, length
, 0, NULL
);
279 put_write_access(inode
);
280 mnt_drop_write_and_out
:
281 mnt_drop_write(nd
.path
.mnt
);
288 asmlinkage
long sys_truncate(const char __user
* path
, unsigned long length
)
290 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
291 return do_sys_truncate(path
, (long)length
);
294 static long do_sys_ftruncate(unsigned int fd
, loff_t length
, int small
)
296 struct inode
* inode
;
297 struct dentry
*dentry
;
309 /* explicitly opened as large or we are on 64-bit box */
310 if (file
->f_flags
& O_LARGEFILE
)
313 dentry
= file
->f_path
.dentry
;
314 inode
= dentry
->d_inode
;
316 if (!S_ISREG(inode
->i_mode
) || !(file
->f_mode
& FMODE_WRITE
))
320 /* Cannot ftruncate over 2^31 bytes without large file support */
321 if (small
&& length
> MAX_NON_LFS
)
325 if (IS_APPEND(inode
))
328 error
= locks_verify_truncate(inode
, file
, length
);
330 error
= do_truncate(dentry
, length
, ATTR_MTIME
|ATTR_CTIME
, file
);
337 asmlinkage
long sys_ftruncate(unsigned int fd
, unsigned long length
)
339 long ret
= do_sys_ftruncate(fd
, length
, 1);
340 /* avoid REGPARM breakage on x86: */
341 asmlinkage_protect(2, ret
, fd
, length
);
345 /* LFS versions of truncate are only needed on 32 bit machines */
346 #if BITS_PER_LONG == 32
347 asmlinkage
long sys_truncate64(const char __user
* path
, loff_t length
)
349 return do_sys_truncate(path
, length
);
352 asmlinkage
long sys_ftruncate64(unsigned int fd
, loff_t length
)
354 long ret
= do_sys_ftruncate(fd
, length
, 0);
355 /* avoid REGPARM breakage on x86: */
356 asmlinkage_protect(2, ret
, fd
, length
);
361 asmlinkage
long sys_fallocate(int fd
, int mode
, loff_t offset
, loff_t len
)
367 if (offset
< 0 || len
<= 0)
370 /* Return error if mode is not supported */
372 if (mode
&& !(mode
& FALLOC_FL_KEEP_SIZE
))
379 if (!(file
->f_mode
& FMODE_WRITE
))
382 * Revalidate the write permissions, in case security policy has
383 * changed since the files were opened.
385 ret
= security_file_permission(file
, MAY_WRITE
);
389 inode
= file
->f_path
.dentry
->d_inode
;
392 if (S_ISFIFO(inode
->i_mode
))
397 * Let individual file system decide if it supports preallocation
398 * for directories or not.
400 if (!S_ISREG(inode
->i_mode
) && !S_ISDIR(inode
->i_mode
))
404 /* Check for wrap through zero too */
405 if (((offset
+ len
) > inode
->i_sb
->s_maxbytes
) || ((offset
+ len
) < 0))
408 if (inode
->i_op
&& inode
->i_op
->fallocate
)
409 ret
= inode
->i_op
->fallocate(inode
, mode
, offset
, len
);
420 * access() needs to use the real uid/gid, not the effective uid/gid.
421 * We do this by temporarily clearing all FS-related capabilities and
422 * switching the fsuid/fsgid around to the real ones.
424 asmlinkage
long sys_faccessat(int dfd
, const char __user
*filename
, int mode
)
427 int old_fsuid
, old_fsgid
;
428 kernel_cap_t old_cap
;
431 if (mode
& ~S_IRWXO
) /* where's F_OK, X_OK, W_OK, R_OK? */
434 old_fsuid
= current
->fsuid
;
435 old_fsgid
= current
->fsgid
;
436 old_cap
= current
->cap_effective
;
438 current
->fsuid
= current
->uid
;
439 current
->fsgid
= current
->gid
;
442 * Clear the capabilities if we switch to a non-root user
444 * FIXME: There is a race here against sys_capset. The
445 * capabilities can change yet we will restore the old
446 * value below. We should hold task_capabilities_lock,
447 * but we cannot because user_path_walk can sleep.
450 cap_clear(current
->cap_effective
);
452 current
->cap_effective
= current
->cap_permitted
;
454 res
= __user_walk_fd(dfd
, filename
, LOOKUP_FOLLOW
|LOOKUP_ACCESS
, &nd
);
458 res
= vfs_permission(&nd
, mode
);
459 /* SuS v2 requires we report a read only fs too */
460 if(res
|| !(mode
& S_IWOTH
) ||
461 special_file(nd
.path
.dentry
->d_inode
->i_mode
))
462 goto out_path_release
;
464 * This is a rare case where using __mnt_is_readonly()
465 * is OK without a mnt_want/drop_write() pair. Since
466 * no actual write to the fs is performed here, we do
467 * not need to telegraph to that to anyone.
469 * By doing this, we accept that this access is
470 * inherently racy and know that the fs may change
471 * state before we even see this result.
473 if (__mnt_is_readonly(nd
.path
.mnt
))
479 current
->fsuid
= old_fsuid
;
480 current
->fsgid
= old_fsgid
;
481 current
->cap_effective
= old_cap
;
486 asmlinkage
long sys_access(const char __user
*filename
, int mode
)
488 return sys_faccessat(AT_FDCWD
, filename
, mode
);
491 asmlinkage
long sys_chdir(const char __user
* filename
)
496 error
= __user_walk(filename
,
497 LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
|LOOKUP_CHDIR
, &nd
);
501 error
= vfs_permission(&nd
, MAY_EXEC
);
505 set_fs_pwd(current
->fs
, &nd
.path
);
513 asmlinkage
long sys_fchdir(unsigned int fd
)
524 inode
= file
->f_path
.dentry
->d_inode
;
527 if (!S_ISDIR(inode
->i_mode
))
530 error
= file_permission(file
, MAY_EXEC
);
532 set_fs_pwd(current
->fs
, &file
->f_path
);
539 asmlinkage
long sys_chroot(const char __user
* filename
)
544 error
= __user_walk(filename
, LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
| LOOKUP_NOALT
, &nd
);
548 error
= vfs_permission(&nd
, MAY_EXEC
);
553 if (!capable(CAP_SYS_CHROOT
))
556 set_fs_root(current
->fs
, &nd
.path
);
565 asmlinkage
long sys_fchmod(unsigned int fd
, mode_t mode
)
567 struct inode
* inode
;
568 struct dentry
* dentry
;
571 struct iattr newattrs
;
577 dentry
= file
->f_path
.dentry
;
578 inode
= dentry
->d_inode
;
580 audit_inode(NULL
, dentry
);
582 err
= mnt_want_write(file
->f_path
.mnt
);
586 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
588 mutex_lock(&inode
->i_mutex
);
589 if (mode
== (mode_t
) -1)
590 mode
= inode
->i_mode
;
591 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
592 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
593 err
= notify_change(dentry
, &newattrs
);
594 mutex_unlock(&inode
->i_mutex
);
597 mnt_drop_write(file
->f_path
.mnt
);
604 asmlinkage
long sys_fchmodat(int dfd
, const char __user
*filename
,
608 struct inode
* inode
;
610 struct iattr newattrs
;
612 error
= __user_walk_fd(dfd
, filename
, LOOKUP_FOLLOW
, &nd
);
615 inode
= nd
.path
.dentry
->d_inode
;
617 error
= mnt_want_write(nd
.path
.mnt
);
622 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
625 mutex_lock(&inode
->i_mutex
);
626 if (mode
== (mode_t
) -1)
627 mode
= inode
->i_mode
;
628 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
629 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
630 error
= notify_change(nd
.path
.dentry
, &newattrs
);
631 mutex_unlock(&inode
->i_mutex
);
634 mnt_drop_write(nd
.path
.mnt
);
641 asmlinkage
long sys_chmod(const char __user
*filename
, mode_t mode
)
643 return sys_fchmodat(AT_FDCWD
, filename
, mode
);
646 static int chown_common(struct dentry
* dentry
, uid_t user
, gid_t group
)
648 struct inode
* inode
;
650 struct iattr newattrs
;
653 if (!(inode
= dentry
->d_inode
)) {
654 printk(KERN_ERR
"chown_common: NULL inode\n");
658 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
660 newattrs
.ia_valid
= ATTR_CTIME
;
661 if (user
!= (uid_t
) -1) {
662 newattrs
.ia_valid
|= ATTR_UID
;
663 newattrs
.ia_uid
= user
;
665 if (group
!= (gid_t
) -1) {
666 newattrs
.ia_valid
|= ATTR_GID
;
667 newattrs
.ia_gid
= group
;
669 if (!S_ISDIR(inode
->i_mode
))
671 ATTR_KILL_SUID
| ATTR_KILL_SGID
| ATTR_KILL_PRIV
;
672 mutex_lock(&inode
->i_mutex
);
673 error
= notify_change(dentry
, &newattrs
);
674 mutex_unlock(&inode
->i_mutex
);
679 asmlinkage
long sys_chown(const char __user
* filename
, uid_t user
, gid_t group
)
684 error
= user_path_walk(filename
, &nd
);
687 error
= mnt_want_write(nd
.path
.mnt
);
690 error
= chown_common(nd
.path
.dentry
, user
, group
);
691 mnt_drop_write(nd
.path
.mnt
);
698 asmlinkage
long sys_fchownat(int dfd
, const char __user
*filename
, uid_t user
,
699 gid_t group
, int flag
)
705 if ((flag
& ~AT_SYMLINK_NOFOLLOW
) != 0)
708 follow
= (flag
& AT_SYMLINK_NOFOLLOW
) ? 0 : LOOKUP_FOLLOW
;
709 error
= __user_walk_fd(dfd
, filename
, follow
, &nd
);
712 error
= mnt_want_write(nd
.path
.mnt
);
715 error
= chown_common(nd
.path
.dentry
, user
, group
);
716 mnt_drop_write(nd
.path
.mnt
);
723 asmlinkage
long sys_lchown(const char __user
* filename
, uid_t user
, gid_t group
)
728 error
= user_path_walk_link(filename
, &nd
);
731 error
= mnt_want_write(nd
.path
.mnt
);
734 error
= chown_common(nd
.path
.dentry
, user
, group
);
735 mnt_drop_write(nd
.path
.mnt
);
743 asmlinkage
long sys_fchown(unsigned int fd
, uid_t user
, gid_t group
)
747 struct dentry
* dentry
;
753 error
= mnt_want_write(file
->f_path
.mnt
);
756 dentry
= file
->f_path
.dentry
;
757 audit_inode(NULL
, dentry
);
758 error
= chown_common(dentry
, user
, group
);
759 mnt_drop_write(file
->f_path
.mnt
);
767 * You have to be very careful that these write
768 * counts get cleaned up in error cases and
769 * upon __fput(). This should probably never
770 * be called outside of __dentry_open().
772 static inline int __get_file_write_access(struct inode
*inode
,
773 struct vfsmount
*mnt
)
776 error
= get_write_access(inode
);
780 * Do not take mount writer counts on
781 * special files since no writes to
782 * the mount itself will occur.
784 if (!special_file(inode
->i_mode
)) {
786 * Balanced in __fput()
788 error
= mnt_want_write(mnt
);
790 put_write_access(inode
);
795 static struct file
*__dentry_open(struct dentry
*dentry
, struct vfsmount
*mnt
,
796 int flags
, struct file
*f
,
797 int (*open
)(struct inode
*, struct file
*))
803 f
->f_mode
= ((flags
+1) & O_ACCMODE
) | FMODE_LSEEK
|
804 FMODE_PREAD
| FMODE_PWRITE
;
805 inode
= dentry
->d_inode
;
806 if (f
->f_mode
& FMODE_WRITE
) {
807 error
= __get_file_write_access(inode
, mnt
);
810 if (!special_file(inode
->i_mode
))
814 f
->f_mapping
= inode
->i_mapping
;
815 f
->f_path
.dentry
= dentry
;
818 f
->f_op
= fops_get(inode
->i_fop
);
819 file_move(f
, &inode
->i_sb
->s_files
);
821 error
= security_dentry_open(f
);
825 if (!open
&& f
->f_op
)
826 open
= f
->f_op
->open
;
828 error
= open(inode
, f
);
833 f
->f_flags
&= ~(O_CREAT
| O_EXCL
| O_NOCTTY
| O_TRUNC
);
835 file_ra_state_init(&f
->f_ra
, f
->f_mapping
->host
->i_mapping
);
837 /* NB: we're sure to have correct a_ops only after f_op->open */
838 if (f
->f_flags
& O_DIRECT
) {
839 if (!f
->f_mapping
->a_ops
||
840 ((!f
->f_mapping
->a_ops
->direct_IO
) &&
841 (!f
->f_mapping
->a_ops
->get_xip_mem
))) {
843 f
= ERR_PTR(-EINVAL
);
851 if (f
->f_mode
& FMODE_WRITE
) {
852 put_write_access(inode
);
853 if (!special_file(inode
->i_mode
)) {
855 * We don't consider this a real
856 * mnt_want/drop_write() pair
857 * because it all happenend right
858 * here, so just reset the state.
865 f
->f_path
.dentry
= NULL
;
866 f
->f_path
.mnt
= NULL
;
871 return ERR_PTR(error
);
875 * lookup_instantiate_filp - instantiates the open intent filp
876 * @nd: pointer to nameidata
877 * @dentry: pointer to dentry
878 * @open: open callback
880 * Helper for filesystems that want to use lookup open intents and pass back
881 * a fully instantiated struct file to the caller.
882 * This function is meant to be called from within a filesystem's
884 * Beware of calling it for non-regular files! Those ->open methods might block
885 * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
886 * leading to a deadlock, as nobody can open that fifo anymore, because
887 * another process to open fifo will block on locked parent when doing lookup).
888 * Note that in case of error, nd->intent.open.file is destroyed, but the
889 * path information remains valid.
890 * If the open callback is set to NULL, then the standard f_op->open()
891 * filesystem callback is substituted.
893 struct file
*lookup_instantiate_filp(struct nameidata
*nd
, struct dentry
*dentry
,
894 int (*open
)(struct inode
*, struct file
*))
896 if (IS_ERR(nd
->intent
.open
.file
))
900 nd
->intent
.open
.file
= __dentry_open(dget(dentry
), mntget(nd
->path
.mnt
),
901 nd
->intent
.open
.flags
- 1,
902 nd
->intent
.open
.file
,
905 return nd
->intent
.open
.file
;
907 release_open_intent(nd
);
908 nd
->intent
.open
.file
= (struct file
*)dentry
;
911 EXPORT_SYMBOL_GPL(lookup_instantiate_filp
);
914 * nameidata_to_filp - convert a nameidata to an open filp.
915 * @nd: pointer to nameidata
918 * Note that this function destroys the original nameidata
920 struct file
*nameidata_to_filp(struct nameidata
*nd
, int flags
)
924 /* Pick up the filp from the open intent */
925 filp
= nd
->intent
.open
.file
;
926 /* Has the filesystem initialised the file for us? */
927 if (filp
->f_path
.dentry
== NULL
)
928 filp
= __dentry_open(nd
->path
.dentry
, nd
->path
.mnt
, flags
, filp
,
936 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
939 struct file
*dentry_open(struct dentry
*dentry
, struct vfsmount
*mnt
, int flags
)
945 * We must always pass in a valid mount pointer. Historically
946 * callers got away with not passing it, but we must enforce this at
947 * the earliest possible point now to avoid strange problems deep in the
951 printk(KERN_WARNING
"%s called with NULL vfsmount\n", __func__
);
953 return ERR_PTR(-EINVAL
);
957 f
= get_empty_filp();
961 return ERR_PTR(error
);
964 return __dentry_open(dentry
, mnt
, flags
, f
, NULL
);
966 EXPORT_SYMBOL(dentry_open
);
969 * Find an empty file descriptor entry, and mark it busy.
971 int get_unused_fd_flags(int flags
)
973 struct files_struct
* files
= current
->files
;
978 spin_lock(&files
->file_lock
);
981 fdt
= files_fdtable(files
);
982 fd
= find_next_zero_bit(fdt
->open_fds
->fds_bits
, fdt
->max_fds
,
986 * N.B. For clone tasks sharing a files structure, this test
987 * will limit the total number of files that can be opened.
989 if (fd
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
992 /* Do we need to expand the fd array or fd set? */
993 error
= expand_files(files
, fd
);
999 * If we needed to expand the fs array we
1000 * might have blocked - try again.
1006 FD_SET(fd
, fdt
->open_fds
);
1007 if (flags
& O_CLOEXEC
)
1008 FD_SET(fd
, fdt
->close_on_exec
);
1010 FD_CLR(fd
, fdt
->close_on_exec
);
1011 files
->next_fd
= fd
+ 1;
1014 if (fdt
->fd
[fd
] != NULL
) {
1015 printk(KERN_WARNING
"get_unused_fd: slot %d not NULL!\n", fd
);
1022 spin_unlock(&files
->file_lock
);
1026 int get_unused_fd(void)
1028 return get_unused_fd_flags(0);
1031 EXPORT_SYMBOL(get_unused_fd
);
1033 static void __put_unused_fd(struct files_struct
*files
, unsigned int fd
)
1035 struct fdtable
*fdt
= files_fdtable(files
);
1036 __FD_CLR(fd
, fdt
->open_fds
);
1037 if (fd
< files
->next_fd
)
1038 files
->next_fd
= fd
;
1041 void put_unused_fd(unsigned int fd
)
1043 struct files_struct
*files
= current
->files
;
1044 spin_lock(&files
->file_lock
);
1045 __put_unused_fd(files
, fd
);
1046 spin_unlock(&files
->file_lock
);
1049 EXPORT_SYMBOL(put_unused_fd
);
1052 * Install a file pointer in the fd array.
1054 * The VFS is full of places where we drop the files lock between
1055 * setting the open_fds bitmap and installing the file in the file
1056 * array. At any such point, we are vulnerable to a dup2() race
1057 * installing a file in the array before us. We need to detect this and
1058 * fput() the struct file we are about to overwrite in this case.
1060 * It should never happen - if we allow dup2() do it, _really_ bad things
1064 void fd_install(unsigned int fd
, struct file
*file
)
1066 struct files_struct
*files
= current
->files
;
1067 struct fdtable
*fdt
;
1068 spin_lock(&files
->file_lock
);
1069 fdt
= files_fdtable(files
);
1070 BUG_ON(fdt
->fd
[fd
] != NULL
);
1071 rcu_assign_pointer(fdt
->fd
[fd
], file
);
1072 spin_unlock(&files
->file_lock
);
1075 EXPORT_SYMBOL(fd_install
);
1077 long do_sys_open(int dfd
, const char __user
*filename
, int flags
, int mode
)
1079 char *tmp
= getname(filename
);
1080 int fd
= PTR_ERR(tmp
);
1083 fd
= get_unused_fd_flags(flags
);
1085 struct file
*f
= do_filp_open(dfd
, tmp
, flags
, mode
);
1090 fsnotify_open(f
->f_path
.dentry
);
1099 asmlinkage
long sys_open(const char __user
*filename
, int flags
, int mode
)
1103 if (force_o_largefile())
1104 flags
|= O_LARGEFILE
;
1106 ret
= do_sys_open(AT_FDCWD
, filename
, flags
, mode
);
1107 /* avoid REGPARM breakage on x86: */
1108 asmlinkage_protect(3, ret
, filename
, flags
, mode
);
1112 asmlinkage
long sys_openat(int dfd
, const char __user
*filename
, int flags
,
1117 if (force_o_largefile())
1118 flags
|= O_LARGEFILE
;
1120 ret
= do_sys_open(dfd
, filename
, flags
, mode
);
1121 /* avoid REGPARM breakage on x86: */
1122 asmlinkage_protect(4, ret
, dfd
, filename
, flags
, mode
);
1129 * For backward compatibility? Maybe this should be moved
1130 * into arch/i386 instead?
1132 asmlinkage
long sys_creat(const char __user
* pathname
, int mode
)
1134 return sys_open(pathname
, O_CREAT
| O_WRONLY
| O_TRUNC
, mode
);
1140 * "id" is the POSIX thread ID. We use the
1141 * files pointer for this..
1143 int filp_close(struct file
*filp
, fl_owner_t id
)
1147 if (!file_count(filp
)) {
1148 printk(KERN_ERR
"VFS: Close: file count is 0\n");
1152 if (filp
->f_op
&& filp
->f_op
->flush
)
1153 retval
= filp
->f_op
->flush(filp
, id
);
1155 dnotify_flush(filp
, id
);
1156 locks_remove_posix(filp
, id
);
1161 EXPORT_SYMBOL(filp_close
);
1164 * Careful here! We test whether the file pointer is NULL before
1165 * releasing the fd. This ensures that one clone task can't release
1166 * an fd while another clone is opening it.
1168 asmlinkage
long sys_close(unsigned int fd
)
1171 struct files_struct
*files
= current
->files
;
1172 struct fdtable
*fdt
;
1175 spin_lock(&files
->file_lock
);
1176 fdt
= files_fdtable(files
);
1177 if (fd
>= fdt
->max_fds
)
1182 rcu_assign_pointer(fdt
->fd
[fd
], NULL
);
1183 FD_CLR(fd
, fdt
->close_on_exec
);
1184 __put_unused_fd(files
, fd
);
1185 spin_unlock(&files
->file_lock
);
1186 retval
= filp_close(filp
, files
);
1188 /* can't restart close syscall because file table entry was cleared */
1189 if (unlikely(retval
== -ERESTARTSYS
||
1190 retval
== -ERESTARTNOINTR
||
1191 retval
== -ERESTARTNOHAND
||
1192 retval
== -ERESTART_RESTARTBLOCK
))
1198 spin_unlock(&files
->file_lock
);
1202 EXPORT_SYMBOL(sys_close
);
1205 * This routine simulates a hangup on the tty, to arrange that users
1206 * are given clean terminals at login time.
1208 asmlinkage
long sys_vhangup(void)
1210 if (capable(CAP_SYS_TTY_CONFIG
)) {
1211 /* XXX: this needs locking */
1212 tty_vhangup(current
->signal
->tty
);
1219 * Called when an inode is about to be open.
1220 * We use this to disallow opening large files on 32bit systems if
1221 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1222 * on this flag in sys_open.
1224 int generic_file_open(struct inode
* inode
, struct file
* filp
)
1226 if (!(filp
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
1231 EXPORT_SYMBOL(generic_file_open
);
1234 * This is used by subsystems that don't want seekable
1237 int nonseekable_open(struct inode
*inode
, struct file
*filp
)
1239 filp
->f_mode
&= ~(FMODE_LSEEK
| FMODE_PREAD
| FMODE_PWRITE
);
1243 EXPORT_SYMBOL(nonseekable_open
);