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
);
440 * complete_walk - successful completion of path walk
441 * @nd: pointer nameidata
443 * If we had been in RCU mode, drop out of it and legitimize nd->path.
444 * Revalidate the final result, unless we'd already done that during
445 * the path walk or the filesystem doesn't ask for it. Return 0 on
446 * success, -error on failure. In case of failure caller does not
447 * need to drop nd->path.
449 static int complete_walk(struct nameidata
*nd
)
451 struct dentry
*dentry
= nd
->path
.dentry
;
454 if (nd
->flags
& LOOKUP_RCU
) {
455 nd
->flags
&= ~LOOKUP_RCU
;
456 if (!(nd
->flags
& LOOKUP_ROOT
))
458 spin_lock(&dentry
->d_lock
);
459 if (unlikely(!__d_rcu_to_refcount(dentry
, nd
->seq
))) {
460 spin_unlock(&dentry
->d_lock
);
462 br_read_unlock(vfsmount_lock
);
465 BUG_ON(nd
->inode
!= dentry
->d_inode
);
466 spin_unlock(&dentry
->d_lock
);
467 mntget(nd
->path
.mnt
);
469 br_read_unlock(vfsmount_lock
);
472 if (likely(!(nd
->flags
& LOOKUP_JUMPED
)))
475 if (likely(!(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)))
478 if (likely(!(dentry
->d_sb
->s_type
->fs_flags
& FS_REVAL_DOT
)))
481 /* Note: we do not d_invalidate() */
482 status
= d_revalidate(dentry
, nd
);
493 static __always_inline
void set_root(struct nameidata
*nd
)
496 get_fs_root(current
->fs
, &nd
->root
);
499 static int link_path_walk(const char *, struct nameidata
*);
501 static __always_inline
void set_root_rcu(struct nameidata
*nd
)
504 struct fs_struct
*fs
= current
->fs
;
508 seq
= read_seqcount_begin(&fs
->seq
);
510 nd
->seq
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
511 } while (read_seqcount_retry(&fs
->seq
, seq
));
515 static __always_inline
int __vfs_follow_link(struct nameidata
*nd
, const char *link
)
527 nd
->flags
|= LOOKUP_JUMPED
;
529 nd
->inode
= nd
->path
.dentry
->d_inode
;
531 ret
= link_path_walk(link
, nd
);
535 return PTR_ERR(link
);
538 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
541 if (path
->mnt
!= nd
->path
.mnt
)
545 static inline void path_to_nameidata(const struct path
*path
,
546 struct nameidata
*nd
)
548 if (!(nd
->flags
& LOOKUP_RCU
)) {
549 dput(nd
->path
.dentry
);
550 if (nd
->path
.mnt
!= path
->mnt
)
551 mntput(nd
->path
.mnt
);
553 nd
->path
.mnt
= path
->mnt
;
554 nd
->path
.dentry
= path
->dentry
;
557 static inline void put_link(struct nameidata
*nd
, struct path
*link
, void *cookie
)
559 struct inode
*inode
= link
->dentry
->d_inode
;
560 if (!IS_ERR(cookie
) && inode
->i_op
->put_link
)
561 inode
->i_op
->put_link(link
->dentry
, nd
, cookie
);
565 static __always_inline
int
566 follow_link(struct path
*link
, struct nameidata
*nd
, void **p
)
569 struct dentry
*dentry
= link
->dentry
;
571 BUG_ON(nd
->flags
& LOOKUP_RCU
);
573 if (link
->mnt
== nd
->path
.mnt
)
576 if (unlikely(current
->total_link_count
>= 40)) {
577 *p
= ERR_PTR(-ELOOP
); /* no ->put_link(), please */
582 current
->total_link_count
++;
584 touch_atime(link
->mnt
, dentry
);
585 nd_set_link(nd
, NULL
);
587 error
= security_inode_follow_link(link
->dentry
, nd
);
589 *p
= ERR_PTR(error
); /* no ->put_link(), please */
594 nd
->last_type
= LAST_BIND
;
595 *p
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
598 char *s
= nd_get_link(nd
);
601 error
= __vfs_follow_link(nd
, s
);
602 else if (nd
->last_type
== LAST_BIND
) {
603 nd
->flags
|= LOOKUP_JUMPED
;
604 nd
->inode
= nd
->path
.dentry
->d_inode
;
605 if (nd
->inode
->i_op
->follow_link
) {
606 /* stepped on a _really_ weird one */
615 static int follow_up_rcu(struct path
*path
)
617 struct vfsmount
*parent
;
618 struct dentry
*mountpoint
;
620 parent
= path
->mnt
->mnt_parent
;
621 if (parent
== path
->mnt
)
623 mountpoint
= path
->mnt
->mnt_mountpoint
;
624 path
->dentry
= mountpoint
;
629 int follow_up(struct path
*path
)
631 struct vfsmount
*parent
;
632 struct dentry
*mountpoint
;
634 br_read_lock(vfsmount_lock
);
635 parent
= path
->mnt
->mnt_parent
;
636 if (parent
== path
->mnt
) {
637 br_read_unlock(vfsmount_lock
);
641 mountpoint
= dget(path
->mnt
->mnt_mountpoint
);
642 br_read_unlock(vfsmount_lock
);
644 path
->dentry
= mountpoint
;
651 * Perform an automount
652 * - return -EISDIR to tell follow_managed() to stop and return the path we
655 static int follow_automount(struct path
*path
, unsigned flags
,
658 struct vfsmount
*mnt
;
661 if (!path
->dentry
->d_op
|| !path
->dentry
->d_op
->d_automount
)
664 /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
665 * and this is the terminal part of the path.
667 if ((flags
& LOOKUP_NO_AUTOMOUNT
) && !(flags
& LOOKUP_PARENT
))
668 return -EISDIR
; /* we actually want to stop here */
670 /* We want to mount if someone is trying to open/create a file of any
671 * type under the mountpoint, wants to traverse through the mountpoint
672 * or wants to open the mounted directory.
674 * We don't want to mount if someone's just doing a stat and they've
675 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
676 * appended a '/' to the name.
678 if (!(flags
& LOOKUP_FOLLOW
) &&
679 !(flags
& (LOOKUP_PARENT
| LOOKUP_DIRECTORY
|
680 LOOKUP_OPEN
| LOOKUP_CREATE
)))
683 current
->total_link_count
++;
684 if (current
->total_link_count
>= 40)
687 mnt
= path
->dentry
->d_op
->d_automount(path
);
690 * The filesystem is allowed to return -EISDIR here to indicate
691 * it doesn't want to automount. For instance, autofs would do
692 * this so that its userspace daemon can mount on this dentry.
694 * However, we can only permit this if it's a terminal point in
695 * the path being looked up; if it wasn't then the remainder of
696 * the path is inaccessible and we should say so.
698 if (PTR_ERR(mnt
) == -EISDIR
&& (flags
& LOOKUP_PARENT
))
703 if (!mnt
) /* mount collision */
707 /* lock_mount() may release path->mnt on error */
711 err
= finish_automount(mnt
, path
);
715 /* Someone else made a mount here whilst we were busy */
720 path
->dentry
= dget(mnt
->mnt_root
);
729 * Handle a dentry that is managed in some way.
730 * - Flagged for transit management (autofs)
731 * - Flagged as mountpoint
732 * - Flagged as automount point
734 * This may only be called in refwalk mode.
736 * Serialization is taken care of in namespace.c
738 static int follow_managed(struct path
*path
, unsigned flags
)
740 struct vfsmount
*mnt
= path
->mnt
; /* held by caller, must be left alone */
742 bool need_mntput
= false;
745 /* Given that we're not holding a lock here, we retain the value in a
746 * local variable for each dentry as we look at it so that we don't see
747 * the components of that value change under us */
748 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
749 managed
&= DCACHE_MANAGED_DENTRY
,
750 unlikely(managed
!= 0)) {
751 /* Allow the filesystem to manage the transit without i_mutex
753 if (managed
& DCACHE_MANAGE_TRANSIT
) {
754 BUG_ON(!path
->dentry
->d_op
);
755 BUG_ON(!path
->dentry
->d_op
->d_manage
);
756 ret
= path
->dentry
->d_op
->d_manage(path
->dentry
, false);
761 /* Transit to a mounted filesystem. */
762 if (managed
& DCACHE_MOUNTED
) {
763 struct vfsmount
*mounted
= lookup_mnt(path
);
769 path
->dentry
= dget(mounted
->mnt_root
);
774 /* Something is mounted on this dentry in another
775 * namespace and/or whatever was mounted there in this
776 * namespace got unmounted before we managed to get the
780 /* Handle an automount point */
781 if (managed
& DCACHE_NEED_AUTOMOUNT
) {
782 ret
= follow_automount(path
, flags
, &need_mntput
);
788 /* We didn't change the current path point */
792 if (need_mntput
&& path
->mnt
== mnt
)
799 int follow_down_one(struct path
*path
)
801 struct vfsmount
*mounted
;
803 mounted
= lookup_mnt(path
);
808 path
->dentry
= dget(mounted
->mnt_root
);
814 static inline bool managed_dentry_might_block(struct dentry
*dentry
)
816 return (dentry
->d_flags
& DCACHE_MANAGE_TRANSIT
&&
817 dentry
->d_op
->d_manage(dentry
, true) < 0);
821 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
822 * we meet a managed dentry that would need blocking.
824 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
825 struct inode
**inode
)
828 struct vfsmount
*mounted
;
830 * Don't forget we might have a non-mountpoint managed dentry
831 * that wants to block transit.
833 if (unlikely(managed_dentry_might_block(path
->dentry
)))
836 if (!d_mountpoint(path
->dentry
))
839 mounted
= __lookup_mnt(path
->mnt
, path
->dentry
, 1);
843 path
->dentry
= mounted
->mnt_root
;
844 nd
->seq
= read_seqcount_begin(&path
->dentry
->d_seq
);
846 * Update the inode too. We don't need to re-check the
847 * dentry sequence number here after this d_inode read,
848 * because a mount-point is always pinned.
850 *inode
= path
->dentry
->d_inode
;
855 static void follow_mount_rcu(struct nameidata
*nd
)
857 while (d_mountpoint(nd
->path
.dentry
)) {
858 struct vfsmount
*mounted
;
859 mounted
= __lookup_mnt(nd
->path
.mnt
, nd
->path
.dentry
, 1);
862 nd
->path
.mnt
= mounted
;
863 nd
->path
.dentry
= mounted
->mnt_root
;
864 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
868 static int follow_dotdot_rcu(struct nameidata
*nd
)
873 if (nd
->path
.dentry
== nd
->root
.dentry
&&
874 nd
->path
.mnt
== nd
->root
.mnt
) {
877 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
878 struct dentry
*old
= nd
->path
.dentry
;
879 struct dentry
*parent
= old
->d_parent
;
882 seq
= read_seqcount_begin(&parent
->d_seq
);
883 if (read_seqcount_retry(&old
->d_seq
, nd
->seq
))
885 nd
->path
.dentry
= parent
;
889 if (!follow_up_rcu(&nd
->path
))
891 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
893 follow_mount_rcu(nd
);
894 nd
->inode
= nd
->path
.dentry
->d_inode
;
898 nd
->flags
&= ~LOOKUP_RCU
;
899 if (!(nd
->flags
& LOOKUP_ROOT
))
902 br_read_unlock(vfsmount_lock
);
907 * Follow down to the covering mount currently visible to userspace. At each
908 * point, the filesystem owning that dentry may be queried as to whether the
909 * caller is permitted to proceed or not.
911 int follow_down(struct path
*path
)
916 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
917 unlikely(managed
& DCACHE_MANAGED_DENTRY
)) {
918 /* Allow the filesystem to manage the transit without i_mutex
921 * We indicate to the filesystem if someone is trying to mount
922 * something here. This gives autofs the chance to deny anyone
923 * other than its daemon the right to mount on its
926 * The filesystem may sleep at this point.
928 if (managed
& DCACHE_MANAGE_TRANSIT
) {
929 BUG_ON(!path
->dentry
->d_op
);
930 BUG_ON(!path
->dentry
->d_op
->d_manage
);
931 ret
= path
->dentry
->d_op
->d_manage(
932 path
->dentry
, false);
934 return ret
== -EISDIR
? 0 : ret
;
937 /* Transit to a mounted filesystem. */
938 if (managed
& DCACHE_MOUNTED
) {
939 struct vfsmount
*mounted
= lookup_mnt(path
);
945 path
->dentry
= dget(mounted
->mnt_root
);
949 /* Don't handle automount points here */
956 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
958 static void follow_mount(struct path
*path
)
960 while (d_mountpoint(path
->dentry
)) {
961 struct vfsmount
*mounted
= lookup_mnt(path
);
967 path
->dentry
= dget(mounted
->mnt_root
);
971 static void follow_dotdot(struct nameidata
*nd
)
976 struct dentry
*old
= nd
->path
.dentry
;
978 if (nd
->path
.dentry
== nd
->root
.dentry
&&
979 nd
->path
.mnt
== nd
->root
.mnt
) {
982 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
983 /* rare case of legitimate dget_parent()... */
984 nd
->path
.dentry
= dget_parent(nd
->path
.dentry
);
988 if (!follow_up(&nd
->path
))
991 follow_mount(&nd
->path
);
992 nd
->inode
= nd
->path
.dentry
->d_inode
;
996 * Allocate a dentry with name and parent, and perform a parent
997 * directory ->lookup on it. Returns the new dentry, or ERR_PTR
998 * on error. parent->d_inode->i_mutex must be held. d_lookup must
999 * have verified that no child exists while under i_mutex.
1001 static struct dentry
*d_alloc_and_lookup(struct dentry
*parent
,
1002 struct qstr
*name
, struct nameidata
*nd
)
1004 struct inode
*inode
= parent
->d_inode
;
1005 struct dentry
*dentry
;
1008 /* Don't create child dentry for a dead directory. */
1009 if (unlikely(IS_DEADDIR(inode
)))
1010 return ERR_PTR(-ENOENT
);
1012 dentry
= d_alloc(parent
, name
);
1013 if (unlikely(!dentry
))
1014 return ERR_PTR(-ENOMEM
);
1016 old
= inode
->i_op
->lookup(inode
, dentry
, nd
);
1017 if (unlikely(old
)) {
1025 * We already have a dentry, but require a lookup to be performed on the parent
1026 * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
1027 * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
1028 * child exists while under i_mutex.
1030 static struct dentry
*d_inode_lookup(struct dentry
*parent
, struct dentry
*dentry
,
1031 struct nameidata
*nd
)
1033 struct inode
*inode
= parent
->d_inode
;
1036 /* Don't create child dentry for a dead directory. */
1037 if (unlikely(IS_DEADDIR(inode
)))
1038 return ERR_PTR(-ENOENT
);
1040 old
= inode
->i_op
->lookup(inode
, dentry
, nd
);
1041 if (unlikely(old
)) {
1049 * It's more convoluted than I'd like it to be, but... it's still fairly
1050 * small and for now I'd prefer to have fast path as straight as possible.
1051 * It _is_ time-critical.
1053 static int do_lookup(struct nameidata
*nd
, struct qstr
*name
,
1054 struct path
*path
, struct inode
**inode
)
1056 struct vfsmount
*mnt
= nd
->path
.mnt
;
1057 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1063 * Rename seqlock is not required here because in the off chance
1064 * of a false negative due to a concurrent rename, we're going to
1065 * do the non-racy lookup, below.
1067 if (nd
->flags
& LOOKUP_RCU
) {
1070 dentry
= __d_lookup_rcu(parent
, name
, &seq
, inode
);
1074 /* Memory barrier in read_seqcount_begin of child is enough */
1075 if (__read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
1079 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)) {
1080 status
= d_revalidate(dentry
, nd
);
1081 if (unlikely(status
<= 0)) {
1082 if (status
!= -ECHILD
)
1087 if (unlikely(d_need_lookup(dentry
)))
1090 path
->dentry
= dentry
;
1091 if (unlikely(!__follow_mount_rcu(nd
, path
, inode
)))
1093 if (unlikely(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
))
1097 if (unlazy_walk(nd
, dentry
))
1100 dentry
= __d_lookup(parent
, name
);
1103 if (dentry
&& unlikely(d_need_lookup(dentry
))) {
1108 if (unlikely(!dentry
)) {
1109 struct inode
*dir
= parent
->d_inode
;
1110 BUG_ON(nd
->inode
!= dir
);
1112 mutex_lock(&dir
->i_mutex
);
1113 dentry
= d_lookup(parent
, name
);
1114 if (likely(!dentry
)) {
1115 dentry
= d_alloc_and_lookup(parent
, name
, nd
);
1116 if (IS_ERR(dentry
)) {
1117 mutex_unlock(&dir
->i_mutex
);
1118 return PTR_ERR(dentry
);
1123 } else if (unlikely(d_need_lookup(dentry
))) {
1124 dentry
= d_inode_lookup(parent
, dentry
, nd
);
1125 if (IS_ERR(dentry
)) {
1126 mutex_unlock(&dir
->i_mutex
);
1127 return PTR_ERR(dentry
);
1133 mutex_unlock(&dir
->i_mutex
);
1135 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
) && need_reval
)
1136 status
= d_revalidate(dentry
, nd
);
1137 if (unlikely(status
<= 0)) {
1142 if (!d_invalidate(dentry
)) {
1151 path
->dentry
= dentry
;
1152 err
= follow_managed(path
, nd
->flags
);
1153 if (unlikely(err
< 0)) {
1154 path_put_conditional(path
, nd
);
1157 *inode
= path
->dentry
->d_inode
;
1161 static inline int may_lookup(struct nameidata
*nd
)
1163 if (nd
->flags
& LOOKUP_RCU
) {
1164 int err
= inode_permission(nd
->inode
, MAY_EXEC
|MAY_NOT_BLOCK
);
1167 if (unlazy_walk(nd
, NULL
))
1170 return inode_permission(nd
->inode
, MAY_EXEC
);
1173 static inline int handle_dots(struct nameidata
*nd
, int type
)
1175 if (type
== LAST_DOTDOT
) {
1176 if (nd
->flags
& LOOKUP_RCU
) {
1177 if (follow_dotdot_rcu(nd
))
1185 static void terminate_walk(struct nameidata
*nd
)
1187 if (!(nd
->flags
& LOOKUP_RCU
)) {
1188 path_put(&nd
->path
);
1190 nd
->flags
&= ~LOOKUP_RCU
;
1191 if (!(nd
->flags
& LOOKUP_ROOT
))
1192 nd
->root
.mnt
= NULL
;
1194 br_read_unlock(vfsmount_lock
);
1198 static inline int walk_component(struct nameidata
*nd
, struct path
*path
,
1199 struct qstr
*name
, int type
, int follow
)
1201 struct inode
*inode
;
1204 * "." and ".." are special - ".." especially so because it has
1205 * to be able to know about the current root directory and
1206 * parent relationships.
1208 if (unlikely(type
!= LAST_NORM
))
1209 return handle_dots(nd
, type
);
1210 err
= do_lookup(nd
, name
, path
, &inode
);
1211 if (unlikely(err
)) {
1216 path_to_nameidata(path
, nd
);
1220 if (unlikely(inode
->i_op
->follow_link
) && follow
) {
1221 if (nd
->flags
& LOOKUP_RCU
) {
1222 if (unlikely(unlazy_walk(nd
, path
->dentry
))) {
1227 BUG_ON(inode
!= path
->dentry
->d_inode
);
1230 path_to_nameidata(path
, nd
);
1236 * This limits recursive symlink follows to 8, while
1237 * limiting consecutive symlinks to 40.
1239 * Without that kind of total limit, nasty chains of consecutive
1240 * symlinks can cause almost arbitrarily long lookups.
1242 static inline int nested_symlink(struct path
*path
, struct nameidata
*nd
)
1246 if (unlikely(current
->link_count
>= MAX_NESTED_LINKS
)) {
1247 path_put_conditional(path
, nd
);
1248 path_put(&nd
->path
);
1251 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
1254 current
->link_count
++;
1257 struct path link
= *path
;
1260 res
= follow_link(&link
, nd
, &cookie
);
1262 res
= walk_component(nd
, path
, &nd
->last
,
1263 nd
->last_type
, LOOKUP_FOLLOW
);
1264 put_link(nd
, &link
, cookie
);
1267 current
->link_count
--;
1274 * This is the basic name resolution function, turning a pathname into
1275 * the final dentry. We expect 'base' to be positive and a directory.
1277 * Returns 0 and nd will have valid dentry and mnt on success.
1278 * Returns error and drops reference to input namei data on failure.
1280 static int link_path_walk(const char *name
, struct nameidata
*nd
)
1290 /* At this point we know we have a real path component. */
1297 err
= may_lookup(nd
);
1302 c
= *(const unsigned char *)name
;
1304 hash
= init_name_hash();
1307 hash
= partial_name_hash(c
, hash
);
1308 c
= *(const unsigned char *)name
;
1309 } while (c
&& (c
!= '/'));
1310 this.len
= name
- (const char *) this.name
;
1311 this.hash
= end_name_hash(hash
);
1314 if (this.name
[0] == '.') switch (this.len
) {
1316 if (this.name
[1] == '.') {
1318 nd
->flags
|= LOOKUP_JUMPED
;
1324 if (likely(type
== LAST_NORM
)) {
1325 struct dentry
*parent
= nd
->path
.dentry
;
1326 nd
->flags
&= ~LOOKUP_JUMPED
;
1327 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
1328 err
= parent
->d_op
->d_hash(parent
, nd
->inode
,
1335 /* remove trailing slashes? */
1337 goto last_component
;
1338 while (*++name
== '/');
1340 goto last_component
;
1342 err
= walk_component(nd
, &next
, &this, type
, LOOKUP_FOLLOW
);
1347 err
= nested_symlink(&next
, nd
);
1352 if (!nd
->inode
->i_op
->lookup
)
1355 /* here ends the main loop */
1359 nd
->last_type
= type
;
1366 static int path_init(int dfd
, const char *name
, unsigned int flags
,
1367 struct nameidata
*nd
, struct file
**fp
)
1373 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
1374 nd
->flags
= flags
| LOOKUP_JUMPED
;
1376 if (flags
& LOOKUP_ROOT
) {
1377 struct inode
*inode
= nd
->root
.dentry
->d_inode
;
1379 if (!inode
->i_op
->lookup
)
1381 retval
= inode_permission(inode
, MAY_EXEC
);
1385 nd
->path
= nd
->root
;
1387 if (flags
& LOOKUP_RCU
) {
1388 br_read_lock(vfsmount_lock
);
1390 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1392 path_get(&nd
->path
);
1397 nd
->root
.mnt
= NULL
;
1400 if (flags
& LOOKUP_RCU
) {
1401 br_read_lock(vfsmount_lock
);
1406 path_get(&nd
->root
);
1408 nd
->path
= nd
->root
;
1409 } else if (dfd
== AT_FDCWD
) {
1410 if (flags
& LOOKUP_RCU
) {
1411 struct fs_struct
*fs
= current
->fs
;
1414 br_read_lock(vfsmount_lock
);
1418 seq
= read_seqcount_begin(&fs
->seq
);
1420 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1421 } while (read_seqcount_retry(&fs
->seq
, seq
));
1423 get_fs_pwd(current
->fs
, &nd
->path
);
1426 struct dentry
*dentry
;
1428 file
= fget_raw_light(dfd
, &fput_needed
);
1433 dentry
= file
->f_path
.dentry
;
1437 if (!S_ISDIR(dentry
->d_inode
->i_mode
))
1440 retval
= inode_permission(dentry
->d_inode
, MAY_EXEC
);
1445 nd
->path
= file
->f_path
;
1446 if (flags
& LOOKUP_RCU
) {
1449 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1450 br_read_lock(vfsmount_lock
);
1453 path_get(&file
->f_path
);
1454 fput_light(file
, fput_needed
);
1458 nd
->inode
= nd
->path
.dentry
->d_inode
;
1462 fput_light(file
, fput_needed
);
1467 static inline int lookup_last(struct nameidata
*nd
, struct path
*path
)
1469 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
1470 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
1472 nd
->flags
&= ~LOOKUP_PARENT
;
1473 return walk_component(nd
, path
, &nd
->last
, nd
->last_type
,
1474 nd
->flags
& LOOKUP_FOLLOW
);
1477 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1478 static int path_lookupat(int dfd
, const char *name
,
1479 unsigned int flags
, struct nameidata
*nd
)
1481 struct file
*base
= NULL
;
1486 * Path walking is largely split up into 2 different synchronisation
1487 * schemes, rcu-walk and ref-walk (explained in
1488 * Documentation/filesystems/path-lookup.txt). These share much of the
1489 * path walk code, but some things particularly setup, cleanup, and
1490 * following mounts are sufficiently divergent that functions are
1491 * duplicated. Typically there is a function foo(), and its RCU
1492 * analogue, foo_rcu().
1494 * -ECHILD is the error number of choice (just to avoid clashes) that
1495 * is returned if some aspect of an rcu-walk fails. Such an error must
1496 * be handled by restarting a traditional ref-walk (which will always
1497 * be able to complete).
1499 err
= path_init(dfd
, name
, flags
| LOOKUP_PARENT
, nd
, &base
);
1504 current
->total_link_count
= 0;
1505 err
= link_path_walk(name
, nd
);
1507 if (!err
&& !(flags
& LOOKUP_PARENT
)) {
1508 err
= lookup_last(nd
, &path
);
1511 struct path link
= path
;
1512 nd
->flags
|= LOOKUP_PARENT
;
1513 err
= follow_link(&link
, nd
, &cookie
);
1515 err
= lookup_last(nd
, &path
);
1516 put_link(nd
, &link
, cookie
);
1521 err
= complete_walk(nd
);
1523 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
) {
1524 if (!nd
->inode
->i_op
->lookup
) {
1525 path_put(&nd
->path
);
1533 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
1534 path_put(&nd
->root
);
1535 nd
->root
.mnt
= NULL
;
1540 static int do_path_lookup(int dfd
, const char *name
,
1541 unsigned int flags
, struct nameidata
*nd
)
1543 int retval
= path_lookupat(dfd
, name
, flags
| LOOKUP_RCU
, nd
);
1544 if (unlikely(retval
== -ECHILD
))
1545 retval
= path_lookupat(dfd
, name
, flags
, nd
);
1546 if (unlikely(retval
== -ESTALE
))
1547 retval
= path_lookupat(dfd
, name
, flags
| LOOKUP_REVAL
, nd
);
1549 if (likely(!retval
)) {
1550 if (unlikely(!audit_dummy_context())) {
1551 if (nd
->path
.dentry
&& nd
->inode
)
1552 audit_inode(name
, nd
->path
.dentry
);
1558 int kern_path_parent(const char *name
, struct nameidata
*nd
)
1560 return do_path_lookup(AT_FDCWD
, name
, LOOKUP_PARENT
, nd
);
1563 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
1565 struct nameidata nd
;
1566 int res
= do_path_lookup(AT_FDCWD
, name
, flags
, &nd
);
1573 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1574 * @dentry: pointer to dentry of the base directory
1575 * @mnt: pointer to vfs mount of the base directory
1576 * @name: pointer to file name
1577 * @flags: lookup flags
1578 * @path: pointer to struct path to fill
1580 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
1581 const char *name
, unsigned int flags
,
1584 struct nameidata nd
;
1586 nd
.root
.dentry
= dentry
;
1588 BUG_ON(flags
& LOOKUP_PARENT
);
1589 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1590 err
= do_path_lookup(AT_FDCWD
, name
, flags
| LOOKUP_ROOT
, &nd
);
1596 static struct dentry
*__lookup_hash(struct qstr
*name
,
1597 struct dentry
*base
, struct nameidata
*nd
)
1599 struct inode
*inode
= base
->d_inode
;
1600 struct dentry
*dentry
;
1603 err
= inode_permission(inode
, MAY_EXEC
);
1605 return ERR_PTR(err
);
1608 * Don't bother with __d_lookup: callers are for creat as
1609 * well as unlink, so a lot of the time it would cost
1612 dentry
= d_lookup(base
, name
);
1614 if (dentry
&& d_need_lookup(dentry
)) {
1616 * __lookup_hash is called with the parent dir's i_mutex already
1617 * held, so we are good to go here.
1619 dentry
= d_inode_lookup(base
, dentry
, nd
);
1624 if (dentry
&& (dentry
->d_flags
& DCACHE_OP_REVALIDATE
)) {
1625 int status
= d_revalidate(dentry
, nd
);
1626 if (unlikely(status
<= 0)) {
1628 * The dentry failed validation.
1629 * If d_revalidate returned 0 attempt to invalidate
1630 * the dentry otherwise d_revalidate is asking us
1631 * to return a fail status.
1635 return ERR_PTR(status
);
1636 } else if (!d_invalidate(dentry
)) {
1644 dentry
= d_alloc_and_lookup(base
, name
, nd
);
1650 * Restricted form of lookup. Doesn't follow links, single-component only,
1651 * needs parent already locked. Doesn't follow mounts.
1654 static struct dentry
*lookup_hash(struct nameidata
*nd
)
1656 return __lookup_hash(&nd
->last
, nd
->path
.dentry
, nd
);
1660 * lookup_one_len - filesystem helper to lookup single pathname component
1661 * @name: pathname component to lookup
1662 * @base: base directory to lookup from
1663 * @len: maximum length @len should be interpreted to
1665 * Note that this routine is purely a helper for filesystem usage and should
1666 * not be called by generic code. Also note that by using this function the
1667 * nameidata argument is passed to the filesystem methods and a filesystem
1668 * using this helper needs to be prepared for that.
1670 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
1676 WARN_ON_ONCE(!mutex_is_locked(&base
->d_inode
->i_mutex
));
1681 return ERR_PTR(-EACCES
);
1683 hash
= init_name_hash();
1685 c
= *(const unsigned char *)name
++;
1686 if (c
== '/' || c
== '\0')
1687 return ERR_PTR(-EACCES
);
1688 hash
= partial_name_hash(c
, hash
);
1690 this.hash
= end_name_hash(hash
);
1692 * See if the low-level filesystem might want
1693 * to use its own hash..
1695 if (base
->d_flags
& DCACHE_OP_HASH
) {
1696 int err
= base
->d_op
->d_hash(base
, base
->d_inode
, &this);
1698 return ERR_PTR(err
);
1701 return __lookup_hash(&this, base
, NULL
);
1704 int user_path_at(int dfd
, const char __user
*name
, unsigned flags
,
1707 struct nameidata nd
;
1708 char *tmp
= getname_flags(name
, flags
);
1709 int err
= PTR_ERR(tmp
);
1712 BUG_ON(flags
& LOOKUP_PARENT
);
1714 err
= do_path_lookup(dfd
, tmp
, flags
, &nd
);
1722 static int user_path_parent(int dfd
, const char __user
*path
,
1723 struct nameidata
*nd
, char **name
)
1725 char *s
= getname(path
);
1731 error
= do_path_lookup(dfd
, s
, LOOKUP_PARENT
, nd
);
1741 * It's inline, so penalty for filesystems that don't use sticky bit is
1744 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
1746 uid_t fsuid
= current_fsuid();
1748 if (!(dir
->i_mode
& S_ISVTX
))
1750 if (current_user_ns() != inode_userns(inode
))
1752 if (inode
->i_uid
== fsuid
)
1754 if (dir
->i_uid
== fsuid
)
1758 return !ns_capable(inode_userns(inode
), CAP_FOWNER
);
1762 * Check whether we can remove a link victim from directory dir, check
1763 * whether the type of victim is right.
1764 * 1. We can't do it if dir is read-only (done in permission())
1765 * 2. We should have write and exec permissions on dir
1766 * 3. We can't remove anything from append-only dir
1767 * 4. We can't do anything with immutable dir (done in permission())
1768 * 5. If the sticky bit on dir is set we should either
1769 * a. be owner of dir, or
1770 * b. be owner of victim, or
1771 * c. have CAP_FOWNER capability
1772 * 6. If the victim is append-only or immutable we can't do antyhing with
1773 * links pointing to it.
1774 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1775 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1776 * 9. We can't remove a root or mountpoint.
1777 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1778 * nfs_async_unlink().
1780 static int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
1784 if (!victim
->d_inode
)
1787 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
1788 audit_inode_child(victim
, dir
);
1790 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
1795 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
1796 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
1799 if (!S_ISDIR(victim
->d_inode
->i_mode
))
1801 if (IS_ROOT(victim
))
1803 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
1805 if (IS_DEADDIR(dir
))
1807 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
1812 /* Check whether we can create an object with dentry child in directory
1814 * 1. We can't do it if child already exists (open has special treatment for
1815 * this case, but since we are inlined it's OK)
1816 * 2. We can't do it if dir is read-only (done in permission())
1817 * 3. We should have write and exec permissions on dir
1818 * 4. We can't do it if dir is immutable (done in permission())
1820 static inline int may_create(struct inode
*dir
, struct dentry
*child
)
1824 if (IS_DEADDIR(dir
))
1826 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
1830 * p1 and p2 should be directories on the same fs.
1832 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
1837 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1841 mutex_lock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1843 p
= d_ancestor(p2
, p1
);
1845 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1846 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
1850 p
= d_ancestor(p1
, p2
);
1852 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
1853 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
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 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
1864 mutex_unlock(&p1
->d_inode
->i_mutex
);
1866 mutex_unlock(&p2
->d_inode
->i_mutex
);
1867 mutex_unlock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
1871 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
1872 struct nameidata
*nd
)
1874 int error
= may_create(dir
, dentry
);
1879 if (!dir
->i_op
->create
)
1880 return -EACCES
; /* shouldn't it be ENOSYS? */
1883 error
= security_inode_create(dir
, dentry
, mode
);
1886 error
= dir
->i_op
->create(dir
, dentry
, mode
, nd
);
1888 fsnotify_create(dir
, dentry
);
1892 static int may_open(struct path
*path
, int acc_mode
, int flag
)
1894 struct dentry
*dentry
= path
->dentry
;
1895 struct inode
*inode
= dentry
->d_inode
;
1905 switch (inode
->i_mode
& S_IFMT
) {
1909 if (acc_mode
& MAY_WRITE
)
1914 if (path
->mnt
->mnt_flags
& MNT_NODEV
)
1923 error
= inode_permission(inode
, acc_mode
);
1928 * An append-only file must be opened in append mode for writing.
1930 if (IS_APPEND(inode
)) {
1931 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
1937 /* O_NOATIME can only be set by the owner or superuser */
1938 if (flag
& O_NOATIME
&& !inode_owner_or_capable(inode
))
1942 * Ensure there are no outstanding leases on the file.
1944 return break_lease(inode
, flag
);
1947 static int handle_truncate(struct file
*filp
)
1949 struct path
*path
= &filp
->f_path
;
1950 struct inode
*inode
= path
->dentry
->d_inode
;
1951 int error
= get_write_access(inode
);
1955 * Refuse to truncate files with mandatory locks held on them.
1957 error
= locks_verify_locked(inode
);
1959 error
= security_path_truncate(path
);
1961 error
= do_truncate(path
->dentry
, 0,
1962 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
1965 put_write_access(inode
);
1969 static inline int open_to_namei_flags(int flag
)
1971 if ((flag
& O_ACCMODE
) == 3)
1977 * Handle the last step of open()
1979 static struct file
*do_last(struct nameidata
*nd
, struct path
*path
,
1980 const struct open_flags
*op
, const char *pathname
)
1982 struct dentry
*dir
= nd
->path
.dentry
;
1983 struct dentry
*dentry
;
1984 int open_flag
= op
->open_flag
;
1985 int will_truncate
= open_flag
& O_TRUNC
;
1987 int acc_mode
= op
->acc_mode
;
1991 nd
->flags
&= ~LOOKUP_PARENT
;
1992 nd
->flags
|= op
->intent
;
1994 switch (nd
->last_type
) {
1997 error
= handle_dots(nd
, nd
->last_type
);
1999 return ERR_PTR(error
);
2002 error
= complete_walk(nd
);
2004 return ERR_PTR(error
);
2005 audit_inode(pathname
, nd
->path
.dentry
);
2006 if (open_flag
& O_CREAT
) {
2012 error
= complete_walk(nd
);
2014 return ERR_PTR(error
);
2015 audit_inode(pathname
, dir
);
2019 if (!(open_flag
& O_CREAT
)) {
2021 if (nd
->last
.name
[nd
->last
.len
])
2022 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2023 if (open_flag
& O_PATH
&& !(nd
->flags
& LOOKUP_FOLLOW
))
2025 /* we _can_ be in RCU mode here */
2026 error
= walk_component(nd
, path
, &nd
->last
, LAST_NORM
,
2029 return ERR_PTR(error
);
2030 if (error
) /* symlink */
2033 error
= complete_walk(nd
);
2035 return ERR_PTR(-ECHILD
);
2038 if (nd
->flags
& LOOKUP_DIRECTORY
) {
2039 if (!nd
->inode
->i_op
->lookup
)
2042 audit_inode(pathname
, nd
->path
.dentry
);
2046 /* create side of things */
2047 error
= complete_walk(nd
);
2049 return ERR_PTR(error
);
2051 audit_inode(pathname
, dir
);
2053 /* trailing slashes? */
2054 if (nd
->last
.name
[nd
->last
.len
])
2057 mutex_lock(&dir
->d_inode
->i_mutex
);
2059 dentry
= lookup_hash(nd
);
2060 error
= PTR_ERR(dentry
);
2061 if (IS_ERR(dentry
)) {
2062 mutex_unlock(&dir
->d_inode
->i_mutex
);
2066 path
->dentry
= dentry
;
2067 path
->mnt
= nd
->path
.mnt
;
2069 /* Negative dentry, just create the file */
2070 if (!dentry
->d_inode
) {
2071 int mode
= op
->mode
;
2072 if (!IS_POSIXACL(dir
->d_inode
))
2073 mode
&= ~current_umask();
2075 * This write is needed to ensure that a
2076 * rw->ro transition does not occur between
2077 * the time when the file is created and when
2078 * a permanent write count is taken through
2079 * the 'struct file' in nameidata_to_filp().
2081 error
= mnt_want_write(nd
->path
.mnt
);
2083 goto exit_mutex_unlock
;
2085 /* Don't check for write permission, don't truncate */
2086 open_flag
&= ~O_TRUNC
;
2088 acc_mode
= MAY_OPEN
;
2089 error
= security_path_mknod(&nd
->path
, dentry
, mode
, 0);
2091 goto exit_mutex_unlock
;
2092 error
= vfs_create(dir
->d_inode
, dentry
, mode
, nd
);
2094 goto exit_mutex_unlock
;
2095 mutex_unlock(&dir
->d_inode
->i_mutex
);
2096 dput(nd
->path
.dentry
);
2097 nd
->path
.dentry
= dentry
;
2102 * It already exists.
2104 mutex_unlock(&dir
->d_inode
->i_mutex
);
2105 audit_inode(pathname
, path
->dentry
);
2108 if (open_flag
& O_EXCL
)
2111 error
= follow_managed(path
, nd
->flags
);
2116 if (!path
->dentry
->d_inode
)
2119 if (path
->dentry
->d_inode
->i_op
->follow_link
)
2122 path_to_nameidata(path
, nd
);
2123 nd
->inode
= path
->dentry
->d_inode
;
2125 if (S_ISDIR(nd
->inode
->i_mode
))
2128 if (!S_ISREG(nd
->inode
->i_mode
))
2131 if (will_truncate
) {
2132 error
= mnt_want_write(nd
->path
.mnt
);
2138 error
= may_open(&nd
->path
, acc_mode
, open_flag
);
2141 filp
= nameidata_to_filp(nd
);
2142 if (!IS_ERR(filp
)) {
2143 error
= ima_file_check(filp
, op
->acc_mode
);
2146 filp
= ERR_PTR(error
);
2149 if (!IS_ERR(filp
)) {
2150 if (will_truncate
) {
2151 error
= handle_truncate(filp
);
2154 filp
= ERR_PTR(error
);
2160 mnt_drop_write(nd
->path
.mnt
);
2161 path_put(&nd
->path
);
2165 mutex_unlock(&dir
->d_inode
->i_mutex
);
2167 path_put_conditional(path
, nd
);
2169 filp
= ERR_PTR(error
);
2173 static struct file
*path_openat(int dfd
, const char *pathname
,
2174 struct nameidata
*nd
, const struct open_flags
*op
, int flags
)
2176 struct file
*base
= NULL
;
2181 filp
= get_empty_filp();
2183 return ERR_PTR(-ENFILE
);
2185 filp
->f_flags
= op
->open_flag
;
2186 nd
->intent
.open
.file
= filp
;
2187 nd
->intent
.open
.flags
= open_to_namei_flags(op
->open_flag
);
2188 nd
->intent
.open
.create_mode
= op
->mode
;
2190 error
= path_init(dfd
, pathname
, flags
| LOOKUP_PARENT
, nd
, &base
);
2191 if (unlikely(error
))
2194 current
->total_link_count
= 0;
2195 error
= link_path_walk(pathname
, nd
);
2196 if (unlikely(error
))
2199 filp
= do_last(nd
, &path
, op
, pathname
);
2200 while (unlikely(!filp
)) { /* trailing symlink */
2201 struct path link
= path
;
2203 if (!(nd
->flags
& LOOKUP_FOLLOW
)) {
2204 path_put_conditional(&path
, nd
);
2205 path_put(&nd
->path
);
2206 filp
= ERR_PTR(-ELOOP
);
2209 nd
->flags
|= LOOKUP_PARENT
;
2210 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
2211 error
= follow_link(&link
, nd
, &cookie
);
2212 if (unlikely(error
))
2213 filp
= ERR_PTR(error
);
2215 filp
= do_last(nd
, &path
, op
, pathname
);
2216 put_link(nd
, &link
, cookie
);
2219 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
))
2220 path_put(&nd
->root
);
2223 release_open_intent(nd
);
2227 filp
= ERR_PTR(error
);
2231 struct file
*do_filp_open(int dfd
, const char *pathname
,
2232 const struct open_flags
*op
, int flags
)
2234 struct nameidata nd
;
2237 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_RCU
);
2238 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
2239 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
);
2240 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
2241 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_REVAL
);
2245 struct file
*do_file_open_root(struct dentry
*dentry
, struct vfsmount
*mnt
,
2246 const char *name
, const struct open_flags
*op
, int flags
)
2248 struct nameidata nd
;
2252 nd
.root
.dentry
= dentry
;
2254 flags
|= LOOKUP_ROOT
;
2256 if (dentry
->d_inode
->i_op
->follow_link
&& op
->intent
& LOOKUP_OPEN
)
2257 return ERR_PTR(-ELOOP
);
2259 file
= path_openat(-1, name
, &nd
, op
, flags
| LOOKUP_RCU
);
2260 if (unlikely(file
== ERR_PTR(-ECHILD
)))
2261 file
= path_openat(-1, name
, &nd
, op
, flags
);
2262 if (unlikely(file
== ERR_PTR(-ESTALE
)))
2263 file
= path_openat(-1, name
, &nd
, op
, flags
| LOOKUP_REVAL
);
2267 struct dentry
*kern_path_create(int dfd
, const char *pathname
, struct path
*path
, int is_dir
)
2269 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
2270 struct nameidata nd
;
2271 int error
= do_path_lookup(dfd
, pathname
, LOOKUP_PARENT
, &nd
);
2273 return ERR_PTR(error
);
2276 * Yucky last component or no last component at all?
2277 * (foo/., foo/.., /////)
2279 if (nd
.last_type
!= LAST_NORM
)
2281 nd
.flags
&= ~LOOKUP_PARENT
;
2282 nd
.flags
|= LOOKUP_CREATE
| LOOKUP_EXCL
;
2283 nd
.intent
.open
.flags
= O_EXCL
;
2286 * Do the final lookup.
2288 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2289 dentry
= lookup_hash(&nd
);
2293 if (dentry
->d_inode
)
2296 * Special case - lookup gave negative, but... we had foo/bar/
2297 * From the vfs_mknod() POV we just have a negative dentry -
2298 * all is fine. Let's be bastards - you had / on the end, you've
2299 * been asking for (non-existent) directory. -ENOENT for you.
2301 if (unlikely(!is_dir
&& nd
.last
.name
[nd
.last
.len
])) {
2303 dentry
= ERR_PTR(-ENOENT
);
2310 dentry
= ERR_PTR(-EEXIST
);
2312 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2317 EXPORT_SYMBOL(kern_path_create
);
2319 struct dentry
*user_path_create(int dfd
, const char __user
*pathname
, struct path
*path
, int is_dir
)
2321 char *tmp
= getname(pathname
);
2324 return ERR_CAST(tmp
);
2325 res
= kern_path_create(dfd
, tmp
, path
, is_dir
);
2329 EXPORT_SYMBOL(user_path_create
);
2331 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
2333 int error
= may_create(dir
, dentry
);
2338 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) &&
2339 !ns_capable(inode_userns(dir
), CAP_MKNOD
))
2342 if (!dir
->i_op
->mknod
)
2345 error
= devcgroup_inode_mknod(mode
, dev
);
2349 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
2353 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
2355 fsnotify_create(dir
, dentry
);
2359 static int may_mknod(mode_t mode
)
2361 switch (mode
& S_IFMT
) {
2367 case 0: /* zero mode translates to S_IFREG */
2376 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, int, mode
,
2379 struct dentry
*dentry
;
2386 dentry
= user_path_create(dfd
, filename
, &path
, 0);
2388 return PTR_ERR(dentry
);
2390 if (!IS_POSIXACL(path
.dentry
->d_inode
))
2391 mode
&= ~current_umask();
2392 error
= may_mknod(mode
);
2395 error
= mnt_want_write(path
.mnt
);
2398 error
= security_path_mknod(&path
, dentry
, mode
, dev
);
2400 goto out_drop_write
;
2401 switch (mode
& S_IFMT
) {
2402 case 0: case S_IFREG
:
2403 error
= vfs_create(path
.dentry
->d_inode
,dentry
,mode
,NULL
);
2405 case S_IFCHR
: case S_IFBLK
:
2406 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,
2407 new_decode_dev(dev
));
2409 case S_IFIFO
: case S_IFSOCK
:
2410 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,0);
2414 mnt_drop_write(path
.mnt
);
2417 mutex_unlock(&path
.dentry
->d_inode
->i_mutex
);
2423 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, int, mode
, unsigned, dev
)
2425 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
2428 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
2430 int error
= may_create(dir
, dentry
);
2435 if (!dir
->i_op
->mkdir
)
2438 mode
&= (S_IRWXUGO
|S_ISVTX
);
2439 error
= security_inode_mkdir(dir
, dentry
, mode
);
2443 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
2445 fsnotify_mkdir(dir
, dentry
);
2449 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, int, mode
)
2451 struct dentry
*dentry
;
2455 dentry
= user_path_create(dfd
, pathname
, &path
, 1);
2457 return PTR_ERR(dentry
);
2459 if (!IS_POSIXACL(path
.dentry
->d_inode
))
2460 mode
&= ~current_umask();
2461 error
= mnt_want_write(path
.mnt
);
2464 error
= security_path_mkdir(&path
, dentry
, mode
);
2466 goto out_drop_write
;
2467 error
= vfs_mkdir(path
.dentry
->d_inode
, dentry
, mode
);
2469 mnt_drop_write(path
.mnt
);
2472 mutex_unlock(&path
.dentry
->d_inode
->i_mutex
);
2477 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, int, mode
)
2479 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
2483 * The dentry_unhash() helper will try to drop the dentry early: we
2484 * should have a usage count of 2 if we're the only user of this
2485 * dentry, and if that is true (possibly after pruning the dcache),
2486 * then we drop the dentry now.
2488 * A low-level filesystem can, if it choses, legally
2491 * if (!d_unhashed(dentry))
2494 * if it cannot handle the case of removing a directory
2495 * that is still in use by something else..
2497 void dentry_unhash(struct dentry
*dentry
)
2499 shrink_dcache_parent(dentry
);
2500 spin_lock(&dentry
->d_lock
);
2501 if (dentry
->d_count
== 1)
2503 spin_unlock(&dentry
->d_lock
);
2506 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2508 int error
= may_delete(dir
, dentry
, 1);
2513 if (!dir
->i_op
->rmdir
)
2516 mutex_lock(&dentry
->d_inode
->i_mutex
);
2519 if (d_mountpoint(dentry
))
2522 error
= security_inode_rmdir(dir
, dentry
);
2526 shrink_dcache_parent(dentry
);
2527 error
= dir
->i_op
->rmdir(dir
, dentry
);
2531 dentry
->d_inode
->i_flags
|= S_DEAD
;
2535 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2541 static long do_rmdir(int dfd
, const char __user
*pathname
)
2545 struct dentry
*dentry
;
2546 struct nameidata nd
;
2548 error
= user_path_parent(dfd
, pathname
, &nd
, &name
);
2552 switch(nd
.last_type
) {
2564 nd
.flags
&= ~LOOKUP_PARENT
;
2566 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2567 dentry
= lookup_hash(&nd
);
2568 error
= PTR_ERR(dentry
);
2571 if (!dentry
->d_inode
) {
2575 error
= mnt_want_write(nd
.path
.mnt
);
2578 error
= security_path_rmdir(&nd
.path
, dentry
);
2581 error
= vfs_rmdir(nd
.path
.dentry
->d_inode
, dentry
);
2583 mnt_drop_write(nd
.path
.mnt
);
2587 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2594 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
2596 return do_rmdir(AT_FDCWD
, pathname
);
2599 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
2601 int error
= may_delete(dir
, dentry
, 0);
2606 if (!dir
->i_op
->unlink
)
2609 mutex_lock(&dentry
->d_inode
->i_mutex
);
2610 if (d_mountpoint(dentry
))
2613 error
= security_inode_unlink(dir
, dentry
);
2615 error
= dir
->i_op
->unlink(dir
, dentry
);
2620 mutex_unlock(&dentry
->d_inode
->i_mutex
);
2622 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2623 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
2624 fsnotify_link_count(dentry
->d_inode
);
2632 * Make sure that the actual truncation of the file will occur outside its
2633 * directory's i_mutex. Truncate can take a long time if there is a lot of
2634 * writeout happening, and we don't want to prevent access to the directory
2635 * while waiting on the I/O.
2637 static long do_unlinkat(int dfd
, const char __user
*pathname
)
2641 struct dentry
*dentry
;
2642 struct nameidata nd
;
2643 struct inode
*inode
= NULL
;
2645 error
= user_path_parent(dfd
, pathname
, &nd
, &name
);
2650 if (nd
.last_type
!= LAST_NORM
)
2653 nd
.flags
&= ~LOOKUP_PARENT
;
2655 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2656 dentry
= lookup_hash(&nd
);
2657 error
= PTR_ERR(dentry
);
2658 if (!IS_ERR(dentry
)) {
2659 /* Why not before? Because we want correct error value */
2660 if (nd
.last
.name
[nd
.last
.len
])
2662 inode
= dentry
->d_inode
;
2666 error
= mnt_want_write(nd
.path
.mnt
);
2669 error
= security_path_unlink(&nd
.path
, dentry
);
2672 error
= vfs_unlink(nd
.path
.dentry
->d_inode
, dentry
);
2674 mnt_drop_write(nd
.path
.mnt
);
2678 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2680 iput(inode
); /* truncate the inode here */
2687 error
= !dentry
->d_inode
? -ENOENT
:
2688 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
2692 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
2694 if ((flag
& ~AT_REMOVEDIR
) != 0)
2697 if (flag
& AT_REMOVEDIR
)
2698 return do_rmdir(dfd
, pathname
);
2700 return do_unlinkat(dfd
, pathname
);
2703 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
2705 return do_unlinkat(AT_FDCWD
, pathname
);
2708 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
2710 int error
= may_create(dir
, dentry
);
2715 if (!dir
->i_op
->symlink
)
2718 error
= security_inode_symlink(dir
, dentry
, oldname
);
2722 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
2724 fsnotify_create(dir
, dentry
);
2728 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
2729 int, newdfd
, const char __user
*, newname
)
2733 struct dentry
*dentry
;
2736 from
= getname(oldname
);
2738 return PTR_ERR(from
);
2740 dentry
= user_path_create(newdfd
, newname
, &path
, 0);
2741 error
= PTR_ERR(dentry
);
2745 error
= mnt_want_write(path
.mnt
);
2748 error
= security_path_symlink(&path
, dentry
, from
);
2750 goto out_drop_write
;
2751 error
= vfs_symlink(path
.dentry
->d_inode
, dentry
, from
);
2753 mnt_drop_write(path
.mnt
);
2756 mutex_unlock(&path
.dentry
->d_inode
->i_mutex
);
2763 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
2765 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
2768 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
2770 struct inode
*inode
= old_dentry
->d_inode
;
2776 error
= may_create(dir
, new_dentry
);
2780 if (dir
->i_sb
!= inode
->i_sb
)
2784 * A link to an append-only or immutable file cannot be created.
2786 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
2788 if (!dir
->i_op
->link
)
2790 if (S_ISDIR(inode
->i_mode
))
2793 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
2797 mutex_lock(&inode
->i_mutex
);
2798 /* Make sure we don't allow creating hardlink to an unlinked file */
2799 if (inode
->i_nlink
== 0)
2802 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
2803 mutex_unlock(&inode
->i_mutex
);
2805 fsnotify_link(dir
, inode
, new_dentry
);
2810 * Hardlinks are often used in delicate situations. We avoid
2811 * security-related surprises by not following symlinks on the
2814 * We don't follow them on the oldname either to be compatible
2815 * with linux 2.0, and to avoid hard-linking to directories
2816 * and other special files. --ADM
2818 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
2819 int, newdfd
, const char __user
*, newname
, int, flags
)
2821 struct dentry
*new_dentry
;
2822 struct path old_path
, new_path
;
2826 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
2829 * To use null names we require CAP_DAC_READ_SEARCH
2830 * This ensures that not everyone will be able to create
2831 * handlink using the passed filedescriptor.
2833 if (flags
& AT_EMPTY_PATH
) {
2834 if (!capable(CAP_DAC_READ_SEARCH
))
2839 if (flags
& AT_SYMLINK_FOLLOW
)
2840 how
|= LOOKUP_FOLLOW
;
2842 error
= user_path_at(olddfd
, oldname
, how
, &old_path
);
2846 new_dentry
= user_path_create(newdfd
, newname
, &new_path
, 0);
2847 error
= PTR_ERR(new_dentry
);
2848 if (IS_ERR(new_dentry
))
2852 if (old_path
.mnt
!= new_path
.mnt
)
2854 error
= mnt_want_write(new_path
.mnt
);
2857 error
= security_path_link(old_path
.dentry
, &new_path
, new_dentry
);
2859 goto out_drop_write
;
2860 error
= vfs_link(old_path
.dentry
, new_path
.dentry
->d_inode
, new_dentry
);
2862 mnt_drop_write(new_path
.mnt
);
2865 mutex_unlock(&new_path
.dentry
->d_inode
->i_mutex
);
2866 path_put(&new_path
);
2868 path_put(&old_path
);
2873 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
2875 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
2879 * The worst of all namespace operations - renaming directory. "Perverted"
2880 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2882 * a) we can get into loop creation. Check is done in is_subdir().
2883 * b) race potential - two innocent renames can create a loop together.
2884 * That's where 4.4 screws up. Current fix: serialization on
2885 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
2887 * c) we have to lock _three_ objects - parents and victim (if it exists).
2888 * And that - after we got ->i_mutex on parents (until then we don't know
2889 * whether the target exists). Solution: try to be smart with locking
2890 * order for inodes. We rely on the fact that tree topology may change
2891 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
2892 * move will be locked. Thus we can rank directories by the tree
2893 * (ancestors first) and rank all non-directories after them.
2894 * That works since everybody except rename does "lock parent, lookup,
2895 * lock child" and rename is under ->s_vfs_rename_mutex.
2896 * HOWEVER, it relies on the assumption that any object with ->lookup()
2897 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2898 * we'd better make sure that there's no link(2) for them.
2899 * d) conversion from fhandle to dentry may come in the wrong moment - when
2900 * we are removing the target. Solution: we will have to grab ->i_mutex
2901 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2902 * ->i_mutex on parents, which works but leads to some truly excessive
2905 static int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
2906 struct inode
*new_dir
, struct dentry
*new_dentry
)
2909 struct inode
*target
= new_dentry
->d_inode
;
2912 * If we are going to change the parent - check write permissions,
2913 * we'll need to flip '..'.
2915 if (new_dir
!= old_dir
) {
2916 error
= inode_permission(old_dentry
->d_inode
, MAY_WRITE
);
2921 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2926 mutex_lock(&target
->i_mutex
);
2929 if (d_mountpoint(old_dentry
) || d_mountpoint(new_dentry
))
2933 shrink_dcache_parent(new_dentry
);
2934 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2939 target
->i_flags
|= S_DEAD
;
2940 dont_mount(new_dentry
);
2944 mutex_unlock(&target
->i_mutex
);
2946 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
2947 d_move(old_dentry
,new_dentry
);
2951 static int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
2952 struct inode
*new_dir
, struct dentry
*new_dentry
)
2954 struct inode
*target
= new_dentry
->d_inode
;
2957 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2963 mutex_lock(&target
->i_mutex
);
2966 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
2969 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
2974 dont_mount(new_dentry
);
2975 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
2976 d_move(old_dentry
, new_dentry
);
2979 mutex_unlock(&target
->i_mutex
);
2984 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
2985 struct inode
*new_dir
, struct dentry
*new_dentry
)
2988 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
2989 const unsigned char *old_name
;
2991 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
2994 error
= may_delete(old_dir
, old_dentry
, is_dir
);
2998 if (!new_dentry
->d_inode
)
2999 error
= may_create(new_dir
, new_dentry
);
3001 error
= may_delete(new_dir
, new_dentry
, is_dir
);
3005 if (!old_dir
->i_op
->rename
)
3008 old_name
= fsnotify_oldname_init(old_dentry
->d_name
.name
);
3011 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
3013 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
3015 fsnotify_move(old_dir
, new_dir
, old_name
, is_dir
,
3016 new_dentry
->d_inode
, old_dentry
);
3017 fsnotify_oldname_free(old_name
);
3022 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
3023 int, newdfd
, const char __user
*, newname
)
3025 struct dentry
*old_dir
, *new_dir
;
3026 struct dentry
*old_dentry
, *new_dentry
;
3027 struct dentry
*trap
;
3028 struct nameidata oldnd
, newnd
;
3033 error
= user_path_parent(olddfd
, oldname
, &oldnd
, &from
);
3037 error
= user_path_parent(newdfd
, newname
, &newnd
, &to
);
3042 if (oldnd
.path
.mnt
!= newnd
.path
.mnt
)
3045 old_dir
= oldnd
.path
.dentry
;
3047 if (oldnd
.last_type
!= LAST_NORM
)
3050 new_dir
= newnd
.path
.dentry
;
3051 if (newnd
.last_type
!= LAST_NORM
)
3054 oldnd
.flags
&= ~LOOKUP_PARENT
;
3055 newnd
.flags
&= ~LOOKUP_PARENT
;
3056 newnd
.flags
|= LOOKUP_RENAME_TARGET
;
3058 trap
= lock_rename(new_dir
, old_dir
);
3060 old_dentry
= lookup_hash(&oldnd
);
3061 error
= PTR_ERR(old_dentry
);
3062 if (IS_ERR(old_dentry
))
3064 /* source must exist */
3066 if (!old_dentry
->d_inode
)
3068 /* unless the source is a directory trailing slashes give -ENOTDIR */
3069 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
3071 if (oldnd
.last
.name
[oldnd
.last
.len
])
3073 if (newnd
.last
.name
[newnd
.last
.len
])
3076 /* source should not be ancestor of target */
3078 if (old_dentry
== trap
)
3080 new_dentry
= lookup_hash(&newnd
);
3081 error
= PTR_ERR(new_dentry
);
3082 if (IS_ERR(new_dentry
))
3084 /* target should not be an ancestor of source */
3086 if (new_dentry
== trap
)
3089 error
= mnt_want_write(oldnd
.path
.mnt
);
3092 error
= security_path_rename(&oldnd
.path
, old_dentry
,
3093 &newnd
.path
, new_dentry
);
3096 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
3097 new_dir
->d_inode
, new_dentry
);
3099 mnt_drop_write(oldnd
.path
.mnt
);
3105 unlock_rename(new_dir
, old_dir
);
3107 path_put(&newnd
.path
);
3110 path_put(&oldnd
.path
);
3116 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
3118 return sys_renameat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
);
3121 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
3125 len
= PTR_ERR(link
);
3130 if (len
> (unsigned) buflen
)
3132 if (copy_to_user(buffer
, link
, len
))
3139 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
3140 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
3141 * using) it for any given inode is up to filesystem.
3143 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
3145 struct nameidata nd
;
3150 cookie
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
3152 return PTR_ERR(cookie
);
3154 res
= vfs_readlink(dentry
, buffer
, buflen
, nd_get_link(&nd
));
3155 if (dentry
->d_inode
->i_op
->put_link
)
3156 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
, cookie
);
3160 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
3162 return __vfs_follow_link(nd
, link
);
3165 /* get the link contents into pagecache */
3166 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
3170 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
3171 page
= read_mapping_page(mapping
, 0, NULL
);
3176 nd_terminate_link(kaddr
, dentry
->d_inode
->i_size
, PAGE_SIZE
- 1);
3180 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
3182 struct page
*page
= NULL
;
3183 char *s
= page_getlink(dentry
, &page
);
3184 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
3187 page_cache_release(page
);
3192 void *page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
3194 struct page
*page
= NULL
;
3195 nd_set_link(nd
, page_getlink(dentry
, &page
));
3199 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
3201 struct page
*page
= cookie
;
3205 page_cache_release(page
);
3210 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
3212 int __page_symlink(struct inode
*inode
, const char *symname
, int len
, int nofs
)
3214 struct address_space
*mapping
= inode
->i_mapping
;
3219 unsigned int flags
= AOP_FLAG_UNINTERRUPTIBLE
;
3221 flags
|= AOP_FLAG_NOFS
;
3224 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
3225 flags
, &page
, &fsdata
);
3229 kaddr
= kmap_atomic(page
, KM_USER0
);
3230 memcpy(kaddr
, symname
, len
-1);
3231 kunmap_atomic(kaddr
, KM_USER0
);
3233 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
3240 mark_inode_dirty(inode
);
3246 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
3248 return __page_symlink(inode
, symname
, len
,
3249 !(mapping_gfp_mask(inode
->i_mapping
) & __GFP_FS
));
3252 const struct inode_operations page_symlink_inode_operations
= {
3253 .readlink
= generic_readlink
,
3254 .follow_link
= page_follow_link_light
,
3255 .put_link
= page_put_link
,
3258 EXPORT_SYMBOL(user_path_at
);
3259 EXPORT_SYMBOL(follow_down_one
);
3260 EXPORT_SYMBOL(follow_down
);
3261 EXPORT_SYMBOL(follow_up
);
3262 EXPORT_SYMBOL(get_write_access
); /* binfmt_aout */
3263 EXPORT_SYMBOL(getname
);
3264 EXPORT_SYMBOL(lock_rename
);
3265 EXPORT_SYMBOL(lookup_one_len
);
3266 EXPORT_SYMBOL(page_follow_link_light
);
3267 EXPORT_SYMBOL(page_put_link
);
3268 EXPORT_SYMBOL(page_readlink
);
3269 EXPORT_SYMBOL(__page_symlink
);
3270 EXPORT_SYMBOL(page_symlink
);
3271 EXPORT_SYMBOL(page_symlink_inode_operations
);
3272 EXPORT_SYMBOL(kern_path
);
3273 EXPORT_SYMBOL(vfs_path_lookup
);
3274 EXPORT_SYMBOL(inode_permission
);
3275 EXPORT_SYMBOL(unlock_rename
);
3276 EXPORT_SYMBOL(vfs_create
);
3277 EXPORT_SYMBOL(vfs_follow_link
);
3278 EXPORT_SYMBOL(vfs_link
);
3279 EXPORT_SYMBOL(vfs_mkdir
);
3280 EXPORT_SYMBOL(vfs_mknod
);
3281 EXPORT_SYMBOL(generic_permission
);
3282 EXPORT_SYMBOL(vfs_readlink
);
3283 EXPORT_SYMBOL(vfs_rename
);
3284 EXPORT_SYMBOL(vfs_rmdir
);
3285 EXPORT_SYMBOL(vfs_symlink
);
3286 EXPORT_SYMBOL(vfs_unlink
);
3287 EXPORT_SYMBOL(dentry_unhash
);
3288 EXPORT_SYMBOL(generic_readlink
);