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 #define CREATE_TRACE_POINTS
37 #include <trace/events/fs.h>
39 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
43 struct iattr newattrs;
45 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
49 newattrs.ia_size = length;
50 newattrs.ia_valid = ATTR_SIZE | time_attrs;
52 newattrs.ia_file = filp;
53 newattrs.ia_valid |= ATTR_FILE;
56 /* Remove suid/sgid on truncate too */
57 ret = should_remove_suid(dentry);
59 newattrs.ia_valid |= ret | ATTR_FORCE;
61 mutex_lock(&dentry->d_inode->i_mutex);
62 ret = notify_change(dentry, &newattrs);
63 mutex_unlock(&dentry->d_inode->i_mutex);
66 EXPORT_SYMBOL(do_truncate);
68 static long do_sys_truncate(const char __user *pathname, loff_t length)
75 if (length < 0) /* sorry, but loff_t says... */
78 error = user_path(pathname, &path);
81 inode = path.dentry->d_inode;
83 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
85 if (S_ISDIR(inode->i_mode))
89 if (!S_ISREG(inode->i_mode))
92 error = mnt_want_write(path.mnt);
96 error = inode_permission(inode, MAY_WRITE);
98 goto mnt_drop_write_and_out;
101 if (IS_APPEND(inode))
102 goto mnt_drop_write_and_out;
104 error = get_write_access(inode);
106 goto mnt_drop_write_and_out;
109 * Make sure that there are no leases. get_write_access() protects
110 * against the truncate racing with a lease-granting setlease().
112 error = break_lease(inode, O_WRONLY);
114 goto put_write_and_out;
116 error = locks_verify_truncate(inode, NULL, length);
118 error = security_path_truncate(&path);
120 error = do_truncate(path.dentry, length, 0, NULL);
123 put_write_access(inode);
124 mnt_drop_write_and_out:
125 mnt_drop_write(path.mnt);
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)
139 struct inode * inode;
140 struct dentry *dentry;
152 /* explicitly opened as large or we are on 64-bit box */
153 if (file->f_flags & O_LARGEFILE)
156 dentry = file->f_path.dentry;
157 inode = dentry->d_inode;
159 if (!S_ISREG(inode->i_mode) || !(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 error = locks_verify_truncate(inode, file, length);
173 error = security_path_truncate(&file->f_path);
175 error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
182 SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
184 long ret = do_sys_ftruncate(fd, length, 1);
185 /* avoid REGPARM breakage on x86: */
186 asmlinkage_protect(2, ret, fd, length);
190 /* LFS versions of truncate are only needed on 32 bit machines */
191 #if BITS_PER_LONG == 32
192 SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length)
194 return do_sys_truncate(path, length);
196 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
197 asmlinkage long SyS_truncate64(long path, loff_t length)
199 return SYSC_truncate64((const char __user *) path, length);
201 SYSCALL_ALIAS(sys_truncate64, SyS_truncate64);
204 SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length)
206 long ret = do_sys_ftruncate(fd, length, 0);
207 /* avoid REGPARM breakage on x86: */
208 asmlinkage_protect(2, ret, fd, length);
211 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
212 asmlinkage long SyS_ftruncate64(long fd, loff_t length)
214 return SYSC_ftruncate64((unsigned int) fd, length);
216 SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64);
218 #endif /* BITS_PER_LONG == 32 */
221 int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
223 struct inode *inode = file->f_path.dentry->d_inode;
226 if (offset < 0 || len <= 0)
229 /* Return error if mode is not supported */
230 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
233 /* Punch hole must have keep size set */
234 if ((mode & FALLOC_FL_PUNCH_HOLE) &&
235 !(mode & FALLOC_FL_KEEP_SIZE))
238 if (!(file->f_mode & FMODE_WRITE))
241 /* It's not possible punch hole on append only file */
242 if (mode & FALLOC_FL_PUNCH_HOLE && IS_APPEND(inode))
245 if (IS_IMMUTABLE(inode))
249 * Revalidate the write permissions, in case security policy has
250 * changed since the files were opened.
252 ret = security_file_permission(file, MAY_WRITE);
256 if (S_ISFIFO(inode->i_mode))
260 * Let individual file system decide if it supports preallocation
261 * for directories or not.
263 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
266 /* Check for wrap through zero too */
267 if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
270 if (!file->f_op->fallocate)
273 return file->f_op->fallocate(file, mode, offset, len);
276 SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len)
283 error = do_fallocate(file, mode, offset, len);
290 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
291 asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len)
293 return SYSC_fallocate((int)fd, (int)mode, offset, len);
295 SYSCALL_ALIAS(sys_fallocate, SyS_fallocate);
299 * access() needs to use the real uid/gid, not the effective uid/gid.
300 * We do this by temporarily clearing all FS-related capabilities and
301 * switching the fsuid/fsgid around to the real ones.
303 SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
305 const struct cred *old_cred;
306 struct cred *override_cred;
311 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
314 override_cred = prepare_creds();
318 override_cred->fsuid = override_cred->uid;
319 override_cred->fsgid = override_cred->gid;
321 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
322 /* Clear the capabilities if we switch to a non-root user */
323 if (override_cred->uid)
324 cap_clear(override_cred->cap_effective);
326 override_cred->cap_effective =
327 override_cred->cap_permitted;
330 old_cred = override_creds(override_cred);
332 res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
336 inode = path.dentry->d_inode;
338 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
340 * MAY_EXEC on regular files is denied if the fs is mounted
341 * with the "noexec" flag.
344 if (path.mnt->mnt_flags & MNT_NOEXEC)
345 goto out_path_release;
348 res = inode_permission(inode, mode | MAY_ACCESS);
349 /* SuS v2 requires we report a read only fs too */
350 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
351 goto out_path_release;
353 * This is a rare case where using __mnt_is_readonly()
354 * is OK without a mnt_want/drop_write() pair. Since
355 * no actual write to the fs is performed here, we do
356 * not need to telegraph to that to anyone.
358 * By doing this, we accept that this access is
359 * inherently racy and know that the fs may change
360 * state before we even see this result.
362 if (__mnt_is_readonly(path.mnt))
368 revert_creds(old_cred);
369 put_cred(override_cred);
373 SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
375 return sys_faccessat(AT_FDCWD, filename, mode);
378 SYSCALL_DEFINE1(chdir, const char __user *, filename)
383 error = user_path_dir(filename, &path);
387 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
391 set_fs_pwd(current->fs, &path);
399 SYSCALL_DEFINE1(fchdir, unsigned int, fd)
410 inode = file->f_path.dentry->d_inode;
413 if (!S_ISDIR(inode->i_mode))
416 error = inode_permission(inode, MAY_EXEC | MAY_CHDIR);
418 set_fs_pwd(current->fs, &file->f_path);
425 SYSCALL_DEFINE1(chroot, const char __user *, filename)
430 error = user_path_dir(filename, &path);
434 error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
439 if (!capable(CAP_SYS_CHROOT))
441 error = security_path_chroot(&path);
445 set_fs_root(current->fs, &path);
453 static int chmod_common(struct path *path, umode_t mode)
455 struct inode *inode = path->dentry->d_inode;
456 struct iattr newattrs;
459 error = mnt_want_write(path->mnt);
462 mutex_lock(&inode->i_mutex);
463 error = security_path_chmod(path, mode);
466 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
467 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
468 error = notify_change(path->dentry, &newattrs);
470 mutex_unlock(&inode->i_mutex);
471 mnt_drop_write(path->mnt);
475 SYSCALL_DEFINE2(fchmod, unsigned int, fd, mode_t, mode)
482 audit_inode(NULL, file->f_path.dentry);
483 err = chmod_common(&file->f_path, mode);
489 SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, mode_t, mode)
494 error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
496 error = chmod_common(&path, mode);
502 SYSCALL_DEFINE2(chmod, const char __user *, filename, mode_t, mode)
504 return sys_fchmodat(AT_FDCWD, filename, mode);
507 static int chown_common(struct path *path, uid_t user, gid_t group)
509 struct inode *inode = path->dentry->d_inode;
511 struct iattr newattrs;
513 newattrs.ia_valid = ATTR_CTIME;
514 if (user != (uid_t) -1) {
515 newattrs.ia_valid |= ATTR_UID;
516 newattrs.ia_uid = user;
518 if (group != (gid_t) -1) {
519 newattrs.ia_valid |= ATTR_GID;
520 newattrs.ia_gid = group;
522 if (!S_ISDIR(inode->i_mode))
524 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
525 mutex_lock(&inode->i_mutex);
526 error = security_path_chown(path, user, group);
528 error = notify_change(path->dentry, &newattrs);
529 mutex_unlock(&inode->i_mutex);
534 SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
539 error = user_path(filename, &path);
542 error = mnt_want_write(path.mnt);
545 error = chown_common(&path, user, group);
546 mnt_drop_write(path.mnt);
553 SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
554 gid_t, group, int, flag)
560 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
563 lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
564 if (flag & AT_EMPTY_PATH)
565 lookup_flags |= LOOKUP_EMPTY;
566 error = user_path_at(dfd, filename, lookup_flags, &path);
569 error = mnt_want_write(path.mnt);
572 error = chown_common(&path, user, group);
573 mnt_drop_write(path.mnt);
580 SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
585 error = user_lpath(filename, &path);
588 error = mnt_want_write(path.mnt);
591 error = chown_common(&path, user, group);
592 mnt_drop_write(path.mnt);
599 SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
603 struct dentry * dentry;
609 error = mnt_want_write_file(file);
612 dentry = file->f_path.dentry;
613 audit_inode(NULL, dentry);
614 error = chown_common(&file->f_path, user, group);
615 mnt_drop_write(file->f_path.mnt);
623 * You have to be very careful that these write
624 * counts get cleaned up in error cases and
625 * upon __fput(). This should probably never
626 * be called outside of __dentry_open().
628 static inline int __get_file_write_access(struct inode *inode,
629 struct vfsmount *mnt)
632 error = get_write_access(inode);
636 * Do not take mount writer counts on
637 * special files since no writes to
638 * the mount itself will occur.
640 if (!special_file(inode->i_mode)) {
642 * Balanced in __fput()
644 error = mnt_want_write(mnt);
646 put_write_access(inode);
651 static struct file *__dentry_open(struct path *path, struct file *f,
652 int (*open)(struct inode *, struct file *),
653 const struct cred *cred)
655 static const struct file_operations empty_fops = {};
660 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
661 FMODE_PREAD | FMODE_PWRITE;
663 if (unlikely(f->f_flags & O_PATH))
664 f->f_mode = FMODE_PATH;
666 inode = path->dentry->d_inode;
667 if (f->f_mode & FMODE_WRITE) {
668 error = __get_file_write_access(inode, path->mnt);
671 if (!special_file(inode->i_mode))
675 f->f_mapping = inode->i_mapping;
678 file_sb_list_add(f, inode->i_sb);
680 if (unlikely(f->f_mode & FMODE_PATH)) {
681 f->f_op = &empty_fops;
685 f->f_op = fops_get(inode->i_fop);
687 error = security_dentry_open(f, cred);
691 error = break_lease(inode, f->f_flags);
695 if (!open && f->f_op)
696 open = f->f_op->open;
698 error = open(inode, f);
702 if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
703 i_readcount_inc(inode);
705 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
707 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
709 /* NB: we're sure to have correct a_ops only after f_op->open */
710 if (f->f_flags & O_DIRECT) {
711 if (!f->f_mapping->a_ops ||
712 ((!f->f_mapping->a_ops->direct_IO) &&
713 (!f->f_mapping->a_ops->get_xip_mem))) {
715 f = ERR_PTR(-EINVAL);
723 if (f->f_mode & FMODE_WRITE) {
724 put_write_access(inode);
725 if (!special_file(inode->i_mode)) {
727 * We don't consider this a real
728 * mnt_want/drop_write() pair
729 * because it all happenend right
730 * here, so just reset the state.
733 mnt_drop_write(path->mnt);
737 f->f_path.dentry = NULL;
738 f->f_path.mnt = NULL;
742 return ERR_PTR(error);
746 * lookup_instantiate_filp - instantiates the open intent filp
747 * @nd: pointer to nameidata
748 * @dentry: pointer to dentry
749 * @open: open callback
751 * Helper for filesystems that want to use lookup open intents and pass back
752 * a fully instantiated struct file to the caller.
753 * This function is meant to be called from within a filesystem's
755 * Beware of calling it for non-regular files! Those ->open methods might block
756 * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
757 * leading to a deadlock, as nobody can open that fifo anymore, because
758 * another process to open fifo will block on locked parent when doing lookup).
759 * Note that in case of error, nd->intent.open.file is destroyed, but the
760 * path information remains valid.
761 * If the open callback is set to NULL, then the standard f_op->open()
762 * filesystem callback is substituted.
764 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
765 int (*open)(struct inode *, struct file *))
767 struct path path = { .dentry = dentry, .mnt = nd->path.mnt };
768 const struct cred *cred = current_cred();
770 if (IS_ERR(nd->intent.open.file))
774 nd->intent.open.file = __dentry_open(&path, nd->intent.open.file,
777 return nd->intent.open.file;
779 release_open_intent(nd);
780 nd->intent.open.file = ERR_CAST(dentry);
783 EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
786 * nameidata_to_filp - convert a nameidata to an open filp.
787 * @nd: pointer to nameidata
790 * Note that this function destroys the original nameidata
792 struct file *nameidata_to_filp(struct nameidata *nd)
794 const struct cred *cred = current_cred();
797 /* Pick up the filp from the open intent */
798 filp = nd->intent.open.file;
799 nd->intent.open.file = NULL;
801 /* Has the filesystem initialised the file for us? */
802 if (filp->f_path.dentry == NULL)
803 filp = vfs_open(&nd->path, filp, cred);
809 * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
812 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
813 const struct cred *cred)
817 struct path path = { .dentry = dentry, .mnt = mnt };
819 validate_creds(cred);
821 /* We must always pass in a valid mount pointer. */
824 ret = ERR_PTR(-ENFILE);
825 f = get_empty_filp();
828 ret = vfs_open(&path, f, cred);
834 EXPORT_SYMBOL(dentry_open);
837 * vfs_open - open the file at the given path
838 * @path: path to open
839 * @filp: newly allocated file with f_flag initialized
840 * @cred: credentials to use
842 * Open the file. If successful, the returned file will have acquired
843 * an additional reference for path.
845 struct file *vfs_open(struct path *path, struct file *filp,
846 const struct cred *cred)
848 struct inode *inode = path->dentry->d_inode;
850 if (inode->i_op->open)
851 return inode->i_op->open(path->dentry, filp, cred);
853 return __dentry_open(path, filp, NULL, cred);
855 EXPORT_SYMBOL(vfs_open);
857 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
859 struct fdtable *fdt = files_fdtable(files);
860 __FD_CLR(fd, fdt->open_fds);
861 if (fd < files->next_fd)
865 void put_unused_fd(unsigned int fd)
867 struct files_struct *files = current->files;
868 spin_lock(&files->file_lock);
869 __put_unused_fd(files, fd);
870 spin_unlock(&files->file_lock);
873 EXPORT_SYMBOL(put_unused_fd);
876 * Install a file pointer in the fd array.
878 * The VFS is full of places where we drop the files lock between
879 * setting the open_fds bitmap and installing the file in the file
880 * array. At any such point, we are vulnerable to a dup2() race
881 * installing a file in the array before us. We need to detect this and
882 * fput() the struct file we are about to overwrite in this case.
884 * It should never happen - if we allow dup2() do it, _really_ bad things
888 void fd_install(unsigned int fd, struct file *file)
890 struct files_struct *files = current->files;
892 spin_lock(&files->file_lock);
893 fdt = files_fdtable(files);
894 BUG_ON(fdt->fd[fd] != NULL);
895 rcu_assign_pointer(fdt->fd[fd], file);
896 spin_unlock(&files->file_lock);
899 EXPORT_SYMBOL(fd_install);
901 static inline int build_open_flags(int flags, int mode, struct open_flags *op)
903 int lookup_flags = 0;
906 if (!(flags & O_CREAT))
910 /* Must never be set by userspace */
911 flags &= ~FMODE_NONOTIFY;
914 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
915 * check for O_DSYNC if the need any syncing at all we enforce it's
916 * always set instead of having to deal with possibly weird behaviour
917 * for malicious applications setting only __O_SYNC.
919 if (flags & __O_SYNC)
923 * If we have O_PATH in the open flag. Then we
924 * cannot have anything other than the below set of flags
926 if (flags & O_PATH) {
927 flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
930 acc_mode = MAY_OPEN | ACC_MODE(flags);
933 op->open_flag = flags;
935 /* O_TRUNC implies we need access checks for write permissions */
937 acc_mode |= MAY_WRITE;
939 /* Allow the LSM permission hook to distinguish append
940 access from general write access. */
941 if (flags & O_APPEND)
942 acc_mode |= MAY_APPEND;
944 op->acc_mode = acc_mode;
946 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
948 if (flags & O_CREAT) {
949 op->intent |= LOOKUP_CREATE;
951 op->intent |= LOOKUP_EXCL;
954 if (flags & O_DIRECTORY)
955 lookup_flags |= LOOKUP_DIRECTORY;
956 if (!(flags & O_NOFOLLOW))
957 lookup_flags |= LOOKUP_FOLLOW;
962 * filp_open - open file and return file pointer
964 * @filename: path to open
965 * @flags: open flags as per the open(2) second argument
966 * @mode: mode for the new file if O_CREAT is set, else ignored
968 * This is the helper to open a file from kernelspace if you really
969 * have to. But in generally you should not do this, so please move
970 * along, nothing to see here..
972 struct file *filp_open(const char *filename, int flags, int mode)
974 struct open_flags op;
975 int lookup = build_open_flags(flags, mode, &op);
976 return do_filp_open(AT_FDCWD, filename, &op, lookup);
978 EXPORT_SYMBOL(filp_open);
980 struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
981 const char *filename, int flags)
983 struct open_flags op;
984 int lookup = build_open_flags(flags, 0, &op);
986 return ERR_PTR(-EINVAL);
987 if (!filename && (flags & O_DIRECTORY))
988 if (!dentry->d_inode->i_op->lookup)
989 return ERR_PTR(-ENOTDIR);
990 return do_file_open_root(dentry, mnt, filename, &op, lookup);
992 EXPORT_SYMBOL(file_open_root);
994 long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
996 struct open_flags op;
997 int lookup = build_open_flags(flags, mode, &op);
998 char *tmp = getname(filename);
999 int fd = PTR_ERR(tmp);
1002 fd = get_unused_fd_flags(flags);
1004 struct file *f = do_filp_open(dfd, tmp, &op, lookup);
1011 trace_do_sys_open(tmp, flags, mode);
1019 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
1023 if (force_o_largefile())
1024 flags |= O_LARGEFILE;
1026 ret = do_sys_open(AT_FDCWD, filename, flags, mode);
1027 /* avoid REGPARM breakage on x86: */
1028 asmlinkage_protect(3, ret, filename, flags, mode);
1032 SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
1037 if (force_o_largefile())
1038 flags |= O_LARGEFILE;
1040 ret = do_sys_open(dfd, filename, flags, mode);
1041 /* avoid REGPARM breakage on x86: */
1042 asmlinkage_protect(4, ret, dfd, filename, flags, mode);
1049 * For backward compatibility? Maybe this should be moved
1050 * into arch/i386 instead?
1052 SYSCALL_DEFINE2(creat, const char __user *, pathname, int, mode)
1054 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1060 * "id" is the POSIX thread ID. We use the
1061 * files pointer for this..
1063 int filp_close(struct file *filp, fl_owner_t id)
1067 if (!file_count(filp)) {
1068 printk(KERN_ERR "VFS: Close: file count is 0\n");
1072 if (filp->f_op && filp->f_op->flush)
1073 retval = filp->f_op->flush(filp, id);
1075 if (likely(!(filp->f_mode & FMODE_PATH))) {
1076 dnotify_flush(filp, id);
1077 locks_remove_posix(filp, id);
1083 EXPORT_SYMBOL(filp_close);
1086 * Careful here! We test whether the file pointer is NULL before
1087 * releasing the fd. This ensures that one clone task can't release
1088 * an fd while another clone is opening it.
1090 SYSCALL_DEFINE1(close, unsigned int, fd)
1093 struct files_struct *files = current->files;
1094 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 retval = filp_close(filp, files);
1110 /* can't restart close syscall because file table entry was cleared */
1111 if (unlikely(retval == -ERESTARTSYS ||
1112 retval == -ERESTARTNOINTR ||
1113 retval == -ERESTARTNOHAND ||
1114 retval == -ERESTART_RESTARTBLOCK))
1120 spin_unlock(&files->file_lock);
1123 EXPORT_SYMBOL(sys_close);
1126 * This routine simulates a hangup on the tty, to arrange that users
1127 * are given clean terminals at login time.
1129 SYSCALL_DEFINE0(vhangup)
1131 if (capable(CAP_SYS_TTY_CONFIG)) {
1139 * Called when an inode is about to be open.
1140 * We use this to disallow opening large files on 32bit systems if
1141 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1142 * on this flag in sys_open.
1144 int generic_file_open(struct inode * inode, struct file * filp)
1146 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1151 EXPORT_SYMBOL(generic_file_open);
1154 * This is used by subsystems that don't want seekable
1155 * file descriptors. The function is not supposed to ever fail, the only
1156 * reason it returns an 'int' and not 'void' is so that it can be plugged
1157 * directly into file_operations structure.
1159 int nonseekable_open(struct inode *inode, struct file *filp)
1161 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1165 EXPORT_SYMBOL(nonseekable_open);