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/slab.h>
21 #include <linux/wordpart.h>
23 #include <linux/filelock.h>
24 #include <linux/namei.h>
25 #include <linux/pagemap.h>
26 #include <linux/sched/mm.h>
27 #include <linux/fsnotify.h>
28 #include <linux/personality.h>
29 #include <linux/security.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
)
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
);
152 * Handle both empty path and copy failure in one go.
154 if (unlikely(len
<= 0)) {
155 if (unlikely(len
< 0)) {
160 /* The empty path is special. */
161 if (!(flags
& LOOKUP_EMPTY
)) {
163 return ERR_PTR(-ENOENT
);
168 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
169 * separate struct filename so we can dedicate the entire
170 * names_cache allocation for the pathname, and re-do the copy from
173 if (unlikely(len
== EMBEDDED_NAME_MAX
)) {
174 const size_t size
= offsetof(struct filename
, iname
[1]);
175 kname
= (char *)result
;
178 * size is chosen that way we to guarantee that
179 * result->iname[0] is within the same object and that
180 * kname can't be equal to result->iname, no matter what.
182 result
= kzalloc(size
, GFP_KERNEL
);
183 if (unlikely(!result
)) {
185 return ERR_PTR(-ENOMEM
);
187 result
->name
= kname
;
188 len
= strncpy_from_user(kname
, filename
, PATH_MAX
);
189 if (unlikely(len
< 0)) {
194 /* The empty path is special. */
195 if (unlikely(!len
) && !(flags
& LOOKUP_EMPTY
)) {
198 return ERR_PTR(-ENOENT
);
200 if (unlikely(len
== PATH_MAX
)) {
203 return ERR_PTR(-ENAMETOOLONG
);
207 atomic_set(&result
->refcnt
, 1);
208 result
->uptr
= filename
;
209 result
->aname
= NULL
;
210 audit_getname(result
);
215 getname_uflags(const char __user
*filename
, int uflags
)
217 int flags
= (uflags
& AT_EMPTY_PATH
) ? LOOKUP_EMPTY
: 0;
219 return getname_flags(filename
, flags
);
223 getname(const char __user
* filename
)
225 return getname_flags(filename
, 0);
229 getname_kernel(const char * filename
)
231 struct filename
*result
;
232 int len
= strlen(filename
) + 1;
234 result
= __getname();
235 if (unlikely(!result
))
236 return ERR_PTR(-ENOMEM
);
238 if (len
<= EMBEDDED_NAME_MAX
) {
239 result
->name
= (char *)result
->iname
;
240 } else if (len
<= PATH_MAX
) {
241 const size_t size
= offsetof(struct filename
, iname
[1]);
242 struct filename
*tmp
;
244 tmp
= kmalloc(size
, GFP_KERNEL
);
245 if (unlikely(!tmp
)) {
247 return ERR_PTR(-ENOMEM
);
249 tmp
->name
= (char *)result
;
253 return ERR_PTR(-ENAMETOOLONG
);
255 memcpy((char *)result
->name
, filename
, len
);
257 result
->aname
= NULL
;
258 atomic_set(&result
->refcnt
, 1);
259 audit_getname(result
);
263 EXPORT_SYMBOL(getname_kernel
);
265 void putname(struct filename
*name
)
270 if (WARN_ON_ONCE(!atomic_read(&name
->refcnt
)))
273 if (!atomic_dec_and_test(&name
->refcnt
))
276 if (name
->name
!= name
->iname
) {
277 __putname(name
->name
);
282 EXPORT_SYMBOL(putname
);
285 * check_acl - perform ACL permission checking
286 * @idmap: idmap of the mount the inode was found from
287 * @inode: inode to check permissions on
288 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
290 * This function performs the ACL permission checking. Since this function
291 * retrieve POSIX acls it needs to know whether it is called from a blocking or
292 * non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
294 * If the inode has been found through an idmapped mount the idmap of
295 * the vfsmount must be passed through @idmap. This function will then take
296 * care to map the inode according to @idmap before checking permissions.
297 * On non-idmapped mounts or if permission checking is to be performed on the
298 * raw inode simply pass @nop_mnt_idmap.
300 static int check_acl(struct mnt_idmap
*idmap
,
301 struct inode
*inode
, int mask
)
303 #ifdef CONFIG_FS_POSIX_ACL
304 struct posix_acl
*acl
;
306 if (mask
& MAY_NOT_BLOCK
) {
307 acl
= get_cached_acl_rcu(inode
, ACL_TYPE_ACCESS
);
310 /* no ->get_inode_acl() calls in RCU mode... */
311 if (is_uncached_acl(acl
))
313 return posix_acl_permission(idmap
, inode
, acl
, mask
);
316 acl
= get_inode_acl(inode
, ACL_TYPE_ACCESS
);
320 int error
= posix_acl_permission(idmap
, inode
, acl
, mask
);
321 posix_acl_release(acl
);
330 * acl_permission_check - perform basic UNIX permission checking
331 * @idmap: idmap of the mount the inode was found from
332 * @inode: inode to check permissions on
333 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
335 * This function performs the basic UNIX permission checking. Since this
336 * function may retrieve POSIX acls it needs to know whether it is called from a
337 * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit.
339 * If the inode has been found through an idmapped mount the idmap of
340 * the vfsmount must be passed through @idmap. This function will then take
341 * care to map the inode according to @idmap before checking permissions.
342 * On non-idmapped mounts or if permission checking is to be performed on the
343 * raw inode simply pass @nop_mnt_idmap.
345 static int acl_permission_check(struct mnt_idmap
*idmap
,
346 struct inode
*inode
, int mask
)
348 unsigned int mode
= inode
->i_mode
;
351 /* Are we the owner? If so, ACL's don't matter */
352 vfsuid
= i_uid_into_vfsuid(idmap
, inode
);
353 if (likely(vfsuid_eq_kuid(vfsuid
, current_fsuid()))) {
356 return (mask
& ~mode
) ? -EACCES
: 0;
359 /* Do we have ACL's? */
360 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
)) {
361 int error
= check_acl(idmap
, inode
, mask
);
362 if (error
!= -EAGAIN
)
366 /* Only RWX matters for group/other mode bits */
370 * Are the group permissions different from
371 * the other permissions in the bits we care
372 * about? Need to check group ownership if so.
374 if (mask
& (mode
^ (mode
>> 3))) {
375 vfsgid_t vfsgid
= i_gid_into_vfsgid(idmap
, inode
);
376 if (vfsgid_in_group_p(vfsgid
))
380 /* Bits in 'mode' clear that we require? */
381 return (mask
& ~mode
) ? -EACCES
: 0;
385 * generic_permission - check for access rights on a Posix-like filesystem
386 * @idmap: idmap of the mount the inode was found from
387 * @inode: inode to check access rights for
388 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC,
389 * %MAY_NOT_BLOCK ...)
391 * Used to check for read/write/execute permissions on a file.
392 * We use "fsuid" for this, letting us set arbitrary permissions
393 * for filesystem access without changing the "normal" uids which
394 * are used for other things.
396 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
397 * request cannot be satisfied (eg. requires blocking or too much complexity).
398 * It would then be called again in ref-walk mode.
400 * If the inode has been found through an idmapped mount the idmap of
401 * the vfsmount must be passed through @idmap. This function will then take
402 * care to map the inode according to @idmap before checking permissions.
403 * On non-idmapped mounts or if permission checking is to be performed on the
404 * raw inode simply pass @nop_mnt_idmap.
406 int generic_permission(struct mnt_idmap
*idmap
, struct inode
*inode
,
412 * Do the basic permission checks.
414 ret
= acl_permission_check(idmap
, inode
, mask
);
418 if (S_ISDIR(inode
->i_mode
)) {
419 /* DACs are overridable for directories */
420 if (!(mask
& MAY_WRITE
))
421 if (capable_wrt_inode_uidgid(idmap
, inode
,
422 CAP_DAC_READ_SEARCH
))
424 if (capable_wrt_inode_uidgid(idmap
, inode
,
431 * Searching includes executable on directories, else just read.
433 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
434 if (mask
== MAY_READ
)
435 if (capable_wrt_inode_uidgid(idmap
, inode
,
436 CAP_DAC_READ_SEARCH
))
439 * Read/write DACs are always overridable.
440 * Executable DACs are overridable when there is
441 * at least one exec bit set.
443 if (!(mask
& MAY_EXEC
) || (inode
->i_mode
& S_IXUGO
))
444 if (capable_wrt_inode_uidgid(idmap
, inode
,
450 EXPORT_SYMBOL(generic_permission
);
453 * do_inode_permission - UNIX permission checking
454 * @idmap: idmap of the mount the inode was found from
455 * @inode: inode to check permissions on
456 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...)
458 * We _really_ want to just do "generic_permission()" without
459 * even looking at the inode->i_op values. So we keep a cache
460 * flag in inode->i_opflags, that says "this has not special
461 * permission function, use the fast case".
463 static inline int do_inode_permission(struct mnt_idmap
*idmap
,
464 struct inode
*inode
, int mask
)
466 if (unlikely(!(inode
->i_opflags
& IOP_FASTPERM
))) {
467 if (likely(inode
->i_op
->permission
))
468 return inode
->i_op
->permission(idmap
, inode
, mask
);
470 /* This gets set once for the inode lifetime */
471 spin_lock(&inode
->i_lock
);
472 inode
->i_opflags
|= IOP_FASTPERM
;
473 spin_unlock(&inode
->i_lock
);
475 return generic_permission(idmap
, inode
, mask
);
479 * sb_permission - Check superblock-level permissions
480 * @sb: Superblock of inode to check permission on
481 * @inode: Inode to check permission on
482 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
484 * Separate out file-system wide checks from inode-specific permission checks.
486 static int sb_permission(struct super_block
*sb
, struct inode
*inode
, int mask
)
488 if (unlikely(mask
& MAY_WRITE
)) {
489 umode_t mode
= inode
->i_mode
;
491 /* Nobody gets write access to a read-only fs. */
492 if (sb_rdonly(sb
) && (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
499 * inode_permission - Check for access rights to a given inode
500 * @idmap: idmap of the mount the inode was found from
501 * @inode: Inode to check permission on
502 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
504 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
505 * this, letting us set arbitrary permissions for filesystem access without
506 * changing the "normal" UIDs which are used for other things.
508 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
510 int inode_permission(struct mnt_idmap
*idmap
,
511 struct inode
*inode
, int mask
)
515 retval
= sb_permission(inode
->i_sb
, inode
, mask
);
519 if (unlikely(mask
& MAY_WRITE
)) {
521 * Nobody gets write access to an immutable file.
523 if (IS_IMMUTABLE(inode
))
527 * Updating mtime will likely cause i_uid and i_gid to be
528 * written back improperly if their true value is unknown
531 if (HAS_UNMAPPED_ID(idmap
, inode
))
535 retval
= do_inode_permission(idmap
, inode
, mask
);
539 retval
= devcgroup_inode_permission(inode
, mask
);
543 return security_inode_permission(inode
, mask
);
545 EXPORT_SYMBOL(inode_permission
);
548 * path_get - get a reference to a path
549 * @path: path to get the reference to
551 * Given a path increment the reference count to the dentry and the vfsmount.
553 void path_get(const struct path
*path
)
558 EXPORT_SYMBOL(path_get
);
561 * path_put - put a reference to a path
562 * @path: path to put the reference to
564 * Given a path decrement the reference count to the dentry and the vfsmount.
566 void path_put(const struct path
*path
)
571 EXPORT_SYMBOL(path_put
);
573 #define EMBEDDED_LEVELS 2
578 struct inode
*inode
; /* path.dentry.d_inode */
579 unsigned int flags
, state
;
580 unsigned seq
, next_seq
, m_seq
, r_seq
;
583 int total_link_count
;
586 struct delayed_call done
;
589 } *stack
, internal
[EMBEDDED_LEVELS
];
590 struct filename
*name
;
591 struct nameidata
*saved
;
596 } __randomize_layout
;
598 #define ND_ROOT_PRESET 1
599 #define ND_ROOT_GRABBED 2
602 static void __set_nameidata(struct nameidata
*p
, int dfd
, struct filename
*name
)
604 struct nameidata
*old
= current
->nameidata
;
605 p
->stack
= p
->internal
;
610 p
->path
.dentry
= NULL
;
611 p
->total_link_count
= old
? old
->total_link_count
: 0;
613 current
->nameidata
= p
;
616 static inline void set_nameidata(struct nameidata
*p
, int dfd
, struct filename
*name
,
617 const struct path
*root
)
619 __set_nameidata(p
, dfd
, name
);
621 if (unlikely(root
)) {
622 p
->state
= ND_ROOT_PRESET
;
627 static void restore_nameidata(void)
629 struct nameidata
*now
= current
->nameidata
, *old
= now
->saved
;
631 current
->nameidata
= old
;
633 old
->total_link_count
= now
->total_link_count
;
634 if (now
->stack
!= now
->internal
)
638 static bool nd_alloc_stack(struct nameidata
*nd
)
642 p
= kmalloc_array(MAXSYMLINKS
, sizeof(struct saved
),
643 nd
->flags
& LOOKUP_RCU
? GFP_ATOMIC
: GFP_KERNEL
);
646 memcpy(p
, nd
->internal
, sizeof(nd
->internal
));
652 * path_connected - Verify that a dentry is below mnt.mnt_root
653 * @mnt: The mountpoint to check.
654 * @dentry: The dentry to check.
656 * Rename can sometimes move a file or directory outside of a bind
657 * mount, path_connected allows those cases to be detected.
659 static bool path_connected(struct vfsmount
*mnt
, struct dentry
*dentry
)
661 struct super_block
*sb
= mnt
->mnt_sb
;
663 /* Bind mounts can have disconnected paths */
664 if (mnt
->mnt_root
== sb
->s_root
)
667 return is_subdir(dentry
, mnt
->mnt_root
);
670 static void drop_links(struct nameidata
*nd
)
674 struct saved
*last
= nd
->stack
+ i
;
675 do_delayed_call(&last
->done
);
676 clear_delayed_call(&last
->done
);
680 static void leave_rcu(struct nameidata
*nd
)
682 nd
->flags
&= ~LOOKUP_RCU
;
683 nd
->seq
= nd
->next_seq
= 0;
687 static void terminate_walk(struct nameidata
*nd
)
690 if (!(nd
->flags
& LOOKUP_RCU
)) {
693 for (i
= 0; i
< nd
->depth
; i
++)
694 path_put(&nd
->stack
[i
].link
);
695 if (nd
->state
& ND_ROOT_GRABBED
) {
697 nd
->state
&= ~ND_ROOT_GRABBED
;
704 nd
->path
.dentry
= NULL
;
707 /* path_put is needed afterwards regardless of success or failure */
708 static bool __legitimize_path(struct path
*path
, unsigned seq
, unsigned mseq
)
710 int res
= __legitimize_mnt(path
->mnt
, mseq
);
717 if (unlikely(!lockref_get_not_dead(&path
->dentry
->d_lockref
))) {
721 return !read_seqcount_retry(&path
->dentry
->d_seq
, seq
);
724 static inline bool legitimize_path(struct nameidata
*nd
,
725 struct path
*path
, unsigned seq
)
727 return __legitimize_path(path
, seq
, nd
->m_seq
);
730 static bool legitimize_links(struct nameidata
*nd
)
733 if (unlikely(nd
->flags
& LOOKUP_CACHED
)) {
738 for (i
= 0; i
< nd
->depth
; i
++) {
739 struct saved
*last
= nd
->stack
+ i
;
740 if (unlikely(!legitimize_path(nd
, &last
->link
, last
->seq
))) {
749 static bool legitimize_root(struct nameidata
*nd
)
751 /* Nothing to do if nd->root is zero or is managed by the VFS user. */
752 if (!nd
->root
.mnt
|| (nd
->state
& ND_ROOT_PRESET
))
754 nd
->state
|= ND_ROOT_GRABBED
;
755 return legitimize_path(nd
, &nd
->root
, nd
->root_seq
);
759 * Path walking has 2 modes, rcu-walk and ref-walk (see
760 * Documentation/filesystems/path-lookup.txt). In situations when we can't
761 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
762 * normal reference counts on dentries and vfsmounts to transition to ref-walk
763 * mode. Refcounts are grabbed at the last known good point before rcu-walk
764 * got stuck, so ref-walk may continue from there. If this is not successful
765 * (eg. a seqcount has changed), then failure is returned and it's up to caller
766 * to restart the path walk from the beginning in ref-walk mode.
770 * try_to_unlazy - try to switch to ref-walk mode.
771 * @nd: nameidata pathwalk data
772 * Returns: true on success, false on failure
774 * try_to_unlazy attempts to legitimize the current nd->path and nd->root
776 * Must be called from rcu-walk context.
777 * Nothing should touch nameidata between try_to_unlazy() failure and
780 static bool try_to_unlazy(struct nameidata
*nd
)
782 struct dentry
*parent
= nd
->path
.dentry
;
784 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
786 if (unlikely(!legitimize_links(nd
)))
788 if (unlikely(!legitimize_path(nd
, &nd
->path
, nd
->seq
)))
790 if (unlikely(!legitimize_root(nd
)))
793 BUG_ON(nd
->inode
!= parent
->d_inode
);
798 nd
->path
.dentry
= NULL
;
805 * try_to_unlazy_next - try to switch to ref-walk mode.
806 * @nd: nameidata pathwalk data
807 * @dentry: next dentry to step into
808 * Returns: true on success, false on failure
810 * Similar to try_to_unlazy(), but here we have the next dentry already
811 * picked by rcu-walk and want to legitimize that in addition to the current
812 * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context.
813 * Nothing should touch nameidata between try_to_unlazy_next() failure and
816 static bool try_to_unlazy_next(struct nameidata
*nd
, struct dentry
*dentry
)
819 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
821 if (unlikely(!legitimize_links(nd
)))
823 res
= __legitimize_mnt(nd
->path
.mnt
, nd
->m_seq
);
829 if (unlikely(!lockref_get_not_dead(&nd
->path
.dentry
->d_lockref
)))
833 * We need to move both the parent and the dentry from the RCU domain
834 * to be properly refcounted. And the sequence number in the dentry
835 * validates *both* dentry counters, since we checked the sequence
836 * number of the parent after we got the child sequence number. So we
837 * know the parent must still be valid if the child sequence number is
839 if (unlikely(!lockref_get_not_dead(&dentry
->d_lockref
)))
841 if (read_seqcount_retry(&dentry
->d_seq
, nd
->next_seq
))
844 * Sequence counts matched. Now make sure that the root is
845 * still valid and get it if required.
847 if (unlikely(!legitimize_root(nd
)))
855 nd
->path
.dentry
= NULL
;
865 static inline int d_revalidate(struct dentry
*dentry
, unsigned int flags
)
867 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
))
868 return dentry
->d_op
->d_revalidate(dentry
, flags
);
874 * complete_walk - successful completion of path walk
875 * @nd: pointer nameidata
877 * If we had been in RCU mode, drop out of it and legitimize nd->path.
878 * Revalidate the final result, unless we'd already done that during
879 * the path walk or the filesystem doesn't ask for it. Return 0 on
880 * success, -error on failure. In case of failure caller does not
881 * need to drop nd->path.
883 static int complete_walk(struct nameidata
*nd
)
885 struct dentry
*dentry
= nd
->path
.dentry
;
888 if (nd
->flags
& LOOKUP_RCU
) {
890 * We don't want to zero nd->root for scoped-lookups or
891 * externally-managed nd->root.
893 if (!(nd
->state
& ND_ROOT_PRESET
))
894 if (!(nd
->flags
& LOOKUP_IS_SCOPED
))
896 nd
->flags
&= ~LOOKUP_CACHED
;
897 if (!try_to_unlazy(nd
))
901 if (unlikely(nd
->flags
& LOOKUP_IS_SCOPED
)) {
903 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
904 * ever step outside the root during lookup" and should already
905 * be guaranteed by the rest of namei, we want to avoid a namei
906 * BUG resulting in userspace being given a path that was not
907 * scoped within the root at some point during the lookup.
909 * So, do a final sanity-check to make sure that in the
910 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
911 * we won't silently return an fd completely outside of the
912 * requested root to userspace.
914 * Userspace could move the path outside the root after this
915 * check, but as discussed elsewhere this is not a concern (the
916 * resolved file was inside the root at some point).
918 if (!path_is_under(&nd
->path
, &nd
->root
))
922 if (likely(!(nd
->state
& ND_JUMPED
)))
925 if (likely(!(dentry
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
)))
928 status
= dentry
->d_op
->d_weak_revalidate(dentry
, nd
->flags
);
938 static int set_root(struct nameidata
*nd
)
940 struct fs_struct
*fs
= current
->fs
;
943 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
944 * still have to ensure it doesn't happen because it will cause a breakout
947 if (WARN_ON(nd
->flags
& LOOKUP_IS_SCOPED
))
948 return -ENOTRECOVERABLE
;
950 if (nd
->flags
& LOOKUP_RCU
) {
954 seq
= read_seqcount_begin(&fs
->seq
);
956 nd
->root_seq
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
957 } while (read_seqcount_retry(&fs
->seq
, seq
));
959 get_fs_root(fs
, &nd
->root
);
960 nd
->state
|= ND_ROOT_GRABBED
;
965 static int nd_jump_root(struct nameidata
*nd
)
967 if (unlikely(nd
->flags
& LOOKUP_BENEATH
))
969 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
)) {
970 /* Absolute path arguments to path_init() are allowed. */
971 if (nd
->path
.mnt
!= NULL
&& nd
->path
.mnt
!= nd
->root
.mnt
)
975 int error
= set_root(nd
);
979 if (nd
->flags
& LOOKUP_RCU
) {
983 nd
->inode
= d
->d_inode
;
984 nd
->seq
= nd
->root_seq
;
985 if (read_seqcount_retry(&d
->d_seq
, nd
->seq
))
991 nd
->inode
= nd
->path
.dentry
->d_inode
;
993 nd
->state
|= ND_JUMPED
;
998 * Helper to directly jump to a known parsed path from ->get_link,
999 * caller must have taken a reference to path beforehand.
1001 int nd_jump_link(const struct path
*path
)
1004 struct nameidata
*nd
= current
->nameidata
;
1006 if (unlikely(nd
->flags
& LOOKUP_NO_MAGICLINKS
))
1010 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
)) {
1011 if (nd
->path
.mnt
!= path
->mnt
)
1014 /* Not currently safe for scoped-lookups. */
1015 if (unlikely(nd
->flags
& LOOKUP_IS_SCOPED
))
1018 path_put(&nd
->path
);
1020 nd
->inode
= nd
->path
.dentry
->d_inode
;
1021 nd
->state
|= ND_JUMPED
;
1029 static inline void put_link(struct nameidata
*nd
)
1031 struct saved
*last
= nd
->stack
+ --nd
->depth
;
1032 do_delayed_call(&last
->done
);
1033 if (!(nd
->flags
& LOOKUP_RCU
))
1034 path_put(&last
->link
);
1037 static int sysctl_protected_symlinks __read_mostly
;
1038 static int sysctl_protected_hardlinks __read_mostly
;
1039 static int sysctl_protected_fifos __read_mostly
;
1040 static int sysctl_protected_regular __read_mostly
;
1042 #ifdef CONFIG_SYSCTL
1043 static struct ctl_table namei_sysctls
[] = {
1045 .procname
= "protected_symlinks",
1046 .data
= &sysctl_protected_symlinks
,
1047 .maxlen
= sizeof(int),
1049 .proc_handler
= proc_dointvec_minmax
,
1050 .extra1
= SYSCTL_ZERO
,
1051 .extra2
= SYSCTL_ONE
,
1054 .procname
= "protected_hardlinks",
1055 .data
= &sysctl_protected_hardlinks
,
1056 .maxlen
= sizeof(int),
1058 .proc_handler
= proc_dointvec_minmax
,
1059 .extra1
= SYSCTL_ZERO
,
1060 .extra2
= SYSCTL_ONE
,
1063 .procname
= "protected_fifos",
1064 .data
= &sysctl_protected_fifos
,
1065 .maxlen
= sizeof(int),
1067 .proc_handler
= proc_dointvec_minmax
,
1068 .extra1
= SYSCTL_ZERO
,
1069 .extra2
= SYSCTL_TWO
,
1072 .procname
= "protected_regular",
1073 .data
= &sysctl_protected_regular
,
1074 .maxlen
= sizeof(int),
1076 .proc_handler
= proc_dointvec_minmax
,
1077 .extra1
= SYSCTL_ZERO
,
1078 .extra2
= SYSCTL_TWO
,
1082 static int __init
init_fs_namei_sysctls(void)
1084 register_sysctl_init("fs", namei_sysctls
);
1087 fs_initcall(init_fs_namei_sysctls
);
1089 #endif /* CONFIG_SYSCTL */
1092 * may_follow_link - Check symlink following for unsafe situations
1093 * @nd: nameidata pathwalk data
1094 * @inode: Used for idmapping.
1096 * In the case of the sysctl_protected_symlinks sysctl being enabled,
1097 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
1098 * in a sticky world-writable directory. This is to protect privileged
1099 * processes from failing races against path names that may change out
1100 * from under them by way of other users creating malicious symlinks.
1101 * It will permit symlinks to be followed only when outside a sticky
1102 * world-writable directory, or when the uid of the symlink and follower
1103 * match, or when the directory owner matches the symlink's owner.
1105 * Returns 0 if following the symlink is allowed, -ve on error.
1107 static inline int may_follow_link(struct nameidata
*nd
, const struct inode
*inode
)
1109 struct mnt_idmap
*idmap
;
1112 if (!sysctl_protected_symlinks
)
1115 idmap
= mnt_idmap(nd
->path
.mnt
);
1116 vfsuid
= i_uid_into_vfsuid(idmap
, inode
);
1117 /* Allowed if owner and follower match. */
1118 if (vfsuid_eq_kuid(vfsuid
, current_fsuid()))
1121 /* Allowed if parent directory not sticky and world-writable. */
1122 if ((nd
->dir_mode
& (S_ISVTX
|S_IWOTH
)) != (S_ISVTX
|S_IWOTH
))
1125 /* Allowed if parent directory and link owner match. */
1126 if (vfsuid_valid(nd
->dir_vfsuid
) && vfsuid_eq(nd
->dir_vfsuid
, vfsuid
))
1129 if (nd
->flags
& LOOKUP_RCU
)
1132 audit_inode(nd
->name
, nd
->stack
[0].link
.dentry
, 0);
1133 audit_log_path_denied(AUDIT_ANOM_LINK
, "follow_link");
1138 * safe_hardlink_source - Check for safe hardlink conditions
1139 * @idmap: idmap of the mount the inode was found from
1140 * @inode: the source inode to hardlink from
1142 * Return false if at least one of the following conditions:
1143 * - inode is not a regular file
1145 * - inode is setgid and group-exec
1146 * - access failure for read and write
1148 * Otherwise returns true.
1150 static bool safe_hardlink_source(struct mnt_idmap
*idmap
,
1151 struct inode
*inode
)
1153 umode_t mode
= inode
->i_mode
;
1155 /* Special files should not get pinned to the filesystem. */
1159 /* Setuid files should not get pinned to the filesystem. */
1163 /* Executable setgid files should not get pinned to the filesystem. */
1164 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
))
1167 /* Hardlinking to unreadable or unwritable sources is dangerous. */
1168 if (inode_permission(idmap
, inode
, MAY_READ
| MAY_WRITE
))
1175 * may_linkat - Check permissions for creating a hardlink
1176 * @idmap: idmap of the mount the inode was found from
1177 * @link: the source to hardlink from
1179 * Block hardlink when all of:
1180 * - sysctl_protected_hardlinks enabled
1181 * - fsuid does not match inode
1182 * - hardlink source is unsafe (see safe_hardlink_source() above)
1183 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
1185 * If the inode has been found through an idmapped mount the idmap of
1186 * the vfsmount must be passed through @idmap. This function will then take
1187 * care to map the inode according to @idmap before checking permissions.
1188 * On non-idmapped mounts or if permission checking is to be performed on the
1189 * raw inode simply pass @nop_mnt_idmap.
1191 * Returns 0 if successful, -ve on error.
1193 int may_linkat(struct mnt_idmap
*idmap
, const struct path
*link
)
1195 struct inode
*inode
= link
->dentry
->d_inode
;
1197 /* Inode writeback is not safe when the uid or gid are invalid. */
1198 if (!vfsuid_valid(i_uid_into_vfsuid(idmap
, inode
)) ||
1199 !vfsgid_valid(i_gid_into_vfsgid(idmap
, inode
)))
1202 if (!sysctl_protected_hardlinks
)
1205 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1206 * otherwise, it must be a safe source.
1208 if (safe_hardlink_source(idmap
, inode
) ||
1209 inode_owner_or_capable(idmap
, inode
))
1212 audit_log_path_denied(AUDIT_ANOM_LINK
, "linkat");
1217 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1218 * should be allowed, or not, on files that already
1220 * @idmap: idmap of the mount the inode was found from
1221 * @nd: nameidata pathwalk data
1222 * @inode: the inode of the file to open
1224 * Block an O_CREAT open of a FIFO (or a regular file) when:
1225 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1226 * - the file already exists
1227 * - we are in a sticky directory
1228 * - we don't own the file
1229 * - the owner of the directory doesn't own the file
1230 * - the directory is world writable
1231 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1232 * the directory doesn't have to be world writable: being group writable will
1235 * If the inode has been found through an idmapped mount the idmap of
1236 * the vfsmount must be passed through @idmap. This function will then take
1237 * care to map the inode according to @idmap before checking permissions.
1238 * On non-idmapped mounts or if permission checking is to be performed on the
1239 * raw inode simply pass @nop_mnt_idmap.
1241 * Returns 0 if the open is allowed, -ve on error.
1243 static int may_create_in_sticky(struct mnt_idmap
*idmap
, struct nameidata
*nd
,
1244 struct inode
*const inode
)
1246 umode_t dir_mode
= nd
->dir_mode
;
1247 vfsuid_t dir_vfsuid
= nd
->dir_vfsuid
, i_vfsuid
;
1249 if (likely(!(dir_mode
& S_ISVTX
)))
1252 if (S_ISREG(inode
->i_mode
) && !sysctl_protected_regular
)
1255 if (S_ISFIFO(inode
->i_mode
) && !sysctl_protected_fifos
)
1258 i_vfsuid
= i_uid_into_vfsuid(idmap
, inode
);
1260 if (vfsuid_eq(i_vfsuid
, dir_vfsuid
))
1263 if (vfsuid_eq_kuid(i_vfsuid
, current_fsuid()))
1266 if (likely(dir_mode
& 0002)) {
1267 audit_log_path_denied(AUDIT_ANOM_CREAT
, "sticky_create");
1271 if (dir_mode
& 0020) {
1272 if (sysctl_protected_fifos
>= 2 && S_ISFIFO(inode
->i_mode
)) {
1273 audit_log_path_denied(AUDIT_ANOM_CREAT
,
1274 "sticky_create_fifo");
1278 if (sysctl_protected_regular
>= 2 && S_ISREG(inode
->i_mode
)) {
1279 audit_log_path_denied(AUDIT_ANOM_CREAT
,
1280 "sticky_create_regular");
1289 * follow_up - Find the mountpoint of path's vfsmount
1291 * Given a path, find the mountpoint of its source file system.
1292 * Replace @path with the path of the mountpoint in the parent mount.
1295 * Return 1 if we went up a level and 0 if we were already at the
1298 int follow_up(struct path
*path
)
1300 struct mount
*mnt
= real_mount(path
->mnt
);
1301 struct mount
*parent
;
1302 struct dentry
*mountpoint
;
1304 read_seqlock_excl(&mount_lock
);
1305 parent
= mnt
->mnt_parent
;
1306 if (parent
== mnt
) {
1307 read_sequnlock_excl(&mount_lock
);
1310 mntget(&parent
->mnt
);
1311 mountpoint
= dget(mnt
->mnt_mountpoint
);
1312 read_sequnlock_excl(&mount_lock
);
1314 path
->dentry
= mountpoint
;
1316 path
->mnt
= &parent
->mnt
;
1319 EXPORT_SYMBOL(follow_up
);
1321 static bool choose_mountpoint_rcu(struct mount
*m
, const struct path
*root
,
1322 struct path
*path
, unsigned *seqp
)
1324 while (mnt_has_parent(m
)) {
1325 struct dentry
*mountpoint
= m
->mnt_mountpoint
;
1328 if (unlikely(root
->dentry
== mountpoint
&&
1329 root
->mnt
== &m
->mnt
))
1331 if (mountpoint
!= m
->mnt
.mnt_root
) {
1332 path
->mnt
= &m
->mnt
;
1333 path
->dentry
= mountpoint
;
1334 *seqp
= read_seqcount_begin(&mountpoint
->d_seq
);
1341 static bool choose_mountpoint(struct mount
*m
, const struct path
*root
,
1348 unsigned seq
, mseq
= read_seqbegin(&mount_lock
);
1350 found
= choose_mountpoint_rcu(m
, root
, path
, &seq
);
1351 if (unlikely(!found
)) {
1352 if (!read_seqretry(&mount_lock
, mseq
))
1355 if (likely(__legitimize_path(path
, seq
, mseq
)))
1367 * Perform an automount
1368 * - return -EISDIR to tell follow_managed() to stop and return the path we
1371 static int follow_automount(struct path
*path
, int *count
, unsigned lookup_flags
)
1373 struct dentry
*dentry
= path
->dentry
;
1375 /* We don't want to mount if someone's just doing a stat -
1376 * unless they're stat'ing a directory and appended a '/' to
1379 * We do, however, want to mount if someone wants to open or
1380 * create a file of any type under the mountpoint, wants to
1381 * traverse through the mountpoint or wants to open the
1382 * mounted directory. Also, autofs may mark negative dentries
1383 * as being automount points. These will need the attentions
1384 * of the daemon to instantiate them before they can be used.
1386 if (!(lookup_flags
& (LOOKUP_PARENT
| LOOKUP_DIRECTORY
|
1387 LOOKUP_OPEN
| LOOKUP_CREATE
| LOOKUP_AUTOMOUNT
)) &&
1391 if (count
&& (*count
)++ >= MAXSYMLINKS
)
1394 return finish_automount(dentry
->d_op
->d_automount(path
), path
);
1398 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1399 * dentries are pinned but not locked here, so negative dentry can go
1400 * positive right under us. Use of smp_load_acquire() provides a barrier
1401 * sufficient for ->d_inode and ->d_flags consistency.
1403 static int __traverse_mounts(struct path
*path
, unsigned flags
, bool *jumped
,
1404 int *count
, unsigned lookup_flags
)
1406 struct vfsmount
*mnt
= path
->mnt
;
1407 bool need_mntput
= false;
1410 while (flags
& DCACHE_MANAGED_DENTRY
) {
1411 /* Allow the filesystem to manage the transit without i_mutex
1413 if (flags
& DCACHE_MANAGE_TRANSIT
) {
1414 ret
= path
->dentry
->d_op
->d_manage(path
, false);
1415 flags
= smp_load_acquire(&path
->dentry
->d_flags
);
1420 if (flags
& DCACHE_MOUNTED
) { // something's mounted on it..
1421 struct vfsmount
*mounted
= lookup_mnt(path
);
1422 if (mounted
) { // ... in our namespace
1426 path
->mnt
= mounted
;
1427 path
->dentry
= dget(mounted
->mnt_root
);
1428 // here we know it's positive
1429 flags
= path
->dentry
->d_flags
;
1435 if (!(flags
& DCACHE_NEED_AUTOMOUNT
))
1438 // uncovered automount point
1439 ret
= follow_automount(path
, count
, lookup_flags
);
1440 flags
= smp_load_acquire(&path
->dentry
->d_flags
);
1447 // possible if you race with several mount --move
1448 if (need_mntput
&& path
->mnt
== mnt
)
1450 if (!ret
&& unlikely(d_flags_negative(flags
)))
1452 *jumped
= need_mntput
;
1456 static inline int traverse_mounts(struct path
*path
, bool *jumped
,
1457 int *count
, unsigned lookup_flags
)
1459 unsigned flags
= smp_load_acquire(&path
->dentry
->d_flags
);
1462 if (likely(!(flags
& DCACHE_MANAGED_DENTRY
))) {
1464 if (unlikely(d_flags_negative(flags
)))
1468 return __traverse_mounts(path
, flags
, jumped
, count
, lookup_flags
);
1471 int follow_down_one(struct path
*path
)
1473 struct vfsmount
*mounted
;
1475 mounted
= lookup_mnt(path
);
1479 path
->mnt
= mounted
;
1480 path
->dentry
= dget(mounted
->mnt_root
);
1485 EXPORT_SYMBOL(follow_down_one
);
1488 * Follow down to the covering mount currently visible to userspace. At each
1489 * point, the filesystem owning that dentry may be queried as to whether the
1490 * caller is permitted to proceed or not.
1492 int follow_down(struct path
*path
, unsigned int flags
)
1494 struct vfsmount
*mnt
= path
->mnt
;
1496 int ret
= traverse_mounts(path
, &jumped
, NULL
, flags
);
1498 if (path
->mnt
!= mnt
)
1502 EXPORT_SYMBOL(follow_down
);
1505 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1506 * we meet a managed dentry that would need blocking.
1508 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
)
1510 struct dentry
*dentry
= path
->dentry
;
1511 unsigned int flags
= dentry
->d_flags
;
1513 if (likely(!(flags
& DCACHE_MANAGED_DENTRY
)))
1516 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
))
1521 * Don't forget we might have a non-mountpoint managed dentry
1522 * that wants to block transit.
1524 if (unlikely(flags
& DCACHE_MANAGE_TRANSIT
)) {
1525 int res
= dentry
->d_op
->d_manage(path
, true);
1527 return res
== -EISDIR
;
1528 flags
= dentry
->d_flags
;
1531 if (flags
& DCACHE_MOUNTED
) {
1532 struct mount
*mounted
= __lookup_mnt(path
->mnt
, dentry
);
1534 path
->mnt
= &mounted
->mnt
;
1535 dentry
= path
->dentry
= mounted
->mnt
.mnt_root
;
1536 nd
->state
|= ND_JUMPED
;
1537 nd
->next_seq
= read_seqcount_begin(&dentry
->d_seq
);
1538 flags
= dentry
->d_flags
;
1539 // makes sure that non-RCU pathwalk could reach
1541 if (read_seqretry(&mount_lock
, nd
->m_seq
))
1545 if (read_seqretry(&mount_lock
, nd
->m_seq
))
1548 return !(flags
& DCACHE_NEED_AUTOMOUNT
);
1552 static inline int handle_mounts(struct nameidata
*nd
, struct dentry
*dentry
,
1558 path
->mnt
= nd
->path
.mnt
;
1559 path
->dentry
= dentry
;
1560 if (nd
->flags
& LOOKUP_RCU
) {
1561 unsigned int seq
= nd
->next_seq
;
1562 if (likely(__follow_mount_rcu(nd
, path
)))
1564 // *path and nd->next_seq might've been clobbered
1565 path
->mnt
= nd
->path
.mnt
;
1566 path
->dentry
= dentry
;
1568 if (!try_to_unlazy_next(nd
, dentry
))
1571 ret
= traverse_mounts(path
, &jumped
, &nd
->total_link_count
, nd
->flags
);
1573 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
))
1576 nd
->state
|= ND_JUMPED
;
1578 if (unlikely(ret
)) {
1580 if (path
->mnt
!= nd
->path
.mnt
)
1587 * This looks up the name in dcache and possibly revalidates the found dentry.
1588 * NULL is returned if the dentry does not exist in the cache.
1590 static struct dentry
*lookup_dcache(const struct qstr
*name
,
1594 struct dentry
*dentry
= d_lookup(dir
, name
);
1596 int error
= d_revalidate(dentry
, flags
);
1597 if (unlikely(error
<= 0)) {
1599 d_invalidate(dentry
);
1601 return ERR_PTR(error
);
1608 * Parent directory has inode locked exclusive. This is one
1609 * and only case when ->lookup() gets called on non in-lookup
1610 * dentries - as the matter of fact, this only gets called
1611 * when directory is guaranteed to have no in-lookup children
1614 struct dentry
*lookup_one_qstr_excl(const struct qstr
*name
,
1615 struct dentry
*base
,
1618 struct dentry
*dentry
= lookup_dcache(name
, base
, flags
);
1620 struct inode
*dir
= base
->d_inode
;
1625 /* Don't create child dentry for a dead directory. */
1626 if (unlikely(IS_DEADDIR(dir
)))
1627 return ERR_PTR(-ENOENT
);
1629 dentry
= d_alloc(base
, name
);
1630 if (unlikely(!dentry
))
1631 return ERR_PTR(-ENOMEM
);
1633 old
= dir
->i_op
->lookup(dir
, dentry
, flags
);
1634 if (unlikely(old
)) {
1640 EXPORT_SYMBOL(lookup_one_qstr_excl
);
1643 * lookup_fast - do fast lockless (but racy) lookup of a dentry
1644 * @nd: current nameidata
1646 * Do a fast, but racy lookup in the dcache for the given dentry, and
1647 * revalidate it. Returns a valid dentry pointer or NULL if one wasn't
1648 * found. On error, an ERR_PTR will be returned.
1650 * If this function returns a valid dentry and the walk is no longer
1651 * lazy, the dentry will carry a reference that must later be put. If
1652 * RCU mode is still in force, then this is not the case and the dentry
1653 * must be legitimized before use. If this returns NULL, then the walk
1654 * will no longer be in RCU mode.
1656 static struct dentry
*lookup_fast(struct nameidata
*nd
)
1658 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1662 * Rename seqlock is not required here because in the off chance
1663 * of a false negative due to a concurrent rename, the caller is
1664 * going to fall back to non-racy lookup.
1666 if (nd
->flags
& LOOKUP_RCU
) {
1667 dentry
= __d_lookup_rcu(parent
, &nd
->last
, &nd
->next_seq
);
1668 if (unlikely(!dentry
)) {
1669 if (!try_to_unlazy(nd
))
1670 return ERR_PTR(-ECHILD
);
1675 * This sequence count validates that the parent had no
1676 * changes while we did the lookup of the dentry above.
1678 if (read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
1679 return ERR_PTR(-ECHILD
);
1681 status
= d_revalidate(dentry
, nd
->flags
);
1682 if (likely(status
> 0))
1684 if (!try_to_unlazy_next(nd
, dentry
))
1685 return ERR_PTR(-ECHILD
);
1686 if (status
== -ECHILD
)
1687 /* we'd been told to redo it in non-rcu mode */
1688 status
= d_revalidate(dentry
, nd
->flags
);
1690 dentry
= __d_lookup(parent
, &nd
->last
);
1691 if (unlikely(!dentry
))
1693 status
= d_revalidate(dentry
, nd
->flags
);
1695 if (unlikely(status
<= 0)) {
1697 d_invalidate(dentry
);
1699 return ERR_PTR(status
);
1704 /* Fast lookup failed, do it the slow way */
1705 static struct dentry
*__lookup_slow(const struct qstr
*name
,
1709 struct dentry
*dentry
, *old
;
1710 struct inode
*inode
= dir
->d_inode
;
1711 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
1713 /* Don't go there if it's already dead */
1714 if (unlikely(IS_DEADDIR(inode
)))
1715 return ERR_PTR(-ENOENT
);
1717 dentry
= d_alloc_parallel(dir
, name
, &wq
);
1720 if (unlikely(!d_in_lookup(dentry
))) {
1721 int error
= d_revalidate(dentry
, flags
);
1722 if (unlikely(error
<= 0)) {
1724 d_invalidate(dentry
);
1729 dentry
= ERR_PTR(error
);
1732 old
= inode
->i_op
->lookup(inode
, dentry
, flags
);
1733 d_lookup_done(dentry
);
1734 if (unlikely(old
)) {
1742 static struct dentry
*lookup_slow(const struct qstr
*name
,
1746 struct inode
*inode
= dir
->d_inode
;
1748 inode_lock_shared(inode
);
1749 res
= __lookup_slow(name
, dir
, flags
);
1750 inode_unlock_shared(inode
);
1754 static inline int may_lookup(struct mnt_idmap
*idmap
,
1755 struct nameidata
*restrict nd
)
1759 mask
= nd
->flags
& LOOKUP_RCU
? MAY_NOT_BLOCK
: 0;
1760 err
= inode_permission(idmap
, nd
->inode
, mask
| MAY_EXEC
);
1764 // If we failed, and we weren't in LOOKUP_RCU, it's final
1765 if (!(nd
->flags
& LOOKUP_RCU
))
1768 // Drop out of RCU mode to make sure it wasn't transient
1769 if (!try_to_unlazy(nd
))
1770 return -ECHILD
; // redo it all non-lazy
1772 if (err
!= -ECHILD
) // hard error
1775 return inode_permission(idmap
, nd
->inode
, MAY_EXEC
);
1778 static int reserve_stack(struct nameidata
*nd
, struct path
*link
)
1780 if (unlikely(nd
->total_link_count
++ >= MAXSYMLINKS
))
1783 if (likely(nd
->depth
!= EMBEDDED_LEVELS
))
1785 if (likely(nd
->stack
!= nd
->internal
))
1787 if (likely(nd_alloc_stack(nd
)))
1790 if (nd
->flags
& LOOKUP_RCU
) {
1791 // we need to grab link before we do unlazy. And we can't skip
1792 // unlazy even if we fail to grab the link - cleanup needs it
1793 bool grabbed_link
= legitimize_path(nd
, link
, nd
->next_seq
);
1795 if (!try_to_unlazy(nd
) || !grabbed_link
)
1798 if (nd_alloc_stack(nd
))
1804 enum {WALK_TRAILING
= 1, WALK_MORE
= 2, WALK_NOFOLLOW
= 4};
1806 static const char *pick_link(struct nameidata
*nd
, struct path
*link
,
1807 struct inode
*inode
, int flags
)
1811 int error
= reserve_stack(nd
, link
);
1813 if (unlikely(error
)) {
1814 if (!(nd
->flags
& LOOKUP_RCU
))
1816 return ERR_PTR(error
);
1818 last
= nd
->stack
+ nd
->depth
++;
1820 clear_delayed_call(&last
->done
);
1821 last
->seq
= nd
->next_seq
;
1823 if (flags
& WALK_TRAILING
) {
1824 error
= may_follow_link(nd
, inode
);
1825 if (unlikely(error
))
1826 return ERR_PTR(error
);
1829 if (unlikely(nd
->flags
& LOOKUP_NO_SYMLINKS
) ||
1830 unlikely(link
->mnt
->mnt_flags
& MNT_NOSYMFOLLOW
))
1831 return ERR_PTR(-ELOOP
);
1833 if (!(nd
->flags
& LOOKUP_RCU
)) {
1834 touch_atime(&last
->link
);
1836 } else if (atime_needs_update(&last
->link
, inode
)) {
1837 if (!try_to_unlazy(nd
))
1838 return ERR_PTR(-ECHILD
);
1839 touch_atime(&last
->link
);
1842 error
= security_inode_follow_link(link
->dentry
, inode
,
1843 nd
->flags
& LOOKUP_RCU
);
1844 if (unlikely(error
))
1845 return ERR_PTR(error
);
1847 res
= READ_ONCE(inode
->i_link
);
1849 const char * (*get
)(struct dentry
*, struct inode
*,
1850 struct delayed_call
*);
1851 get
= inode
->i_op
->get_link
;
1852 if (nd
->flags
& LOOKUP_RCU
) {
1853 res
= get(NULL
, inode
, &last
->done
);
1854 if (res
== ERR_PTR(-ECHILD
) && try_to_unlazy(nd
))
1855 res
= get(link
->dentry
, inode
, &last
->done
);
1857 res
= get(link
->dentry
, inode
, &last
->done
);
1865 error
= nd_jump_root(nd
);
1866 if (unlikely(error
))
1867 return ERR_PTR(error
);
1868 while (unlikely(*++res
== '/'))
1873 all_done
: // pure jump
1879 * Do we need to follow links? We _really_ want to be able
1880 * to do this check without having to look at inode->i_op,
1881 * so we keep a cache of "no, this doesn't need follow_link"
1882 * for the common case.
1884 * NOTE: dentry must be what nd->next_seq had been sampled from.
1886 static const char *step_into(struct nameidata
*nd
, int flags
,
1887 struct dentry
*dentry
)
1890 struct inode
*inode
;
1891 int err
= handle_mounts(nd
, dentry
, &path
);
1894 return ERR_PTR(err
);
1895 inode
= path
.dentry
->d_inode
;
1896 if (likely(!d_is_symlink(path
.dentry
)) ||
1897 ((flags
& WALK_TRAILING
) && !(nd
->flags
& LOOKUP_FOLLOW
)) ||
1898 (flags
& WALK_NOFOLLOW
)) {
1899 /* not a symlink or should not follow */
1900 if (nd
->flags
& LOOKUP_RCU
) {
1901 if (read_seqcount_retry(&path
.dentry
->d_seq
, nd
->next_seq
))
1902 return ERR_PTR(-ECHILD
);
1903 if (unlikely(!inode
))
1904 return ERR_PTR(-ENOENT
);
1906 dput(nd
->path
.dentry
);
1907 if (nd
->path
.mnt
!= path
.mnt
)
1908 mntput(nd
->path
.mnt
);
1912 nd
->seq
= nd
->next_seq
;
1915 if (nd
->flags
& LOOKUP_RCU
) {
1916 /* make sure that d_is_symlink above matches inode */
1917 if (read_seqcount_retry(&path
.dentry
->d_seq
, nd
->next_seq
))
1918 return ERR_PTR(-ECHILD
);
1920 if (path
.mnt
== nd
->path
.mnt
)
1923 return pick_link(nd
, &path
, inode
, flags
);
1926 static struct dentry
*follow_dotdot_rcu(struct nameidata
*nd
)
1928 struct dentry
*parent
, *old
;
1930 if (path_equal(&nd
->path
, &nd
->root
))
1932 if (unlikely(nd
->path
.dentry
== nd
->path
.mnt
->mnt_root
)) {
1935 if (!choose_mountpoint_rcu(real_mount(nd
->path
.mnt
),
1936 &nd
->root
, &path
, &seq
))
1938 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
))
1939 return ERR_PTR(-ECHILD
);
1941 nd
->inode
= path
.dentry
->d_inode
;
1943 // makes sure that non-RCU pathwalk could reach this state
1944 if (read_seqretry(&mount_lock
, nd
->m_seq
))
1945 return ERR_PTR(-ECHILD
);
1946 /* we know that mountpoint was pinned */
1948 old
= nd
->path
.dentry
;
1949 parent
= old
->d_parent
;
1950 nd
->next_seq
= read_seqcount_begin(&parent
->d_seq
);
1951 // makes sure that non-RCU pathwalk could reach this state
1952 if (read_seqcount_retry(&old
->d_seq
, nd
->seq
))
1953 return ERR_PTR(-ECHILD
);
1954 if (unlikely(!path_connected(nd
->path
.mnt
, parent
)))
1955 return ERR_PTR(-ECHILD
);
1958 if (read_seqretry(&mount_lock
, nd
->m_seq
))
1959 return ERR_PTR(-ECHILD
);
1960 if (unlikely(nd
->flags
& LOOKUP_BENEATH
))
1961 return ERR_PTR(-ECHILD
);
1962 nd
->next_seq
= nd
->seq
;
1963 return nd
->path
.dentry
;
1966 static struct dentry
*follow_dotdot(struct nameidata
*nd
)
1968 struct dentry
*parent
;
1970 if (path_equal(&nd
->path
, &nd
->root
))
1972 if (unlikely(nd
->path
.dentry
== nd
->path
.mnt
->mnt_root
)) {
1975 if (!choose_mountpoint(real_mount(nd
->path
.mnt
),
1978 path_put(&nd
->path
);
1980 nd
->inode
= path
.dentry
->d_inode
;
1981 if (unlikely(nd
->flags
& LOOKUP_NO_XDEV
))
1982 return ERR_PTR(-EXDEV
);
1984 /* rare case of legitimate dget_parent()... */
1985 parent
= dget_parent(nd
->path
.dentry
);
1986 if (unlikely(!path_connected(nd
->path
.mnt
, parent
))) {
1988 return ERR_PTR(-ENOENT
);
1993 if (unlikely(nd
->flags
& LOOKUP_BENEATH
))
1994 return ERR_PTR(-EXDEV
);
1995 return dget(nd
->path
.dentry
);
1998 static const char *handle_dots(struct nameidata
*nd
, int type
)
2000 if (type
== LAST_DOTDOT
) {
2001 const char *error
= NULL
;
2002 struct dentry
*parent
;
2004 if (!nd
->root
.mnt
) {
2005 error
= ERR_PTR(set_root(nd
));
2009 if (nd
->flags
& LOOKUP_RCU
)
2010 parent
= follow_dotdot_rcu(nd
);
2012 parent
= follow_dotdot(nd
);
2014 return ERR_CAST(parent
);
2015 error
= step_into(nd
, WALK_NOFOLLOW
, parent
);
2016 if (unlikely(error
))
2019 if (unlikely(nd
->flags
& LOOKUP_IS_SCOPED
)) {
2021 * If there was a racing rename or mount along our
2022 * path, then we can't be sure that ".." hasn't jumped
2023 * above nd->root (and so userspace should retry or use
2027 if (__read_seqcount_retry(&mount_lock
.seqcount
, nd
->m_seq
))
2028 return ERR_PTR(-EAGAIN
);
2029 if (__read_seqcount_retry(&rename_lock
.seqcount
, nd
->r_seq
))
2030 return ERR_PTR(-EAGAIN
);
2036 static const char *walk_component(struct nameidata
*nd
, int flags
)
2038 struct dentry
*dentry
;
2040 * "." and ".." are special - ".." especially so because it has
2041 * to be able to know about the current root directory and
2042 * parent relationships.
2044 if (unlikely(nd
->last_type
!= LAST_NORM
)) {
2045 if (!(flags
& WALK_MORE
) && nd
->depth
)
2047 return handle_dots(nd
, nd
->last_type
);
2049 dentry
= lookup_fast(nd
);
2051 return ERR_CAST(dentry
);
2052 if (unlikely(!dentry
)) {
2053 dentry
= lookup_slow(&nd
->last
, nd
->path
.dentry
, nd
->flags
);
2055 return ERR_CAST(dentry
);
2057 if (!(flags
& WALK_MORE
) && nd
->depth
)
2059 return step_into(nd
, flags
, dentry
);
2063 * We can do the critical dentry name comparison and hashing
2064 * operations one word at a time, but we are limited to:
2066 * - Architectures with fast unaligned word accesses. We could
2067 * do a "get_unaligned()" if this helps and is sufficiently
2070 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
2071 * do not trap on the (extremely unlikely) case of a page
2072 * crossing operation.
2074 * - Furthermore, we need an efficient 64-bit compile for the
2075 * 64-bit case in order to generate the "number of bytes in
2076 * the final mask". Again, that could be replaced with a
2077 * efficient population count instruction or similar.
2079 #ifdef CONFIG_DCACHE_WORD_ACCESS
2081 #include <asm/word-at-a-time.h>
2085 /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
2087 #elif defined(CONFIG_64BIT)
2089 * Register pressure in the mixing function is an issue, particularly
2090 * on 32-bit x86, but almost any function requires one state value and
2091 * one temporary. Instead, use a function designed for two state values
2092 * and no temporaries.
2094 * This function cannot create a collision in only two iterations, so
2095 * we have two iterations to achieve avalanche. In those two iterations,
2096 * we have six layers of mixing, which is enough to spread one bit's
2097 * influence out to 2^6 = 64 state bits.
2099 * Rotate constants are scored by considering either 64 one-bit input
2100 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
2101 * probability of that delta causing a change to each of the 128 output
2102 * bits, using a sample of random initial states.
2104 * The Shannon entropy of the computed probabilities is then summed
2105 * to produce a score. Ideally, any input change has a 50% chance of
2106 * toggling any given output bit.
2108 * Mixing scores (in bits) for (12,45):
2109 * Input delta: 1-bit 2-bit
2110 * 1 round: 713.3 42542.6
2111 * 2 rounds: 2753.7 140389.8
2112 * 3 rounds: 5954.1 233458.2
2113 * 4 rounds: 7862.6 256672.2
2114 * Perfect: 8192 258048
2115 * (64*128) (64*63/2 * 128)
2117 #define HASH_MIX(x, y, a) \
2119 y ^= x, x = rol64(x,12),\
2120 x += y, y = rol64(y,45),\
2124 * Fold two longs into one 32-bit hash value. This must be fast, but
2125 * latency isn't quite as critical, as there is a fair bit of additional
2126 * work done before the hash value is used.
2128 static inline unsigned int fold_hash(unsigned long x
, unsigned long y
)
2130 y
^= x
* GOLDEN_RATIO_64
;
2131 y
*= GOLDEN_RATIO_64
;
2135 #else /* 32-bit case */
2138 * Mixing scores (in bits) for (7,20):
2139 * Input delta: 1-bit 2-bit
2140 * 1 round: 330.3 9201.6
2141 * 2 rounds: 1246.4 25475.4
2142 * 3 rounds: 1907.1 31295.1
2143 * 4 rounds: 2042.3 31718.6
2144 * Perfect: 2048 31744
2145 * (32*64) (32*31/2 * 64)
2147 #define HASH_MIX(x, y, a) \
2149 y ^= x, x = rol32(x, 7),\
2150 x += y, y = rol32(y,20),\
2153 static inline unsigned int fold_hash(unsigned long x
, unsigned long y
)
2155 /* Use arch-optimized multiply if one exists */
2156 return __hash_32(y
^ __hash_32(x
));
2162 * Return the hash of a string of known length. This is carfully
2163 * designed to match hash_name(), which is the more critical function.
2164 * In particular, we must end by hashing a final word containing 0..7
2165 * payload bytes, to match the way that hash_name() iterates until it
2166 * finds the delimiter after the name.
2168 unsigned int full_name_hash(const void *salt
, const char *name
, unsigned int len
)
2170 unsigned long a
, x
= 0, y
= (unsigned long)salt
;
2175 a
= load_unaligned_zeropad(name
);
2176 if (len
< sizeof(unsigned long))
2179 name
+= sizeof(unsigned long);
2180 len
-= sizeof(unsigned long);
2182 x
^= a
& bytemask_from_count(len
);
2184 return fold_hash(x
, y
);
2186 EXPORT_SYMBOL(full_name_hash
);
2188 /* Return the "hash_len" (hash and length) of a null-terminated string */
2189 u64
hashlen_string(const void *salt
, const char *name
)
2191 unsigned long a
= 0, x
= 0, y
= (unsigned long)salt
;
2192 unsigned long adata
, mask
, len
;
2193 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
2200 len
+= sizeof(unsigned long);
2202 a
= load_unaligned_zeropad(name
+len
);
2203 } while (!has_zero(a
, &adata
, &constants
));
2205 adata
= prep_zero_mask(a
, adata
, &constants
);
2206 mask
= create_zero_mask(adata
);
2207 x
^= a
& zero_bytemask(mask
);
2209 return hashlen_create(fold_hash(x
, y
), len
+ find_zero(mask
));
2211 EXPORT_SYMBOL(hashlen_string
);
2214 * Calculate the length and hash of the path component, and
2215 * return the length as the result.
2217 static inline const char *hash_name(struct nameidata
*nd
,
2219 unsigned long *lastword
)
2221 unsigned long a
, b
, x
, y
= (unsigned long)nd
->path
.dentry
;
2222 unsigned long adata
, bdata
, mask
, len
;
2223 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
2226 * The first iteration is special, because it can result in
2227 * '.' and '..' and has no mixing other than the final fold.
2229 a
= load_unaligned_zeropad(name
);
2230 b
= a
^ REPEAT_BYTE('/');
2231 if (has_zero(a
, &adata
, &constants
) | has_zero(b
, &bdata
, &constants
)) {
2232 adata
= prep_zero_mask(a
, adata
, &constants
);
2233 bdata
= prep_zero_mask(b
, bdata
, &constants
);
2234 mask
= create_zero_mask(adata
| bdata
);
2235 a
&= zero_bytemask(mask
);
2237 len
= find_zero(mask
);
2238 nd
->last
.hash
= fold_hash(a
, y
);
2247 len
+= sizeof(unsigned long);
2248 a
= load_unaligned_zeropad(name
+len
);
2249 b
= a
^ REPEAT_BYTE('/');
2250 } while (!(has_zero(a
, &adata
, &constants
) | has_zero(b
, &bdata
, &constants
)));
2252 adata
= prep_zero_mask(a
, adata
, &constants
);
2253 bdata
= prep_zero_mask(b
, bdata
, &constants
);
2254 mask
= create_zero_mask(adata
| bdata
);
2255 a
&= zero_bytemask(mask
);
2257 len
+= find_zero(mask
);
2258 *lastword
= 0; // Multi-word components cannot be DOT or DOTDOT
2260 nd
->last
.hash
= fold_hash(x
, y
);
2266 * Note that the 'last' word is always zero-masked, but
2267 * was loaded as a possibly big-endian word.
2270 #define LAST_WORD_IS_DOT (0x2eul << (BITS_PER_LONG-8))
2271 #define LAST_WORD_IS_DOTDOT (0x2e2eul << (BITS_PER_LONG-16))
2274 #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
2276 /* Return the hash of a string of known length */
2277 unsigned int full_name_hash(const void *salt
, const char *name
, unsigned int len
)
2279 unsigned long hash
= init_name_hash(salt
);
2281 hash
= partial_name_hash((unsigned char)*name
++, hash
);
2282 return end_name_hash(hash
);
2284 EXPORT_SYMBOL(full_name_hash
);
2286 /* Return the "hash_len" (hash and length) of a null-terminated string */
2287 u64
hashlen_string(const void *salt
, const char *name
)
2289 unsigned long hash
= init_name_hash(salt
);
2290 unsigned long len
= 0, c
;
2292 c
= (unsigned char)*name
;
2295 hash
= partial_name_hash(c
, hash
);
2296 c
= (unsigned char)name
[len
];
2298 return hashlen_create(end_name_hash(hash
), len
);
2300 EXPORT_SYMBOL(hashlen_string
);
2303 * We know there's a real path component here of at least
2306 static inline const char *hash_name(struct nameidata
*nd
, const char *name
, unsigned long *lastword
)
2308 unsigned long hash
= init_name_hash(nd
->path
.dentry
);
2309 unsigned long len
= 0, c
, last
= 0;
2311 c
= (unsigned char)*name
;
2313 last
= (last
<< 8) + c
;
2315 hash
= partial_name_hash(c
, hash
);
2316 c
= (unsigned char)name
[len
];
2317 } while (c
&& c
!= '/');
2319 // This is reliable for DOT or DOTDOT, since the component
2320 // cannot contain NUL characters - top bits being zero means
2321 // we cannot have had any other pathnames.
2323 nd
->last
.hash
= end_name_hash(hash
);
2330 #ifndef LAST_WORD_IS_DOT
2331 #define LAST_WORD_IS_DOT 0x2e
2332 #define LAST_WORD_IS_DOTDOT 0x2e2e
2337 * This is the basic name resolution function, turning a pathname into
2338 * the final dentry. We expect 'base' to be positive and a directory.
2340 * Returns 0 and nd will have valid dentry and mnt on success.
2341 * Returns error and drops reference to input namei data on failure.
2343 static int link_path_walk(const char *name
, struct nameidata
*nd
)
2345 int depth
= 0; // depth <= nd->depth
2348 nd
->last_type
= LAST_ROOT
;
2349 nd
->flags
|= LOOKUP_PARENT
;
2351 return PTR_ERR(name
);
2355 nd
->dir_mode
= 0; // short-circuit the 'hardening' idiocy
2359 /* At this point we know we have a real path component. */
2361 struct mnt_idmap
*idmap
;
2363 unsigned long lastword
;
2365 idmap
= mnt_idmap(nd
->path
.mnt
);
2366 err
= may_lookup(idmap
, nd
);
2370 nd
->last
.name
= name
;
2371 name
= hash_name(nd
, name
, &lastword
);
2374 case LAST_WORD_IS_DOTDOT
:
2375 nd
->last_type
= LAST_DOTDOT
;
2376 nd
->state
|= ND_JUMPED
;
2379 case LAST_WORD_IS_DOT
:
2380 nd
->last_type
= LAST_DOT
;
2384 nd
->last_type
= LAST_NORM
;
2385 nd
->state
&= ~ND_JUMPED
;
2387 struct dentry
*parent
= nd
->path
.dentry
;
2388 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
2389 err
= parent
->d_op
->d_hash(parent
, &nd
->last
);
2398 * If it wasn't NUL, we know it was '/'. Skip that
2399 * slash, and continue until no more slashes.
2403 } while (unlikely(*name
== '/'));
2404 if (unlikely(!*name
)) {
2406 /* pathname or trailing symlink, done */
2408 nd
->dir_vfsuid
= i_uid_into_vfsuid(idmap
, nd
->inode
);
2409 nd
->dir_mode
= nd
->inode
->i_mode
;
2410 nd
->flags
&= ~LOOKUP_PARENT
;
2413 /* last component of nested symlink */
2414 name
= nd
->stack
[--depth
].name
;
2415 link
= walk_component(nd
, 0);
2417 /* not the last component */
2418 link
= walk_component(nd
, WALK_MORE
);
2420 if (unlikely(link
)) {
2422 return PTR_ERR(link
);
2423 /* a symlink to follow */
2424 nd
->stack
[depth
++].name
= name
;
2428 if (unlikely(!d_can_lookup(nd
->path
.dentry
))) {
2429 if (nd
->flags
& LOOKUP_RCU
) {
2430 if (!try_to_unlazy(nd
))
2438 /* must be paired with terminate_walk() */
2439 static const char *path_init(struct nameidata
*nd
, unsigned flags
)
2442 const char *s
= nd
->name
->name
;
2444 /* LOOKUP_CACHED requires RCU, ask caller to retry */
2445 if ((flags
& (LOOKUP_RCU
| LOOKUP_CACHED
)) == LOOKUP_CACHED
)
2446 return ERR_PTR(-EAGAIN
);
2449 flags
&= ~LOOKUP_RCU
;
2450 if (flags
& LOOKUP_RCU
)
2453 nd
->seq
= nd
->next_seq
= 0;
2456 nd
->state
|= ND_JUMPED
;
2458 nd
->m_seq
= __read_seqcount_begin(&mount_lock
.seqcount
);
2459 nd
->r_seq
= __read_seqcount_begin(&rename_lock
.seqcount
);
2462 if (nd
->state
& ND_ROOT_PRESET
) {
2463 struct dentry
*root
= nd
->root
.dentry
;
2464 struct inode
*inode
= root
->d_inode
;
2465 if (*s
&& unlikely(!d_can_lookup(root
)))
2466 return ERR_PTR(-ENOTDIR
);
2467 nd
->path
= nd
->root
;
2469 if (flags
& LOOKUP_RCU
) {
2470 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2471 nd
->root_seq
= nd
->seq
;
2473 path_get(&nd
->path
);
2478 nd
->root
.mnt
= NULL
;
2480 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2481 if (*s
== '/' && !(flags
& LOOKUP_IN_ROOT
)) {
2482 error
= nd_jump_root(nd
);
2483 if (unlikely(error
))
2484 return ERR_PTR(error
);
2488 /* Relative pathname -- get the starting-point it is relative to. */
2489 if (nd
->dfd
== AT_FDCWD
) {
2490 if (flags
& LOOKUP_RCU
) {
2491 struct fs_struct
*fs
= current
->fs
;
2495 seq
= read_seqcount_begin(&fs
->seq
);
2497 nd
->inode
= nd
->path
.dentry
->d_inode
;
2498 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2499 } while (read_seqcount_retry(&fs
->seq
, seq
));
2501 get_fs_pwd(current
->fs
, &nd
->path
);
2502 nd
->inode
= nd
->path
.dentry
->d_inode
;
2505 /* Caller must check execute permissions on the starting path component */
2506 struct fd f
= fdget_raw(nd
->dfd
);
2507 struct dentry
*dentry
;
2510 return ERR_PTR(-EBADF
);
2512 if (flags
& LOOKUP_LINKAT_EMPTY
) {
2513 if (fd_file(f
)->f_cred
!= current_cred() &&
2514 !ns_capable(fd_file(f
)->f_cred
->user_ns
, CAP_DAC_READ_SEARCH
)) {
2516 return ERR_PTR(-ENOENT
);
2520 dentry
= fd_file(f
)->f_path
.dentry
;
2522 if (*s
&& unlikely(!d_can_lookup(dentry
))) {
2524 return ERR_PTR(-ENOTDIR
);
2527 nd
->path
= fd_file(f
)->f_path
;
2528 if (flags
& LOOKUP_RCU
) {
2529 nd
->inode
= nd
->path
.dentry
->d_inode
;
2530 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2532 path_get(&nd
->path
);
2533 nd
->inode
= nd
->path
.dentry
->d_inode
;
2538 /* For scoped-lookups we need to set the root to the dirfd as well. */
2539 if (flags
& LOOKUP_IS_SCOPED
) {
2540 nd
->root
= nd
->path
;
2541 if (flags
& LOOKUP_RCU
) {
2542 nd
->root_seq
= nd
->seq
;
2544 path_get(&nd
->root
);
2545 nd
->state
|= ND_ROOT_GRABBED
;
2551 static inline const char *lookup_last(struct nameidata
*nd
)
2553 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
2554 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2556 return walk_component(nd
, WALK_TRAILING
);
2559 static int handle_lookup_down(struct nameidata
*nd
)
2561 if (!(nd
->flags
& LOOKUP_RCU
))
2562 dget(nd
->path
.dentry
);
2563 nd
->next_seq
= nd
->seq
;
2564 return PTR_ERR(step_into(nd
, WALK_NOFOLLOW
, nd
->path
.dentry
));
2567 /* Returns 0 and nd will be valid on success; Returns error, otherwise. */
2568 static int path_lookupat(struct nameidata
*nd
, unsigned flags
, struct path
*path
)
2570 const char *s
= path_init(nd
, flags
);
2573 if (unlikely(flags
& LOOKUP_DOWN
) && !IS_ERR(s
)) {
2574 err
= handle_lookup_down(nd
);
2575 if (unlikely(err
< 0))
2579 while (!(err
= link_path_walk(s
, nd
)) &&
2580 (s
= lookup_last(nd
)) != NULL
)
2582 if (!err
&& unlikely(nd
->flags
& LOOKUP_MOUNTPOINT
)) {
2583 err
= handle_lookup_down(nd
);
2584 nd
->state
&= ~ND_JUMPED
; // no d_weak_revalidate(), please...
2587 err
= complete_walk(nd
);
2589 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
)
2590 if (!d_can_lookup(nd
->path
.dentry
))
2594 nd
->path
.mnt
= NULL
;
2595 nd
->path
.dentry
= NULL
;
2601 int filename_lookup(int dfd
, struct filename
*name
, unsigned flags
,
2602 struct path
*path
, struct path
*root
)
2605 struct nameidata nd
;
2607 return PTR_ERR(name
);
2608 set_nameidata(&nd
, dfd
, name
, root
);
2609 retval
= path_lookupat(&nd
, flags
| LOOKUP_RCU
, path
);
2610 if (unlikely(retval
== -ECHILD
))
2611 retval
= path_lookupat(&nd
, flags
, path
);
2612 if (unlikely(retval
== -ESTALE
))
2613 retval
= path_lookupat(&nd
, flags
| LOOKUP_REVAL
, path
);
2615 if (likely(!retval
))
2616 audit_inode(name
, path
->dentry
,
2617 flags
& LOOKUP_MOUNTPOINT
? AUDIT_INODE_NOEVAL
: 0);
2618 restore_nameidata();
2622 /* Returns 0 and nd will be valid on success; Returns error, otherwise. */
2623 static int path_parentat(struct nameidata
*nd
, unsigned flags
,
2624 struct path
*parent
)
2626 const char *s
= path_init(nd
, flags
);
2627 int err
= link_path_walk(s
, nd
);
2629 err
= complete_walk(nd
);
2632 nd
->path
.mnt
= NULL
;
2633 nd
->path
.dentry
= NULL
;
2639 /* Note: this does not consume "name" */
2640 static int __filename_parentat(int dfd
, struct filename
*name
,
2641 unsigned int flags
, struct path
*parent
,
2642 struct qstr
*last
, int *type
,
2643 const struct path
*root
)
2646 struct nameidata nd
;
2649 return PTR_ERR(name
);
2650 set_nameidata(&nd
, dfd
, name
, root
);
2651 retval
= path_parentat(&nd
, flags
| LOOKUP_RCU
, parent
);
2652 if (unlikely(retval
== -ECHILD
))
2653 retval
= path_parentat(&nd
, flags
, parent
);
2654 if (unlikely(retval
== -ESTALE
))
2655 retval
= path_parentat(&nd
, flags
| LOOKUP_REVAL
, parent
);
2656 if (likely(!retval
)) {
2658 *type
= nd
.last_type
;
2659 audit_inode(name
, parent
->dentry
, AUDIT_INODE_PARENT
);
2661 restore_nameidata();
2665 static int filename_parentat(int dfd
, struct filename
*name
,
2666 unsigned int flags
, struct path
*parent
,
2667 struct qstr
*last
, int *type
)
2669 return __filename_parentat(dfd
, name
, flags
, parent
, last
, type
, NULL
);
2672 /* does lookup, returns the object with parent locked */
2673 static struct dentry
*__kern_path_locked(int dfd
, struct filename
*name
, struct path
*path
)
2679 error
= filename_parentat(dfd
, name
, 0, path
, &last
, &type
);
2681 return ERR_PTR(error
);
2682 if (unlikely(type
!= LAST_NORM
)) {
2684 return ERR_PTR(-EINVAL
);
2686 inode_lock_nested(path
->dentry
->d_inode
, I_MUTEX_PARENT
);
2687 d
= lookup_one_qstr_excl(&last
, path
->dentry
, 0);
2689 inode_unlock(path
->dentry
->d_inode
);
2695 struct dentry
*kern_path_locked(const char *name
, struct path
*path
)
2697 struct filename
*filename
= getname_kernel(name
);
2698 struct dentry
*res
= __kern_path_locked(AT_FDCWD
, filename
, path
);
2704 struct dentry
*user_path_locked_at(int dfd
, const char __user
*name
, struct path
*path
)
2706 struct filename
*filename
= getname(name
);
2707 struct dentry
*res
= __kern_path_locked(dfd
, filename
, path
);
2712 EXPORT_SYMBOL(user_path_locked_at
);
2714 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
2716 struct filename
*filename
= getname_kernel(name
);
2717 int ret
= filename_lookup(AT_FDCWD
, filename
, flags
, path
, NULL
);
2723 EXPORT_SYMBOL(kern_path
);
2726 * vfs_path_parent_lookup - lookup a parent path relative to a dentry-vfsmount pair
2727 * @filename: filename structure
2728 * @flags: lookup flags
2729 * @parent: pointer to struct path to fill
2730 * @last: last component
2731 * @type: type of the last component
2732 * @root: pointer to struct path of the base directory
2734 int vfs_path_parent_lookup(struct filename
*filename
, unsigned int flags
,
2735 struct path
*parent
, struct qstr
*last
, int *type
,
2736 const struct path
*root
)
2738 return __filename_parentat(AT_FDCWD
, filename
, flags
, parent
, last
,
2741 EXPORT_SYMBOL(vfs_path_parent_lookup
);
2744 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2745 * @dentry: pointer to dentry of the base directory
2746 * @mnt: pointer to vfs mount of the base directory
2747 * @name: pointer to file name
2748 * @flags: lookup flags
2749 * @path: pointer to struct path to fill
2751 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
2752 const char *name
, unsigned int flags
,
2755 struct filename
*filename
;
2756 struct path root
= {.mnt
= mnt
, .dentry
= dentry
};
2759 filename
= getname_kernel(name
);
2760 /* the first argument of filename_lookup() is ignored with root */
2761 ret
= filename_lookup(AT_FDCWD
, filename
, flags
, path
, &root
);
2765 EXPORT_SYMBOL(vfs_path_lookup
);
2767 static int lookup_one_common(struct mnt_idmap
*idmap
,
2768 const char *name
, struct dentry
*base
, int len
,
2773 this->hash
= full_name_hash(base
, name
, len
);
2777 if (is_dot_dotdot(name
, len
))
2781 unsigned int c
= *(const unsigned char *)name
++;
2782 if (c
== '/' || c
== '\0')
2786 * See if the low-level filesystem might want
2787 * to use its own hash..
2789 if (base
->d_flags
& DCACHE_OP_HASH
) {
2790 int err
= base
->d_op
->d_hash(base
, this);
2795 return inode_permission(idmap
, base
->d_inode
, MAY_EXEC
);
2799 * try_lookup_one_len - filesystem helper to lookup single pathname component
2800 * @name: pathname component to lookup
2801 * @base: base directory to lookup from
2802 * @len: maximum length @len should be interpreted to
2804 * Look up a dentry by name in the dcache, returning NULL if it does not
2805 * currently exist. The function does not try to create a dentry.
2807 * Note that this routine is purely a helper for filesystem usage and should
2808 * not be called by generic code.
2810 * The caller must hold base->i_mutex.
2812 struct dentry
*try_lookup_one_len(const char *name
, struct dentry
*base
, int len
)
2817 WARN_ON_ONCE(!inode_is_locked(base
->d_inode
));
2819 err
= lookup_one_common(&nop_mnt_idmap
, name
, base
, len
, &this);
2821 return ERR_PTR(err
);
2823 return lookup_dcache(&this, base
, 0);
2825 EXPORT_SYMBOL(try_lookup_one_len
);
2828 * lookup_one_len - filesystem helper to lookup single pathname component
2829 * @name: pathname component to lookup
2830 * @base: base directory to lookup from
2831 * @len: maximum length @len should be interpreted to
2833 * Note that this routine is purely a helper for filesystem usage and should
2834 * not be called by generic code.
2836 * The caller must hold base->i_mutex.
2838 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
2840 struct dentry
*dentry
;
2844 WARN_ON_ONCE(!inode_is_locked(base
->d_inode
));
2846 err
= lookup_one_common(&nop_mnt_idmap
, name
, base
, len
, &this);
2848 return ERR_PTR(err
);
2850 dentry
= lookup_dcache(&this, base
, 0);
2851 return dentry
? dentry
: __lookup_slow(&this, base
, 0);
2853 EXPORT_SYMBOL(lookup_one_len
);
2856 * lookup_one - filesystem helper to lookup single pathname component
2857 * @idmap: idmap of the mount the lookup is performed from
2858 * @name: pathname component to lookup
2859 * @base: base directory to lookup from
2860 * @len: maximum length @len should be interpreted to
2862 * Note that this routine is purely a helper for filesystem usage and should
2863 * not be called by generic code.
2865 * The caller must hold base->i_mutex.
2867 struct dentry
*lookup_one(struct mnt_idmap
*idmap
, const char *name
,
2868 struct dentry
*base
, int len
)
2870 struct dentry
*dentry
;
2874 WARN_ON_ONCE(!inode_is_locked(base
->d_inode
));
2876 err
= lookup_one_common(idmap
, name
, base
, len
, &this);
2878 return ERR_PTR(err
);
2880 dentry
= lookup_dcache(&this, base
, 0);
2881 return dentry
? dentry
: __lookup_slow(&this, base
, 0);
2883 EXPORT_SYMBOL(lookup_one
);
2886 * lookup_one_unlocked - filesystem helper to lookup single pathname component
2887 * @idmap: idmap of the mount the lookup is performed from
2888 * @name: pathname component to lookup
2889 * @base: base directory to lookup from
2890 * @len: maximum length @len should be interpreted to
2892 * Note that this routine is purely a helper for filesystem usage and should
2893 * not be called by generic code.
2895 * Unlike lookup_one_len, it should be called without the parent
2896 * i_mutex held, and will take the i_mutex itself if necessary.
2898 struct dentry
*lookup_one_unlocked(struct mnt_idmap
*idmap
,
2899 const char *name
, struct dentry
*base
,
2906 err
= lookup_one_common(idmap
, name
, base
, len
, &this);
2908 return ERR_PTR(err
);
2910 ret
= lookup_dcache(&this, base
, 0);
2912 ret
= lookup_slow(&this, base
, 0);
2915 EXPORT_SYMBOL(lookup_one_unlocked
);
2918 * lookup_one_positive_unlocked - filesystem helper to lookup single
2919 * pathname component
2920 * @idmap: idmap of the mount the lookup is performed from
2921 * @name: pathname component to lookup
2922 * @base: base directory to lookup from
2923 * @len: maximum length @len should be interpreted to
2925 * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns
2926 * known positive or ERR_PTR(). This is what most of the users want.
2928 * Note that pinned negative with unlocked parent _can_ become positive at any
2929 * time, so callers of lookup_one_unlocked() need to be very careful; pinned
2930 * positives have >d_inode stable, so this one avoids such problems.
2932 * Note that this routine is purely a helper for filesystem usage and should
2933 * not be called by generic code.
2935 * The helper should be called without i_mutex held.
2937 struct dentry
*lookup_one_positive_unlocked(struct mnt_idmap
*idmap
,
2939 struct dentry
*base
, int len
)
2941 struct dentry
*ret
= lookup_one_unlocked(idmap
, name
, base
, len
);
2943 if (!IS_ERR(ret
) && d_flags_negative(smp_load_acquire(&ret
->d_flags
))) {
2945 ret
= ERR_PTR(-ENOENT
);
2949 EXPORT_SYMBOL(lookup_one_positive_unlocked
);
2952 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2953 * @name: pathname component to lookup
2954 * @base: base directory to lookup from
2955 * @len: maximum length @len should be interpreted to
2957 * Note that this routine is purely a helper for filesystem usage and should
2958 * not be called by generic code.
2960 * Unlike lookup_one_len, it should be called without the parent
2961 * i_mutex held, and will take the i_mutex itself if necessary.
2963 struct dentry
*lookup_one_len_unlocked(const char *name
,
2964 struct dentry
*base
, int len
)
2966 return lookup_one_unlocked(&nop_mnt_idmap
, name
, base
, len
);
2968 EXPORT_SYMBOL(lookup_one_len_unlocked
);
2971 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2972 * on negatives. Returns known positive or ERR_PTR(); that's what
2973 * most of the users want. Note that pinned negative with unlocked parent
2974 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2975 * need to be very careful; pinned positives have ->d_inode stable, so
2976 * this one avoids such problems.
2978 struct dentry
*lookup_positive_unlocked(const char *name
,
2979 struct dentry
*base
, int len
)
2981 return lookup_one_positive_unlocked(&nop_mnt_idmap
, name
, base
, len
);
2983 EXPORT_SYMBOL(lookup_positive_unlocked
);
2985 #ifdef CONFIG_UNIX98_PTYS
2986 int path_pts(struct path
*path
)
2988 /* Find something mounted on "pts" in the same directory as
2991 struct dentry
*parent
= dget_parent(path
->dentry
);
2992 struct dentry
*child
;
2993 struct qstr
this = QSTR_INIT("pts", 3);
2995 if (unlikely(!path_connected(path
->mnt
, parent
))) {
3000 path
->dentry
= parent
;
3001 child
= d_hash_and_lookup(parent
, &this);
3002 if (IS_ERR_OR_NULL(child
))
3005 path
->dentry
= child
;
3007 follow_down(path
, 0);
3012 int user_path_at(int dfd
, const char __user
*name
, unsigned flags
,
3015 struct filename
*filename
= getname_flags(name
, flags
);
3016 int ret
= filename_lookup(dfd
, filename
, flags
, path
, NULL
);
3021 EXPORT_SYMBOL(user_path_at
);
3023 int __check_sticky(struct mnt_idmap
*idmap
, struct inode
*dir
,
3024 struct inode
*inode
)
3026 kuid_t fsuid
= current_fsuid();
3028 if (vfsuid_eq_kuid(i_uid_into_vfsuid(idmap
, inode
), fsuid
))
3030 if (vfsuid_eq_kuid(i_uid_into_vfsuid(idmap
, dir
), fsuid
))
3032 return !capable_wrt_inode_uidgid(idmap
, inode
, CAP_FOWNER
);
3034 EXPORT_SYMBOL(__check_sticky
);
3037 * Check whether we can remove a link victim from directory dir, check
3038 * whether the type of victim is right.
3039 * 1. We can't do it if dir is read-only (done in permission())
3040 * 2. We should have write and exec permissions on dir
3041 * 3. We can't remove anything from append-only dir
3042 * 4. We can't do anything with immutable dir (done in permission())
3043 * 5. If the sticky bit on dir is set we should either
3044 * a. be owner of dir, or
3045 * b. be owner of victim, or
3046 * c. have CAP_FOWNER capability
3047 * 6. If the victim is append-only or immutable we can't do antyhing with
3048 * links pointing to it.
3049 * 7. If the victim has an unknown uid or gid we can't change the inode.
3050 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
3051 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
3052 * 10. We can't remove a root or mountpoint.
3053 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
3054 * nfs_async_unlink().
3056 static int may_delete(struct mnt_idmap
*idmap
, struct inode
*dir
,
3057 struct dentry
*victim
, bool isdir
)
3059 struct inode
*inode
= d_backing_inode(victim
);
3062 if (d_is_negative(victim
))
3066 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
3068 /* Inode writeback is not safe when the uid or gid are invalid. */
3069 if (!vfsuid_valid(i_uid_into_vfsuid(idmap
, inode
)) ||
3070 !vfsgid_valid(i_gid_into_vfsgid(idmap
, inode
)))
3073 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
3075 error
= inode_permission(idmap
, dir
, MAY_WRITE
| MAY_EXEC
);
3081 if (check_sticky(idmap
, dir
, inode
) || IS_APPEND(inode
) ||
3082 IS_IMMUTABLE(inode
) || IS_SWAPFILE(inode
) ||
3083 HAS_UNMAPPED_ID(idmap
, inode
))
3086 if (!d_is_dir(victim
))
3088 if (IS_ROOT(victim
))
3090 } else if (d_is_dir(victim
))
3092 if (IS_DEADDIR(dir
))
3094 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
3099 /* Check whether we can create an object with dentry child in directory
3101 * 1. We can't do it if child already exists (open has special treatment for
3102 * this case, but since we are inlined it's OK)
3103 * 2. We can't do it if dir is read-only (done in permission())
3104 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
3105 * 4. We should have write and exec permissions on dir
3106 * 5. We can't do it if dir is immutable (done in permission())
3108 static inline int may_create(struct mnt_idmap
*idmap
,
3109 struct inode
*dir
, struct dentry
*child
)
3111 audit_inode_child(dir
, child
, AUDIT_TYPE_CHILD_CREATE
);
3114 if (IS_DEADDIR(dir
))
3116 if (!fsuidgid_has_mapping(dir
->i_sb
, idmap
))
3119 return inode_permission(idmap
, dir
, MAY_WRITE
| MAY_EXEC
);
3122 // p1 != p2, both are on the same filesystem, ->s_vfs_rename_mutex is held
3123 static struct dentry
*lock_two_directories(struct dentry
*p1
, struct dentry
*p2
)
3125 struct dentry
*p
= p1
, *q
= p2
, *r
;
3127 while ((r
= p
->d_parent
) != p2
&& r
!= p
)
3130 // p is a child of p2 and an ancestor of p1 or p1 itself
3131 inode_lock_nested(p2
->d_inode
, I_MUTEX_PARENT
);
3132 inode_lock_nested(p1
->d_inode
, I_MUTEX_PARENT2
);
3135 // p is the root of connected component that contains p1
3136 // p2 does not occur on the path from p to p1
3137 while ((r
= q
->d_parent
) != p1
&& r
!= p
&& r
!= q
)
3140 // q is a child of p1 and an ancestor of p2 or p2 itself
3141 inode_lock_nested(p1
->d_inode
, I_MUTEX_PARENT
);
3142 inode_lock_nested(p2
->d_inode
, I_MUTEX_PARENT2
);
3144 } else if (likely(r
== p
)) {
3145 // both p2 and p1 are descendents of p
3146 inode_lock_nested(p1
->d_inode
, I_MUTEX_PARENT
);
3147 inode_lock_nested(p2
->d_inode
, I_MUTEX_PARENT2
);
3149 } else { // no common ancestor at the time we'd been called
3150 mutex_unlock(&p1
->d_sb
->s_vfs_rename_mutex
);
3151 return ERR_PTR(-EXDEV
);
3156 * p1 and p2 should be directories on the same fs.
3158 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
3161 inode_lock_nested(p1
->d_inode
, I_MUTEX_PARENT
);
3165 mutex_lock(&p1
->d_sb
->s_vfs_rename_mutex
);
3166 return lock_two_directories(p1
, p2
);
3168 EXPORT_SYMBOL(lock_rename
);
3171 * c1 and p2 should be on the same fs.
3173 struct dentry
*lock_rename_child(struct dentry
*c1
, struct dentry
*p2
)
3175 if (READ_ONCE(c1
->d_parent
) == p2
) {
3177 * hopefully won't need to touch ->s_vfs_rename_mutex at all.
3179 inode_lock_nested(p2
->d_inode
, I_MUTEX_PARENT
);
3181 * now that p2 is locked, nobody can move in or out of it,
3182 * so the test below is safe.
3184 if (likely(c1
->d_parent
== p2
))
3188 * c1 got moved out of p2 while we'd been taking locks;
3189 * unlock and fall back to slow case.
3191 inode_unlock(p2
->d_inode
);
3194 mutex_lock(&c1
->d_sb
->s_vfs_rename_mutex
);
3196 * nobody can move out of any directories on this fs.
3198 if (likely(c1
->d_parent
!= p2
))
3199 return lock_two_directories(c1
->d_parent
, p2
);
3202 * c1 got moved into p2 while we were taking locks;
3203 * we need p2 locked and ->s_vfs_rename_mutex unlocked,
3204 * for consistency with lock_rename().
3206 inode_lock_nested(p2
->d_inode
, I_MUTEX_PARENT
);
3207 mutex_unlock(&c1
->d_sb
->s_vfs_rename_mutex
);
3210 EXPORT_SYMBOL(lock_rename_child
);
3212 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
3214 inode_unlock(p1
->d_inode
);
3216 inode_unlock(p2
->d_inode
);
3217 mutex_unlock(&p1
->d_sb
->s_vfs_rename_mutex
);
3220 EXPORT_SYMBOL(unlock_rename
);
3223 * vfs_prepare_mode - prepare the mode to be used for a new inode
3224 * @idmap: idmap of the mount the inode was found from
3225 * @dir: parent directory of the new inode
3226 * @mode: mode of the new inode
3227 * @mask_perms: allowed permission by the vfs
3228 * @type: type of file to be created
3230 * This helper consolidates and enforces vfs restrictions on the @mode of a new
3231 * object to be created.
3233 * Umask stripping depends on whether the filesystem supports POSIX ACLs (see
3234 * the kernel documentation for mode_strip_umask()). Moving umask stripping
3235 * after setgid stripping allows the same ordering for both non-POSIX ACL and
3236 * POSIX ACL supporting filesystems.
3238 * Note that it's currently valid for @type to be 0 if a directory is created.
3239 * Filesystems raise that flag individually and we need to check whether each
3240 * filesystem can deal with receiving S_IFDIR from the vfs before we enforce a
3243 * Returns: mode to be passed to the filesystem
3245 static inline umode_t
vfs_prepare_mode(struct mnt_idmap
*idmap
,
3246 const struct inode
*dir
, umode_t mode
,
3247 umode_t mask_perms
, umode_t type
)
3249 mode
= mode_strip_sgid(idmap
, dir
, mode
);
3250 mode
= mode_strip_umask(dir
, mode
);
3253 * Apply the vfs mandated allowed permission mask and set the type of
3254 * file to be created before we call into the filesystem.
3256 mode
&= (mask_perms
& ~S_IFMT
);
3257 mode
|= (type
& S_IFMT
);
3263 * vfs_create - create new file
3264 * @idmap: idmap of the mount the inode was found from
3265 * @dir: inode of the parent directory
3266 * @dentry: dentry of the child file
3267 * @mode: mode of the child file
3268 * @want_excl: whether the file must not yet exist
3270 * Create a new file.
3272 * If the inode has been found through an idmapped mount the idmap of
3273 * the vfsmount must be passed through @idmap. This function will then take
3274 * care to map the inode according to @idmap before checking permissions.
3275 * On non-idmapped mounts or if permission checking is to be performed on the
3276 * raw inode simply pass @nop_mnt_idmap.
3278 int vfs_create(struct mnt_idmap
*idmap
, struct inode
*dir
,
3279 struct dentry
*dentry
, umode_t mode
, bool want_excl
)
3283 error
= may_create(idmap
, dir
, dentry
);
3287 if (!dir
->i_op
->create
)
3288 return -EACCES
; /* shouldn't it be ENOSYS? */
3290 mode
= vfs_prepare_mode(idmap
, dir
, mode
, S_IALLUGO
, S_IFREG
);
3291 error
= security_inode_create(dir
, dentry
, mode
);
3294 error
= dir
->i_op
->create(idmap
, dir
, dentry
, mode
, want_excl
);
3296 fsnotify_create(dir
, dentry
);
3299 EXPORT_SYMBOL(vfs_create
);
3301 int vfs_mkobj(struct dentry
*dentry
, umode_t mode
,
3302 int (*f
)(struct dentry
*, umode_t
, void *),
3305 struct inode
*dir
= dentry
->d_parent
->d_inode
;
3306 int error
= may_create(&nop_mnt_idmap
, dir
, dentry
);
3312 error
= security_inode_create(dir
, dentry
, mode
);
3315 error
= f(dentry
, mode
, arg
);
3317 fsnotify_create(dir
, dentry
);
3320 EXPORT_SYMBOL(vfs_mkobj
);
3322 bool may_open_dev(const struct path
*path
)
3324 return !(path
->mnt
->mnt_flags
& MNT_NODEV
) &&
3325 !(path
->mnt
->mnt_sb
->s_iflags
& SB_I_NODEV
);
3328 static int may_open(struct mnt_idmap
*idmap
, const struct path
*path
,
3329 int acc_mode
, int flag
)
3331 struct dentry
*dentry
= path
->dentry
;
3332 struct inode
*inode
= dentry
->d_inode
;
3338 switch (inode
->i_mode
& S_IFMT
) {
3342 if (acc_mode
& MAY_WRITE
)
3344 if (acc_mode
& MAY_EXEC
)
3349 if (!may_open_dev(path
))
3354 if (acc_mode
& MAY_EXEC
)
3359 if ((acc_mode
& MAY_EXEC
) && path_noexec(path
))
3364 error
= inode_permission(idmap
, inode
, MAY_OPEN
| acc_mode
);
3369 * An append-only file must be opened in append mode for writing.
3371 if (IS_APPEND(inode
)) {
3372 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
3378 /* O_NOATIME can only be set by the owner or superuser */
3379 if (flag
& O_NOATIME
&& !inode_owner_or_capable(idmap
, inode
))
3385 static int handle_truncate(struct mnt_idmap
*idmap
, struct file
*filp
)
3387 const struct path
*path
= &filp
->f_path
;
3388 struct inode
*inode
= path
->dentry
->d_inode
;
3389 int error
= get_write_access(inode
);
3393 error
= security_file_truncate(filp
);
3395 error
= do_truncate(idmap
, path
->dentry
, 0,
3396 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
3399 put_write_access(inode
);
3403 static inline int open_to_namei_flags(int flag
)
3405 if ((flag
& O_ACCMODE
) == 3)
3410 static int may_o_create(struct mnt_idmap
*idmap
,
3411 const struct path
*dir
, struct dentry
*dentry
,
3414 int error
= security_path_mknod(dir
, dentry
, mode
, 0);
3418 if (!fsuidgid_has_mapping(dir
->dentry
->d_sb
, idmap
))
3421 error
= inode_permission(idmap
, dir
->dentry
->d_inode
,
3422 MAY_WRITE
| MAY_EXEC
);
3426 return security_inode_create(dir
->dentry
->d_inode
, dentry
, mode
);
3430 * Attempt to atomically look up, create and open a file from a negative
3433 * Returns 0 if successful. The file will have been created and attached to
3434 * @file by the filesystem calling finish_open().
3436 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3437 * be set. The caller will need to perform the open themselves. @path will
3438 * have been updated to point to the new dentry. This may be negative.
3440 * Returns an error code otherwise.
3442 static struct dentry
*atomic_open(struct nameidata
*nd
, struct dentry
*dentry
,
3444 int open_flag
, umode_t mode
)
3446 struct dentry
*const DENTRY_NOT_SET
= (void *) -1UL;
3447 struct inode
*dir
= nd
->path
.dentry
->d_inode
;
3450 if (nd
->flags
& LOOKUP_DIRECTORY
)
3451 open_flag
|= O_DIRECTORY
;
3453 file
->f_path
.dentry
= DENTRY_NOT_SET
;
3454 file
->f_path
.mnt
= nd
->path
.mnt
;
3455 error
= dir
->i_op
->atomic_open(dir
, dentry
, file
,
3456 open_to_namei_flags(open_flag
), mode
);
3457 d_lookup_done(dentry
);
3459 if (file
->f_mode
& FMODE_OPENED
) {
3460 if (unlikely(dentry
!= file
->f_path
.dentry
)) {
3462 dentry
= dget(file
->f_path
.dentry
);
3464 } else if (WARN_ON(file
->f_path
.dentry
== DENTRY_NOT_SET
)) {
3467 if (file
->f_path
.dentry
) {
3469 dentry
= file
->f_path
.dentry
;
3471 if (unlikely(d_is_negative(dentry
)))
3477 dentry
= ERR_PTR(error
);
3483 * Look up and maybe create and open the last component.
3485 * Must be called with parent locked (exclusive in O_CREAT case).
3487 * Returns 0 on success, that is, if
3488 * the file was successfully atomically created (if necessary) and opened, or
3489 * the file was not completely opened at this time, though lookups and
3490 * creations were performed.
3491 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3492 * In the latter case dentry returned in @path might be negative if O_CREAT
3493 * hadn't been specified.
3495 * An error code is returned on failure.
3497 static struct dentry
*lookup_open(struct nameidata
*nd
, struct file
*file
,
3498 const struct open_flags
*op
,
3501 struct mnt_idmap
*idmap
;
3502 struct dentry
*dir
= nd
->path
.dentry
;
3503 struct inode
*dir_inode
= dir
->d_inode
;
3504 int open_flag
= op
->open_flag
;
3505 struct dentry
*dentry
;
3506 int error
, create_error
= 0;
3507 umode_t mode
= op
->mode
;
3508 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
3510 if (unlikely(IS_DEADDIR(dir_inode
)))
3511 return ERR_PTR(-ENOENT
);
3513 file
->f_mode
&= ~FMODE_CREATED
;
3514 dentry
= d_lookup(dir
, &nd
->last
);
3517 dentry
= d_alloc_parallel(dir
, &nd
->last
, &wq
);
3521 if (d_in_lookup(dentry
))
3524 error
= d_revalidate(dentry
, nd
->flags
);
3525 if (likely(error
> 0))
3529 d_invalidate(dentry
);
3533 if (dentry
->d_inode
) {
3534 /* Cached positive dentry: will open in f_op->open */
3538 if (open_flag
& O_CREAT
)
3539 audit_inode(nd
->name
, dir
, AUDIT_INODE_PARENT
);
3542 * Checking write permission is tricky, bacuse we don't know if we are
3543 * going to actually need it: O_CREAT opens should work as long as the
3544 * file exists. But checking existence breaks atomicity. The trick is
3545 * to check access and if not granted clear O_CREAT from the flags.
3547 * Another problem is returing the "right" error value (e.g. for an
3548 * O_EXCL open we want to return EEXIST not EROFS).
3550 if (unlikely(!got_write
))
3551 open_flag
&= ~O_TRUNC
;
3552 idmap
= mnt_idmap(nd
->path
.mnt
);
3553 if (open_flag
& O_CREAT
) {
3554 if (open_flag
& O_EXCL
)
3555 open_flag
&= ~O_TRUNC
;
3556 mode
= vfs_prepare_mode(idmap
, dir
->d_inode
, mode
, mode
, mode
);
3557 if (likely(got_write
))
3558 create_error
= may_o_create(idmap
, &nd
->path
,
3561 create_error
= -EROFS
;
3564 open_flag
&= ~O_CREAT
;
3565 if (dir_inode
->i_op
->atomic_open
) {
3566 dentry
= atomic_open(nd
, dentry
, file
, open_flag
, mode
);
3567 if (unlikely(create_error
) && dentry
== ERR_PTR(-ENOENT
))
3568 dentry
= ERR_PTR(create_error
);
3572 if (d_in_lookup(dentry
)) {
3573 struct dentry
*res
= dir_inode
->i_op
->lookup(dir_inode
, dentry
,
3575 d_lookup_done(dentry
);
3576 if (unlikely(res
)) {
3578 error
= PTR_ERR(res
);
3586 /* Negative dentry, just create the file */
3587 if (!dentry
->d_inode
&& (open_flag
& O_CREAT
)) {
3588 file
->f_mode
|= FMODE_CREATED
;
3589 audit_inode_child(dir_inode
, dentry
, AUDIT_TYPE_CHILD_CREATE
);
3590 if (!dir_inode
->i_op
->create
) {
3595 error
= dir_inode
->i_op
->create(idmap
, dir_inode
, dentry
,
3596 mode
, open_flag
& O_EXCL
);
3600 if (unlikely(create_error
) && !dentry
->d_inode
) {
3601 error
= create_error
;
3608 return ERR_PTR(error
);
3611 static inline bool trailing_slashes(struct nameidata
*nd
)
3613 return (bool)nd
->last
.name
[nd
->last
.len
];
3616 static struct dentry
*lookup_fast_for_open(struct nameidata
*nd
, int open_flag
)
3618 struct dentry
*dentry
;
3620 if (open_flag
& O_CREAT
) {
3621 if (trailing_slashes(nd
))
3622 return ERR_PTR(-EISDIR
);
3624 /* Don't bother on an O_EXCL create */
3625 if (open_flag
& O_EXCL
)
3629 if (trailing_slashes(nd
))
3630 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
3632 dentry
= lookup_fast(nd
);
3633 if (IS_ERR_OR_NULL(dentry
))
3636 if (open_flag
& O_CREAT
) {
3637 /* Discard negative dentries. Need inode_lock to do the create */
3638 if (!dentry
->d_inode
) {
3639 if (!(nd
->flags
& LOOKUP_RCU
))
3647 static const char *open_last_lookups(struct nameidata
*nd
,
3648 struct file
*file
, const struct open_flags
*op
)
3650 struct dentry
*dir
= nd
->path
.dentry
;
3651 int open_flag
= op
->open_flag
;
3652 bool got_write
= false;
3653 struct dentry
*dentry
;
3656 nd
->flags
|= op
->intent
;
3658 if (nd
->last_type
!= LAST_NORM
) {
3661 return handle_dots(nd
, nd
->last_type
);
3664 /* We _can_ be in RCU mode here */
3665 dentry
= lookup_fast_for_open(nd
, open_flag
);
3667 return ERR_CAST(dentry
);
3672 if (!(open_flag
& O_CREAT
)) {
3673 if (WARN_ON_ONCE(nd
->flags
& LOOKUP_RCU
))
3674 return ERR_PTR(-ECHILD
);
3676 if (nd
->flags
& LOOKUP_RCU
) {
3677 if (!try_to_unlazy(nd
))
3678 return ERR_PTR(-ECHILD
);
3682 if (open_flag
& (O_CREAT
| O_TRUNC
| O_WRONLY
| O_RDWR
)) {
3683 got_write
= !mnt_want_write(nd
->path
.mnt
);
3685 * do _not_ fail yet - we might not need that or fail with
3686 * a different error; let lookup_open() decide; we'll be
3687 * dropping this one anyway.
3690 if (open_flag
& O_CREAT
)
3691 inode_lock(dir
->d_inode
);
3693 inode_lock_shared(dir
->d_inode
);
3694 dentry
= lookup_open(nd
, file
, op
, got_write
);
3695 if (!IS_ERR(dentry
)) {
3696 if (file
->f_mode
& FMODE_CREATED
)
3697 fsnotify_create(dir
->d_inode
, dentry
);
3698 if (file
->f_mode
& FMODE_OPENED
)
3699 fsnotify_open(file
);
3701 if (open_flag
& O_CREAT
)
3702 inode_unlock(dir
->d_inode
);
3704 inode_unlock_shared(dir
->d_inode
);
3707 mnt_drop_write(nd
->path
.mnt
);
3710 return ERR_CAST(dentry
);
3712 if (file
->f_mode
& (FMODE_OPENED
| FMODE_CREATED
)) {
3713 dput(nd
->path
.dentry
);
3714 nd
->path
.dentry
= dentry
;
3721 res
= step_into(nd
, WALK_TRAILING
, dentry
);
3723 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
3728 * Handle the last step of open()
3730 static int do_open(struct nameidata
*nd
,
3731 struct file
*file
, const struct open_flags
*op
)
3733 struct mnt_idmap
*idmap
;
3734 int open_flag
= op
->open_flag
;
3739 if (!(file
->f_mode
& (FMODE_OPENED
| FMODE_CREATED
))) {
3740 error
= complete_walk(nd
);
3744 if (!(file
->f_mode
& FMODE_CREATED
))
3745 audit_inode(nd
->name
, nd
->path
.dentry
, 0);
3746 idmap
= mnt_idmap(nd
->path
.mnt
);
3747 if (open_flag
& O_CREAT
) {
3748 if ((open_flag
& O_EXCL
) && !(file
->f_mode
& FMODE_CREATED
))
3750 if (d_is_dir(nd
->path
.dentry
))
3752 error
= may_create_in_sticky(idmap
, nd
,
3753 d_backing_inode(nd
->path
.dentry
));
3754 if (unlikely(error
))
3757 if ((nd
->flags
& LOOKUP_DIRECTORY
) && !d_can_lookup(nd
->path
.dentry
))
3760 do_truncate
= false;
3761 acc_mode
= op
->acc_mode
;
3762 if (file
->f_mode
& FMODE_CREATED
) {
3763 /* Don't check for write permission, don't truncate */
3764 open_flag
&= ~O_TRUNC
;
3766 } else if (d_is_reg(nd
->path
.dentry
) && open_flag
& O_TRUNC
) {
3767 error
= mnt_want_write(nd
->path
.mnt
);
3772 error
= may_open(idmap
, &nd
->path
, acc_mode
, open_flag
);
3773 if (!error
&& !(file
->f_mode
& FMODE_OPENED
))
3774 error
= vfs_open(&nd
->path
, file
);
3776 error
= security_file_post_open(file
, op
->acc_mode
);
3777 if (!error
&& do_truncate
)
3778 error
= handle_truncate(idmap
, file
);
3779 if (unlikely(error
> 0)) {
3784 mnt_drop_write(nd
->path
.mnt
);
3789 * vfs_tmpfile - create tmpfile
3790 * @idmap: idmap of the mount the inode was found from
3791 * @parentpath: pointer to the path of the base directory
3792 * @file: file descriptor of the new tmpfile
3793 * @mode: mode of the new tmpfile
3795 * Create a temporary file.
3797 * If the inode has been found through an idmapped mount the idmap of
3798 * the vfsmount must be passed through @idmap. This function will then take
3799 * care to map the inode according to @idmap before checking permissions.
3800 * On non-idmapped mounts or if permission checking is to be performed on the
3801 * raw inode simply pass @nop_mnt_idmap.
3803 int vfs_tmpfile(struct mnt_idmap
*idmap
,
3804 const struct path
*parentpath
,
3805 struct file
*file
, umode_t mode
)
3807 struct dentry
*child
;
3808 struct inode
*dir
= d_inode(parentpath
->dentry
);
3809 struct inode
*inode
;
3811 int open_flag
= file
->f_flags
;
3813 /* we want directory to be writable */
3814 error
= inode_permission(idmap
, dir
, MAY_WRITE
| MAY_EXEC
);
3817 if (!dir
->i_op
->tmpfile
)
3819 child
= d_alloc(parentpath
->dentry
, &slash_name
);
3820 if (unlikely(!child
))
3822 file
->f_path
.mnt
= parentpath
->mnt
;
3823 file
->f_path
.dentry
= child
;
3824 mode
= vfs_prepare_mode(idmap
, dir
, mode
, mode
, mode
);
3825 error
= dir
->i_op
->tmpfile(idmap
, dir
, file
, mode
);
3827 if (file
->f_mode
& FMODE_OPENED
)
3828 fsnotify_open(file
);
3831 /* Don't check for other permissions, the inode was just created */
3832 error
= may_open(idmap
, &file
->f_path
, 0, file
->f_flags
);
3835 inode
= file_inode(file
);
3836 if (!(open_flag
& O_EXCL
)) {
3837 spin_lock(&inode
->i_lock
);
3838 inode
->i_state
|= I_LINKABLE
;
3839 spin_unlock(&inode
->i_lock
);
3841 security_inode_post_create_tmpfile(idmap
, inode
);
3846 * kernel_tmpfile_open - open a tmpfile for kernel internal use
3847 * @idmap: idmap of the mount the inode was found from
3848 * @parentpath: path of the base directory
3849 * @mode: mode of the new tmpfile
3851 * @cred: credentials for open
3853 * Create and open a temporary file. The file is not accounted in nr_files,
3854 * hence this is only for kernel internal use, and must not be installed into
3855 * file tables or such.
3857 struct file
*kernel_tmpfile_open(struct mnt_idmap
*idmap
,
3858 const struct path
*parentpath
,
3859 umode_t mode
, int open_flag
,
3860 const struct cred
*cred
)
3865 file
= alloc_empty_file_noaccount(open_flag
, cred
);
3869 error
= vfs_tmpfile(idmap
, parentpath
, file
, mode
);
3872 file
= ERR_PTR(error
);
3876 EXPORT_SYMBOL(kernel_tmpfile_open
);
3878 static int do_tmpfile(struct nameidata
*nd
, unsigned flags
,
3879 const struct open_flags
*op
,
3883 int error
= path_lookupat(nd
, flags
| LOOKUP_DIRECTORY
, &path
);
3885 if (unlikely(error
))
3887 error
= mnt_want_write(path
.mnt
);
3888 if (unlikely(error
))
3890 error
= vfs_tmpfile(mnt_idmap(path
.mnt
), &path
, file
, op
->mode
);
3893 audit_inode(nd
->name
, file
->f_path
.dentry
, 0);
3895 mnt_drop_write(path
.mnt
);
3901 static int do_o_path(struct nameidata
*nd
, unsigned flags
, struct file
*file
)
3904 int error
= path_lookupat(nd
, flags
, &path
);
3906 audit_inode(nd
->name
, path
.dentry
, 0);
3907 error
= vfs_open(&path
, file
);
3913 static struct file
*path_openat(struct nameidata
*nd
,
3914 const struct open_flags
*op
, unsigned flags
)
3919 file
= alloc_empty_file(op
->open_flag
, current_cred());
3923 if (unlikely(file
->f_flags
& __O_TMPFILE
)) {
3924 error
= do_tmpfile(nd
, flags
, op
, file
);
3925 } else if (unlikely(file
->f_flags
& O_PATH
)) {
3926 error
= do_o_path(nd
, flags
, file
);
3928 const char *s
= path_init(nd
, flags
);
3929 while (!(error
= link_path_walk(s
, nd
)) &&
3930 (s
= open_last_lookups(nd
, file
, op
)) != NULL
)
3933 error
= do_open(nd
, file
, op
);
3936 if (likely(!error
)) {
3937 if (likely(file
->f_mode
& FMODE_OPENED
))
3943 if (error
== -EOPENSTALE
) {
3944 if (flags
& LOOKUP_RCU
)
3949 return ERR_PTR(error
);
3952 struct file
*do_filp_open(int dfd
, struct filename
*pathname
,
3953 const struct open_flags
*op
)
3955 struct nameidata nd
;
3956 int flags
= op
->lookup_flags
;
3959 set_nameidata(&nd
, dfd
, pathname
, NULL
);
3960 filp
= path_openat(&nd
, op
, flags
| LOOKUP_RCU
);
3961 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
3962 filp
= path_openat(&nd
, op
, flags
);
3963 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
3964 filp
= path_openat(&nd
, op
, flags
| LOOKUP_REVAL
);
3965 restore_nameidata();
3969 struct file
*do_file_open_root(const struct path
*root
,
3970 const char *name
, const struct open_flags
*op
)
3972 struct nameidata nd
;
3974 struct filename
*filename
;
3975 int flags
= op
->lookup_flags
;
3977 if (d_is_symlink(root
->dentry
) && op
->intent
& LOOKUP_OPEN
)
3978 return ERR_PTR(-ELOOP
);
3980 filename
= getname_kernel(name
);
3981 if (IS_ERR(filename
))
3982 return ERR_CAST(filename
);
3984 set_nameidata(&nd
, -1, filename
, root
);
3985 file
= path_openat(&nd
, op
, flags
| LOOKUP_RCU
);
3986 if (unlikely(file
== ERR_PTR(-ECHILD
)))
3987 file
= path_openat(&nd
, op
, flags
);
3988 if (unlikely(file
== ERR_PTR(-ESTALE
)))
3989 file
= path_openat(&nd
, op
, flags
| LOOKUP_REVAL
);
3990 restore_nameidata();
3995 static struct dentry
*filename_create(int dfd
, struct filename
*name
,
3996 struct path
*path
, unsigned int lookup_flags
)
3998 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
4000 bool want_dir
= lookup_flags
& LOOKUP_DIRECTORY
;
4001 unsigned int reval_flag
= lookup_flags
& LOOKUP_REVAL
;
4002 unsigned int create_flags
= LOOKUP_CREATE
| LOOKUP_EXCL
;
4007 error
= filename_parentat(dfd
, name
, reval_flag
, path
, &last
, &type
);
4009 return ERR_PTR(error
);
4012 * Yucky last component or no last component at all?
4013 * (foo/., foo/.., /////)
4015 if (unlikely(type
!= LAST_NORM
))
4018 /* don't fail immediately if it's r/o, at least try to report other errors */
4019 err2
= mnt_want_write(path
->mnt
);
4021 * Do the final lookup. Suppress 'create' if there is a trailing
4022 * '/', and a directory wasn't requested.
4024 if (last
.name
[last
.len
] && !want_dir
)
4026 inode_lock_nested(path
->dentry
->d_inode
, I_MUTEX_PARENT
);
4027 dentry
= lookup_one_qstr_excl(&last
, path
->dentry
,
4028 reval_flag
| create_flags
);
4033 if (d_is_positive(dentry
))
4037 * Special case - lookup gave negative, but... we had foo/bar/
4038 * From the vfs_mknod() POV we just have a negative dentry -
4039 * all is fine. Let's be bastards - you had / on the end, you've
4040 * been asking for (non-existent) directory. -ENOENT for you.
4042 if (unlikely(!create_flags
)) {
4046 if (unlikely(err2
)) {
4053 dentry
= ERR_PTR(error
);
4055 inode_unlock(path
->dentry
->d_inode
);
4057 mnt_drop_write(path
->mnt
);
4063 struct dentry
*kern_path_create(int dfd
, const char *pathname
,
4064 struct path
*path
, unsigned int lookup_flags
)
4066 struct filename
*filename
= getname_kernel(pathname
);
4067 struct dentry
*res
= filename_create(dfd
, filename
, path
, lookup_flags
);
4072 EXPORT_SYMBOL(kern_path_create
);
4074 void done_path_create(struct path
*path
, struct dentry
*dentry
)
4077 inode_unlock(path
->dentry
->d_inode
);
4078 mnt_drop_write(path
->mnt
);
4081 EXPORT_SYMBOL(done_path_create
);
4083 inline struct dentry
*user_path_create(int dfd
, const char __user
*pathname
,
4084 struct path
*path
, unsigned int lookup_flags
)
4086 struct filename
*filename
= getname(pathname
);
4087 struct dentry
*res
= filename_create(dfd
, filename
, path
, lookup_flags
);
4092 EXPORT_SYMBOL(user_path_create
);
4095 * vfs_mknod - create device node or file
4096 * @idmap: idmap of the mount the inode was found from
4097 * @dir: inode of the parent directory
4098 * @dentry: dentry of the child device node
4099 * @mode: mode of the child device node
4100 * @dev: device number of device to create
4102 * Create a device node or file.
4104 * If the inode has been found through an idmapped mount the idmap of
4105 * the vfsmount must be passed through @idmap. This function will then take
4106 * care to map the inode according to @idmap before checking permissions.
4107 * On non-idmapped mounts or if permission checking is to be performed on the
4108 * raw inode simply pass @nop_mnt_idmap.
4110 int vfs_mknod(struct mnt_idmap
*idmap
, struct inode
*dir
,
4111 struct dentry
*dentry
, umode_t mode
, dev_t dev
)
4113 bool is_whiteout
= S_ISCHR(mode
) && dev
== WHITEOUT_DEV
;
4114 int error
= may_create(idmap
, dir
, dentry
);
4119 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !is_whiteout
&&
4120 !capable(CAP_MKNOD
))
4123 if (!dir
->i_op
->mknod
)
4126 mode
= vfs_prepare_mode(idmap
, dir
, mode
, mode
, mode
);
4127 error
= devcgroup_inode_mknod(mode
, dev
);
4131 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
4135 error
= dir
->i_op
->mknod(idmap
, dir
, dentry
, mode
, dev
);
4137 fsnotify_create(dir
, dentry
);
4140 EXPORT_SYMBOL(vfs_mknod
);
4142 static int may_mknod(umode_t mode
)
4144 switch (mode
& S_IFMT
) {
4150 case 0: /* zero mode translates to S_IFREG */
4159 static int do_mknodat(int dfd
, struct filename
*name
, umode_t mode
,
4162 struct mnt_idmap
*idmap
;
4163 struct dentry
*dentry
;
4166 unsigned int lookup_flags
= 0;
4168 error
= may_mknod(mode
);
4172 dentry
= filename_create(dfd
, name
, &path
, lookup_flags
);
4173 error
= PTR_ERR(dentry
);
4177 error
= security_path_mknod(&path
, dentry
,
4178 mode_strip_umask(path
.dentry
->d_inode
, mode
), dev
);
4182 idmap
= mnt_idmap(path
.mnt
);
4183 switch (mode
& S_IFMT
) {
4184 case 0: case S_IFREG
:
4185 error
= vfs_create(idmap
, path
.dentry
->d_inode
,
4186 dentry
, mode
, true);
4188 security_path_post_mknod(idmap
, dentry
);
4190 case S_IFCHR
: case S_IFBLK
:
4191 error
= vfs_mknod(idmap
, path
.dentry
->d_inode
,
4192 dentry
, mode
, new_decode_dev(dev
));
4194 case S_IFIFO
: case S_IFSOCK
:
4195 error
= vfs_mknod(idmap
, path
.dentry
->d_inode
,
4200 done_path_create(&path
, dentry
);
4201 if (retry_estale(error
, lookup_flags
)) {
4202 lookup_flags
|= LOOKUP_REVAL
;
4210 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, umode_t
, mode
,
4213 return do_mknodat(dfd
, getname(filename
), mode
, dev
);
4216 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, umode_t
, mode
, unsigned, dev
)
4218 return do_mknodat(AT_FDCWD
, getname(filename
), mode
, dev
);
4222 * vfs_mkdir - create directory
4223 * @idmap: idmap of the mount the inode was found from
4224 * @dir: inode of the parent directory
4225 * @dentry: dentry of the child directory
4226 * @mode: mode of the child directory
4228 * Create a directory.
4230 * If the inode has been found through an idmapped mount the idmap of
4231 * the vfsmount must be passed through @idmap. This function will then take
4232 * care to map the inode according to @idmap before checking permissions.
4233 * On non-idmapped mounts or if permission checking is to be performed on the
4234 * raw inode simply pass @nop_mnt_idmap.
4236 int vfs_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
4237 struct dentry
*dentry
, umode_t mode
)
4240 unsigned max_links
= dir
->i_sb
->s_max_links
;
4242 error
= may_create(idmap
, dir
, dentry
);
4246 if (!dir
->i_op
->mkdir
)
4249 mode
= vfs_prepare_mode(idmap
, dir
, mode
, S_IRWXUGO
| S_ISVTX
, 0);
4250 error
= security_inode_mkdir(dir
, dentry
, mode
);
4254 if (max_links
&& dir
->i_nlink
>= max_links
)
4257 error
= dir
->i_op
->mkdir(idmap
, dir
, dentry
, mode
);
4259 fsnotify_mkdir(dir
, dentry
);
4262 EXPORT_SYMBOL(vfs_mkdir
);
4264 int do_mkdirat(int dfd
, struct filename
*name
, umode_t mode
)
4266 struct dentry
*dentry
;
4269 unsigned int lookup_flags
= LOOKUP_DIRECTORY
;
4272 dentry
= filename_create(dfd
, name
, &path
, lookup_flags
);
4273 error
= PTR_ERR(dentry
);
4277 error
= security_path_mkdir(&path
, dentry
,
4278 mode_strip_umask(path
.dentry
->d_inode
, mode
));
4280 error
= vfs_mkdir(mnt_idmap(path
.mnt
), path
.dentry
->d_inode
,
4283 done_path_create(&path
, dentry
);
4284 if (retry_estale(error
, lookup_flags
)) {
4285 lookup_flags
|= LOOKUP_REVAL
;
4293 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, umode_t
, mode
)
4295 return do_mkdirat(dfd
, getname(pathname
), mode
);
4298 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, umode_t
, mode
)
4300 return do_mkdirat(AT_FDCWD
, getname(pathname
), mode
);
4304 * vfs_rmdir - remove directory
4305 * @idmap: idmap of the mount the inode was found from
4306 * @dir: inode of the parent directory
4307 * @dentry: dentry of the child directory
4309 * Remove a directory.
4311 * If the inode has been found through an idmapped mount the idmap of
4312 * the vfsmount must be passed through @idmap. This function will then take
4313 * care to map the inode according to @idmap before checking permissions.
4314 * On non-idmapped mounts or if permission checking is to be performed on the
4315 * raw inode simply pass @nop_mnt_idmap.
4317 int vfs_rmdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
4318 struct dentry
*dentry
)
4320 int error
= may_delete(idmap
, dir
, dentry
, 1);
4325 if (!dir
->i_op
->rmdir
)
4329 inode_lock(dentry
->d_inode
);
4332 if (is_local_mountpoint(dentry
) ||
4333 (dentry
->d_inode
->i_flags
& S_KERNEL_FILE
))
4336 error
= security_inode_rmdir(dir
, dentry
);
4340 error
= dir
->i_op
->rmdir(dir
, dentry
);
4344 shrink_dcache_parent(dentry
);
4345 dentry
->d_inode
->i_flags
|= S_DEAD
;
4347 detach_mounts(dentry
);
4350 inode_unlock(dentry
->d_inode
);
4353 d_delete_notify(dir
, dentry
);
4356 EXPORT_SYMBOL(vfs_rmdir
);
4358 int do_rmdir(int dfd
, struct filename
*name
)
4361 struct dentry
*dentry
;
4365 unsigned int lookup_flags
= 0;
4367 error
= filename_parentat(dfd
, name
, lookup_flags
, &path
, &last
, &type
);
4383 error
= mnt_want_write(path
.mnt
);
4387 inode_lock_nested(path
.dentry
->d_inode
, I_MUTEX_PARENT
);
4388 dentry
= lookup_one_qstr_excl(&last
, path
.dentry
, lookup_flags
);
4389 error
= PTR_ERR(dentry
);
4392 if (!dentry
->d_inode
) {
4396 error
= security_path_rmdir(&path
, dentry
);
4399 error
= vfs_rmdir(mnt_idmap(path
.mnt
), path
.dentry
->d_inode
, dentry
);
4403 inode_unlock(path
.dentry
->d_inode
);
4404 mnt_drop_write(path
.mnt
);
4407 if (retry_estale(error
, lookup_flags
)) {
4408 lookup_flags
|= LOOKUP_REVAL
;
4416 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
4418 return do_rmdir(AT_FDCWD
, getname(pathname
));
4422 * vfs_unlink - unlink a filesystem object
4423 * @idmap: idmap of the mount the inode was found from
4424 * @dir: parent directory
4426 * @delegated_inode: returns victim inode, if the inode is delegated.
4428 * The caller must hold dir->i_mutex.
4430 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
4431 * return a reference to the inode in delegated_inode. The caller
4432 * should then break the delegation on that inode and retry. Because
4433 * breaking a delegation may take a long time, the caller should drop
4434 * dir->i_mutex before doing so.
4436 * Alternatively, a caller may pass NULL for delegated_inode. This may
4437 * be appropriate for callers that expect the underlying filesystem not
4438 * to be NFS exported.
4440 * If the inode has been found through an idmapped mount the idmap of
4441 * the vfsmount must be passed through @idmap. This function will then take
4442 * care to map the inode according to @idmap before checking permissions.
4443 * On non-idmapped mounts or if permission checking is to be performed on the
4444 * raw inode simply pass @nop_mnt_idmap.
4446 int vfs_unlink(struct mnt_idmap
*idmap
, struct inode
*dir
,
4447 struct dentry
*dentry
, struct inode
**delegated_inode
)
4449 struct inode
*target
= dentry
->d_inode
;
4450 int error
= may_delete(idmap
, dir
, dentry
, 0);
4455 if (!dir
->i_op
->unlink
)
4459 if (IS_SWAPFILE(target
))
4461 else if (is_local_mountpoint(dentry
))
4464 error
= security_inode_unlink(dir
, dentry
);
4466 error
= try_break_deleg(target
, delegated_inode
);
4469 error
= dir
->i_op
->unlink(dir
, dentry
);
4472 detach_mounts(dentry
);
4477 inode_unlock(target
);
4479 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4480 if (!error
&& dentry
->d_flags
& DCACHE_NFSFS_RENAMED
) {
4481 fsnotify_unlink(dir
, dentry
);
4482 } else if (!error
) {
4483 fsnotify_link_count(target
);
4484 d_delete_notify(dir
, dentry
);
4489 EXPORT_SYMBOL(vfs_unlink
);
4492 * Make sure that the actual truncation of the file will occur outside its
4493 * directory's i_mutex. Truncate can take a long time if there is a lot of
4494 * writeout happening, and we don't want to prevent access to the directory
4495 * while waiting on the I/O.
4497 int do_unlinkat(int dfd
, struct filename
*name
)
4500 struct dentry
*dentry
;
4504 struct inode
*inode
= NULL
;
4505 struct inode
*delegated_inode
= NULL
;
4506 unsigned int lookup_flags
= 0;
4508 error
= filename_parentat(dfd
, name
, lookup_flags
, &path
, &last
, &type
);
4513 if (type
!= LAST_NORM
)
4516 error
= mnt_want_write(path
.mnt
);
4520 inode_lock_nested(path
.dentry
->d_inode
, I_MUTEX_PARENT
);
4521 dentry
= lookup_one_qstr_excl(&last
, path
.dentry
, lookup_flags
);
4522 error
= PTR_ERR(dentry
);
4523 if (!IS_ERR(dentry
)) {
4525 /* Why not before? Because we want correct error value */
4526 if (last
.name
[last
.len
] || d_is_negative(dentry
))
4528 inode
= dentry
->d_inode
;
4530 error
= security_path_unlink(&path
, dentry
);
4533 error
= vfs_unlink(mnt_idmap(path
.mnt
), path
.dentry
->d_inode
,
4534 dentry
, &delegated_inode
);
4538 inode_unlock(path
.dentry
->d_inode
);
4540 iput(inode
); /* truncate the inode here */
4542 if (delegated_inode
) {
4543 error
= break_deleg_wait(&delegated_inode
);
4547 mnt_drop_write(path
.mnt
);
4550 if (retry_estale(error
, lookup_flags
)) {
4551 lookup_flags
|= LOOKUP_REVAL
;
4560 if (d_is_negative(dentry
))
4562 else if (d_is_dir(dentry
))
4569 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
4571 if ((flag
& ~AT_REMOVEDIR
) != 0)
4574 if (flag
& AT_REMOVEDIR
)
4575 return do_rmdir(dfd
, getname(pathname
));
4576 return do_unlinkat(dfd
, getname(pathname
));
4579 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
4581 return do_unlinkat(AT_FDCWD
, getname(pathname
));
4585 * vfs_symlink - create symlink
4586 * @idmap: idmap of the mount the inode was found from
4587 * @dir: inode of the parent directory
4588 * @dentry: dentry of the child symlink file
4589 * @oldname: name of the file to link to
4593 * If the inode has been found through an idmapped mount the idmap of
4594 * the vfsmount must be passed through @idmap. This function will then take
4595 * care to map the inode according to @idmap before checking permissions.
4596 * On non-idmapped mounts or if permission checking is to be performed on the
4597 * raw inode simply pass @nop_mnt_idmap.
4599 int vfs_symlink(struct mnt_idmap
*idmap
, struct inode
*dir
,
4600 struct dentry
*dentry
, const char *oldname
)
4604 error
= may_create(idmap
, dir
, dentry
);
4608 if (!dir
->i_op
->symlink
)
4611 error
= security_inode_symlink(dir
, dentry
, oldname
);
4615 error
= dir
->i_op
->symlink(idmap
, dir
, dentry
, oldname
);
4617 fsnotify_create(dir
, dentry
);
4620 EXPORT_SYMBOL(vfs_symlink
);
4622 int do_symlinkat(struct filename
*from
, int newdfd
, struct filename
*to
)
4625 struct dentry
*dentry
;
4627 unsigned int lookup_flags
= 0;
4630 error
= PTR_ERR(from
);
4634 dentry
= filename_create(newdfd
, to
, &path
, lookup_flags
);
4635 error
= PTR_ERR(dentry
);
4639 error
= security_path_symlink(&path
, dentry
, from
->name
);
4641 error
= vfs_symlink(mnt_idmap(path
.mnt
), path
.dentry
->d_inode
,
4642 dentry
, from
->name
);
4643 done_path_create(&path
, dentry
);
4644 if (retry_estale(error
, lookup_flags
)) {
4645 lookup_flags
|= LOOKUP_REVAL
;
4654 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
4655 int, newdfd
, const char __user
*, newname
)
4657 return do_symlinkat(getname(oldname
), newdfd
, getname(newname
));
4660 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
4662 return do_symlinkat(getname(oldname
), AT_FDCWD
, getname(newname
));
4666 * vfs_link - create a new link
4667 * @old_dentry: object to be linked
4668 * @idmap: idmap of the mount
4670 * @new_dentry: where to create the new link
4671 * @delegated_inode: returns inode needing a delegation break
4673 * The caller must hold dir->i_mutex
4675 * If vfs_link discovers a delegation on the to-be-linked file in need
4676 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4677 * inode in delegated_inode. The caller should then break the delegation
4678 * and retry. Because breaking a delegation may take a long time, the
4679 * caller should drop the i_mutex before doing so.
4681 * Alternatively, a caller may pass NULL for delegated_inode. This may
4682 * be appropriate for callers that expect the underlying filesystem not
4683 * to be NFS exported.
4685 * If the inode has been found through an idmapped mount the idmap of
4686 * the vfsmount must be passed through @idmap. This function will then take
4687 * care to map the inode according to @idmap before checking permissions.
4688 * On non-idmapped mounts or if permission checking is to be performed on the
4689 * raw inode simply pass @nop_mnt_idmap.
4691 int vfs_link(struct dentry
*old_dentry
, struct mnt_idmap
*idmap
,
4692 struct inode
*dir
, struct dentry
*new_dentry
,
4693 struct inode
**delegated_inode
)
4695 struct inode
*inode
= old_dentry
->d_inode
;
4696 unsigned max_links
= dir
->i_sb
->s_max_links
;
4702 error
= may_create(idmap
, dir
, new_dentry
);
4706 if (dir
->i_sb
!= inode
->i_sb
)
4710 * A link to an append-only or immutable file cannot be created.
4712 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
4715 * Updating the link count will likely cause i_uid and i_gid to
4716 * be writen back improperly if their true value is unknown to
4719 if (HAS_UNMAPPED_ID(idmap
, inode
))
4721 if (!dir
->i_op
->link
)
4723 if (S_ISDIR(inode
->i_mode
))
4726 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
4731 /* Make sure we don't allow creating hardlink to an unlinked file */
4732 if (inode
->i_nlink
== 0 && !(inode
->i_state
& I_LINKABLE
))
4734 else if (max_links
&& inode
->i_nlink
>= max_links
)
4737 error
= try_break_deleg(inode
, delegated_inode
);
4739 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
4742 if (!error
&& (inode
->i_state
& I_LINKABLE
)) {
4743 spin_lock(&inode
->i_lock
);
4744 inode
->i_state
&= ~I_LINKABLE
;
4745 spin_unlock(&inode
->i_lock
);
4747 inode_unlock(inode
);
4749 fsnotify_link(dir
, inode
, new_dentry
);
4752 EXPORT_SYMBOL(vfs_link
);
4755 * Hardlinks are often used in delicate situations. We avoid
4756 * security-related surprises by not following symlinks on the
4759 * We don't follow them on the oldname either to be compatible
4760 * with linux 2.0, and to avoid hard-linking to directories
4761 * and other special files. --ADM
4763 int do_linkat(int olddfd
, struct filename
*old
, int newdfd
,
4764 struct filename
*new, int flags
)
4766 struct mnt_idmap
*idmap
;
4767 struct dentry
*new_dentry
;
4768 struct path old_path
, new_path
;
4769 struct inode
*delegated_inode
= NULL
;
4773 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0) {
4778 * To use null names we require CAP_DAC_READ_SEARCH or
4779 * that the open-time creds of the dfd matches current.
4780 * This ensures that not everyone will be able to create
4781 * a hardlink using the passed file descriptor.
4783 if (flags
& AT_EMPTY_PATH
)
4784 how
|= LOOKUP_LINKAT_EMPTY
;
4786 if (flags
& AT_SYMLINK_FOLLOW
)
4787 how
|= LOOKUP_FOLLOW
;
4789 error
= filename_lookup(olddfd
, old
, how
, &old_path
, NULL
);
4793 new_dentry
= filename_create(newdfd
, new, &new_path
,
4794 (how
& LOOKUP_REVAL
));
4795 error
= PTR_ERR(new_dentry
);
4796 if (IS_ERR(new_dentry
))
4800 if (old_path
.mnt
!= new_path
.mnt
)
4802 idmap
= mnt_idmap(new_path
.mnt
);
4803 error
= may_linkat(idmap
, &old_path
);
4804 if (unlikely(error
))
4806 error
= security_path_link(old_path
.dentry
, &new_path
, new_dentry
);
4809 error
= vfs_link(old_path
.dentry
, idmap
, new_path
.dentry
->d_inode
,
4810 new_dentry
, &delegated_inode
);
4812 done_path_create(&new_path
, new_dentry
);
4813 if (delegated_inode
) {
4814 error
= break_deleg_wait(&delegated_inode
);
4816 path_put(&old_path
);
4820 if (retry_estale(error
, how
)) {
4821 path_put(&old_path
);
4822 how
|= LOOKUP_REVAL
;
4826 path_put(&old_path
);
4834 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
4835 int, newdfd
, const char __user
*, newname
, int, flags
)
4837 return do_linkat(olddfd
, getname_uflags(oldname
, flags
),
4838 newdfd
, getname(newname
), flags
);
4841 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
4843 return do_linkat(AT_FDCWD
, getname(oldname
), AT_FDCWD
, getname(newname
), 0);
4847 * vfs_rename - rename a filesystem object
4848 * @rd: pointer to &struct renamedata info
4850 * The caller must hold multiple mutexes--see lock_rename()).
4852 * If vfs_rename discovers a delegation in need of breaking at either
4853 * the source or destination, it will return -EWOULDBLOCK and return a
4854 * reference to the inode in delegated_inode. The caller should then
4855 * break the delegation and retry. Because breaking a delegation may
4856 * take a long time, the caller should drop all locks before doing
4859 * Alternatively, a caller may pass NULL for delegated_inode. This may
4860 * be appropriate for callers that expect the underlying filesystem not
4861 * to be NFS exported.
4863 * The worst of all namespace operations - renaming directory. "Perverted"
4864 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4867 * a) we can get into loop creation.
4868 * b) race potential - two innocent renames can create a loop together.
4869 * That's where 4.4BSD screws up. Current fix: serialization on
4870 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4872 * c) we may have to lock up to _four_ objects - parents and victim (if it exists),
4873 * and source (if it's a non-directory or a subdirectory that moves to
4874 * different parent).
4875 * And that - after we got ->i_mutex on parents (until then we don't know
4876 * whether the target exists). Solution: try to be smart with locking
4877 * order for inodes. We rely on the fact that tree topology may change
4878 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4879 * move will be locked. Thus we can rank directories by the tree
4880 * (ancestors first) and rank all non-directories after them.
4881 * That works since everybody except rename does "lock parent, lookup,
4882 * lock child" and rename is under ->s_vfs_rename_mutex.
4883 * HOWEVER, it relies on the assumption that any object with ->lookup()
4884 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4885 * we'd better make sure that there's no link(2) for them.
4886 * d) conversion from fhandle to dentry may come in the wrong moment - when
4887 * we are removing the target. Solution: we will have to grab ->i_mutex
4888 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4889 * ->i_mutex on parents, which works but leads to some truly excessive
4892 int vfs_rename(struct renamedata
*rd
)
4895 struct inode
*old_dir
= rd
->old_dir
, *new_dir
= rd
->new_dir
;
4896 struct dentry
*old_dentry
= rd
->old_dentry
;
4897 struct dentry
*new_dentry
= rd
->new_dentry
;
4898 struct inode
**delegated_inode
= rd
->delegated_inode
;
4899 unsigned int flags
= rd
->flags
;
4900 bool is_dir
= d_is_dir(old_dentry
);
4901 struct inode
*source
= old_dentry
->d_inode
;
4902 struct inode
*target
= new_dentry
->d_inode
;
4903 bool new_is_dir
= false;
4904 unsigned max_links
= new_dir
->i_sb
->s_max_links
;
4905 struct name_snapshot old_name
;
4906 bool lock_old_subdir
, lock_new_subdir
;
4908 if (source
== target
)
4911 error
= may_delete(rd
->old_mnt_idmap
, old_dir
, old_dentry
, is_dir
);
4916 error
= may_create(rd
->new_mnt_idmap
, new_dir
, new_dentry
);
4918 new_is_dir
= d_is_dir(new_dentry
);
4920 if (!(flags
& RENAME_EXCHANGE
))
4921 error
= may_delete(rd
->new_mnt_idmap
, new_dir
,
4922 new_dentry
, is_dir
);
4924 error
= may_delete(rd
->new_mnt_idmap
, new_dir
,
4925 new_dentry
, new_is_dir
);
4930 if (!old_dir
->i_op
->rename
)
4934 * If we are going to change the parent - check write permissions,
4935 * we'll need to flip '..'.
4937 if (new_dir
!= old_dir
) {
4939 error
= inode_permission(rd
->old_mnt_idmap
, source
,
4944 if ((flags
& RENAME_EXCHANGE
) && new_is_dir
) {
4945 error
= inode_permission(rd
->new_mnt_idmap
, target
,
4952 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
4957 take_dentry_name_snapshot(&old_name
, old_dentry
);
4961 * The source subdirectory needs to be locked on cross-directory
4962 * rename or cross-directory exchange since its parent changes.
4963 * The target subdirectory needs to be locked on cross-directory
4964 * exchange due to parent change and on any rename due to becoming
4966 * Non-directories need locking in all cases (for NFS reasons);
4967 * they get locked after any subdirectories (in inode address order).
4969 * NOTE: WE ONLY LOCK UNRELATED DIRECTORIES IN CROSS-DIRECTORY CASE.
4970 * NEVER, EVER DO THAT WITHOUT ->s_vfs_rename_mutex.
4972 lock_old_subdir
= new_dir
!= old_dir
;
4973 lock_new_subdir
= new_dir
!= old_dir
|| !(flags
& RENAME_EXCHANGE
);
4975 if (lock_old_subdir
)
4976 inode_lock_nested(source
, I_MUTEX_CHILD
);
4977 if (target
&& (!new_is_dir
|| lock_new_subdir
))
4979 } else if (new_is_dir
) {
4980 if (lock_new_subdir
)
4981 inode_lock_nested(target
, I_MUTEX_CHILD
);
4984 lock_two_nondirectories(source
, target
);
4988 if (IS_SWAPFILE(source
) || (target
&& IS_SWAPFILE(target
)))
4992 if (is_local_mountpoint(old_dentry
) || is_local_mountpoint(new_dentry
))
4995 if (max_links
&& new_dir
!= old_dir
) {
4997 if (is_dir
&& !new_is_dir
&& new_dir
->i_nlink
>= max_links
)
4999 if ((flags
& RENAME_EXCHANGE
) && !is_dir
&& new_is_dir
&&
5000 old_dir
->i_nlink
>= max_links
)
5004 error
= try_break_deleg(source
, delegated_inode
);
5008 if (target
&& !new_is_dir
) {
5009 error
= try_break_deleg(target
, delegated_inode
);
5013 error
= old_dir
->i_op
->rename(rd
->new_mnt_idmap
, old_dir
, old_dentry
,
5014 new_dir
, new_dentry
, flags
);
5018 if (!(flags
& RENAME_EXCHANGE
) && target
) {
5020 shrink_dcache_parent(new_dentry
);
5021 target
->i_flags
|= S_DEAD
;
5023 dont_mount(new_dentry
);
5024 detach_mounts(new_dentry
);
5026 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
)) {
5027 if (!(flags
& RENAME_EXCHANGE
))
5028 d_move(old_dentry
, new_dentry
);
5030 d_exchange(old_dentry
, new_dentry
);
5033 if (!is_dir
|| lock_old_subdir
)
5034 inode_unlock(source
);
5035 if (target
&& (!new_is_dir
|| lock_new_subdir
))
5036 inode_unlock(target
);
5039 fsnotify_move(old_dir
, new_dir
, &old_name
.name
, is_dir
,
5040 !(flags
& RENAME_EXCHANGE
) ? target
: NULL
, old_dentry
);
5041 if (flags
& RENAME_EXCHANGE
) {
5042 fsnotify_move(new_dir
, old_dir
, &old_dentry
->d_name
,
5043 new_is_dir
, NULL
, new_dentry
);
5046 release_dentry_name_snapshot(&old_name
);
5050 EXPORT_SYMBOL(vfs_rename
);
5052 int do_renameat2(int olddfd
, struct filename
*from
, int newdfd
,
5053 struct filename
*to
, unsigned int flags
)
5055 struct renamedata rd
;
5056 struct dentry
*old_dentry
, *new_dentry
;
5057 struct dentry
*trap
;
5058 struct path old_path
, new_path
;
5059 struct qstr old_last
, new_last
;
5060 int old_type
, new_type
;
5061 struct inode
*delegated_inode
= NULL
;
5062 unsigned int lookup_flags
= 0, target_flags
= LOOKUP_RENAME_TARGET
;
5063 bool should_retry
= false;
5064 int error
= -EINVAL
;
5066 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
5069 if ((flags
& (RENAME_NOREPLACE
| RENAME_WHITEOUT
)) &&
5070 (flags
& RENAME_EXCHANGE
))
5073 if (flags
& RENAME_EXCHANGE
)
5077 error
= filename_parentat(olddfd
, from
, lookup_flags
, &old_path
,
5078 &old_last
, &old_type
);
5082 error
= filename_parentat(newdfd
, to
, lookup_flags
, &new_path
, &new_last
,
5088 if (old_path
.mnt
!= new_path
.mnt
)
5092 if (old_type
!= LAST_NORM
)
5095 if (flags
& RENAME_NOREPLACE
)
5097 if (new_type
!= LAST_NORM
)
5100 error
= mnt_want_write(old_path
.mnt
);
5105 trap
= lock_rename(new_path
.dentry
, old_path
.dentry
);
5107 error
= PTR_ERR(trap
);
5108 goto exit_lock_rename
;
5111 old_dentry
= lookup_one_qstr_excl(&old_last
, old_path
.dentry
,
5113 error
= PTR_ERR(old_dentry
);
5114 if (IS_ERR(old_dentry
))
5116 /* source must exist */
5118 if (d_is_negative(old_dentry
))
5120 new_dentry
= lookup_one_qstr_excl(&new_last
, new_path
.dentry
,
5121 lookup_flags
| target_flags
);
5122 error
= PTR_ERR(new_dentry
);
5123 if (IS_ERR(new_dentry
))
5126 if ((flags
& RENAME_NOREPLACE
) && d_is_positive(new_dentry
))
5128 if (flags
& RENAME_EXCHANGE
) {
5130 if (d_is_negative(new_dentry
))
5133 if (!d_is_dir(new_dentry
)) {
5135 if (new_last
.name
[new_last
.len
])
5139 /* unless the source is a directory trailing slashes give -ENOTDIR */
5140 if (!d_is_dir(old_dentry
)) {
5142 if (old_last
.name
[old_last
.len
])
5144 if (!(flags
& RENAME_EXCHANGE
) && new_last
.name
[new_last
.len
])
5147 /* source should not be ancestor of target */
5149 if (old_dentry
== trap
)
5151 /* target should not be an ancestor of source */
5152 if (!(flags
& RENAME_EXCHANGE
))
5154 if (new_dentry
== trap
)
5157 error
= security_path_rename(&old_path
, old_dentry
,
5158 &new_path
, new_dentry
, flags
);
5162 rd
.old_dir
= old_path
.dentry
->d_inode
;
5163 rd
.old_dentry
= old_dentry
;
5164 rd
.old_mnt_idmap
= mnt_idmap(old_path
.mnt
);
5165 rd
.new_dir
= new_path
.dentry
->d_inode
;
5166 rd
.new_dentry
= new_dentry
;
5167 rd
.new_mnt_idmap
= mnt_idmap(new_path
.mnt
);
5168 rd
.delegated_inode
= &delegated_inode
;
5170 error
= vfs_rename(&rd
);
5176 unlock_rename(new_path
.dentry
, old_path
.dentry
);
5178 if (delegated_inode
) {
5179 error
= break_deleg_wait(&delegated_inode
);
5183 mnt_drop_write(old_path
.mnt
);
5185 if (retry_estale(error
, lookup_flags
))
5186 should_retry
= true;
5187 path_put(&new_path
);
5189 path_put(&old_path
);
5191 should_retry
= false;
5192 lookup_flags
|= LOOKUP_REVAL
;
5201 SYSCALL_DEFINE5(renameat2
, int, olddfd
, const char __user
*, oldname
,
5202 int, newdfd
, const char __user
*, newname
, unsigned int, flags
)
5204 return do_renameat2(olddfd
, getname(oldname
), newdfd
, getname(newname
),
5208 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
5209 int, newdfd
, const char __user
*, newname
)
5211 return do_renameat2(olddfd
, getname(oldname
), newdfd
, getname(newname
),
5215 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
5217 return do_renameat2(AT_FDCWD
, getname(oldname
), AT_FDCWD
,
5218 getname(newname
), 0);
5221 int readlink_copy(char __user
*buffer
, int buflen
, const char *link
)
5223 int len
= PTR_ERR(link
);
5228 if (len
> (unsigned) buflen
)
5230 if (copy_to_user(buffer
, link
, len
))
5237 * vfs_readlink - copy symlink body into userspace buffer
5238 * @dentry: dentry on which to get symbolic link
5239 * @buffer: user memory pointer
5240 * @buflen: size of buffer
5242 * Does not touch atime. That's up to the caller if necessary
5244 * Does not call security hook.
5246 int vfs_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
5248 struct inode
*inode
= d_inode(dentry
);
5249 DEFINE_DELAYED_CALL(done
);
5253 if (unlikely(!(inode
->i_opflags
& IOP_DEFAULT_READLINK
))) {
5254 if (unlikely(inode
->i_op
->readlink
))
5255 return inode
->i_op
->readlink(dentry
, buffer
, buflen
);
5257 if (!d_is_symlink(dentry
))
5260 spin_lock(&inode
->i_lock
);
5261 inode
->i_opflags
|= IOP_DEFAULT_READLINK
;
5262 spin_unlock(&inode
->i_lock
);
5265 link
= READ_ONCE(inode
->i_link
);
5267 link
= inode
->i_op
->get_link(dentry
, inode
, &done
);
5269 return PTR_ERR(link
);
5271 res
= readlink_copy(buffer
, buflen
, link
);
5272 do_delayed_call(&done
);
5275 EXPORT_SYMBOL(vfs_readlink
);
5278 * vfs_get_link - get symlink body
5279 * @dentry: dentry on which to get symbolic link
5280 * @done: caller needs to free returned data with this
5282 * Calls security hook and i_op->get_link() on the supplied inode.
5284 * It does not touch atime. That's up to the caller if necessary.
5286 * Does not work on "special" symlinks like /proc/$$/fd/N
5288 const char *vfs_get_link(struct dentry
*dentry
, struct delayed_call
*done
)
5290 const char *res
= ERR_PTR(-EINVAL
);
5291 struct inode
*inode
= d_inode(dentry
);
5293 if (d_is_symlink(dentry
)) {
5294 res
= ERR_PTR(security_inode_readlink(dentry
));
5296 res
= inode
->i_op
->get_link(dentry
, inode
, done
);
5300 EXPORT_SYMBOL(vfs_get_link
);
5302 /* get the link contents into pagecache */
5303 const char *page_get_link(struct dentry
*dentry
, struct inode
*inode
,
5304 struct delayed_call
*callback
)
5308 struct address_space
*mapping
= inode
->i_mapping
;
5311 page
= find_get_page(mapping
, 0);
5313 return ERR_PTR(-ECHILD
);
5314 if (!PageUptodate(page
)) {
5316 return ERR_PTR(-ECHILD
);
5319 page
= read_mapping_page(mapping
, 0, NULL
);
5323 set_delayed_call(callback
, page_put_link
, page
);
5324 BUG_ON(mapping_gfp_mask(mapping
) & __GFP_HIGHMEM
);
5325 kaddr
= page_address(page
);
5326 nd_terminate_link(kaddr
, inode
->i_size
, PAGE_SIZE
- 1);
5330 EXPORT_SYMBOL(page_get_link
);
5332 void page_put_link(void *arg
)
5336 EXPORT_SYMBOL(page_put_link
);
5338 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
5340 DEFINE_DELAYED_CALL(done
);
5341 int res
= readlink_copy(buffer
, buflen
,
5342 page_get_link(dentry
, d_inode(dentry
),
5344 do_delayed_call(&done
);
5347 EXPORT_SYMBOL(page_readlink
);
5349 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
5351 struct address_space
*mapping
= inode
->i_mapping
;
5352 const struct address_space_operations
*aops
= mapping
->a_ops
;
5353 bool nofs
= !mapping_gfp_constraint(mapping
, __GFP_FS
);
5354 struct folio
*folio
;
5355 void *fsdata
= NULL
;
5361 flags
= memalloc_nofs_save();
5362 err
= aops
->write_begin(NULL
, mapping
, 0, len
-1, &folio
, &fsdata
);
5364 memalloc_nofs_restore(flags
);
5368 memcpy(folio_address(folio
), symname
, len
- 1);
5370 err
= aops
->write_end(NULL
, mapping
, 0, len
- 1, len
- 1,
5377 mark_inode_dirty(inode
);
5382 EXPORT_SYMBOL(page_symlink
);
5384 const struct inode_operations page_symlink_inode_operations
= {
5385 .get_link
= page_get_link
,
5387 EXPORT_SYMBOL(page_symlink_inode_operations
);