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/security.h>
20 #include <linux/mount.h>
21 #include <linux/vfs.h>
22 #include <asm/uaccess.h>
24 #include <linux/personality.h>
25 #include <linux/pagemap.h>
26 #include <linux/syscalls.h>
27 #include <linux/rcupdate.h>
29 #include <asm/unistd.h>
31 int vfs_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
37 if (sb
->s_op
->statfs
) {
38 memset(buf
, 0, sizeof(*buf
));
39 retval
= security_sb_statfs(sb
);
42 retval
= sb
->s_op
->statfs(sb
, buf
);
43 if (retval
== 0 && buf
->f_frsize
== 0)
44 buf
->f_frsize
= buf
->f_bsize
;
50 EXPORT_SYMBOL(vfs_statfs
);
52 static int vfs_statfs_native(struct super_block
*sb
, struct statfs
*buf
)
57 retval
= vfs_statfs(sb
, &st
);
61 if (sizeof(*buf
) == sizeof(st
))
62 memcpy(buf
, &st
, sizeof(st
));
64 if (sizeof buf
->f_blocks
== 4) {
65 if ((st
.f_blocks
| st
.f_bfree
| st
.f_bavail
) &
66 0xffffffff00000000ULL
)
69 * f_files and f_ffree may be -1; it's okay to stuff
72 if (st
.f_files
!= -1 &&
73 (st
.f_files
& 0xffffffff00000000ULL
))
75 if (st
.f_ffree
!= -1 &&
76 (st
.f_ffree
& 0xffffffff00000000ULL
))
80 buf
->f_type
= st
.f_type
;
81 buf
->f_bsize
= st
.f_bsize
;
82 buf
->f_blocks
= st
.f_blocks
;
83 buf
->f_bfree
= st
.f_bfree
;
84 buf
->f_bavail
= st
.f_bavail
;
85 buf
->f_files
= st
.f_files
;
86 buf
->f_ffree
= st
.f_ffree
;
87 buf
->f_fsid
= st
.f_fsid
;
88 buf
->f_namelen
= st
.f_namelen
;
89 buf
->f_frsize
= st
.f_frsize
;
90 memset(buf
->f_spare
, 0, sizeof(buf
->f_spare
));
95 static int vfs_statfs64(struct super_block
*sb
, struct statfs64
*buf
)
100 retval
= vfs_statfs(sb
, &st
);
104 if (sizeof(*buf
) == sizeof(st
))
105 memcpy(buf
, &st
, sizeof(st
));
107 buf
->f_type
= st
.f_type
;
108 buf
->f_bsize
= st
.f_bsize
;
109 buf
->f_blocks
= st
.f_blocks
;
110 buf
->f_bfree
= st
.f_bfree
;
111 buf
->f_bavail
= st
.f_bavail
;
112 buf
->f_files
= st
.f_files
;
113 buf
->f_ffree
= st
.f_ffree
;
114 buf
->f_fsid
= st
.f_fsid
;
115 buf
->f_namelen
= st
.f_namelen
;
116 buf
->f_frsize
= st
.f_frsize
;
117 memset(buf
->f_spare
, 0, sizeof(buf
->f_spare
));
122 asmlinkage
long sys_statfs(const char __user
* path
, struct statfs __user
* buf
)
127 error
= user_path_walk(path
, &nd
);
130 error
= vfs_statfs_native(nd
.dentry
->d_inode
->i_sb
, &tmp
);
131 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
139 asmlinkage
long sys_statfs64(const char __user
*path
, size_t sz
, struct statfs64 __user
*buf
)
144 if (sz
!= sizeof(*buf
))
146 error
= user_path_walk(path
, &nd
);
149 error
= vfs_statfs64(nd
.dentry
->d_inode
->i_sb
, &tmp
);
150 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
158 asmlinkage
long sys_fstatfs(unsigned int fd
, struct statfs __user
* buf
)
168 error
= vfs_statfs_native(file
->f_dentry
->d_inode
->i_sb
, &tmp
);
169 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
176 asmlinkage
long sys_fstatfs64(unsigned int fd
, size_t sz
, struct statfs64 __user
*buf
)
182 if (sz
!= sizeof(*buf
))
189 error
= vfs_statfs64(file
->f_dentry
->d_inode
->i_sb
, &tmp
);
190 if (!error
&& copy_to_user(buf
, &tmp
, sizeof(tmp
)))
197 int do_truncate(struct dentry
*dentry
, loff_t length
, unsigned int time_attrs
,
201 struct iattr newattrs
;
203 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
207 newattrs
.ia_size
= length
;
208 newattrs
.ia_valid
= ATTR_SIZE
| time_attrs
;
210 newattrs
.ia_file
= filp
;
211 newattrs
.ia_valid
|= ATTR_FILE
;
214 down(&dentry
->d_inode
->i_sem
);
215 err
= notify_change(dentry
, &newattrs
);
216 up(&dentry
->d_inode
->i_sem
);
220 static long do_sys_truncate(const char __user
* path
, loff_t length
)
223 struct inode
* inode
;
227 if (length
< 0) /* sorry, but loff_t says... */
230 error
= user_path_walk(path
, &nd
);
233 inode
= nd
.dentry
->d_inode
;
235 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
237 if (S_ISDIR(inode
->i_mode
))
241 if (!S_ISREG(inode
->i_mode
))
244 error
= vfs_permission(&nd
, MAY_WRITE
);
249 if (IS_RDONLY(inode
))
253 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
257 * Make sure that there are no leases.
259 error
= break_lease(inode
, FMODE_WRITE
);
263 error
= get_write_access(inode
);
267 error
= locks_verify_truncate(inode
, NULL
, length
);
270 error
= do_truncate(nd
.dentry
, length
, 0, NULL
);
272 put_write_access(inode
);
280 asmlinkage
long sys_truncate(const char __user
* path
, unsigned long length
)
282 /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
283 return do_sys_truncate(path
, (long)length
);
286 static long do_sys_ftruncate(unsigned int fd
, loff_t length
, int small
)
288 struct inode
* inode
;
289 struct dentry
*dentry
;
301 /* explicitly opened as large or we are on 64-bit box */
302 if (file
->f_flags
& O_LARGEFILE
)
305 dentry
= file
->f_dentry
;
306 inode
= dentry
->d_inode
;
308 if (!S_ISREG(inode
->i_mode
) || !(file
->f_mode
& FMODE_WRITE
))
312 /* Cannot ftruncate over 2^31 bytes without large file support */
313 if (small
&& length
> MAX_NON_LFS
)
317 if (IS_APPEND(inode
))
320 error
= locks_verify_truncate(inode
, file
, length
);
322 error
= do_truncate(dentry
, length
, 0, file
);
329 asmlinkage
long sys_ftruncate(unsigned int fd
, unsigned long length
)
331 return do_sys_ftruncate(fd
, length
, 1);
334 /* LFS versions of truncate are only needed on 32 bit machines */
335 #if BITS_PER_LONG == 32
336 asmlinkage
long sys_truncate64(const char __user
* path
, loff_t length
)
338 return do_sys_truncate(path
, length
);
341 asmlinkage
long sys_ftruncate64(unsigned int fd
, loff_t length
)
343 return do_sys_ftruncate(fd
, length
, 0);
347 #ifdef __ARCH_WANT_SYS_UTIME
350 * sys_utime() can be implemented in user-level using sys_utimes().
351 * Is this for backwards compatibility? If so, why not move it
352 * into the appropriate arch directory (for those architectures that
356 /* If times==NULL, set access and modification to current time,
357 * must be owner or have write permission.
358 * Else, update from *times, must be owner or super user.
360 asmlinkage
long sys_utime(char __user
* filename
, struct utimbuf __user
* times
)
364 struct inode
* inode
;
365 struct iattr newattrs
;
367 error
= user_path_walk(filename
, &nd
);
370 inode
= nd
.dentry
->d_inode
;
373 if (IS_RDONLY(inode
))
376 /* Don't worry, the checks are done in inode_change_ok() */
377 newattrs
.ia_valid
= ATTR_CTIME
| ATTR_MTIME
| ATTR_ATIME
;
380 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
383 error
= get_user(newattrs
.ia_atime
.tv_sec
, ×
->actime
);
384 newattrs
.ia_atime
.tv_nsec
= 0;
386 error
= get_user(newattrs
.ia_mtime
.tv_sec
, ×
->modtime
);
387 newattrs
.ia_mtime
.tv_nsec
= 0;
391 newattrs
.ia_valid
|= ATTR_ATIME_SET
| ATTR_MTIME_SET
;
394 if (IS_IMMUTABLE(inode
))
397 if (current
->fsuid
!= inode
->i_uid
&&
398 (error
= vfs_permission(&nd
, MAY_WRITE
)) != 0)
402 error
= notify_change(nd
.dentry
, &newattrs
);
412 /* If times==NULL, set access and modification to current time,
413 * must be owner or have write permission.
414 * Else, update from *times, must be owner or super user.
416 long do_utimes(char __user
* filename
, struct timeval
* times
)
420 struct inode
* inode
;
421 struct iattr newattrs
;
423 error
= user_path_walk(filename
, &nd
);
427 inode
= nd
.dentry
->d_inode
;
430 if (IS_RDONLY(inode
))
433 /* Don't worry, the checks are done in inode_change_ok() */
434 newattrs
.ia_valid
= ATTR_CTIME
| ATTR_MTIME
| ATTR_ATIME
;
437 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
440 newattrs
.ia_atime
.tv_sec
= times
[0].tv_sec
;
441 newattrs
.ia_atime
.tv_nsec
= times
[0].tv_usec
* 1000;
442 newattrs
.ia_mtime
.tv_sec
= times
[1].tv_sec
;
443 newattrs
.ia_mtime
.tv_nsec
= times
[1].tv_usec
* 1000;
444 newattrs
.ia_valid
|= ATTR_ATIME_SET
| ATTR_MTIME_SET
;
447 if (IS_IMMUTABLE(inode
))
450 if (current
->fsuid
!= inode
->i_uid
&&
451 (error
= vfs_permission(&nd
, MAY_WRITE
)) != 0)
455 error
= notify_change(nd
.dentry
, &newattrs
);
463 asmlinkage
long sys_utimes(char __user
* filename
, struct timeval __user
* utimes
)
465 struct timeval times
[2];
467 if (utimes
&& copy_from_user(×
, utimes
, sizeof(times
)))
469 return do_utimes(filename
, utimes
? times
: NULL
);
474 * access() needs to use the real uid/gid, not the effective uid/gid.
475 * We do this by temporarily clearing all FS-related capabilities and
476 * switching the fsuid/fsgid around to the real ones.
478 asmlinkage
long sys_access(const char __user
* filename
, int mode
)
481 int old_fsuid
, old_fsgid
;
482 kernel_cap_t old_cap
;
485 if (mode
& ~S_IRWXO
) /* where's F_OK, X_OK, W_OK, R_OK? */
488 old_fsuid
= current
->fsuid
;
489 old_fsgid
= current
->fsgid
;
490 old_cap
= current
->cap_effective
;
492 current
->fsuid
= current
->uid
;
493 current
->fsgid
= current
->gid
;
496 * Clear the capabilities if we switch to a non-root user
498 * FIXME: There is a race here against sys_capset. The
499 * capabilities can change yet we will restore the old
500 * value below. We should hold task_capabilities_lock,
501 * but we cannot because user_path_walk can sleep.
504 cap_clear(current
->cap_effective
);
506 current
->cap_effective
= current
->cap_permitted
;
508 res
= __user_walk(filename
, LOOKUP_FOLLOW
|LOOKUP_ACCESS
, &nd
);
510 res
= vfs_permission(&nd
, mode
);
511 /* SuS v2 requires we report a read only fs too */
512 if(!res
&& (mode
& S_IWOTH
) && IS_RDONLY(nd
.dentry
->d_inode
)
513 && !special_file(nd
.dentry
->d_inode
->i_mode
))
518 current
->fsuid
= old_fsuid
;
519 current
->fsgid
= old_fsgid
;
520 current
->cap_effective
= old_cap
;
525 asmlinkage
long sys_chdir(const char __user
* filename
)
530 error
= __user_walk(filename
, LOOKUP_FOLLOW
|LOOKUP_DIRECTORY
, &nd
);
534 error
= vfs_permission(&nd
, MAY_EXEC
);
538 set_fs_pwd(current
->fs
, nd
.mnt
, nd
.dentry
);
546 asmlinkage
long sys_fchdir(unsigned int fd
)
549 struct dentry
*dentry
;
551 struct vfsmount
*mnt
;
559 dentry
= file
->f_dentry
;
560 mnt
= file
->f_vfsmnt
;
561 inode
= dentry
->d_inode
;
564 if (!S_ISDIR(inode
->i_mode
))
567 error
= file_permission(file
, MAY_EXEC
);
569 set_fs_pwd(current
->fs
, mnt
, dentry
);
576 asmlinkage
long sys_chroot(const char __user
* filename
)
581 error
= __user_walk(filename
, LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
| LOOKUP_NOALT
, &nd
);
585 error
= vfs_permission(&nd
, MAY_EXEC
);
590 if (!capable(CAP_SYS_CHROOT
))
593 set_fs_root(current
->fs
, nd
.mnt
, nd
.dentry
);
602 asmlinkage
long sys_fchmod(unsigned int fd
, mode_t mode
)
604 struct inode
* inode
;
605 struct dentry
* dentry
;
608 struct iattr newattrs
;
614 dentry
= file
->f_dentry
;
615 inode
= dentry
->d_inode
;
618 if (IS_RDONLY(inode
))
621 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
624 if (mode
== (mode_t
) -1)
625 mode
= inode
->i_mode
;
626 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
627 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
628 err
= notify_change(dentry
, &newattrs
);
637 asmlinkage
long sys_chmod(const char __user
* filename
, mode_t mode
)
640 struct inode
* inode
;
642 struct iattr newattrs
;
644 error
= user_path_walk(filename
, &nd
);
647 inode
= nd
.dentry
->d_inode
;
650 if (IS_RDONLY(inode
))
654 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
658 if (mode
== (mode_t
) -1)
659 mode
= inode
->i_mode
;
660 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
661 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
662 error
= notify_change(nd
.dentry
, &newattrs
);
671 static int chown_common(struct dentry
* dentry
, uid_t user
, gid_t group
)
673 struct inode
* inode
;
675 struct iattr newattrs
;
678 if (!(inode
= dentry
->d_inode
)) {
679 printk(KERN_ERR
"chown_common: NULL inode\n");
683 if (IS_RDONLY(inode
))
686 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
688 newattrs
.ia_valid
= ATTR_CTIME
;
689 if (user
!= (uid_t
) -1) {
690 newattrs
.ia_valid
|= ATTR_UID
;
691 newattrs
.ia_uid
= user
;
693 if (group
!= (gid_t
) -1) {
694 newattrs
.ia_valid
|= ATTR_GID
;
695 newattrs
.ia_gid
= group
;
697 if (!S_ISDIR(inode
->i_mode
))
698 newattrs
.ia_valid
|= ATTR_KILL_SUID
|ATTR_KILL_SGID
;
700 error
= notify_change(dentry
, &newattrs
);
706 asmlinkage
long sys_chown(const char __user
* filename
, uid_t user
, gid_t group
)
711 error
= user_path_walk(filename
, &nd
);
713 error
= chown_common(nd
.dentry
, user
, group
);
719 asmlinkage
long sys_lchown(const char __user
* filename
, uid_t user
, gid_t group
)
724 error
= user_path_walk_link(filename
, &nd
);
726 error
= chown_common(nd
.dentry
, user
, group
);
733 asmlinkage
long sys_fchown(unsigned int fd
, uid_t user
, gid_t group
)
740 error
= chown_common(file
->f_dentry
, user
, group
);
746 static struct file
*__dentry_open(struct dentry
*dentry
, struct vfsmount
*mnt
,
747 int flags
, struct file
*f
,
748 int (*open
)(struct inode
*, struct file
*))
754 f
->f_mode
= ((flags
+1) & O_ACCMODE
) | FMODE_LSEEK
|
755 FMODE_PREAD
| FMODE_PWRITE
;
756 inode
= dentry
->d_inode
;
757 if (f
->f_mode
& FMODE_WRITE
) {
758 error
= get_write_access(inode
);
763 f
->f_mapping
= inode
->i_mapping
;
764 f
->f_dentry
= dentry
;
767 f
->f_op
= fops_get(inode
->i_fop
);
768 file_move(f
, &inode
->i_sb
->s_files
);
770 if (!open
&& f
->f_op
)
771 open
= f
->f_op
->open
;
773 error
= open(inode
, f
);
778 f
->f_flags
&= ~(O_CREAT
| O_EXCL
| O_NOCTTY
| O_TRUNC
);
780 file_ra_state_init(&f
->f_ra
, f
->f_mapping
->host
->i_mapping
);
782 /* NB: we're sure to have correct a_ops only after f_op->open */
783 if (f
->f_flags
& O_DIRECT
) {
784 if (!f
->f_mapping
->a_ops
||
785 ((!f
->f_mapping
->a_ops
->direct_IO
) &&
786 (!f
->f_mapping
->a_ops
->get_xip_page
))) {
788 f
= ERR_PTR(-EINVAL
);
796 if (f
->f_mode
& FMODE_WRITE
)
797 put_write_access(inode
);
805 return ERR_PTR(error
);
809 * Note that while the flag value (low two bits) for sys_open means:
815 * 00 - no permissions needed
816 * 01 - read-permission
817 * 10 - write-permission
819 * for the internal routines (ie open_namei()/follow_link() etc). 00 is
822 struct file
*filp_open(const char * filename
, int flags
, int mode
)
824 int namei_flags
, error
;
828 if ((namei_flags
+1) & O_ACCMODE
)
831 error
= open_namei(filename
, namei_flags
, mode
, &nd
);
833 return nameidata_to_filp(&nd
, flags
);
835 return ERR_PTR(error
);
837 EXPORT_SYMBOL(filp_open
);
840 * lookup_instantiate_filp - instantiates the open intent filp
841 * @nd: pointer to nameidata
842 * @dentry: pointer to dentry
843 * @open: open callback
845 * Helper for filesystems that want to use lookup open intents and pass back
846 * a fully instantiated struct file to the caller.
847 * This function is meant to be called from within a filesystem's
849 * Note that in case of error, nd->intent.open.file is destroyed, but the
850 * path information remains valid.
851 * If the open callback is set to NULL, then the standard f_op->open()
852 * filesystem callback is substituted.
854 struct file
*lookup_instantiate_filp(struct nameidata
*nd
, struct dentry
*dentry
,
855 int (*open
)(struct inode
*, struct file
*))
857 if (IS_ERR(nd
->intent
.open
.file
))
861 nd
->intent
.open
.file
= __dentry_open(dget(dentry
), mntget(nd
->mnt
),
862 nd
->intent
.open
.flags
- 1,
863 nd
->intent
.open
.file
,
866 return nd
->intent
.open
.file
;
868 release_open_intent(nd
);
869 nd
->intent
.open
.file
= (struct file
*)dentry
;
872 EXPORT_SYMBOL_GPL(lookup_instantiate_filp
);
875 * nameidata_to_filp - convert a nameidata to an open filp.
876 * @nd: pointer to nameidata
879 * Note that this function destroys the original nameidata
881 struct file
*nameidata_to_filp(struct nameidata
*nd
, int flags
)
885 /* Pick up the filp from the open intent */
886 filp
= nd
->intent
.open
.file
;
887 /* Has the filesystem initialised the file for us? */
888 if (filp
->f_dentry
== NULL
)
889 filp
= __dentry_open(nd
->dentry
, nd
->mnt
, flags
, filp
, NULL
);
896 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
899 struct file
*dentry_open(struct dentry
*dentry
, struct vfsmount
*mnt
, int flags
)
905 f
= get_empty_filp();
909 return ERR_PTR(error
);
912 return __dentry_open(dentry
, mnt
, flags
, f
, NULL
);
914 EXPORT_SYMBOL(dentry_open
);
917 * Find an empty file descriptor entry, and mark it busy.
919 int get_unused_fd(void)
921 struct files_struct
* files
= current
->files
;
926 spin_lock(&files
->file_lock
);
929 fdt
= files_fdtable(files
);
930 fd
= find_next_zero_bit(fdt
->open_fds
->fds_bits
,
935 * N.B. For clone tasks sharing a files structure, this test
936 * will limit the total number of files that can be opened.
938 if (fd
>= current
->signal
->rlim
[RLIMIT_NOFILE
].rlim_cur
)
941 /* Do we need to expand the fd array or fd set? */
942 error
= expand_files(files
, fd
);
948 * If we needed to expand the fs array we
949 * might have blocked - try again.
955 FD_SET(fd
, fdt
->open_fds
);
956 FD_CLR(fd
, fdt
->close_on_exec
);
957 fdt
->next_fd
= fd
+ 1;
960 if (fdt
->fd
[fd
] != NULL
) {
961 printk(KERN_WARNING
"get_unused_fd: slot %d not NULL!\n", fd
);
968 spin_unlock(&files
->file_lock
);
972 EXPORT_SYMBOL(get_unused_fd
);
974 static void __put_unused_fd(struct files_struct
*files
, unsigned int fd
)
976 struct fdtable
*fdt
= files_fdtable(files
);
977 __FD_CLR(fd
, fdt
->open_fds
);
978 if (fd
< fdt
->next_fd
)
982 void fastcall
put_unused_fd(unsigned int fd
)
984 struct files_struct
*files
= current
->files
;
985 spin_lock(&files
->file_lock
);
986 __put_unused_fd(files
, fd
);
987 spin_unlock(&files
->file_lock
);
990 EXPORT_SYMBOL(put_unused_fd
);
993 * Install a file pointer in the fd array.
995 * The VFS is full of places where we drop the files lock between
996 * setting the open_fds bitmap and installing the file in the file
997 * array. At any such point, we are vulnerable to a dup2() race
998 * installing a file in the array before us. We need to detect this and
999 * fput() the struct file we are about to overwrite in this case.
1001 * It should never happen - if we allow dup2() do it, _really_ bad things
1005 void fastcall
fd_install(unsigned int fd
, struct file
* file
)
1007 struct files_struct
*files
= current
->files
;
1008 struct fdtable
*fdt
;
1009 spin_lock(&files
->file_lock
);
1010 fdt
= files_fdtable(files
);
1011 BUG_ON(fdt
->fd
[fd
] != NULL
);
1012 rcu_assign_pointer(fdt
->fd
[fd
], file
);
1013 spin_unlock(&files
->file_lock
);
1016 EXPORT_SYMBOL(fd_install
);
1018 long do_sys_open(const char __user
*filename
, int flags
, int mode
)
1020 char *tmp
= getname(filename
);
1021 int fd
= PTR_ERR(tmp
);
1024 fd
= get_unused_fd();
1026 struct file
*f
= filp_open(tmp
, flags
, mode
);
1031 fsnotify_open(f
->f_dentry
);
1040 asmlinkage
long sys_open(const char __user
*filename
, int flags
, int mode
)
1042 if (force_o_largefile())
1043 flags
|= O_LARGEFILE
;
1045 return do_sys_open(filename
, flags
, mode
);
1047 EXPORT_SYMBOL_GPL(sys_open
);
1052 * For backward compatibility? Maybe this should be moved
1053 * into arch/i386 instead?
1055 asmlinkage
long sys_creat(const char __user
* pathname
, int mode
)
1057 return sys_open(pathname
, O_CREAT
| O_WRONLY
| O_TRUNC
, mode
);
1063 * "id" is the POSIX thread ID. We use the
1064 * files pointer for this..
1066 int filp_close(struct file
*filp
, fl_owner_t id
)
1070 if (!file_count(filp
)) {
1071 printk(KERN_ERR
"VFS: Close: file count is 0\n");
1075 if (filp
->f_op
&& filp
->f_op
->flush
)
1076 retval
= filp
->f_op
->flush(filp
);
1078 dnotify_flush(filp
, id
);
1079 locks_remove_posix(filp
, id
);
1084 EXPORT_SYMBOL(filp_close
);
1087 * Careful here! We test whether the file pointer is NULL before
1088 * releasing the fd. This ensures that one clone task can't release
1089 * an fd while another clone is opening it.
1091 asmlinkage
long sys_close(unsigned int fd
)
1094 struct files_struct
*files
= current
->files
;
1095 struct fdtable
*fdt
;
1097 spin_lock(&files
->file_lock
);
1098 fdt
= files_fdtable(files
);
1099 if (fd
>= fdt
->max_fds
)
1104 rcu_assign_pointer(fdt
->fd
[fd
], NULL
);
1105 FD_CLR(fd
, fdt
->close_on_exec
);
1106 __put_unused_fd(files
, fd
);
1107 spin_unlock(&files
->file_lock
);
1108 return filp_close(filp
, files
);
1111 spin_unlock(&files
->file_lock
);
1115 EXPORT_SYMBOL(sys_close
);
1118 * This routine simulates a hangup on the tty, to arrange that users
1119 * are given clean terminals at login time.
1121 asmlinkage
long sys_vhangup(void)
1123 if (capable(CAP_SYS_TTY_CONFIG
)) {
1124 tty_vhangup(current
->signal
->tty
);
1131 * Called when an inode is about to be open.
1132 * We use this to disallow opening large files on 32bit systems if
1133 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1134 * on this flag in sys_open.
1136 int generic_file_open(struct inode
* inode
, struct file
* filp
)
1138 if (!(filp
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
1143 EXPORT_SYMBOL(generic_file_open
);
1146 * This is used by subsystems that don't want seekable
1149 int nonseekable_open(struct inode
*inode
, struct file
*filp
)
1151 filp
->f_mode
&= ~(FMODE_LSEEK
| FMODE_PREAD
| FMODE_PWRITE
);
1155 EXPORT_SYMBOL(nonseekable_open
);