4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Some corrections by tytso.
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
22 #include <linux/namei.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/ima.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/device_cgroup.h>
35 #include <linux/fs_struct.h>
36 #include <linux/posix_acl.h>
37 #include <linux/hash.h>
38 #include <asm/uaccess.h>
43 /* [Feb-1997 T. Schoebel-Theuer]
44 * Fundamental changes in the pathname lookup mechanisms (namei)
45 * were necessary because of omirr. The reason is that omirr needs
46 * to know the _real_ pathname, not the user-supplied one, in case
47 * of symlinks (and also when transname replacements occur).
49 * The new code replaces the old recursive symlink resolution with
50 * an iterative one (in case of non-nested symlink chains). It does
51 * this with calls to <fs>_follow_link().
52 * As a side effect, dir_namei(), _namei() and follow_link() are now
53 * replaced with a single function lookup_dentry() that can handle all
54 * the special cases of the former code.
56 * With the new dcache, the pathname is stored at each inode, at least as
57 * long as the refcount of the inode is positive. As a side effect, the
58 * size of the dcache depends on the inode cache and thus is dynamic.
60 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
61 * resolution to correspond with current state of the code.
63 * Note that the symlink resolution is not *completely* iterative.
64 * There is still a significant amount of tail- and mid- recursion in
65 * the algorithm. Also, note that <fs>_readlink() is not used in
66 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
67 * may return different results than <fs>_follow_link(). Many virtual
68 * filesystems (including /proc) exhibit this behavior.
71 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
72 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
73 * and the name already exists in form of a symlink, try to create the new
74 * name indicated by the symlink. The old code always complained that the
75 * name already exists, due to not following the symlink even if its target
76 * is nonexistent. The new semantics affects also mknod() and link() when
77 * the name is a symlink pointing to a non-existent name.
79 * I don't know which semantics is the right one, since I have no access
80 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
81 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
82 * "old" one. Personally, I think the new semantics is much more logical.
83 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
84 * file does succeed in both HP-UX and SunOs, but not in Solaris
85 * and in the old Linux semantics.
88 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
89 * semantics. See the comments in "open_namei" and "do_link" below.
91 * [10-Sep-98 Alan Modra] Another symlink change.
94 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
95 * inside the path - always follow.
96 * in the last component in creation/removal/renaming - never follow.
97 * if LOOKUP_FOLLOW passed - follow.
98 * if the pathname has trailing slashes - follow.
99 * otherwise - don't follow.
100 * (applied in that order).
102 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
103 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
104 * During the 2.4 we need to fix the userland stuff depending on it -
105 * hopefully we will be able to get rid of that wart in 2.5. So far only
106 * XEmacs seems to be relying on it...
109 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
110 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
111 * any extra contention...
114 /* In order to reduce some races, while at the same time doing additional
115 * checking and hopefully speeding things up, we copy filenames to the
116 * kernel data space before using them..
118 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
119 * PATH_MAX includes the nul terminator --RR.
122 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
125 getname_flags(const char __user
*filename
, int flags
, int *empty
)
127 struct filename
*result
;
131 result
= audit_reusename(filename
);
135 result
= __getname();
136 if (unlikely(!result
))
137 return ERR_PTR(-ENOMEM
);
140 * First, try to embed the struct filename inside the names_cache
143 kname
= (char *)result
->iname
;
144 result
->name
= kname
;
146 len
= strncpy_from_user(kname
, filename
, EMBEDDED_NAME_MAX
);
147 if (unlikely(len
< 0)) {
153 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
154 * separate struct filename so we can dedicate the entire
155 * names_cache allocation for the pathname, and re-do the copy from
158 if (unlikely(len
== EMBEDDED_NAME_MAX
)) {
159 const size_t size
= offsetof(struct filename
, iname
[1]);
160 kname
= (char *)result
;
163 * size is chosen that way we to guarantee that
164 * result->iname[0] is within the same object and that
165 * kname can't be equal to result->iname, no matter what.
167 result
= kzalloc(size
, GFP_KERNEL
);
168 if (unlikely(!result
)) {
170 return ERR_PTR(-ENOMEM
);
172 result
->name
= kname
;
173 len
= strncpy_from_user(kname
, filename
, PATH_MAX
);
174 if (unlikely(len
< 0)) {
179 if (unlikely(len
== PATH_MAX
)) {
182 return ERR_PTR(-ENAMETOOLONG
);
187 /* The empty path is special. */
188 if (unlikely(!len
)) {
191 if (!(flags
& LOOKUP_EMPTY
)) {
193 return ERR_PTR(-ENOENT
);
197 result
->uptr
= filename
;
198 result
->aname
= NULL
;
199 audit_getname(result
);
204 getname(const char __user
* filename
)
206 return getname_flags(filename
, 0, NULL
);
210 getname_kernel(const char * filename
)
212 struct filename
*result
;
213 int len
= strlen(filename
) + 1;
215 result
= __getname();
216 if (unlikely(!result
))
217 return ERR_PTR(-ENOMEM
);
219 if (len
<= EMBEDDED_NAME_MAX
) {
220 result
->name
= (char *)result
->iname
;
221 } else if (len
<= PATH_MAX
) {
222 const size_t size
= offsetof(struct filename
, iname
[1]);
223 struct filename
*tmp
;
225 tmp
= kmalloc(size
, GFP_KERNEL
);
226 if (unlikely(!tmp
)) {
228 return ERR_PTR(-ENOMEM
);
230 tmp
->name
= (char *)result
;
234 return ERR_PTR(-ENAMETOOLONG
);
236 memcpy((char *)result
->name
, filename
, len
);
238 result
->aname
= NULL
;
240 audit_getname(result
);
245 void putname(struct filename
*name
)
247 BUG_ON(name
->refcnt
<= 0);
249 if (--name
->refcnt
> 0)
252 if (name
->name
!= name
->iname
) {
253 __putname(name
->name
);
259 static int check_acl(struct inode
*inode
, int mask
)
261 #ifdef CONFIG_FS_POSIX_ACL
262 struct posix_acl
*acl
;
264 if (mask
& MAY_NOT_BLOCK
) {
265 acl
= get_cached_acl_rcu(inode
, ACL_TYPE_ACCESS
);
268 /* no ->get_acl() calls in RCU mode... */
269 if (acl
== ACL_NOT_CACHED
)
271 return posix_acl_permission(inode
, acl
, mask
& ~MAY_NOT_BLOCK
);
274 acl
= get_acl(inode
, ACL_TYPE_ACCESS
);
278 int error
= posix_acl_permission(inode
, acl
, mask
);
279 posix_acl_release(acl
);
288 * This does the basic permission checking
290 static int acl_permission_check(struct inode
*inode
, int mask
)
292 unsigned int mode
= inode
->i_mode
;
294 if (likely(uid_eq(current_fsuid(), inode
->i_uid
)))
297 if (IS_POSIXACL(inode
) && (mode
& S_IRWXG
)) {
298 int error
= check_acl(inode
, mask
);
299 if (error
!= -EAGAIN
)
303 if (in_group_p(inode
->i_gid
))
308 * If the DACs are ok we don't need any capability check.
310 if ((mask
& ~mode
& (MAY_READ
| MAY_WRITE
| MAY_EXEC
)) == 0)
316 * generic_permission - check for access rights on a Posix-like filesystem
317 * @inode: inode to check access rights for
318 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
320 * Used to check for read/write/execute permissions on a file.
321 * We use "fsuid" for this, letting us set arbitrary permissions
322 * for filesystem access without changing the "normal" uids which
323 * are used for other things.
325 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
326 * request cannot be satisfied (eg. requires blocking or too much complexity).
327 * It would then be called again in ref-walk mode.
329 int generic_permission(struct inode
*inode
, int mask
)
334 * Do the basic permission checks.
336 ret
= acl_permission_check(inode
, mask
);
340 if (S_ISDIR(inode
->i_mode
)) {
341 /* DACs are overridable for directories */
342 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
344 if (!(mask
& MAY_WRITE
))
345 if (capable_wrt_inode_uidgid(inode
,
346 CAP_DAC_READ_SEARCH
))
351 * Read/write DACs are always overridable.
352 * Executable DACs are overridable when there is
353 * at least one exec bit set.
355 if (!(mask
& MAY_EXEC
) || (inode
->i_mode
& S_IXUGO
))
356 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_OVERRIDE
))
360 * Searching includes executable on directories, else just read.
362 mask
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
363 if (mask
== MAY_READ
)
364 if (capable_wrt_inode_uidgid(inode
, CAP_DAC_READ_SEARCH
))
369 EXPORT_SYMBOL(generic_permission
);
372 * We _really_ want to just do "generic_permission()" without
373 * even looking at the inode->i_op values. So we keep a cache
374 * flag in inode->i_opflags, that says "this has not special
375 * permission function, use the fast case".
377 static inline int do_inode_permission(struct inode
*inode
, int mask
)
379 if (unlikely(!(inode
->i_opflags
& IOP_FASTPERM
))) {
380 if (likely(inode
->i_op
->permission
))
381 return inode
->i_op
->permission(inode
, mask
);
383 /* This gets set once for the inode lifetime */
384 spin_lock(&inode
->i_lock
);
385 inode
->i_opflags
|= IOP_FASTPERM
;
386 spin_unlock(&inode
->i_lock
);
388 return generic_permission(inode
, mask
);
392 * __inode_permission - Check for access rights to a given inode
393 * @inode: Inode to check permission on
394 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
396 * Check for read/write/execute permissions on an inode.
398 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
400 * This does not check for a read-only file system. You probably want
401 * inode_permission().
403 int __inode_permission(struct inode
*inode
, int mask
)
407 if (unlikely(mask
& MAY_WRITE
)) {
409 * Nobody gets write access to an immutable file.
411 if (IS_IMMUTABLE(inode
))
415 retval
= do_inode_permission(inode
, mask
);
419 retval
= devcgroup_inode_permission(inode
, mask
);
423 return security_inode_permission(inode
, mask
);
425 EXPORT_SYMBOL(__inode_permission
);
428 * sb_permission - Check superblock-level permissions
429 * @sb: Superblock of inode to check permission on
430 * @inode: Inode to check permission on
431 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
433 * Separate out file-system wide checks from inode-specific permission checks.
435 static int sb_permission(struct super_block
*sb
, struct inode
*inode
, int mask
)
437 if (unlikely(mask
& MAY_WRITE
)) {
438 umode_t mode
= inode
->i_mode
;
440 /* Nobody gets write access to a read-only fs. */
441 if ((sb
->s_flags
& MS_RDONLY
) &&
442 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
449 * inode_permission - Check for access rights to a given inode
450 * @inode: Inode to check permission on
451 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
453 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
454 * this, letting us set arbitrary permissions for filesystem access without
455 * changing the "normal" UIDs which are used for other things.
457 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
459 int inode_permission(struct inode
*inode
, int mask
)
463 retval
= sb_permission(inode
->i_sb
, inode
, mask
);
466 return __inode_permission(inode
, mask
);
468 EXPORT_SYMBOL(inode_permission
);
471 * path_get - get a reference to a path
472 * @path: path to get the reference to
474 * Given a path increment the reference count to the dentry and the vfsmount.
476 void path_get(const struct path
*path
)
481 EXPORT_SYMBOL(path_get
);
484 * path_put - put a reference to a path
485 * @path: path to put the reference to
487 * Given a path decrement the reference count to the dentry and the vfsmount.
489 void path_put(const struct path
*path
)
494 EXPORT_SYMBOL(path_put
);
496 #define EMBEDDED_LEVELS 2
501 struct inode
*inode
; /* path.dentry.d_inode */
506 int total_link_count
;
513 } *stack
, internal
[EMBEDDED_LEVELS
];
514 struct filename
*name
;
515 struct nameidata
*saved
;
520 static void set_nameidata(struct nameidata
*p
, int dfd
, struct filename
*name
)
522 struct nameidata
*old
= current
->nameidata
;
523 p
->stack
= p
->internal
;
526 p
->total_link_count
= old
? old
->total_link_count
: 0;
528 current
->nameidata
= p
;
531 static void restore_nameidata(void)
533 struct nameidata
*now
= current
->nameidata
, *old
= now
->saved
;
535 current
->nameidata
= old
;
537 old
->total_link_count
= now
->total_link_count
;
538 if (now
->stack
!= now
->internal
) {
540 now
->stack
= now
->internal
;
544 static int __nd_alloc_stack(struct nameidata
*nd
)
548 if (nd
->flags
& LOOKUP_RCU
) {
549 p
= kmalloc(MAXSYMLINKS
* sizeof(struct saved
),
554 p
= kmalloc(MAXSYMLINKS
* sizeof(struct saved
),
559 memcpy(p
, nd
->internal
, sizeof(nd
->internal
));
565 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
566 * @path: nameidate to verify
568 * Rename can sometimes move a file or directory outside of a bind
569 * mount, path_connected allows those cases to be detected.
571 static bool path_connected(const struct path
*path
)
573 struct vfsmount
*mnt
= path
->mnt
;
574 struct super_block
*sb
= mnt
->mnt_sb
;
576 /* Bind mounts and multi-root filesystems can have disconnected paths */
577 if (!(sb
->s_iflags
& SB_I_MULTIROOT
) && (mnt
->mnt_root
== sb
->s_root
))
580 return is_subdir(path
->dentry
, mnt
->mnt_root
);
583 static inline int nd_alloc_stack(struct nameidata
*nd
)
585 if (likely(nd
->depth
!= EMBEDDED_LEVELS
))
587 if (likely(nd
->stack
!= nd
->internal
))
589 return __nd_alloc_stack(nd
);
592 static void drop_links(struct nameidata
*nd
)
596 struct saved
*last
= nd
->stack
+ i
;
597 struct inode
*inode
= last
->inode
;
598 if (last
->cookie
&& inode
->i_op
->put_link
) {
599 inode
->i_op
->put_link(inode
, last
->cookie
);
605 static void terminate_walk(struct nameidata
*nd
)
608 if (!(nd
->flags
& LOOKUP_RCU
)) {
611 for (i
= 0; i
< nd
->depth
; i
++)
612 path_put(&nd
->stack
[i
].link
);
613 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
618 nd
->flags
&= ~LOOKUP_RCU
;
619 if (!(nd
->flags
& LOOKUP_ROOT
))
626 /* path_put is needed afterwards regardless of success or failure */
627 static bool legitimize_path(struct nameidata
*nd
,
628 struct path
*path
, unsigned seq
)
630 int res
= __legitimize_mnt(path
->mnt
, nd
->m_seq
);
637 if (unlikely(!lockref_get_not_dead(&path
->dentry
->d_lockref
))) {
641 return !read_seqcount_retry(&path
->dentry
->d_seq
, seq
);
644 static bool legitimize_links(struct nameidata
*nd
)
647 for (i
= 0; i
< nd
->depth
; i
++) {
648 struct saved
*last
= nd
->stack
+ i
;
649 if (unlikely(!legitimize_path(nd
, &last
->link
, last
->seq
))) {
659 * Path walking has 2 modes, rcu-walk and ref-walk (see
660 * Documentation/filesystems/path-lookup.txt). In situations when we can't
661 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
662 * normal reference counts on dentries and vfsmounts to transition to rcu-walk
663 * mode. Refcounts are grabbed at the last known good point before rcu-walk
664 * got stuck, so ref-walk may continue from there. If this is not successful
665 * (eg. a seqcount has changed), then failure is returned and it's up to caller
666 * to restart the path walk from the beginning in ref-walk mode.
670 * unlazy_walk - try to switch to ref-walk mode.
671 * @nd: nameidata pathwalk data
672 * @dentry: child of nd->path.dentry or NULL
673 * @seq: seq number to check dentry against
674 * Returns: 0 on success, -ECHILD on failure
676 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
677 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
678 * @nd or NULL. Must be called from rcu-walk context.
679 * Nothing should touch nameidata between unlazy_walk() failure and
682 static int unlazy_walk(struct nameidata
*nd
, struct dentry
*dentry
, unsigned seq
)
684 struct dentry
*parent
= nd
->path
.dentry
;
686 BUG_ON(!(nd
->flags
& LOOKUP_RCU
));
688 nd
->flags
&= ~LOOKUP_RCU
;
689 if (unlikely(!legitimize_links(nd
)))
691 if (unlikely(!legitimize_mnt(nd
->path
.mnt
, nd
->m_seq
)))
693 if (unlikely(!lockref_get_not_dead(&parent
->d_lockref
)))
697 * For a negative lookup, the lookup sequence point is the parents
698 * sequence point, and it only needs to revalidate the parent dentry.
700 * For a positive lookup, we need to move both the parent and the
701 * dentry from the RCU domain to be properly refcounted. And the
702 * sequence number in the dentry validates *both* dentry counters,
703 * since we checked the sequence number of the parent after we got
704 * the child sequence number. So we know the parent must still
705 * be valid if the child sequence number is still valid.
708 if (read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
710 BUG_ON(nd
->inode
!= parent
->d_inode
);
712 if (!lockref_get_not_dead(&dentry
->d_lockref
))
714 if (read_seqcount_retry(&dentry
->d_seq
, seq
))
719 * Sequence counts matched. Now make sure that the root is
720 * still valid and get it if required.
722 if (nd
->root
.mnt
&& !(nd
->flags
& LOOKUP_ROOT
)) {
723 if (unlikely(!legitimize_path(nd
, &nd
->root
, nd
->root_seq
))) {
740 nd
->path
.dentry
= NULL
;
744 if (!(nd
->flags
& LOOKUP_ROOT
))
749 static int unlazy_link(struct nameidata
*nd
, struct path
*link
, unsigned seq
)
751 if (unlikely(!legitimize_path(nd
, link
, seq
))) {
754 nd
->flags
&= ~LOOKUP_RCU
;
756 nd
->path
.dentry
= NULL
;
757 if (!(nd
->flags
& LOOKUP_ROOT
))
760 } else if (likely(unlazy_walk(nd
, NULL
, 0)) == 0) {
767 static inline int d_revalidate(struct dentry
*dentry
, unsigned int flags
)
769 return dentry
->d_op
->d_revalidate(dentry
, flags
);
773 * complete_walk - successful completion of path walk
774 * @nd: pointer nameidata
776 * If we had been in RCU mode, drop out of it and legitimize nd->path.
777 * Revalidate the final result, unless we'd already done that during
778 * the path walk or the filesystem doesn't ask for it. Return 0 on
779 * success, -error on failure. In case of failure caller does not
780 * need to drop nd->path.
782 static int complete_walk(struct nameidata
*nd
)
784 struct dentry
*dentry
= nd
->path
.dentry
;
787 if (nd
->flags
& LOOKUP_RCU
) {
788 if (!(nd
->flags
& LOOKUP_ROOT
))
790 if (unlikely(unlazy_walk(nd
, NULL
, 0)))
794 if (likely(!(nd
->flags
& LOOKUP_JUMPED
)))
797 if (likely(!(dentry
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
)))
800 status
= dentry
->d_op
->d_weak_revalidate(dentry
, nd
->flags
);
810 static void set_root(struct nameidata
*nd
)
812 get_fs_root(current
->fs
, &nd
->root
);
815 static void set_root_rcu(struct nameidata
*nd
)
817 struct fs_struct
*fs
= current
->fs
;
821 seq
= read_seqcount_begin(&fs
->seq
);
823 nd
->root_seq
= __read_seqcount_begin(&nd
->root
.dentry
->d_seq
);
824 } while (read_seqcount_retry(&fs
->seq
, seq
));
827 static void path_put_conditional(struct path
*path
, struct nameidata
*nd
)
830 if (path
->mnt
!= nd
->path
.mnt
)
834 static inline void path_to_nameidata(const struct path
*path
,
835 struct nameidata
*nd
)
837 if (!(nd
->flags
& LOOKUP_RCU
)) {
838 dput(nd
->path
.dentry
);
839 if (nd
->path
.mnt
!= path
->mnt
)
840 mntput(nd
->path
.mnt
);
842 nd
->path
.mnt
= path
->mnt
;
843 nd
->path
.dentry
= path
->dentry
;
847 * Helper to directly jump to a known parsed path from ->follow_link,
848 * caller must have taken a reference to path beforehand.
850 void nd_jump_link(struct path
*path
)
852 struct nameidata
*nd
= current
->nameidata
;
856 nd
->inode
= nd
->path
.dentry
->d_inode
;
857 nd
->flags
|= LOOKUP_JUMPED
;
860 static inline void put_link(struct nameidata
*nd
)
862 struct saved
*last
= nd
->stack
+ --nd
->depth
;
863 struct inode
*inode
= last
->inode
;
864 if (last
->cookie
&& inode
->i_op
->put_link
)
865 inode
->i_op
->put_link(inode
, last
->cookie
);
866 if (!(nd
->flags
& LOOKUP_RCU
))
867 path_put(&last
->link
);
870 int sysctl_protected_symlinks __read_mostly
= 0;
871 int sysctl_protected_hardlinks __read_mostly
= 0;
874 * may_follow_link - Check symlink following for unsafe situations
875 * @nd: nameidata pathwalk data
877 * In the case of the sysctl_protected_symlinks sysctl being enabled,
878 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
879 * in a sticky world-writable directory. This is to protect privileged
880 * processes from failing races against path names that may change out
881 * from under them by way of other users creating malicious symlinks.
882 * It will permit symlinks to be followed only when outside a sticky
883 * world-writable directory, or when the uid of the symlink and follower
884 * match, or when the directory owner matches the symlink's owner.
886 * Returns 0 if following the symlink is allowed, -ve on error.
888 static inline int may_follow_link(struct nameidata
*nd
)
890 const struct inode
*inode
;
891 const struct inode
*parent
;
894 if (!sysctl_protected_symlinks
)
897 /* Allowed if owner and follower match. */
898 inode
= nd
->stack
[0].inode
;
899 if (uid_eq(current_cred()->fsuid
, inode
->i_uid
))
902 /* Allowed if parent directory not sticky and world-writable. */
904 if ((parent
->i_mode
& (S_ISVTX
|S_IWOTH
)) != (S_ISVTX
|S_IWOTH
))
907 /* Allowed if parent directory and link owner match. */
908 puid
= parent
->i_uid
;
909 if (uid_valid(puid
) && uid_eq(puid
, inode
->i_uid
))
912 if (nd
->flags
& LOOKUP_RCU
)
915 audit_log_link_denied("follow_link", &nd
->stack
[0].link
);
920 * safe_hardlink_source - Check for safe hardlink conditions
921 * @inode: the source inode to hardlink from
923 * Return false if at least one of the following conditions:
924 * - inode is not a regular file
926 * - inode is setgid and group-exec
927 * - access failure for read and write
929 * Otherwise returns true.
931 static bool safe_hardlink_source(struct inode
*inode
)
933 umode_t mode
= inode
->i_mode
;
935 /* Special files should not get pinned to the filesystem. */
939 /* Setuid files should not get pinned to the filesystem. */
943 /* Executable setgid files should not get pinned to the filesystem. */
944 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
))
947 /* Hardlinking to unreadable or unwritable sources is dangerous. */
948 if (inode_permission(inode
, MAY_READ
| MAY_WRITE
))
955 * may_linkat - Check permissions for creating a hardlink
956 * @link: the source to hardlink from
958 * Block hardlink when all of:
959 * - sysctl_protected_hardlinks enabled
960 * - fsuid does not match inode
961 * - hardlink source is unsafe (see safe_hardlink_source() above)
962 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
964 * Returns 0 if successful, -ve on error.
966 static int may_linkat(struct path
*link
)
970 if (!sysctl_protected_hardlinks
)
973 inode
= link
->dentry
->d_inode
;
975 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
976 * otherwise, it must be a safe source.
978 if (inode_owner_or_capable(inode
) || safe_hardlink_source(inode
))
981 audit_log_link_denied("linkat", link
);
985 static __always_inline
986 const char *get_link(struct nameidata
*nd
)
988 struct saved
*last
= nd
->stack
+ nd
->depth
- 1;
989 struct dentry
*dentry
= last
->link
.dentry
;
990 struct inode
*inode
= last
->inode
;
994 if (!(nd
->flags
& LOOKUP_RCU
)) {
995 touch_atime(&last
->link
);
997 } else if (atime_needs_update(&last
->link
, inode
)) {
998 if (unlikely(unlazy_walk(nd
, NULL
, 0)))
999 return ERR_PTR(-ECHILD
);
1000 touch_atime(&last
->link
);
1003 error
= security_inode_follow_link(dentry
, inode
,
1004 nd
->flags
& LOOKUP_RCU
);
1005 if (unlikely(error
))
1006 return ERR_PTR(error
);
1008 nd
->last_type
= LAST_BIND
;
1009 res
= inode
->i_link
;
1011 if (nd
->flags
& LOOKUP_RCU
) {
1012 if (unlikely(unlazy_walk(nd
, NULL
, 0)))
1013 return ERR_PTR(-ECHILD
);
1015 res
= inode
->i_op
->follow_link(dentry
, &last
->cookie
);
1016 if (IS_ERR_OR_NULL(res
)) {
1017 last
->cookie
= NULL
;
1022 if (nd
->flags
& LOOKUP_RCU
) {
1026 nd
->path
= nd
->root
;
1027 d
= nd
->path
.dentry
;
1028 nd
->inode
= d
->d_inode
;
1029 nd
->seq
= nd
->root_seq
;
1030 if (unlikely(read_seqcount_retry(&d
->d_seq
, nd
->seq
)))
1031 return ERR_PTR(-ECHILD
);
1035 path_put(&nd
->path
);
1036 nd
->path
= nd
->root
;
1037 path_get(&nd
->root
);
1038 nd
->inode
= nd
->path
.dentry
->d_inode
;
1040 nd
->flags
|= LOOKUP_JUMPED
;
1041 while (unlikely(*++res
== '/'))
1050 * follow_up - Find the mountpoint of path's vfsmount
1052 * Given a path, find the mountpoint of its source file system.
1053 * Replace @path with the path of the mountpoint in the parent mount.
1056 * Return 1 if we went up a level and 0 if we were already at the
1059 int follow_up(struct path
*path
)
1061 struct mount
*mnt
= real_mount(path
->mnt
);
1062 struct mount
*parent
;
1063 struct dentry
*mountpoint
;
1065 read_seqlock_excl(&mount_lock
);
1066 parent
= mnt
->mnt_parent
;
1067 if (parent
== mnt
) {
1068 read_sequnlock_excl(&mount_lock
);
1071 mntget(&parent
->mnt
);
1072 mountpoint
= dget(mnt
->mnt_mountpoint
);
1073 read_sequnlock_excl(&mount_lock
);
1075 path
->dentry
= mountpoint
;
1077 path
->mnt
= &parent
->mnt
;
1080 EXPORT_SYMBOL(follow_up
);
1083 * Perform an automount
1084 * - return -EISDIR to tell follow_managed() to stop and return the path we
1087 static int follow_automount(struct path
*path
, struct nameidata
*nd
,
1090 struct vfsmount
*mnt
;
1093 if (!path
->dentry
->d_op
|| !path
->dentry
->d_op
->d_automount
)
1096 /* We don't want to mount if someone's just doing a stat -
1097 * unless they're stat'ing a directory and appended a '/' to
1100 * We do, however, want to mount if someone wants to open or
1101 * create a file of any type under the mountpoint, wants to
1102 * traverse through the mountpoint or wants to open the
1103 * mounted directory. Also, autofs may mark negative dentries
1104 * as being automount points. These will need the attentions
1105 * of the daemon to instantiate them before they can be used.
1107 if (!(nd
->flags
& (LOOKUP_PARENT
| LOOKUP_DIRECTORY
|
1108 LOOKUP_OPEN
| LOOKUP_CREATE
| LOOKUP_AUTOMOUNT
)) &&
1109 path
->dentry
->d_inode
)
1112 nd
->total_link_count
++;
1113 if (nd
->total_link_count
>= 40)
1116 mnt
= path
->dentry
->d_op
->d_automount(path
);
1119 * The filesystem is allowed to return -EISDIR here to indicate
1120 * it doesn't want to automount. For instance, autofs would do
1121 * this so that its userspace daemon can mount on this dentry.
1123 * However, we can only permit this if it's a terminal point in
1124 * the path being looked up; if it wasn't then the remainder of
1125 * the path is inaccessible and we should say so.
1127 if (PTR_ERR(mnt
) == -EISDIR
&& (nd
->flags
& LOOKUP_PARENT
))
1129 return PTR_ERR(mnt
);
1132 if (!mnt
) /* mount collision */
1135 if (!*need_mntput
) {
1136 /* lock_mount() may release path->mnt on error */
1138 *need_mntput
= true;
1140 err
= finish_automount(mnt
, path
);
1144 /* Someone else made a mount here whilst we were busy */
1149 path
->dentry
= dget(mnt
->mnt_root
);
1158 * Handle a dentry that is managed in some way.
1159 * - Flagged for transit management (autofs)
1160 * - Flagged as mountpoint
1161 * - Flagged as automount point
1163 * This may only be called in refwalk mode.
1165 * Serialization is taken care of in namespace.c
1167 static int follow_managed(struct path
*path
, struct nameidata
*nd
)
1169 struct vfsmount
*mnt
= path
->mnt
; /* held by caller, must be left alone */
1171 bool need_mntput
= false;
1174 /* Given that we're not holding a lock here, we retain the value in a
1175 * local variable for each dentry as we look at it so that we don't see
1176 * the components of that value change under us */
1177 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1178 managed
&= DCACHE_MANAGED_DENTRY
,
1179 unlikely(managed
!= 0)) {
1180 /* Allow the filesystem to manage the transit without i_mutex
1182 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1183 BUG_ON(!path
->dentry
->d_op
);
1184 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1185 ret
= path
->dentry
->d_op
->d_manage(path
->dentry
, false);
1190 /* Transit to a mounted filesystem. */
1191 if (managed
& DCACHE_MOUNTED
) {
1192 struct vfsmount
*mounted
= lookup_mnt(path
);
1197 path
->mnt
= mounted
;
1198 path
->dentry
= dget(mounted
->mnt_root
);
1203 /* Something is mounted on this dentry in another
1204 * namespace and/or whatever was mounted there in this
1205 * namespace got unmounted before lookup_mnt() could
1209 /* Handle an automount point */
1210 if (managed
& DCACHE_NEED_AUTOMOUNT
) {
1211 ret
= follow_automount(path
, nd
, &need_mntput
);
1217 /* We didn't change the current path point */
1221 if (need_mntput
&& path
->mnt
== mnt
)
1226 nd
->flags
|= LOOKUP_JUMPED
;
1227 if (unlikely(ret
< 0))
1228 path_put_conditional(path
, nd
);
1232 int follow_down_one(struct path
*path
)
1234 struct vfsmount
*mounted
;
1236 mounted
= lookup_mnt(path
);
1240 path
->mnt
= mounted
;
1241 path
->dentry
= dget(mounted
->mnt_root
);
1246 EXPORT_SYMBOL(follow_down_one
);
1248 static inline int managed_dentry_rcu(struct dentry
*dentry
)
1250 return (dentry
->d_flags
& DCACHE_MANAGE_TRANSIT
) ?
1251 dentry
->d_op
->d_manage(dentry
, true) : 0;
1255 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1256 * we meet a managed dentry that would need blocking.
1258 static bool __follow_mount_rcu(struct nameidata
*nd
, struct path
*path
,
1259 struct inode
**inode
, unsigned *seqp
)
1262 struct mount
*mounted
;
1264 * Don't forget we might have a non-mountpoint managed dentry
1265 * that wants to block transit.
1267 switch (managed_dentry_rcu(path
->dentry
)) {
1277 if (!d_mountpoint(path
->dentry
))
1278 return !(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
);
1280 mounted
= __lookup_mnt(path
->mnt
, path
->dentry
);
1283 path
->mnt
= &mounted
->mnt
;
1284 path
->dentry
= mounted
->mnt
.mnt_root
;
1285 nd
->flags
|= LOOKUP_JUMPED
;
1286 *seqp
= read_seqcount_begin(&path
->dentry
->d_seq
);
1288 * Update the inode too. We don't need to re-check the
1289 * dentry sequence number here after this d_inode read,
1290 * because a mount-point is always pinned.
1292 *inode
= path
->dentry
->d_inode
;
1294 return !read_seqretry(&mount_lock
, nd
->m_seq
) &&
1295 !(path
->dentry
->d_flags
& DCACHE_NEED_AUTOMOUNT
);
1298 static int follow_dotdot_rcu(struct nameidata
*nd
)
1300 struct inode
*inode
= nd
->inode
;
1305 if (path_equal(&nd
->path
, &nd
->root
))
1307 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1308 struct dentry
*old
= nd
->path
.dentry
;
1309 struct dentry
*parent
= old
->d_parent
;
1312 inode
= parent
->d_inode
;
1313 seq
= read_seqcount_begin(&parent
->d_seq
);
1314 if (unlikely(read_seqcount_retry(&old
->d_seq
, nd
->seq
)))
1316 nd
->path
.dentry
= parent
;
1318 if (unlikely(!path_connected(&nd
->path
)))
1322 struct mount
*mnt
= real_mount(nd
->path
.mnt
);
1323 struct mount
*mparent
= mnt
->mnt_parent
;
1324 struct dentry
*mountpoint
= mnt
->mnt_mountpoint
;
1325 struct inode
*inode2
= mountpoint
->d_inode
;
1326 unsigned seq
= read_seqcount_begin(&mountpoint
->d_seq
);
1327 if (unlikely(read_seqretry(&mount_lock
, nd
->m_seq
)))
1329 if (&mparent
->mnt
== nd
->path
.mnt
)
1331 /* we know that mountpoint was pinned */
1332 nd
->path
.dentry
= mountpoint
;
1333 nd
->path
.mnt
= &mparent
->mnt
;
1338 while (unlikely(d_mountpoint(nd
->path
.dentry
))) {
1339 struct mount
*mounted
;
1340 mounted
= __lookup_mnt(nd
->path
.mnt
, nd
->path
.dentry
);
1341 if (unlikely(read_seqretry(&mount_lock
, nd
->m_seq
)))
1345 nd
->path
.mnt
= &mounted
->mnt
;
1346 nd
->path
.dentry
= mounted
->mnt
.mnt_root
;
1347 inode
= nd
->path
.dentry
->d_inode
;
1348 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
1355 * Follow down to the covering mount currently visible to userspace. At each
1356 * point, the filesystem owning that dentry may be queried as to whether the
1357 * caller is permitted to proceed or not.
1359 int follow_down(struct path
*path
)
1364 while (managed
= ACCESS_ONCE(path
->dentry
->d_flags
),
1365 unlikely(managed
& DCACHE_MANAGED_DENTRY
)) {
1366 /* Allow the filesystem to manage the transit without i_mutex
1369 * We indicate to the filesystem if someone is trying to mount
1370 * something here. This gives autofs the chance to deny anyone
1371 * other than its daemon the right to mount on its
1374 * The filesystem may sleep at this point.
1376 if (managed
& DCACHE_MANAGE_TRANSIT
) {
1377 BUG_ON(!path
->dentry
->d_op
);
1378 BUG_ON(!path
->dentry
->d_op
->d_manage
);
1379 ret
= path
->dentry
->d_op
->d_manage(
1380 path
->dentry
, false);
1382 return ret
== -EISDIR
? 0 : ret
;
1385 /* Transit to a mounted filesystem. */
1386 if (managed
& DCACHE_MOUNTED
) {
1387 struct vfsmount
*mounted
= lookup_mnt(path
);
1392 path
->mnt
= mounted
;
1393 path
->dentry
= dget(mounted
->mnt_root
);
1397 /* Don't handle automount points here */
1402 EXPORT_SYMBOL(follow_down
);
1405 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1407 static void follow_mount(struct path
*path
)
1409 while (d_mountpoint(path
->dentry
)) {
1410 struct vfsmount
*mounted
= lookup_mnt(path
);
1415 path
->mnt
= mounted
;
1416 path
->dentry
= dget(mounted
->mnt_root
);
1420 static int follow_dotdot(struct nameidata
*nd
)
1426 struct dentry
*old
= nd
->path
.dentry
;
1428 if (nd
->path
.dentry
== nd
->root
.dentry
&&
1429 nd
->path
.mnt
== nd
->root
.mnt
) {
1432 if (nd
->path
.dentry
!= nd
->path
.mnt
->mnt_root
) {
1433 /* rare case of legitimate dget_parent()... */
1434 nd
->path
.dentry
= dget_parent(nd
->path
.dentry
);
1436 if (unlikely(!path_connected(&nd
->path
)))
1440 if (!follow_up(&nd
->path
))
1443 follow_mount(&nd
->path
);
1444 nd
->inode
= nd
->path
.dentry
->d_inode
;
1449 * This looks up the name in dcache, possibly revalidates the old dentry and
1450 * allocates a new one if not found or not valid. In the need_lookup argument
1451 * returns whether i_op->lookup is necessary.
1453 * dir->d_inode->i_mutex must be held
1455 static struct dentry
*lookup_dcache(struct qstr
*name
, struct dentry
*dir
,
1456 unsigned int flags
, bool *need_lookup
)
1458 struct dentry
*dentry
;
1461 *need_lookup
= false;
1462 dentry
= d_lookup(dir
, name
);
1464 if (dentry
->d_flags
& DCACHE_OP_REVALIDATE
) {
1465 error
= d_revalidate(dentry
, flags
);
1466 if (unlikely(error
<= 0)) {
1469 return ERR_PTR(error
);
1471 d_invalidate(dentry
);
1480 dentry
= d_alloc(dir
, name
);
1481 if (unlikely(!dentry
))
1482 return ERR_PTR(-ENOMEM
);
1484 *need_lookup
= true;
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(struct qstr
*name
,
1515 struct dentry
*base
, unsigned int flags
)
1518 struct dentry
*dentry
;
1520 dentry
= lookup_dcache(name
, base
, flags
, &need_lookup
);
1524 return lookup_real(base
->d_inode
, dentry
, flags
);
1528 * It's more convoluted than I'd like it to be, but... it's still fairly
1529 * small and for now I'd prefer to have fast path as straight as possible.
1530 * It _is_ time-critical.
1532 static int lookup_fast(struct nameidata
*nd
,
1533 struct path
*path
, struct inode
**inode
,
1536 struct vfsmount
*mnt
= nd
->path
.mnt
;
1537 struct dentry
*dentry
, *parent
= nd
->path
.dentry
;
1543 * Rename seqlock is not required here because in the off chance
1544 * of a false negative due to a concurrent rename, we're going to
1545 * do the non-racy lookup, below.
1547 if (nd
->flags
& LOOKUP_RCU
) {
1550 dentry
= __d_lookup_rcu(parent
, &nd
->last
, &seq
);
1555 * This sequence count validates that the inode matches
1556 * the dentry name information from lookup.
1558 *inode
= d_backing_inode(dentry
);
1559 negative
= d_is_negative(dentry
);
1560 if (read_seqcount_retry(&dentry
->d_seq
, seq
))
1564 * This sequence count validates that the parent had no
1565 * changes while we did the lookup of the dentry above.
1567 * The memory barrier in read_seqcount_begin of child is
1568 * enough, we can use __read_seqcount_retry here.
1570 if (__read_seqcount_retry(&parent
->d_seq
, nd
->seq
))
1574 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
)) {
1575 status
= d_revalidate(dentry
, nd
->flags
);
1576 if (unlikely(status
<= 0)) {
1577 if (status
!= -ECHILD
)
1583 * Note: do negative dentry check after revalidation in
1584 * case that drops it.
1589 path
->dentry
= dentry
;
1590 if (likely(__follow_mount_rcu(nd
, path
, inode
, seqp
)))
1593 if (unlazy_walk(nd
, dentry
, seq
))
1596 dentry
= __d_lookup(parent
, &nd
->last
);
1599 if (unlikely(!dentry
))
1602 if (unlikely(dentry
->d_flags
& DCACHE_OP_REVALIDATE
) && need_reval
)
1603 status
= d_revalidate(dentry
, nd
->flags
);
1604 if (unlikely(status
<= 0)) {
1609 d_invalidate(dentry
);
1614 if (unlikely(d_is_negative(dentry
))) {
1619 path
->dentry
= dentry
;
1620 err
= follow_managed(path
, nd
);
1622 *inode
= d_backing_inode(path
->dentry
);
1629 /* Fast lookup failed, do it the slow way */
1630 static int lookup_slow(struct nameidata
*nd
, struct path
*path
)
1632 struct dentry
*dentry
, *parent
;
1634 parent
= nd
->path
.dentry
;
1635 BUG_ON(nd
->inode
!= parent
->d_inode
);
1637 mutex_lock(&parent
->d_inode
->i_mutex
);
1638 dentry
= __lookup_hash(&nd
->last
, parent
, nd
->flags
);
1639 mutex_unlock(&parent
->d_inode
->i_mutex
);
1641 return PTR_ERR(dentry
);
1642 path
->mnt
= nd
->path
.mnt
;
1643 path
->dentry
= dentry
;
1644 return follow_managed(path
, nd
);
1647 static inline int may_lookup(struct nameidata
*nd
)
1649 if (nd
->flags
& LOOKUP_RCU
) {
1650 int err
= inode_permission(nd
->inode
, MAY_EXEC
|MAY_NOT_BLOCK
);
1653 if (unlazy_walk(nd
, NULL
, 0))
1656 return inode_permission(nd
->inode
, MAY_EXEC
);
1659 static inline int handle_dots(struct nameidata
*nd
, int type
)
1661 if (type
== LAST_DOTDOT
) {
1662 if (nd
->flags
& LOOKUP_RCU
) {
1663 return follow_dotdot_rcu(nd
);
1665 return follow_dotdot(nd
);
1670 static int pick_link(struct nameidata
*nd
, struct path
*link
,
1671 struct inode
*inode
, unsigned seq
)
1675 if (unlikely(nd
->total_link_count
++ >= MAXSYMLINKS
)) {
1676 path_to_nameidata(link
, nd
);
1679 if (!(nd
->flags
& LOOKUP_RCU
)) {
1680 if (link
->mnt
== nd
->path
.mnt
)
1683 error
= nd_alloc_stack(nd
);
1684 if (unlikely(error
)) {
1685 if (error
== -ECHILD
) {
1686 if (unlikely(unlazy_link(nd
, link
, seq
)))
1688 error
= nd_alloc_stack(nd
);
1696 last
= nd
->stack
+ nd
->depth
++;
1698 last
->cookie
= NULL
;
1699 last
->inode
= inode
;
1705 * Do we need to follow links? We _really_ want to be able
1706 * to do this check without having to look at inode->i_op,
1707 * so we keep a cache of "no, this doesn't need follow_link"
1708 * for the common case.
1710 static inline int should_follow_link(struct nameidata
*nd
, struct path
*link
,
1712 struct inode
*inode
, unsigned seq
)
1714 if (likely(!d_is_symlink(link
->dentry
)))
1718 /* make sure that d_is_symlink above matches inode */
1719 if (nd
->flags
& LOOKUP_RCU
) {
1720 if (read_seqcount_retry(&link
->dentry
->d_seq
, seq
))
1723 return pick_link(nd
, link
, inode
, seq
);
1726 enum {WALK_GET
= 1, WALK_PUT
= 2};
1728 static int walk_component(struct nameidata
*nd
, int flags
)
1731 struct inode
*inode
;
1735 * "." and ".." are special - ".." especially so because it has
1736 * to be able to know about the current root directory and
1737 * parent relationships.
1739 if (unlikely(nd
->last_type
!= LAST_NORM
)) {
1740 err
= handle_dots(nd
, nd
->last_type
);
1741 if (flags
& WALK_PUT
)
1745 err
= lookup_fast(nd
, &path
, &inode
, &seq
);
1746 if (unlikely(err
)) {
1750 err
= lookup_slow(nd
, &path
);
1754 seq
= 0; /* we are already out of RCU mode */
1756 if (d_is_negative(path
.dentry
))
1758 inode
= d_backing_inode(path
.dentry
);
1761 if (flags
& WALK_PUT
)
1763 err
= should_follow_link(nd
, &path
, flags
& WALK_GET
, inode
, seq
);
1766 path_to_nameidata(&path
, nd
);
1772 path_to_nameidata(&path
, nd
);
1777 * We can do the critical dentry name comparison and hashing
1778 * operations one word at a time, but we are limited to:
1780 * - Architectures with fast unaligned word accesses. We could
1781 * do a "get_unaligned()" if this helps and is sufficiently
1784 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1785 * do not trap on the (extremely unlikely) case of a page
1786 * crossing operation.
1788 * - Furthermore, we need an efficient 64-bit compile for the
1789 * 64-bit case in order to generate the "number of bytes in
1790 * the final mask". Again, that could be replaced with a
1791 * efficient population count instruction or similar.
1793 #ifdef CONFIG_DCACHE_WORD_ACCESS
1795 #include <asm/word-at-a-time.h>
1799 static inline unsigned int fold_hash(unsigned long hash
)
1801 return hash_64(hash
, 32);
1804 #else /* 32-bit case */
1806 #define fold_hash(x) (x)
1810 unsigned int full_name_hash(const unsigned char *name
, unsigned int len
)
1812 unsigned long a
, mask
;
1813 unsigned long hash
= 0;
1816 a
= load_unaligned_zeropad(name
);
1817 if (len
< sizeof(unsigned long))
1821 name
+= sizeof(unsigned long);
1822 len
-= sizeof(unsigned long);
1826 mask
= bytemask_from_count(len
);
1829 return fold_hash(hash
);
1831 EXPORT_SYMBOL(full_name_hash
);
1834 * Calculate the length and hash of the path component, and
1835 * return the "hash_len" as the result.
1837 static inline u64
hash_name(const char *name
)
1839 unsigned long a
, b
, adata
, bdata
, mask
, hash
, len
;
1840 const struct word_at_a_time constants
= WORD_AT_A_TIME_CONSTANTS
;
1843 len
= -sizeof(unsigned long);
1845 hash
= (hash
+ a
) * 9;
1846 len
+= sizeof(unsigned long);
1847 a
= load_unaligned_zeropad(name
+len
);
1848 b
= a
^ REPEAT_BYTE('/');
1849 } while (!(has_zero(a
, &adata
, &constants
) | has_zero(b
, &bdata
, &constants
)));
1851 adata
= prep_zero_mask(a
, adata
, &constants
);
1852 bdata
= prep_zero_mask(b
, bdata
, &constants
);
1854 mask
= create_zero_mask(adata
| bdata
);
1856 hash
+= a
& zero_bytemask(mask
);
1857 len
+= find_zero(mask
);
1858 return hashlen_create(fold_hash(hash
), len
);
1863 unsigned int full_name_hash(const unsigned char *name
, unsigned int len
)
1865 unsigned long hash
= init_name_hash();
1867 hash
= partial_name_hash(*name
++, hash
);
1868 return end_name_hash(hash
);
1870 EXPORT_SYMBOL(full_name_hash
);
1873 * We know there's a real path component here of at least
1876 static inline u64
hash_name(const char *name
)
1878 unsigned long hash
= init_name_hash();
1879 unsigned long len
= 0, c
;
1881 c
= (unsigned char)*name
;
1884 hash
= partial_name_hash(c
, hash
);
1885 c
= (unsigned char)name
[len
];
1886 } while (c
&& c
!= '/');
1887 return hashlen_create(end_name_hash(hash
), len
);
1894 * This is the basic name resolution function, turning a pathname into
1895 * the final dentry. We expect 'base' to be positive and a directory.
1897 * Returns 0 and nd will have valid dentry and mnt on success.
1898 * Returns error and drops reference to input namei data on failure.
1900 static int link_path_walk(const char *name
, struct nameidata
*nd
)
1909 /* At this point we know we have a real path component. */
1914 err
= may_lookup(nd
);
1918 hash_len
= hash_name(name
);
1921 if (name
[0] == '.') switch (hashlen_len(hash_len
)) {
1923 if (name
[1] == '.') {
1925 nd
->flags
|= LOOKUP_JUMPED
;
1931 if (likely(type
== LAST_NORM
)) {
1932 struct dentry
*parent
= nd
->path
.dentry
;
1933 nd
->flags
&= ~LOOKUP_JUMPED
;
1934 if (unlikely(parent
->d_flags
& DCACHE_OP_HASH
)) {
1935 struct qstr
this = { { .hash_len
= hash_len
}, .name
= name
};
1936 err
= parent
->d_op
->d_hash(parent
, &this);
1939 hash_len
= this.hash_len
;
1944 nd
->last
.hash_len
= hash_len
;
1945 nd
->last
.name
= name
;
1946 nd
->last_type
= type
;
1948 name
+= hashlen_len(hash_len
);
1952 * If it wasn't NUL, we know it was '/'. Skip that
1953 * slash, and continue until no more slashes.
1957 } while (unlikely(*name
== '/'));
1958 if (unlikely(!*name
)) {
1960 /* pathname body, done */
1963 name
= nd
->stack
[nd
->depth
- 1].name
;
1964 /* trailing symlink, done */
1967 /* last component of nested symlink */
1968 err
= walk_component(nd
, WALK_GET
| WALK_PUT
);
1970 err
= walk_component(nd
, WALK_GET
);
1976 const char *s
= get_link(nd
);
1985 nd
->stack
[nd
->depth
- 1].name
= name
;
1990 if (unlikely(!d_can_lookup(nd
->path
.dentry
))) {
1991 if (nd
->flags
& LOOKUP_RCU
) {
1992 if (unlazy_walk(nd
, NULL
, 0))
2000 static const char *path_init(struct nameidata
*nd
, unsigned flags
)
2003 const char *s
= nd
->name
->name
;
2006 flags
&= ~LOOKUP_RCU
;
2008 nd
->last_type
= LAST_ROOT
; /* if there are only slashes... */
2009 nd
->flags
= flags
| LOOKUP_JUMPED
| LOOKUP_PARENT
;
2011 if (flags
& LOOKUP_ROOT
) {
2012 struct dentry
*root
= nd
->root
.dentry
;
2013 struct inode
*inode
= root
->d_inode
;
2015 if (!d_can_lookup(root
))
2016 return ERR_PTR(-ENOTDIR
);
2017 retval
= inode_permission(inode
, MAY_EXEC
);
2019 return ERR_PTR(retval
);
2021 nd
->path
= nd
->root
;
2023 if (flags
& LOOKUP_RCU
) {
2025 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2026 nd
->root_seq
= nd
->seq
;
2027 nd
->m_seq
= read_seqbegin(&mount_lock
);
2029 path_get(&nd
->path
);
2034 nd
->root
.mnt
= NULL
;
2036 nd
->m_seq
= read_seqbegin(&mount_lock
);
2038 if (flags
& LOOKUP_RCU
) {
2041 nd
->seq
= nd
->root_seq
;
2044 path_get(&nd
->root
);
2046 nd
->path
= nd
->root
;
2047 } else if (nd
->dfd
== AT_FDCWD
) {
2048 if (flags
& LOOKUP_RCU
) {
2049 struct fs_struct
*fs
= current
->fs
;
2055 seq
= read_seqcount_begin(&fs
->seq
);
2057 nd
->seq
= __read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2058 } while (read_seqcount_retry(&fs
->seq
, seq
));
2060 get_fs_pwd(current
->fs
, &nd
->path
);
2063 /* Caller must check execute permissions on the starting path component */
2064 struct fd f
= fdget_raw(nd
->dfd
);
2065 struct dentry
*dentry
;
2068 return ERR_PTR(-EBADF
);
2070 dentry
= f
.file
->f_path
.dentry
;
2073 if (!d_can_lookup(dentry
)) {
2075 return ERR_PTR(-ENOTDIR
);
2079 nd
->path
= f
.file
->f_path
;
2080 if (flags
& LOOKUP_RCU
) {
2082 nd
->inode
= nd
->path
.dentry
->d_inode
;
2083 nd
->seq
= read_seqcount_begin(&nd
->path
.dentry
->d_seq
);
2085 path_get(&nd
->path
);
2086 nd
->inode
= nd
->path
.dentry
->d_inode
;
2092 nd
->inode
= nd
->path
.dentry
->d_inode
;
2093 if (!(flags
& LOOKUP_RCU
))
2095 if (likely(!read_seqcount_retry(&nd
->path
.dentry
->d_seq
, nd
->seq
)))
2097 if (!(nd
->flags
& LOOKUP_ROOT
))
2098 nd
->root
.mnt
= NULL
;
2100 return ERR_PTR(-ECHILD
);
2103 static const char *trailing_symlink(struct nameidata
*nd
)
2106 int error
= may_follow_link(nd
);
2107 if (unlikely(error
))
2108 return ERR_PTR(error
);
2109 nd
->flags
|= LOOKUP_PARENT
;
2110 nd
->stack
[0].name
= NULL
;
2115 static inline int lookup_last(struct nameidata
*nd
)
2117 if (nd
->last_type
== LAST_NORM
&& nd
->last
.name
[nd
->last
.len
])
2118 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
2120 nd
->flags
&= ~LOOKUP_PARENT
;
2121 return walk_component(nd
,
2122 nd
->flags
& LOOKUP_FOLLOW
2124 ? WALK_PUT
| WALK_GET
2129 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2130 static int path_lookupat(struct nameidata
*nd
, unsigned flags
, struct path
*path
)
2132 const char *s
= path_init(nd
, flags
);
2137 while (!(err
= link_path_walk(s
, nd
))
2138 && ((err
= lookup_last(nd
)) > 0)) {
2139 s
= trailing_symlink(nd
);
2146 err
= complete_walk(nd
);
2148 if (!err
&& nd
->flags
& LOOKUP_DIRECTORY
)
2149 if (!d_can_lookup(nd
->path
.dentry
))
2153 nd
->path
.mnt
= NULL
;
2154 nd
->path
.dentry
= NULL
;
2160 static int filename_lookup(int dfd
, struct filename
*name
, unsigned flags
,
2161 struct path
*path
, struct path
*root
)
2164 struct nameidata nd
;
2166 return PTR_ERR(name
);
2167 if (unlikely(root
)) {
2169 flags
|= LOOKUP_ROOT
;
2171 set_nameidata(&nd
, dfd
, name
);
2172 retval
= path_lookupat(&nd
, flags
| LOOKUP_RCU
, path
);
2173 if (unlikely(retval
== -ECHILD
))
2174 retval
= path_lookupat(&nd
, flags
, path
);
2175 if (unlikely(retval
== -ESTALE
))
2176 retval
= path_lookupat(&nd
, flags
| LOOKUP_REVAL
, path
);
2178 if (likely(!retval
))
2179 audit_inode(name
, path
->dentry
, flags
& LOOKUP_PARENT
);
2180 restore_nameidata();
2185 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
2186 static int path_parentat(struct nameidata
*nd
, unsigned flags
,
2187 struct path
*parent
)
2189 const char *s
= path_init(nd
, flags
);
2193 err
= link_path_walk(s
, nd
);
2195 err
= complete_walk(nd
);
2198 nd
->path
.mnt
= NULL
;
2199 nd
->path
.dentry
= NULL
;
2205 static struct filename
*filename_parentat(int dfd
, struct filename
*name
,
2206 unsigned int flags
, struct path
*parent
,
2207 struct qstr
*last
, int *type
)
2210 struct nameidata nd
;
2214 set_nameidata(&nd
, dfd
, name
);
2215 retval
= path_parentat(&nd
, flags
| LOOKUP_RCU
, parent
);
2216 if (unlikely(retval
== -ECHILD
))
2217 retval
= path_parentat(&nd
, flags
, parent
);
2218 if (unlikely(retval
== -ESTALE
))
2219 retval
= path_parentat(&nd
, flags
| LOOKUP_REVAL
, parent
);
2220 if (likely(!retval
)) {
2222 *type
= nd
.last_type
;
2223 audit_inode(name
, parent
->dentry
, LOOKUP_PARENT
);
2226 name
= ERR_PTR(retval
);
2228 restore_nameidata();
2232 /* does lookup, returns the object with parent locked */
2233 struct dentry
*kern_path_locked(const char *name
, struct path
*path
)
2235 struct filename
*filename
;
2240 filename
= filename_parentat(AT_FDCWD
, getname_kernel(name
), 0, path
,
2242 if (IS_ERR(filename
))
2243 return ERR_CAST(filename
);
2244 if (unlikely(type
!= LAST_NORM
)) {
2247 return ERR_PTR(-EINVAL
);
2249 mutex_lock_nested(&path
->dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2250 d
= __lookup_hash(&last
, path
->dentry
, 0);
2252 mutex_unlock(&path
->dentry
->d_inode
->i_mutex
);
2259 int kern_path(const char *name
, unsigned int flags
, struct path
*path
)
2261 return filename_lookup(AT_FDCWD
, getname_kernel(name
),
2264 EXPORT_SYMBOL(kern_path
);
2267 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2268 * @dentry: pointer to dentry of the base directory
2269 * @mnt: pointer to vfs mount of the base directory
2270 * @name: pointer to file name
2271 * @flags: lookup flags
2272 * @path: pointer to struct path to fill
2274 int vfs_path_lookup(struct dentry
*dentry
, struct vfsmount
*mnt
,
2275 const char *name
, unsigned int flags
,
2278 struct path root
= {.mnt
= mnt
, .dentry
= dentry
};
2279 /* the first argument of filename_lookup() is ignored with root */
2280 return filename_lookup(AT_FDCWD
, getname_kernel(name
),
2281 flags
, path
, &root
);
2283 EXPORT_SYMBOL(vfs_path_lookup
);
2286 * lookup_one_len - filesystem helper to lookup single pathname component
2287 * @name: pathname component to lookup
2288 * @base: base directory to lookup from
2289 * @len: maximum length @len should be interpreted to
2291 * Note that this routine is purely a helper for filesystem usage and should
2292 * not be called by generic code.
2294 struct dentry
*lookup_one_len(const char *name
, struct dentry
*base
, int len
)
2300 WARN_ON_ONCE(!mutex_is_locked(&base
->d_inode
->i_mutex
));
2304 this.hash
= full_name_hash(name
, len
);
2306 return ERR_PTR(-EACCES
);
2308 if (unlikely(name
[0] == '.')) {
2309 if (len
< 2 || (len
== 2 && name
[1] == '.'))
2310 return ERR_PTR(-EACCES
);
2314 c
= *(const unsigned char *)name
++;
2315 if (c
== '/' || c
== '\0')
2316 return ERR_PTR(-EACCES
);
2319 * See if the low-level filesystem might want
2320 * to use its own hash..
2322 if (base
->d_flags
& DCACHE_OP_HASH
) {
2323 int err
= base
->d_op
->d_hash(base
, &this);
2325 return ERR_PTR(err
);
2328 err
= inode_permission(base
->d_inode
, MAY_EXEC
);
2330 return ERR_PTR(err
);
2332 return __lookup_hash(&this, base
, 0);
2334 EXPORT_SYMBOL(lookup_one_len
);
2336 int user_path_at_empty(int dfd
, const char __user
*name
, unsigned flags
,
2337 struct path
*path
, int *empty
)
2339 return filename_lookup(dfd
, getname_flags(name
, flags
, empty
),
2342 EXPORT_SYMBOL(user_path_at_empty
);
2345 * NB: most callers don't do anything directly with the reference to the
2346 * to struct filename, but the nd->last pointer points into the name string
2347 * allocated by getname. So we must hold the reference to it until all
2348 * path-walking is complete.
2350 static inline struct filename
*
2351 user_path_parent(int dfd
, const char __user
*path
,
2352 struct path
*parent
,
2357 /* only LOOKUP_REVAL is allowed in extra flags */
2358 return filename_parentat(dfd
, getname(path
), flags
& LOOKUP_REVAL
,
2359 parent
, last
, type
);
2363 * mountpoint_last - look up last component for umount
2364 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2365 * @path: pointer to container for result
2367 * This is a special lookup_last function just for umount. In this case, we
2368 * need to resolve the path without doing any revalidation.
2370 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2371 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2372 * in almost all cases, this lookup will be served out of the dcache. The only
2373 * cases where it won't are if nd->last refers to a symlink or the path is
2374 * bogus and it doesn't exist.
2377 * -error: if there was an error during lookup. This includes -ENOENT if the
2378 * lookup found a negative dentry. The nd->path reference will also be
2381 * 0: if we successfully resolved nd->path and found it to not to be a
2382 * symlink that needs to be followed. "path" will also be populated.
2383 * The nd->path reference will also be put.
2385 * 1: if we successfully resolved nd->last and found it to be a symlink
2386 * that needs to be followed. "path" will be populated with the path
2387 * to the link, and nd->path will *not* be put.
2390 mountpoint_last(struct nameidata
*nd
, struct path
*path
)
2393 struct dentry
*dentry
;
2394 struct dentry
*dir
= nd
->path
.dentry
;
2396 /* If we're in rcuwalk, drop out of it to handle last component */
2397 if (nd
->flags
& LOOKUP_RCU
) {
2398 if (unlazy_walk(nd
, NULL
, 0))
2402 nd
->flags
&= ~LOOKUP_PARENT
;
2404 if (unlikely(nd
->last_type
!= LAST_NORM
)) {
2405 error
= handle_dots(nd
, nd
->last_type
);
2408 dentry
= dget(nd
->path
.dentry
);
2412 mutex_lock(&dir
->d_inode
->i_mutex
);
2413 dentry
= d_lookup(dir
, &nd
->last
);
2416 * No cached dentry. Mounted dentries are pinned in the cache,
2417 * so that means that this dentry is probably a symlink or the
2418 * path doesn't actually point to a mounted dentry.
2420 dentry
= d_alloc(dir
, &nd
->last
);
2422 mutex_unlock(&dir
->d_inode
->i_mutex
);
2425 dentry
= lookup_real(dir
->d_inode
, dentry
, nd
->flags
);
2426 if (IS_ERR(dentry
)) {
2427 mutex_unlock(&dir
->d_inode
->i_mutex
);
2428 return PTR_ERR(dentry
);
2431 mutex_unlock(&dir
->d_inode
->i_mutex
);
2434 if (d_is_negative(dentry
)) {
2440 path
->dentry
= dentry
;
2441 path
->mnt
= nd
->path
.mnt
;
2442 error
= should_follow_link(nd
, path
, nd
->flags
& LOOKUP_FOLLOW
,
2443 d_backing_inode(dentry
), 0);
2444 if (unlikely(error
))
2452 * path_mountpoint - look up a path to be umounted
2453 * @nd: lookup context
2454 * @flags: lookup flags
2455 * @path: pointer to container for result
2457 * Look up the given name, but don't attempt to revalidate the last component.
2458 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2461 path_mountpoint(struct nameidata
*nd
, unsigned flags
, struct path
*path
)
2463 const char *s
= path_init(nd
, flags
);
2467 while (!(err
= link_path_walk(s
, nd
)) &&
2468 (err
= mountpoint_last(nd
, path
)) > 0) {
2469 s
= trailing_symlink(nd
);
2480 filename_mountpoint(int dfd
, struct filename
*name
, struct path
*path
,
2483 struct nameidata nd
;
2486 return PTR_ERR(name
);
2487 set_nameidata(&nd
, dfd
, name
);
2488 error
= path_mountpoint(&nd
, flags
| LOOKUP_RCU
, path
);
2489 if (unlikely(error
== -ECHILD
))
2490 error
= path_mountpoint(&nd
, flags
, path
);
2491 if (unlikely(error
== -ESTALE
))
2492 error
= path_mountpoint(&nd
, flags
| LOOKUP_REVAL
, path
);
2494 audit_inode(name
, path
->dentry
, 0);
2495 restore_nameidata();
2501 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2502 * @dfd: directory file descriptor
2503 * @name: pathname from userland
2504 * @flags: lookup flags
2505 * @path: pointer to container to hold result
2507 * A umount is a special case for path walking. We're not actually interested
2508 * in the inode in this situation, and ESTALE errors can be a problem. We
2509 * simply want track down the dentry and vfsmount attached at the mountpoint
2510 * and avoid revalidating the last component.
2512 * Returns 0 and populates "path" on success.
2515 user_path_mountpoint_at(int dfd
, const char __user
*name
, unsigned int flags
,
2518 return filename_mountpoint(dfd
, getname(name
), path
, flags
);
2522 kern_path_mountpoint(int dfd
, const char *name
, struct path
*path
,
2525 return filename_mountpoint(dfd
, getname_kernel(name
), path
, flags
);
2527 EXPORT_SYMBOL(kern_path_mountpoint
);
2529 int __check_sticky(struct inode
*dir
, struct inode
*inode
)
2531 kuid_t fsuid
= current_fsuid();
2533 if (uid_eq(inode
->i_uid
, fsuid
))
2535 if (uid_eq(dir
->i_uid
, fsuid
))
2537 return !capable_wrt_inode_uidgid(inode
, CAP_FOWNER
);
2539 EXPORT_SYMBOL(__check_sticky
);
2542 * Check whether we can remove a link victim from directory dir, check
2543 * whether the type of victim is right.
2544 * 1. We can't do it if dir is read-only (done in permission())
2545 * 2. We should have write and exec permissions on dir
2546 * 3. We can't remove anything from append-only dir
2547 * 4. We can't do anything with immutable dir (done in permission())
2548 * 5. If the sticky bit on dir is set we should either
2549 * a. be owner of dir, or
2550 * b. be owner of victim, or
2551 * c. have CAP_FOWNER capability
2552 * 6. If the victim is append-only or immutable we can't do antyhing with
2553 * links pointing to it.
2554 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2555 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2556 * 9. We can't remove a root or mountpoint.
2557 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
2558 * nfs_async_unlink().
2560 static int may_delete(struct inode
*dir
, struct dentry
*victim
, bool isdir
)
2562 struct inode
*inode
= d_backing_inode(victim
);
2565 if (d_is_negative(victim
))
2569 BUG_ON(victim
->d_parent
->d_inode
!= dir
);
2570 audit_inode_child(dir
, victim
, AUDIT_TYPE_CHILD_DELETE
);
2572 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
2578 if (check_sticky(dir
, inode
) || IS_APPEND(inode
) ||
2579 IS_IMMUTABLE(inode
) || IS_SWAPFILE(inode
))
2582 if (!d_is_dir(victim
))
2584 if (IS_ROOT(victim
))
2586 } else if (d_is_dir(victim
))
2588 if (IS_DEADDIR(dir
))
2590 if (victim
->d_flags
& DCACHE_NFSFS_RENAMED
)
2595 /* Check whether we can create an object with dentry child in directory
2597 * 1. We can't do it if child already exists (open has special treatment for
2598 * this case, but since we are inlined it's OK)
2599 * 2. We can't do it if dir is read-only (done in permission())
2600 * 3. We should have write and exec permissions on dir
2601 * 4. We can't do it if dir is immutable (done in permission())
2603 static inline int may_create(struct inode
*dir
, struct dentry
*child
)
2605 audit_inode_child(dir
, child
, AUDIT_TYPE_CHILD_CREATE
);
2608 if (IS_DEADDIR(dir
))
2610 return inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
2614 * p1 and p2 should be directories on the same fs.
2616 struct dentry
*lock_rename(struct dentry
*p1
, struct dentry
*p2
)
2621 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2625 mutex_lock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
2627 p
= d_ancestor(p2
, p1
);
2629 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2630 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2634 p
= d_ancestor(p1
, p2
);
2636 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2637 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_CHILD
);
2641 mutex_lock_nested(&p1
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
2642 mutex_lock_nested(&p2
->d_inode
->i_mutex
, I_MUTEX_PARENT2
);
2645 EXPORT_SYMBOL(lock_rename
);
2647 void unlock_rename(struct dentry
*p1
, struct dentry
*p2
)
2649 mutex_unlock(&p1
->d_inode
->i_mutex
);
2651 mutex_unlock(&p2
->d_inode
->i_mutex
);
2652 mutex_unlock(&p1
->d_inode
->i_sb
->s_vfs_rename_mutex
);
2655 EXPORT_SYMBOL(unlock_rename
);
2657 int vfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2660 int error
= may_create(dir
, dentry
);
2664 if (!dir
->i_op
->create
)
2665 return -EACCES
; /* shouldn't it be ENOSYS? */
2668 error
= security_inode_create(dir
, dentry
, mode
);
2671 error
= dir
->i_op
->create(dir
, dentry
, mode
, want_excl
);
2673 fsnotify_create(dir
, dentry
);
2676 EXPORT_SYMBOL(vfs_create
);
2678 static int may_open(struct path
*path
, int acc_mode
, int flag
)
2680 struct dentry
*dentry
= path
->dentry
;
2681 struct inode
*inode
= dentry
->d_inode
;
2691 switch (inode
->i_mode
& S_IFMT
) {
2695 if (acc_mode
& MAY_WRITE
)
2700 if (path
->mnt
->mnt_flags
& MNT_NODEV
)
2709 error
= inode_permission(inode
, acc_mode
);
2714 * An append-only file must be opened in append mode for writing.
2716 if (IS_APPEND(inode
)) {
2717 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
2723 /* O_NOATIME can only be set by the owner or superuser */
2724 if (flag
& O_NOATIME
&& !inode_owner_or_capable(inode
))
2730 static int handle_truncate(struct file
*filp
)
2732 struct path
*path
= &filp
->f_path
;
2733 struct inode
*inode
= path
->dentry
->d_inode
;
2734 int error
= get_write_access(inode
);
2738 * Refuse to truncate files with mandatory locks held on them.
2740 error
= locks_verify_locked(filp
);
2742 error
= security_path_truncate(path
);
2744 error
= do_truncate(path
->dentry
, 0,
2745 ATTR_MTIME
|ATTR_CTIME
|ATTR_OPEN
,
2748 put_write_access(inode
);
2752 static inline int open_to_namei_flags(int flag
)
2754 if ((flag
& O_ACCMODE
) == 3)
2759 static int may_o_create(struct path
*dir
, struct dentry
*dentry
, umode_t mode
)
2761 int error
= security_path_mknod(dir
, dentry
, mode
, 0);
2765 error
= inode_permission(dir
->dentry
->d_inode
, MAY_WRITE
| MAY_EXEC
);
2769 return security_inode_create(dir
->dentry
->d_inode
, dentry
, mode
);
2773 * Attempt to atomically look up, create and open a file from a negative
2776 * Returns 0 if successful. The file will have been created and attached to
2777 * @file by the filesystem calling finish_open().
2779 * Returns 1 if the file was looked up only or didn't need creating. The
2780 * caller will need to perform the open themselves. @path will have been
2781 * updated to point to the new dentry. This may be negative.
2783 * Returns an error code otherwise.
2785 static int atomic_open(struct nameidata
*nd
, struct dentry
*dentry
,
2786 struct path
*path
, struct file
*file
,
2787 const struct open_flags
*op
,
2788 bool got_write
, bool need_lookup
,
2791 struct inode
*dir
= nd
->path
.dentry
->d_inode
;
2792 unsigned open_flag
= open_to_namei_flags(op
->open_flag
);
2796 int create_error
= 0;
2797 struct dentry
*const DENTRY_NOT_SET
= (void *) -1UL;
2800 BUG_ON(dentry
->d_inode
);
2802 /* Don't create child dentry for a dead directory. */
2803 if (unlikely(IS_DEADDIR(dir
))) {
2809 if ((open_flag
& O_CREAT
) && !IS_POSIXACL(dir
))
2810 mode
&= ~current_umask();
2812 excl
= (open_flag
& (O_EXCL
| O_CREAT
)) == (O_EXCL
| O_CREAT
);
2814 open_flag
&= ~O_TRUNC
;
2817 * Checking write permission is tricky, bacuse we don't know if we are
2818 * going to actually need it: O_CREAT opens should work as long as the
2819 * file exists. But checking existence breaks atomicity. The trick is
2820 * to check access and if not granted clear O_CREAT from the flags.
2822 * Another problem is returing the "right" error value (e.g. for an
2823 * O_EXCL open we want to return EEXIST not EROFS).
2825 if (((open_flag
& (O_CREAT
| O_TRUNC
)) ||
2826 (open_flag
& O_ACCMODE
) != O_RDONLY
) && unlikely(!got_write
)) {
2827 if (!(open_flag
& O_CREAT
)) {
2829 * No O_CREATE -> atomicity not a requirement -> fall
2830 * back to lookup + open
2833 } else if (open_flag
& (O_EXCL
| O_TRUNC
)) {
2834 /* Fall back and fail with the right error */
2835 create_error
= -EROFS
;
2838 /* No side effects, safe to clear O_CREAT */
2839 create_error
= -EROFS
;
2840 open_flag
&= ~O_CREAT
;
2844 if (open_flag
& O_CREAT
) {
2845 error
= may_o_create(&nd
->path
, dentry
, mode
);
2847 create_error
= error
;
2848 if (open_flag
& O_EXCL
)
2850 open_flag
&= ~O_CREAT
;
2854 if (nd
->flags
& LOOKUP_DIRECTORY
)
2855 open_flag
|= O_DIRECTORY
;
2857 file
->f_path
.dentry
= DENTRY_NOT_SET
;
2858 file
->f_path
.mnt
= nd
->path
.mnt
;
2859 error
= dir
->i_op
->atomic_open(dir
, dentry
, file
, open_flag
, mode
,
2862 if (create_error
&& error
== -ENOENT
)
2863 error
= create_error
;
2867 if (error
) { /* returned 1, that is */
2868 if (WARN_ON(file
->f_path
.dentry
== DENTRY_NOT_SET
)) {
2872 if (file
->f_path
.dentry
) {
2874 dentry
= file
->f_path
.dentry
;
2876 if (*opened
& FILE_CREATED
)
2877 fsnotify_create(dir
, dentry
);
2878 if (!dentry
->d_inode
) {
2879 WARN_ON(*opened
& FILE_CREATED
);
2881 error
= create_error
;
2885 if (excl
&& !(*opened
& FILE_CREATED
)) {
2894 * We didn't have the inode before the open, so check open permission
2897 acc_mode
= op
->acc_mode
;
2898 if (*opened
& FILE_CREATED
) {
2899 WARN_ON(!(open_flag
& O_CREAT
));
2900 fsnotify_create(dir
, dentry
);
2901 acc_mode
= MAY_OPEN
;
2903 error
= may_open(&file
->f_path
, acc_mode
, open_flag
);
2913 dentry
= lookup_real(dir
, dentry
, nd
->flags
);
2915 return PTR_ERR(dentry
);
2917 if (create_error
&& !dentry
->d_inode
) {
2918 error
= create_error
;
2922 path
->dentry
= dentry
;
2923 path
->mnt
= nd
->path
.mnt
;
2928 * Look up and maybe create and open the last component.
2930 * Must be called with i_mutex held on parent.
2932 * Returns 0 if the file was successfully atomically created (if necessary) and
2933 * opened. In this case the file will be returned attached to @file.
2935 * Returns 1 if the file was not completely opened at this time, though lookups
2936 * and creations will have been performed and the dentry returned in @path will
2937 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
2938 * specified then a negative dentry may be returned.
2940 * An error code is returned otherwise.
2942 * FILE_CREATE will be set in @*opened if the dentry was created and will be
2943 * cleared otherwise prior to returning.
2945 static int lookup_open(struct nameidata
*nd
, struct path
*path
,
2947 const struct open_flags
*op
,
2948 bool got_write
, int *opened
)
2950 struct dentry
*dir
= nd
->path
.dentry
;
2951 struct inode
*dir_inode
= dir
->d_inode
;
2952 struct dentry
*dentry
;
2956 *opened
&= ~FILE_CREATED
;
2957 dentry
= lookup_dcache(&nd
->last
, dir
, nd
->flags
, &need_lookup
);
2959 return PTR_ERR(dentry
);
2961 /* Cached positive dentry: will open in f_op->open */
2962 if (!need_lookup
&& dentry
->d_inode
)
2965 if ((nd
->flags
& LOOKUP_OPEN
) && dir_inode
->i_op
->atomic_open
) {
2966 return atomic_open(nd
, dentry
, path
, file
, op
, got_write
,
2967 need_lookup
, opened
);
2971 BUG_ON(dentry
->d_inode
);
2973 dentry
= lookup_real(dir_inode
, dentry
, nd
->flags
);
2975 return PTR_ERR(dentry
);
2978 /* Negative dentry, just create the file */
2979 if (!dentry
->d_inode
&& (op
->open_flag
& O_CREAT
)) {
2980 umode_t mode
= op
->mode
;
2981 if (!IS_POSIXACL(dir
->d_inode
))
2982 mode
&= ~current_umask();
2984 * This write is needed to ensure that a
2985 * rw->ro transition does not occur between
2986 * the time when the file is created and when
2987 * a permanent write count is taken through
2988 * the 'struct file' in finish_open().
2994 *opened
|= FILE_CREATED
;
2995 error
= security_path_mknod(&nd
->path
, dentry
, mode
, 0);
2998 error
= vfs_create(dir
->d_inode
, dentry
, mode
,
2999 nd
->flags
& LOOKUP_EXCL
);
3004 path
->dentry
= dentry
;
3005 path
->mnt
= nd
->path
.mnt
;
3014 * Handle the last step of open()
3016 static int do_last(struct nameidata
*nd
,
3017 struct file
*file
, const struct open_flags
*op
,
3020 struct dentry
*dir
= nd
->path
.dentry
;
3021 int open_flag
= op
->open_flag
;
3022 bool will_truncate
= (open_flag
& O_TRUNC
) != 0;
3023 bool got_write
= false;
3024 int acc_mode
= op
->acc_mode
;
3026 struct inode
*inode
;
3027 struct path save_parent
= { .dentry
= NULL
, .mnt
= NULL
};
3029 bool retried
= false;
3032 nd
->flags
&= ~LOOKUP_PARENT
;
3033 nd
->flags
|= op
->intent
;
3035 if (nd
->last_type
!= LAST_NORM
) {
3036 error
= handle_dots(nd
, nd
->last_type
);
3037 if (unlikely(error
))
3042 if (!(open_flag
& O_CREAT
)) {
3043 if (nd
->last
.name
[nd
->last
.len
])
3044 nd
->flags
|= LOOKUP_FOLLOW
| LOOKUP_DIRECTORY
;
3045 /* we _can_ be in RCU mode here */
3046 error
= lookup_fast(nd
, &path
, &inode
, &seq
);
3053 BUG_ON(nd
->inode
!= dir
->d_inode
);
3055 /* create side of things */
3057 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3058 * has been cleared when we got to the last component we are
3061 error
= complete_walk(nd
);
3065 audit_inode(nd
->name
, dir
, LOOKUP_PARENT
);
3066 /* trailing slashes? */
3067 if (unlikely(nd
->last
.name
[nd
->last
.len
]))
3072 if (op
->open_flag
& (O_CREAT
| O_TRUNC
| O_WRONLY
| O_RDWR
)) {
3073 error
= mnt_want_write(nd
->path
.mnt
);
3077 * do _not_ fail yet - we might not need that or fail with
3078 * a different error; let lookup_open() decide; we'll be
3079 * dropping this one anyway.
3082 mutex_lock(&dir
->d_inode
->i_mutex
);
3083 error
= lookup_open(nd
, &path
, file
, op
, got_write
, opened
);
3084 mutex_unlock(&dir
->d_inode
->i_mutex
);
3090 if ((*opened
& FILE_CREATED
) ||
3091 !S_ISREG(file_inode(file
)->i_mode
))
3092 will_truncate
= false;
3094 audit_inode(nd
->name
, file
->f_path
.dentry
, 0);
3098 if (*opened
& FILE_CREATED
) {
3099 /* Don't check for write permission, don't truncate */
3100 open_flag
&= ~O_TRUNC
;
3101 will_truncate
= false;
3102 acc_mode
= MAY_OPEN
;
3103 path_to_nameidata(&path
, nd
);
3104 goto finish_open_created
;
3108 * create/update audit record if it already exists.
3110 if (d_is_positive(path
.dentry
))
3111 audit_inode(nd
->name
, path
.dentry
, 0);
3114 * If atomic_open() acquired write access it is dropped now due to
3115 * possible mount and symlink following (this might be optimized away if
3119 mnt_drop_write(nd
->path
.mnt
);
3123 if (unlikely((open_flag
& (O_EXCL
| O_CREAT
)) == (O_EXCL
| O_CREAT
))) {
3124 path_to_nameidata(&path
, nd
);
3128 error
= follow_managed(&path
, nd
);
3129 if (unlikely(error
< 0))
3132 BUG_ON(nd
->flags
& LOOKUP_RCU
);
3133 seq
= 0; /* out of RCU mode, so the value doesn't matter */
3134 if (unlikely(d_is_negative(path
.dentry
))) {
3135 path_to_nameidata(&path
, nd
);
3138 inode
= d_backing_inode(path
.dentry
);
3142 error
= should_follow_link(nd
, &path
, nd
->flags
& LOOKUP_FOLLOW
,
3144 if (unlikely(error
))
3147 if ((nd
->flags
& LOOKUP_RCU
) || nd
->path
.mnt
!= path
.mnt
) {
3148 path_to_nameidata(&path
, nd
);
3150 save_parent
.dentry
= nd
->path
.dentry
;
3151 save_parent
.mnt
= mntget(path
.mnt
);
3152 nd
->path
.dentry
= path
.dentry
;
3157 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3159 error
= complete_walk(nd
);
3161 path_put(&save_parent
);
3164 audit_inode(nd
->name
, nd
->path
.dentry
, 0);
3165 if (unlikely(d_is_symlink(nd
->path
.dentry
)) && !(open_flag
& O_PATH
)) {
3170 if ((open_flag
& O_CREAT
) && d_is_dir(nd
->path
.dentry
))
3173 if ((nd
->flags
& LOOKUP_DIRECTORY
) && !d_can_lookup(nd
->path
.dentry
))
3175 if (!d_is_reg(nd
->path
.dentry
))
3176 will_truncate
= false;
3178 if (will_truncate
) {
3179 error
= mnt_want_write(nd
->path
.mnt
);
3184 finish_open_created
:
3185 error
= may_open(&nd
->path
, acc_mode
, open_flag
);
3189 BUG_ON(*opened
& FILE_OPENED
); /* once it's opened, it's opened */
3190 error
= vfs_open(&nd
->path
, file
, current_cred());
3192 *opened
|= FILE_OPENED
;
3194 if (error
== -EOPENSTALE
)
3199 error
= open_check_o_direct(file
);
3202 error
= ima_file_check(file
, op
->acc_mode
, *opened
);
3206 if (will_truncate
) {
3207 error
= handle_truncate(file
);
3212 if (unlikely(error
> 0)) {
3217 mnt_drop_write(nd
->path
.mnt
);
3218 path_put(&save_parent
);
3226 /* If no saved parent or already retried then can't retry */
3227 if (!save_parent
.dentry
|| retried
)
3230 BUG_ON(save_parent
.dentry
!= dir
);
3231 path_put(&nd
->path
);
3232 nd
->path
= save_parent
;
3233 nd
->inode
= dir
->d_inode
;
3234 save_parent
.mnt
= NULL
;
3235 save_parent
.dentry
= NULL
;
3237 mnt_drop_write(nd
->path
.mnt
);
3244 static int do_tmpfile(struct nameidata
*nd
, unsigned flags
,
3245 const struct open_flags
*op
,
3246 struct file
*file
, int *opened
)
3248 static const struct qstr name
= QSTR_INIT("/", 1);
3249 struct dentry
*child
;
3252 int error
= path_lookupat(nd
, flags
| LOOKUP_DIRECTORY
, &path
);
3253 if (unlikely(error
))
3255 error
= mnt_want_write(path
.mnt
);
3256 if (unlikely(error
))
3258 dir
= path
.dentry
->d_inode
;
3259 /* we want directory to be writable */
3260 error
= inode_permission(dir
, MAY_WRITE
| MAY_EXEC
);
3263 if (!dir
->i_op
->tmpfile
) {
3264 error
= -EOPNOTSUPP
;
3267 child
= d_alloc(path
.dentry
, &name
);
3268 if (unlikely(!child
)) {
3273 path
.dentry
= child
;
3274 error
= dir
->i_op
->tmpfile(dir
, child
, op
->mode
);
3277 audit_inode(nd
->name
, child
, 0);
3278 /* Don't check for other permissions, the inode was just created */
3279 error
= may_open(&path
, MAY_OPEN
, op
->open_flag
);
3282 file
->f_path
.mnt
= path
.mnt
;
3283 error
= finish_open(file
, child
, NULL
, opened
);
3286 error
= open_check_o_direct(file
);
3289 } else if (!(op
->open_flag
& O_EXCL
)) {
3290 struct inode
*inode
= file_inode(file
);
3291 spin_lock(&inode
->i_lock
);
3292 inode
->i_state
|= I_LINKABLE
;
3293 spin_unlock(&inode
->i_lock
);
3296 mnt_drop_write(path
.mnt
);
3302 static struct file
*path_openat(struct nameidata
*nd
,
3303 const struct open_flags
*op
, unsigned flags
)
3310 file
= get_empty_filp();
3314 file
->f_flags
= op
->open_flag
;
3316 if (unlikely(file
->f_flags
& __O_TMPFILE
)) {
3317 error
= do_tmpfile(nd
, flags
, op
, file
, &opened
);
3321 s
= path_init(nd
, flags
);
3326 while (!(error
= link_path_walk(s
, nd
)) &&
3327 (error
= do_last(nd
, file
, op
, &opened
)) > 0) {
3328 nd
->flags
&= ~(LOOKUP_OPEN
|LOOKUP_CREATE
|LOOKUP_EXCL
);
3329 s
= trailing_symlink(nd
);
3337 if (!(opened
& FILE_OPENED
)) {
3341 if (unlikely(error
)) {
3342 if (error
== -EOPENSTALE
) {
3343 if (flags
& LOOKUP_RCU
)
3348 file
= ERR_PTR(error
);
3353 struct file
*do_filp_open(int dfd
, struct filename
*pathname
,
3354 const struct open_flags
*op
)
3356 struct nameidata nd
;
3357 int flags
= op
->lookup_flags
;
3360 set_nameidata(&nd
, dfd
, pathname
);
3361 filp
= path_openat(&nd
, op
, flags
| LOOKUP_RCU
);
3362 if (unlikely(filp
== ERR_PTR(-ECHILD
)))
3363 filp
= path_openat(&nd
, op
, flags
);
3364 if (unlikely(filp
== ERR_PTR(-ESTALE
)))
3365 filp
= path_openat(&nd
, op
, flags
| LOOKUP_REVAL
);
3366 restore_nameidata();
3370 struct file
*do_file_open_root(struct dentry
*dentry
, struct vfsmount
*mnt
,
3371 const char *name
, const struct open_flags
*op
)
3373 struct nameidata nd
;
3375 struct filename
*filename
;
3376 int flags
= op
->lookup_flags
| LOOKUP_ROOT
;
3379 nd
.root
.dentry
= dentry
;
3381 if (d_is_symlink(dentry
) && op
->intent
& LOOKUP_OPEN
)
3382 return ERR_PTR(-ELOOP
);
3384 filename
= getname_kernel(name
);
3385 if (IS_ERR(filename
))
3386 return ERR_CAST(filename
);
3388 set_nameidata(&nd
, -1, filename
);
3389 file
= path_openat(&nd
, op
, flags
| LOOKUP_RCU
);
3390 if (unlikely(file
== ERR_PTR(-ECHILD
)))
3391 file
= path_openat(&nd
, op
, flags
);
3392 if (unlikely(file
== ERR_PTR(-ESTALE
)))
3393 file
= path_openat(&nd
, op
, flags
| LOOKUP_REVAL
);
3394 restore_nameidata();
3399 static struct dentry
*filename_create(int dfd
, struct filename
*name
,
3400 struct path
*path
, unsigned int lookup_flags
)
3402 struct dentry
*dentry
= ERR_PTR(-EEXIST
);
3407 bool is_dir
= (lookup_flags
& LOOKUP_DIRECTORY
);
3410 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3411 * other flags passed in are ignored!
3413 lookup_flags
&= LOOKUP_REVAL
;
3415 name
= filename_parentat(dfd
, name
, lookup_flags
, path
, &last
, &type
);
3417 return ERR_CAST(name
);
3420 * Yucky last component or no last component at all?
3421 * (foo/., foo/.., /////)
3423 if (unlikely(type
!= LAST_NORM
))
3426 /* don't fail immediately if it's r/o, at least try to report other errors */
3427 err2
= mnt_want_write(path
->mnt
);
3429 * Do the final lookup.
3431 lookup_flags
|= LOOKUP_CREATE
| LOOKUP_EXCL
;
3432 mutex_lock_nested(&path
->dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3433 dentry
= __lookup_hash(&last
, path
->dentry
, lookup_flags
);
3438 if (d_is_positive(dentry
))
3442 * Special case - lookup gave negative, but... we had foo/bar/
3443 * From the vfs_mknod() POV we just have a negative dentry -
3444 * all is fine. Let's be bastards - you had / on the end, you've
3445 * been asking for (non-existent) directory. -ENOENT for you.
3447 if (unlikely(!is_dir
&& last
.name
[last
.len
])) {
3451 if (unlikely(err2
)) {
3459 dentry
= ERR_PTR(error
);
3461 mutex_unlock(&path
->dentry
->d_inode
->i_mutex
);
3463 mnt_drop_write(path
->mnt
);
3470 struct dentry
*kern_path_create(int dfd
, const char *pathname
,
3471 struct path
*path
, unsigned int lookup_flags
)
3473 return filename_create(dfd
, getname_kernel(pathname
),
3474 path
, lookup_flags
);
3476 EXPORT_SYMBOL(kern_path_create
);
3478 void done_path_create(struct path
*path
, struct dentry
*dentry
)
3481 mutex_unlock(&path
->dentry
->d_inode
->i_mutex
);
3482 mnt_drop_write(path
->mnt
);
3485 EXPORT_SYMBOL(done_path_create
);
3487 inline struct dentry
*user_path_create(int dfd
, const char __user
*pathname
,
3488 struct path
*path
, unsigned int lookup_flags
)
3490 return filename_create(dfd
, getname(pathname
), path
, lookup_flags
);
3492 EXPORT_SYMBOL(user_path_create
);
3494 int vfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
3496 int error
= may_create(dir
, dentry
);
3501 if ((S_ISCHR(mode
) || S_ISBLK(mode
)) && !capable(CAP_MKNOD
))
3504 if (!dir
->i_op
->mknod
)
3507 error
= devcgroup_inode_mknod(mode
, dev
);
3511 error
= security_inode_mknod(dir
, dentry
, mode
, dev
);
3515 error
= dir
->i_op
->mknod(dir
, dentry
, mode
, dev
);
3517 fsnotify_create(dir
, dentry
);
3520 EXPORT_SYMBOL(vfs_mknod
);
3522 static int may_mknod(umode_t mode
)
3524 switch (mode
& S_IFMT
) {
3530 case 0: /* zero mode translates to S_IFREG */
3539 SYSCALL_DEFINE4(mknodat
, int, dfd
, const char __user
*, filename
, umode_t
, mode
,
3542 struct dentry
*dentry
;
3545 unsigned int lookup_flags
= 0;
3547 error
= may_mknod(mode
);
3551 dentry
= user_path_create(dfd
, filename
, &path
, lookup_flags
);
3553 return PTR_ERR(dentry
);
3555 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3556 mode
&= ~current_umask();
3557 error
= security_path_mknod(&path
, dentry
, mode
, dev
);
3560 switch (mode
& S_IFMT
) {
3561 case 0: case S_IFREG
:
3562 error
= vfs_create(path
.dentry
->d_inode
,dentry
,mode
,true);
3564 case S_IFCHR
: case S_IFBLK
:
3565 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,
3566 new_decode_dev(dev
));
3568 case S_IFIFO
: case S_IFSOCK
:
3569 error
= vfs_mknod(path
.dentry
->d_inode
,dentry
,mode
,0);
3573 done_path_create(&path
, dentry
);
3574 if (retry_estale(error
, lookup_flags
)) {
3575 lookup_flags
|= LOOKUP_REVAL
;
3581 SYSCALL_DEFINE3(mknod
, const char __user
*, filename
, umode_t
, mode
, unsigned, dev
)
3583 return sys_mknodat(AT_FDCWD
, filename
, mode
, dev
);
3586 int vfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
3588 int error
= may_create(dir
, dentry
);
3589 unsigned max_links
= dir
->i_sb
->s_max_links
;
3594 if (!dir
->i_op
->mkdir
)
3597 mode
&= (S_IRWXUGO
|S_ISVTX
);
3598 error
= security_inode_mkdir(dir
, dentry
, mode
);
3602 if (max_links
&& dir
->i_nlink
>= max_links
)
3605 error
= dir
->i_op
->mkdir(dir
, dentry
, mode
);
3607 fsnotify_mkdir(dir
, dentry
);
3610 EXPORT_SYMBOL(vfs_mkdir
);
3612 SYSCALL_DEFINE3(mkdirat
, int, dfd
, const char __user
*, pathname
, umode_t
, mode
)
3614 struct dentry
*dentry
;
3617 unsigned int lookup_flags
= LOOKUP_DIRECTORY
;
3620 dentry
= user_path_create(dfd
, pathname
, &path
, lookup_flags
);
3622 return PTR_ERR(dentry
);
3624 if (!IS_POSIXACL(path
.dentry
->d_inode
))
3625 mode
&= ~current_umask();
3626 error
= security_path_mkdir(&path
, dentry
, mode
);
3628 error
= vfs_mkdir(path
.dentry
->d_inode
, dentry
, mode
);
3629 done_path_create(&path
, dentry
);
3630 if (retry_estale(error
, lookup_flags
)) {
3631 lookup_flags
|= LOOKUP_REVAL
;
3637 SYSCALL_DEFINE2(mkdir
, const char __user
*, pathname
, umode_t
, mode
)
3639 return sys_mkdirat(AT_FDCWD
, pathname
, mode
);
3643 * The dentry_unhash() helper will try to drop the dentry early: we
3644 * should have a usage count of 1 if we're the only user of this
3645 * dentry, and if that is true (possibly after pruning the dcache),
3646 * then we drop the dentry now.
3648 * A low-level filesystem can, if it choses, legally
3651 * if (!d_unhashed(dentry))
3654 * if it cannot handle the case of removing a directory
3655 * that is still in use by something else..
3657 void dentry_unhash(struct dentry
*dentry
)
3659 shrink_dcache_parent(dentry
);
3660 spin_lock(&dentry
->d_lock
);
3661 if (dentry
->d_lockref
.count
== 1)
3663 spin_unlock(&dentry
->d_lock
);
3665 EXPORT_SYMBOL(dentry_unhash
);
3667 int vfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3669 int error
= may_delete(dir
, dentry
, 1);
3674 if (!dir
->i_op
->rmdir
)
3678 mutex_lock(&dentry
->d_inode
->i_mutex
);
3681 if (is_local_mountpoint(dentry
))
3684 error
= security_inode_rmdir(dir
, dentry
);
3688 shrink_dcache_parent(dentry
);
3689 error
= dir
->i_op
->rmdir(dir
, dentry
);
3693 dentry
->d_inode
->i_flags
|= S_DEAD
;
3695 detach_mounts(dentry
);
3698 mutex_unlock(&dentry
->d_inode
->i_mutex
);
3704 EXPORT_SYMBOL(vfs_rmdir
);
3706 static long do_rmdir(int dfd
, const char __user
*pathname
)
3709 struct filename
*name
;
3710 struct dentry
*dentry
;
3714 unsigned int lookup_flags
= 0;
3716 name
= user_path_parent(dfd
, pathname
,
3717 &path
, &last
, &type
, lookup_flags
);
3719 return PTR_ERR(name
);
3733 error
= mnt_want_write(path
.mnt
);
3737 mutex_lock_nested(&path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3738 dentry
= __lookup_hash(&last
, path
.dentry
, lookup_flags
);
3739 error
= PTR_ERR(dentry
);
3742 if (!dentry
->d_inode
) {
3746 error
= security_path_rmdir(&path
, dentry
);
3749 error
= vfs_rmdir(path
.dentry
->d_inode
, dentry
);
3753 mutex_unlock(&path
.dentry
->d_inode
->i_mutex
);
3754 mnt_drop_write(path
.mnt
);
3758 if (retry_estale(error
, lookup_flags
)) {
3759 lookup_flags
|= LOOKUP_REVAL
;
3765 SYSCALL_DEFINE1(rmdir
, const char __user
*, pathname
)
3767 return do_rmdir(AT_FDCWD
, pathname
);
3771 * vfs_unlink - unlink a filesystem object
3772 * @dir: parent directory
3774 * @delegated_inode: returns victim inode, if the inode is delegated.
3776 * The caller must hold dir->i_mutex.
3778 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3779 * return a reference to the inode in delegated_inode. The caller
3780 * should then break the delegation on that inode and retry. Because
3781 * breaking a delegation may take a long time, the caller should drop
3782 * dir->i_mutex before doing so.
3784 * Alternatively, a caller may pass NULL for delegated_inode. This may
3785 * be appropriate for callers that expect the underlying filesystem not
3786 * to be NFS exported.
3788 int vfs_unlink(struct inode
*dir
, struct dentry
*dentry
, struct inode
**delegated_inode
)
3790 struct inode
*target
= dentry
->d_inode
;
3791 int error
= may_delete(dir
, dentry
, 0);
3796 if (!dir
->i_op
->unlink
)
3799 mutex_lock(&target
->i_mutex
);
3800 if (is_local_mountpoint(dentry
))
3803 error
= security_inode_unlink(dir
, dentry
);
3805 error
= try_break_deleg(target
, delegated_inode
);
3808 error
= dir
->i_op
->unlink(dir
, dentry
);
3811 detach_mounts(dentry
);
3816 mutex_unlock(&target
->i_mutex
);
3818 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3819 if (!error
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)) {
3820 fsnotify_link_count(target
);
3826 EXPORT_SYMBOL(vfs_unlink
);
3829 * Make sure that the actual truncation of the file will occur outside its
3830 * directory's i_mutex. Truncate can take a long time if there is a lot of
3831 * writeout happening, and we don't want to prevent access to the directory
3832 * while waiting on the I/O.
3834 static long do_unlinkat(int dfd
, const char __user
*pathname
)
3837 struct filename
*name
;
3838 struct dentry
*dentry
;
3842 struct inode
*inode
= NULL
;
3843 struct inode
*delegated_inode
= NULL
;
3844 unsigned int lookup_flags
= 0;
3846 name
= user_path_parent(dfd
, pathname
,
3847 &path
, &last
, &type
, lookup_flags
);
3849 return PTR_ERR(name
);
3852 if (type
!= LAST_NORM
)
3855 error
= mnt_want_write(path
.mnt
);
3859 mutex_lock_nested(&path
.dentry
->d_inode
->i_mutex
, I_MUTEX_PARENT
);
3860 dentry
= __lookup_hash(&last
, path
.dentry
, lookup_flags
);
3861 error
= PTR_ERR(dentry
);
3862 if (!IS_ERR(dentry
)) {
3863 /* Why not before? Because we want correct error value */
3864 if (last
.name
[last
.len
])
3866 inode
= dentry
->d_inode
;
3867 if (d_is_negative(dentry
))
3870 error
= security_path_unlink(&path
, dentry
);
3873 error
= vfs_unlink(path
.dentry
->d_inode
, dentry
, &delegated_inode
);
3877 mutex_unlock(&path
.dentry
->d_inode
->i_mutex
);
3879 iput(inode
); /* truncate the inode here */
3881 if (delegated_inode
) {
3882 error
= break_deleg_wait(&delegated_inode
);
3886 mnt_drop_write(path
.mnt
);
3890 if (retry_estale(error
, lookup_flags
)) {
3891 lookup_flags
|= LOOKUP_REVAL
;
3898 if (d_is_negative(dentry
))
3900 else if (d_is_dir(dentry
))
3907 SYSCALL_DEFINE3(unlinkat
, int, dfd
, const char __user
*, pathname
, int, flag
)
3909 if ((flag
& ~AT_REMOVEDIR
) != 0)
3912 if (flag
& AT_REMOVEDIR
)
3913 return do_rmdir(dfd
, pathname
);
3915 return do_unlinkat(dfd
, pathname
);
3918 SYSCALL_DEFINE1(unlink
, const char __user
*, pathname
)
3920 return do_unlinkat(AT_FDCWD
, pathname
);
3923 int vfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *oldname
)
3925 int error
= may_create(dir
, dentry
);
3930 if (!dir
->i_op
->symlink
)
3933 error
= security_inode_symlink(dir
, dentry
, oldname
);
3937 error
= dir
->i_op
->symlink(dir
, dentry
, oldname
);
3939 fsnotify_create(dir
, dentry
);
3942 EXPORT_SYMBOL(vfs_symlink
);
3944 SYSCALL_DEFINE3(symlinkat
, const char __user
*, oldname
,
3945 int, newdfd
, const char __user
*, newname
)
3948 struct filename
*from
;
3949 struct dentry
*dentry
;
3951 unsigned int lookup_flags
= 0;
3953 from
= getname(oldname
);
3955 return PTR_ERR(from
);
3957 dentry
= user_path_create(newdfd
, newname
, &path
, lookup_flags
);
3958 error
= PTR_ERR(dentry
);
3962 error
= security_path_symlink(&path
, dentry
, from
->name
);
3964 error
= vfs_symlink(path
.dentry
->d_inode
, dentry
, from
->name
);
3965 done_path_create(&path
, dentry
);
3966 if (retry_estale(error
, lookup_flags
)) {
3967 lookup_flags
|= LOOKUP_REVAL
;
3975 SYSCALL_DEFINE2(symlink
, const char __user
*, oldname
, const char __user
*, newname
)
3977 return sys_symlinkat(oldname
, AT_FDCWD
, newname
);
3981 * vfs_link - create a new link
3982 * @old_dentry: object to be linked
3984 * @new_dentry: where to create the new link
3985 * @delegated_inode: returns inode needing a delegation break
3987 * The caller must hold dir->i_mutex
3989 * If vfs_link discovers a delegation on the to-be-linked file in need
3990 * of breaking, it will return -EWOULDBLOCK and return a reference to the
3991 * inode in delegated_inode. The caller should then break the delegation
3992 * and retry. Because breaking a delegation may take a long time, the
3993 * caller should drop the i_mutex before doing so.
3995 * Alternatively, a caller may pass NULL for delegated_inode. This may
3996 * be appropriate for callers that expect the underlying filesystem not
3997 * to be NFS exported.
3999 int vfs_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*new_dentry
, struct inode
**delegated_inode
)
4001 struct inode
*inode
= old_dentry
->d_inode
;
4002 unsigned max_links
= dir
->i_sb
->s_max_links
;
4008 error
= may_create(dir
, new_dentry
);
4012 if (dir
->i_sb
!= inode
->i_sb
)
4016 * A link to an append-only or immutable file cannot be created.
4018 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
4020 if (!dir
->i_op
->link
)
4022 if (S_ISDIR(inode
->i_mode
))
4025 error
= security_inode_link(old_dentry
, dir
, new_dentry
);
4029 mutex_lock(&inode
->i_mutex
);
4030 /* Make sure we don't allow creating hardlink to an unlinked file */
4031 if (inode
->i_nlink
== 0 && !(inode
->i_state
& I_LINKABLE
))
4033 else if (max_links
&& inode
->i_nlink
>= max_links
)
4036 error
= try_break_deleg(inode
, delegated_inode
);
4038 error
= dir
->i_op
->link(old_dentry
, dir
, new_dentry
);
4041 if (!error
&& (inode
->i_state
& I_LINKABLE
)) {
4042 spin_lock(&inode
->i_lock
);
4043 inode
->i_state
&= ~I_LINKABLE
;
4044 spin_unlock(&inode
->i_lock
);
4046 mutex_unlock(&inode
->i_mutex
);
4048 fsnotify_link(dir
, inode
, new_dentry
);
4051 EXPORT_SYMBOL(vfs_link
);
4054 * Hardlinks are often used in delicate situations. We avoid
4055 * security-related surprises by not following symlinks on the
4058 * We don't follow them on the oldname either to be compatible
4059 * with linux 2.0, and to avoid hard-linking to directories
4060 * and other special files. --ADM
4062 SYSCALL_DEFINE5(linkat
, int, olddfd
, const char __user
*, oldname
,
4063 int, newdfd
, const char __user
*, newname
, int, flags
)
4065 struct dentry
*new_dentry
;
4066 struct path old_path
, new_path
;
4067 struct inode
*delegated_inode
= NULL
;
4071 if ((flags
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
4074 * To use null names we require CAP_DAC_READ_SEARCH
4075 * This ensures that not everyone will be able to create
4076 * handlink using the passed filedescriptor.
4078 if (flags
& AT_EMPTY_PATH
) {
4079 if (!capable(CAP_DAC_READ_SEARCH
))
4084 if (flags
& AT_SYMLINK_FOLLOW
)
4085 how
|= LOOKUP_FOLLOW
;
4087 error
= user_path_at(olddfd
, oldname
, how
, &old_path
);
4091 new_dentry
= user_path_create(newdfd
, newname
, &new_path
,
4092 (how
& LOOKUP_REVAL
));
4093 error
= PTR_ERR(new_dentry
);
4094 if (IS_ERR(new_dentry
))
4098 if (old_path
.mnt
!= new_path
.mnt
)
4100 error
= may_linkat(&old_path
);
4101 if (unlikely(error
))
4103 error
= security_path_link(old_path
.dentry
, &new_path
, new_dentry
);
4106 error
= vfs_link(old_path
.dentry
, new_path
.dentry
->d_inode
, new_dentry
, &delegated_inode
);
4108 done_path_create(&new_path
, new_dentry
);
4109 if (delegated_inode
) {
4110 error
= break_deleg_wait(&delegated_inode
);
4112 path_put(&old_path
);
4116 if (retry_estale(error
, how
)) {
4117 path_put(&old_path
);
4118 how
|= LOOKUP_REVAL
;
4122 path_put(&old_path
);
4127 SYSCALL_DEFINE2(link
, const char __user
*, oldname
, const char __user
*, newname
)
4129 return sys_linkat(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
4133 * vfs_rename - rename a filesystem object
4134 * @old_dir: parent of source
4135 * @old_dentry: source
4136 * @new_dir: parent of destination
4137 * @new_dentry: destination
4138 * @delegated_inode: returns an inode needing a delegation break
4139 * @flags: rename flags
4141 * The caller must hold multiple mutexes--see lock_rename()).
4143 * If vfs_rename discovers a delegation in need of breaking at either
4144 * the source or destination, it will return -EWOULDBLOCK and return a
4145 * reference to the inode in delegated_inode. The caller should then
4146 * break the delegation and retry. Because breaking a delegation may
4147 * take a long time, the caller should drop all locks before doing
4150 * Alternatively, a caller may pass NULL for delegated_inode. This may
4151 * be appropriate for callers that expect the underlying filesystem not
4152 * to be NFS exported.
4154 * The worst of all namespace operations - renaming directory. "Perverted"
4155 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4157 * a) we can get into loop creation.
4158 * b) race potential - two innocent renames can create a loop together.
4159 * That's where 4.4 screws up. Current fix: serialization on
4160 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4162 * c) we have to lock _four_ objects - parents and victim (if it exists),
4163 * and source (if it is not a directory).
4164 * And that - after we got ->i_mutex on parents (until then we don't know
4165 * whether the target exists). Solution: try to be smart with locking
4166 * order for inodes. We rely on the fact that tree topology may change
4167 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4168 * move will be locked. Thus we can rank directories by the tree
4169 * (ancestors first) and rank all non-directories after them.
4170 * That works since everybody except rename does "lock parent, lookup,
4171 * lock child" and rename is under ->s_vfs_rename_mutex.
4172 * HOWEVER, it relies on the assumption that any object with ->lookup()
4173 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4174 * we'd better make sure that there's no link(2) for them.
4175 * d) conversion from fhandle to dentry may come in the wrong moment - when
4176 * we are removing the target. Solution: we will have to grab ->i_mutex
4177 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4178 * ->i_mutex on parents, which works but leads to some truly excessive
4181 int vfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
4182 struct inode
*new_dir
, struct dentry
*new_dentry
,
4183 struct inode
**delegated_inode
, unsigned int flags
)
4186 bool is_dir
= d_is_dir(old_dentry
);
4187 struct inode
*source
= old_dentry
->d_inode
;
4188 struct inode
*target
= new_dentry
->d_inode
;
4189 bool new_is_dir
= false;
4190 unsigned max_links
= new_dir
->i_sb
->s_max_links
;
4191 struct name_snapshot old_name
;
4194 * Check source == target.
4195 * On overlayfs need to look at underlying inodes.
4197 if (vfs_select_inode(old_dentry
, 0) == vfs_select_inode(new_dentry
, 0))
4200 error
= may_delete(old_dir
, old_dentry
, is_dir
);
4205 error
= may_create(new_dir
, new_dentry
);
4207 new_is_dir
= d_is_dir(new_dentry
);
4209 if (!(flags
& RENAME_EXCHANGE
))
4210 error
= may_delete(new_dir
, new_dentry
, is_dir
);
4212 error
= may_delete(new_dir
, new_dentry
, new_is_dir
);
4217 if (!old_dir
->i_op
->rename
&& !old_dir
->i_op
->rename2
)
4220 if (flags
&& !old_dir
->i_op
->rename2
)
4224 * If we are going to change the parent - check write permissions,
4225 * we'll need to flip '..'.
4227 if (new_dir
!= old_dir
) {
4229 error
= inode_permission(source
, MAY_WRITE
);
4233 if ((flags
& RENAME_EXCHANGE
) && new_is_dir
) {
4234 error
= inode_permission(target
, MAY_WRITE
);
4240 error
= security_inode_rename(old_dir
, old_dentry
, new_dir
, new_dentry
,
4245 take_dentry_name_snapshot(&old_name
, old_dentry
);
4247 if (!is_dir
|| (flags
& RENAME_EXCHANGE
))
4248 lock_two_nondirectories(source
, target
);
4250 mutex_lock(&target
->i_mutex
);
4253 if (is_local_mountpoint(old_dentry
) || is_local_mountpoint(new_dentry
))
4256 if (max_links
&& new_dir
!= old_dir
) {
4258 if (is_dir
&& !new_is_dir
&& new_dir
->i_nlink
>= max_links
)
4260 if ((flags
& RENAME_EXCHANGE
) && !is_dir
&& new_is_dir
&&
4261 old_dir
->i_nlink
>= max_links
)
4264 if (is_dir
&& !(flags
& RENAME_EXCHANGE
) && target
)
4265 shrink_dcache_parent(new_dentry
);
4267 error
= try_break_deleg(source
, delegated_inode
);
4271 if (target
&& !new_is_dir
) {
4272 error
= try_break_deleg(target
, delegated_inode
);
4276 if (!old_dir
->i_op
->rename2
) {
4277 error
= old_dir
->i_op
->rename(old_dir
, old_dentry
,
4278 new_dir
, new_dentry
);
4280 WARN_ON(old_dir
->i_op
->rename
!= NULL
);
4281 error
= old_dir
->i_op
->rename2(old_dir
, old_dentry
,
4282 new_dir
, new_dentry
, flags
);
4287 if (!(flags
& RENAME_EXCHANGE
) && target
) {
4289 target
->i_flags
|= S_DEAD
;
4290 dont_mount(new_dentry
);
4291 detach_mounts(new_dentry
);
4293 if (!(old_dir
->i_sb
->s_type
->fs_flags
& FS_RENAME_DOES_D_MOVE
)) {
4294 if (!(flags
& RENAME_EXCHANGE
))
4295 d_move(old_dentry
, new_dentry
);
4297 d_exchange(old_dentry
, new_dentry
);
4300 if (!is_dir
|| (flags
& RENAME_EXCHANGE
))
4301 unlock_two_nondirectories(source
, target
);
4303 mutex_unlock(&target
->i_mutex
);
4306 fsnotify_move(old_dir
, new_dir
, old_name
.name
, is_dir
,
4307 !(flags
& RENAME_EXCHANGE
) ? target
: NULL
, old_dentry
);
4308 if (flags
& RENAME_EXCHANGE
) {
4309 fsnotify_move(new_dir
, old_dir
, old_dentry
->d_name
.name
,
4310 new_is_dir
, NULL
, new_dentry
);
4313 release_dentry_name_snapshot(&old_name
);
4317 EXPORT_SYMBOL(vfs_rename
);
4319 SYSCALL_DEFINE5(renameat2
, int, olddfd
, const char __user
*, oldname
,
4320 int, newdfd
, const char __user
*, newname
, unsigned int, flags
)
4322 struct dentry
*old_dentry
, *new_dentry
;
4323 struct dentry
*trap
;
4324 struct path old_path
, new_path
;
4325 struct qstr old_last
, new_last
;
4326 int old_type
, new_type
;
4327 struct inode
*delegated_inode
= NULL
;
4328 struct filename
*from
;
4329 struct filename
*to
;
4330 unsigned int lookup_flags
= 0, target_flags
= LOOKUP_RENAME_TARGET
;
4331 bool should_retry
= false;
4334 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
4337 if ((flags
& (RENAME_NOREPLACE
| RENAME_WHITEOUT
)) &&
4338 (flags
& RENAME_EXCHANGE
))
4341 if ((flags
& RENAME_WHITEOUT
) && !capable(CAP_MKNOD
))
4344 if (flags
& RENAME_EXCHANGE
)
4348 from
= user_path_parent(olddfd
, oldname
,
4349 &old_path
, &old_last
, &old_type
, lookup_flags
);
4351 error
= PTR_ERR(from
);
4355 to
= user_path_parent(newdfd
, newname
,
4356 &new_path
, &new_last
, &new_type
, lookup_flags
);
4358 error
= PTR_ERR(to
);
4363 if (old_path
.mnt
!= new_path
.mnt
)
4367 if (old_type
!= LAST_NORM
)
4370 if (flags
& RENAME_NOREPLACE
)
4372 if (new_type
!= LAST_NORM
)
4375 error
= mnt_want_write(old_path
.mnt
);
4380 trap
= lock_rename(new_path
.dentry
, old_path
.dentry
);
4382 old_dentry
= __lookup_hash(&old_last
, old_path
.dentry
, lookup_flags
);
4383 error
= PTR_ERR(old_dentry
);
4384 if (IS_ERR(old_dentry
))
4386 /* source must exist */
4388 if (d_is_negative(old_dentry
))
4390 new_dentry
= __lookup_hash(&new_last
, new_path
.dentry
, lookup_flags
| target_flags
);
4391 error
= PTR_ERR(new_dentry
);
4392 if (IS_ERR(new_dentry
))
4395 if ((flags
& RENAME_NOREPLACE
) && d_is_positive(new_dentry
))
4397 if (flags
& RENAME_EXCHANGE
) {
4399 if (d_is_negative(new_dentry
))
4402 if (!d_is_dir(new_dentry
)) {
4404 if (new_last
.name
[new_last
.len
])
4408 /* unless the source is a directory trailing slashes give -ENOTDIR */
4409 if (!d_is_dir(old_dentry
)) {
4411 if (old_last
.name
[old_last
.len
])
4413 if (!(flags
& RENAME_EXCHANGE
) && new_last
.name
[new_last
.len
])
4416 /* source should not be ancestor of target */
4418 if (old_dentry
== trap
)
4420 /* target should not be an ancestor of source */
4421 if (!(flags
& RENAME_EXCHANGE
))
4423 if (new_dentry
== trap
)
4426 error
= security_path_rename(&old_path
, old_dentry
,
4427 &new_path
, new_dentry
, flags
);
4430 error
= vfs_rename(old_path
.dentry
->d_inode
, old_dentry
,
4431 new_path
.dentry
->d_inode
, new_dentry
,
4432 &delegated_inode
, flags
);
4438 unlock_rename(new_path
.dentry
, old_path
.dentry
);
4439 if (delegated_inode
) {
4440 error
= break_deleg_wait(&delegated_inode
);
4444 mnt_drop_write(old_path
.mnt
);
4446 if (retry_estale(error
, lookup_flags
))
4447 should_retry
= true;
4448 path_put(&new_path
);
4451 path_put(&old_path
);
4454 should_retry
= false;
4455 lookup_flags
|= LOOKUP_REVAL
;
4462 SYSCALL_DEFINE4(renameat
, int, olddfd
, const char __user
*, oldname
,
4463 int, newdfd
, const char __user
*, newname
)
4465 return sys_renameat2(olddfd
, oldname
, newdfd
, newname
, 0);
4468 SYSCALL_DEFINE2(rename
, const char __user
*, oldname
, const char __user
*, newname
)
4470 return sys_renameat2(AT_FDCWD
, oldname
, AT_FDCWD
, newname
, 0);
4473 int vfs_whiteout(struct inode
*dir
, struct dentry
*dentry
)
4475 int error
= may_create(dir
, dentry
);
4479 if (!dir
->i_op
->mknod
)
4482 return dir
->i_op
->mknod(dir
, dentry
,
4483 S_IFCHR
| WHITEOUT_MODE
, WHITEOUT_DEV
);
4485 EXPORT_SYMBOL(vfs_whiteout
);
4487 int readlink_copy(char __user
*buffer
, int buflen
, const char *link
)
4489 int len
= PTR_ERR(link
);
4494 if (len
> (unsigned) buflen
)
4496 if (copy_to_user(buffer
, link
, len
))
4501 EXPORT_SYMBOL(readlink_copy
);
4504 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4505 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
4506 * using) it for any given inode is up to filesystem.
4508 int generic_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4511 struct inode
*inode
= d_inode(dentry
);
4512 const char *link
= inode
->i_link
;
4516 link
= inode
->i_op
->follow_link(dentry
, &cookie
);
4518 return PTR_ERR(link
);
4520 res
= readlink_copy(buffer
, buflen
, link
);
4521 if (inode
->i_op
->put_link
)
4522 inode
->i_op
->put_link(inode
, cookie
);
4525 EXPORT_SYMBOL(generic_readlink
);
4527 /* get the link contents into pagecache */
4528 static char *page_getlink(struct dentry
* dentry
, struct page
**ppage
)
4532 struct address_space
*mapping
= dentry
->d_inode
->i_mapping
;
4533 page
= read_mapping_page(mapping
, 0, NULL
);
4538 nd_terminate_link(kaddr
, dentry
->d_inode
->i_size
, PAGE_SIZE
- 1);
4542 int page_readlink(struct dentry
*dentry
, char __user
*buffer
, int buflen
)
4544 struct page
*page
= NULL
;
4545 int res
= readlink_copy(buffer
, buflen
, page_getlink(dentry
, &page
));
4548 page_cache_release(page
);
4552 EXPORT_SYMBOL(page_readlink
);
4554 const char *page_follow_link_light(struct dentry
*dentry
, void **cookie
)
4556 struct page
*page
= NULL
;
4557 char *res
= page_getlink(dentry
, &page
);
4562 EXPORT_SYMBOL(page_follow_link_light
);
4564 void page_put_link(struct inode
*unused
, void *cookie
)
4566 struct page
*page
= cookie
;
4568 page_cache_release(page
);
4570 EXPORT_SYMBOL(page_put_link
);
4573 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4575 int __page_symlink(struct inode
*inode
, const char *symname
, int len
, int nofs
)
4577 struct address_space
*mapping
= inode
->i_mapping
;
4582 unsigned int flags
= AOP_FLAG_UNINTERRUPTIBLE
;
4584 flags
|= AOP_FLAG_NOFS
;
4587 err
= pagecache_write_begin(NULL
, mapping
, 0, len
-1,
4588 flags
, &page
, &fsdata
);
4592 kaddr
= kmap_atomic(page
);
4593 memcpy(kaddr
, symname
, len
-1);
4594 kunmap_atomic(kaddr
);
4596 err
= pagecache_write_end(NULL
, mapping
, 0, len
-1, len
-1,
4603 mark_inode_dirty(inode
);
4608 EXPORT_SYMBOL(__page_symlink
);
4610 int page_symlink(struct inode
*inode
, const char *symname
, int len
)
4612 return __page_symlink(inode
, symname
, len
,
4613 !mapping_gfp_constraint(inode
->i_mapping
, __GFP_FS
));
4615 EXPORT_SYMBOL(page_symlink
);
4617 const struct inode_operations page_symlink_inode_operations
= {
4618 .readlink
= generic_readlink
,
4619 .follow_link
= page_follow_link_light
,
4620 .put_link
= page_put_link
,
4622 EXPORT_SYMBOL(page_symlink_inode_operations
);