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 <asm/uaccess.h>
25 #include <linux/personality.h>
26 #include <linux/pagemap.h>
27 #include <linux/syscalls.h>
28 #include <linux/rcupdate.h>
30 #include <asm/unistd.h>
32 int vfs_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
38 if (sb
->s_op
->statfs
) {
39 memset(buf
, 0, sizeof(*buf
));
40 retval
= security_sb_statfs(sb
);
43 retval
= sb
->s_op
->statfs(sb
, 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 super_block
*sb
, struct statfs
*buf
)
58 retval
= vfs_statfs(sb
, &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 super_block
*sb
, struct statfs64
*buf
)
101 retval
= vfs_statfs(sb
, &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
.dentry
->d_inode
->i_sb
, &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
.dentry
->d_inode
->i_sb
, &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_dentry
->d_inode
->i_sb
, &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_dentry
->d_inode
->i_sb
, &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 mutex_lock(&dentry
->d_inode
->i_mutex
);
216 err
= notify_change(dentry
, &newattrs
);
217 mutex_unlock(&dentry
->d_inode
->i_mutex
);
221 static long do_sys_truncate(const char __user
* path
, loff_t length
)
224 struct inode
* inode
;
228 if (length
< 0) /* sorry, but loff_t says... */
231 error
= user_path_walk(path
, &nd
);
234 inode
= nd
.dentry
->d_inode
;
236 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
238 if (S_ISDIR(inode
->i_mode
))
242 if (!S_ISREG(inode
->i_mode
))
245 error
= vfs_permission(&nd
, MAY_WRITE
);
250 if (IS_RDONLY(inode
))
254 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
258 * Make sure that there are no leases.
260 error
= break_lease(inode
, FMODE_WRITE
);
264 error
= get_write_access(inode
);
268 error
= locks_verify_truncate(inode
, NULL
, length
);
271 error
= do_truncate(nd
.dentry
, length
, 0, NULL
);
273 put_write_access(inode
);
281 asmlinkage
long sys_truncate(const char __user
* path
, unsigned long length
)
283 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
284 return do_sys_truncate(path
, (long)length
);
287 static long do_sys_ftruncate(unsigned int fd
, loff_t length
, int small
)
289 struct inode
* inode
;
290 struct dentry
*dentry
;
302 /* explicitly opened as large or we are on 64-bit box */
303 if (file
->f_flags
& O_LARGEFILE
)
306 dentry
= file
->f_dentry
;
307 inode
= dentry
->d_inode
;
309 if (!S_ISREG(inode
->i_mode
) || !(file
->f_mode
& FMODE_WRITE
))
313 /* Cannot ftruncate over 2^31 bytes without large file support */
314 if (small
&& length
> MAX_NON_LFS
)
318 if (IS_APPEND(inode
))
321 error
= locks_verify_truncate(inode
, file
, length
);
323 error
= do_truncate(dentry
, length
, 0, file
);
330 asmlinkage
long sys_ftruncate(unsigned int fd
, unsigned long length
)
332 return do_sys_ftruncate(fd
, length
, 1);
335 /* LFS versions of truncate are only needed on 32 bit machines */
336 #if BITS_PER_LONG == 32
337 asmlinkage
long sys_truncate64(const char __user
* path
, loff_t length
)
339 return do_sys_truncate(path
, length
);
342 asmlinkage
long sys_ftruncate64(unsigned int fd
, loff_t length
)
344 return do_sys_ftruncate(fd
, length
, 0);
348 #ifdef __ARCH_WANT_SYS_UTIME
351 * sys_utime() can be implemented in user-level using sys_utimes().
352 * Is this for backwards compatibility? If so, why not move it
353 * into the appropriate arch directory (for those architectures that
357 /* If times==NULL, set access and modification to current time,
358 * must be owner or have write permission.
359 * Else, update from *times, must be owner or super user.
361 asmlinkage
long sys_utime(char __user
* filename
, struct utimbuf __user
* times
)
365 struct inode
* inode
;
366 struct iattr newattrs
;
368 error
= user_path_walk(filename
, &nd
);
371 inode
= nd
.dentry
->d_inode
;
374 if (IS_RDONLY(inode
))
377 /* Don't worry, the checks are done in inode_change_ok() */
378 newattrs
.ia_valid
= ATTR_CTIME
| ATTR_MTIME
| ATTR_ATIME
;
381 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
384 error
= get_user(newattrs
.ia_atime
.tv_sec
, ×
->actime
);
385 newattrs
.ia_atime
.tv_nsec
= 0;
387 error
= get_user(newattrs
.ia_mtime
.tv_sec
, ×
->modtime
);
388 newattrs
.ia_mtime
.tv_nsec
= 0;
392 newattrs
.ia_valid
|= ATTR_ATIME_SET
| ATTR_MTIME_SET
;
395 if (IS_IMMUTABLE(inode
))
398 if (current
->fsuid
!= inode
->i_uid
&&
399 (error
= vfs_permission(&nd
, MAY_WRITE
)) != 0)
402 mutex_lock(&inode
->i_mutex
);
403 error
= notify_change(nd
.dentry
, &newattrs
);
404 mutex_unlock(&inode
->i_mutex
);
413 /* If times==NULL, set access and modification to current time,
414 * must be owner or have write permission.
415 * Else, update from *times, must be owner or super user.
417 long do_utimes(char __user
* filename
, struct timeval
* times
)
421 struct inode
* inode
;
422 struct iattr newattrs
;
424 error
= user_path_walk(filename
, &nd
);
428 inode
= nd
.dentry
->d_inode
;
431 if (IS_RDONLY(inode
))
434 /* Don't worry, the checks are done in inode_change_ok() */
435 newattrs
.ia_valid
= ATTR_CTIME
| ATTR_MTIME
| ATTR_ATIME
;
438 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
441 newattrs
.ia_atime
.tv_sec
= times
[0].tv_sec
;
442 newattrs
.ia_atime
.tv_nsec
= times
[0].tv_usec
* 1000;
443 newattrs
.ia_mtime
.tv_sec
= times
[1].tv_sec
;
444 newattrs
.ia_mtime
.tv_nsec
= times
[1].tv_usec
* 1000;
445 newattrs
.ia_valid
|= ATTR_ATIME_SET
| ATTR_MTIME_SET
;
448 if (IS_IMMUTABLE(inode
))
451 if (current
->fsuid
!= inode
->i_uid
&&
452 (error
= vfs_permission(&nd
, MAY_WRITE
)) != 0)
455 mutex_lock(&inode
->i_mutex
);
456 error
= notify_change(nd
.dentry
, &newattrs
);
457 mutex_unlock(&inode
->i_mutex
);
464 asmlinkage
long sys_utimes(char __user
* filename
, struct timeval __user
* utimes
)
466 struct timeval times
[2];
468 if (utimes
&& copy_from_user(×
, utimes
, sizeof(times
)))
470 return do_utimes(filename
, utimes
? times
: NULL
);
475 * access() needs to use the real uid/gid, not the effective uid/gid.
476 * We do this by temporarily clearing all FS-related capabilities and
477 * switching the fsuid/fsgid around to the real ones.
479 asmlinkage
long sys_access(const char __user
* filename
, int mode
)
482 int old_fsuid
, old_fsgid
;
483 kernel_cap_t old_cap
;
486 if (mode
& ~S_IRWXO
) /* where's F_OK, X_OK, W_OK, R_OK? */
489 old_fsuid
= current
->fsuid
;
490 old_fsgid
= current
->fsgid
;
491 old_cap
= current
->cap_effective
;
493 current
->fsuid
= current
->uid
;
494 current
->fsgid
= current
->gid
;
497 * Clear the capabilities if we switch to a non-root user
499 * FIXME: There is a race here against sys_capset. The
500 * capabilities can change yet we will restore the old
501 * value below. We should hold task_capabilities_lock,
502 * but we cannot because user_path_walk can sleep.
505 cap_clear(current
->cap_effective
);
507 current
->cap_effective
= current
->cap_permitted
;
509 res
= __user_walk(filename
, LOOKUP_FOLLOW
|LOOKUP_ACCESS
, &nd
);
511 res
= vfs_permission(&nd
, mode
);
512 /* SuS v2 requires we report a read only fs too */
513 if(!res
&& (mode
& S_IWOTH
) && IS_RDONLY(nd
.dentry
->d_inode
)
514 && !special_file(nd
.dentry
->d_inode
->i_mode
))
519 current
->fsuid
= old_fsuid
;
520 current
->fsgid
= old_fsgid
;
521 current
->cap_effective
= old_cap
;
526 asmlinkage
long sys_chdir(const char __user
* filename
)
531 error
= __user_walk(filename
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
, &nd
);
535 error
= vfs_permission(&nd
, MAY_EXEC
);
539 set_fs_pwd(current
->fs
, nd
.mnt
, nd
.dentry
);
547 asmlinkage
long sys_fchdir(unsigned int fd
)
550 struct dentry
*dentry
;
552 struct vfsmount
*mnt
;
560 dentry
= file
->f_dentry
;
561 mnt
= file
->f_vfsmnt
;
562 inode
= dentry
->d_inode
;
565 if (!S_ISDIR(inode
->i_mode
))
568 error
= file_permission(file
, MAY_EXEC
);
570 set_fs_pwd(current
->fs
, mnt
, dentry
);
577 asmlinkage
long sys_chroot(const char __user
* filename
)
582 error
= __user_walk(filename
, LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
| LOOKUP_NOALT
, &nd
);
586 error
= vfs_permission(&nd
, MAY_EXEC
);
591 if (!capable(CAP_SYS_CHROOT
))
594 set_fs_root(current
->fs
, nd
.mnt
, nd
.dentry
);
603 asmlinkage
long sys_fchmod(unsigned int fd
, mode_t mode
)
605 struct inode
* inode
;
606 struct dentry
* dentry
;
609 struct iattr newattrs
;
615 dentry
= file
->f_dentry
;
616 inode
= dentry
->d_inode
;
619 if (IS_RDONLY(inode
))
622 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
624 mutex_lock(&inode
->i_mutex
);
625 if (mode
== (mode_t
) -1)
626 mode
= inode
->i_mode
;
627 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
628 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
629 err
= notify_change(dentry
, &newattrs
);
630 mutex_unlock(&inode
->i_mutex
);
638 asmlinkage
long sys_chmod(const char __user
* filename
, mode_t mode
)
641 struct inode
* inode
;
643 struct iattr newattrs
;
645 error
= user_path_walk(filename
, &nd
);
648 inode
= nd
.dentry
->d_inode
;
651 if (IS_RDONLY(inode
))
655 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
658 mutex_lock(&inode
->i_mutex
);
659 if (mode
== (mode_t
) -1)
660 mode
= inode
->i_mode
;
661 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
662 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
663 error
= notify_change(nd
.dentry
, &newattrs
);
664 mutex_unlock(&inode
->i_mutex
);
672 static int chown_common(struct dentry
* dentry
, uid_t user
, gid_t group
)
674 struct inode
* inode
;
676 struct iattr newattrs
;
679 if (!(inode
= dentry
->d_inode
)) {
680 printk(KERN_ERR
"chown_common: NULL inode\n");
684 if (IS_RDONLY(inode
))
687 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
689 newattrs
.ia_valid
= ATTR_CTIME
;
690 if (user
!= (uid_t
) -1) {
691 newattrs
.ia_valid
|= ATTR_UID
;
692 newattrs
.ia_uid
= user
;
694 if (group
!= (gid_t
) -1) {
695 newattrs
.ia_valid
|= ATTR_GID
;
696 newattrs
.ia_gid
= group
;
698 if (!S_ISDIR(inode
->i_mode
))
699 newattrs
.ia_valid
|= ATTR_KILL_SUID
|ATTR_KILL_SGID
;
700 mutex_lock(&inode
->i_mutex
);
701 error
= notify_change(dentry
, &newattrs
);
702 mutex_unlock(&inode
->i_mutex
);
707 asmlinkage
long sys_chown(const char __user
* filename
, uid_t user
, gid_t group
)
712 error
= user_path_walk(filename
, &nd
);
714 error
= chown_common(nd
.dentry
, user
, group
);
720 asmlinkage
long sys_lchown(const char __user
* filename
, uid_t user
, gid_t group
)
725 error
= user_path_walk_link(filename
, &nd
);
727 error
= chown_common(nd
.dentry
, user
, group
);
734 asmlinkage
long sys_fchown(unsigned int fd
, uid_t user
, gid_t group
)
741 error
= chown_common(file
->f_dentry
, user
, group
);
747 static struct file
*__dentry_open(struct dentry
*dentry
, struct vfsmount
*mnt
,
748 int flags
, struct file
*f
,
749 int (*open
)(struct inode
*, struct file
*))
755 f
->f_mode
= ((flags
+1) & O_ACCMODE
) | FMODE_LSEEK
|
756 FMODE_PREAD
| FMODE_PWRITE
;
757 inode
= dentry
->d_inode
;
758 if (f
->f_mode
& FMODE_WRITE
) {
759 error
= get_write_access(inode
);
764 f
->f_mapping
= inode
->i_mapping
;
765 f
->f_dentry
= dentry
;
768 f
->f_op
= fops_get(inode
->i_fop
);
769 file_move(f
, &inode
->i_sb
->s_files
);
771 if (!open
&& f
->f_op
)
772 open
= f
->f_op
->open
;
774 error
= open(inode
, f
);
779 f
->f_flags
&= ~(O_CREAT
| O_EXCL
| O_NOCTTY
| O_TRUNC
);
781 file_ra_state_init(&f
->f_ra
, f
->f_mapping
->host
->i_mapping
);
783 /* NB: we're sure to have correct a_ops only after f_op->open */
784 if (f
->f_flags
& O_DIRECT
) {
785 if (!f
->f_mapping
->a_ops
||
786 ((!f
->f_mapping
->a_ops
->direct_IO
) &&
787 (!f
->f_mapping
->a_ops
->get_xip_page
))) {
789 f
= ERR_PTR(-EINVAL
);
797 if (f
->f_mode
& FMODE_WRITE
)
798 put_write_access(inode
);
806 return ERR_PTR(error
);
810 * Note that while the flag value (low two bits) for sys_open means:
816 * 00 - no permissions needed
817 * 01 - read-permission
818 * 10 - write-permission
820 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
823 struct file
*filp_open(const char * filename
, int flags
, int mode
)
825 int namei_flags
, error
;
829 if ((namei_flags
+1) & O_ACCMODE
)
832 error
= open_namei(filename
, namei_flags
, mode
, &nd
);
834 return nameidata_to_filp(&nd
, flags
);
836 return ERR_PTR(error
);
838 EXPORT_SYMBOL(filp_open
);
841 * lookup_instantiate_filp - instantiates the open intent filp
842 * @nd: pointer to nameidata
843 * @dentry: pointer to dentry
844 * @open: open callback
846 * Helper for filesystems that want to use lookup open intents and pass back
847 * a fully instantiated struct file to the caller.
848 * This function is meant to be called from within a filesystem's
850 * Note that in case of error, nd->intent.open.file is destroyed, but the
851 * path information remains valid.
852 * If the open callback is set to NULL, then the standard f_op->open()
853 * filesystem callback is substituted.
855 struct file
*lookup_instantiate_filp(struct nameidata
*nd
, struct dentry
*dentry
,
856 int (*open
)(struct inode
*, struct file
*))
858 if (IS_ERR(nd
->intent
.open
.file
))
862 nd
->intent
.open
.file
= __dentry_open(dget(dentry
), mntget(nd
->mnt
),
863 nd
->intent
.open
.flags
- 1,
864 nd
->intent
.open
.file
,
867 return nd
->intent
.open
.file
;
869 release_open_intent(nd
);
870 nd
->intent
.open
.file
= (struct file
*)dentry
;
873 EXPORT_SYMBOL_GPL(lookup_instantiate_filp
);
876 * nameidata_to_filp - convert a nameidata to an open filp.
877 * @nd: pointer to nameidata
880 * Note that this function destroys the original nameidata
882 struct file
*nameidata_to_filp(struct nameidata
*nd
, int flags
)
886 /* Pick up the filp from the open intent */
887 filp
= nd
->intent
.open
.file
;
888 /* Has the filesystem initialised the file for us? */
889 if (filp
->f_dentry
== NULL
)
890 filp
= __dentry_open(nd
->dentry
, nd
->mnt
, flags
, filp
, NULL
);
897 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
900 struct file
*dentry_open(struct dentry
*dentry
, struct vfsmount
*mnt
, int flags
)
906 f
= get_empty_filp();
910 return ERR_PTR(error
);
913 return __dentry_open(dentry
, mnt
, flags
, f
, NULL
);
915 EXPORT_SYMBOL(dentry_open
);
918 * Find an empty file descriptor entry, and mark it busy.
920 int get_unused_fd(void)
922 struct files_struct
* files
= current
->files
;
927 spin_lock(&files
->file_lock
);
930 fdt
= files_fdtable(files
);
931 fd
= find_next_zero_bit(fdt
->open_fds
->fds_bits
,
936 * N.B. For clone tasks sharing a files structure, this test
937 * will limit the total number of files that can be opened.
939 if (fd
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
942 /* Do we need to expand the fd array or fd set? */
943 error
= expand_files(files
, fd
);
949 * If we needed to expand the fs array we
950 * might have blocked - try again.
956 FD_SET(fd
, fdt
->open_fds
);
957 FD_CLR(fd
, fdt
->close_on_exec
);
958 fdt
->next_fd
= fd
+ 1;
961 if (fdt
->fd
[fd
] != NULL
) {
962 printk(KERN_WARNING
"get_unused_fd: slot %d not NULL!\n", fd
);
969 spin_unlock(&files
->file_lock
);
973 EXPORT_SYMBOL(get_unused_fd
);
975 static void __put_unused_fd(struct files_struct
*files
, unsigned int fd
)
977 struct fdtable
*fdt
= files_fdtable(files
);
978 __FD_CLR(fd
, fdt
->open_fds
);
979 if (fd
< fdt
->next_fd
)
983 void fastcall
put_unused_fd(unsigned int fd
)
985 struct files_struct
*files
= current
->files
;
986 spin_lock(&files
->file_lock
);
987 __put_unused_fd(files
, fd
);
988 spin_unlock(&files
->file_lock
);
991 EXPORT_SYMBOL(put_unused_fd
);
994 * Install a file pointer in the fd array.
996 * The VFS is full of places where we drop the files lock between
997 * setting the open_fds bitmap and installing the file in the file
998 * array. At any such point, we are vulnerable to a dup2() race
999 * installing a file in the array before us. We need to detect this and
1000 * fput() the struct file we are about to overwrite in this case.
1002 * It should never happen - if we allow dup2() do it, _really_ bad things
1006 void fastcall
fd_install(unsigned int fd
, struct file
* file
)
1008 struct files_struct
*files
= current
->files
;
1009 struct fdtable
*fdt
;
1010 spin_lock(&files
->file_lock
);
1011 fdt
= files_fdtable(files
);
1012 BUG_ON(fdt
->fd
[fd
] != NULL
);
1013 rcu_assign_pointer(fdt
->fd
[fd
], file
);
1014 spin_unlock(&files
->file_lock
);
1017 EXPORT_SYMBOL(fd_install
);
1019 long do_sys_open(const char __user
*filename
, int flags
, int mode
)
1021 char *tmp
= getname(filename
);
1022 int fd
= PTR_ERR(tmp
);
1025 fd
= get_unused_fd();
1027 struct file
*f
= filp_open(tmp
, flags
, mode
);
1032 fsnotify_open(f
->f_dentry
);
1041 asmlinkage
long sys_open(const char __user
*filename
, int flags
, int mode
)
1043 if (force_o_largefile())
1044 flags
|= O_LARGEFILE
;
1046 return do_sys_open(filename
, flags
, mode
);
1048 EXPORT_SYMBOL_GPL(sys_open
);
1053 * For backward compatibility? Maybe this should be moved
1054 * into arch/i386 instead?
1056 asmlinkage
long sys_creat(const char __user
* pathname
, int mode
)
1058 return sys_open(pathname
, O_CREAT
| O_WRONLY
| O_TRUNC
, mode
);
1064 * "id" is the POSIX thread ID. We use the
1065 * files pointer for this..
1067 int filp_close(struct file
*filp
, fl_owner_t id
)
1071 if (!file_count(filp
)) {
1072 printk(KERN_ERR
"VFS: Close: file count is 0\n");
1076 if (filp
->f_op
&& filp
->f_op
->flush
)
1077 retval
= filp
->f_op
->flush(filp
);
1079 dnotify_flush(filp
, id
);
1080 locks_remove_posix(filp
, id
);
1085 EXPORT_SYMBOL(filp_close
);
1088 * Careful here! We test whether the file pointer is NULL before
1089 * releasing the fd. This ensures that one clone task can't release
1090 * an fd while another clone is opening it.
1092 asmlinkage
long sys_close(unsigned int fd
)
1095 struct files_struct
*files
= current
->files
;
1096 struct fdtable
*fdt
;
1098 spin_lock(&files
->file_lock
);
1099 fdt
= files_fdtable(files
);
1100 if (fd
>= fdt
->max_fds
)
1105 rcu_assign_pointer(fdt
->fd
[fd
], NULL
);
1106 FD_CLR(fd
, fdt
->close_on_exec
);
1107 __put_unused_fd(files
, fd
);
1108 spin_unlock(&files
->file_lock
);
1109 return filp_close(filp
, files
);
1112 spin_unlock(&files
->file_lock
);
1116 EXPORT_SYMBOL(sys_close
);
1119 * This routine simulates a hangup on the tty, to arrange that users
1120 * are given clean terminals at login time.
1122 asmlinkage
long sys_vhangup(void)
1124 if (capable(CAP_SYS_TTY_CONFIG
)) {
1125 tty_vhangup(current
->signal
->tty
);
1132 * Called when an inode is about to be open.
1133 * We use this to disallow opening large files on 32bit systems if
1134 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1135 * on this flag in sys_open.
1137 int generic_file_open(struct inode
* inode
, struct file
* filp
)
1139 if (!(filp
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
1144 EXPORT_SYMBOL(generic_file_open
);
1147 * This is used by subsystems that don't want seekable
1150 int nonseekable_open(struct inode
*inode
, struct file
*filp
)
1152 filp
->f_mode
&= ~(FMODE_LSEEK
| FMODE_PREAD
| FMODE_PWRITE
);
1156 EXPORT_SYMBOL(nonseekable_open
);