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 <asm/uaccess.h>
42 /* [Feb-1997 T. Schoebel-Theuer]
43 * Fundamental changes in the pathname lookup mechanisms (namei)
44 * were necessary because of omirr. The reason is that omirr needs
45 * to know the _real_ pathname, not the user-supplied one, in case
46 * of symlinks (and also when transname replacements occur).
48 * The new code replaces the old recursive symlink resolution with
49 * an iterative one (in case of non-nested symlink chains). It does
50 * this with calls to <fs>_follow_link().
51 * As a side effect, dir_namei(), _namei() and follow_link() are now
52 * replaced with a single function lookup_dentry() that can handle all
53 * the special cases of the former code.
55 * With the new dcache, the pathname is stored at each inode, at least as
56 * long as the refcount of the inode is positive. As a side effect, the
57 * size of the dcache depends on the inode cache and thus is dynamic.
59 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
60 * resolution to correspond with current state of the code.
62 * Note that the symlink resolution is not *completely* iterative.
63 * There is still a significant amount of tail- and mid- recursion in
64 * the algorithm. Also, note that <fs>_readlink() is not used in
65 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
66 * may return different results than <fs>_follow_link(). Many virtual
67 * filesystems (including /proc) exhibit this behavior.
70 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
71 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
72 * and the name already exists in form of a symlink, try to create the new
73 * name indicated by the symlink. The old code always complained that the
74 * name already exists, due to not following the symlink even if its target
75 * is nonexistent. The new semantics affects also mknod() and link() when
76 * the name is a symlink pointing to a non-existent name.
78 * I don't know which semantics is the right one, since I have no access
79 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
80 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
81 * "old" one. Personally, I think the new semantics is much more logical.
82 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
83 * file does succeed in both HP-UX and SunOs, but not in Solaris
84 * and in the old Linux semantics.
87 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
88 * semantics. See the comments in "open_namei" and "do_link" below.
90 * [10-Sep-98 Alan Modra] Another symlink change.
93 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
94 * inside the path - always follow.
95 * in the last component in creation/removal/renaming - never follow.
96 * if LOOKUP_FOLLOW passed - follow.
97 * if the pathname has trailing slashes - follow.
98 * otherwise - don't follow.
99 * (applied in that order).
101 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
102 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
103 * During the 2.4 we need to fix the userland stuff depending on it -
104 * hopefully we will be able to get rid of that wart in 2.5. So far only
105 * XEmacs seems to be relying on it...
108 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
109 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
110 * any extra contention...
113 /* In order to reduce some races, while at the same time doing additional
114 * checking and hopefully speeding things up, we copy filenames to the
115 * kernel data space before using them..
117 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
118 * PATH_MAX includes the nul terminator --RR.
120 void final_putname(struct filename
*name
)
122 if (name
->separate
) {
123 __putname(name
->name
);
130 #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
132 static struct filename
*
133 getname_flags(const char __user
*filename
, int flags
, int *empty
)
135 struct filename
*result
, *err
;
140 result
= audit_reusename(filename
);
144 result
= __getname();
145 if (unlikely(!result
))
146 return ERR_PTR(-ENOMEM
);
149 * First, try to embed the struct filename inside the names_cache
152 kname
= (char *)result
+ sizeof(*result
);
153 result
->name
= kname
;
154 result
->separate
= false;
155 max
= EMBEDDED_NAME_MAX
;
158 len
= strncpy_from_user(kname
, filename
, max
);
159 if (unlikely(len
< 0)) {
165 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
166 * separate struct filename so we can dedicate the entire
167 * names_cache allocation for the pathname, and re-do the copy from
170 if (len
== EMBEDDED_NAME_MAX
&& max
== EMBEDDED_NAME_MAX
) {
171 kname
= (char *)result
;
173 result
= kzalloc(sizeof(*result
), GFP_KERNEL
);
175 err
= ERR_PTR(-ENOMEM
);
176 result
= (struct filename
*)kname
;
179 result
->name
= kname
;
180 result
->separate
= true;
185 /* The empty path is special. */
186 if (unlikely(!len
)) {
189 err
= ERR_PTR(-ENOENT
);
190 if (!(flags
& LOOKUP_EMPTY
))
194 err
= ERR_PTR(-ENAMETOOLONG
);
195 if (unlikely(len
>= PATH_MAX
))
198 result
->uptr
= filename
;
199 audit_getname(result
);
203 final_putname(result
);
208 getname(const char __user
* filename
)
210 return getname_flags(filename
, 0, NULL
);
212 EXPORT_SYMBOL(getname
);
214 #ifdef CONFIG_AUDITSYSCALL
215 void putname(struct filename
*name
)
217 if (unlikely(!audit_dummy_context()))
218 return audit_putname(name
);
223 static int check_acl(struct inode
*inode
, int mask
)
225 #ifdef CONFIG_FS_POSIX_ACL
226 struct posix_acl
*acl
;
228 if (mask
& MAY_NOT_BLOCK
) {
229 acl
= get_cached_acl_rcu(inode
, ACL_TYPE_ACCESS
);
232 /* no ->get_acl() calls in RCU mode... */
233 if (acl
== ACL_NOT_CACHED
)
235 return posix_acl_permission(inode
, acl
, mask
& ~MAY_NOT_BLOCK
);
238 acl
= get_cached_acl(inode
, ACL_TYPE_ACCESS
);
241 * A filesystem can force a ACL callback by just never filling the
242 * ACL cache. But normally you'd fill the cache either at inode
243 * instantiation time, or on the first ->get_acl call.
245 * If the filesystem doesn't have a get_acl() function at all, we'll
246 * just create the negative cache entry.
248 if (acl
== ACL_NOT_CACHED
) {
249 if (inode
->i_op
->get_acl
) {
250 acl
= inode
->i_op
->get_acl(inode
, ACL_TYPE_ACCESS
);
254 set_cached_acl(inode
, ACL_TYPE_ACCESS
, NULL
);
260 int error
= posix_acl_permission(inode
, acl
, mask
);
261 posix_acl_release(acl
);
270 * This does the basic permission checking
272 static int acl_permission_check(struct inode
*inode
, int mask
)
274 unsigned int mode
= inode
->i_mode
;
276 if (likely(uid_eq(current_fsuid(), inode
->i_uid
)))
279 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
)) {
280 int error
= check_acl(inode
, mask
);
281 if (error
!= -EAGAIN
)
285 if (in_group_p(inode
->i_gid
))
290 * If the DACs are ok we don't need any capability check.
292 if ((mask
& ~mode
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
)) == 0)
298 * generic_permission - check for access rights on a Posix-like filesystem
299 * @inode: inode to check access rights for
300 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
302 * Used to check for read/write/execute permissions on a file.
303 * We use "fsuid" for this, letting us set arbitrary permissions
304 * for filesystem access without changing the "normal" uids which
305 * are used for other things.
307 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
308 * request cannot be satisfied (eg. requires blocking or too much complexity).
309 * It would then be called again in ref-walk mode.
311 int generic_permission(struct inode
*inode
, int mask
)
316 * Do the basic permission checks.
318 ret
= acl_permission_check(inode
, mask
);
322 if (S_ISDIR(inode
->i_mode
)) {
323 /* DACs are overridable for directories */
324 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
326 if (!(mask
& MAY_WRITE
))
327 if (capable_wrt_inode_uidgid(inode
,
328 CAP_DAC_READ_SEARCH
))
333 * Read/write DACs are always overridable.
334 * Executable DACs are overridable when there is
335 * at least one exec bit set.
337 if (!(mask
& MAY_EXEC
) || (inode
->i_mode
& S_IXUGO
))
338 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
342 * Searching includes executable on directories, else just read.
344 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
345 if (mask
== MAY_READ
)
346 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_READ_SEARCH
))
353 * We _really_ want to just do "generic_permission()" without
354 * even looking at the inode->i_op values. So we keep a cache
355 * flag in inode->i_opflags, that says "this has not special
356 * permission function, use the fast case".
358 static inline int do_inode_permission(struct inode
*inode
, int mask
)
360 if (unlikely(!(inode
->i_opflags
& IOP_FASTPERM
))) {
361 if (likely(inode
->i_op
->permission
))
362 return inode
->i_op
->permission(inode
, mask
);
364 /* This gets set once for the inode lifetime */
365 spin_lock(&inode
->i_lock
);
366 inode
->i_opflags
|= IOP_FASTPERM
;
367 spin_unlock(&inode
->i_lock
);
369 return generic_permission(inode
, mask
);
373 * __inode_permission - Check for access rights to a given inode
374 * @inode: Inode to check permission on
375 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
377 * Check for read/write/execute permissions on an inode.
379 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
381 * This does not check for a read-only file system. You probably want
382 * inode_permission().
384 int __inode_permission(struct inode
*inode
, int mask
)
388 if (unlikely(mask
& MAY_WRITE
)) {
390 * Nobody gets write access to an immutable file.
392 if (IS_IMMUTABLE(inode
))
396 retval
= do_inode_permission(inode
, mask
);
400 retval
= devcgroup_inode_permission(inode
, mask
);
404 return security_inode_permission(inode
, mask
);
408 * sb_permission - Check superblock-level permissions
409 * @sb: Superblock of inode to check permission on
410 * @inode: Inode to check permission on
411 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
413 * Separate out file-system wide checks from inode-specific permission checks.
415 static int sb_permission(struct super_block
*sb
, struct inode
*inode
, int mask
)
417 if (unlikely(mask
& MAY_WRITE
)) {
418 umode_t mode
= inode
->i_mode
;
420 /* Nobody gets write access to a read-only fs. */
421 if ((sb
->s_flags
& MS_RDONLY
) &&
422 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
429 * inode_permission - Check for access rights to a given inode
430 * @inode: Inode to check permission on
431 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
433 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
434 * this, letting us set arbitrary permissions for filesystem access without
435 * changing the "normal" UIDs which are used for other things.
437 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
439 int inode_permission(struct inode
*inode
, int mask
)
443 retval
= sb_permission(inode
->i_sb
, inode
, mask
);
446 return __inode_permission(inode
, mask
);
450 * path_get - get a reference to a path
451 * @path: path to get the reference to
453 * Given a path increment the reference count to the dentry and the vfsmount.
455 void path_get(const struct path
*path
)
460 EXPORT_SYMBOL(path_get
);
463 * path_put - put a reference to a path
464 * @path: path to put the reference to
466 * Given a path decrement the reference count to the dentry and the vfsmount.
468 void path_put(const struct path
*path
)
473 EXPORT_SYMBOL(path_put
);
476 * Path walking has 2 modes, rcu-walk and ref-walk (see
477 * Documentation/filesystems/path-lookup.txt). In situations when we can't
478 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
479 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
480 * mode. Refcounts are grabbed at the last known good point before rcu-walk
481 * got stuck, so ref-walk may continue from there. If this is not successful
482 * (eg. a seqcount has changed), then failure is returned and it's up to caller
483 * to restart the path walk from the beginning in ref-walk mode.
486 static inline void lock_rcu_walk(void)
488 br_read_lock(&vfsmount_lock
);
492 static inline void unlock_rcu_walk(void)
495 br_read_unlock(&vfsmount_lock
);
499 * unlazy_walk - try to switch to ref-walk mode.
500 * @nd: nameidata pathwalk data
501 * @dentry: child of nd->path.dentry or NULL
502 * Returns: 0 on success, -ECHILD on failure
504 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
505 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
506 * @nd or NULL. Must be called from rcu-walk context.
508 static int unlazy_walk(struct nameidata
*nd
, struct dentry
*dentry
)
510 struct fs_struct
*fs
= current
->fs
;
511 struct dentry
*parent
= nd
->path
.dentry
;
513 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
516 * Get a reference to the parent first: we're
517 * going to make "path_put(nd->path)" valid in
518 * non-RCU context for "terminate_walk()".
520 * If this doesn't work, return immediately with
521 * RCU walking still active (and then we will do
522 * the RCU walk cleanup in terminate_walk()).
524 if (!lockref_get_not_dead(&parent
->d_lockref
))
528 * After the mntget(), we terminate_walk() will do
529 * the right thing for non-RCU mode, and all our
530 * subsequent exit cases should unlock_rcu_walk()
533 mntget(nd
->path
.mnt
);
534 nd
->flags
&= ~LOOKUP_RCU
;
537 * For a negative lookup, the lookup sequence point is the parents
538 * sequence point, and it only needs to revalidate the parent dentry.
540 * For a positive lookup, we need to move both the parent and the
541 * dentry from the RCU domain to be properly refcounted. And the
542 * sequence number in the dentry validates *both* dentry counters,
543 * since we checked the sequence number of the parent after we got
544 * the child sequence number. So we know the parent must still
545 * be valid if the child sequence number is still valid.
548 if (read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
550 BUG_ON(nd
->inode
!= parent
->d_inode
);
552 if (!lockref_get_not_dead(&dentry
->d_lockref
))
554 if (read_seqcount_retry(&dentry
->d_seq
, nd
->seq
))
559 * Sequence counts matched. Now make sure that the root is
560 * still valid and get it if required.
562 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
563 spin_lock(&fs
->lock
);
564 if (nd
->root
.mnt
!= fs
->root
.mnt
|| nd
->root
.dentry
!= fs
->root
.dentry
)
565 goto unlock_and_drop_dentry
;
567 spin_unlock(&fs
->lock
);
573 unlock_and_drop_dentry
:
574 spin_unlock(&fs
->lock
);
582 if (!(nd
->flags
& LOOKUP_ROOT
))
587 static inline int d_revalidate(struct dentry
*dentry
, unsigned int flags
)
589 return dentry
->d_op
->d_revalidate(dentry
, flags
);
593 * complete_walk - successful completion of path walk
594 * @nd: pointer nameidata
596 * If we had been in RCU mode, drop out of it and legitimize nd->path.
597 * Revalidate the final result, unless we'd already done that during
598 * the path walk or the filesystem doesn't ask for it. Return 0 on
599 * success, -error on failure. In case of failure caller does not
600 * need to drop nd->path.
602 static int complete_walk(struct nameidata
*nd
)
604 struct dentry
*dentry
= nd
->path
.dentry
;
607 if (nd
->flags
& LOOKUP_RCU
) {
608 nd
->flags
&= ~LOOKUP_RCU
;
609 if (!(nd
->flags
& LOOKUP_ROOT
))
612 if (unlikely(!lockref_get_not_dead(&dentry
->d_lockref
))) {
616 if (read_seqcount_retry(&dentry
->d_seq
, nd
->seq
)) {
621 mntget(nd
->path
.mnt
);
625 if (likely(!(nd
->flags
& LOOKUP_JUMPED
)))
628 if (likely(!(dentry
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
)))
631 status
= dentry
->d_op
->d_weak_revalidate(dentry
, nd
->flags
);
642 static __always_inline
void set_root(struct nameidata
*nd
)
645 get_fs_root(current
->fs
, &nd
->root
);
648 static int link_path_walk(const char *, struct nameidata
*);
650 static __always_inline
void set_root_rcu(struct nameidata
*nd
)
653 struct fs_struct
*fs
= current
->fs
;
657 seq
= read_seqcount_begin(&fs
->seq
);
659 nd
->seq
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
660 } while (read_seqcount_retry(&fs
->seq
, seq
));
664 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
667 if (path
->mnt
!= nd
->path
.mnt
)
671 static inline void path_to_nameidata(const struct path
*path
,
672 struct nameidata
*nd
)
674 if (!(nd
->flags
& LOOKUP_RCU
)) {
675 dput(nd
->path
.dentry
);
676 if (nd
->path
.mnt
!= path
->mnt
)
677 mntput(nd
->path
.mnt
);
679 nd
->path
.mnt
= path
->mnt
;
680 nd
->path
.dentry
= path
->dentry
;
684 * Helper to directly jump to a known parsed path from ->follow_link,
685 * caller must have taken a reference to path beforehand.
687 void nd_jump_link(struct nameidata
*nd
, struct path
*path
)
692 nd
->inode
= nd
->path
.dentry
->d_inode
;
693 nd
->flags
|= LOOKUP_JUMPED
;
696 static inline void put_link(struct nameidata
*nd
, struct path
*link
, void *cookie
)
698 struct inode
*inode
= link
->dentry
->d_inode
;
699 if (inode
->i_op
->put_link
)
700 inode
->i_op
->put_link(link
->dentry
, nd
, cookie
);
704 int sysctl_protected_symlinks __read_mostly
= 0;
705 int sysctl_protected_hardlinks __read_mostly
= 0;
708 * may_follow_link - Check symlink following for unsafe situations
709 * @link: The path of the symlink
710 * @nd: nameidata pathwalk data
712 * In the case of the sysctl_protected_symlinks sysctl being enabled,
713 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
714 * in a sticky world-writable directory. This is to protect privileged
715 * processes from failing races against path names that may change out
716 * from under them by way of other users creating malicious symlinks.
717 * It will permit symlinks to be followed only when outside a sticky
718 * world-writable directory, or when the uid of the symlink and follower
719 * match, or when the directory owner matches the symlink's owner.
721 * Returns 0 if following the symlink is allowed, -ve on error.
723 static inline int may_follow_link(struct path
*link
, struct nameidata
*nd
)
725 const struct inode
*inode
;
726 const struct inode
*parent
;
728 if (!sysctl_protected_symlinks
)
731 /* Allowed if owner and follower match. */
732 inode
= link
->dentry
->d_inode
;
733 if (uid_eq(current_cred()->fsuid
, inode
->i_uid
))
736 /* Allowed if parent directory not sticky and world-writable. */
737 parent
= nd
->path
.dentry
->d_inode
;
738 if ((parent
->i_mode
& (S_ISVTX
|S_IWOTH
)) != (S_ISVTX
|S_IWOTH
))
741 /* Allowed if parent directory and link owner match. */
742 if (uid_eq(parent
->i_uid
, inode
->i_uid
))
745 audit_log_link_denied("follow_link", link
);
746 path_put_conditional(link
, nd
);
752 * safe_hardlink_source - Check for safe hardlink conditions
753 * @inode: the source inode to hardlink from
755 * Return false if at least one of the following conditions:
756 * - inode is not a regular file
758 * - inode is setgid and group-exec
759 * - access failure for read and write
761 * Otherwise returns true.
763 static bool safe_hardlink_source(struct inode
*inode
)
765 umode_t mode
= inode
->i_mode
;
767 /* Special files should not get pinned to the filesystem. */
771 /* Setuid files should not get pinned to the filesystem. */
775 /* Executable setgid files should not get pinned to the filesystem. */
776 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
))
779 /* Hardlinking to unreadable or unwritable sources is dangerous. */
780 if (inode_permission(inode
, MAY_READ
| MAY_WRITE
))
787 * may_linkat - Check permissions for creating a hardlink
788 * @link: the source to hardlink from
790 * Block hardlink when all of:
791 * - sysctl_protected_hardlinks enabled
792 * - fsuid does not match inode
793 * - hardlink source is unsafe (see safe_hardlink_source() above)
796 * Returns 0 if successful, -ve on error.
798 static int may_linkat(struct path
*link
)
800 const struct cred
*cred
;
803 if (!sysctl_protected_hardlinks
)
806 cred
= current_cred();
807 inode
= link
->dentry
->d_inode
;
809 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
810 * otherwise, it must be a safe source.
812 if (uid_eq(cred
->fsuid
, inode
->i_uid
) || safe_hardlink_source(inode
) ||
816 audit_log_link_denied("linkat", link
);
820 static __always_inline
int
821 follow_link(struct path
*link
, struct nameidata
*nd
, void **p
)
823 struct dentry
*dentry
= link
->dentry
;
827 BUG_ON(nd
->flags
& LOOKUP_RCU
);
829 if (link
->mnt
== nd
->path
.mnt
)
833 if (unlikely(current
->total_link_count
>= 40))
834 goto out_put_nd_path
;
837 current
->total_link_count
++;
840 nd_set_link(nd
, NULL
);
842 error
= security_inode_follow_link(link
->dentry
, nd
);
844 goto out_put_nd_path
;
846 nd
->last_type
= LAST_BIND
;
847 *p
= dentry
->d_inode
->i_op
->follow_link(dentry
, nd
);
850 goto out_put_nd_path
;
855 if (unlikely(IS_ERR(s
))) {
857 put_link(nd
, link
, *p
);
865 nd
->flags
|= LOOKUP_JUMPED
;
867 nd
->inode
= nd
->path
.dentry
->d_inode
;
868 error
= link_path_walk(s
, nd
);
870 put_link(nd
, link
, *p
);
882 static int follow_up_rcu(struct path
*path
)
884 struct mount
*mnt
= real_mount(path
->mnt
);
885 struct mount
*parent
;
886 struct dentry
*mountpoint
;
888 parent
= mnt
->mnt_parent
;
889 if (&parent
->mnt
== path
->mnt
)
891 mountpoint
= mnt
->mnt_mountpoint
;
892 path
->dentry
= mountpoint
;
893 path
->mnt
= &parent
->mnt
;
898 * follow_up - Find the mountpoint of path's vfsmount
900 * Given a path, find the mountpoint of its source file system.
901 * Replace @path with the path of the mountpoint in the parent mount.
904 * Return 1 if we went up a level and 0 if we were already at the
907 int follow_up(struct path
*path
)
909 struct mount
*mnt
= real_mount(path
->mnt
);
910 struct mount
*parent
;
911 struct dentry
*mountpoint
;
913 br_read_lock(&vfsmount_lock
);
914 parent
= mnt
->mnt_parent
;
916 br_read_unlock(&vfsmount_lock
);
919 mntget(&parent
->mnt
);
920 mountpoint
= dget(mnt
->mnt_mountpoint
);
921 br_read_unlock(&vfsmount_lock
);
923 path
->dentry
= mountpoint
;
925 path
->mnt
= &parent
->mnt
;
930 * Perform an automount
931 * - return -EISDIR to tell follow_managed() to stop and return the path we
934 static int follow_automount(struct path
*path
, unsigned flags
,
937 struct vfsmount
*mnt
;
940 if (!path
->dentry
->d_op
|| !path
->dentry
->d_op
->d_automount
)
943 /* We don't want to mount if someone's just doing a stat -
944 * unless they're stat'ing a directory and appended a '/' to
947 * We do, however, want to mount if someone wants to open or
948 * create a file of any type under the mountpoint, wants to
949 * traverse through the mountpoint or wants to open the
950 * mounted directory. Also, autofs may mark negative dentries
951 * as being automount points. These will need the attentions
952 * of the daemon to instantiate them before they can be used.
954 if (!(flags
& (LOOKUP_PARENT
| LOOKUP_DIRECTORY
|
955 LOOKUP_OPEN
| LOOKUP_CREATE
| LOOKUP_AUTOMOUNT
)) &&
956 path
->dentry
->d_inode
)
959 current
->total_link_count
++;
960 if (current
->total_link_count
>= 40)
963 mnt
= path
->dentry
->d_op
->d_automount(path
);
966 * The filesystem is allowed to return -EISDIR here to indicate
967 * it doesn't want to automount. For instance, autofs would do
968 * this so that its userspace daemon can mount on this dentry.
970 * However, we can only permit this if it's a terminal point in
971 * the path being looked up; if it wasn't then the remainder of
972 * the path is inaccessible and we should say so.
974 if (PTR_ERR(mnt
) == -EISDIR
&& (flags
& LOOKUP_PARENT
))
979 if (!mnt
) /* mount collision */
983 /* lock_mount() may release path->mnt on error */
987 err
= finish_automount(mnt
, path
);
991 /* Someone else made a mount here whilst we were busy */
996 path
->dentry
= dget(mnt
->mnt_root
);
1005 * Handle a dentry that is managed in some way.
1006 * - Flagged for transit management (autofs)
1007 * - Flagged as mountpoint
1008 * - Flagged as automount point
1010 * This may only be called in refwalk mode.
1012 * Serialization is taken care of in namespace.c
1014 static int follow_managed(struct path
*path
, unsigned flags
)
1016 struct vfsmount
*mnt
= path
->mnt
; /* held by caller, must be left alone */
1018 bool need_mntput
= false;
1021 /* Given that we're not holding a lock here, we retain the value in a
1022 * local variable for each dentry as we look at it so that we don't see
1023 * the components of that value change under us */
1024 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1025 managed
&= DCACHE_MANAGED_DENTRY
,
1026 unlikely(managed
!= 0)) {
1027 /* Allow the filesystem to manage the transit without i_mutex
1029 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1030 BUG_ON(!path
->dentry
->d_op
);
1031 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1032 ret
= path
->dentry
->d_op
->d_manage(path
->dentry
, false);
1037 /* Transit to a mounted filesystem. */
1038 if (managed
& DCACHE_MOUNTED
) {
1039 struct vfsmount
*mounted
= lookup_mnt(path
);
1044 path
->mnt
= mounted
;
1045 path
->dentry
= dget(mounted
->mnt_root
);
1050 /* Something is mounted on this dentry in another
1051 * namespace and/or whatever was mounted there in this
1052 * namespace got unmounted before we managed to get the
1056 /* Handle an automount point */
1057 if (managed
& DCACHE_NEED_AUTOMOUNT
) {
1058 ret
= follow_automount(path
, flags
, &need_mntput
);
1064 /* We didn't change the current path point */
1068 if (need_mntput
&& path
->mnt
== mnt
)
1072 return ret
< 0 ? ret
: need_mntput
;
1075 int follow_down_one(struct path
*path
)
1077 struct vfsmount
*mounted
;
1079 mounted
= lookup_mnt(path
);
1083 path
->mnt
= mounted
;
1084 path
->dentry
= dget(mounted
->mnt_root
);
1090 static inline bool managed_dentry_might_block(struct dentry
*dentry
)
1092 return (dentry
->d_flags
& DCACHE_MANAGE_TRANSIT
&&
1093 dentry
->d_op
->d_manage(dentry
, true) < 0);
1097 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1098 * we meet a managed dentry that would need blocking.
1100 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
1101 struct inode
**inode
)
1104 struct mount
*mounted
;
1106 * Don't forget we might have a non-mountpoint managed dentry
1107 * that wants to block transit.
1109 if (unlikely(managed_dentry_might_block(path
->dentry
)))
1112 if (!d_mountpoint(path
->dentry
))
1115 mounted
= __lookup_mnt(path
->mnt
, path
->dentry
, 1);
1118 path
->mnt
= &mounted
->mnt
;
1119 path
->dentry
= mounted
->mnt
.mnt_root
;
1120 nd
->flags
|= LOOKUP_JUMPED
;
1121 nd
->seq
= read_seqcount_begin(&path
->dentry
->d_seq
);
1123 * Update the inode too. We don't need to re-check the
1124 * dentry sequence number here after this d_inode read,
1125 * because a mount-point is always pinned.
1127 *inode
= path
->dentry
->d_inode
;
1132 static void follow_mount_rcu(struct nameidata
*nd
)
1134 while (d_mountpoint(nd
->path
.dentry
)) {
1135 struct mount
*mounted
;
1136 mounted
= __lookup_mnt(nd
->path
.mnt
, nd
->path
.dentry
, 1);
1139 nd
->path
.mnt
= &mounted
->mnt
;
1140 nd
->path
.dentry
= mounted
->mnt
.mnt_root
;
1141 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1145 static int follow_dotdot_rcu(struct nameidata
*nd
)
1150 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1151 nd
->path
.mnt
== nd
->root
.mnt
) {
1154 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1155 struct dentry
*old
= nd
->path
.dentry
;
1156 struct dentry
*parent
= old
->d_parent
;
1159 seq
= read_seqcount_begin(&parent
->d_seq
);
1160 if (read_seqcount_retry(&old
->d_seq
, nd
->seq
))
1162 nd
->path
.dentry
= parent
;
1166 if (!follow_up_rcu(&nd
->path
))
1168 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1170 follow_mount_rcu(nd
);
1171 nd
->inode
= nd
->path
.dentry
->d_inode
;
1175 nd
->flags
&= ~LOOKUP_RCU
;
1176 if (!(nd
->flags
& LOOKUP_ROOT
))
1177 nd
->root
.mnt
= NULL
;
1183 * Follow down to the covering mount currently visible to userspace. At each
1184 * point, the filesystem owning that dentry may be queried as to whether the
1185 * caller is permitted to proceed or not.
1187 int follow_down(struct path
*path
)
1192 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1193 unlikely(managed
& DCACHE_MANAGED_DENTRY
)) {
1194 /* Allow the filesystem to manage the transit without i_mutex
1197 * We indicate to the filesystem if someone is trying to mount
1198 * something here. This gives autofs the chance to deny anyone
1199 * other than its daemon the right to mount on its
1202 * The filesystem may sleep at this point.
1204 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1205 BUG_ON(!path
->dentry
->d_op
);
1206 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1207 ret
= path
->dentry
->d_op
->d_manage(
1208 path
->dentry
, false);
1210 return ret
== -EISDIR
? 0 : ret
;
1213 /* Transit to a mounted filesystem. */
1214 if (managed
& DCACHE_MOUNTED
) {
1215 struct vfsmount
*mounted
= lookup_mnt(path
);
1220 path
->mnt
= mounted
;
1221 path
->dentry
= dget(mounted
->mnt_root
);
1225 /* Don't handle automount points here */
1232 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1234 static void follow_mount(struct path
*path
)
1236 while (d_mountpoint(path
->dentry
)) {
1237 struct vfsmount
*mounted
= lookup_mnt(path
);
1242 path
->mnt
= mounted
;
1243 path
->dentry
= dget(mounted
->mnt_root
);
1247 static void follow_dotdot(struct nameidata
*nd
)
1252 struct dentry
*old
= nd
->path
.dentry
;
1254 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1255 nd
->path
.mnt
== nd
->root
.mnt
) {
1258 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1259 /* rare case of legitimate dget_parent()... */
1260 nd
->path
.dentry
= dget_parent(nd
->path
.dentry
);
1264 if (!follow_up(&nd
->path
))
1267 follow_mount(&nd
->path
);
1268 nd
->inode
= nd
->path
.dentry
->d_inode
;
1272 * This looks up the name in dcache, possibly revalidates the old dentry and
1273 * allocates a new one if not found or not valid. In the need_lookup argument
1274 * returns whether i_op->lookup is necessary.
1276 * dir->d_inode->i_mutex must be held
1278 static struct dentry
*lookup_dcache(struct qstr
*name
, struct dentry
*dir
,
1279 unsigned int flags
, bool *need_lookup
)
1281 struct dentry
*dentry
;
1284 *need_lookup
= false;
1285 dentry
= d_lookup(dir
, name
);
1287 if (dentry
->d_flags
& DCACHE_OP_REVALIDATE
) {
1288 error
= d_revalidate(dentry
, flags
);
1289 if (unlikely(error
<= 0)) {
1292 return ERR_PTR(error
);
1293 } else if (!d_invalidate(dentry
)) {
1302 dentry
= d_alloc(dir
, name
);
1303 if (unlikely(!dentry
))
1304 return ERR_PTR(-ENOMEM
);
1306 *need_lookup
= true;
1312 * Call i_op->lookup on the dentry. The dentry must be negative but may be
1313 * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1315 * dir->d_inode->i_mutex must be held
1317 static struct dentry
*lookup_real(struct inode
*dir
, struct dentry
*dentry
,
1322 /* Don't create child dentry for a dead directory. */
1323 if (unlikely(IS_DEADDIR(dir
))) {
1325 return ERR_PTR(-ENOENT
);
1328 old
= dir
->i_op
->lookup(dir
, dentry
, flags
);
1329 if (unlikely(old
)) {
1336 static struct dentry
*__lookup_hash(struct qstr
*name
,
1337 struct dentry
*base
, unsigned int flags
)
1340 struct dentry
*dentry
;
1342 dentry
= lookup_dcache(name
, base
, flags
, &need_lookup
);
1346 return lookup_real(base
->d_inode
, dentry
, flags
);
1350 * It's more convoluted than I'd like it to be, but... it's still fairly
1351 * small and for now I'd prefer to have fast path as straight as possible.
1352 * It _is_ time-critical.
1354 static int lookup_fast(struct nameidata
*nd
,
1355 struct path
*path
, struct inode
**inode
)
1357 struct vfsmount
*mnt
= nd
->path
.mnt
;
1358 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1364 * Rename seqlock is not required here because in the off chance
1365 * of a false negative due to a concurrent rename, we're going to
1366 * do the non-racy lookup, below.
1368 if (nd
->flags
& LOOKUP_RCU
) {
1370 dentry
= __d_lookup_rcu(parent
, &nd
->last
, &seq
);
1375 * This sequence count validates that the inode matches
1376 * the dentry name information from lookup.
1378 *inode
= dentry
->d_inode
;
1379 if (read_seqcount_retry(&dentry
->d_seq
, seq
))
1383 * This sequence count validates that the parent had no
1384 * changes while we did the lookup of the dentry above.
1386 * The memory barrier in read_seqcount_begin of child is
1387 * enough, we can use __read_seqcount_retry here.
1389 if (__read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
1393 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)) {
1394 status
= d_revalidate(dentry
, nd
->flags
);
1395 if (unlikely(status
<= 0)) {
1396 if (status
!= -ECHILD
)
1402 path
->dentry
= dentry
;
1403 if (unlikely(!__follow_mount_rcu(nd
, path
, inode
)))
1405 if (unlikely(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
))
1409 if (unlazy_walk(nd
, dentry
))
1412 dentry
= __d_lookup(parent
, &nd
->last
);
1415 if (unlikely(!dentry
))
1418 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
) && need_reval
)
1419 status
= d_revalidate(dentry
, nd
->flags
);
1420 if (unlikely(status
<= 0)) {
1425 if (!d_invalidate(dentry
)) {
1432 path
->dentry
= dentry
;
1433 err
= follow_managed(path
, nd
->flags
);
1434 if (unlikely(err
< 0)) {
1435 path_put_conditional(path
, nd
);
1439 nd
->flags
|= LOOKUP_JUMPED
;
1440 *inode
= path
->dentry
->d_inode
;
1447 /* Fast lookup failed, do it the slow way */
1448 static int lookup_slow(struct nameidata
*nd
, struct path
*path
)
1450 struct dentry
*dentry
, *parent
;
1453 parent
= nd
->path
.dentry
;
1454 BUG_ON(nd
->inode
!= parent
->d_inode
);
1456 mutex_lock(&parent
->d_inode
->i_mutex
);
1457 dentry
= __lookup_hash(&nd
->last
, parent
, nd
->flags
);
1458 mutex_unlock(&parent
->d_inode
->i_mutex
);
1460 return PTR_ERR(dentry
);
1461 path
->mnt
= nd
->path
.mnt
;
1462 path
->dentry
= dentry
;
1463 err
= follow_managed(path
, nd
->flags
);
1464 if (unlikely(err
< 0)) {
1465 path_put_conditional(path
, nd
);
1469 nd
->flags
|= LOOKUP_JUMPED
;
1473 static inline int may_lookup(struct nameidata
*nd
)
1475 if (nd
->flags
& LOOKUP_RCU
) {
1476 int err
= inode_permission(nd
->inode
, MAY_EXEC
|MAY_NOT_BLOCK
);
1479 if (unlazy_walk(nd
, NULL
))
1482 return inode_permission(nd
->inode
, MAY_EXEC
);
1485 static inline int handle_dots(struct nameidata
*nd
, int type
)
1487 if (type
== LAST_DOTDOT
) {
1488 if (nd
->flags
& LOOKUP_RCU
) {
1489 if (follow_dotdot_rcu(nd
))
1497 static void terminate_walk(struct nameidata
*nd
)
1499 if (!(nd
->flags
& LOOKUP_RCU
)) {
1500 path_put(&nd
->path
);
1502 nd
->flags
&= ~LOOKUP_RCU
;
1503 if (!(nd
->flags
& LOOKUP_ROOT
))
1504 nd
->root
.mnt
= NULL
;
1510 * Do we need to follow links? We _really_ want to be able
1511 * to do this check without having to look at inode->i_op,
1512 * so we keep a cache of "no, this doesn't need follow_link"
1513 * for the common case.
1515 static inline int should_follow_link(struct inode
*inode
, int follow
)
1517 if (unlikely(!(inode
->i_opflags
& IOP_NOFOLLOW
))) {
1518 if (likely(inode
->i_op
->follow_link
))
1521 /* This gets set once for the inode lifetime */
1522 spin_lock(&inode
->i_lock
);
1523 inode
->i_opflags
|= IOP_NOFOLLOW
;
1524 spin_unlock(&inode
->i_lock
);
1529 static inline int walk_component(struct nameidata
*nd
, struct path
*path
,
1532 struct inode
*inode
;
1535 * "." and ".." are special - ".." especially so because it has
1536 * to be able to know about the current root directory and
1537 * parent relationships.
1539 if (unlikely(nd
->last_type
!= LAST_NORM
))
1540 return handle_dots(nd
, nd
->last_type
);
1541 err
= lookup_fast(nd
, path
, &inode
);
1542 if (unlikely(err
)) {
1546 err
= lookup_slow(nd
, path
);
1550 inode
= path
->dentry
->d_inode
;
1556 if (should_follow_link(inode
, follow
)) {
1557 if (nd
->flags
& LOOKUP_RCU
) {
1558 if (unlikely(unlazy_walk(nd
, path
->dentry
))) {
1563 BUG_ON(inode
!= path
->dentry
->d_inode
);
1566 path_to_nameidata(path
, nd
);
1571 path_to_nameidata(path
, nd
);
1578 * This limits recursive symlink follows to 8, while
1579 * limiting consecutive symlinks to 40.
1581 * Without that kind of total limit, nasty chains of consecutive
1582 * symlinks can cause almost arbitrarily long lookups.
1584 static inline int nested_symlink(struct path
*path
, struct nameidata
*nd
)
1588 if (unlikely(current
->link_count
>= MAX_NESTED_LINKS
)) {
1589 path_put_conditional(path
, nd
);
1590 path_put(&nd
->path
);
1593 BUG_ON(nd
->depth
>= MAX_NESTED_LINKS
);
1596 current
->link_count
++;
1599 struct path link
= *path
;
1602 res
= follow_link(&link
, nd
, &cookie
);
1605 res
= walk_component(nd
, path
, LOOKUP_FOLLOW
);
1606 put_link(nd
, &link
, cookie
);
1609 current
->link_count
--;
1615 * We really don't want to look at inode->i_op->lookup
1616 * when we don't have to. So we keep a cache bit in
1617 * the inode ->i_opflags field that says "yes, we can
1618 * do lookup on this inode".
1620 static inline int can_lookup(struct inode
*inode
)
1622 if (likely(inode
->i_opflags
& IOP_LOOKUP
))
1624 if (likely(!inode
->i_op
->lookup
))
1627 /* We do this once for the lifetime of the inode */
1628 spin_lock(&inode
->i_lock
);
1629 inode
->i_opflags
|= IOP_LOOKUP
;
1630 spin_unlock(&inode
->i_lock
);
1635 * We can do the critical dentry name comparison and hashing
1636 * operations one word at a time, but we are limited to:
1638 * - Architectures with fast unaligned word accesses. We could
1639 * do a "get_unaligned()" if this helps and is sufficiently
1642 * - Little-endian machines (so that we can generate the mask
1643 * of low bytes efficiently). Again, we *could* do a byte
1644 * swapping load on big-endian architectures if that is not
1645 * expensive enough to make the optimization worthless.
1647 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1648 * do not trap on the (extremely unlikely) case of a page
1649 * crossing operation.
1651 * - Furthermore, we need an efficient 64-bit compile for the
1652 * 64-bit case in order to generate the "number of bytes in
1653 * the final mask". Again, that could be replaced with a
1654 * efficient population count instruction or similar.
1656 #ifdef CONFIG_DCACHE_WORD_ACCESS
1658 #include <asm/word-at-a-time.h>
1662 static inline unsigned int fold_hash(unsigned long hash
)
1664 hash
+= hash
>> (8*sizeof(int));
1668 #else /* 32-bit case */
1670 #define fold_hash(x) (x)
1674 unsigned int full_name_hash(const unsigned char *name
, unsigned int len
)
1676 unsigned long a
, mask
;
1677 unsigned long hash
= 0;
1680 a
= load_unaligned_zeropad(name
);
1681 if (len
< sizeof(unsigned long))
1685 name
+= sizeof(unsigned long);
1686 len
-= sizeof(unsigned long);
1690 mask
= ~(~0ul << len
*8);
1693 return fold_hash(hash
);
1695 EXPORT_SYMBOL(full_name_hash
);
1698 * Calculate the length and hash of the path component, and
1699 * return the length of the component;
1701 static inline unsigned long hash_name(const char *name
, unsigned int *hashp
)
1703 unsigned long a
, b
, adata
, bdata
, mask
, hash
, len
;
1704 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
1707 len
= -sizeof(unsigned long);
1709 hash
= (hash
+ a
) * 9;
1710 len
+= sizeof(unsigned long);
1711 a
= load_unaligned_zeropad(name
+len
);
1712 b
= a
^ REPEAT_BYTE('/');
1713 } while (!(has_zero(a
, &adata
, &constants
) | has_zero(b
, &bdata
, &constants
)));
1715 adata
= prep_zero_mask(a
, adata
, &constants
);
1716 bdata
= prep_zero_mask(b
, bdata
, &constants
);
1718 mask
= create_zero_mask(adata
| bdata
);
1720 hash
+= a
& zero_bytemask(mask
);
1721 *hashp
= fold_hash(hash
);
1723 return len
+ find_zero(mask
);
1728 unsigned int full_name_hash(const unsigned char *name
, unsigned int len
)
1730 unsigned long hash
= init_name_hash();
1732 hash
= partial_name_hash(*name
++, hash
);
1733 return end_name_hash(hash
);
1735 EXPORT_SYMBOL(full_name_hash
);
1738 * We know there's a real path component here of at least
1741 static inline unsigned long hash_name(const char *name
, unsigned int *hashp
)
1743 unsigned long hash
= init_name_hash();
1744 unsigned long len
= 0, c
;
1746 c
= (unsigned char)*name
;
1749 hash
= partial_name_hash(c
, hash
);
1750 c
= (unsigned char)name
[len
];
1751 } while (c
&& c
!= '/');
1752 *hashp
= end_name_hash(hash
);
1760 * This is the basic name resolution function, turning a pathname into
1761 * the final dentry. We expect 'base' to be positive and a directory.
1763 * Returns 0 and nd will have valid dentry and mnt on success.
1764 * Returns error and drops reference to input namei data on failure.
1766 static int link_path_walk(const char *name
, struct nameidata
*nd
)
1776 /* At this point we know we have a real path component. */
1782 err
= may_lookup(nd
);
1786 len
= hash_name(name
, &this.hash
);
1791 if (name
[0] == '.') switch (len
) {
1793 if (name
[1] == '.') {
1795 nd
->flags
|= LOOKUP_JUMPED
;
1801 if (likely(type
== LAST_NORM
)) {
1802 struct dentry
*parent
= nd
->path
.dentry
;
1803 nd
->flags
&= ~LOOKUP_JUMPED
;
1804 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
1805 err
= parent
->d_op
->d_hash(parent
, &this);
1812 nd
->last_type
= type
;
1817 * If it wasn't NUL, we know it was '/'. Skip that
1818 * slash, and continue until no more slashes.
1822 } while (unlikely(name
[len
] == '/'));
1828 err
= walk_component(nd
, &next
, LOOKUP_FOLLOW
);
1833 err
= nested_symlink(&next
, nd
);
1837 if (!can_lookup(nd
->inode
)) {
1846 static int path_init(int dfd
, const char *name
, unsigned int flags
,
1847 struct nameidata
*nd
, struct file
**fp
)
1851 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
1852 nd
->flags
= flags
| LOOKUP_JUMPED
;
1854 if (flags
& LOOKUP_ROOT
) {
1855 struct inode
*inode
= nd
->root
.dentry
->d_inode
;
1857 if (!can_lookup(inode
))
1859 retval
= inode_permission(inode
, MAY_EXEC
);
1863 nd
->path
= nd
->root
;
1865 if (flags
& LOOKUP_RCU
) {
1867 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1869 path_get(&nd
->path
);
1874 nd
->root
.mnt
= NULL
;
1877 if (flags
& LOOKUP_RCU
) {
1882 path_get(&nd
->root
);
1884 nd
->path
= nd
->root
;
1885 } else if (dfd
== AT_FDCWD
) {
1886 if (flags
& LOOKUP_RCU
) {
1887 struct fs_struct
*fs
= current
->fs
;
1893 seq
= read_seqcount_begin(&fs
->seq
);
1895 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1896 } while (read_seqcount_retry(&fs
->seq
, seq
));
1898 get_fs_pwd(current
->fs
, &nd
->path
);
1901 /* Caller must check execute permissions on the starting path component */
1902 struct fd f
= fdget_raw(dfd
);
1903 struct dentry
*dentry
;
1908 dentry
= f
.file
->f_path
.dentry
;
1911 if (!can_lookup(dentry
->d_inode
)) {
1917 nd
->path
= f
.file
->f_path
;
1918 if (flags
& LOOKUP_RCU
) {
1921 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1924 path_get(&nd
->path
);
1929 nd
->inode
= nd
->path
.dentry
->d_inode
;
1933 static inline int lookup_last(struct nameidata
*nd
, struct path
*path
)
1935 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
1936 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
1938 nd
->flags
&= ~LOOKUP_PARENT
;
1939 return walk_component(nd
, path
, nd
->flags
& LOOKUP_FOLLOW
);
1942 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1943 static int path_lookupat(int dfd
, const char *name
,
1944 unsigned int flags
, struct nameidata
*nd
)
1946 struct file
*base
= NULL
;
1951 * Path walking is largely split up into 2 different synchronisation
1952 * schemes, rcu-walk and ref-walk (explained in
1953 * Documentation/filesystems/path-lookup.txt). These share much of the
1954 * path walk code, but some things particularly setup, cleanup, and
1955 * following mounts are sufficiently divergent that functions are
1956 * duplicated. Typically there is a function foo(), and its RCU
1957 * analogue, foo_rcu().
1959 * -ECHILD is the error number of choice (just to avoid clashes) that
1960 * is returned if some aspect of an rcu-walk fails. Such an error must
1961 * be handled by restarting a traditional ref-walk (which will always
1962 * be able to complete).
1964 err
= path_init(dfd
, name
, flags
| LOOKUP_PARENT
, nd
, &base
);
1969 current
->total_link_count
= 0;
1970 err
= link_path_walk(name
, nd
);
1972 if (!err
&& !(flags
& LOOKUP_PARENT
)) {
1973 err
= lookup_last(nd
, &path
);
1976 struct path link
= path
;
1977 err
= may_follow_link(&link
, nd
);
1980 nd
->flags
|= LOOKUP_PARENT
;
1981 err
= follow_link(&link
, nd
, &cookie
);
1984 err
= lookup_last(nd
, &path
);
1985 put_link(nd
, &link
, cookie
);
1990 err
= complete_walk(nd
);
1992 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
) {
1993 if (!can_lookup(nd
->inode
)) {
1994 path_put(&nd
->path
);
2002 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
2003 path_put(&nd
->root
);
2004 nd
->root
.mnt
= NULL
;
2009 static int filename_lookup(int dfd
, struct filename
*name
,
2010 unsigned int flags
, struct nameidata
*nd
)
2012 int retval
= path_lookupat(dfd
, name
->name
, flags
| LOOKUP_RCU
, nd
);
2013 if (unlikely(retval
== -ECHILD
))
2014 retval
= path_lookupat(dfd
, name
->name
, flags
, nd
);
2015 if (unlikely(retval
== -ESTALE
))
2016 retval
= path_lookupat(dfd
, name
->name
,
2017 flags
| LOOKUP_REVAL
, nd
);
2019 if (likely(!retval
))
2020 audit_inode(name
, nd
->path
.dentry
, flags
& LOOKUP_PARENT
);
2024 static int do_path_lookup(int dfd
, const char *name
,
2025 unsigned int flags
, struct nameidata
*nd
)
2027 struct filename filename
= { .name
= name
};
2029 return filename_lookup(dfd
, &filename
, flags
, nd
);
2032 /* does lookup, returns the object with parent locked */
2033 struct dentry
*kern_path_locked(const char *name
, struct path
*path
)
2035 struct nameidata nd
;
2037 int err
= do_path_lookup(AT_FDCWD
, name
, LOOKUP_PARENT
, &nd
);
2039 return ERR_PTR(err
);
2040 if (nd
.last_type
!= LAST_NORM
) {
2042 return ERR_PTR(-EINVAL
);
2044 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2045 d
= __lookup_hash(&nd
.last
, nd
.path
.dentry
, 0);
2047 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
2055 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
2057 struct nameidata nd
;
2058 int res
= do_path_lookup(AT_FDCWD
, name
, flags
, &nd
);
2065 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2066 * @dentry: pointer to dentry of the base directory
2067 * @mnt: pointer to vfs mount of the base directory
2068 * @name: pointer to file name
2069 * @flags: lookup flags
2070 * @path: pointer to struct path to fill
2072 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
2073 const char *name
, unsigned int flags
,
2076 struct nameidata nd
;
2078 nd
.root
.dentry
= dentry
;
2080 BUG_ON(flags
& LOOKUP_PARENT
);
2081 /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2082 err
= do_path_lookup(AT_FDCWD
, name
, flags
| LOOKUP_ROOT
, &nd
);
2089 * Restricted form of lookup. Doesn't follow links, single-component only,
2090 * needs parent already locked. Doesn't follow mounts.
2093 static struct dentry
*lookup_hash(struct nameidata
*nd
)
2095 return __lookup_hash(&nd
->last
, nd
->path
.dentry
, nd
->flags
);
2099 * lookup_one_len - filesystem helper to lookup single pathname component
2100 * @name: pathname component to lookup
2101 * @base: base directory to lookup from
2102 * @len: maximum length @len should be interpreted to
2104 * Note that this routine is purely a helper for filesystem usage and should
2105 * not be called by generic code. Also note that by using this function the
2106 * nameidata argument is passed to the filesystem methods and a filesystem
2107 * using this helper needs to be prepared for that.
2109 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
2115 WARN_ON_ONCE(!mutex_is_locked(&base
->d_inode
->i_mutex
));
2119 this.hash
= full_name_hash(name
, len
);
2121 return ERR_PTR(-EACCES
);
2123 if (unlikely(name
[0] == '.')) {
2124 if (len
< 2 || (len
== 2 && name
[1] == '.'))
2125 return ERR_PTR(-EACCES
);
2129 c
= *(const unsigned char *)name
++;
2130 if (c
== '/' || c
== '\0')
2131 return ERR_PTR(-EACCES
);
2134 * See if the low-level filesystem might want
2135 * to use its own hash..
2137 if (base
->d_flags
& DCACHE_OP_HASH
) {
2138 int err
= base
->d_op
->d_hash(base
, &this);
2140 return ERR_PTR(err
);
2143 err
= inode_permission(base
->d_inode
, MAY_EXEC
);
2145 return ERR_PTR(err
);
2147 return __lookup_hash(&this, base
, 0);
2150 int user_path_at_empty(int dfd
, const char __user
*name
, unsigned flags
,
2151 struct path
*path
, int *empty
)
2153 struct nameidata nd
;
2154 struct filename
*tmp
= getname_flags(name
, flags
, empty
);
2155 int err
= PTR_ERR(tmp
);
2158 BUG_ON(flags
& LOOKUP_PARENT
);
2160 err
= filename_lookup(dfd
, tmp
, flags
, &nd
);
2168 int user_path_at(int dfd
, const char __user
*name
, unsigned flags
,
2171 return user_path_at_empty(dfd
, name
, flags
, path
, NULL
);
2175 * NB: most callers don't do anything directly with the reference to the
2176 * to struct filename, but the nd->last pointer points into the name string
2177 * allocated by getname. So we must hold the reference to it until all
2178 * path-walking is complete.
2180 static struct filename
*
2181 user_path_parent(int dfd
, const char __user
*path
, struct nameidata
*nd
,
2184 struct filename
*s
= getname(path
);
2187 /* only LOOKUP_REVAL is allowed in extra flags */
2188 flags
&= LOOKUP_REVAL
;
2193 error
= filename_lookup(dfd
, s
, flags
| LOOKUP_PARENT
, nd
);
2196 return ERR_PTR(error
);
2203 * mountpoint_last - look up last component for umount
2204 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2205 * @path: pointer to container for result
2207 * This is a special lookup_last function just for umount. In this case, we
2208 * need to resolve the path without doing any revalidation.
2210 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2211 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2212 * in almost all cases, this lookup will be served out of the dcache. The only
2213 * cases where it won't are if nd->last refers to a symlink or the path is
2214 * bogus and it doesn't exist.
2217 * -error: if there was an error during lookup. This includes -ENOENT if the
2218 * lookup found a negative dentry. The nd->path reference will also be
2221 * 0: if we successfully resolved nd->path and found it to not to be a
2222 * symlink that needs to be followed. "path" will also be populated.
2223 * The nd->path reference will also be put.
2225 * 1: if we successfully resolved nd->last and found it to be a symlink
2226 * that needs to be followed. "path" will be populated with the path
2227 * to the link, and nd->path will *not* be put.
2230 mountpoint_last(struct nameidata
*nd
, struct path
*path
)
2233 struct dentry
*dentry
;
2234 struct dentry
*dir
= nd
->path
.dentry
;
2236 /* If we're in rcuwalk, drop out of it to handle last component */
2237 if (nd
->flags
& LOOKUP_RCU
) {
2238 if (unlazy_walk(nd
, NULL
)) {
2244 nd
->flags
&= ~LOOKUP_PARENT
;
2246 if (unlikely(nd
->last_type
!= LAST_NORM
)) {
2247 error
= handle_dots(nd
, nd
->last_type
);
2250 dentry
= dget(nd
->path
.dentry
);
2254 mutex_lock(&dir
->d_inode
->i_mutex
);
2255 dentry
= d_lookup(dir
, &nd
->last
);
2258 * No cached dentry. Mounted dentries are pinned in the cache,
2259 * so that means that this dentry is probably a symlink or the
2260 * path doesn't actually point to a mounted dentry.
2262 dentry
= d_alloc(dir
, &nd
->last
);
2265 mutex_unlock(&dir
->d_inode
->i_mutex
);
2268 dentry
= lookup_real(dir
->d_inode
, dentry
, nd
->flags
);
2269 error
= PTR_ERR(dentry
);
2270 if (IS_ERR(dentry
)) {
2271 mutex_unlock(&dir
->d_inode
->i_mutex
);
2275 mutex_unlock(&dir
->d_inode
->i_mutex
);
2278 if (!dentry
->d_inode
) {
2283 path
->dentry
= dentry
;
2284 path
->mnt
= nd
->path
.mnt
;
2285 if (should_follow_link(dentry
->d_inode
, nd
->flags
& LOOKUP_FOLLOW
))
2296 * path_mountpoint - look up a path to be umounted
2297 * @dfd: directory file descriptor to start walk from
2298 * @name: full pathname to walk
2299 * @path: pointer to container for result
2300 * @flags: lookup flags
2302 * Look up the given name, but don't attempt to revalidate the last component.
2303 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2306 path_mountpoint(int dfd
, const char *name
, struct path
*path
, unsigned int flags
)
2308 struct file
*base
= NULL
;
2309 struct nameidata nd
;
2312 err
= path_init(dfd
, name
, flags
| LOOKUP_PARENT
, &nd
, &base
);
2316 current
->total_link_count
= 0;
2317 err
= link_path_walk(name
, &nd
);
2321 err
= mountpoint_last(&nd
, path
);
2324 struct path link
= *path
;
2325 err
= may_follow_link(&link
, &nd
);
2328 nd
.flags
|= LOOKUP_PARENT
;
2329 err
= follow_link(&link
, &nd
, &cookie
);
2332 err
= mountpoint_last(&nd
, path
);
2333 put_link(&nd
, &link
, cookie
);
2339 if (nd
.root
.mnt
&& !(nd
.flags
& LOOKUP_ROOT
))
2346 filename_mountpoint(int dfd
, struct filename
*s
, struct path
*path
,
2349 int error
= path_mountpoint(dfd
, s
->name
, path
, flags
| LOOKUP_RCU
);
2350 if (unlikely(error
== -ECHILD
))
2351 error
= path_mountpoint(dfd
, s
->name
, path
, flags
);
2352 if (unlikely(error
== -ESTALE
))
2353 error
= path_mountpoint(dfd
, s
->name
, path
, flags
| LOOKUP_REVAL
);
2355 audit_inode(s
, path
->dentry
, 0);
2360 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2361 * @dfd: directory file descriptor
2362 * @name: pathname from userland
2363 * @flags: lookup flags
2364 * @path: pointer to container to hold result
2366 * A umount is a special case for path walking. We're not actually interested
2367 * in the inode in this situation, and ESTALE errors can be a problem. We
2368 * simply want track down the dentry and vfsmount attached at the mountpoint
2369 * and avoid revalidating the last component.
2371 * Returns 0 and populates "path" on success.
2374 user_path_mountpoint_at(int dfd
, const char __user
*name
, unsigned int flags
,
2377 struct filename
*s
= getname(name
);
2381 error
= filename_mountpoint(dfd
, s
, path
, flags
);
2387 kern_path_mountpoint(int dfd
, const char *name
, struct path
*path
,
2390 struct filename s
= {.name
= name
};
2391 return filename_mountpoint(dfd
, &s
, path
, flags
);
2393 EXPORT_SYMBOL(kern_path_mountpoint
);
2396 * It's inline, so penalty for filesystems that don't use sticky bit is
2399 static inline int check_sticky(struct inode
*dir
, struct inode
*inode
)
2401 kuid_t fsuid
= current_fsuid();
2403 if (!(dir
->i_mode
& S_ISVTX
))
2405 if (uid_eq(inode
->i_uid
, fsuid
))
2407 if (uid_eq(dir
->i_uid
, fsuid
))
2409 return !capable_wrt_inode_uidgid(inode
, CAP_FOWNER
);
2413 * Check whether we can remove a link victim from directory dir, check
2414 * whether the type of victim is right.
2415 * 1. We can't do it if dir is read-only (done in permission())
2416 * 2. We should have write and exec permissions on dir
2417 * 3. We can't remove anything from append-only dir
2418 * 4. We can't do anything with immutable dir (done in permission())
2419 * 5. If the sticky bit on dir is set we should either
2420 * a. be owner of dir, or
2421 * b. be owner of victim, or
2422 * c. have CAP_FOWNER capability
2423 * 6. If the victim is append-only or immutable we can't do antyhing with
2424 * links pointing to it.
2425 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2426 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2427 * 9. We can't remove a root or mountpoint.
2428 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2429 * nfs_async_unlink().
2431 static int may_delete(struct inode
*dir
,struct dentry
*victim
,int isdir
)
2435 if (!victim
->d_inode
)
2438 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
2439 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
2441 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
2446 if (check_sticky(dir
, victim
->d_inode
)||IS_APPEND(victim
->d_inode
)||
2447 IS_IMMUTABLE(victim
->d_inode
) || IS_SWAPFILE(victim
->d_inode
))
2450 if (!S_ISDIR(victim
->d_inode
->i_mode
))
2452 if (IS_ROOT(victim
))
2454 } else if (S_ISDIR(victim
->d_inode
->i_mode
))
2456 if (IS_DEADDIR(dir
))
2458 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
2463 /* Check whether we can create an object with dentry child in directory
2465 * 1. We can't do it if child already exists (open has special treatment for
2466 * this case, but since we are inlined it's OK)
2467 * 2. We can't do it if dir is read-only (done in permission())
2468 * 3. We should have write and exec permissions on dir
2469 * 4. We can't do it if dir is immutable (done in permission())
2471 static inline int may_create(struct inode
*dir
, struct dentry
*child
)
2473 audit_inode_child(dir
, child
, AUDIT_TYPE_CHILD_CREATE
);
2476 if (IS_DEADDIR(dir
))
2478 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
2482 * p1 and p2 should be directories on the same fs.
2484 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
2489 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2493 mutex_lock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
2495 p
= d_ancestor(p2
, p1
);
2497 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2498 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2502 p
= d_ancestor(p1
, p2
);
2504 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2505 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2509 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2510 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2514 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
2516 mutex_unlock(&p1
->d_inode
->i_mutex
);
2518 mutex_unlock(&p2
->d_inode
->i_mutex
);
2519 mutex_unlock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
2523 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2526 int error
= may_create(dir
, dentry
);
2530 if (!dir
->i_op
->create
)
2531 return -EACCES
; /* shouldn't it be ENOSYS? */
2534 error
= security_inode_create(dir
, dentry
, mode
);
2537 error
= dir
->i_op
->create(dir
, dentry
, mode
, want_excl
);
2539 fsnotify_create(dir
, dentry
);
2543 static int may_open(struct path
*path
, int acc_mode
, int flag
)
2545 struct dentry
*dentry
= path
->dentry
;
2546 struct inode
*inode
= dentry
->d_inode
;
2556 switch (inode
->i_mode
& S_IFMT
) {
2560 if (acc_mode
& MAY_WRITE
)
2565 if (path
->mnt
->mnt_flags
& MNT_NODEV
)
2574 error
= inode_permission(inode
, acc_mode
);
2579 * An append-only file must be opened in append mode for writing.
2581 if (IS_APPEND(inode
)) {
2582 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
2588 /* O_NOATIME can only be set by the owner or superuser */
2589 if (flag
& O_NOATIME
&& !inode_owner_or_capable(inode
))
2595 static int handle_truncate(struct file
*filp
)
2597 struct path
*path
= &filp
->f_path
;
2598 struct inode
*inode
= path
->dentry
->d_inode
;
2599 int error
= get_write_access(inode
);
2603 * Refuse to truncate files with mandatory locks held on them.
2605 error
= locks_verify_locked(inode
);
2607 error
= security_path_truncate(path
);
2609 error
= do_truncate(path
->dentry
, 0,
2610 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
2613 put_write_access(inode
);
2617 static inline int open_to_namei_flags(int flag
)
2619 if ((flag
& O_ACCMODE
) == 3)
2624 static int may_o_create(struct path
*dir
, struct dentry
*dentry
, umode_t mode
)
2626 int error
= security_path_mknod(dir
, dentry
, mode
, 0);
2630 error
= inode_permission(dir
->dentry
->d_inode
, MAY_WRITE
| MAY_EXEC
);
2634 return security_inode_create(dir
->dentry
->d_inode
, dentry
, mode
);
2638 * Attempt to atomically look up, create and open a file from a negative
2641 * Returns 0 if successful. The file will have been created and attached to
2642 * @file by the filesystem calling finish_open().
2644 * Returns 1 if the file was looked up only or didn't need creating. The
2645 * caller will need to perform the open themselves. @path will have been
2646 * updated to point to the new dentry. This may be negative.
2648 * Returns an error code otherwise.
2650 static int atomic_open(struct nameidata
*nd
, struct dentry
*dentry
,
2651 struct path
*path
, struct file
*file
,
2652 const struct open_flags
*op
,
2653 bool got_write
, bool need_lookup
,
2656 struct inode
*dir
= nd
->path
.dentry
->d_inode
;
2657 unsigned open_flag
= open_to_namei_flags(op
->open_flag
);
2661 int create_error
= 0;
2662 struct dentry
*const DENTRY_NOT_SET
= (void *) -1UL;
2665 BUG_ON(dentry
->d_inode
);
2667 /* Don't create child dentry for a dead directory. */
2668 if (unlikely(IS_DEADDIR(dir
))) {
2674 if ((open_flag
& O_CREAT
) && !IS_POSIXACL(dir
))
2675 mode
&= ~current_umask();
2677 excl
= (open_flag
& (O_EXCL
| O_CREAT
)) == (O_EXCL
| O_CREAT
);
2679 open_flag
&= ~O_TRUNC
;
2682 * Checking write permission is tricky, bacuse we don't know if we are
2683 * going to actually need it: O_CREAT opens should work as long as the
2684 * file exists. But checking existence breaks atomicity. The trick is
2685 * to check access and if not granted clear O_CREAT from the flags.
2687 * Another problem is returing the "right" error value (e.g. for an
2688 * O_EXCL open we want to return EEXIST not EROFS).
2690 if (((open_flag
& (O_CREAT
| O_TRUNC
)) ||
2691 (open_flag
& O_ACCMODE
) != O_RDONLY
) && unlikely(!got_write
)) {
2692 if (!(open_flag
& O_CREAT
)) {
2694 * No O_CREATE -> atomicity not a requirement -> fall
2695 * back to lookup + open
2698 } else if (open_flag
& (O_EXCL
| O_TRUNC
)) {
2699 /* Fall back and fail with the right error */
2700 create_error
= -EROFS
;
2703 /* No side effects, safe to clear O_CREAT */
2704 create_error
= -EROFS
;
2705 open_flag
&= ~O_CREAT
;
2709 if (open_flag
& O_CREAT
) {
2710 error
= may_o_create(&nd
->path
, dentry
, mode
);
2712 create_error
= error
;
2713 if (open_flag
& O_EXCL
)
2715 open_flag
&= ~O_CREAT
;
2719 if (nd
->flags
& LOOKUP_DIRECTORY
)
2720 open_flag
|= O_DIRECTORY
;
2722 file
->f_path
.dentry
= DENTRY_NOT_SET
;
2723 file
->f_path
.mnt
= nd
->path
.mnt
;
2724 error
= dir
->i_op
->atomic_open(dir
, dentry
, file
, open_flag
, mode
,
2727 if (create_error
&& error
== -ENOENT
)
2728 error
= create_error
;
2732 if (error
) { /* returned 1, that is */
2733 if (WARN_ON(file
->f_path
.dentry
== DENTRY_NOT_SET
)) {
2737 if (file
->f_path
.dentry
) {
2739 dentry
= file
->f_path
.dentry
;
2741 if (*opened
& FILE_CREATED
)
2742 fsnotify_create(dir
, dentry
);
2743 if (!dentry
->d_inode
) {
2744 WARN_ON(*opened
& FILE_CREATED
);
2746 error
= create_error
;
2750 if (excl
&& !(*opened
& FILE_CREATED
)) {
2759 * We didn't have the inode before the open, so check open permission
2762 acc_mode
= op
->acc_mode
;
2763 if (*opened
& FILE_CREATED
) {
2764 WARN_ON(!(open_flag
& O_CREAT
));
2765 fsnotify_create(dir
, dentry
);
2766 acc_mode
= MAY_OPEN
;
2768 error
= may_open(&file
->f_path
, acc_mode
, open_flag
);
2778 dentry
= lookup_real(dir
, dentry
, nd
->flags
);
2780 return PTR_ERR(dentry
);
2783 int open_flag
= op
->open_flag
;
2785 error
= create_error
;
2786 if ((open_flag
& O_EXCL
)) {
2787 if (!dentry
->d_inode
)
2789 } else if (!dentry
->d_inode
) {
2791 } else if ((open_flag
& O_TRUNC
) &&
2792 S_ISREG(dentry
->d_inode
->i_mode
)) {
2795 /* will fail later, go on to get the right error */
2799 path
->dentry
= dentry
;
2800 path
->mnt
= nd
->path
.mnt
;
2805 * Look up and maybe create and open the last component.
2807 * Must be called with i_mutex held on parent.
2809 * Returns 0 if the file was successfully atomically created (if necessary) and
2810 * opened. In this case the file will be returned attached to @file.
2812 * Returns 1 if the file was not completely opened at this time, though lookups
2813 * and creations will have been performed and the dentry returned in @path will
2814 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2815 * specified then a negative dentry may be returned.
2817 * An error code is returned otherwise.
2819 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2820 * cleared otherwise prior to returning.
2822 static int lookup_open(struct nameidata
*nd
, struct path
*path
,
2824 const struct open_flags
*op
,
2825 bool got_write
, int *opened
)
2827 struct dentry
*dir
= nd
->path
.dentry
;
2828 struct inode
*dir_inode
= dir
->d_inode
;
2829 struct dentry
*dentry
;
2833 *opened
&= ~FILE_CREATED
;
2834 dentry
= lookup_dcache(&nd
->last
, dir
, nd
->flags
, &need_lookup
);
2836 return PTR_ERR(dentry
);
2838 /* Cached positive dentry: will open in f_op->open */
2839 if (!need_lookup
&& dentry
->d_inode
)
2842 if ((nd
->flags
& LOOKUP_OPEN
) && dir_inode
->i_op
->atomic_open
) {
2843 return atomic_open(nd
, dentry
, path
, file
, op
, got_write
,
2844 need_lookup
, opened
);
2848 BUG_ON(dentry
->d_inode
);
2850 dentry
= lookup_real(dir_inode
, dentry
, nd
->flags
);
2852 return PTR_ERR(dentry
);
2855 /* Negative dentry, just create the file */
2856 if (!dentry
->d_inode
&& (op
->open_flag
& O_CREAT
)) {
2857 umode_t mode
= op
->mode
;
2858 if (!IS_POSIXACL(dir
->d_inode
))
2859 mode
&= ~current_umask();
2861 * This write is needed to ensure that a
2862 * rw->ro transition does not occur between
2863 * the time when the file is created and when
2864 * a permanent write count is taken through
2865 * the 'struct file' in finish_open().
2871 *opened
|= FILE_CREATED
;
2872 error
= security_path_mknod(&nd
->path
, dentry
, mode
, 0);
2875 error
= vfs_create(dir
->d_inode
, dentry
, mode
,
2876 nd
->flags
& LOOKUP_EXCL
);
2881 path
->dentry
= dentry
;
2882 path
->mnt
= nd
->path
.mnt
;
2891 * Handle the last step of open()
2893 static int do_last(struct nameidata
*nd
, struct path
*path
,
2894 struct file
*file
, const struct open_flags
*op
,
2895 int *opened
, struct filename
*name
)
2897 struct dentry
*dir
= nd
->path
.dentry
;
2898 int open_flag
= op
->open_flag
;
2899 bool will_truncate
= (open_flag
& O_TRUNC
) != 0;
2900 bool got_write
= false;
2901 int acc_mode
= op
->acc_mode
;
2902 struct inode
*inode
;
2903 bool symlink_ok
= false;
2904 struct path save_parent
= { .dentry
= NULL
, .mnt
= NULL
};
2905 bool retried
= false;
2908 nd
->flags
&= ~LOOKUP_PARENT
;
2909 nd
->flags
|= op
->intent
;
2911 if (nd
->last_type
!= LAST_NORM
) {
2912 error
= handle_dots(nd
, nd
->last_type
);
2918 if (!(open_flag
& O_CREAT
)) {
2919 if (nd
->last
.name
[nd
->last
.len
])
2920 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2921 if (open_flag
& O_PATH
&& !(nd
->flags
& LOOKUP_FOLLOW
))
2923 /* we _can_ be in RCU mode here */
2924 error
= lookup_fast(nd
, path
, &inode
);
2931 BUG_ON(nd
->inode
!= dir
->d_inode
);
2933 /* create side of things */
2935 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2936 * has been cleared when we got to the last component we are
2939 error
= complete_walk(nd
);
2943 audit_inode(name
, dir
, LOOKUP_PARENT
);
2945 /* trailing slashes? */
2946 if (nd
->last
.name
[nd
->last
.len
])
2951 if (op
->open_flag
& (O_CREAT
| O_TRUNC
| O_WRONLY
| O_RDWR
)) {
2952 error
= mnt_want_write(nd
->path
.mnt
);
2956 * do _not_ fail yet - we might not need that or fail with
2957 * a different error; let lookup_open() decide; we'll be
2958 * dropping this one anyway.
2961 mutex_lock(&dir
->d_inode
->i_mutex
);
2962 error
= lookup_open(nd
, path
, file
, op
, got_write
, opened
);
2963 mutex_unlock(&dir
->d_inode
->i_mutex
);
2969 if ((*opened
& FILE_CREATED
) ||
2970 !S_ISREG(file_inode(file
)->i_mode
))
2971 will_truncate
= false;
2973 audit_inode(name
, file
->f_path
.dentry
, 0);
2977 if (*opened
& FILE_CREATED
) {
2978 /* Don't check for write permission, don't truncate */
2979 open_flag
&= ~O_TRUNC
;
2980 will_truncate
= false;
2981 acc_mode
= MAY_OPEN
;
2982 path_to_nameidata(path
, nd
);
2983 goto finish_open_created
;
2987 * create/update audit record if it already exists.
2989 if (path
->dentry
->d_inode
)
2990 audit_inode(name
, path
->dentry
, 0);
2993 * If atomic_open() acquired write access it is dropped now due to
2994 * possible mount and symlink following (this might be optimized away if
2998 mnt_drop_write(nd
->path
.mnt
);
3003 if ((open_flag
& (O_EXCL
| O_CREAT
)) == (O_EXCL
| O_CREAT
))
3006 error
= follow_managed(path
, nd
->flags
);
3011 nd
->flags
|= LOOKUP_JUMPED
;
3013 BUG_ON(nd
->flags
& LOOKUP_RCU
);
3014 inode
= path
->dentry
->d_inode
;
3016 /* we _can_ be in RCU mode here */
3019 path_to_nameidata(path
, nd
);
3023 if (should_follow_link(inode
, !symlink_ok
)) {
3024 if (nd
->flags
& LOOKUP_RCU
) {
3025 if (unlikely(unlazy_walk(nd
, path
->dentry
))) {
3030 BUG_ON(inode
!= path
->dentry
->d_inode
);
3034 if ((nd
->flags
& LOOKUP_RCU
) || nd
->path
.mnt
!= path
->mnt
) {
3035 path_to_nameidata(path
, nd
);
3037 save_parent
.dentry
= nd
->path
.dentry
;
3038 save_parent
.mnt
= mntget(path
->mnt
);
3039 nd
->path
.dentry
= path
->dentry
;
3043 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3045 error
= complete_walk(nd
);
3047 path_put(&save_parent
);
3050 audit_inode(name
, nd
->path
.dentry
, 0);
3052 if ((open_flag
& O_CREAT
) && S_ISDIR(nd
->inode
->i_mode
))
3055 if ((nd
->flags
& LOOKUP_DIRECTORY
) && !can_lookup(nd
->inode
))
3057 if (!S_ISREG(nd
->inode
->i_mode
))
3058 will_truncate
= false;
3060 if (will_truncate
) {
3061 error
= mnt_want_write(nd
->path
.mnt
);
3066 finish_open_created
:
3067 error
= may_open(&nd
->path
, acc_mode
, open_flag
);
3070 file
->f_path
.mnt
= nd
->path
.mnt
;
3071 error
= finish_open(file
, nd
->path
.dentry
, NULL
, opened
);
3073 if (error
== -EOPENSTALE
)
3078 error
= open_check_o_direct(file
);
3081 error
= ima_file_check(file
, op
->acc_mode
);
3085 if (will_truncate
) {
3086 error
= handle_truncate(file
);
3092 mnt_drop_write(nd
->path
.mnt
);
3093 path_put(&save_parent
);
3098 path_put_conditional(path
, nd
);
3105 /* If no saved parent or already retried then can't retry */
3106 if (!save_parent
.dentry
|| retried
)
3109 BUG_ON(save_parent
.dentry
!= dir
);
3110 path_put(&nd
->path
);
3111 nd
->path
= save_parent
;
3112 nd
->inode
= dir
->d_inode
;
3113 save_parent
.mnt
= NULL
;
3114 save_parent
.dentry
= NULL
;
3116 mnt_drop_write(nd
->path
.mnt
);
3123 static int do_tmpfile(int dfd
, struct filename
*pathname
,
3124 struct nameidata
*nd
, int flags
,
3125 const struct open_flags
*op
,
3126 struct file
*file
, int *opened
)
3128 static const struct qstr name
= QSTR_INIT("/", 1);
3129 struct dentry
*dentry
, *child
;
3131 int error
= path_lookupat(dfd
, pathname
->name
,
3132 flags
| LOOKUP_DIRECTORY
, nd
);
3133 if (unlikely(error
))
3135 error
= mnt_want_write(nd
->path
.mnt
);
3136 if (unlikely(error
))
3138 /* we want directory to be writable */
3139 error
= inode_permission(nd
->inode
, MAY_WRITE
| MAY_EXEC
);
3142 dentry
= nd
->path
.dentry
;
3143 dir
= dentry
->d_inode
;
3144 if (!dir
->i_op
->tmpfile
) {
3145 error
= -EOPNOTSUPP
;
3148 child
= d_alloc(dentry
, &name
);
3149 if (unlikely(!child
)) {
3153 nd
->flags
&= ~LOOKUP_DIRECTORY
;
3154 nd
->flags
|= op
->intent
;
3155 dput(nd
->path
.dentry
);
3156 nd
->path
.dentry
= child
;
3157 error
= dir
->i_op
->tmpfile(dir
, nd
->path
.dentry
, op
->mode
);
3160 audit_inode(pathname
, nd
->path
.dentry
, 0);
3161 error
= may_open(&nd
->path
, op
->acc_mode
, op
->open_flag
);
3164 file
->f_path
.mnt
= nd
->path
.mnt
;
3165 error
= finish_open(file
, nd
->path
.dentry
, NULL
, opened
);
3168 error
= open_check_o_direct(file
);
3171 } else if (!(op
->open_flag
& O_EXCL
)) {
3172 struct inode
*inode
= file_inode(file
);
3173 spin_lock(&inode
->i_lock
);
3174 inode
->i_state
|= I_LINKABLE
;
3175 spin_unlock(&inode
->i_lock
);
3178 mnt_drop_write(nd
->path
.mnt
);
3180 path_put(&nd
->path
);
3184 static struct file
*path_openat(int dfd
, struct filename
*pathname
,
3185 struct nameidata
*nd
, const struct open_flags
*op
, int flags
)
3187 struct file
*base
= NULL
;
3193 file
= get_empty_filp();
3197 file
->f_flags
= op
->open_flag
;
3199 if (unlikely(file
->f_flags
& __O_TMPFILE
)) {
3200 error
= do_tmpfile(dfd
, pathname
, nd
, flags
, op
, file
, &opened
);
3204 error
= path_init(dfd
, pathname
->name
, flags
| LOOKUP_PARENT
, nd
, &base
);
3205 if (unlikely(error
))
3208 current
->total_link_count
= 0;
3209 error
= link_path_walk(pathname
->name
, nd
);
3210 if (unlikely(error
))
3213 error
= do_last(nd
, &path
, file
, op
, &opened
, pathname
);
3214 while (unlikely(error
> 0)) { /* trailing symlink */
3215 struct path link
= path
;
3217 if (!(nd
->flags
& LOOKUP_FOLLOW
)) {
3218 path_put_conditional(&path
, nd
);
3219 path_put(&nd
->path
);
3223 error
= may_follow_link(&link
, nd
);
3224 if (unlikely(error
))
3226 nd
->flags
|= LOOKUP_PARENT
;
3227 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
3228 error
= follow_link(&link
, nd
, &cookie
);
3229 if (unlikely(error
))
3231 error
= do_last(nd
, &path
, file
, op
, &opened
, pathname
);
3232 put_link(nd
, &link
, cookie
);
3235 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
))
3236 path_put(&nd
->root
);
3239 if (!(opened
& FILE_OPENED
)) {
3243 if (unlikely(error
)) {
3244 if (error
== -EOPENSTALE
) {
3245 if (flags
& LOOKUP_RCU
)
3250 file
= ERR_PTR(error
);
3255 struct file
*do_filp_open(int dfd
, struct filename
*pathname
,
3256 const struct open_flags
*op
)
3258 struct nameidata nd
;
3259 int flags
= op
->lookup_flags
;
3262 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_RCU
);
3263 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
3264 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
);
3265 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
3266 filp
= path_openat(dfd
, pathname
, &nd
, op
, flags
| LOOKUP_REVAL
);
3270 struct file
*do_file_open_root(struct dentry
*dentry
, struct vfsmount
*mnt
,
3271 const char *name
, const struct open_flags
*op
)
3273 struct nameidata nd
;
3275 struct filename filename
= { .name
= name
};
3276 int flags
= op
->lookup_flags
| LOOKUP_ROOT
;
3279 nd
.root
.dentry
= dentry
;
3281 if (dentry
->d_inode
->i_op
->follow_link
&& op
->intent
& LOOKUP_OPEN
)
3282 return ERR_PTR(-ELOOP
);
3284 file
= path_openat(-1, &filename
, &nd
, op
, flags
| LOOKUP_RCU
);
3285 if (unlikely(file
== ERR_PTR(-ECHILD
)))
3286 file
= path_openat(-1, &filename
, &nd
, op
, flags
);
3287 if (unlikely(file
== ERR_PTR(-ESTALE
)))
3288 file
= path_openat(-1, &filename
, &nd
, op
, flags
| LOOKUP_REVAL
);
3292 struct dentry
*kern_path_create(int dfd
, const char *pathname
,
3293 struct path
*path
, unsigned int lookup_flags
)
3295 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
3296 struct nameidata nd
;
3299 bool is_dir
= (lookup_flags
& LOOKUP_DIRECTORY
);
3302 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3303 * other flags passed in are ignored!
3305 lookup_flags
&= LOOKUP_REVAL
;
3307 error
= do_path_lookup(dfd
, pathname
, LOOKUP_PARENT
|lookup_flags
, &nd
);
3309 return ERR_PTR(error
);
3312 * Yucky last component or no last component at all?
3313 * (foo/., foo/.., /////)
3315 if (nd
.last_type
!= LAST_NORM
)
3317 nd
.flags
&= ~LOOKUP_PARENT
;
3318 nd
.flags
|= LOOKUP_CREATE
| LOOKUP_EXCL
;
3320 /* don't fail immediately if it's r/o, at least try to report other errors */
3321 err2
= mnt_want_write(nd
.path
.mnt
);
3323 * Do the final lookup.
3325 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3326 dentry
= lookup_hash(&nd
);
3331 if (dentry
->d_inode
)
3334 * Special case - lookup gave negative, but... we had foo/bar/
3335 * From the vfs_mknod() POV we just have a negative dentry -
3336 * all is fine. Let's be bastards - you had / on the end, you've
3337 * been asking for (non-existent) directory. -ENOENT for you.
3339 if (unlikely(!is_dir
&& nd
.last
.name
[nd
.last
.len
])) {
3343 if (unlikely(err2
)) {
3351 dentry
= ERR_PTR(error
);
3353 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
3355 mnt_drop_write(nd
.path
.mnt
);
3360 EXPORT_SYMBOL(kern_path_create
);
3362 void done_path_create(struct path
*path
, struct dentry
*dentry
)
3365 mutex_unlock(&path
->dentry
->d_inode
->i_mutex
);
3366 mnt_drop_write(path
->mnt
);
3369 EXPORT_SYMBOL(done_path_create
);
3371 struct dentry
*user_path_create(int dfd
, const char __user
*pathname
,
3372 struct path
*path
, unsigned int lookup_flags
)
3374 struct filename
*tmp
= getname(pathname
);
3377 return ERR_CAST(tmp
);
3378 res
= kern_path_create(dfd
, tmp
->name
, path
, lookup_flags
);
3382 EXPORT_SYMBOL(user_path_create
);
3384 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
3386 int error
= may_create(dir
, dentry
);
3391 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
3394 if (!dir
->i_op
->mknod
)
3397 error
= devcgroup_inode_mknod(mode
, dev
);
3401 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
3405 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
3407 fsnotify_create(dir
, dentry
);
3411 static int may_mknod(umode_t mode
)
3413 switch (mode
& S_IFMT
) {
3419 case 0: /* zero mode translates to S_IFREG */
3428 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, umode_t
, mode
,
3431 struct dentry
*dentry
;
3434 unsigned int lookup_flags
= 0;
3436 error
= may_mknod(mode
);
3440 dentry
= user_path_create(dfd
, filename
, &path
, lookup_flags
);
3442 return PTR_ERR(dentry
);
3444 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3445 mode
&= ~current_umask();
3446 error
= security_path_mknod(&path
, dentry
, mode
, dev
);
3449 switch (mode
& S_IFMT
) {
3450 case 0: case S_IFREG
:
3451 error
= vfs_create(path
.dentry
->d_inode
,dentry
,mode
,true);
3453 case S_IFCHR
: case S_IFBLK
:
3454 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,
3455 new_decode_dev(dev
));
3457 case S_IFIFO
: case S_IFSOCK
:
3458 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,0);
3462 done_path_create(&path
, dentry
);
3463 if (retry_estale(error
, lookup_flags
)) {
3464 lookup_flags
|= LOOKUP_REVAL
;
3470 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, umode_t
, mode
, unsigned, dev
)
3472 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
3475 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
3477 int error
= may_create(dir
, dentry
);
3478 unsigned max_links
= dir
->i_sb
->s_max_links
;
3483 if (!dir
->i_op
->mkdir
)
3486 mode
&= (S_IRWXUGO
|S_ISVTX
);
3487 error
= security_inode_mkdir(dir
, dentry
, mode
);
3491 if (max_links
&& dir
->i_nlink
>= max_links
)
3494 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
3496 fsnotify_mkdir(dir
, dentry
);
3500 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, umode_t
, mode
)
3502 struct dentry
*dentry
;
3505 unsigned int lookup_flags
= LOOKUP_DIRECTORY
;
3508 dentry
= user_path_create(dfd
, pathname
, &path
, lookup_flags
);
3510 return PTR_ERR(dentry
);
3512 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3513 mode
&= ~current_umask();
3514 error
= security_path_mkdir(&path
, dentry
, mode
);
3516 error
= vfs_mkdir(path
.dentry
->d_inode
, dentry
, mode
);
3517 done_path_create(&path
, dentry
);
3518 if (retry_estale(error
, lookup_flags
)) {
3519 lookup_flags
|= LOOKUP_REVAL
;
3525 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, umode_t
, mode
)
3527 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
3531 * The dentry_unhash() helper will try to drop the dentry early: we
3532 * should have a usage count of 1 if we're the only user of this
3533 * dentry, and if that is true (possibly after pruning the dcache),
3534 * then we drop the dentry now.
3536 * A low-level filesystem can, if it choses, legally
3539 * if (!d_unhashed(dentry))
3542 * if it cannot handle the case of removing a directory
3543 * that is still in use by something else..
3545 void dentry_unhash(struct dentry
*dentry
)
3547 shrink_dcache_parent(dentry
);
3548 spin_lock(&dentry
->d_lock
);
3549 if (dentry
->d_lockref
.count
== 1)
3551 spin_unlock(&dentry
->d_lock
);
3554 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3556 int error
= may_delete(dir
, dentry
, 1);
3561 if (!dir
->i_op
->rmdir
)
3565 mutex_lock(&dentry
->d_inode
->i_mutex
);
3568 if (d_mountpoint(dentry
))
3571 error
= security_inode_rmdir(dir
, dentry
);
3575 shrink_dcache_parent(dentry
);
3576 error
= dir
->i_op
->rmdir(dir
, dentry
);
3580 dentry
->d_inode
->i_flags
|= S_DEAD
;
3584 mutex_unlock(&dentry
->d_inode
->i_mutex
);
3591 static long do_rmdir(int dfd
, const char __user
*pathname
)
3594 struct filename
*name
;
3595 struct dentry
*dentry
;
3596 struct nameidata nd
;
3597 unsigned int lookup_flags
= 0;
3599 name
= user_path_parent(dfd
, pathname
, &nd
, lookup_flags
);
3601 return PTR_ERR(name
);
3603 switch(nd
.last_type
) {
3615 nd
.flags
&= ~LOOKUP_PARENT
;
3616 error
= mnt_want_write(nd
.path
.mnt
);
3620 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3621 dentry
= lookup_hash(&nd
);
3622 error
= PTR_ERR(dentry
);
3625 if (!dentry
->d_inode
) {
3629 error
= security_path_rmdir(&nd
.path
, dentry
);
3632 error
= vfs_rmdir(nd
.path
.dentry
->d_inode
, dentry
);
3636 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
3637 mnt_drop_write(nd
.path
.mnt
);
3641 if (retry_estale(error
, lookup_flags
)) {
3642 lookup_flags
|= LOOKUP_REVAL
;
3648 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
3650 return do_rmdir(AT_FDCWD
, pathname
);
3653 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3655 int error
= may_delete(dir
, dentry
, 0);
3660 if (!dir
->i_op
->unlink
)
3663 mutex_lock(&dentry
->d_inode
->i_mutex
);
3664 if (d_mountpoint(dentry
))
3667 error
= security_inode_unlink(dir
, dentry
);
3669 error
= dir
->i_op
->unlink(dir
, dentry
);
3674 mutex_unlock(&dentry
->d_inode
->i_mutex
);
3676 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3677 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
3678 fsnotify_link_count(dentry
->d_inode
);
3686 * Make sure that the actual truncation of the file will occur outside its
3687 * directory's i_mutex. Truncate can take a long time if there is a lot of
3688 * writeout happening, and we don't want to prevent access to the directory
3689 * while waiting on the I/O.
3691 static long do_unlinkat(int dfd
, const char __user
*pathname
)
3694 struct filename
*name
;
3695 struct dentry
*dentry
;
3696 struct nameidata nd
;
3697 struct inode
*inode
= NULL
;
3698 unsigned int lookup_flags
= 0;
3700 name
= user_path_parent(dfd
, pathname
, &nd
, lookup_flags
);
3702 return PTR_ERR(name
);
3705 if (nd
.last_type
!= LAST_NORM
)
3708 nd
.flags
&= ~LOOKUP_PARENT
;
3709 error
= mnt_want_write(nd
.path
.mnt
);
3713 mutex_lock_nested(&nd
.path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3714 dentry
= lookup_hash(&nd
);
3715 error
= PTR_ERR(dentry
);
3716 if (!IS_ERR(dentry
)) {
3717 /* Why not before? Because we want correct error value */
3718 if (nd
.last
.name
[nd
.last
.len
])
3720 inode
= dentry
->d_inode
;
3724 error
= security_path_unlink(&nd
.path
, dentry
);
3727 error
= vfs_unlink(nd
.path
.dentry
->d_inode
, dentry
);
3731 mutex_unlock(&nd
.path
.dentry
->d_inode
->i_mutex
);
3733 iput(inode
); /* truncate the inode here */
3734 mnt_drop_write(nd
.path
.mnt
);
3738 if (retry_estale(error
, lookup_flags
)) {
3739 lookup_flags
|= LOOKUP_REVAL
;
3746 error
= !dentry
->d_inode
? -ENOENT
:
3747 S_ISDIR(dentry
->d_inode
->i_mode
) ? -EISDIR
: -ENOTDIR
;
3751 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
3753 if ((flag
& ~AT_REMOVEDIR
) != 0)
3756 if (flag
& AT_REMOVEDIR
)
3757 return do_rmdir(dfd
, pathname
);
3759 return do_unlinkat(dfd
, pathname
);
3762 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
3764 return do_unlinkat(AT_FDCWD
, pathname
);
3767 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
3769 int error
= may_create(dir
, dentry
);
3774 if (!dir
->i_op
->symlink
)
3777 error
= security_inode_symlink(dir
, dentry
, oldname
);
3781 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
3783 fsnotify_create(dir
, dentry
);
3787 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
3788 int, newdfd
, const char __user
*, newname
)
3791 struct filename
*from
;
3792 struct dentry
*dentry
;
3794 unsigned int lookup_flags
= 0;
3796 from
= getname(oldname
);
3798 return PTR_ERR(from
);
3800 dentry
= user_path_create(newdfd
, newname
, &path
, lookup_flags
);
3801 error
= PTR_ERR(dentry
);
3805 error
= security_path_symlink(&path
, dentry
, from
->name
);
3807 error
= vfs_symlink(path
.dentry
->d_inode
, dentry
, from
->name
);
3808 done_path_create(&path
, dentry
);
3809 if (retry_estale(error
, lookup_flags
)) {
3810 lookup_flags
|= LOOKUP_REVAL
;
3818 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
3820 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
3823 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
3825 struct inode
*inode
= old_dentry
->d_inode
;
3826 unsigned max_links
= dir
->i_sb
->s_max_links
;
3832 error
= may_create(dir
, new_dentry
);
3836 if (dir
->i_sb
!= inode
->i_sb
)
3840 * A link to an append-only or immutable file cannot be created.
3842 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
3844 if (!dir
->i_op
->link
)
3846 if (S_ISDIR(inode
->i_mode
))
3849 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
3853 mutex_lock(&inode
->i_mutex
);
3854 /* Make sure we don't allow creating hardlink to an unlinked file */
3855 if (inode
->i_nlink
== 0 && !(inode
->i_state
& I_LINKABLE
))
3857 else if (max_links
&& inode
->i_nlink
>= max_links
)
3860 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
3862 if (!error
&& (inode
->i_state
& I_LINKABLE
)) {
3863 spin_lock(&inode
->i_lock
);
3864 inode
->i_state
&= ~I_LINKABLE
;
3865 spin_unlock(&inode
->i_lock
);
3867 mutex_unlock(&inode
->i_mutex
);
3869 fsnotify_link(dir
, inode
, new_dentry
);
3874 * Hardlinks are often used in delicate situations. We avoid
3875 * security-related surprises by not following symlinks on the
3878 * We don't follow them on the oldname either to be compatible
3879 * with linux 2.0, and to avoid hard-linking to directories
3880 * and other special files. --ADM
3882 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
3883 int, newdfd
, const char __user
*, newname
, int, flags
)
3885 struct dentry
*new_dentry
;
3886 struct path old_path
, new_path
;
3890 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
3893 * To use null names we require CAP_DAC_READ_SEARCH
3894 * This ensures that not everyone will be able to create
3895 * handlink using the passed filedescriptor.
3897 if (flags
& AT_EMPTY_PATH
) {
3898 if (!capable(CAP_DAC_READ_SEARCH
))
3903 if (flags
& AT_SYMLINK_FOLLOW
)
3904 how
|= LOOKUP_FOLLOW
;
3906 error
= user_path_at(olddfd
, oldname
, how
, &old_path
);
3910 new_dentry
= user_path_create(newdfd
, newname
, &new_path
,
3911 (how
& LOOKUP_REVAL
));
3912 error
= PTR_ERR(new_dentry
);
3913 if (IS_ERR(new_dentry
))
3917 if (old_path
.mnt
!= new_path
.mnt
)
3919 error
= may_linkat(&old_path
);
3920 if (unlikely(error
))
3922 error
= security_path_link(old_path
.dentry
, &new_path
, new_dentry
);
3925 error
= vfs_link(old_path
.dentry
, new_path
.dentry
->d_inode
, new_dentry
);
3927 done_path_create(&new_path
, new_dentry
);
3928 if (retry_estale(error
, how
)) {
3929 path_put(&old_path
);
3930 how
|= LOOKUP_REVAL
;
3934 path_put(&old_path
);
3939 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
3941 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
3945 * The worst of all namespace operations - renaming directory. "Perverted"
3946 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
3948 * a) we can get into loop creation. Check is done in is_subdir().
3949 * b) race potential - two innocent renames can create a loop together.
3950 * That's where 4.4 screws up. Current fix: serialization on
3951 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
3953 * c) we have to lock _three_ objects - parents and victim (if it exists).
3954 * And that - after we got ->i_mutex on parents (until then we don't know
3955 * whether the target exists). Solution: try to be smart with locking
3956 * order for inodes. We rely on the fact that tree topology may change
3957 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
3958 * move will be locked. Thus we can rank directories by the tree
3959 * (ancestors first) and rank all non-directories after them.
3960 * That works since everybody except rename does "lock parent, lookup,
3961 * lock child" and rename is under ->s_vfs_rename_mutex.
3962 * HOWEVER, it relies on the assumption that any object with ->lookup()
3963 * has no more than 1 dentry. If "hybrid" objects will ever appear,
3964 * we'd better make sure that there's no link(2) for them.
3965 * d) conversion from fhandle to dentry may come in the wrong moment - when
3966 * we are removing the target. Solution: we will have to grab ->i_mutex
3967 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3968 * ->i_mutex on parents, which works but leads to some truly excessive
3971 static int vfs_rename_dir(struct inode
*old_dir
, struct dentry
*old_dentry
,
3972 struct inode
*new_dir
, struct dentry
*new_dentry
)
3975 struct inode
*target
= new_dentry
->d_inode
;
3976 unsigned max_links
= new_dir
->i_sb
->s_max_links
;
3979 * If we are going to change the parent - check write permissions,
3980 * we'll need to flip '..'.
3982 if (new_dir
!= old_dir
) {
3983 error
= inode_permission(old_dentry
->d_inode
, MAY_WRITE
);
3988 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
3994 mutex_lock(&target
->i_mutex
);
3997 if (d_mountpoint(old_dentry
) || d_mountpoint(new_dentry
))
4001 if (max_links
&& !target
&& new_dir
!= old_dir
&&
4002 new_dir
->i_nlink
>= max_links
)
4006 shrink_dcache_parent(new_dentry
);
4007 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
4012 target
->i_flags
|= S_DEAD
;
4013 dont_mount(new_dentry
);
4017 mutex_unlock(&target
->i_mutex
);
4020 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
4021 d_move(old_dentry
,new_dentry
);
4025 static int vfs_rename_other(struct inode
*old_dir
, struct dentry
*old_dentry
,
4026 struct inode
*new_dir
, struct dentry
*new_dentry
)
4028 struct inode
*target
= new_dentry
->d_inode
;
4031 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
4037 mutex_lock(&target
->i_mutex
);
4040 if (d_mountpoint(old_dentry
)||d_mountpoint(new_dentry
))
4043 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
4048 dont_mount(new_dentry
);
4049 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
))
4050 d_move(old_dentry
, new_dentry
);
4053 mutex_unlock(&target
->i_mutex
);
4058 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
4059 struct inode
*new_dir
, struct dentry
*new_dentry
)
4062 int is_dir
= S_ISDIR(old_dentry
->d_inode
->i_mode
);
4063 const unsigned char *old_name
;
4065 if (old_dentry
->d_inode
== new_dentry
->d_inode
)
4068 error
= may_delete(old_dir
, old_dentry
, is_dir
);
4072 if (!new_dentry
->d_inode
)
4073 error
= may_create(new_dir
, new_dentry
);
4075 error
= may_delete(new_dir
, new_dentry
, is_dir
);
4079 if (!old_dir
->i_op
->rename
)
4082 old_name
= fsnotify_oldname_init(old_dentry
->d_name
.name
);
4085 error
= vfs_rename_dir(old_dir
,old_dentry
,new_dir
,new_dentry
);
4087 error
= vfs_rename_other(old_dir
,old_dentry
,new_dir
,new_dentry
);
4089 fsnotify_move(old_dir
, new_dir
, old_name
, is_dir
,
4090 new_dentry
->d_inode
, old_dentry
);
4091 fsnotify_oldname_free(old_name
);
4096 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
4097 int, newdfd
, const char __user
*, newname
)
4099 struct dentry
*old_dir
, *new_dir
;
4100 struct dentry
*old_dentry
, *new_dentry
;
4101 struct dentry
*trap
;
4102 struct nameidata oldnd
, newnd
;
4103 struct filename
*from
;
4104 struct filename
*to
;
4105 unsigned int lookup_flags
= 0;
4106 bool should_retry
= false;
4109 from
= user_path_parent(olddfd
, oldname
, &oldnd
, lookup_flags
);
4111 error
= PTR_ERR(from
);
4115 to
= user_path_parent(newdfd
, newname
, &newnd
, lookup_flags
);
4117 error
= PTR_ERR(to
);
4122 if (oldnd
.path
.mnt
!= newnd
.path
.mnt
)
4125 old_dir
= oldnd
.path
.dentry
;
4127 if (oldnd
.last_type
!= LAST_NORM
)
4130 new_dir
= newnd
.path
.dentry
;
4131 if (newnd
.last_type
!= LAST_NORM
)
4134 error
= mnt_want_write(oldnd
.path
.mnt
);
4138 oldnd
.flags
&= ~LOOKUP_PARENT
;
4139 newnd
.flags
&= ~LOOKUP_PARENT
;
4140 newnd
.flags
|= LOOKUP_RENAME_TARGET
;
4142 trap
= lock_rename(new_dir
, old_dir
);
4144 old_dentry
= lookup_hash(&oldnd
);
4145 error
= PTR_ERR(old_dentry
);
4146 if (IS_ERR(old_dentry
))
4148 /* source must exist */
4150 if (!old_dentry
->d_inode
)
4152 /* unless the source is a directory trailing slashes give -ENOTDIR */
4153 if (!S_ISDIR(old_dentry
->d_inode
->i_mode
)) {
4155 if (oldnd
.last
.name
[oldnd
.last
.len
])
4157 if (newnd
.last
.name
[newnd
.last
.len
])
4160 /* source should not be ancestor of target */
4162 if (old_dentry
== trap
)
4164 new_dentry
= lookup_hash(&newnd
);
4165 error
= PTR_ERR(new_dentry
);
4166 if (IS_ERR(new_dentry
))
4168 /* target should not be an ancestor of source */
4170 if (new_dentry
== trap
)
4173 error
= security_path_rename(&oldnd
.path
, old_dentry
,
4174 &newnd
.path
, new_dentry
);
4177 error
= vfs_rename(old_dir
->d_inode
, old_dentry
,
4178 new_dir
->d_inode
, new_dentry
);
4184 unlock_rename(new_dir
, old_dir
);
4185 mnt_drop_write(oldnd
.path
.mnt
);
4187 if (retry_estale(error
, lookup_flags
))
4188 should_retry
= true;
4189 path_put(&newnd
.path
);
4192 path_put(&oldnd
.path
);
4195 should_retry
= false;
4196 lookup_flags
|= LOOKUP_REVAL
;
4203 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
4205 return sys_renameat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
);
4208 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
, const char *link
)
4212 len
= PTR_ERR(link
);
4217 if (len
> (unsigned) buflen
)
4219 if (copy_to_user(buffer
, link
, len
))
4226 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4227 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4228 * using) it for any given inode is up to filesystem.
4230 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4232 struct nameidata nd
;
4237 cookie
= dentry
->d_inode
->i_op
->follow_link(dentry
, &nd
);
4239 return PTR_ERR(cookie
);
4241 res
= vfs_readlink(dentry
, buffer
, buflen
, nd_get_link(&nd
));
4242 if (dentry
->d_inode
->i_op
->put_link
)
4243 dentry
->d_inode
->i_op
->put_link(dentry
, &nd
, cookie
);
4247 /* get the link contents into pagecache */
4248 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
4252 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
4253 page
= read_mapping_page(mapping
, 0, NULL
);
4258 nd_terminate_link(kaddr
, dentry
->d_inode
->i_size
, PAGE_SIZE
- 1);
4262 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4264 struct page
*page
= NULL
;
4265 char *s
= page_getlink(dentry
, &page
);
4266 int res
= vfs_readlink(dentry
,buffer
,buflen
,s
);
4269 page_cache_release(page
);
4274 void *page_follow_link_light(struct dentry
*dentry
, struct nameidata
*nd
)
4276 struct page
*page
= NULL
;
4277 nd_set_link(nd
, page_getlink(dentry
, &page
));
4281 void page_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
4283 struct page
*page
= cookie
;
4287 page_cache_release(page
);
4292 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4294 int __page_symlink(struct inode
*inode
, const char *symname
, int len
, int nofs
)
4296 struct address_space
*mapping
= inode
->i_mapping
;
4301 unsigned int flags
= AOP_FLAG_UNINTERRUPTIBLE
;
4303 flags
|= AOP_FLAG_NOFS
;
4306 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
4307 flags
, &page
, &fsdata
);
4311 kaddr
= kmap_atomic(page
);
4312 memcpy(kaddr
, symname
, len
-1);
4313 kunmap_atomic(kaddr
);
4315 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
4322 mark_inode_dirty(inode
);
4328 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
4330 return __page_symlink(inode
, symname
, len
,
4331 !(mapping_gfp_mask(inode
->i_mapping
) & __GFP_FS
));
4334 const struct inode_operations page_symlink_inode_operations
= {
4335 .readlink
= generic_readlink
,
4336 .follow_link
= page_follow_link_light
,
4337 .put_link
= page_put_link
,
4340 EXPORT_SYMBOL(user_path_at
);
4341 EXPORT_SYMBOL(follow_down_one
);
4342 EXPORT_SYMBOL(follow_down
);
4343 EXPORT_SYMBOL(follow_up
);
4344 EXPORT_SYMBOL(get_write_access
); /* nfsd */
4345 EXPORT_SYMBOL(lock_rename
);
4346 EXPORT_SYMBOL(lookup_one_len
);
4347 EXPORT_SYMBOL(page_follow_link_light
);
4348 EXPORT_SYMBOL(page_put_link
);
4349 EXPORT_SYMBOL(page_readlink
);
4350 EXPORT_SYMBOL(__page_symlink
);
4351 EXPORT_SYMBOL(page_symlink
);
4352 EXPORT_SYMBOL(page_symlink_inode_operations
);
4353 EXPORT_SYMBOL(kern_path
);
4354 EXPORT_SYMBOL(vfs_path_lookup
);
4355 EXPORT_SYMBOL(inode_permission
);
4356 EXPORT_SYMBOL(unlock_rename
);
4357 EXPORT_SYMBOL(vfs_create
);
4358 EXPORT_SYMBOL(vfs_link
);
4359 EXPORT_SYMBOL(vfs_mkdir
);
4360 EXPORT_SYMBOL(vfs_mknod
);
4361 EXPORT_SYMBOL(generic_permission
);
4362 EXPORT_SYMBOL(vfs_readlink
);
4363 EXPORT_SYMBOL(vfs_rename
);
4364 EXPORT_SYMBOL(vfs_rmdir
);
4365 EXPORT_SYMBOL(vfs_symlink
);
4366 EXPORT_SYMBOL(vfs_unlink
);
4367 EXPORT_SYMBOL(dentry_unhash
);
4368 EXPORT_SYMBOL(generic_readlink
);