4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
21 #include <linux/namei.h>
22 #include <linux/pagemap.h>
23 #include <linux/fsnotify.h>
24 #include <linux/personality.h>
25 #include <linux/security.h>
26 #include <linux/ima.h>
27 #include <linux/syscalls.h>
28 #include <linux/mount.h>
29 #include <linux/audit.h>
30 #include <linux/capability.h>
31 #include <linux/file.h>
32 #include <linux/fcntl.h>
33 #include <linux/device_cgroup.h>
34 #include <linux/fs_struct.h>
35 #include <asm/uaccess.h>
39 /* [Feb-1997 T. Schoebel-Theuer]
40 * Fundamental changes in the pathname lookup mechanisms (namei)
41 * were necessary because of omirr. The reason is that omirr needs
42 * to know the _real_ pathname, not the user-supplied one, in case
43 * of symlinks (and also when transname replacements occur).
45 * The new code replaces the old recursive symlink resolution with
46 * an iterative one (in case of non-nested symlink chains). It does
47 * this with calls to <fs>_follow_link().
48 * As a side effect, dir_namei(), _namei() and follow_link() are now
49 * replaced with a single function lookup_dentry() that can handle all
50 * the special cases of the former code.
52 * With the new dcache, the pathname is stored at each inode, at least as
53 * long as the refcount of the inode is positive. As a side effect, the
54 * size of the dcache depends on the inode cache and thus is dynamic.
56 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
57 * resolution to correspond with current state of the code.
59 * Note that the symlink resolution is not *completely* iterative.
60 * There is still a significant amount of tail- and mid- recursion in
61 * the algorithm. Also, note that <fs>_readlink() is not used in
62 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
63 * may return different results than <fs>_follow_link(). Many virtual
64 * filesystems (including /proc) exhibit this behavior.
67 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
68 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
69 * and the name already exists in form of a symlink, try to create the new
70 * name indicated by the symlink. The old code always complained that the
71 * name already exists, due to not following the symlink even if its target
72 * is nonexistent. The new semantics affects also mknod() and link() when
73 * the name is a symlink pointing to a non-existent name.
75 * I don't know which semantics is the right one, since I have no access
76 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
77 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
78 * "old" one. Personally, I think the new semantics is much more logical.
79 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
80 * file does succeed in both HP-UX and SunOs, but not in Solaris
81 * and in the old Linux semantics.
84 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
85 * semantics. See the comments in "open_namei" and "do_link" below.
87 * [10-Sep-98 Alan Modra] Another symlink change.
90 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
91 * inside the path - always follow.
92 * in the last component in creation/removal/renaming - never follow.
93 * if LOOKUP_FOLLOW passed - follow.
94 * if the pathname has trailing slashes - follow.
95 * otherwise - don't follow.
96 * (applied in that order).
98 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
99 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
100 * During the 2.4 we need to fix the userland stuff depending on it -
101 * hopefully we will be able to get rid of that wart in 2.5. So far only
102 * XEmacs seems to be relying on it...
105 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
107 * any extra contention...
110 /* In order to reduce some races, while at the same time doing additional
111 * checking and hopefully speeding things up, we copy filenames to the
112 * kernel data space before using them..
114 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
115 * PATH_MAX includes the nul terminator --RR.
117 static int do_getname(const char __user
*filename
, char *page
)
120 unsigned long len
= PATH_MAX
;
122 if (!segment_eq(get_fs(), KERNEL_DS
)) {
123 if ((unsigned long) filename
>= TASK_SIZE
)
125 if (TASK_SIZE
- (unsigned long) filename
< PATH_MAX
)
126 len
= TASK_SIZE
- (unsigned long) filename
;
129 retval
= strncpy_from_user(page
, filename
, len
);
133 return -ENAMETOOLONG
;
139 static char *getname_flags(const char __user
* filename
, int flags
)
143 result
= ERR_PTR(-ENOMEM
);
146 int retval
= do_getname(filename
, tmp
);
150 if (retval
!= -ENOENT
|| !(flags
& LOOKUP_EMPTY
)) {
152 result
= ERR_PTR(retval
);
156 audit_getname(result
);
160 char *getname(const char __user
* filename
)
162 return getname_flags(filename
, 0);
165 #ifdef CONFIG_AUDITSYSCALL
166 void putname(const char *name
)
168 if (unlikely(!audit_dummy_context()))
173 EXPORT_SYMBOL(putname
);
177 * This does basic POSIX ACL permission checking
179 static int acl_permission_check(struct inode
*inode
, int mask
)
181 int (*check_acl
)(struct inode
*inode
, int mask
);
182 unsigned int mode
= inode
->i_mode
;
184 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
| MAY_NOT_BLOCK
;
186 if (current_user_ns() != inode_userns(inode
))
189 if (current_fsuid() == inode
->i_uid
)
192 check_acl
= inode
->i_op
->check_acl
;
193 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
) && check_acl
) {
194 int error
= check_acl(inode
, mask
);
195 if (error
!= -EAGAIN
)
199 if (in_group_p(inode
->i_gid
))
205 * If the DACs are ok we don't need any capability check.
207 if ((mask
& ~mode
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
)) == 0)
213 * generic_permission - check for access rights on a Posix-like filesystem
214 * @inode: inode to check access rights for
215 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
216 * @flags: IPERM_FLAG_ flags.
218 * Used to check for read/write/execute permissions on a file.
219 * We use "fsuid" for this, letting us set arbitrary permissions
220 * for filesystem access without changing the "normal" uids which
221 * are used for other things.
223 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224 * request cannot be satisfied (eg. requires blocking or too much complexity).
225 * It would then be called again in ref-walk mode.
227 int generic_permission(struct inode
*inode
, int mask
)
232 * Do the basic POSIX ACL permission checks.
234 ret
= acl_permission_check(inode
, mask
);
238 if (S_ISDIR(inode
->i_mode
)) {
239 /* DACs are overridable for directories */
240 if (ns_capable(inode_userns(inode
), CAP_DAC_OVERRIDE
))
242 if (!(mask
& MAY_WRITE
))
243 if (ns_capable(inode_userns(inode
), CAP_DAC_READ_SEARCH
))
248 * Read/write DACs are always overridable.
249 * Executable DACs are overridable when there is
250 * at least one exec bit set.
252 if (!(mask
& MAY_EXEC
) || (inode
->i_mode
& S_IXUGO
))
253 if (ns_capable(inode_userns(inode
), CAP_DAC_OVERRIDE
))
257 * Searching includes executable on directories, else just read.
259 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
260 if (mask
== MAY_READ
)
261 if (ns_capable(inode_userns(inode
), CAP_DAC_READ_SEARCH
))
268 * inode_permission - check for access rights to a given inode
269 * @inode: inode to check permission on
270 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
272 * Used to check for read/write/execute permissions on an inode.
273 * We use "fsuid" for this, letting us set arbitrary permissions
274 * for filesystem access without changing the "normal" uids which
275 * are used for other things.
277 int inode_permission(struct inode
*inode
, int mask
)
281 if (mask
& MAY_WRITE
) {
282 umode_t mode
= inode
->i_mode
;
285 * Nobody gets write access to a read-only fs.
287 if (IS_RDONLY(inode
) &&
288 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
292 * Nobody gets write access to an immutable file.
294 if (IS_IMMUTABLE(inode
))
298 if (inode
->i_op
->permission
)
299 retval
= inode
->i_op
->permission(inode
, mask
);
301 retval
= generic_permission(inode
, mask
);
306 retval
= devcgroup_inode_permission(inode
, mask
);
310 return security_inode_permission(inode
, mask
);
314 * path_get - get a reference to a path
315 * @path: path to get the reference to
317 * Given a path increment the reference count to the dentry and the vfsmount.
319 void path_get(struct path
*path
)
324 EXPORT_SYMBOL(path_get
);
327 * path_put - put a reference to a path
328 * @path: path to put the reference to
330 * Given a path decrement the reference count to the dentry and the vfsmount.
332 void path_put(struct path
*path
)
337 EXPORT_SYMBOL(path_put
);
340 * Path walking has 2 modes, rcu-walk and ref-walk (see
341 * Documentation/filesystems/path-lookup.txt). In situations when we can't
342 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
343 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
344 * mode. Refcounts are grabbed at the last known good point before rcu-walk
345 * got stuck, so ref-walk may continue from there. If this is not successful
346 * (eg. a seqcount has changed), then failure is returned and it's up to caller
347 * to restart the path walk from the beginning in ref-walk mode.
351 * unlazy_walk - try to switch to ref-walk mode.
352 * @nd: nameidata pathwalk data
353 * @dentry: child of nd->path.dentry or NULL
354 * Returns: 0 on success, -ECHILD on failure
356 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
357 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
358 * @nd or NULL. Must be called from rcu-walk context.
360 static int unlazy_walk(struct nameidata
*nd
, struct dentry
*dentry
)
362 struct fs_struct
*fs
= current
->fs
;
363 struct dentry
*parent
= nd
->path
.dentry
;
366 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
367 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
369 spin_lock(&fs
->lock
);
370 if (nd
->root
.mnt
!= fs
->root
.mnt
||
371 nd
->root
.dentry
!= fs
->root
.dentry
)
374 spin_lock(&parent
->d_lock
);
376 if (!__d_rcu_to_refcount(parent
, nd
->seq
))
378 BUG_ON(nd
->inode
!= parent
->d_inode
);
380 if (dentry
->d_parent
!= parent
)
382 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
383 if (!__d_rcu_to_refcount(dentry
, nd
->seq
))
386 * If the sequence check on the child dentry passed, then
387 * the child has not been removed from its parent. This
388 * means the parent dentry must be valid and able to take
389 * a reference at this point.
391 BUG_ON(!IS_ROOT(dentry
) && dentry
->d_parent
!= parent
);
392 BUG_ON(!parent
->d_count
);
394 spin_unlock(&dentry
->d_lock
);
396 spin_unlock(&parent
->d_lock
);
399 spin_unlock(&fs
->lock
);
401 mntget(nd
->path
.mnt
);
404 br_read_unlock(vfsmount_lock
);
405 nd
->flags
&= ~LOOKUP_RCU
;
409 spin_unlock(&dentry
->d_lock
);
411 spin_unlock(&parent
->d_lock
);
414 spin_unlock(&fs
->lock
);
419 * release_open_intent - free up open intent resources
420 * @nd: pointer to nameidata
422 void release_open_intent(struct nameidata
*nd
)
424 struct file
*file
= nd
->intent
.open
.file
;
426 if (file
&& !IS_ERR(file
)) {
427 if (file
->f_path
.dentry
== NULL
)
434 static inline int d_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
436 return dentry
->d_op
->d_revalidate(dentry
, nd
);
439 static struct dentry
*
440 do_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
442 int status
= d_revalidate(dentry
, nd
);
443 if (unlikely(status
<= 0)) {
445 * The dentry failed validation.
446 * If d_revalidate returned 0 attempt to invalidate
447 * the dentry otherwise d_revalidate is asking us
448 * to return a fail status.
452 dentry
= ERR_PTR(status
);
453 } else if (!d_invalidate(dentry
)) {
462 * complete_walk - successful completion of path walk
463 * @nd: pointer nameidata
465 * If we had been in RCU mode, drop out of it and legitimize nd->path.
466 * Revalidate the final result, unless we'd already done that during
467 * the path walk or the filesystem doesn't ask for it. Return 0 on
468 * success, -error on failure. In case of failure caller does not
469 * need to drop nd->path.
471 static int complete_walk(struct nameidata
*nd
)
473 struct dentry
*dentry
= nd
->path
.dentry
;
476 if (nd
->flags
& LOOKUP_RCU
) {
477 nd
->flags
&= ~LOOKUP_RCU
;
478 if (!(nd
->flags
& LOOKUP_ROOT
))
480 spin_lock(&dentry
->d_lock
);
481 if (unlikely(!__d_rcu_to_refcount(dentry
, nd
->seq
))) {
482 spin_unlock(&dentry
->d_lock
);
484 br_read_unlock(vfsmount_lock
);
487 BUG_ON(nd
->inode
!= dentry
->d_inode
);
488 spin_unlock(&dentry
->d_lock
);
489 mntget(nd
->path
.mnt
);
491 br_read_unlock(vfsmount_lock
);
494 if (likely(!(nd
->flags
& LOOKUP_JUMPED
)))
497 if (likely(!(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)))
500 if (likely(!(dentry
->d_sb
->s_type
->fs_flags
& FS_REVAL_DOT
)))
503 /* Note: we do not d_invalidate() */
504 status
= d_revalidate(dentry
, nd
);
515 static __always_inline
void set_root(struct nameidata
*nd
)
518 get_fs_root(current
->fs
, &nd
->root
);
521 static int link_path_walk(const char *, struct nameidata
*);
523 static __always_inline
void set_root_rcu(struct nameidata
*nd
)
526 struct fs_struct
*fs
= current
->fs
;
530 seq
= read_seqcount_begin(&fs
->seq
);
532 nd
->seq
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
533 } while (read_seqcount_retry(&fs
->seq
, seq
));
537 static __always_inline
int __vfs_follow_link(struct nameidata
*nd
, const char *link
)
549 nd
->flags
|= LOOKUP_JUMPED
;
551 nd
->inode
= nd
->path
.dentry
->d_inode
;
553 ret
= link_path_walk(link
, nd
);
557 return PTR_ERR(link
);
560 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
563 if (path
->mnt
!= nd
->path
.mnt
)
567 static inline void path_to_nameidata(const struct path
*path
,
568 struct nameidata
*nd
)
570 if (!(nd
->flags
& LOOKUP_RCU
)) {
571 dput(nd
->path
.dentry
);
572 if (nd
->path
.mnt
!= path
->mnt
)
573 mntput(nd
->path
.mnt
);
575 nd
->path
.mnt
= path
->mnt
;
576 nd
->path
.dentry
= path
->dentry
;
579 static inline void put_link(struct nameidata
*nd
, struct path
*link
, void *cookie
)
581 struct inode
*inode
= link
->dentry
->d_inode
;
582 if (!IS_ERR(cookie
) && inode
->i_op
->put_link
)
583 inode
->i_op
->put_link(link
->dentry
, nd
, cookie
);
587 static __always_inline
int
588 follow_link(struct path
*link
, struct nameidata
*nd
, void **p
)
591 struct dentry
*dentry
= link
->dentry
;
593 BUG_ON(nd
->flags
& LOOKUP_RCU
);
595 if (link
->mnt
== nd
->path
.mnt
)
598 if (unlikely(current
->total_link_count
>= 40)) {
599 *p
= ERR_PTR(-ELOOP
); /* no ->put_link(), please */
604 current
->total_link_count
++;
606 touch_atime(link
->mnt
, dentry
);
607 nd_set_link(nd
, NULL
);
609 error
= security_inode_follow_link(link
->dentry
, nd
);
611 *p
= ERR_PTR(error
); /* no ->put_link(), please */
616 nd
->last_type
= LAST_BIND
;
617 *p
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
620 char *s
= nd_get_link(nd
);
623 error
= __vfs_follow_link(nd
, s
);
624 else if (nd
->last_type
== LAST_BIND
) {
625 nd
->flags
|= LOOKUP_JUMPED
;
626 nd
->inode
= nd
->path
.dentry
->d_inode
;
627 if (nd
->inode
->i_op
->follow_link
) {
628 /* stepped on a _really_ weird one */
637 static int follow_up_rcu(struct path
*path
)
639 struct vfsmount
*parent
;
640 struct dentry
*mountpoint
;
642 parent
= path
->mnt
->mnt_parent
;
643 if (parent
== path
->mnt
)
645 mountpoint
= path
->mnt
->mnt_mountpoint
;
646 path
->dentry
= mountpoint
;
651 int follow_up(struct path
*path
)
653 struct vfsmount
*parent
;
654 struct dentry
*mountpoint
;
656 br_read_lock(vfsmount_lock
);
657 parent
= path
->mnt
->mnt_parent
;
658 if (parent
== path
->mnt
) {
659 br_read_unlock(vfsmount_lock
);
663 mountpoint
= dget(path
->mnt
->mnt_mountpoint
);
664 br_read_unlock(vfsmount_lock
);
666 path
->dentry
= mountpoint
;
673 * Perform an automount
674 * - return -EISDIR to tell follow_managed() to stop and return the path we
677 static int follow_automount(struct path
*path
, unsigned flags
,
680 struct vfsmount
*mnt
;
683 if (!path
->dentry
->d_op
|| !path
->dentry
->d_op
->d_automount
)
686 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
687 * and this is the terminal part of the path.
689 if ((flags
& LOOKUP_NO_AUTOMOUNT
) && !(flags
& LOOKUP_CONTINUE
))
690 return -EISDIR
; /* we actually want to stop here */
692 /* We want to mount if someone is trying to open/create a file of any
693 * type under the mountpoint, wants to traverse through the mountpoint
694 * or wants to open the mounted directory.
696 * We don't want to mount if someone's just doing a stat and they've
697 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
698 * appended a '/' to the name.
700 if (!(flags
& LOOKUP_FOLLOW
) &&
701 !(flags
& (LOOKUP_CONTINUE
| LOOKUP_DIRECTORY
|
702 LOOKUP_OPEN
| LOOKUP_CREATE
)))
705 current
->total_link_count
++;
706 if (current
->total_link_count
>= 40)
709 mnt
= path
->dentry
->d_op
->d_automount(path
);
712 * The filesystem is allowed to return -EISDIR here to indicate
713 * it doesn't want to automount. For instance, autofs would do
714 * this so that its userspace daemon can mount on this dentry.
716 * However, we can only permit this if it's a terminal point in
717 * the path being looked up; if it wasn't then the remainder of
718 * the path is inaccessible and we should say so.
720 if (PTR_ERR(mnt
) == -EISDIR
&& (flags
& LOOKUP_CONTINUE
))
725 if (!mnt
) /* mount collision */
729 /* lock_mount() may release path->mnt on error */
733 err
= finish_automount(mnt
, path
);
737 /* Someone else made a mount here whilst we were busy */
742 path
->dentry
= dget(mnt
->mnt_root
);
751 * Handle a dentry that is managed in some way.
752 * - Flagged for transit management (autofs)
753 * - Flagged as mountpoint
754 * - Flagged as automount point
756 * This may only be called in refwalk mode.
758 * Serialization is taken care of in namespace.c
760 static int follow_managed(struct path
*path
, unsigned flags
)
762 struct vfsmount
*mnt
= path
->mnt
; /* held by caller, must be left alone */
764 bool need_mntput
= false;
767 /* Given that we're not holding a lock here, we retain the value in a
768 * local variable for each dentry as we look at it so that we don't see
769 * the components of that value change under us */
770 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
771 managed
&= DCACHE_MANAGED_DENTRY
,
772 unlikely(managed
!= 0)) {
773 /* Allow the filesystem to manage the transit without i_mutex
775 if (managed
& DCACHE_MANAGE_TRANSIT
) {
776 BUG_ON(!path
->dentry
->d_op
);
777 BUG_ON(!path
->dentry
->d_op
->d_manage
);
778 ret
= path
->dentry
->d_op
->d_manage(path
->dentry
, false);
783 /* Transit to a mounted filesystem. */
784 if (managed
& DCACHE_MOUNTED
) {
785 struct vfsmount
*mounted
= lookup_mnt(path
);
791 path
->dentry
= dget(mounted
->mnt_root
);
796 /* Something is mounted on this dentry in another
797 * namespace and/or whatever was mounted there in this
798 * namespace got unmounted before we managed to get the
802 /* Handle an automount point */
803 if (managed
& DCACHE_NEED_AUTOMOUNT
) {
804 ret
= follow_automount(path
, flags
, &need_mntput
);
810 /* We didn't change the current path point */
814 if (need_mntput
&& path
->mnt
== mnt
)
821 int follow_down_one(struct path
*path
)
823 struct vfsmount
*mounted
;
825 mounted
= lookup_mnt(path
);
830 path
->dentry
= dget(mounted
->mnt_root
);
836 static inline bool managed_dentry_might_block(struct dentry
*dentry
)
838 return (dentry
->d_flags
& DCACHE_MANAGE_TRANSIT
&&
839 dentry
->d_op
->d_manage(dentry
, true) < 0);
843 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
844 * we meet a managed dentry that would need blocking.
846 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
847 struct inode
**inode
)
850 struct vfsmount
*mounted
;
852 * Don't forget we might have a non-mountpoint managed dentry
853 * that wants to block transit.
855 if (unlikely(managed_dentry_might_block(path
->dentry
)))
858 if (!d_mountpoint(path
->dentry
))
861 mounted
= __lookup_mnt(path
->mnt
, path
->dentry
, 1);
865 path
->dentry
= mounted
->mnt_root
;
866 nd
->seq
= read_seqcount_begin(&path
->dentry
->d_seq
);
868 * Update the inode too. We don't need to re-check the
869 * dentry sequence number here after this d_inode read,
870 * because a mount-point is always pinned.
872 *inode
= path
->dentry
->d_inode
;
877 static void follow_mount_rcu(struct nameidata
*nd
)
879 while (d_mountpoint(nd
->path
.dentry
)) {
880 struct vfsmount
*mounted
;
881 mounted
= __lookup_mnt(nd
->path
.mnt
, nd
->path
.dentry
, 1);
884 nd
->path
.mnt
= mounted
;
885 nd
->path
.dentry
= mounted
->mnt_root
;
886 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
890 static int follow_dotdot_rcu(struct nameidata
*nd
)
895 if (nd
->path
.dentry
== nd
->root
.dentry
&&
896 nd
->path
.mnt
== nd
->root
.mnt
) {
899 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
900 struct dentry
*old
= nd
->path
.dentry
;
901 struct dentry
*parent
= old
->d_parent
;
904 seq
= read_seqcount_begin(&parent
->d_seq
);
905 if (read_seqcount_retry(&old
->d_seq
, nd
->seq
))
907 nd
->path
.dentry
= parent
;
911 if (!follow_up_rcu(&nd
->path
))
913 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
915 follow_mount_rcu(nd
);
916 nd
->inode
= nd
->path
.dentry
->d_inode
;
920 nd
->flags
&= ~LOOKUP_RCU
;
921 if (!(nd
->flags
& LOOKUP_ROOT
))
924 br_read_unlock(vfsmount_lock
);
929 * Follow down to the covering mount currently visible to userspace. At each
930 * point, the filesystem owning that dentry may be queried as to whether the
931 * caller is permitted to proceed or not.
933 int follow_down(struct path
*path
)
938 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
939 unlikely(managed
& DCACHE_MANAGED_DENTRY
)) {
940 /* Allow the filesystem to manage the transit without i_mutex
943 * We indicate to the filesystem if someone is trying to mount
944 * something here. This gives autofs the chance to deny anyone
945 * other than its daemon the right to mount on its
948 * The filesystem may sleep at this point.
950 if (managed
& DCACHE_MANAGE_TRANSIT
) {
951 BUG_ON(!path
->dentry
->d_op
);
952 BUG_ON(!path
->dentry
->d_op
->d_manage
);
953 ret
= path
->dentry
->d_op
->d_manage(
954 path
->dentry
, false);
956 return ret
== -EISDIR
? 0 : ret
;
959 /* Transit to a mounted filesystem. */
960 if (managed
& DCACHE_MOUNTED
) {
961 struct vfsmount
*mounted
= lookup_mnt(path
);
967 path
->dentry
= dget(mounted
->mnt_root
);
971 /* Don't handle automount points here */
978 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
980 static void follow_mount(struct path
*path
)
982 while (d_mountpoint(path
->dentry
)) {
983 struct vfsmount
*mounted
= lookup_mnt(path
);
989 path
->dentry
= dget(mounted
->mnt_root
);
993 static void follow_dotdot(struct nameidata
*nd
)
998 struct dentry
*old
= nd
->path
.dentry
;
1000 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1001 nd
->path
.mnt
== nd
->root
.mnt
) {
1004 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1005 /* rare case of legitimate dget_parent()... */
1006 nd
->path
.dentry
= dget_parent(nd
->path
.dentry
);
1010 if (!follow_up(&nd
->path
))
1013 follow_mount(&nd
->path
);
1014 nd
->inode
= nd
->path
.dentry
->d_inode
;
1018 * Allocate a dentry with name and parent, and perform a parent
1019 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1020 * on error. parent->d_inode->i_mutex must be held. d_lookup must
1021 * have verified that no child exists while under i_mutex.
1023 static struct dentry
*d_alloc_and_lookup(struct dentry
*parent
,
1024 struct qstr
*name
, struct nameidata
*nd
)
1026 struct inode
*inode
= parent
->d_inode
;
1027 struct dentry
*dentry
;
1030 /* Don't create child dentry for a dead directory. */
1031 if (unlikely(IS_DEADDIR(inode
)))
1032 return ERR_PTR(-ENOENT
);
1034 dentry
= d_alloc(parent
, name
);
1035 if (unlikely(!dentry
))
1036 return ERR_PTR(-ENOMEM
);
1038 old
= inode
->i_op
->lookup(inode
, dentry
, nd
);
1039 if (unlikely(old
)) {
1047 * We already have a dentry, but require a lookup to be performed on the parent
1048 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1049 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1050 * child exists while under i_mutex.
1052 static struct dentry
*d_inode_lookup(struct dentry
*parent
, struct dentry
*dentry
,
1053 struct nameidata
*nd
)
1055 struct inode
*inode
= parent
->d_inode
;
1058 /* Don't create child dentry for a dead directory. */
1059 if (unlikely(IS_DEADDIR(inode
)))
1060 return ERR_PTR(-ENOENT
);
1062 old
= inode
->i_op
->lookup(inode
, dentry
, nd
);
1063 if (unlikely(old
)) {
1071 * It's more convoluted than I'd like it to be, but... it's still fairly
1072 * small and for now I'd prefer to have fast path as straight as possible.
1073 * It _is_ time-critical.
1075 static int do_lookup(struct nameidata
*nd
, struct qstr
*name
,
1076 struct path
*path
, struct inode
**inode
)
1078 struct vfsmount
*mnt
= nd
->path
.mnt
;
1079 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1085 * Rename seqlock is not required here because in the off chance
1086 * of a false negative due to a concurrent rename, we're going to
1087 * do the non-racy lookup, below.
1089 if (nd
->flags
& LOOKUP_RCU
) {
1092 dentry
= __d_lookup_rcu(parent
, name
, &seq
, inode
);
1096 /* Memory barrier in read_seqcount_begin of child is enough */
1097 if (__read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
1101 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)) {
1102 status
= d_revalidate(dentry
, nd
);
1103 if (unlikely(status
<= 0)) {
1104 if (status
!= -ECHILD
)
1109 if (unlikely(d_need_lookup(dentry
)))
1112 path
->dentry
= dentry
;
1113 if (unlikely(!__follow_mount_rcu(nd
, path
, inode
)))
1115 if (unlikely(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
))
1119 if (unlazy_walk(nd
, dentry
))
1122 dentry
= __d_lookup(parent
, name
);
1125 if (dentry
&& unlikely(d_need_lookup(dentry
))) {
1130 if (unlikely(!dentry
)) {
1131 struct inode
*dir
= parent
->d_inode
;
1132 BUG_ON(nd
->inode
!= dir
);
1134 mutex_lock(&dir
->i_mutex
);
1135 dentry
= d_lookup(parent
, name
);
1136 if (likely(!dentry
)) {
1137 dentry
= d_alloc_and_lookup(parent
, name
, nd
);
1138 if (IS_ERR(dentry
)) {
1139 mutex_unlock(&dir
->i_mutex
);
1140 return PTR_ERR(dentry
);
1145 } else if (unlikely(d_need_lookup(dentry
))) {
1146 dentry
= d_inode_lookup(parent
, dentry
, nd
);
1147 if (IS_ERR(dentry
)) {
1148 mutex_unlock(&dir
->i_mutex
);
1149 return PTR_ERR(dentry
);
1155 mutex_unlock(&dir
->i_mutex
);
1157 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
) && need_reval
)
1158 status
= d_revalidate(dentry
, nd
);
1159 if (unlikely(status
<= 0)) {
1164 if (!d_invalidate(dentry
)) {
1173 path
->dentry
= dentry
;
1174 err
= follow_managed(path
, nd
->flags
);
1175 if (unlikely(err
< 0)) {
1176 path_put_conditional(path
, nd
);
1179 *inode
= path
->dentry
->d_inode
;
1183 static inline int may_lookup(struct nameidata
*nd
)
1185 if (nd
->flags
& LOOKUP_RCU
) {
1186 int err
= inode_permission(nd
->inode
, MAY_EXEC
|MAY_NOT_BLOCK
);
1189 if (unlazy_walk(nd
, NULL
))
1192 return inode_permission(nd
->inode
, MAY_EXEC
);
1195 static inline int handle_dots(struct nameidata
*nd
, int type
)
1197 if (type
== LAST_DOTDOT
) {
1198 if (nd
->flags
& LOOKUP_RCU
) {
1199 if (follow_dotdot_rcu(nd
))
1207 static void terminate_walk(struct nameidata
*nd
)
1209 if (!(nd
->flags
& LOOKUP_RCU
)) {
1210 path_put(&nd
->path
);
1212 nd
->flags
&= ~LOOKUP_RCU
;
1213 if (!(nd
->flags
& LOOKUP_ROOT
))
1214 nd
->root
.mnt
= NULL
;
1216 br_read_unlock(vfsmount_lock
);
1220 static inline int walk_component(struct nameidata
*nd
, struct path
*path
,
1221 struct qstr
*name
, int type
, int follow
)
1223 struct inode
*inode
;
1226 * "." and ".." are special - ".." especially so because it has
1227 * to be able to know about the current root directory and
1228 * parent relationships.
1230 if (unlikely(type
!= LAST_NORM
))
1231 return handle_dots(nd
, type
);
1232 err
= do_lookup(nd
, name
, path
, &inode
);
1233 if (unlikely(err
)) {
1238 path_to_nameidata(path
, nd
);
1242 if (unlikely(inode
->i_op
->follow_link
) && follow
) {
1243 if (nd
->flags
& LOOKUP_RCU
) {
1244 if (unlikely(unlazy_walk(nd
, path
->dentry
))) {
1249 BUG_ON(inode
!= path
->dentry
->d_inode
);
1252 path_to_nameidata(path
, nd
);
1258 * This limits recursive symlink follows to 8, while
1259 * limiting consecutive symlinks to 40.
1261 * Without that kind of total limit, nasty chains of consecutive
1262 * symlinks can cause almost arbitrarily long lookups.
1264 static inline int nested_symlink(struct path
*path
, struct nameidata
*nd
)
1268 if (unlikely(current
->link_count
>= MAX_NESTED_LINKS
)) {
1269 path_put_conditional(path
, nd
);
1270 path_put(&nd
->path
);
1273 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
1276 current
->link_count
++;
1279 struct path link
= *path
;
1282 res
= follow_link(&link
, nd
, &cookie
);
1284 res
= walk_component(nd
, path
, &nd
->last
,
1285 nd
->last_type
, LOOKUP_FOLLOW
);
1286 put_link(nd
, &link
, cookie
);
1289 current
->link_count
--;
1296 * This is the basic name resolution function, turning a pathname into
1297 * the final dentry. We expect 'base' to be positive and a directory.
1299 * Returns 0 and nd will have valid dentry and mnt on success.
1300 * Returns error and drops reference to input namei data on failure.
1302 static int link_path_walk(const char *name
, struct nameidata
*nd
)
1306 unsigned int lookup_flags
= nd
->flags
;
1313 /* At this point we know we have a real path component. */
1320 nd
->flags
|= LOOKUP_CONTINUE
;
1322 err
= may_lookup(nd
);
1327 c
= *(const unsigned char *)name
;
1329 hash
= init_name_hash();
1332 hash
= partial_name_hash(c
, hash
);
1333 c
= *(const unsigned char *)name
;
1334 } while (c
&& (c
!= '/'));
1335 this.len
= name
- (const char *) this.name
;
1336 this.hash
= end_name_hash(hash
);
1339 if (this.name
[0] == '.') switch (this.len
) {
1341 if (this.name
[1] == '.') {
1343 nd
->flags
|= LOOKUP_JUMPED
;
1349 if (likely(type
== LAST_NORM
)) {
1350 struct dentry
*parent
= nd
->path
.dentry
;
1351 nd
->flags
&= ~LOOKUP_JUMPED
;
1352 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
1353 err
= parent
->d_op
->d_hash(parent
, nd
->inode
,
1360 /* remove trailing slashes? */
1362 goto last_component
;
1363 while (*++name
== '/');
1365 goto last_component
;
1367 err
= walk_component(nd
, &next
, &this, type
, LOOKUP_FOLLOW
);
1372 err
= nested_symlink(&next
, nd
);
1377 if (!nd
->inode
->i_op
->lookup
)
1380 /* here ends the main loop */
1383 /* Clear LOOKUP_CONTINUE iff it was previously unset */
1384 nd
->flags
&= lookup_flags
| ~LOOKUP_CONTINUE
;
1386 nd
->last_type
= type
;
1393 static int path_init(int dfd
, const char *name
, unsigned int flags
,
1394 struct nameidata
*nd
, struct file
**fp
)
1400 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
1401 nd
->flags
= flags
| LOOKUP_JUMPED
;
1403 if (flags
& LOOKUP_ROOT
) {
1404 struct inode
*inode
= nd
->root
.dentry
->d_inode
;
1406 if (!inode
->i_op
->lookup
)
1408 retval
= inode_permission(inode
, MAY_EXEC
);
1412 nd
->path
= nd
->root
;
1414 if (flags
& LOOKUP_RCU
) {
1415 br_read_lock(vfsmount_lock
);
1417 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1419 path_get(&nd
->path
);
1424 nd
->root
.mnt
= NULL
;
1427 if (flags
& LOOKUP_RCU
) {
1428 br_read_lock(vfsmount_lock
);
1433 path_get(&nd
->root
);
1435 nd
->path
= nd
->root
;
1436 } else if (dfd
== AT_FDCWD
) {
1437 if (flags
& LOOKUP_RCU
) {
1438 struct fs_struct
*fs
= current
->fs
;
1441 br_read_lock(vfsmount_lock
);
1445 seq
= read_seqcount_begin(&fs
->seq
);
1447 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1448 } while (read_seqcount_retry(&fs
->seq
, seq
));
1450 get_fs_pwd(current
->fs
, &nd
->path
);
1453 struct dentry
*dentry
;
1455 file
= fget_raw_light(dfd
, &fput_needed
);
1460 dentry
= file
->f_path
.dentry
;
1464 if (!S_ISDIR(dentry
->d_inode
->i_mode
))
1467 retval
= inode_permission(dentry
->d_inode
, MAY_EXEC
);
1472 nd
->path
= file
->f_path
;
1473 if (flags
& LOOKUP_RCU
) {
1476 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1477 br_read_lock(vfsmount_lock
);
1480 path_get(&file
->f_path
);
1481 fput_light(file
, fput_needed
);
1485 nd
->inode
= nd
->path
.dentry
->d_inode
;
1489 fput_light(file
, fput_needed
);
1494 static inline int lookup_last(struct nameidata
*nd
, struct path
*path
)
1496 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
1497 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
1499 nd
->flags
&= ~LOOKUP_PARENT
;
1500 return walk_component(nd
, path
, &nd
->last
, nd
->last_type
,
1501 nd
->flags
& LOOKUP_FOLLOW
);
1504 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1505 static int path_lookupat(int dfd
, const char *name
,
1506 unsigned int flags
, struct nameidata
*nd
)
1508 struct file
*base
= NULL
;
1513 * Path walking is largely split up into 2 different synchronisation
1514 * schemes, rcu-walk and ref-walk (explained in
1515 * Documentation/filesystems/path-lookup.txt). These share much of the
1516 * path walk code, but some things particularly setup, cleanup, and
1517 * following mounts are sufficiently divergent that functions are
1518 * duplicated. Typically there is a function foo(), and its RCU
1519 * analogue, foo_rcu().
1521 * -ECHILD is the error number of choice (just to avoid clashes) that
1522 * is returned if some aspect of an rcu-walk fails. Such an error must
1523 * be handled by restarting a traditional ref-walk (which will always
1524 * be able to complete).
1526 err
= path_init(dfd
, name
, flags
| LOOKUP_PARENT
, nd
, &base
);
1531 current
->total_link_count
= 0;
1532 err
= link_path_walk(name
, nd
);
1534 if (!err
&& !(flags
& LOOKUP_PARENT
)) {
1535 err
= lookup_last(nd
, &path
);
1538 struct path link
= path
;
1539 nd
->flags
|= LOOKUP_PARENT
;
1540 err
= follow_link(&link
, nd
, &cookie
);
1542 err
= lookup_last(nd
, &path
);
1543 put_link(nd
, &link
, cookie
);
1548 err
= complete_walk(nd
);
1550 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
) {
1551 if (!nd
->inode
->i_op
->lookup
) {
1552 path_put(&nd
->path
);
1560 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
1561 path_put(&nd
->root
);
1562 nd
->root
.mnt
= NULL
;
1567 static int do_path_lookup(int dfd
, const char *name
,
1568 unsigned int flags
, struct nameidata
*nd
)
1570 int retval
= path_lookupat(dfd
, name
, flags
| LOOKUP_RCU
, nd
);
1571 if (unlikely(retval
== -ECHILD
))
1572 retval
= path_lookupat(dfd
, name
, flags
, nd
);
1573 if (unlikely(retval
== -ESTALE
))
1574 retval
= path_lookupat(dfd
, name
, flags
| LOOKUP_REVAL
, nd
);
1576 if (likely(!retval
)) {
1577 if (unlikely(!audit_dummy_context())) {
1578 if (nd
->path
.dentry
&& nd
->inode
)
1579 audit_inode(name
, nd
->path
.dentry
);
1585 int kern_path_parent(const char *name
, struct nameidata
*nd
)
1587 return do_path_lookup(AT_FDCWD
, name
, LOOKUP_PARENT
, nd
);
1590 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
1592 struct nameidata nd
;
1593 int res
= do_path_lookup(AT_FDCWD
, name
, flags
, &nd
);
1600 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1601 * @dentry: pointer to dentry of the base directory
1602 * @mnt: pointer to vfs mount of the base directory
1603 * @name: pointer to file name
1604 * @flags: lookup flags
1605 * @nd: pointer to nameidata
1607 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
1608 const char *name
, unsigned int flags
,
1609 struct nameidata
*nd
)
1611 nd
->root
.dentry
= dentry
;
1613 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1614 return do_path_lookup(AT_FDCWD
, name
, flags
| LOOKUP_ROOT
, nd
);
1617 static struct dentry
*__lookup_hash(struct qstr
*name
,
1618 struct dentry
*base
, struct nameidata
*nd
)
1620 struct inode
*inode
= base
->d_inode
;
1621 struct dentry
*dentry
;
1624 err
= inode_permission(inode
, MAY_EXEC
);
1626 return ERR_PTR(err
);
1629 * Don't bother with __d_lookup: callers are for creat as
1630 * well as unlink, so a lot of the time it would cost
1633 dentry
= d_lookup(base
, name
);
1635 if (dentry
&& d_need_lookup(dentry
)) {
1637 * __lookup_hash is called with the parent dir's i_mutex already
1638 * held, so we are good to go here.
1640 dentry
= d_inode_lookup(base
, dentry
, nd
);
1645 if (dentry
&& (dentry
->d_flags
& DCACHE_OP_REVALIDATE
))
1646 dentry
= do_revalidate(dentry
, nd
);
1649 dentry
= d_alloc_and_lookup(base
, name
, nd
);
1655 * Restricted form of lookup. Doesn't follow links, single-component only,
1656 * needs parent already locked. Doesn't follow mounts.
1659 static struct dentry
*lookup_hash(struct nameidata
*nd
)
1661 return __lookup_hash(&nd
->last
, nd
->path
.dentry
, nd
);
1665 * lookup_one_len - filesystem helper to lookup single pathname component
1666 * @name: pathname component to lookup
1667 * @base: base directory to lookup from
1668 * @len: maximum length @len should be interpreted to
1670 * Note that this routine is purely a helper for filesystem usage and should
1671 * not be called by generic code. Also note that by using this function the
1672 * nameidata argument is passed to the filesystem methods and a filesystem
1673 * using this helper needs to be prepared for that.
1675 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
1681 WARN_ON_ONCE(!mutex_is_locked(&base
->d_inode
->i_mutex
));
1686 return ERR_PTR(-EACCES
);
1688 hash
= init_name_hash();
1690 c
= *(const unsigned char *)name
++;
1691 if (c
== '/' || c
== '\0')
1692 return ERR_PTR(-EACCES
);
1693 hash
= partial_name_hash(c
, hash
);
1695 this.hash
= end_name_hash(hash
);
1697 * See if the low-level filesystem might want
1698 * to use its own hash..
1700 if (base
->d_flags
& DCACHE_OP_HASH
) {
1701 int err
= base
->d_op
->d_hash(base
, base
->d_inode
, &this);
1703 return ERR_PTR(err
);
1706 return __lookup_hash(&this, base
, NULL
);
1709 int user_path_at(int dfd
, const char __user
*name
, unsigned flags
,
1712 struct nameidata nd
;
1713 char *tmp
= getname_flags(name
, flags
);
1714 int err
= PTR_ERR(tmp
);
1717 BUG_ON(flags
& LOOKUP_PARENT
);
1719 err
= do_path_lookup(dfd
, tmp
, flags
, &nd
);
1727 static int user_path_parent(int dfd
, const char __user
*path
,
1728 struct nameidata
*nd
, char **name
)
1730 char *s
= getname(path
);
1736 error
= do_path_lookup(dfd
, s
, LOOKUP_PARENT
, nd
);
1746 * It's inline, so penalty for filesystems that don't use sticky bit is
1749 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
1751 uid_t fsuid
= current_fsuid();
1753 if (!(dir
->i_mode
& S_ISVTX
))
1755 if (current_user_ns() != inode_userns(inode
))
1757 if (inode
->i_uid
== fsuid
)
1759 if (dir
->i_uid
== fsuid
)
1763 return !ns_capable(inode_userns(inode
), CAP_FOWNER
);
1767 * Check whether we can remove a link victim from directory dir, check
1768 * whether the type of victim is right.
1769 * 1. We can't do it if dir is read-only (done in permission())
1770 * 2. We should have write and exec permissions on dir
1771 * 3. We can't remove anything from append-only dir
1772 * 4. We can't do anything with immutable dir (done in permission())
1773 * 5. If the sticky bit on dir is set we should either
1774 * a. be owner of dir, or
1775 * b. be owner of victim, or
1776 * c. have CAP_FOWNER capability
1777 * 6. If the victim is append-only or immutable we can't do antyhing with
1778 * links pointing to it.
1779 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1780 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1781 * 9. We can't remove a root or mountpoint.
1782 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1783 * nfs_async_unlink().
1785 static int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
1789 if (!victim
->d_inode
)
1792 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
1793 audit_inode_child(victim
, dir
);
1795 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
1800 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
1801 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
1804 if (!S_ISDIR(victim
->d_inode
->i_mode
))
1806 if (IS_ROOT(victim
))
1808 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
1810 if (IS_DEADDIR(dir
))
1812 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
1817 /* Check whether we can create an object with dentry child in directory
1819 * 1. We can't do it if child already exists (open has special treatment for
1820 * this case, but since we are inlined it's OK)
1821 * 2. We can't do it if dir is read-only (done in permission())
1822 * 3. We should have write and exec permissions on dir
1823 * 4. We can't do it if dir is immutable (done in permission())
1825 static inline int may_create(struct inode
*dir
, struct dentry
*child
)
1829 if (IS_DEADDIR(dir
))
1831 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
1835 * p1 and p2 should be directories on the same fs.
1837 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
1842 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1846 mutex_lock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1848 p
= d_ancestor(p2
, p1
);
1850 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1851 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1855 p
= d_ancestor(p1
, p2
);
1857 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1858 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1862 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1863 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1867 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
1869 mutex_unlock(&p1
->d_inode
->i_mutex
);
1871 mutex_unlock(&p2
->d_inode
->i_mutex
);
1872 mutex_unlock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1876 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1877 struct nameidata
*nd
)
1879 int error
= may_create(dir
, dentry
);
1884 if (!dir
->i_op
->create
)
1885 return -EACCES
; /* shouldn't it be ENOSYS? */
1888 error
= security_inode_create(dir
, dentry
, mode
);
1891 error
= dir
->i_op
->create(dir
, dentry
, mode
, nd
);
1893 fsnotify_create(dir
, dentry
);
1897 static int may_open(struct path
*path
, int acc_mode
, int flag
)
1899 struct dentry
*dentry
= path
->dentry
;
1900 struct inode
*inode
= dentry
->d_inode
;
1910 switch (inode
->i_mode
& S_IFMT
) {
1914 if (acc_mode
& MAY_WRITE
)
1919 if (path
->mnt
->mnt_flags
& MNT_NODEV
)
1928 error
= inode_permission(inode
, acc_mode
);
1933 * An append-only file must be opened in append mode for writing.
1935 if (IS_APPEND(inode
)) {
1936 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
1942 /* O_NOATIME can only be set by the owner or superuser */
1943 if (flag
& O_NOATIME
&& !inode_owner_or_capable(inode
))
1947 * Ensure there are no outstanding leases on the file.
1949 return break_lease(inode
, flag
);
1952 static int handle_truncate(struct file
*filp
)
1954 struct path
*path
= &filp
->f_path
;
1955 struct inode
*inode
= path
->dentry
->d_inode
;
1956 int error
= get_write_access(inode
);
1960 * Refuse to truncate files with mandatory locks held on them.
1962 error
= locks_verify_locked(inode
);
1964 error
= security_path_truncate(path
);
1966 error
= do_truncate(path
->dentry
, 0,
1967 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
1970 put_write_access(inode
);
1975 * Note that while the flag value (low two bits) for sys_open means:
1980 * it is changed into
1981 * 00 - no permissions needed
1982 * 01 - read-permission
1983 * 10 - write-permission
1985 * for the internal routines (ie open_namei()/follow_link() etc)
1986 * This is more logical, and also allows the 00 "no perm needed"
1987 * to be used for symlinks (where the permissions are checked
1991 static inline int open_to_namei_flags(int flag
)
1993 if ((flag
+1) & O_ACCMODE
)
1999 * Handle the last step of open()
2001 static struct file
*do_last(struct nameidata
*nd
, struct path
*path
,
2002 const struct open_flags
*op
, const char *pathname
)
2004 struct dentry
*dir
= nd
->path
.dentry
;
2005 struct dentry
*dentry
;
2006 int open_flag
= op
->open_flag
;
2007 int will_truncate
= open_flag
& O_TRUNC
;
2009 int acc_mode
= op
->acc_mode
;
2013 nd
->flags
&= ~LOOKUP_PARENT
;
2014 nd
->flags
|= op
->intent
;
2016 switch (nd
->last_type
) {
2019 error
= handle_dots(nd
, nd
->last_type
);
2021 return ERR_PTR(error
);
2024 error
= complete_walk(nd
);
2026 return ERR_PTR(error
);
2027 audit_inode(pathname
, nd
->path
.dentry
);
2028 if (open_flag
& O_CREAT
) {
2034 error
= complete_walk(nd
);
2036 return ERR_PTR(error
);
2037 audit_inode(pathname
, dir
);
2041 if (!(open_flag
& O_CREAT
)) {
2043 if (nd
->last
.name
[nd
->last
.len
])
2044 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2045 if (open_flag
& O_PATH
&& !(nd
->flags
& LOOKUP_FOLLOW
))
2047 /* we _can_ be in RCU mode here */
2048 error
= walk_component(nd
, path
, &nd
->last
, LAST_NORM
,
2051 return ERR_PTR(error
);
2052 if (error
) /* symlink */
2055 error
= complete_walk(nd
);
2057 return ERR_PTR(-ECHILD
);
2060 if (nd
->flags
& LOOKUP_DIRECTORY
) {
2061 if (!nd
->inode
->i_op
->lookup
)
2064 audit_inode(pathname
, nd
->path
.dentry
);
2068 /* create side of things */
2069 error
= complete_walk(nd
);
2071 return ERR_PTR(error
);
2073 audit_inode(pathname
, dir
);
2075 /* trailing slashes? */
2076 if (nd
->last
.name
[nd
->last
.len
])
2079 mutex_lock(&dir
->d_inode
->i_mutex
);
2081 dentry
= lookup_hash(nd
);
2082 error
= PTR_ERR(dentry
);
2083 if (IS_ERR(dentry
)) {
2084 mutex_unlock(&dir
->d_inode
->i_mutex
);
2088 path
->dentry
= dentry
;
2089 path
->mnt
= nd
->path
.mnt
;
2091 /* Negative dentry, just create the file */
2092 if (!dentry
->d_inode
) {
2093 int mode
= op
->mode
;
2094 if (!IS_POSIXACL(dir
->d_inode
))
2095 mode
&= ~current_umask();
2097 * This write is needed to ensure that a
2098 * rw->ro transition does not occur between
2099 * the time when the file is created and when
2100 * a permanent write count is taken through
2101 * the 'struct file' in nameidata_to_filp().
2103 error
= mnt_want_write(nd
->path
.mnt
);
2105 goto exit_mutex_unlock
;
2107 /* Don't check for write permission, don't truncate */
2108 open_flag
&= ~O_TRUNC
;
2110 acc_mode
= MAY_OPEN
;
2111 error
= security_path_mknod(&nd
->path
, dentry
, mode
, 0);
2113 goto exit_mutex_unlock
;
2114 error
= vfs_create(dir
->d_inode
, dentry
, mode
, nd
);
2116 goto exit_mutex_unlock
;
2117 mutex_unlock(&dir
->d_inode
->i_mutex
);
2118 dput(nd
->path
.dentry
);
2119 nd
->path
.dentry
= dentry
;
2124 * It already exists.
2126 mutex_unlock(&dir
->d_inode
->i_mutex
);
2127 audit_inode(pathname
, path
->dentry
);
2130 if (open_flag
& O_EXCL
)
2133 error
= follow_managed(path
, nd
->flags
);
2138 if (!path
->dentry
->d_inode
)
2141 if (path
->dentry
->d_inode
->i_op
->follow_link
)
2144 path_to_nameidata(path
, nd
);
2145 nd
->inode
= path
->dentry
->d_inode
;
2147 if (S_ISDIR(nd
->inode
->i_mode
))
2150 if (!S_ISREG(nd
->inode
->i_mode
))
2153 if (will_truncate
) {
2154 error
= mnt_want_write(nd
->path
.mnt
);
2160 error
= may_open(&nd
->path
, acc_mode
, open_flag
);
2163 filp
= nameidata_to_filp(nd
);
2164 if (!IS_ERR(filp
)) {
2165 error
= ima_file_check(filp
, op
->acc_mode
);
2168 filp
= ERR_PTR(error
);
2171 if (!IS_ERR(filp
)) {
2172 if (will_truncate
) {
2173 error
= handle_truncate(filp
);
2176 filp
= ERR_PTR(error
);
2182 mnt_drop_write(nd
->path
.mnt
);
2183 path_put(&nd
->path
);
2187 mutex_unlock(&dir
->d_inode
->i_mutex
);
2189 path_put_conditional(path
, nd
);
2191 filp
= ERR_PTR(error
);
2195 static struct file
*path_openat(int dfd
, const char *pathname
,
2196 struct nameidata
*nd
, const struct open_flags
*op
, int flags
)
2198 struct file
*base
= NULL
;
2203 filp
= get_empty_filp();
2205 return ERR_PTR(-ENFILE
);
2207 filp
->f_flags
= op
->open_flag
;
2208 nd
->intent
.open
.file
= filp
;
2209 nd
->intent
.open
.flags
= open_to_namei_flags(op
->open_flag
);
2210 nd
->intent
.open
.create_mode
= op
->mode
;
2212 error
= path_init(dfd
, pathname
, flags
| LOOKUP_PARENT
, nd
, &base
);
2213 if (unlikely(error
))
2216 current
->total_link_count
= 0;
2217 error
= link_path_walk(pathname
, nd
);
2218 if (unlikely(error
))
2221 filp
= do_last(nd
, &path
, op
, pathname
);
2222 while (unlikely(!filp
)) { /* trailing symlink */
2223 struct path link
= path
;
2225 if (!(nd
->flags
& LOOKUP_FOLLOW
)) {
2226 path_put_conditional(&path
, nd
);
2227 path_put(&nd
->path
);
2228 filp
= ERR_PTR(-ELOOP
);
2231 nd
->flags
|= LOOKUP_PARENT
;
2232 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
2233 error
= follow_link(&link
, nd
, &cookie
);
2234 if (unlikely(error
))
2235 filp
= ERR_PTR(error
);
2237 filp
= do_last(nd
, &path
, op
, pathname
);
2238 put_link(nd
, &link
, cookie
);
2241 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
))
2242 path_put(&nd
->root
);
2245 release_open_intent(nd
);
2249 filp
= ERR_PTR(error
);
2253 struct file
*do_filp_open(int dfd
, const char *pathname
,
2254 const struct open_flags
*op
, int flags
)
2256 struct nameidata nd
;
2259 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_RCU
);
2260 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
2261 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
);
2262 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
2263 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_REVAL
);
2267 struct file
*do_file_open_root(struct dentry
*dentry
, struct vfsmount
*mnt
,
2268 const char *name
, const struct open_flags
*op
, int flags
)
2270 struct nameidata nd
;
2274 nd
.root
.dentry
= dentry
;
2276 flags
|= LOOKUP_ROOT
;
2278 if (dentry
->d_inode
->i_op
->follow_link
&& op
->intent
& LOOKUP_OPEN
)
2279 return ERR_PTR(-ELOOP
);
2281 file
= path_openat(-1, name
, &nd
, op
, flags
| LOOKUP_RCU
);
2282 if (unlikely(file
== ERR_PTR(-ECHILD
)))
2283 file
= path_openat(-1, name
, &nd
, op
, flags
);
2284 if (unlikely(file
== ERR_PTR(-ESTALE
)))
2285 file
= path_openat(-1, name
, &nd
, op
, flags
| LOOKUP_REVAL
);
2290 * lookup_create - lookup a dentry, creating it if it doesn't exist
2291 * @nd: nameidata info
2292 * @is_dir: directory flag
2294 * Simple function to lookup and return a dentry and create it
2295 * if it doesn't exist. Is SMP-safe.
2297 * Returns with nd->path.dentry->d_inode->i_mutex locked.
2299 struct dentry
*lookup_create(struct nameidata
*nd
, int is_dir
)
2301 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
2303 mutex_lock_nested(&nd
->path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2305 * Yucky last component or no last component at all?
2306 * (foo/., foo/.., /////)
2308 if (nd
->last_type
!= LAST_NORM
)
2310 nd
->flags
&= ~LOOKUP_PARENT
;
2311 nd
->flags
|= LOOKUP_CREATE
| LOOKUP_EXCL
;
2312 nd
->intent
.open
.flags
= O_EXCL
;
2315 * Do the final lookup.
2317 dentry
= lookup_hash(nd
);
2321 if (dentry
->d_inode
)
2324 * Special case - lookup gave negative, but... we had foo/bar/
2325 * From the vfs_mknod() POV we just have a negative dentry -
2326 * all is fine. Let's be bastards - you had / on the end, you've
2327 * been asking for (non-existent) directory. -ENOENT for you.
2329 if (unlikely(!is_dir
&& nd
->last
.name
[nd
->last
.len
])) {
2331 dentry
= ERR_PTR(-ENOENT
);
2336 dentry
= ERR_PTR(-EEXIST
);
2340 EXPORT_SYMBOL_GPL(lookup_create
);
2342 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
2344 int error
= may_create(dir
, dentry
);
2349 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) &&
2350 !ns_capable(inode_userns(dir
), CAP_MKNOD
))
2353 if (!dir
->i_op
->mknod
)
2356 error
= devcgroup_inode_mknod(mode
, dev
);
2360 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
2364 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
2366 fsnotify_create(dir
, dentry
);
2370 static int may_mknod(mode_t mode
)
2372 switch (mode
& S_IFMT
) {
2378 case 0: /* zero mode translates to S_IFREG */
2387 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, int, mode
,
2392 struct dentry
*dentry
;
2393 struct nameidata nd
;
2398 error
= user_path_parent(dfd
, filename
, &nd
, &tmp
);
2402 dentry
= lookup_create(&nd
, 0);
2403 if (IS_ERR(dentry
)) {
2404 error
= PTR_ERR(dentry
);
2407 if (!IS_POSIXACL(nd
.path
.dentry
->d_inode
))
2408 mode
&= ~current_umask();
2409 error
= may_mknod(mode
);
2412 error
= mnt_want_write(nd
.path
.mnt
);
2415 error
= security_path_mknod(&nd
.path
, dentry
, mode
, dev
);
2417 goto out_drop_write
;
2418 switch (mode
& S_IFMT
) {
2419 case 0: case S_IFREG
:
2420 error
= vfs_create(nd
.path
.dentry
->d_inode
,dentry
,mode
,&nd
);
2422 case S_IFCHR
: case S_IFBLK
:
2423 error
= vfs_mknod(nd
.path
.dentry
->d_inode
,dentry
,mode
,
2424 new_decode_dev(dev
));
2426 case S_IFIFO
: case S_IFSOCK
:
2427 error
= vfs_mknod(nd
.path
.dentry
->d_inode
,dentry
,mode
,0);
2431 mnt_drop_write(nd
.path
.mnt
);
2435 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2442 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, int, mode
, unsigned, dev
)
2444 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
2447 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2449 int error
= may_create(dir
, dentry
);
2454 if (!dir
->i_op
->mkdir
)
2457 mode
&= (S_IRWXUGO
|S_ISVTX
);
2458 error
= security_inode_mkdir(dir
, dentry
, mode
);
2462 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
2464 fsnotify_mkdir(dir
, dentry
);
2468 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, int, mode
)
2472 struct dentry
*dentry
;
2473 struct nameidata nd
;
2475 error
= user_path_parent(dfd
, pathname
, &nd
, &tmp
);
2479 dentry
= lookup_create(&nd
, 1);
2480 error
= PTR_ERR(dentry
);
2484 if (!IS_POSIXACL(nd
.path
.dentry
->d_inode
))
2485 mode
&= ~current_umask();
2486 error
= mnt_want_write(nd
.path
.mnt
);
2489 error
= security_path_mkdir(&nd
.path
, dentry
, mode
);
2491 goto out_drop_write
;
2492 error
= vfs_mkdir(nd
.path
.dentry
->d_inode
, dentry
, mode
);
2494 mnt_drop_write(nd
.path
.mnt
);
2498 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2505 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, int, mode
)
2507 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
2511 * The dentry_unhash() helper will try to drop the dentry early: we
2512 * should have a usage count of 2 if we're the only user of this
2513 * dentry, and if that is true (possibly after pruning the dcache),
2514 * then we drop the dentry now.
2516 * A low-level filesystem can, if it choses, legally
2519 * if (!d_unhashed(dentry))
2522 * if it cannot handle the case of removing a directory
2523 * that is still in use by something else..
2525 void dentry_unhash(struct dentry
*dentry
)
2527 shrink_dcache_parent(dentry
);
2528 spin_lock(&dentry
->d_lock
);
2529 if (dentry
->d_count
== 1)
2531 spin_unlock(&dentry
->d_lock
);
2534 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2536 int error
= may_delete(dir
, dentry
, 1);
2541 if (!dir
->i_op
->rmdir
)
2544 mutex_lock(&dentry
->d_inode
->i_mutex
);
2547 if (d_mountpoint(dentry
))
2550 error
= security_inode_rmdir(dir
, dentry
);
2554 shrink_dcache_parent(dentry
);
2555 error
= dir
->i_op
->rmdir(dir
, dentry
);
2559 dentry
->d_inode
->i_flags
|= S_DEAD
;
2563 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2569 static long do_rmdir(int dfd
, const char __user
*pathname
)
2573 struct dentry
*dentry
;
2574 struct nameidata nd
;
2576 error
= user_path_parent(dfd
, pathname
, &nd
, &name
);
2580 switch(nd
.last_type
) {
2592 nd
.flags
&= ~LOOKUP_PARENT
;
2594 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2595 dentry
= lookup_hash(&nd
);
2596 error
= PTR_ERR(dentry
);
2599 if (!dentry
->d_inode
) {
2603 error
= mnt_want_write(nd
.path
.mnt
);
2606 error
= security_path_rmdir(&nd
.path
, dentry
);
2609 error
= vfs_rmdir(nd
.path
.dentry
->d_inode
, dentry
);
2611 mnt_drop_write(nd
.path
.mnt
);
2615 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2622 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
2624 return do_rmdir(AT_FDCWD
, pathname
);
2627 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2629 int error
= may_delete(dir
, dentry
, 0);
2634 if (!dir
->i_op
->unlink
)
2637 mutex_lock(&dentry
->d_inode
->i_mutex
);
2638 if (d_mountpoint(dentry
))
2641 error
= security_inode_unlink(dir
, dentry
);
2643 error
= dir
->i_op
->unlink(dir
, dentry
);
2648 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2650 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2651 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
2652 fsnotify_link_count(dentry
->d_inode
);
2660 * Make sure that the actual truncation of the file will occur outside its
2661 * directory's i_mutex. Truncate can take a long time if there is a lot of
2662 * writeout happening, and we don't want to prevent access to the directory
2663 * while waiting on the I/O.
2665 static long do_unlinkat(int dfd
, const char __user
*pathname
)
2669 struct dentry
*dentry
;
2670 struct nameidata nd
;
2671 struct inode
*inode
= NULL
;
2673 error
= user_path_parent(dfd
, pathname
, &nd
, &name
);
2678 if (nd
.last_type
!= LAST_NORM
)
2681 nd
.flags
&= ~LOOKUP_PARENT
;
2683 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2684 dentry
= lookup_hash(&nd
);
2685 error
= PTR_ERR(dentry
);
2686 if (!IS_ERR(dentry
)) {
2687 /* Why not before? Because we want correct error value */
2688 if (nd
.last
.name
[nd
.last
.len
])
2690 inode
= dentry
->d_inode
;
2694 error
= mnt_want_write(nd
.path
.mnt
);
2697 error
= security_path_unlink(&nd
.path
, dentry
);
2700 error
= vfs_unlink(nd
.path
.dentry
->d_inode
, dentry
);
2702 mnt_drop_write(nd
.path
.mnt
);
2706 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2708 iput(inode
); /* truncate the inode here */
2715 error
= !dentry
->d_inode
? -ENOENT
:
2716 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
2720 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
2722 if ((flag
& ~AT_REMOVEDIR
) != 0)
2725 if (flag
& AT_REMOVEDIR
)
2726 return do_rmdir(dfd
, pathname
);
2728 return do_unlinkat(dfd
, pathname
);
2731 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
2733 return do_unlinkat(AT_FDCWD
, pathname
);
2736 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
2738 int error
= may_create(dir
, dentry
);
2743 if (!dir
->i_op
->symlink
)
2746 error
= security_inode_symlink(dir
, dentry
, oldname
);
2750 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
2752 fsnotify_create(dir
, dentry
);
2756 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
2757 int, newdfd
, const char __user
*, newname
)
2762 struct dentry
*dentry
;
2763 struct nameidata nd
;
2765 from
= getname(oldname
);
2767 return PTR_ERR(from
);
2769 error
= user_path_parent(newdfd
, newname
, &nd
, &to
);
2773 dentry
= lookup_create(&nd
, 0);
2774 error
= PTR_ERR(dentry
);
2778 error
= mnt_want_write(nd
.path
.mnt
);
2781 error
= security_path_symlink(&nd
.path
, dentry
, from
);
2783 goto out_drop_write
;
2784 error
= vfs_symlink(nd
.path
.dentry
->d_inode
, dentry
, from
);
2786 mnt_drop_write(nd
.path
.mnt
);
2790 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2798 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
2800 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
2803 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
2805 struct inode
*inode
= old_dentry
->d_inode
;
2811 error
= may_create(dir
, new_dentry
);
2815 if (dir
->i_sb
!= inode
->i_sb
)
2819 * A link to an append-only or immutable file cannot be created.
2821 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2823 if (!dir
->i_op
->link
)
2825 if (S_ISDIR(inode
->i_mode
))
2828 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
2832 mutex_lock(&inode
->i_mutex
);
2833 /* Make sure we don't allow creating hardlink to an unlinked file */
2834 if (inode
->i_nlink
== 0)
2837 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
2838 mutex_unlock(&inode
->i_mutex
);
2840 fsnotify_link(dir
, inode
, new_dentry
);
2845 * Hardlinks are often used in delicate situations. We avoid
2846 * security-related surprises by not following symlinks on the
2849 * We don't follow them on the oldname either to be compatible
2850 * with linux 2.0, and to avoid hard-linking to directories
2851 * and other special files. --ADM
2853 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
2854 int, newdfd
, const char __user
*, newname
, int, flags
)
2856 struct dentry
*new_dentry
;
2857 struct nameidata nd
;
2858 struct path old_path
;
2863 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
2866 * To use null names we require CAP_DAC_READ_SEARCH
2867 * This ensures that not everyone will be able to create
2868 * handlink using the passed filedescriptor.
2870 if (flags
& AT_EMPTY_PATH
) {
2871 if (!capable(CAP_DAC_READ_SEARCH
))
2876 if (flags
& AT_SYMLINK_FOLLOW
)
2877 how
|= LOOKUP_FOLLOW
;
2879 error
= user_path_at(olddfd
, oldname
, how
, &old_path
);
2883 error
= user_path_parent(newdfd
, newname
, &nd
, &to
);
2887 if (old_path
.mnt
!= nd
.path
.mnt
)
2889 new_dentry
= lookup_create(&nd
, 0);
2890 error
= PTR_ERR(new_dentry
);
2891 if (IS_ERR(new_dentry
))
2893 error
= mnt_want_write(nd
.path
.mnt
);
2896 error
= security_path_link(old_path
.dentry
, &nd
.path
, new_dentry
);
2898 goto out_drop_write
;
2899 error
= vfs_link(old_path
.dentry
, nd
.path
.dentry
->d_inode
, new_dentry
);
2901 mnt_drop_write(nd
.path
.mnt
);
2905 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2910 path_put(&old_path
);
2915 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
2917 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
2921 * The worst of all namespace operations - renaming directory. "Perverted"
2922 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2924 * a) we can get into loop creation. Check is done in is_subdir().
2925 * b) race potential - two innocent renames can create a loop together.
2926 * That's where 4.4 screws up. Current fix: serialization on
2927 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2929 * c) we have to lock _three_ objects - parents and victim (if it exists).
2930 * And that - after we got ->i_mutex on parents (until then we don't know
2931 * whether the target exists). Solution: try to be smart with locking
2932 * order for inodes. We rely on the fact that tree topology may change
2933 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
2934 * move will be locked. Thus we can rank directories by the tree
2935 * (ancestors first) and rank all non-directories after them.
2936 * That works since everybody except rename does "lock parent, lookup,
2937 * lock child" and rename is under ->s_vfs_rename_mutex.
2938 * HOWEVER, it relies on the assumption that any object with ->lookup()
2939 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2940 * we'd better make sure that there's no link(2) for them.
2941 * d) conversion from fhandle to dentry may come in the wrong moment - when
2942 * we are removing the target. Solution: we will have to grab ->i_mutex
2943 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2944 * ->i_mutex on parents, which works but leads to some truly excessive
2947 static int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
2948 struct inode
*new_dir
, struct dentry
*new_dentry
)
2951 struct inode
*target
= new_dentry
->d_inode
;
2954 * If we are going to change the parent - check write permissions,
2955 * we'll need to flip '..'.
2957 if (new_dir
!= old_dir
) {
2958 error
= inode_permission(old_dentry
->d_inode
, MAY_WRITE
);
2963 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2968 mutex_lock(&target
->i_mutex
);
2971 if (d_mountpoint(old_dentry
) || d_mountpoint(new_dentry
))
2975 shrink_dcache_parent(new_dentry
);
2976 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2981 target
->i_flags
|= S_DEAD
;
2982 dont_mount(new_dentry
);
2986 mutex_unlock(&target
->i_mutex
);
2988 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
2989 d_move(old_dentry
,new_dentry
);
2993 static int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
2994 struct inode
*new_dir
, struct dentry
*new_dentry
)
2996 struct inode
*target
= new_dentry
->d_inode
;
2999 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3005 mutex_lock(&target
->i_mutex
);
3008 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
3011 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3016 dont_mount(new_dentry
);
3017 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
3018 d_move(old_dentry
, new_dentry
);
3021 mutex_unlock(&target
->i_mutex
);
3026 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
3027 struct inode
*new_dir
, struct dentry
*new_dentry
)
3030 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
3031 const unsigned char *old_name
;
3033 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
3036 error
= may_delete(old_dir
, old_dentry
, is_dir
);
3040 if (!new_dentry
->d_inode
)
3041 error
= may_create(new_dir
, new_dentry
);
3043 error
= may_delete(new_dir
, new_dentry
, is_dir
);
3047 if (!old_dir
->i_op
->rename
)
3050 old_name
= fsnotify_oldname_init(old_dentry
->d_name
.name
);
3053 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
3055 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
3057 fsnotify_move(old_dir
, new_dir
, old_name
, is_dir
,
3058 new_dentry
->d_inode
, old_dentry
);
3059 fsnotify_oldname_free(old_name
);
3064 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
3065 int, newdfd
, const char __user
*, newname
)
3067 struct dentry
*old_dir
, *new_dir
;
3068 struct dentry
*old_dentry
, *new_dentry
;
3069 struct dentry
*trap
;
3070 struct nameidata oldnd
, newnd
;
3075 error
= user_path_parent(olddfd
, oldname
, &oldnd
, &from
);
3079 error
= user_path_parent(newdfd
, newname
, &newnd
, &to
);
3084 if (oldnd
.path
.mnt
!= newnd
.path
.mnt
)
3087 old_dir
= oldnd
.path
.dentry
;
3089 if (oldnd
.last_type
!= LAST_NORM
)
3092 new_dir
= newnd
.path
.dentry
;
3093 if (newnd
.last_type
!= LAST_NORM
)
3096 oldnd
.flags
&= ~LOOKUP_PARENT
;
3097 newnd
.flags
&= ~LOOKUP_PARENT
;
3098 newnd
.flags
|= LOOKUP_RENAME_TARGET
;
3100 trap
= lock_rename(new_dir
, old_dir
);
3102 old_dentry
= lookup_hash(&oldnd
);
3103 error
= PTR_ERR(old_dentry
);
3104 if (IS_ERR(old_dentry
))
3106 /* source must exist */
3108 if (!old_dentry
->d_inode
)
3110 /* unless the source is a directory trailing slashes give -ENOTDIR */
3111 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
3113 if (oldnd
.last
.name
[oldnd
.last
.len
])
3115 if (newnd
.last
.name
[newnd
.last
.len
])
3118 /* source should not be ancestor of target */
3120 if (old_dentry
== trap
)
3122 new_dentry
= lookup_hash(&newnd
);
3123 error
= PTR_ERR(new_dentry
);
3124 if (IS_ERR(new_dentry
))
3126 /* target should not be an ancestor of source */
3128 if (new_dentry
== trap
)
3131 error
= mnt_want_write(oldnd
.path
.mnt
);
3134 error
= security_path_rename(&oldnd
.path
, old_dentry
,
3135 &newnd
.path
, new_dentry
);
3138 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
3139 new_dir
->d_inode
, new_dentry
);
3141 mnt_drop_write(oldnd
.path
.mnt
);
3147 unlock_rename(new_dir
, old_dir
);
3149 path_put(&newnd
.path
);
3152 path_put(&oldnd
.path
);
3158 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
3160 return sys_renameat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
);
3163 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
3167 len
= PTR_ERR(link
);
3172 if (len
> (unsigned) buflen
)
3174 if (copy_to_user(buffer
, link
, len
))
3181 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3182 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3183 * using) it for any given inode is up to filesystem.
3185 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
3187 struct nameidata nd
;
3192 cookie
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
3194 return PTR_ERR(cookie
);
3196 res
= vfs_readlink(dentry
, buffer
, buflen
, nd_get_link(&nd
));
3197 if (dentry
->d_inode
->i_op
->put_link
)
3198 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
, cookie
);
3202 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
3204 return __vfs_follow_link(nd
, link
);
3207 /* get the link contents into pagecache */
3208 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
3212 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
3213 page
= read_mapping_page(mapping
, 0, NULL
);
3218 nd_terminate_link(kaddr
, dentry
->d_inode
->i_size
, PAGE_SIZE
- 1);
3222 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
3224 struct page
*page
= NULL
;
3225 char *s
= page_getlink(dentry
, &page
);
3226 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
3229 page_cache_release(page
);
3234 void *page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
3236 struct page
*page
= NULL
;
3237 nd_set_link(nd
, page_getlink(dentry
, &page
));
3241 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
3243 struct page
*page
= cookie
;
3247 page_cache_release(page
);
3252 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3254 int __page_symlink(struct inode
*inode
, const char *symname
, int len
, int nofs
)
3256 struct address_space
*mapping
= inode
->i_mapping
;
3261 unsigned int flags
= AOP_FLAG_UNINTERRUPTIBLE
;
3263 flags
|= AOP_FLAG_NOFS
;
3266 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
3267 flags
, &page
, &fsdata
);
3271 kaddr
= kmap_atomic(page
, KM_USER0
);
3272 memcpy(kaddr
, symname
, len
-1);
3273 kunmap_atomic(kaddr
, KM_USER0
);
3275 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
3282 mark_inode_dirty(inode
);
3288 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
3290 return __page_symlink(inode
, symname
, len
,
3291 !(mapping_gfp_mask(inode
->i_mapping
) & __GFP_FS
));
3294 const struct inode_operations page_symlink_inode_operations
= {
3295 .readlink
= generic_readlink
,
3296 .follow_link
= page_follow_link_light
,
3297 .put_link
= page_put_link
,
3300 EXPORT_SYMBOL(user_path_at
);
3301 EXPORT_SYMBOL(follow_down_one
);
3302 EXPORT_SYMBOL(follow_down
);
3303 EXPORT_SYMBOL(follow_up
);
3304 EXPORT_SYMBOL(get_write_access
); /* binfmt_aout */
3305 EXPORT_SYMBOL(getname
);
3306 EXPORT_SYMBOL(lock_rename
);
3307 EXPORT_SYMBOL(lookup_one_len
);
3308 EXPORT_SYMBOL(page_follow_link_light
);
3309 EXPORT_SYMBOL(page_put_link
);
3310 EXPORT_SYMBOL(page_readlink
);
3311 EXPORT_SYMBOL(__page_symlink
);
3312 EXPORT_SYMBOL(page_symlink
);
3313 EXPORT_SYMBOL(page_symlink_inode_operations
);
3314 EXPORT_SYMBOL(kern_path_parent
);
3315 EXPORT_SYMBOL(kern_path
);
3316 EXPORT_SYMBOL(vfs_path_lookup
);
3317 EXPORT_SYMBOL(inode_permission
);
3318 EXPORT_SYMBOL(unlock_rename
);
3319 EXPORT_SYMBOL(vfs_create
);
3320 EXPORT_SYMBOL(vfs_follow_link
);
3321 EXPORT_SYMBOL(vfs_link
);
3322 EXPORT_SYMBOL(vfs_mkdir
);
3323 EXPORT_SYMBOL(vfs_mknod
);
3324 EXPORT_SYMBOL(generic_permission
);
3325 EXPORT_SYMBOL(vfs_readlink
);
3326 EXPORT_SYMBOL(vfs_rename
);
3327 EXPORT_SYMBOL(vfs_rmdir
);
3328 EXPORT_SYMBOL(vfs_symlink
);
3329 EXPORT_SYMBOL(vfs_unlink
);
3330 EXPORT_SYMBOL(dentry_unhash
);
3331 EXPORT_SYMBOL(generic_readlink
);