4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
22 #include <linux/namei.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/ima.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/device_cgroup.h>
35 #include <linux/fs_struct.h>
36 #include <linux/posix_acl.h>
37 #include <linux/hash.h>
38 #include <linux/bitops.h>
39 #include <linux/init_task.h>
40 #include <asm/uaccess.h>
45 /* [Feb-1997 T. Schoebel-Theuer]
46 * Fundamental changes in the pathname lookup mechanisms (namei)
47 * were necessary because of omirr. The reason is that omirr needs
48 * to know the _real_ pathname, not the user-supplied one, in case
49 * of symlinks (and also when transname replacements occur).
51 * The new code replaces the old recursive symlink resolution with
52 * an iterative one (in case of non-nested symlink chains). It does
53 * this with calls to <fs>_follow_link().
54 * As a side effect, dir_namei(), _namei() and follow_link() are now
55 * replaced with a single function lookup_dentry() that can handle all
56 * the special cases of the former code.
58 * With the new dcache, the pathname is stored at each inode, at least as
59 * long as the refcount of the inode is positive. As a side effect, the
60 * size of the dcache depends on the inode cache and thus is dynamic.
62 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
63 * resolution to correspond with current state of the code.
65 * Note that the symlink resolution is not *completely* iterative.
66 * There is still a significant amount of tail- and mid- recursion in
67 * the algorithm. Also, note that <fs>_readlink() is not used in
68 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
69 * may return different results than <fs>_follow_link(). Many virtual
70 * filesystems (including /proc) exhibit this behavior.
73 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
74 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
75 * and the name already exists in form of a symlink, try to create the new
76 * name indicated by the symlink. The old code always complained that the
77 * name already exists, due to not following the symlink even if its target
78 * is nonexistent. The new semantics affects also mknod() and link() when
79 * the name is a symlink pointing to a non-existent name.
81 * I don't know which semantics is the right one, since I have no access
82 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
83 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
84 * "old" one. Personally, I think the new semantics is much more logical.
85 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
86 * file does succeed in both HP-UX and SunOs, but not in Solaris
87 * and in the old Linux semantics.
90 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
91 * semantics. See the comments in "open_namei" and "do_link" below.
93 * [10-Sep-98 Alan Modra] Another symlink change.
96 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
97 * inside the path - always follow.
98 * in the last component in creation/removal/renaming - never follow.
99 * if LOOKUP_FOLLOW passed - follow.
100 * if the pathname has trailing slashes - follow.
101 * otherwise - don't follow.
102 * (applied in that order).
104 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
105 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
106 * During the 2.4 we need to fix the userland stuff depending on it -
107 * hopefully we will be able to get rid of that wart in 2.5. So far only
108 * XEmacs seems to be relying on it...
111 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
112 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
113 * any extra contention...
116 /* In order to reduce some races, while at the same time doing additional
117 * checking and hopefully speeding things up, we copy filenames to the
118 * kernel data space before using them..
120 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
121 * PATH_MAX includes the nul terminator --RR.
124 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
127 getname_flags(const char __user
*filename
, int flags
, int *empty
)
129 struct filename
*result
;
133 result
= audit_reusename(filename
);
137 result
= __getname();
138 if (unlikely(!result
))
139 return ERR_PTR(-ENOMEM
);
142 * First, try to embed the struct filename inside the names_cache
145 kname
= (char *)result
->iname
;
146 result
->name
= kname
;
148 len
= strncpy_from_user(kname
, filename
, EMBEDDED_NAME_MAX
);
149 if (unlikely(len
< 0)) {
155 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
156 * separate struct filename so we can dedicate the entire
157 * names_cache allocation for the pathname, and re-do the copy from
160 if (unlikely(len
== EMBEDDED_NAME_MAX
)) {
161 const size_t size
= offsetof(struct filename
, iname
[1]);
162 kname
= (char *)result
;
165 * size is chosen that way we to guarantee that
166 * result->iname[0] is within the same object and that
167 * kname can't be equal to result->iname, no matter what.
169 result
= kzalloc(size
, GFP_KERNEL
);
170 if (unlikely(!result
)) {
172 return ERR_PTR(-ENOMEM
);
174 result
->name
= kname
;
175 len
= strncpy_from_user(kname
, filename
, PATH_MAX
);
176 if (unlikely(len
< 0)) {
181 if (unlikely(len
== PATH_MAX
)) {
184 return ERR_PTR(-ENAMETOOLONG
);
189 /* The empty path is special. */
190 if (unlikely(!len
)) {
193 if (!(flags
& LOOKUP_EMPTY
)) {
195 return ERR_PTR(-ENOENT
);
199 result
->uptr
= filename
;
200 result
->aname
= NULL
;
201 audit_getname(result
);
206 getname(const char __user
* filename
)
208 return getname_flags(filename
, 0, NULL
);
212 getname_kernel(const char * filename
)
214 struct filename
*result
;
215 int len
= strlen(filename
) + 1;
217 result
= __getname();
218 if (unlikely(!result
))
219 return ERR_PTR(-ENOMEM
);
221 if (len
<= EMBEDDED_NAME_MAX
) {
222 result
->name
= (char *)result
->iname
;
223 } else if (len
<= PATH_MAX
) {
224 struct filename
*tmp
;
226 tmp
= kmalloc(sizeof(*tmp
), GFP_KERNEL
);
227 if (unlikely(!tmp
)) {
229 return ERR_PTR(-ENOMEM
);
231 tmp
->name
= (char *)result
;
235 return ERR_PTR(-ENAMETOOLONG
);
237 memcpy((char *)result
->name
, filename
, len
);
239 result
->aname
= NULL
;
241 audit_getname(result
);
246 void putname(struct filename
*name
)
248 BUG_ON(name
->refcnt
<= 0);
250 if (--name
->refcnt
> 0)
253 if (name
->name
!= name
->iname
) {
254 __putname(name
->name
);
260 static int check_acl(struct inode
*inode
, int mask
)
262 #ifdef CONFIG_FS_POSIX_ACL
263 struct posix_acl
*acl
;
265 if (mask
& MAY_NOT_BLOCK
) {
266 acl
= get_cached_acl_rcu(inode
, ACL_TYPE_ACCESS
);
269 /* no ->get_acl() calls in RCU mode... */
270 if (is_uncached_acl(acl
))
272 return posix_acl_permission(inode
, acl
, mask
& ~MAY_NOT_BLOCK
);
275 acl
= get_acl(inode
, ACL_TYPE_ACCESS
);
279 int error
= posix_acl_permission(inode
, acl
, mask
);
280 posix_acl_release(acl
);
289 * This does the basic permission checking
291 static int acl_permission_check(struct inode
*inode
, int mask
)
293 unsigned int mode
= inode
->i_mode
;
295 if (likely(uid_eq(current_fsuid(), inode
->i_uid
)))
298 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
)) {
299 int error
= check_acl(inode
, mask
);
300 if (error
!= -EAGAIN
)
304 if (in_group_p(inode
->i_gid
))
309 * If the DACs are ok we don't need any capability check.
311 if ((mask
& ~mode
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
)) == 0)
317 * generic_permission - check for access rights on a Posix-like filesystem
318 * @inode: inode to check access rights for
319 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
321 * Used to check for read/write/execute permissions on a file.
322 * We use "fsuid" for this, letting us set arbitrary permissions
323 * for filesystem access without changing the "normal" uids which
324 * are used for other things.
326 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
327 * request cannot be satisfied (eg. requires blocking or too much complexity).
328 * It would then be called again in ref-walk mode.
330 int generic_permission(struct inode
*inode
, int mask
)
335 * Do the basic permission checks.
337 ret
= acl_permission_check(inode
, mask
);
341 if (S_ISDIR(inode
->i_mode
)) {
342 /* DACs are overridable for directories */
343 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
345 if (!(mask
& MAY_WRITE
))
346 if (capable_wrt_inode_uidgid(inode
,
347 CAP_DAC_READ_SEARCH
))
352 * Read/write DACs are always overridable.
353 * Executable DACs are overridable when there is
354 * at least one exec bit set.
356 if (!(mask
& MAY_EXEC
) || (inode
->i_mode
& S_IXUGO
))
357 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
361 * Searching includes executable on directories, else just read.
363 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
364 if (mask
== MAY_READ
)
365 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_READ_SEARCH
))
370 EXPORT_SYMBOL(generic_permission
);
373 * We _really_ want to just do "generic_permission()" without
374 * even looking at the inode->i_op values. So we keep a cache
375 * flag in inode->i_opflags, that says "this has not special
376 * permission function, use the fast case".
378 static inline int do_inode_permission(struct inode
*inode
, int mask
)
380 if (unlikely(!(inode
->i_opflags
& IOP_FASTPERM
))) {
381 if (likely(inode
->i_op
->permission
))
382 return inode
->i_op
->permission(inode
, mask
);
384 /* This gets set once for the inode lifetime */
385 spin_lock(&inode
->i_lock
);
386 inode
->i_opflags
|= IOP_FASTPERM
;
387 spin_unlock(&inode
->i_lock
);
389 return generic_permission(inode
, mask
);
393 * __inode_permission - Check for access rights to a given inode
394 * @inode: Inode to check permission on
395 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
397 * Check for read/write/execute permissions on an inode.
399 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
401 * This does not check for a read-only file system. You probably want
402 * inode_permission().
404 int __inode_permission(struct inode
*inode
, int mask
)
408 if (unlikely(mask
& MAY_WRITE
)) {
410 * Nobody gets write access to an immutable file.
412 if (IS_IMMUTABLE(inode
))
416 * Updating mtime will likely cause i_uid and i_gid to be
417 * written back improperly if their true value is unknown
420 if (HAS_UNMAPPED_ID(inode
))
424 retval
= do_inode_permission(inode
, mask
);
428 retval
= devcgroup_inode_permission(inode
, mask
);
432 return security_inode_permission(inode
, mask
);
434 EXPORT_SYMBOL(__inode_permission
);
437 * sb_permission - Check superblock-level permissions
438 * @sb: Superblock of inode to check permission on
439 * @inode: Inode to check permission on
440 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
442 * Separate out file-system wide checks from inode-specific permission checks.
444 static int sb_permission(struct super_block
*sb
, struct inode
*inode
, int mask
)
446 if (unlikely(mask
& MAY_WRITE
)) {
447 umode_t mode
= inode
->i_mode
;
449 /* Nobody gets write access to a read-only fs. */
450 if ((sb
->s_flags
& MS_RDONLY
) &&
451 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
458 * inode_permission - Check for access rights to a given inode
459 * @inode: Inode to check permission on
460 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
462 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
463 * this, letting us set arbitrary permissions for filesystem access without
464 * changing the "normal" UIDs which are used for other things.
466 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
468 int inode_permission(struct inode
*inode
, int mask
)
472 retval
= sb_permission(inode
->i_sb
, inode
, mask
);
475 return __inode_permission(inode
, mask
);
477 EXPORT_SYMBOL(inode_permission
);
480 * path_get - get a reference to a path
481 * @path: path to get the reference to
483 * Given a path increment the reference count to the dentry and the vfsmount.
485 void path_get(const struct path
*path
)
490 EXPORT_SYMBOL(path_get
);
493 * path_put - put a reference to a path
494 * @path: path to put the reference to
496 * Given a path decrement the reference count to the dentry and the vfsmount.
498 void path_put(const struct path
*path
)
503 EXPORT_SYMBOL(path_put
);
505 #define EMBEDDED_LEVELS 2
510 struct inode
*inode
; /* path.dentry.d_inode */
515 int total_link_count
;
518 struct delayed_call done
;
521 } *stack
, internal
[EMBEDDED_LEVELS
];
522 struct filename
*name
;
523 struct nameidata
*saved
;
524 struct inode
*link_inode
;
529 static void set_nameidata(struct nameidata
*p
, int dfd
, struct filename
*name
)
531 struct nameidata
*old
= current
->nameidata
;
532 p
->stack
= p
->internal
;
535 p
->total_link_count
= old
? old
->total_link_count
: 0;
537 current
->nameidata
= p
;
540 static void restore_nameidata(void)
542 struct nameidata
*now
= current
->nameidata
, *old
= now
->saved
;
544 current
->nameidata
= old
;
546 old
->total_link_count
= now
->total_link_count
;
547 if (now
->stack
!= now
->internal
)
551 static int __nd_alloc_stack(struct nameidata
*nd
)
555 if (nd
->flags
& LOOKUP_RCU
) {
556 p
= kmalloc(MAXSYMLINKS
* sizeof(struct saved
),
561 p
= kmalloc(MAXSYMLINKS
* sizeof(struct saved
),
566 memcpy(p
, nd
->internal
, sizeof(nd
->internal
));
572 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
573 * @path: nameidate to verify
575 * Rename can sometimes move a file or directory outside of a bind
576 * mount, path_connected allows those cases to be detected.
578 static bool path_connected(const struct path
*path
)
580 struct vfsmount
*mnt
= path
->mnt
;
581 struct super_block
*sb
= mnt
->mnt_sb
;
583 /* Bind mounts and multi-root filesystems can have disconnected paths */
584 if (!(sb
->s_iflags
& SB_I_MULTIROOT
) && (mnt
->mnt_root
== sb
->s_root
))
587 return is_subdir(path
->dentry
, mnt
->mnt_root
);
590 static inline int nd_alloc_stack(struct nameidata
*nd
)
592 if (likely(nd
->depth
!= EMBEDDED_LEVELS
))
594 if (likely(nd
->stack
!= nd
->internal
))
596 return __nd_alloc_stack(nd
);
599 static void drop_links(struct nameidata
*nd
)
603 struct saved
*last
= nd
->stack
+ i
;
604 do_delayed_call(&last
->done
);
605 clear_delayed_call(&last
->done
);
609 static void terminate_walk(struct nameidata
*nd
)
612 if (!(nd
->flags
& LOOKUP_RCU
)) {
615 for (i
= 0; i
< nd
->depth
; i
++)
616 path_put(&nd
->stack
[i
].link
);
617 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
622 nd
->flags
&= ~LOOKUP_RCU
;
623 if (!(nd
->flags
& LOOKUP_ROOT
))
630 /* path_put is needed afterwards regardless of success or failure */
631 static bool legitimize_path(struct nameidata
*nd
,
632 struct path
*path
, unsigned seq
)
634 int res
= __legitimize_mnt(path
->mnt
, nd
->m_seq
);
641 if (unlikely(!lockref_get_not_dead(&path
->dentry
->d_lockref
))) {
645 return !read_seqcount_retry(&path
->dentry
->d_seq
, seq
);
648 static bool legitimize_links(struct nameidata
*nd
)
651 for (i
= 0; i
< nd
->depth
; i
++) {
652 struct saved
*last
= nd
->stack
+ i
;
653 if (unlikely(!legitimize_path(nd
, &last
->link
, last
->seq
))) {
663 * Path walking has 2 modes, rcu-walk and ref-walk (see
664 * Documentation/filesystems/path-lookup.txt). In situations when we can't
665 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
666 * normal reference counts on dentries and vfsmounts to transition to ref-walk
667 * mode. Refcounts are grabbed at the last known good point before rcu-walk
668 * got stuck, so ref-walk may continue from there. If this is not successful
669 * (eg. a seqcount has changed), then failure is returned and it's up to caller
670 * to restart the path walk from the beginning in ref-walk mode.
674 * unlazy_walk - try to switch to ref-walk mode.
675 * @nd: nameidata pathwalk data
676 * @dentry: child of nd->path.dentry or NULL
677 * @seq: seq number to check dentry against
678 * Returns: 0 on success, -ECHILD on failure
680 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
681 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
682 * @nd or NULL. Must be called from rcu-walk context.
683 * Nothing should touch nameidata between unlazy_walk() failure and
686 static int unlazy_walk(struct nameidata
*nd
, struct dentry
*dentry
, unsigned seq
)
688 struct dentry
*parent
= nd
->path
.dentry
;
690 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
692 nd
->flags
&= ~LOOKUP_RCU
;
693 if (unlikely(!legitimize_links(nd
)))
695 if (unlikely(!legitimize_mnt(nd
->path
.mnt
, nd
->m_seq
)))
697 if (unlikely(!lockref_get_not_dead(&parent
->d_lockref
)))
701 * For a negative lookup, the lookup sequence point is the parents
702 * sequence point, and it only needs to revalidate the parent dentry.
704 * For a positive lookup, we need to move both the parent and the
705 * dentry from the RCU domain to be properly refcounted. And the
706 * sequence number in the dentry validates *both* dentry counters,
707 * since we checked the sequence number of the parent after we got
708 * the child sequence number. So we know the parent must still
709 * be valid if the child sequence number is still valid.
712 if (read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
714 BUG_ON(nd
->inode
!= parent
->d_inode
);
716 if (!lockref_get_not_dead(&dentry
->d_lockref
))
718 if (read_seqcount_retry(&dentry
->d_seq
, seq
))
723 * Sequence counts matched. Now make sure that the root is
724 * still valid and get it if required.
726 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
727 if (unlikely(!legitimize_path(nd
, &nd
->root
, nd
->root_seq
))) {
744 nd
->path
.dentry
= NULL
;
748 if (!(nd
->flags
& LOOKUP_ROOT
))
753 static int unlazy_link(struct nameidata
*nd
, struct path
*link
, unsigned seq
)
755 if (unlikely(!legitimize_path(nd
, link
, seq
))) {
758 nd
->flags
&= ~LOOKUP_RCU
;
760 nd
->path
.dentry
= NULL
;
761 if (!(nd
->flags
& LOOKUP_ROOT
))
764 } else if (likely(unlazy_walk(nd
, NULL
, 0)) == 0) {
771 static inline int d_revalidate(struct dentry
*dentry
, unsigned int flags
)
773 return dentry
->d_op
->d_revalidate(dentry
, flags
);
777 * complete_walk - successful completion of path walk
778 * @nd: pointer nameidata
780 * If we had been in RCU mode, drop out of it and legitimize nd->path.
781 * Revalidate the final result, unless we'd already done that during
782 * the path walk or the filesystem doesn't ask for it. Return 0 on
783 * success, -error on failure. In case of failure caller does not
784 * need to drop nd->path.
786 static int complete_walk(struct nameidata
*nd
)
788 struct dentry
*dentry
= nd
->path
.dentry
;
791 if (nd
->flags
& LOOKUP_RCU
) {
792 if (!(nd
->flags
& LOOKUP_ROOT
))
794 if (unlikely(unlazy_walk(nd
, NULL
, 0)))
798 if (likely(!(nd
->flags
& LOOKUP_JUMPED
)))
801 if (likely(!(dentry
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
)))
804 status
= dentry
->d_op
->d_weak_revalidate(dentry
, nd
->flags
);
814 static void set_root(struct nameidata
*nd
)
816 struct fs_struct
*fs
= current
->fs
;
818 if (nd
->flags
& LOOKUP_RCU
) {
822 seq
= read_seqcount_begin(&fs
->seq
);
824 nd
->root_seq
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
825 } while (read_seqcount_retry(&fs
->seq
, seq
));
827 get_fs_root(fs
, &nd
->root
);
831 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
834 if (path
->mnt
!= nd
->path
.mnt
)
838 static inline void path_to_nameidata(const struct path
*path
,
839 struct nameidata
*nd
)
841 if (!(nd
->flags
& LOOKUP_RCU
)) {
842 dput(nd
->path
.dentry
);
843 if (nd
->path
.mnt
!= path
->mnt
)
844 mntput(nd
->path
.mnt
);
846 nd
->path
.mnt
= path
->mnt
;
847 nd
->path
.dentry
= path
->dentry
;
850 static int nd_jump_root(struct nameidata
*nd
)
852 if (nd
->flags
& LOOKUP_RCU
) {
856 nd
->inode
= d
->d_inode
;
857 nd
->seq
= nd
->root_seq
;
858 if (unlikely(read_seqcount_retry(&d
->d_seq
, nd
->seq
)))
864 nd
->inode
= nd
->path
.dentry
->d_inode
;
866 nd
->flags
|= LOOKUP_JUMPED
;
871 * Helper to directly jump to a known parsed path from ->get_link,
872 * caller must have taken a reference to path beforehand.
874 void nd_jump_link(struct path
*path
)
876 struct nameidata
*nd
= current
->nameidata
;
880 nd
->inode
= nd
->path
.dentry
->d_inode
;
881 nd
->flags
|= LOOKUP_JUMPED
;
884 static inline void put_link(struct nameidata
*nd
)
886 struct saved
*last
= nd
->stack
+ --nd
->depth
;
887 do_delayed_call(&last
->done
);
888 if (!(nd
->flags
& LOOKUP_RCU
))
889 path_put(&last
->link
);
892 int sysctl_protected_symlinks __read_mostly
= 0;
893 int sysctl_protected_hardlinks __read_mostly
= 0;
896 * may_follow_link - Check symlink following for unsafe situations
897 * @nd: nameidata pathwalk data
899 * In the case of the sysctl_protected_symlinks sysctl being enabled,
900 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
901 * in a sticky world-writable directory. This is to protect privileged
902 * processes from failing races against path names that may change out
903 * from under them by way of other users creating malicious symlinks.
904 * It will permit symlinks to be followed only when outside a sticky
905 * world-writable directory, or when the uid of the symlink and follower
906 * match, or when the directory owner matches the symlink's owner.
908 * Returns 0 if following the symlink is allowed, -ve on error.
910 static inline int may_follow_link(struct nameidata
*nd
)
912 const struct inode
*inode
;
913 const struct inode
*parent
;
916 if (!sysctl_protected_symlinks
)
919 /* Allowed if owner and follower match. */
920 inode
= nd
->link_inode
;
921 if (uid_eq(current_cred()->fsuid
, inode
->i_uid
))
924 /* Allowed if parent directory not sticky and world-writable. */
926 if ((parent
->i_mode
& (S_ISVTX
|S_IWOTH
)) != (S_ISVTX
|S_IWOTH
))
929 /* Allowed if parent directory and link owner match. */
930 puid
= parent
->i_uid
;
931 if (uid_valid(puid
) && uid_eq(puid
, inode
->i_uid
))
934 if (nd
->flags
& LOOKUP_RCU
)
937 audit_log_link_denied("follow_link", &nd
->stack
[0].link
);
942 * safe_hardlink_source - Check for safe hardlink conditions
943 * @inode: the source inode to hardlink from
945 * Return false if at least one of the following conditions:
946 * - inode is not a regular file
948 * - inode is setgid and group-exec
949 * - access failure for read and write
951 * Otherwise returns true.
953 static bool safe_hardlink_source(struct inode
*inode
)
955 umode_t mode
= inode
->i_mode
;
957 /* Special files should not get pinned to the filesystem. */
961 /* Setuid files should not get pinned to the filesystem. */
965 /* Executable setgid files should not get pinned to the filesystem. */
966 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
))
969 /* Hardlinking to unreadable or unwritable sources is dangerous. */
970 if (inode_permission(inode
, MAY_READ
| MAY_WRITE
))
977 * may_linkat - Check permissions for creating a hardlink
978 * @link: the source to hardlink from
980 * Block hardlink when all of:
981 * - sysctl_protected_hardlinks enabled
982 * - fsuid does not match inode
983 * - hardlink source is unsafe (see safe_hardlink_source() above)
984 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
986 * Returns 0 if successful, -ve on error.
988 static int may_linkat(struct path
*link
)
992 if (!sysctl_protected_hardlinks
)
995 inode
= link
->dentry
->d_inode
;
997 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
998 * otherwise, it must be a safe source.
1000 if (inode_owner_or_capable(inode
) || safe_hardlink_source(inode
))
1003 audit_log_link_denied("linkat", link
);
1007 static __always_inline
1008 const char *get_link(struct nameidata
*nd
)
1010 struct saved
*last
= nd
->stack
+ nd
->depth
- 1;
1011 struct dentry
*dentry
= last
->link
.dentry
;
1012 struct inode
*inode
= nd
->link_inode
;
1016 if (!(nd
->flags
& LOOKUP_RCU
)) {
1017 touch_atime(&last
->link
);
1019 } else if (atime_needs_update_rcu(&last
->link
, inode
)) {
1020 if (unlikely(unlazy_walk(nd
, NULL
, 0)))
1021 return ERR_PTR(-ECHILD
);
1022 touch_atime(&last
->link
);
1025 error
= security_inode_follow_link(dentry
, inode
,
1026 nd
->flags
& LOOKUP_RCU
);
1027 if (unlikely(error
))
1028 return ERR_PTR(error
);
1030 nd
->last_type
= LAST_BIND
;
1031 res
= inode
->i_link
;
1033 const char * (*get
)(struct dentry
*, struct inode
*,
1034 struct delayed_call
*);
1035 get
= inode
->i_op
->get_link
;
1036 if (nd
->flags
& LOOKUP_RCU
) {
1037 res
= get(NULL
, inode
, &last
->done
);
1038 if (res
== ERR_PTR(-ECHILD
)) {
1039 if (unlikely(unlazy_walk(nd
, NULL
, 0)))
1040 return ERR_PTR(-ECHILD
);
1041 res
= get(dentry
, inode
, &last
->done
);
1044 res
= get(dentry
, inode
, &last
->done
);
1046 if (IS_ERR_OR_NULL(res
))
1052 if (unlikely(nd_jump_root(nd
)))
1053 return ERR_PTR(-ECHILD
);
1054 while (unlikely(*++res
== '/'))
1063 * follow_up - Find the mountpoint of path's vfsmount
1065 * Given a path, find the mountpoint of its source file system.
1066 * Replace @path with the path of the mountpoint in the parent mount.
1069 * Return 1 if we went up a level and 0 if we were already at the
1072 int follow_up(struct path
*path
)
1074 struct mount
*mnt
= real_mount(path
->mnt
);
1075 struct mount
*parent
;
1076 struct dentry
*mountpoint
;
1078 read_seqlock_excl(&mount_lock
);
1079 parent
= mnt
->mnt_parent
;
1080 if (parent
== mnt
) {
1081 read_sequnlock_excl(&mount_lock
);
1084 mntget(&parent
->mnt
);
1085 mountpoint
= dget(mnt
->mnt_mountpoint
);
1086 read_sequnlock_excl(&mount_lock
);
1088 path
->dentry
= mountpoint
;
1090 path
->mnt
= &parent
->mnt
;
1093 EXPORT_SYMBOL(follow_up
);
1096 * Perform an automount
1097 * - return -EISDIR to tell follow_managed() to stop and return the path we
1100 static int follow_automount(struct path
*path
, struct nameidata
*nd
,
1103 struct vfsmount
*mnt
;
1106 if (!path
->dentry
->d_op
|| !path
->dentry
->d_op
->d_automount
)
1109 /* We don't want to mount if someone's just doing a stat -
1110 * unless they're stat'ing a directory and appended a '/' to
1113 * We do, however, want to mount if someone wants to open or
1114 * create a file of any type under the mountpoint, wants to
1115 * traverse through the mountpoint or wants to open the
1116 * mounted directory. Also, autofs may mark negative dentries
1117 * as being automount points. These will need the attentions
1118 * of the daemon to instantiate them before they can be used.
1120 if (!(nd
->flags
& (LOOKUP_PARENT
| LOOKUP_DIRECTORY
|
1121 LOOKUP_OPEN
| LOOKUP_CREATE
| LOOKUP_AUTOMOUNT
)) &&
1122 path
->dentry
->d_inode
)
1125 nd
->total_link_count
++;
1126 if (nd
->total_link_count
>= 40)
1129 mnt
= path
->dentry
->d_op
->d_automount(path
);
1132 * The filesystem is allowed to return -EISDIR here to indicate
1133 * it doesn't want to automount. For instance, autofs would do
1134 * this so that its userspace daemon can mount on this dentry.
1136 * However, we can only permit this if it's a terminal point in
1137 * the path being looked up; if it wasn't then the remainder of
1138 * the path is inaccessible and we should say so.
1140 if (PTR_ERR(mnt
) == -EISDIR
&& (nd
->flags
& LOOKUP_PARENT
))
1142 return PTR_ERR(mnt
);
1145 if (!mnt
) /* mount collision */
1148 if (!*need_mntput
) {
1149 /* lock_mount() may release path->mnt on error */
1151 *need_mntput
= true;
1153 err
= finish_automount(mnt
, path
);
1157 /* Someone else made a mount here whilst we were busy */
1162 path
->dentry
= dget(mnt
->mnt_root
);
1171 * Handle a dentry that is managed in some way.
1172 * - Flagged for transit management (autofs)
1173 * - Flagged as mountpoint
1174 * - Flagged as automount point
1176 * This may only be called in refwalk mode.
1178 * Serialization is taken care of in namespace.c
1180 static int follow_managed(struct path
*path
, struct nameidata
*nd
)
1182 struct vfsmount
*mnt
= path
->mnt
; /* held by caller, must be left alone */
1184 bool need_mntput
= false;
1187 /* Given that we're not holding a lock here, we retain the value in a
1188 * local variable for each dentry as we look at it so that we don't see
1189 * the components of that value change under us */
1190 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1191 managed
&= DCACHE_MANAGED_DENTRY
,
1192 unlikely(managed
!= 0)) {
1193 /* Allow the filesystem to manage the transit without i_mutex
1195 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1196 BUG_ON(!path
->dentry
->d_op
);
1197 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1198 ret
= path
->dentry
->d_op
->d_manage(path
->dentry
, false);
1203 /* Transit to a mounted filesystem. */
1204 if (managed
& DCACHE_MOUNTED
) {
1205 struct vfsmount
*mounted
= lookup_mnt(path
);
1210 path
->mnt
= mounted
;
1211 path
->dentry
= dget(mounted
->mnt_root
);
1216 /* Something is mounted on this dentry in another
1217 * namespace and/or whatever was mounted there in this
1218 * namespace got unmounted before lookup_mnt() could
1222 /* Handle an automount point */
1223 if (managed
& DCACHE_NEED_AUTOMOUNT
) {
1224 ret
= follow_automount(path
, nd
, &need_mntput
);
1230 /* We didn't change the current path point */
1234 if (need_mntput
&& path
->mnt
== mnt
)
1236 if (ret
== -EISDIR
|| !ret
)
1239 nd
->flags
|= LOOKUP_JUMPED
;
1240 if (unlikely(ret
< 0))
1241 path_put_conditional(path
, nd
);
1245 int follow_down_one(struct path
*path
)
1247 struct vfsmount
*mounted
;
1249 mounted
= lookup_mnt(path
);
1253 path
->mnt
= mounted
;
1254 path
->dentry
= dget(mounted
->mnt_root
);
1259 EXPORT_SYMBOL(follow_down_one
);
1261 static inline int managed_dentry_rcu(struct dentry
*dentry
)
1263 return (dentry
->d_flags
& DCACHE_MANAGE_TRANSIT
) ?
1264 dentry
->d_op
->d_manage(dentry
, true) : 0;
1268 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1269 * we meet a managed dentry that would need blocking.
1271 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
1272 struct inode
**inode
, unsigned *seqp
)
1275 struct mount
*mounted
;
1277 * Don't forget we might have a non-mountpoint managed dentry
1278 * that wants to block transit.
1280 switch (managed_dentry_rcu(path
->dentry
)) {
1290 if (!d_mountpoint(path
->dentry
))
1291 return !(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
);
1293 mounted
= __lookup_mnt(path
->mnt
, path
->dentry
);
1296 path
->mnt
= &mounted
->mnt
;
1297 path
->dentry
= mounted
->mnt
.mnt_root
;
1298 nd
->flags
|= LOOKUP_JUMPED
;
1299 *seqp
= read_seqcount_begin(&path
->dentry
->d_seq
);
1301 * Update the inode too. We don't need to re-check the
1302 * dentry sequence number here after this d_inode read,
1303 * because a mount-point is always pinned.
1305 *inode
= path
->dentry
->d_inode
;
1307 return !read_seqretry(&mount_lock
, nd
->m_seq
) &&
1308 !(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
);
1311 static int follow_dotdot_rcu(struct nameidata
*nd
)
1313 struct inode
*inode
= nd
->inode
;
1316 if (path_equal(&nd
->path
, &nd
->root
))
1318 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1319 struct dentry
*old
= nd
->path
.dentry
;
1320 struct dentry
*parent
= old
->d_parent
;
1323 inode
= parent
->d_inode
;
1324 seq
= read_seqcount_begin(&parent
->d_seq
);
1325 if (unlikely(read_seqcount_retry(&old
->d_seq
, nd
->seq
)))
1327 nd
->path
.dentry
= parent
;
1329 if (unlikely(!path_connected(&nd
->path
)))
1333 struct mount
*mnt
= real_mount(nd
->path
.mnt
);
1334 struct mount
*mparent
= mnt
->mnt_parent
;
1335 struct dentry
*mountpoint
= mnt
->mnt_mountpoint
;
1336 struct inode
*inode2
= mountpoint
->d_inode
;
1337 unsigned seq
= read_seqcount_begin(&mountpoint
->d_seq
);
1338 if (unlikely(read_seqretry(&mount_lock
, nd
->m_seq
)))
1340 if (&mparent
->mnt
== nd
->path
.mnt
)
1342 /* we know that mountpoint was pinned */
1343 nd
->path
.dentry
= mountpoint
;
1344 nd
->path
.mnt
= &mparent
->mnt
;
1349 while (unlikely(d_mountpoint(nd
->path
.dentry
))) {
1350 struct mount
*mounted
;
1351 mounted
= __lookup_mnt(nd
->path
.mnt
, nd
->path
.dentry
);
1352 if (unlikely(read_seqretry(&mount_lock
, nd
->m_seq
)))
1356 nd
->path
.mnt
= &mounted
->mnt
;
1357 nd
->path
.dentry
= mounted
->mnt
.mnt_root
;
1358 inode
= nd
->path
.dentry
->d_inode
;
1359 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1366 * Follow down to the covering mount currently visible to userspace. At each
1367 * point, the filesystem owning that dentry may be queried as to whether the
1368 * caller is permitted to proceed or not.
1370 int follow_down(struct path
*path
)
1375 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1376 unlikely(managed
& DCACHE_MANAGED_DENTRY
)) {
1377 /* Allow the filesystem to manage the transit without i_mutex
1380 * We indicate to the filesystem if someone is trying to mount
1381 * something here. This gives autofs the chance to deny anyone
1382 * other than its daemon the right to mount on its
1385 * The filesystem may sleep at this point.
1387 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1388 BUG_ON(!path
->dentry
->d_op
);
1389 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1390 ret
= path
->dentry
->d_op
->d_manage(
1391 path
->dentry
, false);
1393 return ret
== -EISDIR
? 0 : ret
;
1396 /* Transit to a mounted filesystem. */
1397 if (managed
& DCACHE_MOUNTED
) {
1398 struct vfsmount
*mounted
= lookup_mnt(path
);
1403 path
->mnt
= mounted
;
1404 path
->dentry
= dget(mounted
->mnt_root
);
1408 /* Don't handle automount points here */
1413 EXPORT_SYMBOL(follow_down
);
1416 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1418 static void follow_mount(struct path
*path
)
1420 while (d_mountpoint(path
->dentry
)) {
1421 struct vfsmount
*mounted
= lookup_mnt(path
);
1426 path
->mnt
= mounted
;
1427 path
->dentry
= dget(mounted
->mnt_root
);
1431 static int path_parent_directory(struct path
*path
)
1433 struct dentry
*old
= path
->dentry
;
1434 /* rare case of legitimate dget_parent()... */
1435 path
->dentry
= dget_parent(path
->dentry
);
1437 if (unlikely(!path_connected(path
)))
1442 static int follow_dotdot(struct nameidata
*nd
)
1445 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1446 nd
->path
.mnt
== nd
->root
.mnt
) {
1449 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1450 int ret
= path_parent_directory(&nd
->path
);
1455 if (!follow_up(&nd
->path
))
1458 follow_mount(&nd
->path
);
1459 nd
->inode
= nd
->path
.dentry
->d_inode
;
1464 * This looks up the name in dcache and possibly revalidates the found dentry.
1465 * NULL is returned if the dentry does not exist in the cache.
1467 static struct dentry
*lookup_dcache(const struct qstr
*name
,
1471 struct dentry
*dentry
;
1474 dentry
= d_lookup(dir
, name
);
1476 if (dentry
->d_flags
& DCACHE_OP_REVALIDATE
) {
1477 error
= d_revalidate(dentry
, flags
);
1478 if (unlikely(error
<= 0)) {
1480 d_invalidate(dentry
);
1482 return ERR_PTR(error
);
1490 * Call i_op->lookup on the dentry. The dentry must be negative and
1493 * dir->d_inode->i_mutex must be held
1495 static struct dentry
*lookup_real(struct inode
*dir
, struct dentry
*dentry
,
1500 /* Don't create child dentry for a dead directory. */
1501 if (unlikely(IS_DEADDIR(dir
))) {
1503 return ERR_PTR(-ENOENT
);
1506 old
= dir
->i_op
->lookup(dir
, dentry
, flags
);
1507 if (unlikely(old
)) {
1514 static struct dentry
*__lookup_hash(const struct qstr
*name
,
1515 struct dentry
*base
, unsigned int flags
)
1517 struct dentry
*dentry
= lookup_dcache(name
, base
, flags
);
1522 dentry
= d_alloc(base
, name
);
1523 if (unlikely(!dentry
))
1524 return ERR_PTR(-ENOMEM
);
1526 return lookup_real(base
->d_inode
, dentry
, flags
);
1529 static int lookup_fast(struct nameidata
*nd
,
1530 struct path
*path
, struct inode
**inode
,
1533 struct vfsmount
*mnt
= nd
->path
.mnt
;
1534 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1539 * Rename seqlock is not required here because in the off chance
1540 * of a false negative due to a concurrent rename, the caller is
1541 * going to fall back to non-racy lookup.
1543 if (nd
->flags
& LOOKUP_RCU
) {
1546 dentry
= __d_lookup_rcu(parent
, &nd
->last
, &seq
);
1547 if (unlikely(!dentry
)) {
1548 if (unlazy_walk(nd
, NULL
, 0))
1554 * This sequence count validates that the inode matches
1555 * the dentry name information from lookup.
1557 *inode
= d_backing_inode(dentry
);
1558 negative
= d_is_negative(dentry
);
1559 if (unlikely(read_seqcount_retry(&dentry
->d_seq
, seq
)))
1563 * This sequence count validates that the parent had no
1564 * changes while we did the lookup of the dentry above.
1566 * The memory barrier in read_seqcount_begin of child is
1567 * enough, we can use __read_seqcount_retry here.
1569 if (unlikely(__read_seqcount_retry(&parent
->d_seq
, nd
->seq
)))
1573 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
))
1574 status
= d_revalidate(dentry
, nd
->flags
);
1575 if (unlikely(status
<= 0)) {
1576 if (unlazy_walk(nd
, dentry
, seq
))
1578 if (status
== -ECHILD
)
1579 status
= d_revalidate(dentry
, nd
->flags
);
1582 * Note: do negative dentry check after revalidation in
1583 * case that drops it.
1585 if (unlikely(negative
))
1588 path
->dentry
= dentry
;
1589 if (likely(__follow_mount_rcu(nd
, path
, inode
, seqp
)))
1591 if (unlazy_walk(nd
, dentry
, seq
))
1595 dentry
= __d_lookup(parent
, &nd
->last
);
1596 if (unlikely(!dentry
))
1598 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
))
1599 status
= d_revalidate(dentry
, nd
->flags
);
1601 if (unlikely(status
<= 0)) {
1603 d_invalidate(dentry
);
1607 if (unlikely(d_is_negative(dentry
))) {
1613 path
->dentry
= dentry
;
1614 err
= follow_managed(path
, nd
);
1615 if (likely(err
> 0))
1616 *inode
= d_backing_inode(path
->dentry
);
1620 /* Fast lookup failed, do it the slow way */
1621 static struct dentry
*lookup_slow(const struct qstr
*name
,
1625 struct dentry
*dentry
= ERR_PTR(-ENOENT
), *old
;
1626 struct inode
*inode
= dir
->d_inode
;
1627 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
1629 inode_lock_shared(inode
);
1630 /* Don't go there if it's already dead */
1631 if (unlikely(IS_DEADDIR(inode
)))
1634 dentry
= d_alloc_parallel(dir
, name
, &wq
);
1637 if (unlikely(!d_in_lookup(dentry
))) {
1638 if ((dentry
->d_flags
& DCACHE_OP_REVALIDATE
) &&
1639 !(flags
& LOOKUP_NO_REVAL
)) {
1640 int error
= d_revalidate(dentry
, flags
);
1641 if (unlikely(error
<= 0)) {
1643 d_invalidate(dentry
);
1648 dentry
= ERR_PTR(error
);
1652 old
= inode
->i_op
->lookup(inode
, dentry
, flags
);
1653 d_lookup_done(dentry
);
1654 if (unlikely(old
)) {
1660 inode_unlock_shared(inode
);
1664 static inline int may_lookup(struct nameidata
*nd
)
1666 if (nd
->flags
& LOOKUP_RCU
) {
1667 int err
= inode_permission(nd
->inode
, MAY_EXEC
|MAY_NOT_BLOCK
);
1670 if (unlazy_walk(nd
, NULL
, 0))
1673 return inode_permission(nd
->inode
, MAY_EXEC
);
1676 static inline int handle_dots(struct nameidata
*nd
, int type
)
1678 if (type
== LAST_DOTDOT
) {
1681 if (nd
->flags
& LOOKUP_RCU
) {
1682 return follow_dotdot_rcu(nd
);
1684 return follow_dotdot(nd
);
1689 static int pick_link(struct nameidata
*nd
, struct path
*link
,
1690 struct inode
*inode
, unsigned seq
)
1694 if (unlikely(nd
->total_link_count
++ >= MAXSYMLINKS
)) {
1695 path_to_nameidata(link
, nd
);
1698 if (!(nd
->flags
& LOOKUP_RCU
)) {
1699 if (link
->mnt
== nd
->path
.mnt
)
1702 error
= nd_alloc_stack(nd
);
1703 if (unlikely(error
)) {
1704 if (error
== -ECHILD
) {
1705 if (unlikely(unlazy_link(nd
, link
, seq
)))
1707 error
= nd_alloc_stack(nd
);
1715 last
= nd
->stack
+ nd
->depth
++;
1717 clear_delayed_call(&last
->done
);
1718 nd
->link_inode
= inode
;
1724 * Do we need to follow links? We _really_ want to be able
1725 * to do this check without having to look at inode->i_op,
1726 * so we keep a cache of "no, this doesn't need follow_link"
1727 * for the common case.
1729 static inline int should_follow_link(struct nameidata
*nd
, struct path
*link
,
1731 struct inode
*inode
, unsigned seq
)
1733 if (likely(!d_is_symlink(link
->dentry
)))
1737 /* make sure that d_is_symlink above matches inode */
1738 if (nd
->flags
& LOOKUP_RCU
) {
1739 if (read_seqcount_retry(&link
->dentry
->d_seq
, seq
))
1742 return pick_link(nd
, link
, inode
, seq
);
1745 enum {WALK_GET
= 1, WALK_PUT
= 2};
1747 static int walk_component(struct nameidata
*nd
, int flags
)
1750 struct inode
*inode
;
1754 * "." and ".." are special - ".." especially so because it has
1755 * to be able to know about the current root directory and
1756 * parent relationships.
1758 if (unlikely(nd
->last_type
!= LAST_NORM
)) {
1759 err
= handle_dots(nd
, nd
->last_type
);
1760 if (flags
& WALK_PUT
)
1764 err
= lookup_fast(nd
, &path
, &inode
, &seq
);
1765 if (unlikely(err
<= 0)) {
1768 path
.dentry
= lookup_slow(&nd
->last
, nd
->path
.dentry
,
1770 if (IS_ERR(path
.dentry
))
1771 return PTR_ERR(path
.dentry
);
1773 path
.mnt
= nd
->path
.mnt
;
1774 err
= follow_managed(&path
, nd
);
1775 if (unlikely(err
< 0))
1778 if (unlikely(d_is_negative(path
.dentry
))) {
1779 path_to_nameidata(&path
, nd
);
1783 seq
= 0; /* we are already out of RCU mode */
1784 inode
= d_backing_inode(path
.dentry
);
1787 if (flags
& WALK_PUT
)
1789 err
= should_follow_link(nd
, &path
, flags
& WALK_GET
, inode
, seq
);
1792 path_to_nameidata(&path
, nd
);
1799 * We can do the critical dentry name comparison and hashing
1800 * operations one word at a time, but we are limited to:
1802 * - Architectures with fast unaligned word accesses. We could
1803 * do a "get_unaligned()" if this helps and is sufficiently
1806 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1807 * do not trap on the (extremely unlikely) case of a page
1808 * crossing operation.
1810 * - Furthermore, we need an efficient 64-bit compile for the
1811 * 64-bit case in order to generate the "number of bytes in
1812 * the final mask". Again, that could be replaced with a
1813 * efficient population count instruction or similar.
1815 #ifdef CONFIG_DCACHE_WORD_ACCESS
1817 #include <asm/word-at-a-time.h>
1821 /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1823 #elif defined(CONFIG_64BIT)
1825 * Register pressure in the mixing function is an issue, particularly
1826 * on 32-bit x86, but almost any function requires one state value and
1827 * one temporary. Instead, use a function designed for two state values
1828 * and no temporaries.
1830 * This function cannot create a collision in only two iterations, so
1831 * we have two iterations to achieve avalanche. In those two iterations,
1832 * we have six layers of mixing, which is enough to spread one bit's
1833 * influence out to 2^6 = 64 state bits.
1835 * Rotate constants are scored by considering either 64 one-bit input
1836 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1837 * probability of that delta causing a change to each of the 128 output
1838 * bits, using a sample of random initial states.
1840 * The Shannon entropy of the computed probabilities is then summed
1841 * to produce a score. Ideally, any input change has a 50% chance of
1842 * toggling any given output bit.
1844 * Mixing scores (in bits) for (12,45):
1845 * Input delta: 1-bit 2-bit
1846 * 1 round: 713.3 42542.6
1847 * 2 rounds: 2753.7 140389.8
1848 * 3 rounds: 5954.1 233458.2
1849 * 4 rounds: 7862.6 256672.2
1850 * Perfect: 8192 258048
1851 * (64*128) (64*63/2 * 128)
1853 #define HASH_MIX(x, y, a) \
1855 y ^= x, x = rol64(x,12),\
1856 x += y, y = rol64(y,45),\
1860 * Fold two longs into one 32-bit hash value. This must be fast, but
1861 * latency isn't quite as critical, as there is a fair bit of additional
1862 * work done before the hash value is used.
1864 static inline unsigned int fold_hash(unsigned long x
, unsigned long y
)
1866 y
^= x
* GOLDEN_RATIO_64
;
1867 y
*= GOLDEN_RATIO_64
;
1871 #else /* 32-bit case */
1874 * Mixing scores (in bits) for (7,20):
1875 * Input delta: 1-bit 2-bit
1876 * 1 round: 330.3 9201.6
1877 * 2 rounds: 1246.4 25475.4
1878 * 3 rounds: 1907.1 31295.1
1879 * 4 rounds: 2042.3 31718.6
1880 * Perfect: 2048 31744
1881 * (32*64) (32*31/2 * 64)
1883 #define HASH_MIX(x, y, a) \
1885 y ^= x, x = rol32(x, 7),\
1886 x += y, y = rol32(y,20),\
1889 static inline unsigned int fold_hash(unsigned long x
, unsigned long y
)
1891 /* Use arch-optimized multiply if one exists */
1892 return __hash_32(y
^ __hash_32(x
));
1898 * Return the hash of a string of known length. This is carfully
1899 * designed to match hash_name(), which is the more critical function.
1900 * In particular, we must end by hashing a final word containing 0..7
1901 * payload bytes, to match the way that hash_name() iterates until it
1902 * finds the delimiter after the name.
1904 unsigned int full_name_hash(const void *salt
, const char *name
, unsigned int len
)
1906 unsigned long a
, x
= 0, y
= (unsigned long)salt
;
1911 a
= load_unaligned_zeropad(name
);
1912 if (len
< sizeof(unsigned long))
1915 name
+= sizeof(unsigned long);
1916 len
-= sizeof(unsigned long);
1918 x
^= a
& bytemask_from_count(len
);
1920 return fold_hash(x
, y
);
1922 EXPORT_SYMBOL(full_name_hash
);
1924 /* Return the "hash_len" (hash and length) of a null-terminated string */
1925 u64
hashlen_string(const void *salt
, const char *name
)
1927 unsigned long a
= 0, x
= 0, y
= (unsigned long)salt
;
1928 unsigned long adata
, mask
, len
;
1929 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
1936 len
+= sizeof(unsigned long);
1938 a
= load_unaligned_zeropad(name
+len
);
1939 } while (!has_zero(a
, &adata
, &constants
));
1941 adata
= prep_zero_mask(a
, adata
, &constants
);
1942 mask
= create_zero_mask(adata
);
1943 x
^= a
& zero_bytemask(mask
);
1945 return hashlen_create(fold_hash(x
, y
), len
+ find_zero(mask
));
1947 EXPORT_SYMBOL(hashlen_string
);
1950 * Calculate the length and hash of the path component, and
1951 * return the "hash_len" as the result.
1953 static inline u64
hash_name(const void *salt
, const char *name
)
1955 unsigned long a
= 0, b
, x
= 0, y
= (unsigned long)salt
;
1956 unsigned long adata
, bdata
, mask
, len
;
1957 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
1964 len
+= sizeof(unsigned long);
1966 a
= load_unaligned_zeropad(name
+len
);
1967 b
= a
^ REPEAT_BYTE('/');
1968 } while (!(has_zero(a
, &adata
, &constants
) | has_zero(b
, &bdata
, &constants
)));
1970 adata
= prep_zero_mask(a
, adata
, &constants
);
1971 bdata
= prep_zero_mask(b
, bdata
, &constants
);
1972 mask
= create_zero_mask(adata
| bdata
);
1973 x
^= a
& zero_bytemask(mask
);
1975 return hashlen_create(fold_hash(x
, y
), len
+ find_zero(mask
));
1978 #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
1980 /* Return the hash of a string of known length */
1981 unsigned int full_name_hash(const void *salt
, const char *name
, unsigned int len
)
1983 unsigned long hash
= init_name_hash(salt
);
1985 hash
= partial_name_hash((unsigned char)*name
++, hash
);
1986 return end_name_hash(hash
);
1988 EXPORT_SYMBOL(full_name_hash
);
1990 /* Return the "hash_len" (hash and length) of a null-terminated string */
1991 u64
hashlen_string(const void *salt
, const char *name
)
1993 unsigned long hash
= init_name_hash(salt
);
1994 unsigned long len
= 0, c
;
1996 c
= (unsigned char)*name
;
1999 hash
= partial_name_hash(c
, hash
);
2000 c
= (unsigned char)name
[len
];
2002 return hashlen_create(end_name_hash(hash
), len
);
2004 EXPORT_SYMBOL(hashlen_string
);
2007 * We know there's a real path component here of at least
2010 static inline u64
hash_name(const void *salt
, const char *name
)
2012 unsigned long hash
= init_name_hash(salt
);
2013 unsigned long len
= 0, c
;
2015 c
= (unsigned char)*name
;
2018 hash
= partial_name_hash(c
, hash
);
2019 c
= (unsigned char)name
[len
];
2020 } while (c
&& c
!= '/');
2021 return hashlen_create(end_name_hash(hash
), len
);
2028 * This is the basic name resolution function, turning a pathname into
2029 * the final dentry. We expect 'base' to be positive and a directory.
2031 * Returns 0 and nd will have valid dentry and mnt on success.
2032 * Returns error and drops reference to input namei data on failure.
2034 static int link_path_walk(const char *name
, struct nameidata
*nd
)
2043 /* At this point we know we have a real path component. */
2048 err
= may_lookup(nd
);
2052 hash_len
= hash_name(nd
->path
.dentry
, name
);
2055 if (name
[0] == '.') switch (hashlen_len(hash_len
)) {
2057 if (name
[1] == '.') {
2059 nd
->flags
|= LOOKUP_JUMPED
;
2065 if (likely(type
== LAST_NORM
)) {
2066 struct dentry
*parent
= nd
->path
.dentry
;
2067 nd
->flags
&= ~LOOKUP_JUMPED
;
2068 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
2069 struct qstr
this = { { .hash_len
= hash_len
}, .name
= name
};
2070 err
= parent
->d_op
->d_hash(parent
, &this);
2073 hash_len
= this.hash_len
;
2078 nd
->last
.hash_len
= hash_len
;
2079 nd
->last
.name
= name
;
2080 nd
->last_type
= type
;
2082 name
+= hashlen_len(hash_len
);
2086 * If it wasn't NUL, we know it was '/'. Skip that
2087 * slash, and continue until no more slashes.
2091 } while (unlikely(*name
== '/'));
2092 if (unlikely(!*name
)) {
2094 /* pathname body, done */
2097 name
= nd
->stack
[nd
->depth
- 1].name
;
2098 /* trailing symlink, done */
2101 /* last component of nested symlink */
2102 err
= walk_component(nd
, WALK_GET
| WALK_PUT
);
2104 err
= walk_component(nd
, WALK_GET
);
2110 const char *s
= get_link(nd
);
2119 nd
->stack
[nd
->depth
- 1].name
= name
;
2124 if (unlikely(!d_can_lookup(nd
->path
.dentry
))) {
2125 if (nd
->flags
& LOOKUP_RCU
) {
2126 if (unlazy_walk(nd
, NULL
, 0))
2134 static const char *path_init(struct nameidata
*nd
, unsigned flags
)
2137 const char *s
= nd
->name
->name
;
2140 flags
&= ~LOOKUP_RCU
;
2142 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
2143 nd
->flags
= flags
| LOOKUP_JUMPED
| LOOKUP_PARENT
;
2145 if (flags
& LOOKUP_ROOT
) {
2146 struct dentry
*root
= nd
->root
.dentry
;
2147 struct inode
*inode
= root
->d_inode
;
2149 if (!d_can_lookup(root
))
2150 return ERR_PTR(-ENOTDIR
);
2151 retval
= inode_permission(inode
, MAY_EXEC
);
2153 return ERR_PTR(retval
);
2155 nd
->path
= nd
->root
;
2157 if (flags
& LOOKUP_RCU
) {
2159 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2160 nd
->root_seq
= nd
->seq
;
2161 nd
->m_seq
= read_seqbegin(&mount_lock
);
2163 path_get(&nd
->path
);
2168 nd
->root
.mnt
= NULL
;
2169 nd
->path
.mnt
= NULL
;
2170 nd
->path
.dentry
= NULL
;
2172 nd
->m_seq
= read_seqbegin(&mount_lock
);
2174 if (flags
& LOOKUP_RCU
)
2177 if (likely(!nd_jump_root(nd
)))
2179 nd
->root
.mnt
= NULL
;
2181 return ERR_PTR(-ECHILD
);
2182 } else if (nd
->dfd
== AT_FDCWD
) {
2183 if (flags
& LOOKUP_RCU
) {
2184 struct fs_struct
*fs
= current
->fs
;
2190 seq
= read_seqcount_begin(&fs
->seq
);
2192 nd
->inode
= nd
->path
.dentry
->d_inode
;
2193 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2194 } while (read_seqcount_retry(&fs
->seq
, seq
));
2196 get_fs_pwd(current
->fs
, &nd
->path
);
2197 nd
->inode
= nd
->path
.dentry
->d_inode
;
2201 /* Caller must check execute permissions on the starting path component */
2202 struct fd f
= fdget_raw(nd
->dfd
);
2203 struct dentry
*dentry
;
2206 return ERR_PTR(-EBADF
);
2208 dentry
= f
.file
->f_path
.dentry
;
2211 if (!d_can_lookup(dentry
)) {
2213 return ERR_PTR(-ENOTDIR
);
2217 nd
->path
= f
.file
->f_path
;
2218 if (flags
& LOOKUP_RCU
) {
2220 nd
->inode
= nd
->path
.dentry
->d_inode
;
2221 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2223 path_get(&nd
->path
);
2224 nd
->inode
= nd
->path
.dentry
->d_inode
;
2231 static const char *trailing_symlink(struct nameidata
*nd
)
2234 int error
= may_follow_link(nd
);
2235 if (unlikely(error
))
2236 return ERR_PTR(error
);
2237 nd
->flags
|= LOOKUP_PARENT
;
2238 nd
->stack
[0].name
= NULL
;
2243 static inline int lookup_last(struct nameidata
*nd
)
2245 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
2246 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2248 nd
->flags
&= ~LOOKUP_PARENT
;
2249 return walk_component(nd
,
2250 nd
->flags
& LOOKUP_FOLLOW
2252 ? WALK_PUT
| WALK_GET
2257 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2258 static int path_lookupat(struct nameidata
*nd
, unsigned flags
, struct path
*path
)
2260 const char *s
= path_init(nd
, flags
);
2265 while (!(err
= link_path_walk(s
, nd
))
2266 && ((err
= lookup_last(nd
)) > 0)) {
2267 s
= trailing_symlink(nd
);
2274 err
= complete_walk(nd
);
2276 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
)
2277 if (!d_can_lookup(nd
->path
.dentry
))
2281 nd
->path
.mnt
= NULL
;
2282 nd
->path
.dentry
= NULL
;
2288 static int filename_lookup(int dfd
, struct filename
*name
, unsigned flags
,
2289 struct path
*path
, struct path
*root
)
2292 struct nameidata nd
;
2294 return PTR_ERR(name
);
2295 if (unlikely(root
)) {
2297 flags
|= LOOKUP_ROOT
;
2299 set_nameidata(&nd
, dfd
, name
);
2300 retval
= path_lookupat(&nd
, flags
| LOOKUP_RCU
, path
);
2301 if (unlikely(retval
== -ECHILD
))
2302 retval
= path_lookupat(&nd
, flags
, path
);
2303 if (unlikely(retval
== -ESTALE
))
2304 retval
= path_lookupat(&nd
, flags
| LOOKUP_REVAL
, path
);
2306 if (likely(!retval
))
2307 audit_inode(name
, path
->dentry
, flags
& LOOKUP_PARENT
);
2308 restore_nameidata();
2313 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2314 static int path_parentat(struct nameidata
*nd
, unsigned flags
,
2315 struct path
*parent
)
2317 const char *s
= path_init(nd
, flags
);
2321 err
= link_path_walk(s
, nd
);
2323 err
= complete_walk(nd
);
2326 nd
->path
.mnt
= NULL
;
2327 nd
->path
.dentry
= NULL
;
2333 static struct filename
*filename_parentat(int dfd
, struct filename
*name
,
2334 unsigned int flags
, struct path
*parent
,
2335 struct qstr
*last
, int *type
)
2338 struct nameidata nd
;
2342 set_nameidata(&nd
, dfd
, name
);
2343 retval
= path_parentat(&nd
, flags
| LOOKUP_RCU
, parent
);
2344 if (unlikely(retval
== -ECHILD
))
2345 retval
= path_parentat(&nd
, flags
, parent
);
2346 if (unlikely(retval
== -ESTALE
))
2347 retval
= path_parentat(&nd
, flags
| LOOKUP_REVAL
, parent
);
2348 if (likely(!retval
)) {
2350 *type
= nd
.last_type
;
2351 audit_inode(name
, parent
->dentry
, LOOKUP_PARENT
);
2354 name
= ERR_PTR(retval
);
2356 restore_nameidata();
2360 /* does lookup, returns the object with parent locked */
2361 struct dentry
*kern_path_locked(const char *name
, struct path
*path
)
2363 struct filename
*filename
;
2368 filename
= filename_parentat(AT_FDCWD
, getname_kernel(name
), 0, path
,
2370 if (IS_ERR(filename
))
2371 return ERR_CAST(filename
);
2372 if (unlikely(type
!= LAST_NORM
)) {
2375 return ERR_PTR(-EINVAL
);
2377 inode_lock_nested(path
->dentry
->d_inode
, I_MUTEX_PARENT
);
2378 d
= __lookup_hash(&last
, path
->dentry
, 0);
2380 inode_unlock(path
->dentry
->d_inode
);
2387 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
2389 return filename_lookup(AT_FDCWD
, getname_kernel(name
),
2392 EXPORT_SYMBOL(kern_path
);
2395 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2396 * @dentry: pointer to dentry of the base directory
2397 * @mnt: pointer to vfs mount of the base directory
2398 * @name: pointer to file name
2399 * @flags: lookup flags
2400 * @path: pointer to struct path to fill
2402 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
2403 const char *name
, unsigned int flags
,
2406 struct path root
= {.mnt
= mnt
, .dentry
= dentry
};
2407 /* the first argument of filename_lookup() is ignored with root */
2408 return filename_lookup(AT_FDCWD
, getname_kernel(name
),
2409 flags
, path
, &root
);
2411 EXPORT_SYMBOL(vfs_path_lookup
);
2414 * lookup_one_len - filesystem helper to lookup single pathname component
2415 * @name: pathname component to lookup
2416 * @base: base directory to lookup from
2417 * @len: maximum length @len should be interpreted to
2419 * Note that this routine is purely a helper for filesystem usage and should
2420 * not be called by generic code.
2422 * The caller must hold base->i_mutex.
2424 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
2430 WARN_ON_ONCE(!inode_is_locked(base
->d_inode
));
2434 this.hash
= full_name_hash(base
, name
, len
);
2436 return ERR_PTR(-EACCES
);
2438 if (unlikely(name
[0] == '.')) {
2439 if (len
< 2 || (len
== 2 && name
[1] == '.'))
2440 return ERR_PTR(-EACCES
);
2444 c
= *(const unsigned char *)name
++;
2445 if (c
== '/' || c
== '\0')
2446 return ERR_PTR(-EACCES
);
2449 * See if the low-level filesystem might want
2450 * to use its own hash..
2452 if (base
->d_flags
& DCACHE_OP_HASH
) {
2453 int err
= base
->d_op
->d_hash(base
, &this);
2455 return ERR_PTR(err
);
2458 err
= inode_permission(base
->d_inode
, MAY_EXEC
);
2460 return ERR_PTR(err
);
2462 return __lookup_hash(&this, base
, 0);
2464 EXPORT_SYMBOL(lookup_one_len
);
2467 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2468 * @name: pathname component to lookup
2469 * @base: base directory to lookup from
2470 * @len: maximum length @len should be interpreted to
2472 * Note that this routine is purely a helper for filesystem usage and should
2473 * not be called by generic code.
2475 * Unlike lookup_one_len, it should be called without the parent
2476 * i_mutex held, and will take the i_mutex itself if necessary.
2478 struct dentry
*lookup_one_len_unlocked(const char *name
,
2479 struct dentry
*base
, int len
)
2488 this.hash
= full_name_hash(base
, name
, len
);
2490 return ERR_PTR(-EACCES
);
2492 if (unlikely(name
[0] == '.')) {
2493 if (len
< 2 || (len
== 2 && name
[1] == '.'))
2494 return ERR_PTR(-EACCES
);
2498 c
= *(const unsigned char *)name
++;
2499 if (c
== '/' || c
== '\0')
2500 return ERR_PTR(-EACCES
);
2503 * See if the low-level filesystem might want
2504 * to use its own hash..
2506 if (base
->d_flags
& DCACHE_OP_HASH
) {
2507 int err
= base
->d_op
->d_hash(base
, &this);
2509 return ERR_PTR(err
);
2512 err
= inode_permission(base
->d_inode
, MAY_EXEC
);
2514 return ERR_PTR(err
);
2516 ret
= lookup_dcache(&this, base
, 0);
2518 ret
= lookup_slow(&this, base
, 0);
2521 EXPORT_SYMBOL(lookup_one_len_unlocked
);
2523 #ifdef CONFIG_UNIX98_PTYS
2524 int path_pts(struct path
*path
)
2526 /* Find something mounted on "pts" in the same directory as
2529 struct dentry
*child
, *parent
;
2533 ret
= path_parent_directory(path
);
2537 parent
= path
->dentry
;
2540 child
= d_hash_and_lookup(parent
, &this);
2544 path
->dentry
= child
;
2551 int user_path_at_empty(int dfd
, const char __user
*name
, unsigned flags
,
2552 struct path
*path
, int *empty
)
2554 return filename_lookup(dfd
, getname_flags(name
, flags
, empty
),
2557 EXPORT_SYMBOL(user_path_at_empty
);
2560 * NB: most callers don't do anything directly with the reference to the
2561 * to struct filename, but the nd->last pointer points into the name string
2562 * allocated by getname. So we must hold the reference to it until all
2563 * path-walking is complete.
2565 static inline struct filename
*
2566 user_path_parent(int dfd
, const char __user
*path
,
2567 struct path
*parent
,
2572 /* only LOOKUP_REVAL is allowed in extra flags */
2573 return filename_parentat(dfd
, getname(path
), flags
& LOOKUP_REVAL
,
2574 parent
, last
, type
);
2578 * mountpoint_last - look up last component for umount
2579 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2580 * @path: pointer to container for result
2582 * This is a special lookup_last function just for umount. In this case, we
2583 * need to resolve the path without doing any revalidation.
2585 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2586 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2587 * in almost all cases, this lookup will be served out of the dcache. The only
2588 * cases where it won't are if nd->last refers to a symlink or the path is
2589 * bogus and it doesn't exist.
2592 * -error: if there was an error during lookup. This includes -ENOENT if the
2593 * lookup found a negative dentry. The nd->path reference will also be
2596 * 0: if we successfully resolved nd->path and found it to not to be a
2597 * symlink that needs to be followed. "path" will also be populated.
2598 * The nd->path reference will also be put.
2600 * 1: if we successfully resolved nd->last and found it to be a symlink
2601 * that needs to be followed. "path" will be populated with the path
2602 * to the link, and nd->path will *not* be put.
2605 mountpoint_last(struct nameidata
*nd
, struct path
*path
)
2608 struct dentry
*dentry
;
2609 struct dentry
*dir
= nd
->path
.dentry
;
2611 /* If we're in rcuwalk, drop out of it to handle last component */
2612 if (nd
->flags
& LOOKUP_RCU
) {
2613 if (unlazy_walk(nd
, NULL
, 0))
2617 nd
->flags
&= ~LOOKUP_PARENT
;
2619 if (unlikely(nd
->last_type
!= LAST_NORM
)) {
2620 error
= handle_dots(nd
, nd
->last_type
);
2623 dentry
= dget(nd
->path
.dentry
);
2625 dentry
= d_lookup(dir
, &nd
->last
);
2628 * No cached dentry. Mounted dentries are pinned in the
2629 * cache, so that means that this dentry is probably
2630 * a symlink or the path doesn't actually point
2631 * to a mounted dentry.
2633 dentry
= lookup_slow(&nd
->last
, dir
,
2634 nd
->flags
| LOOKUP_NO_REVAL
);
2636 return PTR_ERR(dentry
);
2639 if (d_is_negative(dentry
)) {
2645 path
->dentry
= dentry
;
2646 path
->mnt
= nd
->path
.mnt
;
2647 error
= should_follow_link(nd
, path
, nd
->flags
& LOOKUP_FOLLOW
,
2648 d_backing_inode(dentry
), 0);
2649 if (unlikely(error
))
2657 * path_mountpoint - look up a path to be umounted
2658 * @nd: lookup context
2659 * @flags: lookup flags
2660 * @path: pointer to container for result
2662 * Look up the given name, but don't attempt to revalidate the last component.
2663 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2666 path_mountpoint(struct nameidata
*nd
, unsigned flags
, struct path
*path
)
2668 const char *s
= path_init(nd
, flags
);
2672 while (!(err
= link_path_walk(s
, nd
)) &&
2673 (err
= mountpoint_last(nd
, path
)) > 0) {
2674 s
= trailing_symlink(nd
);
2685 filename_mountpoint(int dfd
, struct filename
*name
, struct path
*path
,
2688 struct nameidata nd
;
2691 return PTR_ERR(name
);
2692 set_nameidata(&nd
, dfd
, name
);
2693 error
= path_mountpoint(&nd
, flags
| LOOKUP_RCU
, path
);
2694 if (unlikely(error
== -ECHILD
))
2695 error
= path_mountpoint(&nd
, flags
, path
);
2696 if (unlikely(error
== -ESTALE
))
2697 error
= path_mountpoint(&nd
, flags
| LOOKUP_REVAL
, path
);
2699 audit_inode(name
, path
->dentry
, 0);
2700 restore_nameidata();
2706 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2707 * @dfd: directory file descriptor
2708 * @name: pathname from userland
2709 * @flags: lookup flags
2710 * @path: pointer to container to hold result
2712 * A umount is a special case for path walking. We're not actually interested
2713 * in the inode in this situation, and ESTALE errors can be a problem. We
2714 * simply want track down the dentry and vfsmount attached at the mountpoint
2715 * and avoid revalidating the last component.
2717 * Returns 0 and populates "path" on success.
2720 user_path_mountpoint_at(int dfd
, const char __user
*name
, unsigned int flags
,
2723 return filename_mountpoint(dfd
, getname(name
), path
, flags
);
2727 kern_path_mountpoint(int dfd
, const char *name
, struct path
*path
,
2730 return filename_mountpoint(dfd
, getname_kernel(name
), path
, flags
);
2732 EXPORT_SYMBOL(kern_path_mountpoint
);
2734 int __check_sticky(struct inode
*dir
, struct inode
*inode
)
2736 kuid_t fsuid
= current_fsuid();
2738 if (uid_eq(inode
->i_uid
, fsuid
))
2740 if (uid_eq(dir
->i_uid
, fsuid
))
2742 return !capable_wrt_inode_uidgid(inode
, CAP_FOWNER
);
2744 EXPORT_SYMBOL(__check_sticky
);
2747 * Check whether we can remove a link victim from directory dir, check
2748 * whether the type of victim is right.
2749 * 1. We can't do it if dir is read-only (done in permission())
2750 * 2. We should have write and exec permissions on dir
2751 * 3. We can't remove anything from append-only dir
2752 * 4. We can't do anything with immutable dir (done in permission())
2753 * 5. If the sticky bit on dir is set we should either
2754 * a. be owner of dir, or
2755 * b. be owner of victim, or
2756 * c. have CAP_FOWNER capability
2757 * 6. If the victim is append-only or immutable we can't do antyhing with
2758 * links pointing to it.
2759 * 7. If the victim has an unknown uid or gid we can't change the inode.
2760 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2761 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2762 * 10. We can't remove a root or mountpoint.
2763 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
2764 * nfs_async_unlink().
2766 static int may_delete(struct inode
*dir
, struct dentry
*victim
, bool isdir
)
2768 struct inode
*inode
= d_backing_inode(victim
);
2771 if (d_is_negative(victim
))
2775 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
2776 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
2778 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
2784 if (check_sticky(dir
, inode
) || IS_APPEND(inode
) ||
2785 IS_IMMUTABLE(inode
) || IS_SWAPFILE(inode
) || HAS_UNMAPPED_ID(inode
))
2788 if (!d_is_dir(victim
))
2790 if (IS_ROOT(victim
))
2792 } else if (d_is_dir(victim
))
2794 if (IS_DEADDIR(dir
))
2796 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
2801 /* Check whether we can create an object with dentry child in directory
2803 * 1. We can't do it if child already exists (open has special treatment for
2804 * this case, but since we are inlined it's OK)
2805 * 2. We can't do it if dir is read-only (done in permission())
2806 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2807 * 4. We should have write and exec permissions on dir
2808 * 5. We can't do it if dir is immutable (done in permission())
2810 static inline int may_create(struct inode
*dir
, struct dentry
*child
)
2812 struct user_namespace
*s_user_ns
;
2813 audit_inode_child(dir
, child
, AUDIT_TYPE_CHILD_CREATE
);
2816 if (IS_DEADDIR(dir
))
2818 s_user_ns
= dir
->i_sb
->s_user_ns
;
2819 if (!kuid_has_mapping(s_user_ns
, current_fsuid()) ||
2820 !kgid_has_mapping(s_user_ns
, current_fsgid()))
2822 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
2826 * p1 and p2 should be directories on the same fs.
2828 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
2833 inode_lock_nested(p1
->d_inode
, I_MUTEX_PARENT
);
2837 mutex_lock(&p1
->d_sb
->s_vfs_rename_mutex
);
2839 p
= d_ancestor(p2
, p1
);
2841 inode_lock_nested(p2
->d_inode
, I_MUTEX_PARENT
);
2842 inode_lock_nested(p1
->d_inode
, I_MUTEX_CHILD
);
2846 p
= d_ancestor(p1
, p2
);
2848 inode_lock_nested(p1
->d_inode
, I_MUTEX_PARENT
);
2849 inode_lock_nested(p2
->d_inode
, I_MUTEX_CHILD
);
2853 inode_lock_nested(p1
->d_inode
, I_MUTEX_PARENT
);
2854 inode_lock_nested(p2
->d_inode
, I_MUTEX_PARENT2
);
2857 EXPORT_SYMBOL(lock_rename
);
2859 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
2861 inode_unlock(p1
->d_inode
);
2863 inode_unlock(p2
->d_inode
);
2864 mutex_unlock(&p1
->d_sb
->s_vfs_rename_mutex
);
2867 EXPORT_SYMBOL(unlock_rename
);
2869 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2872 int error
= may_create(dir
, dentry
);
2876 if (!dir
->i_op
->create
)
2877 return -EACCES
; /* shouldn't it be ENOSYS? */
2880 error
= security_inode_create(dir
, dentry
, mode
);
2883 error
= dir
->i_op
->create(dir
, dentry
, mode
, want_excl
);
2885 fsnotify_create(dir
, dentry
);
2888 EXPORT_SYMBOL(vfs_create
);
2890 bool may_open_dev(const struct path
*path
)
2892 return !(path
->mnt
->mnt_flags
& MNT_NODEV
) &&
2893 !(path
->mnt
->mnt_sb
->s_iflags
& SB_I_NODEV
);
2896 static int may_open(struct path
*path
, int acc_mode
, int flag
)
2898 struct dentry
*dentry
= path
->dentry
;
2899 struct inode
*inode
= dentry
->d_inode
;
2905 switch (inode
->i_mode
& S_IFMT
) {
2909 if (acc_mode
& MAY_WRITE
)
2914 if (!may_open_dev(path
))
2923 error
= inode_permission(inode
, MAY_OPEN
| acc_mode
);
2928 * An append-only file must be opened in append mode for writing.
2930 if (IS_APPEND(inode
)) {
2931 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
2937 /* O_NOATIME can only be set by the owner or superuser */
2938 if (flag
& O_NOATIME
&& !inode_owner_or_capable(inode
))
2944 static int handle_truncate(struct file
*filp
)
2946 struct path
*path
= &filp
->f_path
;
2947 struct inode
*inode
= path
->dentry
->d_inode
;
2948 int error
= get_write_access(inode
);
2952 * Refuse to truncate files with mandatory locks held on them.
2954 error
= locks_verify_locked(filp
);
2956 error
= security_path_truncate(path
);
2958 error
= do_truncate(path
->dentry
, 0,
2959 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
2962 put_write_access(inode
);
2966 static inline int open_to_namei_flags(int flag
)
2968 if ((flag
& O_ACCMODE
) == 3)
2973 static int may_o_create(const struct path
*dir
, struct dentry
*dentry
, umode_t mode
)
2975 struct user_namespace
*s_user_ns
;
2976 int error
= security_path_mknod(dir
, dentry
, mode
, 0);
2980 s_user_ns
= dir
->dentry
->d_sb
->s_user_ns
;
2981 if (!kuid_has_mapping(s_user_ns
, current_fsuid()) ||
2982 !kgid_has_mapping(s_user_ns
, current_fsgid()))
2985 error
= inode_permission(dir
->dentry
->d_inode
, MAY_WRITE
| MAY_EXEC
);
2989 return security_inode_create(dir
->dentry
->d_inode
, dentry
, mode
);
2993 * Attempt to atomically look up, create and open a file from a negative
2996 * Returns 0 if successful. The file will have been created and attached to
2997 * @file by the filesystem calling finish_open().
2999 * Returns 1 if the file was looked up only or didn't need creating. The
3000 * caller will need to perform the open themselves. @path will have been
3001 * updated to point to the new dentry. This may be negative.
3003 * Returns an error code otherwise.
3005 static int atomic_open(struct nameidata
*nd
, struct dentry
*dentry
,
3006 struct path
*path
, struct file
*file
,
3007 const struct open_flags
*op
,
3008 int open_flag
, umode_t mode
,
3011 struct dentry
*const DENTRY_NOT_SET
= (void *) -1UL;
3012 struct inode
*dir
= nd
->path
.dentry
->d_inode
;
3015 if (!(~open_flag
& (O_EXCL
| O_CREAT
))) /* both O_EXCL and O_CREAT */
3016 open_flag
&= ~O_TRUNC
;
3018 if (nd
->flags
& LOOKUP_DIRECTORY
)
3019 open_flag
|= O_DIRECTORY
;
3021 file
->f_path
.dentry
= DENTRY_NOT_SET
;
3022 file
->f_path
.mnt
= nd
->path
.mnt
;
3023 error
= dir
->i_op
->atomic_open(dir
, dentry
, file
,
3024 open_to_namei_flags(open_flag
),
3026 d_lookup_done(dentry
);
3029 * We didn't have the inode before the open, so check open
3032 int acc_mode
= op
->acc_mode
;
3033 if (*opened
& FILE_CREATED
) {
3034 WARN_ON(!(open_flag
& O_CREAT
));
3035 fsnotify_create(dir
, dentry
);
3038 error
= may_open(&file
->f_path
, acc_mode
, open_flag
);
3039 if (WARN_ON(error
> 0))
3041 } else if (error
> 0) {
3042 if (WARN_ON(file
->f_path
.dentry
== DENTRY_NOT_SET
)) {
3045 if (file
->f_path
.dentry
) {
3047 dentry
= file
->f_path
.dentry
;
3049 if (*opened
& FILE_CREATED
)
3050 fsnotify_create(dir
, dentry
);
3051 if (unlikely(d_is_negative(dentry
))) {
3054 path
->dentry
= dentry
;
3055 path
->mnt
= nd
->path
.mnt
;
3065 * Look up and maybe create and open the last component.
3067 * Must be called with i_mutex held on parent.
3069 * Returns 0 if the file was successfully atomically created (if necessary) and
3070 * opened. In this case the file will be returned attached to @file.
3072 * Returns 1 if the file was not completely opened at this time, though lookups
3073 * and creations will have been performed and the dentry returned in @path will
3074 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
3075 * specified then a negative dentry may be returned.
3077 * An error code is returned otherwise.
3079 * FILE_CREATE will be set in @*opened if the dentry was created and will be
3080 * cleared otherwise prior to returning.
3082 static int lookup_open(struct nameidata
*nd
, struct path
*path
,
3084 const struct open_flags
*op
,
3085 bool got_write
, int *opened
)
3087 struct dentry
*dir
= nd
->path
.dentry
;
3088 struct inode
*dir_inode
= dir
->d_inode
;
3089 int open_flag
= op
->open_flag
;
3090 struct dentry
*dentry
;
3091 int error
, create_error
= 0;
3092 umode_t mode
= op
->mode
;
3093 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
3095 if (unlikely(IS_DEADDIR(dir_inode
)))
3098 *opened
&= ~FILE_CREATED
;
3099 dentry
= d_lookup(dir
, &nd
->last
);
3102 dentry
= d_alloc_parallel(dir
, &nd
->last
, &wq
);
3104 return PTR_ERR(dentry
);
3106 if (d_in_lookup(dentry
))
3109 if (!(dentry
->d_flags
& DCACHE_OP_REVALIDATE
))
3112 error
= d_revalidate(dentry
, nd
->flags
);
3113 if (likely(error
> 0))
3117 d_invalidate(dentry
);
3121 if (dentry
->d_inode
) {
3122 /* Cached positive dentry: will open in f_op->open */
3127 * Checking write permission is tricky, bacuse we don't know if we are
3128 * going to actually need it: O_CREAT opens should work as long as the
3129 * file exists. But checking existence breaks atomicity. The trick is
3130 * to check access and if not granted clear O_CREAT from the flags.
3132 * Another problem is returing the "right" error value (e.g. for an
3133 * O_EXCL open we want to return EEXIST not EROFS).
3135 if (open_flag
& O_CREAT
) {
3136 if (!IS_POSIXACL(dir
->d_inode
))
3137 mode
&= ~current_umask();
3138 if (unlikely(!got_write
)) {
3139 create_error
= -EROFS
;
3140 open_flag
&= ~O_CREAT
;
3141 if (open_flag
& (O_EXCL
| O_TRUNC
))
3143 /* No side effects, safe to clear O_CREAT */
3145 create_error
= may_o_create(&nd
->path
, dentry
, mode
);
3147 open_flag
&= ~O_CREAT
;
3148 if (open_flag
& O_EXCL
)
3152 } else if ((open_flag
& (O_TRUNC
|O_WRONLY
|O_RDWR
)) &&
3153 unlikely(!got_write
)) {
3155 * No O_CREATE -> atomicity not a requirement -> fall
3156 * back to lookup + open
3161 if (dir_inode
->i_op
->atomic_open
) {
3162 error
= atomic_open(nd
, dentry
, path
, file
, op
, open_flag
,
3164 if (unlikely(error
== -ENOENT
) && create_error
)
3165 error
= create_error
;
3170 if (d_in_lookup(dentry
)) {
3171 struct dentry
*res
= dir_inode
->i_op
->lookup(dir_inode
, dentry
,
3173 d_lookup_done(dentry
);
3174 if (unlikely(res
)) {
3176 error
= PTR_ERR(res
);
3184 /* Negative dentry, just create the file */
3185 if (!dentry
->d_inode
&& (open_flag
& O_CREAT
)) {
3186 *opened
|= FILE_CREATED
;
3187 audit_inode_child(dir_inode
, dentry
, AUDIT_TYPE_CHILD_CREATE
);
3188 if (!dir_inode
->i_op
->create
) {
3192 error
= dir_inode
->i_op
->create(dir_inode
, dentry
, mode
,
3193 open_flag
& O_EXCL
);
3196 fsnotify_create(dir_inode
, dentry
);
3198 if (unlikely(create_error
) && !dentry
->d_inode
) {
3199 error
= create_error
;
3203 path
->dentry
= dentry
;
3204 path
->mnt
= nd
->path
.mnt
;
3213 * Handle the last step of open()
3215 static int do_last(struct nameidata
*nd
,
3216 struct file
*file
, const struct open_flags
*op
,
3219 struct dentry
*dir
= nd
->path
.dentry
;
3220 int open_flag
= op
->open_flag
;
3221 bool will_truncate
= (open_flag
& O_TRUNC
) != 0;
3222 bool got_write
= false;
3223 int acc_mode
= op
->acc_mode
;
3225 struct inode
*inode
;
3229 nd
->flags
&= ~LOOKUP_PARENT
;
3230 nd
->flags
|= op
->intent
;
3232 if (nd
->last_type
!= LAST_NORM
) {
3233 error
= handle_dots(nd
, nd
->last_type
);
3234 if (unlikely(error
))
3239 if (!(open_flag
& O_CREAT
)) {
3240 if (nd
->last
.name
[nd
->last
.len
])
3241 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
3242 /* we _can_ be in RCU mode here */
3243 error
= lookup_fast(nd
, &path
, &inode
, &seq
);
3244 if (likely(error
> 0))
3250 BUG_ON(nd
->inode
!= dir
->d_inode
);
3251 BUG_ON(nd
->flags
& LOOKUP_RCU
);
3253 /* create side of things */
3255 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3256 * has been cleared when we got to the last component we are
3259 error
= complete_walk(nd
);
3263 audit_inode(nd
->name
, dir
, LOOKUP_PARENT
);
3264 /* trailing slashes? */
3265 if (unlikely(nd
->last
.name
[nd
->last
.len
]))
3269 if (open_flag
& (O_CREAT
| O_TRUNC
| O_WRONLY
| O_RDWR
)) {
3270 error
= mnt_want_write(nd
->path
.mnt
);
3274 * do _not_ fail yet - we might not need that or fail with
3275 * a different error; let lookup_open() decide; we'll be
3276 * dropping this one anyway.
3279 if (open_flag
& O_CREAT
)
3280 inode_lock(dir
->d_inode
);
3282 inode_lock_shared(dir
->d_inode
);
3283 error
= lookup_open(nd
, &path
, file
, op
, got_write
, opened
);
3284 if (open_flag
& O_CREAT
)
3285 inode_unlock(dir
->d_inode
);
3287 inode_unlock_shared(dir
->d_inode
);
3293 if ((*opened
& FILE_CREATED
) ||
3294 !S_ISREG(file_inode(file
)->i_mode
))
3295 will_truncate
= false;
3297 audit_inode(nd
->name
, file
->f_path
.dentry
, 0);
3301 if (*opened
& FILE_CREATED
) {
3302 /* Don't check for write permission, don't truncate */
3303 open_flag
&= ~O_TRUNC
;
3304 will_truncate
= false;
3306 path_to_nameidata(&path
, nd
);
3307 goto finish_open_created
;
3311 * If atomic_open() acquired write access it is dropped now due to
3312 * possible mount and symlink following (this might be optimized away if
3316 mnt_drop_write(nd
->path
.mnt
);
3320 error
= follow_managed(&path
, nd
);
3321 if (unlikely(error
< 0))
3324 if (unlikely(d_is_negative(path
.dentry
))) {
3325 path_to_nameidata(&path
, nd
);
3330 * create/update audit record if it already exists.
3332 audit_inode(nd
->name
, path
.dentry
, 0);
3334 if (unlikely((open_flag
& (O_EXCL
| O_CREAT
)) == (O_EXCL
| O_CREAT
))) {
3335 path_to_nameidata(&path
, nd
);
3339 seq
= 0; /* out of RCU mode, so the value doesn't matter */
3340 inode
= d_backing_inode(path
.dentry
);
3344 error
= should_follow_link(nd
, &path
, nd
->flags
& LOOKUP_FOLLOW
,
3346 if (unlikely(error
))
3349 path_to_nameidata(&path
, nd
);
3352 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3354 error
= complete_walk(nd
);
3357 audit_inode(nd
->name
, nd
->path
.dentry
, 0);
3359 if ((open_flag
& O_CREAT
) && d_is_dir(nd
->path
.dentry
))
3362 if ((nd
->flags
& LOOKUP_DIRECTORY
) && !d_can_lookup(nd
->path
.dentry
))
3364 if (!d_is_reg(nd
->path
.dentry
))
3365 will_truncate
= false;
3367 if (will_truncate
) {
3368 error
= mnt_want_write(nd
->path
.mnt
);
3373 finish_open_created
:
3374 error
= may_open(&nd
->path
, acc_mode
, open_flag
);
3377 BUG_ON(*opened
& FILE_OPENED
); /* once it's opened, it's opened */
3378 error
= vfs_open(&nd
->path
, file
, current_cred());
3381 *opened
|= FILE_OPENED
;
3383 error
= open_check_o_direct(file
);
3385 error
= ima_file_check(file
, op
->acc_mode
, *opened
);
3386 if (!error
&& will_truncate
)
3387 error
= handle_truncate(file
);
3389 if (unlikely(error
) && (*opened
& FILE_OPENED
))
3391 if (unlikely(error
> 0)) {
3396 mnt_drop_write(nd
->path
.mnt
);
3400 static int do_tmpfile(struct nameidata
*nd
, unsigned flags
,
3401 const struct open_flags
*op
,
3402 struct file
*file
, int *opened
)
3404 static const struct qstr name
= QSTR_INIT("/", 1);
3405 struct dentry
*child
;
3408 int error
= path_lookupat(nd
, flags
| LOOKUP_DIRECTORY
, &path
);
3409 if (unlikely(error
))
3411 error
= mnt_want_write(path
.mnt
);
3412 if (unlikely(error
))
3414 dir
= path
.dentry
->d_inode
;
3415 /* we want directory to be writable */
3416 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
3419 if (!dir
->i_op
->tmpfile
) {
3420 error
= -EOPNOTSUPP
;
3423 child
= d_alloc(path
.dentry
, &name
);
3424 if (unlikely(!child
)) {
3429 path
.dentry
= child
;
3430 error
= dir
->i_op
->tmpfile(dir
, child
, op
->mode
);
3433 audit_inode(nd
->name
, child
, 0);
3434 /* Don't check for other permissions, the inode was just created */
3435 error
= may_open(&path
, 0, op
->open_flag
);
3438 file
->f_path
.mnt
= path
.mnt
;
3439 error
= finish_open(file
, child
, NULL
, opened
);
3442 error
= open_check_o_direct(file
);
3445 } else if (!(op
->open_flag
& O_EXCL
)) {
3446 struct inode
*inode
= file_inode(file
);
3447 spin_lock(&inode
->i_lock
);
3448 inode
->i_state
|= I_LINKABLE
;
3449 spin_unlock(&inode
->i_lock
);
3452 mnt_drop_write(path
.mnt
);
3458 static int do_o_path(struct nameidata
*nd
, unsigned flags
, struct file
*file
)
3461 int error
= path_lookupat(nd
, flags
, &path
);
3463 audit_inode(nd
->name
, path
.dentry
, 0);
3464 error
= vfs_open(&path
, file
, current_cred());
3470 static struct file
*path_openat(struct nameidata
*nd
,
3471 const struct open_flags
*op
, unsigned flags
)
3478 file
= get_empty_filp();
3482 file
->f_flags
= op
->open_flag
;
3484 if (unlikely(file
->f_flags
& __O_TMPFILE
)) {
3485 error
= do_tmpfile(nd
, flags
, op
, file
, &opened
);
3489 if (unlikely(file
->f_flags
& O_PATH
)) {
3490 error
= do_o_path(nd
, flags
, file
);
3492 opened
|= FILE_OPENED
;
3496 s
= path_init(nd
, flags
);
3501 while (!(error
= link_path_walk(s
, nd
)) &&
3502 (error
= do_last(nd
, file
, op
, &opened
)) > 0) {
3503 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
3504 s
= trailing_symlink(nd
);
3512 if (!(opened
& FILE_OPENED
)) {
3516 if (unlikely(error
)) {
3517 if (error
== -EOPENSTALE
) {
3518 if (flags
& LOOKUP_RCU
)
3523 file
= ERR_PTR(error
);
3528 struct file
*do_filp_open(int dfd
, struct filename
*pathname
,
3529 const struct open_flags
*op
)
3531 struct nameidata nd
;
3532 int flags
= op
->lookup_flags
;
3535 set_nameidata(&nd
, dfd
, pathname
);
3536 filp
= path_openat(&nd
, op
, flags
| LOOKUP_RCU
);
3537 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
3538 filp
= path_openat(&nd
, op
, flags
);
3539 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
3540 filp
= path_openat(&nd
, op
, flags
| LOOKUP_REVAL
);
3541 restore_nameidata();
3545 struct file
*do_file_open_root(struct dentry
*dentry
, struct vfsmount
*mnt
,
3546 const char *name
, const struct open_flags
*op
)
3548 struct nameidata nd
;
3550 struct filename
*filename
;
3551 int flags
= op
->lookup_flags
| LOOKUP_ROOT
;
3554 nd
.root
.dentry
= dentry
;
3556 if (d_is_symlink(dentry
) && op
->intent
& LOOKUP_OPEN
)
3557 return ERR_PTR(-ELOOP
);
3559 filename
= getname_kernel(name
);
3560 if (IS_ERR(filename
))
3561 return ERR_CAST(filename
);
3563 set_nameidata(&nd
, -1, filename
);
3564 file
= path_openat(&nd
, op
, flags
| LOOKUP_RCU
);
3565 if (unlikely(file
== ERR_PTR(-ECHILD
)))
3566 file
= path_openat(&nd
, op
, flags
);
3567 if (unlikely(file
== ERR_PTR(-ESTALE
)))
3568 file
= path_openat(&nd
, op
, flags
| LOOKUP_REVAL
);
3569 restore_nameidata();
3574 static struct dentry
*filename_create(int dfd
, struct filename
*name
,
3575 struct path
*path
, unsigned int lookup_flags
)
3577 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
3582 bool is_dir
= (lookup_flags
& LOOKUP_DIRECTORY
);
3585 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3586 * other flags passed in are ignored!
3588 lookup_flags
&= LOOKUP_REVAL
;
3590 name
= filename_parentat(dfd
, name
, lookup_flags
, path
, &last
, &type
);
3592 return ERR_CAST(name
);
3595 * Yucky last component or no last component at all?
3596 * (foo/., foo/.., /////)
3598 if (unlikely(type
!= LAST_NORM
))
3601 /* don't fail immediately if it's r/o, at least try to report other errors */
3602 err2
= mnt_want_write(path
->mnt
);
3604 * Do the final lookup.
3606 lookup_flags
|= LOOKUP_CREATE
| LOOKUP_EXCL
;
3607 inode_lock_nested(path
->dentry
->d_inode
, I_MUTEX_PARENT
);
3608 dentry
= __lookup_hash(&last
, path
->dentry
, lookup_flags
);
3613 if (d_is_positive(dentry
))
3617 * Special case - lookup gave negative, but... we had foo/bar/
3618 * From the vfs_mknod() POV we just have a negative dentry -
3619 * all is fine. Let's be bastards - you had / on the end, you've
3620 * been asking for (non-existent) directory. -ENOENT for you.
3622 if (unlikely(!is_dir
&& last
.name
[last
.len
])) {
3626 if (unlikely(err2
)) {
3634 dentry
= ERR_PTR(error
);
3636 inode_unlock(path
->dentry
->d_inode
);
3638 mnt_drop_write(path
->mnt
);
3645 struct dentry
*kern_path_create(int dfd
, const char *pathname
,
3646 struct path
*path
, unsigned int lookup_flags
)
3648 return filename_create(dfd
, getname_kernel(pathname
),
3649 path
, lookup_flags
);
3651 EXPORT_SYMBOL(kern_path_create
);
3653 void done_path_create(struct path
*path
, struct dentry
*dentry
)
3656 inode_unlock(path
->dentry
->d_inode
);
3657 mnt_drop_write(path
->mnt
);
3660 EXPORT_SYMBOL(done_path_create
);
3662 inline struct dentry
*user_path_create(int dfd
, const char __user
*pathname
,
3663 struct path
*path
, unsigned int lookup_flags
)
3665 return filename_create(dfd
, getname(pathname
), path
, lookup_flags
);
3667 EXPORT_SYMBOL(user_path_create
);
3669 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
3671 int error
= may_create(dir
, dentry
);
3676 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
3679 if (!dir
->i_op
->mknod
)
3682 error
= devcgroup_inode_mknod(mode
, dev
);
3686 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
3690 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
3692 fsnotify_create(dir
, dentry
);
3695 EXPORT_SYMBOL(vfs_mknod
);
3697 static int may_mknod(umode_t mode
)
3699 switch (mode
& S_IFMT
) {
3705 case 0: /* zero mode translates to S_IFREG */
3714 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, umode_t
, mode
,
3717 struct dentry
*dentry
;
3720 unsigned int lookup_flags
= 0;
3722 error
= may_mknod(mode
);
3726 dentry
= user_path_create(dfd
, filename
, &path
, lookup_flags
);
3728 return PTR_ERR(dentry
);
3730 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3731 mode
&= ~current_umask();
3732 error
= security_path_mknod(&path
, dentry
, mode
, dev
);
3735 switch (mode
& S_IFMT
) {
3736 case 0: case S_IFREG
:
3737 error
= vfs_create(path
.dentry
->d_inode
,dentry
,mode
,true);
3739 ima_post_path_mknod(dentry
);
3741 case S_IFCHR
: case S_IFBLK
:
3742 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,
3743 new_decode_dev(dev
));
3745 case S_IFIFO
: case S_IFSOCK
:
3746 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,0);
3750 done_path_create(&path
, dentry
);
3751 if (retry_estale(error
, lookup_flags
)) {
3752 lookup_flags
|= LOOKUP_REVAL
;
3758 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, umode_t
, mode
, unsigned, dev
)
3760 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
3763 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
3765 int error
= may_create(dir
, dentry
);
3766 unsigned max_links
= dir
->i_sb
->s_max_links
;
3771 if (!dir
->i_op
->mkdir
)
3774 mode
&= (S_IRWXUGO
|S_ISVTX
);
3775 error
= security_inode_mkdir(dir
, dentry
, mode
);
3779 if (max_links
&& dir
->i_nlink
>= max_links
)
3782 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
3784 fsnotify_mkdir(dir
, dentry
);
3787 EXPORT_SYMBOL(vfs_mkdir
);
3789 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, umode_t
, mode
)
3791 struct dentry
*dentry
;
3794 unsigned int lookup_flags
= LOOKUP_DIRECTORY
;
3797 dentry
= user_path_create(dfd
, pathname
, &path
, lookup_flags
);
3799 return PTR_ERR(dentry
);
3801 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3802 mode
&= ~current_umask();
3803 error
= security_path_mkdir(&path
, dentry
, mode
);
3805 error
= vfs_mkdir(path
.dentry
->d_inode
, dentry
, mode
);
3806 done_path_create(&path
, dentry
);
3807 if (retry_estale(error
, lookup_flags
)) {
3808 lookup_flags
|= LOOKUP_REVAL
;
3814 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, umode_t
, mode
)
3816 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
3819 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3821 int error
= may_delete(dir
, dentry
, 1);
3826 if (!dir
->i_op
->rmdir
)
3830 inode_lock(dentry
->d_inode
);
3833 if (is_local_mountpoint(dentry
))
3836 error
= security_inode_rmdir(dir
, dentry
);
3840 shrink_dcache_parent(dentry
);
3841 error
= dir
->i_op
->rmdir(dir
, dentry
);
3845 dentry
->d_inode
->i_flags
|= S_DEAD
;
3847 detach_mounts(dentry
);
3850 inode_unlock(dentry
->d_inode
);
3856 EXPORT_SYMBOL(vfs_rmdir
);
3858 static long do_rmdir(int dfd
, const char __user
*pathname
)
3861 struct filename
*name
;
3862 struct dentry
*dentry
;
3866 unsigned int lookup_flags
= 0;
3868 name
= user_path_parent(dfd
, pathname
,
3869 &path
, &last
, &type
, lookup_flags
);
3871 return PTR_ERR(name
);
3885 error
= mnt_want_write(path
.mnt
);
3889 inode_lock_nested(path
.dentry
->d_inode
, I_MUTEX_PARENT
);
3890 dentry
= __lookup_hash(&last
, path
.dentry
, lookup_flags
);
3891 error
= PTR_ERR(dentry
);
3894 if (!dentry
->d_inode
) {
3898 error
= security_path_rmdir(&path
, dentry
);
3901 error
= vfs_rmdir(path
.dentry
->d_inode
, dentry
);
3905 inode_unlock(path
.dentry
->d_inode
);
3906 mnt_drop_write(path
.mnt
);
3910 if (retry_estale(error
, lookup_flags
)) {
3911 lookup_flags
|= LOOKUP_REVAL
;
3917 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
3919 return do_rmdir(AT_FDCWD
, pathname
);
3923 * vfs_unlink - unlink a filesystem object
3924 * @dir: parent directory
3926 * @delegated_inode: returns victim inode, if the inode is delegated.
3928 * The caller must hold dir->i_mutex.
3930 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3931 * return a reference to the inode in delegated_inode. The caller
3932 * should then break the delegation on that inode and retry. Because
3933 * breaking a delegation may take a long time, the caller should drop
3934 * dir->i_mutex before doing so.
3936 * Alternatively, a caller may pass NULL for delegated_inode. This may
3937 * be appropriate for callers that expect the underlying filesystem not
3938 * to be NFS exported.
3940 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
, struct inode
**delegated_inode
)
3942 struct inode
*target
= dentry
->d_inode
;
3943 int error
= may_delete(dir
, dentry
, 0);
3948 if (!dir
->i_op
->unlink
)
3952 if (is_local_mountpoint(dentry
))
3955 error
= security_inode_unlink(dir
, dentry
);
3957 error
= try_break_deleg(target
, delegated_inode
);
3960 error
= dir
->i_op
->unlink(dir
, dentry
);
3963 detach_mounts(dentry
);
3968 inode_unlock(target
);
3970 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3971 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
3972 fsnotify_link_count(target
);
3978 EXPORT_SYMBOL(vfs_unlink
);
3981 * Make sure that the actual truncation of the file will occur outside its
3982 * directory's i_mutex. Truncate can take a long time if there is a lot of
3983 * writeout happening, and we don't want to prevent access to the directory
3984 * while waiting on the I/O.
3986 static long do_unlinkat(int dfd
, const char __user
*pathname
)
3989 struct filename
*name
;
3990 struct dentry
*dentry
;
3994 struct inode
*inode
= NULL
;
3995 struct inode
*delegated_inode
= NULL
;
3996 unsigned int lookup_flags
= 0;
3998 name
= user_path_parent(dfd
, pathname
,
3999 &path
, &last
, &type
, lookup_flags
);
4001 return PTR_ERR(name
);
4004 if (type
!= LAST_NORM
)
4007 error
= mnt_want_write(path
.mnt
);
4011 inode_lock_nested(path
.dentry
->d_inode
, I_MUTEX_PARENT
);
4012 dentry
= __lookup_hash(&last
, path
.dentry
, lookup_flags
);
4013 error
= PTR_ERR(dentry
);
4014 if (!IS_ERR(dentry
)) {
4015 /* Why not before? Because we want correct error value */
4016 if (last
.name
[last
.len
])
4018 inode
= dentry
->d_inode
;
4019 if (d_is_negative(dentry
))
4022 error
= security_path_unlink(&path
, dentry
);
4025 error
= vfs_unlink(path
.dentry
->d_inode
, dentry
, &delegated_inode
);
4029 inode_unlock(path
.dentry
->d_inode
);
4031 iput(inode
); /* truncate the inode here */
4033 if (delegated_inode
) {
4034 error
= break_deleg_wait(&delegated_inode
);
4038 mnt_drop_write(path
.mnt
);
4042 if (retry_estale(error
, lookup_flags
)) {
4043 lookup_flags
|= LOOKUP_REVAL
;
4050 if (d_is_negative(dentry
))
4052 else if (d_is_dir(dentry
))
4059 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
4061 if ((flag
& ~AT_REMOVEDIR
) != 0)
4064 if (flag
& AT_REMOVEDIR
)
4065 return do_rmdir(dfd
, pathname
);
4067 return do_unlinkat(dfd
, pathname
);
4070 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
4072 return do_unlinkat(AT_FDCWD
, pathname
);
4075 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
4077 int error
= may_create(dir
, dentry
);
4082 if (!dir
->i_op
->symlink
)
4085 error
= security_inode_symlink(dir
, dentry
, oldname
);
4089 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
4091 fsnotify_create(dir
, dentry
);
4094 EXPORT_SYMBOL(vfs_symlink
);
4096 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
4097 int, newdfd
, const char __user
*, newname
)
4100 struct filename
*from
;
4101 struct dentry
*dentry
;
4103 unsigned int lookup_flags
= 0;
4105 from
= getname(oldname
);
4107 return PTR_ERR(from
);
4109 dentry
= user_path_create(newdfd
, newname
, &path
, lookup_flags
);
4110 error
= PTR_ERR(dentry
);
4114 error
= security_path_symlink(&path
, dentry
, from
->name
);
4116 error
= vfs_symlink(path
.dentry
->d_inode
, dentry
, from
->name
);
4117 done_path_create(&path
, dentry
);
4118 if (retry_estale(error
, lookup_flags
)) {
4119 lookup_flags
|= LOOKUP_REVAL
;
4127 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
4129 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
4133 * vfs_link - create a new link
4134 * @old_dentry: object to be linked
4136 * @new_dentry: where to create the new link
4137 * @delegated_inode: returns inode needing a delegation break
4139 * The caller must hold dir->i_mutex
4141 * If vfs_link discovers a delegation on the to-be-linked file in need
4142 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4143 * inode in delegated_inode. The caller should then break the delegation
4144 * and retry. Because breaking a delegation may take a long time, the
4145 * caller should drop the i_mutex before doing so.
4147 * Alternatively, a caller may pass NULL for delegated_inode. This may
4148 * be appropriate for callers that expect the underlying filesystem not
4149 * to be NFS exported.
4151 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
, struct inode
**delegated_inode
)
4153 struct inode
*inode
= old_dentry
->d_inode
;
4154 unsigned max_links
= dir
->i_sb
->s_max_links
;
4160 error
= may_create(dir
, new_dentry
);
4164 if (dir
->i_sb
!= inode
->i_sb
)
4168 * A link to an append-only or immutable file cannot be created.
4170 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
4173 * Updating the link count will likely cause i_uid and i_gid to
4174 * be writen back improperly if their true value is unknown to
4177 if (HAS_UNMAPPED_ID(inode
))
4179 if (!dir
->i_op
->link
)
4181 if (S_ISDIR(inode
->i_mode
))
4184 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
4189 /* Make sure we don't allow creating hardlink to an unlinked file */
4190 if (inode
->i_nlink
== 0 && !(inode
->i_state
& I_LINKABLE
))
4192 else if (max_links
&& inode
->i_nlink
>= max_links
)
4195 error
= try_break_deleg(inode
, delegated_inode
);
4197 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
4200 if (!error
&& (inode
->i_state
& I_LINKABLE
)) {
4201 spin_lock(&inode
->i_lock
);
4202 inode
->i_state
&= ~I_LINKABLE
;
4203 spin_unlock(&inode
->i_lock
);
4205 inode_unlock(inode
);
4207 fsnotify_link(dir
, inode
, new_dentry
);
4210 EXPORT_SYMBOL(vfs_link
);
4213 * Hardlinks are often used in delicate situations. We avoid
4214 * security-related surprises by not following symlinks on the
4217 * We don't follow them on the oldname either to be compatible
4218 * with linux 2.0, and to avoid hard-linking to directories
4219 * and other special files. --ADM
4221 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
4222 int, newdfd
, const char __user
*, newname
, int, flags
)
4224 struct dentry
*new_dentry
;
4225 struct path old_path
, new_path
;
4226 struct inode
*delegated_inode
= NULL
;
4230 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
4233 * To use null names we require CAP_DAC_READ_SEARCH
4234 * This ensures that not everyone will be able to create
4235 * handlink using the passed filedescriptor.
4237 if (flags
& AT_EMPTY_PATH
) {
4238 if (!capable(CAP_DAC_READ_SEARCH
))
4243 if (flags
& AT_SYMLINK_FOLLOW
)
4244 how
|= LOOKUP_FOLLOW
;
4246 error
= user_path_at(olddfd
, oldname
, how
, &old_path
);
4250 new_dentry
= user_path_create(newdfd
, newname
, &new_path
,
4251 (how
& LOOKUP_REVAL
));
4252 error
= PTR_ERR(new_dentry
);
4253 if (IS_ERR(new_dentry
))
4257 if (old_path
.mnt
!= new_path
.mnt
)
4259 error
= may_linkat(&old_path
);
4260 if (unlikely(error
))
4262 error
= security_path_link(old_path
.dentry
, &new_path
, new_dentry
);
4265 error
= vfs_link(old_path
.dentry
, new_path
.dentry
->d_inode
, new_dentry
, &delegated_inode
);
4267 done_path_create(&new_path
, new_dentry
);
4268 if (delegated_inode
) {
4269 error
= break_deleg_wait(&delegated_inode
);
4271 path_put(&old_path
);
4275 if (retry_estale(error
, how
)) {
4276 path_put(&old_path
);
4277 how
|= LOOKUP_REVAL
;
4281 path_put(&old_path
);
4286 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
4288 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
4292 * vfs_rename - rename a filesystem object
4293 * @old_dir: parent of source
4294 * @old_dentry: source
4295 * @new_dir: parent of destination
4296 * @new_dentry: destination
4297 * @delegated_inode: returns an inode needing a delegation break
4298 * @flags: rename flags
4300 * The caller must hold multiple mutexes--see lock_rename()).
4302 * If vfs_rename discovers a delegation in need of breaking at either
4303 * the source or destination, it will return -EWOULDBLOCK and return a
4304 * reference to the inode in delegated_inode. The caller should then
4305 * break the delegation and retry. Because breaking a delegation may
4306 * take a long time, the caller should drop all locks before doing
4309 * Alternatively, a caller may pass NULL for delegated_inode. This may
4310 * be appropriate for callers that expect the underlying filesystem not
4311 * to be NFS exported.
4313 * The worst of all namespace operations - renaming directory. "Perverted"
4314 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4316 * a) we can get into loop creation.
4317 * b) race potential - two innocent renames can create a loop together.
4318 * That's where 4.4 screws up. Current fix: serialization on
4319 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4321 * c) we have to lock _four_ objects - parents and victim (if it exists),
4322 * and source (if it is not a directory).
4323 * And that - after we got ->i_mutex on parents (until then we don't know
4324 * whether the target exists). Solution: try to be smart with locking
4325 * order for inodes. We rely on the fact that tree topology may change
4326 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4327 * move will be locked. Thus we can rank directories by the tree
4328 * (ancestors first) and rank all non-directories after them.
4329 * That works since everybody except rename does "lock parent, lookup,
4330 * lock child" and rename is under ->s_vfs_rename_mutex.
4331 * HOWEVER, it relies on the assumption that any object with ->lookup()
4332 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4333 * we'd better make sure that there's no link(2) for them.
4334 * d) conversion from fhandle to dentry may come in the wrong moment - when
4335 * we are removing the target. Solution: we will have to grab ->i_mutex
4336 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4337 * ->i_mutex on parents, which works but leads to some truly excessive
4340 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
4341 struct inode
*new_dir
, struct dentry
*new_dentry
,
4342 struct inode
**delegated_inode
, unsigned int flags
)
4345 bool is_dir
= d_is_dir(old_dentry
);
4346 struct inode
*source
= old_dentry
->d_inode
;
4347 struct inode
*target
= new_dentry
->d_inode
;
4348 bool new_is_dir
= false;
4349 unsigned max_links
= new_dir
->i_sb
->s_max_links
;
4350 struct name_snapshot old_name
;
4353 * Check source == target.
4354 * On overlayfs need to look at underlying inodes.
4356 if (d_real_inode(old_dentry
) == d_real_inode(new_dentry
))
4359 error
= may_delete(old_dir
, old_dentry
, is_dir
);
4364 error
= may_create(new_dir
, new_dentry
);
4366 new_is_dir
= d_is_dir(new_dentry
);
4368 if (!(flags
& RENAME_EXCHANGE
))
4369 error
= may_delete(new_dir
, new_dentry
, is_dir
);
4371 error
= may_delete(new_dir
, new_dentry
, new_is_dir
);
4376 if (!old_dir
->i_op
->rename
)
4380 * If we are going to change the parent - check write permissions,
4381 * we'll need to flip '..'.
4383 if (new_dir
!= old_dir
) {
4385 error
= inode_permission(source
, MAY_WRITE
);
4389 if ((flags
& RENAME_EXCHANGE
) && new_is_dir
) {
4390 error
= inode_permission(target
, MAY_WRITE
);
4396 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
4401 take_dentry_name_snapshot(&old_name
, old_dentry
);
4403 if (!is_dir
|| (flags
& RENAME_EXCHANGE
))
4404 lock_two_nondirectories(source
, target
);
4409 if (is_local_mountpoint(old_dentry
) || is_local_mountpoint(new_dentry
))
4412 if (max_links
&& new_dir
!= old_dir
) {
4414 if (is_dir
&& !new_is_dir
&& new_dir
->i_nlink
>= max_links
)
4416 if ((flags
& RENAME_EXCHANGE
) && !is_dir
&& new_is_dir
&&
4417 old_dir
->i_nlink
>= max_links
)
4420 if (is_dir
&& !(flags
& RENAME_EXCHANGE
) && target
)
4421 shrink_dcache_parent(new_dentry
);
4423 error
= try_break_deleg(source
, delegated_inode
);
4427 if (target
&& !new_is_dir
) {
4428 error
= try_break_deleg(target
, delegated_inode
);
4432 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
,
4433 new_dir
, new_dentry
, flags
);
4437 if (!(flags
& RENAME_EXCHANGE
) && target
) {
4439 target
->i_flags
|= S_DEAD
;
4440 dont_mount(new_dentry
);
4441 detach_mounts(new_dentry
);
4443 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
)) {
4444 if (!(flags
& RENAME_EXCHANGE
))
4445 d_move(old_dentry
, new_dentry
);
4447 d_exchange(old_dentry
, new_dentry
);
4450 if (!is_dir
|| (flags
& RENAME_EXCHANGE
))
4451 unlock_two_nondirectories(source
, target
);
4453 inode_unlock(target
);
4456 fsnotify_move(old_dir
, new_dir
, old_name
.name
, is_dir
,
4457 !(flags
& RENAME_EXCHANGE
) ? target
: NULL
, old_dentry
);
4458 if (flags
& RENAME_EXCHANGE
) {
4459 fsnotify_move(new_dir
, old_dir
, old_dentry
->d_name
.name
,
4460 new_is_dir
, NULL
, new_dentry
);
4463 release_dentry_name_snapshot(&old_name
);
4467 EXPORT_SYMBOL(vfs_rename
);
4469 SYSCALL_DEFINE5(renameat2
, int, olddfd
, const char __user
*, oldname
,
4470 int, newdfd
, const char __user
*, newname
, unsigned int, flags
)
4472 struct dentry
*old_dentry
, *new_dentry
;
4473 struct dentry
*trap
;
4474 struct path old_path
, new_path
;
4475 struct qstr old_last
, new_last
;
4476 int old_type
, new_type
;
4477 struct inode
*delegated_inode
= NULL
;
4478 struct filename
*from
;
4479 struct filename
*to
;
4480 unsigned int lookup_flags
= 0, target_flags
= LOOKUP_RENAME_TARGET
;
4481 bool should_retry
= false;
4484 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
4487 if ((flags
& (RENAME_NOREPLACE
| RENAME_WHITEOUT
)) &&
4488 (flags
& RENAME_EXCHANGE
))
4491 if ((flags
& RENAME_WHITEOUT
) && !capable(CAP_MKNOD
))
4494 if (flags
& RENAME_EXCHANGE
)
4498 from
= user_path_parent(olddfd
, oldname
,
4499 &old_path
, &old_last
, &old_type
, lookup_flags
);
4501 error
= PTR_ERR(from
);
4505 to
= user_path_parent(newdfd
, newname
,
4506 &new_path
, &new_last
, &new_type
, lookup_flags
);
4508 error
= PTR_ERR(to
);
4513 if (old_path
.mnt
!= new_path
.mnt
)
4517 if (old_type
!= LAST_NORM
)
4520 if (flags
& RENAME_NOREPLACE
)
4522 if (new_type
!= LAST_NORM
)
4525 error
= mnt_want_write(old_path
.mnt
);
4530 trap
= lock_rename(new_path
.dentry
, old_path
.dentry
);
4532 old_dentry
= __lookup_hash(&old_last
, old_path
.dentry
, lookup_flags
);
4533 error
= PTR_ERR(old_dentry
);
4534 if (IS_ERR(old_dentry
))
4536 /* source must exist */
4538 if (d_is_negative(old_dentry
))
4540 new_dentry
= __lookup_hash(&new_last
, new_path
.dentry
, lookup_flags
| target_flags
);
4541 error
= PTR_ERR(new_dentry
);
4542 if (IS_ERR(new_dentry
))
4545 if ((flags
& RENAME_NOREPLACE
) && d_is_positive(new_dentry
))
4547 if (flags
& RENAME_EXCHANGE
) {
4549 if (d_is_negative(new_dentry
))
4552 if (!d_is_dir(new_dentry
)) {
4554 if (new_last
.name
[new_last
.len
])
4558 /* unless the source is a directory trailing slashes give -ENOTDIR */
4559 if (!d_is_dir(old_dentry
)) {
4561 if (old_last
.name
[old_last
.len
])
4563 if (!(flags
& RENAME_EXCHANGE
) && new_last
.name
[new_last
.len
])
4566 /* source should not be ancestor of target */
4568 if (old_dentry
== trap
)
4570 /* target should not be an ancestor of source */
4571 if (!(flags
& RENAME_EXCHANGE
))
4573 if (new_dentry
== trap
)
4576 error
= security_path_rename(&old_path
, old_dentry
,
4577 &new_path
, new_dentry
, flags
);
4580 error
= vfs_rename(old_path
.dentry
->d_inode
, old_dentry
,
4581 new_path
.dentry
->d_inode
, new_dentry
,
4582 &delegated_inode
, flags
);
4588 unlock_rename(new_path
.dentry
, old_path
.dentry
);
4589 if (delegated_inode
) {
4590 error
= break_deleg_wait(&delegated_inode
);
4594 mnt_drop_write(old_path
.mnt
);
4596 if (retry_estale(error
, lookup_flags
))
4597 should_retry
= true;
4598 path_put(&new_path
);
4601 path_put(&old_path
);
4604 should_retry
= false;
4605 lookup_flags
|= LOOKUP_REVAL
;
4612 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
4613 int, newdfd
, const char __user
*, newname
)
4615 return sys_renameat2(olddfd
, oldname
, newdfd
, newname
, 0);
4618 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
4620 return sys_renameat2(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
4623 int vfs_whiteout(struct inode
*dir
, struct dentry
*dentry
)
4625 int error
= may_create(dir
, dentry
);
4629 if (!dir
->i_op
->mknod
)
4632 return dir
->i_op
->mknod(dir
, dentry
,
4633 S_IFCHR
| WHITEOUT_MODE
, WHITEOUT_DEV
);
4635 EXPORT_SYMBOL(vfs_whiteout
);
4637 int readlink_copy(char __user
*buffer
, int buflen
, const char *link
)
4639 int len
= PTR_ERR(link
);
4644 if (len
> (unsigned) buflen
)
4646 if (copy_to_user(buffer
, link
, len
))
4653 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4654 * have ->get_link() not calling nd_jump_link(). Using (or not using) it
4655 * for any given inode is up to filesystem.
4657 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4659 DEFINE_DELAYED_CALL(done
);
4660 struct inode
*inode
= d_inode(dentry
);
4661 const char *link
= inode
->i_link
;
4665 link
= inode
->i_op
->get_link(dentry
, inode
, &done
);
4667 return PTR_ERR(link
);
4669 res
= readlink_copy(buffer
, buflen
, link
);
4670 do_delayed_call(&done
);
4673 EXPORT_SYMBOL(generic_readlink
);
4676 * vfs_get_link - get symlink body
4677 * @dentry: dentry on which to get symbolic link
4678 * @done: caller needs to free returned data with this
4680 * Calls security hook and i_op->get_link() on the supplied inode.
4682 * It does not touch atime. That's up to the caller if necessary.
4684 * Does not work on "special" symlinks like /proc/$$/fd/N
4686 const char *vfs_get_link(struct dentry
*dentry
, struct delayed_call
*done
)
4688 const char *res
= ERR_PTR(-EINVAL
);
4689 struct inode
*inode
= d_inode(dentry
);
4691 if (d_is_symlink(dentry
)) {
4692 res
= ERR_PTR(security_inode_readlink(dentry
));
4694 res
= inode
->i_op
->get_link(dentry
, inode
, done
);
4698 EXPORT_SYMBOL(vfs_get_link
);
4700 /* get the link contents into pagecache */
4701 const char *page_get_link(struct dentry
*dentry
, struct inode
*inode
,
4702 struct delayed_call
*callback
)
4706 struct address_space
*mapping
= inode
->i_mapping
;
4709 page
= find_get_page(mapping
, 0);
4711 return ERR_PTR(-ECHILD
);
4712 if (!PageUptodate(page
)) {
4714 return ERR_PTR(-ECHILD
);
4717 page
= read_mapping_page(mapping
, 0, NULL
);
4721 set_delayed_call(callback
, page_put_link
, page
);
4722 BUG_ON(mapping_gfp_mask(mapping
) & __GFP_HIGHMEM
);
4723 kaddr
= page_address(page
);
4724 nd_terminate_link(kaddr
, inode
->i_size
, PAGE_SIZE
- 1);
4728 EXPORT_SYMBOL(page_get_link
);
4730 void page_put_link(void *arg
)
4734 EXPORT_SYMBOL(page_put_link
);
4736 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4738 DEFINE_DELAYED_CALL(done
);
4739 int res
= readlink_copy(buffer
, buflen
,
4740 page_get_link(dentry
, d_inode(dentry
),
4742 do_delayed_call(&done
);
4745 EXPORT_SYMBOL(page_readlink
);
4748 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4750 int __page_symlink(struct inode
*inode
, const char *symname
, int len
, int nofs
)
4752 struct address_space
*mapping
= inode
->i_mapping
;
4756 unsigned int flags
= AOP_FLAG_UNINTERRUPTIBLE
;
4758 flags
|= AOP_FLAG_NOFS
;
4761 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
4762 flags
, &page
, &fsdata
);
4766 memcpy(page_address(page
), symname
, len
-1);
4768 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
4775 mark_inode_dirty(inode
);
4780 EXPORT_SYMBOL(__page_symlink
);
4782 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
4784 return __page_symlink(inode
, symname
, len
,
4785 !mapping_gfp_constraint(inode
->i_mapping
, __GFP_FS
));
4787 EXPORT_SYMBOL(page_symlink
);
4789 const struct inode_operations page_symlink_inode_operations
= {
4790 .readlink
= generic_readlink
,
4791 .get_link
= page_get_link
,
4793 EXPORT_SYMBOL(page_symlink_inode_operations
);