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/fsnotify.h>
12 #include <linux/module.h>
13 #include <linux/tty.h>
14 #include <linux/namei.h>
15 #include <linux/backing-dev.h>
16 #include <linux/capability.h>
17 #include <linux/securebits.h>
18 #include <linux/security.h>
19 #include <linux/mount.h>
20 #include <linux/fcntl.h>
21 #include <linux/slab.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>
28 #include <linux/audit.h>
29 #include <linux/falloc.h>
30 #include <linux/fs_struct.h>
31 #include <linux/ima.h>
32 #include <linux/dnotify.h>
36 int do_truncate(struct dentry
*dentry
, loff_t length
, unsigned int time_attrs
,
40 struct iattr newattrs
;
42 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
46 newattrs
.ia_size
= length
;
47 newattrs
.ia_valid
= ATTR_SIZE
| time_attrs
;
49 newattrs
.ia_file
= filp
;
50 newattrs
.ia_valid
|= ATTR_FILE
;
53 /* Remove suid/sgid on truncate too */
54 ret
= should_remove_suid(dentry
);
56 newattrs
.ia_valid
|= ret
| ATTR_FORCE
;
58 mutex_lock(&dentry
->d_inode
->i_mutex
);
59 ret
= notify_change(dentry
, &newattrs
);
60 mutex_unlock(&dentry
->d_inode
->i_mutex
);
64 long vfs_truncate(struct path
*path
, loff_t length
)
69 inode
= path
->dentry
->d_inode
;
71 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
72 if (S_ISDIR(inode
->i_mode
))
74 if (!S_ISREG(inode
->i_mode
))
77 error
= mnt_want_write(path
->mnt
);
81 error
= inode_permission(inode
, MAY_WRITE
);
83 goto mnt_drop_write_and_out
;
87 goto mnt_drop_write_and_out
;
89 error
= get_write_access(inode
);
91 goto mnt_drop_write_and_out
;
94 * Make sure that there are no leases. get_write_access() protects
95 * against the truncate racing with a lease-granting setlease().
97 error
= break_lease(inode
, O_WRONLY
);
99 goto put_write_and_out
;
101 error
= locks_verify_truncate(inode
, NULL
, length
);
103 error
= security_path_truncate(path
);
105 error
= do_truncate(path
->dentry
, length
, 0, NULL
);
108 put_write_access(inode
);
109 mnt_drop_write_and_out
:
110 mnt_drop_write(path
->mnt
);
114 EXPORT_SYMBOL_GPL(vfs_truncate
);
116 static long do_sys_truncate(const char __user
*pathname
, loff_t length
)
121 if (length
< 0) /* sorry, but loff_t says... */
124 error
= user_path(pathname
, &path
);
126 error
= vfs_truncate(&path
, length
);
132 SYSCALL_DEFINE2(truncate
, const char __user
*, path
, long, length
)
134 return do_sys_truncate(path
, length
);
137 static long do_sys_ftruncate(unsigned int fd
, loff_t length
, int small
)
140 struct dentry
*dentry
;
152 /* explicitly opened as large or we are on 64-bit box */
153 if (f
.file
->f_flags
& O_LARGEFILE
)
156 dentry
= f
.file
->f_path
.dentry
;
157 inode
= dentry
->d_inode
;
159 if (!S_ISREG(inode
->i_mode
) || !(f
.file
->f_mode
& FMODE_WRITE
))
163 /* Cannot ftruncate over 2^31 bytes without large file support */
164 if (small
&& length
> MAX_NON_LFS
)
168 if (IS_APPEND(inode
))
171 sb_start_write(inode
->i_sb
);
172 error
= locks_verify_truncate(inode
, f
.file
, length
);
174 error
= security_path_truncate(&f
.file
->f_path
);
176 error
= do_truncate(dentry
, length
, ATTR_MTIME
|ATTR_CTIME
, f
.file
);
177 sb_end_write(inode
->i_sb
);
184 SYSCALL_DEFINE2(ftruncate
, unsigned int, fd
, unsigned long, length
)
186 long ret
= do_sys_ftruncate(fd
, length
, 1);
187 /* avoid REGPARM breakage on x86: */
188 asmlinkage_protect(2, ret
, fd
, length
);
192 /* LFS versions of truncate are only needed on 32 bit machines */
193 #if BITS_PER_LONG == 32
194 SYSCALL_DEFINE(truncate64
)(const char __user
* path
, loff_t length
)
196 return do_sys_truncate(path
, length
);
198 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
199 asmlinkage
long SyS_truncate64(long path
, loff_t length
)
201 return SYSC_truncate64((const char __user
*) path
, length
);
203 SYSCALL_ALIAS(sys_truncate64
, SyS_truncate64
);
206 SYSCALL_DEFINE(ftruncate64
)(unsigned int fd
, loff_t length
)
208 long ret
= do_sys_ftruncate(fd
, length
, 0);
209 /* avoid REGPARM breakage on x86: */
210 asmlinkage_protect(2, ret
, fd
, length
);
213 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
214 asmlinkage
long SyS_ftruncate64(long fd
, loff_t length
)
216 return SYSC_ftruncate64((unsigned int) fd
, length
);
218 SYSCALL_ALIAS(sys_ftruncate64
, SyS_ftruncate64
);
220 #endif /* BITS_PER_LONG == 32 */
223 int do_fallocate(struct file
*file
, int mode
, loff_t offset
, loff_t len
)
225 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
228 if (offset
< 0 || len
<= 0)
231 /* Return error if mode is not supported */
232 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
235 /* Punch hole must have keep size set */
236 if ((mode
& FALLOC_FL_PUNCH_HOLE
) &&
237 !(mode
& FALLOC_FL_KEEP_SIZE
))
240 if (!(file
->f_mode
& FMODE_WRITE
))
243 /* It's not possible punch hole on append only file */
244 if (mode
& FALLOC_FL_PUNCH_HOLE
&& IS_APPEND(inode
))
247 if (IS_IMMUTABLE(inode
))
251 * Revalidate the write permissions, in case security policy has
252 * changed since the files were opened.
254 ret
= security_file_permission(file
, MAY_WRITE
);
258 if (S_ISFIFO(inode
->i_mode
))
262 * Let individual file system decide if it supports preallocation
263 * for directories or not.
265 if (!S_ISREG(inode
->i_mode
) && !S_ISDIR(inode
->i_mode
))
268 /* Check for wrap through zero too */
269 if (((offset
+ len
) > inode
->i_sb
->s_maxbytes
) || ((offset
+ len
) < 0))
272 if (!file
->f_op
->fallocate
)
275 sb_start_write(inode
->i_sb
);
276 ret
= file
->f_op
->fallocate(file
, mode
, offset
, len
);
277 sb_end_write(inode
->i_sb
);
281 SYSCALL_DEFINE(fallocate
)(int fd
, int mode
, loff_t offset
, loff_t len
)
283 struct fd f
= fdget(fd
);
287 error
= do_fallocate(f
.file
, mode
, offset
, len
);
293 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
294 asmlinkage
long SyS_fallocate(long fd
, long mode
, loff_t offset
, loff_t len
)
296 return SYSC_fallocate((int)fd
, (int)mode
, offset
, len
);
298 SYSCALL_ALIAS(sys_fallocate
, SyS_fallocate
);
302 * access() needs to use the real uid/gid, not the effective uid/gid.
303 * We do this by temporarily clearing all FS-related capabilities and
304 * switching the fsuid/fsgid around to the real ones.
306 SYSCALL_DEFINE3(faccessat
, int, dfd
, const char __user
*, filename
, int, mode
)
308 const struct cred
*old_cred
;
309 struct cred
*override_cred
;
314 if (mode
& ~S_IRWXO
) /* where's F_OK, X_OK, W_OK, R_OK? */
317 override_cred
= prepare_creds();
321 override_cred
->fsuid
= override_cred
->uid
;
322 override_cred
->fsgid
= override_cred
->gid
;
324 if (!issecure(SECURE_NO_SETUID_FIXUP
)) {
325 /* Clear the capabilities if we switch to a non-root user */
326 kuid_t root_uid
= make_kuid(override_cred
->user_ns
, 0);
327 if (!uid_eq(override_cred
->uid
, root_uid
))
328 cap_clear(override_cred
->cap_effective
);
330 override_cred
->cap_effective
=
331 override_cred
->cap_permitted
;
334 old_cred
= override_creds(override_cred
);
336 res
= user_path_at(dfd
, filename
, LOOKUP_FOLLOW
, &path
);
340 inode
= path
.dentry
->d_inode
;
342 if ((mode
& MAY_EXEC
) && S_ISREG(inode
->i_mode
)) {
344 * MAY_EXEC on regular files is denied if the fs is mounted
345 * with the "noexec" flag.
348 if (path
.mnt
->mnt_flags
& MNT_NOEXEC
)
349 goto out_path_release
;
352 res
= inode_permission(inode
, mode
| MAY_ACCESS
);
353 /* SuS v2 requires we report a read only fs too */
354 if (res
|| !(mode
& S_IWOTH
) || special_file(inode
->i_mode
))
355 goto out_path_release
;
357 * This is a rare case where using __mnt_is_readonly()
358 * is OK without a mnt_want/drop_write() pair. Since
359 * no actual write to the fs is performed here, we do
360 * not need to telegraph to that to anyone.
362 * By doing this, we accept that this access is
363 * inherently racy and know that the fs may change
364 * state before we even see this result.
366 if (__mnt_is_readonly(path
.mnt
))
372 revert_creds(old_cred
);
373 put_cred(override_cred
);
377 SYSCALL_DEFINE2(access
, const char __user
*, filename
, int, mode
)
379 return sys_faccessat(AT_FDCWD
, filename
, mode
);
382 SYSCALL_DEFINE1(chdir
, const char __user
*, filename
)
387 error
= user_path_dir(filename
, &path
);
391 error
= inode_permission(path
.dentry
->d_inode
, MAY_EXEC
| MAY_CHDIR
);
395 set_fs_pwd(current
->fs
, &path
);
403 SYSCALL_DEFINE1(fchdir
, unsigned int, fd
)
405 struct fd f
= fdget_raw(fd
);
413 inode
= f
.file
->f_path
.dentry
->d_inode
;
416 if (!S_ISDIR(inode
->i_mode
))
419 error
= inode_permission(inode
, MAY_EXEC
| MAY_CHDIR
);
421 set_fs_pwd(current
->fs
, &f
.file
->f_path
);
428 SYSCALL_DEFINE1(chroot
, const char __user
*, filename
)
433 error
= user_path_dir(filename
, &path
);
437 error
= inode_permission(path
.dentry
->d_inode
, MAY_EXEC
| MAY_CHDIR
);
442 if (!nsown_capable(CAP_SYS_CHROOT
))
444 error
= security_path_chroot(&path
);
448 set_fs_root(current
->fs
, &path
);
456 static int chmod_common(struct path
*path
, umode_t mode
)
458 struct inode
*inode
= path
->dentry
->d_inode
;
459 struct iattr newattrs
;
462 error
= mnt_want_write(path
->mnt
);
465 mutex_lock(&inode
->i_mutex
);
466 error
= security_path_chmod(path
, mode
);
469 newattrs
.ia_mode
= (mode
& S_IALLUGO
) | (inode
->i_mode
& ~S_IALLUGO
);
470 newattrs
.ia_valid
= ATTR_MODE
| ATTR_CTIME
;
471 error
= notify_change(path
->dentry
, &newattrs
);
473 mutex_unlock(&inode
->i_mutex
);
474 mnt_drop_write(path
->mnt
);
478 SYSCALL_DEFINE2(fchmod
, unsigned int, fd
, umode_t
, mode
)
485 audit_inode(NULL
, file
->f_path
.dentry
, 0);
486 err
= chmod_common(&file
->f_path
, mode
);
492 SYSCALL_DEFINE3(fchmodat
, int, dfd
, const char __user
*, filename
, umode_t
, mode
)
497 error
= user_path_at(dfd
, filename
, LOOKUP_FOLLOW
, &path
);
499 error
= chmod_common(&path
, mode
);
505 SYSCALL_DEFINE2(chmod
, const char __user
*, filename
, umode_t
, mode
)
507 return sys_fchmodat(AT_FDCWD
, filename
, mode
);
510 static int chown_common(struct path
*path
, uid_t user
, gid_t group
)
512 struct inode
*inode
= path
->dentry
->d_inode
;
514 struct iattr newattrs
;
518 uid
= make_kuid(current_user_ns(), user
);
519 gid
= make_kgid(current_user_ns(), group
);
521 newattrs
.ia_valid
= ATTR_CTIME
;
522 if (user
!= (uid_t
) -1) {
525 newattrs
.ia_valid
|= ATTR_UID
;
526 newattrs
.ia_uid
= uid
;
528 if (group
!= (gid_t
) -1) {
531 newattrs
.ia_valid
|= ATTR_GID
;
532 newattrs
.ia_gid
= gid
;
534 if (!S_ISDIR(inode
->i_mode
))
536 ATTR_KILL_SUID
| ATTR_KILL_SGID
| ATTR_KILL_PRIV
;
537 mutex_lock(&inode
->i_mutex
);
538 error
= security_path_chown(path
, uid
, gid
);
540 error
= notify_change(path
->dentry
, &newattrs
);
541 mutex_unlock(&inode
->i_mutex
);
546 SYSCALL_DEFINE5(fchownat
, int, dfd
, const char __user
*, filename
, uid_t
, user
,
547 gid_t
, group
, int, flag
)
553 if ((flag
& ~(AT_SYMLINK_NOFOLLOW
| AT_EMPTY_PATH
)) != 0)
556 lookup_flags
= (flag
& AT_SYMLINK_NOFOLLOW
) ? 0 : LOOKUP_FOLLOW
;
557 if (flag
& AT_EMPTY_PATH
)
558 lookup_flags
|= LOOKUP_EMPTY
;
559 error
= user_path_at(dfd
, filename
, lookup_flags
, &path
);
562 error
= mnt_want_write(path
.mnt
);
565 error
= chown_common(&path
, user
, group
);
566 mnt_drop_write(path
.mnt
);
573 SYSCALL_DEFINE3(chown
, const char __user
*, filename
, uid_t
, user
, gid_t
, group
)
575 return sys_fchownat(AT_FDCWD
, filename
, user
, group
, 0);
578 SYSCALL_DEFINE3(lchown
, const char __user
*, filename
, uid_t
, user
, gid_t
, group
)
580 return sys_fchownat(AT_FDCWD
, filename
, user
, group
,
581 AT_SYMLINK_NOFOLLOW
);
584 SYSCALL_DEFINE3(fchown
, unsigned int, fd
, uid_t
, user
, gid_t
, group
)
586 struct fd f
= fdget(fd
);
592 error
= mnt_want_write_file(f
.file
);
595 audit_inode(NULL
, f
.file
->f_path
.dentry
, 0);
596 error
= chown_common(&f
.file
->f_path
, user
, group
);
597 mnt_drop_write_file(f
.file
);
605 * You have to be very careful that these write
606 * counts get cleaned up in error cases and
607 * upon __fput(). This should probably never
608 * be called outside of __dentry_open().
610 static inline int __get_file_write_access(struct inode
*inode
,
611 struct vfsmount
*mnt
)
614 error
= get_write_access(inode
);
618 * Do not take mount writer counts on
619 * special files since no writes to
620 * the mount itself will occur.
622 if (!special_file(inode
->i_mode
)) {
624 * Balanced in __fput()
626 error
= __mnt_want_write(mnt
);
628 put_write_access(inode
);
633 int open_check_o_direct(struct file
*f
)
635 /* NB: we're sure to have correct a_ops only after f_op->open */
636 if (f
->f_flags
& O_DIRECT
) {
637 if (!f
->f_mapping
->a_ops
||
638 ((!f
->f_mapping
->a_ops
->direct_IO
) &&
639 (!f
->f_mapping
->a_ops
->get_xip_mem
))) {
646 static int do_dentry_open(struct file
*f
,
647 int (*open
)(struct inode
*, struct file
*),
648 const struct cred
*cred
)
650 static const struct file_operations empty_fops
= {};
654 f
->f_mode
= OPEN_FMODE(f
->f_flags
) | FMODE_LSEEK
|
655 FMODE_PREAD
| FMODE_PWRITE
;
657 if (unlikely(f
->f_flags
& O_PATH
))
658 f
->f_mode
= FMODE_PATH
;
660 path_get(&f
->f_path
);
661 inode
= f
->f_path
.dentry
->d_inode
;
662 if (f
->f_mode
& FMODE_WRITE
) {
663 error
= __get_file_write_access(inode
, f
->f_path
.mnt
);
666 if (!special_file(inode
->i_mode
))
670 f
->f_mapping
= inode
->i_mapping
;
672 file_sb_list_add(f
, inode
->i_sb
);
674 if (unlikely(f
->f_mode
& FMODE_PATH
)) {
675 f
->f_op
= &empty_fops
;
679 f
->f_op
= fops_get(inode
->i_fop
);
681 error
= security_file_open(f
, cred
);
685 error
= break_lease(inode
, f
->f_flags
);
689 if (!open
&& f
->f_op
)
690 open
= f
->f_op
->open
;
692 error
= open(inode
, f
);
696 if ((f
->f_mode
& (FMODE_READ
| FMODE_WRITE
)) == FMODE_READ
)
697 i_readcount_inc(inode
);
699 f
->f_flags
&= ~(O_CREAT
| O_EXCL
| O_NOCTTY
| O_TRUNC
);
701 file_ra_state_init(&f
->f_ra
, f
->f_mapping
->host
->i_mapping
);
708 if (f
->f_mode
& FMODE_WRITE
) {
709 put_write_access(inode
);
710 if (!special_file(inode
->i_mode
)) {
712 * We don't consider this a real
713 * mnt_want/drop_write() pair
714 * because it all happenend right
715 * here, so just reset the state.
718 __mnt_drop_write(f
->f_path
.mnt
);
722 path_put(&f
->f_path
);
723 f
->f_path
.mnt
= NULL
;
724 f
->f_path
.dentry
= NULL
;
729 * finish_open - finish opening a file
730 * @od: opaque open data
731 * @dentry: pointer to dentry
732 * @open: open callback
734 * This can be used to finish opening a file passed to i_op->atomic_open().
736 * If the open callback is set to NULL, then the standard f_op->open()
737 * filesystem callback is substituted.
739 int finish_open(struct file
*file
, struct dentry
*dentry
,
740 int (*open
)(struct inode
*, struct file
*),
744 BUG_ON(*opened
& FILE_OPENED
); /* once it's opened, it's opened */
746 file
->f_path
.dentry
= dentry
;
747 error
= do_dentry_open(file
, open
, current_cred());
749 *opened
|= FILE_OPENED
;
753 EXPORT_SYMBOL(finish_open
);
756 * finish_no_open - finish ->atomic_open() without opening the file
758 * @od: opaque open data
759 * @dentry: dentry or NULL (as returned from ->lookup())
761 * This can be used to set the result of a successful lookup in ->atomic_open().
762 * The filesystem's atomic_open() method shall return NULL after calling this.
764 int finish_no_open(struct file
*file
, struct dentry
*dentry
)
766 file
->f_path
.dentry
= dentry
;
769 EXPORT_SYMBOL(finish_no_open
);
771 struct file
*dentry_open(const struct path
*path
, int flags
,
772 const struct cred
*cred
)
777 validate_creds(cred
);
779 /* We must always pass in a valid mount pointer. */
783 f
= get_empty_filp();
785 return ERR_PTR(error
);
789 error
= do_dentry_open(f
, NULL
, cred
);
791 error
= open_check_o_direct(f
);
802 EXPORT_SYMBOL(dentry_open
);
804 static inline int build_open_flags(int flags
, umode_t mode
, struct open_flags
*op
)
806 int lookup_flags
= 0;
810 op
->mode
= (mode
& S_IALLUGO
) | S_IFREG
;
814 /* Must never be set by userspace */
815 flags
&= ~FMODE_NONOTIFY
& ~O_CLOEXEC
;
818 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
819 * check for O_DSYNC if the need any syncing at all we enforce it's
820 * always set instead of having to deal with possibly weird behaviour
821 * for malicious applications setting only __O_SYNC.
823 if (flags
& __O_SYNC
)
827 * If we have O_PATH in the open flag. Then we
828 * cannot have anything other than the below set of flags
830 if (flags
& O_PATH
) {
831 flags
&= O_DIRECTORY
| O_NOFOLLOW
| O_PATH
;
834 acc_mode
= MAY_OPEN
| ACC_MODE(flags
);
837 op
->open_flag
= flags
;
839 /* O_TRUNC implies we need access checks for write permissions */
841 acc_mode
|= MAY_WRITE
;
843 /* Allow the LSM permission hook to distinguish append
844 access from general write access. */
845 if (flags
& O_APPEND
)
846 acc_mode
|= MAY_APPEND
;
848 op
->acc_mode
= acc_mode
;
850 op
->intent
= flags
& O_PATH
? 0 : LOOKUP_OPEN
;
852 if (flags
& O_CREAT
) {
853 op
->intent
|= LOOKUP_CREATE
;
855 op
->intent
|= LOOKUP_EXCL
;
858 if (flags
& O_DIRECTORY
)
859 lookup_flags
|= LOOKUP_DIRECTORY
;
860 if (!(flags
& O_NOFOLLOW
))
861 lookup_flags
|= LOOKUP_FOLLOW
;
866 * file_open_name - open file and return file pointer
868 * @name: struct filename containing path to open
869 * @flags: open flags as per the open(2) second argument
870 * @mode: mode for the new file if O_CREAT is set, else ignored
872 * This is the helper to open a file from kernelspace if you really
873 * have to. But in generally you should not do this, so please move
874 * along, nothing to see here..
876 struct file
*file_open_name(struct filename
*name
, int flags
, umode_t mode
)
878 struct open_flags op
;
879 int lookup
= build_open_flags(flags
, mode
, &op
);
880 return do_filp_open(AT_FDCWD
, name
, &op
, lookup
);
884 * filp_open - open file and return file pointer
886 * @filename: path to open
887 * @flags: open flags as per the open(2) second argument
888 * @mode: mode for the new file if O_CREAT is set, else ignored
890 * This is the helper to open a file from kernelspace if you really
891 * have to. But in generally you should not do this, so please move
892 * along, nothing to see here..
894 struct file
*filp_open(const char *filename
, int flags
, umode_t mode
)
896 struct filename name
= {.name
= filename
};
897 return file_open_name(&name
, flags
, mode
);
899 EXPORT_SYMBOL(filp_open
);
901 struct file
*file_open_root(struct dentry
*dentry
, struct vfsmount
*mnt
,
902 const char *filename
, int flags
)
904 struct open_flags op
;
905 int lookup
= build_open_flags(flags
, 0, &op
);
907 return ERR_PTR(-EINVAL
);
908 if (!filename
&& (flags
& O_DIRECTORY
))
909 if (!dentry
->d_inode
->i_op
->lookup
)
910 return ERR_PTR(-ENOTDIR
);
911 return do_file_open_root(dentry
, mnt
, filename
, &op
, lookup
);
913 EXPORT_SYMBOL(file_open_root
);
915 long do_sys_open(int dfd
, const char __user
*filename
, int flags
, umode_t mode
)
917 struct open_flags op
;
918 int lookup
= build_open_flags(flags
, mode
, &op
);
919 struct filename
*tmp
= getname(filename
);
920 int fd
= PTR_ERR(tmp
);
923 fd
= get_unused_fd_flags(flags
);
925 struct file
*f
= do_filp_open(dfd
, tmp
, &op
, lookup
);
939 SYSCALL_DEFINE3(open
, const char __user
*, filename
, int, flags
, umode_t
, mode
)
943 if (force_o_largefile())
944 flags
|= O_LARGEFILE
;
946 ret
= do_sys_open(AT_FDCWD
, filename
, flags
, mode
);
947 /* avoid REGPARM breakage on x86: */
948 asmlinkage_protect(3, ret
, filename
, flags
, mode
);
952 SYSCALL_DEFINE4(openat
, int, dfd
, const char __user
*, filename
, int, flags
,
957 if (force_o_largefile())
958 flags
|= O_LARGEFILE
;
960 ret
= do_sys_open(dfd
, filename
, flags
, mode
);
961 /* avoid REGPARM breakage on x86: */
962 asmlinkage_protect(4, ret
, dfd
, filename
, flags
, mode
);
969 * For backward compatibility? Maybe this should be moved
970 * into arch/i386 instead?
972 SYSCALL_DEFINE2(creat
, const char __user
*, pathname
, umode_t
, mode
)
974 return sys_open(pathname
, O_CREAT
| O_WRONLY
| O_TRUNC
, mode
);
980 * "id" is the POSIX thread ID. We use the
981 * files pointer for this..
983 int filp_close(struct file
*filp
, fl_owner_t id
)
987 if (!file_count(filp
)) {
988 printk(KERN_ERR
"VFS: Close: file count is 0\n");
992 if (filp
->f_op
&& filp
->f_op
->flush
)
993 retval
= filp
->f_op
->flush(filp
, id
);
995 if (likely(!(filp
->f_mode
& FMODE_PATH
))) {
996 dnotify_flush(filp
, id
);
997 locks_remove_posix(filp
, id
);
1003 EXPORT_SYMBOL(filp_close
);
1006 * Careful here! We test whether the file pointer is NULL before
1007 * releasing the fd. This ensures that one clone task can't release
1008 * an fd while another clone is opening it.
1010 SYSCALL_DEFINE1(close
, unsigned int, fd
)
1012 int retval
= __close_fd(current
->files
, fd
);
1014 /* can't restart close syscall because file table entry was cleared */
1015 if (unlikely(retval
== -ERESTARTSYS
||
1016 retval
== -ERESTARTNOINTR
||
1017 retval
== -ERESTARTNOHAND
||
1018 retval
== -ERESTART_RESTARTBLOCK
))
1023 EXPORT_SYMBOL(sys_close
);
1026 * This routine simulates a hangup on the tty, to arrange that users
1027 * are given clean terminals at login time.
1029 SYSCALL_DEFINE0(vhangup
)
1031 if (capable(CAP_SYS_TTY_CONFIG
)) {
1039 * Called when an inode is about to be open.
1040 * We use this to disallow opening large files on 32bit systems if
1041 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1042 * on this flag in sys_open.
1044 int generic_file_open(struct inode
* inode
, struct file
* filp
)
1046 if (!(filp
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
1051 EXPORT_SYMBOL(generic_file_open
);
1054 * This is used by subsystems that don't want seekable
1055 * file descriptors. The function is not supposed to ever fail, the only
1056 * reason it returns an 'int' and not 'void' is so that it can be plugged
1057 * directly into file_operations structure.
1059 int nonseekable_open(struct inode
*inode
, struct file
*filp
)
1061 filp
->f_mode
&= ~(FMODE_LSEEK
| FMODE_PREAD
| FMODE_PWRITE
);
1065 EXPORT_SYMBOL(nonseekable_open
);