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>
33 #include <linux/compat.h>
37 int do_truncate2(struct vfsmount *mnt, struct dentry *dentry, loff_t length,
38 unsigned int time_attrs, struct file *filp)
41 struct iattr newattrs;
43 /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
47 newattrs.ia_size = length;
48 newattrs.ia_valid = ATTR_SIZE | time_attrs;
50 newattrs.ia_file = filp;
51 newattrs.ia_valid |= ATTR_FILE;
54 /* Remove suid, sgid, and file capabilities on truncate too */
55 ret = dentry_needs_remove_privs(dentry);
59 newattrs.ia_valid |= ret | ATTR_FORCE;
61 inode_lock(dentry->d_inode);
62 /* Note any delegations or leases have already been broken: */
63 ret = notify_change2(mnt, dentry, &newattrs, NULL);
64 inode_unlock(dentry->d_inode);
67 int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
70 return do_truncate2(NULL, dentry, length, time_attrs, filp);
73 long vfs_truncate(const struct path *path, loff_t length)
77 struct dentry *upperdentry;
80 inode = path->dentry->d_inode;
83 /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
84 if (S_ISDIR(inode->i_mode))
86 if (!S_ISREG(inode->i_mode))
89 error = mnt_want_write(path->mnt);
93 error = inode_permission2(mnt, inode, MAY_WRITE);
95 goto mnt_drop_write_and_out;
99 goto mnt_drop_write_and_out;
102 * If this is an overlayfs then do as if opening the file so we get
103 * write access on the upper inode, not on the overlay inode. For
104 * non-overlay filesystems d_real() is an identity function.
106 upperdentry = d_real(path->dentry, NULL, O_WRONLY);
107 error = PTR_ERR(upperdentry);
108 if (IS_ERR(upperdentry))
109 goto mnt_drop_write_and_out;
111 error = get_write_access(upperdentry->d_inode);
113 goto mnt_drop_write_and_out;
116 * Make sure that there are no leases. get_write_access() protects
117 * against the truncate racing with a lease-granting setlease().
119 error = break_lease(inode, O_WRONLY);
121 goto put_write_and_out;
123 error = locks_verify_truncate(inode, NULL, length);
125 error = security_path_truncate(path);
127 error = do_truncate2(mnt, path->dentry, length, 0, NULL);
130 put_write_access(upperdentry->d_inode);
131 mnt_drop_write_and_out:
132 mnt_drop_write(path->mnt);
136 EXPORT_SYMBOL_GPL(vfs_truncate);
138 static long do_sys_truncate(const char __user *pathname, loff_t length)
140 unsigned int lookup_flags = LOOKUP_FOLLOW;
144 if (length < 0) /* sorry, but loff_t says... */
148 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
150 error = vfs_truncate(&path, length);
153 if (retry_estale(error, lookup_flags)) {
154 lookup_flags |= LOOKUP_REVAL;
160 SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
162 return do_sys_truncate(path, length);
166 COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
168 return do_sys_truncate(path, length);
172 static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
175 struct dentry *dentry;
176 struct vfsmount *mnt;
188 /* explicitly opened as large or we are on 64-bit box */
189 if (f.file->f_flags & O_LARGEFILE)
192 dentry = f.file->f_path.dentry;
193 mnt = f.file->f_path.mnt;
194 inode = dentry->d_inode;
196 if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
200 /* Cannot ftruncate over 2^31 bytes without large file support */
201 if (small && length > MAX_NON_LFS)
205 if (IS_APPEND(inode))
208 sb_start_write(inode->i_sb);
209 error = locks_verify_truncate(inode, f.file, length);
211 error = security_path_truncate(&f.file->f_path);
213 error = do_truncate2(mnt, dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
214 sb_end_write(inode->i_sb);
221 SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
223 return do_sys_ftruncate(fd, length, 1);
227 COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
229 return do_sys_ftruncate(fd, length, 1);
233 /* LFS versions of truncate are only needed on 32 bit machines */
234 #if BITS_PER_LONG == 32
235 SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
237 return do_sys_truncate(path, length);
240 SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
242 return do_sys_ftruncate(fd, length, 0);
244 #endif /* BITS_PER_LONG == 32 */
247 int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
249 struct inode *inode = file_inode(file);
252 if (offset < 0 || len <= 0)
255 /* Return error if mode is not supported */
256 if (mode & ~FALLOC_FL_SUPPORTED_MASK)
259 /* Punch hole and zero range are mutually exclusive */
260 if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
261 (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
264 /* Punch hole must have keep size set */
265 if ((mode & FALLOC_FL_PUNCH_HOLE) &&
266 !(mode & FALLOC_FL_KEEP_SIZE))
269 /* Collapse range should only be used exclusively. */
270 if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
271 (mode & ~FALLOC_FL_COLLAPSE_RANGE))
274 /* Insert range should only be used exclusively. */
275 if ((mode & FALLOC_FL_INSERT_RANGE) &&
276 (mode & ~FALLOC_FL_INSERT_RANGE))
279 /* Unshare range should only be used with allocate mode. */
280 if ((mode & FALLOC_FL_UNSHARE_RANGE) &&
281 (mode & ~(FALLOC_FL_UNSHARE_RANGE | FALLOC_FL_KEEP_SIZE)))
284 if (!(file->f_mode & FMODE_WRITE))
288 * We can only allow pure fallocate on append only files
290 if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
293 if (IS_IMMUTABLE(inode))
297 * We cannot allow any fallocate operation on an active swapfile
299 if (IS_SWAPFILE(inode))
303 * Revalidate the write permissions, in case security policy has
304 * changed since the files were opened.
306 ret = security_file_permission(file, MAY_WRITE);
310 if (S_ISFIFO(inode->i_mode))
314 * Let individual file system decide if it supports preallocation
315 * for directories or not.
317 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode) &&
318 !S_ISBLK(inode->i_mode))
321 /* Check for wrap through zero too */
322 if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
325 if (!file->f_op->fallocate)
328 sb_start_write(inode->i_sb);
329 ret = file->f_op->fallocate(file, mode, offset, len);
332 * Create inotify and fanotify events.
334 * To keep the logic simple always create events if fallocate succeeds.
335 * This implies that events are even created if the file size remains
336 * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
339 fsnotify_modify(file);
341 sb_end_write(inode->i_sb);
344 EXPORT_SYMBOL_GPL(vfs_fallocate);
346 SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
348 struct fd f = fdget(fd);
352 error = vfs_fallocate(f.file, mode, offset, len);
359 * access() needs to use the real uid/gid, not the effective uid/gid.
360 * We do this by temporarily clearing all FS-related capabilities and
361 * switching the fsuid/fsgid around to the real ones.
363 SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
365 const struct cred *old_cred;
366 struct cred *override_cred;
369 struct vfsmount *mnt;
371 unsigned int lookup_flags = LOOKUP_FOLLOW;
373 if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
376 override_cred = prepare_creds();
380 override_cred->fsuid = override_cred->uid;
381 override_cred->fsgid = override_cred->gid;
383 if (!issecure(SECURE_NO_SETUID_FIXUP)) {
384 /* Clear the capabilities if we switch to a non-root user */
385 kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
386 if (!uid_eq(override_cred->uid, root_uid))
387 cap_clear(override_cred->cap_effective);
389 override_cred->cap_effective =
390 override_cred->cap_permitted;
394 * The new set of credentials can *only* be used in
395 * task-synchronous circumstances, and does not need
396 * RCU freeing, unless somebody then takes a separate
399 * NOTE! This is _only_ true because this credential
400 * is used purely for override_creds() that installs
401 * it as the subjective cred. Other threads will be
402 * accessing ->real_cred, not the subjective cred.
404 * If somebody _does_ make a copy of this (using the
405 * 'get_current_cred()' function), that will clear the
406 * non_rcu field, because now that other user may be
407 * expecting RCU freeing. But normal thread-synchronous
408 * cred accesses will keep things non-RCY.
410 override_cred->non_rcu = 1;
412 old_cred = override_creds(override_cred);
414 res = user_path_at(dfd, filename, lookup_flags, &path);
418 inode = d_backing_inode(path.dentry);
421 if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
423 * MAY_EXEC on regular files is denied if the fs is mounted
424 * with the "noexec" flag.
427 if (path_noexec(&path))
428 goto out_path_release;
431 res = inode_permission2(mnt, inode, mode | MAY_ACCESS);
432 /* SuS v2 requires we report a read only fs too */
433 if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
434 goto out_path_release;
436 * This is a rare case where using __mnt_is_readonly()
437 * is OK without a mnt_want/drop_write() pair. Since
438 * no actual write to the fs is performed here, we do
439 * not need to telegraph to that to anyone.
441 * By doing this, we accept that this access is
442 * inherently racy and know that the fs may change
443 * state before we even see this result.
445 if (__mnt_is_readonly(path.mnt))
450 if (retry_estale(res, lookup_flags)) {
451 lookup_flags |= LOOKUP_REVAL;
455 revert_creds(old_cred);
456 put_cred(override_cred);
460 SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
462 return sys_faccessat(AT_FDCWD, filename, mode);
465 SYSCALL_DEFINE1(chdir, const char __user *, filename)
469 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
471 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
475 error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
479 set_fs_pwd(current->fs, &path);
483 if (retry_estale(error, lookup_flags)) {
484 lookup_flags |= LOOKUP_REVAL;
491 SYSCALL_DEFINE1(fchdir, unsigned int, fd)
493 struct fd f = fdget_raw(fd);
495 struct vfsmount *mnt;
502 inode = file_inode(f.file);
503 mnt = f.file->f_path.mnt;
506 if (!S_ISDIR(inode->i_mode))
509 error = inode_permission2(mnt, inode, MAY_EXEC | MAY_CHDIR);
511 set_fs_pwd(current->fs, &f.file->f_path);
518 SYSCALL_DEFINE1(chroot, const char __user *, filename)
522 unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
524 error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
528 error = inode_permission2(path.mnt, path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
533 if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
535 error = security_path_chroot(&path);
539 set_fs_root(current->fs, &path);
543 if (retry_estale(error, lookup_flags)) {
544 lookup_flags |= LOOKUP_REVAL;
551 static int chmod_common(const struct path *path, umode_t mode)
553 struct inode *inode = path->dentry->d_inode;
554 struct inode *delegated_inode = NULL;
555 struct iattr newattrs;
558 error = mnt_want_write(path->mnt);
563 error = security_path_chmod(path, mode);
566 newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
567 newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
568 error = notify_change2(path->mnt, path->dentry, &newattrs, &delegated_inode);
571 if (delegated_inode) {
572 error = break_deleg_wait(&delegated_inode);
576 mnt_drop_write(path->mnt);
580 SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
582 struct fd f = fdget(fd);
587 err = chmod_common(&f.file->f_path, mode);
593 SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
597 unsigned int lookup_flags = LOOKUP_FOLLOW;
599 error = user_path_at(dfd, filename, lookup_flags, &path);
601 error = chmod_common(&path, mode);
603 if (retry_estale(error, lookup_flags)) {
604 lookup_flags |= LOOKUP_REVAL;
611 SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
613 return sys_fchmodat(AT_FDCWD, filename, mode);
616 static int chown_common(const struct path *path, uid_t user, gid_t group)
618 struct inode *inode = path->dentry->d_inode;
619 struct inode *delegated_inode = NULL;
621 struct iattr newattrs;
625 uid = make_kuid(current_user_ns(), user);
626 gid = make_kgid(current_user_ns(), group);
629 newattrs.ia_valid = ATTR_CTIME;
630 if (user != (uid_t) -1) {
633 newattrs.ia_valid |= ATTR_UID;
634 newattrs.ia_uid = uid;
636 if (group != (gid_t) -1) {
639 newattrs.ia_valid |= ATTR_GID;
640 newattrs.ia_gid = gid;
642 if (!S_ISDIR(inode->i_mode))
644 ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
646 error = security_path_chown(path, uid, gid);
648 error = notify_change2(path->mnt, path->dentry, &newattrs, &delegated_inode);
650 if (delegated_inode) {
651 error = break_deleg_wait(&delegated_inode);
658 SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
659 gid_t, group, int, flag)
665 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
668 lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
669 if (flag & AT_EMPTY_PATH)
670 lookup_flags |= LOOKUP_EMPTY;
672 error = user_path_at(dfd, filename, lookup_flags, &path);
675 error = mnt_want_write(path.mnt);
678 error = chown_common(&path, user, group);
679 mnt_drop_write(path.mnt);
682 if (retry_estale(error, lookup_flags)) {
683 lookup_flags |= LOOKUP_REVAL;
690 SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
692 return sys_fchownat(AT_FDCWD, filename, user, group, 0);
695 SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
697 return sys_fchownat(AT_FDCWD, filename, user, group,
698 AT_SYMLINK_NOFOLLOW);
701 SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
703 struct fd f = fdget(fd);
709 error = mnt_want_write_file(f.file);
713 error = chown_common(&f.file->f_path, user, group);
714 mnt_drop_write_file(f.file);
721 int open_check_o_direct(struct file *f)
723 /* NB: we're sure to have correct a_ops only after f_op->open */
724 if (f->f_flags & O_DIRECT) {
725 if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
731 static int do_dentry_open(struct file *f,
733 int (*open)(struct inode *, struct file *),
734 const struct cred *cred)
736 static const struct file_operations empty_fops = {};
739 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
740 FMODE_PREAD | FMODE_PWRITE;
742 path_get(&f->f_path);
744 f->f_mapping = inode->i_mapping;
746 if (unlikely(f->f_flags & O_PATH)) {
747 f->f_mode = FMODE_PATH;
748 f->f_op = &empty_fops;
752 /* Any file opened for execve()/uselib() has to be a regular file. */
753 if (unlikely(f->f_flags & FMODE_EXEC && !S_ISREG(inode->i_mode))) {
758 if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
759 error = get_write_access(inode);
762 error = __mnt_want_write(f->f_path.mnt);
763 if (unlikely(error)) {
764 put_write_access(inode);
767 f->f_mode |= FMODE_WRITER;
770 /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
771 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))
772 f->f_mode |= FMODE_ATOMIC_POS;
774 f->f_op = fops_get(inode->i_fop);
775 if (unlikely(WARN_ON(!f->f_op))) {
780 error = security_file_open(f, cred);
784 error = break_lease(locks_inode(f), f->f_flags);
789 open = f->f_op->open;
791 error = open(inode, f);
795 if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
796 i_readcount_inc(inode);
797 if ((f->f_mode & FMODE_READ) &&
798 likely(f->f_op->read || f->f_op->read_iter))
799 f->f_mode |= FMODE_CAN_READ;
800 if ((f->f_mode & FMODE_WRITE) &&
801 likely(f->f_op->write || f->f_op->write_iter))
802 f->f_mode |= FMODE_CAN_WRITE;
804 f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
806 file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
812 if (f->f_mode & FMODE_WRITER) {
813 put_write_access(inode);
814 __mnt_drop_write(f->f_path.mnt);
817 path_put(&f->f_path);
818 f->f_path.mnt = NULL;
819 f->f_path.dentry = NULL;
825 * finish_open - finish opening a file
826 * @file: file pointer
827 * @dentry: pointer to dentry
828 * @open: open callback
829 * @opened: state of open
831 * This can be used to finish opening a file passed to i_op->atomic_open().
833 * If the open callback is set to NULL, then the standard f_op->open()
834 * filesystem callback is substituted.
836 * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
837 * the return value of d_splice_alias(), then the caller needs to perform dput()
838 * on it after finish_open().
840 * On successful return @file is a fully instantiated open file. After this, if
841 * an error occurs in ->atomic_open(), it needs to clean up with fput().
843 * Returns zero on success or -errno if the open failed.
845 int finish_open(struct file *file, struct dentry *dentry,
846 int (*open)(struct inode *, struct file *),
850 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
852 file->f_path.dentry = dentry;
853 error = do_dentry_open(file, d_backing_inode(dentry), open,
856 *opened |= FILE_OPENED;
860 EXPORT_SYMBOL(finish_open);
863 * finish_no_open - finish ->atomic_open() without opening the file
865 * @file: file pointer
866 * @dentry: dentry or NULL (as returned from ->lookup())
868 * This can be used to set the result of a successful lookup in ->atomic_open().
870 * NB: unlike finish_open() this function does consume the dentry reference and
871 * the caller need not dput() it.
873 * Returns "1" which must be the return value of ->atomic_open() after having
874 * called this function.
876 int finish_no_open(struct file *file, struct dentry *dentry)
878 file->f_path.dentry = dentry;
881 EXPORT_SYMBOL(finish_no_open);
883 char *file_path(struct file *filp, char *buf, int buflen)
885 return d_path(&filp->f_path, buf, buflen);
887 EXPORT_SYMBOL(file_path);
890 * vfs_open - open the file at the given path
891 * @path: path to open
892 * @file: newly allocated file with f_flag initialized
893 * @cred: credentials to use
895 int vfs_open(const struct path *path, struct file *file,
896 const struct cred *cred)
898 struct dentry *dentry = d_real(path->dentry, NULL, file->f_flags);
901 return PTR_ERR(dentry);
903 file->f_path = *path;
904 return do_dentry_open(file, d_backing_inode(dentry), NULL, cred);
907 struct file *dentry_open(const struct path *path, int flags,
908 const struct cred *cred)
913 validate_creds(cred);
915 /* We must always pass in a valid mount pointer. */
918 f = get_empty_filp();
921 error = vfs_open(path, f, cred);
923 /* from now on we need fput() to dispose of f */
924 error = open_check_o_direct(f);
936 EXPORT_SYMBOL(dentry_open);
938 static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
940 int lookup_flags = 0;
941 int acc_mode = ACC_MODE(flags);
944 * Clear out all open flags we don't know about so that we don't report
945 * them in fcntl(F_GETFD) or similar interfaces.
947 flags &= VALID_OPEN_FLAGS;
949 if (flags & (O_CREAT | __O_TMPFILE))
950 op->mode = (mode & S_IALLUGO) | S_IFREG;
954 /* Must never be set by userspace */
955 flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
958 * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
959 * check for O_DSYNC if the need any syncing at all we enforce it's
960 * always set instead of having to deal with possibly weird behaviour
961 * for malicious applications setting only __O_SYNC.
963 if (flags & __O_SYNC)
966 if (flags & __O_TMPFILE) {
967 if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
969 if (!(acc_mode & MAY_WRITE))
971 } else if (flags & O_PATH) {
973 * If we have O_PATH in the open flag. Then we
974 * cannot have anything other than the below set of flags
976 flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
980 op->open_flag = flags;
982 /* O_TRUNC implies we need access checks for write permissions */
984 acc_mode |= MAY_WRITE;
986 /* Allow the LSM permission hook to distinguish append
987 access from general write access. */
988 if (flags & O_APPEND)
989 acc_mode |= MAY_APPEND;
991 op->acc_mode = acc_mode;
993 op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
995 if (flags & O_CREAT) {
996 op->intent |= LOOKUP_CREATE;
998 op->intent |= LOOKUP_EXCL;
1001 if (flags & O_DIRECTORY)
1002 lookup_flags |= LOOKUP_DIRECTORY;
1003 if (!(flags & O_NOFOLLOW))
1004 lookup_flags |= LOOKUP_FOLLOW;
1005 op->lookup_flags = lookup_flags;
1010 * file_open_name - open file and return file pointer
1012 * @name: struct filename containing path to open
1013 * @flags: open flags as per the open(2) second argument
1014 * @mode: mode for the new file if O_CREAT is set, else ignored
1016 * This is the helper to open a file from kernelspace if you really
1017 * have to. But in generally you should not do this, so please move
1018 * along, nothing to see here..
1020 struct file *file_open_name(struct filename *name, int flags, umode_t mode)
1022 struct open_flags op;
1023 int err = build_open_flags(flags, mode, &op);
1024 return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
1028 * filp_open - open file and return file pointer
1030 * @filename: path to open
1031 * @flags: open flags as per the open(2) second argument
1032 * @mode: mode for the new file if O_CREAT is set, else ignored
1034 * This is the helper to open a file from kernelspace if you really
1035 * have to. But in generally you should not do this, so please move
1036 * along, nothing to see here..
1038 struct file *filp_open(const char *filename, int flags, umode_t mode)
1040 struct filename *name = getname_kernel(filename);
1041 struct file *file = ERR_CAST(name);
1043 if (!IS_ERR(name)) {
1044 file = file_open_name(name, flags, mode);
1049 EXPORT_SYMBOL(filp_open);
1051 struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
1052 const char *filename, int flags, umode_t mode)
1054 struct open_flags op;
1055 int err = build_open_flags(flags, mode, &op);
1057 return ERR_PTR(err);
1058 return do_file_open_root(dentry, mnt, filename, &op);
1060 EXPORT_SYMBOL(file_open_root);
1062 struct file *filp_clone_open(struct file *oldfile)
1067 file = get_empty_filp();
1071 file->f_flags = oldfile->f_flags;
1072 retval = vfs_open(&oldfile->f_path, file, oldfile->f_cred);
1075 return ERR_PTR(retval);
1080 EXPORT_SYMBOL(filp_clone_open);
1082 long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
1084 struct open_flags op;
1085 int fd = build_open_flags(flags, mode, &op);
1086 struct filename *tmp;
1091 tmp = getname(filename);
1093 return PTR_ERR(tmp);
1095 fd = get_unused_fd_flags(flags);
1097 struct file *f = do_filp_open(dfd, tmp, &op);
1110 SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1112 if (force_o_largefile())
1113 flags |= O_LARGEFILE;
1115 return do_sys_open(AT_FDCWD, filename, flags, mode);
1118 SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
1121 if (force_o_largefile())
1122 flags |= O_LARGEFILE;
1124 return do_sys_open(dfd, filename, flags, mode);
1130 * For backward compatibility? Maybe this should be moved
1131 * into arch/i386 instead?
1133 SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
1135 return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
1141 * "id" is the POSIX thread ID. We use the
1142 * files pointer for this..
1144 int filp_close(struct file *filp, fl_owner_t id)
1148 if (!file_count(filp)) {
1149 printk(KERN_ERR "VFS: Close: file count is 0\n");
1153 if (filp->f_op->flush)
1154 retval = filp->f_op->flush(filp, id);
1156 if (likely(!(filp->f_mode & FMODE_PATH))) {
1157 dnotify_flush(filp, id);
1158 locks_remove_posix(filp, id);
1164 EXPORT_SYMBOL(filp_close);
1167 * Careful here! We test whether the file pointer is NULL before
1168 * releasing the fd. This ensures that one clone task can't release
1169 * an fd while another clone is opening it.
1171 SYSCALL_DEFINE1(close, unsigned int, fd)
1173 int retval = __close_fd(current->files, fd);
1175 /* can't restart close syscall because file table entry was cleared */
1176 if (unlikely(retval == -ERESTARTSYS ||
1177 retval == -ERESTARTNOINTR ||
1178 retval == -ERESTARTNOHAND ||
1179 retval == -ERESTART_RESTARTBLOCK))
1184 EXPORT_SYMBOL(sys_close);
1187 * This routine simulates a hangup on the tty, to arrange that users
1188 * are given clean terminals at login time.
1190 SYSCALL_DEFINE0(vhangup)
1192 if (capable(CAP_SYS_TTY_CONFIG)) {
1200 * Called when an inode is about to be open.
1201 * We use this to disallow opening large files on 32bit systems if
1202 * the caller didn't specify O_LARGEFILE. On 64bit systems we force
1203 * on this flag in sys_open.
1205 int generic_file_open(struct inode * inode, struct file * filp)
1207 if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
1212 EXPORT_SYMBOL(generic_file_open);
1215 * This is used by subsystems that don't want seekable
1216 * file descriptors. The function is not supposed to ever fail, the only
1217 * reason it returns an 'int' and not 'void' is so that it can be plugged
1218 * directly into file_operations structure.
1220 int nonseekable_open(struct inode *inode, struct file *filp)
1222 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
1226 EXPORT_SYMBOL(nonseekable_open);
1229 * stream_open is used by subsystems that want stream-like file descriptors.
1230 * Such file descriptors are not seekable and don't have notion of position
1231 * (file.f_pos is always 0). Contrary to file descriptors of other regular
1232 * files, .read() and .write() can run simultaneously.
1234 * stream_open never fails and is marked to return int so that it could be
1235 * directly used as file_operations.open .
1237 int stream_open(struct inode *inode, struct file *filp)
1239 filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE | FMODE_ATOMIC_POS);
1240 filp->f_mode |= FMODE_STREAM;
1244 EXPORT_SYMBOL(stream_open);