1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
9 * Some corrections by tytso.
12 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
15 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
18 #include <linux/init.h>
19 #include <linux/export.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
23 #include <linux/namei.h>
24 #include <linux/pagemap.h>
25 #include <linux/sched/mm.h>
26 #include <linux/fsnotify.h>
27 #include <linux/personality.h>
28 #include <linux/security.h>
29 #include <linux/ima.h>
30 #include <linux/syscalls.h>
31 #include <linux/mount.h>
32 #include <linux/audit.h>
33 #include <linux/capability.h>
34 #include <linux/file.h>
35 #include <linux/fcntl.h>
36 #include <linux/device_cgroup.h>
37 #include <linux/fs_struct.h>
38 #include <linux/posix_acl.h>
39 #include <linux/hash.h>
40 #include <linux/bitops.h>
41 #include <linux/init_task.h>
42 #include <linux/uaccess.h>
47 /* [Feb-1997 T. Schoebel-Theuer]
48 * Fundamental changes in the pathname lookup mechanisms (namei)
49 * were necessary because of omirr. The reason is that omirr needs
50 * to know the _real_ pathname, not the user-supplied one, in case
51 * of symlinks (and also when transname replacements occur).
53 * The new code replaces the old recursive symlink resolution with
54 * an iterative one (in case of non-nested symlink chains). It does
55 * this with calls to <fs>_follow_link().
56 * As a side effect, dir_namei(), _namei() and follow_link() are now
57 * replaced with a single function lookup_dentry() that can handle all
58 * the special cases of the former code.
60 * With the new dcache, the pathname is stored at each inode, at least as
61 * long as the refcount of the inode is positive. As a side effect, the
62 * size of the dcache depends on the inode cache and thus is dynamic.
64 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
65 * resolution to correspond with current state of the code.
67 * Note that the symlink resolution is not *completely* iterative.
68 * There is still a significant amount of tail- and mid- recursion in
69 * the algorithm. Also, note that <fs>_readlink() is not used in
70 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
71 * may return different results than <fs>_follow_link(). Many virtual
72 * filesystems (including /proc) exhibit this behavior.
75 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
76 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
77 * and the name already exists in form of a symlink, try to create the new
78 * name indicated by the symlink. The old code always complained that the
79 * name already exists, due to not following the symlink even if its target
80 * is nonexistent. The new semantics affects also mknod() and link() when
81 * the name is a symlink pointing to a non-existent name.
83 * I don't know which semantics is the right one, since I have no access
84 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
85 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
86 * "old" one. Personally, I think the new semantics is much more logical.
87 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
88 * file does succeed in both HP-UX and SunOs, but not in Solaris
89 * and in the old Linux semantics.
92 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
93 * semantics. See the comments in "open_namei" and "do_link" below.
95 * [10-Sep-98 Alan Modra] Another symlink change.
98 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
99 * inside the path - always follow.
100 * in the last component in creation/removal/renaming - never follow.
101 * if LOOKUP_FOLLOW passed - follow.
102 * if the pathname has trailing slashes - follow.
103 * otherwise - don't follow.
104 * (applied in that order).
106 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
107 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
108 * During the 2.4 we need to fix the userland stuff depending on it -
109 * hopefully we will be able to get rid of that wart in 2.5. So far only
110 * XEmacs seems to be relying on it...
113 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
114 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
115 * any extra contention...
118 /* In order to reduce some races, while at the same time doing additional
119 * checking and hopefully speeding things up, we copy filenames to the
120 * kernel data space before using them..
122 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
123 * PATH_MAX includes the nul terminator --RR.
126 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
129 getname_flags(const char __user
*filename
, int flags
, int *empty
)
131 struct filename
*result
;
135 result
= audit_reusename(filename
);
139 result
= __getname();
140 if (unlikely(!result
))
141 return ERR_PTR(-ENOMEM
);
144 * First, try to embed the struct filename inside the names_cache
147 kname
= (char *)result
->iname
;
148 result
->name
= kname
;
150 len
= strncpy_from_user(kname
, filename
, EMBEDDED_NAME_MAX
);
151 if (unlikely(len
< 0)) {
157 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
158 * separate struct filename so we can dedicate the entire
159 * names_cache allocation for the pathname, and re-do the copy from
162 if (unlikely(len
== EMBEDDED_NAME_MAX
)) {
163 const size_t size
= offsetof(struct filename
, iname
[1]);
164 kname
= (char *)result
;
167 * size is chosen that way we to guarantee that
168 * result->iname[0] is within the same object and that
169 * kname can't be equal to result->iname, no matter what.
171 result
= kzalloc(size
, GFP_KERNEL
);
172 if (unlikely(!result
)) {
174 return ERR_PTR(-ENOMEM
);
176 result
->name
= kname
;
177 len
= strncpy_from_user(kname
, filename
, PATH_MAX
);
178 if (unlikely(len
< 0)) {
183 if (unlikely(len
== PATH_MAX
)) {
186 return ERR_PTR(-ENAMETOOLONG
);
191 /* The empty path is special. */
192 if (unlikely(!len
)) {
195 if (!(flags
& LOOKUP_EMPTY
)) {
197 return ERR_PTR(-ENOENT
);
201 result
->uptr
= filename
;
202 result
->aname
= NULL
;
203 audit_getname(result
);
208 getname_uflags(const char __user
*filename
, int uflags
)
210 int flags
= (uflags
& AT_EMPTY_PATH
) ? LOOKUP_EMPTY
: 0;
212 return getname_flags(filename
, flags
, NULL
);
216 getname(const char __user
* filename
)
218 return getname_flags(filename
, 0, NULL
);
222 getname_kernel(const char * filename
)
224 struct filename
*result
;
225 int len
= strlen(filename
) + 1;
227 result
= __getname();
228 if (unlikely(!result
))
229 return ERR_PTR(-ENOMEM
);
231 if (len
<= EMBEDDED_NAME_MAX
) {
232 result
->name
= (char *)result
->iname
;
233 } else if (len
<= PATH_MAX
) {
234 const size_t size
= offsetof(struct filename
, iname
[1]);
235 struct filename
*tmp
;
237 tmp
= kmalloc(size
, GFP_KERNEL
);
238 if (unlikely(!tmp
)) {
240 return ERR_PTR(-ENOMEM
);
242 tmp
->name
= (char *)result
;
246 return ERR_PTR(-ENAMETOOLONG
);
248 memcpy((char *)result
->name
, filename
, len
);
250 result
->aname
= NULL
;
252 audit_getname(result
);
257 void putname(struct filename
*name
)
262 BUG_ON(name
->refcnt
<= 0);
264 if (--name
->refcnt
> 0)
267 if (name
->name
!= name
->iname
) {
268 __putname(name
->name
);
275 * check_acl - perform ACL permission checking
276 * @mnt_userns: user namespace of the mount the inode was found from
277 * @inode: inode to check permissions on
278 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
280 * This function performs the ACL permission checking. Since this function
281 * retrieve POSIX acls it needs to know whether it is called from a blocking or
282 * non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
284 * If the inode has been found through an idmapped mount the user namespace of
285 * the vfsmount must be passed through @mnt_userns. This function will then take
286 * care to map the inode according to @mnt_userns before checking permissions.
287 * On non-idmapped mounts or if permission checking is to be performed on the
288 * raw inode simply passs init_user_ns.
290 static int check_acl(struct user_namespace
*mnt_userns
,
291 struct inode
*inode
, int mask
)
293 #ifdef CONFIG_FS_POSIX_ACL
294 struct posix_acl
*acl
;
296 if (mask
& MAY_NOT_BLOCK
) {
297 acl
= get_cached_acl_rcu(inode
, ACL_TYPE_ACCESS
);
300 /* no ->get_acl() calls in RCU mode... */
301 if (is_uncached_acl(acl
))
303 return posix_acl_permission(mnt_userns
, inode
, acl
, mask
);
306 acl
= get_acl(inode
, ACL_TYPE_ACCESS
);
310 int error
= posix_acl_permission(mnt_userns
, inode
, acl
, mask
);
311 posix_acl_release(acl
);
320 * acl_permission_check - perform basic UNIX permission checking
321 * @mnt_userns: user namespace of the mount the inode was found from
322 * @inode: inode to check permissions on
323 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
325 * This function performs the basic UNIX permission checking. Since this
326 * function may retrieve POSIX acls it needs to know whether it is called from a
327 * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
329 * If the inode has been found through an idmapped mount the user namespace of
330 * the vfsmount must be passed through @mnt_userns. This function will then take
331 * care to map the inode according to @mnt_userns before checking permissions.
332 * On non-idmapped mounts or if permission checking is to be performed on the
333 * raw inode simply passs init_user_ns.
335 static int acl_permission_check(struct user_namespace
*mnt_userns
,
336 struct inode
*inode
, int mask
)
338 unsigned int mode
= inode
->i_mode
;
341 /* Are we the owner? If so, ACL's don't matter */
342 i_uid
= i_uid_into_mnt(mnt_userns
, inode
);
343 if (likely(uid_eq(current_fsuid(), i_uid
))) {
346 return (mask
& ~mode
) ? -EACCES
: 0;
349 /* Do we have ACL's? */
350 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
)) {
351 int error
= check_acl(mnt_userns
, inode
, mask
);
352 if (error
!= -EAGAIN
)
356 /* Only RWX matters for group/other mode bits */
360 * Are the group permissions different from
361 * the other permissions in the bits we care
362 * about? Need to check group ownership if so.
364 if (mask
& (mode
^ (mode
>> 3))) {
365 kgid_t kgid
= i_gid_into_mnt(mnt_userns
, inode
);
366 if (in_group_p(kgid
))
370 /* Bits in 'mode' clear that we require? */
371 return (mask
& ~mode
) ? -EACCES
: 0;
375 * generic_permission - check for access rights on a Posix-like filesystem
376 * @mnt_userns: user namespace of the mount the inode was found from
377 * @inode: inode to check access rights for
378 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC,
379 * %MAY_NOT_BLOCK ...)
381 * Used to check for read/write/execute permissions on a file.
382 * We use "fsuid" for this, letting us set arbitrary permissions
383 * for filesystem access without changing the "normal" uids which
384 * are used for other things.
386 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
387 * request cannot be satisfied (eg. requires blocking or too much complexity).
388 * It would then be called again in ref-walk mode.
390 * If the inode has been found through an idmapped mount the user namespace of
391 * the vfsmount must be passed through @mnt_userns. This function will then take
392 * care to map the inode according to @mnt_userns before checking permissions.
393 * On non-idmapped mounts or if permission checking is to be performed on the
394 * raw inode simply passs init_user_ns.
396 int generic_permission(struct user_namespace
*mnt_userns
, struct inode
*inode
,
402 * Do the basic permission checks.
404 ret
= acl_permission_check(mnt_userns
, inode
, mask
);
408 if (S_ISDIR(inode
->i_mode
)) {
409 /* DACs are overridable for directories */
410 if (!(mask
& MAY_WRITE
))
411 if (capable_wrt_inode_uidgid(mnt_userns
, inode
,
412 CAP_DAC_READ_SEARCH
))
414 if (capable_wrt_inode_uidgid(mnt_userns
, inode
,
421 * Searching includes executable on directories, else just read.
423 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
424 if (mask
== MAY_READ
)
425 if (capable_wrt_inode_uidgid(mnt_userns
, inode
,
426 CAP_DAC_READ_SEARCH
))
429 * Read/write DACs are always overridable.
430 * Executable DACs are overridable when there is
431 * at least one exec bit set.
433 if (!(mask
& MAY_EXEC
) || (inode
->i_mode
& S_IXUGO
))
434 if (capable_wrt_inode_uidgid(mnt_userns
, inode
,
440 EXPORT_SYMBOL(generic_permission
);
443 * do_inode_permission - UNIX permission checking
444 * @mnt_userns: user namespace of the mount the inode was found from
445 * @inode: inode to check permissions on
446 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
448 * We _really_ want to just do "generic_permission()" without
449 * even looking at the inode->i_op values. So we keep a cache
450 * flag in inode->i_opflags, that says "this has not special
451 * permission function, use the fast case".
453 static inline int do_inode_permission(struct user_namespace
*mnt_userns
,
454 struct inode
*inode
, int mask
)
456 if (unlikely(!(inode
->i_opflags
& IOP_FASTPERM
))) {
457 if (likely(inode
->i_op
->permission
))
458 return inode
->i_op
->permission(mnt_userns
, inode
, mask
);
460 /* This gets set once for the inode lifetime */
461 spin_lock(&inode
->i_lock
);
462 inode
->i_opflags
|= IOP_FASTPERM
;
463 spin_unlock(&inode
->i_lock
);
465 return generic_permission(mnt_userns
, inode
, mask
);
469 * sb_permission - Check superblock-level permissions
470 * @sb: Superblock of inode to check permission on
471 * @inode: Inode to check permission on
472 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
474 * Separate out file-system wide checks from inode-specific permission checks.
476 static int sb_permission(struct super_block
*sb
, struct inode
*inode
, int mask
)
478 if (unlikely(mask
& MAY_WRITE
)) {
479 umode_t mode
= inode
->i_mode
;
481 /* Nobody gets write access to a read-only fs. */
482 if (sb_rdonly(sb
) && (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
489 * inode_permission - Check for access rights to a given inode
490 * @mnt_userns: User namespace of the mount the inode was found from
491 * @inode: Inode to check permission on
492 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
494 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
495 * this, letting us set arbitrary permissions for filesystem access without
496 * changing the "normal" UIDs which are used for other things.
498 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
500 int inode_permission(struct user_namespace
*mnt_userns
,
501 struct inode
*inode
, int mask
)
505 retval
= sb_permission(inode
->i_sb
, inode
, mask
);
509 if (unlikely(mask
& MAY_WRITE
)) {
511 * Nobody gets write access to an immutable file.
513 if (IS_IMMUTABLE(inode
))
517 * Updating mtime will likely cause i_uid and i_gid to be
518 * written back improperly if their true value is unknown
521 if (HAS_UNMAPPED_ID(mnt_userns
, inode
))
525 retval
= do_inode_permission(mnt_userns
, inode
, mask
);
529 retval
= devcgroup_inode_permission(inode
, mask
);
533 return security_inode_permission(inode
, mask
);
535 EXPORT_SYMBOL(inode_permission
);
538 * path_get - get a reference to a path
539 * @path: path to get the reference to
541 * Given a path increment the reference count to the dentry and the vfsmount.
543 void path_get(const struct path
*path
)
548 EXPORT_SYMBOL(path_get
);
551 * path_put - put a reference to a path
552 * @path: path to put the reference to
554 * Given a path decrement the reference count to the dentry and the vfsmount.
556 void path_put(const struct path
*path
)
561 EXPORT_SYMBOL(path_put
);
563 #define EMBEDDED_LEVELS 2
568 struct inode
*inode
; /* path.dentry.d_inode */
569 unsigned int flags
, state
;
570 unsigned seq
, m_seq
, r_seq
;
573 int total_link_count
;
576 struct delayed_call done
;
579 } *stack
, internal
[EMBEDDED_LEVELS
];
580 struct filename
*name
;
581 struct nameidata
*saved
;
586 } __randomize_layout
;
588 #define ND_ROOT_PRESET 1
589 #define ND_ROOT_GRABBED 2
592 static void __set_nameidata(struct nameidata
*p
, int dfd
, struct filename
*name
)
594 struct nameidata
*old
= current
->nameidata
;
595 p
->stack
= p
->internal
;
600 p
->path
.dentry
= NULL
;
601 p
->total_link_count
= old
? old
->total_link_count
: 0;
603 current
->nameidata
= p
;
606 static inline void set_nameidata(struct nameidata
*p
, int dfd
, struct filename
*name
,
607 const struct path
*root
)
609 __set_nameidata(p
, dfd
, name
);
611 if (unlikely(root
)) {
612 p
->state
= ND_ROOT_PRESET
;
617 static void restore_nameidata(void)
619 struct nameidata
*now
= current
->nameidata
, *old
= now
->saved
;
621 current
->nameidata
= old
;
623 old
->total_link_count
= now
->total_link_count
;
624 if (now
->stack
!= now
->internal
)
628 static bool nd_alloc_stack(struct nameidata
*nd
)
632 p
= kmalloc_array(MAXSYMLINKS
, sizeof(struct saved
),
633 nd
->flags
& LOOKUP_RCU
? GFP_ATOMIC
: GFP_KERNEL
);
636 memcpy(p
, nd
->internal
, sizeof(nd
->internal
));
642 * path_connected - Verify that a dentry is below mnt.mnt_root
644 * Rename can sometimes move a file or directory outside of a bind
645 * mount, path_connected allows those cases to be detected.
647 static bool path_connected(struct vfsmount
*mnt
, struct dentry
*dentry
)
649 struct super_block
*sb
= mnt
->mnt_sb
;
651 /* Bind mounts can have disconnected paths */
652 if (mnt
->mnt_root
== sb
->s_root
)
655 return is_subdir(dentry
, mnt
->mnt_root
);
658 static void drop_links(struct nameidata
*nd
)
662 struct saved
*last
= nd
->stack
+ i
;
663 do_delayed_call(&last
->done
);
664 clear_delayed_call(&last
->done
);
668 static void terminate_walk(struct nameidata
*nd
)
671 if (!(nd
->flags
& LOOKUP_RCU
)) {
674 for (i
= 0; i
< nd
->depth
; i
++)
675 path_put(&nd
->stack
[i
].link
);
676 if (nd
->state
& ND_ROOT_GRABBED
) {
678 nd
->state
&= ~ND_ROOT_GRABBED
;
681 nd
->flags
&= ~LOOKUP_RCU
;
686 nd
->path
.dentry
= NULL
;
689 /* path_put is needed afterwards regardless of success or failure */
690 static bool __legitimize_path(struct path
*path
, unsigned seq
, unsigned mseq
)
692 int res
= __legitimize_mnt(path
->mnt
, mseq
);
699 if (unlikely(!lockref_get_not_dead(&path
->dentry
->d_lockref
))) {
703 return !read_seqcount_retry(&path
->dentry
->d_seq
, seq
);
706 static inline bool legitimize_path(struct nameidata
*nd
,
707 struct path
*path
, unsigned seq
)
709 return __legitimize_path(path
, seq
, nd
->m_seq
);
712 static bool legitimize_links(struct nameidata
*nd
)
715 if (unlikely(nd
->flags
& LOOKUP_CACHED
)) {
720 for (i
= 0; i
< nd
->depth
; i
++) {
721 struct saved
*last
= nd
->stack
+ i
;
722 if (unlikely(!legitimize_path(nd
, &last
->link
, last
->seq
))) {
731 static bool legitimize_root(struct nameidata
*nd
)
733 /* Nothing to do if nd->root is zero or is managed by the VFS user. */
734 if (!nd
->root
.mnt
|| (nd
->state
& ND_ROOT_PRESET
))
736 nd
->state
|= ND_ROOT_GRABBED
;
737 return legitimize_path(nd
, &nd
->root
, nd
->root_seq
);
741 * Path walking has 2 modes, rcu-walk and ref-walk (see
742 * Documentation/filesystems/path-lookup.txt). In situations when we can't
743 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
744 * normal reference counts on dentries and vfsmounts to transition to ref-walk
745 * mode. Refcounts are grabbed at the last known good point before rcu-walk
746 * got stuck, so ref-walk may continue from there. If this is not successful
747 * (eg. a seqcount has changed), then failure is returned and it's up to caller
748 * to restart the path walk from the beginning in ref-walk mode.
752 * try_to_unlazy - try to switch to ref-walk mode.
753 * @nd: nameidata pathwalk data
754 * Returns: true on success, false on failure
756 * try_to_unlazy attempts to legitimize the current nd->path and nd->root
758 * Must be called from rcu-walk context.
759 * Nothing should touch nameidata between try_to_unlazy() failure and
762 static bool try_to_unlazy(struct nameidata
*nd
)
764 struct dentry
*parent
= nd
->path
.dentry
;
766 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
768 nd
->flags
&= ~LOOKUP_RCU
;
769 if (unlikely(!legitimize_links(nd
)))
771 if (unlikely(!legitimize_path(nd
, &nd
->path
, nd
->seq
)))
773 if (unlikely(!legitimize_root(nd
)))
776 BUG_ON(nd
->inode
!= parent
->d_inode
);
781 nd
->path
.dentry
= NULL
;
788 * try_to_unlazy_next - try to switch to ref-walk mode.
789 * @nd: nameidata pathwalk data
790 * @dentry: next dentry to step into
791 * @seq: seq number to check @dentry against
792 * Returns: true on success, false on failure
794 * Similar to try_to_unlazy(), but here we have the next dentry already
795 * picked by rcu-walk and want to legitimize that in addition to the current
796 * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context.
797 * Nothing should touch nameidata between try_to_unlazy_next() failure and
800 static bool try_to_unlazy_next(struct nameidata
*nd
, struct dentry
*dentry
, unsigned seq
)
802 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
804 nd
->flags
&= ~LOOKUP_RCU
;
805 if (unlikely(!legitimize_links(nd
)))
807 if (unlikely(!legitimize_mnt(nd
->path
.mnt
, nd
->m_seq
)))
809 if (unlikely(!lockref_get_not_dead(&nd
->path
.dentry
->d_lockref
)))
813 * We need to move both the parent and the dentry from the RCU domain
814 * to be properly refcounted. And the sequence number in the dentry
815 * validates *both* dentry counters, since we checked the sequence
816 * number of the parent after we got the child sequence number. So we
817 * know the parent must still be valid if the child sequence number is
819 if (unlikely(!lockref_get_not_dead(&dentry
->d_lockref
)))
821 if (unlikely(read_seqcount_retry(&dentry
->d_seq
, seq
)))
824 * Sequence counts matched. Now make sure that the root is
825 * still valid and get it if required.
827 if (unlikely(!legitimize_root(nd
)))
835 nd
->path
.dentry
= NULL
;
845 static inline int d_revalidate(struct dentry
*dentry
, unsigned int flags
)
847 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
))
848 return dentry
->d_op
->d_revalidate(dentry
, flags
);
854 * complete_walk - successful completion of path walk
855 * @nd: pointer nameidata
857 * If we had been in RCU mode, drop out of it and legitimize nd->path.
858 * Revalidate the final result, unless we'd already done that during
859 * the path walk or the filesystem doesn't ask for it. Return 0 on
860 * success, -error on failure. In case of failure caller does not
861 * need to drop nd->path.
863 static int complete_walk(struct nameidata
*nd
)
865 struct dentry
*dentry
= nd
->path
.dentry
;
868 if (nd
->flags
& LOOKUP_RCU
) {
870 * We don't want to zero nd->root for scoped-lookups or
871 * externally-managed nd->root.
873 if (!(nd
->state
& ND_ROOT_PRESET
))
874 if (!(nd
->flags
& LOOKUP_IS_SCOPED
))
876 nd
->flags
&= ~LOOKUP_CACHED
;
877 if (!try_to_unlazy(nd
))
881 if (unlikely(nd
->flags
& LOOKUP_IS_SCOPED
)) {
883 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
884 * ever step outside the root during lookup" and should already
885 * be guaranteed by the rest of namei, we want to avoid a namei
886 * BUG resulting in userspace being given a path that was not
887 * scoped within the root at some point during the lookup.
889 * So, do a final sanity-check to make sure that in the
890 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
891 * we won't silently return an fd completely outside of the
892 * requested root to userspace.
894 * Userspace could move the path outside the root after this
895 * check, but as discussed elsewhere this is not a concern (the
896 * resolved file was inside the root at some point).
898 if (!path_is_under(&nd
->path
, &nd
->root
))
902 if (likely(!(nd
->state
& ND_JUMPED
)))
905 if (likely(!(dentry
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
)))
908 status
= dentry
->d_op
->d_weak_revalidate(dentry
, nd
->flags
);
918 static int set_root(struct nameidata
*nd
)
920 struct fs_struct
*fs
= current
->fs
;
923 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
924 * still have to ensure it doesn't happen because it will cause a breakout
927 if (WARN_ON(nd
->flags
& LOOKUP_IS_SCOPED
))
928 return -ENOTRECOVERABLE
;
930 if (nd
->flags
& LOOKUP_RCU
) {
934 seq
= read_seqcount_begin(&fs
->seq
);
936 nd
->root_seq
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
937 } while (read_seqcount_retry(&fs
->seq
, seq
));
939 get_fs_root(fs
, &nd
->root
);
940 nd
->state
|= ND_ROOT_GRABBED
;
945 static int nd_jump_root(struct nameidata
*nd
)
947 if (unlikely(nd
->flags
& LOOKUP_BENEATH
))
949 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
)) {
950 /* Absolute path arguments to path_init() are allowed. */
951 if (nd
->path
.mnt
!= NULL
&& nd
->path
.mnt
!= nd
->root
.mnt
)
955 int error
= set_root(nd
);
959 if (nd
->flags
& LOOKUP_RCU
) {
963 nd
->inode
= d
->d_inode
;
964 nd
->seq
= nd
->root_seq
;
965 if (unlikely(read_seqcount_retry(&d
->d_seq
, nd
->seq
)))
971 nd
->inode
= nd
->path
.dentry
->d_inode
;
973 nd
->state
|= ND_JUMPED
;
978 * Helper to directly jump to a known parsed path from ->get_link,
979 * caller must have taken a reference to path beforehand.
981 int nd_jump_link(struct path
*path
)
984 struct nameidata
*nd
= current
->nameidata
;
986 if (unlikely(nd
->flags
& LOOKUP_NO_MAGICLINKS
))
990 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
)) {
991 if (nd
->path
.mnt
!= path
->mnt
)
994 /* Not currently safe for scoped-lookups. */
995 if (unlikely(nd
->flags
& LOOKUP_IS_SCOPED
))
1000 nd
->inode
= nd
->path
.dentry
->d_inode
;
1001 nd
->state
|= ND_JUMPED
;
1009 static inline void put_link(struct nameidata
*nd
)
1011 struct saved
*last
= nd
->stack
+ --nd
->depth
;
1012 do_delayed_call(&last
->done
);
1013 if (!(nd
->flags
& LOOKUP_RCU
))
1014 path_put(&last
->link
);
1017 static int sysctl_protected_symlinks __read_mostly
;
1018 static int sysctl_protected_hardlinks __read_mostly
;
1019 static int sysctl_protected_fifos __read_mostly
;
1020 static int sysctl_protected_regular __read_mostly
;
1022 #ifdef CONFIG_SYSCTL
1023 static struct ctl_table namei_sysctls
[] = {
1025 .procname
= "protected_symlinks",
1026 .data
= &sysctl_protected_symlinks
,
1027 .maxlen
= sizeof(int),
1029 .proc_handler
= proc_dointvec_minmax
,
1030 .extra1
= SYSCTL_ZERO
,
1031 .extra2
= SYSCTL_ONE
,
1034 .procname
= "protected_hardlinks",
1035 .data
= &sysctl_protected_hardlinks
,
1036 .maxlen
= sizeof(int),
1038 .proc_handler
= proc_dointvec_minmax
,
1039 .extra1
= SYSCTL_ZERO
,
1040 .extra2
= SYSCTL_ONE
,
1043 .procname
= "protected_fifos",
1044 .data
= &sysctl_protected_fifos
,
1045 .maxlen
= sizeof(int),
1047 .proc_handler
= proc_dointvec_minmax
,
1048 .extra1
= SYSCTL_ZERO
,
1049 .extra2
= SYSCTL_TWO
,
1052 .procname
= "protected_regular",
1053 .data
= &sysctl_protected_regular
,
1054 .maxlen
= sizeof(int),
1056 .proc_handler
= proc_dointvec_minmax
,
1057 .extra1
= SYSCTL_ZERO
,
1058 .extra2
= SYSCTL_TWO
,
1063 static int __init
init_fs_namei_sysctls(void)
1065 register_sysctl_init("fs", namei_sysctls
);
1068 fs_initcall(init_fs_namei_sysctls
);
1070 #endif /* CONFIG_SYSCTL */
1073 * may_follow_link - Check symlink following for unsafe situations
1074 * @nd: nameidata pathwalk data
1076 * In the case of the sysctl_protected_symlinks sysctl being enabled,
1077 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
1078 * in a sticky world-writable directory. This is to protect privileged
1079 * processes from failing races against path names that may change out
1080 * from under them by way of other users creating malicious symlinks.
1081 * It will permit symlinks to be followed only when outside a sticky
1082 * world-writable directory, or when the uid of the symlink and follower
1083 * match, or when the directory owner matches the symlink's owner.
1085 * Returns 0 if following the symlink is allowed, -ve on error.
1087 static inline int may_follow_link(struct nameidata
*nd
, const struct inode
*inode
)
1089 struct user_namespace
*mnt_userns
;
1092 if (!sysctl_protected_symlinks
)
1095 mnt_userns
= mnt_user_ns(nd
->path
.mnt
);
1096 i_uid
= i_uid_into_mnt(mnt_userns
, inode
);
1097 /* Allowed if owner and follower match. */
1098 if (uid_eq(current_cred()->fsuid
, i_uid
))
1101 /* Allowed if parent directory not sticky and world-writable. */
1102 if ((nd
->dir_mode
& (S_ISVTX
|S_IWOTH
)) != (S_ISVTX
|S_IWOTH
))
1105 /* Allowed if parent directory and link owner match. */
1106 if (uid_valid(nd
->dir_uid
) && uid_eq(nd
->dir_uid
, i_uid
))
1109 if (nd
->flags
& LOOKUP_RCU
)
1112 audit_inode(nd
->name
, nd
->stack
[0].link
.dentry
, 0);
1113 audit_log_path_denied(AUDIT_ANOM_LINK
, "follow_link");
1118 * safe_hardlink_source - Check for safe hardlink conditions
1119 * @mnt_userns: user namespace of the mount the inode was found from
1120 * @inode: the source inode to hardlink from
1122 * Return false if at least one of the following conditions:
1123 * - inode is not a regular file
1125 * - inode is setgid and group-exec
1126 * - access failure for read and write
1128 * Otherwise returns true.
1130 static bool safe_hardlink_source(struct user_namespace
*mnt_userns
,
1131 struct inode
*inode
)
1133 umode_t mode
= inode
->i_mode
;
1135 /* Special files should not get pinned to the filesystem. */
1139 /* Setuid files should not get pinned to the filesystem. */
1143 /* Executable setgid files should not get pinned to the filesystem. */
1144 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
))
1147 /* Hardlinking to unreadable or unwritable sources is dangerous. */
1148 if (inode_permission(mnt_userns
, inode
, MAY_READ
| MAY_WRITE
))
1155 * may_linkat - Check permissions for creating a hardlink
1156 * @mnt_userns: user namespace of the mount the inode was found from
1157 * @link: the source to hardlink from
1159 * Block hardlink when all of:
1160 * - sysctl_protected_hardlinks enabled
1161 * - fsuid does not match inode
1162 * - hardlink source is unsafe (see safe_hardlink_source() above)
1163 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
1165 * If the inode has been found through an idmapped mount the user namespace of
1166 * the vfsmount must be passed through @mnt_userns. This function will then take
1167 * care to map the inode according to @mnt_userns before checking permissions.
1168 * On non-idmapped mounts or if permission checking is to be performed on the
1169 * raw inode simply passs init_user_ns.
1171 * Returns 0 if successful, -ve on error.
1173 int may_linkat(struct user_namespace
*mnt_userns
, struct path
*link
)
1175 struct inode
*inode
= link
->dentry
->d_inode
;
1177 /* Inode writeback is not safe when the uid or gid are invalid. */
1178 if (!uid_valid(i_uid_into_mnt(mnt_userns
, inode
)) ||
1179 !gid_valid(i_gid_into_mnt(mnt_userns
, inode
)))
1182 if (!sysctl_protected_hardlinks
)
1185 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1186 * otherwise, it must be a safe source.
1188 if (safe_hardlink_source(mnt_userns
, inode
) ||
1189 inode_owner_or_capable(mnt_userns
, inode
))
1192 audit_log_path_denied(AUDIT_ANOM_LINK
, "linkat");
1197 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1198 * should be allowed, or not, on files that already
1200 * @mnt_userns: user namespace of the mount the inode was found from
1201 * @nd: nameidata pathwalk data
1202 * @inode: the inode of the file to open
1204 * Block an O_CREAT open of a FIFO (or a regular file) when:
1205 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1206 * - the file already exists
1207 * - we are in a sticky directory
1208 * - we don't own the file
1209 * - the owner of the directory doesn't own the file
1210 * - the directory is world writable
1211 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1212 * the directory doesn't have to be world writable: being group writable will
1215 * If the inode has been found through an idmapped mount the user namespace of
1216 * the vfsmount must be passed through @mnt_userns. This function will then take
1217 * care to map the inode according to @mnt_userns before checking permissions.
1218 * On non-idmapped mounts or if permission checking is to be performed on the
1219 * raw inode simply passs init_user_ns.
1221 * Returns 0 if the open is allowed, -ve on error.
1223 static int may_create_in_sticky(struct user_namespace
*mnt_userns
,
1224 struct nameidata
*nd
, struct inode
*const inode
)
1226 umode_t dir_mode
= nd
->dir_mode
;
1227 kuid_t dir_uid
= nd
->dir_uid
;
1229 if ((!sysctl_protected_fifos
&& S_ISFIFO(inode
->i_mode
)) ||
1230 (!sysctl_protected_regular
&& S_ISREG(inode
->i_mode
)) ||
1231 likely(!(dir_mode
& S_ISVTX
)) ||
1232 uid_eq(i_uid_into_mnt(mnt_userns
, inode
), dir_uid
) ||
1233 uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns
, inode
)))
1236 if (likely(dir_mode
& 0002) ||
1238 ((sysctl_protected_fifos
>= 2 && S_ISFIFO(inode
->i_mode
)) ||
1239 (sysctl_protected_regular
>= 2 && S_ISREG(inode
->i_mode
))))) {
1240 const char *operation
= S_ISFIFO(inode
->i_mode
) ?
1241 "sticky_create_fifo" :
1242 "sticky_create_regular";
1243 audit_log_path_denied(AUDIT_ANOM_CREAT
, operation
);
1250 * follow_up - Find the mountpoint of path's vfsmount
1252 * Given a path, find the mountpoint of its source file system.
1253 * Replace @path with the path of the mountpoint in the parent mount.
1256 * Return 1 if we went up a level and 0 if we were already at the
1259 int follow_up(struct path
*path
)
1261 struct mount
*mnt
= real_mount(path
->mnt
);
1262 struct mount
*parent
;
1263 struct dentry
*mountpoint
;
1265 read_seqlock_excl(&mount_lock
);
1266 parent
= mnt
->mnt_parent
;
1267 if (parent
== mnt
) {
1268 read_sequnlock_excl(&mount_lock
);
1271 mntget(&parent
->mnt
);
1272 mountpoint
= dget(mnt
->mnt_mountpoint
);
1273 read_sequnlock_excl(&mount_lock
);
1275 path
->dentry
= mountpoint
;
1277 path
->mnt
= &parent
->mnt
;
1280 EXPORT_SYMBOL(follow_up
);
1282 static bool choose_mountpoint_rcu(struct mount
*m
, const struct path
*root
,
1283 struct path
*path
, unsigned *seqp
)
1285 while (mnt_has_parent(m
)) {
1286 struct dentry
*mountpoint
= m
->mnt_mountpoint
;
1289 if (unlikely(root
->dentry
== mountpoint
&&
1290 root
->mnt
== &m
->mnt
))
1292 if (mountpoint
!= m
->mnt
.mnt_root
) {
1293 path
->mnt
= &m
->mnt
;
1294 path
->dentry
= mountpoint
;
1295 *seqp
= read_seqcount_begin(&mountpoint
->d_seq
);
1302 static bool choose_mountpoint(struct mount
*m
, const struct path
*root
,
1309 unsigned seq
, mseq
= read_seqbegin(&mount_lock
);
1311 found
= choose_mountpoint_rcu(m
, root
, path
, &seq
);
1312 if (unlikely(!found
)) {
1313 if (!read_seqretry(&mount_lock
, mseq
))
1316 if (likely(__legitimize_path(path
, seq
, mseq
)))
1328 * Perform an automount
1329 * - return -EISDIR to tell follow_managed() to stop and return the path we
1332 static int follow_automount(struct path
*path
, int *count
, unsigned lookup_flags
)
1334 struct dentry
*dentry
= path
->dentry
;
1336 /* We don't want to mount if someone's just doing a stat -
1337 * unless they're stat'ing a directory and appended a '/' to
1340 * We do, however, want to mount if someone wants to open or
1341 * create a file of any type under the mountpoint, wants to
1342 * traverse through the mountpoint or wants to open the
1343 * mounted directory. Also, autofs may mark negative dentries
1344 * as being automount points. These will need the attentions
1345 * of the daemon to instantiate them before they can be used.
1347 if (!(lookup_flags
& (LOOKUP_PARENT
| LOOKUP_DIRECTORY
|
1348 LOOKUP_OPEN
| LOOKUP_CREATE
| LOOKUP_AUTOMOUNT
)) &&
1352 if (count
&& (*count
)++ >= MAXSYMLINKS
)
1355 return finish_automount(dentry
->d_op
->d_automount(path
), path
);
1359 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1360 * dentries are pinned but not locked here, so negative dentry can go
1361 * positive right under us. Use of smp_load_acquire() provides a barrier
1362 * sufficient for ->d_inode and ->d_flags consistency.
1364 static int __traverse_mounts(struct path
*path
, unsigned flags
, bool *jumped
,
1365 int *count
, unsigned lookup_flags
)
1367 struct vfsmount
*mnt
= path
->mnt
;
1368 bool need_mntput
= false;
1371 while (flags
& DCACHE_MANAGED_DENTRY
) {
1372 /* Allow the filesystem to manage the transit without i_mutex
1374 if (flags
& DCACHE_MANAGE_TRANSIT
) {
1375 ret
= path
->dentry
->d_op
->d_manage(path
, false);
1376 flags
= smp_load_acquire(&path
->dentry
->d_flags
);
1381 if (flags
& DCACHE_MOUNTED
) { // something's mounted on it..
1382 struct vfsmount
*mounted
= lookup_mnt(path
);
1383 if (mounted
) { // ... in our namespace
1387 path
->mnt
= mounted
;
1388 path
->dentry
= dget(mounted
->mnt_root
);
1389 // here we know it's positive
1390 flags
= path
->dentry
->d_flags
;
1396 if (!(flags
& DCACHE_NEED_AUTOMOUNT
))
1399 // uncovered automount point
1400 ret
= follow_automount(path
, count
, lookup_flags
);
1401 flags
= smp_load_acquire(&path
->dentry
->d_flags
);
1408 // possible if you race with several mount --move
1409 if (need_mntput
&& path
->mnt
== mnt
)
1411 if (!ret
&& unlikely(d_flags_negative(flags
)))
1413 *jumped
= need_mntput
;
1417 static inline int traverse_mounts(struct path
*path
, bool *jumped
,
1418 int *count
, unsigned lookup_flags
)
1420 unsigned flags
= smp_load_acquire(&path
->dentry
->d_flags
);
1423 if (likely(!(flags
& DCACHE_MANAGED_DENTRY
))) {
1425 if (unlikely(d_flags_negative(flags
)))
1429 return __traverse_mounts(path
, flags
, jumped
, count
, lookup_flags
);
1432 int follow_down_one(struct path
*path
)
1434 struct vfsmount
*mounted
;
1436 mounted
= lookup_mnt(path
);
1440 path
->mnt
= mounted
;
1441 path
->dentry
= dget(mounted
->mnt_root
);
1446 EXPORT_SYMBOL(follow_down_one
);
1449 * Follow down to the covering mount currently visible to userspace. At each
1450 * point, the filesystem owning that dentry may be queried as to whether the
1451 * caller is permitted to proceed or not.
1453 int follow_down(struct path
*path
)
1455 struct vfsmount
*mnt
= path
->mnt
;
1457 int ret
= traverse_mounts(path
, &jumped
, NULL
, 0);
1459 if (path
->mnt
!= mnt
)
1463 EXPORT_SYMBOL(follow_down
);
1466 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1467 * we meet a managed dentry that would need blocking.
1469 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
1470 struct inode
**inode
, unsigned *seqp
)
1472 struct dentry
*dentry
= path
->dentry
;
1473 unsigned int flags
= dentry
->d_flags
;
1475 if (likely(!(flags
& DCACHE_MANAGED_DENTRY
)))
1478 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
))
1483 * Don't forget we might have a non-mountpoint managed dentry
1484 * that wants to block transit.
1486 if (unlikely(flags
& DCACHE_MANAGE_TRANSIT
)) {
1487 int res
= dentry
->d_op
->d_manage(path
, true);
1489 return res
== -EISDIR
;
1490 flags
= dentry
->d_flags
;
1493 if (flags
& DCACHE_MOUNTED
) {
1494 struct mount
*mounted
= __lookup_mnt(path
->mnt
, dentry
);
1496 path
->mnt
= &mounted
->mnt
;
1497 dentry
= path
->dentry
= mounted
->mnt
.mnt_root
;
1498 nd
->state
|= ND_JUMPED
;
1499 *seqp
= read_seqcount_begin(&dentry
->d_seq
);
1500 *inode
= dentry
->d_inode
;
1502 * We don't need to re-check ->d_seq after this
1503 * ->d_inode read - there will be an RCU delay
1504 * between mount hash removal and ->mnt_root
1505 * becoming unpinned.
1507 flags
= dentry
->d_flags
;
1510 if (read_seqretry(&mount_lock
, nd
->m_seq
))
1513 return !(flags
& DCACHE_NEED_AUTOMOUNT
);
1517 static inline int handle_mounts(struct nameidata
*nd
, struct dentry
*dentry
,
1518 struct path
*path
, struct inode
**inode
,
1524 path
->mnt
= nd
->path
.mnt
;
1525 path
->dentry
= dentry
;
1526 if (nd
->flags
& LOOKUP_RCU
) {
1527 unsigned int seq
= *seqp
;
1528 if (unlikely(!*inode
))
1530 if (likely(__follow_mount_rcu(nd
, path
, inode
, seqp
)))
1532 if (!try_to_unlazy_next(nd
, dentry
, seq
))
1534 // *path might've been clobbered by __follow_mount_rcu()
1535 path
->mnt
= nd
->path
.mnt
;
1536 path
->dentry
= dentry
;
1538 ret
= traverse_mounts(path
, &jumped
, &nd
->total_link_count
, nd
->flags
);
1540 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
))
1543 nd
->state
|= ND_JUMPED
;
1545 if (unlikely(ret
)) {
1547 if (path
->mnt
!= nd
->path
.mnt
)
1550 *inode
= d_backing_inode(path
->dentry
);
1551 *seqp
= 0; /* out of RCU mode, so the value doesn't matter */
1557 * This looks up the name in dcache and possibly revalidates the found dentry.
1558 * NULL is returned if the dentry does not exist in the cache.
1560 static struct dentry
*lookup_dcache(const struct qstr
*name
,
1564 struct dentry
*dentry
= d_lookup(dir
, name
);
1566 int error
= d_revalidate(dentry
, flags
);
1567 if (unlikely(error
<= 0)) {
1569 d_invalidate(dentry
);
1571 return ERR_PTR(error
);
1578 * Parent directory has inode locked exclusive. This is one
1579 * and only case when ->lookup() gets called on non in-lookup
1580 * dentries - as the matter of fact, this only gets called
1581 * when directory is guaranteed to have no in-lookup children
1584 static struct dentry
*__lookup_hash(const struct qstr
*name
,
1585 struct dentry
*base
, unsigned int flags
)
1587 struct dentry
*dentry
= lookup_dcache(name
, base
, flags
);
1589 struct inode
*dir
= base
->d_inode
;
1594 /* Don't create child dentry for a dead directory. */
1595 if (unlikely(IS_DEADDIR(dir
)))
1596 return ERR_PTR(-ENOENT
);
1598 dentry
= d_alloc(base
, name
);
1599 if (unlikely(!dentry
))
1600 return ERR_PTR(-ENOMEM
);
1602 old
= dir
->i_op
->lookup(dir
, dentry
, flags
);
1603 if (unlikely(old
)) {
1610 static struct dentry
*lookup_fast(struct nameidata
*nd
,
1611 struct inode
**inode
,
1614 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1618 * Rename seqlock is not required here because in the off chance
1619 * of a false negative due to a concurrent rename, the caller is
1620 * going to fall back to non-racy lookup.
1622 if (nd
->flags
& LOOKUP_RCU
) {
1624 dentry
= __d_lookup_rcu(parent
, &nd
->last
, &seq
);
1625 if (unlikely(!dentry
)) {
1626 if (!try_to_unlazy(nd
))
1627 return ERR_PTR(-ECHILD
);
1632 * This sequence count validates that the inode matches
1633 * the dentry name information from lookup.
1635 *inode
= d_backing_inode(dentry
);
1636 if (unlikely(read_seqcount_retry(&dentry
->d_seq
, seq
)))
1637 return ERR_PTR(-ECHILD
);
1640 * This sequence count validates that the parent had no
1641 * changes while we did the lookup of the dentry above.
1643 * The memory barrier in read_seqcount_begin of child is
1644 * enough, we can use __read_seqcount_retry here.
1646 if (unlikely(__read_seqcount_retry(&parent
->d_seq
, nd
->seq
)))
1647 return ERR_PTR(-ECHILD
);
1650 status
= d_revalidate(dentry
, nd
->flags
);
1651 if (likely(status
> 0))
1653 if (!try_to_unlazy_next(nd
, dentry
, seq
))
1654 return ERR_PTR(-ECHILD
);
1655 if (status
== -ECHILD
)
1656 /* we'd been told to redo it in non-rcu mode */
1657 status
= d_revalidate(dentry
, nd
->flags
);
1659 dentry
= __d_lookup(parent
, &nd
->last
);
1660 if (unlikely(!dentry
))
1662 status
= d_revalidate(dentry
, nd
->flags
);
1664 if (unlikely(status
<= 0)) {
1666 d_invalidate(dentry
);
1668 return ERR_PTR(status
);
1673 /* Fast lookup failed, do it the slow way */
1674 static struct dentry
*__lookup_slow(const struct qstr
*name
,
1678 struct dentry
*dentry
, *old
;
1679 struct inode
*inode
= dir
->d_inode
;
1680 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
1682 /* Don't go there if it's already dead */
1683 if (unlikely(IS_DEADDIR(inode
)))
1684 return ERR_PTR(-ENOENT
);
1686 dentry
= d_alloc_parallel(dir
, name
, &wq
);
1689 if (unlikely(!d_in_lookup(dentry
))) {
1690 int error
= d_revalidate(dentry
, flags
);
1691 if (unlikely(error
<= 0)) {
1693 d_invalidate(dentry
);
1698 dentry
= ERR_PTR(error
);
1701 old
= inode
->i_op
->lookup(inode
, dentry
, flags
);
1702 d_lookup_done(dentry
);
1703 if (unlikely(old
)) {
1711 static struct dentry
*lookup_slow(const struct qstr
*name
,
1715 struct inode
*inode
= dir
->d_inode
;
1717 inode_lock_shared(inode
);
1718 res
= __lookup_slow(name
, dir
, flags
);
1719 inode_unlock_shared(inode
);
1723 static inline int may_lookup(struct user_namespace
*mnt_userns
,
1724 struct nameidata
*nd
)
1726 if (nd
->flags
& LOOKUP_RCU
) {
1727 int err
= inode_permission(mnt_userns
, nd
->inode
, MAY_EXEC
|MAY_NOT_BLOCK
);
1728 if (err
!= -ECHILD
|| !try_to_unlazy(nd
))
1731 return inode_permission(mnt_userns
, nd
->inode
, MAY_EXEC
);
1734 static int reserve_stack(struct nameidata
*nd
, struct path
*link
, unsigned seq
)
1736 if (unlikely(nd
->total_link_count
++ >= MAXSYMLINKS
))
1739 if (likely(nd
->depth
!= EMBEDDED_LEVELS
))
1741 if (likely(nd
->stack
!= nd
->internal
))
1743 if (likely(nd_alloc_stack(nd
)))
1746 if (nd
->flags
& LOOKUP_RCU
) {
1747 // we need to grab link before we do unlazy. And we can't skip
1748 // unlazy even if we fail to grab the link - cleanup needs it
1749 bool grabbed_link
= legitimize_path(nd
, link
, seq
);
1751 if (!try_to_unlazy(nd
) || !grabbed_link
)
1754 if (nd_alloc_stack(nd
))
1760 enum {WALK_TRAILING
= 1, WALK_MORE
= 2, WALK_NOFOLLOW
= 4};
1762 static const char *pick_link(struct nameidata
*nd
, struct path
*link
,
1763 struct inode
*inode
, unsigned seq
, int flags
)
1767 int error
= reserve_stack(nd
, link
, seq
);
1769 if (unlikely(error
)) {
1770 if (!(nd
->flags
& LOOKUP_RCU
))
1772 return ERR_PTR(error
);
1774 last
= nd
->stack
+ nd
->depth
++;
1776 clear_delayed_call(&last
->done
);
1779 if (flags
& WALK_TRAILING
) {
1780 error
= may_follow_link(nd
, inode
);
1781 if (unlikely(error
))
1782 return ERR_PTR(error
);
1785 if (unlikely(nd
->flags
& LOOKUP_NO_SYMLINKS
) ||
1786 unlikely(link
->mnt
->mnt_flags
& MNT_NOSYMFOLLOW
))
1787 return ERR_PTR(-ELOOP
);
1789 if (!(nd
->flags
& LOOKUP_RCU
)) {
1790 touch_atime(&last
->link
);
1792 } else if (atime_needs_update(&last
->link
, inode
)) {
1793 if (!try_to_unlazy(nd
))
1794 return ERR_PTR(-ECHILD
);
1795 touch_atime(&last
->link
);
1798 error
= security_inode_follow_link(link
->dentry
, inode
,
1799 nd
->flags
& LOOKUP_RCU
);
1800 if (unlikely(error
))
1801 return ERR_PTR(error
);
1803 res
= READ_ONCE(inode
->i_link
);
1805 const char * (*get
)(struct dentry
*, struct inode
*,
1806 struct delayed_call
*);
1807 get
= inode
->i_op
->get_link
;
1808 if (nd
->flags
& LOOKUP_RCU
) {
1809 res
= get(NULL
, inode
, &last
->done
);
1810 if (res
== ERR_PTR(-ECHILD
) && try_to_unlazy(nd
))
1811 res
= get(link
->dentry
, inode
, &last
->done
);
1813 res
= get(link
->dentry
, inode
, &last
->done
);
1821 error
= nd_jump_root(nd
);
1822 if (unlikely(error
))
1823 return ERR_PTR(error
);
1824 while (unlikely(*++res
== '/'))
1829 all_done
: // pure jump
1835 * Do we need to follow links? We _really_ want to be able
1836 * to do this check without having to look at inode->i_op,
1837 * so we keep a cache of "no, this doesn't need follow_link"
1838 * for the common case.
1840 static const char *step_into(struct nameidata
*nd
, int flags
,
1841 struct dentry
*dentry
, struct inode
*inode
, unsigned seq
)
1844 int err
= handle_mounts(nd
, dentry
, &path
, &inode
, &seq
);
1847 return ERR_PTR(err
);
1848 if (likely(!d_is_symlink(path
.dentry
)) ||
1849 ((flags
& WALK_TRAILING
) && !(nd
->flags
& LOOKUP_FOLLOW
)) ||
1850 (flags
& WALK_NOFOLLOW
)) {
1851 /* not a symlink or should not follow */
1852 if (!(nd
->flags
& LOOKUP_RCU
)) {
1853 dput(nd
->path
.dentry
);
1854 if (nd
->path
.mnt
!= path
.mnt
)
1855 mntput(nd
->path
.mnt
);
1862 if (nd
->flags
& LOOKUP_RCU
) {
1863 /* make sure that d_is_symlink above matches inode */
1864 if (read_seqcount_retry(&path
.dentry
->d_seq
, seq
))
1865 return ERR_PTR(-ECHILD
);
1867 if (path
.mnt
== nd
->path
.mnt
)
1870 return pick_link(nd
, &path
, inode
, seq
, flags
);
1873 static struct dentry
*follow_dotdot_rcu(struct nameidata
*nd
,
1874 struct inode
**inodep
,
1877 struct dentry
*parent
, *old
;
1879 if (path_equal(&nd
->path
, &nd
->root
))
1881 if (unlikely(nd
->path
.dentry
== nd
->path
.mnt
->mnt_root
)) {
1884 if (!choose_mountpoint_rcu(real_mount(nd
->path
.mnt
),
1885 &nd
->root
, &path
, &seq
))
1887 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
))
1888 return ERR_PTR(-ECHILD
);
1890 nd
->inode
= path
.dentry
->d_inode
;
1892 if (unlikely(read_seqretry(&mount_lock
, nd
->m_seq
)))
1893 return ERR_PTR(-ECHILD
);
1894 /* we know that mountpoint was pinned */
1896 old
= nd
->path
.dentry
;
1897 parent
= old
->d_parent
;
1898 *inodep
= parent
->d_inode
;
1899 *seqp
= read_seqcount_begin(&parent
->d_seq
);
1900 if (unlikely(read_seqcount_retry(&old
->d_seq
, nd
->seq
)))
1901 return ERR_PTR(-ECHILD
);
1902 if (unlikely(!path_connected(nd
->path
.mnt
, parent
)))
1903 return ERR_PTR(-ECHILD
);
1906 if (unlikely(read_seqretry(&mount_lock
, nd
->m_seq
)))
1907 return ERR_PTR(-ECHILD
);
1908 if (unlikely(nd
->flags
& LOOKUP_BENEATH
))
1909 return ERR_PTR(-ECHILD
);
1913 static struct dentry
*follow_dotdot(struct nameidata
*nd
,
1914 struct inode
**inodep
,
1917 struct dentry
*parent
;
1919 if (path_equal(&nd
->path
, &nd
->root
))
1921 if (unlikely(nd
->path
.dentry
== nd
->path
.mnt
->mnt_root
)) {
1924 if (!choose_mountpoint(real_mount(nd
->path
.mnt
),
1927 path_put(&nd
->path
);
1929 nd
->inode
= path
.dentry
->d_inode
;
1930 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
))
1931 return ERR_PTR(-EXDEV
);
1933 /* rare case of legitimate dget_parent()... */
1934 parent
= dget_parent(nd
->path
.dentry
);
1935 if (unlikely(!path_connected(nd
->path
.mnt
, parent
))) {
1937 return ERR_PTR(-ENOENT
);
1940 *inodep
= parent
->d_inode
;
1944 if (unlikely(nd
->flags
& LOOKUP_BENEATH
))
1945 return ERR_PTR(-EXDEV
);
1946 dget(nd
->path
.dentry
);
1950 static const char *handle_dots(struct nameidata
*nd
, int type
)
1952 if (type
== LAST_DOTDOT
) {
1953 const char *error
= NULL
;
1954 struct dentry
*parent
;
1955 struct inode
*inode
;
1958 if (!nd
->root
.mnt
) {
1959 error
= ERR_PTR(set_root(nd
));
1963 if (nd
->flags
& LOOKUP_RCU
)
1964 parent
= follow_dotdot_rcu(nd
, &inode
, &seq
);
1966 parent
= follow_dotdot(nd
, &inode
, &seq
);
1968 return ERR_CAST(parent
);
1969 if (unlikely(!parent
))
1970 error
= step_into(nd
, WALK_NOFOLLOW
,
1971 nd
->path
.dentry
, nd
->inode
, nd
->seq
);
1973 error
= step_into(nd
, WALK_NOFOLLOW
,
1974 parent
, inode
, seq
);
1975 if (unlikely(error
))
1978 if (unlikely(nd
->flags
& LOOKUP_IS_SCOPED
)) {
1980 * If there was a racing rename or mount along our
1981 * path, then we can't be sure that ".." hasn't jumped
1982 * above nd->root (and so userspace should retry or use
1986 if (unlikely(__read_seqcount_retry(&mount_lock
.seqcount
, nd
->m_seq
)))
1987 return ERR_PTR(-EAGAIN
);
1988 if (unlikely(__read_seqcount_retry(&rename_lock
.seqcount
, nd
->r_seq
)))
1989 return ERR_PTR(-EAGAIN
);
1995 static const char *walk_component(struct nameidata
*nd
, int flags
)
1997 struct dentry
*dentry
;
1998 struct inode
*inode
;
2001 * "." and ".." are special - ".." especially so because it has
2002 * to be able to know about the current root directory and
2003 * parent relationships.
2005 if (unlikely(nd
->last_type
!= LAST_NORM
)) {
2006 if (!(flags
& WALK_MORE
) && nd
->depth
)
2008 return handle_dots(nd
, nd
->last_type
);
2010 dentry
= lookup_fast(nd
, &inode
, &seq
);
2012 return ERR_CAST(dentry
);
2013 if (unlikely(!dentry
)) {
2014 dentry
= lookup_slow(&nd
->last
, nd
->path
.dentry
, nd
->flags
);
2016 return ERR_CAST(dentry
);
2018 if (!(flags
& WALK_MORE
) && nd
->depth
)
2020 return step_into(nd
, flags
, dentry
, inode
, seq
);
2024 * We can do the critical dentry name comparison and hashing
2025 * operations one word at a time, but we are limited to:
2027 * - Architectures with fast unaligned word accesses. We could
2028 * do a "get_unaligned()" if this helps and is sufficiently
2031 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
2032 * do not trap on the (extremely unlikely) case of a page
2033 * crossing operation.
2035 * - Furthermore, we need an efficient 64-bit compile for the
2036 * 64-bit case in order to generate the "number of bytes in
2037 * the final mask". Again, that could be replaced with a
2038 * efficient population count instruction or similar.
2040 #ifdef CONFIG_DCACHE_WORD_ACCESS
2042 #include <asm/word-at-a-time.h>
2046 /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
2048 #elif defined(CONFIG_64BIT)
2050 * Register pressure in the mixing function is an issue, particularly
2051 * on 32-bit x86, but almost any function requires one state value and
2052 * one temporary. Instead, use a function designed for two state values
2053 * and no temporaries.
2055 * This function cannot create a collision in only two iterations, so
2056 * we have two iterations to achieve avalanche. In those two iterations,
2057 * we have six layers of mixing, which is enough to spread one bit's
2058 * influence out to 2^6 = 64 state bits.
2060 * Rotate constants are scored by considering either 64 one-bit input
2061 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
2062 * probability of that delta causing a change to each of the 128 output
2063 * bits, using a sample of random initial states.
2065 * The Shannon entropy of the computed probabilities is then summed
2066 * to produce a score. Ideally, any input change has a 50% chance of
2067 * toggling any given output bit.
2069 * Mixing scores (in bits) for (12,45):
2070 * Input delta: 1-bit 2-bit
2071 * 1 round: 713.3 42542.6
2072 * 2 rounds: 2753.7 140389.8
2073 * 3 rounds: 5954.1 233458.2
2074 * 4 rounds: 7862.6 256672.2
2075 * Perfect: 8192 258048
2076 * (64*128) (64*63/2 * 128)
2078 #define HASH_MIX(x, y, a) \
2080 y ^= x, x = rol64(x,12),\
2081 x += y, y = rol64(y,45),\
2085 * Fold two longs into one 32-bit hash value. This must be fast, but
2086 * latency isn't quite as critical, as there is a fair bit of additional
2087 * work done before the hash value is used.
2089 static inline unsigned int fold_hash(unsigned long x
, unsigned long y
)
2091 y
^= x
* GOLDEN_RATIO_64
;
2092 y
*= GOLDEN_RATIO_64
;
2096 #else /* 32-bit case */
2099 * Mixing scores (in bits) for (7,20):
2100 * Input delta: 1-bit 2-bit
2101 * 1 round: 330.3 9201.6
2102 * 2 rounds: 1246.4 25475.4
2103 * 3 rounds: 1907.1 31295.1
2104 * 4 rounds: 2042.3 31718.6
2105 * Perfect: 2048 31744
2106 * (32*64) (32*31/2 * 64)
2108 #define HASH_MIX(x, y, a) \
2110 y ^= x, x = rol32(x, 7),\
2111 x += y, y = rol32(y,20),\
2114 static inline unsigned int fold_hash(unsigned long x
, unsigned long y
)
2116 /* Use arch-optimized multiply if one exists */
2117 return __hash_32(y
^ __hash_32(x
));
2123 * Return the hash of a string of known length. This is carfully
2124 * designed to match hash_name(), which is the more critical function.
2125 * In particular, we must end by hashing a final word containing 0..7
2126 * payload bytes, to match the way that hash_name() iterates until it
2127 * finds the delimiter after the name.
2129 unsigned int full_name_hash(const void *salt
, const char *name
, unsigned int len
)
2131 unsigned long a
, x
= 0, y
= (unsigned long)salt
;
2136 a
= load_unaligned_zeropad(name
);
2137 if (len
< sizeof(unsigned long))
2140 name
+= sizeof(unsigned long);
2141 len
-= sizeof(unsigned long);
2143 x
^= a
& bytemask_from_count(len
);
2145 return fold_hash(x
, y
);
2147 EXPORT_SYMBOL(full_name_hash
);
2149 /* Return the "hash_len" (hash and length) of a null-terminated string */
2150 u64
hashlen_string(const void *salt
, const char *name
)
2152 unsigned long a
= 0, x
= 0, y
= (unsigned long)salt
;
2153 unsigned long adata
, mask
, len
;
2154 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
2161 len
+= sizeof(unsigned long);
2163 a
= load_unaligned_zeropad(name
+len
);
2164 } while (!has_zero(a
, &adata
, &constants
));
2166 adata
= prep_zero_mask(a
, adata
, &constants
);
2167 mask
= create_zero_mask(adata
);
2168 x
^= a
& zero_bytemask(mask
);
2170 return hashlen_create(fold_hash(x
, y
), len
+ find_zero(mask
));
2172 EXPORT_SYMBOL(hashlen_string
);
2175 * Calculate the length and hash of the path component, and
2176 * return the "hash_len" as the result.
2178 static inline u64
hash_name(const void *salt
, const char *name
)
2180 unsigned long a
= 0, b
, x
= 0, y
= (unsigned long)salt
;
2181 unsigned long adata
, bdata
, mask
, len
;
2182 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
2189 len
+= sizeof(unsigned long);
2191 a
= load_unaligned_zeropad(name
+len
);
2192 b
= a
^ REPEAT_BYTE('/');
2193 } while (!(has_zero(a
, &adata
, &constants
) | has_zero(b
, &bdata
, &constants
)));
2195 adata
= prep_zero_mask(a
, adata
, &constants
);
2196 bdata
= prep_zero_mask(b
, bdata
, &constants
);
2197 mask
= create_zero_mask(adata
| bdata
);
2198 x
^= a
& zero_bytemask(mask
);
2200 return hashlen_create(fold_hash(x
, y
), len
+ find_zero(mask
));
2203 #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
2205 /* Return the hash of a string of known length */
2206 unsigned int full_name_hash(const void *salt
, const char *name
, unsigned int len
)
2208 unsigned long hash
= init_name_hash(salt
);
2210 hash
= partial_name_hash((unsigned char)*name
++, hash
);
2211 return end_name_hash(hash
);
2213 EXPORT_SYMBOL(full_name_hash
);
2215 /* Return the "hash_len" (hash and length) of a null-terminated string */
2216 u64
hashlen_string(const void *salt
, const char *name
)
2218 unsigned long hash
= init_name_hash(salt
);
2219 unsigned long len
= 0, c
;
2221 c
= (unsigned char)*name
;
2224 hash
= partial_name_hash(c
, hash
);
2225 c
= (unsigned char)name
[len
];
2227 return hashlen_create(end_name_hash(hash
), len
);
2229 EXPORT_SYMBOL(hashlen_string
);
2232 * We know there's a real path component here of at least
2235 static inline u64
hash_name(const void *salt
, const char *name
)
2237 unsigned long hash
= init_name_hash(salt
);
2238 unsigned long len
= 0, c
;
2240 c
= (unsigned char)*name
;
2243 hash
= partial_name_hash(c
, hash
);
2244 c
= (unsigned char)name
[len
];
2245 } while (c
&& c
!= '/');
2246 return hashlen_create(end_name_hash(hash
), len
);
2253 * This is the basic name resolution function, turning a pathname into
2254 * the final dentry. We expect 'base' to be positive and a directory.
2256 * Returns 0 and nd will have valid dentry and mnt on success.
2257 * Returns error and drops reference to input namei data on failure.
2259 static int link_path_walk(const char *name
, struct nameidata
*nd
)
2261 int depth
= 0; // depth <= nd->depth
2264 nd
->last_type
= LAST_ROOT
;
2265 nd
->flags
|= LOOKUP_PARENT
;
2267 return PTR_ERR(name
);
2271 nd
->dir_mode
= 0; // short-circuit the 'hardening' idiocy
2275 /* At this point we know we have a real path component. */
2277 struct user_namespace
*mnt_userns
;
2282 mnt_userns
= mnt_user_ns(nd
->path
.mnt
);
2283 err
= may_lookup(mnt_userns
, nd
);
2287 hash_len
= hash_name(nd
->path
.dentry
, name
);
2290 if (name
[0] == '.') switch (hashlen_len(hash_len
)) {
2292 if (name
[1] == '.') {
2294 nd
->state
|= ND_JUMPED
;
2300 if (likely(type
== LAST_NORM
)) {
2301 struct dentry
*parent
= nd
->path
.dentry
;
2302 nd
->state
&= ~ND_JUMPED
;
2303 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
2304 struct qstr
this = { { .hash_len
= hash_len
}, .name
= name
};
2305 err
= parent
->d_op
->d_hash(parent
, &this);
2308 hash_len
= this.hash_len
;
2313 nd
->last
.hash_len
= hash_len
;
2314 nd
->last
.name
= name
;
2315 nd
->last_type
= type
;
2317 name
+= hashlen_len(hash_len
);
2321 * If it wasn't NUL, we know it was '/'. Skip that
2322 * slash, and continue until no more slashes.
2326 } while (unlikely(*name
== '/'));
2327 if (unlikely(!*name
)) {
2329 /* pathname or trailing symlink, done */
2331 nd
->dir_uid
= i_uid_into_mnt(mnt_userns
, nd
->inode
);
2332 nd
->dir_mode
= nd
->inode
->i_mode
;
2333 nd
->flags
&= ~LOOKUP_PARENT
;
2336 /* last component of nested symlink */
2337 name
= nd
->stack
[--depth
].name
;
2338 link
= walk_component(nd
, 0);
2340 /* not the last component */
2341 link
= walk_component(nd
, WALK_MORE
);
2343 if (unlikely(link
)) {
2345 return PTR_ERR(link
);
2346 /* a symlink to follow */
2347 nd
->stack
[depth
++].name
= name
;
2351 if (unlikely(!d_can_lookup(nd
->path
.dentry
))) {
2352 if (nd
->flags
& LOOKUP_RCU
) {
2353 if (!try_to_unlazy(nd
))
2361 /* must be paired with terminate_walk() */
2362 static const char *path_init(struct nameidata
*nd
, unsigned flags
)
2365 const char *s
= nd
->name
->name
;
2367 /* LOOKUP_CACHED requires RCU, ask caller to retry */
2368 if ((flags
& (LOOKUP_RCU
| LOOKUP_CACHED
)) == LOOKUP_CACHED
)
2369 return ERR_PTR(-EAGAIN
);
2372 flags
&= ~LOOKUP_RCU
;
2373 if (flags
& LOOKUP_RCU
)
2377 nd
->state
|= ND_JUMPED
;
2379 nd
->m_seq
= __read_seqcount_begin(&mount_lock
.seqcount
);
2380 nd
->r_seq
= __read_seqcount_begin(&rename_lock
.seqcount
);
2383 if (nd
->state
& ND_ROOT_PRESET
) {
2384 struct dentry
*root
= nd
->root
.dentry
;
2385 struct inode
*inode
= root
->d_inode
;
2386 if (*s
&& unlikely(!d_can_lookup(root
)))
2387 return ERR_PTR(-ENOTDIR
);
2388 nd
->path
= nd
->root
;
2390 if (flags
& LOOKUP_RCU
) {
2391 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2392 nd
->root_seq
= nd
->seq
;
2394 path_get(&nd
->path
);
2399 nd
->root
.mnt
= NULL
;
2401 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2402 if (*s
== '/' && !(flags
& LOOKUP_IN_ROOT
)) {
2403 error
= nd_jump_root(nd
);
2404 if (unlikely(error
))
2405 return ERR_PTR(error
);
2409 /* Relative pathname -- get the starting-point it is relative to. */
2410 if (nd
->dfd
== AT_FDCWD
) {
2411 if (flags
& LOOKUP_RCU
) {
2412 struct fs_struct
*fs
= current
->fs
;
2416 seq
= read_seqcount_begin(&fs
->seq
);
2418 nd
->inode
= nd
->path
.dentry
->d_inode
;
2419 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2420 } while (read_seqcount_retry(&fs
->seq
, seq
));
2422 get_fs_pwd(current
->fs
, &nd
->path
);
2423 nd
->inode
= nd
->path
.dentry
->d_inode
;
2426 /* Caller must check execute permissions on the starting path component */
2427 struct fd f
= fdget_raw(nd
->dfd
);
2428 struct dentry
*dentry
;
2431 return ERR_PTR(-EBADF
);
2433 dentry
= f
.file
->f_path
.dentry
;
2435 if (*s
&& unlikely(!d_can_lookup(dentry
))) {
2437 return ERR_PTR(-ENOTDIR
);
2440 nd
->path
= f
.file
->f_path
;
2441 if (flags
& LOOKUP_RCU
) {
2442 nd
->inode
= nd
->path
.dentry
->d_inode
;
2443 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2445 path_get(&nd
->path
);
2446 nd
->inode
= nd
->path
.dentry
->d_inode
;
2451 /* For scoped-lookups we need to set the root to the dirfd as well. */
2452 if (flags
& LOOKUP_IS_SCOPED
) {
2453 nd
->root
= nd
->path
;
2454 if (flags
& LOOKUP_RCU
) {
2455 nd
->root_seq
= nd
->seq
;
2457 path_get(&nd
->root
);
2458 nd
->state
|= ND_ROOT_GRABBED
;
2464 static inline const char *lookup_last(struct nameidata
*nd
)
2466 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
2467 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2469 return walk_component(nd
, WALK_TRAILING
);
2472 static int handle_lookup_down(struct nameidata
*nd
)
2474 if (!(nd
->flags
& LOOKUP_RCU
))
2475 dget(nd
->path
.dentry
);
2476 return PTR_ERR(step_into(nd
, WALK_NOFOLLOW
,
2477 nd
->path
.dentry
, nd
->inode
, nd
->seq
));
2480 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2481 static int path_lookupat(struct nameidata
*nd
, unsigned flags
, struct path
*path
)
2483 const char *s
= path_init(nd
, flags
);
2486 if (unlikely(flags
& LOOKUP_DOWN
) && !IS_ERR(s
)) {
2487 err
= handle_lookup_down(nd
);
2488 if (unlikely(err
< 0))
2492 while (!(err
= link_path_walk(s
, nd
)) &&
2493 (s
= lookup_last(nd
)) != NULL
)
2495 if (!err
&& unlikely(nd
->flags
& LOOKUP_MOUNTPOINT
)) {
2496 err
= handle_lookup_down(nd
);
2497 nd
->state
&= ~ND_JUMPED
; // no d_weak_revalidate(), please...
2500 err
= complete_walk(nd
);
2502 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
)
2503 if (!d_can_lookup(nd
->path
.dentry
))
2507 nd
->path
.mnt
= NULL
;
2508 nd
->path
.dentry
= NULL
;
2514 int filename_lookup(int dfd
, struct filename
*name
, unsigned flags
,
2515 struct path
*path
, struct path
*root
)
2518 struct nameidata nd
;
2520 return PTR_ERR(name
);
2521 set_nameidata(&nd
, dfd
, name
, root
);
2522 retval
= path_lookupat(&nd
, flags
| LOOKUP_RCU
, path
);
2523 if (unlikely(retval
== -ECHILD
))
2524 retval
= path_lookupat(&nd
, flags
, path
);
2525 if (unlikely(retval
== -ESTALE
))
2526 retval
= path_lookupat(&nd
, flags
| LOOKUP_REVAL
, path
);
2528 if (likely(!retval
))
2529 audit_inode(name
, path
->dentry
,
2530 flags
& LOOKUP_MOUNTPOINT
? AUDIT_INODE_NOEVAL
: 0);
2531 restore_nameidata();
2535 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2536 static int path_parentat(struct nameidata
*nd
, unsigned flags
,
2537 struct path
*parent
)
2539 const char *s
= path_init(nd
, flags
);
2540 int err
= link_path_walk(s
, nd
);
2542 err
= complete_walk(nd
);
2545 nd
->path
.mnt
= NULL
;
2546 nd
->path
.dentry
= NULL
;
2552 /* Note: this does not consume "name" */
2553 static int filename_parentat(int dfd
, struct filename
*name
,
2554 unsigned int flags
, struct path
*parent
,
2555 struct qstr
*last
, int *type
)
2558 struct nameidata nd
;
2561 return PTR_ERR(name
);
2562 set_nameidata(&nd
, dfd
, name
, NULL
);
2563 retval
= path_parentat(&nd
, flags
| LOOKUP_RCU
, parent
);
2564 if (unlikely(retval
== -ECHILD
))
2565 retval
= path_parentat(&nd
, flags
, parent
);
2566 if (unlikely(retval
== -ESTALE
))
2567 retval
= path_parentat(&nd
, flags
| LOOKUP_REVAL
, parent
);
2568 if (likely(!retval
)) {
2570 *type
= nd
.last_type
;
2571 audit_inode(name
, parent
->dentry
, AUDIT_INODE_PARENT
);
2573 restore_nameidata();
2577 /* does lookup, returns the object with parent locked */
2578 static struct dentry
*__kern_path_locked(struct filename
*name
, struct path
*path
)
2584 error
= filename_parentat(AT_FDCWD
, name
, 0, path
, &last
, &type
);
2586 return ERR_PTR(error
);
2587 if (unlikely(type
!= LAST_NORM
)) {
2589 return ERR_PTR(-EINVAL
);
2591 inode_lock_nested(path
->dentry
->d_inode
, I_MUTEX_PARENT
);
2592 d
= __lookup_hash(&last
, path
->dentry
, 0);
2594 inode_unlock(path
->dentry
->d_inode
);
2600 struct dentry
*kern_path_locked(const char *name
, struct path
*path
)
2602 struct filename
*filename
= getname_kernel(name
);
2603 struct dentry
*res
= __kern_path_locked(filename
, path
);
2609 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
2611 struct filename
*filename
= getname_kernel(name
);
2612 int ret
= filename_lookup(AT_FDCWD
, filename
, flags
, path
, NULL
);
2618 EXPORT_SYMBOL(kern_path
);
2621 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2622 * @dentry: pointer to dentry of the base directory
2623 * @mnt: pointer to vfs mount of the base directory
2624 * @name: pointer to file name
2625 * @flags: lookup flags
2626 * @path: pointer to struct path to fill
2628 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
2629 const char *name
, unsigned int flags
,
2632 struct filename
*filename
;
2633 struct path root
= {.mnt
= mnt
, .dentry
= dentry
};
2636 filename
= getname_kernel(name
);
2637 /* the first argument of filename_lookup() is ignored with root */
2638 ret
= filename_lookup(AT_FDCWD
, filename
, flags
, path
, &root
);
2642 EXPORT_SYMBOL(vfs_path_lookup
);
2644 static int lookup_one_common(struct user_namespace
*mnt_userns
,
2645 const char *name
, struct dentry
*base
, int len
,
2650 this->hash
= full_name_hash(base
, name
, len
);
2654 if (unlikely(name
[0] == '.')) {
2655 if (len
< 2 || (len
== 2 && name
[1] == '.'))
2660 unsigned int c
= *(const unsigned char *)name
++;
2661 if (c
== '/' || c
== '\0')
2665 * See if the low-level filesystem might want
2666 * to use its own hash..
2668 if (base
->d_flags
& DCACHE_OP_HASH
) {
2669 int err
= base
->d_op
->d_hash(base
, this);
2674 return inode_permission(mnt_userns
, base
->d_inode
, MAY_EXEC
);
2678 * try_lookup_one_len - filesystem helper to lookup single pathname component
2679 * @name: pathname component to lookup
2680 * @base: base directory to lookup from
2681 * @len: maximum length @len should be interpreted to
2683 * Look up a dentry by name in the dcache, returning NULL if it does not
2684 * currently exist. The function does not try to create a dentry.
2686 * Note that this routine is purely a helper for filesystem usage and should
2687 * not be called by generic code.
2689 * The caller must hold base->i_mutex.
2691 struct dentry
*try_lookup_one_len(const char *name
, struct dentry
*base
, int len
)
2696 WARN_ON_ONCE(!inode_is_locked(base
->d_inode
));
2698 err
= lookup_one_common(&init_user_ns
, name
, base
, len
, &this);
2700 return ERR_PTR(err
);
2702 return lookup_dcache(&this, base
, 0);
2704 EXPORT_SYMBOL(try_lookup_one_len
);
2707 * lookup_one_len - filesystem helper to lookup single pathname component
2708 * @name: pathname component to lookup
2709 * @base: base directory to lookup from
2710 * @len: maximum length @len should be interpreted to
2712 * Note that this routine is purely a helper for filesystem usage and should
2713 * not be called by generic code.
2715 * The caller must hold base->i_mutex.
2717 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
2719 struct dentry
*dentry
;
2723 WARN_ON_ONCE(!inode_is_locked(base
->d_inode
));
2725 err
= lookup_one_common(&init_user_ns
, name
, base
, len
, &this);
2727 return ERR_PTR(err
);
2729 dentry
= lookup_dcache(&this, base
, 0);
2730 return dentry
? dentry
: __lookup_slow(&this, base
, 0);
2732 EXPORT_SYMBOL(lookup_one_len
);
2735 * lookup_one - filesystem helper to lookup single pathname component
2736 * @mnt_userns: user namespace of the mount the lookup is performed from
2737 * @name: pathname component to lookup
2738 * @base: base directory to lookup from
2739 * @len: maximum length @len should be interpreted to
2741 * Note that this routine is purely a helper for filesystem usage and should
2742 * not be called by generic code.
2744 * The caller must hold base->i_mutex.
2746 struct dentry
*lookup_one(struct user_namespace
*mnt_userns
, const char *name
,
2747 struct dentry
*base
, int len
)
2749 struct dentry
*dentry
;
2753 WARN_ON_ONCE(!inode_is_locked(base
->d_inode
));
2755 err
= lookup_one_common(mnt_userns
, name
, base
, len
, &this);
2757 return ERR_PTR(err
);
2759 dentry
= lookup_dcache(&this, base
, 0);
2760 return dentry
? dentry
: __lookup_slow(&this, base
, 0);
2762 EXPORT_SYMBOL(lookup_one
);
2765 * lookup_one_unlocked - filesystem helper to lookup single pathname component
2766 * @mnt_userns: idmapping of the mount the lookup is performed from
2767 * @name: pathname component to lookup
2768 * @base: base directory to lookup from
2769 * @len: maximum length @len should be interpreted to
2771 * Note that this routine is purely a helper for filesystem usage and should
2772 * not be called by generic code.
2774 * Unlike lookup_one_len, it should be called without the parent
2775 * i_mutex held, and will take the i_mutex itself if necessary.
2777 struct dentry
*lookup_one_unlocked(struct user_namespace
*mnt_userns
,
2778 const char *name
, struct dentry
*base
,
2785 err
= lookup_one_common(mnt_userns
, name
, base
, len
, &this);
2787 return ERR_PTR(err
);
2789 ret
= lookup_dcache(&this, base
, 0);
2791 ret
= lookup_slow(&this, base
, 0);
2794 EXPORT_SYMBOL(lookup_one_unlocked
);
2797 * lookup_one_positive_unlocked - filesystem helper to lookup single
2798 * pathname component
2799 * @mnt_userns: idmapping of the mount the lookup is performed from
2800 * @name: pathname component to lookup
2801 * @base: base directory to lookup from
2802 * @len: maximum length @len should be interpreted to
2804 * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns
2805 * known positive or ERR_PTR(). This is what most of the users want.
2807 * Note that pinned negative with unlocked parent _can_ become positive at any
2808 * time, so callers of lookup_one_unlocked() need to be very careful; pinned
2809 * positives have >d_inode stable, so this one avoids such problems.
2811 * Note that this routine is purely a helper for filesystem usage and should
2812 * not be called by generic code.
2814 * The helper should be called without i_mutex held.
2816 struct dentry
*lookup_one_positive_unlocked(struct user_namespace
*mnt_userns
,
2818 struct dentry
*base
, int len
)
2820 struct dentry
*ret
= lookup_one_unlocked(mnt_userns
, name
, base
, len
);
2822 if (!IS_ERR(ret
) && d_flags_negative(smp_load_acquire(&ret
->d_flags
))) {
2824 ret
= ERR_PTR(-ENOENT
);
2828 EXPORT_SYMBOL(lookup_one_positive_unlocked
);
2831 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2832 * @name: pathname component to lookup
2833 * @base: base directory to lookup from
2834 * @len: maximum length @len should be interpreted to
2836 * Note that this routine is purely a helper for filesystem usage and should
2837 * not be called by generic code.
2839 * Unlike lookup_one_len, it should be called without the parent
2840 * i_mutex held, and will take the i_mutex itself if necessary.
2842 struct dentry
*lookup_one_len_unlocked(const char *name
,
2843 struct dentry
*base
, int len
)
2845 return lookup_one_unlocked(&init_user_ns
, name
, base
, len
);
2847 EXPORT_SYMBOL(lookup_one_len_unlocked
);
2850 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2851 * on negatives. Returns known positive or ERR_PTR(); that's what
2852 * most of the users want. Note that pinned negative with unlocked parent
2853 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2854 * need to be very careful; pinned positives have ->d_inode stable, so
2855 * this one avoids such problems.
2857 struct dentry
*lookup_positive_unlocked(const char *name
,
2858 struct dentry
*base
, int len
)
2860 return lookup_one_positive_unlocked(&init_user_ns
, name
, base
, len
);
2862 EXPORT_SYMBOL(lookup_positive_unlocked
);
2864 #ifdef CONFIG_UNIX98_PTYS
2865 int path_pts(struct path
*path
)
2867 /* Find something mounted on "pts" in the same directory as
2870 struct dentry
*parent
= dget_parent(path
->dentry
);
2871 struct dentry
*child
;
2872 struct qstr
this = QSTR_INIT("pts", 3);
2874 if (unlikely(!path_connected(path
->mnt
, parent
))) {
2879 path
->dentry
= parent
;
2880 child
= d_hash_and_lookup(parent
, &this);
2884 path
->dentry
= child
;
2891 int user_path_at_empty(int dfd
, const char __user
*name
, unsigned flags
,
2892 struct path
*path
, int *empty
)
2894 struct filename
*filename
= getname_flags(name
, flags
, empty
);
2895 int ret
= filename_lookup(dfd
, filename
, flags
, path
, NULL
);
2900 EXPORT_SYMBOL(user_path_at_empty
);
2902 int __check_sticky(struct user_namespace
*mnt_userns
, struct inode
*dir
,
2903 struct inode
*inode
)
2905 kuid_t fsuid
= current_fsuid();
2907 if (uid_eq(i_uid_into_mnt(mnt_userns
, inode
), fsuid
))
2909 if (uid_eq(i_uid_into_mnt(mnt_userns
, dir
), fsuid
))
2911 return !capable_wrt_inode_uidgid(mnt_userns
, inode
, CAP_FOWNER
);
2913 EXPORT_SYMBOL(__check_sticky
);
2916 * Check whether we can remove a link victim from directory dir, check
2917 * whether the type of victim is right.
2918 * 1. We can't do it if dir is read-only (done in permission())
2919 * 2. We should have write and exec permissions on dir
2920 * 3. We can't remove anything from append-only dir
2921 * 4. We can't do anything with immutable dir (done in permission())
2922 * 5. If the sticky bit on dir is set we should either
2923 * a. be owner of dir, or
2924 * b. be owner of victim, or
2925 * c. have CAP_FOWNER capability
2926 * 6. If the victim is append-only or immutable we can't do antyhing with
2927 * links pointing to it.
2928 * 7. If the victim has an unknown uid or gid we can't change the inode.
2929 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2930 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2931 * 10. We can't remove a root or mountpoint.
2932 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
2933 * nfs_async_unlink().
2935 static int may_delete(struct user_namespace
*mnt_userns
, struct inode
*dir
,
2936 struct dentry
*victim
, bool isdir
)
2938 struct inode
*inode
= d_backing_inode(victim
);
2941 if (d_is_negative(victim
))
2945 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
2947 /* Inode writeback is not safe when the uid or gid are invalid. */
2948 if (!uid_valid(i_uid_into_mnt(mnt_userns
, inode
)) ||
2949 !gid_valid(i_gid_into_mnt(mnt_userns
, inode
)))
2952 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
2954 error
= inode_permission(mnt_userns
, dir
, MAY_WRITE
| MAY_EXEC
);
2960 if (check_sticky(mnt_userns
, dir
, inode
) || IS_APPEND(inode
) ||
2961 IS_IMMUTABLE(inode
) || IS_SWAPFILE(inode
) ||
2962 HAS_UNMAPPED_ID(mnt_userns
, inode
))
2965 if (!d_is_dir(victim
))
2967 if (IS_ROOT(victim
))
2969 } else if (d_is_dir(victim
))
2971 if (IS_DEADDIR(dir
))
2973 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
2978 /* Check whether we can create an object with dentry child in directory
2980 * 1. We can't do it if child already exists (open has special treatment for
2981 * this case, but since we are inlined it's OK)
2982 * 2. We can't do it if dir is read-only (done in permission())
2983 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2984 * 4. We should have write and exec permissions on dir
2985 * 5. We can't do it if dir is immutable (done in permission())
2987 static inline int may_create(struct user_namespace
*mnt_userns
,
2988 struct inode
*dir
, struct dentry
*child
)
2990 audit_inode_child(dir
, child
, AUDIT_TYPE_CHILD_CREATE
);
2993 if (IS_DEADDIR(dir
))
2995 if (!fsuidgid_has_mapping(dir
->i_sb
, mnt_userns
))
2998 return inode_permission(mnt_userns
, dir
, MAY_WRITE
| MAY_EXEC
);
3002 * p1 and p2 should be directories on the same fs.
3004 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
3009 inode_lock_nested(p1
->d_inode
, I_MUTEX_PARENT
);
3013 mutex_lock(&p1
->d_sb
->s_vfs_rename_mutex
);
3015 p
= d_ancestor(p2
, p1
);
3017 inode_lock_nested(p2
->d_inode
, I_MUTEX_PARENT
);
3018 inode_lock_nested(p1
->d_inode
, I_MUTEX_CHILD
);
3022 p
= d_ancestor(p1
, p2
);
3024 inode_lock_nested(p1
->d_inode
, I_MUTEX_PARENT
);
3025 inode_lock_nested(p2
->d_inode
, I_MUTEX_CHILD
);
3029 inode_lock_nested(p1
->d_inode
, I_MUTEX_PARENT
);
3030 inode_lock_nested(p2
->d_inode
, I_MUTEX_PARENT2
);
3033 EXPORT_SYMBOL(lock_rename
);
3035 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
3037 inode_unlock(p1
->d_inode
);
3039 inode_unlock(p2
->d_inode
);
3040 mutex_unlock(&p1
->d_sb
->s_vfs_rename_mutex
);
3043 EXPORT_SYMBOL(unlock_rename
);
3046 * vfs_create - create new file
3047 * @mnt_userns: user namespace of the mount the inode was found from
3048 * @dir: inode of @dentry
3049 * @dentry: pointer to dentry of the base directory
3050 * @mode: mode of the new file
3051 * @want_excl: whether the file must not yet exist
3053 * Create a new file.
3055 * If the inode has been found through an idmapped mount the user namespace of
3056 * the vfsmount must be passed through @mnt_userns. This function will then take
3057 * care to map the inode according to @mnt_userns before checking permissions.
3058 * On non-idmapped mounts or if permission checking is to be performed on the
3059 * raw inode simply passs init_user_ns.
3061 int vfs_create(struct user_namespace
*mnt_userns
, struct inode
*dir
,
3062 struct dentry
*dentry
, umode_t mode
, bool want_excl
)
3064 int error
= may_create(mnt_userns
, dir
, dentry
);
3068 if (!dir
->i_op
->create
)
3069 return -EACCES
; /* shouldn't it be ENOSYS? */
3072 error
= security_inode_create(dir
, dentry
, mode
);
3075 error
= dir
->i_op
->create(mnt_userns
, dir
, dentry
, mode
, want_excl
);
3077 fsnotify_create(dir
, dentry
);
3080 EXPORT_SYMBOL(vfs_create
);
3082 int vfs_mkobj(struct dentry
*dentry
, umode_t mode
,
3083 int (*f
)(struct dentry
*, umode_t
, void *),
3086 struct inode
*dir
= dentry
->d_parent
->d_inode
;
3087 int error
= may_create(&init_user_ns
, dir
, dentry
);
3093 error
= security_inode_create(dir
, dentry
, mode
);
3096 error
= f(dentry
, mode
, arg
);
3098 fsnotify_create(dir
, dentry
);
3101 EXPORT_SYMBOL(vfs_mkobj
);
3103 bool may_open_dev(const struct path
*path
)
3105 return !(path
->mnt
->mnt_flags
& MNT_NODEV
) &&
3106 !(path
->mnt
->mnt_sb
->s_iflags
& SB_I_NODEV
);
3109 static int may_open(struct user_namespace
*mnt_userns
, const struct path
*path
,
3110 int acc_mode
, int flag
)
3112 struct dentry
*dentry
= path
->dentry
;
3113 struct inode
*inode
= dentry
->d_inode
;
3119 switch (inode
->i_mode
& S_IFMT
) {
3123 if (acc_mode
& MAY_WRITE
)
3125 if (acc_mode
& MAY_EXEC
)
3130 if (!may_open_dev(path
))
3135 if (acc_mode
& MAY_EXEC
)
3140 if ((acc_mode
& MAY_EXEC
) && path_noexec(path
))
3145 error
= inode_permission(mnt_userns
, inode
, MAY_OPEN
| acc_mode
);
3150 * An append-only file must be opened in append mode for writing.
3152 if (IS_APPEND(inode
)) {
3153 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
3159 /* O_NOATIME can only be set by the owner or superuser */
3160 if (flag
& O_NOATIME
&& !inode_owner_or_capable(mnt_userns
, inode
))
3166 static int handle_truncate(struct user_namespace
*mnt_userns
, struct file
*filp
)
3168 const struct path
*path
= &filp
->f_path
;
3169 struct inode
*inode
= path
->dentry
->d_inode
;
3170 int error
= get_write_access(inode
);
3174 error
= security_path_truncate(path
);
3176 error
= do_truncate(mnt_userns
, path
->dentry
, 0,
3177 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
3180 put_write_access(inode
);
3184 static inline int open_to_namei_flags(int flag
)
3186 if ((flag
& O_ACCMODE
) == 3)
3191 static int may_o_create(struct user_namespace
*mnt_userns
,
3192 const struct path
*dir
, struct dentry
*dentry
,
3195 int error
= security_path_mknod(dir
, dentry
, mode
, 0);
3199 if (!fsuidgid_has_mapping(dir
->dentry
->d_sb
, mnt_userns
))
3202 error
= inode_permission(mnt_userns
, dir
->dentry
->d_inode
,
3203 MAY_WRITE
| MAY_EXEC
);
3207 return security_inode_create(dir
->dentry
->d_inode
, dentry
, mode
);
3211 * Attempt to atomically look up, create and open a file from a negative
3214 * Returns 0 if successful. The file will have been created and attached to
3215 * @file by the filesystem calling finish_open().
3217 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3218 * be set. The caller will need to perform the open themselves. @path will
3219 * have been updated to point to the new dentry. This may be negative.
3221 * Returns an error code otherwise.
3223 static struct dentry
*atomic_open(struct nameidata
*nd
, struct dentry
*dentry
,
3225 int open_flag
, umode_t mode
)
3227 struct dentry
*const DENTRY_NOT_SET
= (void *) -1UL;
3228 struct inode
*dir
= nd
->path
.dentry
->d_inode
;
3231 if (nd
->flags
& LOOKUP_DIRECTORY
)
3232 open_flag
|= O_DIRECTORY
;
3234 file
->f_path
.dentry
= DENTRY_NOT_SET
;
3235 file
->f_path
.mnt
= nd
->path
.mnt
;
3236 error
= dir
->i_op
->atomic_open(dir
, dentry
, file
,
3237 open_to_namei_flags(open_flag
), mode
);
3238 d_lookup_done(dentry
);
3240 if (file
->f_mode
& FMODE_OPENED
) {
3241 if (unlikely(dentry
!= file
->f_path
.dentry
)) {
3243 dentry
= dget(file
->f_path
.dentry
);
3245 } else if (WARN_ON(file
->f_path
.dentry
== DENTRY_NOT_SET
)) {
3248 if (file
->f_path
.dentry
) {
3250 dentry
= file
->f_path
.dentry
;
3252 if (unlikely(d_is_negative(dentry
)))
3258 dentry
= ERR_PTR(error
);
3264 * Look up and maybe create and open the last component.
3266 * Must be called with parent locked (exclusive in O_CREAT case).
3268 * Returns 0 on success, that is, if
3269 * the file was successfully atomically created (if necessary) and opened, or
3270 * the file was not completely opened at this time, though lookups and
3271 * creations were performed.
3272 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3273 * In the latter case dentry returned in @path might be negative if O_CREAT
3274 * hadn't been specified.
3276 * An error code is returned on failure.
3278 static struct dentry
*lookup_open(struct nameidata
*nd
, struct file
*file
,
3279 const struct open_flags
*op
,
3282 struct user_namespace
*mnt_userns
;
3283 struct dentry
*dir
= nd
->path
.dentry
;
3284 struct inode
*dir_inode
= dir
->d_inode
;
3285 int open_flag
= op
->open_flag
;
3286 struct dentry
*dentry
;
3287 int error
, create_error
= 0;
3288 umode_t mode
= op
->mode
;
3289 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
3291 if (unlikely(IS_DEADDIR(dir_inode
)))
3292 return ERR_PTR(-ENOENT
);
3294 file
->f_mode
&= ~FMODE_CREATED
;
3295 dentry
= d_lookup(dir
, &nd
->last
);
3298 dentry
= d_alloc_parallel(dir
, &nd
->last
, &wq
);
3302 if (d_in_lookup(dentry
))
3305 error
= d_revalidate(dentry
, nd
->flags
);
3306 if (likely(error
> 0))
3310 d_invalidate(dentry
);
3314 if (dentry
->d_inode
) {
3315 /* Cached positive dentry: will open in f_op->open */
3320 * Checking write permission is tricky, bacuse we don't know if we are
3321 * going to actually need it: O_CREAT opens should work as long as the
3322 * file exists. But checking existence breaks atomicity. The trick is
3323 * to check access and if not granted clear O_CREAT from the flags.
3325 * Another problem is returing the "right" error value (e.g. for an
3326 * O_EXCL open we want to return EEXIST not EROFS).
3328 if (unlikely(!got_write
))
3329 open_flag
&= ~O_TRUNC
;
3330 mnt_userns
= mnt_user_ns(nd
->path
.mnt
);
3331 if (open_flag
& O_CREAT
) {
3332 if (open_flag
& O_EXCL
)
3333 open_flag
&= ~O_TRUNC
;
3334 if (!IS_POSIXACL(dir
->d_inode
))
3335 mode
&= ~current_umask();
3336 if (likely(got_write
))
3337 create_error
= may_o_create(mnt_userns
, &nd
->path
,
3340 create_error
= -EROFS
;
3343 open_flag
&= ~O_CREAT
;
3344 if (dir_inode
->i_op
->atomic_open
) {
3345 dentry
= atomic_open(nd
, dentry
, file
, open_flag
, mode
);
3346 if (unlikely(create_error
) && dentry
== ERR_PTR(-ENOENT
))
3347 dentry
= ERR_PTR(create_error
);
3351 if (d_in_lookup(dentry
)) {
3352 struct dentry
*res
= dir_inode
->i_op
->lookup(dir_inode
, dentry
,
3354 d_lookup_done(dentry
);
3355 if (unlikely(res
)) {
3357 error
= PTR_ERR(res
);
3365 /* Negative dentry, just create the file */
3366 if (!dentry
->d_inode
&& (open_flag
& O_CREAT
)) {
3367 file
->f_mode
|= FMODE_CREATED
;
3368 audit_inode_child(dir_inode
, dentry
, AUDIT_TYPE_CHILD_CREATE
);
3369 if (!dir_inode
->i_op
->create
) {
3374 error
= dir_inode
->i_op
->create(mnt_userns
, dir_inode
, dentry
,
3375 mode
, open_flag
& O_EXCL
);
3379 if (unlikely(create_error
) && !dentry
->d_inode
) {
3380 error
= create_error
;
3387 return ERR_PTR(error
);
3390 static const char *open_last_lookups(struct nameidata
*nd
,
3391 struct file
*file
, const struct open_flags
*op
)
3393 struct dentry
*dir
= nd
->path
.dentry
;
3394 int open_flag
= op
->open_flag
;
3395 bool got_write
= false;
3397 struct inode
*inode
;
3398 struct dentry
*dentry
;
3401 nd
->flags
|= op
->intent
;
3403 if (nd
->last_type
!= LAST_NORM
) {
3406 return handle_dots(nd
, nd
->last_type
);
3409 if (!(open_flag
& O_CREAT
)) {
3410 if (nd
->last
.name
[nd
->last
.len
])
3411 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
3412 /* we _can_ be in RCU mode here */
3413 dentry
= lookup_fast(nd
, &inode
, &seq
);
3415 return ERR_CAST(dentry
);
3419 BUG_ON(nd
->flags
& LOOKUP_RCU
);
3421 /* create side of things */
3422 if (nd
->flags
& LOOKUP_RCU
) {
3423 if (!try_to_unlazy(nd
))
3424 return ERR_PTR(-ECHILD
);
3426 audit_inode(nd
->name
, dir
, AUDIT_INODE_PARENT
);
3427 /* trailing slashes? */
3428 if (unlikely(nd
->last
.name
[nd
->last
.len
]))
3429 return ERR_PTR(-EISDIR
);
3432 if (open_flag
& (O_CREAT
| O_TRUNC
| O_WRONLY
| O_RDWR
)) {
3433 got_write
= !mnt_want_write(nd
->path
.mnt
);
3435 * do _not_ fail yet - we might not need that or fail with
3436 * a different error; let lookup_open() decide; we'll be
3437 * dropping this one anyway.
3440 if (open_flag
& O_CREAT
)
3441 inode_lock(dir
->d_inode
);
3443 inode_lock_shared(dir
->d_inode
);
3444 dentry
= lookup_open(nd
, file
, op
, got_write
);
3445 if (!IS_ERR(dentry
) && (file
->f_mode
& FMODE_CREATED
))
3446 fsnotify_create(dir
->d_inode
, dentry
);
3447 if (open_flag
& O_CREAT
)
3448 inode_unlock(dir
->d_inode
);
3450 inode_unlock_shared(dir
->d_inode
);
3453 mnt_drop_write(nd
->path
.mnt
);
3456 return ERR_CAST(dentry
);
3458 if (file
->f_mode
& (FMODE_OPENED
| FMODE_CREATED
)) {
3459 dput(nd
->path
.dentry
);
3460 nd
->path
.dentry
= dentry
;
3467 res
= step_into(nd
, WALK_TRAILING
, dentry
, inode
, seq
);
3469 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
3474 * Handle the last step of open()
3476 static int do_open(struct nameidata
*nd
,
3477 struct file
*file
, const struct open_flags
*op
)
3479 struct user_namespace
*mnt_userns
;
3480 int open_flag
= op
->open_flag
;
3485 if (!(file
->f_mode
& (FMODE_OPENED
| FMODE_CREATED
))) {
3486 error
= complete_walk(nd
);
3490 if (!(file
->f_mode
& FMODE_CREATED
))
3491 audit_inode(nd
->name
, nd
->path
.dentry
, 0);
3492 mnt_userns
= mnt_user_ns(nd
->path
.mnt
);
3493 if (open_flag
& O_CREAT
) {
3494 if ((open_flag
& O_EXCL
) && !(file
->f_mode
& FMODE_CREATED
))
3496 if (d_is_dir(nd
->path
.dentry
))
3498 error
= may_create_in_sticky(mnt_userns
, nd
,
3499 d_backing_inode(nd
->path
.dentry
));
3500 if (unlikely(error
))
3503 if ((nd
->flags
& LOOKUP_DIRECTORY
) && !d_can_lookup(nd
->path
.dentry
))
3506 do_truncate
= false;
3507 acc_mode
= op
->acc_mode
;
3508 if (file
->f_mode
& FMODE_CREATED
) {
3509 /* Don't check for write permission, don't truncate */
3510 open_flag
&= ~O_TRUNC
;
3512 } else if (d_is_reg(nd
->path
.dentry
) && open_flag
& O_TRUNC
) {
3513 error
= mnt_want_write(nd
->path
.mnt
);
3518 error
= may_open(mnt_userns
, &nd
->path
, acc_mode
, open_flag
);
3519 if (!error
&& !(file
->f_mode
& FMODE_OPENED
))
3520 error
= vfs_open(&nd
->path
, file
);
3522 error
= ima_file_check(file
, op
->acc_mode
);
3523 if (!error
&& do_truncate
)
3524 error
= handle_truncate(mnt_userns
, file
);
3525 if (unlikely(error
> 0)) {
3530 mnt_drop_write(nd
->path
.mnt
);
3535 * vfs_tmpfile - create tmpfile
3536 * @mnt_userns: user namespace of the mount the inode was found from
3537 * @dentry: pointer to dentry of the base directory
3538 * @mode: mode of the new tmpfile
3541 * Create a temporary file.
3543 * If the inode has been found through an idmapped mount the user namespace of
3544 * the vfsmount must be passed through @mnt_userns. This function will then take
3545 * care to map the inode according to @mnt_userns before checking permissions.
3546 * On non-idmapped mounts or if permission checking is to be performed on the
3547 * raw inode simply passs init_user_ns.
3549 struct dentry
*vfs_tmpfile(struct user_namespace
*mnt_userns
,
3550 struct dentry
*dentry
, umode_t mode
, int open_flag
)
3552 struct dentry
*child
= NULL
;
3553 struct inode
*dir
= dentry
->d_inode
;
3554 struct inode
*inode
;
3557 /* we want directory to be writable */
3558 error
= inode_permission(mnt_userns
, dir
, MAY_WRITE
| MAY_EXEC
);
3561 error
= -EOPNOTSUPP
;
3562 if (!dir
->i_op
->tmpfile
)
3565 child
= d_alloc(dentry
, &slash_name
);
3566 if (unlikely(!child
))
3568 error
= dir
->i_op
->tmpfile(mnt_userns
, dir
, child
, mode
);
3572 inode
= child
->d_inode
;
3573 if (unlikely(!inode
))
3575 if (!(open_flag
& O_EXCL
)) {
3576 spin_lock(&inode
->i_lock
);
3577 inode
->i_state
|= I_LINKABLE
;
3578 spin_unlock(&inode
->i_lock
);
3580 ima_post_create_tmpfile(mnt_userns
, inode
);
3585 return ERR_PTR(error
);
3587 EXPORT_SYMBOL(vfs_tmpfile
);
3589 static int do_tmpfile(struct nameidata
*nd
, unsigned flags
,
3590 const struct open_flags
*op
,
3593 struct user_namespace
*mnt_userns
;
3594 struct dentry
*child
;
3596 int error
= path_lookupat(nd
, flags
| LOOKUP_DIRECTORY
, &path
);
3597 if (unlikely(error
))
3599 error
= mnt_want_write(path
.mnt
);
3600 if (unlikely(error
))
3602 mnt_userns
= mnt_user_ns(path
.mnt
);
3603 child
= vfs_tmpfile(mnt_userns
, path
.dentry
, op
->mode
, op
->open_flag
);
3604 error
= PTR_ERR(child
);
3608 path
.dentry
= child
;
3609 audit_inode(nd
->name
, child
, 0);
3610 /* Don't check for other permissions, the inode was just created */
3611 error
= may_open(mnt_userns
, &path
, 0, op
->open_flag
);
3613 error
= vfs_open(&path
, file
);
3615 mnt_drop_write(path
.mnt
);
3621 static int do_o_path(struct nameidata
*nd
, unsigned flags
, struct file
*file
)
3624 int error
= path_lookupat(nd
, flags
, &path
);
3626 audit_inode(nd
->name
, path
.dentry
, 0);
3627 error
= vfs_open(&path
, file
);
3633 static struct file
*path_openat(struct nameidata
*nd
,
3634 const struct open_flags
*op
, unsigned flags
)
3639 file
= alloc_empty_file(op
->open_flag
, current_cred());
3643 if (unlikely(file
->f_flags
& __O_TMPFILE
)) {
3644 error
= do_tmpfile(nd
, flags
, op
, file
);
3645 } else if (unlikely(file
->f_flags
& O_PATH
)) {
3646 error
= do_o_path(nd
, flags
, file
);
3648 const char *s
= path_init(nd
, flags
);
3649 while (!(error
= link_path_walk(s
, nd
)) &&
3650 (s
= open_last_lookups(nd
, file
, op
)) != NULL
)
3653 error
= do_open(nd
, file
, op
);
3656 if (likely(!error
)) {
3657 if (likely(file
->f_mode
& FMODE_OPENED
))
3663 if (error
== -EOPENSTALE
) {
3664 if (flags
& LOOKUP_RCU
)
3669 return ERR_PTR(error
);
3672 struct file
*do_filp_open(int dfd
, struct filename
*pathname
,
3673 const struct open_flags
*op
)
3675 struct nameidata nd
;
3676 int flags
= op
->lookup_flags
;
3679 set_nameidata(&nd
, dfd
, pathname
, NULL
);
3680 filp
= path_openat(&nd
, op
, flags
| LOOKUP_RCU
);
3681 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
3682 filp
= path_openat(&nd
, op
, flags
);
3683 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
3684 filp
= path_openat(&nd
, op
, flags
| LOOKUP_REVAL
);
3685 restore_nameidata();
3689 struct file
*do_file_open_root(const struct path
*root
,
3690 const char *name
, const struct open_flags
*op
)
3692 struct nameidata nd
;
3694 struct filename
*filename
;
3695 int flags
= op
->lookup_flags
;
3697 if (d_is_symlink(root
->dentry
) && op
->intent
& LOOKUP_OPEN
)
3698 return ERR_PTR(-ELOOP
);
3700 filename
= getname_kernel(name
);
3701 if (IS_ERR(filename
))
3702 return ERR_CAST(filename
);
3704 set_nameidata(&nd
, -1, filename
, root
);
3705 file
= path_openat(&nd
, op
, flags
| LOOKUP_RCU
);
3706 if (unlikely(file
== ERR_PTR(-ECHILD
)))
3707 file
= path_openat(&nd
, op
, flags
);
3708 if (unlikely(file
== ERR_PTR(-ESTALE
)))
3709 file
= path_openat(&nd
, op
, flags
| LOOKUP_REVAL
);
3710 restore_nameidata();
3715 static struct dentry
*filename_create(int dfd
, struct filename
*name
,
3716 struct path
*path
, unsigned int lookup_flags
)
3718 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
3720 bool want_dir
= lookup_flags
& LOOKUP_DIRECTORY
;
3721 unsigned int reval_flag
= lookup_flags
& LOOKUP_REVAL
;
3722 unsigned int create_flags
= LOOKUP_CREATE
| LOOKUP_EXCL
;
3727 error
= filename_parentat(dfd
, name
, reval_flag
, path
, &last
, &type
);
3729 return ERR_PTR(error
);
3732 * Yucky last component or no last component at all?
3733 * (foo/., foo/.., /////)
3735 if (unlikely(type
!= LAST_NORM
))
3738 /* don't fail immediately if it's r/o, at least try to report other errors */
3739 err2
= mnt_want_write(path
->mnt
);
3741 * Do the final lookup. Suppress 'create' if there is a trailing
3742 * '/', and a directory wasn't requested.
3744 if (last
.name
[last
.len
] && !want_dir
)
3746 inode_lock_nested(path
->dentry
->d_inode
, I_MUTEX_PARENT
);
3747 dentry
= __lookup_hash(&last
, path
->dentry
, reval_flag
| create_flags
);
3752 if (d_is_positive(dentry
))
3756 * Special case - lookup gave negative, but... we had foo/bar/
3757 * From the vfs_mknod() POV we just have a negative dentry -
3758 * all is fine. Let's be bastards - you had / on the end, you've
3759 * been asking for (non-existent) directory. -ENOENT for you.
3761 if (unlikely(!create_flags
)) {
3765 if (unlikely(err2
)) {
3772 dentry
= ERR_PTR(error
);
3774 inode_unlock(path
->dentry
->d_inode
);
3776 mnt_drop_write(path
->mnt
);
3782 struct dentry
*kern_path_create(int dfd
, const char *pathname
,
3783 struct path
*path
, unsigned int lookup_flags
)
3785 struct filename
*filename
= getname_kernel(pathname
);
3786 struct dentry
*res
= filename_create(dfd
, filename
, path
, lookup_flags
);
3791 EXPORT_SYMBOL(kern_path_create
);
3793 void done_path_create(struct path
*path
, struct dentry
*dentry
)
3796 inode_unlock(path
->dentry
->d_inode
);
3797 mnt_drop_write(path
->mnt
);
3800 EXPORT_SYMBOL(done_path_create
);
3802 inline struct dentry
*user_path_create(int dfd
, const char __user
*pathname
,
3803 struct path
*path
, unsigned int lookup_flags
)
3805 struct filename
*filename
= getname(pathname
);
3806 struct dentry
*res
= filename_create(dfd
, filename
, path
, lookup_flags
);
3811 EXPORT_SYMBOL(user_path_create
);
3814 * vfs_mknod - create device node or file
3815 * @mnt_userns: user namespace of the mount the inode was found from
3816 * @dir: inode of @dentry
3817 * @dentry: pointer to dentry of the base directory
3818 * @mode: mode of the new device node or file
3819 * @dev: device number of device to create
3821 * Create a device node or file.
3823 * If the inode has been found through an idmapped mount the user namespace of
3824 * the vfsmount must be passed through @mnt_userns. This function will then take
3825 * care to map the inode according to @mnt_userns before checking permissions.
3826 * On non-idmapped mounts or if permission checking is to be performed on the
3827 * raw inode simply passs init_user_ns.
3829 int vfs_mknod(struct user_namespace
*mnt_userns
, struct inode
*dir
,
3830 struct dentry
*dentry
, umode_t mode
, dev_t dev
)
3832 bool is_whiteout
= S_ISCHR(mode
) && dev
== WHITEOUT_DEV
;
3833 int error
= may_create(mnt_userns
, dir
, dentry
);
3838 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !is_whiteout
&&
3839 !capable(CAP_MKNOD
))
3842 if (!dir
->i_op
->mknod
)
3845 error
= devcgroup_inode_mknod(mode
, dev
);
3849 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
3853 error
= dir
->i_op
->mknod(mnt_userns
, dir
, dentry
, mode
, dev
);
3855 fsnotify_create(dir
, dentry
);
3858 EXPORT_SYMBOL(vfs_mknod
);
3860 static int may_mknod(umode_t mode
)
3862 switch (mode
& S_IFMT
) {
3868 case 0: /* zero mode translates to S_IFREG */
3877 static int do_mknodat(int dfd
, struct filename
*name
, umode_t mode
,
3880 struct user_namespace
*mnt_userns
;
3881 struct dentry
*dentry
;
3884 unsigned int lookup_flags
= 0;
3886 error
= may_mknod(mode
);
3890 dentry
= filename_create(dfd
, name
, &path
, lookup_flags
);
3891 error
= PTR_ERR(dentry
);
3895 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3896 mode
&= ~current_umask();
3897 error
= security_path_mknod(&path
, dentry
, mode
, dev
);
3901 mnt_userns
= mnt_user_ns(path
.mnt
);
3902 switch (mode
& S_IFMT
) {
3903 case 0: case S_IFREG
:
3904 error
= vfs_create(mnt_userns
, path
.dentry
->d_inode
,
3905 dentry
, mode
, true);
3907 ima_post_path_mknod(mnt_userns
, dentry
);
3909 case S_IFCHR
: case S_IFBLK
:
3910 error
= vfs_mknod(mnt_userns
, path
.dentry
->d_inode
,
3911 dentry
, mode
, new_decode_dev(dev
));
3913 case S_IFIFO
: case S_IFSOCK
:
3914 error
= vfs_mknod(mnt_userns
, path
.dentry
->d_inode
,
3919 done_path_create(&path
, dentry
);
3920 if (retry_estale(error
, lookup_flags
)) {
3921 lookup_flags
|= LOOKUP_REVAL
;
3929 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, umode_t
, mode
,
3932 return do_mknodat(dfd
, getname(filename
), mode
, dev
);
3935 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, umode_t
, mode
, unsigned, dev
)
3937 return do_mknodat(AT_FDCWD
, getname(filename
), mode
, dev
);
3941 * vfs_mkdir - create directory
3942 * @mnt_userns: user namespace of the mount the inode was found from
3943 * @dir: inode of @dentry
3944 * @dentry: pointer to dentry of the base directory
3945 * @mode: mode of the new directory
3947 * Create a directory.
3949 * If the inode has been found through an idmapped mount the user namespace of
3950 * the vfsmount must be passed through @mnt_userns. This function will then take
3951 * care to map the inode according to @mnt_userns before checking permissions.
3952 * On non-idmapped mounts or if permission checking is to be performed on the
3953 * raw inode simply passs init_user_ns.
3955 int vfs_mkdir(struct user_namespace
*mnt_userns
, struct inode
*dir
,
3956 struct dentry
*dentry
, umode_t mode
)
3958 int error
= may_create(mnt_userns
, dir
, dentry
);
3959 unsigned max_links
= dir
->i_sb
->s_max_links
;
3964 if (!dir
->i_op
->mkdir
)
3967 mode
&= (S_IRWXUGO
|S_ISVTX
);
3968 error
= security_inode_mkdir(dir
, dentry
, mode
);
3972 if (max_links
&& dir
->i_nlink
>= max_links
)
3975 error
= dir
->i_op
->mkdir(mnt_userns
, dir
, dentry
, mode
);
3977 fsnotify_mkdir(dir
, dentry
);
3980 EXPORT_SYMBOL(vfs_mkdir
);
3982 int do_mkdirat(int dfd
, struct filename
*name
, umode_t mode
)
3984 struct dentry
*dentry
;
3987 unsigned int lookup_flags
= LOOKUP_DIRECTORY
;
3990 dentry
= filename_create(dfd
, name
, &path
, lookup_flags
);
3991 error
= PTR_ERR(dentry
);
3995 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3996 mode
&= ~current_umask();
3997 error
= security_path_mkdir(&path
, dentry
, mode
);
3999 struct user_namespace
*mnt_userns
;
4000 mnt_userns
= mnt_user_ns(path
.mnt
);
4001 error
= vfs_mkdir(mnt_userns
, path
.dentry
->d_inode
, dentry
,
4004 done_path_create(&path
, dentry
);
4005 if (retry_estale(error
, lookup_flags
)) {
4006 lookup_flags
|= LOOKUP_REVAL
;
4014 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, umode_t
, mode
)
4016 return do_mkdirat(dfd
, getname(pathname
), mode
);
4019 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, umode_t
, mode
)
4021 return do_mkdirat(AT_FDCWD
, getname(pathname
), mode
);
4025 * vfs_rmdir - remove directory
4026 * @mnt_userns: user namespace of the mount the inode was found from
4027 * @dir: inode of @dentry
4028 * @dentry: pointer to dentry of the base directory
4030 * Remove a directory.
4032 * If the inode has been found through an idmapped mount the user namespace of
4033 * the vfsmount must be passed through @mnt_userns. This function will then take
4034 * care to map the inode according to @mnt_userns before checking permissions.
4035 * On non-idmapped mounts or if permission checking is to be performed on the
4036 * raw inode simply passs init_user_ns.
4038 int vfs_rmdir(struct user_namespace
*mnt_userns
, struct inode
*dir
,
4039 struct dentry
*dentry
)
4041 int error
= may_delete(mnt_userns
, dir
, dentry
, 1);
4046 if (!dir
->i_op
->rmdir
)
4050 inode_lock(dentry
->d_inode
);
4053 if (is_local_mountpoint(dentry
) ||
4054 (dentry
->d_inode
->i_flags
& S_KERNEL_FILE
))
4057 error
= security_inode_rmdir(dir
, dentry
);
4061 error
= dir
->i_op
->rmdir(dir
, dentry
);
4065 shrink_dcache_parent(dentry
);
4066 dentry
->d_inode
->i_flags
|= S_DEAD
;
4068 detach_mounts(dentry
);
4071 inode_unlock(dentry
->d_inode
);
4074 d_delete_notify(dir
, dentry
);
4077 EXPORT_SYMBOL(vfs_rmdir
);
4079 int do_rmdir(int dfd
, struct filename
*name
)
4081 struct user_namespace
*mnt_userns
;
4083 struct dentry
*dentry
;
4087 unsigned int lookup_flags
= 0;
4089 error
= filename_parentat(dfd
, name
, lookup_flags
, &path
, &last
, &type
);
4105 error
= mnt_want_write(path
.mnt
);
4109 inode_lock_nested(path
.dentry
->d_inode
, I_MUTEX_PARENT
);
4110 dentry
= __lookup_hash(&last
, path
.dentry
, lookup_flags
);
4111 error
= PTR_ERR(dentry
);
4114 if (!dentry
->d_inode
) {
4118 error
= security_path_rmdir(&path
, dentry
);
4121 mnt_userns
= mnt_user_ns(path
.mnt
);
4122 error
= vfs_rmdir(mnt_userns
, path
.dentry
->d_inode
, dentry
);
4126 inode_unlock(path
.dentry
->d_inode
);
4127 mnt_drop_write(path
.mnt
);
4130 if (retry_estale(error
, lookup_flags
)) {
4131 lookup_flags
|= LOOKUP_REVAL
;
4139 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
4141 return do_rmdir(AT_FDCWD
, getname(pathname
));
4145 * vfs_unlink - unlink a filesystem object
4146 * @mnt_userns: user namespace of the mount the inode was found from
4147 * @dir: parent directory
4149 * @delegated_inode: returns victim inode, if the inode is delegated.
4151 * The caller must hold dir->i_mutex.
4153 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
4154 * return a reference to the inode in delegated_inode. The caller
4155 * should then break the delegation on that inode and retry. Because
4156 * breaking a delegation may take a long time, the caller should drop
4157 * dir->i_mutex before doing so.
4159 * Alternatively, a caller may pass NULL for delegated_inode. This may
4160 * be appropriate for callers that expect the underlying filesystem not
4161 * to be NFS exported.
4163 * If the inode has been found through an idmapped mount the user namespace of
4164 * the vfsmount must be passed through @mnt_userns. This function will then take
4165 * care to map the inode according to @mnt_userns before checking permissions.
4166 * On non-idmapped mounts or if permission checking is to be performed on the
4167 * raw inode simply passs init_user_ns.
4169 int vfs_unlink(struct user_namespace
*mnt_userns
, struct inode
*dir
,
4170 struct dentry
*dentry
, struct inode
**delegated_inode
)
4172 struct inode
*target
= dentry
->d_inode
;
4173 int error
= may_delete(mnt_userns
, dir
, dentry
, 0);
4178 if (!dir
->i_op
->unlink
)
4182 if (IS_SWAPFILE(target
))
4184 else if (is_local_mountpoint(dentry
))
4187 error
= security_inode_unlink(dir
, dentry
);
4189 error
= try_break_deleg(target
, delegated_inode
);
4192 error
= dir
->i_op
->unlink(dir
, dentry
);
4195 detach_mounts(dentry
);
4200 inode_unlock(target
);
4202 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4203 if (!error
&& dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
4204 fsnotify_unlink(dir
, dentry
);
4205 } else if (!error
) {
4206 fsnotify_link_count(target
);
4207 d_delete_notify(dir
, dentry
);
4212 EXPORT_SYMBOL(vfs_unlink
);
4215 * Make sure that the actual truncation of the file will occur outside its
4216 * directory's i_mutex. Truncate can take a long time if there is a lot of
4217 * writeout happening, and we don't want to prevent access to the directory
4218 * while waiting on the I/O.
4220 int do_unlinkat(int dfd
, struct filename
*name
)
4223 struct dentry
*dentry
;
4227 struct inode
*inode
= NULL
;
4228 struct inode
*delegated_inode
= NULL
;
4229 unsigned int lookup_flags
= 0;
4231 error
= filename_parentat(dfd
, name
, lookup_flags
, &path
, &last
, &type
);
4236 if (type
!= LAST_NORM
)
4239 error
= mnt_want_write(path
.mnt
);
4243 inode_lock_nested(path
.dentry
->d_inode
, I_MUTEX_PARENT
);
4244 dentry
= __lookup_hash(&last
, path
.dentry
, lookup_flags
);
4245 error
= PTR_ERR(dentry
);
4246 if (!IS_ERR(dentry
)) {
4247 struct user_namespace
*mnt_userns
;
4249 /* Why not before? Because we want correct error value */
4250 if (last
.name
[last
.len
])
4252 inode
= dentry
->d_inode
;
4253 if (d_is_negative(dentry
))
4256 error
= security_path_unlink(&path
, dentry
);
4259 mnt_userns
= mnt_user_ns(path
.mnt
);
4260 error
= vfs_unlink(mnt_userns
, path
.dentry
->d_inode
, dentry
,
4265 inode_unlock(path
.dentry
->d_inode
);
4267 iput(inode
); /* truncate the inode here */
4269 if (delegated_inode
) {
4270 error
= break_deleg_wait(&delegated_inode
);
4274 mnt_drop_write(path
.mnt
);
4277 if (retry_estale(error
, lookup_flags
)) {
4278 lookup_flags
|= LOOKUP_REVAL
;
4287 if (d_is_negative(dentry
))
4289 else if (d_is_dir(dentry
))
4296 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
4298 if ((flag
& ~AT_REMOVEDIR
) != 0)
4301 if (flag
& AT_REMOVEDIR
)
4302 return do_rmdir(dfd
, getname(pathname
));
4303 return do_unlinkat(dfd
, getname(pathname
));
4306 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
4308 return do_unlinkat(AT_FDCWD
, getname(pathname
));
4312 * vfs_symlink - create symlink
4313 * @mnt_userns: user namespace of the mount the inode was found from
4314 * @dir: inode of @dentry
4315 * @dentry: pointer to dentry of the base directory
4316 * @oldname: name of the file to link to
4320 * If the inode has been found through an idmapped mount the user namespace of
4321 * the vfsmount must be passed through @mnt_userns. This function will then take
4322 * care to map the inode according to @mnt_userns before checking permissions.
4323 * On non-idmapped mounts or if permission checking is to be performed on the
4324 * raw inode simply passs init_user_ns.
4326 int vfs_symlink(struct user_namespace
*mnt_userns
, struct inode
*dir
,
4327 struct dentry
*dentry
, const char *oldname
)
4329 int error
= may_create(mnt_userns
, dir
, dentry
);
4334 if (!dir
->i_op
->symlink
)
4337 error
= security_inode_symlink(dir
, dentry
, oldname
);
4341 error
= dir
->i_op
->symlink(mnt_userns
, dir
, dentry
, oldname
);
4343 fsnotify_create(dir
, dentry
);
4346 EXPORT_SYMBOL(vfs_symlink
);
4348 int do_symlinkat(struct filename
*from
, int newdfd
, struct filename
*to
)
4351 struct dentry
*dentry
;
4353 unsigned int lookup_flags
= 0;
4356 error
= PTR_ERR(from
);
4360 dentry
= filename_create(newdfd
, to
, &path
, lookup_flags
);
4361 error
= PTR_ERR(dentry
);
4365 error
= security_path_symlink(&path
, dentry
, from
->name
);
4367 struct user_namespace
*mnt_userns
;
4369 mnt_userns
= mnt_user_ns(path
.mnt
);
4370 error
= vfs_symlink(mnt_userns
, path
.dentry
->d_inode
, dentry
,
4373 done_path_create(&path
, dentry
);
4374 if (retry_estale(error
, lookup_flags
)) {
4375 lookup_flags
|= LOOKUP_REVAL
;
4384 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
4385 int, newdfd
, const char __user
*, newname
)
4387 return do_symlinkat(getname(oldname
), newdfd
, getname(newname
));
4390 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
4392 return do_symlinkat(getname(oldname
), AT_FDCWD
, getname(newname
));
4396 * vfs_link - create a new link
4397 * @old_dentry: object to be linked
4398 * @mnt_userns: the user namespace of the mount
4400 * @new_dentry: where to create the new link
4401 * @delegated_inode: returns inode needing a delegation break
4403 * The caller must hold dir->i_mutex
4405 * If vfs_link discovers a delegation on the to-be-linked file in need
4406 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4407 * inode in delegated_inode. The caller should then break the delegation
4408 * and retry. Because breaking a delegation may take a long time, the
4409 * caller should drop the i_mutex before doing so.
4411 * Alternatively, a caller may pass NULL for delegated_inode. This may
4412 * be appropriate for callers that expect the underlying filesystem not
4413 * to be NFS exported.
4415 * If the inode has been found through an idmapped mount the user namespace of
4416 * the vfsmount must be passed through @mnt_userns. This function will then take
4417 * care to map the inode according to @mnt_userns before checking permissions.
4418 * On non-idmapped mounts or if permission checking is to be performed on the
4419 * raw inode simply passs init_user_ns.
4421 int vfs_link(struct dentry
*old_dentry
, struct user_namespace
*mnt_userns
,
4422 struct inode
*dir
, struct dentry
*new_dentry
,
4423 struct inode
**delegated_inode
)
4425 struct inode
*inode
= old_dentry
->d_inode
;
4426 unsigned max_links
= dir
->i_sb
->s_max_links
;
4432 error
= may_create(mnt_userns
, dir
, new_dentry
);
4436 if (dir
->i_sb
!= inode
->i_sb
)
4440 * A link to an append-only or immutable file cannot be created.
4442 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
4445 * Updating the link count will likely cause i_uid and i_gid to
4446 * be writen back improperly if their true value is unknown to
4449 if (HAS_UNMAPPED_ID(mnt_userns
, inode
))
4451 if (!dir
->i_op
->link
)
4453 if (S_ISDIR(inode
->i_mode
))
4456 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
4461 /* Make sure we don't allow creating hardlink to an unlinked file */
4462 if (inode
->i_nlink
== 0 && !(inode
->i_state
& I_LINKABLE
))
4464 else if (max_links
&& inode
->i_nlink
>= max_links
)
4467 error
= try_break_deleg(inode
, delegated_inode
);
4469 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
4472 if (!error
&& (inode
->i_state
& I_LINKABLE
)) {
4473 spin_lock(&inode
->i_lock
);
4474 inode
->i_state
&= ~I_LINKABLE
;
4475 spin_unlock(&inode
->i_lock
);
4477 inode_unlock(inode
);
4479 fsnotify_link(dir
, inode
, new_dentry
);
4482 EXPORT_SYMBOL(vfs_link
);
4485 * Hardlinks are often used in delicate situations. We avoid
4486 * security-related surprises by not following symlinks on the
4489 * We don't follow them on the oldname either to be compatible
4490 * with linux 2.0, and to avoid hard-linking to directories
4491 * and other special files. --ADM
4493 int do_linkat(int olddfd
, struct filename
*old
, int newdfd
,
4494 struct filename
*new, int flags
)
4496 struct user_namespace
*mnt_userns
;
4497 struct dentry
*new_dentry
;
4498 struct path old_path
, new_path
;
4499 struct inode
*delegated_inode
= NULL
;
4503 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0) {
4508 * To use null names we require CAP_DAC_READ_SEARCH
4509 * This ensures that not everyone will be able to create
4510 * handlink using the passed filedescriptor.
4512 if (flags
& AT_EMPTY_PATH
&& !capable(CAP_DAC_READ_SEARCH
)) {
4517 if (flags
& AT_SYMLINK_FOLLOW
)
4518 how
|= LOOKUP_FOLLOW
;
4520 error
= filename_lookup(olddfd
, old
, how
, &old_path
, NULL
);
4524 new_dentry
= filename_create(newdfd
, new, &new_path
,
4525 (how
& LOOKUP_REVAL
));
4526 error
= PTR_ERR(new_dentry
);
4527 if (IS_ERR(new_dentry
))
4531 if (old_path
.mnt
!= new_path
.mnt
)
4533 mnt_userns
= mnt_user_ns(new_path
.mnt
);
4534 error
= may_linkat(mnt_userns
, &old_path
);
4535 if (unlikely(error
))
4537 error
= security_path_link(old_path
.dentry
, &new_path
, new_dentry
);
4540 error
= vfs_link(old_path
.dentry
, mnt_userns
, new_path
.dentry
->d_inode
,
4541 new_dentry
, &delegated_inode
);
4543 done_path_create(&new_path
, new_dentry
);
4544 if (delegated_inode
) {
4545 error
= break_deleg_wait(&delegated_inode
);
4547 path_put(&old_path
);
4551 if (retry_estale(error
, how
)) {
4552 path_put(&old_path
);
4553 how
|= LOOKUP_REVAL
;
4557 path_put(&old_path
);
4565 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
4566 int, newdfd
, const char __user
*, newname
, int, flags
)
4568 return do_linkat(olddfd
, getname_uflags(oldname
, flags
),
4569 newdfd
, getname(newname
), flags
);
4572 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
4574 return do_linkat(AT_FDCWD
, getname(oldname
), AT_FDCWD
, getname(newname
), 0);
4578 * vfs_rename - rename a filesystem object
4579 * @rd: pointer to &struct renamedata info
4581 * The caller must hold multiple mutexes--see lock_rename()).
4583 * If vfs_rename discovers a delegation in need of breaking at either
4584 * the source or destination, it will return -EWOULDBLOCK and return a
4585 * reference to the inode in delegated_inode. The caller should then
4586 * break the delegation and retry. Because breaking a delegation may
4587 * take a long time, the caller should drop all locks before doing
4590 * Alternatively, a caller may pass NULL for delegated_inode. This may
4591 * be appropriate for callers that expect the underlying filesystem not
4592 * to be NFS exported.
4594 * The worst of all namespace operations - renaming directory. "Perverted"
4595 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4598 * a) we can get into loop creation.
4599 * b) race potential - two innocent renames can create a loop together.
4600 * That's where 4.4 screws up. Current fix: serialization on
4601 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4603 * c) we have to lock _four_ objects - parents and victim (if it exists),
4604 * and source (if it is not a directory).
4605 * And that - after we got ->i_mutex on parents (until then we don't know
4606 * whether the target exists). Solution: try to be smart with locking
4607 * order for inodes. We rely on the fact that tree topology may change
4608 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4609 * move will be locked. Thus we can rank directories by the tree
4610 * (ancestors first) and rank all non-directories after them.
4611 * That works since everybody except rename does "lock parent, lookup,
4612 * lock child" and rename is under ->s_vfs_rename_mutex.
4613 * HOWEVER, it relies on the assumption that any object with ->lookup()
4614 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4615 * we'd better make sure that there's no link(2) for them.
4616 * d) conversion from fhandle to dentry may come in the wrong moment - when
4617 * we are removing the target. Solution: we will have to grab ->i_mutex
4618 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4619 * ->i_mutex on parents, which works but leads to some truly excessive
4622 int vfs_rename(struct renamedata
*rd
)
4625 struct inode
*old_dir
= rd
->old_dir
, *new_dir
= rd
->new_dir
;
4626 struct dentry
*old_dentry
= rd
->old_dentry
;
4627 struct dentry
*new_dentry
= rd
->new_dentry
;
4628 struct inode
**delegated_inode
= rd
->delegated_inode
;
4629 unsigned int flags
= rd
->flags
;
4630 bool is_dir
= d_is_dir(old_dentry
);
4631 struct inode
*source
= old_dentry
->d_inode
;
4632 struct inode
*target
= new_dentry
->d_inode
;
4633 bool new_is_dir
= false;
4634 unsigned max_links
= new_dir
->i_sb
->s_max_links
;
4635 struct name_snapshot old_name
;
4637 if (source
== target
)
4640 error
= may_delete(rd
->old_mnt_userns
, old_dir
, old_dentry
, is_dir
);
4645 error
= may_create(rd
->new_mnt_userns
, new_dir
, new_dentry
);
4647 new_is_dir
= d_is_dir(new_dentry
);
4649 if (!(flags
& RENAME_EXCHANGE
))
4650 error
= may_delete(rd
->new_mnt_userns
, new_dir
,
4651 new_dentry
, is_dir
);
4653 error
= may_delete(rd
->new_mnt_userns
, new_dir
,
4654 new_dentry
, new_is_dir
);
4659 if (!old_dir
->i_op
->rename
)
4663 * If we are going to change the parent - check write permissions,
4664 * we'll need to flip '..'.
4666 if (new_dir
!= old_dir
) {
4668 error
= inode_permission(rd
->old_mnt_userns
, source
,
4673 if ((flags
& RENAME_EXCHANGE
) && new_is_dir
) {
4674 error
= inode_permission(rd
->new_mnt_userns
, target
,
4681 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
4686 take_dentry_name_snapshot(&old_name
, old_dentry
);
4688 if (!is_dir
|| (flags
& RENAME_EXCHANGE
))
4689 lock_two_nondirectories(source
, target
);
4694 if (IS_SWAPFILE(source
) || (target
&& IS_SWAPFILE(target
)))
4698 if (is_local_mountpoint(old_dentry
) || is_local_mountpoint(new_dentry
))
4701 if (max_links
&& new_dir
!= old_dir
) {
4703 if (is_dir
&& !new_is_dir
&& new_dir
->i_nlink
>= max_links
)
4705 if ((flags
& RENAME_EXCHANGE
) && !is_dir
&& new_is_dir
&&
4706 old_dir
->i_nlink
>= max_links
)
4710 error
= try_break_deleg(source
, delegated_inode
);
4714 if (target
&& !new_is_dir
) {
4715 error
= try_break_deleg(target
, delegated_inode
);
4719 error
= old_dir
->i_op
->rename(rd
->new_mnt_userns
, old_dir
, old_dentry
,
4720 new_dir
, new_dentry
, flags
);
4724 if (!(flags
& RENAME_EXCHANGE
) && target
) {
4726 shrink_dcache_parent(new_dentry
);
4727 target
->i_flags
|= S_DEAD
;
4729 dont_mount(new_dentry
);
4730 detach_mounts(new_dentry
);
4732 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
)) {
4733 if (!(flags
& RENAME_EXCHANGE
))
4734 d_move(old_dentry
, new_dentry
);
4736 d_exchange(old_dentry
, new_dentry
);
4739 if (!is_dir
|| (flags
& RENAME_EXCHANGE
))
4740 unlock_two_nondirectories(source
, target
);
4742 inode_unlock(target
);
4745 fsnotify_move(old_dir
, new_dir
, &old_name
.name
, is_dir
,
4746 !(flags
& RENAME_EXCHANGE
) ? target
: NULL
, old_dentry
);
4747 if (flags
& RENAME_EXCHANGE
) {
4748 fsnotify_move(new_dir
, old_dir
, &old_dentry
->d_name
,
4749 new_is_dir
, NULL
, new_dentry
);
4752 release_dentry_name_snapshot(&old_name
);
4756 EXPORT_SYMBOL(vfs_rename
);
4758 int do_renameat2(int olddfd
, struct filename
*from
, int newdfd
,
4759 struct filename
*to
, unsigned int flags
)
4761 struct renamedata rd
;
4762 struct dentry
*old_dentry
, *new_dentry
;
4763 struct dentry
*trap
;
4764 struct path old_path
, new_path
;
4765 struct qstr old_last
, new_last
;
4766 int old_type
, new_type
;
4767 struct inode
*delegated_inode
= NULL
;
4768 unsigned int lookup_flags
= 0, target_flags
= LOOKUP_RENAME_TARGET
;
4769 bool should_retry
= false;
4770 int error
= -EINVAL
;
4772 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
4775 if ((flags
& (RENAME_NOREPLACE
| RENAME_WHITEOUT
)) &&
4776 (flags
& RENAME_EXCHANGE
))
4779 if (flags
& RENAME_EXCHANGE
)
4783 error
= filename_parentat(olddfd
, from
, lookup_flags
, &old_path
,
4784 &old_last
, &old_type
);
4788 error
= filename_parentat(newdfd
, to
, lookup_flags
, &new_path
, &new_last
,
4794 if (old_path
.mnt
!= new_path
.mnt
)
4798 if (old_type
!= LAST_NORM
)
4801 if (flags
& RENAME_NOREPLACE
)
4803 if (new_type
!= LAST_NORM
)
4806 error
= mnt_want_write(old_path
.mnt
);
4811 trap
= lock_rename(new_path
.dentry
, old_path
.dentry
);
4813 old_dentry
= __lookup_hash(&old_last
, old_path
.dentry
, lookup_flags
);
4814 error
= PTR_ERR(old_dentry
);
4815 if (IS_ERR(old_dentry
))
4817 /* source must exist */
4819 if (d_is_negative(old_dentry
))
4821 new_dentry
= __lookup_hash(&new_last
, new_path
.dentry
, lookup_flags
| target_flags
);
4822 error
= PTR_ERR(new_dentry
);
4823 if (IS_ERR(new_dentry
))
4826 if ((flags
& RENAME_NOREPLACE
) && d_is_positive(new_dentry
))
4828 if (flags
& RENAME_EXCHANGE
) {
4830 if (d_is_negative(new_dentry
))
4833 if (!d_is_dir(new_dentry
)) {
4835 if (new_last
.name
[new_last
.len
])
4839 /* unless the source is a directory trailing slashes give -ENOTDIR */
4840 if (!d_is_dir(old_dentry
)) {
4842 if (old_last
.name
[old_last
.len
])
4844 if (!(flags
& RENAME_EXCHANGE
) && new_last
.name
[new_last
.len
])
4847 /* source should not be ancestor of target */
4849 if (old_dentry
== trap
)
4851 /* target should not be an ancestor of source */
4852 if (!(flags
& RENAME_EXCHANGE
))
4854 if (new_dentry
== trap
)
4857 error
= security_path_rename(&old_path
, old_dentry
,
4858 &new_path
, new_dentry
, flags
);
4862 rd
.old_dir
= old_path
.dentry
->d_inode
;
4863 rd
.old_dentry
= old_dentry
;
4864 rd
.old_mnt_userns
= mnt_user_ns(old_path
.mnt
);
4865 rd
.new_dir
= new_path
.dentry
->d_inode
;
4866 rd
.new_dentry
= new_dentry
;
4867 rd
.new_mnt_userns
= mnt_user_ns(new_path
.mnt
);
4868 rd
.delegated_inode
= &delegated_inode
;
4870 error
= vfs_rename(&rd
);
4876 unlock_rename(new_path
.dentry
, old_path
.dentry
);
4877 if (delegated_inode
) {
4878 error
= break_deleg_wait(&delegated_inode
);
4882 mnt_drop_write(old_path
.mnt
);
4884 if (retry_estale(error
, lookup_flags
))
4885 should_retry
= true;
4886 path_put(&new_path
);
4888 path_put(&old_path
);
4890 should_retry
= false;
4891 lookup_flags
|= LOOKUP_REVAL
;
4900 SYSCALL_DEFINE5(renameat2
, int, olddfd
, const char __user
*, oldname
,
4901 int, newdfd
, const char __user
*, newname
, unsigned int, flags
)
4903 return do_renameat2(olddfd
, getname(oldname
), newdfd
, getname(newname
),
4907 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
4908 int, newdfd
, const char __user
*, newname
)
4910 return do_renameat2(olddfd
, getname(oldname
), newdfd
, getname(newname
),
4914 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
4916 return do_renameat2(AT_FDCWD
, getname(oldname
), AT_FDCWD
,
4917 getname(newname
), 0);
4920 int readlink_copy(char __user
*buffer
, int buflen
, const char *link
)
4922 int len
= PTR_ERR(link
);
4927 if (len
> (unsigned) buflen
)
4929 if (copy_to_user(buffer
, link
, len
))
4936 * vfs_readlink - copy symlink body into userspace buffer
4937 * @dentry: dentry on which to get symbolic link
4938 * @buffer: user memory pointer
4939 * @buflen: size of buffer
4941 * Does not touch atime. That's up to the caller if necessary
4943 * Does not call security hook.
4945 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4947 struct inode
*inode
= d_inode(dentry
);
4948 DEFINE_DELAYED_CALL(done
);
4952 if (unlikely(!(inode
->i_opflags
& IOP_DEFAULT_READLINK
))) {
4953 if (unlikely(inode
->i_op
->readlink
))
4954 return inode
->i_op
->readlink(dentry
, buffer
, buflen
);
4956 if (!d_is_symlink(dentry
))
4959 spin_lock(&inode
->i_lock
);
4960 inode
->i_opflags
|= IOP_DEFAULT_READLINK
;
4961 spin_unlock(&inode
->i_lock
);
4964 link
= READ_ONCE(inode
->i_link
);
4966 link
= inode
->i_op
->get_link(dentry
, inode
, &done
);
4968 return PTR_ERR(link
);
4970 res
= readlink_copy(buffer
, buflen
, link
);
4971 do_delayed_call(&done
);
4974 EXPORT_SYMBOL(vfs_readlink
);
4977 * vfs_get_link - get symlink body
4978 * @dentry: dentry on which to get symbolic link
4979 * @done: caller needs to free returned data with this
4981 * Calls security hook and i_op->get_link() on the supplied inode.
4983 * It does not touch atime. That's up to the caller if necessary.
4985 * Does not work on "special" symlinks like /proc/$$/fd/N
4987 const char *vfs_get_link(struct dentry
*dentry
, struct delayed_call
*done
)
4989 const char *res
= ERR_PTR(-EINVAL
);
4990 struct inode
*inode
= d_inode(dentry
);
4992 if (d_is_symlink(dentry
)) {
4993 res
= ERR_PTR(security_inode_readlink(dentry
));
4995 res
= inode
->i_op
->get_link(dentry
, inode
, done
);
4999 EXPORT_SYMBOL(vfs_get_link
);
5001 /* get the link contents into pagecache */
5002 const char *page_get_link(struct dentry
*dentry
, struct inode
*inode
,
5003 struct delayed_call
*callback
)
5007 struct address_space
*mapping
= inode
->i_mapping
;
5010 page
= find_get_page(mapping
, 0);
5012 return ERR_PTR(-ECHILD
);
5013 if (!PageUptodate(page
)) {
5015 return ERR_PTR(-ECHILD
);
5018 page
= read_mapping_page(mapping
, 0, NULL
);
5022 set_delayed_call(callback
, page_put_link
, page
);
5023 BUG_ON(mapping_gfp_mask(mapping
) & __GFP_HIGHMEM
);
5024 kaddr
= page_address(page
);
5025 nd_terminate_link(kaddr
, inode
->i_size
, PAGE_SIZE
- 1);
5029 EXPORT_SYMBOL(page_get_link
);
5031 void page_put_link(void *arg
)
5035 EXPORT_SYMBOL(page_put_link
);
5037 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
5039 DEFINE_DELAYED_CALL(done
);
5040 int res
= readlink_copy(buffer
, buflen
,
5041 page_get_link(dentry
, d_inode(dentry
),
5043 do_delayed_call(&done
);
5046 EXPORT_SYMBOL(page_readlink
);
5048 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
5050 struct address_space
*mapping
= inode
->i_mapping
;
5051 const struct address_space_operations
*aops
= mapping
->a_ops
;
5052 bool nofs
= !mapping_gfp_constraint(mapping
, __GFP_FS
);
5060 flags
= memalloc_nofs_save();
5061 err
= aops
->write_begin(NULL
, mapping
, 0, len
-1, &page
, &fsdata
);
5063 memalloc_nofs_restore(flags
);
5067 memcpy(page_address(page
), symname
, len
-1);
5069 err
= aops
->write_end(NULL
, mapping
, 0, len
-1, len
-1,
5076 mark_inode_dirty(inode
);
5081 EXPORT_SYMBOL(page_symlink
);
5083 const struct inode_operations page_symlink_inode_operations
= {
5084 .get_link
= page_get_link
,
5086 EXPORT_SYMBOL(page_symlink_inode_operations
);