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/export.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
22 #include <linux/namei.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/ima.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/device_cgroup.h>
35 #include <linux/fs_struct.h>
36 #include <linux/posix_acl.h>
37 #include <linux/hash.h>
38 #include <asm/uaccess.h>
43 /* [Feb-1997 T. Schoebel-Theuer]
44 * Fundamental changes in the pathname lookup mechanisms (namei)
45 * were necessary because of omirr. The reason is that omirr needs
46 * to know the _real_ pathname, not the user-supplied one, in case
47 * of symlinks (and also when transname replacements occur).
49 * The new code replaces the old recursive symlink resolution with
50 * an iterative one (in case of non-nested symlink chains). It does
51 * this with calls to <fs>_follow_link().
52 * As a side effect, dir_namei(), _namei() and follow_link() are now
53 * replaced with a single function lookup_dentry() that can handle all
54 * the special cases of the former code.
56 * With the new dcache, the pathname is stored at each inode, at least as
57 * long as the refcount of the inode is positive. As a side effect, the
58 * size of the dcache depends on the inode cache and thus is dynamic.
60 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
61 * resolution to correspond with current state of the code.
63 * Note that the symlink resolution is not *completely* iterative.
64 * There is still a significant amount of tail- and mid- recursion in
65 * the algorithm. Also, note that <fs>_readlink() is not used in
66 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
67 * may return different results than <fs>_follow_link(). Many virtual
68 * filesystems (including /proc) exhibit this behavior.
71 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
72 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
73 * and the name already exists in form of a symlink, try to create the new
74 * name indicated by the symlink. The old code always complained that the
75 * name already exists, due to not following the symlink even if its target
76 * is nonexistent. The new semantics affects also mknod() and link() when
77 * the name is a symlink pointing to a non-existent name.
79 * I don't know which semantics is the right one, since I have no access
80 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
81 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
82 * "old" one. Personally, I think the new semantics is much more logical.
83 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
84 * file does succeed in both HP-UX and SunOs, but not in Solaris
85 * and in the old Linux semantics.
88 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
89 * semantics. See the comments in "open_namei" and "do_link" below.
91 * [10-Sep-98 Alan Modra] Another symlink change.
94 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
95 * inside the path - always follow.
96 * in the last component in creation/removal/renaming - never follow.
97 * if LOOKUP_FOLLOW passed - follow.
98 * if the pathname has trailing slashes - follow.
99 * otherwise - don't follow.
100 * (applied in that order).
102 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
103 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
104 * During the 2.4 we need to fix the userland stuff depending on it -
105 * hopefully we will be able to get rid of that wart in 2.5. So far only
106 * XEmacs seems to be relying on it...
109 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
110 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
111 * any extra contention...
114 /* In order to reduce some races, while at the same time doing additional
115 * checking and hopefully speeding things up, we copy filenames to the
116 * kernel data space before using them..
118 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
119 * PATH_MAX includes the nul terminator --RR.
121 void final_putname(struct filename
*name
)
123 if (name
->separate
) {
124 __putname(name
->name
);
131 #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
133 static struct filename
*
134 getname_flags(const char __user
*filename
, int flags
, int *empty
)
136 struct filename
*result
, *err
;
141 result
= audit_reusename(filename
);
145 result
= __getname();
146 if (unlikely(!result
))
147 return ERR_PTR(-ENOMEM
);
150 * First, try to embed the struct filename inside the names_cache
153 kname
= (char *)result
+ sizeof(*result
);
154 result
->name
= kname
;
155 result
->separate
= false;
156 max
= EMBEDDED_NAME_MAX
;
159 len
= strncpy_from_user(kname
, filename
, max
);
160 if (unlikely(len
< 0)) {
166 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
167 * separate struct filename so we can dedicate the entire
168 * names_cache allocation for the pathname, and re-do the copy from
171 if (len
== EMBEDDED_NAME_MAX
&& max
== EMBEDDED_NAME_MAX
) {
172 kname
= (char *)result
;
174 result
= kzalloc(sizeof(*result
), GFP_KERNEL
);
176 err
= ERR_PTR(-ENOMEM
);
177 result
= (struct filename
*)kname
;
180 result
->name
= kname
;
181 result
->separate
= true;
186 /* The empty path is special. */
187 if (unlikely(!len
)) {
190 err
= ERR_PTR(-ENOENT
);
191 if (!(flags
& LOOKUP_EMPTY
))
195 err
= ERR_PTR(-ENAMETOOLONG
);
196 if (unlikely(len
>= PATH_MAX
))
199 result
->uptr
= filename
;
200 audit_getname(result
);
204 final_putname(result
);
209 getname(const char __user
* filename
)
211 return getname_flags(filename
, 0, NULL
);
213 EXPORT_SYMBOL(getname
);
215 #ifdef CONFIG_AUDITSYSCALL
216 void putname(struct filename
*name
)
218 if (unlikely(!audit_dummy_context()))
219 return audit_putname(name
);
224 static int check_acl(struct inode
*inode
, int mask
)
226 #ifdef CONFIG_FS_POSIX_ACL
227 struct posix_acl
*acl
;
229 if (mask
& MAY_NOT_BLOCK
) {
230 acl
= get_cached_acl_rcu(inode
, ACL_TYPE_ACCESS
);
233 /* no ->get_acl() calls in RCU mode... */
234 if (acl
== ACL_NOT_CACHED
)
236 return posix_acl_permission(inode
, acl
, mask
& ~MAY_NOT_BLOCK
);
239 acl
= get_cached_acl(inode
, ACL_TYPE_ACCESS
);
242 * A filesystem can force a ACL callback by just never filling the
243 * ACL cache. But normally you'd fill the cache either at inode
244 * instantiation time, or on the first ->get_acl call.
246 * If the filesystem doesn't have a get_acl() function at all, we'll
247 * just create the negative cache entry.
249 if (acl
== ACL_NOT_CACHED
) {
250 if (inode
->i_op
->get_acl
) {
251 acl
= inode
->i_op
->get_acl(inode
, ACL_TYPE_ACCESS
);
255 set_cached_acl(inode
, ACL_TYPE_ACCESS
, NULL
);
261 int error
= posix_acl_permission(inode
, acl
, mask
);
262 posix_acl_release(acl
);
271 * This does the basic permission checking
273 static int acl_permission_check(struct inode
*inode
, int mask
)
275 unsigned int mode
= inode
->i_mode
;
277 if (likely(uid_eq(current_fsuid(), inode
->i_uid
)))
280 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
)) {
281 int error
= check_acl(inode
, mask
);
282 if (error
!= -EAGAIN
)
286 if (in_group_p(inode
->i_gid
))
291 * If the DACs are ok we don't need any capability check.
293 if ((mask
& ~mode
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
)) == 0)
299 * generic_permission - check for access rights on a Posix-like filesystem
300 * @inode: inode to check access rights for
301 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
303 * Used to check for read/write/execute permissions on a file.
304 * We use "fsuid" for this, letting us set arbitrary permissions
305 * for filesystem access without changing the "normal" uids which
306 * are used for other things.
308 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
309 * request cannot be satisfied (eg. requires blocking or too much complexity).
310 * It would then be called again in ref-walk mode.
312 int generic_permission(struct inode
*inode
, int mask
)
317 * Do the basic permission checks.
319 ret
= acl_permission_check(inode
, mask
);
323 if (S_ISDIR(inode
->i_mode
)) {
324 /* DACs are overridable for directories */
325 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
327 if (!(mask
& MAY_WRITE
))
328 if (capable_wrt_inode_uidgid(inode
,
329 CAP_DAC_READ_SEARCH
))
334 * Read/write DACs are always overridable.
335 * Executable DACs are overridable when there is
336 * at least one exec bit set.
338 if (!(mask
& MAY_EXEC
) || (inode
->i_mode
& S_IXUGO
))
339 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
343 * Searching includes executable on directories, else just read.
345 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
346 if (mask
== MAY_READ
)
347 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_READ_SEARCH
))
354 * We _really_ want to just do "generic_permission()" without
355 * even looking at the inode->i_op values. So we keep a cache
356 * flag in inode->i_opflags, that says "this has not special
357 * permission function, use the fast case".
359 static inline int do_inode_permission(struct vfsmount
*mnt
, struct inode
*inode
, int mask
)
361 if (unlikely(!(inode
->i_opflags
& IOP_FASTPERM
))) {
362 if (likely(mnt
&& inode
->i_op
->permission2
))
363 return inode
->i_op
->permission2(mnt
, inode
, mask
);
364 if (likely(inode
->i_op
->permission
))
365 return inode
->i_op
->permission(inode
, mask
);
367 /* This gets set once for the inode lifetime */
368 spin_lock(&inode
->i_lock
);
369 inode
->i_opflags
|= IOP_FASTPERM
;
370 spin_unlock(&inode
->i_lock
);
372 return generic_permission(inode
, mask
);
376 * __inode_permission - Check for access rights to a given inode
377 * @inode: Inode to check permission on
378 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
380 * Check for read/write/execute permissions on an inode.
382 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
384 * This does not check for a read-only file system. You probably want
385 * inode_permission().
387 int __inode_permission2(struct vfsmount
*mnt
, struct inode
*inode
, int mask
)
391 if (unlikely(mask
& MAY_WRITE
)) {
393 * Nobody gets write access to an immutable file.
395 if (IS_IMMUTABLE(inode
))
399 retval
= do_inode_permission(mnt
, inode
, mask
);
403 retval
= devcgroup_inode_permission(inode
, mask
);
407 return security_inode_permission(inode
, mask
);
409 EXPORT_SYMBOL(__inode_permission2
);
411 int __inode_permission(struct inode
*inode
, int mask
)
413 return __inode_permission2(NULL
, inode
, mask
);
415 EXPORT_SYMBOL(__inode_permission
);
418 * sb_permission - Check superblock-level permissions
419 * @sb: Superblock of inode to check permission on
420 * @inode: Inode to check permission on
421 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
423 * Separate out file-system wide checks from inode-specific permission checks.
425 static int sb_permission(struct super_block
*sb
, struct inode
*inode
, int mask
)
427 if (unlikely(mask
& MAY_WRITE
)) {
428 umode_t mode
= inode
->i_mode
;
430 /* Nobody gets write access to a read-only fs. */
431 if ((sb
->s_flags
& MS_RDONLY
) &&
432 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
439 * inode_permission - Check for access rights to a given inode
440 * @inode: Inode to check permission on
441 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
443 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
444 * this, letting us set arbitrary permissions for filesystem access without
445 * changing the "normal" UIDs which are used for other things.
447 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
449 int inode_permission2(struct vfsmount
*mnt
, struct inode
*inode
, int mask
)
453 retval
= sb_permission(inode
->i_sb
, inode
, mask
);
456 return __inode_permission2(mnt
, inode
, mask
);
458 EXPORT_SYMBOL(inode_permission2
);
460 int inode_permission(struct inode
*inode
, int mask
)
462 return inode_permission2(NULL
, inode
, mask
);
464 EXPORT_SYMBOL(inode_permission
);
467 * path_get - get a reference to a path
468 * @path: path to get the reference to
470 * Given a path increment the reference count to the dentry and the vfsmount.
472 void path_get(const struct path
*path
)
477 EXPORT_SYMBOL(path_get
);
480 * path_put - put a reference to a path
481 * @path: path to put the reference to
483 * Given a path decrement the reference count to the dentry and the vfsmount.
485 void path_put(const struct path
*path
)
490 EXPORT_SYMBOL(path_put
);
493 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
494 * @path: nameidate to verify
496 * Rename can sometimes move a file or directory outside of a bind
497 * mount, path_connected allows those cases to be detected.
499 static bool path_connected(const struct path
*path
)
501 struct vfsmount
*mnt
= path
->mnt
;
503 /* Only bind mounts can have disconnected paths */
504 if (mnt
->mnt_root
== mnt
->mnt_sb
->s_root
)
507 return is_subdir(path
->dentry
, mnt
->mnt_root
);
511 * Path walking has 2 modes, rcu-walk and ref-walk (see
512 * Documentation/filesystems/path-lookup.txt). In situations when we can't
513 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
514 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
515 * mode. Refcounts are grabbed at the last known good point before rcu-walk
516 * got stuck, so ref-walk may continue from there. If this is not successful
517 * (eg. a seqcount has changed), then failure is returned and it's up to caller
518 * to restart the path walk from the beginning in ref-walk mode.
521 static inline void lock_rcu_walk(void)
523 br_read_lock(&vfsmount_lock
);
527 static inline void unlock_rcu_walk(void)
530 br_read_unlock(&vfsmount_lock
);
534 * unlazy_walk - try to switch to ref-walk mode.
535 * @nd: nameidata pathwalk data
536 * @dentry: child of nd->path.dentry or NULL
537 * Returns: 0 on success, -ECHILD on failure
539 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
540 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
541 * @nd or NULL. Must be called from rcu-walk context.
543 static int unlazy_walk(struct nameidata
*nd
, struct dentry
*dentry
)
545 struct fs_struct
*fs
= current
->fs
;
546 struct dentry
*parent
= nd
->path
.dentry
;
549 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
550 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
552 spin_lock(&fs
->lock
);
553 if (nd
->root
.mnt
!= fs
->root
.mnt
||
554 nd
->root
.dentry
!= fs
->root
.dentry
)
557 spin_lock(&parent
->d_lock
);
559 if (!__d_rcu_to_refcount(parent
, nd
->seq
))
561 BUG_ON(nd
->inode
!= parent
->d_inode
);
563 if (dentry
->d_parent
!= parent
)
565 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
566 if (!__d_rcu_to_refcount(dentry
, nd
->seq
))
569 * If the sequence check on the child dentry passed, then
570 * the child has not been removed from its parent. This
571 * means the parent dentry must be valid and able to take
572 * a reference at this point.
574 BUG_ON(!IS_ROOT(dentry
) && dentry
->d_parent
!= parent
);
575 BUG_ON(!parent
->d_count
);
577 spin_unlock(&dentry
->d_lock
);
579 spin_unlock(&parent
->d_lock
);
582 spin_unlock(&fs
->lock
);
584 mntget(nd
->path
.mnt
);
587 nd
->flags
&= ~LOOKUP_RCU
;
591 spin_unlock(&dentry
->d_lock
);
593 spin_unlock(&parent
->d_lock
);
596 spin_unlock(&fs
->lock
);
600 static inline int d_revalidate(struct dentry
*dentry
, unsigned int flags
)
602 return dentry
->d_op
->d_revalidate(dentry
, flags
);
606 * complete_walk - successful completion of path walk
607 * @nd: pointer nameidata
609 * If we had been in RCU mode, drop out of it and legitimize nd->path.
610 * Revalidate the final result, unless we'd already done that during
611 * the path walk or the filesystem doesn't ask for it. Return 0 on
612 * success, -error on failure. In case of failure caller does not
613 * need to drop nd->path.
615 static int complete_walk(struct nameidata
*nd
)
617 struct dentry
*dentry
= nd
->path
.dentry
;
620 if (nd
->flags
& LOOKUP_RCU
) {
621 nd
->flags
&= ~LOOKUP_RCU
;
622 if (!(nd
->flags
& LOOKUP_ROOT
))
624 spin_lock(&dentry
->d_lock
);
625 if (unlikely(!__d_rcu_to_refcount(dentry
, nd
->seq
))) {
626 spin_unlock(&dentry
->d_lock
);
630 BUG_ON(nd
->inode
!= dentry
->d_inode
);
631 spin_unlock(&dentry
->d_lock
);
632 mntget(nd
->path
.mnt
);
636 if (likely(!(nd
->flags
& LOOKUP_JUMPED
)))
639 if (likely(!(dentry
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
)))
642 status
= dentry
->d_op
->d_weak_revalidate(dentry
, nd
->flags
);
653 static __always_inline
void set_root(struct nameidata
*nd
)
656 get_fs_root(current
->fs
, &nd
->root
);
659 static int link_path_walk(const char *, struct nameidata
*);
661 static __always_inline
void set_root_rcu(struct nameidata
*nd
)
664 struct fs_struct
*fs
= current
->fs
;
668 seq
= read_seqcount_begin(&fs
->seq
);
670 nd
->seq
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
671 } while (read_seqcount_retry(&fs
->seq
, seq
));
675 static __always_inline
int __vfs_follow_link(struct nameidata
*nd
, const char *link
)
687 nd
->flags
|= LOOKUP_JUMPED
;
689 nd
->inode
= nd
->path
.dentry
->d_inode
;
691 ret
= link_path_walk(link
, nd
);
695 return PTR_ERR(link
);
698 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
701 if (path
->mnt
!= nd
->path
.mnt
)
705 static inline void path_to_nameidata(const struct path
*path
,
706 struct nameidata
*nd
)
708 if (!(nd
->flags
& LOOKUP_RCU
)) {
709 dput(nd
->path
.dentry
);
710 if (nd
->path
.mnt
!= path
->mnt
)
711 mntput(nd
->path
.mnt
);
713 nd
->path
.mnt
= path
->mnt
;
714 nd
->path
.dentry
= path
->dentry
;
718 * Helper to directly jump to a known parsed path from ->follow_link,
719 * caller must have taken a reference to path beforehand.
721 void nd_jump_link(struct nameidata
*nd
, struct path
*path
)
726 nd
->inode
= nd
->path
.dentry
->d_inode
;
727 nd
->flags
|= LOOKUP_JUMPED
;
730 static inline void put_link(struct nameidata
*nd
, struct path
*link
, void *cookie
)
732 struct inode
*inode
= link
->dentry
->d_inode
;
733 if (inode
->i_op
->put_link
)
734 inode
->i_op
->put_link(link
->dentry
, nd
, cookie
);
738 int sysctl_protected_symlinks __read_mostly
= 0;
739 int sysctl_protected_hardlinks __read_mostly
= 0;
742 * may_follow_link - Check symlink following for unsafe situations
743 * @link: The path of the symlink
744 * @nd: nameidata pathwalk data
746 * In the case of the sysctl_protected_symlinks sysctl being enabled,
747 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
748 * in a sticky world-writable directory. This is to protect privileged
749 * processes from failing races against path names that may change out
750 * from under them by way of other users creating malicious symlinks.
751 * It will permit symlinks to be followed only when outside a sticky
752 * world-writable directory, or when the uid of the symlink and follower
753 * match, or when the directory owner matches the symlink's owner.
755 * Returns 0 if following the symlink is allowed, -ve on error.
757 static inline int may_follow_link(struct path
*link
, struct nameidata
*nd
)
759 const struct inode
*inode
;
760 const struct inode
*parent
;
762 if (!sysctl_protected_symlinks
)
765 /* Allowed if owner and follower match. */
766 inode
= link
->dentry
->d_inode
;
767 if (uid_eq(current_cred()->fsuid
, inode
->i_uid
))
770 /* Allowed if parent directory not sticky and world-writable. */
771 parent
= nd
->path
.dentry
->d_inode
;
772 if ((parent
->i_mode
& (S_ISVTX
|S_IWOTH
)) != (S_ISVTX
|S_IWOTH
))
775 /* Allowed if parent directory and link owner match. */
776 if (uid_eq(parent
->i_uid
, inode
->i_uid
))
779 audit_log_link_denied("follow_link", link
);
780 path_put_conditional(link
, nd
);
786 * safe_hardlink_source - Check for safe hardlink conditions
787 * @inode: the source inode to hardlink from
789 * Return false if at least one of the following conditions:
790 * - inode is not a regular file
792 * - inode is setgid and group-exec
793 * - access failure for read and write
795 * Otherwise returns true.
797 static bool safe_hardlink_source(struct inode
*inode
)
799 umode_t mode
= inode
->i_mode
;
801 /* Special files should not get pinned to the filesystem. */
805 /* Setuid files should not get pinned to the filesystem. */
809 /* Executable setgid files should not get pinned to the filesystem. */
810 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
))
813 /* Hardlinking to unreadable or unwritable sources is dangerous. */
814 if (inode_permission(inode
, MAY_READ
| MAY_WRITE
))
821 * may_linkat - Check permissions for creating a hardlink
822 * @link: the source to hardlink from
824 * Block hardlink when all of:
825 * - sysctl_protected_hardlinks enabled
826 * - fsuid does not match inode
827 * - hardlink source is unsafe (see safe_hardlink_source() above)
830 * Returns 0 if successful, -ve on error.
832 static int may_linkat(struct path
*link
)
834 const struct cred
*cred
;
837 if (!sysctl_protected_hardlinks
)
840 cred
= current_cred();
841 inode
= link
->dentry
->d_inode
;
843 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
844 * otherwise, it must be a safe source.
846 if (uid_eq(cred
->fsuid
, inode
->i_uid
) || safe_hardlink_source(inode
) ||
850 audit_log_link_denied("linkat", link
);
854 static __always_inline
int
855 follow_link(struct path
*link
, struct nameidata
*nd
, void **p
)
857 struct dentry
*dentry
= link
->dentry
;
861 BUG_ON(nd
->flags
& LOOKUP_RCU
);
863 if (link
->mnt
== nd
->path
.mnt
)
867 if (unlikely(current
->total_link_count
>= 40))
868 goto out_put_nd_path
;
871 current
->total_link_count
++;
874 nd_set_link(nd
, NULL
);
876 error
= security_inode_follow_link(link
->dentry
, nd
);
878 goto out_put_nd_path
;
880 nd
->last_type
= LAST_BIND
;
881 *p
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
884 goto out_put_nd_path
;
889 error
= __vfs_follow_link(nd
, s
);
891 put_link(nd
, link
, *p
);
903 static int follow_up_rcu(struct path
*path
)
905 struct mount
*mnt
= real_mount(path
->mnt
);
906 struct mount
*parent
;
907 struct dentry
*mountpoint
;
909 parent
= mnt
->mnt_parent
;
910 if (&parent
->mnt
== path
->mnt
)
912 mountpoint
= mnt
->mnt_mountpoint
;
913 path
->dentry
= mountpoint
;
914 path
->mnt
= &parent
->mnt
;
919 * follow_up - Find the mountpoint of path's vfsmount
921 * Given a path, find the mountpoint of its source file system.
922 * Replace @path with the path of the mountpoint in the parent mount.
925 * Return 1 if we went up a level and 0 if we were already at the
928 int follow_up(struct path
*path
)
930 struct mount
*mnt
= real_mount(path
->mnt
);
931 struct mount
*parent
;
932 struct dentry
*mountpoint
;
934 br_read_lock(&vfsmount_lock
);
935 parent
= mnt
->mnt_parent
;
937 br_read_unlock(&vfsmount_lock
);
940 mntget(&parent
->mnt
);
941 mountpoint
= dget(mnt
->mnt_mountpoint
);
942 br_read_unlock(&vfsmount_lock
);
944 path
->dentry
= mountpoint
;
946 path
->mnt
= &parent
->mnt
;
951 * Perform an automount
952 * - return -EISDIR to tell follow_managed() to stop and return the path we
955 static int follow_automount(struct path
*path
, unsigned flags
,
958 struct vfsmount
*mnt
;
961 if (!path
->dentry
->d_op
|| !path
->dentry
->d_op
->d_automount
)
964 /* We don't want to mount if someone's just doing a stat -
965 * unless they're stat'ing a directory and appended a '/' to
968 * We do, however, want to mount if someone wants to open or
969 * create a file of any type under the mountpoint, wants to
970 * traverse through the mountpoint or wants to open the
971 * mounted directory. Also, autofs may mark negative dentries
972 * as being automount points. These will need the attentions
973 * of the daemon to instantiate them before they can be used.
975 if (!(flags
& (LOOKUP_PARENT
| LOOKUP_DIRECTORY
|
976 LOOKUP_OPEN
| LOOKUP_CREATE
| LOOKUP_AUTOMOUNT
)) &&
977 path
->dentry
->d_inode
)
980 current
->total_link_count
++;
981 if (current
->total_link_count
>= 40)
984 mnt
= path
->dentry
->d_op
->d_automount(path
);
987 * The filesystem is allowed to return -EISDIR here to indicate
988 * it doesn't want to automount. For instance, autofs would do
989 * this so that its userspace daemon can mount on this dentry.
991 * However, we can only permit this if it's a terminal point in
992 * the path being looked up; if it wasn't then the remainder of
993 * the path is inaccessible and we should say so.
995 if (PTR_ERR(mnt
) == -EISDIR
&& (flags
& LOOKUP_PARENT
))
1000 if (!mnt
) /* mount collision */
1003 if (!*need_mntput
) {
1004 /* lock_mount() may release path->mnt on error */
1006 *need_mntput
= true;
1008 err
= finish_automount(mnt
, path
);
1012 /* Someone else made a mount here whilst we were busy */
1017 path
->dentry
= dget(mnt
->mnt_root
);
1026 * Handle a dentry that is managed in some way.
1027 * - Flagged for transit management (autofs)
1028 * - Flagged as mountpoint
1029 * - Flagged as automount point
1031 * This may only be called in refwalk mode.
1033 * Serialization is taken care of in namespace.c
1035 static int follow_managed(struct path
*path
, unsigned flags
)
1037 struct vfsmount
*mnt
= path
->mnt
; /* held by caller, must be left alone */
1039 bool need_mntput
= false;
1042 /* Given that we're not holding a lock here, we retain the value in a
1043 * local variable for each dentry as we look at it so that we don't see
1044 * the components of that value change under us */
1045 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1046 managed
&= DCACHE_MANAGED_DENTRY
,
1047 unlikely(managed
!= 0)) {
1048 /* Allow the filesystem to manage the transit without i_mutex
1050 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1051 BUG_ON(!path
->dentry
->d_op
);
1052 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1053 ret
= path
->dentry
->d_op
->d_manage(path
->dentry
, false);
1058 /* Transit to a mounted filesystem. */
1059 if (managed
& DCACHE_MOUNTED
) {
1060 struct vfsmount
*mounted
= lookup_mnt(path
);
1065 path
->mnt
= mounted
;
1066 path
->dentry
= dget(mounted
->mnt_root
);
1071 /* Something is mounted on this dentry in another
1072 * namespace and/or whatever was mounted there in this
1073 * namespace got unmounted before we managed to get the
1077 /* Handle an automount point */
1078 if (managed
& DCACHE_NEED_AUTOMOUNT
) {
1079 ret
= follow_automount(path
, flags
, &need_mntput
);
1085 /* We didn't change the current path point */
1089 if (need_mntput
&& path
->mnt
== mnt
)
1093 return ret
< 0 ? ret
: need_mntput
;
1096 int follow_down_one(struct path
*path
)
1098 struct vfsmount
*mounted
;
1100 mounted
= lookup_mnt(path
);
1104 path
->mnt
= mounted
;
1105 path
->dentry
= dget(mounted
->mnt_root
);
1111 static inline bool managed_dentry_might_block(struct dentry
*dentry
)
1113 return (dentry
->d_flags
& DCACHE_MANAGE_TRANSIT
&&
1114 dentry
->d_op
->d_manage(dentry
, true) < 0);
1118 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1119 * we meet a managed dentry that would need blocking.
1121 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
1122 struct inode
**inode
)
1125 struct mount
*mounted
;
1127 * Don't forget we might have a non-mountpoint managed dentry
1128 * that wants to block transit.
1130 if (unlikely(managed_dentry_might_block(path
->dentry
)))
1133 if (!d_mountpoint(path
->dentry
))
1136 mounted
= __lookup_mnt(path
->mnt
, path
->dentry
, 1);
1139 path
->mnt
= &mounted
->mnt
;
1140 path
->dentry
= mounted
->mnt
.mnt_root
;
1141 nd
->flags
|= LOOKUP_JUMPED
;
1142 nd
->seq
= read_seqcount_begin(&path
->dentry
->d_seq
);
1144 * Update the inode too. We don't need to re-check the
1145 * dentry sequence number here after this d_inode read,
1146 * because a mount-point is always pinned.
1148 *inode
= path
->dentry
->d_inode
;
1153 static void follow_mount_rcu(struct nameidata
*nd
)
1155 while (d_mountpoint(nd
->path
.dentry
)) {
1156 struct mount
*mounted
;
1157 mounted
= __lookup_mnt(nd
->path
.mnt
, nd
->path
.dentry
, 1);
1160 nd
->path
.mnt
= &mounted
->mnt
;
1161 nd
->path
.dentry
= mounted
->mnt
.mnt_root
;
1162 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1166 static int follow_dotdot_rcu(struct nameidata
*nd
)
1171 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1172 nd
->path
.mnt
== nd
->root
.mnt
) {
1175 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1176 struct dentry
*old
= nd
->path
.dentry
;
1177 struct dentry
*parent
= old
->d_parent
;
1180 seq
= read_seqcount_begin(&parent
->d_seq
);
1181 if (read_seqcount_retry(&old
->d_seq
, nd
->seq
))
1183 nd
->path
.dentry
= parent
;
1185 if (unlikely(!path_connected(&nd
->path
)))
1189 if (!follow_up_rcu(&nd
->path
))
1191 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1193 follow_mount_rcu(nd
);
1194 nd
->inode
= nd
->path
.dentry
->d_inode
;
1198 nd
->flags
&= ~LOOKUP_RCU
;
1199 if (!(nd
->flags
& LOOKUP_ROOT
))
1200 nd
->root
.mnt
= NULL
;
1206 * Follow down to the covering mount currently visible to userspace. At each
1207 * point, the filesystem owning that dentry may be queried as to whether the
1208 * caller is permitted to proceed or not.
1210 int follow_down(struct path
*path
)
1215 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1216 unlikely(managed
& DCACHE_MANAGED_DENTRY
)) {
1217 /* Allow the filesystem to manage the transit without i_mutex
1220 * We indicate to the filesystem if someone is trying to mount
1221 * something here. This gives autofs the chance to deny anyone
1222 * other than its daemon the right to mount on its
1225 * The filesystem may sleep at this point.
1227 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1228 BUG_ON(!path
->dentry
->d_op
);
1229 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1230 ret
= path
->dentry
->d_op
->d_manage(
1231 path
->dentry
, false);
1233 return ret
== -EISDIR
? 0 : ret
;
1236 /* Transit to a mounted filesystem. */
1237 if (managed
& DCACHE_MOUNTED
) {
1238 struct vfsmount
*mounted
= lookup_mnt(path
);
1243 path
->mnt
= mounted
;
1244 path
->dentry
= dget(mounted
->mnt_root
);
1248 /* Don't handle automount points here */
1255 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1257 static void follow_mount(struct path
*path
)
1259 while (d_mountpoint(path
->dentry
)) {
1260 struct vfsmount
*mounted
= lookup_mnt(path
);
1265 path
->mnt
= mounted
;
1266 path
->dentry
= dget(mounted
->mnt_root
);
1270 static int follow_dotdot(struct nameidata
*nd
)
1275 struct dentry
*old
= nd
->path
.dentry
;
1277 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1278 nd
->path
.mnt
== nd
->root
.mnt
) {
1281 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1282 /* rare case of legitimate dget_parent()... */
1283 nd
->path
.dentry
= dget_parent(nd
->path
.dentry
);
1285 if (unlikely(!path_connected(&nd
->path
))) {
1286 path_put(&nd
->path
);
1291 if (!follow_up(&nd
->path
))
1294 follow_mount(&nd
->path
);
1295 nd
->inode
= nd
->path
.dentry
->d_inode
;
1300 * This looks up the name in dcache, possibly revalidates the old dentry and
1301 * allocates a new one if not found or not valid. In the need_lookup argument
1302 * returns whether i_op->lookup is necessary.
1304 * dir->d_inode->i_mutex must be held
1306 static struct dentry
*lookup_dcache(struct qstr
*name
, struct dentry
*dir
,
1307 unsigned int flags
, bool *need_lookup
)
1309 struct dentry
*dentry
;
1312 *need_lookup
= false;
1313 dentry
= d_lookup(dir
, name
);
1315 if (dentry
->d_flags
& DCACHE_OP_REVALIDATE
) {
1316 error
= d_revalidate(dentry
, flags
);
1317 if (unlikely(error
<= 0)) {
1320 return ERR_PTR(error
);
1321 } else if (!d_invalidate(dentry
)) {
1330 dentry
= d_alloc(dir
, name
);
1331 if (unlikely(!dentry
))
1332 return ERR_PTR(-ENOMEM
);
1334 *need_lookup
= true;
1340 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1341 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1343 * dir->d_inode->i_mutex must be held
1345 static struct dentry
*lookup_real(struct inode
*dir
, struct dentry
*dentry
,
1350 /* Don't create child dentry for a dead directory. */
1351 if (unlikely(IS_DEADDIR(dir
))) {
1353 return ERR_PTR(-ENOENT
);
1356 old
= dir
->i_op
->lookup(dir
, dentry
, flags
);
1357 if (unlikely(old
)) {
1364 static struct dentry
*__lookup_hash(struct qstr
*name
,
1365 struct dentry
*base
, unsigned int flags
)
1368 struct dentry
*dentry
;
1370 dentry
= lookup_dcache(name
, base
, flags
, &need_lookup
);
1374 return lookup_real(base
->d_inode
, dentry
, flags
);
1378 * It's more convoluted than I'd like it to be, but... it's still fairly
1379 * small and for now I'd prefer to have fast path as straight as possible.
1380 * It _is_ time-critical.
1382 static int lookup_fast(struct nameidata
*nd
,
1383 struct path
*path
, struct inode
**inode
)
1385 struct vfsmount
*mnt
= nd
->path
.mnt
;
1386 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1392 * Rename seqlock is not required here because in the off chance
1393 * of a false negative due to a concurrent rename, we're going to
1394 * do the non-racy lookup, below.
1396 if (nd
->flags
& LOOKUP_RCU
) {
1398 dentry
= __d_lookup_rcu(parent
, &nd
->last
, &seq
, nd
->inode
);
1403 * This sequence count validates that the inode matches
1404 * the dentry name information from lookup.
1406 *inode
= dentry
->d_inode
;
1407 if (read_seqcount_retry(&dentry
->d_seq
, seq
))
1411 * This sequence count validates that the parent had no
1412 * changes while we did the lookup of the dentry above.
1414 * The memory barrier in read_seqcount_begin of child is
1415 * enough, we can use __read_seqcount_retry here.
1417 if (__read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
1421 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)) {
1422 status
= d_revalidate(dentry
, nd
->flags
);
1423 if (unlikely(status
<= 0)) {
1424 if (status
!= -ECHILD
)
1430 path
->dentry
= dentry
;
1431 if (unlikely(!__follow_mount_rcu(nd
, path
, inode
)))
1433 if (unlikely(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
))
1437 if (unlazy_walk(nd
, dentry
))
1440 dentry
= __d_lookup(parent
, &nd
->last
);
1443 if (unlikely(!dentry
))
1446 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
) && need_reval
)
1447 status
= d_revalidate(dentry
, nd
->flags
);
1448 if (unlikely(status
<= 0)) {
1453 if (!d_invalidate(dentry
)) {
1460 path
->dentry
= dentry
;
1461 err
= follow_managed(path
, nd
->flags
);
1462 if (unlikely(err
< 0)) {
1463 path_put_conditional(path
, nd
);
1467 nd
->flags
|= LOOKUP_JUMPED
;
1468 *inode
= path
->dentry
->d_inode
;
1475 /* Fast lookup failed, do it the slow way */
1476 static int lookup_slow(struct nameidata
*nd
, struct path
*path
)
1478 struct dentry
*dentry
, *parent
;
1481 parent
= nd
->path
.dentry
;
1482 BUG_ON(nd
->inode
!= parent
->d_inode
);
1484 mutex_lock(&parent
->d_inode
->i_mutex
);
1485 dentry
= __lookup_hash(&nd
->last
, parent
, nd
->flags
);
1486 mutex_unlock(&parent
->d_inode
->i_mutex
);
1488 return PTR_ERR(dentry
);
1489 path
->mnt
= nd
->path
.mnt
;
1490 path
->dentry
= dentry
;
1491 err
= follow_managed(path
, nd
->flags
);
1492 if (unlikely(err
< 0)) {
1493 path_put_conditional(path
, nd
);
1497 nd
->flags
|= LOOKUP_JUMPED
;
1501 static inline int may_lookup(struct nameidata
*nd
)
1503 if (nd
->flags
& LOOKUP_RCU
) {
1504 int err
= inode_permission2(nd
->path
.mnt
, nd
->inode
, MAY_EXEC
|MAY_NOT_BLOCK
);
1507 if (unlazy_walk(nd
, NULL
))
1510 return inode_permission2(nd
->path
.mnt
, nd
->inode
, MAY_EXEC
);
1513 static inline int handle_dots(struct nameidata
*nd
, int type
)
1515 if (type
== LAST_DOTDOT
) {
1516 if (nd
->flags
& LOOKUP_RCU
) {
1517 if (follow_dotdot_rcu(nd
))
1520 return follow_dotdot(nd
);
1525 static void terminate_walk(struct nameidata
*nd
)
1527 if (!(nd
->flags
& LOOKUP_RCU
)) {
1528 path_put(&nd
->path
);
1530 nd
->flags
&= ~LOOKUP_RCU
;
1531 if (!(nd
->flags
& LOOKUP_ROOT
))
1532 nd
->root
.mnt
= NULL
;
1538 * Do we need to follow links? We _really_ want to be able
1539 * to do this check without having to look at inode->i_op,
1540 * so we keep a cache of "no, this doesn't need follow_link"
1541 * for the common case.
1543 static inline int should_follow_link(struct inode
*inode
, int follow
)
1545 if (unlikely(!(inode
->i_opflags
& IOP_NOFOLLOW
))) {
1546 if (likely(inode
->i_op
->follow_link
))
1549 /* This gets set once for the inode lifetime */
1550 spin_lock(&inode
->i_lock
);
1551 inode
->i_opflags
|= IOP_NOFOLLOW
;
1552 spin_unlock(&inode
->i_lock
);
1557 static inline int walk_component(struct nameidata
*nd
, struct path
*path
,
1560 struct inode
*inode
;
1563 * "." and ".." are special - ".." especially so because it has
1564 * to be able to know about the current root directory and
1565 * parent relationships.
1567 if (unlikely(nd
->last_type
!= LAST_NORM
))
1568 return handle_dots(nd
, nd
->last_type
);
1569 err
= lookup_fast(nd
, path
, &inode
);
1570 if (unlikely(err
)) {
1574 err
= lookup_slow(nd
, path
);
1578 inode
= path
->dentry
->d_inode
;
1584 if (should_follow_link(inode
, follow
)) {
1585 if (nd
->flags
& LOOKUP_RCU
) {
1586 if (unlikely(nd
->path
.mnt
!= path
->mnt
||
1587 unlazy_walk(nd
, path
->dentry
))) {
1592 BUG_ON(inode
!= path
->dentry
->d_inode
);
1595 path_to_nameidata(path
, nd
);
1600 path_to_nameidata(path
, nd
);
1607 * This limits recursive symlink follows to 8, while
1608 * limiting consecutive symlinks to 40.
1610 * Without that kind of total limit, nasty chains of consecutive
1611 * symlinks can cause almost arbitrarily long lookups.
1613 static inline int nested_symlink(struct path
*path
, struct nameidata
*nd
)
1617 if (unlikely(current
->link_count
>= MAX_NESTED_LINKS
)) {
1618 path_put_conditional(path
, nd
);
1619 path_put(&nd
->path
);
1622 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
1625 current
->link_count
++;
1628 struct path link
= *path
;
1631 res
= follow_link(&link
, nd
, &cookie
);
1634 res
= walk_component(nd
, path
, LOOKUP_FOLLOW
);
1635 put_link(nd
, &link
, cookie
);
1638 current
->link_count
--;
1644 * We really don't want to look at inode->i_op->lookup
1645 * when we don't have to. So we keep a cache bit in
1646 * the inode ->i_opflags field that says "yes, we can
1647 * do lookup on this inode".
1649 static inline int can_lookup(struct inode
*inode
)
1651 if (likely(inode
->i_opflags
& IOP_LOOKUP
))
1653 if (likely(!inode
->i_op
->lookup
))
1656 /* We do this once for the lifetime of the inode */
1657 spin_lock(&inode
->i_lock
);
1658 inode
->i_opflags
|= IOP_LOOKUP
;
1659 spin_unlock(&inode
->i_lock
);
1664 * We can do the critical dentry name comparison and hashing
1665 * operations one word at a time, but we are limited to:
1667 * - Architectures with fast unaligned word accesses. We could
1668 * do a "get_unaligned()" if this helps and is sufficiently
1671 * - Little-endian machines (so that we can generate the mask
1672 * of low bytes efficiently). Again, we *could* do a byte
1673 * swapping load on big-endian architectures if that is not
1674 * expensive enough to make the optimization worthless.
1676 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1677 * do not trap on the (extremely unlikely) case of a page
1678 * crossing operation.
1680 * - Furthermore, we need an efficient 64-bit compile for the
1681 * 64-bit case in order to generate the "number of bytes in
1682 * the final mask". Again, that could be replaced with a
1683 * efficient population count instruction or similar.
1685 #ifdef CONFIG_DCACHE_WORD_ACCESS
1687 #include <asm/word-at-a-time.h>
1691 static inline unsigned int fold_hash(unsigned long hash
)
1693 return hash_64(hash
, 32);
1696 #else /* 32-bit case */
1698 #define fold_hash(x) (x)
1702 unsigned int full_name_hash(const unsigned char *name
, unsigned int len
)
1704 unsigned long a
, mask
;
1705 unsigned long hash
= 0;
1708 a
= load_unaligned_zeropad(name
);
1709 if (len
< sizeof(unsigned long))
1713 name
+= sizeof(unsigned long);
1714 len
-= sizeof(unsigned long);
1718 mask
= ~(~0ul << len
*8);
1721 return fold_hash(hash
);
1723 EXPORT_SYMBOL(full_name_hash
);
1726 * Calculate the length and hash of the path component, and
1727 * return the length of the component;
1729 static inline unsigned long hash_name(const char *name
, unsigned int *hashp
)
1731 unsigned long a
, b
, adata
, bdata
, mask
, hash
, len
;
1732 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
1735 len
= -sizeof(unsigned long);
1737 hash
= (hash
+ a
) * 9;
1738 len
+= sizeof(unsigned long);
1739 a
= load_unaligned_zeropad(name
+len
);
1740 b
= a
^ REPEAT_BYTE('/');
1741 } while (!(has_zero(a
, &adata
, &constants
) | has_zero(b
, &bdata
, &constants
)));
1743 adata
= prep_zero_mask(a
, adata
, &constants
);
1744 bdata
= prep_zero_mask(b
, bdata
, &constants
);
1746 mask
= create_zero_mask(adata
| bdata
);
1748 hash
+= a
& zero_bytemask(mask
);
1749 *hashp
= fold_hash(hash
);
1751 return len
+ find_zero(mask
);
1756 unsigned int full_name_hash(const unsigned char *name
, unsigned int len
)
1758 unsigned long hash
= init_name_hash();
1760 hash
= partial_name_hash(*name
++, hash
);
1761 return end_name_hash(hash
);
1763 EXPORT_SYMBOL(full_name_hash
);
1766 * We know there's a real path component here of at least
1769 static inline unsigned long hash_name(const char *name
, unsigned int *hashp
)
1771 unsigned long hash
= init_name_hash();
1772 unsigned long len
= 0, c
;
1774 c
= (unsigned char)*name
;
1777 hash
= partial_name_hash(c
, hash
);
1778 c
= (unsigned char)name
[len
];
1779 } while (c
&& c
!= '/');
1780 *hashp
= end_name_hash(hash
);
1788 * This is the basic name resolution function, turning a pathname into
1789 * the final dentry. We expect 'base' to be positive and a directory.
1791 * Returns 0 and nd will have valid dentry and mnt on success.
1792 * Returns error and drops reference to input namei data on failure.
1794 static int link_path_walk(const char *name
, struct nameidata
*nd
)
1804 /* At this point we know we have a real path component. */
1810 err
= may_lookup(nd
);
1814 len
= hash_name(name
, &this.hash
);
1819 if (name
[0] == '.') switch (len
) {
1821 if (name
[1] == '.') {
1823 nd
->flags
|= LOOKUP_JUMPED
;
1829 if (likely(type
== LAST_NORM
)) {
1830 struct dentry
*parent
= nd
->path
.dentry
;
1831 nd
->flags
&= ~LOOKUP_JUMPED
;
1832 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
1833 err
= parent
->d_op
->d_hash(parent
, nd
->inode
,
1841 nd
->last_type
= type
;
1846 * If it wasn't NUL, we know it was '/'. Skip that
1847 * slash, and continue until no more slashes.
1851 } while (unlikely(name
[len
] == '/'));
1857 err
= walk_component(nd
, &next
, LOOKUP_FOLLOW
);
1862 err
= nested_symlink(&next
, nd
);
1866 if (!can_lookup(nd
->inode
)) {
1875 static int path_init(int dfd
, const char *name
, unsigned int flags
,
1876 struct nameidata
*nd
, struct file
**fp
)
1880 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
1881 nd
->flags
= flags
| LOOKUP_JUMPED
;
1883 if (flags
& LOOKUP_ROOT
) {
1884 struct inode
*inode
= nd
->root
.dentry
->d_inode
;
1885 struct vfsmount
*mnt
= nd
->root
.mnt
;
1887 if (!can_lookup(inode
))
1889 retval
= inode_permission2(mnt
, inode
, MAY_EXEC
);
1893 nd
->path
= nd
->root
;
1895 if (flags
& LOOKUP_RCU
) {
1897 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1899 path_get(&nd
->path
);
1904 nd
->root
.mnt
= NULL
;
1907 if (flags
& LOOKUP_RCU
) {
1912 path_get(&nd
->root
);
1914 nd
->path
= nd
->root
;
1915 } else if (dfd
== AT_FDCWD
) {
1916 if (flags
& LOOKUP_RCU
) {
1917 struct fs_struct
*fs
= current
->fs
;
1923 seq
= read_seqcount_begin(&fs
->seq
);
1925 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1926 } while (read_seqcount_retry(&fs
->seq
, seq
));
1928 get_fs_pwd(current
->fs
, &nd
->path
);
1931 /* Caller must check execute permissions on the starting path component */
1932 struct fd f
= fdget_raw(dfd
);
1933 struct dentry
*dentry
;
1938 dentry
= f
.file
->f_path
.dentry
;
1941 if (!can_lookup(dentry
->d_inode
)) {
1947 nd
->path
= f
.file
->f_path
;
1948 if (flags
& LOOKUP_RCU
) {
1951 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1954 path_get(&nd
->path
);
1959 nd
->inode
= nd
->path
.dentry
->d_inode
;
1963 static inline int lookup_last(struct nameidata
*nd
, struct path
*path
)
1965 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
1966 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
1968 nd
->flags
&= ~LOOKUP_PARENT
;
1969 return walk_component(nd
, path
, nd
->flags
& LOOKUP_FOLLOW
);
1972 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1973 static int path_lookupat(int dfd
, const char *name
,
1974 unsigned int flags
, struct nameidata
*nd
)
1976 struct file
*base
= NULL
;
1981 * Path walking is largely split up into 2 different synchronisation
1982 * schemes, rcu-walk and ref-walk (explained in
1983 * Documentation/filesystems/path-lookup.txt). These share much of the
1984 * path walk code, but some things particularly setup, cleanup, and
1985 * following mounts are sufficiently divergent that functions are
1986 * duplicated. Typically there is a function foo(), and its RCU
1987 * analogue, foo_rcu().
1989 * -ECHILD is the error number of choice (just to avoid clashes) that
1990 * is returned if some aspect of an rcu-walk fails. Such an error must
1991 * be handled by restarting a traditional ref-walk (which will always
1992 * be able to complete).
1994 err
= path_init(dfd
, name
, flags
| LOOKUP_PARENT
, nd
, &base
);
1999 current
->total_link_count
= 0;
2000 err
= link_path_walk(name
, nd
);
2002 if (!err
&& !(flags
& LOOKUP_PARENT
)) {
2003 err
= lookup_last(nd
, &path
);
2006 struct path link
= path
;
2007 err
= may_follow_link(&link
, nd
);
2010 nd
->flags
|= LOOKUP_PARENT
;
2011 err
= follow_link(&link
, nd
, &cookie
);
2014 err
= lookup_last(nd
, &path
);
2015 put_link(nd
, &link
, cookie
);
2020 err
= complete_walk(nd
);
2022 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
) {
2023 if (!can_lookup(nd
->inode
)) {
2024 path_put(&nd
->path
);
2030 struct super_block
*sb
= nd
->inode
->i_sb
;
2031 if (sb
->s_flags
& MS_RDONLY
) {
2032 if (d_is_su(nd
->path
.dentry
) && !su_visible()) {
2033 path_put(&nd
->path
);
2042 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
2043 path_put(&nd
->root
);
2044 nd
->root
.mnt
= NULL
;
2049 static int filename_lookup(int dfd
, struct filename
*name
,
2050 unsigned int flags
, struct nameidata
*nd
)
2052 int retval
= path_lookupat(dfd
, name
->name
, flags
| LOOKUP_RCU
, nd
);
2053 if (unlikely(retval
== -ECHILD
))
2054 retval
= path_lookupat(dfd
, name
->name
, flags
, nd
);
2055 if (unlikely(retval
== -ESTALE
))
2056 retval
= path_lookupat(dfd
, name
->name
,
2057 flags
| LOOKUP_REVAL
, nd
);
2059 if (likely(!retval
))
2060 audit_inode(name
, nd
->path
.dentry
, flags
& LOOKUP_PARENT
);
2064 static int do_path_lookup(int dfd
, const char *name
,
2065 unsigned int flags
, struct nameidata
*nd
)
2067 struct filename filename
= { .name
= name
};
2069 return filename_lookup(dfd
, &filename
, flags
, nd
);
2072 /* does lookup, returns the object with parent locked */
2073 struct dentry
*kern_path_locked(const char *name
, struct path
*path
)
2075 struct nameidata nd
;
2077 int err
= do_path_lookup(AT_FDCWD
, name
, LOOKUP_PARENT
, &nd
);
2079 return ERR_PTR(err
);
2080 if (nd
.last_type
!= LAST_NORM
) {
2082 return ERR_PTR(-EINVAL
);
2084 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2085 d
= __lookup_hash(&nd
.last
, nd
.path
.dentry
, 0);
2087 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2095 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
2097 struct nameidata nd
;
2098 int res
= do_path_lookup(AT_FDCWD
, name
, flags
, &nd
);
2105 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2106 * @dentry: pointer to dentry of the base directory
2107 * @mnt: pointer to vfs mount of the base directory
2108 * @name: pointer to file name
2109 * @flags: lookup flags
2110 * @path: pointer to struct path to fill
2112 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
2113 const char *name
, unsigned int flags
,
2116 struct nameidata nd
;
2118 nd
.root
.dentry
= dentry
;
2120 BUG_ON(flags
& LOOKUP_PARENT
);
2121 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2122 err
= do_path_lookup(AT_FDCWD
, name
, flags
| LOOKUP_ROOT
, &nd
);
2129 * Restricted form of lookup. Doesn't follow links, single-component only,
2130 * needs parent already locked. Doesn't follow mounts.
2133 static struct dentry
*lookup_hash(struct nameidata
*nd
)
2135 return __lookup_hash(&nd
->last
, nd
->path
.dentry
, nd
->flags
);
2139 * lookup_one_len - filesystem helper to lookup single pathname component
2140 * @name: pathname component to lookup
2141 * @mnt: mount we are looking up on
2142 * @base: base directory to lookup from
2143 * @len: maximum length @len should be interpreted to
2145 * Note that this routine is purely a helper for filesystem usage and should
2146 * not be called by generic code. Also note that by using this function the
2147 * nameidata argument is passed to the filesystem methods and a filesystem
2148 * using this helper needs to be prepared for that.
2150 struct dentry
*lookup_one_len2(const char *name
, struct vfsmount
*mnt
, struct dentry
*base
, int len
)
2156 WARN_ON_ONCE(!mutex_is_locked(&base
->d_inode
->i_mutex
));
2160 this.hash
= full_name_hash(name
, len
);
2162 return ERR_PTR(-EACCES
);
2164 if (unlikely(name
[0] == '.')) {
2165 if (len
< 2 || (len
== 2 && name
[1] == '.'))
2166 return ERR_PTR(-EACCES
);
2170 c
= *(const unsigned char *)name
++;
2171 if (c
== '/' || c
== '\0')
2172 return ERR_PTR(-EACCES
);
2175 * See if the low-level filesystem might want
2176 * to use its own hash..
2178 if (base
->d_flags
& DCACHE_OP_HASH
) {
2179 int err
= base
->d_op
->d_hash(base
, base
->d_inode
, &this);
2181 return ERR_PTR(err
);
2184 err
= inode_permission2(mnt
, base
->d_inode
, MAY_EXEC
);
2186 return ERR_PTR(err
);
2188 return __lookup_hash(&this, base
, 0);
2190 EXPORT_SYMBOL(lookup_one_len2
);
2192 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
2194 return lookup_one_len2(name
, NULL
, base
, len
);
2196 EXPORT_SYMBOL(lookup_one_len
);
2198 int user_path_at_empty(int dfd
, const char __user
*name
, unsigned flags
,
2199 struct path
*path
, int *empty
)
2201 struct nameidata nd
;
2202 struct filename
*tmp
= getname_flags(name
, flags
, empty
);
2203 int err
= PTR_ERR(tmp
);
2206 BUG_ON(flags
& LOOKUP_PARENT
);
2208 err
= filename_lookup(dfd
, tmp
, flags
, &nd
);
2216 int user_path_at(int dfd
, const char __user
*name
, unsigned flags
,
2219 return user_path_at_empty(dfd
, name
, flags
, path
, NULL
);
2223 * NB: most callers don't do anything directly with the reference to the
2224 * to struct filename, but the nd->last pointer points into the name string
2225 * allocated by getname. So we must hold the reference to it until all
2226 * path-walking is complete.
2228 static struct filename
*
2229 user_path_parent(int dfd
, const char __user
*path
, struct nameidata
*nd
,
2232 struct filename
*s
= getname(path
);
2235 /* only LOOKUP_REVAL is allowed in extra flags */
2236 flags
&= LOOKUP_REVAL
;
2241 error
= filename_lookup(dfd
, s
, flags
| LOOKUP_PARENT
, nd
);
2244 return ERR_PTR(error
);
2251 * It's inline, so penalty for filesystems that don't use sticky bit is
2254 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
2256 kuid_t fsuid
= current_fsuid();
2258 if (!(dir
->i_mode
& S_ISVTX
))
2260 if (uid_eq(inode
->i_uid
, fsuid
))
2262 if (uid_eq(dir
->i_uid
, fsuid
))
2264 return !capable_wrt_inode_uidgid(inode
, CAP_FOWNER
);
2268 * Check whether we can remove a link victim from directory dir, check
2269 * whether the type of victim is right.
2270 * 1. We can't do it if dir is read-only (done in permission())
2271 * 2. We should have write and exec permissions on dir
2272 * 3. We can't remove anything from append-only dir
2273 * 4. We can't do anything with immutable dir (done in permission())
2274 * 5. If the sticky bit on dir is set we should either
2275 * a. be owner of dir, or
2276 * b. be owner of victim, or
2277 * c. have CAP_FOWNER capability
2278 * 6. If the victim is append-only or immutable we can't do antyhing with
2279 * links pointing to it.
2280 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2281 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2282 * 9. We can't remove a root or mountpoint.
2283 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2284 * nfs_async_unlink().
2286 static int may_delete(struct vfsmount
*mnt
, struct inode
*dir
,struct dentry
*victim
,int isdir
)
2290 if (!victim
->d_inode
)
2293 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
2294 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
2296 error
= inode_permission2(mnt
, dir
, MAY_WRITE
| MAY_EXEC
);
2301 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
2302 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
2305 if (!S_ISDIR(victim
->d_inode
->i_mode
))
2307 if (IS_ROOT(victim
))
2309 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
2311 if (IS_DEADDIR(dir
))
2313 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
2318 /* Check whether we can create an object with dentry child in directory
2320 * 1. We can't do it if child already exists (open has special treatment for
2321 * this case, but since we are inlined it's OK)
2322 * 2. We can't do it if dir is read-only (done in permission())
2323 * 3. We should have write and exec permissions on dir
2324 * 4. We can't do it if dir is immutable (done in permission())
2326 static inline int may_create(struct vfsmount
*mnt
, struct inode
*dir
, struct dentry
*child
)
2328 audit_inode_child(dir
, child
, AUDIT_TYPE_CHILD_CREATE
);
2331 if (IS_DEADDIR(dir
))
2333 return inode_permission2(mnt
, dir
, MAY_WRITE
| MAY_EXEC
);
2337 * p1 and p2 should be directories on the same fs.
2339 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
2344 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2348 mutex_lock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
2350 p
= d_ancestor(p2
, p1
);
2352 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2353 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2357 p
= d_ancestor(p1
, p2
);
2359 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2360 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2364 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2365 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2369 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
2371 mutex_unlock(&p1
->d_inode
->i_mutex
);
2373 mutex_unlock(&p2
->d_inode
->i_mutex
);
2374 mutex_unlock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
2378 int vfs_create2(struct vfsmount
*mnt
, struct inode
*dir
, struct dentry
*dentry
,
2379 umode_t mode
, bool want_excl
)
2381 int error
= may_create(mnt
, dir
, dentry
);
2385 if (!dir
->i_op
->create
)
2386 return -EACCES
; /* shouldn't it be ENOSYS? */
2389 error
= security_inode_create(dir
, dentry
, mode
);
2392 error
= dir
->i_op
->create(dir
, dentry
, mode
, want_excl
);
2394 fsnotify_create(dir
, dentry
);
2397 EXPORT_SYMBOL(vfs_create2
);
2399 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2402 return vfs_create2(NULL
, dir
, dentry
, mode
, want_excl
);
2404 EXPORT_SYMBOL(vfs_create
);
2406 static int may_open(struct path
*path
, int acc_mode
, int flag
)
2408 struct dentry
*dentry
= path
->dentry
;
2409 struct vfsmount
*mnt
= path
->mnt
;
2410 struct inode
*inode
= dentry
->d_inode
;
2420 switch (inode
->i_mode
& S_IFMT
) {
2424 if (acc_mode
& MAY_WRITE
)
2429 if (path
->mnt
->mnt_flags
& MNT_NODEV
)
2438 error
= inode_permission2(mnt
, inode
, acc_mode
);
2443 * An append-only file must be opened in append mode for writing.
2445 if (IS_APPEND(inode
)) {
2446 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
2452 /* O_NOATIME can only be set by the owner or superuser */
2453 if (flag
& O_NOATIME
&& !inode_owner_or_capable(inode
))
2459 static int handle_truncate(struct file
*filp
)
2461 struct path
*path
= &filp
->f_path
;
2462 struct inode
*inode
= path
->dentry
->d_inode
;
2463 int error
= get_write_access(inode
);
2467 * Refuse to truncate files with mandatory locks held on them.
2469 error
= locks_verify_locked(inode
);
2471 error
= security_path_truncate(path
);
2473 error
= do_truncate2(path
->mnt
, path
->dentry
, 0,
2474 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
2477 put_write_access(inode
);
2481 static inline int open_to_namei_flags(int flag
)
2483 if ((flag
& O_ACCMODE
) == 3)
2488 static int may_o_create(struct path
*dir
, struct dentry
*dentry
, umode_t mode
)
2490 int error
= security_path_mknod(dir
, dentry
, mode
, 0);
2494 error
= inode_permission2(dir
->mnt
, dir
->dentry
->d_inode
, MAY_WRITE
| MAY_EXEC
);
2498 return security_inode_create(dir
->dentry
->d_inode
, dentry
, mode
);
2502 * Attempt to atomically look up, create and open a file from a negative
2505 * Returns 0 if successful. The file will have been created and attached to
2506 * @file by the filesystem calling finish_open().
2508 * Returns 1 if the file was looked up only or didn't need creating. The
2509 * caller will need to perform the open themselves. @path will have been
2510 * updated to point to the new dentry. This may be negative.
2512 * Returns an error code otherwise.
2514 static int atomic_open(struct nameidata
*nd
, struct dentry
*dentry
,
2515 struct path
*path
, struct file
*file
,
2516 const struct open_flags
*op
,
2517 bool got_write
, bool need_lookup
,
2520 struct inode
*dir
= nd
->path
.dentry
->d_inode
;
2521 unsigned open_flag
= open_to_namei_flags(op
->open_flag
);
2525 int create_error
= 0;
2526 struct dentry
*const DENTRY_NOT_SET
= (void *) -1UL;
2528 BUG_ON(dentry
->d_inode
);
2530 /* Don't create child dentry for a dead directory. */
2531 if (unlikely(IS_DEADDIR(dir
))) {
2537 if ((open_flag
& O_CREAT
) && !IS_POSIXACL(dir
))
2538 mode
&= ~current_umask();
2540 if ((open_flag
& (O_EXCL
| O_CREAT
)) == (O_EXCL
| O_CREAT
)) {
2541 open_flag
&= ~O_TRUNC
;
2542 *opened
|= FILE_CREATED
;
2546 * Checking write permission is tricky, bacuse we don't know if we are
2547 * going to actually need it: O_CREAT opens should work as long as the
2548 * file exists. But checking existence breaks atomicity. The trick is
2549 * to check access and if not granted clear O_CREAT from the flags.
2551 * Another problem is returing the "right" error value (e.g. for an
2552 * O_EXCL open we want to return EEXIST not EROFS).
2554 if (((open_flag
& (O_CREAT
| O_TRUNC
)) ||
2555 (open_flag
& O_ACCMODE
) != O_RDONLY
) && unlikely(!got_write
)) {
2556 if (!(open_flag
& O_CREAT
)) {
2558 * No O_CREATE -> atomicity not a requirement -> fall
2559 * back to lookup + open
2562 } else if (open_flag
& (O_EXCL
| O_TRUNC
)) {
2563 /* Fall back and fail with the right error */
2564 create_error
= -EROFS
;
2567 /* No side effects, safe to clear O_CREAT */
2568 create_error
= -EROFS
;
2569 open_flag
&= ~O_CREAT
;
2573 if (open_flag
& O_CREAT
) {
2574 error
= may_o_create(&nd
->path
, dentry
, mode
);
2576 create_error
= error
;
2577 if (open_flag
& O_EXCL
)
2579 open_flag
&= ~O_CREAT
;
2583 if (nd
->flags
& LOOKUP_DIRECTORY
)
2584 open_flag
|= O_DIRECTORY
;
2586 file
->f_path
.dentry
= DENTRY_NOT_SET
;
2587 file
->f_path
.mnt
= nd
->path
.mnt
;
2588 error
= dir
->i_op
->atomic_open(dir
, dentry
, file
, open_flag
, mode
,
2591 if (create_error
&& error
== -ENOENT
)
2592 error
= create_error
;
2596 acc_mode
= op
->acc_mode
;
2597 if (*opened
& FILE_CREATED
) {
2598 fsnotify_create(dir
, dentry
);
2599 acc_mode
= MAY_OPEN
;
2602 if (error
) { /* returned 1, that is */
2603 if (WARN_ON(file
->f_path
.dentry
== DENTRY_NOT_SET
)) {
2607 if (file
->f_path
.dentry
) {
2609 dentry
= file
->f_path
.dentry
;
2611 if (create_error
&& dentry
->d_inode
== NULL
) {
2612 error
= create_error
;
2619 * We didn't have the inode before the open, so check open permission
2622 error
= may_open(&file
->f_path
, acc_mode
, open_flag
);
2632 dentry
= lookup_real(dir
, dentry
, nd
->flags
);
2634 return PTR_ERR(dentry
);
2637 int open_flag
= op
->open_flag
;
2639 error
= create_error
;
2640 if ((open_flag
& O_EXCL
)) {
2641 if (!dentry
->d_inode
)
2643 } else if (!dentry
->d_inode
) {
2645 } else if ((open_flag
& O_TRUNC
) &&
2646 S_ISREG(dentry
->d_inode
->i_mode
)) {
2649 /* will fail later, go on to get the right error */
2653 path
->dentry
= dentry
;
2654 path
->mnt
= nd
->path
.mnt
;
2659 * Look up and maybe create and open the last component.
2661 * Must be called with i_mutex held on parent.
2663 * Returns 0 if the file was successfully atomically created (if necessary) and
2664 * opened. In this case the file will be returned attached to @file.
2666 * Returns 1 if the file was not completely opened at this time, though lookups
2667 * and creations will have been performed and the dentry returned in @path will
2668 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2669 * specified then a negative dentry may be returned.
2671 * An error code is returned otherwise.
2673 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2674 * cleared otherwise prior to returning.
2676 static int lookup_open(struct nameidata
*nd
, struct path
*path
,
2678 const struct open_flags
*op
,
2679 bool got_write
, int *opened
)
2681 struct dentry
*dir
= nd
->path
.dentry
;
2682 struct vfsmount
*mnt
= nd
->path
.mnt
;
2683 struct inode
*dir_inode
= dir
->d_inode
;
2684 struct dentry
*dentry
;
2688 *opened
&= ~FILE_CREATED
;
2689 dentry
= lookup_dcache(&nd
->last
, dir
, nd
->flags
, &need_lookup
);
2691 return PTR_ERR(dentry
);
2693 /* Cached positive dentry: will open in f_op->open */
2694 if (!need_lookup
&& dentry
->d_inode
)
2697 if ((nd
->flags
& LOOKUP_OPEN
) && dir_inode
->i_op
->atomic_open
) {
2698 return atomic_open(nd
, dentry
, path
, file
, op
, got_write
,
2699 need_lookup
, opened
);
2703 BUG_ON(dentry
->d_inode
);
2705 dentry
= lookup_real(dir_inode
, dentry
, nd
->flags
);
2707 return PTR_ERR(dentry
);
2710 /* Negative dentry, just create the file */
2711 if (!dentry
->d_inode
&& (op
->open_flag
& O_CREAT
)) {
2712 umode_t mode
= op
->mode
;
2713 if (!IS_POSIXACL(dir
->d_inode
))
2714 mode
&= ~current_umask();
2716 * This write is needed to ensure that a
2717 * rw->ro transition does not occur between
2718 * the time when the file is created and when
2719 * a permanent write count is taken through
2720 * the 'struct file' in finish_open().
2726 *opened
|= FILE_CREATED
;
2727 error
= security_path_mknod(&nd
->path
, dentry
, mode
, 0);
2730 error
= vfs_create2(mnt
, dir
->d_inode
, dentry
, mode
,
2731 nd
->flags
& LOOKUP_EXCL
);
2736 path
->dentry
= dentry
;
2737 path
->mnt
= nd
->path
.mnt
;
2746 * Handle the last step of open()
2748 static int do_last(struct nameidata
*nd
, struct path
*path
,
2749 struct file
*file
, const struct open_flags
*op
,
2750 int *opened
, struct filename
*name
)
2752 struct dentry
*dir
= nd
->path
.dentry
;
2753 int open_flag
= op
->open_flag
;
2754 bool will_truncate
= (open_flag
& O_TRUNC
) != 0;
2755 bool got_write
= false;
2756 int acc_mode
= op
->acc_mode
;
2757 struct inode
*inode
;
2758 bool symlink_ok
= false;
2759 struct path save_parent
= { .dentry
= NULL
, .mnt
= NULL
};
2760 bool retried
= false;
2763 nd
->flags
&= ~LOOKUP_PARENT
;
2764 nd
->flags
|= op
->intent
;
2766 switch (nd
->last_type
) {
2769 error
= handle_dots(nd
, nd
->last_type
);
2774 error
= complete_walk(nd
);
2777 audit_inode(name
, nd
->path
.dentry
, 0);
2778 if (open_flag
& O_CREAT
) {
2784 error
= complete_walk(nd
);
2787 audit_inode(name
, dir
, 0);
2791 if (!(open_flag
& O_CREAT
)) {
2792 if (nd
->last
.name
[nd
->last
.len
])
2793 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2794 if (open_flag
& O_PATH
&& !(nd
->flags
& LOOKUP_FOLLOW
))
2796 /* we _can_ be in RCU mode here */
2797 error
= lookup_fast(nd
, path
, &inode
);
2804 BUG_ON(nd
->inode
!= dir
->d_inode
);
2806 /* create side of things */
2808 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2809 * has been cleared when we got to the last component we are
2812 error
= complete_walk(nd
);
2816 audit_inode(name
, dir
, LOOKUP_PARENT
);
2818 /* trailing slashes? */
2819 if (nd
->last
.name
[nd
->last
.len
])
2824 if (op
->open_flag
& (O_CREAT
| O_TRUNC
| O_WRONLY
| O_RDWR
)) {
2825 error
= mnt_want_write(nd
->path
.mnt
);
2829 * do _not_ fail yet - we might not need that or fail with
2830 * a different error; let lookup_open() decide; we'll be
2831 * dropping this one anyway.
2834 mutex_lock(&dir
->d_inode
->i_mutex
);
2835 error
= lookup_open(nd
, path
, file
, op
, got_write
, opened
);
2836 mutex_unlock(&dir
->d_inode
->i_mutex
);
2842 if ((*opened
& FILE_CREATED
) ||
2843 !S_ISREG(file_inode(file
)->i_mode
))
2844 will_truncate
= false;
2846 audit_inode(name
, file
->f_path
.dentry
, 0);
2850 if (*opened
& FILE_CREATED
) {
2851 /* Don't check for write permission, don't truncate */
2852 open_flag
&= ~O_TRUNC
;
2853 will_truncate
= false;
2854 acc_mode
= MAY_OPEN
;
2855 path_to_nameidata(path
, nd
);
2856 goto finish_open_created
;
2860 * create/update audit record if it already exists.
2862 if (path
->dentry
->d_inode
)
2863 audit_inode(name
, path
->dentry
, 0);
2866 * If atomic_open() acquired write access it is dropped now due to
2867 * possible mount and symlink following (this might be optimized away if
2871 mnt_drop_write(nd
->path
.mnt
);
2876 if ((open_flag
& (O_EXCL
| O_CREAT
)) == (O_EXCL
| O_CREAT
))
2879 error
= follow_managed(path
, nd
->flags
);
2884 nd
->flags
|= LOOKUP_JUMPED
;
2886 BUG_ON(nd
->flags
& LOOKUP_RCU
);
2887 inode
= path
->dentry
->d_inode
;
2889 /* we _can_ be in RCU mode here */
2892 path_to_nameidata(path
, nd
);
2896 if (should_follow_link(inode
, !symlink_ok
)) {
2897 if (nd
->flags
& LOOKUP_RCU
) {
2898 if (unlikely(nd
->path
.mnt
!= path
->mnt
||
2899 unlazy_walk(nd
, path
->dentry
))) {
2904 BUG_ON(inode
!= path
->dentry
->d_inode
);
2908 if ((nd
->flags
& LOOKUP_RCU
) || nd
->path
.mnt
!= path
->mnt
) {
2909 path_to_nameidata(path
, nd
);
2911 save_parent
.dentry
= nd
->path
.dentry
;
2912 save_parent
.mnt
= mntget(path
->mnt
);
2913 nd
->path
.dentry
= path
->dentry
;
2917 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
2918 error
= complete_walk(nd
);
2920 path_put(&save_parent
);
2924 if ((open_flag
& O_CREAT
) && S_ISDIR(nd
->inode
->i_mode
))
2927 if ((nd
->flags
& LOOKUP_DIRECTORY
) && !can_lookup(nd
->inode
))
2929 audit_inode(name
, nd
->path
.dentry
, 0);
2931 if (!S_ISREG(nd
->inode
->i_mode
))
2932 will_truncate
= false;
2934 if (will_truncate
) {
2935 error
= mnt_want_write(nd
->path
.mnt
);
2940 finish_open_created
:
2941 error
= may_open(&nd
->path
, acc_mode
, open_flag
);
2944 file
->f_path
.mnt
= nd
->path
.mnt
;
2945 error
= finish_open(file
, nd
->path
.dentry
, NULL
, opened
);
2947 if (error
== -EOPENSTALE
)
2952 error
= open_check_o_direct(file
);
2955 error
= ima_file_check(file
, op
->acc_mode
);
2959 if (will_truncate
) {
2960 error
= handle_truncate(file
);
2965 if (unlikely(error
> 0)) {
2970 mnt_drop_write(nd
->path
.mnt
);
2971 path_put(&save_parent
);
2976 path_put_conditional(path
, nd
);
2983 /* If no saved parent or already retried then can't retry */
2984 if (!save_parent
.dentry
|| retried
)
2987 BUG_ON(save_parent
.dentry
!= dir
);
2988 path_put(&nd
->path
);
2989 nd
->path
= save_parent
;
2990 nd
->inode
= dir
->d_inode
;
2991 save_parent
.mnt
= NULL
;
2992 save_parent
.dentry
= NULL
;
2994 mnt_drop_write(nd
->path
.mnt
);
3001 static struct file
*path_openat(int dfd
, struct filename
*pathname
,
3002 struct nameidata
*nd
, const struct open_flags
*op
, int flags
)
3004 struct file
*base
= NULL
;
3010 file
= get_empty_filp();
3014 file
->f_flags
= op
->open_flag
;
3016 error
= path_init(dfd
, pathname
->name
, flags
| LOOKUP_PARENT
, nd
, &base
);
3017 if (unlikely(error
))
3020 current
->total_link_count
= 0;
3021 error
= link_path_walk(pathname
->name
, nd
);
3022 if (unlikely(error
))
3025 error
= do_last(nd
, &path
, file
, op
, &opened
, pathname
);
3026 while (unlikely(error
> 0)) { /* trailing symlink */
3027 struct path link
= path
;
3029 if (!(nd
->flags
& LOOKUP_FOLLOW
)) {
3030 path_put_conditional(&path
, nd
);
3031 path_put(&nd
->path
);
3035 error
= may_follow_link(&link
, nd
);
3036 if (unlikely(error
))
3038 nd
->flags
|= LOOKUP_PARENT
;
3039 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
3040 error
= follow_link(&link
, nd
, &cookie
);
3041 if (unlikely(error
))
3043 error
= do_last(nd
, &path
, file
, op
, &opened
, pathname
);
3044 put_link(nd
, &link
, cookie
);
3047 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
))
3048 path_put(&nd
->root
);
3051 if (!(opened
& FILE_OPENED
)) {
3055 if (unlikely(error
)) {
3056 if (error
== -EOPENSTALE
) {
3057 if (flags
& LOOKUP_RCU
)
3062 file
= ERR_PTR(error
);
3067 struct file
*do_filp_open(int dfd
, struct filename
*pathname
,
3068 const struct open_flags
*op
, int flags
)
3070 struct nameidata nd
;
3073 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_RCU
);
3074 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
3075 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
);
3076 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
3077 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_REVAL
);
3081 struct file
*do_file_open_root(struct dentry
*dentry
, struct vfsmount
*mnt
,
3082 const char *name
, const struct open_flags
*op
, int flags
)
3084 struct nameidata nd
;
3086 struct filename filename
= { .name
= name
};
3089 nd
.root
.dentry
= dentry
;
3091 flags
|= LOOKUP_ROOT
;
3093 if (dentry
->d_inode
->i_op
->follow_link
&& op
->intent
& LOOKUP_OPEN
)
3094 return ERR_PTR(-ELOOP
);
3096 file
= path_openat(-1, &filename
, &nd
, op
, flags
| LOOKUP_RCU
);
3097 if (unlikely(file
== ERR_PTR(-ECHILD
)))
3098 file
= path_openat(-1, &filename
, &nd
, op
, flags
);
3099 if (unlikely(file
== ERR_PTR(-ESTALE
)))
3100 file
= path_openat(-1, &filename
, &nd
, op
, flags
| LOOKUP_REVAL
);
3104 struct dentry
*kern_path_create(int dfd
, const char *pathname
,
3105 struct path
*path
, unsigned int lookup_flags
)
3107 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
3108 struct nameidata nd
;
3111 bool is_dir
= (lookup_flags
& LOOKUP_DIRECTORY
);
3114 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3115 * other flags passed in are ignored!
3117 lookup_flags
&= LOOKUP_REVAL
;
3119 error
= do_path_lookup(dfd
, pathname
, LOOKUP_PARENT
|lookup_flags
, &nd
);
3121 return ERR_PTR(error
);
3124 * Yucky last component or no last component at all?
3125 * (foo/., foo/.., /////)
3127 if (nd
.last_type
!= LAST_NORM
)
3129 nd
.flags
&= ~LOOKUP_PARENT
;
3130 nd
.flags
|= LOOKUP_CREATE
| LOOKUP_EXCL
;
3132 /* don't fail immediately if it's r/o, at least try to report other errors */
3133 err2
= mnt_want_write(nd
.path
.mnt
);
3135 * Do the final lookup.
3137 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3138 dentry
= lookup_hash(&nd
);
3143 if (dentry
->d_inode
)
3146 * Special case - lookup gave negative, but... we had foo/bar/
3147 * From the vfs_mknod() POV we just have a negative dentry -
3148 * all is fine. Let's be bastards - you had / on the end, you've
3149 * been asking for (non-existent) directory. -ENOENT for you.
3151 if (unlikely(!is_dir
&& nd
.last
.name
[nd
.last
.len
])) {
3155 if (unlikely(err2
)) {
3163 dentry
= ERR_PTR(error
);
3165 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
3167 mnt_drop_write(nd
.path
.mnt
);
3172 EXPORT_SYMBOL(kern_path_create
);
3174 void done_path_create(struct path
*path
, struct dentry
*dentry
)
3177 mutex_unlock(&path
->dentry
->d_inode
->i_mutex
);
3178 mnt_drop_write(path
->mnt
);
3181 EXPORT_SYMBOL(done_path_create
);
3183 struct dentry
*user_path_create(int dfd
, const char __user
*pathname
,
3184 struct path
*path
, unsigned int lookup_flags
)
3186 struct filename
*tmp
= getname(pathname
);
3189 return ERR_CAST(tmp
);
3190 res
= kern_path_create(dfd
, tmp
->name
, path
, lookup_flags
);
3194 EXPORT_SYMBOL(user_path_create
);
3196 int vfs_mknod2(struct vfsmount
*mnt
, struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
3198 int error
= may_create(mnt
, dir
, dentry
);
3203 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
3206 if (!dir
->i_op
->mknod
)
3209 error
= devcgroup_inode_mknod(mode
, dev
);
3213 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
3217 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
3219 fsnotify_create(dir
, dentry
);
3222 EXPORT_SYMBOL(vfs_mknod2
);
3224 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
3226 return vfs_mknod2(NULL
, dir
, dentry
, mode
, dev
);
3228 EXPORT_SYMBOL(vfs_mknod
);
3230 static int may_mknod(umode_t mode
)
3232 switch (mode
& S_IFMT
) {
3238 case 0: /* zero mode translates to S_IFREG */
3247 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, umode_t
, mode
,
3250 struct dentry
*dentry
;
3253 unsigned int lookup_flags
= 0;
3255 error
= may_mknod(mode
);
3259 dentry
= user_path_create(dfd
, filename
, &path
, lookup_flags
);
3261 return PTR_ERR(dentry
);
3263 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3264 mode
&= ~current_umask();
3265 error
= security_path_mknod(&path
, dentry
, mode
, dev
);
3268 switch (mode
& S_IFMT
) {
3269 case 0: case S_IFREG
:
3270 error
= vfs_create2(path
.mnt
, path
.dentry
->d_inode
,dentry
,mode
,true);
3272 case S_IFCHR
: case S_IFBLK
:
3273 error
= vfs_mknod2(path
.mnt
, path
.dentry
->d_inode
,dentry
,mode
,
3274 new_decode_dev(dev
));
3276 case S_IFIFO
: case S_IFSOCK
:
3277 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,0);
3281 done_path_create(&path
, dentry
);
3282 if (retry_estale(error
, lookup_flags
)) {
3283 lookup_flags
|= LOOKUP_REVAL
;
3289 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, umode_t
, mode
, unsigned, dev
)
3291 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
3294 int vfs_mkdir2(struct vfsmount
*mnt
, struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
3296 int error
= may_create(mnt
, dir
, dentry
);
3297 unsigned max_links
= dir
->i_sb
->s_max_links
;
3302 if (!dir
->i_op
->mkdir
)
3305 mode
&= (S_IRWXUGO
|S_ISVTX
);
3306 error
= security_inode_mkdir(dir
, dentry
, mode
);
3310 if (max_links
&& dir
->i_nlink
>= max_links
)
3313 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
3315 fsnotify_mkdir(dir
, dentry
);
3318 EXPORT_SYMBOL(vfs_mkdir2
);
3320 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
3322 return vfs_mkdir2(NULL
, dir
, dentry
, mode
);
3324 EXPORT_SYMBOL(vfs_mkdir
);
3326 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, umode_t
, mode
)
3328 struct dentry
*dentry
;
3331 unsigned int lookup_flags
= LOOKUP_DIRECTORY
;
3334 dentry
= user_path_create(dfd
, pathname
, &path
, lookup_flags
);
3336 return PTR_ERR(dentry
);
3338 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3339 mode
&= ~current_umask();
3340 error
= security_path_mkdir(&path
, dentry
, mode
);
3342 error
= vfs_mkdir2(path
.mnt
, path
.dentry
->d_inode
, dentry
, mode
);
3343 done_path_create(&path
, dentry
);
3344 if (retry_estale(error
, lookup_flags
)) {
3345 lookup_flags
|= LOOKUP_REVAL
;
3351 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, umode_t
, mode
)
3353 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
3357 * The dentry_unhash() helper will try to drop the dentry early: we
3358 * should have a usage count of 1 if we're the only user of this
3359 * dentry, and if that is true (possibly after pruning the dcache),
3360 * then we drop the dentry now.
3362 * A low-level filesystem can, if it choses, legally
3365 * if (!d_unhashed(dentry))
3368 * if it cannot handle the case of removing a directory
3369 * that is still in use by something else..
3371 void dentry_unhash(struct dentry
*dentry
)
3373 shrink_dcache_parent(dentry
);
3374 spin_lock(&dentry
->d_lock
);
3375 if (dentry
->d_count
== 1)
3377 spin_unlock(&dentry
->d_lock
);
3380 int vfs_rmdir2(struct vfsmount
*mnt
, struct inode
*dir
, struct dentry
*dentry
)
3382 int error
= may_delete(mnt
, dir
, dentry
, 1);
3387 if (!dir
->i_op
->rmdir
)
3391 mutex_lock(&dentry
->d_inode
->i_mutex
);
3394 if (d_mountpoint(dentry
))
3397 error
= security_inode_rmdir(dir
, dentry
);
3401 shrink_dcache_parent(dentry
);
3402 error
= dir
->i_op
->rmdir(dir
, dentry
);
3406 dentry
->d_inode
->i_flags
|= S_DEAD
;
3410 mutex_unlock(&dentry
->d_inode
->i_mutex
);
3416 EXPORT_SYMBOL(vfs_rmdir2
);
3418 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3420 return vfs_rmdir2(NULL
, dir
, dentry
);
3422 EXPORT_SYMBOL(vfs_rmdir
);
3424 static long do_rmdir(int dfd
, const char __user
*pathname
)
3427 struct filename
*name
;
3428 struct dentry
*dentry
;
3429 struct nameidata nd
;
3430 unsigned int lookup_flags
= 0;
3431 char *path_buf
= NULL
;
3432 char *propagate_path
= NULL
;
3434 name
= user_path_parent(dfd
, pathname
, &nd
, lookup_flags
);
3436 return PTR_ERR(name
);
3438 switch(nd
.last_type
) {
3450 nd
.flags
&= ~LOOKUP_PARENT
;
3451 error
= mnt_want_write(nd
.path
.mnt
);
3455 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3456 dentry
= lookup_hash(&nd
);
3457 error
= PTR_ERR(dentry
);
3460 if (!dentry
->d_inode
) {
3464 error
= security_path_rmdir(&nd
.path
, dentry
);
3467 error
= vfs_rmdir2(nd
.path
.mnt
, nd
.path
.dentry
->d_inode
, dentry
);
3471 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
3472 if (path_buf
&& !error
) {
3473 nd
.path
.dentry
->d_sb
->s_op
->unlink_callback(nd
.path
.dentry
->d_sb
,
3480 mnt_drop_write(nd
.path
.mnt
);
3484 if (retry_estale(error
, lookup_flags
)) {
3485 lookup_flags
|= LOOKUP_REVAL
;
3491 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
3493 return do_rmdir(AT_FDCWD
, pathname
);
3496 int vfs_unlink2(struct vfsmount
*mnt
, struct inode
*dir
, struct dentry
*dentry
)
3498 int error
= may_delete(mnt
, dir
, dentry
, 0);
3503 if (!dir
->i_op
->unlink
)
3506 mutex_lock(&dentry
->d_inode
->i_mutex
);
3507 if (d_mountpoint(dentry
))
3510 error
= security_inode_unlink(dir
, dentry
);
3512 error
= dir
->i_op
->unlink(dir
, dentry
);
3517 mutex_unlock(&dentry
->d_inode
->i_mutex
);
3519 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3520 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
3521 fsnotify_link_count(dentry
->d_inode
);
3527 EXPORT_SYMBOL(vfs_unlink2
);
3529 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3531 return vfs_unlink2(NULL
, dir
, dentry
);
3533 EXPORT_SYMBOL(vfs_unlink
);
3536 * Make sure that the actual truncation of the file will occur outside its
3537 * directory's i_mutex. Truncate can take a long time if there is a lot of
3538 * writeout happening, and we don't want to prevent access to the directory
3539 * while waiting on the I/O.
3541 static long do_unlinkat(int dfd
, const char __user
*pathname
)
3544 struct filename
*name
;
3545 struct dentry
*dentry
;
3546 struct nameidata nd
;
3547 struct inode
*inode
= NULL
;
3548 unsigned int lookup_flags
= 0;
3549 char *path_buf
= NULL
;
3550 char *propagate_path
= NULL
;
3552 name
= user_path_parent(dfd
, pathname
, &nd
, lookup_flags
);
3554 return PTR_ERR(name
);
3557 if (nd
.last_type
!= LAST_NORM
)
3560 nd
.flags
&= ~LOOKUP_PARENT
;
3561 error
= mnt_want_write(nd
.path
.mnt
);
3565 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3566 dentry
= lookup_hash(&nd
);
3567 error
= PTR_ERR(dentry
);
3568 if (!IS_ERR(dentry
)) {
3569 /* Why not before? Because we want correct error value */
3570 if (nd
.last
.name
[nd
.last
.len
])
3572 inode
= dentry
->d_inode
;
3575 if (inode
->i_sb
->s_op
->unlink_callback
) {
3576 path_buf
= kmalloc(PATH_MAX
, GFP_KERNEL
);
3577 propagate_path
= dentry_path_raw(dentry
, path_buf
, PATH_MAX
);
3580 error
= security_path_unlink(&nd
.path
, dentry
);
3583 error
= vfs_unlink2(nd
.path
.mnt
, nd
.path
.dentry
->d_inode
, dentry
);
3587 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
3588 if (path_buf
&& !error
) {
3589 inode
->i_sb
->s_op
->unlink_callback(inode
->i_sb
, propagate_path
);
3596 iput(inode
); /* truncate the inode here */
3597 mnt_drop_write(nd
.path
.mnt
);
3601 if (retry_estale(error
, lookup_flags
)) {
3602 lookup_flags
|= LOOKUP_REVAL
;
3609 error
= !dentry
->d_inode
? -ENOENT
:
3610 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
3614 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
3616 if ((flag
& ~AT_REMOVEDIR
) != 0)
3619 if (flag
& AT_REMOVEDIR
)
3620 return do_rmdir(dfd
, pathname
);
3622 return do_unlinkat(dfd
, pathname
);
3625 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
3627 return do_unlinkat(AT_FDCWD
, pathname
);
3630 int vfs_symlink2(struct vfsmount
*mnt
, struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
3632 int error
= may_create(mnt
, dir
, dentry
);
3637 if (!dir
->i_op
->symlink
)
3640 error
= security_inode_symlink(dir
, dentry
, oldname
);
3644 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
3646 fsnotify_create(dir
, dentry
);
3649 EXPORT_SYMBOL(vfs_symlink2
);
3651 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
3653 return vfs_symlink2(NULL
, dir
, dentry
, oldname
);
3655 EXPORT_SYMBOL(vfs_symlink
);
3657 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
3658 int, newdfd
, const char __user
*, newname
)
3661 struct filename
*from
;
3662 struct dentry
*dentry
;
3664 unsigned int lookup_flags
= 0;
3666 from
= getname(oldname
);
3668 return PTR_ERR(from
);
3670 dentry
= user_path_create(newdfd
, newname
, &path
, lookup_flags
);
3671 error
= PTR_ERR(dentry
);
3675 error
= security_path_symlink(&path
, dentry
, from
->name
);
3677 error
= vfs_symlink2(path
.mnt
, path
.dentry
->d_inode
, dentry
, from
->name
);
3678 done_path_create(&path
, dentry
);
3679 if (retry_estale(error
, lookup_flags
)) {
3680 lookup_flags
|= LOOKUP_REVAL
;
3688 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
3690 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
3693 int vfs_link2(struct vfsmount
*mnt
, struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
3695 struct inode
*inode
= old_dentry
->d_inode
;
3696 unsigned max_links
= dir
->i_sb
->s_max_links
;
3702 error
= may_create(mnt
, dir
, new_dentry
);
3706 if (dir
->i_sb
!= inode
->i_sb
)
3710 * A link to an append-only or immutable file cannot be created.
3712 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
3714 if (!dir
->i_op
->link
)
3716 if (S_ISDIR(inode
->i_mode
))
3719 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
3723 mutex_lock(&inode
->i_mutex
);
3724 /* Make sure we don't allow creating hardlink to an unlinked file */
3725 if (inode
->i_nlink
== 0)
3727 else if (max_links
&& inode
->i_nlink
>= max_links
)
3730 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
3731 mutex_unlock(&inode
->i_mutex
);
3733 fsnotify_link(dir
, inode
, new_dentry
);
3736 EXPORT_SYMBOL(vfs_link2
);
3738 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
3740 return vfs_link2(NULL
, old_dentry
, dir
, new_dentry
);
3742 EXPORT_SYMBOL(vfs_link
);
3745 * Hardlinks are often used in delicate situations. We avoid
3746 * security-related surprises by not following symlinks on the
3749 * We don't follow them on the oldname either to be compatible
3750 * with linux 2.0, and to avoid hard-linking to directories
3751 * and other special files. --ADM
3753 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
3754 int, newdfd
, const char __user
*, newname
, int, flags
)
3756 struct dentry
*new_dentry
;
3757 struct path old_path
, new_path
;
3761 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
3764 * To use null names we require CAP_DAC_READ_SEARCH
3765 * This ensures that not everyone will be able to create
3766 * handlink using the passed filedescriptor.
3768 if (flags
& AT_EMPTY_PATH
) {
3769 if (!capable(CAP_DAC_READ_SEARCH
))
3774 if (flags
& AT_SYMLINK_FOLLOW
)
3775 how
|= LOOKUP_FOLLOW
;
3777 error
= user_path_at(olddfd
, oldname
, how
, &old_path
);
3781 new_dentry
= user_path_create(newdfd
, newname
, &new_path
,
3782 (how
& LOOKUP_REVAL
));
3783 error
= PTR_ERR(new_dentry
);
3784 if (IS_ERR(new_dentry
))
3788 if (old_path
.mnt
!= new_path
.mnt
)
3790 error
= may_linkat(&old_path
);
3791 if (unlikely(error
))
3793 error
= security_path_link(old_path
.dentry
, &new_path
, new_dentry
);
3796 error
= vfs_link2(old_path
.mnt
, old_path
.dentry
, new_path
.dentry
->d_inode
, new_dentry
);
3798 done_path_create(&new_path
, new_dentry
);
3799 if (retry_estale(error
, how
)) {
3800 path_put(&old_path
);
3801 how
|= LOOKUP_REVAL
;
3805 path_put(&old_path
);
3810 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
3812 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
3816 * The worst of all namespace operations - renaming directory. "Perverted"
3817 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3819 * a) we can get into loop creation. Check is done in is_subdir().
3820 * b) race potential - two innocent renames can create a loop together.
3821 * That's where 4.4 screws up. Current fix: serialization on
3822 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3824 * c) we have to lock _three_ objects - parents and victim (if it exists).
3825 * And that - after we got ->i_mutex on parents (until then we don't know
3826 * whether the target exists). Solution: try to be smart with locking
3827 * order for inodes. We rely on the fact that tree topology may change
3828 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3829 * move will be locked. Thus we can rank directories by the tree
3830 * (ancestors first) and rank all non-directories after them.
3831 * That works since everybody except rename does "lock parent, lookup,
3832 * lock child" and rename is under ->s_vfs_rename_mutex.
3833 * HOWEVER, it relies on the assumption that any object with ->lookup()
3834 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3835 * we'd better make sure that there's no link(2) for them.
3836 * d) conversion from fhandle to dentry may come in the wrong moment - when
3837 * we are removing the target. Solution: we will have to grab ->i_mutex
3838 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3839 * ->i_mutex on parents, which works but leads to some truly excessive
3842 static int vfs_rename_dir(struct vfsmount
*mnt
,
3843 struct inode
*old_dir
, struct dentry
*old_dentry
,
3844 struct inode
*new_dir
, struct dentry
*new_dentry
)
3847 struct inode
*target
= new_dentry
->d_inode
;
3848 unsigned max_links
= new_dir
->i_sb
->s_max_links
;
3851 * If we are going to change the parent - check write permissions,
3852 * we'll need to flip '..'.
3854 if (new_dir
!= old_dir
) {
3855 error
= inode_permission2(mnt
, old_dentry
->d_inode
, MAY_WRITE
);
3860 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3866 mutex_lock(&target
->i_mutex
);
3869 if (d_mountpoint(old_dentry
) || d_mountpoint(new_dentry
))
3873 if (max_links
&& !target
&& new_dir
!= old_dir
&&
3874 new_dir
->i_nlink
>= max_links
)
3878 shrink_dcache_parent(new_dentry
);
3879 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3884 target
->i_flags
|= S_DEAD
;
3885 dont_mount(new_dentry
);
3889 mutex_unlock(&target
->i_mutex
);
3892 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
3893 d_move(old_dentry
,new_dentry
);
3897 static int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
3898 struct inode
*new_dir
, struct dentry
*new_dentry
)
3900 struct inode
*target
= new_dentry
->d_inode
;
3903 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3909 mutex_lock(&target
->i_mutex
);
3912 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
3915 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3920 dont_mount(new_dentry
);
3921 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
3922 d_move(old_dentry
, new_dentry
);
3925 mutex_unlock(&target
->i_mutex
);
3930 int vfs_rename2(struct vfsmount
*mnt
,
3931 struct inode
*old_dir
, struct dentry
*old_dentry
,
3932 struct inode
*new_dir
, struct dentry
*new_dentry
)
3935 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
3936 struct name_snapshot old_name
;
3938 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
3941 error
= may_delete(mnt
, old_dir
, old_dentry
, is_dir
);
3945 if (!new_dentry
->d_inode
)
3946 error
= may_create(mnt
, new_dir
, new_dentry
);
3948 error
= may_delete(mnt
, new_dir
, new_dentry
, is_dir
);
3952 if (!old_dir
->i_op
->rename
)
3955 take_dentry_name_snapshot(&old_name
, old_dentry
);
3958 error
= vfs_rename_dir(mnt
, old_dir
,old_dentry
,new_dir
,new_dentry
);
3960 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
3962 fsnotify_move(old_dir
, new_dir
, old_name
.name
, is_dir
,
3963 new_dentry
->d_inode
, old_dentry
);
3964 release_dentry_name_snapshot(&old_name
);
3968 EXPORT_SYMBOL(vfs_rename2
);
3970 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
3971 struct inode
*new_dir
, struct dentry
*new_dentry
)
3973 return vfs_rename2(NULL
, old_dir
, old_dentry
, new_dir
, new_dentry
);
3975 EXPORT_SYMBOL(vfs_rename
);
3977 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
3978 int, newdfd
, const char __user
*, newname
)
3980 struct dentry
*old_dir
, *new_dir
;
3981 struct dentry
*old_dentry
, *new_dentry
;
3982 struct dentry
*trap
;
3983 struct nameidata oldnd
, newnd
;
3984 struct filename
*from
;
3985 struct filename
*to
;
3986 unsigned int lookup_flags
= 0;
3987 bool should_retry
= false;
3990 from
= user_path_parent(olddfd
, oldname
, &oldnd
, lookup_flags
);
3992 error
= PTR_ERR(from
);
3996 to
= user_path_parent(newdfd
, newname
, &newnd
, lookup_flags
);
3998 error
= PTR_ERR(to
);
4003 if (oldnd
.path
.mnt
!= newnd
.path
.mnt
)
4006 old_dir
= oldnd
.path
.dentry
;
4008 if (oldnd
.last_type
!= LAST_NORM
)
4011 new_dir
= newnd
.path
.dentry
;
4012 if (newnd
.last_type
!= LAST_NORM
)
4015 error
= mnt_want_write(oldnd
.path
.mnt
);
4019 oldnd
.flags
&= ~LOOKUP_PARENT
;
4020 newnd
.flags
&= ~LOOKUP_PARENT
;
4021 newnd
.flags
|= LOOKUP_RENAME_TARGET
;
4023 trap
= lock_rename(new_dir
, old_dir
);
4025 old_dentry
= lookup_hash(&oldnd
);
4026 error
= PTR_ERR(old_dentry
);
4027 if (IS_ERR(old_dentry
))
4029 /* source must exist */
4031 if (!old_dentry
->d_inode
)
4033 /* unless the source is a directory trailing slashes give -ENOTDIR */
4034 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
4036 if (oldnd
.last
.name
[oldnd
.last
.len
])
4038 if (newnd
.last
.name
[newnd
.last
.len
])
4041 /* source should not be ancestor of target */
4043 if (old_dentry
== trap
)
4045 new_dentry
= lookup_hash(&newnd
);
4046 error
= PTR_ERR(new_dentry
);
4047 if (IS_ERR(new_dentry
))
4049 /* target should not be an ancestor of source */
4051 if (new_dentry
== trap
)
4054 error
= security_path_rename(&oldnd
.path
, old_dentry
,
4055 &newnd
.path
, new_dentry
);
4058 error
= vfs_rename2(oldnd
.path
.mnt
, old_dir
->d_inode
, old_dentry
,
4059 new_dir
->d_inode
, new_dentry
);
4065 unlock_rename(new_dir
, old_dir
);
4066 mnt_drop_write(oldnd
.path
.mnt
);
4068 if (retry_estale(error
, lookup_flags
))
4069 should_retry
= true;
4070 path_put(&newnd
.path
);
4073 path_put(&oldnd
.path
);
4076 should_retry
= false;
4077 lookup_flags
|= LOOKUP_REVAL
;
4084 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
4086 return sys_renameat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
);
4089 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
4093 len
= PTR_ERR(link
);
4098 if (len
> (unsigned) buflen
)
4100 if (copy_to_user(buffer
, link
, len
))
4107 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4108 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4109 * using) it for any given inode is up to filesystem.
4111 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4113 struct nameidata nd
;
4118 cookie
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
4120 return PTR_ERR(cookie
);
4122 res
= vfs_readlink(dentry
, buffer
, buflen
, nd_get_link(&nd
));
4123 if (dentry
->d_inode
->i_op
->put_link
)
4124 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
, cookie
);
4128 int vfs_follow_link(struct nameidata
*nd
, const char *link
)
4130 return __vfs_follow_link(nd
, link
);
4133 /* get the link contents into pagecache */
4134 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
4138 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
4139 page
= read_mapping_page(mapping
, 0, NULL
);
4144 nd_terminate_link(kaddr
, dentry
->d_inode
->i_size
, PAGE_SIZE
- 1);
4148 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4150 struct page
*page
= NULL
;
4151 char *s
= page_getlink(dentry
, &page
);
4152 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
4155 page_cache_release(page
);
4160 void *page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
4162 struct page
*page
= NULL
;
4163 nd_set_link(nd
, page_getlink(dentry
, &page
));
4167 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
4169 struct page
*page
= cookie
;
4173 page_cache_release(page
);
4178 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4180 int __page_symlink(struct inode
*inode
, const char *symname
, int len
, int nofs
)
4182 struct address_space
*mapping
= inode
->i_mapping
;
4187 unsigned int flags
= AOP_FLAG_UNINTERRUPTIBLE
;
4189 flags
|= AOP_FLAG_NOFS
;
4192 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
4193 flags
, &page
, &fsdata
);
4197 kaddr
= kmap_atomic(page
);
4198 memcpy(kaddr
, symname
, len
-1);
4199 kunmap_atomic(kaddr
);
4201 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
4208 mark_inode_dirty(inode
);
4214 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
4216 return __page_symlink(inode
, symname
, len
,
4217 !(mapping_gfp_mask(inode
->i_mapping
) & __GFP_FS
));
4220 const struct inode_operations page_symlink_inode_operations
= {
4221 .readlink
= generic_readlink
,
4222 .follow_link
= page_follow_link_light
,
4223 .put_link
= page_put_link
,
4226 EXPORT_SYMBOL(user_path_at
);
4227 EXPORT_SYMBOL(follow_down_one
);
4228 EXPORT_SYMBOL(follow_down
);
4229 EXPORT_SYMBOL(follow_up
);
4230 EXPORT_SYMBOL(get_write_access
); /* nfsd */
4231 EXPORT_SYMBOL(lock_rename
);
4232 EXPORT_SYMBOL(page_follow_link_light
);
4233 EXPORT_SYMBOL(page_put_link
);
4234 EXPORT_SYMBOL(page_readlink
);
4235 EXPORT_SYMBOL(__page_symlink
);
4236 EXPORT_SYMBOL(page_symlink
);
4237 EXPORT_SYMBOL(page_symlink_inode_operations
);
4238 EXPORT_SYMBOL(kern_path
);
4239 EXPORT_SYMBOL(vfs_path_lookup
);
4240 EXPORT_SYMBOL(unlock_rename
);
4241 EXPORT_SYMBOL(vfs_follow_link
);
4242 EXPORT_SYMBOL(generic_permission
);
4243 EXPORT_SYMBOL(vfs_readlink
);
4244 EXPORT_SYMBOL(dentry_unhash
);
4245 EXPORT_SYMBOL(generic_readlink
);